blob: 64765f08ae07915502b23ff1f147730938005bd9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/svcsock.c
3 *
4 * These are the RPC server socket internals.
5 *
6 * The server scheduling algorithm does not always distribute the load
7 * evenly when servicing a single client. May need to modify the
Tom Tuckerf6150c32007-12-30 21:07:57 -06008 * svc_xprt_enqueue procedure...
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * TCP support is largely untested and may be a little slow. The problem
11 * is that we currently do two separate recvfrom's, one for the 4-byte
12 * record length, and the second for the actual record. This could possibly
13 * be improved by always reading a minimum size of around 100 bytes and
14 * tucking any superfluous bytes away in a temporary store. Still, that
15 * leaves write requests out in the rain. An alternative may be to peek at
16 * the first skb in the queue, and if it matches the next TCP sequence
17 * number, to extract the record marker. Yuck.
18 *
19 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
20 */
21
Ilpo Järvinen172589c2007-08-28 15:50:33 -070022#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/sched.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040024#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/errno.h>
26#include <linux/fcntl.h>
27#include <linux/net.h>
28#include <linux/in.h>
29#include <linux/inet.h>
30#include <linux/udp.h>
Andrew Morton91483c42005-08-09 20:20:07 -070031#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/unistd.h>
33#include <linux/slab.h>
34#include <linux/netdevice.h>
35#include <linux/skbuff.h>
NeilBrownb41b66d2006-10-02 02:17:48 -070036#include <linux/file.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080037#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <net/sock.h>
39#include <net/checksum.h>
40#include <net/ip.h>
Chuck Leverb92503b2007-02-12 00:53:36 -080041#include <net/ipv6.h>
Paolo Abeni850cbad2016-10-21 13:55:47 +020042#include <net/udp.h>
Chuck Leverb7872fe2008-04-14 12:27:01 -040043#include <net/tcp.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070044#include <net/tcp_states.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080045#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/ioctls.h>
Eric Dumazet22911fc2012-06-27 00:23:44 +000047#include <trace/events/skb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <linux/sunrpc/types.h>
Chuck Leverad06e4b2007-02-12 00:53:32 -080050#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/sunrpc/xdr.h>
Chuck Leverc0401ea2008-04-14 12:27:30 -040052#include <linux/sunrpc/msg_prot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/sunrpc/svcsock.h>
54#include <linux/sunrpc/stats.h>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030055#include <linux/sunrpc/xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
H Hartley Sweeten177e4f92011-06-20 17:54:51 -070057#include "sunrpc.h"
58
Tom Tucker360d87382007-12-30 21:07:17 -060059#define RPCDBG_FACILITY RPCDBG_SVCXPRT
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61
62static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
J. Bruce Fields72c35372012-08-13 17:46:17 -040063 int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int svc_udp_recvfrom(struct svc_rqst *);
65static int svc_udp_sendto(struct svc_rqst *);
Tom Tucker755ccea2007-12-30 21:07:27 -060066static void svc_sock_detach(struct svc_xprt *);
Trond Myklebust69b6ba32008-12-23 16:30:11 -050067static void svc_tcp_sock_detach(struct svc_xprt *);
Tom Tucker755ccea2007-12-30 21:07:27 -060068static void svc_sock_free(struct svc_xprt *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Tom Tuckerb700cbb2007-12-30 21:07:42 -060070static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
Pavel Emelyanov62832c02010-09-29 16:04:18 +040071 struct net *, struct sockaddr *,
72 int, int);
Trond Myklebust9e00abc2011-07-13 19:20:49 -040073#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Andy Adamson1f11a032011-01-06 02:04:26 +000074static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
75 struct net *, struct sockaddr *,
76 int, int);
77static void svc_bc_sock_free(struct svc_xprt *xprt);
Trond Myklebust9e00abc2011-07-13 19:20:49 -040078#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Andy Adamson1f11a032011-01-06 02:04:26 +000079
Peter Zijlstraed075362006-12-06 20:35:24 -080080#ifdef CONFIG_DEBUG_LOCK_ALLOC
81static struct lock_class_key svc_key[2];
82static struct lock_class_key svc_slock_key[2];
83
Tom Tucker0f0257e2007-12-30 21:08:27 -060084static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -080085{
86 struct sock *sk = sock->sk;
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -040087
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +020088 if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -040089 return;
90
Peter Zijlstraed075362006-12-06 20:35:24 -080091 switch (sk->sk_family) {
92 case AF_INET:
93 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -060094 &svc_slock_key[0],
95 "sk_xprt.xpt_lock-AF_INET-NFSD",
96 &svc_key[0]);
Peter Zijlstraed075362006-12-06 20:35:24 -080097 break;
98
99 case AF_INET6:
100 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -0600101 &svc_slock_key[1],
102 "sk_xprt.xpt_lock-AF_INET6-NFSD",
103 &svc_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -0800104 break;
105
106 default:
107 BUG();
108 }
109}
110#else
Tom Tucker0f0257e2007-12-30 21:08:27 -0600111static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -0800112{
113}
114#endif
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/*
117 * Release an skbuff after use
118 */
Tom Tucker5148bf42007-12-30 21:07:25 -0600119static void svc_release_skb(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Tom Tucker5148bf42007-12-30 21:07:25 -0600121 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 if (skb) {
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600124 struct svc_sock *svsk =
125 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tucker5148bf42007-12-30 21:07:25 -0600126 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
Eric Dumazet9d410c72009-10-30 05:03:53 +0000129 skb_free_datagram_locked(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Paolo Abeni850cbad2016-10-21 13:55:47 +0200133static void svc_release_udp_skb(struct svc_rqst *rqstp)
134{
135 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
136
137 if (skb) {
138 rqstp->rq_xprt_ctxt = NULL;
139
140 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
141 consume_skb(skb);
142 }
143}
144
Chuck Leverb92503b2007-02-12 00:53:36 -0800145union svc_pktinfo_u {
146 struct in_pktinfo pkti;
Chuck Leverb92503b2007-02-12 00:53:36 -0800147 struct in6_pktinfo pkti6;
Chuck Leverb92503b2007-02-12 00:53:36 -0800148};
David S. Millerbc375ea2007-04-12 13:35:59 -0700149#define SVC_PKTINFO_SPACE \
150 CMSG_SPACE(sizeof(union svc_pktinfo_u))
Chuck Leverb92503b2007-02-12 00:53:36 -0800151
152static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
153{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600154 struct svc_sock *svsk =
155 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
156 switch (svsk->sk_sk->sk_family) {
Chuck Leverb92503b2007-02-12 00:53:36 -0800157 case AF_INET: {
158 struct in_pktinfo *pki = CMSG_DATA(cmh);
159
160 cmh->cmsg_level = SOL_IP;
161 cmh->cmsg_type = IP_PKTINFO;
162 pki->ipi_ifindex = 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800163 pki->ipi_spec_dst.s_addr =
164 svc_daddr_in(rqstp)->sin_addr.s_addr;
Chuck Leverb92503b2007-02-12 00:53:36 -0800165 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
166 }
167 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800168
Chuck Leverb92503b2007-02-12 00:53:36 -0800169 case AF_INET6: {
170 struct in6_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800171 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
Chuck Leverb92503b2007-02-12 00:53:36 -0800172
173 cmh->cmsg_level = SOL_IPV6;
174 cmh->cmsg_type = IPV6_PKTINFO;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800175 pki->ipi6_ifindex = daddr->sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000176 pki->ipi6_addr = daddr->sin6_addr;
Chuck Leverb92503b2007-02-12 00:53:36 -0800177 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
178 }
179 break;
Chuck Leverb92503b2007-02-12 00:53:36 -0800180 }
Chuck Leverb92503b2007-02-12 00:53:36 -0800181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300184 * send routine intended to be shared by the fore- and back-channel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300186int svc_send_common(struct socket *sock, struct xdr_buf *xdr,
187 struct page *headpage, unsigned long headoffset,
188 struct page *tailpage, unsigned long tailoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int result;
191 int size;
192 struct page **ppage = xdr->pages;
193 size_t base = xdr->page_base;
194 unsigned int pglen = xdr->page_len;
Trond Myklebust226453d2015-10-07 13:54:42 -0400195 unsigned int flags = MSG_MORE | MSG_SENDPAGE_NOTLAST;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300196 int slen;
197 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 slen = xdr->len;
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* send head */
202 if (slen == xdr->head[0].iov_len)
203 flags = 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300204 len = kernel_sendpage(sock, headpage, headoffset,
NeilBrown44524352006-10-04 02:15:46 -0700205 xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (len != xdr->head[0].iov_len)
207 goto out;
208 slen -= xdr->head[0].iov_len;
209 if (slen == 0)
210 goto out;
211
212 /* send page data */
213 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
214 while (pglen > 0) {
215 if (slen == size)
216 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700217 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (result > 0)
219 len += result;
220 if (result != size)
221 goto out;
222 slen -= size;
223 pglen -= size;
224 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
225 base = 0;
226 ppage++;
227 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* send tail */
230 if (xdr->tail[0].iov_len) {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300231 result = kernel_sendpage(sock, tailpage, tailoffset,
232 xdr->tail[0].iov_len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (result > 0)
234 len += result;
235 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300236
237out:
238 return len;
239}
240
241
242/*
243 * Generic sendto routine
244 */
245static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
246{
247 struct svc_sock *svsk =
248 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
249 struct socket *sock = svsk->sk_sock;
250 union {
251 struct cmsghdr hdr;
252 long all[SVC_PKTINFO_SPACE / sizeof(long)];
253 } buffer;
254 struct cmsghdr *cmh = &buffer.hdr;
255 int len = 0;
256 unsigned long tailoff;
257 unsigned long headoff;
258 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
259
260 if (rqstp->rq_prot == IPPROTO_UDP) {
261 struct msghdr msg = {
262 .msg_name = &rqstp->rq_addr,
263 .msg_namelen = rqstp->rq_addrlen,
264 .msg_control = cmh,
265 .msg_controllen = sizeof(buffer),
266 .msg_flags = MSG_MORE,
267 };
268
269 svc_set_cmsg_data(rqstp, cmh);
270
Al Virod8725c82014-12-11 00:02:50 -0500271 if (sock_sendmsg(sock, &msg) < 0)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300272 goto out;
273 }
274
275 tailoff = ((unsigned long)xdr->tail[0].iov_base) & (PAGE_SIZE-1);
276 headoff = 0;
277 len = svc_send_common(sock, xdr, rqstp->rq_respages[0], headoff,
278 rqstp->rq_respages[0], tailoff);
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280out:
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800281 dprintk("svc: socket %p sendto([%p %zu... ], %d) = %d (addr %s)\n",
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600282 svsk, xdr->head[0].iov_base, xdr->head[0].iov_len,
Chuck Leverad06e4b2007-02-12 00:53:32 -0800283 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 return len;
286}
287
288/*
NeilBrown80212d52006-10-02 02:17:47 -0700289 * Report socket names for nfsdfs
290 */
Chuck Levere7942b92009-04-23 19:32:48 -0400291static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
NeilBrown80212d52006-10-02 02:17:47 -0700292{
Chuck Lever017cb472009-04-23 19:33:03 -0400293 const struct sock *sk = svsk->sk_sk;
294 const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
295 "udp" : "tcp";
NeilBrown80212d52006-10-02 02:17:47 -0700296 int len;
297
Chuck Lever017cb472009-04-23 19:33:03 -0400298 switch (sk->sk_family) {
Chuck Levere7942b92009-04-23 19:32:48 -0400299 case PF_INET:
300 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400301 proto_name,
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000302 &inet_sk(sk)->inet_rcv_saddr,
303 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700304 break;
Eric Dumazetc2bb06d2013-10-09 03:05:48 -0700305#if IS_ENABLED(CONFIG_IPV6)
Chuck Lever58de2f82009-04-23 19:32:55 -0400306 case PF_INET6:
307 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400308 proto_name,
Eric Dumazetefe42082013-10-03 15:42:29 -0700309 &sk->sk_v6_rcv_saddr,
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000310 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700311 break;
Eric Dumazetc2bb06d2013-10-09 03:05:48 -0700312#endif
NeilBrown80212d52006-10-02 02:17:47 -0700313 default:
Chuck Levere7942b92009-04-23 19:32:48 -0400314 len = snprintf(buf, remaining, "*unknown-%d*\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400315 sk->sk_family);
NeilBrown80212d52006-10-02 02:17:47 -0700316 }
Chuck Levere7942b92009-04-23 19:32:48 -0400317
318 if (len >= remaining) {
319 *buf = '\0';
320 return -ENAMETOOLONG;
NeilBrown80212d52006-10-02 02:17:47 -0700321 }
322 return len;
323}
324
NeilBrown80212d52006-10-02 02:17:47 -0700325/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 * Generic recvfrom routine.
327 */
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400328static ssize_t svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov,
329 unsigned int nr, size_t buflen, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600331 struct svc_sock *svsk =
332 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400333 struct msghdr msg = { NULL };
334 ssize_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Tom Tucker260c1d12007-12-30 21:08:29 -0600336 rqstp->rq_xprt_hlen = 0;
337
Trond Myklebustf8d1ff42014-08-03 13:03:13 -0400338 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Al Viro1e4ed1b2017-07-01 17:01:59 -0400339 iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, iov, nr, buflen);
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400340 if (base != 0) {
341 iov_iter_advance(&msg.msg_iter, base);
342 buflen -= base;
343 }
344 len = sock_recvmsg(svsk->sk_sock, &msg, MSG_DONTWAIT);
Trond Myklebustf8d1ff42014-08-03 13:03:13 -0400345 /* If we read a full record, then assume there may be more
346 * data to read (stream based sockets only!)
347 */
348 if (len == buflen)
349 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400351 dprintk("svc: socket %p recvfrom(%p, %zu) = %zd\n",
Chuck Lever1ba95102007-02-12 00:53:31 -0800352 svsk, iov[0].iov_base, iov[0].iov_len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return len;
354}
355
356/*
357 * Set socket snd and rcv buffer lengths
358 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600359static void svc_sock_setbufsize(struct socket *sock, unsigned int snd,
360 unsigned int rcv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 lock_sock(sock->sk);
363 sock->sk->sk_sndbuf = snd * 2;
364 sock->sk->sk_rcvbuf = rcv * 2;
Trond Myklebust47fcb032009-05-18 17:47:56 -0400365 sock->sk->sk_write_space(sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 release_sock(sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
Chuck Lever16e4d932014-05-19 13:40:22 -0400368
Chuck Lever989f8812018-03-27 10:49:38 -0400369static void svc_sock_secure_port(struct svc_rqst *rqstp)
Chuck Lever16e4d932014-05-19 13:40:22 -0400370{
Chuck Lever989f8812018-03-27 10:49:38 -0400371 if (svc_port_is_privileged(svc_addr(rqstp)))
372 set_bit(RQ_SECURE, &rqstp->rq_flags);
373 else
374 clear_bit(RQ_SECURE, &rqstp->rq_flags);
Chuck Lever16e4d932014-05-19 13:40:22 -0400375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377/*
378 * INET callback when data has been received on the socket.
379 */
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400380static void svc_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Neil Brown939bb7e2005-09-13 01:25:39 -0700382 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Neil Brown939bb7e2005-09-13 01:25:39 -0700384 if (svsk) {
David S. Miller676d2362014-04-11 16:15:36 -0400385 dprintk("svc: socket %p(inet %p), busy=%d\n",
386 svsk, sk,
Tom Tucker02fc6c32007-12-30 21:07:48 -0600387 test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400388
389 /* Refer to svc_setup_socket() for details. */
390 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400391 svsk->sk_odata(sk);
Trond Myklebust4720b072016-06-24 10:55:49 -0400392 if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags))
393 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397/*
398 * INET callback when space is newly available on the socket.
399 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600400static void svc_write_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
403
404 if (svsk) {
405 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600406 svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400407
408 /* Refer to svc_setup_socket() for details. */
409 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400410 svsk->sk_owspace(sk);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600411 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Trond Myklebustc7fb3f02014-07-24 23:59:32 -0400415static int svc_tcp_has_wspace(struct svc_xprt *xprt)
416{
Trond Myklebust637600f2016-06-24 10:55:51 -0400417 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Trond Myklebustc7fb3f02014-07-24 23:59:32 -0400418
419 if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
420 return 1;
Trond Myklebust637600f2016-06-24 10:55:51 -0400421 return !test_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Trond Myklebust51877682014-07-24 23:59:33 -0400422}
423
Scott Mayhewea08e392016-11-11 13:16:22 -0500424static void svc_tcp_kill_temp_xprt(struct svc_xprt *xprt)
425{
426 struct svc_sock *svsk;
427 struct socket *sock;
428 struct linger no_linger = {
429 .l_onoff = 1,
430 .l_linger = 0,
431 };
432
433 svsk = container_of(xprt, struct svc_sock, sk_xprt);
434 sock = svsk->sk_sock;
435 kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER,
436 (char *)&no_linger, sizeof(no_linger));
437}
438
Tom Tucker9dbc2402007-12-30 21:08:12 -0600439/*
Chuck Lever7702ce42009-07-13 10:54:26 -0400440 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
441 */
442static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
443 struct cmsghdr *cmh)
444{
445 struct in_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800446 struct sockaddr_in *daddr = svc_daddr_in(rqstp);
447
Chuck Lever7702ce42009-07-13 10:54:26 -0400448 if (cmh->cmsg_type != IP_PKTINFO)
449 return 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800450
451 daddr->sin_family = AF_INET;
452 daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
Chuck Lever7702ce42009-07-13 10:54:26 -0400453 return 1;
454}
455
456/*
Tom Parkin73df66f2013-01-31 01:02:24 +0000457 * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
Chuck Lever7702ce42009-07-13 10:54:26 -0400458 */
459static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
460 struct cmsghdr *cmh)
461{
462 struct in6_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800463 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
464
Chuck Lever7702ce42009-07-13 10:54:26 -0400465 if (cmh->cmsg_type != IPV6_PKTINFO)
466 return 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800467
468 daddr->sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000469 daddr->sin6_addr = pki->ipi6_addr;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800470 daddr->sin6_scope_id = pki->ipi6_ifindex;
Chuck Lever7702ce42009-07-13 10:54:26 -0400471 return 1;
472}
473
474/*
Tom Tucker9dbc2402007-12-30 21:08:12 -0600475 * Copy the UDP datagram's destination address to the rqstp structure.
476 * The 'destination' address in this case is the address to which the
477 * peer sent the datagram, i.e. our local address. For multihomed
478 * hosts, this can change from msg to msg. Note that only the IP
479 * address changes, the port number should remain the same.
480 */
Chuck Lever7702ce42009-07-13 10:54:26 -0400481static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
482 struct cmsghdr *cmh)
Chuck Lever95756482007-02-12 00:53:38 -0800483{
Chuck Lever7702ce42009-07-13 10:54:26 -0400484 switch (cmh->cmsg_level) {
485 case SOL_IP:
486 return svc_udp_get_dest_address4(rqstp, cmh);
487 case SOL_IPV6:
488 return svc_udp_get_dest_address6(rqstp, cmh);
Chuck Lever95756482007-02-12 00:53:38 -0800489 }
Chuck Lever7702ce42009-07-13 10:54:26 -0400490
491 return 0;
Chuck Lever95756482007-02-12 00:53:38 -0800492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
495 * Receive a datagram from a UDP socket.
496 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600497static int svc_udp_recvfrom(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600499 struct svc_sock *svsk =
500 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600501 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct sk_buff *skb;
David S. Millerbc375ea2007-04-12 13:35:59 -0700503 union {
504 struct cmsghdr hdr;
505 long all[SVC_PKTINFO_SPACE / sizeof(long)];
506 } buffer;
507 struct cmsghdr *cmh = &buffer.hdr;
NeilBrown7a37f572007-03-06 01:42:21 -0800508 struct msghdr msg = {
509 .msg_name = svc_addr(rqstp),
510 .msg_control = cmh,
511 .msg_controllen = sizeof(buffer),
512 .msg_flags = MSG_DONTWAIT,
513 };
Chuck Leverabc5c442009-04-23 19:31:25 -0400514 size_t len;
515 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Tom Tucker02fc6c32007-12-30 21:07:48 -0600517 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /* udp sockets need large rcvbuf as all pending
519 * requests are still in that buffer. sndbuf must
520 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700521 * for one reply per thread. We count all threads
522 * rather than threads in a particular pool, which
523 * provides an upper bound on the number of threads
524 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
526 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700527 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
528 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Tom Tucker02fc6c32007-12-30 21:07:48 -0600530 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
NeilBrown05ed6902007-05-09 02:34:55 -0700531 skb = NULL;
532 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
533 0, 0, MSG_PEEK | MSG_DONTWAIT);
534 if (err >= 0)
Paolo Abeni7c13f972016-11-04 11:28:59 +0100535 skb = skb_recv_udp(svsk->sk_sk, 0, 1, &err);
NeilBrown05ed6902007-05-09 02:34:55 -0700536
537 if (skb == NULL) {
538 if (err != -EAGAIN) {
539 /* possibly an icmp error */
540 dprintk("svc: recvfrom returned error %d\n", -err);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600541 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -0400543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600545 len = svc_addr_len(svc_addr(rqstp));
Tom Tucker9dbc2402007-12-30 21:08:12 -0600546 rqstp->rq_addrlen = len;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100547 if (skb->tstamp == 0) {
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700548 skb->tstamp = ktime_get_real();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800549 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 need that much accuracy */
551 }
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700552 svsk->sk_sk->sk_stamp = skb->tstamp;
Tom Tucker02fc6c32007-12-30 21:07:48 -0600553 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Willem de Bruijn1da8c6812016-04-07 11:44:58 -0400555 len = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 rqstp->rq_arg.len = len;
557
Chuck Lever95756482007-02-12 00:53:38 -0800558 rqstp->rq_prot = IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Chuck Lever7702ce42009-07-13 10:54:26 -0400560 if (!svc_udp_get_dest_address(rqstp, cmh)) {
Joe Perchese87cc472012-05-13 21:56:26 +0000561 net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
562 cmh->cmsg_level, cmh->cmsg_type);
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400563 goto out_free;
NeilBrown7a37f572007-03-06 01:42:21 -0800564 }
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800565 rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (skb_is_nonlinear(skb)) {
568 /* we have to copy */
569 local_bh_disable();
570 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
571 local_bh_enable();
572 /* checksum error */
Eric Dumazet22911fc2012-06-27 00:23:44 +0000573 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 local_bh_enable();
Paolo Abeni850cbad2016-10-21 13:55:47 +0200576 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 } else {
578 /* we can use it in-place */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -0400579 rqstp->rq_arg.head[0].iov_base = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 rqstp->rq_arg.head[0].iov_len = len;
Eric Dumazet22911fc2012-06-27 00:23:44 +0000581 if (skb_checksum_complete(skb))
582 goto out_free;
Tom Tucker5148bf42007-12-30 21:07:25 -0600583 rqstp->rq_xprt_ctxt = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
586 rqstp->rq_arg.page_base = 0;
587 if (len <= rqstp->rq_arg.head[0].iov_len) {
588 rqstp->rq_arg.head[0].iov_len = len;
589 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700590 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 } else {
592 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700593 rqstp->rq_respages = rqstp->rq_pages + 1 +
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700594 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500596 rqstp->rq_next_page = rqstp->rq_respages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (serv->sv_stats)
599 serv->sv_stats->netudpcnt++;
600
601 return len;
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400602out_free:
Paolo Abeni850cbad2016-10-21 13:55:47 +0200603 kfree_skb(skb);
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400604 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607static int
608svc_udp_sendto(struct svc_rqst *rqstp)
609{
610 int error;
611
612 error = svc_sendto(rqstp, &rqstp->rq_res);
613 if (error == -ECONNREFUSED)
614 /* ICMP error on earlier request. */
615 error = svc_sendto(rqstp, &rqstp->rq_res);
616
617 return error;
618}
619
Tom Tuckere831fe62007-12-30 21:07:29 -0600620static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
621{
622}
623
Tom Tucker323bee32007-12-30 21:07:31 -0600624static int svc_udp_has_wspace(struct svc_xprt *xprt)
625{
626 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600627 struct svc_serv *serv = xprt->xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -0600628 unsigned long required;
629
630 /*
631 * Set the SOCK_NOSPACE flag before checking the available
632 * sock space.
633 */
634 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Tom Tucker7a90e8c2007-12-30 21:07:55 -0600635 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
Tom Tucker323bee32007-12-30 21:07:31 -0600636 if (required*2 > sock_wspace(svsk->sk_sk))
637 return 0;
638 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
639 return 1;
640}
641
Tom Tucker38a417c2007-12-30 21:07:36 -0600642static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
643{
644 BUG();
645 return NULL;
646}
647
Scott Mayhewea08e392016-11-11 13:16:22 -0500648static void svc_udp_kill_temp_xprt(struct svc_xprt *xprt)
649{
650}
651
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600652static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400653 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600654 struct sockaddr *sa, int salen,
655 int flags)
656{
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400657 return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600658}
659
Chuck Lever2412e922017-08-01 11:59:49 -0400660static const struct svc_xprt_ops svc_udp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600661 .xpo_create = svc_udp_create,
Tom Tucker5d137992007-12-30 21:07:23 -0600662 .xpo_recvfrom = svc_udp_recvfrom,
663 .xpo_sendto = svc_udp_sendto,
Paolo Abeni850cbad2016-10-21 13:55:47 +0200664 .xpo_release_rqst = svc_release_udp_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -0600665 .xpo_detach = svc_sock_detach,
666 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -0600667 .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -0600668 .xpo_has_wspace = svc_udp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -0600669 .xpo_accept = svc_udp_accept,
Chuck Lever16e4d932014-05-19 13:40:22 -0400670 .xpo_secure_port = svc_sock_secure_port,
Scott Mayhewea08e392016-11-11 13:16:22 -0500671 .xpo_kill_temp_xprt = svc_udp_kill_temp_xprt,
Tom Tucker360d87382007-12-30 21:07:17 -0600672};
673
674static struct svc_xprt_class svc_udp_class = {
675 .xcl_name = "udp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600676 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -0600677 .xcl_ops = &svc_udp_ops,
Tom Tucker49023152007-12-30 21:07:21 -0600678 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
Chuck Lever3c45ddf2014-07-16 15:38:32 -0400679 .xcl_ident = XPRT_TRANSPORT_UDP,
Tom Tucker360d87382007-12-30 21:07:17 -0600680};
681
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600682static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Chuck Lever7702ce42009-07-13 10:54:26 -0400684 int err, level, optname, one = 1;
NeilBrown7a37f572007-03-06 01:42:21 -0800685
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +0300686 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
687 &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600688 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400689 svsk->sk_sk->sk_data_ready = svc_data_ready;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 svsk->sk_sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800693 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 * svc_udp_recvfrom will re-adjust if necessary
695 */
696 svc_sock_setbufsize(svsk->sk_sock,
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600697 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
698 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Tom Tucker0f0257e2007-12-30 21:08:27 -0600700 /* data might have come in before data_ready set up */
701 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600702 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800703
NeilBrown7a37f572007-03-06 01:42:21 -0800704 /* make sure we get destination address info */
Chuck Lever7702ce42009-07-13 10:54:26 -0400705 switch (svsk->sk_sk->sk_family) {
706 case AF_INET:
707 level = SOL_IP;
708 optname = IP_PKTINFO;
709 break;
710 case AF_INET6:
711 level = SOL_IPV6;
712 optname = IPV6_RECVPKTINFO;
713 break;
714 default:
715 BUG();
716 }
717 err = kernel_setsockopt(svsk->sk_sock, level, optname,
718 (char *)&one, sizeof(one));
719 dprintk("svc: kernel_setsockopt returned %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722/*
723 * A data_ready event on a listening socket means there's a connection
724 * pending. Do not use state_change as a substitute for it.
725 */
David S. Miller676d2362014-04-11 16:15:36 -0400726static void svc_tcp_listen_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Neil Brown939bb7e2005-09-13 01:25:39 -0700728 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700731 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400733 if (svsk) {
734 /* Refer to svc_setup_socket() for details. */
735 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400736 svsk->sk_odata(sk);
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400737 }
738
Neil Brown939bb7e2005-09-13 01:25:39 -0700739 /*
740 * This callback may called twice when a new connection
741 * is established as a child socket inherits everything
742 * from a parent LISTEN socket.
743 * 1) data_ready method of the parent socket will be called
744 * when one of child sockets become ESTABLISHED.
745 * 2) data_ready method of the child socket may be called
746 * when it receives data before the socket is accepted.
747 * In case of 2, we should ignore it silently.
748 */
749 if (sk->sk_state == TCP_LISTEN) {
750 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600751 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600752 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700753 } else
754 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758/*
759 * A state change on a connected socket means it's dying or dead.
760 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600761static void svc_tcp_state_change(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Neil Brown939bb7e2005-09-13 01:25:39 -0700763 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700766 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Neil Brown939bb7e2005-09-13 01:25:39 -0700768 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700770 else {
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400771 /* Refer to svc_setup_socket() for details. */
772 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400773 svsk->sk_ostate(sk);
Trond Myklebustb2f21f72016-07-26 09:51:19 -0400774 if (sk->sk_state != TCP_ESTABLISHED) {
775 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
776 svc_xprt_enqueue(&svsk->sk_xprt);
777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
781/*
782 * Accept a TCP connection
783 */
Tom Tucker38a417c2007-12-30 21:07:36 -0600784static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Tom Tucker38a417c2007-12-30 21:07:36 -0600786 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800787 struct sockaddr_storage addr;
788 struct sockaddr *sin = (struct sockaddr *) &addr;
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600789 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 struct socket *sock = svsk->sk_sock;
791 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct svc_sock *newsvsk;
793 int err, slen;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +0300794 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
797 if (!sock)
Tom Tucker38a417c2007-12-30 21:07:36 -0600798 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Tom Tucker02fc6c32007-12-30 21:07:48 -0600800 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700801 err = kernel_accept(sock, &newsock, O_NONBLOCK);
802 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (err == -ENOMEM)
804 printk(KERN_WARNING "%s: no more sockets!\n",
805 serv->sv_name);
Joe Perchese87cc472012-05-13 21:56:26 +0000806 else if (err != -EAGAIN)
807 net_warn_ratelimited("%s: accept failed (err %d)!\n",
808 serv->sv_name, -err);
Tom Tucker38a417c2007-12-30 21:07:36 -0600809 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Tom Tucker02fc6c32007-12-30 21:07:48 -0600811 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100813 err = kernel_getpeername(newsock, sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (err < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +0000815 net_warn_ratelimited("%s: peername failed (err %d)!\n",
816 serv->sv_name, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 goto failed; /* aborted connection or whatever */
818 }
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100819 slen = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /* Ideally, we would want to reject connections from unauthorized
Chuck Leverad06e4b2007-02-12 00:53:32 -0800822 * hosts here, but when we get encryption, the IP of the host won't
823 * tell us anything. For now just warn about unpriv connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 */
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800825 if (!svc_port_is_privileged(sin)) {
Kinglong Meea48fd0f2014-05-29 12:18:52 +0800826 dprintk("%s: connect from unprivileged port: %s\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800827 serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800828 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Chuck Leverad06e4b2007-02-12 00:53:32 -0800830 dprintk("%s: connect from %s\n", serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800831 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400833 /* Reset the inherited callbacks before calling svc_setup_socket */
834 newsock->sk->sk_state_change = svsk->sk_ostate;
835 newsock->sk->sk_data_ready = svsk->sk_odata;
836 newsock->sk->sk_write_space = svsk->sk_owspace;
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* make sure that a write doesn't block forever when
839 * low on memory
840 */
841 newsock->sk->sk_sndtimeo = HZ*30;
842
J. Bruce Fields72c35372012-08-13 17:46:17 -0400843 newsvsk = svc_setup_socket(serv, newsock,
844 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
845 if (IS_ERR(newsvsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto failed;
Tom Tucker9dbc2402007-12-30 21:08:12 -0600847 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100848 err = kernel_getsockname(newsock, sin);
849 slen = err;
Frank van Maarseveena9747692007-07-09 22:21:39 +0200850 if (unlikely(err < 0)) {
851 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
852 slen = offsetof(struct sockaddr, sa_data);
853 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600854 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -0800855
NeilBrownef11ce22014-05-12 11:22:47 +1000856 if (sock_is_loopback(newsock->sk))
857 set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
858 else
859 clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600860 if (serv->sv_stats)
861 serv->sv_stats->nettcpconn++;
862
863 return &newsvsk->sk_xprt;
864
865failed:
866 sock_release(newsock);
867 return NULL;
868}
869
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800870static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
871{
872 unsigned int i, len, npages;
873
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500874 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800875 return 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500876 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800877 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
878 for (i = 0; i < npages; i++) {
879 if (rqstp->rq_pages[i] != NULL)
880 put_page(rqstp->rq_pages[i]);
881 BUG_ON(svsk->sk_pages[i] == NULL);
882 rqstp->rq_pages[i] = svsk->sk_pages[i];
883 svsk->sk_pages[i] = NULL;
884 }
885 rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
886 return len;
887}
888
889static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
890{
891 unsigned int i, len, npages;
892
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500893 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800894 return;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500895 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800896 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
897 for (i = 0; i < npages; i++) {
898 svsk->sk_pages[i] = rqstp->rq_pages[i];
899 rqstp->rq_pages[i] = NULL;
900 }
901}
902
903static void svc_tcp_clear_pages(struct svc_sock *svsk)
904{
905 unsigned int i, len, npages;
906
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500907 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800908 goto out;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500909 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800910 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
911 for (i = 0; i < npages; i++) {
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -0400912 if (svsk->sk_pages[i] == NULL) {
913 WARN_ON_ONCE(1);
914 continue;
915 }
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800916 put_page(svsk->sk_pages[i]);
917 svsk->sk_pages[i] = NULL;
918 }
919out:
920 svsk->sk_tcplen = 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500921 svsk->sk_datalen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800922}
923
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600924/*
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -0500925 * Receive fragment record header.
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300926 * If we haven't gotten the record length yet, get the next four bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 */
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300928static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600930 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400931 unsigned int want;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300932 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Chuck Leverc0401ea2008-04-14 12:27:30 -0400934 if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 struct kvec iov;
936
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400937 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
939 iov.iov_len = want;
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400940 len = svc_recvfrom(rqstp, &iov, 1, want, 0);
941 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 goto error;
943 svsk->sk_tcplen += len;
944
945 if (len < want) {
Chuck Leverc0401ea2008-04-14 12:27:30 -0400946 dprintk("svc: short recvfrom while reading record "
947 "length (%d of %d)\n", len, want);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800948 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500951 dprintk("svc: TCP record, %d bytes\n", svc_sock_reclen(svsk));
J. Bruce Fields836fbad2012-12-03 15:50:38 -0500952 if (svc_sock_reclen(svsk) + svsk->sk_datalen >
953 serv->sv_max_mesg) {
J. Bruce Fields3a28e332012-12-10 18:15:07 -0500954 net_notice_ratelimited("RPC: fragment too large: %d\n",
955 svc_sock_reclen(svsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 goto err_delete;
957 }
958 }
959
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500960 return svc_sock_reclen(svsk);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800961error:
962 dprintk("RPC: TCP recv_record got %d\n", len);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300963 return len;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800964err_delete:
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300965 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300966 return -EAGAIN;
967}
968
Trond Myklebust586c52c2009-05-18 17:47:56 -0400969static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300970{
Trond Myklebust586c52c2009-05-18 17:47:56 -0400971 struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300972 struct rpc_rqst *req = NULL;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400973 struct kvec *src, *dst;
974 __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
J. Bruce Fields48e65552011-02-14 14:52:03 -0500975 __be32 xid;
976 __be32 calldir;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300977
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300978 xid = *p++;
979 calldir = *p;
980
Trond Myklebust093a1462014-11-12 18:04:04 -0500981 if (!bc_xprt)
Trond Myklebust586c52c2009-05-18 17:47:56 -0400982 return -EAGAIN;
Trond Myklebustce7c2522017-08-16 15:30:35 -0400983 spin_lock(&bc_xprt->recv_lock);
Trond Myklebust093a1462014-11-12 18:04:04 -0500984 req = xprt_lookup_rqst(bc_xprt, xid);
985 if (!req)
986 goto unlock_notfound;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400987
988 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
989 /*
990 * XXX!: cheating for now! Only copying HEAD.
991 * But we know this is good enough for now (in fact, for any
992 * callback reply in the forseeable future).
993 */
994 dst = &req->rq_private_buf.head[0];
995 src = &rqstp->rq_arg.head[0];
996 if (dst->iov_len < src->iov_len)
Trond Myklebust093a1462014-11-12 18:04:04 -0500997 goto unlock_eagain; /* whatever; just giving up. */
Trond Myklebust586c52c2009-05-18 17:47:56 -0400998 memcpy(dst->iov_base, src->iov_base, src->iov_len);
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500999 xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len);
Trond Myklebust586c52c2009-05-18 17:47:56 -04001000 rqstp->rq_arg.len = 0;
Trond Myklebustce7c2522017-08-16 15:30:35 -04001001 spin_unlock(&bc_xprt->recv_lock);
Trond Myklebust586c52c2009-05-18 17:47:56 -04001002 return 0;
Trond Myklebust093a1462014-11-12 18:04:04 -05001003unlock_notfound:
1004 printk(KERN_NOTICE
1005 "%s: Got unrecognized reply: "
1006 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
1007 __func__, ntohl(calldir),
1008 bc_xprt, ntohl(xid));
1009unlock_eagain:
Trond Myklebustce7c2522017-08-16 15:30:35 -04001010 spin_unlock(&bc_xprt->recv_lock);
Trond Myklebust093a1462014-11-12 18:04:04 -05001011 return -EAGAIN;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001012}
1013
1014static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len)
1015{
1016 int i = 0;
1017 int t = 0;
1018
1019 while (t < len) {
1020 vec[i].iov_base = page_address(pages[i]);
1021 vec[i].iov_len = PAGE_SIZE;
1022 i++;
1023 t += PAGE_SIZE;
1024 }
1025 return i;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001026}
1027
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001028static void svc_tcp_fragment_received(struct svc_sock *svsk)
1029{
1030 /* If we have more data, signal svc_xprt_enqueue() to try again */
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001031 dprintk("svc: TCP %s record (%d bytes)\n",
1032 svc_sock_final_rec(svsk) ? "final" : "nonfinal",
1033 svc_sock_reclen(svsk));
1034 svsk->sk_tcplen = 0;
1035 svsk->sk_reclen = 0;
1036}
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001037
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001038/*
1039 * Receive data from a TCP socket.
1040 */
1041static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
1042{
1043 struct svc_sock *svsk =
1044 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
1045 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1046 int len;
1047 struct kvec *vec;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001048 unsigned int want, base;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001049 __be32 *p;
1050 __be32 calldir;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001051 int pnum;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001052
1053 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1054 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
1055 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
1056 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
1057
1058 len = svc_tcp_recv_record(svsk, rqstp);
1059 if (len < 0)
1060 goto error;
1061
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001062 base = svc_tcp_restore_pages(svsk, rqstp);
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001063 want = svc_sock_reclen(svsk) - (svsk->sk_tcplen - sizeof(rpc_fraghdr));
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001064
NeilBrown3cc03b12006-10-04 02:15:47 -07001065 vec = rqstp->rq_vec;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001066
Trond Myklebust4c8e5532018-10-01 10:41:53 -04001067 pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0], base + want);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001068
NeilBrown44524352006-10-04 02:15:46 -07001069 rqstp->rq_respages = &rqstp->rq_pages[pnum];
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001070 rqstp->rq_next_page = rqstp->rq_respages + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /* Now receive data */
Trond Myklebust4c8e5532018-10-01 10:41:53 -04001073 len = svc_recvfrom(rqstp, vec, pnum, base + want, base);
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001074 if (len >= 0) {
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001075 svsk->sk_tcplen += len;
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001076 svsk->sk_datalen += len;
1077 }
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001078 if (len != want || !svc_sock_final_rec(svsk)) {
J. Bruce Fieldsbe1e4442012-08-09 18:12:28 -04001079 svc_tcp_save_pages(svsk, rqstp);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001080 if (len < 0 && len != -EAGAIN)
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001081 goto err_delete;
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001082 if (len == want)
1083 svc_tcp_fragment_received(svsk);
1084 else
J. Bruce Fields3a28e332012-12-10 18:15:07 -05001085 dprintk("svc: incomplete TCP record (%d of %d)\n",
1086 (int)(svsk->sk_tcplen - sizeof(rpc_fraghdr)),
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001087 svc_sock_reclen(svsk));
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001088 goto err_noclose;
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
J. Bruce Fields1f691b02013-06-26 10:55:40 -04001091 if (svsk->sk_datalen < 8) {
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -04001092 svsk->sk_datalen = 0;
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001093 goto err_delete; /* client is nuts. */
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -04001094 }
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001095
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001096 rqstp->rq_arg.len = svsk->sk_datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 rqstp->rq_arg.page_base = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001098 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1099 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 rqstp->rq_arg.page_len = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001101 } else
1102 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Tom Tucker5148bf42007-12-30 21:07:25 -06001104 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 rqstp->rq_prot = IPPROTO_TCP;
Jeff Layton7501cc22014-11-19 07:51:15 -05001106 if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags))
1107 set_bit(RQ_LOCAL, &rqstp->rq_flags);
1108 else
1109 clear_bit(RQ_LOCAL, &rqstp->rq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Trond Myklebust586c52c2009-05-18 17:47:56 -04001111 p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1112 calldir = p[1];
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001113 if (calldir)
Trond Myklebust586c52c2009-05-18 17:47:56 -04001114 len = receive_cb_reply(svsk, rqstp);
Trond Myklebust586c52c2009-05-18 17:47:56 -04001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /* Reset TCP read info */
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001117 svsk->sk_datalen = 0;
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001118 svc_tcp_fragment_received(svsk);
Trond Myklebust0601f792009-05-18 17:47:56 -04001119
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001120 if (len < 0)
1121 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Tom Tucker9dbc2402007-12-30 21:08:12 -06001123 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (serv->sv_stats)
1125 serv->sv_stats->nettcpcnt++;
1126
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001127 return rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001129error:
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001130 if (len != -EAGAIN)
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001131 goto err_delete;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001132 dprintk("RPC: TCP recvfrom got EAGAIN\n");
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -04001133 return 0;
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001134err_delete:
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001135 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1136 svsk->sk_xprt.xpt_server->sv_name, -len);
1137 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1138err_noclose:
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -04001139 return 0; /* record not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
1142/*
1143 * Send out data on TCP socket.
1144 */
Tom Tucker0f0257e2007-12-30 21:08:27 -06001145static int svc_tcp_sendto(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 struct xdr_buf *xbufp = &rqstp->rq_res;
1148 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001149 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 /* Set up the first element of the reply kvec.
1152 * Any other kvecs that may be in use have been taken
1153 * care of by the server implementation itself.
1154 */
1155 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1156 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 sent = svc_sendto(rqstp, &rqstp->rq_res);
1159 if (sent != xbufp->len) {
Tom Tucker0f0257e2007-12-30 21:08:27 -06001160 printk(KERN_NOTICE
1161 "rpc-srv/tcp: %s: %s %d when sending %d bytes "
1162 "- shutting down socket\n",
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001163 rqstp->rq_xprt->xpt_server->sv_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 (sent<0)?"got error":"sent only",
1165 sent, xbufp->len);
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001166 set_bit(XPT_CLOSE, &rqstp->rq_xprt->xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -06001167 svc_xprt_enqueue(rqstp->rq_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 sent = -EAGAIN;
1169 }
1170 return sent;
1171}
1172
Tom Tuckere831fe62007-12-30 21:07:29 -06001173/*
1174 * Setup response header. TCP has a 4B record length field.
1175 */
1176static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
1177{
1178 struct kvec *resv = &rqstp->rq_res.head[0];
1179
1180 /* tcp needs a space for the record length... */
1181 svc_putnl(resv, 0);
1182}
1183
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001184static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001185 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001186 struct sockaddr *sa, int salen,
1187 int flags)
1188{
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001189 return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001190}
1191
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001192#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Andy Adamson1f11a032011-01-06 02:04:26 +00001193static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
1194 struct net *, struct sockaddr *,
1195 int, int);
1196static void svc_bc_sock_free(struct svc_xprt *xprt);
1197
1198static struct svc_xprt *svc_bc_tcp_create(struct svc_serv *serv,
1199 struct net *net,
1200 struct sockaddr *sa, int salen,
1201 int flags)
1202{
1203 return svc_bc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1204}
1205
1206static void svc_bc_tcp_sock_detach(struct svc_xprt *xprt)
1207{
1208}
1209
Chuck Lever2412e922017-08-01 11:59:49 -04001210static const struct svc_xprt_ops svc_tcp_bc_ops = {
Andy Adamson1f11a032011-01-06 02:04:26 +00001211 .xpo_create = svc_bc_tcp_create,
1212 .xpo_detach = svc_bc_tcp_sock_detach,
1213 .xpo_free = svc_bc_sock_free,
1214 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
Chuck Lever16e4d932014-05-19 13:40:22 -04001215 .xpo_secure_port = svc_sock_secure_port,
Andy Adamson1f11a032011-01-06 02:04:26 +00001216};
1217
1218static struct svc_xprt_class svc_tcp_bc_class = {
1219 .xcl_name = "tcp-bc",
1220 .xcl_owner = THIS_MODULE,
1221 .xcl_ops = &svc_tcp_bc_ops,
1222 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1223};
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001224
1225static void svc_init_bc_xprt_sock(void)
1226{
1227 svc_reg_xprt_class(&svc_tcp_bc_class);
1228}
1229
1230static void svc_cleanup_bc_xprt_sock(void)
1231{
1232 svc_unreg_xprt_class(&svc_tcp_bc_class);
1233}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001234#else /* CONFIG_SUNRPC_BACKCHANNEL */
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001235static void svc_init_bc_xprt_sock(void)
1236{
1237}
1238
1239static void svc_cleanup_bc_xprt_sock(void)
1240{
1241}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001242#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Andy Adamson1f11a032011-01-06 02:04:26 +00001243
Chuck Lever2412e922017-08-01 11:59:49 -04001244static const struct svc_xprt_ops svc_tcp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001245 .xpo_create = svc_tcp_create,
Tom Tucker5d137992007-12-30 21:07:23 -06001246 .xpo_recvfrom = svc_tcp_recvfrom,
1247 .xpo_sendto = svc_tcp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -06001248 .xpo_release_rqst = svc_release_skb,
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001249 .xpo_detach = svc_tcp_sock_detach,
Tom Tucker755ccea2007-12-30 21:07:27 -06001250 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -06001251 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -06001252 .xpo_has_wspace = svc_tcp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -06001253 .xpo_accept = svc_tcp_accept,
Chuck Lever16e4d932014-05-19 13:40:22 -04001254 .xpo_secure_port = svc_sock_secure_port,
Scott Mayhewea08e392016-11-11 13:16:22 -05001255 .xpo_kill_temp_xprt = svc_tcp_kill_temp_xprt,
Tom Tucker360d87382007-12-30 21:07:17 -06001256};
1257
1258static struct svc_xprt_class svc_tcp_class = {
1259 .xcl_name = "tcp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001260 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -06001261 .xcl_ops = &svc_tcp_ops,
Tom Tucker49023152007-12-30 21:07:21 -06001262 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
Chuck Lever3c45ddf2014-07-16 15:38:32 -04001263 .xcl_ident = XPRT_TRANSPORT_TCP,
Tom Tucker360d87382007-12-30 21:07:17 -06001264};
1265
1266void svc_init_xprt_sock(void)
1267{
1268 svc_reg_xprt_class(&svc_tcp_class);
1269 svc_reg_xprt_class(&svc_udp_class);
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001270 svc_init_bc_xprt_sock();
Tom Tucker360d87382007-12-30 21:07:17 -06001271}
1272
1273void svc_cleanup_xprt_sock(void)
1274{
1275 svc_unreg_xprt_class(&svc_tcp_class);
1276 svc_unreg_xprt_class(&svc_udp_class);
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001277 svc_cleanup_bc_xprt_sock();
Tom Tucker360d87382007-12-30 21:07:17 -06001278}
1279
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001280static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 struct sock *sk = svsk->sk_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +03001284 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
1285 &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -06001286 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Jeff Layton362142b22017-02-24 13:25:23 -05001287 set_bit(XPT_CONG_CTRL, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (sk->sk_state == TCP_LISTEN) {
1289 dprintk("setting up TCP socket for listening\n");
Chuck Leverece200d2018-03-27 10:51:00 -04001290 strcpy(svsk->sk_xprt.xpt_remotebuf, "listener");
Tom Tucker02fc6c32007-12-30 21:07:48 -06001291 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 sk->sk_data_ready = svc_tcp_listen_data_ready;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001293 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 } else {
1295 dprintk("setting up TCP socket for reading\n");
1296 sk->sk_state_change = svc_tcp_state_change;
Trond Myklebustfa9251a2016-06-24 10:55:48 -04001297 sk->sk_data_ready = svc_data_ready;
Trond Myklebust637600f2016-06-24 10:55:51 -04001298 sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 svsk->sk_reclen = 0;
1301 svsk->sk_tcplen = 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001302 svsk->sk_datalen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001303 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Chuck Leverb7872fe2008-04-14 12:27:01 -04001305 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Tom Tucker02fc6c32007-12-30 21:07:48 -06001307 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Trond Myklebustb2f21f72016-07-26 09:51:19 -04001308 switch (sk->sk_state) {
1309 case TCP_SYN_RECV:
1310 case TCP_ESTABLISHED:
1311 break;
1312 default:
Tom Tucker02fc6c32007-12-30 21:07:48 -06001313 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Trond Myklebustb2f21f72016-07-26 09:51:19 -04001314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316}
1317
Tom Tucker0f0257e2007-12-30 21:08:27 -06001318void svc_sock_update_bufs(struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 /*
1321 * The number of server threads has changed. Update
1322 * rcvbuf and sndbuf accordingly on all sockets
1323 */
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001324 struct svc_sock *svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 spin_lock_bh(&serv->sv_lock);
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001327 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001328 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 spin_unlock_bh(&serv->sv_lock);
1330}
Trond Myklebust24c37672008-12-23 16:30:12 -05001331EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 * Initialize socket for RPC use and create svc_sock struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 */
Chuck Lever6b174332007-02-12 00:53:28 -08001336static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1337 struct socket *sock,
J. Bruce Fields72c35372012-08-13 17:46:17 -04001338 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct svc_sock *svsk;
1341 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001342 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001343 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 dprintk("svc: svc_setup_socket %p\n", sock);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001346 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1347 if (!svsk)
1348 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 inet = sock->sk;
1351
1352 /* Register socket with portmapper */
J. Bruce Fields72c35372012-08-13 17:46:17 -04001353 if (pmap_register)
1354 err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001355 inet->sk_protocol,
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001356 ntohs(inet_sk(inet)->inet_sport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
J. Bruce Fields72c35372012-08-13 17:46:17 -04001358 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 kfree(svsk);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001360 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 svsk->sk_sock = sock;
1364 svsk->sk_sk = inet;
1365 svsk->sk_ostate = inet->sk_state_change;
1366 svsk->sk_odata = inet->sk_data_ready;
1367 svsk->sk_owspace = inet->sk_write_space;
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -04001368 /*
1369 * This barrier is necessary in order to prevent race condition
1370 * with svc_data_ready(), svc_listen_data_ready() and others
1371 * when calling callbacks above.
1372 */
1373 wmb();
1374 inet->sk_user_data = svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 /* Initialize the socket */
1377 if (sock->type == SOCK_DGRAM)
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001378 svc_udp_init(svsk, serv);
Trond Myklebust637600f2016-06-24 10:55:51 -04001379 else
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001380 svc_tcp_init(svsk, serv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Trond Myklebustc7995f82016-07-26 09:51:20 -04001382 dprintk("svc: svc_setup_socket created %p (inet %p), "
1383 "listen %d close %d\n",
1384 svsk, svsk->sk_sk,
1385 test_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags),
1386 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return svsk;
1389}
1390
Stanislav Kinsbursky30646392014-02-26 16:50:01 +03001391bool svc_alien_sock(struct net *net, int fd)
1392{
1393 int err;
1394 struct socket *sock = sockfd_lookup(fd, &err);
1395 bool ret = false;
1396
1397 if (!sock)
1398 goto out;
1399 if (sock_net(sock->sk) != net)
1400 ret = true;
1401 sockfd_put(sock);
1402out:
1403 return ret;
1404}
1405EXPORT_SYMBOL_GPL(svc_alien_sock);
1406
Chuck Leverbfba9ab2009-04-23 19:32:33 -04001407/**
1408 * svc_addsock - add a listener socket to an RPC service
1409 * @serv: pointer to RPC service to which to add a new listener
1410 * @fd: file descriptor of the new listener
1411 * @name_return: pointer to buffer to fill in with name of listener
1412 * @len: size of the buffer
1413 *
1414 * Fills in socket name and returns positive length of name if successful.
1415 * Name is terminated with '\n'. On error, returns a negative errno
1416 * value.
1417 */
1418int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
1419 const size_t len)
NeilBrownb41b66d2006-10-02 02:17:48 -07001420{
1421 int err = 0;
1422 struct socket *so = sockfd_lookup(fd, &err);
1423 struct svc_sock *svsk = NULL;
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001424 struct sockaddr_storage addr;
1425 struct sockaddr *sin = (struct sockaddr *)&addr;
1426 int salen;
NeilBrownb41b66d2006-10-02 02:17:48 -07001427
1428 if (!so)
1429 return err;
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001430 err = -EAFNOSUPPORT;
Aime Le Rouzic205ba422010-01-26 14:03:56 -05001431 if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001432 goto out;
1433 err = -EPROTONOSUPPORT;
1434 if (so->sk->sk_protocol != IPPROTO_TCP &&
NeilBrownb41b66d2006-10-02 02:17:48 -07001435 so->sk->sk_protocol != IPPROTO_UDP)
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001436 goto out;
1437 err = -EISCONN;
1438 if (so->state > SS_UNCONNECTED)
1439 goto out;
1440 err = -ENOENT;
1441 if (!try_module_get(THIS_MODULE))
1442 goto out;
1443 svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1444 if (IS_ERR(svsk)) {
1445 module_put(THIS_MODULE);
1446 err = PTR_ERR(svsk);
1447 goto out;
NeilBrownb41b66d2006-10-02 02:17:48 -07001448 }
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001449 salen = kernel_getsockname(svsk->sk_sock, sin);
1450 if (salen >= 0)
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001451 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
J. Bruce Fields39b55302012-08-14 15:50:34 -04001452 svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
Chuck Levere7942b92009-04-23 19:32:48 -04001453 return svc_one_sock_name(svsk, name_return, len);
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001454out:
1455 sockfd_put(so);
1456 return err;
NeilBrownb41b66d2006-10-02 02:17:48 -07001457}
1458EXPORT_SYMBOL_GPL(svc_addsock);
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460/*
1461 * Create socket for RPC service.
1462 */
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001463static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1464 int protocol,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001465 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001466 struct sockaddr *sin, int len,
1467 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 struct svc_sock *svsk;
1470 struct socket *sock;
1471 int error;
1472 int type;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001473 struct sockaddr_storage addr;
1474 struct sockaddr *newsin = (struct sockaddr *)&addr;
1475 int newlen;
Trond Myklebustc69da772009-03-30 18:59:17 -04001476 int family;
1477 int val;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +03001478 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Chuck Leverad06e4b2007-02-12 00:53:32 -08001480 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1481 serv->sv_program->pg_name, protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001482 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1485 printk(KERN_WARNING "svc: only UDP and TCP "
1486 "sockets supported\n");
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001487 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Trond Myklebustc69da772009-03-30 18:59:17 -04001490 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1491 switch (sin->sa_family) {
1492 case AF_INET6:
1493 family = PF_INET6;
1494 break;
1495 case AF_INET:
1496 family = PF_INET;
1497 break;
1498 default:
1499 return ERR_PTR(-EINVAL);
1500 }
1501
Pavel Emelyanov14ec63c2010-09-29 16:06:57 +04001502 error = __sock_create(net, family, type, protocol, &sock, 1);
Chuck Lever77f1f672007-02-12 00:53:39 -08001503 if (error < 0)
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001504 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Peter Zijlstraed075362006-12-06 20:35:24 -08001506 svc_reclassify_socket(sock);
1507
Trond Myklebustc69da772009-03-30 18:59:17 -04001508 /*
1509 * If this is an PF_INET6 listener, we want to avoid
1510 * getting requests from IPv4 remotes. Those should
1511 * be shunted to a PF_INET listener via rpcbind.
1512 */
1513 val = 1;
1514 if (family == PF_INET6)
1515 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
1516 (char *)&val, sizeof(val));
1517
Eric Sesterhenn18114742006-09-28 14:37:07 -07001518 if (type == SOCK_STREAM)
Pavel Emelyanov4a17fd52012-04-19 03:39:36 +00001519 sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
Chuck Lever77f1f672007-02-12 00:53:39 -08001520 error = kernel_bind(sock, sin, len);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001521 if (error < 0)
1522 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001524 error = kernel_getsockname(sock, newsin);
Tom Tucker9dbc2402007-12-30 21:08:12 -06001525 if (error < 0)
1526 goto bummer;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001527 newlen = error;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001530 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 goto bummer;
1532 }
1533
J. Bruce Fields72c35372012-08-13 17:46:17 -04001534 svsk = svc_setup_socket(serv, sock, flags);
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001535 if (IS_ERR(svsk)) {
1536 error = PTR_ERR(svsk);
1537 goto bummer;
NeilBrowne79eff12007-02-12 00:53:30 -08001538 }
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001539 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1540 return (struct svc_xprt *)svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541bummer:
1542 dprintk("svc: svc_create_socket error = %d\n", -error);
1543 sock_release(sock);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001544 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
1547/*
Tom Tucker755ccea2007-12-30 21:07:27 -06001548 * Detach the svc_sock from the socket so that no
1549 * more callbacks occur.
1550 */
1551static void svc_sock_detach(struct svc_xprt *xprt)
1552{
1553 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1554 struct sock *sk = svsk->sk_sk;
1555
1556 dprintk("svc: svc_sock_detach(%p)\n", svsk);
1557
1558 /* put back the old socket callbacks */
Trond Myklebust069c2252016-06-24 10:55:47 -04001559 lock_sock(sk);
Tom Tucker755ccea2007-12-30 21:07:27 -06001560 sk->sk_state_change = svsk->sk_ostate;
1561 sk->sk_data_ready = svsk->sk_odata;
1562 sk->sk_write_space = svsk->sk_owspace;
Trond Myklebust069c2252016-06-24 10:55:47 -04001563 sk->sk_user_data = NULL;
1564 release_sock(sk);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001565}
1566
1567/*
1568 * Disconnect the socket, and reset the callbacks
1569 */
1570static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1571{
1572 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1573
1574 dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk);
1575
1576 svc_sock_detach(xprt);
1577
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001578 if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1579 svc_tcp_clear_pages(svsk);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001580 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001581 }
Tom Tucker755ccea2007-12-30 21:07:27 -06001582}
1583
1584/*
1585 * Free the svc_sock's socket resources and the svc_sock itself.
1586 */
1587static void svc_sock_free(struct svc_xprt *xprt)
1588{
1589 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1590 dprintk("svc: svc_sock_free(%p)\n", svsk);
1591
Tom Tucker755ccea2007-12-30 21:07:27 -06001592 if (svsk->sk_sock->file)
1593 sockfd_put(svsk->sk_sock);
1594 else
1595 sock_release(svsk->sk_sock);
1596 kfree(svsk);
1597}
Benny Halevy7652e5a2009-04-01 09:23:09 -04001598
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001599#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001600/*
Andy Adamson1f11a032011-01-06 02:04:26 +00001601 * Create a back channel svc_xprt which shares the fore channel socket.
Benny Halevy7652e5a2009-04-01 09:23:09 -04001602 */
Andy Adamson1f11a032011-01-06 02:04:26 +00001603static struct svc_xprt *svc_bc_create_socket(struct svc_serv *serv,
1604 int protocol,
1605 struct net *net,
1606 struct sockaddr *sin, int len,
1607 int flags)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001608{
1609 struct svc_sock *svsk;
Andy Adamson1f11a032011-01-06 02:04:26 +00001610 struct svc_xprt *xprt;
Benny Halevy7652e5a2009-04-01 09:23:09 -04001611
Andy Adamson1f11a032011-01-06 02:04:26 +00001612 if (protocol != IPPROTO_TCP) {
1613 printk(KERN_WARNING "svc: only TCP sockets"
1614 " supported on shared back channel\n");
1615 return ERR_PTR(-EINVAL);
1616 }
1617
Benny Halevy7652e5a2009-04-01 09:23:09 -04001618 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1619 if (!svsk)
Andy Adamson1f11a032011-01-06 02:04:26 +00001620 return ERR_PTR(-ENOMEM);
Benny Halevy7652e5a2009-04-01 09:23:09 -04001621
1622 xprt = &svsk->sk_xprt;
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +03001623 svc_xprt_init(net, &svc_tcp_bc_class, xprt, serv);
Kinglong Mee54272902017-03-07 17:22:43 +08001624 set_bit(XPT_CONG_CTRL, &svsk->sk_xprt.xpt_flags);
Andy Adamson1f11a032011-01-06 02:04:26 +00001625
Andy Adamson4a19de02011-01-06 02:04:35 +00001626 serv->sv_bc_xprt = xprt;
Andy Adamson1f11a032011-01-06 02:04:26 +00001627
Benny Halevy7652e5a2009-04-01 09:23:09 -04001628 return xprt;
1629}
Benny Halevy7652e5a2009-04-01 09:23:09 -04001630
1631/*
Andy Adamson1f11a032011-01-06 02:04:26 +00001632 * Free a back channel svc_sock.
Benny Halevy7652e5a2009-04-01 09:23:09 -04001633 */
Andy Adamson1f11a032011-01-06 02:04:26 +00001634static void svc_bc_sock_free(struct svc_xprt *xprt)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001635{
Andy Adamson778be232011-01-25 15:38:01 +00001636 if (xprt)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001637 kfree(container_of(xprt, struct svc_sock, sk_xprt));
1638}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001639#endif /* CONFIG_SUNRPC_BACKCHANNEL */