blob: dadfec66dbd8abd301fa3ac307431dabe7999ec6 [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);
David S. Miller676d2362014-04-11 16:15:36 -040063static void svc_udp_data_ready(struct sock *);
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
Chuck Leverb92503b2007-02-12 00:53:36 -0800133union svc_pktinfo_u {
134 struct in_pktinfo pkti;
Chuck Leverb92503b2007-02-12 00:53:36 -0800135 struct in6_pktinfo pkti6;
Chuck Leverb92503b2007-02-12 00:53:36 -0800136};
David S. Millerbc375ea2007-04-12 13:35:59 -0700137#define SVC_PKTINFO_SPACE \
138 CMSG_SPACE(sizeof(union svc_pktinfo_u))
Chuck Leverb92503b2007-02-12 00:53:36 -0800139
140static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
141{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600142 struct svc_sock *svsk =
143 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
144 switch (svsk->sk_sk->sk_family) {
Chuck Leverb92503b2007-02-12 00:53:36 -0800145 case AF_INET: {
146 struct in_pktinfo *pki = CMSG_DATA(cmh);
147
148 cmh->cmsg_level = SOL_IP;
149 cmh->cmsg_type = IP_PKTINFO;
150 pki->ipi_ifindex = 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800151 pki->ipi_spec_dst.s_addr =
152 svc_daddr_in(rqstp)->sin_addr.s_addr;
Chuck Leverb92503b2007-02-12 00:53:36 -0800153 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
154 }
155 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800156
Chuck Leverb92503b2007-02-12 00:53:36 -0800157 case AF_INET6: {
158 struct in6_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800159 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
Chuck Leverb92503b2007-02-12 00:53:36 -0800160
161 cmh->cmsg_level = SOL_IPV6;
162 cmh->cmsg_type = IPV6_PKTINFO;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800163 pki->ipi6_ifindex = daddr->sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000164 pki->ipi6_addr = daddr->sin6_addr;
Chuck Leverb92503b2007-02-12 00:53:36 -0800165 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
166 }
167 break;
Chuck Leverb92503b2007-02-12 00:53:36 -0800168 }
Chuck Leverb92503b2007-02-12 00:53:36 -0800169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300172 * send routine intended to be shared by the fore- and back-channel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300174int svc_send_common(struct socket *sock, struct xdr_buf *xdr,
175 struct page *headpage, unsigned long headoffset,
176 struct page *tailpage, unsigned long tailoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int result;
179 int size;
180 struct page **ppage = xdr->pages;
181 size_t base = xdr->page_base;
182 unsigned int pglen = xdr->page_len;
Trond Myklebust226453d2015-10-07 13:54:42 -0400183 unsigned int flags = MSG_MORE | MSG_SENDPAGE_NOTLAST;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300184 int slen;
185 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 slen = xdr->len;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* send head */
190 if (slen == xdr->head[0].iov_len)
191 flags = 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300192 len = kernel_sendpage(sock, headpage, headoffset,
NeilBrown44524352006-10-04 02:15:46 -0700193 xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (len != xdr->head[0].iov_len)
195 goto out;
196 slen -= xdr->head[0].iov_len;
197 if (slen == 0)
198 goto out;
199
200 /* send page data */
201 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
202 while (pglen > 0) {
203 if (slen == size)
204 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700205 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (result > 0)
207 len += result;
208 if (result != size)
209 goto out;
210 slen -= size;
211 pglen -= size;
212 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
213 base = 0;
214 ppage++;
215 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* send tail */
218 if (xdr->tail[0].iov_len) {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300219 result = kernel_sendpage(sock, tailpage, tailoffset,
220 xdr->tail[0].iov_len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (result > 0)
222 len += result;
223 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300224
225out:
226 return len;
227}
228
229
230/*
231 * Generic sendto routine
232 */
233static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
234{
235 struct svc_sock *svsk =
236 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
237 struct socket *sock = svsk->sk_sock;
238 union {
239 struct cmsghdr hdr;
240 long all[SVC_PKTINFO_SPACE / sizeof(long)];
241 } buffer;
242 struct cmsghdr *cmh = &buffer.hdr;
243 int len = 0;
244 unsigned long tailoff;
245 unsigned long headoff;
246 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
247
248 if (rqstp->rq_prot == IPPROTO_UDP) {
249 struct msghdr msg = {
250 .msg_name = &rqstp->rq_addr,
251 .msg_namelen = rqstp->rq_addrlen,
252 .msg_control = cmh,
253 .msg_controllen = sizeof(buffer),
254 .msg_flags = MSG_MORE,
255 };
256
257 svc_set_cmsg_data(rqstp, cmh);
258
Al Virod8725c82014-12-11 00:02:50 -0500259 if (sock_sendmsg(sock, &msg) < 0)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300260 goto out;
261 }
262
263 tailoff = ((unsigned long)xdr->tail[0].iov_base) & (PAGE_SIZE-1);
264 headoff = 0;
265 len = svc_send_common(sock, xdr, rqstp->rq_respages[0], headoff,
266 rqstp->rq_respages[0], tailoff);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268out:
Chuck Leverad06e4b2007-02-12 00:53:32 -0800269 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600270 svsk, xdr->head[0].iov_base, xdr->head[0].iov_len,
Chuck Leverad06e4b2007-02-12 00:53:32 -0800271 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 return len;
274}
275
276/*
NeilBrown80212d52006-10-02 02:17:47 -0700277 * Report socket names for nfsdfs
278 */
Chuck Levere7942b92009-04-23 19:32:48 -0400279static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
NeilBrown80212d52006-10-02 02:17:47 -0700280{
Chuck Lever017cb472009-04-23 19:33:03 -0400281 const struct sock *sk = svsk->sk_sk;
282 const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
283 "udp" : "tcp";
NeilBrown80212d52006-10-02 02:17:47 -0700284 int len;
285
Chuck Lever017cb472009-04-23 19:33:03 -0400286 switch (sk->sk_family) {
Chuck Levere7942b92009-04-23 19:32:48 -0400287 case PF_INET:
288 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400289 proto_name,
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000290 &inet_sk(sk)->inet_rcv_saddr,
291 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700292 break;
Eric Dumazetc2bb06d2013-10-09 03:05:48 -0700293#if IS_ENABLED(CONFIG_IPV6)
Chuck Lever58de2f82009-04-23 19:32:55 -0400294 case PF_INET6:
295 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400296 proto_name,
Eric Dumazetefe42082013-10-03 15:42:29 -0700297 &sk->sk_v6_rcv_saddr,
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000298 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700299 break;
Eric Dumazetc2bb06d2013-10-09 03:05:48 -0700300#endif
NeilBrown80212d52006-10-02 02:17:47 -0700301 default:
Chuck Levere7942b92009-04-23 19:32:48 -0400302 len = snprintf(buf, remaining, "*unknown-%d*\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400303 sk->sk_family);
NeilBrown80212d52006-10-02 02:17:47 -0700304 }
Chuck Levere7942b92009-04-23 19:32:48 -0400305
306 if (len >= remaining) {
307 *buf = '\0';
308 return -ENAMETOOLONG;
NeilBrown80212d52006-10-02 02:17:47 -0700309 }
310 return len;
311}
312
NeilBrown80212d52006-10-02 02:17:47 -0700313/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * Generic recvfrom routine.
315 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600316static int svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr,
317 int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600319 struct svc_sock *svsk =
320 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Chuck Lever1ba95102007-02-12 00:53:31 -0800321 struct msghdr msg = {
322 .msg_flags = MSG_DONTWAIT,
323 };
324 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Tom Tucker260c1d12007-12-30 21:08:29 -0600326 rqstp->rq_xprt_hlen = 0;
327
Trond Myklebustf8d1ff42014-08-03 13:03:13 -0400328 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Chuck Lever1ba95102007-02-12 00:53:31 -0800329 len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
330 msg.msg_flags);
Trond Myklebustf8d1ff42014-08-03 13:03:13 -0400331 /* If we read a full record, then assume there may be more
332 * data to read (stream based sockets only!)
333 */
334 if (len == buflen)
335 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
Chuck Lever1ba95102007-02-12 00:53:31 -0800338 svsk, iov[0].iov_base, iov[0].iov_len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return len;
340}
341
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800342static int svc_partial_recvfrom(struct svc_rqst *rqstp,
343 struct kvec *iov, int nr,
344 int buflen, unsigned int base)
345{
346 size_t save_iovlen;
Trond Myklebust09acfea2012-03-11 15:22:54 -0400347 void *save_iovbase;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800348 unsigned int i;
349 int ret;
350
351 if (base == 0)
352 return svc_recvfrom(rqstp, iov, nr, buflen);
353
354 for (i = 0; i < nr; i++) {
355 if (iov[i].iov_len > base)
356 break;
357 base -= iov[i].iov_len;
358 }
359 save_iovlen = iov[i].iov_len;
360 save_iovbase = iov[i].iov_base;
361 iov[i].iov_len -= base;
362 iov[i].iov_base += base;
363 ret = svc_recvfrom(rqstp, &iov[i], nr - i, buflen);
364 iov[i].iov_len = save_iovlen;
365 iov[i].iov_base = save_iovbase;
366 return ret;
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/*
370 * Set socket snd and rcv buffer lengths
371 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600372static void svc_sock_setbufsize(struct socket *sock, unsigned int snd,
373 unsigned int rcv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375#if 0
376 mm_segment_t oldfs;
377 oldfs = get_fs(); set_fs(KERNEL_DS);
378 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
379 (char*)&snd, sizeof(snd));
380 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
381 (char*)&rcv, sizeof(rcv));
382#else
383 /* sock_setsockopt limits use to sysctl_?mem_max,
384 * which isn't acceptable. Until that is made conditional
385 * on not having CAP_SYS_RESOURCE or similar, we go direct...
386 * DaveM said I could!
387 */
388 lock_sock(sock->sk);
389 sock->sk->sk_sndbuf = snd * 2;
390 sock->sk->sk_rcvbuf = rcv * 2;
Trond Myklebust47fcb032009-05-18 17:47:56 -0400391 sock->sk->sk_write_space(sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 release_sock(sock->sk);
393#endif
394}
Chuck Lever16e4d932014-05-19 13:40:22 -0400395
396static int svc_sock_secure_port(struct svc_rqst *rqstp)
397{
398 return svc_port_is_privileged(svc_addr(rqstp));
399}
400
J. Bruce Fields0442f142015-10-09 01:44:07 +0000401static bool sunrpc_waitqueue_active(wait_queue_head_t *wq)
402{
403 if (!wq)
404 return false;
405 /*
406 * There should normally be a memory * barrier here--see
407 * wq_has_sleeper().
408 *
409 * It appears that isn't currently necessary, though, basically
410 * because callers all appear to have sufficient memory barriers
411 * between the time the relevant change is made and the
412 * time they call these callbacks.
413 *
414 * The nfsd code itself doesn't actually explicitly wait on
415 * these waitqueues, but it may wait on them for example in
416 * sendpage() or sendmsg() calls. (And those may be the only
417 * places, since it it uses nonblocking reads.)
418 *
419 * Maybe we should add the memory barriers anyway, but these are
420 * hot paths so we'd need to be convinced there's no sigificant
421 * penalty.
422 */
423 return waitqueue_active(wq);
424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/*
427 * INET callback when data has been received on the socket.
428 */
David S. Miller676d2362014-04-11 16:15:36 -0400429static void svc_udp_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Neil Brown939bb7e2005-09-13 01:25:39 -0700431 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000432 wait_queue_head_t *wq = sk_sleep(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Neil Brown939bb7e2005-09-13 01:25:39 -0700434 if (svsk) {
David S. Miller676d2362014-04-11 16:15:36 -0400435 dprintk("svc: socket %p(inet %p), busy=%d\n",
436 svsk, sk,
Tom Tucker02fc6c32007-12-30 21:07:48 -0600437 test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
438 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600439 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700440 }
J. Bruce Fields0442f142015-10-09 01:44:07 +0000441 if (sunrpc_waitqueue_active(wq))
Eric Dumazeteaefd112011-02-18 03:26:36 +0000442 wake_up_interruptible(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445/*
446 * INET callback when space is newly available on the socket.
447 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600448static void svc_write_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
Eric Dumazeteaefd112011-02-18 03:26:36 +0000451 wait_queue_head_t *wq = sk_sleep(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if (svsk) {
454 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600455 svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Tom Tuckerf6150c32007-12-30 21:07:57 -0600456 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458
J. Bruce Fields0442f142015-10-09 01:44:07 +0000459 if (sunrpc_waitqueue_active(wq)) {
Neil Brown939bb7e2005-09-13 01:25:39 -0700460 dprintk("RPC svc_write_space: someone sleeping on %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 svsk);
Eric Dumazeteaefd112011-02-18 03:26:36 +0000462 wake_up_interruptible(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464}
465
Trond Myklebustc7fb3f02014-07-24 23:59:32 -0400466static int svc_tcp_has_wspace(struct svc_xprt *xprt)
467{
468 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
469 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
470 int required;
471
472 if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
473 return 1;
474 required = atomic_read(&xprt->xpt_reserved) + serv->sv_max_mesg;
475 if (sk_stream_wspace(svsk->sk_sk) >= required ||
476 (sk_stream_min_wspace(svsk->sk_sk) == 0 &&
477 atomic_read(&xprt->xpt_reserved) == 0))
478 return 1;
479 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
480 return 0;
481}
482
Trond Myklebust47fcb032009-05-18 17:47:56 -0400483static void svc_tcp_write_space(struct sock *sk)
484{
Trond Myklebustc7fb3f02014-07-24 23:59:32 -0400485 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
Trond Myklebust47fcb032009-05-18 17:47:56 -0400486 struct socket *sock = sk->sk_socket;
487
Trond Myklebustc7fb3f02014-07-24 23:59:32 -0400488 if (!sk_stream_is_writeable(sk) || !sock)
489 return;
490 if (!svsk || svc_tcp_has_wspace(&svsk->sk_xprt))
Trond Myklebust47fcb032009-05-18 17:47:56 -0400491 clear_bit(SOCK_NOSPACE, &sock->flags);
492 svc_write_space(sk);
493}
494
Trond Myklebust51877682014-07-24 23:59:33 -0400495static void svc_tcp_adjust_wspace(struct svc_xprt *xprt)
496{
497 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
498
499 if (svc_tcp_has_wspace(xprt))
500 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
501}
502
Tom Tucker9dbc2402007-12-30 21:08:12 -0600503/*
Chuck Lever7702ce42009-07-13 10:54:26 -0400504 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
505 */
506static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
507 struct cmsghdr *cmh)
508{
509 struct in_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800510 struct sockaddr_in *daddr = svc_daddr_in(rqstp);
511
Chuck Lever7702ce42009-07-13 10:54:26 -0400512 if (cmh->cmsg_type != IP_PKTINFO)
513 return 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800514
515 daddr->sin_family = AF_INET;
516 daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
Chuck Lever7702ce42009-07-13 10:54:26 -0400517 return 1;
518}
519
520/*
Tom Parkin73df66f2013-01-31 01:02:24 +0000521 * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
Chuck Lever7702ce42009-07-13 10:54:26 -0400522 */
523static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
524 struct cmsghdr *cmh)
525{
526 struct in6_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800527 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
528
Chuck Lever7702ce42009-07-13 10:54:26 -0400529 if (cmh->cmsg_type != IPV6_PKTINFO)
530 return 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800531
532 daddr->sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000533 daddr->sin6_addr = pki->ipi6_addr;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800534 daddr->sin6_scope_id = pki->ipi6_ifindex;
Chuck Lever7702ce42009-07-13 10:54:26 -0400535 return 1;
536}
537
538/*
Tom Tucker9dbc2402007-12-30 21:08:12 -0600539 * Copy the UDP datagram's destination address to the rqstp structure.
540 * The 'destination' address in this case is the address to which the
541 * peer sent the datagram, i.e. our local address. For multihomed
542 * hosts, this can change from msg to msg. Note that only the IP
543 * address changes, the port number should remain the same.
544 */
Chuck Lever7702ce42009-07-13 10:54:26 -0400545static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
546 struct cmsghdr *cmh)
Chuck Lever95756482007-02-12 00:53:38 -0800547{
Chuck Lever7702ce42009-07-13 10:54:26 -0400548 switch (cmh->cmsg_level) {
549 case SOL_IP:
550 return svc_udp_get_dest_address4(rqstp, cmh);
551 case SOL_IPV6:
552 return svc_udp_get_dest_address6(rqstp, cmh);
Chuck Lever95756482007-02-12 00:53:38 -0800553 }
Chuck Lever7702ce42009-07-13 10:54:26 -0400554
555 return 0;
Chuck Lever95756482007-02-12 00:53:38 -0800556}
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558/*
559 * Receive a datagram from a UDP socket.
560 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600561static int svc_udp_recvfrom(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600563 struct svc_sock *svsk =
564 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600565 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 struct sk_buff *skb;
David S. Millerbc375ea2007-04-12 13:35:59 -0700567 union {
568 struct cmsghdr hdr;
569 long all[SVC_PKTINFO_SPACE / sizeof(long)];
570 } buffer;
571 struct cmsghdr *cmh = &buffer.hdr;
NeilBrown7a37f572007-03-06 01:42:21 -0800572 struct msghdr msg = {
573 .msg_name = svc_addr(rqstp),
574 .msg_control = cmh,
575 .msg_controllen = sizeof(buffer),
576 .msg_flags = MSG_DONTWAIT,
577 };
Chuck Leverabc5c442009-04-23 19:31:25 -0400578 size_t len;
579 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Tom Tucker02fc6c32007-12-30 21:07:48 -0600581 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* udp sockets need large rcvbuf as all pending
583 * requests are still in that buffer. sndbuf must
584 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700585 * for one reply per thread. We count all threads
586 * rather than threads in a particular pool, which
587 * provides an upper bound on the number of threads
588 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
590 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700591 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
592 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Tom Tucker02fc6c32007-12-30 21:07:48 -0600594 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
NeilBrown05ed6902007-05-09 02:34:55 -0700595 skb = NULL;
596 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
597 0, 0, MSG_PEEK | MSG_DONTWAIT);
598 if (err >= 0)
599 skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err);
600
601 if (skb == NULL) {
602 if (err != -EAGAIN) {
603 /* possibly an icmp error */
604 dprintk("svc: recvfrom returned error %d\n", -err);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600605 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -0400607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600609 len = svc_addr_len(svc_addr(rqstp));
Tom Tucker9dbc2402007-12-30 21:08:12 -0600610 rqstp->rq_addrlen = len;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700611 if (skb->tstamp.tv64 == 0) {
612 skb->tstamp = ktime_get_real();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800613 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 need that much accuracy */
615 }
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700616 svsk->sk_sk->sk_stamp = skb->tstamp;
Tom Tucker02fc6c32007-12-30 21:07:48 -0600617 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Willem de Bruijn1da8c6812016-04-07 11:44:58 -0400619 len = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 rqstp->rq_arg.len = len;
621
Chuck Lever95756482007-02-12 00:53:38 -0800622 rqstp->rq_prot = IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Chuck Lever7702ce42009-07-13 10:54:26 -0400624 if (!svc_udp_get_dest_address(rqstp, cmh)) {
Joe Perchese87cc472012-05-13 21:56:26 +0000625 net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
626 cmh->cmsg_level, cmh->cmsg_type);
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400627 goto out_free;
NeilBrown7a37f572007-03-06 01:42:21 -0800628 }
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800629 rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 if (skb_is_nonlinear(skb)) {
632 /* we have to copy */
633 local_bh_disable();
634 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
635 local_bh_enable();
636 /* checksum error */
Eric Dumazet22911fc2012-06-27 00:23:44 +0000637 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639 local_bh_enable();
Eric Dumazet9d410c72009-10-30 05:03:53 +0000640 skb_free_datagram_locked(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 } else {
642 /* we can use it in-place */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -0400643 rqstp->rq_arg.head[0].iov_base = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 rqstp->rq_arg.head[0].iov_len = len;
Eric Dumazet22911fc2012-06-27 00:23:44 +0000645 if (skb_checksum_complete(skb))
646 goto out_free;
Tom Tucker5148bf42007-12-30 21:07:25 -0600647 rqstp->rq_xprt_ctxt = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
650 rqstp->rq_arg.page_base = 0;
651 if (len <= rqstp->rq_arg.head[0].iov_len) {
652 rqstp->rq_arg.head[0].iov_len = len;
653 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700654 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 } else {
656 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700657 rqstp->rq_respages = rqstp->rq_pages + 1 +
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700658 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500660 rqstp->rq_next_page = rqstp->rq_respages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (serv->sv_stats)
663 serv->sv_stats->netudpcnt++;
664
665 return len;
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400666out_free:
667 trace_kfree_skb(skb, svc_udp_recvfrom);
668 skb_free_datagram_locked(svsk->sk_sk, skb);
669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672static int
673svc_udp_sendto(struct svc_rqst *rqstp)
674{
675 int error;
676
677 error = svc_sendto(rqstp, &rqstp->rq_res);
678 if (error == -ECONNREFUSED)
679 /* ICMP error on earlier request. */
680 error = svc_sendto(rqstp, &rqstp->rq_res);
681
682 return error;
683}
684
Tom Tuckere831fe62007-12-30 21:07:29 -0600685static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
686{
687}
688
Tom Tucker323bee32007-12-30 21:07:31 -0600689static int svc_udp_has_wspace(struct svc_xprt *xprt)
690{
691 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600692 struct svc_serv *serv = xprt->xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -0600693 unsigned long required;
694
695 /*
696 * Set the SOCK_NOSPACE flag before checking the available
697 * sock space.
698 */
699 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Tom Tucker7a90e8c2007-12-30 21:07:55 -0600700 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
Tom Tucker323bee32007-12-30 21:07:31 -0600701 if (required*2 > sock_wspace(svsk->sk_sk))
702 return 0;
703 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
704 return 1;
705}
706
Tom Tucker38a417c2007-12-30 21:07:36 -0600707static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
708{
709 BUG();
710 return NULL;
711}
712
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600713static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400714 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600715 struct sockaddr *sa, int salen,
716 int flags)
717{
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400718 return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600719}
720
Tom Tucker360d87382007-12-30 21:07:17 -0600721static struct svc_xprt_ops svc_udp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600722 .xpo_create = svc_udp_create,
Tom Tucker5d137992007-12-30 21:07:23 -0600723 .xpo_recvfrom = svc_udp_recvfrom,
724 .xpo_sendto = svc_udp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -0600725 .xpo_release_rqst = svc_release_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -0600726 .xpo_detach = svc_sock_detach,
727 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -0600728 .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -0600729 .xpo_has_wspace = svc_udp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -0600730 .xpo_accept = svc_udp_accept,
Chuck Lever16e4d932014-05-19 13:40:22 -0400731 .xpo_secure_port = svc_sock_secure_port,
Tom Tucker360d87382007-12-30 21:07:17 -0600732};
733
734static struct svc_xprt_class svc_udp_class = {
735 .xcl_name = "udp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600736 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -0600737 .xcl_ops = &svc_udp_ops,
Tom Tucker49023152007-12-30 21:07:21 -0600738 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
Chuck Lever3c45ddf2014-07-16 15:38:32 -0400739 .xcl_ident = XPRT_TRANSPORT_UDP,
Tom Tucker360d87382007-12-30 21:07:17 -0600740};
741
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600742static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Chuck Lever7702ce42009-07-13 10:54:26 -0400744 int err, level, optname, one = 1;
NeilBrown7a37f572007-03-06 01:42:21 -0800745
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +0300746 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
747 &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600748 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
750 svsk->sk_sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800753 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * svc_udp_recvfrom will re-adjust if necessary
755 */
756 svc_sock_setbufsize(svsk->sk_sock,
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600757 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
758 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Tom Tucker0f0257e2007-12-30 21:08:27 -0600760 /* data might have come in before data_ready set up */
761 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600762 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800763
NeilBrown7a37f572007-03-06 01:42:21 -0800764 /* make sure we get destination address info */
Chuck Lever7702ce42009-07-13 10:54:26 -0400765 switch (svsk->sk_sk->sk_family) {
766 case AF_INET:
767 level = SOL_IP;
768 optname = IP_PKTINFO;
769 break;
770 case AF_INET6:
771 level = SOL_IPV6;
772 optname = IPV6_RECVPKTINFO;
773 break;
774 default:
775 BUG();
776 }
777 err = kernel_setsockopt(svsk->sk_sock, level, optname,
778 (char *)&one, sizeof(one));
779 dprintk("svc: kernel_setsockopt returned %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782/*
783 * A data_ready event on a listening socket means there's a connection
784 * pending. Do not use state_change as a substitute for it.
785 */
David S. Miller676d2362014-04-11 16:15:36 -0400786static void svc_tcp_listen_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Neil Brown939bb7e2005-09-13 01:25:39 -0700788 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000789 wait_queue_head_t *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700792 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Neil Brown939bb7e2005-09-13 01:25:39 -0700794 /*
795 * This callback may called twice when a new connection
796 * is established as a child socket inherits everything
797 * from a parent LISTEN socket.
798 * 1) data_ready method of the parent socket will be called
799 * when one of child sockets become ESTABLISHED.
800 * 2) data_ready method of the child socket may be called
801 * when it receives data before the socket is accepted.
802 * In case of 2, we should ignore it silently.
803 */
804 if (sk->sk_state == TCP_LISTEN) {
805 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600806 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600807 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700808 } else
809 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Neil Brown939bb7e2005-09-13 01:25:39 -0700811
Eric Dumazeteaefd112011-02-18 03:26:36 +0000812 wq = sk_sleep(sk);
J. Bruce Fields0442f142015-10-09 01:44:07 +0000813 if (sunrpc_waitqueue_active(wq))
Eric Dumazeteaefd112011-02-18 03:26:36 +0000814 wake_up_interruptible_all(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
817/*
818 * A state change on a connected socket means it's dying or dead.
819 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600820static void svc_tcp_state_change(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Neil Brown939bb7e2005-09-13 01:25:39 -0700822 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000823 wait_queue_head_t *wq = sk_sleep(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700826 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Neil Brown939bb7e2005-09-13 01:25:39 -0700828 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700830 else {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600831 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600832 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
J. Bruce Fields0442f142015-10-09 01:44:07 +0000834 if (sunrpc_waitqueue_active(wq))
Eric Dumazeteaefd112011-02-18 03:26:36 +0000835 wake_up_interruptible_all(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
David S. Miller676d2362014-04-11 16:15:36 -0400838static void svc_tcp_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Neil Brown939bb7e2005-09-13 01:25:39 -0700840 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000841 wait_queue_head_t *wq = sk_sleep(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700844 sk, sk->sk_user_data);
845 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600846 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600847 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700848 }
J. Bruce Fields0442f142015-10-09 01:44:07 +0000849 if (sunrpc_waitqueue_active(wq))
Eric Dumazeteaefd112011-02-18 03:26:36 +0000850 wake_up_interruptible(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853/*
854 * Accept a TCP connection
855 */
Tom Tucker38a417c2007-12-30 21:07:36 -0600856static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Tom Tucker38a417c2007-12-30 21:07:36 -0600858 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800859 struct sockaddr_storage addr;
860 struct sockaddr *sin = (struct sockaddr *) &addr;
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600861 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 struct socket *sock = svsk->sk_sock;
863 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 struct svc_sock *newsvsk;
865 int err, slen;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +0300866 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
869 if (!sock)
Tom Tucker38a417c2007-12-30 21:07:36 -0600870 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Tom Tucker02fc6c32007-12-30 21:07:48 -0600872 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700873 err = kernel_accept(sock, &newsock, O_NONBLOCK);
874 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (err == -ENOMEM)
876 printk(KERN_WARNING "%s: no more sockets!\n",
877 serv->sv_name);
Joe Perchese87cc472012-05-13 21:56:26 +0000878 else if (err != -EAGAIN)
879 net_warn_ratelimited("%s: accept failed (err %d)!\n",
880 serv->sv_name, -err);
Tom Tucker38a417c2007-12-30 21:07:36 -0600881 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
Tom Tucker02fc6c32007-12-30 21:07:48 -0600883 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800885 err = kernel_getpeername(newsock, sin, &slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (err < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +0000887 net_warn_ratelimited("%s: peername failed (err %d)!\n",
888 serv->sv_name, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 goto failed; /* aborted connection or whatever */
890 }
891
892 /* Ideally, we would want to reject connections from unauthorized
Chuck Leverad06e4b2007-02-12 00:53:32 -0800893 * hosts here, but when we get encryption, the IP of the host won't
894 * tell us anything. For now just warn about unpriv connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800896 if (!svc_port_is_privileged(sin)) {
Kinglong Meea48fd0f2014-05-29 12:18:52 +0800897 dprintk("%s: connect from unprivileged port: %s\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800898 serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800899 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Chuck Leverad06e4b2007-02-12 00:53:32 -0800901 dprintk("%s: connect from %s\n", serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800902 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 /* make sure that a write doesn't block forever when
905 * low on memory
906 */
907 newsock->sk->sk_sndtimeo = HZ*30;
908
J. Bruce Fields72c35372012-08-13 17:46:17 -0400909 newsvsk = svc_setup_socket(serv, newsock,
910 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
911 if (IS_ERR(newsvsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 goto failed;
Tom Tucker9dbc2402007-12-30 21:08:12 -0600913 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
Frank van Maarseveena9747692007-07-09 22:21:39 +0200914 err = kernel_getsockname(newsock, sin, &slen);
915 if (unlikely(err < 0)) {
916 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
917 slen = offsetof(struct sockaddr, sa_data);
918 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600919 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -0800920
NeilBrownef11ce22014-05-12 11:22:47 +1000921 if (sock_is_loopback(newsock->sk))
922 set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
923 else
924 clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600925 if (serv->sv_stats)
926 serv->sv_stats->nettcpconn++;
927
928 return &newsvsk->sk_xprt;
929
930failed:
931 sock_release(newsock);
932 return NULL;
933}
934
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800935static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
936{
937 unsigned int i, len, npages;
938
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500939 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800940 return 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500941 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800942 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
943 for (i = 0; i < npages; i++) {
944 if (rqstp->rq_pages[i] != NULL)
945 put_page(rqstp->rq_pages[i]);
946 BUG_ON(svsk->sk_pages[i] == NULL);
947 rqstp->rq_pages[i] = svsk->sk_pages[i];
948 svsk->sk_pages[i] = NULL;
949 }
950 rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
951 return len;
952}
953
954static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
955{
956 unsigned int i, len, npages;
957
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500958 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800959 return;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500960 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800961 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
962 for (i = 0; i < npages; i++) {
963 svsk->sk_pages[i] = rqstp->rq_pages[i];
964 rqstp->rq_pages[i] = NULL;
965 }
966}
967
968static void svc_tcp_clear_pages(struct svc_sock *svsk)
969{
970 unsigned int i, len, npages;
971
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500972 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800973 goto out;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500974 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800975 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
976 for (i = 0; i < npages; i++) {
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -0400977 if (svsk->sk_pages[i] == NULL) {
978 WARN_ON_ONCE(1);
979 continue;
980 }
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800981 put_page(svsk->sk_pages[i]);
982 svsk->sk_pages[i] = NULL;
983 }
984out:
985 svsk->sk_tcplen = 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500986 svsk->sk_datalen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800987}
988
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600989/*
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -0500990 * Receive fragment record header.
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300991 * If we haven't gotten the record length yet, get the next four bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 */
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300993static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600995 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400996 unsigned int want;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300997 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Chuck Leverc0401ea2008-04-14 12:27:30 -0400999 if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 struct kvec iov;
1001
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001002 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
1004 iov.iov_len = want;
1005 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
1006 goto error;
1007 svsk->sk_tcplen += len;
1008
1009 if (len < want) {
Chuck Leverc0401ea2008-04-14 12:27:30 -04001010 dprintk("svc: short recvfrom while reading record "
1011 "length (%d of %d)\n", len, want);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001012 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
J. Bruce Fieldscc248d42012-12-03 16:11:13 -05001015 dprintk("svc: TCP record, %d bytes\n", svc_sock_reclen(svsk));
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001016 if (svc_sock_reclen(svsk) + svsk->sk_datalen >
1017 serv->sv_max_mesg) {
J. Bruce Fields3a28e332012-12-10 18:15:07 -05001018 net_notice_ratelimited("RPC: fragment too large: %d\n",
1019 svc_sock_reclen(svsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 goto err_delete;
1021 }
1022 }
1023
J. Bruce Fieldscc248d42012-12-03 16:11:13 -05001024 return svc_sock_reclen(svsk);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001025error:
1026 dprintk("RPC: TCP recv_record got %d\n", len);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001027 return len;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001028err_delete:
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001029 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001030 return -EAGAIN;
1031}
1032
Trond Myklebust586c52c2009-05-18 17:47:56 -04001033static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001034{
Trond Myklebust586c52c2009-05-18 17:47:56 -04001035 struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001036 struct rpc_rqst *req = NULL;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001037 struct kvec *src, *dst;
1038 __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
J. Bruce Fields48e65552011-02-14 14:52:03 -05001039 __be32 xid;
1040 __be32 calldir;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001041
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001042 xid = *p++;
1043 calldir = *p;
1044
Trond Myklebust093a1462014-11-12 18:04:04 -05001045 if (!bc_xprt)
Trond Myklebust586c52c2009-05-18 17:47:56 -04001046 return -EAGAIN;
Trond Myklebust093a1462014-11-12 18:04:04 -05001047 spin_lock_bh(&bc_xprt->transport_lock);
1048 req = xprt_lookup_rqst(bc_xprt, xid);
1049 if (!req)
1050 goto unlock_notfound;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001051
1052 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
1053 /*
1054 * XXX!: cheating for now! Only copying HEAD.
1055 * But we know this is good enough for now (in fact, for any
1056 * callback reply in the forseeable future).
1057 */
1058 dst = &req->rq_private_buf.head[0];
1059 src = &rqstp->rq_arg.head[0];
1060 if (dst->iov_len < src->iov_len)
Trond Myklebust093a1462014-11-12 18:04:04 -05001061 goto unlock_eagain; /* whatever; just giving up. */
Trond Myklebust586c52c2009-05-18 17:47:56 -04001062 memcpy(dst->iov_base, src->iov_base, src->iov_len);
J. Bruce Fieldscc248d42012-12-03 16:11:13 -05001063 xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len);
Trond Myklebust586c52c2009-05-18 17:47:56 -04001064 rqstp->rq_arg.len = 0;
Trond Myklebust093a1462014-11-12 18:04:04 -05001065 spin_unlock_bh(&bc_xprt->transport_lock);
Trond Myklebust586c52c2009-05-18 17:47:56 -04001066 return 0;
Trond Myklebust093a1462014-11-12 18:04:04 -05001067unlock_notfound:
1068 printk(KERN_NOTICE
1069 "%s: Got unrecognized reply: "
1070 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
1071 __func__, ntohl(calldir),
1072 bc_xprt, ntohl(xid));
1073unlock_eagain:
1074 spin_unlock_bh(&bc_xprt->transport_lock);
1075 return -EAGAIN;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001076}
1077
1078static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len)
1079{
1080 int i = 0;
1081 int t = 0;
1082
1083 while (t < len) {
1084 vec[i].iov_base = page_address(pages[i]);
1085 vec[i].iov_len = PAGE_SIZE;
1086 i++;
1087 t += PAGE_SIZE;
1088 }
1089 return i;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001090}
1091
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001092static void svc_tcp_fragment_received(struct svc_sock *svsk)
1093{
1094 /* If we have more data, signal svc_xprt_enqueue() to try again */
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001095 dprintk("svc: TCP %s record (%d bytes)\n",
1096 svc_sock_final_rec(svsk) ? "final" : "nonfinal",
1097 svc_sock_reclen(svsk));
1098 svsk->sk_tcplen = 0;
1099 svsk->sk_reclen = 0;
1100}
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001101
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001102/*
1103 * Receive data from a TCP socket.
1104 */
1105static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
1106{
1107 struct svc_sock *svsk =
1108 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
1109 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1110 int len;
1111 struct kvec *vec;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001112 unsigned int want, base;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001113 __be32 *p;
1114 __be32 calldir;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001115 int pnum;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001116
1117 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1118 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
1119 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
1120 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
1121
1122 len = svc_tcp_recv_record(svsk, rqstp);
1123 if (len < 0)
1124 goto error;
1125
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001126 base = svc_tcp_restore_pages(svsk, rqstp);
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001127 want = svc_sock_reclen(svsk) - (svsk->sk_tcplen - sizeof(rpc_fraghdr));
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001128
NeilBrown3cc03b12006-10-04 02:15:47 -07001129 vec = rqstp->rq_vec;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001130
Trond Myklebust586c52c2009-05-18 17:47:56 -04001131 pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0],
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001132 svsk->sk_datalen + want);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001133
NeilBrown44524352006-10-04 02:15:46 -07001134 rqstp->rq_respages = &rqstp->rq_pages[pnum];
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001135 rqstp->rq_next_page = rqstp->rq_respages + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 /* Now receive data */
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001138 len = svc_partial_recvfrom(rqstp, vec, pnum, want, base);
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001139 if (len >= 0) {
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001140 svsk->sk_tcplen += len;
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001141 svsk->sk_datalen += len;
1142 }
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001143 if (len != want || !svc_sock_final_rec(svsk)) {
J. Bruce Fieldsbe1e4442012-08-09 18:12:28 -04001144 svc_tcp_save_pages(svsk, rqstp);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001145 if (len < 0 && len != -EAGAIN)
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001146 goto err_delete;
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001147 if (len == want)
1148 svc_tcp_fragment_received(svsk);
1149 else
J. Bruce Fields3a28e332012-12-10 18:15:07 -05001150 dprintk("svc: incomplete TCP record (%d of %d)\n",
1151 (int)(svsk->sk_tcplen - sizeof(rpc_fraghdr)),
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001152 svc_sock_reclen(svsk));
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001153 goto err_noclose;
1154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
J. Bruce Fields1f691b02013-06-26 10:55:40 -04001156 if (svsk->sk_datalen < 8) {
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -04001157 svsk->sk_datalen = 0;
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001158 goto err_delete; /* client is nuts. */
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -04001159 }
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001160
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001161 rqstp->rq_arg.len = svsk->sk_datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 rqstp->rq_arg.page_base = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001163 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1164 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 rqstp->rq_arg.page_len = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001166 } else
1167 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Tom Tucker5148bf42007-12-30 21:07:25 -06001169 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 rqstp->rq_prot = IPPROTO_TCP;
Jeff Layton7501cc22014-11-19 07:51:15 -05001171 if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags))
1172 set_bit(RQ_LOCAL, &rqstp->rq_flags);
1173 else
1174 clear_bit(RQ_LOCAL, &rqstp->rq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Trond Myklebust586c52c2009-05-18 17:47:56 -04001176 p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1177 calldir = p[1];
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001178 if (calldir)
Trond Myklebust586c52c2009-05-18 17:47:56 -04001179 len = receive_cb_reply(svsk, rqstp);
Trond Myklebust586c52c2009-05-18 17:47:56 -04001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* Reset TCP read info */
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001182 svsk->sk_datalen = 0;
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001183 svc_tcp_fragment_received(svsk);
Trond Myklebust0601f792009-05-18 17:47:56 -04001184
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001185 if (len < 0)
1186 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Tom Tucker9dbc2402007-12-30 21:08:12 -06001188 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (serv->sv_stats)
1190 serv->sv_stats->nettcpcnt++;
1191
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001192 return rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001194error:
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001195 if (len != -EAGAIN)
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001196 goto err_delete;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001197 dprintk("RPC: TCP recvfrom got EAGAIN\n");
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -04001198 return 0;
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001199err_delete:
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001200 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1201 svsk->sk_xprt.xpt_server->sv_name, -len);
1202 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1203err_noclose:
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -04001204 return 0; /* record not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
1207/*
1208 * Send out data on TCP socket.
1209 */
Tom Tucker0f0257e2007-12-30 21:08:27 -06001210static int svc_tcp_sendto(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 struct xdr_buf *xbufp = &rqstp->rq_res;
1213 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001214 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 /* Set up the first element of the reply kvec.
1217 * Any other kvecs that may be in use have been taken
1218 * care of by the server implementation itself.
1219 */
1220 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1221 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 sent = svc_sendto(rqstp, &rqstp->rq_res);
1224 if (sent != xbufp->len) {
Tom Tucker0f0257e2007-12-30 21:08:27 -06001225 printk(KERN_NOTICE
1226 "rpc-srv/tcp: %s: %s %d when sending %d bytes "
1227 "- shutting down socket\n",
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001228 rqstp->rq_xprt->xpt_server->sv_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 (sent<0)?"got error":"sent only",
1230 sent, xbufp->len);
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001231 set_bit(XPT_CLOSE, &rqstp->rq_xprt->xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -06001232 svc_xprt_enqueue(rqstp->rq_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 sent = -EAGAIN;
1234 }
1235 return sent;
1236}
1237
Tom Tuckere831fe62007-12-30 21:07:29 -06001238/*
1239 * Setup response header. TCP has a 4B record length field.
1240 */
1241static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
1242{
1243 struct kvec *resv = &rqstp->rq_res.head[0];
1244
1245 /* tcp needs a space for the record length... */
1246 svc_putnl(resv, 0);
1247}
1248
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001249static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001250 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001251 struct sockaddr *sa, int salen,
1252 int flags)
1253{
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001254 return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001255}
1256
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001257#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Andy Adamson1f11a032011-01-06 02:04:26 +00001258static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
1259 struct net *, struct sockaddr *,
1260 int, int);
1261static void svc_bc_sock_free(struct svc_xprt *xprt);
1262
1263static struct svc_xprt *svc_bc_tcp_create(struct svc_serv *serv,
1264 struct net *net,
1265 struct sockaddr *sa, int salen,
1266 int flags)
1267{
1268 return svc_bc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1269}
1270
1271static void svc_bc_tcp_sock_detach(struct svc_xprt *xprt)
1272{
1273}
1274
1275static struct svc_xprt_ops svc_tcp_bc_ops = {
1276 .xpo_create = svc_bc_tcp_create,
1277 .xpo_detach = svc_bc_tcp_sock_detach,
1278 .xpo_free = svc_bc_sock_free,
1279 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
Chuck Lever16e4d932014-05-19 13:40:22 -04001280 .xpo_secure_port = svc_sock_secure_port,
Andy Adamson1f11a032011-01-06 02:04:26 +00001281};
1282
1283static struct svc_xprt_class svc_tcp_bc_class = {
1284 .xcl_name = "tcp-bc",
1285 .xcl_owner = THIS_MODULE,
1286 .xcl_ops = &svc_tcp_bc_ops,
1287 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1288};
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001289
1290static void svc_init_bc_xprt_sock(void)
1291{
1292 svc_reg_xprt_class(&svc_tcp_bc_class);
1293}
1294
1295static void svc_cleanup_bc_xprt_sock(void)
1296{
1297 svc_unreg_xprt_class(&svc_tcp_bc_class);
1298}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001299#else /* CONFIG_SUNRPC_BACKCHANNEL */
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001300static void svc_init_bc_xprt_sock(void)
1301{
1302}
1303
1304static void svc_cleanup_bc_xprt_sock(void)
1305{
1306}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001307#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Andy Adamson1f11a032011-01-06 02:04:26 +00001308
Tom Tucker360d87382007-12-30 21:07:17 -06001309static struct svc_xprt_ops svc_tcp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001310 .xpo_create = svc_tcp_create,
Tom Tucker5d137992007-12-30 21:07:23 -06001311 .xpo_recvfrom = svc_tcp_recvfrom,
1312 .xpo_sendto = svc_tcp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -06001313 .xpo_release_rqst = svc_release_skb,
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001314 .xpo_detach = svc_tcp_sock_detach,
Tom Tucker755ccea2007-12-30 21:07:27 -06001315 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -06001316 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -06001317 .xpo_has_wspace = svc_tcp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -06001318 .xpo_accept = svc_tcp_accept,
Chuck Lever16e4d932014-05-19 13:40:22 -04001319 .xpo_secure_port = svc_sock_secure_port,
Trond Myklebust51877682014-07-24 23:59:33 -04001320 .xpo_adjust_wspace = svc_tcp_adjust_wspace,
Tom Tucker360d87382007-12-30 21:07:17 -06001321};
1322
1323static struct svc_xprt_class svc_tcp_class = {
1324 .xcl_name = "tcp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001325 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -06001326 .xcl_ops = &svc_tcp_ops,
Tom Tucker49023152007-12-30 21:07:21 -06001327 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
Chuck Lever3c45ddf2014-07-16 15:38:32 -04001328 .xcl_ident = XPRT_TRANSPORT_TCP,
Tom Tucker360d87382007-12-30 21:07:17 -06001329};
1330
1331void svc_init_xprt_sock(void)
1332{
1333 svc_reg_xprt_class(&svc_tcp_class);
1334 svc_reg_xprt_class(&svc_udp_class);
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001335 svc_init_bc_xprt_sock();
Tom Tucker360d87382007-12-30 21:07:17 -06001336}
1337
1338void svc_cleanup_xprt_sock(void)
1339{
1340 svc_unreg_xprt_class(&svc_tcp_class);
1341 svc_unreg_xprt_class(&svc_udp_class);
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001342 svc_cleanup_bc_xprt_sock();
Tom Tucker360d87382007-12-30 21:07:17 -06001343}
1344
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001345static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 struct sock *sk = svsk->sk_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +03001349 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
1350 &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -06001351 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (sk->sk_state == TCP_LISTEN) {
1353 dprintk("setting up TCP socket for listening\n");
Tom Tucker02fc6c32007-12-30 21:07:48 -06001354 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 sk->sk_data_ready = svc_tcp_listen_data_ready;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001356 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 } else {
1358 dprintk("setting up TCP socket for reading\n");
1359 sk->sk_state_change = svc_tcp_state_change;
1360 sk->sk_data_ready = svc_tcp_data_ready;
Trond Myklebust47fcb032009-05-18 17:47:56 -04001361 sk->sk_write_space = svc_tcp_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 svsk->sk_reclen = 0;
1364 svsk->sk_tcplen = 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001365 svsk->sk_datalen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001366 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Chuck Leverb7872fe2008-04-14 12:27:01 -04001368 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Tom Tucker02fc6c32007-12-30 21:07:48 -06001370 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001371 if (sk->sk_state != TCP_ESTABLISHED)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001372 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374}
1375
Tom Tucker0f0257e2007-12-30 21:08:27 -06001376void svc_sock_update_bufs(struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 /*
1379 * The number of server threads has changed. Update
1380 * rcvbuf and sndbuf accordingly on all sockets
1381 */
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001382 struct svc_sock *svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 spin_lock_bh(&serv->sv_lock);
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001385 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001386 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 spin_unlock_bh(&serv->sv_lock);
1388}
Trond Myklebust24c37672008-12-23 16:30:12 -05001389EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 * Initialize socket for RPC use and create svc_sock struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 */
Chuck Lever6b174332007-02-12 00:53:28 -08001394static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1395 struct socket *sock,
J. Bruce Fields72c35372012-08-13 17:46:17 -04001396 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 struct svc_sock *svsk;
1399 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001400 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001401 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 dprintk("svc: svc_setup_socket %p\n", sock);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001404 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1405 if (!svsk)
1406 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 inet = sock->sk;
1409
1410 /* Register socket with portmapper */
J. Bruce Fields72c35372012-08-13 17:46:17 -04001411 if (pmap_register)
1412 err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001413 inet->sk_protocol,
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001414 ntohs(inet_sk(inet)->inet_sport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
J. Bruce Fields72c35372012-08-13 17:46:17 -04001416 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 kfree(svsk);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001418 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 inet->sk_user_data = svsk;
1422 svsk->sk_sock = sock;
1423 svsk->sk_sk = inet;
1424 svsk->sk_ostate = inet->sk_state_change;
1425 svsk->sk_odata = inet->sk_data_ready;
1426 svsk->sk_owspace = inet->sk_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 /* Initialize the socket */
1429 if (sock->type == SOCK_DGRAM)
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001430 svc_udp_init(svsk, serv);
Olga Kornievskaia96604392008-10-21 14:13:47 -04001431 else {
1432 /* initialise setting must have enough space to
1433 * receive and respond to one request.
1434 */
1435 svc_sock_setbufsize(svsk->sk_sock, 4 * serv->sv_max_mesg,
1436 4 * serv->sv_max_mesg);
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001437 svc_tcp_init(svsk, serv);
Olga Kornievskaia96604392008-10-21 14:13:47 -04001438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1441 svsk, svsk->sk_sk);
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return svsk;
1444}
1445
Stanislav Kinsbursky30646392014-02-26 16:50:01 +03001446bool svc_alien_sock(struct net *net, int fd)
1447{
1448 int err;
1449 struct socket *sock = sockfd_lookup(fd, &err);
1450 bool ret = false;
1451
1452 if (!sock)
1453 goto out;
1454 if (sock_net(sock->sk) != net)
1455 ret = true;
1456 sockfd_put(sock);
1457out:
1458 return ret;
1459}
1460EXPORT_SYMBOL_GPL(svc_alien_sock);
1461
Chuck Leverbfba9ab2009-04-23 19:32:33 -04001462/**
1463 * svc_addsock - add a listener socket to an RPC service
1464 * @serv: pointer to RPC service to which to add a new listener
1465 * @fd: file descriptor of the new listener
1466 * @name_return: pointer to buffer to fill in with name of listener
1467 * @len: size of the buffer
1468 *
1469 * Fills in socket name and returns positive length of name if successful.
1470 * Name is terminated with '\n'. On error, returns a negative errno
1471 * value.
1472 */
1473int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
1474 const size_t len)
NeilBrownb41b66d2006-10-02 02:17:48 -07001475{
1476 int err = 0;
1477 struct socket *so = sockfd_lookup(fd, &err);
1478 struct svc_sock *svsk = NULL;
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001479 struct sockaddr_storage addr;
1480 struct sockaddr *sin = (struct sockaddr *)&addr;
1481 int salen;
NeilBrownb41b66d2006-10-02 02:17:48 -07001482
1483 if (!so)
1484 return err;
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001485 err = -EAFNOSUPPORT;
Aime Le Rouzic205ba422010-01-26 14:03:56 -05001486 if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001487 goto out;
1488 err = -EPROTONOSUPPORT;
1489 if (so->sk->sk_protocol != IPPROTO_TCP &&
NeilBrownb41b66d2006-10-02 02:17:48 -07001490 so->sk->sk_protocol != IPPROTO_UDP)
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001491 goto out;
1492 err = -EISCONN;
1493 if (so->state > SS_UNCONNECTED)
1494 goto out;
1495 err = -ENOENT;
1496 if (!try_module_get(THIS_MODULE))
1497 goto out;
1498 svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1499 if (IS_ERR(svsk)) {
1500 module_put(THIS_MODULE);
1501 err = PTR_ERR(svsk);
1502 goto out;
NeilBrownb41b66d2006-10-02 02:17:48 -07001503 }
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001504 if (kernel_getsockname(svsk->sk_sock, sin, &salen) == 0)
1505 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
J. Bruce Fields39b55302012-08-14 15:50:34 -04001506 svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
Chuck Levere7942b92009-04-23 19:32:48 -04001507 return svc_one_sock_name(svsk, name_return, len);
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001508out:
1509 sockfd_put(so);
1510 return err;
NeilBrownb41b66d2006-10-02 02:17:48 -07001511}
1512EXPORT_SYMBOL_GPL(svc_addsock);
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514/*
1515 * Create socket for RPC service.
1516 */
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001517static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1518 int protocol,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001519 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001520 struct sockaddr *sin, int len,
1521 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 struct svc_sock *svsk;
1524 struct socket *sock;
1525 int error;
1526 int type;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001527 struct sockaddr_storage addr;
1528 struct sockaddr *newsin = (struct sockaddr *)&addr;
1529 int newlen;
Trond Myklebustc69da772009-03-30 18:59:17 -04001530 int family;
1531 int val;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +03001532 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Chuck Leverad06e4b2007-02-12 00:53:32 -08001534 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1535 serv->sv_program->pg_name, protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001536 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1539 printk(KERN_WARNING "svc: only UDP and TCP "
1540 "sockets supported\n");
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001541 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Trond Myklebustc69da772009-03-30 18:59:17 -04001544 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1545 switch (sin->sa_family) {
1546 case AF_INET6:
1547 family = PF_INET6;
1548 break;
1549 case AF_INET:
1550 family = PF_INET;
1551 break;
1552 default:
1553 return ERR_PTR(-EINVAL);
1554 }
1555
Pavel Emelyanov14ec63c2010-09-29 16:06:57 +04001556 error = __sock_create(net, family, type, protocol, &sock, 1);
Chuck Lever77f1f672007-02-12 00:53:39 -08001557 if (error < 0)
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001558 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Peter Zijlstraed075362006-12-06 20:35:24 -08001560 svc_reclassify_socket(sock);
1561
Trond Myklebustc69da772009-03-30 18:59:17 -04001562 /*
1563 * If this is an PF_INET6 listener, we want to avoid
1564 * getting requests from IPv4 remotes. Those should
1565 * be shunted to a PF_INET listener via rpcbind.
1566 */
1567 val = 1;
1568 if (family == PF_INET6)
1569 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
1570 (char *)&val, sizeof(val));
1571
Eric Sesterhenn18114742006-09-28 14:37:07 -07001572 if (type == SOCK_STREAM)
Pavel Emelyanov4a17fd52012-04-19 03:39:36 +00001573 sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
Chuck Lever77f1f672007-02-12 00:53:39 -08001574 error = kernel_bind(sock, sin, len);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001575 if (error < 0)
1576 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Tom Tucker9dbc2402007-12-30 21:08:12 -06001578 newlen = len;
1579 error = kernel_getsockname(sock, newsin, &newlen);
1580 if (error < 0)
1581 goto bummer;
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001584 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 goto bummer;
1586 }
1587
J. Bruce Fields72c35372012-08-13 17:46:17 -04001588 svsk = svc_setup_socket(serv, sock, flags);
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001589 if (IS_ERR(svsk)) {
1590 error = PTR_ERR(svsk);
1591 goto bummer;
NeilBrowne79eff12007-02-12 00:53:30 -08001592 }
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001593 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1594 return (struct svc_xprt *)svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595bummer:
1596 dprintk("svc: svc_create_socket error = %d\n", -error);
1597 sock_release(sock);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001598 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
1601/*
Tom Tucker755ccea2007-12-30 21:07:27 -06001602 * Detach the svc_sock from the socket so that no
1603 * more callbacks occur.
1604 */
1605static void svc_sock_detach(struct svc_xprt *xprt)
1606{
1607 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1608 struct sock *sk = svsk->sk_sk;
Eric Dumazeteaefd112011-02-18 03:26:36 +00001609 wait_queue_head_t *wq;
Tom Tucker755ccea2007-12-30 21:07:27 -06001610
1611 dprintk("svc: svc_sock_detach(%p)\n", svsk);
1612
1613 /* put back the old socket callbacks */
1614 sk->sk_state_change = svsk->sk_ostate;
1615 sk->sk_data_ready = svsk->sk_odata;
1616 sk->sk_write_space = svsk->sk_owspace;
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001617
Eric Dumazeteaefd112011-02-18 03:26:36 +00001618 wq = sk_sleep(sk);
J. Bruce Fields0442f142015-10-09 01:44:07 +00001619 if (sunrpc_waitqueue_active(wq))
Eric Dumazeteaefd112011-02-18 03:26:36 +00001620 wake_up_interruptible(wq);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001621}
1622
1623/*
1624 * Disconnect the socket, and reset the callbacks
1625 */
1626static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1627{
1628 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1629
1630 dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk);
1631
1632 svc_sock_detach(xprt);
1633
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001634 if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1635 svc_tcp_clear_pages(svsk);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001636 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001637 }
Tom Tucker755ccea2007-12-30 21:07:27 -06001638}
1639
1640/*
1641 * Free the svc_sock's socket resources and the svc_sock itself.
1642 */
1643static void svc_sock_free(struct svc_xprt *xprt)
1644{
1645 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1646 dprintk("svc: svc_sock_free(%p)\n", svsk);
1647
Tom Tucker755ccea2007-12-30 21:07:27 -06001648 if (svsk->sk_sock->file)
1649 sockfd_put(svsk->sk_sock);
1650 else
1651 sock_release(svsk->sk_sock);
1652 kfree(svsk);
1653}
Benny Halevy7652e5a2009-04-01 09:23:09 -04001654
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001655#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001656/*
Andy Adamson1f11a032011-01-06 02:04:26 +00001657 * Create a back channel svc_xprt which shares the fore channel socket.
Benny Halevy7652e5a2009-04-01 09:23:09 -04001658 */
Andy Adamson1f11a032011-01-06 02:04:26 +00001659static struct svc_xprt *svc_bc_create_socket(struct svc_serv *serv,
1660 int protocol,
1661 struct net *net,
1662 struct sockaddr *sin, int len,
1663 int flags)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001664{
1665 struct svc_sock *svsk;
Andy Adamson1f11a032011-01-06 02:04:26 +00001666 struct svc_xprt *xprt;
Benny Halevy7652e5a2009-04-01 09:23:09 -04001667
Andy Adamson1f11a032011-01-06 02:04:26 +00001668 if (protocol != IPPROTO_TCP) {
1669 printk(KERN_WARNING "svc: only TCP sockets"
1670 " supported on shared back channel\n");
1671 return ERR_PTR(-EINVAL);
1672 }
1673
Benny Halevy7652e5a2009-04-01 09:23:09 -04001674 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1675 if (!svsk)
Andy Adamson1f11a032011-01-06 02:04:26 +00001676 return ERR_PTR(-ENOMEM);
Benny Halevy7652e5a2009-04-01 09:23:09 -04001677
1678 xprt = &svsk->sk_xprt;
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +03001679 svc_xprt_init(net, &svc_tcp_bc_class, xprt, serv);
Andy Adamson1f11a032011-01-06 02:04:26 +00001680
Andy Adamson4a19de02011-01-06 02:04:35 +00001681 serv->sv_bc_xprt = xprt;
Andy Adamson1f11a032011-01-06 02:04:26 +00001682
Benny Halevy7652e5a2009-04-01 09:23:09 -04001683 return xprt;
1684}
Benny Halevy7652e5a2009-04-01 09:23:09 -04001685
1686/*
Andy Adamson1f11a032011-01-06 02:04:26 +00001687 * Free a back channel svc_sock.
Benny Halevy7652e5a2009-04-01 09:23:09 -04001688 */
Andy Adamson1f11a032011-01-06 02:04:26 +00001689static void svc_bc_sock_free(struct svc_xprt *xprt)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001690{
Andy Adamson778be232011-01-25 15:38:01 +00001691 if (xprt)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001692 kfree(container_of(xprt, struct svc_sock, sk_xprt));
1693}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001694#endif /* CONFIG_SUNRPC_BACKCHANNEL */