1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/net/sunrpc/xprtsock.c 4 * 5 * Client-side transport implementation for sockets. 6 * 7 * TCP callback races fixes (C) 1998 Red Hat 8 * TCP send fixes (C) 1998 Red Hat 9 * TCP NFS related read + write fixes 10 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie> 11 * 12 * Rewrite of larges part of the code in order to stabilize TCP stuff. 13 * Fix behaviour when socket buffer is full. 14 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no> 15 * 16 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com> 17 * 18 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005. 19 * <gilles.quillard@bull.net> 20 */ 21 22 #include <linux/types.h> 23 #include <linux/string.h> 24 #include <linux/slab.h> 25 #include <linux/module.h> 26 #include <linux/capability.h> 27 #include <linux/pagemap.h> 28 #include <linux/errno.h> 29 #include <linux/socket.h> 30 #include <linux/in.h> 31 #include <linux/net.h> 32 #include <linux/mm.h> 33 #include <linux/un.h> 34 #include <linux/udp.h> 35 #include <linux/tcp.h> 36 #include <linux/sunrpc/clnt.h> 37 #include <linux/sunrpc/addr.h> 38 #include <linux/sunrpc/sched.h> 39 #include <linux/sunrpc/svcsock.h> 40 #include <linux/sunrpc/xprtsock.h> 41 #include <linux/file.h> 42 #ifdef CONFIG_SUNRPC_BACKCHANNEL 43 #include <linux/sunrpc/bc_xprt.h> 44 #endif 45 46 #include <net/sock.h> 47 #include <net/checksum.h> 48 #include <net/udp.h> 49 #include <net/tcp.h> 50 #include <net/tls_prot.h> 51 #include <net/handshake.h> 52 53 #include <linux/bvec.h> 54 #include <linux/highmem.h> 55 #include <linux/uio.h> 56 #include <linux/sched/mm.h> 57 58 #include <trace/events/sock.h> 59 #include <trace/events/sunrpc.h> 60 61 #include "socklib.h" 62 #include "sunrpc.h" 63 64 static void xs_close(struct rpc_xprt *xprt); 65 static void xs_reset_srcport(struct sock_xprt *transport); 66 static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock); 67 static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 68 struct socket *sock); 69 70 /* 71 * xprtsock tunables 72 */ 73 static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 74 static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE; 75 static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE; 76 77 static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 78 static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 79 80 #define XS_TCP_LINGER_TO (15U * HZ) 81 static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; 82 83 /* 84 * We can register our own files under /proc/sys/sunrpc by 85 * calling register_sysctl() again. The files in that 86 * directory become the union of all files registered there. 87 * 88 * We simply need to make sure that we don't collide with 89 * someone else's file names! 90 */ 91 92 static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 93 static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 94 static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT; 95 static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 96 static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 97 98 static struct ctl_table_header *sunrpc_table_header; 99 100 static struct xprt_class xs_local_transport; 101 static struct xprt_class xs_udp_transport; 102 static struct xprt_class xs_tcp_transport; 103 static struct xprt_class xs_tcp_tls_transport; 104 static struct xprt_class xs_bc_tcp_transport; 105 106 /* 107 * FIXME: changing the UDP slot table size should also resize the UDP 108 * socket buffers for existing UDP transports 109 */ 110 static struct ctl_table xs_tunables_table[] = { 111 { 112 .procname = "udp_slot_table_entries", 113 .data = &xprt_udp_slot_table_entries, 114 .maxlen = sizeof(unsigned int), 115 .mode = 0644, 116 .proc_handler = proc_dointvec_minmax, 117 .extra1 = &min_slot_table_size, 118 .extra2 = &max_slot_table_size 119 }, 120 { 121 .procname = "tcp_slot_table_entries", 122 .data = &xprt_tcp_slot_table_entries, 123 .maxlen = sizeof(unsigned int), 124 .mode = 0644, 125 .proc_handler = proc_dointvec_minmax, 126 .extra1 = &min_slot_table_size, 127 .extra2 = &max_slot_table_size 128 }, 129 { 130 .procname = "tcp_max_slot_table_entries", 131 .data = &xprt_max_tcp_slot_table_entries, 132 .maxlen = sizeof(unsigned int), 133 .mode = 0644, 134 .proc_handler = proc_dointvec_minmax, 135 .extra1 = &min_slot_table_size, 136 .extra2 = &max_tcp_slot_table_limit 137 }, 138 { 139 .procname = "min_resvport", 140 .data = &xprt_min_resvport, 141 .maxlen = sizeof(unsigned int), 142 .mode = 0644, 143 .proc_handler = proc_dointvec_minmax, 144 .extra1 = &xprt_min_resvport_limit, 145 .extra2 = &xprt_max_resvport_limit 146 }, 147 { 148 .procname = "max_resvport", 149 .data = &xprt_max_resvport, 150 .maxlen = sizeof(unsigned int), 151 .mode = 0644, 152 .proc_handler = proc_dointvec_minmax, 153 .extra1 = &xprt_min_resvport_limit, 154 .extra2 = &xprt_max_resvport_limit 155 }, 156 { 157 .procname = "tcp_fin_timeout", 158 .data = &xs_tcp_fin_timeout, 159 .maxlen = sizeof(xs_tcp_fin_timeout), 160 .mode = 0644, 161 .proc_handler = proc_dointvec_jiffies, 162 }, 163 }; 164 165 /* 166 * Wait duration for a reply from the RPC portmapper. 167 */ 168 #define XS_BIND_TO (60U * HZ) 169 170 /* 171 * Delay if a UDP socket connect error occurs. This is most likely some 172 * kind of resource problem on the local host. 173 */ 174 #define XS_UDP_REEST_TO (2U * HZ) 175 176 /* 177 * The reestablish timeout allows clients to delay for a bit before attempting 178 * to reconnect to a server that just dropped our connection. 179 * 180 * We implement an exponential backoff when trying to reestablish a TCP 181 * transport connection with the server. Some servers like to drop a TCP 182 * connection when they are overworked, so we start with a short timeout and 183 * increase over time if the server is down or not responding. 184 */ 185 #define XS_TCP_INIT_REEST_TO (3U * HZ) 186 187 /* 188 * TCP idle timeout; client drops the transport socket if it is idle 189 * for this long. Note that we also timeout UDP sockets to prevent 190 * holding port numbers when there is no RPC traffic. 191 */ 192 #define XS_IDLE_DISC_TO (5U * 60 * HZ) 193 194 /* 195 * TLS handshake timeout. 196 */ 197 #define XS_TLS_HANDSHAKE_TO (10U * HZ) 198 199 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 200 # undef RPC_DEBUG_DATA 201 # define RPCDBG_FACILITY RPCDBG_TRANS 202 #endif 203 204 #ifdef RPC_DEBUG_DATA 205 static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 206 { 207 u8 *buf = (u8 *) packet; 208 int j; 209 210 dprintk("RPC: %s\n", msg); 211 for (j = 0; j < count && j < 128; j += 4) { 212 if (!(j & 31)) { 213 if (j) 214 dprintk("\n"); 215 dprintk("0x%04x ", j); 216 } 217 dprintk("%02x%02x%02x%02x ", 218 buf[j], buf[j+1], buf[j+2], buf[j+3]); 219 } 220 dprintk("\n"); 221 } 222 #else 223 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 224 { 225 /* NOP */ 226 } 227 #endif 228 229 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 230 { 231 return (struct rpc_xprt *) sk->sk_user_data; 232 } 233 234 static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 235 { 236 return (struct sockaddr *) &xprt->addr; 237 } 238 239 static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt) 240 { 241 return (struct sockaddr_un *) &xprt->addr; 242 } 243 244 static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 245 { 246 return (struct sockaddr_in *) &xprt->addr; 247 } 248 249 static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 250 { 251 return (struct sockaddr_in6 *) &xprt->addr; 252 } 253 254 static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) 255 { 256 struct sockaddr *sap = xs_addr(xprt); 257 struct sockaddr_in6 *sin6; 258 struct sockaddr_in *sin; 259 struct sockaddr_un *sun; 260 char buf[128]; 261 262 switch (sap->sa_family) { 263 case AF_LOCAL: 264 sun = xs_addr_un(xprt); 265 if (sun->sun_path[0]) { 266 strscpy(buf, sun->sun_path, sizeof(buf)); 267 } else { 268 buf[0] = '@'; 269 strscpy(buf+1, sun->sun_path+1, sizeof(buf)-1); 270 } 271 xprt->address_strings[RPC_DISPLAY_ADDR] = 272 kstrdup(buf, GFP_KERNEL); 273 break; 274 case AF_INET: 275 (void)rpc_ntop(sap, buf, sizeof(buf)); 276 xprt->address_strings[RPC_DISPLAY_ADDR] = 277 kstrdup(buf, GFP_KERNEL); 278 sin = xs_addr_in(xprt); 279 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); 280 break; 281 case AF_INET6: 282 (void)rpc_ntop(sap, buf, sizeof(buf)); 283 xprt->address_strings[RPC_DISPLAY_ADDR] = 284 kstrdup(buf, GFP_KERNEL); 285 sin6 = xs_addr_in6(xprt); 286 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); 287 break; 288 default: 289 BUG(); 290 } 291 292 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); 293 } 294 295 static void xs_format_common_peer_ports(struct rpc_xprt *xprt) 296 { 297 struct sockaddr *sap = xs_addr(xprt); 298 char buf[128]; 299 300 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); 301 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); 302 303 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); 304 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); 305 } 306 307 static void xs_format_peer_addresses(struct rpc_xprt *xprt, 308 const char *protocol, 309 const char *netid) 310 { 311 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 312 xprt->address_strings[RPC_DISPLAY_NETID] = netid; 313 xs_format_common_peer_addresses(xprt); 314 xs_format_common_peer_ports(xprt); 315 } 316 317 static void xs_update_peer_port(struct rpc_xprt *xprt) 318 { 319 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); 320 kfree(xprt->address_strings[RPC_DISPLAY_PORT]); 321 322 xs_format_common_peer_ports(xprt); 323 } 324 325 static void xs_free_peer_addresses(struct rpc_xprt *xprt) 326 { 327 unsigned int i; 328 329 for (i = 0; i < RPC_DISPLAY_MAX; i++) 330 switch (i) { 331 case RPC_DISPLAY_PROTO: 332 case RPC_DISPLAY_NETID: 333 continue; 334 default: 335 kfree(xprt->address_strings[i]); 336 } 337 } 338 339 static size_t 340 xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp) 341 { 342 size_t i,n; 343 344 if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES)) 345 return want; 346 n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT; 347 for (i = 0; i < n; i++) { 348 if (buf->pages[i]) 349 continue; 350 buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp); 351 if (!buf->pages[i]) { 352 i *= PAGE_SIZE; 353 return i > buf->page_base ? i - buf->page_base : 0; 354 } 355 } 356 return want; 357 } 358 359 static int 360 xs_sock_process_cmsg(struct socket *sock, struct msghdr *msg, 361 struct cmsghdr *cmsg, int ret) 362 { 363 u8 content_type = tls_get_record_type(sock->sk, cmsg); 364 u8 level, description; 365 366 switch (content_type) { 367 case 0: 368 break; 369 case TLS_RECORD_TYPE_DATA: 370 /* TLS sets EOR at the end of each application data 371 * record, even though there might be more frames 372 * waiting to be decrypted. 373 */ 374 msg->msg_flags &= ~MSG_EOR; 375 break; 376 case TLS_RECORD_TYPE_ALERT: 377 tls_alert_recv(sock->sk, msg, &level, &description); 378 ret = (level == TLS_ALERT_LEVEL_FATAL) ? 379 -EACCES : -EAGAIN; 380 break; 381 default: 382 /* discard this record type */ 383 ret = -EAGAIN; 384 } 385 return ret; 386 } 387 388 static int 389 xs_sock_recv_cmsg(struct socket *sock, struct msghdr *msg, int flags) 390 { 391 union { 392 struct cmsghdr cmsg; 393 u8 buf[CMSG_SPACE(sizeof(u8))]; 394 } u; 395 int ret; 396 397 msg->msg_control = &u; 398 msg->msg_controllen = sizeof(u); 399 ret = sock_recvmsg(sock, msg, flags); 400 if (msg->msg_controllen != sizeof(u)) 401 ret = xs_sock_process_cmsg(sock, msg, &u.cmsg, ret); 402 return ret; 403 } 404 405 static ssize_t 406 xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek) 407 { 408 ssize_t ret; 409 if (seek != 0) 410 iov_iter_advance(&msg->msg_iter, seek); 411 ret = xs_sock_recv_cmsg(sock, msg, flags); 412 return ret > 0 ? ret + seek : ret; 413 } 414 415 static ssize_t 416 xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags, 417 struct kvec *kvec, size_t count, size_t seek) 418 { 419 iov_iter_kvec(&msg->msg_iter, ITER_DEST, kvec, 1, count); 420 return xs_sock_recvmsg(sock, msg, flags, seek); 421 } 422 423 static ssize_t 424 xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags, 425 struct bio_vec *bvec, unsigned long nr, size_t count, 426 size_t seek) 427 { 428 iov_iter_bvec(&msg->msg_iter, ITER_DEST, bvec, nr, count); 429 return xs_sock_recvmsg(sock, msg, flags, seek); 430 } 431 432 static ssize_t 433 xs_read_discard(struct socket *sock, struct msghdr *msg, int flags, 434 size_t count) 435 { 436 iov_iter_discard(&msg->msg_iter, ITER_DEST, count); 437 return xs_sock_recv_cmsg(sock, msg, flags); 438 } 439 440 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 441 static void 442 xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 443 { 444 struct bvec_iter bi = { 445 .bi_size = count, 446 }; 447 struct bio_vec bv; 448 449 bvec_iter_advance(bvec, &bi, seek & PAGE_MASK); 450 for_each_bvec(bv, bvec, bi, bi) 451 flush_dcache_page(bv.bv_page); 452 } 453 #else 454 static inline void 455 xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 456 { 457 } 458 #endif 459 460 static ssize_t 461 xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags, 462 struct xdr_buf *buf, size_t count, size_t seek, size_t *read) 463 { 464 size_t want, seek_init = seek, offset = 0; 465 ssize_t ret; 466 467 want = min_t(size_t, count, buf->head[0].iov_len); 468 if (seek < want) { 469 ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek); 470 if (ret <= 0) 471 goto sock_err; 472 offset += ret; 473 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 474 goto out; 475 if (ret != want) 476 goto out; 477 seek = 0; 478 } else { 479 seek -= want; 480 offset += want; 481 } 482 483 want = xs_alloc_sparse_pages( 484 buf, min_t(size_t, count - offset, buf->page_len), 485 GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN); 486 if (seek < want) { 487 ret = xs_read_bvec(sock, msg, flags, buf->bvec, 488 xdr_buf_pagecount(buf), 489 want + buf->page_base, 490 seek + buf->page_base); 491 if (ret <= 0) 492 goto sock_err; 493 xs_flush_bvec(buf->bvec, ret, seek + buf->page_base); 494 ret -= buf->page_base; 495 offset += ret; 496 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 497 goto out; 498 if (ret != want) 499 goto out; 500 seek = 0; 501 } else { 502 seek -= want; 503 offset += want; 504 } 505 506 want = min_t(size_t, count - offset, buf->tail[0].iov_len); 507 if (seek < want) { 508 ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek); 509 if (ret <= 0) 510 goto sock_err; 511 offset += ret; 512 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 513 goto out; 514 if (ret != want) 515 goto out; 516 } else if (offset < seek_init) 517 offset = seek_init; 518 ret = -EMSGSIZE; 519 out: 520 *read = offset - seek_init; 521 return ret; 522 sock_err: 523 offset += seek; 524 goto out; 525 } 526 527 static void 528 xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf) 529 { 530 if (!transport->recv.copied) { 531 if (buf->head[0].iov_len >= transport->recv.offset) 532 memcpy(buf->head[0].iov_base, 533 &transport->recv.xid, 534 transport->recv.offset); 535 transport->recv.copied = transport->recv.offset; 536 } 537 } 538 539 static bool 540 xs_read_stream_request_done(struct sock_xprt *transport) 541 { 542 return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT); 543 } 544 545 static void 546 xs_read_stream_check_eor(struct sock_xprt *transport, 547 struct msghdr *msg) 548 { 549 if (xs_read_stream_request_done(transport)) 550 msg->msg_flags |= MSG_EOR; 551 } 552 553 static ssize_t 554 xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg, 555 int flags, struct rpc_rqst *req) 556 { 557 struct xdr_buf *buf = &req->rq_private_buf; 558 size_t want, read; 559 ssize_t ret; 560 561 xs_read_header(transport, buf); 562 563 want = transport->recv.len - transport->recv.offset; 564 if (want != 0) { 565 ret = xs_read_xdr_buf(transport->sock, msg, flags, buf, 566 transport->recv.copied + want, 567 transport->recv.copied, 568 &read); 569 transport->recv.offset += read; 570 transport->recv.copied += read; 571 } 572 573 if (transport->recv.offset == transport->recv.len) 574 xs_read_stream_check_eor(transport, msg); 575 576 if (want == 0) 577 return 0; 578 579 switch (ret) { 580 default: 581 break; 582 case -EFAULT: 583 case -EMSGSIZE: 584 msg->msg_flags |= MSG_TRUNC; 585 return read; 586 case 0: 587 return -ESHUTDOWN; 588 } 589 return ret < 0 ? ret : read; 590 } 591 592 static size_t 593 xs_read_stream_headersize(bool isfrag) 594 { 595 if (isfrag) 596 return sizeof(__be32); 597 return 3 * sizeof(__be32); 598 } 599 600 static ssize_t 601 xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg, 602 int flags, size_t want, size_t seek) 603 { 604 struct kvec kvec = { 605 .iov_base = &transport->recv.fraghdr, 606 .iov_len = want, 607 }; 608 return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek); 609 } 610 611 #if defined(CONFIG_SUNRPC_BACKCHANNEL) 612 static ssize_t 613 xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 614 { 615 struct rpc_xprt *xprt = &transport->xprt; 616 struct rpc_rqst *req; 617 ssize_t ret; 618 619 /* Is this transport associated with the backchannel? */ 620 if (!xprt->bc_serv) 621 return -ESHUTDOWN; 622 623 /* Look up and lock the request corresponding to the given XID */ 624 req = xprt_lookup_bc_request(xprt, transport->recv.xid); 625 if (!req) { 626 printk(KERN_WARNING "Callback slot table overflowed\n"); 627 return -ESHUTDOWN; 628 } 629 if (transport->recv.copied && !req->rq_private_buf.len) 630 return -ESHUTDOWN; 631 632 ret = xs_read_stream_request(transport, msg, flags, req); 633 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 634 xprt_complete_bc_request(req, transport->recv.copied); 635 else 636 req->rq_private_buf.len = transport->recv.copied; 637 638 return ret; 639 } 640 #else /* CONFIG_SUNRPC_BACKCHANNEL */ 641 static ssize_t 642 xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 643 { 644 return -ESHUTDOWN; 645 } 646 #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 647 648 static ssize_t 649 xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags) 650 { 651 struct rpc_xprt *xprt = &transport->xprt; 652 struct rpc_rqst *req; 653 ssize_t ret = 0; 654 655 /* Look up and lock the request corresponding to the given XID */ 656 spin_lock(&xprt->queue_lock); 657 req = xprt_lookup_rqst(xprt, transport->recv.xid); 658 if (!req || (transport->recv.copied && !req->rq_private_buf.len)) { 659 msg->msg_flags |= MSG_TRUNC; 660 goto out; 661 } 662 xprt_pin_rqst(req); 663 spin_unlock(&xprt->queue_lock); 664 665 ret = xs_read_stream_request(transport, msg, flags, req); 666 667 spin_lock(&xprt->queue_lock); 668 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 669 xprt_complete_rqst(req->rq_task, transport->recv.copied); 670 else 671 req->rq_private_buf.len = transport->recv.copied; 672 xprt_unpin_rqst(req); 673 out: 674 spin_unlock(&xprt->queue_lock); 675 return ret; 676 } 677 678 static ssize_t 679 xs_read_stream(struct sock_xprt *transport, int flags) 680 { 681 struct msghdr msg = { 0 }; 682 size_t want, read = 0; 683 ssize_t ret = 0; 684 685 if (transport->recv.len == 0) { 686 want = xs_read_stream_headersize(transport->recv.copied != 0); 687 ret = xs_read_stream_header(transport, &msg, flags, want, 688 transport->recv.offset); 689 if (ret <= 0) 690 goto out_err; 691 transport->recv.offset = ret; 692 if (transport->recv.offset != want) 693 return transport->recv.offset; 694 transport->recv.len = be32_to_cpu(transport->recv.fraghdr) & 695 RPC_FRAGMENT_SIZE_MASK; 696 transport->recv.offset -= sizeof(transport->recv.fraghdr); 697 read = ret; 698 } 699 700 switch (be32_to_cpu(transport->recv.calldir)) { 701 default: 702 msg.msg_flags |= MSG_TRUNC; 703 break; 704 case RPC_CALL: 705 ret = xs_read_stream_call(transport, &msg, flags); 706 break; 707 case RPC_REPLY: 708 ret = xs_read_stream_reply(transport, &msg, flags); 709 } 710 if (msg.msg_flags & MSG_TRUNC) { 711 transport->recv.calldir = cpu_to_be32(-1); 712 transport->recv.copied = -1; 713 } 714 if (ret < 0) 715 goto out_err; 716 read += ret; 717 if (transport->recv.offset < transport->recv.len) { 718 if (!(msg.msg_flags & MSG_TRUNC)) 719 return read; 720 msg.msg_flags = 0; 721 ret = xs_read_discard(transport->sock, &msg, flags, 722 transport->recv.len - transport->recv.offset); 723 if (ret <= 0) 724 goto out_err; 725 transport->recv.offset += ret; 726 read += ret; 727 if (transport->recv.offset != transport->recv.len) 728 return read; 729 } 730 if (xs_read_stream_request_done(transport)) { 731 trace_xs_stream_read_request(transport); 732 transport->recv.copied = 0; 733 } 734 transport->recv.offset = 0; 735 transport->recv.len = 0; 736 return read; 737 out_err: 738 return ret != 0 ? ret : -ESHUTDOWN; 739 } 740 741 static __poll_t xs_poll_socket(struct sock_xprt *transport) 742 { 743 return transport->sock->ops->poll(transport->file, transport->sock, 744 NULL); 745 } 746 747 static bool xs_poll_socket_readable(struct sock_xprt *transport) 748 { 749 __poll_t events = xs_poll_socket(transport); 750 751 return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP); 752 } 753 754 static void xs_poll_check_readable(struct sock_xprt *transport) 755 { 756 757 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 758 if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state)) 759 return; 760 if (!xs_poll_socket_readable(transport)) 761 return; 762 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 763 queue_work(xprtiod_workqueue, &transport->recv_worker); 764 } 765 766 static void xs_stream_data_receive(struct sock_xprt *transport) 767 { 768 size_t read = 0; 769 ssize_t ret = 0; 770 771 mutex_lock(&transport->recv_mutex); 772 if (transport->sock == NULL) 773 goto out; 774 for (;;) { 775 ret = xs_read_stream(transport, MSG_DONTWAIT); 776 if (ret < 0) 777 break; 778 read += ret; 779 cond_resched(); 780 } 781 if (ret == -ESHUTDOWN) 782 kernel_sock_shutdown(transport->sock, SHUT_RDWR); 783 else if (ret == -EACCES) 784 xprt_wake_pending_tasks(&transport->xprt, -EACCES); 785 else 786 xs_poll_check_readable(transport); 787 out: 788 mutex_unlock(&transport->recv_mutex); 789 trace_xs_stream_read_data(&transport->xprt, ret, read); 790 } 791 792 static void xs_stream_data_receive_workfn(struct work_struct *work) 793 { 794 struct sock_xprt *transport = 795 container_of(work, struct sock_xprt, recv_worker); 796 unsigned int pflags = memalloc_nofs_save(); 797 798 xs_stream_data_receive(transport); 799 memalloc_nofs_restore(pflags); 800 } 801 802 static void 803 xs_stream_reset_connect(struct sock_xprt *transport) 804 { 805 transport->recv.offset = 0; 806 transport->recv.len = 0; 807 transport->recv.copied = 0; 808 transport->xmit.offset = 0; 809 } 810 811 static void 812 xs_stream_start_connect(struct sock_xprt *transport) 813 { 814 transport->xprt.stat.connect_count++; 815 transport->xprt.stat.connect_start = jiffies; 816 } 817 818 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 819 820 /** 821 * xs_nospace - handle transmit was incomplete 822 * @req: pointer to RPC request 823 * @transport: pointer to struct sock_xprt 824 * 825 */ 826 static int xs_nospace(struct rpc_rqst *req, struct sock_xprt *transport) 827 { 828 struct rpc_xprt *xprt = &transport->xprt; 829 struct sock *sk = transport->inet; 830 int ret = -EAGAIN; 831 832 trace_rpc_socket_nospace(req, transport); 833 834 /* Protect against races with write_space */ 835 spin_lock(&xprt->transport_lock); 836 837 /* Don't race with disconnect */ 838 if (xprt_connected(xprt)) { 839 /* wait for more buffer space */ 840 set_bit(XPRT_SOCK_NOSPACE, &transport->sock_state); 841 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 842 sk->sk_write_pending++; 843 xprt_wait_for_buffer_space(xprt); 844 } else 845 ret = -ENOTCONN; 846 847 spin_unlock(&xprt->transport_lock); 848 return ret; 849 } 850 851 static int xs_sock_nospace(struct rpc_rqst *req) 852 { 853 struct sock_xprt *transport = 854 container_of(req->rq_xprt, struct sock_xprt, xprt); 855 struct sock *sk = transport->inet; 856 int ret = -EAGAIN; 857 858 lock_sock(sk); 859 if (!sock_writeable(sk)) 860 ret = xs_nospace(req, transport); 861 release_sock(sk); 862 return ret; 863 } 864 865 static int xs_stream_nospace(struct rpc_rqst *req, bool vm_wait) 866 { 867 struct sock_xprt *transport = 868 container_of(req->rq_xprt, struct sock_xprt, xprt); 869 struct sock *sk = transport->inet; 870 int ret = -EAGAIN; 871 872 if (vm_wait) 873 return -ENOBUFS; 874 lock_sock(sk); 875 if (!sk_stream_memory_free(sk)) 876 ret = xs_nospace(req, transport); 877 release_sock(sk); 878 return ret; 879 } 880 881 static int xs_stream_prepare_request(struct rpc_rqst *req, struct xdr_buf *buf) 882 { 883 return xdr_alloc_bvec(buf, rpc_task_gfp_mask()); 884 } 885 886 static void xs_stream_abort_send_request(struct rpc_rqst *req) 887 { 888 struct rpc_xprt *xprt = req->rq_xprt; 889 struct sock_xprt *transport = 890 container_of(xprt, struct sock_xprt, xprt); 891 892 if (transport->xmit.offset != 0 && 893 !test_bit(XPRT_CLOSE_WAIT, &xprt->state)) 894 xprt_force_disconnect(xprt); 895 } 896 897 /* 898 * Determine if the previous message in the stream was aborted before it 899 * could complete transmission. 900 */ 901 static bool 902 xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req) 903 { 904 return transport->xmit.offset != 0 && req->rq_bytes_sent == 0; 905 } 906 907 /* 908 * Return the stream record marker field for a record of length < 2^31-1 909 */ 910 static rpc_fraghdr 911 xs_stream_record_marker(struct xdr_buf *xdr) 912 { 913 if (!xdr->len) 914 return 0; 915 return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len); 916 } 917 918 /** 919 * xs_local_send_request - write an RPC request to an AF_LOCAL socket 920 * @req: pointer to RPC request 921 * 922 * Return values: 923 * 0: The request has been sent 924 * EAGAIN: The socket was blocked, please call again later to 925 * complete the request 926 * ENOTCONN: Caller needs to invoke connect logic then call again 927 * other: Some other error occurred, the request was not sent 928 */ 929 static int xs_local_send_request(struct rpc_rqst *req) 930 { 931 struct rpc_xprt *xprt = req->rq_xprt; 932 struct sock_xprt *transport = 933 container_of(xprt, struct sock_xprt, xprt); 934 struct xdr_buf *xdr = &req->rq_snd_buf; 935 rpc_fraghdr rm = xs_stream_record_marker(xdr); 936 unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; 937 struct msghdr msg = { 938 .msg_flags = XS_SENDMSG_FLAGS, 939 }; 940 bool vm_wait; 941 unsigned int sent; 942 int status; 943 944 /* Close the stream if the previous transmission was incomplete */ 945 if (xs_send_request_was_aborted(transport, req)) { 946 xprt_force_disconnect(xprt); 947 return -ENOTCONN; 948 } 949 950 xs_pktdump("packet data:", 951 req->rq_svec->iov_base, req->rq_svec->iov_len); 952 953 vm_wait = sk_stream_is_writeable(transport->inet) ? true : false; 954 955 req->rq_xtime = ktime_get(); 956 status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 957 transport->xmit.offset, rm, &sent); 958 dprintk("RPC: %s(%u) = %d\n", 959 __func__, xdr->len - transport->xmit.offset, status); 960 961 if (likely(sent > 0) || status == 0) { 962 transport->xmit.offset += sent; 963 req->rq_bytes_sent = transport->xmit.offset; 964 if (likely(req->rq_bytes_sent >= msglen)) { 965 req->rq_xmit_bytes_sent += transport->xmit.offset; 966 transport->xmit.offset = 0; 967 return 0; 968 } 969 status = -EAGAIN; 970 vm_wait = false; 971 } 972 973 switch (status) { 974 case -EAGAIN: 975 status = xs_stream_nospace(req, vm_wait); 976 break; 977 default: 978 dprintk("RPC: sendmsg returned unrecognized error %d\n", 979 -status); 980 fallthrough; 981 case -EPIPE: 982 xprt_force_disconnect(xprt); 983 status = -ENOTCONN; 984 } 985 986 return status; 987 } 988 989 /** 990 * xs_udp_send_request - write an RPC request to a UDP socket 991 * @req: pointer to RPC request 992 * 993 * Return values: 994 * 0: The request has been sent 995 * EAGAIN: The socket was blocked, please call again later to 996 * complete the request 997 * ENOTCONN: Caller needs to invoke connect logic then call again 998 * other: Some other error occurred, the request was not sent 999 */ 1000 static int xs_udp_send_request(struct rpc_rqst *req) 1001 { 1002 struct rpc_xprt *xprt = req->rq_xprt; 1003 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1004 struct xdr_buf *xdr = &req->rq_snd_buf; 1005 struct msghdr msg = { 1006 .msg_name = xs_addr(xprt), 1007 .msg_namelen = xprt->addrlen, 1008 .msg_flags = XS_SENDMSG_FLAGS, 1009 }; 1010 unsigned int sent; 1011 int status; 1012 1013 xs_pktdump("packet data:", 1014 req->rq_svec->iov_base, 1015 req->rq_svec->iov_len); 1016 1017 if (!xprt_bound(xprt)) 1018 return -ENOTCONN; 1019 1020 if (!xprt_request_get_cong(xprt, req)) 1021 return -EBADSLT; 1022 1023 status = xdr_alloc_bvec(xdr, rpc_task_gfp_mask()); 1024 if (status < 0) 1025 return status; 1026 req->rq_xtime = ktime_get(); 1027 status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent); 1028 1029 dprintk("RPC: xs_udp_send_request(%u) = %d\n", 1030 xdr->len, status); 1031 1032 /* firewall is blocking us, don't return -EAGAIN or we end up looping */ 1033 if (status == -EPERM) 1034 goto process_status; 1035 1036 if (status == -EAGAIN && sock_writeable(transport->inet)) 1037 status = -ENOBUFS; 1038 1039 if (sent > 0 || status == 0) { 1040 req->rq_xmit_bytes_sent += sent; 1041 if (sent >= req->rq_slen) 1042 return 0; 1043 /* Still some bytes left; set up for a retry later. */ 1044 status = -EAGAIN; 1045 } 1046 1047 process_status: 1048 switch (status) { 1049 case -ENOTSOCK: 1050 status = -ENOTCONN; 1051 /* Should we call xs_close() here? */ 1052 break; 1053 case -EAGAIN: 1054 status = xs_sock_nospace(req); 1055 break; 1056 case -ENETUNREACH: 1057 case -ENOBUFS: 1058 case -EPIPE: 1059 case -ECONNREFUSED: 1060 case -EPERM: 1061 /* When the server has died, an ICMP port unreachable message 1062 * prompts ECONNREFUSED. */ 1063 break; 1064 default: 1065 dprintk("RPC: sendmsg returned unrecognized error %d\n", 1066 -status); 1067 } 1068 1069 return status; 1070 } 1071 1072 /** 1073 * xs_tcp_send_request - write an RPC request to a TCP socket 1074 * @req: pointer to RPC request 1075 * 1076 * Return values: 1077 * 0: The request has been sent 1078 * EAGAIN: The socket was blocked, please call again later to 1079 * complete the request 1080 * ENOTCONN: Caller needs to invoke connect logic then call again 1081 * other: Some other error occurred, the request was not sent 1082 * 1083 * XXX: In the case of soft timeouts, should we eventually give up 1084 * if sendmsg is not able to make progress? 1085 */ 1086 static int xs_tcp_send_request(struct rpc_rqst *req) 1087 { 1088 struct rpc_xprt *xprt = req->rq_xprt; 1089 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1090 struct xdr_buf *xdr = &req->rq_snd_buf; 1091 rpc_fraghdr rm = xs_stream_record_marker(xdr); 1092 unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; 1093 struct msghdr msg = { 1094 .msg_flags = XS_SENDMSG_FLAGS, 1095 }; 1096 bool vm_wait; 1097 unsigned int sent; 1098 int status; 1099 1100 /* Close the stream if the previous transmission was incomplete */ 1101 if (xs_send_request_was_aborted(transport, req)) { 1102 if (transport->sock != NULL) 1103 kernel_sock_shutdown(transport->sock, SHUT_RDWR); 1104 return -ENOTCONN; 1105 } 1106 if (!transport->inet) 1107 return -ENOTCONN; 1108 1109 xs_pktdump("packet data:", 1110 req->rq_svec->iov_base, 1111 req->rq_svec->iov_len); 1112 1113 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state)) 1114 xs_tcp_set_socket_timeouts(xprt, transport->sock); 1115 1116 xs_set_srcport(transport, transport->sock); 1117 1118 /* Continue transmitting the packet/record. We must be careful 1119 * to cope with writespace callbacks arriving _after_ we have 1120 * called sendmsg(). */ 1121 req->rq_xtime = ktime_get(); 1122 tcp_sock_set_cork(transport->inet, true); 1123 1124 vm_wait = sk_stream_is_writeable(transport->inet) ? true : false; 1125 1126 do { 1127 status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 1128 transport->xmit.offset, rm, &sent); 1129 1130 dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 1131 xdr->len - transport->xmit.offset, status); 1132 1133 /* If we've sent the entire packet, immediately 1134 * reset the count of bytes sent. */ 1135 transport->xmit.offset += sent; 1136 req->rq_bytes_sent = transport->xmit.offset; 1137 if (likely(req->rq_bytes_sent >= msglen)) { 1138 req->rq_xmit_bytes_sent += transport->xmit.offset; 1139 transport->xmit.offset = 0; 1140 if (atomic_long_read(&xprt->xmit_queuelen) == 1) 1141 tcp_sock_set_cork(transport->inet, false); 1142 return 0; 1143 } 1144 1145 WARN_ON_ONCE(sent == 0 && status == 0); 1146 1147 if (sent > 0) 1148 vm_wait = false; 1149 1150 } while (status == 0); 1151 1152 switch (status) { 1153 case -ENOTSOCK: 1154 status = -ENOTCONN; 1155 /* Should we call xs_close() here? */ 1156 break; 1157 case -EAGAIN: 1158 status = xs_stream_nospace(req, vm_wait); 1159 break; 1160 case -ECONNRESET: 1161 case -ECONNREFUSED: 1162 case -ENOTCONN: 1163 case -EADDRINUSE: 1164 case -ENOBUFS: 1165 case -EPIPE: 1166 break; 1167 default: 1168 dprintk("RPC: sendmsg returned unrecognized error %d\n", 1169 -status); 1170 } 1171 1172 return status; 1173 } 1174 1175 static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 1176 { 1177 transport->old_data_ready = sk->sk_data_ready; 1178 transport->old_state_change = sk->sk_state_change; 1179 transport->old_write_space = sk->sk_write_space; 1180 transport->old_error_report = sk->sk_error_report; 1181 } 1182 1183 static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 1184 { 1185 sk->sk_data_ready = transport->old_data_ready; 1186 sk->sk_state_change = transport->old_state_change; 1187 sk->sk_write_space = transport->old_write_space; 1188 sk->sk_error_report = transport->old_error_report; 1189 } 1190 1191 static void xs_sock_reset_state_flags(struct rpc_xprt *xprt) 1192 { 1193 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1194 1195 transport->xprt_err = 0; 1196 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 1197 clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state); 1198 clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state); 1199 clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state); 1200 clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state); 1201 } 1202 1203 static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr) 1204 { 1205 set_bit(nr, &transport->sock_state); 1206 queue_work(xprtiod_workqueue, &transport->error_worker); 1207 } 1208 1209 static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) 1210 { 1211 xprt->connect_cookie++; 1212 smp_mb__before_atomic(); 1213 clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 1214 clear_bit(XPRT_CLOSING, &xprt->state); 1215 xs_sock_reset_state_flags(xprt); 1216 smp_mb__after_atomic(); 1217 } 1218 1219 /** 1220 * xs_error_report - callback to handle TCP socket state errors 1221 * @sk: socket 1222 * 1223 * Note: we don't call sock_error() since there may be a rpc_task 1224 * using the socket, and so we don't want to clear sk->sk_err. 1225 */ 1226 static void xs_error_report(struct sock *sk) 1227 { 1228 struct sock_xprt *transport; 1229 struct rpc_xprt *xprt; 1230 1231 if (!(xprt = xprt_from_sock(sk))) 1232 return; 1233 1234 transport = container_of(xprt, struct sock_xprt, xprt); 1235 transport->xprt_err = -sk->sk_err; 1236 if (transport->xprt_err == 0) 1237 return; 1238 dprintk("RPC: xs_error_report client %p, error=%d...\n", 1239 xprt, -transport->xprt_err); 1240 trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err); 1241 1242 /* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */ 1243 smp_mb__before_atomic(); 1244 xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR); 1245 } 1246 1247 static void xs_reset_transport(struct sock_xprt *transport) 1248 { 1249 struct socket *sock = transport->sock; 1250 struct sock *sk = transport->inet; 1251 struct rpc_xprt *xprt = &transport->xprt; 1252 struct file *filp = transport->file; 1253 1254 if (sk == NULL) 1255 return; 1256 /* 1257 * Make sure we're calling this in a context from which it is safe 1258 * to call __fput_sync(). In practice that means rpciod and the 1259 * system workqueue. 1260 */ 1261 if (!(current->flags & PF_WQ_WORKER)) { 1262 WARN_ON_ONCE(1); 1263 set_bit(XPRT_CLOSE_WAIT, &xprt->state); 1264 return; 1265 } 1266 1267 if (atomic_read(&transport->xprt.swapper)) 1268 sk_clear_memalloc(sk); 1269 1270 tls_handshake_cancel(sk); 1271 1272 kernel_sock_shutdown(sock, SHUT_RDWR); 1273 1274 mutex_lock(&transport->recv_mutex); 1275 lock_sock(sk); 1276 transport->inet = NULL; 1277 transport->sock = NULL; 1278 transport->file = NULL; 1279 1280 sk->sk_user_data = NULL; 1281 1282 xs_restore_old_callbacks(transport, sk); 1283 xprt_clear_connected(xprt); 1284 xs_sock_reset_connection_flags(xprt); 1285 /* Reset stream record info */ 1286 xs_stream_reset_connect(transport); 1287 release_sock(sk); 1288 mutex_unlock(&transport->recv_mutex); 1289 1290 trace_rpc_socket_close(xprt, sock); 1291 __fput_sync(filp); 1292 1293 xprt_disconnect_done(xprt); 1294 } 1295 1296 /** 1297 * xs_close - close a socket 1298 * @xprt: transport 1299 * 1300 * This is used when all requests are complete; ie, no DRC state remains 1301 * on the server we want to save. 1302 * 1303 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 1304 * xs_reset_transport() zeroing the socket from underneath a writer. 1305 */ 1306 static void xs_close(struct rpc_xprt *xprt) 1307 { 1308 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1309 1310 dprintk("RPC: xs_close xprt %p\n", xprt); 1311 1312 if (transport->sock) 1313 tls_handshake_close(transport->sock); 1314 xs_reset_transport(transport); 1315 xprt->reestablish_timeout = 0; 1316 } 1317 1318 static void xs_inject_disconnect(struct rpc_xprt *xprt) 1319 { 1320 dprintk("RPC: injecting transport disconnect on xprt=%p\n", 1321 xprt); 1322 xprt_disconnect_done(xprt); 1323 } 1324 1325 static void xs_xprt_free(struct rpc_xprt *xprt) 1326 { 1327 xs_free_peer_addresses(xprt); 1328 xprt_free(xprt); 1329 } 1330 1331 /** 1332 * xs_destroy - prepare to shutdown a transport 1333 * @xprt: doomed transport 1334 * 1335 */ 1336 static void xs_destroy(struct rpc_xprt *xprt) 1337 { 1338 struct sock_xprt *transport = container_of(xprt, 1339 struct sock_xprt, xprt); 1340 dprintk("RPC: xs_destroy xprt %p\n", xprt); 1341 1342 cancel_delayed_work_sync(&transport->connect_worker); 1343 xs_close(xprt); 1344 cancel_work_sync(&transport->recv_worker); 1345 cancel_work_sync(&transport->error_worker); 1346 xs_xprt_free(xprt); 1347 module_put(THIS_MODULE); 1348 } 1349 1350 /** 1351 * xs_udp_data_read_skb - receive callback for UDP sockets 1352 * @xprt: transport 1353 * @sk: socket 1354 * @skb: skbuff 1355 * 1356 */ 1357 static void xs_udp_data_read_skb(struct rpc_xprt *xprt, 1358 struct sock *sk, 1359 struct sk_buff *skb) 1360 { 1361 struct rpc_task *task; 1362 struct rpc_rqst *rovr; 1363 int repsize, copied; 1364 u32 _xid; 1365 __be32 *xp; 1366 1367 repsize = skb->len; 1368 if (repsize < 4) { 1369 dprintk("RPC: impossible RPC reply size %d!\n", repsize); 1370 return; 1371 } 1372 1373 /* Copy the XID from the skb... */ 1374 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); 1375 if (xp == NULL) 1376 return; 1377 1378 /* Look up and lock the request corresponding to the given XID */ 1379 spin_lock(&xprt->queue_lock); 1380 rovr = xprt_lookup_rqst(xprt, *xp); 1381 if (!rovr) 1382 goto out_unlock; 1383 xprt_pin_rqst(rovr); 1384 xprt_update_rtt(rovr->rq_task); 1385 spin_unlock(&xprt->queue_lock); 1386 task = rovr->rq_task; 1387 1388 if ((copied = rovr->rq_private_buf.buflen) > repsize) 1389 copied = repsize; 1390 1391 /* Suck it into the iovec, verify checksum if not done by hw. */ 1392 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 1393 spin_lock(&xprt->queue_lock); 1394 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS); 1395 goto out_unpin; 1396 } 1397 1398 1399 spin_lock(&xprt->transport_lock); 1400 xprt_adjust_cwnd(xprt, task, copied); 1401 spin_unlock(&xprt->transport_lock); 1402 spin_lock(&xprt->queue_lock); 1403 xprt_complete_rqst(task, copied); 1404 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS); 1405 out_unpin: 1406 xprt_unpin_rqst(rovr); 1407 out_unlock: 1408 spin_unlock(&xprt->queue_lock); 1409 } 1410 1411 static void xs_udp_data_receive(struct sock_xprt *transport) 1412 { 1413 struct sk_buff *skb; 1414 struct sock *sk; 1415 int err; 1416 1417 mutex_lock(&transport->recv_mutex); 1418 sk = transport->inet; 1419 if (sk == NULL) 1420 goto out; 1421 for (;;) { 1422 skb = skb_recv_udp(sk, MSG_DONTWAIT, &err); 1423 if (skb == NULL) 1424 break; 1425 xs_udp_data_read_skb(&transport->xprt, sk, skb); 1426 consume_skb(skb); 1427 cond_resched(); 1428 } 1429 xs_poll_check_readable(transport); 1430 out: 1431 mutex_unlock(&transport->recv_mutex); 1432 } 1433 1434 static void xs_udp_data_receive_workfn(struct work_struct *work) 1435 { 1436 struct sock_xprt *transport = 1437 container_of(work, struct sock_xprt, recv_worker); 1438 unsigned int pflags = memalloc_nofs_save(); 1439 1440 xs_udp_data_receive(transport); 1441 memalloc_nofs_restore(pflags); 1442 } 1443 1444 /** 1445 * xs_data_ready - "data ready" callback for sockets 1446 * @sk: socket with data to read 1447 * 1448 */ 1449 static void xs_data_ready(struct sock *sk) 1450 { 1451 struct rpc_xprt *xprt; 1452 1453 trace_sk_data_ready(sk); 1454 1455 xprt = xprt_from_sock(sk); 1456 if (xprt != NULL) { 1457 struct sock_xprt *transport = container_of(xprt, 1458 struct sock_xprt, xprt); 1459 1460 trace_xs_data_ready(xprt); 1461 1462 transport->old_data_ready(sk); 1463 1464 if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state)) 1465 return; 1466 1467 /* Any data means we had a useful conversation, so 1468 * then we don't need to delay the next reconnect 1469 */ 1470 if (xprt->reestablish_timeout) 1471 xprt->reestablish_timeout = 0; 1472 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 1473 queue_work(xprtiod_workqueue, &transport->recv_worker); 1474 } 1475 } 1476 1477 /* 1478 * Helper function to force a TCP close if the server is sending 1479 * junk and/or it has put us in CLOSE_WAIT 1480 */ 1481 static void xs_tcp_force_close(struct rpc_xprt *xprt) 1482 { 1483 xprt_force_disconnect(xprt); 1484 } 1485 1486 #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1487 static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt) 1488 { 1489 return PAGE_SIZE; 1490 } 1491 #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 1492 1493 /** 1494 * xs_local_state_change - callback to handle AF_LOCAL socket state changes 1495 * @sk: socket whose state has changed 1496 * 1497 */ 1498 static void xs_local_state_change(struct sock *sk) 1499 { 1500 struct rpc_xprt *xprt; 1501 struct sock_xprt *transport; 1502 1503 if (!(xprt = xprt_from_sock(sk))) 1504 return; 1505 transport = container_of(xprt, struct sock_xprt, xprt); 1506 if (sk->sk_shutdown & SHUTDOWN_MASK) { 1507 clear_bit(XPRT_CONNECTED, &xprt->state); 1508 /* Trigger the socket release */ 1509 xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1510 } 1511 } 1512 1513 /** 1514 * xs_tcp_state_change - callback to handle TCP socket state changes 1515 * @sk: socket whose state has changed 1516 * 1517 */ 1518 static void xs_tcp_state_change(struct sock *sk) 1519 { 1520 struct rpc_xprt *xprt; 1521 struct sock_xprt *transport; 1522 1523 if (!(xprt = xprt_from_sock(sk))) 1524 return; 1525 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1526 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1527 sk->sk_state, xprt_connected(xprt), 1528 sock_flag(sk, SOCK_DEAD), 1529 sock_flag(sk, SOCK_ZAPPED), 1530 sk->sk_shutdown); 1531 1532 transport = container_of(xprt, struct sock_xprt, xprt); 1533 trace_rpc_socket_state_change(xprt, sk->sk_socket); 1534 switch (sk->sk_state) { 1535 case TCP_ESTABLISHED: 1536 if (!xprt_test_and_set_connected(xprt)) { 1537 xprt->connect_cookie++; 1538 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 1539 xprt_clear_connecting(xprt); 1540 1541 xprt->stat.connect_count++; 1542 xprt->stat.connect_time += (long)jiffies - 1543 xprt->stat.connect_start; 1544 xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING); 1545 } 1546 break; 1547 case TCP_FIN_WAIT1: 1548 /* The client initiated a shutdown of the socket */ 1549 xprt->connect_cookie++; 1550 xprt->reestablish_timeout = 0; 1551 set_bit(XPRT_CLOSING, &xprt->state); 1552 smp_mb__before_atomic(); 1553 clear_bit(XPRT_CONNECTED, &xprt->state); 1554 clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 1555 smp_mb__after_atomic(); 1556 break; 1557 case TCP_CLOSE_WAIT: 1558 /* The server initiated a shutdown of the socket */ 1559 xprt->connect_cookie++; 1560 clear_bit(XPRT_CONNECTED, &xprt->state); 1561 xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1562 fallthrough; 1563 case TCP_CLOSING: 1564 /* 1565 * If the server closed down the connection, make sure that 1566 * we back off before reconnecting 1567 */ 1568 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1569 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 1570 break; 1571 case TCP_LAST_ACK: 1572 set_bit(XPRT_CLOSING, &xprt->state); 1573 smp_mb__before_atomic(); 1574 clear_bit(XPRT_CONNECTED, &xprt->state); 1575 smp_mb__after_atomic(); 1576 break; 1577 case TCP_CLOSE: 1578 if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 1579 &transport->sock_state)) { 1580 xs_reset_srcport(transport); 1581 xprt_clear_connecting(xprt); 1582 } 1583 clear_bit(XPRT_CLOSING, &xprt->state); 1584 /* Trigger the socket release */ 1585 xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); 1586 } 1587 } 1588 1589 static void xs_write_space(struct sock *sk) 1590 { 1591 struct sock_xprt *transport; 1592 struct rpc_xprt *xprt; 1593 1594 if (!sk->sk_socket) 1595 return; 1596 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 1597 1598 if (unlikely(!(xprt = xprt_from_sock(sk)))) 1599 return; 1600 transport = container_of(xprt, struct sock_xprt, xprt); 1601 if (!test_and_clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state)) 1602 return; 1603 xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE); 1604 sk->sk_write_pending--; 1605 } 1606 1607 /** 1608 * xs_udp_write_space - callback invoked when socket buffer space 1609 * becomes available 1610 * @sk: socket whose state has changed 1611 * 1612 * Called when more output buffer space is available for this socket. 1613 * We try not to wake our writers until they can make "significant" 1614 * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1615 * with a bunch of small requests. 1616 */ 1617 static void xs_udp_write_space(struct sock *sk) 1618 { 1619 /* from net/core/sock.c:sock_def_write_space */ 1620 if (sock_writeable(sk)) 1621 xs_write_space(sk); 1622 } 1623 1624 /** 1625 * xs_tcp_write_space - callback invoked when socket buffer space 1626 * becomes available 1627 * @sk: socket whose state has changed 1628 * 1629 * Called when more output buffer space is available for this socket. 1630 * We try not to wake our writers until they can make "significant" 1631 * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1632 * with a bunch of small requests. 1633 */ 1634 static void xs_tcp_write_space(struct sock *sk) 1635 { 1636 /* from net/core/stream.c:sk_stream_write_space */ 1637 if (sk_stream_is_writeable(sk)) 1638 xs_write_space(sk); 1639 } 1640 1641 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1642 { 1643 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1644 struct sock *sk = transport->inet; 1645 1646 if (transport->rcvsize) { 1647 sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 1648 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1649 } 1650 if (transport->sndsize) { 1651 sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 1652 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1653 sk->sk_write_space(sk); 1654 } 1655 } 1656 1657 /** 1658 * xs_udp_set_buffer_size - set send and receive limits 1659 * @xprt: generic transport 1660 * @sndsize: requested size of send buffer, in bytes 1661 * @rcvsize: requested size of receive buffer, in bytes 1662 * 1663 * Set socket send and receive buffer size limits. 1664 */ 1665 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 1666 { 1667 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1668 1669 transport->sndsize = 0; 1670 if (sndsize) 1671 transport->sndsize = sndsize + 1024; 1672 transport->rcvsize = 0; 1673 if (rcvsize) 1674 transport->rcvsize = rcvsize + 1024; 1675 1676 xs_udp_do_set_buffer_size(xprt); 1677 } 1678 1679 /** 1680 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 1681 * @xprt: controlling transport 1682 * @task: task that timed out 1683 * 1684 * Adjust the congestion window after a retransmit timeout has occurred. 1685 */ 1686 static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 1687 { 1688 spin_lock(&xprt->transport_lock); 1689 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 1690 spin_unlock(&xprt->transport_lock); 1691 } 1692 1693 static int xs_get_random_port(void) 1694 { 1695 unsigned short min = xprt_min_resvport, max = xprt_max_resvport; 1696 unsigned short range; 1697 unsigned short rand; 1698 1699 if (max < min) 1700 return -EADDRINUSE; 1701 range = max - min + 1; 1702 rand = get_random_u32_below(range); 1703 return rand + min; 1704 } 1705 1706 static unsigned short xs_sock_getport(struct socket *sock) 1707 { 1708 struct sockaddr_storage buf; 1709 unsigned short port = 0; 1710 1711 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0) 1712 goto out; 1713 switch (buf.ss_family) { 1714 case AF_INET6: 1715 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 1716 break; 1717 case AF_INET: 1718 port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 1719 } 1720 out: 1721 return port; 1722 } 1723 1724 /** 1725 * xs_set_port - reset the port number in the remote endpoint address 1726 * @xprt: generic transport 1727 * @port: new port number 1728 * 1729 */ 1730 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 1731 { 1732 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1733 1734 rpc_set_port(xs_addr(xprt), port); 1735 xs_update_peer_port(xprt); 1736 } 1737 1738 static void xs_reset_srcport(struct sock_xprt *transport) 1739 { 1740 transport->srcport = 0; 1741 } 1742 1743 static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 1744 { 1745 if (transport->srcport == 0 && transport->xprt.reuseport) 1746 transport->srcport = xs_sock_getport(sock); 1747 } 1748 1749 static int xs_get_srcport(struct sock_xprt *transport) 1750 { 1751 int port = transport->srcport; 1752 1753 if (port == 0 && transport->xprt.resvport) 1754 port = xs_get_random_port(); 1755 return port; 1756 } 1757 1758 static unsigned short xs_sock_srcport(struct rpc_xprt *xprt) 1759 { 1760 struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt); 1761 unsigned short ret = 0; 1762 mutex_lock(&sock->recv_mutex); 1763 if (sock->sock) 1764 ret = xs_sock_getport(sock->sock); 1765 mutex_unlock(&sock->recv_mutex); 1766 return ret; 1767 } 1768 1769 static int xs_sock_srcaddr(struct rpc_xprt *xprt, char *buf, size_t buflen) 1770 { 1771 struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt); 1772 union { 1773 struct sockaddr sa; 1774 struct sockaddr_storage st; 1775 } saddr; 1776 int ret = -ENOTCONN; 1777 1778 mutex_lock(&sock->recv_mutex); 1779 if (sock->sock) { 1780 ret = kernel_getsockname(sock->sock, &saddr.sa); 1781 if (ret >= 0) 1782 ret = snprintf(buf, buflen, "%pISc", &saddr.sa); 1783 } 1784 mutex_unlock(&sock->recv_mutex); 1785 return ret; 1786 } 1787 1788 static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 1789 { 1790 if (transport->srcport != 0) 1791 transport->srcport = 0; 1792 if (!transport->xprt.resvport) 1793 return 0; 1794 if (port <= xprt_min_resvport || port > xprt_max_resvport) 1795 return xprt_max_resvport; 1796 return --port; 1797 } 1798 static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1799 { 1800 struct sockaddr_storage myaddr; 1801 int err, nloop = 0; 1802 int port = xs_get_srcport(transport); 1803 unsigned short last; 1804 1805 /* 1806 * If we are asking for any ephemeral port (i.e. port == 0 && 1807 * transport->xprt.resvport == 0), don't bind. Let the local 1808 * port selection happen implicitly when the socket is used 1809 * (for example at connect time). 1810 * 1811 * This ensures that we can continue to establish TCP 1812 * connections even when all local ephemeral ports are already 1813 * a part of some TCP connection. This makes no difference 1814 * for UDP sockets, but also doesn't harm them. 1815 * 1816 * If we're asking for any reserved port (i.e. port == 0 && 1817 * transport->xprt.resvport == 1) xs_get_srcport above will 1818 * ensure that port is non-zero and we will bind as needed. 1819 */ 1820 if (port <= 0) 1821 return port; 1822 1823 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1824 do { 1825 rpc_set_port((struct sockaddr *)&myaddr, port); 1826 err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1827 transport->xprt.addrlen); 1828 if (err == 0) { 1829 if (transport->xprt.reuseport) 1830 transport->srcport = port; 1831 break; 1832 } 1833 last = port; 1834 port = xs_next_srcport(transport, port); 1835 if (port > last) 1836 nloop++; 1837 } while (err == -EADDRINUSE && nloop != 2); 1838 1839 if (myaddr.ss_family == AF_INET) 1840 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1841 &((struct sockaddr_in *)&myaddr)->sin_addr, 1842 port, err ? "failed" : "ok", err); 1843 else 1844 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1845 &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 1846 port, err ? "failed" : "ok", err); 1847 return err; 1848 } 1849 1850 /* 1851 * We don't support autobind on AF_LOCAL sockets 1852 */ 1853 static void xs_local_rpcbind(struct rpc_task *task) 1854 { 1855 xprt_set_bound(task->tk_xprt); 1856 } 1857 1858 static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1859 { 1860 } 1861 1862 #ifdef CONFIG_DEBUG_LOCK_ALLOC 1863 static struct lock_class_key xs_key[3]; 1864 static struct lock_class_key xs_slock_key[3]; 1865 1866 static inline void xs_reclassify_socketu(struct socket *sock) 1867 { 1868 struct sock *sk = sock->sk; 1869 1870 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1871 &xs_slock_key[0], "sk_lock-AF_LOCAL-RPC", &xs_key[0]); 1872 } 1873 1874 static inline void xs_reclassify_socket4(struct socket *sock) 1875 { 1876 struct sock *sk = sock->sk; 1877 1878 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 1879 &xs_slock_key[1], "sk_lock-AF_INET-RPC", &xs_key[1]); 1880 } 1881 1882 static inline void xs_reclassify_socket6(struct socket *sock) 1883 { 1884 struct sock *sk = sock->sk; 1885 1886 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 1887 &xs_slock_key[2], "sk_lock-AF_INET6-RPC", &xs_key[2]); 1888 } 1889 1890 static inline void xs_reclassify_socket(int family, struct socket *sock) 1891 { 1892 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) 1893 return; 1894 1895 switch (family) { 1896 case AF_LOCAL: 1897 xs_reclassify_socketu(sock); 1898 break; 1899 case AF_INET: 1900 xs_reclassify_socket4(sock); 1901 break; 1902 case AF_INET6: 1903 xs_reclassify_socket6(sock); 1904 break; 1905 } 1906 } 1907 #else 1908 static inline void xs_reclassify_socket(int family, struct socket *sock) 1909 { 1910 } 1911 #endif 1912 1913 static void xs_dummy_setup_socket(struct work_struct *work) 1914 { 1915 } 1916 1917 static struct socket *xs_create_sock(struct rpc_xprt *xprt, 1918 struct sock_xprt *transport, int family, int type, 1919 int protocol, bool reuseport) 1920 { 1921 struct file *filp; 1922 struct socket *sock; 1923 int err; 1924 1925 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 1926 if (err < 0) { 1927 dprintk("RPC: can't create %d transport socket (%d).\n", 1928 protocol, -err); 1929 goto out; 1930 } 1931 xs_reclassify_socket(family, sock); 1932 1933 if (reuseport) 1934 sock_set_reuseport(sock->sk); 1935 1936 err = xs_bind(transport, sock); 1937 if (err) { 1938 sock_release(sock); 1939 goto out; 1940 } 1941 1942 filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 1943 if (IS_ERR(filp)) 1944 return ERR_CAST(filp); 1945 transport->file = filp; 1946 1947 return sock; 1948 out: 1949 return ERR_PTR(err); 1950 } 1951 1952 static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1953 struct socket *sock) 1954 { 1955 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1956 xprt); 1957 1958 if (!transport->inet) { 1959 struct sock *sk = sock->sk; 1960 1961 lock_sock(sk); 1962 1963 xs_save_old_callbacks(transport, sk); 1964 1965 sk->sk_user_data = xprt; 1966 sk->sk_data_ready = xs_data_ready; 1967 sk->sk_write_space = xs_udp_write_space; 1968 sk->sk_state_change = xs_local_state_change; 1969 sk->sk_error_report = xs_error_report; 1970 sk->sk_use_task_frag = false; 1971 1972 xprt_clear_connected(xprt); 1973 1974 /* Reset to new socket */ 1975 transport->sock = sock; 1976 transport->inet = sk; 1977 1978 release_sock(sk); 1979 } 1980 1981 xs_stream_start_connect(transport); 1982 1983 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 1984 } 1985 1986 /** 1987 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 1988 * @transport: socket transport to connect 1989 */ 1990 static int xs_local_setup_socket(struct sock_xprt *transport) 1991 { 1992 struct rpc_xprt *xprt = &transport->xprt; 1993 struct file *filp; 1994 struct socket *sock; 1995 int status; 1996 1997 status = __sock_create(xprt->xprt_net, AF_LOCAL, 1998 SOCK_STREAM, 0, &sock, 1); 1999 if (status < 0) { 2000 dprintk("RPC: can't create AF_LOCAL " 2001 "transport socket (%d).\n", -status); 2002 goto out; 2003 } 2004 xs_reclassify_socket(AF_LOCAL, sock); 2005 2006 filp = sock_alloc_file(sock, O_NONBLOCK, NULL); 2007 if (IS_ERR(filp)) { 2008 status = PTR_ERR(filp); 2009 goto out; 2010 } 2011 transport->file = filp; 2012 2013 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 2014 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2015 2016 status = xs_local_finish_connecting(xprt, sock); 2017 trace_rpc_socket_connect(xprt, sock, status); 2018 switch (status) { 2019 case 0: 2020 dprintk("RPC: xprt %p connected to %s\n", 2021 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2022 xprt->stat.connect_count++; 2023 xprt->stat.connect_time += (long)jiffies - 2024 xprt->stat.connect_start; 2025 xprt_set_connected(xprt); 2026 break; 2027 case -ENOBUFS: 2028 break; 2029 case -ENOENT: 2030 dprintk("RPC: xprt %p: socket %s does not exist\n", 2031 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2032 break; 2033 case -ECONNREFUSED: 2034 dprintk("RPC: xprt %p: connection refused for %s\n", 2035 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 2036 break; 2037 default: 2038 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 2039 __func__, -status, 2040 xprt->address_strings[RPC_DISPLAY_ADDR]); 2041 } 2042 2043 out: 2044 xprt_clear_connecting(xprt); 2045 xprt_wake_pending_tasks(xprt, status); 2046 return status; 2047 } 2048 2049 static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2050 { 2051 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2052 int ret; 2053 2054 if (transport->file) 2055 goto force_disconnect; 2056 2057 if (RPC_IS_ASYNC(task)) { 2058 /* 2059 * We want the AF_LOCAL connect to be resolved in the 2060 * filesystem namespace of the process making the rpc 2061 * call. Thus we connect synchronously. 2062 * 2063 * If we want to support asynchronous AF_LOCAL calls, 2064 * we'll need to figure out how to pass a namespace to 2065 * connect. 2066 */ 2067 rpc_task_set_rpc_status(task, -ENOTCONN); 2068 goto out_wake; 2069 } 2070 ret = xs_local_setup_socket(transport); 2071 if (ret && !RPC_IS_SOFTCONN(task)) 2072 msleep_interruptible(15000); 2073 return; 2074 force_disconnect: 2075 xprt_force_disconnect(xprt); 2076 out_wake: 2077 xprt_clear_connecting(xprt); 2078 xprt_wake_pending_tasks(xprt, -ENOTCONN); 2079 } 2080 2081 #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 2082 /* 2083 * Note that this should be called with XPRT_LOCKED held, or recv_mutex 2084 * held, or when we otherwise know that we have exclusive access to the 2085 * socket, to guard against races with xs_reset_transport. 2086 */ 2087 static void xs_set_memalloc(struct rpc_xprt *xprt) 2088 { 2089 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 2090 xprt); 2091 2092 /* 2093 * If there's no sock, then we have nothing to set. The 2094 * reconnecting process will get it for us. 2095 */ 2096 if (!transport->inet) 2097 return; 2098 if (atomic_read(&xprt->swapper)) 2099 sk_set_memalloc(transport->inet); 2100 } 2101 2102 /** 2103 * xs_enable_swap - Tag this transport as being used for swap. 2104 * @xprt: transport to tag 2105 * 2106 * Take a reference to this transport on behalf of the rpc_clnt, and 2107 * optionally mark it for swapping if it wasn't already. 2108 */ 2109 static int 2110 xs_enable_swap(struct rpc_xprt *xprt) 2111 { 2112 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2113 2114 mutex_lock(&xs->recv_mutex); 2115 if (atomic_inc_return(&xprt->swapper) == 1 && 2116 xs->inet) 2117 sk_set_memalloc(xs->inet); 2118 mutex_unlock(&xs->recv_mutex); 2119 return 0; 2120 } 2121 2122 /** 2123 * xs_disable_swap - Untag this transport as being used for swap. 2124 * @xprt: transport to tag 2125 * 2126 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 2127 * swapper refcount goes to 0, untag the socket as a memalloc socket. 2128 */ 2129 static void 2130 xs_disable_swap(struct rpc_xprt *xprt) 2131 { 2132 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2133 2134 mutex_lock(&xs->recv_mutex); 2135 if (atomic_dec_and_test(&xprt->swapper) && 2136 xs->inet) 2137 sk_clear_memalloc(xs->inet); 2138 mutex_unlock(&xs->recv_mutex); 2139 } 2140 #else 2141 static void xs_set_memalloc(struct rpc_xprt *xprt) 2142 { 2143 } 2144 2145 static int 2146 xs_enable_swap(struct rpc_xprt *xprt) 2147 { 2148 return -EINVAL; 2149 } 2150 2151 static void 2152 xs_disable_swap(struct rpc_xprt *xprt) 2153 { 2154 } 2155 #endif 2156 2157 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2158 { 2159 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2160 2161 if (!transport->inet) { 2162 struct sock *sk = sock->sk; 2163 2164 lock_sock(sk); 2165 2166 xs_save_old_callbacks(transport, sk); 2167 2168 sk->sk_user_data = xprt; 2169 sk->sk_data_ready = xs_data_ready; 2170 sk->sk_write_space = xs_udp_write_space; 2171 sk->sk_use_task_frag = false; 2172 2173 xprt_set_connected(xprt); 2174 2175 /* Reset to new socket */ 2176 transport->sock = sock; 2177 transport->inet = sk; 2178 2179 xs_set_memalloc(xprt); 2180 2181 release_sock(sk); 2182 } 2183 xs_udp_do_set_buffer_size(xprt); 2184 2185 xprt->stat.connect_start = jiffies; 2186 } 2187 2188 static void xs_udp_setup_socket(struct work_struct *work) 2189 { 2190 struct sock_xprt *transport = 2191 container_of(work, struct sock_xprt, connect_worker.work); 2192 struct rpc_xprt *xprt = &transport->xprt; 2193 struct socket *sock; 2194 int status = -EIO; 2195 unsigned int pflags = current->flags; 2196 2197 if (atomic_read(&xprt->swapper)) 2198 current->flags |= PF_MEMALLOC; 2199 sock = xs_create_sock(xprt, transport, 2200 xs_addr(xprt)->sa_family, SOCK_DGRAM, 2201 IPPROTO_UDP, false); 2202 if (IS_ERR(sock)) 2203 goto out; 2204 2205 dprintk("RPC: worker connecting xprt %p via %s to " 2206 "%s (port %s)\n", xprt, 2207 xprt->address_strings[RPC_DISPLAY_PROTO], 2208 xprt->address_strings[RPC_DISPLAY_ADDR], 2209 xprt->address_strings[RPC_DISPLAY_PORT]); 2210 2211 xs_udp_finish_connecting(xprt, sock); 2212 trace_rpc_socket_connect(xprt, sock, 0); 2213 status = 0; 2214 out: 2215 xprt_clear_connecting(xprt); 2216 xprt_unlock_connect(xprt, transport); 2217 xprt_wake_pending_tasks(xprt, status); 2218 current_restore_flags(pflags, PF_MEMALLOC); 2219 } 2220 2221 /** 2222 * xs_tcp_shutdown - gracefully shut down a TCP socket 2223 * @xprt: transport 2224 * 2225 * Initiates a graceful shutdown of the TCP socket by calling the 2226 * equivalent of shutdown(SHUT_RDWR); 2227 */ 2228 static void xs_tcp_shutdown(struct rpc_xprt *xprt) 2229 { 2230 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2231 struct socket *sock = transport->sock; 2232 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; 2233 2234 if (sock == NULL) 2235 return; 2236 if (!xprt->reuseport) { 2237 xs_close(xprt); 2238 return; 2239 } 2240 switch (skst) { 2241 case TCP_FIN_WAIT1: 2242 case TCP_FIN_WAIT2: 2243 case TCP_LAST_ACK: 2244 break; 2245 case TCP_ESTABLISHED: 2246 case TCP_CLOSE_WAIT: 2247 kernel_sock_shutdown(sock, SHUT_RDWR); 2248 trace_rpc_socket_shutdown(xprt, sock); 2249 break; 2250 default: 2251 xs_reset_transport(transport); 2252 } 2253 } 2254 2255 static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 2256 struct socket *sock) 2257 { 2258 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2259 struct net *net = sock_net(sock->sk); 2260 unsigned long connect_timeout; 2261 unsigned long syn_retries; 2262 unsigned int keepidle; 2263 unsigned int keepcnt; 2264 unsigned int timeo; 2265 unsigned long t; 2266 2267 spin_lock(&xprt->transport_lock); 2268 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); 2269 keepcnt = xprt->timeout->to_retries + 1; 2270 timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 2271 (xprt->timeout->to_retries + 1); 2272 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2273 spin_unlock(&xprt->transport_lock); 2274 2275 /* TCP Keepalive options */ 2276 sock_set_keepalive(sock->sk); 2277 tcp_sock_set_keepidle(sock->sk, keepidle); 2278 tcp_sock_set_keepintvl(sock->sk, keepidle); 2279 tcp_sock_set_keepcnt(sock->sk, keepcnt); 2280 2281 /* TCP user timeout (see RFC5482) */ 2282 tcp_sock_set_user_timeout(sock->sk, timeo); 2283 2284 /* Connect timeout */ 2285 connect_timeout = max_t(unsigned long, 2286 DIV_ROUND_UP(xprt->connect_timeout, HZ), 1); 2287 syn_retries = max_t(unsigned long, 2288 READ_ONCE(net->ipv4.sysctl_tcp_syn_retries), 1); 2289 for (t = 0; t <= syn_retries && (1UL << t) < connect_timeout; t++) 2290 ; 2291 if (t <= syn_retries) 2292 tcp_sock_set_syncnt(sock->sk, t - 1); 2293 } 2294 2295 static void xs_tcp_do_set_connect_timeout(struct rpc_xprt *xprt, 2296 unsigned long connect_timeout) 2297 { 2298 struct sock_xprt *transport = 2299 container_of(xprt, struct sock_xprt, xprt); 2300 struct rpc_timeout to; 2301 unsigned long initval; 2302 2303 memcpy(&to, xprt->timeout, sizeof(to)); 2304 /* Arbitrary lower limit */ 2305 initval = max_t(unsigned long, connect_timeout, XS_TCP_INIT_REEST_TO); 2306 to.to_initval = initval; 2307 to.to_maxval = initval; 2308 to.to_retries = 0; 2309 memcpy(&transport->tcp_timeout, &to, sizeof(transport->tcp_timeout)); 2310 xprt->timeout = &transport->tcp_timeout; 2311 xprt->connect_timeout = connect_timeout; 2312 } 2313 2314 static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, 2315 unsigned long connect_timeout, 2316 unsigned long reconnect_timeout) 2317 { 2318 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2319 2320 spin_lock(&xprt->transport_lock); 2321 if (reconnect_timeout < xprt->max_reconnect_timeout) 2322 xprt->max_reconnect_timeout = reconnect_timeout; 2323 if (connect_timeout < xprt->connect_timeout) 2324 xs_tcp_do_set_connect_timeout(xprt, connect_timeout); 2325 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2326 spin_unlock(&xprt->transport_lock); 2327 } 2328 2329 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2330 { 2331 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2332 2333 if (!transport->inet) { 2334 struct sock *sk = sock->sk; 2335 2336 /* Avoid temporary address, they are bad for long-lived 2337 * connections such as NFS mounts. 2338 * RFC4941, section 3.6 suggests that: 2339 * Individual applications, which have specific 2340 * knowledge about the normal duration of connections, 2341 * MAY override this as appropriate. 2342 */ 2343 if (xs_addr(xprt)->sa_family == PF_INET6) { 2344 ip6_sock_set_addr_preferences(sk, 2345 IPV6_PREFER_SRC_PUBLIC); 2346 } 2347 2348 xs_tcp_set_socket_timeouts(xprt, sock); 2349 tcp_sock_set_nodelay(sk); 2350 2351 lock_sock(sk); 2352 2353 xs_save_old_callbacks(transport, sk); 2354 2355 sk->sk_user_data = xprt; 2356 sk->sk_data_ready = xs_data_ready; 2357 sk->sk_state_change = xs_tcp_state_change; 2358 sk->sk_write_space = xs_tcp_write_space; 2359 sk->sk_error_report = xs_error_report; 2360 sk->sk_use_task_frag = false; 2361 2362 /* socket options */ 2363 sock_reset_flag(sk, SOCK_LINGER); 2364 2365 xprt_clear_connected(xprt); 2366 2367 /* Reset to new socket */ 2368 transport->sock = sock; 2369 transport->inet = sk; 2370 2371 release_sock(sk); 2372 } 2373 2374 if (!xprt_bound(xprt)) 2375 return -ENOTCONN; 2376 2377 xs_set_memalloc(xprt); 2378 2379 xs_stream_start_connect(transport); 2380 2381 /* Tell the socket layer to start connecting... */ 2382 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2383 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 2384 } 2385 2386 /** 2387 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 2388 * @work: queued work item 2389 * 2390 * Invoked by a work queue tasklet. 2391 */ 2392 static void xs_tcp_setup_socket(struct work_struct *work) 2393 { 2394 struct sock_xprt *transport = 2395 container_of(work, struct sock_xprt, connect_worker.work); 2396 struct socket *sock = transport->sock; 2397 struct rpc_xprt *xprt = &transport->xprt; 2398 int status; 2399 unsigned int pflags = current->flags; 2400 2401 if (atomic_read(&xprt->swapper)) 2402 current->flags |= PF_MEMALLOC; 2403 2404 if (xprt_connected(xprt)) 2405 goto out; 2406 if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT, 2407 &transport->sock_state) || 2408 !sock) { 2409 xs_reset_transport(transport); 2410 sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family, 2411 SOCK_STREAM, IPPROTO_TCP, true); 2412 if (IS_ERR(sock)) { 2413 xprt_wake_pending_tasks(xprt, PTR_ERR(sock)); 2414 goto out; 2415 } 2416 } 2417 2418 dprintk("RPC: worker connecting xprt %p via %s to " 2419 "%s (port %s)\n", xprt, 2420 xprt->address_strings[RPC_DISPLAY_PROTO], 2421 xprt->address_strings[RPC_DISPLAY_ADDR], 2422 xprt->address_strings[RPC_DISPLAY_PORT]); 2423 2424 status = xs_tcp_finish_connecting(xprt, sock); 2425 trace_rpc_socket_connect(xprt, sock, status); 2426 dprintk("RPC: %p connect status %d connected %d sock state %d\n", 2427 xprt, -status, xprt_connected(xprt), 2428 sock->sk->sk_state); 2429 switch (status) { 2430 case 0: 2431 case -EINPROGRESS: 2432 /* SYN_SENT! */ 2433 set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state); 2434 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2435 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2436 fallthrough; 2437 case -EALREADY: 2438 goto out_unlock; 2439 case -EADDRNOTAVAIL: 2440 /* Source port number is unavailable. Try a new one! */ 2441 transport->srcport = 0; 2442 status = -EAGAIN; 2443 break; 2444 case -EINVAL: 2445 /* Happens, for instance, if the user specified a link 2446 * local IPv6 address without a scope-id. 2447 */ 2448 case -ECONNREFUSED: 2449 case -ECONNRESET: 2450 case -ENETDOWN: 2451 case -ENETUNREACH: 2452 case -EHOSTUNREACH: 2453 case -EADDRINUSE: 2454 case -ENOBUFS: 2455 break; 2456 default: 2457 printk("%s: connect returned unhandled error %d\n", 2458 __func__, status); 2459 status = -EAGAIN; 2460 } 2461 2462 /* xs_tcp_force_close() wakes tasks with a fixed error code. 2463 * We need to wake them first to ensure the correct error code. 2464 */ 2465 xprt_wake_pending_tasks(xprt, status); 2466 xs_tcp_force_close(xprt); 2467 out: 2468 xprt_clear_connecting(xprt); 2469 out_unlock: 2470 xprt_unlock_connect(xprt, transport); 2471 current_restore_flags(pflags, PF_MEMALLOC); 2472 } 2473 2474 /* 2475 * Transfer the connected socket to @upper_transport, then mark that 2476 * xprt CONNECTED. 2477 */ 2478 static int xs_tcp_tls_finish_connecting(struct rpc_xprt *lower_xprt, 2479 struct sock_xprt *upper_transport) 2480 { 2481 struct sock_xprt *lower_transport = 2482 container_of(lower_xprt, struct sock_xprt, xprt); 2483 struct rpc_xprt *upper_xprt = &upper_transport->xprt; 2484 2485 if (!upper_transport->inet) { 2486 struct socket *sock = lower_transport->sock; 2487 struct sock *sk = sock->sk; 2488 2489 /* Avoid temporary address, they are bad for long-lived 2490 * connections such as NFS mounts. 2491 * RFC4941, section 3.6 suggests that: 2492 * Individual applications, which have specific 2493 * knowledge about the normal duration of connections, 2494 * MAY override this as appropriate. 2495 */ 2496 if (xs_addr(upper_xprt)->sa_family == PF_INET6) 2497 ip6_sock_set_addr_preferences(sk, IPV6_PREFER_SRC_PUBLIC); 2498 2499 xs_tcp_set_socket_timeouts(upper_xprt, sock); 2500 tcp_sock_set_nodelay(sk); 2501 2502 lock_sock(sk); 2503 2504 /* @sk is already connected, so it now has the RPC callbacks. 2505 * Reach into @lower_transport to save the original ones. 2506 */ 2507 upper_transport->old_data_ready = lower_transport->old_data_ready; 2508 upper_transport->old_state_change = lower_transport->old_state_change; 2509 upper_transport->old_write_space = lower_transport->old_write_space; 2510 upper_transport->old_error_report = lower_transport->old_error_report; 2511 sk->sk_user_data = upper_xprt; 2512 2513 /* socket options */ 2514 sock_reset_flag(sk, SOCK_LINGER); 2515 2516 xprt_clear_connected(upper_xprt); 2517 2518 upper_transport->sock = sock; 2519 upper_transport->inet = sk; 2520 upper_transport->file = lower_transport->file; 2521 2522 release_sock(sk); 2523 2524 /* Reset lower_transport before shutting down its clnt */ 2525 mutex_lock(&lower_transport->recv_mutex); 2526 lower_transport->inet = NULL; 2527 lower_transport->sock = NULL; 2528 lower_transport->file = NULL; 2529 2530 xprt_clear_connected(lower_xprt); 2531 xs_sock_reset_connection_flags(lower_xprt); 2532 xs_stream_reset_connect(lower_transport); 2533 mutex_unlock(&lower_transport->recv_mutex); 2534 } 2535 2536 if (!xprt_bound(upper_xprt)) 2537 return -ENOTCONN; 2538 2539 xs_set_memalloc(upper_xprt); 2540 2541 if (!xprt_test_and_set_connected(upper_xprt)) { 2542 upper_xprt->connect_cookie++; 2543 clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state); 2544 xprt_clear_connecting(upper_xprt); 2545 2546 upper_xprt->stat.connect_count++; 2547 upper_xprt->stat.connect_time += (long)jiffies - 2548 upper_xprt->stat.connect_start; 2549 xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING); 2550 } 2551 return 0; 2552 } 2553 2554 /** 2555 * xs_tls_handshake_done - TLS handshake completion handler 2556 * @data: address of xprt to wake 2557 * @status: status of handshake 2558 * @peerid: serial number of key containing the remote's identity 2559 * 2560 */ 2561 static void xs_tls_handshake_done(void *data, int status, key_serial_t peerid) 2562 { 2563 struct rpc_xprt *lower_xprt = data; 2564 struct sock_xprt *lower_transport = 2565 container_of(lower_xprt, struct sock_xprt, xprt); 2566 2567 lower_transport->xprt_err = status ? -EACCES : 0; 2568 complete(&lower_transport->handshake_done); 2569 xprt_put(lower_xprt); 2570 } 2571 2572 static int xs_tls_handshake_sync(struct rpc_xprt *lower_xprt, struct xprtsec_parms *xprtsec) 2573 { 2574 struct sock_xprt *lower_transport = 2575 container_of(lower_xprt, struct sock_xprt, xprt); 2576 struct tls_handshake_args args = { 2577 .ta_sock = lower_transport->sock, 2578 .ta_done = xs_tls_handshake_done, 2579 .ta_data = xprt_get(lower_xprt), 2580 .ta_peername = lower_xprt->servername, 2581 }; 2582 struct sock *sk = lower_transport->inet; 2583 int rc; 2584 2585 init_completion(&lower_transport->handshake_done); 2586 set_bit(XPRT_SOCK_IGNORE_RECV, &lower_transport->sock_state); 2587 lower_transport->xprt_err = -ETIMEDOUT; 2588 switch (xprtsec->policy) { 2589 case RPC_XPRTSEC_TLS_ANON: 2590 rc = tls_client_hello_anon(&args, GFP_KERNEL); 2591 if (rc) 2592 goto out_put_xprt; 2593 break; 2594 case RPC_XPRTSEC_TLS_X509: 2595 args.ta_my_cert = xprtsec->cert_serial; 2596 args.ta_my_privkey = xprtsec->privkey_serial; 2597 rc = tls_client_hello_x509(&args, GFP_KERNEL); 2598 if (rc) 2599 goto out_put_xprt; 2600 break; 2601 default: 2602 rc = -EACCES; 2603 goto out_put_xprt; 2604 } 2605 2606 rc = wait_for_completion_interruptible_timeout(&lower_transport->handshake_done, 2607 XS_TLS_HANDSHAKE_TO); 2608 if (rc <= 0) { 2609 if (!tls_handshake_cancel(sk)) { 2610 if (rc == 0) 2611 rc = -ETIMEDOUT; 2612 goto out_put_xprt; 2613 } 2614 } 2615 2616 rc = lower_transport->xprt_err; 2617 2618 out: 2619 xs_stream_reset_connect(lower_transport); 2620 clear_bit(XPRT_SOCK_IGNORE_RECV, &lower_transport->sock_state); 2621 return rc; 2622 2623 out_put_xprt: 2624 xprt_put(lower_xprt); 2625 goto out; 2626 } 2627 2628 /** 2629 * xs_tcp_tls_setup_socket - establish a TLS session on a TCP socket 2630 * @work: queued work item 2631 * 2632 * Invoked by a work queue tasklet. 2633 * 2634 * For RPC-with-TLS, there is a two-stage connection process. 2635 * 2636 * The "upper-layer xprt" is visible to the RPC consumer. Once it has 2637 * been marked connected, the consumer knows that a TCP connection and 2638 * a TLS session have been established. 2639 * 2640 * A "lower-layer xprt", created in this function, handles the mechanics 2641 * of connecting the TCP socket, performing the RPC_AUTH_TLS probe, and 2642 * then driving the TLS handshake. Once all that is complete, the upper 2643 * layer xprt is marked connected. 2644 */ 2645 static void xs_tcp_tls_setup_socket(struct work_struct *work) 2646 { 2647 struct sock_xprt *upper_transport = 2648 container_of(work, struct sock_xprt, connect_worker.work); 2649 struct rpc_clnt *upper_clnt = upper_transport->clnt; 2650 struct rpc_xprt *upper_xprt = &upper_transport->xprt; 2651 struct rpc_create_args args = { 2652 .net = upper_xprt->xprt_net, 2653 .protocol = upper_xprt->prot, 2654 .address = (struct sockaddr *)&upper_xprt->addr, 2655 .addrsize = upper_xprt->addrlen, 2656 .timeout = upper_clnt->cl_timeout, 2657 .servername = upper_xprt->servername, 2658 .program = upper_clnt->cl_program, 2659 .prognumber = upper_clnt->cl_prog, 2660 .version = upper_clnt->cl_vers, 2661 .authflavor = RPC_AUTH_TLS, 2662 .cred = upper_clnt->cl_cred, 2663 .xprtsec = { 2664 .policy = RPC_XPRTSEC_NONE, 2665 }, 2666 .stats = upper_clnt->cl_stats, 2667 }; 2668 unsigned int pflags = current->flags; 2669 struct rpc_clnt *lower_clnt; 2670 struct rpc_xprt *lower_xprt; 2671 int status; 2672 2673 if (atomic_read(&upper_xprt->swapper)) 2674 current->flags |= PF_MEMALLOC; 2675 2676 xs_stream_start_connect(upper_transport); 2677 2678 /* This implicitly sends an RPC_AUTH_TLS probe */ 2679 lower_clnt = rpc_create(&args); 2680 if (IS_ERR(lower_clnt)) { 2681 trace_rpc_tls_unavailable(upper_clnt, upper_xprt); 2682 clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state); 2683 xprt_clear_connecting(upper_xprt); 2684 xprt_wake_pending_tasks(upper_xprt, PTR_ERR(lower_clnt)); 2685 xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING); 2686 goto out_unlock; 2687 } 2688 2689 /* RPC_AUTH_TLS probe was successful. Try a TLS handshake on 2690 * the lower xprt. 2691 */ 2692 rcu_read_lock(); 2693 lower_xprt = rcu_dereference(lower_clnt->cl_xprt); 2694 rcu_read_unlock(); 2695 2696 if (wait_on_bit_lock(&lower_xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2697 goto out_unlock; 2698 2699 status = xs_tls_handshake_sync(lower_xprt, &upper_xprt->xprtsec); 2700 if (status) { 2701 trace_rpc_tls_not_started(upper_clnt, upper_xprt); 2702 goto out_close; 2703 } 2704 2705 status = xs_tcp_tls_finish_connecting(lower_xprt, upper_transport); 2706 if (status) 2707 goto out_close; 2708 xprt_release_write(lower_xprt, NULL); 2709 2710 trace_rpc_socket_connect(upper_xprt, upper_transport->sock, 0); 2711 if (!xprt_test_and_set_connected(upper_xprt)) { 2712 upper_xprt->connect_cookie++; 2713 clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state); 2714 xprt_clear_connecting(upper_xprt); 2715 2716 upper_xprt->stat.connect_count++; 2717 upper_xprt->stat.connect_time += (long)jiffies - 2718 upper_xprt->stat.connect_start; 2719 xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING); 2720 } 2721 rpc_shutdown_client(lower_clnt); 2722 2723 out_unlock: 2724 current_restore_flags(pflags, PF_MEMALLOC); 2725 upper_transport->clnt = NULL; 2726 xprt_unlock_connect(upper_xprt, upper_transport); 2727 return; 2728 2729 out_close: 2730 xprt_release_write(lower_xprt, NULL); 2731 rpc_shutdown_client(lower_clnt); 2732 2733 /* xprt_force_disconnect() wakes tasks with a fixed tk_status code. 2734 * Wake them first here to ensure they get our tk_status code. 2735 */ 2736 xprt_wake_pending_tasks(upper_xprt, status); 2737 xs_tcp_force_close(upper_xprt); 2738 xprt_clear_connecting(upper_xprt); 2739 goto out_unlock; 2740 } 2741 2742 /** 2743 * xs_connect - connect a socket to a remote endpoint 2744 * @xprt: pointer to transport structure 2745 * @task: address of RPC task that manages state of connect request 2746 * 2747 * TCP: If the remote end dropped the connection, delay reconnecting. 2748 * 2749 * UDP socket connects are synchronous, but we use a work queue anyway 2750 * to guarantee that even unprivileged user processes can set up a 2751 * socket on a privileged port. 2752 * 2753 * If a UDP socket connect fails, the delay behavior here prevents 2754 * retry floods (hard mounts). 2755 */ 2756 static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2757 { 2758 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2759 unsigned long delay = 0; 2760 2761 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2762 2763 if (transport->sock != NULL) { 2764 dprintk("RPC: xs_connect delayed xprt %p for %lu " 2765 "seconds\n", xprt, xprt->reestablish_timeout / HZ); 2766 2767 delay = xprt_reconnect_delay(xprt); 2768 xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO); 2769 2770 } else 2771 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 2772 2773 transport->clnt = task->tk_client; 2774 queue_delayed_work(xprtiod_workqueue, 2775 &transport->connect_worker, 2776 delay); 2777 } 2778 2779 static void xs_wake_disconnect(struct sock_xprt *transport) 2780 { 2781 if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state)) 2782 xs_tcp_force_close(&transport->xprt); 2783 } 2784 2785 static void xs_wake_write(struct sock_xprt *transport) 2786 { 2787 if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state)) 2788 xprt_write_space(&transport->xprt); 2789 } 2790 2791 static void xs_wake_error(struct sock_xprt *transport) 2792 { 2793 int sockerr; 2794 2795 if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 2796 return; 2797 sockerr = xchg(&transport->xprt_err, 0); 2798 if (sockerr < 0) { 2799 xprt_wake_pending_tasks(&transport->xprt, sockerr); 2800 xs_tcp_force_close(&transport->xprt); 2801 } 2802 } 2803 2804 static void xs_wake_pending(struct sock_xprt *transport) 2805 { 2806 if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state)) 2807 xprt_wake_pending_tasks(&transport->xprt, -EAGAIN); 2808 } 2809 2810 static void xs_error_handle(struct work_struct *work) 2811 { 2812 struct sock_xprt *transport = container_of(work, 2813 struct sock_xprt, error_worker); 2814 2815 xs_wake_disconnect(transport); 2816 xs_wake_write(transport); 2817 xs_wake_error(transport); 2818 xs_wake_pending(transport); 2819 } 2820 2821 /** 2822 * xs_local_print_stats - display AF_LOCAL socket-specific stats 2823 * @xprt: rpc_xprt struct containing statistics 2824 * @seq: output file 2825 * 2826 */ 2827 static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2828 { 2829 long idle_time = 0; 2830 2831 if (xprt_connected(xprt)) 2832 idle_time = (long)(jiffies - xprt->last_used) / HZ; 2833 2834 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 2835 "%llu %llu %lu %llu %llu\n", 2836 xprt->stat.bind_count, 2837 xprt->stat.connect_count, 2838 xprt->stat.connect_time / HZ, 2839 idle_time, 2840 xprt->stat.sends, 2841 xprt->stat.recvs, 2842 xprt->stat.bad_xids, 2843 xprt->stat.req_u, 2844 xprt->stat.bklog_u, 2845 xprt->stat.max_slots, 2846 xprt->stat.sending_u, 2847 xprt->stat.pending_u); 2848 } 2849 2850 /** 2851 * xs_udp_print_stats - display UDP socket-specific stats 2852 * @xprt: rpc_xprt struct containing statistics 2853 * @seq: output file 2854 * 2855 */ 2856 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2857 { 2858 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2859 2860 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 2861 "%lu %llu %llu\n", 2862 transport->srcport, 2863 xprt->stat.bind_count, 2864 xprt->stat.sends, 2865 xprt->stat.recvs, 2866 xprt->stat.bad_xids, 2867 xprt->stat.req_u, 2868 xprt->stat.bklog_u, 2869 xprt->stat.max_slots, 2870 xprt->stat.sending_u, 2871 xprt->stat.pending_u); 2872 } 2873 2874 /** 2875 * xs_tcp_print_stats - display TCP socket-specific stats 2876 * @xprt: rpc_xprt struct containing statistics 2877 * @seq: output file 2878 * 2879 */ 2880 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2881 { 2882 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2883 long idle_time = 0; 2884 2885 if (xprt_connected(xprt)) 2886 idle_time = (long)(jiffies - xprt->last_used) / HZ; 2887 2888 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 2889 "%llu %llu %lu %llu %llu\n", 2890 transport->srcport, 2891 xprt->stat.bind_count, 2892 xprt->stat.connect_count, 2893 xprt->stat.connect_time / HZ, 2894 idle_time, 2895 xprt->stat.sends, 2896 xprt->stat.recvs, 2897 xprt->stat.bad_xids, 2898 xprt->stat.req_u, 2899 xprt->stat.bklog_u, 2900 xprt->stat.max_slots, 2901 xprt->stat.sending_u, 2902 xprt->stat.pending_u); 2903 } 2904 2905 /* 2906 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 2907 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 2908 * to use the server side send routines. 2909 */ 2910 static int bc_malloc(struct rpc_task *task) 2911 { 2912 struct rpc_rqst *rqst = task->tk_rqstp; 2913 size_t size = rqst->rq_callsize; 2914 struct page *page; 2915 struct rpc_buffer *buf; 2916 2917 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { 2918 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", 2919 size); 2920 return -EINVAL; 2921 } 2922 2923 page = alloc_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN); 2924 if (!page) 2925 return -ENOMEM; 2926 2927 buf = page_address(page); 2928 buf->len = PAGE_SIZE; 2929 2930 rqst->rq_buffer = buf->data; 2931 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 2932 return 0; 2933 } 2934 2935 /* 2936 * Free the space allocated in the bc_alloc routine 2937 */ 2938 static void bc_free(struct rpc_task *task) 2939 { 2940 void *buffer = task->tk_rqstp->rq_buffer; 2941 struct rpc_buffer *buf; 2942 2943 buf = container_of(buffer, struct rpc_buffer, data); 2944 free_page((unsigned long)buf); 2945 } 2946 2947 static int bc_sendto(struct rpc_rqst *req) 2948 { 2949 struct xdr_buf *xdr = &req->rq_snd_buf; 2950 struct sock_xprt *transport = 2951 container_of(req->rq_xprt, struct sock_xprt, xprt); 2952 struct msghdr msg = { 2953 .msg_flags = 0, 2954 }; 2955 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | 2956 (u32)xdr->len); 2957 unsigned int sent = 0; 2958 int err; 2959 2960 req->rq_xtime = ktime_get(); 2961 err = xdr_alloc_bvec(xdr, rpc_task_gfp_mask()); 2962 if (err < 0) 2963 return err; 2964 err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent); 2965 xdr_free_bvec(xdr); 2966 if (err < 0 || sent != (xdr->len + sizeof(marker))) 2967 return -EAGAIN; 2968 return sent; 2969 } 2970 2971 /** 2972 * bc_send_request - Send a backchannel Call on a TCP socket 2973 * @req: rpc_rqst containing Call message to be sent 2974 * 2975 * xpt_mutex ensures @rqstp's whole message is written to the socket 2976 * without interruption. 2977 * 2978 * Return values: 2979 * %0 if the message was sent successfully 2980 * %ENOTCONN if the message was not sent 2981 */ 2982 static int bc_send_request(struct rpc_rqst *req) 2983 { 2984 struct svc_xprt *xprt; 2985 int len; 2986 2987 /* 2988 * Get the server socket associated with this callback xprt 2989 */ 2990 xprt = req->rq_xprt->bc_xprt; 2991 2992 /* 2993 * Grab the mutex to serialize data as the connection is shared 2994 * with the fore channel 2995 */ 2996 mutex_lock(&xprt->xpt_mutex); 2997 if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 2998 len = -ENOTCONN; 2999 else 3000 len = bc_sendto(req); 3001 mutex_unlock(&xprt->xpt_mutex); 3002 3003 if (len > 0) 3004 len = 0; 3005 3006 return len; 3007 } 3008 3009 static void bc_close(struct rpc_xprt *xprt) 3010 { 3011 xprt_disconnect_done(xprt); 3012 } 3013 3014 static void bc_destroy(struct rpc_xprt *xprt) 3015 { 3016 dprintk("RPC: bc_destroy xprt %p\n", xprt); 3017 3018 xs_xprt_free(xprt); 3019 module_put(THIS_MODULE); 3020 } 3021 3022 static const struct rpc_xprt_ops xs_local_ops = { 3023 .reserve_xprt = xprt_reserve_xprt, 3024 .release_xprt = xprt_release_xprt, 3025 .alloc_slot = xprt_alloc_slot, 3026 .free_slot = xprt_free_slot, 3027 .rpcbind = xs_local_rpcbind, 3028 .set_port = xs_local_set_port, 3029 .connect = xs_local_connect, 3030 .buf_alloc = rpc_malloc, 3031 .buf_free = rpc_free, 3032 .prepare_request = xs_stream_prepare_request, 3033 .send_request = xs_local_send_request, 3034 .abort_send_request = xs_stream_abort_send_request, 3035 .wait_for_reply_request = xprt_wait_for_reply_request_def, 3036 .close = xs_close, 3037 .destroy = xs_destroy, 3038 .print_stats = xs_local_print_stats, 3039 .enable_swap = xs_enable_swap, 3040 .disable_swap = xs_disable_swap, 3041 }; 3042 3043 static const struct rpc_xprt_ops xs_udp_ops = { 3044 .set_buffer_size = xs_udp_set_buffer_size, 3045 .reserve_xprt = xprt_reserve_xprt_cong, 3046 .release_xprt = xprt_release_xprt_cong, 3047 .alloc_slot = xprt_alloc_slot, 3048 .free_slot = xprt_free_slot, 3049 .rpcbind = rpcb_getport_async, 3050 .set_port = xs_set_port, 3051 .connect = xs_connect, 3052 .get_srcaddr = xs_sock_srcaddr, 3053 .get_srcport = xs_sock_srcport, 3054 .buf_alloc = rpc_malloc, 3055 .buf_free = rpc_free, 3056 .send_request = xs_udp_send_request, 3057 .wait_for_reply_request = xprt_wait_for_reply_request_rtt, 3058 .timer = xs_udp_timer, 3059 .release_request = xprt_release_rqst_cong, 3060 .close = xs_close, 3061 .destroy = xs_destroy, 3062 .print_stats = xs_udp_print_stats, 3063 .enable_swap = xs_enable_swap, 3064 .disable_swap = xs_disable_swap, 3065 .inject_disconnect = xs_inject_disconnect, 3066 }; 3067 3068 static const struct rpc_xprt_ops xs_tcp_ops = { 3069 .reserve_xprt = xprt_reserve_xprt, 3070 .release_xprt = xprt_release_xprt, 3071 .alloc_slot = xprt_alloc_slot, 3072 .free_slot = xprt_free_slot, 3073 .rpcbind = rpcb_getport_async, 3074 .set_port = xs_set_port, 3075 .connect = xs_connect, 3076 .get_srcaddr = xs_sock_srcaddr, 3077 .get_srcport = xs_sock_srcport, 3078 .buf_alloc = rpc_malloc, 3079 .buf_free = rpc_free, 3080 .prepare_request = xs_stream_prepare_request, 3081 .send_request = xs_tcp_send_request, 3082 .abort_send_request = xs_stream_abort_send_request, 3083 .wait_for_reply_request = xprt_wait_for_reply_request_def, 3084 .close = xs_tcp_shutdown, 3085 .destroy = xs_destroy, 3086 .set_connect_timeout = xs_tcp_set_connect_timeout, 3087 .print_stats = xs_tcp_print_stats, 3088 .enable_swap = xs_enable_swap, 3089 .disable_swap = xs_disable_swap, 3090 .inject_disconnect = xs_inject_disconnect, 3091 #ifdef CONFIG_SUNRPC_BACKCHANNEL 3092 .bc_setup = xprt_setup_bc, 3093 .bc_maxpayload = xs_tcp_bc_maxpayload, 3094 .bc_num_slots = xprt_bc_max_slots, 3095 .bc_free_rqst = xprt_free_bc_rqst, 3096 .bc_destroy = xprt_destroy_bc, 3097 #endif 3098 }; 3099 3100 /* 3101 * The rpc_xprt_ops for the server backchannel 3102 */ 3103 3104 static const struct rpc_xprt_ops bc_tcp_ops = { 3105 .reserve_xprt = xprt_reserve_xprt, 3106 .release_xprt = xprt_release_xprt, 3107 .alloc_slot = xprt_alloc_slot, 3108 .free_slot = xprt_free_slot, 3109 .buf_alloc = bc_malloc, 3110 .buf_free = bc_free, 3111 .send_request = bc_send_request, 3112 .wait_for_reply_request = xprt_wait_for_reply_request_def, 3113 .close = bc_close, 3114 .destroy = bc_destroy, 3115 .print_stats = xs_tcp_print_stats, 3116 .enable_swap = xs_enable_swap, 3117 .disable_swap = xs_disable_swap, 3118 .inject_disconnect = xs_inject_disconnect, 3119 }; 3120 3121 static int xs_init_anyaddr(const int family, struct sockaddr *sap) 3122 { 3123 static const struct sockaddr_in sin = { 3124 .sin_family = AF_INET, 3125 .sin_addr.s_addr = htonl(INADDR_ANY), 3126 }; 3127 static const struct sockaddr_in6 sin6 = { 3128 .sin6_family = AF_INET6, 3129 .sin6_addr = IN6ADDR_ANY_INIT, 3130 }; 3131 3132 switch (family) { 3133 case AF_LOCAL: 3134 break; 3135 case AF_INET: 3136 memcpy(sap, &sin, sizeof(sin)); 3137 break; 3138 case AF_INET6: 3139 memcpy(sap, &sin6, sizeof(sin6)); 3140 break; 3141 default: 3142 dprintk("RPC: %s: Bad address family\n", __func__); 3143 return -EAFNOSUPPORT; 3144 } 3145 return 0; 3146 } 3147 3148 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 3149 unsigned int slot_table_size, 3150 unsigned int max_slot_table_size) 3151 { 3152 struct rpc_xprt *xprt; 3153 struct sock_xprt *new; 3154 3155 if (args->addrlen > sizeof(xprt->addr)) { 3156 dprintk("RPC: xs_setup_xprt: address too large\n"); 3157 return ERR_PTR(-EBADF); 3158 } 3159 3160 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 3161 max_slot_table_size); 3162 if (xprt == NULL) { 3163 dprintk("RPC: xs_setup_xprt: couldn't allocate " 3164 "rpc_xprt\n"); 3165 return ERR_PTR(-ENOMEM); 3166 } 3167 3168 new = container_of(xprt, struct sock_xprt, xprt); 3169 mutex_init(&new->recv_mutex); 3170 memcpy(&xprt->addr, args->dstaddr, args->addrlen); 3171 xprt->addrlen = args->addrlen; 3172 if (args->srcaddr) 3173 memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 3174 else { 3175 int err; 3176 err = xs_init_anyaddr(args->dstaddr->sa_family, 3177 (struct sockaddr *)&new->srcaddr); 3178 if (err != 0) { 3179 xprt_free(xprt); 3180 return ERR_PTR(err); 3181 } 3182 } 3183 3184 return xprt; 3185 } 3186 3187 static const struct rpc_timeout xs_local_default_timeout = { 3188 .to_initval = 10 * HZ, 3189 .to_maxval = 10 * HZ, 3190 .to_retries = 2, 3191 }; 3192 3193 /** 3194 * xs_setup_local - Set up transport to use an AF_LOCAL socket 3195 * @args: rpc transport creation arguments 3196 * 3197 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 3198 */ 3199 static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 3200 { 3201 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 3202 struct sock_xprt *transport; 3203 struct rpc_xprt *xprt; 3204 struct rpc_xprt *ret; 3205 3206 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3207 xprt_max_tcp_slot_table_entries); 3208 if (IS_ERR(xprt)) 3209 return xprt; 3210 transport = container_of(xprt, struct sock_xprt, xprt); 3211 3212 xprt->prot = 0; 3213 xprt->xprt_class = &xs_local_transport; 3214 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3215 3216 xprt->bind_timeout = XS_BIND_TO; 3217 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 3218 xprt->idle_timeout = XS_IDLE_DISC_TO; 3219 3220 xprt->ops = &xs_local_ops; 3221 xprt->timeout = &xs_local_default_timeout; 3222 3223 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 3224 INIT_WORK(&transport->error_worker, xs_error_handle); 3225 INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket); 3226 3227 switch (sun->sun_family) { 3228 case AF_LOCAL: 3229 if (sun->sun_path[0] != '/' && sun->sun_path[0] != '\0') { 3230 dprintk("RPC: bad AF_LOCAL address: %s\n", 3231 sun->sun_path); 3232 ret = ERR_PTR(-EINVAL); 3233 goto out_err; 3234 } 3235 xprt_set_bound(xprt); 3236 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 3237 break; 3238 default: 3239 ret = ERR_PTR(-EAFNOSUPPORT); 3240 goto out_err; 3241 } 3242 3243 dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 3244 xprt->address_strings[RPC_DISPLAY_ADDR]); 3245 3246 if (try_module_get(THIS_MODULE)) 3247 return xprt; 3248 ret = ERR_PTR(-EINVAL); 3249 out_err: 3250 xs_xprt_free(xprt); 3251 return ret; 3252 } 3253 3254 static const struct rpc_timeout xs_udp_default_timeout = { 3255 .to_initval = 5 * HZ, 3256 .to_maxval = 30 * HZ, 3257 .to_increment = 5 * HZ, 3258 .to_retries = 5, 3259 }; 3260 3261 /** 3262 * xs_setup_udp - Set up transport to use a UDP socket 3263 * @args: rpc transport creation arguments 3264 * 3265 */ 3266 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 3267 { 3268 struct sockaddr *addr = args->dstaddr; 3269 struct rpc_xprt *xprt; 3270 struct sock_xprt *transport; 3271 struct rpc_xprt *ret; 3272 3273 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 3274 xprt_udp_slot_table_entries); 3275 if (IS_ERR(xprt)) 3276 return xprt; 3277 transport = container_of(xprt, struct sock_xprt, xprt); 3278 3279 xprt->prot = IPPROTO_UDP; 3280 xprt->xprt_class = &xs_udp_transport; 3281 /* XXX: header size can vary due to auth type, IPv6, etc. */ 3282 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 3283 3284 xprt->bind_timeout = XS_BIND_TO; 3285 xprt->reestablish_timeout = XS_UDP_REEST_TO; 3286 xprt->idle_timeout = XS_IDLE_DISC_TO; 3287 3288 xprt->ops = &xs_udp_ops; 3289 3290 xprt->timeout = &xs_udp_default_timeout; 3291 3292 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 3293 INIT_WORK(&transport->error_worker, xs_error_handle); 3294 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 3295 3296 switch (addr->sa_family) { 3297 case AF_INET: 3298 if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 3299 xprt_set_bound(xprt); 3300 3301 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 3302 break; 3303 case AF_INET6: 3304 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 3305 xprt_set_bound(xprt); 3306 3307 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 3308 break; 3309 default: 3310 ret = ERR_PTR(-EAFNOSUPPORT); 3311 goto out_err; 3312 } 3313 3314 if (xprt_bound(xprt)) 3315 dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3316 xprt->address_strings[RPC_DISPLAY_ADDR], 3317 xprt->address_strings[RPC_DISPLAY_PORT], 3318 xprt->address_strings[RPC_DISPLAY_PROTO]); 3319 else 3320 dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3321 xprt->address_strings[RPC_DISPLAY_ADDR], 3322 xprt->address_strings[RPC_DISPLAY_PROTO]); 3323 3324 if (try_module_get(THIS_MODULE)) 3325 return xprt; 3326 ret = ERR_PTR(-EINVAL); 3327 out_err: 3328 xs_xprt_free(xprt); 3329 return ret; 3330 } 3331 3332 static const struct rpc_timeout xs_tcp_default_timeout = { 3333 .to_initval = 60 * HZ, 3334 .to_maxval = 60 * HZ, 3335 .to_retries = 2, 3336 }; 3337 3338 /** 3339 * xs_setup_tcp - Set up transport to use a TCP socket 3340 * @args: rpc transport creation arguments 3341 * 3342 */ 3343 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 3344 { 3345 struct sockaddr *addr = args->dstaddr; 3346 struct rpc_xprt *xprt; 3347 struct sock_xprt *transport; 3348 struct rpc_xprt *ret; 3349 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 3350 3351 if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 3352 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 3353 3354 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3355 max_slot_table_size); 3356 if (IS_ERR(xprt)) 3357 return xprt; 3358 transport = container_of(xprt, struct sock_xprt, xprt); 3359 3360 xprt->prot = IPPROTO_TCP; 3361 xprt->xprt_class = &xs_tcp_transport; 3362 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3363 3364 xprt->bind_timeout = XS_BIND_TO; 3365 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 3366 xprt->idle_timeout = XS_IDLE_DISC_TO; 3367 3368 xprt->ops = &xs_tcp_ops; 3369 xprt->timeout = &xs_tcp_default_timeout; 3370 3371 xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 3372 if (args->reconnect_timeout) 3373 xprt->max_reconnect_timeout = args->reconnect_timeout; 3374 3375 xprt->connect_timeout = xprt->timeout->to_initval * 3376 (xprt->timeout->to_retries + 1); 3377 if (args->connect_timeout) 3378 xs_tcp_do_set_connect_timeout(xprt, args->connect_timeout); 3379 3380 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 3381 INIT_WORK(&transport->error_worker, xs_error_handle); 3382 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 3383 3384 switch (addr->sa_family) { 3385 case AF_INET: 3386 if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 3387 xprt_set_bound(xprt); 3388 3389 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 3390 break; 3391 case AF_INET6: 3392 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 3393 xprt_set_bound(xprt); 3394 3395 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 3396 break; 3397 default: 3398 ret = ERR_PTR(-EAFNOSUPPORT); 3399 goto out_err; 3400 } 3401 3402 if (xprt_bound(xprt)) 3403 dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3404 xprt->address_strings[RPC_DISPLAY_ADDR], 3405 xprt->address_strings[RPC_DISPLAY_PORT], 3406 xprt->address_strings[RPC_DISPLAY_PROTO]); 3407 else 3408 dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3409 xprt->address_strings[RPC_DISPLAY_ADDR], 3410 xprt->address_strings[RPC_DISPLAY_PROTO]); 3411 3412 if (try_module_get(THIS_MODULE)) 3413 return xprt; 3414 ret = ERR_PTR(-EINVAL); 3415 out_err: 3416 xs_xprt_free(xprt); 3417 return ret; 3418 } 3419 3420 /** 3421 * xs_setup_tcp_tls - Set up transport to use a TCP with TLS 3422 * @args: rpc transport creation arguments 3423 * 3424 */ 3425 static struct rpc_xprt *xs_setup_tcp_tls(struct xprt_create *args) 3426 { 3427 struct sockaddr *addr = args->dstaddr; 3428 struct rpc_xprt *xprt; 3429 struct sock_xprt *transport; 3430 struct rpc_xprt *ret; 3431 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 3432 3433 if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 3434 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 3435 3436 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3437 max_slot_table_size); 3438 if (IS_ERR(xprt)) 3439 return xprt; 3440 transport = container_of(xprt, struct sock_xprt, xprt); 3441 3442 xprt->prot = IPPROTO_TCP; 3443 xprt->xprt_class = &xs_tcp_transport; 3444 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3445 3446 xprt->bind_timeout = XS_BIND_TO; 3447 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 3448 xprt->idle_timeout = XS_IDLE_DISC_TO; 3449 3450 xprt->ops = &xs_tcp_ops; 3451 xprt->timeout = &xs_tcp_default_timeout; 3452 3453 xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 3454 xprt->connect_timeout = xprt->timeout->to_initval * 3455 (xprt->timeout->to_retries + 1); 3456 3457 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 3458 INIT_WORK(&transport->error_worker, xs_error_handle); 3459 3460 switch (args->xprtsec.policy) { 3461 case RPC_XPRTSEC_TLS_ANON: 3462 case RPC_XPRTSEC_TLS_X509: 3463 xprt->xprtsec = args->xprtsec; 3464 INIT_DELAYED_WORK(&transport->connect_worker, 3465 xs_tcp_tls_setup_socket); 3466 break; 3467 default: 3468 ret = ERR_PTR(-EACCES); 3469 goto out_err; 3470 } 3471 3472 switch (addr->sa_family) { 3473 case AF_INET: 3474 if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 3475 xprt_set_bound(xprt); 3476 3477 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 3478 break; 3479 case AF_INET6: 3480 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 3481 xprt_set_bound(xprt); 3482 3483 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 3484 break; 3485 default: 3486 ret = ERR_PTR(-EAFNOSUPPORT); 3487 goto out_err; 3488 } 3489 3490 if (xprt_bound(xprt)) 3491 dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3492 xprt->address_strings[RPC_DISPLAY_ADDR], 3493 xprt->address_strings[RPC_DISPLAY_PORT], 3494 xprt->address_strings[RPC_DISPLAY_PROTO]); 3495 else 3496 dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3497 xprt->address_strings[RPC_DISPLAY_ADDR], 3498 xprt->address_strings[RPC_DISPLAY_PROTO]); 3499 3500 if (try_module_get(THIS_MODULE)) 3501 return xprt; 3502 ret = ERR_PTR(-EINVAL); 3503 out_err: 3504 xs_xprt_free(xprt); 3505 return ret; 3506 } 3507 3508 /** 3509 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 3510 * @args: rpc transport creation arguments 3511 * 3512 */ 3513 static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 3514 { 3515 struct sockaddr *addr = args->dstaddr; 3516 struct rpc_xprt *xprt; 3517 struct sock_xprt *transport; 3518 struct svc_sock *bc_sock; 3519 struct rpc_xprt *ret; 3520 3521 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3522 xprt_tcp_slot_table_entries); 3523 if (IS_ERR(xprt)) 3524 return xprt; 3525 transport = container_of(xprt, struct sock_xprt, xprt); 3526 3527 xprt->prot = IPPROTO_TCP; 3528 xprt->xprt_class = &xs_bc_tcp_transport; 3529 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3530 xprt->timeout = &xs_tcp_default_timeout; 3531 3532 /* backchannel */ 3533 xprt_set_bound(xprt); 3534 xprt->bind_timeout = 0; 3535 xprt->reestablish_timeout = 0; 3536 xprt->idle_timeout = 0; 3537 3538 xprt->ops = &bc_tcp_ops; 3539 3540 switch (addr->sa_family) { 3541 case AF_INET: 3542 xs_format_peer_addresses(xprt, "tcp", 3543 RPCBIND_NETID_TCP); 3544 break; 3545 case AF_INET6: 3546 xs_format_peer_addresses(xprt, "tcp", 3547 RPCBIND_NETID_TCP6); 3548 break; 3549 default: 3550 ret = ERR_PTR(-EAFNOSUPPORT); 3551 goto out_err; 3552 } 3553 3554 dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3555 xprt->address_strings[RPC_DISPLAY_ADDR], 3556 xprt->address_strings[RPC_DISPLAY_PORT], 3557 xprt->address_strings[RPC_DISPLAY_PROTO]); 3558 3559 /* 3560 * Once we've associated a backchannel xprt with a connection, 3561 * we want to keep it around as long as the connection lasts, 3562 * in case we need to start using it for a backchannel again; 3563 * this reference won't be dropped until bc_xprt is destroyed. 3564 */ 3565 xprt_get(xprt); 3566 args->bc_xprt->xpt_bc_xprt = xprt; 3567 xprt->bc_xprt = args->bc_xprt; 3568 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 3569 transport->sock = bc_sock->sk_sock; 3570 transport->inet = bc_sock->sk_sk; 3571 3572 /* 3573 * Since we don't want connections for the backchannel, we set 3574 * the xprt status to connected 3575 */ 3576 xprt_set_connected(xprt); 3577 3578 if (try_module_get(THIS_MODULE)) 3579 return xprt; 3580 3581 args->bc_xprt->xpt_bc_xprt = NULL; 3582 args->bc_xprt->xpt_bc_xps = NULL; 3583 xprt_put(xprt); 3584 ret = ERR_PTR(-EINVAL); 3585 out_err: 3586 xs_xprt_free(xprt); 3587 return ret; 3588 } 3589 3590 static struct xprt_class xs_local_transport = { 3591 .list = LIST_HEAD_INIT(xs_local_transport.list), 3592 .name = "named UNIX socket", 3593 .owner = THIS_MODULE, 3594 .ident = XPRT_TRANSPORT_LOCAL, 3595 .setup = xs_setup_local, 3596 .netid = { "" }, 3597 }; 3598 3599 static struct xprt_class xs_udp_transport = { 3600 .list = LIST_HEAD_INIT(xs_udp_transport.list), 3601 .name = "udp", 3602 .owner = THIS_MODULE, 3603 .ident = XPRT_TRANSPORT_UDP, 3604 .setup = xs_setup_udp, 3605 .netid = { "udp", "udp6", "" }, 3606 }; 3607 3608 static struct xprt_class xs_tcp_transport = { 3609 .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3610 .name = "tcp", 3611 .owner = THIS_MODULE, 3612 .ident = XPRT_TRANSPORT_TCP, 3613 .setup = xs_setup_tcp, 3614 .netid = { "tcp", "tcp6", "" }, 3615 }; 3616 3617 static struct xprt_class xs_tcp_tls_transport = { 3618 .list = LIST_HEAD_INIT(xs_tcp_tls_transport.list), 3619 .name = "tcp-with-tls", 3620 .owner = THIS_MODULE, 3621 .ident = XPRT_TRANSPORT_TCP_TLS, 3622 .setup = xs_setup_tcp_tls, 3623 .netid = { "tcp", "tcp6", "" }, 3624 }; 3625 3626 static struct xprt_class xs_bc_tcp_transport = { 3627 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3628 .name = "tcp NFSv4.1 backchannel", 3629 .owner = THIS_MODULE, 3630 .ident = XPRT_TRANSPORT_BC_TCP, 3631 .setup = xs_setup_bc_tcp, 3632 .netid = { "" }, 3633 }; 3634 3635 /** 3636 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3637 * 3638 */ 3639 int init_socket_xprt(void) 3640 { 3641 if (!sunrpc_table_header) 3642 sunrpc_table_header = register_sysctl("sunrpc", xs_tunables_table); 3643 3644 xprt_register_transport(&xs_local_transport); 3645 xprt_register_transport(&xs_udp_transport); 3646 xprt_register_transport(&xs_tcp_transport); 3647 xprt_register_transport(&xs_tcp_tls_transport); 3648 xprt_register_transport(&xs_bc_tcp_transport); 3649 3650 return 0; 3651 } 3652 3653 /** 3654 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3655 * 3656 */ 3657 void cleanup_socket_xprt(void) 3658 { 3659 if (sunrpc_table_header) { 3660 unregister_sysctl_table(sunrpc_table_header); 3661 sunrpc_table_header = NULL; 3662 } 3663 3664 xprt_unregister_transport(&xs_local_transport); 3665 xprt_unregister_transport(&xs_udp_transport); 3666 xprt_unregister_transport(&xs_tcp_transport); 3667 xprt_unregister_transport(&xs_tcp_tls_transport); 3668 xprt_unregister_transport(&xs_bc_tcp_transport); 3669 } 3670 3671 static int param_set_portnr(const char *val, const struct kernel_param *kp) 3672 { 3673 return param_set_uint_minmax(val, kp, 3674 RPC_MIN_RESVPORT, 3675 RPC_MAX_RESVPORT); 3676 } 3677 3678 static const struct kernel_param_ops param_ops_portnr = { 3679 .set = param_set_portnr, 3680 .get = param_get_uint, 3681 }; 3682 3683 #define param_check_portnr(name, p) \ 3684 __param_check(name, p, unsigned int); 3685 3686 module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3687 module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3688 3689 static int param_set_slot_table_size(const char *val, 3690 const struct kernel_param *kp) 3691 { 3692 return param_set_uint_minmax(val, kp, 3693 RPC_MIN_SLOT_TABLE, 3694 RPC_MAX_SLOT_TABLE); 3695 } 3696 3697 static const struct kernel_param_ops param_ops_slot_table_size = { 3698 .set = param_set_slot_table_size, 3699 .get = param_get_uint, 3700 }; 3701 3702 #define param_check_slot_table_size(name, p) \ 3703 __param_check(name, p, unsigned int); 3704 3705 static int param_set_max_slot_table_size(const char *val, 3706 const struct kernel_param *kp) 3707 { 3708 return param_set_uint_minmax(val, kp, 3709 RPC_MIN_SLOT_TABLE, 3710 RPC_MAX_SLOT_TABLE_LIMIT); 3711 } 3712 3713 static const struct kernel_param_ops param_ops_max_slot_table_size = { 3714 .set = param_set_max_slot_table_size, 3715 .get = param_get_uint, 3716 }; 3717 3718 #define param_check_max_slot_table_size(name, p) \ 3719 __param_check(name, p, unsigned int); 3720 3721 module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3722 slot_table_size, 0644); 3723 module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3724 max_slot_table_size, 0644); 3725 module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3726 slot_table_size, 0644); 3727