blob: 478f857cdaed4548e7c181b3a6cc41ecf5a1425b [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/net/sunrpc/svcsock.c
4 *
5 * These are the RPC server socket internals.
6 *
7 * The server scheduling algorithm does not always distribute the load
8 * evenly when servicing a single client. May need to modify the
Tom Tuckerf6150c32007-12-30 21:07:57 -06009 * svc_xprt_enqueue procedure...
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * TCP support is largely untested and may be a little slow. The problem
12 * is that we currently do two separate recvfrom's, one for the 4-byte
13 * record length, and the second for the actual record. This could possibly
14 * be improved by always reading a minimum size of around 100 bytes and
15 * tucking any superfluous bytes away in a temporary store. Still, that
16 * leaves write requests out in the rain. An alternative may be to peek at
17 * the first skb in the queue, and if it matches the next TCP sequence
18 * number, to extract the record marker. Yuck.
19 *
20 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
21 */
22
Ilpo Järvinen172589c2007-08-28 15:50:33 -070023#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/sched.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040025#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/errno.h>
27#include <linux/fcntl.h>
28#include <linux/net.h>
29#include <linux/in.h>
30#include <linux/inet.h>
31#include <linux/udp.h>
Andrew Morton91483c42005-08-09 20:20:07 -070032#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/unistd.h>
34#include <linux/slab.h>
35#include <linux/netdevice.h>
36#include <linux/skbuff.h>
NeilBrownb41b66d2006-10-02 02:17:48 -070037#include <linux/file.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080038#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <net/sock.h>
40#include <net/checksum.h>
41#include <net/ip.h>
Chuck Leverb92503b2007-02-12 00:53:36 -080042#include <net/ipv6.h>
Paolo Abeni850cbad2016-10-21 13:55:47 +020043#include <net/udp.h>
Chuck Leverb7872fe2008-04-14 12:27:01 -040044#include <net/tcp.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070045#include <net/tcp_states.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080046#include <linux/uaccess.h>
Christophe Leroybecd2012020-06-15 06:25:23 +000047#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/ioctls.h>
49
50#include <linux/sunrpc/types.h>
Chuck Leverad06e4b2007-02-12 00:53:32 -080051#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/sunrpc/xdr.h>
Chuck Leverc0401ea2008-04-14 12:27:30 -040053#include <linux/sunrpc/msg_prot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/sunrpc/svcsock.h>
55#include <linux/sunrpc/stats.h>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030056#include <linux/sunrpc/xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Chuck Lever998024d2020-03-17 15:06:31 -040058#include <trace/events/sunrpc.h>
59
Chuck Lever9e55eef42020-03-02 15:19:54 -050060#include "socklib.h"
H Hartley Sweeten177e4f92011-06-20 17:54:51 -070061#include "sunrpc.h"
62
Tom Tucker360d87382007-12-30 21:07:17 -060063#define RPCDBG_FACILITY RPCDBG_SVCXPRT
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65
66static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
J. Bruce Fields72c35372012-08-13 17:46:17 -040067 int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static int svc_udp_recvfrom(struct svc_rqst *);
69static int svc_udp_sendto(struct svc_rqst *);
Tom Tucker755ccea2007-12-30 21:07:27 -060070static void svc_sock_detach(struct svc_xprt *);
Trond Myklebust69b6ba32008-12-23 16:30:11 -050071static void svc_tcp_sock_detach(struct svc_xprt *);
Tom Tucker755ccea2007-12-30 21:07:27 -060072static void svc_sock_free(struct svc_xprt *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Tom Tuckerb700cbb2007-12-30 21:07:42 -060074static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
Pavel Emelyanov62832c02010-09-29 16:04:18 +040075 struct net *, struct sockaddr *,
76 int, int);
Peter Zijlstraed075362006-12-06 20:35:24 -080077#ifdef CONFIG_DEBUG_LOCK_ALLOC
78static struct lock_class_key svc_key[2];
79static struct lock_class_key svc_slock_key[2];
80
Tom Tucker0f0257e2007-12-30 21:08:27 -060081static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -080082{
83 struct sock *sk = sock->sk;
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -040084
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +020085 if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -040086 return;
87
Peter Zijlstraed075362006-12-06 20:35:24 -080088 switch (sk->sk_family) {
89 case AF_INET:
90 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -060091 &svc_slock_key[0],
92 "sk_xprt.xpt_lock-AF_INET-NFSD",
93 &svc_key[0]);
Peter Zijlstraed075362006-12-06 20:35:24 -080094 break;
95
96 case AF_INET6:
97 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -060098 &svc_slock_key[1],
99 "sk_xprt.xpt_lock-AF_INET6-NFSD",
100 &svc_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -0800101 break;
102
103 default:
104 BUG();
105 }
106}
107#else
Tom Tucker0f0257e2007-12-30 21:08:27 -0600108static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -0800109{
110}
111#endif
112
Chuck Levercca557a2020-05-20 12:53:05 -0400113/**
114 * svc_tcp_release_rqst - Release transport-related resources
115 * @rqstp: request structure with resources to be released
116 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Chuck Levercca557a2020-05-20 12:53:05 -0400118static void svc_tcp_release_rqst(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Tom Tucker5148bf42007-12-30 21:07:25 -0600120 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (skb) {
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600123 struct svc_sock *svsk =
124 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Chuck Lever7dae1dd2020-05-20 13:41:02 -0400126 rqstp->rq_xprt_ctxt = NULL;
Eric Dumazet9d410c72009-10-30 05:03:53 +0000127 skb_free_datagram_locked(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Chuck Levercca557a2020-05-20 12:53:05 -0400131/**
132 * svc_udp_release_rqst - Release transport-related resources
133 * @rqstp: request structure with resources to be released
134 *
135 */
136static void svc_udp_release_rqst(struct svc_rqst *rqstp)
Paolo Abeni850cbad2016-10-21 13:55:47 +0200137{
138 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
139
140 if (skb) {
141 rqstp->rq_xprt_ctxt = NULL;
Paolo Abeni850cbad2016-10-21 13:55:47 +0200142 consume_skb(skb);
143 }
144}
145
Chuck Leverb92503b2007-02-12 00:53:36 -0800146union svc_pktinfo_u {
147 struct in_pktinfo pkti;
Chuck Leverb92503b2007-02-12 00:53:36 -0800148 struct in6_pktinfo pkti6;
Chuck Leverb92503b2007-02-12 00:53:36 -0800149};
David S. Millerbc375ea2007-04-12 13:35:59 -0700150#define SVC_PKTINFO_SPACE \
151 CMSG_SPACE(sizeof(union svc_pktinfo_u))
Chuck Leverb92503b2007-02-12 00:53:36 -0800152
153static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
154{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600155 struct svc_sock *svsk =
156 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
157 switch (svsk->sk_sk->sk_family) {
Chuck Leverb92503b2007-02-12 00:53:36 -0800158 case AF_INET: {
159 struct in_pktinfo *pki = CMSG_DATA(cmh);
160
161 cmh->cmsg_level = SOL_IP;
162 cmh->cmsg_type = IP_PKTINFO;
163 pki->ipi_ifindex = 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800164 pki->ipi_spec_dst.s_addr =
165 svc_daddr_in(rqstp)->sin_addr.s_addr;
Chuck Leverb92503b2007-02-12 00:53:36 -0800166 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
167 }
168 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800169
Chuck Leverb92503b2007-02-12 00:53:36 -0800170 case AF_INET6: {
171 struct in6_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800172 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
Chuck Leverb92503b2007-02-12 00:53:36 -0800173
174 cmh->cmsg_level = SOL_IPV6;
175 cmh->cmsg_type = IPV6_PKTINFO;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800176 pki->ipi6_ifindex = daddr->sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000177 pki->ipi6_addr = daddr->sin6_addr;
Chuck Leverb92503b2007-02-12 00:53:36 -0800178 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
179 }
180 break;
Chuck Leverb92503b2007-02-12 00:53:36 -0800181 }
Chuck Leverb92503b2007-02-12 00:53:36 -0800182}
183
Chuck Lever03493bc2020-06-10 10:36:42 -0400184static int svc_sock_result_payload(struct svc_rqst *rqstp, unsigned int offset,
185 unsigned int length)
Chuck Lever41205532020-03-02 14:45:53 -0500186{
187 return 0;
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
NeilBrown80212d52006-10-02 02:17:47 -0700191 * Report socket names for nfsdfs
192 */
Chuck Levere7942b92009-04-23 19:32:48 -0400193static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
NeilBrown80212d52006-10-02 02:17:47 -0700194{
Chuck Lever017cb472009-04-23 19:33:03 -0400195 const struct sock *sk = svsk->sk_sk;
196 const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
197 "udp" : "tcp";
NeilBrown80212d52006-10-02 02:17:47 -0700198 int len;
199
Chuck Lever017cb472009-04-23 19:33:03 -0400200 switch (sk->sk_family) {
Chuck Levere7942b92009-04-23 19:32:48 -0400201 case PF_INET:
202 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400203 proto_name,
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000204 &inet_sk(sk)->inet_rcv_saddr,
205 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700206 break;
Eric Dumazetc2bb06d2013-10-09 03:05:48 -0700207#if IS_ENABLED(CONFIG_IPV6)
Chuck Lever58de2f82009-04-23 19:32:55 -0400208 case PF_INET6:
209 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400210 proto_name,
Eric Dumazetefe42082013-10-03 15:42:29 -0700211 &sk->sk_v6_rcv_saddr,
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000212 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700213 break;
Eric Dumazetc2bb06d2013-10-09 03:05:48 -0700214#endif
NeilBrown80212d52006-10-02 02:17:47 -0700215 default:
Chuck Levere7942b92009-04-23 19:32:48 -0400216 len = snprintf(buf, remaining, "*unknown-%d*\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400217 sk->sk_family);
NeilBrown80212d52006-10-02 02:17:47 -0700218 }
Chuck Levere7942b92009-04-23 19:32:48 -0400219
220 if (len >= remaining) {
221 *buf = '\0';
222 return -ENAMETOOLONG;
NeilBrown80212d52006-10-02 02:17:47 -0700223 }
224 return len;
225}
226
Chuck Leverca07eda2020-05-20 17:30:12 -0400227#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
228static void svc_flush_bvec(const struct bio_vec *bvec, size_t size, size_t seek)
229{
230 struct bvec_iter bi = {
Chuck Lever13a9a9d2020-09-20 13:46:25 -0400231 .bi_size = size + seek,
Chuck Leverca07eda2020-05-20 17:30:12 -0400232 };
233 struct bio_vec bv;
234
235 bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
236 for_each_bvec(bv, bvec, bi, bi)
237 flush_dcache_page(bv.bv_page);
238}
239#else
240static inline void svc_flush_bvec(const struct bio_vec *bvec, size_t size,
241 size_t seek)
242{
243}
244#endif
245
NeilBrown80212d52006-10-02 02:17:47 -0700246/*
Chuck Leverca07eda2020-05-20 17:30:12 -0400247 * Read from @rqstp's transport socket. The incoming message fills whole
248 * pages in @rqstp's rq_pages array until the last page of the message
249 * has been received into a partial page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 */
Chuck Leverca07eda2020-05-20 17:30:12 -0400251static ssize_t svc_tcp_read_msg(struct svc_rqst *rqstp, size_t buflen,
252 size_t seek)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600254 struct svc_sock *svsk =
255 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Chuck Leverca07eda2020-05-20 17:30:12 -0400256 struct bio_vec *bvec = rqstp->rq_bvec;
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400257 struct msghdr msg = { NULL };
Chuck Leverca07eda2020-05-20 17:30:12 -0400258 unsigned int i;
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400259 ssize_t len;
Chuck Leverca07eda2020-05-20 17:30:12 -0400260 size_t t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Tom Tucker260c1d12007-12-30 21:08:29 -0600262 rqstp->rq_xprt_hlen = 0;
263
Trond Myklebustf8d1ff42014-08-03 13:03:13 -0400264 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Chuck Leverca07eda2020-05-20 17:30:12 -0400265
266 for (i = 0, t = 0; t < buflen; i++, t += PAGE_SIZE) {
267 bvec[i].bv_page = rqstp->rq_pages[i];
268 bvec[i].bv_len = PAGE_SIZE;
269 bvec[i].bv_offset = 0;
270 }
271 rqstp->rq_respages = &rqstp->rq_pages[i];
272 rqstp->rq_next_page = rqstp->rq_respages + 1;
273
274 iov_iter_bvec(&msg.msg_iter, READ, bvec, i, buflen);
275 if (seek) {
276 iov_iter_advance(&msg.msg_iter, seek);
277 buflen -= seek;
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400278 }
279 len = sock_recvmsg(svsk->sk_sock, &msg, MSG_DONTWAIT);
Chuck Leverca07eda2020-05-20 17:30:12 -0400280 if (len > 0)
281 svc_flush_bvec(bvec, len, seek);
282
Trond Myklebustf8d1ff42014-08-03 13:03:13 -0400283 /* If we read a full record, then assume there may be more
284 * data to read (stream based sockets only!)
285 */
286 if (len == buflen)
287 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return len;
290}
291
292/*
293 * Set socket snd and rcv buffer lengths
294 */
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500295static void svc_sock_setbufsize(struct svc_sock *svsk, unsigned int nreqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500297 unsigned int max_mesg = svsk->sk_xprt.xpt_server->sv_max_mesg;
298 struct socket *sock = svsk->sk_sock;
299
300 nreqs = min(nreqs, INT_MAX / 2 / max_mesg);
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 lock_sock(sock->sk);
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500303 sock->sk->sk_sndbuf = nreqs * max_mesg * 2;
304 sock->sk->sk_rcvbuf = nreqs * max_mesg * 2;
Trond Myklebust47fcb032009-05-18 17:47:56 -0400305 sock->sk->sk_write_space(sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 release_sock(sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
Chuck Lever16e4d932014-05-19 13:40:22 -0400308
Chuck Lever989f8812018-03-27 10:49:38 -0400309static void svc_sock_secure_port(struct svc_rqst *rqstp)
Chuck Lever16e4d932014-05-19 13:40:22 -0400310{
Chuck Lever989f8812018-03-27 10:49:38 -0400311 if (svc_port_is_privileged(svc_addr(rqstp)))
312 set_bit(RQ_SECURE, &rqstp->rq_flags);
313 else
314 clear_bit(RQ_SECURE, &rqstp->rq_flags);
Chuck Lever16e4d932014-05-19 13:40:22 -0400315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/*
318 * INET callback when data has been received on the socket.
319 */
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400320static void svc_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Neil Brown939bb7e2005-09-13 01:25:39 -0700322 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Neil Brown939bb7e2005-09-13 01:25:39 -0700324 if (svsk) {
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400325 /* Refer to svc_setup_socket() for details. */
326 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400327 svsk->sk_odata(sk);
Chuck Lever998024d2020-03-17 15:06:31 -0400328 trace_svcsock_data_ready(&svsk->sk_xprt, 0);
Trond Myklebust4720b072016-06-24 10:55:49 -0400329 if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags))
330 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334/*
335 * INET callback when space is newly available on the socket.
336 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600337static void svc_write_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
340
341 if (svsk) {
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400342 /* Refer to svc_setup_socket() for details. */
343 rmb();
Chuck Lever998024d2020-03-17 15:06:31 -0400344 trace_svcsock_write_space(&svsk->sk_xprt, 0);
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400345 svsk->sk_owspace(sk);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600346 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Trond Myklebustc7fb3f02014-07-24 23:59:32 -0400350static int svc_tcp_has_wspace(struct svc_xprt *xprt)
351{
Trond Myklebust637600f2016-06-24 10:55:51 -0400352 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Trond Myklebustc7fb3f02014-07-24 23:59:32 -0400353
354 if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
355 return 1;
Trond Myklebust637600f2016-06-24 10:55:51 -0400356 return !test_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Trond Myklebust51877682014-07-24 23:59:33 -0400357}
358
Scott Mayhewea08e392016-11-11 13:16:22 -0500359static void svc_tcp_kill_temp_xprt(struct svc_xprt *xprt)
360{
Christoph Hellwigc4335942020-05-28 07:12:10 +0200361 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Scott Mayhewea08e392016-11-11 13:16:22 -0500362
Christoph Hellwigc4335942020-05-28 07:12:10 +0200363 sock_no_linger(svsk->sk_sock->sk);
Scott Mayhewea08e392016-11-11 13:16:22 -0500364}
365
Tom Tucker9dbc2402007-12-30 21:08:12 -0600366/*
Chuck Lever7702ce42009-07-13 10:54:26 -0400367 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
368 */
369static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
370 struct cmsghdr *cmh)
371{
372 struct in_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800373 struct sockaddr_in *daddr = svc_daddr_in(rqstp);
374
Chuck Lever7702ce42009-07-13 10:54:26 -0400375 if (cmh->cmsg_type != IP_PKTINFO)
376 return 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800377
378 daddr->sin_family = AF_INET;
379 daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
Chuck Lever7702ce42009-07-13 10:54:26 -0400380 return 1;
381}
382
383/*
Tom Parkin73df66f2013-01-31 01:02:24 +0000384 * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
Chuck Lever7702ce42009-07-13 10:54:26 -0400385 */
386static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
387 struct cmsghdr *cmh)
388{
389 struct in6_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800390 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
391
Chuck Lever7702ce42009-07-13 10:54:26 -0400392 if (cmh->cmsg_type != IPV6_PKTINFO)
393 return 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800394
395 daddr->sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000396 daddr->sin6_addr = pki->ipi6_addr;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800397 daddr->sin6_scope_id = pki->ipi6_ifindex;
Chuck Lever7702ce42009-07-13 10:54:26 -0400398 return 1;
399}
400
401/*
Tom Tucker9dbc2402007-12-30 21:08:12 -0600402 * Copy the UDP datagram's destination address to the rqstp structure.
403 * The 'destination' address in this case is the address to which the
404 * peer sent the datagram, i.e. our local address. For multihomed
405 * hosts, this can change from msg to msg. Note that only the IP
406 * address changes, the port number should remain the same.
407 */
Chuck Lever7702ce42009-07-13 10:54:26 -0400408static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
409 struct cmsghdr *cmh)
Chuck Lever95756482007-02-12 00:53:38 -0800410{
Chuck Lever7702ce42009-07-13 10:54:26 -0400411 switch (cmh->cmsg_level) {
412 case SOL_IP:
413 return svc_udp_get_dest_address4(rqstp, cmh);
414 case SOL_IPV6:
415 return svc_udp_get_dest_address6(rqstp, cmh);
Chuck Lever95756482007-02-12 00:53:38 -0800416 }
Chuck Lever7702ce42009-07-13 10:54:26 -0400417
418 return 0;
Chuck Lever95756482007-02-12 00:53:38 -0800419}
420
Chuck Leverfff1ebb2020-05-20 17:30:24 -0400421/**
422 * svc_udp_recvfrom - Receive a datagram from a UDP socket.
423 * @rqstp: request structure into which to receive an RPC Call
424 *
425 * Called in a loop when XPT_DATA has been set.
426 *
427 * Returns:
428 * On success, the number of bytes in a received RPC Call, or
429 * %0 if a complete RPC Call message was not ready to return
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600431static int svc_udp_recvfrom(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600433 struct svc_sock *svsk =
434 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600435 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct sk_buff *skb;
David S. Millerbc375ea2007-04-12 13:35:59 -0700437 union {
438 struct cmsghdr hdr;
439 long all[SVC_PKTINFO_SPACE / sizeof(long)];
440 } buffer;
441 struct cmsghdr *cmh = &buffer.hdr;
NeilBrown7a37f572007-03-06 01:42:21 -0800442 struct msghdr msg = {
443 .msg_name = svc_addr(rqstp),
444 .msg_control = cmh,
445 .msg_controllen = sizeof(buffer),
446 .msg_flags = MSG_DONTWAIT,
447 };
Chuck Leverabc5c442009-04-23 19:31:25 -0400448 size_t len;
449 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Tom Tucker02fc6c32007-12-30 21:07:48 -0600451 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /* udp sockets need large rcvbuf as all pending
453 * requests are still in that buffer. sndbuf must
454 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700455 * for one reply per thread. We count all threads
456 * rather than threads in a particular pool, which
457 * provides an upper bound on the number of threads
458 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 */
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500460 svc_sock_setbufsize(svsk, serv->sv_nrthreads + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Tom Tucker02fc6c32007-12-30 21:07:48 -0600462 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
NeilBrown05ed6902007-05-09 02:34:55 -0700463 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
464 0, 0, MSG_PEEK | MSG_DONTWAIT);
Chuck Leverfff1ebb2020-05-20 17:30:24 -0400465 if (err < 0)
466 goto out_recv_err;
467 skb = skb_recv_udp(svsk->sk_sk, 0, 1, &err);
468 if (!skb)
469 goto out_recv_err;
NeilBrown05ed6902007-05-09 02:34:55 -0700470
Tom Tucker9dbc2402007-12-30 21:08:12 -0600471 len = svc_addr_len(svc_addr(rqstp));
Tom Tucker9dbc2402007-12-30 21:08:12 -0600472 rqstp->rq_addrlen = len;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100473 if (skb->tstamp == 0) {
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700474 skb->tstamp = ktime_get_real();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800475 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 need that much accuracy */
477 }
Deepa Dinamani3a0ed3e92018-12-27 18:55:09 -0800478 sock_write_timestamp(svsk->sk_sk, skb->tstamp);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600479 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Chuck Leverfff1ebb2020-05-20 17:30:24 -0400481 len = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 rqstp->rq_arg.len = len;
Chuck Leverfff1ebb2020-05-20 17:30:24 -0400483 trace_svcsock_udp_recv(&svsk->sk_xprt, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Chuck Lever95756482007-02-12 00:53:38 -0800485 rqstp->rq_prot = IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Chuck Leverfff1ebb2020-05-20 17:30:24 -0400487 if (!svc_udp_get_dest_address(rqstp, cmh))
488 goto out_cmsg_err;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800489 rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 if (skb_is_nonlinear(skb)) {
492 /* we have to copy */
493 local_bh_disable();
Chuck Leverfff1ebb2020-05-20 17:30:24 -0400494 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb))
495 goto out_bh_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 local_bh_enable();
Paolo Abeni850cbad2016-10-21 13:55:47 +0200497 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 } else {
499 /* we can use it in-place */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -0400500 rqstp->rq_arg.head[0].iov_base = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 rqstp->rq_arg.head[0].iov_len = len;
Eric Dumazet22911fc2012-06-27 00:23:44 +0000502 if (skb_checksum_complete(skb))
503 goto out_free;
Tom Tucker5148bf42007-12-30 21:07:25 -0600504 rqstp->rq_xprt_ctxt = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506
507 rqstp->rq_arg.page_base = 0;
508 if (len <= rqstp->rq_arg.head[0].iov_len) {
509 rqstp->rq_arg.head[0].iov_len = len;
510 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700511 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 } else {
513 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700514 rqstp->rq_respages = rqstp->rq_pages + 1 +
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700515 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500517 rqstp->rq_next_page = rqstp->rq_respages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (serv->sv_stats)
520 serv->sv_stats->netudpcnt++;
521
Chuck Lever82011c82021-01-05 10:15:09 -0500522 svc_xprt_received(rqstp->rq_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return len;
Chuck Leverfff1ebb2020-05-20 17:30:24 -0400524
525out_recv_err:
526 if (err != -EAGAIN) {
527 /* possibly an icmp error */
528 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
529 }
530 trace_svcsock_udp_recv_err(&svsk->sk_xprt, err);
Chuck Lever82011c82021-01-05 10:15:09 -0500531 goto out_clear_busy;
Chuck Leverfff1ebb2020-05-20 17:30:24 -0400532out_cmsg_err:
533 net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
534 cmh->cmsg_level, cmh->cmsg_type);
535 goto out_free;
536out_bh_enable:
537 local_bh_enable();
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400538out_free:
Paolo Abeni850cbad2016-10-21 13:55:47 +0200539 kfree_skb(skb);
Chuck Lever82011c82021-01-05 10:15:09 -0500540out_clear_busy:
541 svc_xprt_received(rqstp->rq_xprt);
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Chuck Leverda1661b2020-03-02 15:20:33 -0500545/**
546 * svc_udp_sendto - Send out a reply on a UDP socket
547 * @rqstp: completed svc_rqst
548 *
Chuck Leverca4faf52020-05-02 10:37:44 -0400549 * xpt_mutex ensures @rqstp's whole message is written to the socket
550 * without interruption.
551 *
Chuck Leverda1661b2020-03-02 15:20:33 -0500552 * Returns the number of bytes sent, or a negative errno.
553 */
554static int svc_udp_sendto(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Chuck Leverda1661b2020-03-02 15:20:33 -0500556 struct svc_xprt *xprt = rqstp->rq_xprt;
557 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
558 struct xdr_buf *xdr = &rqstp->rq_res;
559 union {
560 struct cmsghdr hdr;
561 long all[SVC_PKTINFO_SPACE / sizeof(long)];
562 } buffer;
563 struct cmsghdr *cmh = &buffer.hdr;
564 struct msghdr msg = {
565 .msg_name = &rqstp->rq_addr,
566 .msg_namelen = rqstp->rq_addrlen,
567 .msg_control = cmh,
568 .msg_controllen = sizeof(buffer),
569 };
Kees Cook3f649ab2020-06-03 13:09:38 -0700570 unsigned int sent;
Chuck Leverda1661b2020-03-02 15:20:33 -0500571 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Chuck Levercca557a2020-05-20 12:53:05 -0400573 svc_udp_release_rqst(rqstp);
Chuck Lever23cf1ee2020-03-31 17:02:33 -0400574
Chuck Leverda1661b2020-03-02 15:20:33 -0500575 svc_set_cmsg_data(rqstp, cmh);
576
Chuck Leverca4faf52020-05-02 10:37:44 -0400577 mutex_lock(&xprt->xpt_mutex);
578
579 if (svc_xprt_is_dead(xprt))
580 goto out_notconn;
581
Chuck Leverda1661b2020-03-02 15:20:33 -0500582 err = xprt_sock_sendmsg(svsk->sk_sock, &msg, xdr, 0, 0, &sent);
583 xdr_free_bvec(xdr);
584 if (err == -ECONNREFUSED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* ICMP error on earlier request. */
Chuck Leverda1661b2020-03-02 15:20:33 -0500586 err = xprt_sock_sendmsg(svsk->sk_sock, &msg, xdr, 0, 0, &sent);
587 xdr_free_bvec(xdr);
588 }
Chuck Lever998024d2020-03-17 15:06:31 -0400589 trace_svcsock_udp_send(xprt, err);
Chuck Leverca4faf52020-05-02 10:37:44 -0400590
591 mutex_unlock(&xprt->xpt_mutex);
Chuck Leverda1661b2020-03-02 15:20:33 -0500592 if (err < 0)
593 return err;
594 return sent;
Chuck Leverca4faf52020-05-02 10:37:44 -0400595
596out_notconn:
597 mutex_unlock(&xprt->xpt_mutex);
598 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Tom Tucker323bee32007-12-30 21:07:31 -0600601static int svc_udp_has_wspace(struct svc_xprt *xprt)
602{
603 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600604 struct svc_serv *serv = xprt->xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -0600605 unsigned long required;
606
607 /*
608 * Set the SOCK_NOSPACE flag before checking the available
609 * sock space.
610 */
611 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Tom Tucker7a90e8c2007-12-30 21:07:55 -0600612 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
Tom Tucker323bee32007-12-30 21:07:31 -0600613 if (required*2 > sock_wspace(svsk->sk_sk))
614 return 0;
615 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
616 return 1;
617}
618
Tom Tucker38a417c2007-12-30 21:07:36 -0600619static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
620{
621 BUG();
622 return NULL;
623}
624
Scott Mayhewea08e392016-11-11 13:16:22 -0500625static void svc_udp_kill_temp_xprt(struct svc_xprt *xprt)
626{
627}
628
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600629static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400630 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600631 struct sockaddr *sa, int salen,
632 int flags)
633{
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400634 return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600635}
636
Chuck Lever2412e922017-08-01 11:59:49 -0400637static const struct svc_xprt_ops svc_udp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600638 .xpo_create = svc_udp_create,
Tom Tucker5d137992007-12-30 21:07:23 -0600639 .xpo_recvfrom = svc_udp_recvfrom,
640 .xpo_sendto = svc_udp_sendto,
Chuck Lever03493bc2020-06-10 10:36:42 -0400641 .xpo_result_payload = svc_sock_result_payload,
Chuck Levercca557a2020-05-20 12:53:05 -0400642 .xpo_release_rqst = svc_udp_release_rqst,
Tom Tucker755ccea2007-12-30 21:07:27 -0600643 .xpo_detach = svc_sock_detach,
644 .xpo_free = svc_sock_free,
Tom Tucker323bee32007-12-30 21:07:31 -0600645 .xpo_has_wspace = svc_udp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -0600646 .xpo_accept = svc_udp_accept,
Chuck Lever16e4d932014-05-19 13:40:22 -0400647 .xpo_secure_port = svc_sock_secure_port,
Scott Mayhewea08e392016-11-11 13:16:22 -0500648 .xpo_kill_temp_xprt = svc_udp_kill_temp_xprt,
Tom Tucker360d87382007-12-30 21:07:17 -0600649};
650
651static struct svc_xprt_class svc_udp_class = {
652 .xcl_name = "udp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600653 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -0600654 .xcl_ops = &svc_udp_ops,
Tom Tucker49023152007-12-30 21:07:21 -0600655 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
Chuck Lever3c45ddf2014-07-16 15:38:32 -0400656 .xcl_ident = XPRT_TRANSPORT_UDP,
Tom Tucker360d87382007-12-30 21:07:17 -0600657};
658
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600659static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +0300661 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
662 &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600663 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400664 svsk->sk_sk->sk_data_ready = svc_data_ready;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 svsk->sk_sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800668 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 * svc_udp_recvfrom will re-adjust if necessary
670 */
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500671 svc_sock_setbufsize(svsk, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Tom Tucker0f0257e2007-12-30 21:08:27 -0600673 /* data might have come in before data_ready set up */
674 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600675 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800676
NeilBrown7a37f572007-03-06 01:42:21 -0800677 /* make sure we get destination address info */
Chuck Lever7702ce42009-07-13 10:54:26 -0400678 switch (svsk->sk_sk->sk_family) {
679 case AF_INET:
Christoph Hellwigc1f9ec52020-05-28 07:12:30 +0200680 ip_sock_set_pktinfo(svsk->sk_sock->sk);
Christoph Hellwig7d7207c2020-05-28 07:12:34 +0200681 break;
Chuck Lever7702ce42009-07-13 10:54:26 -0400682 case AF_INET6:
Christoph Hellwig7d7207c2020-05-28 07:12:34 +0200683 ip6_sock_set_recvpktinfo(svsk->sk_sock->sk);
Chuck Lever7702ce42009-07-13 10:54:26 -0400684 break;
685 default:
686 BUG();
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690/*
691 * A data_ready event on a listening socket means there's a connection
692 * pending. Do not use state_change as a substitute for it.
693 */
David S. Miller676d2362014-04-11 16:15:36 -0400694static void svc_tcp_listen_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Neil Brown939bb7e2005-09-13 01:25:39 -0700696 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400698 if (svsk) {
699 /* Refer to svc_setup_socket() for details. */
700 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400701 svsk->sk_odata(sk);
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400702 }
703
Neil Brown939bb7e2005-09-13 01:25:39 -0700704 /*
705 * This callback may called twice when a new connection
706 * is established as a child socket inherits everything
707 * from a parent LISTEN socket.
708 * 1) data_ready method of the parent socket will be called
709 * when one of child sockets become ESTABLISHED.
710 * 2) data_ready method of the child socket may be called
711 * when it receives data before the socket is accepted.
712 * In case of 2, we should ignore it silently.
713 */
714 if (sk->sk_state == TCP_LISTEN) {
715 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600716 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600717 svc_xprt_enqueue(&svsk->sk_xprt);
Chuck Levera0469f42020-03-18 11:20:50 -0400718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722/*
723 * A state change on a connected socket means it's dying or dead.
724 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600725static void svc_tcp_state_change(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Neil Brown939bb7e2005-09-13 01:25:39 -0700727 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Chuck Levera0469f42020-03-18 11:20:50 -0400729 if (svsk) {
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400730 /* Refer to svc_setup_socket() for details. */
731 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400732 svsk->sk_ostate(sk);
Chuck Levera0469f42020-03-18 11:20:50 -0400733 trace_svcsock_tcp_state(&svsk->sk_xprt, svsk->sk_sock);
Chuck Levere844d302021-02-20 18:53:40 -0500734 if (sk->sk_state != TCP_ESTABLISHED)
735 svc_xprt_deferred_close(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739/*
740 * Accept a TCP connection
741 */
Tom Tucker38a417c2007-12-30 21:07:36 -0600742static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Tom Tucker38a417c2007-12-30 21:07:36 -0600744 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800745 struct sockaddr_storage addr;
746 struct sockaddr *sin = (struct sockaddr *) &addr;
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600747 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 struct socket *sock = svsk->sk_sock;
749 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 struct svc_sock *newsvsk;
751 int err, slen;
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (!sock)
Tom Tucker38a417c2007-12-30 21:07:36 -0600754 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Tom Tucker02fc6c32007-12-30 21:07:48 -0600756 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700757 err = kernel_accept(sock, &newsock, O_NONBLOCK);
758 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (err == -ENOMEM)
760 printk(KERN_WARNING "%s: no more sockets!\n",
761 serv->sv_name);
Joe Perchese87cc472012-05-13 21:56:26 +0000762 else if (err != -EAGAIN)
763 net_warn_ratelimited("%s: accept failed (err %d)!\n",
764 serv->sv_name, -err);
Chuck Levera0469f42020-03-18 11:20:50 -0400765 trace_svcsock_accept_err(xprt, serv->sv_name, err);
Tom Tucker38a417c2007-12-30 21:07:36 -0600766 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Tom Tucker02fc6c32007-12-30 21:07:48 -0600768 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100770 err = kernel_getpeername(newsock, sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (err < 0) {
Chuck Levera0469f42020-03-18 11:20:50 -0400772 trace_svcsock_getpeername_err(xprt, serv->sv_name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 goto failed; /* aborted connection or whatever */
774 }
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100775 slen = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400777 /* Reset the inherited callbacks before calling svc_setup_socket */
778 newsock->sk->sk_state_change = svsk->sk_ostate;
779 newsock->sk->sk_data_ready = svsk->sk_odata;
780 newsock->sk->sk_write_space = svsk->sk_owspace;
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /* make sure that a write doesn't block forever when
783 * low on memory
784 */
785 newsock->sk->sk_sndtimeo = HZ*30;
786
J. Bruce Fields72c35372012-08-13 17:46:17 -0400787 newsvsk = svc_setup_socket(serv, newsock,
788 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
789 if (IS_ERR(newsvsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 goto failed;
Tom Tucker9dbc2402007-12-30 21:08:12 -0600791 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100792 err = kernel_getsockname(newsock, sin);
793 slen = err;
Chuck Levera0469f42020-03-18 11:20:50 -0400794 if (unlikely(err < 0))
Frank van Maarseveena9747692007-07-09 22:21:39 +0200795 slen = offsetof(struct sockaddr, sa_data);
Tom Tucker9dbc2402007-12-30 21:08:12 -0600796 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -0800797
NeilBrownef11ce22014-05-12 11:22:47 +1000798 if (sock_is_loopback(newsock->sk))
799 set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
800 else
801 clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600802 if (serv->sv_stats)
803 serv->sv_stats->nettcpconn++;
804
805 return &newsvsk->sk_xprt;
806
807failed:
808 sock_release(newsock);
809 return NULL;
810}
811
Chuck Leverca07eda2020-05-20 17:30:12 -0400812static size_t svc_tcp_restore_pages(struct svc_sock *svsk,
813 struct svc_rqst *rqstp)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800814{
Chuck Leverca07eda2020-05-20 17:30:12 -0400815 size_t len = svsk->sk_datalen;
816 unsigned int i, npages;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800817
Chuck Leverca07eda2020-05-20 17:30:12 -0400818 if (!len)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800819 return 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800820 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
821 for (i = 0; i < npages; i++) {
822 if (rqstp->rq_pages[i] != NULL)
823 put_page(rqstp->rq_pages[i]);
824 BUG_ON(svsk->sk_pages[i] == NULL);
825 rqstp->rq_pages[i] = svsk->sk_pages[i];
826 svsk->sk_pages[i] = NULL;
827 }
828 rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
829 return len;
830}
831
832static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
833{
834 unsigned int i, len, npages;
835
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500836 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800837 return;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500838 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800839 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
840 for (i = 0; i < npages; i++) {
841 svsk->sk_pages[i] = rqstp->rq_pages[i];
842 rqstp->rq_pages[i] = NULL;
843 }
844}
845
846static void svc_tcp_clear_pages(struct svc_sock *svsk)
847{
848 unsigned int i, len, npages;
849
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500850 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800851 goto out;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500852 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800853 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
854 for (i = 0; i < npages; i++) {
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -0400855 if (svsk->sk_pages[i] == NULL) {
856 WARN_ON_ONCE(1);
857 continue;
858 }
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800859 put_page(svsk->sk_pages[i]);
860 svsk->sk_pages[i] = NULL;
861 }
862out:
863 svsk->sk_tcplen = 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500864 svsk->sk_datalen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800865}
866
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600867/*
Chuck Levera5cda732020-03-16 14:53:04 -0400868 * Receive fragment record header into sk_marker.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
Chuck Levera5cda732020-03-16 14:53:04 -0400870static ssize_t svc_tcp_read_marker(struct svc_sock *svsk,
871 struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Chuck Levera5cda732020-03-16 14:53:04 -0400873 ssize_t want, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Chuck Levera5cda732020-03-16 14:53:04 -0400875 /* If we haven't gotten the record length yet,
876 * get the next four bytes.
877 */
Chuck Leverc0401ea2008-04-14 12:27:30 -0400878 if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
Chuck Levera5cda732020-03-16 14:53:04 -0400879 struct msghdr msg = { NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct kvec iov;
881
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400882 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
Chuck Lever02648902020-03-17 14:12:15 -0400883 iov.iov_base = ((char *)&svsk->sk_marker) + svsk->sk_tcplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 iov.iov_len = want;
Chuck Levera5cda732020-03-16 14:53:04 -0400885 iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, want);
886 len = sock_recvmsg(svsk->sk_sock, &msg, MSG_DONTWAIT);
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400887 if (len < 0)
Chuck Levera5cda732020-03-16 14:53:04 -0400888 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 svsk->sk_tcplen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (len < want) {
Chuck Levera5cda732020-03-16 14:53:04 -0400891 /* call again to read the remaining bytes */
892 goto err_short;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
Chuck Levera5cda732020-03-16 14:53:04 -0400894 trace_svcsock_marker(&svsk->sk_xprt, svsk->sk_marker);
J. Bruce Fields836fbad2012-12-03 15:50:38 -0500895 if (svc_sock_reclen(svsk) + svsk->sk_datalen >
Chuck Levera5cda732020-03-16 14:53:04 -0400896 svsk->sk_xprt.xpt_server->sv_max_mesg)
897 goto err_too_large;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500899 return svc_sock_reclen(svsk);
Chuck Levera5cda732020-03-16 14:53:04 -0400900
901err_too_large:
902 net_notice_ratelimited("svc: %s %s RPC fragment too large: %d\n",
903 __func__, svsk->sk_xprt.xpt_server->sv_name,
904 svc_sock_reclen(svsk));
Chuck Levere844d302021-02-20 18:53:40 -0500905 svc_xprt_deferred_close(&svsk->sk_xprt);
Chuck Levera5cda732020-03-16 14:53:04 -0400906err_short:
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300907 return -EAGAIN;
908}
909
Trond Myklebust586c52c2009-05-18 17:47:56 -0400910static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300911{
Trond Myklebust586c52c2009-05-18 17:47:56 -0400912 struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300913 struct rpc_rqst *req = NULL;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400914 struct kvec *src, *dst;
915 __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
J. Bruce Fields48e65552011-02-14 14:52:03 -0500916 __be32 xid;
917 __be32 calldir;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300918
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300919 xid = *p++;
920 calldir = *p;
921
Trond Myklebust093a1462014-11-12 18:04:04 -0500922 if (!bc_xprt)
Trond Myklebust586c52c2009-05-18 17:47:56 -0400923 return -EAGAIN;
Trond Myklebust75c84152018-08-31 10:21:00 -0400924 spin_lock(&bc_xprt->queue_lock);
Trond Myklebust093a1462014-11-12 18:04:04 -0500925 req = xprt_lookup_rqst(bc_xprt, xid);
926 if (!req)
927 goto unlock_notfound;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400928
929 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
930 /*
931 * XXX!: cheating for now! Only copying HEAD.
932 * But we know this is good enough for now (in fact, for any
933 * callback reply in the forseeable future).
934 */
935 dst = &req->rq_private_buf.head[0];
936 src = &rqstp->rq_arg.head[0];
937 if (dst->iov_len < src->iov_len)
Trond Myklebust093a1462014-11-12 18:04:04 -0500938 goto unlock_eagain; /* whatever; just giving up. */
Trond Myklebust586c52c2009-05-18 17:47:56 -0400939 memcpy(dst->iov_base, src->iov_base, src->iov_len);
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500940 xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len);
Trond Myklebust586c52c2009-05-18 17:47:56 -0400941 rqstp->rq_arg.len = 0;
Trond Myklebust75c84152018-08-31 10:21:00 -0400942 spin_unlock(&bc_xprt->queue_lock);
Trond Myklebust586c52c2009-05-18 17:47:56 -0400943 return 0;
Trond Myklebust093a1462014-11-12 18:04:04 -0500944unlock_notfound:
945 printk(KERN_NOTICE
946 "%s: Got unrecognized reply: "
947 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
948 __func__, ntohl(calldir),
949 bc_xprt, ntohl(xid));
950unlock_eagain:
Trond Myklebust75c84152018-08-31 10:21:00 -0400951 spin_unlock(&bc_xprt->queue_lock);
Trond Myklebust093a1462014-11-12 18:04:04 -0500952 return -EAGAIN;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400953}
954
J. Bruce Fields836fbad2012-12-03 15:50:38 -0500955static void svc_tcp_fragment_received(struct svc_sock *svsk)
956{
957 /* If we have more data, signal svc_xprt_enqueue() to try again */
J. Bruce Fields836fbad2012-12-03 15:50:38 -0500958 svsk->sk_tcplen = 0;
Chuck Lever02648902020-03-17 14:12:15 -0400959 svsk->sk_marker = xdr_zero;
J. Bruce Fields836fbad2012-12-03 15:50:38 -0500960}
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800961
Chuck Leverca07eda2020-05-20 17:30:12 -0400962/**
963 * svc_tcp_recvfrom - Receive data from a TCP socket
964 * @rqstp: request structure into which to receive an RPC Call
965 *
966 * Called in a loop when XPT_DATA has been set.
967 *
968 * Read the 4-byte stream record marker, then use the record length
969 * in that marker to set up exactly the resources needed to receive
970 * the next RPC message into @rqstp.
971 *
972 * Returns:
973 * On success, the number of bytes in a received RPC Call, or
974 * %0 if a complete RPC Call message was not ready to return
975 *
976 * The zero return case handles partial receives and callback Replies.
977 * The state of a partial receive is preserved in the svc_sock for
978 * the next call to svc_tcp_recvfrom.
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300979 */
980static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
981{
982 struct svc_sock *svsk =
983 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
984 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Chuck Leverca07eda2020-05-20 17:30:12 -0400985 size_t want, base;
986 ssize_t len;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400987 __be32 *p;
988 __be32 calldir;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300989
Chuck Levera5cda732020-03-16 14:53:04 -0400990 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
991 len = svc_tcp_read_marker(svsk, rqstp);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300992 if (len < 0)
993 goto error;
994
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800995 base = svc_tcp_restore_pages(svsk, rqstp);
Chuck Levera5cda732020-03-16 14:53:04 -0400996 want = len - (svsk->sk_tcplen - sizeof(rpc_fraghdr));
Chuck Leverca07eda2020-05-20 17:30:12 -0400997 len = svc_tcp_read_msg(rqstp, base + want, base);
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500998 if (len >= 0) {
Chuck Lever7dae1dd2020-05-20 13:41:02 -0400999 trace_svcsock_tcp_recv(&svsk->sk_xprt, len);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001000 svsk->sk_tcplen += len;
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001001 svsk->sk_datalen += len;
1002 }
Chuck Lever6be8c592020-05-20 12:29:13 -04001003 if (len != want || !svc_sock_final_rec(svsk))
1004 goto err_incomplete;
1005 if (svsk->sk_datalen < 8)
1006 goto err_nuts;
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001007
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001008 rqstp->rq_arg.len = svsk->sk_datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 rqstp->rq_arg.page_base = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001010 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1011 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 rqstp->rq_arg.page_len = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001013 } else
1014 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Tom Tucker5148bf42007-12-30 21:07:25 -06001016 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 rqstp->rq_prot = IPPROTO_TCP;
Jeff Layton7501cc22014-11-19 07:51:15 -05001018 if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags))
1019 set_bit(RQ_LOCAL, &rqstp->rq_flags);
1020 else
1021 clear_bit(RQ_LOCAL, &rqstp->rq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Trond Myklebust586c52c2009-05-18 17:47:56 -04001023 p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1024 calldir = p[1];
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001025 if (calldir)
Trond Myklebust586c52c2009-05-18 17:47:56 -04001026 len = receive_cb_reply(svsk, rqstp);
Trond Myklebust586c52c2009-05-18 17:47:56 -04001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /* Reset TCP read info */
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001029 svsk->sk_datalen = 0;
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001030 svc_tcp_fragment_received(svsk);
Trond Myklebust0601f792009-05-18 17:47:56 -04001031
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001032 if (len < 0)
1033 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Tom Tucker9dbc2402007-12-30 21:08:12 -06001035 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (serv->sv_stats)
1037 serv->sv_stats->nettcpcnt++;
1038
Chuck Lever82011c82021-01-05 10:15:09 -05001039 svc_xprt_received(rqstp->rq_xprt);
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001040 return rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Chuck Lever6be8c592020-05-20 12:29:13 -04001042err_incomplete:
1043 svc_tcp_save_pages(svsk, rqstp);
1044 if (len < 0 && len != -EAGAIN)
1045 goto err_delete;
1046 if (len == want)
1047 svc_tcp_fragment_received(svsk);
1048 else
1049 trace_svcsock_tcp_recv_short(&svsk->sk_xprt,
1050 svc_sock_reclen(svsk),
1051 svsk->sk_tcplen - sizeof(rpc_fraghdr));
1052 goto err_noclose;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001053error:
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001054 if (len != -EAGAIN)
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001055 goto err_delete;
Chuck Lever6be8c592020-05-20 12:29:13 -04001056 trace_svcsock_tcp_recv_eagain(&svsk->sk_xprt, 0);
Chuck Lever82011c82021-01-05 10:15:09 -05001057 goto err_noclose;
Chuck Lever6be8c592020-05-20 12:29:13 -04001058err_nuts:
1059 svsk->sk_datalen = 0;
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001060err_delete:
Chuck Lever6be8c592020-05-20 12:29:13 -04001061 trace_svcsock_tcp_recv_err(&svsk->sk_xprt, len);
Chuck Levere844d302021-02-20 18:53:40 -05001062 svc_xprt_deferred_close(&svsk->sk_xprt);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001063err_noclose:
Chuck Lever82011c82021-01-05 10:15:09 -05001064 svc_xprt_received(rqstp->rq_xprt);
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -04001065 return 0; /* record not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Chuck Lever4a85a6a2020-12-18 12:28:41 -05001068static int svc_tcp_send_kvec(struct socket *sock, const struct kvec *vec,
1069 int flags)
1070{
1071 return kernel_sendpage(sock, virt_to_page(vec->iov_base),
1072 offset_in_page(vec->iov_base),
1073 vec->iov_len, flags);
1074}
1075
1076/*
1077 * kernel_sendpage() is used exclusively to reduce the number of
1078 * copy operations in this path. Therefore the caller must ensure
1079 * that the pages backing @xdr are unchanging.
1080 *
1081 * In addition, the logic assumes that * .bv_len is never larger
1082 * than PAGE_SIZE.
1083 */
Chuck Lever4d12b722021-02-16 12:37:40 -05001084static int svc_tcp_sendmsg(struct socket *sock, struct xdr_buf *xdr,
1085 rpc_fraghdr marker, unsigned int *sentp)
Chuck Lever4a85a6a2020-12-18 12:28:41 -05001086{
1087 const struct kvec *head = xdr->head;
1088 const struct kvec *tail = xdr->tail;
1089 struct kvec rm = {
1090 .iov_base = &marker,
1091 .iov_len = sizeof(marker),
1092 };
Chuck Lever4d12b722021-02-16 12:37:40 -05001093 struct msghdr msg = {
1094 .msg_flags = 0,
1095 };
Trond Myklebust987c7b12021-02-16 12:17:23 -05001096 int ret;
Chuck Lever4a85a6a2020-12-18 12:28:41 -05001097
1098 *sentp = 0;
1099 xdr_alloc_bvec(xdr, GFP_KERNEL);
1100
Chuck Lever4d12b722021-02-16 12:37:40 -05001101 ret = kernel_sendmsg(sock, &msg, &rm, 1, rm.iov_len);
Chuck Lever4a85a6a2020-12-18 12:28:41 -05001102 if (ret < 0)
1103 return ret;
1104 *sentp += ret;
1105 if (ret != rm.iov_len)
1106 return -EAGAIN;
1107
Trond Myklebust987c7b12021-02-16 12:17:23 -05001108 ret = svc_tcp_send_kvec(sock, head, 0);
Chuck Lever4a85a6a2020-12-18 12:28:41 -05001109 if (ret < 0)
1110 return ret;
1111 *sentp += ret;
1112 if (ret != head->iov_len)
1113 goto out;
1114
1115 if (xdr->page_len) {
1116 unsigned int offset, len, remaining;
1117 struct bio_vec *bvec;
1118
Chuck Leverbad4c6e2021-01-31 16:16:23 -05001119 bvec = xdr->bvec + (xdr->page_base >> PAGE_SHIFT);
1120 offset = offset_in_page(xdr->page_base);
Chuck Lever4a85a6a2020-12-18 12:28:41 -05001121 remaining = xdr->page_len;
Chuck Lever4a85a6a2020-12-18 12:28:41 -05001122 while (remaining > 0) {
Chuck Leverbad4c6e2021-01-31 16:16:23 -05001123 len = min(remaining, bvec->bv_len - offset);
Chuck Lever4a85a6a2020-12-18 12:28:41 -05001124 ret = kernel_sendpage(sock, bvec->bv_page,
1125 bvec->bv_offset + offset,
Trond Myklebust987c7b12021-02-16 12:17:23 -05001126 len, 0);
Chuck Lever4a85a6a2020-12-18 12:28:41 -05001127 if (ret < 0)
1128 return ret;
1129 *sentp += ret;
1130 if (ret != len)
1131 goto out;
1132 remaining -= len;
1133 offset = 0;
1134 bvec++;
1135 }
1136 }
1137
1138 if (tail->iov_len) {
1139 ret = svc_tcp_send_kvec(sock, tail, 0);
1140 if (ret < 0)
1141 return ret;
1142 *sentp += ret;
1143 }
1144
1145out:
1146 return 0;
1147}
1148
Chuck Leverda1661b2020-03-02 15:20:33 -05001149/**
1150 * svc_tcp_sendto - Send out a reply on a TCP socket
1151 * @rqstp: completed svc_rqst
1152 *
Chuck Leverca4faf52020-05-02 10:37:44 -04001153 * xpt_mutex ensures @rqstp's whole message is written to the socket
1154 * without interruption.
1155 *
Chuck Leverda1661b2020-03-02 15:20:33 -05001156 * Returns the number of bytes sent, or a negative errno.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 */
Tom Tucker0f0257e2007-12-30 21:08:27 -06001158static int svc_tcp_sendto(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Chuck Leverda1661b2020-03-02 15:20:33 -05001160 struct svc_xprt *xprt = rqstp->rq_xprt;
1161 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1162 struct xdr_buf *xdr = &rqstp->rq_res;
1163 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
1164 (u32)xdr->len);
Kees Cook3f649ab2020-06-03 13:09:38 -07001165 unsigned int sent;
Chuck Leverda1661b2020-03-02 15:20:33 -05001166 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Chuck Levercca557a2020-05-20 12:53:05 -04001168 svc_tcp_release_rqst(rqstp);
Chuck Lever23cf1ee2020-03-31 17:02:33 -04001169
Trond Myklebuste0a912e2021-02-16 12:17:22 -05001170 atomic_inc(&svsk->sk_sendqlen);
Chuck Leverca4faf52020-05-02 10:37:44 -04001171 mutex_lock(&xprt->xpt_mutex);
1172 if (svc_xprt_is_dead(xprt))
1173 goto out_notconn;
Trond Myklebuste0a912e2021-02-16 12:17:22 -05001174 tcp_sock_set_cork(svsk->sk_sk, true);
Chuck Lever4d12b722021-02-16 12:37:40 -05001175 err = svc_tcp_sendmsg(svsk->sk_sock, xdr, marker, &sent);
Chuck Leverda1661b2020-03-02 15:20:33 -05001176 xdr_free_bvec(xdr);
Dan Carpentercb5790862021-04-22 12:14:37 +03001177 trace_svcsock_tcp_send(xprt, err < 0 ? (long)err : sent);
Chuck Leverda1661b2020-03-02 15:20:33 -05001178 if (err < 0 || sent != (xdr->len + sizeof(marker)))
1179 goto out_close;
Trond Myklebuste0a912e2021-02-16 12:17:22 -05001180 if (atomic_dec_and_test(&svsk->sk_sendqlen))
1181 tcp_sock_set_cork(svsk->sk_sk, false);
Chuck Leverca4faf52020-05-02 10:37:44 -04001182 mutex_unlock(&xprt->xpt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return sent;
Chuck Leverda1661b2020-03-02 15:20:33 -05001184
Chuck Leverca4faf52020-05-02 10:37:44 -04001185out_notconn:
Trond Myklebuste0a912e2021-02-16 12:17:22 -05001186 atomic_dec(&svsk->sk_sendqlen);
Chuck Leverca4faf52020-05-02 10:37:44 -04001187 mutex_unlock(&xprt->xpt_mutex);
1188 return -ENOTCONN;
Chuck Leverda1661b2020-03-02 15:20:33 -05001189out_close:
1190 pr_notice("rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1191 xprt->xpt_server->sv_name,
1192 (err < 0) ? "got error" : "sent",
1193 (err < 0) ? err : sent, xdr->len);
Chuck Levere844d302021-02-20 18:53:40 -05001194 svc_xprt_deferred_close(xprt);
Trond Myklebuste0a912e2021-02-16 12:17:22 -05001195 atomic_dec(&svsk->sk_sendqlen);
Chuck Leverca4faf52020-05-02 10:37:44 -04001196 mutex_unlock(&xprt->xpt_mutex);
Chuck Leverda1661b2020-03-02 15:20:33 -05001197 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001200static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001201 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001202 struct sockaddr *sa, int salen,
1203 int flags)
1204{
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001205 return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001206}
1207
Chuck Lever2412e922017-08-01 11:59:49 -04001208static const struct svc_xprt_ops svc_tcp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001209 .xpo_create = svc_tcp_create,
Tom Tucker5d137992007-12-30 21:07:23 -06001210 .xpo_recvfrom = svc_tcp_recvfrom,
1211 .xpo_sendto = svc_tcp_sendto,
Chuck Lever03493bc2020-06-10 10:36:42 -04001212 .xpo_result_payload = svc_sock_result_payload,
Chuck Levercca557a2020-05-20 12:53:05 -04001213 .xpo_release_rqst = svc_tcp_release_rqst,
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001214 .xpo_detach = svc_tcp_sock_detach,
Tom Tucker755ccea2007-12-30 21:07:27 -06001215 .xpo_free = svc_sock_free,
Tom Tucker323bee32007-12-30 21:07:31 -06001216 .xpo_has_wspace = svc_tcp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -06001217 .xpo_accept = svc_tcp_accept,
Chuck Lever16e4d932014-05-19 13:40:22 -04001218 .xpo_secure_port = svc_sock_secure_port,
Scott Mayhewea08e392016-11-11 13:16:22 -05001219 .xpo_kill_temp_xprt = svc_tcp_kill_temp_xprt,
Tom Tucker360d87382007-12-30 21:07:17 -06001220};
1221
1222static struct svc_xprt_class svc_tcp_class = {
1223 .xcl_name = "tcp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001224 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -06001225 .xcl_ops = &svc_tcp_ops,
Tom Tucker49023152007-12-30 21:07:21 -06001226 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
Chuck Lever3c45ddf2014-07-16 15:38:32 -04001227 .xcl_ident = XPRT_TRANSPORT_TCP,
Tom Tucker360d87382007-12-30 21:07:17 -06001228};
1229
1230void svc_init_xprt_sock(void)
1231{
1232 svc_reg_xprt_class(&svc_tcp_class);
1233 svc_reg_xprt_class(&svc_udp_class);
1234}
1235
1236void svc_cleanup_xprt_sock(void)
1237{
1238 svc_unreg_xprt_class(&svc_tcp_class);
1239 svc_unreg_xprt_class(&svc_udp_class);
1240}
1241
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001242static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 struct sock *sk = svsk->sk_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +03001246 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
1247 &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -06001248 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Jeff Layton362142b22017-02-24 13:25:23 -05001249 set_bit(XPT_CONG_CTRL, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (sk->sk_state == TCP_LISTEN) {
Chuck Leverece200d2018-03-27 10:51:00 -04001251 strcpy(svsk->sk_xprt.xpt_remotebuf, "listener");
Tom Tucker02fc6c32007-12-30 21:07:48 -06001252 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 sk->sk_data_ready = svc_tcp_listen_data_ready;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001254 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 sk->sk_state_change = svc_tcp_state_change;
Trond Myklebustfa9251a2016-06-24 10:55:48 -04001257 sk->sk_data_ready = svc_data_ready;
Trond Myklebust637600f2016-06-24 10:55:51 -04001258 sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Chuck Lever02648902020-03-17 14:12:15 -04001260 svsk->sk_marker = xdr_zero;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 svsk->sk_tcplen = 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001262 svsk->sk_datalen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001263 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Trond Myklebuste0a912e2021-02-16 12:17:22 -05001265 tcp_sock_set_nodelay(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Tom Tucker02fc6c32007-12-30 21:07:48 -06001267 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Trond Myklebustb2f21f72016-07-26 09:51:19 -04001268 switch (sk->sk_state) {
1269 case TCP_SYN_RECV:
1270 case TCP_ESTABLISHED:
1271 break;
1272 default:
Chuck Levere844d302021-02-20 18:53:40 -05001273 svc_xprt_deferred_close(&svsk->sk_xprt);
Trond Myklebustb2f21f72016-07-26 09:51:19 -04001274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276}
1277
Tom Tucker0f0257e2007-12-30 21:08:27 -06001278void svc_sock_update_bufs(struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 /*
1281 * The number of server threads has changed. Update
1282 * rcvbuf and sndbuf accordingly on all sockets
1283 */
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001284 struct svc_sock *svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 spin_lock_bh(&serv->sv_lock);
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001287 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001288 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 spin_unlock_bh(&serv->sv_lock);
1290}
Trond Myklebust24c37672008-12-23 16:30:12 -05001291EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 * Initialize socket for RPC use and create svc_sock struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 */
Chuck Lever6b174332007-02-12 00:53:28 -08001296static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1297 struct socket *sock,
J. Bruce Fields72c35372012-08-13 17:46:17 -04001298 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 struct svc_sock *svsk;
1301 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001302 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001303 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
J. Bruce Fields72c35372012-08-13 17:46:17 -04001305 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1306 if (!svsk)
1307 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 inet = sock->sk;
1310
1311 /* Register socket with portmapper */
J. Bruce Fields72c35372012-08-13 17:46:17 -04001312 if (pmap_register)
1313 err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001314 inet->sk_protocol,
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001315 ntohs(inet_sk(inet)->inet_sport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
J. Bruce Fields72c35372012-08-13 17:46:17 -04001317 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 kfree(svsk);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001319 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 svsk->sk_sock = sock;
1323 svsk->sk_sk = inet;
1324 svsk->sk_ostate = inet->sk_state_change;
1325 svsk->sk_odata = inet->sk_data_ready;
1326 svsk->sk_owspace = inet->sk_write_space;
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -04001327 /*
1328 * This barrier is necessary in order to prevent race condition
1329 * with svc_data_ready(), svc_listen_data_ready() and others
1330 * when calling callbacks above.
1331 */
1332 wmb();
1333 inet->sk_user_data = svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 /* Initialize the socket */
1336 if (sock->type == SOCK_DGRAM)
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001337 svc_udp_init(svsk, serv);
Trond Myklebust637600f2016-06-24 10:55:51 -04001338 else
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001339 svc_tcp_init(svsk, serv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Chuck Lever998024d2020-03-17 15:06:31 -04001341 trace_svcsock_new_socket(sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return svsk;
1343}
1344
Stanislav Kinsbursky30646392014-02-26 16:50:01 +03001345bool svc_alien_sock(struct net *net, int fd)
1346{
1347 int err;
1348 struct socket *sock = sockfd_lookup(fd, &err);
1349 bool ret = false;
1350
1351 if (!sock)
1352 goto out;
1353 if (sock_net(sock->sk) != net)
1354 ret = true;
1355 sockfd_put(sock);
1356out:
1357 return ret;
1358}
1359EXPORT_SYMBOL_GPL(svc_alien_sock);
1360
Chuck Leverbfba9ab2009-04-23 19:32:33 -04001361/**
1362 * svc_addsock - add a listener socket to an RPC service
1363 * @serv: pointer to RPC service to which to add a new listener
1364 * @fd: file descriptor of the new listener
1365 * @name_return: pointer to buffer to fill in with name of listener
1366 * @len: size of the buffer
Trond Myklebust4df493a2019-04-09 12:13:37 -04001367 * @cred: credential
Chuck Leverbfba9ab2009-04-23 19:32:33 -04001368 *
1369 * Fills in socket name and returns positive length of name if successful.
1370 * Name is terminated with '\n'. On error, returns a negative errno
1371 * value.
1372 */
1373int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
Trond Myklebust4df493a2019-04-09 12:13:37 -04001374 const size_t len, const struct cred *cred)
NeilBrownb41b66d2006-10-02 02:17:48 -07001375{
1376 int err = 0;
1377 struct socket *so = sockfd_lookup(fd, &err);
1378 struct svc_sock *svsk = NULL;
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001379 struct sockaddr_storage addr;
1380 struct sockaddr *sin = (struct sockaddr *)&addr;
1381 int salen;
NeilBrownb41b66d2006-10-02 02:17:48 -07001382
1383 if (!so)
1384 return err;
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001385 err = -EAFNOSUPPORT;
Aime Le Rouzic205ba422010-01-26 14:03:56 -05001386 if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001387 goto out;
1388 err = -EPROTONOSUPPORT;
1389 if (so->sk->sk_protocol != IPPROTO_TCP &&
NeilBrownb41b66d2006-10-02 02:17:48 -07001390 so->sk->sk_protocol != IPPROTO_UDP)
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001391 goto out;
1392 err = -EISCONN;
1393 if (so->state > SS_UNCONNECTED)
1394 goto out;
1395 err = -ENOENT;
1396 if (!try_module_get(THIS_MODULE))
1397 goto out;
1398 svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1399 if (IS_ERR(svsk)) {
1400 module_put(THIS_MODULE);
1401 err = PTR_ERR(svsk);
1402 goto out;
NeilBrownb41b66d2006-10-02 02:17:48 -07001403 }
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001404 salen = kernel_getsockname(svsk->sk_sock, sin);
1405 if (salen >= 0)
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001406 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
Trond Myklebust4df493a2019-04-09 12:13:37 -04001407 svsk->sk_xprt.xpt_cred = get_cred(cred);
J. Bruce Fields39b55302012-08-14 15:50:34 -04001408 svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
Chuck Levere7942b92009-04-23 19:32:48 -04001409 return svc_one_sock_name(svsk, name_return, len);
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001410out:
1411 sockfd_put(so);
1412 return err;
NeilBrownb41b66d2006-10-02 02:17:48 -07001413}
1414EXPORT_SYMBOL_GPL(svc_addsock);
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416/*
1417 * Create socket for RPC service.
1418 */
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001419static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1420 int protocol,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001421 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001422 struct sockaddr *sin, int len,
1423 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 struct svc_sock *svsk;
1426 struct socket *sock;
1427 int error;
1428 int type;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001429 struct sockaddr_storage addr;
1430 struct sockaddr *newsin = (struct sockaddr *)&addr;
1431 int newlen;
Trond Myklebustc69da772009-03-30 18:59:17 -04001432 int family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1435 printk(KERN_WARNING "svc: only UDP and TCP "
1436 "sockets supported\n");
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001437 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Trond Myklebustc69da772009-03-30 18:59:17 -04001440 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1441 switch (sin->sa_family) {
1442 case AF_INET6:
1443 family = PF_INET6;
1444 break;
1445 case AF_INET:
1446 family = PF_INET;
1447 break;
1448 default:
1449 return ERR_PTR(-EINVAL);
1450 }
1451
Pavel Emelyanov14ec63c2010-09-29 16:06:57 +04001452 error = __sock_create(net, family, type, protocol, &sock, 1);
Chuck Lever77f1f672007-02-12 00:53:39 -08001453 if (error < 0)
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001454 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Peter Zijlstraed075362006-12-06 20:35:24 -08001456 svc_reclassify_socket(sock);
1457
Trond Myklebustc69da772009-03-30 18:59:17 -04001458 /*
1459 * If this is an PF_INET6 listener, we want to avoid
1460 * getting requests from IPv4 remotes. Those should
1461 * be shunted to a PF_INET listener via rpcbind.
1462 */
Trond Myklebustc69da772009-03-30 18:59:17 -04001463 if (family == PF_INET6)
Christoph Hellwig9b115742020-05-28 07:12:31 +02001464 ip6_sock_set_v6only(sock->sk);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001465 if (type == SOCK_STREAM)
Pavel Emelyanov4a17fd52012-04-19 03:39:36 +00001466 sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
Chuck Lever77f1f672007-02-12 00:53:39 -08001467 error = kernel_bind(sock, sin, len);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001468 if (error < 0)
1469 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001471 error = kernel_getsockname(sock, newsin);
Tom Tucker9dbc2402007-12-30 21:08:12 -06001472 if (error < 0)
1473 goto bummer;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001474 newlen = error;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001477 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 goto bummer;
1479 }
1480
J. Bruce Fields72c35372012-08-13 17:46:17 -04001481 svsk = svc_setup_socket(serv, sock, flags);
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001482 if (IS_ERR(svsk)) {
1483 error = PTR_ERR(svsk);
1484 goto bummer;
NeilBrowne79eff12007-02-12 00:53:30 -08001485 }
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001486 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1487 return (struct svc_xprt *)svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488bummer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 sock_release(sock);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001490 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
1493/*
Tom Tucker755ccea2007-12-30 21:07:27 -06001494 * Detach the svc_sock from the socket so that no
1495 * more callbacks occur.
1496 */
1497static void svc_sock_detach(struct svc_xprt *xprt)
1498{
1499 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1500 struct sock *sk = svsk->sk_sk;
1501
Tom Tucker755ccea2007-12-30 21:07:27 -06001502 /* put back the old socket callbacks */
Trond Myklebust069c2252016-06-24 10:55:47 -04001503 lock_sock(sk);
Tom Tucker755ccea2007-12-30 21:07:27 -06001504 sk->sk_state_change = svsk->sk_ostate;
1505 sk->sk_data_ready = svsk->sk_odata;
1506 sk->sk_write_space = svsk->sk_owspace;
Trond Myklebust069c2252016-06-24 10:55:47 -04001507 sk->sk_user_data = NULL;
1508 release_sock(sk);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001509}
1510
1511/*
1512 * Disconnect the socket, and reset the callbacks
1513 */
1514static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1515{
1516 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1517
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001518 svc_sock_detach(xprt);
1519
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001520 if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1521 svc_tcp_clear_pages(svsk);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001522 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001523 }
Tom Tucker755ccea2007-12-30 21:07:27 -06001524}
1525
1526/*
1527 * Free the svc_sock's socket resources and the svc_sock itself.
1528 */
1529static void svc_sock_free(struct svc_xprt *xprt)
1530{
1531 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tucker755ccea2007-12-30 21:07:27 -06001532
Tom Tucker755ccea2007-12-30 21:07:27 -06001533 if (svsk->sk_sock->file)
1534 sockfd_put(svsk->sk_sock);
1535 else
1536 sock_release(svsk->sk_sock);
1537 kfree(svsk);
1538}