1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/kernel.h> 3 #include <linux/errno.h> 4 #include <linux/file.h> 5 #include <linux/slab.h> 6 #include <linux/net.h> 7 #include <linux/compat.h> 8 #include <net/compat.h> 9 #include <linux/io_uring.h> 10 11 #include <uapi/linux/io_uring.h> 12 13 #include "io_uring.h" 14 #include "kbuf.h" 15 #include "alloc_cache.h" 16 #include "net.h" 17 #include "notif.h" 18 #include "rsrc.h" 19 20 #if defined(CONFIG_NET) 21 struct io_shutdown { 22 struct file *file; 23 int how; 24 }; 25 26 struct io_accept { 27 struct file *file; 28 struct sockaddr __user *addr; 29 int __user *addr_len; 30 int flags; 31 u32 file_slot; 32 unsigned long nofile; 33 }; 34 35 struct io_socket { 36 struct file *file; 37 int domain; 38 int type; 39 int protocol; 40 int flags; 41 u32 file_slot; 42 unsigned long nofile; 43 }; 44 45 struct io_connect { 46 struct file *file; 47 struct sockaddr __user *addr; 48 int addr_len; 49 bool in_progress; 50 bool seen_econnaborted; 51 }; 52 53 struct io_sr_msg { 54 struct file *file; 55 union { 56 struct compat_msghdr __user *umsg_compat; 57 struct user_msghdr __user *umsg; 58 void __user *buf; 59 }; 60 unsigned len; 61 unsigned done_io; 62 unsigned msg_flags; 63 u16 flags; 64 /* initialised and used only by !msg send variants */ 65 u16 addr_len; 66 u16 buf_group; 67 void __user *addr; 68 /* used only for send zerocopy */ 69 struct io_kiocb *notif; 70 }; 71 72 static inline bool io_check_multishot(struct io_kiocb *req, 73 unsigned int issue_flags) 74 { 75 /* 76 * When ->locked_cq is set we only allow to post CQEs from the original 77 * task context. Usual request completions will be handled in other 78 * generic paths but multipoll may decide to post extra cqes. 79 */ 80 return !(issue_flags & IO_URING_F_IOWQ) || 81 !(issue_flags & IO_URING_F_MULTISHOT) || 82 !req->ctx->task_complete; 83 } 84 85 int io_shutdown_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) 86 { 87 struct io_shutdown *shutdown = io_kiocb_to_cmd(req, struct io_shutdown); 88 89 if (unlikely(sqe->off || sqe->addr || sqe->rw_flags || 90 sqe->buf_index || sqe->splice_fd_in)) 91 return -EINVAL; 92 93 shutdown->how = READ_ONCE(sqe->len); 94 req->flags |= REQ_F_FORCE_ASYNC; 95 return 0; 96 } 97 98 int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) 99 { 100 struct io_shutdown *shutdown = io_kiocb_to_cmd(req, struct io_shutdown); 101 struct socket *sock; 102 int ret; 103 104 WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK); 105 106 sock = sock_from_file(req->file); 107 if (unlikely(!sock)) 108 return -ENOTSOCK; 109 110 ret = __sys_shutdown_sock(sock, shutdown->how); 111 io_req_set_res(req, ret, 0); 112 return IOU_OK; 113 } 114 115 static bool io_net_retry(struct socket *sock, int flags) 116 { 117 if (!(flags & MSG_WAITALL)) 118 return false; 119 return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET; 120 } 121 122 static void io_netmsg_recycle(struct io_kiocb *req, unsigned int issue_flags) 123 { 124 struct io_async_msghdr *hdr = req->async_data; 125 126 if (!req_has_async_data(req) || issue_flags & IO_URING_F_UNLOCKED) 127 return; 128 129 /* Let normal cleanup path reap it if we fail adding to the cache */ 130 if (io_alloc_cache_put(&req->ctx->netmsg_cache, &hdr->cache)) { 131 req->async_data = NULL; 132 req->flags &= ~REQ_F_ASYNC_DATA; 133 } 134 } 135 136 static struct io_async_msghdr *io_msg_alloc_async(struct io_kiocb *req, 137 unsigned int issue_flags) 138 { 139 struct io_ring_ctx *ctx = req->ctx; 140 struct io_cache_entry *entry; 141 struct io_async_msghdr *hdr; 142 143 if (!(issue_flags & IO_URING_F_UNLOCKED)) { 144 entry = io_alloc_cache_get(&ctx->netmsg_cache); 145 if (entry) { 146 hdr = container_of(entry, struct io_async_msghdr, cache); 147 hdr->free_iov = NULL; 148 req->flags |= REQ_F_ASYNC_DATA; 149 req->async_data = hdr; 150 return hdr; 151 } 152 } 153 154 if (!io_alloc_async_data(req)) { 155 hdr = req->async_data; 156 hdr->free_iov = NULL; 157 return hdr; 158 } 159 return NULL; 160 } 161 162 static inline struct io_async_msghdr *io_msg_alloc_async_prep(struct io_kiocb *req) 163 { 164 /* ->prep_async is always called from the submission context */ 165 return io_msg_alloc_async(req, 0); 166 } 167 168 static int io_setup_async_msg(struct io_kiocb *req, 169 struct io_async_msghdr *kmsg, 170 unsigned int issue_flags) 171 { 172 struct io_async_msghdr *async_msg; 173 174 if (req_has_async_data(req)) 175 return -EAGAIN; 176 async_msg = io_msg_alloc_async(req, issue_flags); 177 if (!async_msg) { 178 kfree(kmsg->free_iov); 179 return -ENOMEM; 180 } 181 req->flags |= REQ_F_NEED_CLEANUP; 182 memcpy(async_msg, kmsg, sizeof(*kmsg)); 183 if (async_msg->msg.msg_name) 184 async_msg->msg.msg_name = &async_msg->addr; 185 /* if were using fast_iov, set it to the new one */ 186 if (iter_is_iovec(&kmsg->msg.msg_iter) && !kmsg->free_iov) { 187 size_t fast_idx = kmsg->msg.msg_iter.iov - kmsg->fast_iov; 188 async_msg->msg.msg_iter.iov = &async_msg->fast_iov[fast_idx]; 189 } 190 191 return -EAGAIN; 192 } 193 194 static int io_sendmsg_copy_hdr(struct io_kiocb *req, 195 struct io_async_msghdr *iomsg) 196 { 197 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg); 198 199 iomsg->msg.msg_name = &iomsg->addr; 200 iomsg->free_iov = iomsg->fast_iov; 201 return sendmsg_copy_msghdr(&iomsg->msg, sr->umsg, sr->msg_flags, 202 &iomsg->free_iov); 203 } 204 205 int io_send_prep_async(struct io_kiocb *req) 206 { 207 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg); 208 struct io_async_msghdr *io; 209 int ret; 210 211 if (!zc->addr || req_has_async_data(req)) 212 return 0; 213 io = io_msg_alloc_async_prep(req); 214 if (!io) 215 return -ENOMEM; 216 ret = move_addr_to_kernel(zc->addr, zc->addr_len, &io->addr); 217 return ret; 218 } 219 220 static int io_setup_async_addr(struct io_kiocb *req, 221 struct sockaddr_storage *addr_storage, 222 unsigned int issue_flags) 223 { 224 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg); 225 struct io_async_msghdr *io; 226 227 if (!sr->addr || req_has_async_data(req)) 228 return -EAGAIN; 229 io = io_msg_alloc_async(req, issue_flags); 230 if (!io) 231 return -ENOMEM; 232 memcpy(&io->addr, addr_storage, sizeof(io->addr)); 233 return -EAGAIN; 234 } 235 236 int io_sendmsg_prep_async(struct io_kiocb *req) 237 { 238 int ret; 239 240 if (!io_msg_alloc_async_prep(req)) 241 return -ENOMEM; 242 ret = io_sendmsg_copy_hdr(req, req->async_data); 243 if (!ret) 244 req->flags |= REQ_F_NEED_CLEANUP; 245 return ret; 246 } 247 248 void io_sendmsg_recvmsg_cleanup(struct io_kiocb *req) 249 { 250 struct io_async_msghdr *io = req->async_data; 251 252 kfree(io->free_iov); 253 } 254 255 int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) 256 { 257 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg); 258 259 if (req->opcode == IORING_OP_SEND) { 260 if (READ_ONCE(sqe->__pad3[0])) 261 return -EINVAL; 262 sr->addr = u64_to_user_ptr(READ_ONCE(sqe->addr2)); 263 sr->addr_len = READ_ONCE(sqe->addr_len); 264 } else if (sqe->addr2 || sqe->file_index) { 265 return -EINVAL; 266 } 267 268 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); 269 sr->len = READ_ONCE(sqe->len); 270 sr->flags = READ_ONCE(sqe->ioprio); 271 if (sr->flags & ~IORING_RECVSEND_POLL_FIRST) 272 return -EINVAL; 273 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; 274 if (sr->msg_flags & MSG_DONTWAIT) 275 req->flags |= REQ_F_NOWAIT; 276 277 #ifdef CONFIG_COMPAT 278 if (req->ctx->compat) 279 sr->msg_flags |= MSG_CMSG_COMPAT; 280 #endif 281 sr->done_io = 0; 282 return 0; 283 } 284 285 int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) 286 { 287 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg); 288 struct io_async_msghdr iomsg, *kmsg; 289 struct socket *sock; 290 unsigned flags; 291 int min_ret = 0; 292 int ret; 293 294 sock = sock_from_file(req->file); 295 if (unlikely(!sock)) 296 return -ENOTSOCK; 297 298 if (req_has_async_data(req)) { 299 kmsg = req->async_data; 300 } else { 301 ret = io_sendmsg_copy_hdr(req, &iomsg); 302 if (ret) 303 return ret; 304 kmsg = &iomsg; 305 } 306 307 if (!(req->flags & REQ_F_POLLED) && 308 (sr->flags & IORING_RECVSEND_POLL_FIRST)) 309 return io_setup_async_msg(req, kmsg, issue_flags); 310 311 flags = sr->msg_flags; 312 if (issue_flags & IO_URING_F_NONBLOCK) 313 flags |= MSG_DONTWAIT; 314 if (flags & MSG_WAITALL) 315 min_ret = iov_iter_count(&kmsg->msg.msg_iter); 316 317 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); 318 319 if (ret < min_ret) { 320 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) 321 return io_setup_async_msg(req, kmsg, issue_flags); 322 if (ret > 0 && io_net_retry(sock, flags)) { 323 sr->done_io += ret; 324 req->flags |= REQ_F_PARTIAL_IO; 325 return io_setup_async_msg(req, kmsg, issue_flags); 326 } 327 if (ret == -ERESTARTSYS) 328 ret = -EINTR; 329 req_set_fail(req); 330 } 331 /* fast path, check for non-NULL to avoid function call */ 332 if (kmsg->free_iov) 333 kfree(kmsg->free_iov); 334 req->flags &= ~REQ_F_NEED_CLEANUP; 335 io_netmsg_recycle(req, issue_flags); 336 if (ret >= 0) 337 ret += sr->done_io; 338 else if (sr->done_io) 339 ret = sr->done_io; 340 io_req_set_res(req, ret, 0); 341 return IOU_OK; 342 } 343 344 int io_send(struct io_kiocb *req, unsigned int issue_flags) 345 { 346 struct sockaddr_storage __address; 347 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg); 348 struct msghdr msg; 349 struct socket *sock; 350 unsigned flags; 351 int min_ret = 0; 352 int ret; 353 354 msg.msg_name = NULL; 355 msg.msg_control = NULL; 356 msg.msg_controllen = 0; 357 msg.msg_namelen = 0; 358 msg.msg_ubuf = NULL; 359 360 if (sr->addr) { 361 if (req_has_async_data(req)) { 362 struct io_async_msghdr *io = req->async_data; 363 364 msg.msg_name = &io->addr; 365 } else { 366 ret = move_addr_to_kernel(sr->addr, sr->addr_len, &__address); 367 if (unlikely(ret < 0)) 368 return ret; 369 msg.msg_name = (struct sockaddr *)&__address; 370 } 371 msg.msg_namelen = sr->addr_len; 372 } 373 374 if (!(req->flags & REQ_F_POLLED) && 375 (sr->flags & IORING_RECVSEND_POLL_FIRST)) 376 return io_setup_async_addr(req, &__address, issue_flags); 377 378 sock = sock_from_file(req->file); 379 if (unlikely(!sock)) 380 return -ENOTSOCK; 381 382 ret = import_ubuf(ITER_SOURCE, sr->buf, sr->len, &msg.msg_iter); 383 if (unlikely(ret)) 384 return ret; 385 386 flags = sr->msg_flags; 387 if (issue_flags & IO_URING_F_NONBLOCK) 388 flags |= MSG_DONTWAIT; 389 if (flags & MSG_WAITALL) 390 min_ret = iov_iter_count(&msg.msg_iter); 391 392 msg.msg_flags = flags; 393 ret = sock_sendmsg(sock, &msg); 394 if (ret < min_ret) { 395 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) 396 return io_setup_async_addr(req, &__address, issue_flags); 397 398 if (ret > 0 && io_net_retry(sock, flags)) { 399 sr->len -= ret; 400 sr->buf += ret; 401 sr->done_io += ret; 402 req->flags |= REQ_F_PARTIAL_IO; 403 return io_setup_async_addr(req, &__address, issue_flags); 404 } 405 if (ret == -ERESTARTSYS) 406 ret = -EINTR; 407 req_set_fail(req); 408 } 409 if (ret >= 0) 410 ret += sr->done_io; 411 else if (sr->done_io) 412 ret = sr->done_io; 413 io_req_set_res(req, ret, 0); 414 return IOU_OK; 415 } 416 417 static bool io_recvmsg_multishot_overflow(struct io_async_msghdr *iomsg) 418 { 419 int hdr; 420 421 if (iomsg->namelen < 0) 422 return true; 423 if (check_add_overflow((int)sizeof(struct io_uring_recvmsg_out), 424 iomsg->namelen, &hdr)) 425 return true; 426 if (check_add_overflow(hdr, (int)iomsg->controllen, &hdr)) 427 return true; 428 429 return false; 430 } 431 432 static int __io_recvmsg_copy_hdr(struct io_kiocb *req, 433 struct io_async_msghdr *iomsg) 434 { 435 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg); 436 struct user_msghdr msg; 437 int ret; 438 439 if (copy_from_user(&msg, sr->umsg, sizeof(*sr->umsg))) 440 return -EFAULT; 441 442 ret = __copy_msghdr(&iomsg->msg, &msg, &iomsg->uaddr); 443 if (ret) 444 return ret; 445 446 if (req->flags & REQ_F_BUFFER_SELECT) { 447 if (msg.msg_iovlen == 0) { 448 sr->len = iomsg->fast_iov[0].iov_len = 0; 449 iomsg->fast_iov[0].iov_base = NULL; 450 iomsg->free_iov = NULL; 451 } else if (msg.msg_iovlen > 1) { 452 return -EINVAL; 453 } else { 454 if (copy_from_user(iomsg->fast_iov, msg.msg_iov, sizeof(*msg.msg_iov))) 455 return -EFAULT; 456 sr->len = iomsg->fast_iov[0].iov_len; 457 iomsg->free_iov = NULL; 458 } 459 460 if (req->flags & REQ_F_APOLL_MULTISHOT) { 461 iomsg->namelen = msg.msg_namelen; 462 iomsg->controllen = msg.msg_controllen; 463 if (io_recvmsg_multishot_overflow(iomsg)) 464 return -EOVERFLOW; 465 } 466 } else { 467 iomsg->free_iov = iomsg->fast_iov; 468 ret = __import_iovec(ITER_DEST, msg.msg_iov, msg.msg_iovlen, UIO_FASTIOV, 469 &iomsg->free_iov, &iomsg->msg.msg_iter, 470 false); 471 if (ret > 0) 472 ret = 0; 473 } 474 475 return ret; 476 } 477 478 #ifdef CONFIG_COMPAT 479 static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, 480 struct io_async_msghdr *iomsg) 481 { 482 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg); 483 struct compat_msghdr msg; 484 struct compat_iovec __user *uiov; 485 int ret; 486 487 if (copy_from_user(&msg, sr->umsg_compat, sizeof(msg))) 488 return -EFAULT; 489 490 ret = __get_compat_msghdr(&iomsg->msg, &msg, &iomsg->uaddr); 491 if (ret) 492 return ret; 493 494 uiov = compat_ptr(msg.msg_iov); 495 if (req->flags & REQ_F_BUFFER_SELECT) { 496 compat_ssize_t clen; 497 498 iomsg->free_iov = NULL; 499 if (msg.msg_iovlen == 0) { 500 sr->len = 0; 501 } else if (msg.msg_iovlen > 1) { 502 return -EINVAL; 503 } else { 504 if (!access_ok(uiov, sizeof(*uiov))) 505 return -EFAULT; 506 if (__get_user(clen, &uiov->iov_len)) 507 return -EFAULT; 508 if (clen < 0) 509 return -EINVAL; 510 sr->len = clen; 511 } 512 513 if (req->flags & REQ_F_APOLL_MULTISHOT) { 514 iomsg->namelen = msg.msg_namelen; 515 iomsg->controllen = msg.msg_controllen; 516 if (io_recvmsg_multishot_overflow(iomsg)) 517 return -EOVERFLOW; 518 } 519 } else { 520 iomsg->free_iov = iomsg->fast_iov; 521 ret = __import_iovec(ITER_DEST, (struct iovec __user *)uiov, msg.msg_iovlen, 522 UIO_FASTIOV, &iomsg->free_iov, 523 &iomsg->msg.msg_iter, true); 524 if (ret < 0) 525 return ret; 526 } 527 528 return 0; 529 } 530 #endif 531 532 static int io_recvmsg_copy_hdr(struct io_kiocb *req, 533 struct io_async_msghdr *iomsg) 534 { 535 iomsg->msg.msg_name = &iomsg->addr; 536 537 #ifdef CONFIG_COMPAT 538 if (req->ctx->compat) 539 return __io_compat_recvmsg_copy_hdr(req, iomsg); 540 #endif 541 542 return __io_recvmsg_copy_hdr(req, iomsg); 543 } 544 545 int io_recvmsg_prep_async(struct io_kiocb *req) 546 { 547 int ret; 548 549 if (!io_msg_alloc_async_prep(req)) 550 return -ENOMEM; 551 ret = io_recvmsg_copy_hdr(req, req->async_data); 552 if (!ret) 553 req->flags |= REQ_F_NEED_CLEANUP; 554 return ret; 555 } 556 557 #define RECVMSG_FLAGS (IORING_RECVSEND_POLL_FIRST | IORING_RECV_MULTISHOT) 558 559 int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) 560 { 561 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg); 562 563 if (unlikely(sqe->file_index || sqe->addr2)) 564 return -EINVAL; 565 566 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); 567 sr->len = READ_ONCE(sqe->len); 568 sr->flags = READ_ONCE(sqe->ioprio); 569 if (sr->flags & ~(RECVMSG_FLAGS)) 570 return -EINVAL; 571 sr->msg_flags = READ_ONCE(sqe->msg_flags); 572 if (sr->msg_flags & MSG_DONTWAIT) 573 req->flags |= REQ_F_NOWAIT; 574 if (sr->msg_flags & MSG_ERRQUEUE) 575 req->flags |= REQ_F_CLEAR_POLLIN; 576 if (sr->flags & IORING_RECV_MULTISHOT) { 577 if (!(req->flags & REQ_F_BUFFER_SELECT)) 578 return -EINVAL; 579 if (sr->msg_flags & MSG_WAITALL) 580 return -EINVAL; 581 if (req->opcode == IORING_OP_RECV && sr->len) 582 return -EINVAL; 583 req->flags |= REQ_F_APOLL_MULTISHOT; 584 /* 585 * Store the buffer group for this multishot receive separately, 586 * as if we end up doing an io-wq based issue that selects a 587 * buffer, it has to be committed immediately and that will 588 * clear ->buf_list. This means we lose the link to the buffer 589 * list, and the eventual buffer put on completion then cannot 590 * restore it. 591 */ 592 sr->buf_group = req->buf_index; 593 } 594 595 #ifdef CONFIG_COMPAT 596 if (req->ctx->compat) 597 sr->msg_flags |= MSG_CMSG_COMPAT; 598 #endif 599 sr->done_io = 0; 600 return 0; 601 } 602 603 static inline void io_recv_prep_retry(struct io_kiocb *req) 604 { 605 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg); 606 607 sr->done_io = 0; 608 sr->len = 0; /* get from the provided buffer */ 609 req->buf_index = sr->buf_group; 610 } 611 612 /* 613 * Finishes io_recv and io_recvmsg. 614 * 615 * Returns true if it is actually finished, or false if it should run 616 * again (for multishot). 617 */ 618 static inline bool io_recv_finish(struct io_kiocb *req, int *ret, 619 unsigned int cflags, bool mshot_finished, 620 unsigned issue_flags) 621 { 622 if (!(req->flags & REQ_F_APOLL_MULTISHOT)) { 623 io_req_set_res(req, *ret, cflags); 624 *ret = IOU_OK; 625 return true; 626 } 627 628 if (!mshot_finished) { 629 if (io_aux_cqe(req->ctx, issue_flags & IO_URING_F_COMPLETE_DEFER, 630 req->cqe.user_data, *ret, cflags | IORING_CQE_F_MORE, true)) { 631 io_recv_prep_retry(req); 632 return false; 633 } 634 /* Otherwise stop multishot but use the current result. */ 635 } 636 637 io_req_set_res(req, *ret, cflags); 638 639 if (issue_flags & IO_URING_F_MULTISHOT) 640 *ret = IOU_STOP_MULTISHOT; 641 else 642 *ret = IOU_OK; 643 return true; 644 } 645 646 static int io_recvmsg_prep_multishot(struct io_async_msghdr *kmsg, 647 struct io_sr_msg *sr, void __user **buf, 648 size_t *len) 649 { 650 unsigned long ubuf = (unsigned long) *buf; 651 unsigned long hdr; 652 653 hdr = sizeof(struct io_uring_recvmsg_out) + kmsg->namelen + 654 kmsg->controllen; 655 if (*len < hdr) 656 return -EFAULT; 657 658 if (kmsg->controllen) { 659 unsigned long control = ubuf + hdr - kmsg->controllen; 660 661 kmsg->msg.msg_control_user = (void __user *) control; 662 kmsg->msg.msg_controllen = kmsg->controllen; 663 } 664 665 sr->buf = *buf; /* stash for later copy */ 666 *buf = (void __user *) (ubuf + hdr); 667 kmsg->payloadlen = *len = *len - hdr; 668 return 0; 669 } 670 671 struct io_recvmsg_multishot_hdr { 672 struct io_uring_recvmsg_out msg; 673 struct sockaddr_storage addr; 674 }; 675 676 static int io_recvmsg_multishot(struct socket *sock, struct io_sr_msg *io, 677 struct io_async_msghdr *kmsg, 678 unsigned int flags, bool *finished) 679 { 680 int err; 681 int copy_len; 682 struct io_recvmsg_multishot_hdr hdr; 683 684 if (kmsg->namelen) 685 kmsg->msg.msg_name = &hdr.addr; 686 kmsg->msg.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT); 687 kmsg->msg.msg_namelen = 0; 688 689 if (sock->file->f_flags & O_NONBLOCK) 690 flags |= MSG_DONTWAIT; 691 692 err = sock_recvmsg(sock, &kmsg->msg, flags); 693 *finished = err <= 0; 694 if (err < 0) 695 return err; 696 697 hdr.msg = (struct io_uring_recvmsg_out) { 698 .controllen = kmsg->controllen - kmsg->msg.msg_controllen, 699 .flags = kmsg->msg.msg_flags & ~MSG_CMSG_COMPAT 700 }; 701 702 hdr.msg.payloadlen = err; 703 if (err > kmsg->payloadlen) 704 err = kmsg->payloadlen; 705 706 copy_len = sizeof(struct io_uring_recvmsg_out); 707 if (kmsg->msg.msg_namelen > kmsg->namelen) 708 copy_len += kmsg->namelen; 709 else 710 copy_len += kmsg->msg.msg_namelen; 711 712 /* 713 * "fromlen shall refer to the value before truncation.." 714 * 1003.1g 715 */ 716 hdr.msg.namelen = kmsg->msg.msg_namelen; 717 718 /* ensure that there is no gap between hdr and sockaddr_storage */ 719 BUILD_BUG_ON(offsetof(struct io_recvmsg_multishot_hdr, addr) != 720 sizeof(struct io_uring_recvmsg_out)); 721 if (copy_to_user(io->buf, &hdr, copy_len)) { 722 *finished = true; 723 return -EFAULT; 724 } 725 726 return sizeof(struct io_uring_recvmsg_out) + kmsg->namelen + 727 kmsg->controllen + err; 728 } 729 730 int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) 731 { 732 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg); 733 struct io_async_msghdr iomsg, *kmsg; 734 struct socket *sock; 735 unsigned int cflags; 736 unsigned flags; 737 int ret, min_ret = 0; 738 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; 739 bool mshot_finished = true; 740 741 sock = sock_from_file(req->file); 742 if (unlikely(!sock)) 743 return -ENOTSOCK; 744 745 if (req_has_async_data(req)) { 746 kmsg = req->async_data; 747 } else { 748 ret = io_recvmsg_copy_hdr(req, &iomsg); 749 if (ret) 750 return ret; 751 kmsg = &iomsg; 752 } 753 754 if (!(req->flags & REQ_F_POLLED) && 755 (sr->flags & IORING_RECVSEND_POLL_FIRST)) 756 return io_setup_async_msg(req, kmsg, issue_flags); 757 758 if (!io_check_multishot(req, issue_flags)) 759 return io_setup_async_msg(req, kmsg, issue_flags); 760 761 retry_multishot: 762 if (io_do_buffer_select(req)) { 763 void __user *buf; 764 size_t len = sr->len; 765 766 buf = io_buffer_select(req, &len, issue_flags); 767 if (!buf) 768 return -ENOBUFS; 769 770 if (req->flags & REQ_F_APOLL_MULTISHOT) { 771 ret = io_recvmsg_prep_multishot(kmsg, sr, &buf, &len); 772 if (ret) { 773 io_kbuf_recycle(req, issue_flags); 774 return ret; 775 } 776 } 777 778 iov_iter_ubuf(&kmsg->msg.msg_iter, ITER_DEST, buf, len); 779 } 780 781 flags = sr->msg_flags; 782 if (force_nonblock) 783 flags |= MSG_DONTWAIT; 784 if (flags & MSG_WAITALL) 785 min_ret = iov_iter_count(&kmsg->msg.msg_iter); 786 787 kmsg->msg.msg_get_inq = 1; 788 if (req->flags & REQ_F_APOLL_MULTISHOT) 789 ret = io_recvmsg_multishot(sock, sr, kmsg, flags, 790 &mshot_finished); 791 else 792 ret = __sys_recvmsg_sock(sock, &kmsg->msg, sr->umsg, 793 kmsg->uaddr, flags); 794 795 if (ret < min_ret) { 796 if (ret == -EAGAIN && force_nonblock) { 797 ret = io_setup_async_msg(req, kmsg, issue_flags); 798 if (ret == -EAGAIN && (issue_flags & IO_URING_F_MULTISHOT)) { 799 io_kbuf_recycle(req, issue_flags); 800 return IOU_ISSUE_SKIP_COMPLETE; 801 } 802 return ret; 803 } 804 if (ret > 0 && io_net_retry(sock, flags)) { 805 sr->done_io += ret; 806 req->flags |= REQ_F_PARTIAL_IO; 807 return io_setup_async_msg(req, kmsg, issue_flags); 808 } 809 if (ret == -ERESTARTSYS) 810 ret = -EINTR; 811 req_set_fail(req); 812 } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { 813 req_set_fail(req); 814 } 815 816 if (ret > 0) 817 ret += sr->done_io; 818 else if (sr->done_io) 819 ret = sr->done_io; 820 else 821 io_kbuf_recycle(req, issue_flags); 822 823 cflags = io_put_kbuf(req, issue_flags); 824 if (kmsg->msg.msg_inq) 825 cflags |= IORING_CQE_F_SOCK_NONEMPTY; 826 827 if (!io_recv_finish(req, &ret, cflags, mshot_finished, issue_flags)) 828 goto retry_multishot; 829 830 if (mshot_finished) { 831 /* fast path, check for non-NULL to avoid function call */ 832 if (kmsg->free_iov) 833 kfree(kmsg->free_iov); 834 io_netmsg_recycle(req, issue_flags); 835 req->flags &= ~REQ_F_NEED_CLEANUP; 836 } 837 838 return ret; 839 } 840 841 int io_recv(struct io_kiocb *req, unsigned int issue_flags) 842 { 843 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg); 844 struct msghdr msg; 845 struct socket *sock; 846 unsigned int cflags; 847 unsigned flags; 848 int ret, min_ret = 0; 849 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; 850 size_t len = sr->len; 851 852 if (!(req->flags & REQ_F_POLLED) && 853 (sr->flags & IORING_RECVSEND_POLL_FIRST)) 854 return -EAGAIN; 855 856 if (!io_check_multishot(req, issue_flags)) 857 return -EAGAIN; 858 859 sock = sock_from_file(req->file); 860 if (unlikely(!sock)) 861 return -ENOTSOCK; 862 863 retry_multishot: 864 if (io_do_buffer_select(req)) { 865 void __user *buf; 866 867 buf = io_buffer_select(req, &len, issue_flags); 868 if (!buf) 869 return -ENOBUFS; 870 sr->buf = buf; 871 } 872 873 ret = import_ubuf(ITER_DEST, sr->buf, len, &msg.msg_iter); 874 if (unlikely(ret)) 875 goto out_free; 876 877 msg.msg_name = NULL; 878 msg.msg_namelen = 0; 879 msg.msg_control = NULL; 880 msg.msg_get_inq = 1; 881 msg.msg_flags = 0; 882 msg.msg_controllen = 0; 883 msg.msg_iocb = NULL; 884 msg.msg_ubuf = NULL; 885 886 flags = sr->msg_flags; 887 if (force_nonblock) 888 flags |= MSG_DONTWAIT; 889 if (flags & MSG_WAITALL) 890 min_ret = iov_iter_count(&msg.msg_iter); 891 892 ret = sock_recvmsg(sock, &msg, flags); 893 if (ret < min_ret) { 894 if (ret == -EAGAIN && force_nonblock) { 895 if (issue_flags & IO_URING_F_MULTISHOT) { 896 io_kbuf_recycle(req, issue_flags); 897 return IOU_ISSUE_SKIP_COMPLETE; 898 } 899 900 return -EAGAIN; 901 } 902 if (ret > 0 && io_net_retry(sock, flags)) { 903 sr->len -= ret; 904 sr->buf += ret; 905 sr->done_io += ret; 906 req->flags |= REQ_F_PARTIAL_IO; 907 return -EAGAIN; 908 } 909 if (ret == -ERESTARTSYS) 910 ret = -EINTR; 911 req_set_fail(req); 912 } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { 913 out_free: 914 req_set_fail(req); 915 } 916 917 if (ret > 0) 918 ret += sr->done_io; 919 else if (sr->done_io) 920 ret = sr->done_io; 921 else 922 io_kbuf_recycle(req, issue_flags); 923 924 cflags = io_put_kbuf(req, issue_flags); 925 if (msg.msg_inq) 926 cflags |= IORING_CQE_F_SOCK_NONEMPTY; 927 928 if (!io_recv_finish(req, &ret, cflags, ret <= 0, issue_flags)) 929 goto retry_multishot; 930 931 return ret; 932 } 933 934 void io_send_zc_cleanup(struct io_kiocb *req) 935 { 936 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg); 937 struct io_async_msghdr *io; 938 939 if (req_has_async_data(req)) { 940 io = req->async_data; 941 /* might be ->fast_iov if *msg_copy_hdr failed */ 942 if (io->free_iov != io->fast_iov) 943 kfree(io->free_iov); 944 } 945 if (zc->notif) { 946 io_notif_flush(zc->notif); 947 zc->notif = NULL; 948 } 949 } 950 951 #define IO_ZC_FLAGS_COMMON (IORING_RECVSEND_POLL_FIRST | IORING_RECVSEND_FIXED_BUF) 952 #define IO_ZC_FLAGS_VALID (IO_ZC_FLAGS_COMMON | IORING_SEND_ZC_REPORT_USAGE) 953 954 int io_send_zc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) 955 { 956 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg); 957 struct io_ring_ctx *ctx = req->ctx; 958 struct io_kiocb *notif; 959 960 if (unlikely(READ_ONCE(sqe->__pad2[0]) || READ_ONCE(sqe->addr3))) 961 return -EINVAL; 962 /* we don't support IOSQE_CQE_SKIP_SUCCESS just yet */ 963 if (req->flags & REQ_F_CQE_SKIP) 964 return -EINVAL; 965 966 notif = zc->notif = io_alloc_notif(ctx); 967 if (!notif) 968 return -ENOMEM; 969 notif->cqe.user_data = req->cqe.user_data; 970 notif->cqe.res = 0; 971 notif->cqe.flags = IORING_CQE_F_NOTIF; 972 req->flags |= REQ_F_NEED_CLEANUP; 973 974 zc->flags = READ_ONCE(sqe->ioprio); 975 if (unlikely(zc->flags & ~IO_ZC_FLAGS_COMMON)) { 976 if (zc->flags & ~IO_ZC_FLAGS_VALID) 977 return -EINVAL; 978 if (zc->flags & IORING_SEND_ZC_REPORT_USAGE) { 979 io_notif_set_extended(notif); 980 io_notif_to_data(notif)->zc_report = true; 981 } 982 } 983 984 if (zc->flags & IORING_RECVSEND_FIXED_BUF) { 985 unsigned idx = READ_ONCE(sqe->buf_index); 986 987 if (unlikely(idx >= ctx->nr_user_bufs)) 988 return -EFAULT; 989 idx = array_index_nospec(idx, ctx->nr_user_bufs); 990 req->imu = READ_ONCE(ctx->user_bufs[idx]); 991 io_req_set_rsrc_node(notif, ctx, 0); 992 } 993 994 if (req->opcode == IORING_OP_SEND_ZC) { 995 if (READ_ONCE(sqe->__pad3[0])) 996 return -EINVAL; 997 zc->addr = u64_to_user_ptr(READ_ONCE(sqe->addr2)); 998 zc->addr_len = READ_ONCE(sqe->addr_len); 999 } else { 1000 if (unlikely(sqe->addr2 || sqe->file_index)) 1001 return -EINVAL; 1002 if (unlikely(zc->flags & IORING_RECVSEND_FIXED_BUF)) 1003 return -EINVAL; 1004 } 1005 1006 zc->buf = u64_to_user_ptr(READ_ONCE(sqe->addr)); 1007 zc->len = READ_ONCE(sqe->len); 1008 zc->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; 1009 if (zc->msg_flags & MSG_DONTWAIT) 1010 req->flags |= REQ_F_NOWAIT; 1011 1012 zc->done_io = 0; 1013 1014 #ifdef CONFIG_COMPAT 1015 if (req->ctx->compat) 1016 zc->msg_flags |= MSG_CMSG_COMPAT; 1017 #endif 1018 return 0; 1019 } 1020 1021 static int io_sg_from_iter_iovec(struct sock *sk, struct sk_buff *skb, 1022 struct iov_iter *from, size_t length) 1023 { 1024 skb_zcopy_downgrade_managed(skb); 1025 return __zerocopy_sg_from_iter(NULL, sk, skb, from, length); 1026 } 1027 1028 static int io_sg_from_iter(struct sock *sk, struct sk_buff *skb, 1029 struct iov_iter *from, size_t length) 1030 { 1031 struct skb_shared_info *shinfo = skb_shinfo(skb); 1032 int frag = shinfo->nr_frags; 1033 int ret = 0; 1034 struct bvec_iter bi; 1035 ssize_t copied = 0; 1036 unsigned long truesize = 0; 1037 1038 if (!frag) 1039 shinfo->flags |= SKBFL_MANAGED_FRAG_REFS; 1040 else if (unlikely(!skb_zcopy_managed(skb))) 1041 return __zerocopy_sg_from_iter(NULL, sk, skb, from, length); 1042 1043 bi.bi_size = min(from->count, length); 1044 bi.bi_bvec_done = from->iov_offset; 1045 bi.bi_idx = 0; 1046 1047 while (bi.bi_size && frag < MAX_SKB_FRAGS) { 1048 struct bio_vec v = mp_bvec_iter_bvec(from->bvec, bi); 1049 1050 copied += v.bv_len; 1051 truesize += PAGE_ALIGN(v.bv_len + v.bv_offset); 1052 __skb_fill_page_desc_noacc(shinfo, frag++, v.bv_page, 1053 v.bv_offset, v.bv_len); 1054 bvec_iter_advance_single(from->bvec, &bi, v.bv_len); 1055 } 1056 if (bi.bi_size) 1057 ret = -EMSGSIZE; 1058 1059 shinfo->nr_frags = frag; 1060 from->bvec += bi.bi_idx; 1061 from->nr_segs -= bi.bi_idx; 1062 from->count -= copied; 1063 from->iov_offset = bi.bi_bvec_done; 1064 1065 skb->data_len += copied; 1066 skb->len += copied; 1067 skb->truesize += truesize; 1068 1069 if (sk && sk->sk_type == SOCK_STREAM) { 1070 sk_wmem_queued_add(sk, truesize); 1071 if (!skb_zcopy_pure(skb)) 1072 sk_mem_charge(sk, truesize); 1073 } else { 1074 refcount_add(truesize, &skb->sk->sk_wmem_alloc); 1075 } 1076 return ret; 1077 } 1078 1079 int io_send_zc(struct io_kiocb *req, unsigned int issue_flags) 1080 { 1081 struct sockaddr_storage __address; 1082 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg); 1083 struct msghdr msg; 1084 struct socket *sock; 1085 unsigned msg_flags; 1086 int ret, min_ret = 0; 1087 1088 sock = sock_from_file(req->file); 1089 if (unlikely(!sock)) 1090 return -ENOTSOCK; 1091 if (!test_bit(SOCK_SUPPORT_ZC, &sock->flags)) 1092 return -EOPNOTSUPP; 1093 1094 msg.msg_name = NULL; 1095 msg.msg_control = NULL; 1096 msg.msg_controllen = 0; 1097 msg.msg_namelen = 0; 1098 1099 if (zc->addr) { 1100 if (req_has_async_data(req)) { 1101 struct io_async_msghdr *io = req->async_data; 1102 1103 msg.msg_name = &io->addr; 1104 } else { 1105 ret = move_addr_to_kernel(zc->addr, zc->addr_len, &__address); 1106 if (unlikely(ret < 0)) 1107 return ret; 1108 msg.msg_name = (struct sockaddr *)&__address; 1109 } 1110 msg.msg_namelen = zc->addr_len; 1111 } 1112 1113 if (!(req->flags & REQ_F_POLLED) && 1114 (zc->flags & IORING_RECVSEND_POLL_FIRST)) 1115 return io_setup_async_addr(req, &__address, issue_flags); 1116 1117 if (zc->flags & IORING_RECVSEND_FIXED_BUF) { 1118 ret = io_import_fixed(ITER_SOURCE, &msg.msg_iter, req->imu, 1119 (u64)(uintptr_t)zc->buf, zc->len); 1120 if (unlikely(ret)) 1121 return ret; 1122 msg.sg_from_iter = io_sg_from_iter; 1123 } else { 1124 io_notif_set_extended(zc->notif); 1125 ret = import_ubuf(ITER_SOURCE, zc->buf, zc->len, &msg.msg_iter); 1126 if (unlikely(ret)) 1127 return ret; 1128 ret = io_notif_account_mem(zc->notif, zc->len); 1129 if (unlikely(ret)) 1130 return ret; 1131 msg.sg_from_iter = io_sg_from_iter_iovec; 1132 } 1133 1134 msg_flags = zc->msg_flags | MSG_ZEROCOPY; 1135 if (issue_flags & IO_URING_F_NONBLOCK) 1136 msg_flags |= MSG_DONTWAIT; 1137 if (msg_flags & MSG_WAITALL) 1138 min_ret = iov_iter_count(&msg.msg_iter); 1139 1140 msg.msg_flags = msg_flags; 1141 msg.msg_ubuf = &io_notif_to_data(zc->notif)->uarg; 1142 ret = sock_sendmsg(sock, &msg); 1143 1144 if (unlikely(ret < min_ret)) { 1145 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) 1146 return io_setup_async_addr(req, &__address, issue_flags); 1147 1148 if (ret > 0 && io_net_retry(sock, msg.msg_flags)) { 1149 zc->len -= ret; 1150 zc->buf += ret; 1151 zc->done_io += ret; 1152 req->flags |= REQ_F_PARTIAL_IO; 1153 return io_setup_async_addr(req, &__address, issue_flags); 1154 } 1155 if (ret == -ERESTARTSYS) 1156 ret = -EINTR; 1157 req_set_fail(req); 1158 } 1159 1160 if (ret >= 0) 1161 ret += zc->done_io; 1162 else if (zc->done_io) 1163 ret = zc->done_io; 1164 1165 /* 1166 * If we're in io-wq we can't rely on tw ordering guarantees, defer 1167 * flushing notif to io_send_zc_cleanup() 1168 */ 1169 if (!(issue_flags & IO_URING_F_UNLOCKED)) { 1170 io_notif_flush(zc->notif); 1171 req->flags &= ~REQ_F_NEED_CLEANUP; 1172 } 1173 io_req_set_res(req, ret, IORING_CQE_F_MORE); 1174 return IOU_OK; 1175 } 1176 1177 int io_sendmsg_zc(struct io_kiocb *req, unsigned int issue_flags) 1178 { 1179 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg); 1180 struct io_async_msghdr iomsg, *kmsg; 1181 struct socket *sock; 1182 unsigned flags; 1183 int ret, min_ret = 0; 1184 1185 io_notif_set_extended(sr->notif); 1186 1187 sock = sock_from_file(req->file); 1188 if (unlikely(!sock)) 1189 return -ENOTSOCK; 1190 if (!test_bit(SOCK_SUPPORT_ZC, &sock->flags)) 1191 return -EOPNOTSUPP; 1192 1193 if (req_has_async_data(req)) { 1194 kmsg = req->async_data; 1195 } else { 1196 ret = io_sendmsg_copy_hdr(req, &iomsg); 1197 if (ret) 1198 return ret; 1199 kmsg = &iomsg; 1200 } 1201 1202 if (!(req->flags & REQ_F_POLLED) && 1203 (sr->flags & IORING_RECVSEND_POLL_FIRST)) 1204 return io_setup_async_msg(req, kmsg, issue_flags); 1205 1206 flags = sr->msg_flags | MSG_ZEROCOPY; 1207 if (issue_flags & IO_URING_F_NONBLOCK) 1208 flags |= MSG_DONTWAIT; 1209 if (flags & MSG_WAITALL) 1210 min_ret = iov_iter_count(&kmsg->msg.msg_iter); 1211 1212 kmsg->msg.msg_ubuf = &io_notif_to_data(sr->notif)->uarg; 1213 kmsg->msg.sg_from_iter = io_sg_from_iter_iovec; 1214 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); 1215 1216 if (unlikely(ret < min_ret)) { 1217 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) 1218 return io_setup_async_msg(req, kmsg, issue_flags); 1219 1220 if (ret > 0 && io_net_retry(sock, flags)) { 1221 sr->done_io += ret; 1222 req->flags |= REQ_F_PARTIAL_IO; 1223 return io_setup_async_msg(req, kmsg, issue_flags); 1224 } 1225 if (ret == -ERESTARTSYS) 1226 ret = -EINTR; 1227 req_set_fail(req); 1228 } 1229 /* fast path, check for non-NULL to avoid function call */ 1230 if (kmsg->free_iov) { 1231 kfree(kmsg->free_iov); 1232 kmsg->free_iov = NULL; 1233 } 1234 1235 io_netmsg_recycle(req, issue_flags); 1236 if (ret >= 0) 1237 ret += sr->done_io; 1238 else if (sr->done_io) 1239 ret = sr->done_io; 1240 1241 /* 1242 * If we're in io-wq we can't rely on tw ordering guarantees, defer 1243 * flushing notif to io_send_zc_cleanup() 1244 */ 1245 if (!(issue_flags & IO_URING_F_UNLOCKED)) { 1246 io_notif_flush(sr->notif); 1247 req->flags &= ~REQ_F_NEED_CLEANUP; 1248 } 1249 io_req_set_res(req, ret, IORING_CQE_F_MORE); 1250 return IOU_OK; 1251 } 1252 1253 void io_sendrecv_fail(struct io_kiocb *req) 1254 { 1255 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg); 1256 1257 if (req->flags & REQ_F_PARTIAL_IO) 1258 req->cqe.res = sr->done_io; 1259 1260 if ((req->flags & REQ_F_NEED_CLEANUP) && 1261 (req->opcode == IORING_OP_SEND_ZC || req->opcode == IORING_OP_SENDMSG_ZC)) 1262 req->cqe.flags |= IORING_CQE_F_MORE; 1263 } 1264 1265 int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) 1266 { 1267 struct io_accept *accept = io_kiocb_to_cmd(req, struct io_accept); 1268 unsigned flags; 1269 1270 if (sqe->len || sqe->buf_index) 1271 return -EINVAL; 1272 1273 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); 1274 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); 1275 accept->flags = READ_ONCE(sqe->accept_flags); 1276 accept->nofile = rlimit(RLIMIT_NOFILE); 1277 flags = READ_ONCE(sqe->ioprio); 1278 if (flags & ~IORING_ACCEPT_MULTISHOT) 1279 return -EINVAL; 1280 1281 accept->file_slot = READ_ONCE(sqe->file_index); 1282 if (accept->file_slot) { 1283 if (accept->flags & SOCK_CLOEXEC) 1284 return -EINVAL; 1285 if (flags & IORING_ACCEPT_MULTISHOT && 1286 accept->file_slot != IORING_FILE_INDEX_ALLOC) 1287 return -EINVAL; 1288 } 1289 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) 1290 return -EINVAL; 1291 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) 1292 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; 1293 if (flags & IORING_ACCEPT_MULTISHOT) 1294 req->flags |= REQ_F_APOLL_MULTISHOT; 1295 return 0; 1296 } 1297 1298 int io_accept(struct io_kiocb *req, unsigned int issue_flags) 1299 { 1300 struct io_ring_ctx *ctx = req->ctx; 1301 struct io_accept *accept = io_kiocb_to_cmd(req, struct io_accept); 1302 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; 1303 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; 1304 bool fixed = !!accept->file_slot; 1305 struct file *file; 1306 int ret, fd; 1307 1308 if (!io_check_multishot(req, issue_flags)) 1309 return -EAGAIN; 1310 retry: 1311 if (!fixed) { 1312 fd = __get_unused_fd_flags(accept->flags, accept->nofile); 1313 if (unlikely(fd < 0)) 1314 return fd; 1315 } 1316 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, 1317 accept->flags); 1318 if (IS_ERR(file)) { 1319 if (!fixed) 1320 put_unused_fd(fd); 1321 ret = PTR_ERR(file); 1322 if (ret == -EAGAIN && force_nonblock) { 1323 /* 1324 * if it's multishot and polled, we don't need to 1325 * return EAGAIN to arm the poll infra since it 1326 * has already been done 1327 */ 1328 if (issue_flags & IO_URING_F_MULTISHOT) 1329 ret = IOU_ISSUE_SKIP_COMPLETE; 1330 return ret; 1331 } 1332 if (ret == -ERESTARTSYS) 1333 ret = -EINTR; 1334 req_set_fail(req); 1335 } else if (!fixed) { 1336 fd_install(fd, file); 1337 ret = fd; 1338 } else { 1339 ret = io_fixed_fd_install(req, issue_flags, file, 1340 accept->file_slot); 1341 } 1342 1343 if (!(req->flags & REQ_F_APOLL_MULTISHOT)) { 1344 io_req_set_res(req, ret, 0); 1345 return IOU_OK; 1346 } 1347 1348 if (ret < 0) 1349 return ret; 1350 if (io_aux_cqe(ctx, issue_flags & IO_URING_F_COMPLETE_DEFER, 1351 req->cqe.user_data, ret, IORING_CQE_F_MORE, true)) 1352 goto retry; 1353 1354 return -ECANCELED; 1355 } 1356 1357 int io_socket_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) 1358 { 1359 struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket); 1360 1361 if (sqe->addr || sqe->rw_flags || sqe->buf_index) 1362 return -EINVAL; 1363 1364 sock->domain = READ_ONCE(sqe->fd); 1365 sock->type = READ_ONCE(sqe->off); 1366 sock->protocol = READ_ONCE(sqe->len); 1367 sock->file_slot = READ_ONCE(sqe->file_index); 1368 sock->nofile = rlimit(RLIMIT_NOFILE); 1369 1370 sock->flags = sock->type & ~SOCK_TYPE_MASK; 1371 if (sock->file_slot && (sock->flags & SOCK_CLOEXEC)) 1372 return -EINVAL; 1373 if (sock->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) 1374 return -EINVAL; 1375 return 0; 1376 } 1377 1378 int io_socket(struct io_kiocb *req, unsigned int issue_flags) 1379 { 1380 struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket); 1381 bool fixed = !!sock->file_slot; 1382 struct file *file; 1383 int ret, fd; 1384 1385 if (!fixed) { 1386 fd = __get_unused_fd_flags(sock->flags, sock->nofile); 1387 if (unlikely(fd < 0)) 1388 return fd; 1389 } 1390 file = __sys_socket_file(sock->domain, sock->type, sock->protocol); 1391 if (IS_ERR(file)) { 1392 if (!fixed) 1393 put_unused_fd(fd); 1394 ret = PTR_ERR(file); 1395 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) 1396 return -EAGAIN; 1397 if (ret == -ERESTARTSYS) 1398 ret = -EINTR; 1399 req_set_fail(req); 1400 } else if (!fixed) { 1401 fd_install(fd, file); 1402 ret = fd; 1403 } else { 1404 ret = io_fixed_fd_install(req, issue_flags, file, 1405 sock->file_slot); 1406 } 1407 io_req_set_res(req, ret, 0); 1408 return IOU_OK; 1409 } 1410 1411 int io_connect_prep_async(struct io_kiocb *req) 1412 { 1413 struct io_async_connect *io = req->async_data; 1414 struct io_connect *conn = io_kiocb_to_cmd(req, struct io_connect); 1415 1416 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); 1417 } 1418 1419 int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) 1420 { 1421 struct io_connect *conn = io_kiocb_to_cmd(req, struct io_connect); 1422 1423 if (sqe->len || sqe->buf_index || sqe->rw_flags || sqe->splice_fd_in) 1424 return -EINVAL; 1425 1426 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); 1427 conn->addr_len = READ_ONCE(sqe->addr2); 1428 conn->in_progress = conn->seen_econnaborted = false; 1429 return 0; 1430 } 1431 1432 int io_connect(struct io_kiocb *req, unsigned int issue_flags) 1433 { 1434 struct io_connect *connect = io_kiocb_to_cmd(req, struct io_connect); 1435 struct io_async_connect __io, *io; 1436 unsigned file_flags; 1437 int ret; 1438 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; 1439 1440 if (connect->in_progress) { 1441 struct socket *socket; 1442 1443 ret = -ENOTSOCK; 1444 socket = sock_from_file(req->file); 1445 if (socket) 1446 ret = sock_error(socket->sk); 1447 goto out; 1448 } 1449 1450 if (req_has_async_data(req)) { 1451 io = req->async_data; 1452 } else { 1453 ret = move_addr_to_kernel(connect->addr, 1454 connect->addr_len, 1455 &__io.address); 1456 if (ret) 1457 goto out; 1458 io = &__io; 1459 } 1460 1461 file_flags = force_nonblock ? O_NONBLOCK : 0; 1462 1463 ret = __sys_connect_file(req->file, &io->address, 1464 connect->addr_len, file_flags); 1465 if ((ret == -EAGAIN || ret == -EINPROGRESS || ret == -ECONNABORTED) 1466 && force_nonblock) { 1467 if (ret == -EINPROGRESS) { 1468 connect->in_progress = true; 1469 return -EAGAIN; 1470 } 1471 if (ret == -ECONNABORTED) { 1472 if (connect->seen_econnaborted) 1473 goto out; 1474 connect->seen_econnaborted = true; 1475 } 1476 if (req_has_async_data(req)) 1477 return -EAGAIN; 1478 if (io_alloc_async_data(req)) { 1479 ret = -ENOMEM; 1480 goto out; 1481 } 1482 memcpy(req->async_data, &__io, sizeof(__io)); 1483 return -EAGAIN; 1484 } 1485 if (ret == -ERESTARTSYS) 1486 ret = -EINTR; 1487 out: 1488 if (ret < 0) 1489 req_set_fail(req); 1490 io_req_set_res(req, ret, 0); 1491 return IOU_OK; 1492 } 1493 1494 void io_netmsg_cache_free(struct io_cache_entry *entry) 1495 { 1496 kfree(container_of(entry, struct io_async_msghdr, cache)); 1497 } 1498 #endif 1499