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