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