blob: 57625f64efd56edaa65fe0940aaa7979fe65ef87 [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>
Chuck Leverb7872fe2008-04-14 12:27:01 -040042#include <net/tcp.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070043#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45#include <asm/ioctls.h>
Eric Dumazet22911fc2012-06-27 00:23:44 +000046#include <trace/events/skb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <linux/sunrpc/types.h>
Chuck Leverad06e4b2007-02-12 00:53:32 -080049#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/sunrpc/xdr.h>
Chuck Leverc0401ea2008-04-14 12:27:30 -040051#include <linux/sunrpc/msg_prot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/sunrpc/svcsock.h>
53#include <linux/sunrpc/stats.h>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030054#include <linux/sunrpc/xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
H Hartley Sweeten177e4f92011-06-20 17:54:51 -070056#include "sunrpc.h"
57
Tom Tucker360d87382007-12-30 21:07:17 -060058#define RPCDBG_FACILITY RPCDBG_SVCXPRT
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60
61static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
J. Bruce Fields72c35372012-08-13 17:46:17 -040062 int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int svc_udp_recvfrom(struct svc_rqst *);
64static int svc_udp_sendto(struct svc_rqst *);
Tom Tucker755ccea2007-12-30 21:07:27 -060065static void svc_sock_detach(struct svc_xprt *);
Trond Myklebust69b6ba32008-12-23 16:30:11 -050066static void svc_tcp_sock_detach(struct svc_xprt *);
Tom Tucker755ccea2007-12-30 21:07:27 -060067static void svc_sock_free(struct svc_xprt *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Tom Tuckerb700cbb2007-12-30 21:07:42 -060069static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
Pavel Emelyanov62832c02010-09-29 16:04:18 +040070 struct net *, struct sockaddr *,
71 int, int);
Trond Myklebust9e00abc2011-07-13 19:20:49 -040072#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Andy Adamson1f11a032011-01-06 02:04:26 +000073static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
74 struct net *, struct sockaddr *,
75 int, int);
76static void svc_bc_sock_free(struct svc_xprt *xprt);
Trond Myklebust9e00abc2011-07-13 19:20:49 -040077#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Andy Adamson1f11a032011-01-06 02:04:26 +000078
Peter Zijlstraed075362006-12-06 20:35:24 -080079#ifdef CONFIG_DEBUG_LOCK_ALLOC
80static struct lock_class_key svc_key[2];
81static struct lock_class_key svc_slock_key[2];
82
Tom Tucker0f0257e2007-12-30 21:08:27 -060083static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -080084{
85 struct sock *sk = sock->sk;
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -040086
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +020087 if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -040088 return;
89
Peter Zijlstraed075362006-12-06 20:35:24 -080090 switch (sk->sk_family) {
91 case AF_INET:
92 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -060093 &svc_slock_key[0],
94 "sk_xprt.xpt_lock-AF_INET-NFSD",
95 &svc_key[0]);
Peter Zijlstraed075362006-12-06 20:35:24 -080096 break;
97
98 case AF_INET6:
99 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -0600100 &svc_slock_key[1],
101 "sk_xprt.xpt_lock-AF_INET6-NFSD",
102 &svc_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -0800103 break;
104
105 default:
106 BUG();
107 }
108}
109#else
Tom Tucker0f0257e2007-12-30 21:08:27 -0600110static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -0800111{
112}
113#endif
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/*
116 * Release an skbuff after use
117 */
Tom Tucker5148bf42007-12-30 21:07:25 -0600118static void svc_release_skb(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Tom Tucker5148bf42007-12-30 21:07:25 -0600120 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (skb) {
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600123 struct svc_sock *svsk =
124 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tucker5148bf42007-12-30 21:07:25 -0600125 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
Eric Dumazet9d410c72009-10-30 05:03:53 +0000128 skb_free_datagram_locked(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Chuck Leverb92503b2007-02-12 00:53:36 -0800132union svc_pktinfo_u {
133 struct in_pktinfo pkti;
Chuck Leverb92503b2007-02-12 00:53:36 -0800134 struct in6_pktinfo pkti6;
Chuck Leverb92503b2007-02-12 00:53:36 -0800135};
David S. Millerbc375ea2007-04-12 13:35:59 -0700136#define SVC_PKTINFO_SPACE \
137 CMSG_SPACE(sizeof(union svc_pktinfo_u))
Chuck Leverb92503b2007-02-12 00:53:36 -0800138
139static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
140{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600141 struct svc_sock *svsk =
142 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
143 switch (svsk->sk_sk->sk_family) {
Chuck Leverb92503b2007-02-12 00:53:36 -0800144 case AF_INET: {
145 struct in_pktinfo *pki = CMSG_DATA(cmh);
146
147 cmh->cmsg_level = SOL_IP;
148 cmh->cmsg_type = IP_PKTINFO;
149 pki->ipi_ifindex = 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800150 pki->ipi_spec_dst.s_addr =
151 svc_daddr_in(rqstp)->sin_addr.s_addr;
Chuck Leverb92503b2007-02-12 00:53:36 -0800152 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
153 }
154 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800155
Chuck Leverb92503b2007-02-12 00:53:36 -0800156 case AF_INET6: {
157 struct in6_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800158 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
Chuck Leverb92503b2007-02-12 00:53:36 -0800159
160 cmh->cmsg_level = SOL_IPV6;
161 cmh->cmsg_type = IPV6_PKTINFO;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800162 pki->ipi6_ifindex = daddr->sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000163 pki->ipi6_addr = daddr->sin6_addr;
Chuck Leverb92503b2007-02-12 00:53:36 -0800164 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
165 }
166 break;
Chuck Leverb92503b2007-02-12 00:53:36 -0800167 }
Chuck Leverb92503b2007-02-12 00:53:36 -0800168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300171 * send routine intended to be shared by the fore- and back-channel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300173int svc_send_common(struct socket *sock, struct xdr_buf *xdr,
174 struct page *headpage, unsigned long headoffset,
175 struct page *tailpage, unsigned long tailoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 int result;
178 int size;
179 struct page **ppage = xdr->pages;
180 size_t base = xdr->page_base;
181 unsigned int pglen = xdr->page_len;
Trond Myklebust226453d2015-10-07 13:54:42 -0400182 unsigned int flags = MSG_MORE | MSG_SENDPAGE_NOTLAST;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300183 int slen;
184 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 slen = xdr->len;
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /* send head */
189 if (slen == xdr->head[0].iov_len)
190 flags = 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300191 len = kernel_sendpage(sock, headpage, headoffset,
NeilBrown44524352006-10-04 02:15:46 -0700192 xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (len != xdr->head[0].iov_len)
194 goto out;
195 slen -= xdr->head[0].iov_len;
196 if (slen == 0)
197 goto out;
198
199 /* send page data */
200 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
201 while (pglen > 0) {
202 if (slen == size)
203 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700204 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (result > 0)
206 len += result;
207 if (result != size)
208 goto out;
209 slen -= size;
210 pglen -= size;
211 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
212 base = 0;
213 ppage++;
214 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* send tail */
217 if (xdr->tail[0].iov_len) {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300218 result = kernel_sendpage(sock, tailpage, tailoffset,
219 xdr->tail[0].iov_len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (result > 0)
221 len += result;
222 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300223
224out:
225 return len;
226}
227
228
229/*
230 * Generic sendto routine
231 */
232static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
233{
234 struct svc_sock *svsk =
235 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
236 struct socket *sock = svsk->sk_sock;
237 union {
238 struct cmsghdr hdr;
239 long all[SVC_PKTINFO_SPACE / sizeof(long)];
240 } buffer;
241 struct cmsghdr *cmh = &buffer.hdr;
242 int len = 0;
243 unsigned long tailoff;
244 unsigned long headoff;
245 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
246
247 if (rqstp->rq_prot == IPPROTO_UDP) {
248 struct msghdr msg = {
249 .msg_name = &rqstp->rq_addr,
250 .msg_namelen = rqstp->rq_addrlen,
251 .msg_control = cmh,
252 .msg_controllen = sizeof(buffer),
253 .msg_flags = MSG_MORE,
254 };
255
256 svc_set_cmsg_data(rqstp, cmh);
257
Al Virod8725c82014-12-11 00:02:50 -0500258 if (sock_sendmsg(sock, &msg) < 0)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300259 goto out;
260 }
261
262 tailoff = ((unsigned long)xdr->tail[0].iov_base) & (PAGE_SIZE-1);
263 headoff = 0;
264 len = svc_send_common(sock, xdr, rqstp->rq_respages[0], headoff,
265 rqstp->rq_respages[0], tailoff);
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267out:
Chuck Leverad06e4b2007-02-12 00:53:32 -0800268 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600269 svsk, xdr->head[0].iov_base, xdr->head[0].iov_len,
Chuck Leverad06e4b2007-02-12 00:53:32 -0800270 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 return len;
273}
274
275/*
NeilBrown80212d52006-10-02 02:17:47 -0700276 * Report socket names for nfsdfs
277 */
Chuck Levere7942b92009-04-23 19:32:48 -0400278static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
NeilBrown80212d52006-10-02 02:17:47 -0700279{
Chuck Lever017cb472009-04-23 19:33:03 -0400280 const struct sock *sk = svsk->sk_sk;
281 const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
282 "udp" : "tcp";
NeilBrown80212d52006-10-02 02:17:47 -0700283 int len;
284
Chuck Lever017cb472009-04-23 19:33:03 -0400285 switch (sk->sk_family) {
Chuck Levere7942b92009-04-23 19:32:48 -0400286 case PF_INET:
287 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400288 proto_name,
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000289 &inet_sk(sk)->inet_rcv_saddr,
290 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700291 break;
Eric Dumazetc2bb06d2013-10-09 03:05:48 -0700292#if IS_ENABLED(CONFIG_IPV6)
Chuck Lever58de2f82009-04-23 19:32:55 -0400293 case PF_INET6:
294 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400295 proto_name,
Eric Dumazetefe42082013-10-03 15:42:29 -0700296 &sk->sk_v6_rcv_saddr,
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000297 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700298 break;
Eric Dumazetc2bb06d2013-10-09 03:05:48 -0700299#endif
NeilBrown80212d52006-10-02 02:17:47 -0700300 default:
Chuck Levere7942b92009-04-23 19:32:48 -0400301 len = snprintf(buf, remaining, "*unknown-%d*\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400302 sk->sk_family);
NeilBrown80212d52006-10-02 02:17:47 -0700303 }
Chuck Levere7942b92009-04-23 19:32:48 -0400304
305 if (len >= remaining) {
306 *buf = '\0';
307 return -ENAMETOOLONG;
NeilBrown80212d52006-10-02 02:17:47 -0700308 }
309 return len;
310}
311
NeilBrown80212d52006-10-02 02:17:47 -0700312/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * Generic recvfrom routine.
314 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600315static int svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr,
316 int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600318 struct svc_sock *svsk =
319 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Chuck Lever1ba95102007-02-12 00:53:31 -0800320 struct msghdr msg = {
321 .msg_flags = MSG_DONTWAIT,
322 };
323 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Tom Tucker260c1d12007-12-30 21:08:29 -0600325 rqstp->rq_xprt_hlen = 0;
326
Trond Myklebustf8d1ff42014-08-03 13:03:13 -0400327 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Chuck Lever1ba95102007-02-12 00:53:31 -0800328 len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
329 msg.msg_flags);
Trond Myklebustf8d1ff42014-08-03 13:03:13 -0400330 /* If we read a full record, then assume there may be more
331 * data to read (stream based sockets only!)
332 */
333 if (len == buflen)
334 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
Chuck Lever1ba95102007-02-12 00:53:31 -0800337 svsk, iov[0].iov_base, iov[0].iov_len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return len;
339}
340
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800341static int svc_partial_recvfrom(struct svc_rqst *rqstp,
342 struct kvec *iov, int nr,
343 int buflen, unsigned int base)
344{
345 size_t save_iovlen;
Trond Myklebust09acfea2012-03-11 15:22:54 -0400346 void *save_iovbase;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800347 unsigned int i;
348 int ret;
349
350 if (base == 0)
351 return svc_recvfrom(rqstp, iov, nr, buflen);
352
353 for (i = 0; i < nr; i++) {
354 if (iov[i].iov_len > base)
355 break;
356 base -= iov[i].iov_len;
357 }
358 save_iovlen = iov[i].iov_len;
359 save_iovbase = iov[i].iov_base;
360 iov[i].iov_len -= base;
361 iov[i].iov_base += base;
362 ret = svc_recvfrom(rqstp, &iov[i], nr - i, buflen);
363 iov[i].iov_len = save_iovlen;
364 iov[i].iov_base = save_iovbase;
365 return ret;
366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/*
369 * Set socket snd and rcv buffer lengths
370 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600371static void svc_sock_setbufsize(struct socket *sock, unsigned int snd,
372 unsigned int rcv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374#if 0
375 mm_segment_t oldfs;
376 oldfs = get_fs(); set_fs(KERNEL_DS);
377 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
378 (char*)&snd, sizeof(snd));
379 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
380 (char*)&rcv, sizeof(rcv));
381#else
382 /* sock_setsockopt limits use to sysctl_?mem_max,
383 * which isn't acceptable. Until that is made conditional
384 * on not having CAP_SYS_RESOURCE or similar, we go direct...
385 * DaveM said I could!
386 */
387 lock_sock(sock->sk);
388 sock->sk->sk_sndbuf = snd * 2;
389 sock->sk->sk_rcvbuf = rcv * 2;
Trond Myklebust47fcb032009-05-18 17:47:56 -0400390 sock->sk->sk_write_space(sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 release_sock(sock->sk);
392#endif
393}
Chuck Lever16e4d932014-05-19 13:40:22 -0400394
395static int svc_sock_secure_port(struct svc_rqst *rqstp)
396{
397 return svc_port_is_privileged(svc_addr(rqstp));
398}
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/*
401 * INET callback when data has been received on the socket.
402 */
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400403static void svc_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Neil Brown939bb7e2005-09-13 01:25:39 -0700405 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Neil Brown939bb7e2005-09-13 01:25:39 -0700407 if (svsk) {
David S. Miller676d2362014-04-11 16:15:36 -0400408 dprintk("svc: socket %p(inet %p), busy=%d\n",
409 svsk, sk,
Tom Tucker02fc6c32007-12-30 21:07:48 -0600410 test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400411 svsk->sk_odata(sk);
Trond Myklebust4720b072016-06-24 10:55:49 -0400412 if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags))
413 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417/*
418 * INET callback when space is newly available on the socket.
419 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600420static void svc_write_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
423
424 if (svsk) {
425 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600426 svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400427 svsk->sk_owspace(sk);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600428 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Trond Myklebustc7fb3f02014-07-24 23:59:32 -0400432static int svc_tcp_has_wspace(struct svc_xprt *xprt)
433{
Trond Myklebust637600f2016-06-24 10:55:51 -0400434 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Trond Myklebustc7fb3f02014-07-24 23:59:32 -0400435
436 if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
437 return 1;
Trond Myklebust637600f2016-06-24 10:55:51 -0400438 return !test_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Trond Myklebust51877682014-07-24 23:59:33 -0400439}
440
Tom Tucker9dbc2402007-12-30 21:08:12 -0600441/*
Chuck Lever7702ce42009-07-13 10:54:26 -0400442 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
443 */
444static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
445 struct cmsghdr *cmh)
446{
447 struct in_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800448 struct sockaddr_in *daddr = svc_daddr_in(rqstp);
449
Chuck Lever7702ce42009-07-13 10:54:26 -0400450 if (cmh->cmsg_type != IP_PKTINFO)
451 return 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800452
453 daddr->sin_family = AF_INET;
454 daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
Chuck Lever7702ce42009-07-13 10:54:26 -0400455 return 1;
456}
457
458/*
Tom Parkin73df66f2013-01-31 01:02:24 +0000459 * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
Chuck Lever7702ce42009-07-13 10:54:26 -0400460 */
461static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
462 struct cmsghdr *cmh)
463{
464 struct in6_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800465 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
466
Chuck Lever7702ce42009-07-13 10:54:26 -0400467 if (cmh->cmsg_type != IPV6_PKTINFO)
468 return 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800469
470 daddr->sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000471 daddr->sin6_addr = pki->ipi6_addr;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800472 daddr->sin6_scope_id = pki->ipi6_ifindex;
Chuck Lever7702ce42009-07-13 10:54:26 -0400473 return 1;
474}
475
476/*
Tom Tucker9dbc2402007-12-30 21:08:12 -0600477 * Copy the UDP datagram's destination address to the rqstp structure.
478 * The 'destination' address in this case is the address to which the
479 * peer sent the datagram, i.e. our local address. For multihomed
480 * hosts, this can change from msg to msg. Note that only the IP
481 * address changes, the port number should remain the same.
482 */
Chuck Lever7702ce42009-07-13 10:54:26 -0400483static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
484 struct cmsghdr *cmh)
Chuck Lever95756482007-02-12 00:53:38 -0800485{
Chuck Lever7702ce42009-07-13 10:54:26 -0400486 switch (cmh->cmsg_level) {
487 case SOL_IP:
488 return svc_udp_get_dest_address4(rqstp, cmh);
489 case SOL_IPV6:
490 return svc_udp_get_dest_address6(rqstp, cmh);
Chuck Lever95756482007-02-12 00:53:38 -0800491 }
Chuck Lever7702ce42009-07-13 10:54:26 -0400492
493 return 0;
Chuck Lever95756482007-02-12 00:53:38 -0800494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/*
497 * Receive a datagram from a UDP socket.
498 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600499static int svc_udp_recvfrom(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600501 struct svc_sock *svsk =
502 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600503 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct sk_buff *skb;
David S. Millerbc375ea2007-04-12 13:35:59 -0700505 union {
506 struct cmsghdr hdr;
507 long all[SVC_PKTINFO_SPACE / sizeof(long)];
508 } buffer;
509 struct cmsghdr *cmh = &buffer.hdr;
NeilBrown7a37f572007-03-06 01:42:21 -0800510 struct msghdr msg = {
511 .msg_name = svc_addr(rqstp),
512 .msg_control = cmh,
513 .msg_controllen = sizeof(buffer),
514 .msg_flags = MSG_DONTWAIT,
515 };
Chuck Leverabc5c442009-04-23 19:31:25 -0400516 size_t len;
517 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Tom Tucker02fc6c32007-12-30 21:07:48 -0600519 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* udp sockets need large rcvbuf as all pending
521 * requests are still in that buffer. sndbuf must
522 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700523 * for one reply per thread. We count all threads
524 * rather than threads in a particular pool, which
525 * provides an upper bound on the number of threads
526 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
528 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700529 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
530 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Tom Tucker02fc6c32007-12-30 21:07:48 -0600532 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
NeilBrown05ed6902007-05-09 02:34:55 -0700533 skb = NULL;
534 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
535 0, 0, MSG_PEEK | MSG_DONTWAIT);
536 if (err >= 0)
537 skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err);
538
539 if (skb == NULL) {
540 if (err != -EAGAIN) {
541 /* possibly an icmp error */
542 dprintk("svc: recvfrom returned error %d\n", -err);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600543 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -0400545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600547 len = svc_addr_len(svc_addr(rqstp));
Tom Tucker9dbc2402007-12-30 21:08:12 -0600548 rqstp->rq_addrlen = len;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700549 if (skb->tstamp.tv64 == 0) {
550 skb->tstamp = ktime_get_real();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800551 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 need that much accuracy */
553 }
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700554 svsk->sk_sk->sk_stamp = skb->tstamp;
Tom Tucker02fc6c32007-12-30 21:07:48 -0600555 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Willem de Bruijn1da8c6812016-04-07 11:44:58 -0400557 len = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 rqstp->rq_arg.len = len;
559
Chuck Lever95756482007-02-12 00:53:38 -0800560 rqstp->rq_prot = IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Chuck Lever7702ce42009-07-13 10:54:26 -0400562 if (!svc_udp_get_dest_address(rqstp, cmh)) {
Joe Perchese87cc472012-05-13 21:56:26 +0000563 net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
564 cmh->cmsg_level, cmh->cmsg_type);
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400565 goto out_free;
NeilBrown7a37f572007-03-06 01:42:21 -0800566 }
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800567 rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 if (skb_is_nonlinear(skb)) {
570 /* we have to copy */
571 local_bh_disable();
572 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
573 local_bh_enable();
574 /* checksum error */
Eric Dumazet22911fc2012-06-27 00:23:44 +0000575 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577 local_bh_enable();
Eric Dumazet9d410c72009-10-30 05:03:53 +0000578 skb_free_datagram_locked(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 } else {
580 /* we can use it in-place */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -0400581 rqstp->rq_arg.head[0].iov_base = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 rqstp->rq_arg.head[0].iov_len = len;
Eric Dumazet22911fc2012-06-27 00:23:44 +0000583 if (skb_checksum_complete(skb))
584 goto out_free;
Tom Tucker5148bf42007-12-30 21:07:25 -0600585 rqstp->rq_xprt_ctxt = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
588 rqstp->rq_arg.page_base = 0;
589 if (len <= rqstp->rq_arg.head[0].iov_len) {
590 rqstp->rq_arg.head[0].iov_len = len;
591 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700592 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 } else {
594 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700595 rqstp->rq_respages = rqstp->rq_pages + 1 +
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700596 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500598 rqstp->rq_next_page = rqstp->rq_respages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 if (serv->sv_stats)
601 serv->sv_stats->netudpcnt++;
602
603 return len;
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400604out_free:
605 trace_kfree_skb(skb, svc_udp_recvfrom);
606 skb_free_datagram_locked(svsk->sk_sk, skb);
607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610static int
611svc_udp_sendto(struct svc_rqst *rqstp)
612{
613 int error;
614
615 error = svc_sendto(rqstp, &rqstp->rq_res);
616 if (error == -ECONNREFUSED)
617 /* ICMP error on earlier request. */
618 error = svc_sendto(rqstp, &rqstp->rq_res);
619
620 return error;
621}
622
Tom Tuckere831fe62007-12-30 21:07:29 -0600623static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
624{
625}
626
Tom Tucker323bee32007-12-30 21:07:31 -0600627static int svc_udp_has_wspace(struct svc_xprt *xprt)
628{
629 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600630 struct svc_serv *serv = xprt->xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -0600631 unsigned long required;
632
633 /*
634 * Set the SOCK_NOSPACE flag before checking the available
635 * sock space.
636 */
637 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Tom Tucker7a90e8c2007-12-30 21:07:55 -0600638 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
Tom Tucker323bee32007-12-30 21:07:31 -0600639 if (required*2 > sock_wspace(svsk->sk_sk))
640 return 0;
641 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
642 return 1;
643}
644
Tom Tucker38a417c2007-12-30 21:07:36 -0600645static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
646{
647 BUG();
648 return NULL;
649}
650
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600651static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400652 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600653 struct sockaddr *sa, int salen,
654 int flags)
655{
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400656 return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600657}
658
Tom Tucker360d87382007-12-30 21:07:17 -0600659static struct svc_xprt_ops svc_udp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600660 .xpo_create = svc_udp_create,
Tom Tucker5d137992007-12-30 21:07:23 -0600661 .xpo_recvfrom = svc_udp_recvfrom,
662 .xpo_sendto = svc_udp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -0600663 .xpo_release_rqst = svc_release_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -0600664 .xpo_detach = svc_sock_detach,
665 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -0600666 .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -0600667 .xpo_has_wspace = svc_udp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -0600668 .xpo_accept = svc_udp_accept,
Chuck Lever16e4d932014-05-19 13:40:22 -0400669 .xpo_secure_port = svc_sock_secure_port,
Tom Tucker360d87382007-12-30 21:07:17 -0600670};
671
672static struct svc_xprt_class svc_udp_class = {
673 .xcl_name = "udp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600674 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -0600675 .xcl_ops = &svc_udp_ops,
Tom Tucker49023152007-12-30 21:07:21 -0600676 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
Chuck Lever3c45ddf2014-07-16 15:38:32 -0400677 .xcl_ident = XPRT_TRANSPORT_UDP,
Tom Tucker360d87382007-12-30 21:07:17 -0600678};
679
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600680static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Chuck Lever7702ce42009-07-13 10:54:26 -0400682 int err, level, optname, one = 1;
NeilBrown7a37f572007-03-06 01:42:21 -0800683
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +0300684 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
685 &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600686 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400687 svsk->sk_sk->sk_data_ready = svc_data_ready;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 svsk->sk_sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800691 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * svc_udp_recvfrom will re-adjust if necessary
693 */
694 svc_sock_setbufsize(svsk->sk_sock,
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600695 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
696 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Tom Tucker0f0257e2007-12-30 21:08:27 -0600698 /* data might have come in before data_ready set up */
699 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600700 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800701
NeilBrown7a37f572007-03-06 01:42:21 -0800702 /* make sure we get destination address info */
Chuck Lever7702ce42009-07-13 10:54:26 -0400703 switch (svsk->sk_sk->sk_family) {
704 case AF_INET:
705 level = SOL_IP;
706 optname = IP_PKTINFO;
707 break;
708 case AF_INET6:
709 level = SOL_IPV6;
710 optname = IPV6_RECVPKTINFO;
711 break;
712 default:
713 BUG();
714 }
715 err = kernel_setsockopt(svsk->sk_sock, level, optname,
716 (char *)&one, sizeof(one));
717 dprintk("svc: kernel_setsockopt returned %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
720/*
721 * A data_ready event on a listening socket means there's a connection
722 * pending. Do not use state_change as a substitute for it.
723 */
David S. Miller676d2362014-04-11 16:15:36 -0400724static void svc_tcp_listen_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Neil Brown939bb7e2005-09-13 01:25:39 -0700726 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700729 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400731 if (svsk)
732 svsk->sk_odata(sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700733 /*
734 * This callback may called twice when a new connection
735 * is established as a child socket inherits everything
736 * from a parent LISTEN socket.
737 * 1) data_ready method of the parent socket will be called
738 * when one of child sockets become ESTABLISHED.
739 * 2) data_ready method of the child socket may be called
740 * when it receives data before the socket is accepted.
741 * In case of 2, we should ignore it silently.
742 */
743 if (sk->sk_state == TCP_LISTEN) {
744 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600745 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600746 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700747 } else
748 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
752/*
753 * A state change on a connected socket means it's dying or dead.
754 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600755static void svc_tcp_state_change(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Neil Brown939bb7e2005-09-13 01:25:39 -0700757 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700760 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Neil Brown939bb7e2005-09-13 01:25:39 -0700762 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700764 else {
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400765 svsk->sk_ostate(sk);
Trond Myklebustb2f21f72016-07-26 09:51:19 -0400766 if (sk->sk_state != TCP_ESTABLISHED) {
767 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
768 svc_xprt_enqueue(&svsk->sk_xprt);
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773/*
774 * Accept a TCP connection
775 */
Tom Tucker38a417c2007-12-30 21:07:36 -0600776static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Tom Tucker38a417c2007-12-30 21:07:36 -0600778 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800779 struct sockaddr_storage addr;
780 struct sockaddr *sin = (struct sockaddr *) &addr;
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600781 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct socket *sock = svsk->sk_sock;
783 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 struct svc_sock *newsvsk;
785 int err, slen;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +0300786 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
789 if (!sock)
Tom Tucker38a417c2007-12-30 21:07:36 -0600790 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Tom Tucker02fc6c32007-12-30 21:07:48 -0600792 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700793 err = kernel_accept(sock, &newsock, O_NONBLOCK);
794 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (err == -ENOMEM)
796 printk(KERN_WARNING "%s: no more sockets!\n",
797 serv->sv_name);
Joe Perchese87cc472012-05-13 21:56:26 +0000798 else if (err != -EAGAIN)
799 net_warn_ratelimited("%s: accept failed (err %d)!\n",
800 serv->sv_name, -err);
Tom Tucker38a417c2007-12-30 21:07:36 -0600801 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
Tom Tucker02fc6c32007-12-30 21:07:48 -0600803 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800805 err = kernel_getpeername(newsock, sin, &slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (err < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +0000807 net_warn_ratelimited("%s: peername failed (err %d)!\n",
808 serv->sv_name, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 goto failed; /* aborted connection or whatever */
810 }
811
812 /* Ideally, we would want to reject connections from unauthorized
Chuck Leverad06e4b2007-02-12 00:53:32 -0800813 * hosts here, but when we get encryption, the IP of the host won't
814 * tell us anything. For now just warn about unpriv connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 */
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800816 if (!svc_port_is_privileged(sin)) {
Kinglong Meea48fd0f2014-05-29 12:18:52 +0800817 dprintk("%s: connect from unprivileged port: %s\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800818 serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800819 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
Chuck Leverad06e4b2007-02-12 00:53:32 -0800821 dprintk("%s: connect from %s\n", serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800822 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400824 /* Reset the inherited callbacks before calling svc_setup_socket */
825 newsock->sk->sk_state_change = svsk->sk_ostate;
826 newsock->sk->sk_data_ready = svsk->sk_odata;
827 newsock->sk->sk_write_space = svsk->sk_owspace;
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* make sure that a write doesn't block forever when
830 * low on memory
831 */
832 newsock->sk->sk_sndtimeo = HZ*30;
833
J. Bruce Fields72c35372012-08-13 17:46:17 -0400834 newsvsk = svc_setup_socket(serv, newsock,
835 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
836 if (IS_ERR(newsvsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 goto failed;
Tom Tucker9dbc2402007-12-30 21:08:12 -0600838 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
Frank van Maarseveena9747692007-07-09 22:21:39 +0200839 err = kernel_getsockname(newsock, sin, &slen);
840 if (unlikely(err < 0)) {
841 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
842 slen = offsetof(struct sockaddr, sa_data);
843 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600844 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -0800845
NeilBrownef11ce22014-05-12 11:22:47 +1000846 if (sock_is_loopback(newsock->sk))
847 set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
848 else
849 clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600850 if (serv->sv_stats)
851 serv->sv_stats->nettcpconn++;
852
853 return &newsvsk->sk_xprt;
854
855failed:
856 sock_release(newsock);
857 return NULL;
858}
859
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800860static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
861{
862 unsigned int i, len, npages;
863
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500864 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800865 return 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500866 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800867 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
868 for (i = 0; i < npages; i++) {
869 if (rqstp->rq_pages[i] != NULL)
870 put_page(rqstp->rq_pages[i]);
871 BUG_ON(svsk->sk_pages[i] == NULL);
872 rqstp->rq_pages[i] = svsk->sk_pages[i];
873 svsk->sk_pages[i] = NULL;
874 }
875 rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
876 return len;
877}
878
879static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
880{
881 unsigned int i, len, npages;
882
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500883 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800884 return;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500885 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800886 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
887 for (i = 0; i < npages; i++) {
888 svsk->sk_pages[i] = rqstp->rq_pages[i];
889 rqstp->rq_pages[i] = NULL;
890 }
891}
892
893static void svc_tcp_clear_pages(struct svc_sock *svsk)
894{
895 unsigned int i, len, npages;
896
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500897 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800898 goto out;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500899 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800900 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
901 for (i = 0; i < npages; i++) {
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -0400902 if (svsk->sk_pages[i] == NULL) {
903 WARN_ON_ONCE(1);
904 continue;
905 }
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800906 put_page(svsk->sk_pages[i]);
907 svsk->sk_pages[i] = NULL;
908 }
909out:
910 svsk->sk_tcplen = 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500911 svsk->sk_datalen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800912}
913
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600914/*
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -0500915 * Receive fragment record header.
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300916 * If we haven't gotten the record length yet, get the next four bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 */
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300918static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600920 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400921 unsigned int want;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300922 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Chuck Leverc0401ea2008-04-14 12:27:30 -0400924 if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 struct kvec iov;
926
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400927 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
929 iov.iov_len = want;
930 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
931 goto error;
932 svsk->sk_tcplen += len;
933
934 if (len < want) {
Chuck Leverc0401ea2008-04-14 12:27:30 -0400935 dprintk("svc: short recvfrom while reading record "
936 "length (%d of %d)\n", len, want);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800937 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500940 dprintk("svc: TCP record, %d bytes\n", svc_sock_reclen(svsk));
J. Bruce Fields836fbad2012-12-03 15:50:38 -0500941 if (svc_sock_reclen(svsk) + svsk->sk_datalen >
942 serv->sv_max_mesg) {
J. Bruce Fields3a28e332012-12-10 18:15:07 -0500943 net_notice_ratelimited("RPC: fragment too large: %d\n",
944 svc_sock_reclen(svsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 goto err_delete;
946 }
947 }
948
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500949 return svc_sock_reclen(svsk);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800950error:
951 dprintk("RPC: TCP recv_record got %d\n", len);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300952 return len;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800953err_delete:
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300954 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300955 return -EAGAIN;
956}
957
Trond Myklebust586c52c2009-05-18 17:47:56 -0400958static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300959{
Trond Myklebust586c52c2009-05-18 17:47:56 -0400960 struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300961 struct rpc_rqst *req = NULL;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400962 struct kvec *src, *dst;
963 __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
J. Bruce Fields48e65552011-02-14 14:52:03 -0500964 __be32 xid;
965 __be32 calldir;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300966
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300967 xid = *p++;
968 calldir = *p;
969
Trond Myklebust093a1462014-11-12 18:04:04 -0500970 if (!bc_xprt)
Trond Myklebust586c52c2009-05-18 17:47:56 -0400971 return -EAGAIN;
Trond Myklebust093a1462014-11-12 18:04:04 -0500972 spin_lock_bh(&bc_xprt->transport_lock);
973 req = xprt_lookup_rqst(bc_xprt, xid);
974 if (!req)
975 goto unlock_notfound;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400976
977 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
978 /*
979 * XXX!: cheating for now! Only copying HEAD.
980 * But we know this is good enough for now (in fact, for any
981 * callback reply in the forseeable future).
982 */
983 dst = &req->rq_private_buf.head[0];
984 src = &rqstp->rq_arg.head[0];
985 if (dst->iov_len < src->iov_len)
Trond Myklebust093a1462014-11-12 18:04:04 -0500986 goto unlock_eagain; /* whatever; just giving up. */
Trond Myklebust586c52c2009-05-18 17:47:56 -0400987 memcpy(dst->iov_base, src->iov_base, src->iov_len);
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500988 xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len);
Trond Myklebust586c52c2009-05-18 17:47:56 -0400989 rqstp->rq_arg.len = 0;
Trond Myklebust093a1462014-11-12 18:04:04 -0500990 spin_unlock_bh(&bc_xprt->transport_lock);
Trond Myklebust586c52c2009-05-18 17:47:56 -0400991 return 0;
Trond Myklebust093a1462014-11-12 18:04:04 -0500992unlock_notfound:
993 printk(KERN_NOTICE
994 "%s: Got unrecognized reply: "
995 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
996 __func__, ntohl(calldir),
997 bc_xprt, ntohl(xid));
998unlock_eagain:
999 spin_unlock_bh(&bc_xprt->transport_lock);
1000 return -EAGAIN;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001001}
1002
1003static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len)
1004{
1005 int i = 0;
1006 int t = 0;
1007
1008 while (t < len) {
1009 vec[i].iov_base = page_address(pages[i]);
1010 vec[i].iov_len = PAGE_SIZE;
1011 i++;
1012 t += PAGE_SIZE;
1013 }
1014 return i;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001015}
1016
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001017static void svc_tcp_fragment_received(struct svc_sock *svsk)
1018{
1019 /* If we have more data, signal svc_xprt_enqueue() to try again */
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001020 dprintk("svc: TCP %s record (%d bytes)\n",
1021 svc_sock_final_rec(svsk) ? "final" : "nonfinal",
1022 svc_sock_reclen(svsk));
1023 svsk->sk_tcplen = 0;
1024 svsk->sk_reclen = 0;
1025}
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001026
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001027/*
1028 * Receive data from a TCP socket.
1029 */
1030static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
1031{
1032 struct svc_sock *svsk =
1033 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
1034 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1035 int len;
1036 struct kvec *vec;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001037 unsigned int want, base;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001038 __be32 *p;
1039 __be32 calldir;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001040 int pnum;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001041
1042 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1043 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
1044 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
1045 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
1046
1047 len = svc_tcp_recv_record(svsk, rqstp);
1048 if (len < 0)
1049 goto error;
1050
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001051 base = svc_tcp_restore_pages(svsk, rqstp);
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001052 want = svc_sock_reclen(svsk) - (svsk->sk_tcplen - sizeof(rpc_fraghdr));
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001053
NeilBrown3cc03b12006-10-04 02:15:47 -07001054 vec = rqstp->rq_vec;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001055
Trond Myklebust586c52c2009-05-18 17:47:56 -04001056 pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0],
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001057 svsk->sk_datalen + want);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001058
NeilBrown44524352006-10-04 02:15:46 -07001059 rqstp->rq_respages = &rqstp->rq_pages[pnum];
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001060 rqstp->rq_next_page = rqstp->rq_respages + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 /* Now receive data */
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001063 len = svc_partial_recvfrom(rqstp, vec, pnum, want, base);
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001064 if (len >= 0) {
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001065 svsk->sk_tcplen += len;
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001066 svsk->sk_datalen += len;
1067 }
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001068 if (len != want || !svc_sock_final_rec(svsk)) {
J. Bruce Fieldsbe1e4442012-08-09 18:12:28 -04001069 svc_tcp_save_pages(svsk, rqstp);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001070 if (len < 0 && len != -EAGAIN)
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001071 goto err_delete;
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001072 if (len == want)
1073 svc_tcp_fragment_received(svsk);
1074 else
J. Bruce Fields3a28e332012-12-10 18:15:07 -05001075 dprintk("svc: incomplete TCP record (%d of %d)\n",
1076 (int)(svsk->sk_tcplen - sizeof(rpc_fraghdr)),
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001077 svc_sock_reclen(svsk));
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001078 goto err_noclose;
1079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
J. Bruce Fields1f691b02013-06-26 10:55:40 -04001081 if (svsk->sk_datalen < 8) {
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -04001082 svsk->sk_datalen = 0;
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001083 goto err_delete; /* client is nuts. */
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -04001084 }
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001085
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001086 rqstp->rq_arg.len = svsk->sk_datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 rqstp->rq_arg.page_base = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001088 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1089 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 rqstp->rq_arg.page_len = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001091 } else
1092 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Tom Tucker5148bf42007-12-30 21:07:25 -06001094 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 rqstp->rq_prot = IPPROTO_TCP;
Jeff Layton7501cc22014-11-19 07:51:15 -05001096 if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags))
1097 set_bit(RQ_LOCAL, &rqstp->rq_flags);
1098 else
1099 clear_bit(RQ_LOCAL, &rqstp->rq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Trond Myklebust586c52c2009-05-18 17:47:56 -04001101 p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1102 calldir = p[1];
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001103 if (calldir)
Trond Myklebust586c52c2009-05-18 17:47:56 -04001104 len = receive_cb_reply(svsk, rqstp);
Trond Myklebust586c52c2009-05-18 17:47:56 -04001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /* Reset TCP read info */
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001107 svsk->sk_datalen = 0;
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001108 svc_tcp_fragment_received(svsk);
Trond Myklebust0601f792009-05-18 17:47:56 -04001109
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001110 if (len < 0)
1111 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Tom Tucker9dbc2402007-12-30 21:08:12 -06001113 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (serv->sv_stats)
1115 serv->sv_stats->nettcpcnt++;
1116
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001117 return rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001119error:
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001120 if (len != -EAGAIN)
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001121 goto err_delete;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001122 dprintk("RPC: TCP recvfrom got EAGAIN\n");
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -04001123 return 0;
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001124err_delete:
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001125 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1126 svsk->sk_xprt.xpt_server->sv_name, -len);
1127 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1128err_noclose:
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -04001129 return 0; /* record not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
1132/*
1133 * Send out data on TCP socket.
1134 */
Tom Tucker0f0257e2007-12-30 21:08:27 -06001135static int svc_tcp_sendto(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 struct xdr_buf *xbufp = &rqstp->rq_res;
1138 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001139 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 /* Set up the first element of the reply kvec.
1142 * Any other kvecs that may be in use have been taken
1143 * care of by the server implementation itself.
1144 */
1145 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1146 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 sent = svc_sendto(rqstp, &rqstp->rq_res);
1149 if (sent != xbufp->len) {
Tom Tucker0f0257e2007-12-30 21:08:27 -06001150 printk(KERN_NOTICE
1151 "rpc-srv/tcp: %s: %s %d when sending %d bytes "
1152 "- shutting down socket\n",
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001153 rqstp->rq_xprt->xpt_server->sv_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 (sent<0)?"got error":"sent only",
1155 sent, xbufp->len);
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001156 set_bit(XPT_CLOSE, &rqstp->rq_xprt->xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -06001157 svc_xprt_enqueue(rqstp->rq_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 sent = -EAGAIN;
1159 }
1160 return sent;
1161}
1162
Tom Tuckere831fe62007-12-30 21:07:29 -06001163/*
1164 * Setup response header. TCP has a 4B record length field.
1165 */
1166static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
1167{
1168 struct kvec *resv = &rqstp->rq_res.head[0];
1169
1170 /* tcp needs a space for the record length... */
1171 svc_putnl(resv, 0);
1172}
1173
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001174static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001175 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001176 struct sockaddr *sa, int salen,
1177 int flags)
1178{
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001179 return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001180}
1181
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001182#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Andy Adamson1f11a032011-01-06 02:04:26 +00001183static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
1184 struct net *, struct sockaddr *,
1185 int, int);
1186static void svc_bc_sock_free(struct svc_xprt *xprt);
1187
1188static struct svc_xprt *svc_bc_tcp_create(struct svc_serv *serv,
1189 struct net *net,
1190 struct sockaddr *sa, int salen,
1191 int flags)
1192{
1193 return svc_bc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1194}
1195
1196static void svc_bc_tcp_sock_detach(struct svc_xprt *xprt)
1197{
1198}
1199
1200static struct svc_xprt_ops svc_tcp_bc_ops = {
1201 .xpo_create = svc_bc_tcp_create,
1202 .xpo_detach = svc_bc_tcp_sock_detach,
1203 .xpo_free = svc_bc_sock_free,
1204 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
Chuck Lever16e4d932014-05-19 13:40:22 -04001205 .xpo_secure_port = svc_sock_secure_port,
Andy Adamson1f11a032011-01-06 02:04:26 +00001206};
1207
1208static struct svc_xprt_class svc_tcp_bc_class = {
1209 .xcl_name = "tcp-bc",
1210 .xcl_owner = THIS_MODULE,
1211 .xcl_ops = &svc_tcp_bc_ops,
1212 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1213};
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001214
1215static void svc_init_bc_xprt_sock(void)
1216{
1217 svc_reg_xprt_class(&svc_tcp_bc_class);
1218}
1219
1220static void svc_cleanup_bc_xprt_sock(void)
1221{
1222 svc_unreg_xprt_class(&svc_tcp_bc_class);
1223}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001224#else /* CONFIG_SUNRPC_BACKCHANNEL */
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001225static void svc_init_bc_xprt_sock(void)
1226{
1227}
1228
1229static void svc_cleanup_bc_xprt_sock(void)
1230{
1231}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001232#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Andy Adamson1f11a032011-01-06 02:04:26 +00001233
Tom Tucker360d87382007-12-30 21:07:17 -06001234static struct svc_xprt_ops svc_tcp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001235 .xpo_create = svc_tcp_create,
Tom Tucker5d137992007-12-30 21:07:23 -06001236 .xpo_recvfrom = svc_tcp_recvfrom,
1237 .xpo_sendto = svc_tcp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -06001238 .xpo_release_rqst = svc_release_skb,
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001239 .xpo_detach = svc_tcp_sock_detach,
Tom Tucker755ccea2007-12-30 21:07:27 -06001240 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -06001241 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -06001242 .xpo_has_wspace = svc_tcp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -06001243 .xpo_accept = svc_tcp_accept,
Chuck Lever16e4d932014-05-19 13:40:22 -04001244 .xpo_secure_port = svc_sock_secure_port,
Tom Tucker360d87382007-12-30 21:07:17 -06001245};
1246
1247static struct svc_xprt_class svc_tcp_class = {
1248 .xcl_name = "tcp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001249 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -06001250 .xcl_ops = &svc_tcp_ops,
Tom Tucker49023152007-12-30 21:07:21 -06001251 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
Chuck Lever3c45ddf2014-07-16 15:38:32 -04001252 .xcl_ident = XPRT_TRANSPORT_TCP,
Tom Tucker360d87382007-12-30 21:07:17 -06001253};
1254
1255void svc_init_xprt_sock(void)
1256{
1257 svc_reg_xprt_class(&svc_tcp_class);
1258 svc_reg_xprt_class(&svc_udp_class);
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001259 svc_init_bc_xprt_sock();
Tom Tucker360d87382007-12-30 21:07:17 -06001260}
1261
1262void svc_cleanup_xprt_sock(void)
1263{
1264 svc_unreg_xprt_class(&svc_tcp_class);
1265 svc_unreg_xprt_class(&svc_udp_class);
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001266 svc_cleanup_bc_xprt_sock();
Tom Tucker360d87382007-12-30 21:07:17 -06001267}
1268
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001269static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 struct sock *sk = svsk->sk_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +03001273 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
1274 &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -06001275 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (sk->sk_state == TCP_LISTEN) {
1277 dprintk("setting up TCP socket for listening\n");
Tom Tucker02fc6c32007-12-30 21:07:48 -06001278 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 sk->sk_data_ready = svc_tcp_listen_data_ready;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001280 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 } else {
1282 dprintk("setting up TCP socket for reading\n");
1283 sk->sk_state_change = svc_tcp_state_change;
Trond Myklebustfa9251a2016-06-24 10:55:48 -04001284 sk->sk_data_ready = svc_data_ready;
Trond Myklebust637600f2016-06-24 10:55:51 -04001285 sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 svsk->sk_reclen = 0;
1288 svsk->sk_tcplen = 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001289 svsk->sk_datalen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001290 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Chuck Leverb7872fe2008-04-14 12:27:01 -04001292 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Tom Tucker02fc6c32007-12-30 21:07:48 -06001294 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Trond Myklebustb2f21f72016-07-26 09:51:19 -04001295 switch (sk->sk_state) {
1296 case TCP_SYN_RECV:
1297 case TCP_ESTABLISHED:
1298 break;
1299 default:
Tom Tucker02fc6c32007-12-30 21:07:48 -06001300 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Trond Myklebustb2f21f72016-07-26 09:51:19 -04001301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303}
1304
Tom Tucker0f0257e2007-12-30 21:08:27 -06001305void svc_sock_update_bufs(struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 /*
1308 * The number of server threads has changed. Update
1309 * rcvbuf and sndbuf accordingly on all sockets
1310 */
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001311 struct svc_sock *svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 spin_lock_bh(&serv->sv_lock);
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001314 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001315 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 spin_unlock_bh(&serv->sv_lock);
1317}
Trond Myklebust24c37672008-12-23 16:30:12 -05001318EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 * Initialize socket for RPC use and create svc_sock struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 */
Chuck Lever6b174332007-02-12 00:53:28 -08001323static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1324 struct socket *sock,
J. Bruce Fields72c35372012-08-13 17:46:17 -04001325 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 struct svc_sock *svsk;
1328 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001329 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001330 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 dprintk("svc: svc_setup_socket %p\n", sock);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001333 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1334 if (!svsk)
1335 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 inet = sock->sk;
1338
1339 /* Register socket with portmapper */
J. Bruce Fields72c35372012-08-13 17:46:17 -04001340 if (pmap_register)
1341 err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001342 inet->sk_protocol,
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001343 ntohs(inet_sk(inet)->inet_sport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
J. Bruce Fields72c35372012-08-13 17:46:17 -04001345 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 kfree(svsk);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001347 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 inet->sk_user_data = svsk;
1351 svsk->sk_sock = sock;
1352 svsk->sk_sk = inet;
1353 svsk->sk_ostate = inet->sk_state_change;
1354 svsk->sk_odata = inet->sk_data_ready;
1355 svsk->sk_owspace = inet->sk_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /* Initialize the socket */
1358 if (sock->type == SOCK_DGRAM)
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001359 svc_udp_init(svsk, serv);
Trond Myklebust637600f2016-06-24 10:55:51 -04001360 else
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001361 svc_tcp_init(svsk, serv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Trond Myklebustc7995f82016-07-26 09:51:20 -04001363 dprintk("svc: svc_setup_socket created %p (inet %p), "
1364 "listen %d close %d\n",
1365 svsk, svsk->sk_sk,
1366 test_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags),
1367 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return svsk;
1370}
1371
Stanislav Kinsbursky30646392014-02-26 16:50:01 +03001372bool svc_alien_sock(struct net *net, int fd)
1373{
1374 int err;
1375 struct socket *sock = sockfd_lookup(fd, &err);
1376 bool ret = false;
1377
1378 if (!sock)
1379 goto out;
1380 if (sock_net(sock->sk) != net)
1381 ret = true;
1382 sockfd_put(sock);
1383out:
1384 return ret;
1385}
1386EXPORT_SYMBOL_GPL(svc_alien_sock);
1387
Chuck Leverbfba9ab2009-04-23 19:32:33 -04001388/**
1389 * svc_addsock - add a listener socket to an RPC service
1390 * @serv: pointer to RPC service to which to add a new listener
1391 * @fd: file descriptor of the new listener
1392 * @name_return: pointer to buffer to fill in with name of listener
1393 * @len: size of the buffer
1394 *
1395 * Fills in socket name and returns positive length of name if successful.
1396 * Name is terminated with '\n'. On error, returns a negative errno
1397 * value.
1398 */
1399int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
1400 const size_t len)
NeilBrownb41b66d2006-10-02 02:17:48 -07001401{
1402 int err = 0;
1403 struct socket *so = sockfd_lookup(fd, &err);
1404 struct svc_sock *svsk = NULL;
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001405 struct sockaddr_storage addr;
1406 struct sockaddr *sin = (struct sockaddr *)&addr;
1407 int salen;
NeilBrownb41b66d2006-10-02 02:17:48 -07001408
1409 if (!so)
1410 return err;
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001411 err = -EAFNOSUPPORT;
Aime Le Rouzic205ba422010-01-26 14:03:56 -05001412 if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001413 goto out;
1414 err = -EPROTONOSUPPORT;
1415 if (so->sk->sk_protocol != IPPROTO_TCP &&
NeilBrownb41b66d2006-10-02 02:17:48 -07001416 so->sk->sk_protocol != IPPROTO_UDP)
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001417 goto out;
1418 err = -EISCONN;
1419 if (so->state > SS_UNCONNECTED)
1420 goto out;
1421 err = -ENOENT;
1422 if (!try_module_get(THIS_MODULE))
1423 goto out;
1424 svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1425 if (IS_ERR(svsk)) {
1426 module_put(THIS_MODULE);
1427 err = PTR_ERR(svsk);
1428 goto out;
NeilBrownb41b66d2006-10-02 02:17:48 -07001429 }
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001430 if (kernel_getsockname(svsk->sk_sock, sin, &salen) == 0)
1431 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
J. Bruce Fields39b55302012-08-14 15:50:34 -04001432 svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
Chuck Levere7942b92009-04-23 19:32:48 -04001433 return svc_one_sock_name(svsk, name_return, len);
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001434out:
1435 sockfd_put(so);
1436 return err;
NeilBrownb41b66d2006-10-02 02:17:48 -07001437}
1438EXPORT_SYMBOL_GPL(svc_addsock);
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440/*
1441 * Create socket for RPC service.
1442 */
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001443static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1444 int protocol,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001445 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001446 struct sockaddr *sin, int len,
1447 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 struct svc_sock *svsk;
1450 struct socket *sock;
1451 int error;
1452 int type;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001453 struct sockaddr_storage addr;
1454 struct sockaddr *newsin = (struct sockaddr *)&addr;
1455 int newlen;
Trond Myklebustc69da772009-03-30 18:59:17 -04001456 int family;
1457 int val;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +03001458 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Chuck Leverad06e4b2007-02-12 00:53:32 -08001460 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1461 serv->sv_program->pg_name, protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001462 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1465 printk(KERN_WARNING "svc: only UDP and TCP "
1466 "sockets supported\n");
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001467 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Trond Myklebustc69da772009-03-30 18:59:17 -04001470 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1471 switch (sin->sa_family) {
1472 case AF_INET6:
1473 family = PF_INET6;
1474 break;
1475 case AF_INET:
1476 family = PF_INET;
1477 break;
1478 default:
1479 return ERR_PTR(-EINVAL);
1480 }
1481
Pavel Emelyanov14ec63c2010-09-29 16:06:57 +04001482 error = __sock_create(net, family, type, protocol, &sock, 1);
Chuck Lever77f1f672007-02-12 00:53:39 -08001483 if (error < 0)
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001484 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Peter Zijlstraed075362006-12-06 20:35:24 -08001486 svc_reclassify_socket(sock);
1487
Trond Myklebustc69da772009-03-30 18:59:17 -04001488 /*
1489 * If this is an PF_INET6 listener, we want to avoid
1490 * getting requests from IPv4 remotes. Those should
1491 * be shunted to a PF_INET listener via rpcbind.
1492 */
1493 val = 1;
1494 if (family == PF_INET6)
1495 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
1496 (char *)&val, sizeof(val));
1497
Eric Sesterhenn18114742006-09-28 14:37:07 -07001498 if (type == SOCK_STREAM)
Pavel Emelyanov4a17fd52012-04-19 03:39:36 +00001499 sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
Chuck Lever77f1f672007-02-12 00:53:39 -08001500 error = kernel_bind(sock, sin, len);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001501 if (error < 0)
1502 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Tom Tucker9dbc2402007-12-30 21:08:12 -06001504 newlen = len;
1505 error = kernel_getsockname(sock, newsin, &newlen);
1506 if (error < 0)
1507 goto bummer;
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001510 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 goto bummer;
1512 }
1513
J. Bruce Fields72c35372012-08-13 17:46:17 -04001514 svsk = svc_setup_socket(serv, sock, flags);
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001515 if (IS_ERR(svsk)) {
1516 error = PTR_ERR(svsk);
1517 goto bummer;
NeilBrowne79eff12007-02-12 00:53:30 -08001518 }
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001519 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1520 return (struct svc_xprt *)svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521bummer:
1522 dprintk("svc: svc_create_socket error = %d\n", -error);
1523 sock_release(sock);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001524 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
1527/*
Tom Tucker755ccea2007-12-30 21:07:27 -06001528 * Detach the svc_sock from the socket so that no
1529 * more callbacks occur.
1530 */
1531static void svc_sock_detach(struct svc_xprt *xprt)
1532{
1533 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1534 struct sock *sk = svsk->sk_sk;
1535
1536 dprintk("svc: svc_sock_detach(%p)\n", svsk);
1537
1538 /* put back the old socket callbacks */
Trond Myklebust069c2252016-06-24 10:55:47 -04001539 lock_sock(sk);
Tom Tucker755ccea2007-12-30 21:07:27 -06001540 sk->sk_state_change = svsk->sk_ostate;
1541 sk->sk_data_ready = svsk->sk_odata;
1542 sk->sk_write_space = svsk->sk_owspace;
Trond Myklebust069c2252016-06-24 10:55:47 -04001543 sk->sk_user_data = NULL;
1544 release_sock(sk);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001545}
1546
1547/*
1548 * Disconnect the socket, and reset the callbacks
1549 */
1550static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1551{
1552 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1553
1554 dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk);
1555
1556 svc_sock_detach(xprt);
1557
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001558 if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1559 svc_tcp_clear_pages(svsk);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001560 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001561 }
Tom Tucker755ccea2007-12-30 21:07:27 -06001562}
1563
1564/*
1565 * Free the svc_sock's socket resources and the svc_sock itself.
1566 */
1567static void svc_sock_free(struct svc_xprt *xprt)
1568{
1569 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1570 dprintk("svc: svc_sock_free(%p)\n", svsk);
1571
Tom Tucker755ccea2007-12-30 21:07:27 -06001572 if (svsk->sk_sock->file)
1573 sockfd_put(svsk->sk_sock);
1574 else
1575 sock_release(svsk->sk_sock);
1576 kfree(svsk);
1577}
Benny Halevy7652e5a2009-04-01 09:23:09 -04001578
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001579#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001580/*
Andy Adamson1f11a032011-01-06 02:04:26 +00001581 * Create a back channel svc_xprt which shares the fore channel socket.
Benny Halevy7652e5a2009-04-01 09:23:09 -04001582 */
Andy Adamson1f11a032011-01-06 02:04:26 +00001583static struct svc_xprt *svc_bc_create_socket(struct svc_serv *serv,
1584 int protocol,
1585 struct net *net,
1586 struct sockaddr *sin, int len,
1587 int flags)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001588{
1589 struct svc_sock *svsk;
Andy Adamson1f11a032011-01-06 02:04:26 +00001590 struct svc_xprt *xprt;
Benny Halevy7652e5a2009-04-01 09:23:09 -04001591
Andy Adamson1f11a032011-01-06 02:04:26 +00001592 if (protocol != IPPROTO_TCP) {
1593 printk(KERN_WARNING "svc: only TCP sockets"
1594 " supported on shared back channel\n");
1595 return ERR_PTR(-EINVAL);
1596 }
1597
Benny Halevy7652e5a2009-04-01 09:23:09 -04001598 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1599 if (!svsk)
Andy Adamson1f11a032011-01-06 02:04:26 +00001600 return ERR_PTR(-ENOMEM);
Benny Halevy7652e5a2009-04-01 09:23:09 -04001601
1602 xprt = &svsk->sk_xprt;
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +03001603 svc_xprt_init(net, &svc_tcp_bc_class, xprt, serv);
Andy Adamson1f11a032011-01-06 02:04:26 +00001604
Andy Adamson4a19de02011-01-06 02:04:35 +00001605 serv->sv_bc_xprt = xprt;
Andy Adamson1f11a032011-01-06 02:04:26 +00001606
Benny Halevy7652e5a2009-04-01 09:23:09 -04001607 return xprt;
1608}
Benny Halevy7652e5a2009-04-01 09:23:09 -04001609
1610/*
Andy Adamson1f11a032011-01-06 02:04:26 +00001611 * Free a back channel svc_sock.
Benny Halevy7652e5a2009-04-01 09:23:09 -04001612 */
Andy Adamson1f11a032011-01-06 02:04:26 +00001613static void svc_bc_sock_free(struct svc_xprt *xprt)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001614{
Andy Adamson778be232011-01-25 15:38:01 +00001615 if (xprt)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001616 kfree(container_of(xprt, struct svc_sock, sk_xprt));
1617}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001618#endif /* CONFIG_SUNRPC_BACKCHANNEL */