blob: 519cf9c4f8fd8bf46539c8b0ddca293838fde26a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/ioctls.h>
Eric Dumazet22911fc2012-06-27 00:23:44 +000048#include <trace/events/skb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
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 Lever9e55eef42020-03-02 15:19:54 -050058#include "socklib.h"
H Hartley Sweeten177e4f92011-06-20 17:54:51 -070059#include "sunrpc.h"
60
Tom Tucker360d87382007-12-30 21:07:17 -060061#define RPCDBG_FACILITY RPCDBG_SVCXPRT
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63
64static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
J. Bruce Fields72c35372012-08-13 17:46:17 -040065 int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static int svc_udp_recvfrom(struct svc_rqst *);
67static int svc_udp_sendto(struct svc_rqst *);
Tom Tucker755ccea2007-12-30 21:07:27 -060068static void svc_sock_detach(struct svc_xprt *);
Trond Myklebust69b6ba32008-12-23 16:30:11 -050069static void svc_tcp_sock_detach(struct svc_xprt *);
Tom Tucker755ccea2007-12-30 21:07:27 -060070static void svc_sock_free(struct svc_xprt *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Tom Tuckerb700cbb2007-12-30 21:07:42 -060072static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
Pavel Emelyanov62832c02010-09-29 16:04:18 +040073 struct net *, struct sockaddr *,
74 int, int);
Peter Zijlstraed075362006-12-06 20:35:24 -080075#ifdef CONFIG_DEBUG_LOCK_ALLOC
76static struct lock_class_key svc_key[2];
77static struct lock_class_key svc_slock_key[2];
78
Tom Tucker0f0257e2007-12-30 21:08:27 -060079static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -080080{
81 struct sock *sk = sock->sk;
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -040082
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +020083 if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -040084 return;
85
Peter Zijlstraed075362006-12-06 20:35:24 -080086 switch (sk->sk_family) {
87 case AF_INET:
88 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -060089 &svc_slock_key[0],
90 "sk_xprt.xpt_lock-AF_INET-NFSD",
91 &svc_key[0]);
Peter Zijlstraed075362006-12-06 20:35:24 -080092 break;
93
94 case AF_INET6:
95 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -060096 &svc_slock_key[1],
97 "sk_xprt.xpt_lock-AF_INET6-NFSD",
98 &svc_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -080099 break;
100
101 default:
102 BUG();
103 }
104}
105#else
Tom Tucker0f0257e2007-12-30 21:08:27 -0600106static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -0800107{
108}
109#endif
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/*
112 * Release an skbuff after use
113 */
Tom Tucker5148bf42007-12-30 21:07:25 -0600114static void svc_release_skb(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Tom Tucker5148bf42007-12-30 21:07:25 -0600116 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 if (skb) {
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600119 struct svc_sock *svsk =
120 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tucker5148bf42007-12-30 21:07:25 -0600121 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
Eric Dumazet9d410c72009-10-30 05:03:53 +0000124 skb_free_datagram_locked(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Paolo Abeni850cbad2016-10-21 13:55:47 +0200128static void svc_release_udp_skb(struct svc_rqst *rqstp)
129{
130 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
131
132 if (skb) {
133 rqstp->rq_xprt_ctxt = NULL;
134
135 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
136 consume_skb(skb);
137 }
138}
139
Chuck Leverb92503b2007-02-12 00:53:36 -0800140union svc_pktinfo_u {
141 struct in_pktinfo pkti;
Chuck Leverb92503b2007-02-12 00:53:36 -0800142 struct in6_pktinfo pkti6;
Chuck Leverb92503b2007-02-12 00:53:36 -0800143};
David S. Millerbc375ea2007-04-12 13:35:59 -0700144#define SVC_PKTINFO_SPACE \
145 CMSG_SPACE(sizeof(union svc_pktinfo_u))
Chuck Leverb92503b2007-02-12 00:53:36 -0800146
147static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
148{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600149 struct svc_sock *svsk =
150 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
151 switch (svsk->sk_sk->sk_family) {
Chuck Leverb92503b2007-02-12 00:53:36 -0800152 case AF_INET: {
153 struct in_pktinfo *pki = CMSG_DATA(cmh);
154
155 cmh->cmsg_level = SOL_IP;
156 cmh->cmsg_type = IP_PKTINFO;
157 pki->ipi_ifindex = 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800158 pki->ipi_spec_dst.s_addr =
159 svc_daddr_in(rqstp)->sin_addr.s_addr;
Chuck Leverb92503b2007-02-12 00:53:36 -0800160 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
161 }
162 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800163
Chuck Leverb92503b2007-02-12 00:53:36 -0800164 case AF_INET6: {
165 struct in6_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800166 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
Chuck Leverb92503b2007-02-12 00:53:36 -0800167
168 cmh->cmsg_level = SOL_IPV6;
169 cmh->cmsg_type = IPV6_PKTINFO;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800170 pki->ipi6_ifindex = daddr->sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000171 pki->ipi6_addr = daddr->sin6_addr;
Chuck Leverb92503b2007-02-12 00:53:36 -0800172 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
173 }
174 break;
Chuck Leverb92503b2007-02-12 00:53:36 -0800175 }
Chuck Leverb92503b2007-02-12 00:53:36 -0800176}
177
Chuck Lever41205532020-03-02 14:45:53 -0500178static int svc_sock_read_payload(struct svc_rqst *rqstp, unsigned int offset,
179 unsigned int length)
180{
181 return 0;
182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/*
NeilBrown80212d52006-10-02 02:17:47 -0700185 * Report socket names for nfsdfs
186 */
Chuck Levere7942b92009-04-23 19:32:48 -0400187static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
NeilBrown80212d52006-10-02 02:17:47 -0700188{
Chuck Lever017cb472009-04-23 19:33:03 -0400189 const struct sock *sk = svsk->sk_sk;
190 const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
191 "udp" : "tcp";
NeilBrown80212d52006-10-02 02:17:47 -0700192 int len;
193
Chuck Lever017cb472009-04-23 19:33:03 -0400194 switch (sk->sk_family) {
Chuck Levere7942b92009-04-23 19:32:48 -0400195 case PF_INET:
196 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400197 proto_name,
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000198 &inet_sk(sk)->inet_rcv_saddr,
199 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700200 break;
Eric Dumazetc2bb06d2013-10-09 03:05:48 -0700201#if IS_ENABLED(CONFIG_IPV6)
Chuck Lever58de2f82009-04-23 19:32:55 -0400202 case PF_INET6:
203 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400204 proto_name,
Eric Dumazetefe42082013-10-03 15:42:29 -0700205 &sk->sk_v6_rcv_saddr,
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000206 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700207 break;
Eric Dumazetc2bb06d2013-10-09 03:05:48 -0700208#endif
NeilBrown80212d52006-10-02 02:17:47 -0700209 default:
Chuck Levere7942b92009-04-23 19:32:48 -0400210 len = snprintf(buf, remaining, "*unknown-%d*\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400211 sk->sk_family);
NeilBrown80212d52006-10-02 02:17:47 -0700212 }
Chuck Levere7942b92009-04-23 19:32:48 -0400213
214 if (len >= remaining) {
215 *buf = '\0';
216 return -ENAMETOOLONG;
NeilBrown80212d52006-10-02 02:17:47 -0700217 }
218 return len;
219}
220
NeilBrown80212d52006-10-02 02:17:47 -0700221/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * Generic recvfrom routine.
223 */
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400224static ssize_t svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov,
225 unsigned int nr, size_t buflen, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600227 struct svc_sock *svsk =
228 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400229 struct msghdr msg = { NULL };
230 ssize_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Tom Tucker260c1d12007-12-30 21:08:29 -0600232 rqstp->rq_xprt_hlen = 0;
233
Trond Myklebustf8d1ff42014-08-03 13:03:13 -0400234 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
David Howellsaa563d72018-10-20 00:57:56 +0100235 iov_iter_kvec(&msg.msg_iter, READ, iov, nr, buflen);
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400236 if (base != 0) {
237 iov_iter_advance(&msg.msg_iter, base);
238 buflen -= base;
239 }
240 len = sock_recvmsg(svsk->sk_sock, &msg, MSG_DONTWAIT);
Trond Myklebustf8d1ff42014-08-03 13:03:13 -0400241 /* If we read a full record, then assume there may be more
242 * data to read (stream based sockets only!)
243 */
244 if (len == buflen)
245 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400247 dprintk("svc: socket %p recvfrom(%p, %zu) = %zd\n",
Chuck Lever1ba95102007-02-12 00:53:31 -0800248 svsk, iov[0].iov_base, iov[0].iov_len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return len;
250}
251
252/*
253 * Set socket snd and rcv buffer lengths
254 */
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500255static void svc_sock_setbufsize(struct svc_sock *svsk, unsigned int nreqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500257 unsigned int max_mesg = svsk->sk_xprt.xpt_server->sv_max_mesg;
258 struct socket *sock = svsk->sk_sock;
259
260 nreqs = min(nreqs, INT_MAX / 2 / max_mesg);
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 lock_sock(sock->sk);
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500263 sock->sk->sk_sndbuf = nreqs * max_mesg * 2;
264 sock->sk->sk_rcvbuf = nreqs * max_mesg * 2;
Trond Myklebust47fcb032009-05-18 17:47:56 -0400265 sock->sk->sk_write_space(sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 release_sock(sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
Chuck Lever16e4d932014-05-19 13:40:22 -0400268
Chuck Lever989f8812018-03-27 10:49:38 -0400269static void svc_sock_secure_port(struct svc_rqst *rqstp)
Chuck Lever16e4d932014-05-19 13:40:22 -0400270{
Chuck Lever989f8812018-03-27 10:49:38 -0400271 if (svc_port_is_privileged(svc_addr(rqstp)))
272 set_bit(RQ_SECURE, &rqstp->rq_flags);
273 else
274 clear_bit(RQ_SECURE, &rqstp->rq_flags);
Chuck Lever16e4d932014-05-19 13:40:22 -0400275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/*
278 * INET callback when data has been received on the socket.
279 */
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400280static void svc_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Neil Brown939bb7e2005-09-13 01:25:39 -0700282 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Neil Brown939bb7e2005-09-13 01:25:39 -0700284 if (svsk) {
David S. Miller676d2362014-04-11 16:15:36 -0400285 dprintk("svc: socket %p(inet %p), busy=%d\n",
286 svsk, sk,
Tom Tucker02fc6c32007-12-30 21:07:48 -0600287 test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400288
289 /* Refer to svc_setup_socket() for details. */
290 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400291 svsk->sk_odata(sk);
Trond Myklebust4720b072016-06-24 10:55:49 -0400292 if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags))
293 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297/*
298 * INET callback when space is newly available on the socket.
299 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600300static void svc_write_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
303
304 if (svsk) {
305 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600306 svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400307
308 /* Refer to svc_setup_socket() for details. */
309 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400310 svsk->sk_owspace(sk);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600311 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Trond Myklebustc7fb3f02014-07-24 23:59:32 -0400315static int svc_tcp_has_wspace(struct svc_xprt *xprt)
316{
Trond Myklebust637600f2016-06-24 10:55:51 -0400317 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Trond Myklebustc7fb3f02014-07-24 23:59:32 -0400318
319 if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
320 return 1;
Trond Myklebust637600f2016-06-24 10:55:51 -0400321 return !test_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Trond Myklebust51877682014-07-24 23:59:33 -0400322}
323
Scott Mayhewea08e392016-11-11 13:16:22 -0500324static void svc_tcp_kill_temp_xprt(struct svc_xprt *xprt)
325{
326 struct svc_sock *svsk;
327 struct socket *sock;
328 struct linger no_linger = {
329 .l_onoff = 1,
330 .l_linger = 0,
331 };
332
333 svsk = container_of(xprt, struct svc_sock, sk_xprt);
334 sock = svsk->sk_sock;
335 kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER,
336 (char *)&no_linger, sizeof(no_linger));
337}
338
Tom Tucker9dbc2402007-12-30 21:08:12 -0600339/*
Chuck Lever7702ce42009-07-13 10:54:26 -0400340 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
341 */
342static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
343 struct cmsghdr *cmh)
344{
345 struct in_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800346 struct sockaddr_in *daddr = svc_daddr_in(rqstp);
347
Chuck Lever7702ce42009-07-13 10:54:26 -0400348 if (cmh->cmsg_type != IP_PKTINFO)
349 return 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800350
351 daddr->sin_family = AF_INET;
352 daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
Chuck Lever7702ce42009-07-13 10:54:26 -0400353 return 1;
354}
355
356/*
Tom Parkin73df66f2013-01-31 01:02:24 +0000357 * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
Chuck Lever7702ce42009-07-13 10:54:26 -0400358 */
359static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
360 struct cmsghdr *cmh)
361{
362 struct in6_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800363 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
364
Chuck Lever7702ce42009-07-13 10:54:26 -0400365 if (cmh->cmsg_type != IPV6_PKTINFO)
366 return 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800367
368 daddr->sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000369 daddr->sin6_addr = pki->ipi6_addr;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800370 daddr->sin6_scope_id = pki->ipi6_ifindex;
Chuck Lever7702ce42009-07-13 10:54:26 -0400371 return 1;
372}
373
374/*
Tom Tucker9dbc2402007-12-30 21:08:12 -0600375 * Copy the UDP datagram's destination address to the rqstp structure.
376 * The 'destination' address in this case is the address to which the
377 * peer sent the datagram, i.e. our local address. For multihomed
378 * hosts, this can change from msg to msg. Note that only the IP
379 * address changes, the port number should remain the same.
380 */
Chuck Lever7702ce42009-07-13 10:54:26 -0400381static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
382 struct cmsghdr *cmh)
Chuck Lever95756482007-02-12 00:53:38 -0800383{
Chuck Lever7702ce42009-07-13 10:54:26 -0400384 switch (cmh->cmsg_level) {
385 case SOL_IP:
386 return svc_udp_get_dest_address4(rqstp, cmh);
387 case SOL_IPV6:
388 return svc_udp_get_dest_address6(rqstp, cmh);
Chuck Lever95756482007-02-12 00:53:38 -0800389 }
Chuck Lever7702ce42009-07-13 10:54:26 -0400390
391 return 0;
Chuck Lever95756482007-02-12 00:53:38 -0800392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/*
395 * Receive a datagram from a UDP socket.
396 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600397static int svc_udp_recvfrom(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600399 struct svc_sock *svsk =
400 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600401 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 struct sk_buff *skb;
David S. Millerbc375ea2007-04-12 13:35:59 -0700403 union {
404 struct cmsghdr hdr;
405 long all[SVC_PKTINFO_SPACE / sizeof(long)];
406 } buffer;
407 struct cmsghdr *cmh = &buffer.hdr;
NeilBrown7a37f572007-03-06 01:42:21 -0800408 struct msghdr msg = {
409 .msg_name = svc_addr(rqstp),
410 .msg_control = cmh,
411 .msg_controllen = sizeof(buffer),
412 .msg_flags = MSG_DONTWAIT,
413 };
Chuck Leverabc5c442009-04-23 19:31:25 -0400414 size_t len;
415 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Tom Tucker02fc6c32007-12-30 21:07:48 -0600417 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /* udp sockets need large rcvbuf as all pending
419 * requests are still in that buffer. sndbuf must
420 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700421 * for one reply per thread. We count all threads
422 * rather than threads in a particular pool, which
423 * provides an upper bound on the number of threads
424 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 */
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500426 svc_sock_setbufsize(svsk, serv->sv_nrthreads + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Tom Tucker02fc6c32007-12-30 21:07:48 -0600428 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
NeilBrown05ed6902007-05-09 02:34:55 -0700429 skb = NULL;
430 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
431 0, 0, MSG_PEEK | MSG_DONTWAIT);
432 if (err >= 0)
Paolo Abeni7c13f972016-11-04 11:28:59 +0100433 skb = skb_recv_udp(svsk->sk_sk, 0, 1, &err);
NeilBrown05ed6902007-05-09 02:34:55 -0700434
435 if (skb == NULL) {
436 if (err != -EAGAIN) {
437 /* possibly an icmp error */
438 dprintk("svc: recvfrom returned error %d\n", -err);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600439 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -0400441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600443 len = svc_addr_len(svc_addr(rqstp));
Tom Tucker9dbc2402007-12-30 21:08:12 -0600444 rqstp->rq_addrlen = len;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100445 if (skb->tstamp == 0) {
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700446 skb->tstamp = ktime_get_real();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800447 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 need that much accuracy */
449 }
Deepa Dinamani3a0ed3e92018-12-27 18:55:09 -0800450 sock_write_timestamp(svsk->sk_sk, skb->tstamp);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600451 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Willem de Bruijn1da8c6812016-04-07 11:44:58 -0400453 len = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 rqstp->rq_arg.len = len;
455
Chuck Lever95756482007-02-12 00:53:38 -0800456 rqstp->rq_prot = IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Chuck Lever7702ce42009-07-13 10:54:26 -0400458 if (!svc_udp_get_dest_address(rqstp, cmh)) {
Joe Perchese87cc472012-05-13 21:56:26 +0000459 net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
460 cmh->cmsg_level, cmh->cmsg_type);
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400461 goto out_free;
NeilBrown7a37f572007-03-06 01:42:21 -0800462 }
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800463 rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 if (skb_is_nonlinear(skb)) {
466 /* we have to copy */
467 local_bh_disable();
468 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
469 local_bh_enable();
470 /* checksum error */
Eric Dumazet22911fc2012-06-27 00:23:44 +0000471 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473 local_bh_enable();
Paolo Abeni850cbad2016-10-21 13:55:47 +0200474 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 } else {
476 /* we can use it in-place */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -0400477 rqstp->rq_arg.head[0].iov_base = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 rqstp->rq_arg.head[0].iov_len = len;
Eric Dumazet22911fc2012-06-27 00:23:44 +0000479 if (skb_checksum_complete(skb))
480 goto out_free;
Tom Tucker5148bf42007-12-30 21:07:25 -0600481 rqstp->rq_xprt_ctxt = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483
484 rqstp->rq_arg.page_base = 0;
485 if (len <= rqstp->rq_arg.head[0].iov_len) {
486 rqstp->rq_arg.head[0].iov_len = len;
487 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700488 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 } else {
490 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700491 rqstp->rq_respages = rqstp->rq_pages + 1 +
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700492 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500494 rqstp->rq_next_page = rqstp->rq_respages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 if (serv->sv_stats)
497 serv->sv_stats->netudpcnt++;
498
499 return len;
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400500out_free:
Paolo Abeni850cbad2016-10-21 13:55:47 +0200501 kfree_skb(skb);
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Chuck Leverda1661b2020-03-02 15:20:33 -0500505/**
506 * svc_udp_sendto - Send out a reply on a UDP socket
507 * @rqstp: completed svc_rqst
508 *
509 * Returns the number of bytes sent, or a negative errno.
510 */
511static int svc_udp_sendto(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Chuck Leverda1661b2020-03-02 15:20:33 -0500513 struct svc_xprt *xprt = rqstp->rq_xprt;
514 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
515 struct xdr_buf *xdr = &rqstp->rq_res;
516 union {
517 struct cmsghdr hdr;
518 long all[SVC_PKTINFO_SPACE / sizeof(long)];
519 } buffer;
520 struct cmsghdr *cmh = &buffer.hdr;
521 struct msghdr msg = {
522 .msg_name = &rqstp->rq_addr,
523 .msg_namelen = rqstp->rq_addrlen,
524 .msg_control = cmh,
525 .msg_controllen = sizeof(buffer),
526 };
527 unsigned int uninitialized_var(sent);
528 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Chuck Leverda1661b2020-03-02 15:20:33 -0500530 svc_set_cmsg_data(rqstp, cmh);
531
532 err = xprt_sock_sendmsg(svsk->sk_sock, &msg, xdr, 0, 0, &sent);
533 xdr_free_bvec(xdr);
534 if (err == -ECONNREFUSED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* ICMP error on earlier request. */
Chuck Leverda1661b2020-03-02 15:20:33 -0500536 err = xprt_sock_sendmsg(svsk->sk_sock, &msg, xdr, 0, 0, &sent);
537 xdr_free_bvec(xdr);
538 }
539 if (err < 0)
540 return err;
541 return sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Tom Tucker323bee32007-12-30 21:07:31 -0600544static int svc_udp_has_wspace(struct svc_xprt *xprt)
545{
546 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600547 struct svc_serv *serv = xprt->xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -0600548 unsigned long required;
549
550 /*
551 * Set the SOCK_NOSPACE flag before checking the available
552 * sock space.
553 */
554 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Tom Tucker7a90e8c2007-12-30 21:07:55 -0600555 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
Tom Tucker323bee32007-12-30 21:07:31 -0600556 if (required*2 > sock_wspace(svsk->sk_sk))
557 return 0;
558 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
559 return 1;
560}
561
Tom Tucker38a417c2007-12-30 21:07:36 -0600562static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
563{
564 BUG();
565 return NULL;
566}
567
Scott Mayhewea08e392016-11-11 13:16:22 -0500568static void svc_udp_kill_temp_xprt(struct svc_xprt *xprt)
569{
570}
571
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600572static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400573 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600574 struct sockaddr *sa, int salen,
575 int flags)
576{
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400577 return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600578}
579
Chuck Lever2412e922017-08-01 11:59:49 -0400580static const struct svc_xprt_ops svc_udp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600581 .xpo_create = svc_udp_create,
Tom Tucker5d137992007-12-30 21:07:23 -0600582 .xpo_recvfrom = svc_udp_recvfrom,
583 .xpo_sendto = svc_udp_sendto,
Chuck Lever41205532020-03-02 14:45:53 -0500584 .xpo_read_payload = svc_sock_read_payload,
Paolo Abeni850cbad2016-10-21 13:55:47 +0200585 .xpo_release_rqst = svc_release_udp_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -0600586 .xpo_detach = svc_sock_detach,
587 .xpo_free = svc_sock_free,
Tom Tucker323bee32007-12-30 21:07:31 -0600588 .xpo_has_wspace = svc_udp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -0600589 .xpo_accept = svc_udp_accept,
Chuck Lever16e4d932014-05-19 13:40:22 -0400590 .xpo_secure_port = svc_sock_secure_port,
Scott Mayhewea08e392016-11-11 13:16:22 -0500591 .xpo_kill_temp_xprt = svc_udp_kill_temp_xprt,
Tom Tucker360d87382007-12-30 21:07:17 -0600592};
593
594static struct svc_xprt_class svc_udp_class = {
595 .xcl_name = "udp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600596 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -0600597 .xcl_ops = &svc_udp_ops,
Tom Tucker49023152007-12-30 21:07:21 -0600598 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
Chuck Lever3c45ddf2014-07-16 15:38:32 -0400599 .xcl_ident = XPRT_TRANSPORT_UDP,
Tom Tucker360d87382007-12-30 21:07:17 -0600600};
601
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600602static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Chuck Lever7702ce42009-07-13 10:54:26 -0400604 int err, level, optname, one = 1;
NeilBrown7a37f572007-03-06 01:42:21 -0800605
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +0300606 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
607 &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600608 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400609 svsk->sk_sk->sk_data_ready = svc_data_ready;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 svsk->sk_sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800613 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * svc_udp_recvfrom will re-adjust if necessary
615 */
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500616 svc_sock_setbufsize(svsk, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Tom Tucker0f0257e2007-12-30 21:08:27 -0600618 /* data might have come in before data_ready set up */
619 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600620 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800621
NeilBrown7a37f572007-03-06 01:42:21 -0800622 /* make sure we get destination address info */
Chuck Lever7702ce42009-07-13 10:54:26 -0400623 switch (svsk->sk_sk->sk_family) {
624 case AF_INET:
625 level = SOL_IP;
626 optname = IP_PKTINFO;
627 break;
628 case AF_INET6:
629 level = SOL_IPV6;
630 optname = IPV6_RECVPKTINFO;
631 break;
632 default:
633 BUG();
634 }
635 err = kernel_setsockopt(svsk->sk_sock, level, optname,
636 (char *)&one, sizeof(one));
637 dprintk("svc: kernel_setsockopt returned %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640/*
641 * A data_ready event on a listening socket means there's a connection
642 * pending. Do not use state_change as a substitute for it.
643 */
David S. Miller676d2362014-04-11 16:15:36 -0400644static void svc_tcp_listen_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Neil Brown939bb7e2005-09-13 01:25:39 -0700646 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700649 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400651 if (svsk) {
652 /* Refer to svc_setup_socket() for details. */
653 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400654 svsk->sk_odata(sk);
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400655 }
656
Neil Brown939bb7e2005-09-13 01:25:39 -0700657 /*
658 * This callback may called twice when a new connection
659 * is established as a child socket inherits everything
660 * from a parent LISTEN socket.
661 * 1) data_ready method of the parent socket will be called
662 * when one of child sockets become ESTABLISHED.
663 * 2) data_ready method of the child socket may be called
664 * when it receives data before the socket is accepted.
665 * In case of 2, we should ignore it silently.
666 */
667 if (sk->sk_state == TCP_LISTEN) {
668 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600669 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600670 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700671 } else
672 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
676/*
677 * A state change on a connected socket means it's dying or dead.
678 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600679static void svc_tcp_state_change(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Neil Brown939bb7e2005-09-13 01:25:39 -0700681 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700684 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Neil Brown939bb7e2005-09-13 01:25:39 -0700686 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700688 else {
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400689 /* Refer to svc_setup_socket() for details. */
690 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400691 svsk->sk_ostate(sk);
Trond Myklebustb2f21f72016-07-26 09:51:19 -0400692 if (sk->sk_state != TCP_ESTABLISHED) {
693 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
694 svc_xprt_enqueue(&svsk->sk_xprt);
695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699/*
700 * Accept a TCP connection
701 */
Tom Tucker38a417c2007-12-30 21:07:36 -0600702static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Tom Tucker38a417c2007-12-30 21:07:36 -0600704 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800705 struct sockaddr_storage addr;
706 struct sockaddr *sin = (struct sockaddr *) &addr;
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600707 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct socket *sock = svsk->sk_sock;
709 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 struct svc_sock *newsvsk;
711 int err, slen;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +0300712 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
715 if (!sock)
Tom Tucker38a417c2007-12-30 21:07:36 -0600716 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Tom Tucker02fc6c32007-12-30 21:07:48 -0600718 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700719 err = kernel_accept(sock, &newsock, O_NONBLOCK);
720 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (err == -ENOMEM)
722 printk(KERN_WARNING "%s: no more sockets!\n",
723 serv->sv_name);
Joe Perchese87cc472012-05-13 21:56:26 +0000724 else if (err != -EAGAIN)
725 net_warn_ratelimited("%s: accept failed (err %d)!\n",
726 serv->sv_name, -err);
Tom Tucker38a417c2007-12-30 21:07:36 -0600727 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Tom Tucker02fc6c32007-12-30 21:07:48 -0600729 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100731 err = kernel_getpeername(newsock, sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (err < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +0000733 net_warn_ratelimited("%s: peername failed (err %d)!\n",
734 serv->sv_name, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 goto failed; /* aborted connection or whatever */
736 }
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100737 slen = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 /* Ideally, we would want to reject connections from unauthorized
Chuck Leverad06e4b2007-02-12 00:53:32 -0800740 * hosts here, but when we get encryption, the IP of the host won't
741 * tell us anything. For now just warn about unpriv connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800743 if (!svc_port_is_privileged(sin)) {
Kinglong Meea48fd0f2014-05-29 12:18:52 +0800744 dprintk("%s: connect from unprivileged port: %s\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800745 serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800746 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Chuck Leverad06e4b2007-02-12 00:53:32 -0800748 dprintk("%s: connect from %s\n", serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800749 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400751 /* Reset the inherited callbacks before calling svc_setup_socket */
752 newsock->sk->sk_state_change = svsk->sk_ostate;
753 newsock->sk->sk_data_ready = svsk->sk_odata;
754 newsock->sk->sk_write_space = svsk->sk_owspace;
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /* make sure that a write doesn't block forever when
757 * low on memory
758 */
759 newsock->sk->sk_sndtimeo = HZ*30;
760
J. Bruce Fields72c35372012-08-13 17:46:17 -0400761 newsvsk = svc_setup_socket(serv, newsock,
762 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
763 if (IS_ERR(newsvsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 goto failed;
Tom Tucker9dbc2402007-12-30 21:08:12 -0600765 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100766 err = kernel_getsockname(newsock, sin);
767 slen = err;
Frank van Maarseveena9747692007-07-09 22:21:39 +0200768 if (unlikely(err < 0)) {
769 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
770 slen = offsetof(struct sockaddr, sa_data);
771 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600772 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -0800773
NeilBrownef11ce22014-05-12 11:22:47 +1000774 if (sock_is_loopback(newsock->sk))
775 set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
776 else
777 clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600778 if (serv->sv_stats)
779 serv->sv_stats->nettcpconn++;
780
781 return &newsvsk->sk_xprt;
782
783failed:
784 sock_release(newsock);
785 return NULL;
786}
787
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800788static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
789{
790 unsigned int i, len, npages;
791
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500792 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800793 return 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500794 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800795 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
796 for (i = 0; i < npages; i++) {
797 if (rqstp->rq_pages[i] != NULL)
798 put_page(rqstp->rq_pages[i]);
799 BUG_ON(svsk->sk_pages[i] == NULL);
800 rqstp->rq_pages[i] = svsk->sk_pages[i];
801 svsk->sk_pages[i] = NULL;
802 }
803 rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
804 return len;
805}
806
807static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
808{
809 unsigned int i, len, npages;
810
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500811 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800812 return;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500813 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800814 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
815 for (i = 0; i < npages; i++) {
816 svsk->sk_pages[i] = rqstp->rq_pages[i];
817 rqstp->rq_pages[i] = NULL;
818 }
819}
820
821static void svc_tcp_clear_pages(struct svc_sock *svsk)
822{
823 unsigned int i, len, npages;
824
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500825 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800826 goto out;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500827 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800828 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
829 for (i = 0; i < npages; i++) {
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -0400830 if (svsk->sk_pages[i] == NULL) {
831 WARN_ON_ONCE(1);
832 continue;
833 }
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800834 put_page(svsk->sk_pages[i]);
835 svsk->sk_pages[i] = NULL;
836 }
837out:
838 svsk->sk_tcplen = 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500839 svsk->sk_datalen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800840}
841
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600842/*
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -0500843 * Receive fragment record header.
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300844 * If we haven't gotten the record length yet, get the next four bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 */
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300846static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600848 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400849 unsigned int want;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300850 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Chuck Leverc0401ea2008-04-14 12:27:30 -0400852 if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct kvec iov;
854
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400855 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
857 iov.iov_len = want;
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400858 len = svc_recvfrom(rqstp, &iov, 1, want, 0);
859 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 goto error;
861 svsk->sk_tcplen += len;
862
863 if (len < want) {
Chuck Leverc0401ea2008-04-14 12:27:30 -0400864 dprintk("svc: short recvfrom while reading record "
865 "length (%d of %d)\n", len, want);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800866 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500869 dprintk("svc: TCP record, %d bytes\n", svc_sock_reclen(svsk));
J. Bruce Fields836fbad2012-12-03 15:50:38 -0500870 if (svc_sock_reclen(svsk) + svsk->sk_datalen >
871 serv->sv_max_mesg) {
J. Bruce Fields3a28e332012-12-10 18:15:07 -0500872 net_notice_ratelimited("RPC: fragment too large: %d\n",
873 svc_sock_reclen(svsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 goto err_delete;
875 }
876 }
877
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500878 return svc_sock_reclen(svsk);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800879error:
880 dprintk("RPC: TCP recv_record got %d\n", len);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300881 return len;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800882err_delete:
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300883 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300884 return -EAGAIN;
885}
886
Trond Myklebust586c52c2009-05-18 17:47:56 -0400887static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300888{
Trond Myklebust586c52c2009-05-18 17:47:56 -0400889 struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300890 struct rpc_rqst *req = NULL;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400891 struct kvec *src, *dst;
892 __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
J. Bruce Fields48e65552011-02-14 14:52:03 -0500893 __be32 xid;
894 __be32 calldir;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300895
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300896 xid = *p++;
897 calldir = *p;
898
Trond Myklebust093a1462014-11-12 18:04:04 -0500899 if (!bc_xprt)
Trond Myklebust586c52c2009-05-18 17:47:56 -0400900 return -EAGAIN;
Trond Myklebust75c84152018-08-31 10:21:00 -0400901 spin_lock(&bc_xprt->queue_lock);
Trond Myklebust093a1462014-11-12 18:04:04 -0500902 req = xprt_lookup_rqst(bc_xprt, xid);
903 if (!req)
904 goto unlock_notfound;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400905
906 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
907 /*
908 * XXX!: cheating for now! Only copying HEAD.
909 * But we know this is good enough for now (in fact, for any
910 * callback reply in the forseeable future).
911 */
912 dst = &req->rq_private_buf.head[0];
913 src = &rqstp->rq_arg.head[0];
914 if (dst->iov_len < src->iov_len)
Trond Myklebust093a1462014-11-12 18:04:04 -0500915 goto unlock_eagain; /* whatever; just giving up. */
Trond Myklebust586c52c2009-05-18 17:47:56 -0400916 memcpy(dst->iov_base, src->iov_base, src->iov_len);
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500917 xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len);
Trond Myklebust586c52c2009-05-18 17:47:56 -0400918 rqstp->rq_arg.len = 0;
Trond Myklebust75c84152018-08-31 10:21:00 -0400919 spin_unlock(&bc_xprt->queue_lock);
Trond Myklebust586c52c2009-05-18 17:47:56 -0400920 return 0;
Trond Myklebust093a1462014-11-12 18:04:04 -0500921unlock_notfound:
922 printk(KERN_NOTICE
923 "%s: Got unrecognized reply: "
924 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
925 __func__, ntohl(calldir),
926 bc_xprt, ntohl(xid));
927unlock_eagain:
Trond Myklebust75c84152018-08-31 10:21:00 -0400928 spin_unlock(&bc_xprt->queue_lock);
Trond Myklebust093a1462014-11-12 18:04:04 -0500929 return -EAGAIN;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400930}
931
932static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len)
933{
934 int i = 0;
935 int t = 0;
936
937 while (t < len) {
938 vec[i].iov_base = page_address(pages[i]);
939 vec[i].iov_len = PAGE_SIZE;
940 i++;
941 t += PAGE_SIZE;
942 }
943 return i;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300944}
945
J. Bruce Fields836fbad2012-12-03 15:50:38 -0500946static void svc_tcp_fragment_received(struct svc_sock *svsk)
947{
948 /* If we have more data, signal svc_xprt_enqueue() to try again */
J. Bruce Fields836fbad2012-12-03 15:50:38 -0500949 dprintk("svc: TCP %s record (%d bytes)\n",
950 svc_sock_final_rec(svsk) ? "final" : "nonfinal",
951 svc_sock_reclen(svsk));
952 svsk->sk_tcplen = 0;
953 svsk->sk_reclen = 0;
954}
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800955
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300956/*
957 * Receive data from a TCP socket.
958 */
959static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
960{
961 struct svc_sock *svsk =
962 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
963 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
964 int len;
965 struct kvec *vec;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800966 unsigned int want, base;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400967 __be32 *p;
968 __be32 calldir;
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400969 int pnum;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300970
971 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
972 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
973 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
974 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
975
976 len = svc_tcp_recv_record(svsk, rqstp);
977 if (len < 0)
978 goto error;
979
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800980 base = svc_tcp_restore_pages(svsk, rqstp);
J. Bruce Fields836fbad2012-12-03 15:50:38 -0500981 want = svc_sock_reclen(svsk) - (svsk->sk_tcplen - sizeof(rpc_fraghdr));
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800982
NeilBrown3cc03b12006-10-04 02:15:47 -0700983 vec = rqstp->rq_vec;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300984
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400985 pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0], base + want);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300986
NeilBrown44524352006-10-04 02:15:46 -0700987 rqstp->rq_respages = &rqstp->rq_pages[pnum];
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500988 rqstp->rq_next_page = rqstp->rq_respages + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 /* Now receive data */
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400991 len = svc_recvfrom(rqstp, vec, pnum, base + want, base);
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500992 if (len >= 0) {
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800993 svsk->sk_tcplen += len;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500994 svsk->sk_datalen += len;
995 }
J. Bruce Fields836fbad2012-12-03 15:50:38 -0500996 if (len != want || !svc_sock_final_rec(svsk)) {
J. Bruce Fieldsbe1e4442012-08-09 18:12:28 -0400997 svc_tcp_save_pages(svsk, rqstp);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800998 if (len < 0 && len != -EAGAIN)
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -0500999 goto err_delete;
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001000 if (len == want)
1001 svc_tcp_fragment_received(svsk);
1002 else
J. Bruce Fields3a28e332012-12-10 18:15:07 -05001003 dprintk("svc: incomplete TCP record (%d of %d)\n",
1004 (int)(svsk->sk_tcplen - sizeof(rpc_fraghdr)),
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001005 svc_sock_reclen(svsk));
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001006 goto err_noclose;
1007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
J. Bruce Fields1f691b02013-06-26 10:55:40 -04001009 if (svsk->sk_datalen < 8) {
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -04001010 svsk->sk_datalen = 0;
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001011 goto err_delete; /* client is nuts. */
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -04001012 }
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001013
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001014 rqstp->rq_arg.len = svsk->sk_datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 rqstp->rq_arg.page_base = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001016 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1017 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 rqstp->rq_arg.page_len = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001019 } else
1020 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Tom Tucker5148bf42007-12-30 21:07:25 -06001022 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 rqstp->rq_prot = IPPROTO_TCP;
Jeff Layton7501cc22014-11-19 07:51:15 -05001024 if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags))
1025 set_bit(RQ_LOCAL, &rqstp->rq_flags);
1026 else
1027 clear_bit(RQ_LOCAL, &rqstp->rq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Trond Myklebust586c52c2009-05-18 17:47:56 -04001029 p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1030 calldir = p[1];
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001031 if (calldir)
Trond Myklebust586c52c2009-05-18 17:47:56 -04001032 len = receive_cb_reply(svsk, rqstp);
Trond Myklebust586c52c2009-05-18 17:47:56 -04001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /* Reset TCP read info */
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001035 svsk->sk_datalen = 0;
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001036 svc_tcp_fragment_received(svsk);
Trond Myklebust0601f792009-05-18 17:47:56 -04001037
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001038 if (len < 0)
1039 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Tom Tucker9dbc2402007-12-30 21:08:12 -06001041 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (serv->sv_stats)
1043 serv->sv_stats->nettcpcnt++;
1044
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001045 return rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001047error:
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001048 if (len != -EAGAIN)
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001049 goto err_delete;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001050 dprintk("RPC: TCP recvfrom got EAGAIN\n");
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -04001051 return 0;
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001052err_delete:
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001053 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1054 svsk->sk_xprt.xpt_server->sv_name, -len);
1055 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1056err_noclose:
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -04001057 return 0; /* record not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
Chuck Leverda1661b2020-03-02 15:20:33 -05001060/**
1061 * svc_tcp_sendto - Send out a reply on a TCP socket
1062 * @rqstp: completed svc_rqst
1063 *
1064 * Returns the number of bytes sent, or a negative errno.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 */
Tom Tucker0f0257e2007-12-30 21:08:27 -06001066static int svc_tcp_sendto(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Chuck Leverda1661b2020-03-02 15:20:33 -05001068 struct svc_xprt *xprt = rqstp->rq_xprt;
1069 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1070 struct xdr_buf *xdr = &rqstp->rq_res;
1071 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
1072 (u32)xdr->len);
1073 struct msghdr msg = {
1074 .msg_flags = 0,
1075 };
1076 unsigned int uninitialized_var(sent);
1077 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Chuck Leverda1661b2020-03-02 15:20:33 -05001079 err = xprt_sock_sendmsg(svsk->sk_sock, &msg, xdr, 0, marker, &sent);
1080 xdr_free_bvec(xdr);
1081 if (err < 0 || sent != (xdr->len + sizeof(marker)))
1082 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return sent;
Chuck Leverda1661b2020-03-02 15:20:33 -05001084
1085out_close:
1086 pr_notice("rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1087 xprt->xpt_server->sv_name,
1088 (err < 0) ? "got error" : "sent",
1089 (err < 0) ? err : sent, xdr->len);
1090 set_bit(XPT_CLOSE, &xprt->xpt_flags);
1091 svc_xprt_enqueue(xprt);
1092 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001095static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001096 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001097 struct sockaddr *sa, int salen,
1098 int flags)
1099{
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001100 return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001101}
1102
Chuck Lever2412e922017-08-01 11:59:49 -04001103static const struct svc_xprt_ops svc_tcp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001104 .xpo_create = svc_tcp_create,
Tom Tucker5d137992007-12-30 21:07:23 -06001105 .xpo_recvfrom = svc_tcp_recvfrom,
1106 .xpo_sendto = svc_tcp_sendto,
Chuck Lever41205532020-03-02 14:45:53 -05001107 .xpo_read_payload = svc_sock_read_payload,
Tom Tucker5148bf42007-12-30 21:07:25 -06001108 .xpo_release_rqst = svc_release_skb,
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001109 .xpo_detach = svc_tcp_sock_detach,
Tom Tucker755ccea2007-12-30 21:07:27 -06001110 .xpo_free = svc_sock_free,
Tom Tucker323bee32007-12-30 21:07:31 -06001111 .xpo_has_wspace = svc_tcp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -06001112 .xpo_accept = svc_tcp_accept,
Chuck Lever16e4d932014-05-19 13:40:22 -04001113 .xpo_secure_port = svc_sock_secure_port,
Scott Mayhewea08e392016-11-11 13:16:22 -05001114 .xpo_kill_temp_xprt = svc_tcp_kill_temp_xprt,
Tom Tucker360d87382007-12-30 21:07:17 -06001115};
1116
1117static struct svc_xprt_class svc_tcp_class = {
1118 .xcl_name = "tcp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001119 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -06001120 .xcl_ops = &svc_tcp_ops,
Tom Tucker49023152007-12-30 21:07:21 -06001121 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
Chuck Lever3c45ddf2014-07-16 15:38:32 -04001122 .xcl_ident = XPRT_TRANSPORT_TCP,
Tom Tucker360d87382007-12-30 21:07:17 -06001123};
1124
1125void svc_init_xprt_sock(void)
1126{
1127 svc_reg_xprt_class(&svc_tcp_class);
1128 svc_reg_xprt_class(&svc_udp_class);
1129}
1130
1131void svc_cleanup_xprt_sock(void)
1132{
1133 svc_unreg_xprt_class(&svc_tcp_class);
1134 svc_unreg_xprt_class(&svc_udp_class);
1135}
1136
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001137static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 struct sock *sk = svsk->sk_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +03001141 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
1142 &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -06001143 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Jeff Layton362142b22017-02-24 13:25:23 -05001144 set_bit(XPT_CONG_CTRL, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (sk->sk_state == TCP_LISTEN) {
1146 dprintk("setting up TCP socket for listening\n");
Chuck Leverece200d2018-03-27 10:51:00 -04001147 strcpy(svsk->sk_xprt.xpt_remotebuf, "listener");
Tom Tucker02fc6c32007-12-30 21:07:48 -06001148 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 sk->sk_data_ready = svc_tcp_listen_data_ready;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001150 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 } else {
1152 dprintk("setting up TCP socket for reading\n");
1153 sk->sk_state_change = svc_tcp_state_change;
Trond Myklebustfa9251a2016-06-24 10:55:48 -04001154 sk->sk_data_ready = svc_data_ready;
Trond Myklebust637600f2016-06-24 10:55:51 -04001155 sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 svsk->sk_reclen = 0;
1158 svsk->sk_tcplen = 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001159 svsk->sk_datalen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001160 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Chuck Leverb7872fe2008-04-14 12:27:01 -04001162 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Tom Tucker02fc6c32007-12-30 21:07:48 -06001164 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Trond Myklebustb2f21f72016-07-26 09:51:19 -04001165 switch (sk->sk_state) {
1166 case TCP_SYN_RECV:
1167 case TCP_ESTABLISHED:
1168 break;
1169 default:
Tom Tucker02fc6c32007-12-30 21:07:48 -06001170 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Trond Myklebustb2f21f72016-07-26 09:51:19 -04001171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173}
1174
Tom Tucker0f0257e2007-12-30 21:08:27 -06001175void svc_sock_update_bufs(struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 /*
1178 * The number of server threads has changed. Update
1179 * rcvbuf and sndbuf accordingly on all sockets
1180 */
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001181 struct svc_sock *svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 spin_lock_bh(&serv->sv_lock);
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001184 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001185 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 spin_unlock_bh(&serv->sv_lock);
1187}
Trond Myklebust24c37672008-12-23 16:30:12 -05001188EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 * Initialize socket for RPC use and create svc_sock struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 */
Chuck Lever6b174332007-02-12 00:53:28 -08001193static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1194 struct socket *sock,
J. Bruce Fields72c35372012-08-13 17:46:17 -04001195 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 struct svc_sock *svsk;
1198 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001199 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001200 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 dprintk("svc: svc_setup_socket %p\n", sock);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001203 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1204 if (!svsk)
1205 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 inet = sock->sk;
1208
1209 /* Register socket with portmapper */
J. Bruce Fields72c35372012-08-13 17:46:17 -04001210 if (pmap_register)
1211 err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001212 inet->sk_protocol,
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001213 ntohs(inet_sk(inet)->inet_sport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
J. Bruce Fields72c35372012-08-13 17:46:17 -04001215 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 kfree(svsk);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001217 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 svsk->sk_sock = sock;
1221 svsk->sk_sk = inet;
1222 svsk->sk_ostate = inet->sk_state_change;
1223 svsk->sk_odata = inet->sk_data_ready;
1224 svsk->sk_owspace = inet->sk_write_space;
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -04001225 /*
1226 * This barrier is necessary in order to prevent race condition
1227 * with svc_data_ready(), svc_listen_data_ready() and others
1228 * when calling callbacks above.
1229 */
1230 wmb();
1231 inet->sk_user_data = svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 /* Initialize the socket */
1234 if (sock->type == SOCK_DGRAM)
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001235 svc_udp_init(svsk, serv);
Trond Myklebust637600f2016-06-24 10:55:51 -04001236 else
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001237 svc_tcp_init(svsk, serv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Trond Myklebustc7995f82016-07-26 09:51:20 -04001239 dprintk("svc: svc_setup_socket created %p (inet %p), "
1240 "listen %d close %d\n",
1241 svsk, svsk->sk_sk,
1242 test_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags),
1243 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return svsk;
1246}
1247
Stanislav Kinsbursky30646392014-02-26 16:50:01 +03001248bool svc_alien_sock(struct net *net, int fd)
1249{
1250 int err;
1251 struct socket *sock = sockfd_lookup(fd, &err);
1252 bool ret = false;
1253
1254 if (!sock)
1255 goto out;
1256 if (sock_net(sock->sk) != net)
1257 ret = true;
1258 sockfd_put(sock);
1259out:
1260 return ret;
1261}
1262EXPORT_SYMBOL_GPL(svc_alien_sock);
1263
Chuck Leverbfba9ab2009-04-23 19:32:33 -04001264/**
1265 * svc_addsock - add a listener socket to an RPC service
1266 * @serv: pointer to RPC service to which to add a new listener
1267 * @fd: file descriptor of the new listener
1268 * @name_return: pointer to buffer to fill in with name of listener
1269 * @len: size of the buffer
Trond Myklebust4df493a2019-04-09 12:13:37 -04001270 * @cred: credential
Chuck Leverbfba9ab2009-04-23 19:32:33 -04001271 *
1272 * Fills in socket name and returns positive length of name if successful.
1273 * Name is terminated with '\n'. On error, returns a negative errno
1274 * value.
1275 */
1276int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
Trond Myklebust4df493a2019-04-09 12:13:37 -04001277 const size_t len, const struct cred *cred)
NeilBrownb41b66d2006-10-02 02:17:48 -07001278{
1279 int err = 0;
1280 struct socket *so = sockfd_lookup(fd, &err);
1281 struct svc_sock *svsk = NULL;
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001282 struct sockaddr_storage addr;
1283 struct sockaddr *sin = (struct sockaddr *)&addr;
1284 int salen;
NeilBrownb41b66d2006-10-02 02:17:48 -07001285
1286 if (!so)
1287 return err;
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001288 err = -EAFNOSUPPORT;
Aime Le Rouzic205ba422010-01-26 14:03:56 -05001289 if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001290 goto out;
1291 err = -EPROTONOSUPPORT;
1292 if (so->sk->sk_protocol != IPPROTO_TCP &&
NeilBrownb41b66d2006-10-02 02:17:48 -07001293 so->sk->sk_protocol != IPPROTO_UDP)
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001294 goto out;
1295 err = -EISCONN;
1296 if (so->state > SS_UNCONNECTED)
1297 goto out;
1298 err = -ENOENT;
1299 if (!try_module_get(THIS_MODULE))
1300 goto out;
1301 svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1302 if (IS_ERR(svsk)) {
1303 module_put(THIS_MODULE);
1304 err = PTR_ERR(svsk);
1305 goto out;
NeilBrownb41b66d2006-10-02 02:17:48 -07001306 }
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001307 salen = kernel_getsockname(svsk->sk_sock, sin);
1308 if (salen >= 0)
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001309 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
Trond Myklebust4df493a2019-04-09 12:13:37 -04001310 svsk->sk_xprt.xpt_cred = get_cred(cred);
J. Bruce Fields39b55302012-08-14 15:50:34 -04001311 svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
Chuck Levere7942b92009-04-23 19:32:48 -04001312 return svc_one_sock_name(svsk, name_return, len);
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001313out:
1314 sockfd_put(so);
1315 return err;
NeilBrownb41b66d2006-10-02 02:17:48 -07001316}
1317EXPORT_SYMBOL_GPL(svc_addsock);
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319/*
1320 * Create socket for RPC service.
1321 */
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001322static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1323 int protocol,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001324 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001325 struct sockaddr *sin, int len,
1326 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 struct svc_sock *svsk;
1329 struct socket *sock;
1330 int error;
1331 int type;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001332 struct sockaddr_storage addr;
1333 struct sockaddr *newsin = (struct sockaddr *)&addr;
1334 int newlen;
Trond Myklebustc69da772009-03-30 18:59:17 -04001335 int family;
1336 int val;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +03001337 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Chuck Leverad06e4b2007-02-12 00:53:32 -08001339 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1340 serv->sv_program->pg_name, protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001341 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1344 printk(KERN_WARNING "svc: only UDP and TCP "
1345 "sockets supported\n");
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001346 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Trond Myklebustc69da772009-03-30 18:59:17 -04001349 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1350 switch (sin->sa_family) {
1351 case AF_INET6:
1352 family = PF_INET6;
1353 break;
1354 case AF_INET:
1355 family = PF_INET;
1356 break;
1357 default:
1358 return ERR_PTR(-EINVAL);
1359 }
1360
Pavel Emelyanov14ec63c2010-09-29 16:06:57 +04001361 error = __sock_create(net, family, type, protocol, &sock, 1);
Chuck Lever77f1f672007-02-12 00:53:39 -08001362 if (error < 0)
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001363 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Peter Zijlstraed075362006-12-06 20:35:24 -08001365 svc_reclassify_socket(sock);
1366
Trond Myklebustc69da772009-03-30 18:59:17 -04001367 /*
1368 * If this is an PF_INET6 listener, we want to avoid
1369 * getting requests from IPv4 remotes. Those should
1370 * be shunted to a PF_INET listener via rpcbind.
1371 */
1372 val = 1;
1373 if (family == PF_INET6)
1374 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
1375 (char *)&val, sizeof(val));
1376
Eric Sesterhenn18114742006-09-28 14:37:07 -07001377 if (type == SOCK_STREAM)
Pavel Emelyanov4a17fd52012-04-19 03:39:36 +00001378 sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
Chuck Lever77f1f672007-02-12 00:53:39 -08001379 error = kernel_bind(sock, sin, len);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001380 if (error < 0)
1381 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001383 error = kernel_getsockname(sock, newsin);
Tom Tucker9dbc2402007-12-30 21:08:12 -06001384 if (error < 0)
1385 goto bummer;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001386 newlen = error;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001389 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 goto bummer;
1391 }
1392
J. Bruce Fields72c35372012-08-13 17:46:17 -04001393 svsk = svc_setup_socket(serv, sock, flags);
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001394 if (IS_ERR(svsk)) {
1395 error = PTR_ERR(svsk);
1396 goto bummer;
NeilBrowne79eff12007-02-12 00:53:30 -08001397 }
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001398 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1399 return (struct svc_xprt *)svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400bummer:
1401 dprintk("svc: svc_create_socket error = %d\n", -error);
1402 sock_release(sock);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001403 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
1406/*
Tom Tucker755ccea2007-12-30 21:07:27 -06001407 * Detach the svc_sock from the socket so that no
1408 * more callbacks occur.
1409 */
1410static void svc_sock_detach(struct svc_xprt *xprt)
1411{
1412 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1413 struct sock *sk = svsk->sk_sk;
1414
1415 dprintk("svc: svc_sock_detach(%p)\n", svsk);
1416
1417 /* put back the old socket callbacks */
Trond Myklebust069c2252016-06-24 10:55:47 -04001418 lock_sock(sk);
Tom Tucker755ccea2007-12-30 21:07:27 -06001419 sk->sk_state_change = svsk->sk_ostate;
1420 sk->sk_data_ready = svsk->sk_odata;
1421 sk->sk_write_space = svsk->sk_owspace;
Trond Myklebust069c2252016-06-24 10:55:47 -04001422 sk->sk_user_data = NULL;
1423 release_sock(sk);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001424}
1425
1426/*
1427 * Disconnect the socket, and reset the callbacks
1428 */
1429static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1430{
1431 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1432
1433 dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk);
1434
1435 svc_sock_detach(xprt);
1436
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001437 if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1438 svc_tcp_clear_pages(svsk);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001439 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001440 }
Tom Tucker755ccea2007-12-30 21:07:27 -06001441}
1442
1443/*
1444 * Free the svc_sock's socket resources and the svc_sock itself.
1445 */
1446static void svc_sock_free(struct svc_xprt *xprt)
1447{
1448 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1449 dprintk("svc: svc_sock_free(%p)\n", svsk);
1450
Tom Tucker755ccea2007-12-30 21:07:27 -06001451 if (svsk->sk_sock->file)
1452 sockfd_put(svsk->sk_sock);
1453 else
1454 sock_release(svsk->sk_sock);
1455 kfree(svsk);
1456}