blob: 43590a968b734b41b89c55f9fa72366ad1cb01f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/svcsock.c
3 *
4 * These are the RPC server socket internals.
5 *
6 * The server scheduling algorithm does not always distribute the load
7 * evenly when servicing a single client. May need to modify the
Tom Tuckerf6150c32007-12-30 21:07:57 -06008 * svc_xprt_enqueue procedure...
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * TCP support is largely untested and may be a little slow. The problem
11 * is that we currently do two separate recvfrom's, one for the 4-byte
12 * record length, and the second for the actual record. This could possibly
13 * be improved by always reading a minimum size of around 100 bytes and
14 * tucking any superfluous bytes away in a temporary store. Still, that
15 * leaves write requests out in the rain. An alternative may be to peek at
16 * the first skb in the queue, and if it matches the next TCP sequence
17 * number, to extract the record marker. Yuck.
18 *
19 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
20 */
21
Ilpo Järvinen172589c2007-08-28 15:50:33 -070022#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/sched.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040024#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/errno.h>
26#include <linux/fcntl.h>
27#include <linux/net.h>
28#include <linux/in.h>
29#include <linux/inet.h>
30#include <linux/udp.h>
Andrew Morton91483c42005-08-09 20:20:07 -070031#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/unistd.h>
33#include <linux/slab.h>
34#include <linux/netdevice.h>
35#include <linux/skbuff.h>
NeilBrownb41b66d2006-10-02 02:17:48 -070036#include <linux/file.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080037#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <net/sock.h>
39#include <net/checksum.h>
40#include <net/ip.h>
Chuck Leverb92503b2007-02-12 00:53:36 -080041#include <net/ipv6.h>
Paolo Abeni850cbad2016-10-21 13:55:47 +020042#include <net/udp.h>
Chuck Leverb7872fe2008-04-14 12:27:01 -040043#include <net/tcp.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070044#include <net/tcp_states.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080045#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/ioctls.h>
Eric Dumazet22911fc2012-06-27 00:23:44 +000047#include <trace/events/skb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <linux/sunrpc/types.h>
Chuck Leverad06e4b2007-02-12 00:53:32 -080050#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/sunrpc/xdr.h>
Chuck Leverc0401ea2008-04-14 12:27:30 -040052#include <linux/sunrpc/msg_prot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/sunrpc/svcsock.h>
54#include <linux/sunrpc/stats.h>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030055#include <linux/sunrpc/xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
H Hartley Sweeten177e4f92011-06-20 17:54:51 -070057#include "sunrpc.h"
58
Tom Tucker360d87382007-12-30 21:07:17 -060059#define RPCDBG_FACILITY RPCDBG_SVCXPRT
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61
62static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
J. Bruce Fields72c35372012-08-13 17:46:17 -040063 int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int svc_udp_recvfrom(struct svc_rqst *);
65static int svc_udp_sendto(struct svc_rqst *);
Tom Tucker755ccea2007-12-30 21:07:27 -060066static void svc_sock_detach(struct svc_xprt *);
Trond Myklebust69b6ba32008-12-23 16:30:11 -050067static void svc_tcp_sock_detach(struct svc_xprt *);
Tom Tucker755ccea2007-12-30 21:07:27 -060068static void svc_sock_free(struct svc_xprt *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Tom Tuckerb700cbb2007-12-30 21:07:42 -060070static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
Pavel Emelyanov62832c02010-09-29 16:04:18 +040071 struct net *, struct sockaddr *,
72 int, int);
Peter Zijlstraed075362006-12-06 20:35:24 -080073#ifdef CONFIG_DEBUG_LOCK_ALLOC
74static struct lock_class_key svc_key[2];
75static struct lock_class_key svc_slock_key[2];
76
Tom Tucker0f0257e2007-12-30 21:08:27 -060077static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -080078{
79 struct sock *sk = sock->sk;
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -040080
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +020081 if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -040082 return;
83
Peter Zijlstraed075362006-12-06 20:35:24 -080084 switch (sk->sk_family) {
85 case AF_INET:
86 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -060087 &svc_slock_key[0],
88 "sk_xprt.xpt_lock-AF_INET-NFSD",
89 &svc_key[0]);
Peter Zijlstraed075362006-12-06 20:35:24 -080090 break;
91
92 case AF_INET6:
93 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -060094 &svc_slock_key[1],
95 "sk_xprt.xpt_lock-AF_INET6-NFSD",
96 &svc_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -080097 break;
98
99 default:
100 BUG();
101 }
102}
103#else
Tom Tucker0f0257e2007-12-30 21:08:27 -0600104static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -0800105{
106}
107#endif
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * Release an skbuff after use
111 */
Tom Tucker5148bf42007-12-30 21:07:25 -0600112static void svc_release_skb(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Tom Tucker5148bf42007-12-30 21:07:25 -0600114 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (skb) {
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600117 struct svc_sock *svsk =
118 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tucker5148bf42007-12-30 21:07:25 -0600119 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
Eric Dumazet9d410c72009-10-30 05:03:53 +0000122 skb_free_datagram_locked(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Paolo Abeni850cbad2016-10-21 13:55:47 +0200126static void svc_release_udp_skb(struct svc_rqst *rqstp)
127{
128 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
129
130 if (skb) {
131 rqstp->rq_xprt_ctxt = NULL;
132
133 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
134 consume_skb(skb);
135 }
136}
137
Chuck Leverb92503b2007-02-12 00:53:36 -0800138union svc_pktinfo_u {
139 struct in_pktinfo pkti;
Chuck Leverb92503b2007-02-12 00:53:36 -0800140 struct in6_pktinfo pkti6;
Chuck Leverb92503b2007-02-12 00:53:36 -0800141};
David S. Millerbc375ea2007-04-12 13:35:59 -0700142#define SVC_PKTINFO_SPACE \
143 CMSG_SPACE(sizeof(union svc_pktinfo_u))
Chuck Leverb92503b2007-02-12 00:53:36 -0800144
145static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
146{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600147 struct svc_sock *svsk =
148 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
149 switch (svsk->sk_sk->sk_family) {
Chuck Leverb92503b2007-02-12 00:53:36 -0800150 case AF_INET: {
151 struct in_pktinfo *pki = CMSG_DATA(cmh);
152
153 cmh->cmsg_level = SOL_IP;
154 cmh->cmsg_type = IP_PKTINFO;
155 pki->ipi_ifindex = 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800156 pki->ipi_spec_dst.s_addr =
157 svc_daddr_in(rqstp)->sin_addr.s_addr;
Chuck Leverb92503b2007-02-12 00:53:36 -0800158 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
159 }
160 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800161
Chuck Leverb92503b2007-02-12 00:53:36 -0800162 case AF_INET6: {
163 struct in6_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800164 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
Chuck Leverb92503b2007-02-12 00:53:36 -0800165
166 cmh->cmsg_level = SOL_IPV6;
167 cmh->cmsg_type = IPV6_PKTINFO;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800168 pki->ipi6_ifindex = daddr->sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000169 pki->ipi6_addr = daddr->sin6_addr;
Chuck Leverb92503b2007-02-12 00:53:36 -0800170 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
171 }
172 break;
Chuck Leverb92503b2007-02-12 00:53:36 -0800173 }
Chuck Leverb92503b2007-02-12 00:53:36 -0800174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/*
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300177 * send routine intended to be shared by the fore- and back-channel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300179int svc_send_common(struct socket *sock, struct xdr_buf *xdr,
180 struct page *headpage, unsigned long headoffset,
181 struct page *tailpage, unsigned long tailoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int result;
184 int size;
185 struct page **ppage = xdr->pages;
186 size_t base = xdr->page_base;
187 unsigned int pglen = xdr->page_len;
Trond Myklebust226453d2015-10-07 13:54:42 -0400188 unsigned int flags = MSG_MORE | MSG_SENDPAGE_NOTLAST;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300189 int slen;
190 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 slen = xdr->len;
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* send head */
195 if (slen == xdr->head[0].iov_len)
196 flags = 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300197 len = kernel_sendpage(sock, headpage, headoffset,
NeilBrown44524352006-10-04 02:15:46 -0700198 xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (len != xdr->head[0].iov_len)
200 goto out;
201 slen -= xdr->head[0].iov_len;
202 if (slen == 0)
203 goto out;
204
205 /* send page data */
206 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
207 while (pglen > 0) {
208 if (slen == size)
209 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700210 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (result > 0)
212 len += result;
213 if (result != size)
214 goto out;
215 slen -= size;
216 pglen -= size;
217 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
218 base = 0;
219 ppage++;
220 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* send tail */
223 if (xdr->tail[0].iov_len) {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300224 result = kernel_sendpage(sock, tailpage, tailoffset,
225 xdr->tail[0].iov_len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (result > 0)
227 len += result;
228 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300229
230out:
231 return len;
232}
233
234
235/*
236 * Generic sendto routine
237 */
238static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
239{
240 struct svc_sock *svsk =
241 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
242 struct socket *sock = svsk->sk_sock;
243 union {
244 struct cmsghdr hdr;
245 long all[SVC_PKTINFO_SPACE / sizeof(long)];
246 } buffer;
247 struct cmsghdr *cmh = &buffer.hdr;
248 int len = 0;
249 unsigned long tailoff;
250 unsigned long headoff;
251 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
252
253 if (rqstp->rq_prot == IPPROTO_UDP) {
254 struct msghdr msg = {
255 .msg_name = &rqstp->rq_addr,
256 .msg_namelen = rqstp->rq_addrlen,
257 .msg_control = cmh,
258 .msg_controllen = sizeof(buffer),
259 .msg_flags = MSG_MORE,
260 };
261
262 svc_set_cmsg_data(rqstp, cmh);
263
Al Virod8725c82014-12-11 00:02:50 -0500264 if (sock_sendmsg(sock, &msg) < 0)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300265 goto out;
266 }
267
268 tailoff = ((unsigned long)xdr->tail[0].iov_base) & (PAGE_SIZE-1);
269 headoff = 0;
270 len = svc_send_common(sock, xdr, rqstp->rq_respages[0], headoff,
271 rqstp->rq_respages[0], tailoff);
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273out:
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800274 dprintk("svc: socket %p sendto([%p %zu... ], %d) = %d (addr %s)\n",
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600275 svsk, xdr->head[0].iov_base, xdr->head[0].iov_len,
Chuck Leverad06e4b2007-02-12 00:53:32 -0800276 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 return len;
279}
280
281/*
NeilBrown80212d52006-10-02 02:17:47 -0700282 * Report socket names for nfsdfs
283 */
Chuck Levere7942b92009-04-23 19:32:48 -0400284static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
NeilBrown80212d52006-10-02 02:17:47 -0700285{
Chuck Lever017cb472009-04-23 19:33:03 -0400286 const struct sock *sk = svsk->sk_sk;
287 const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
288 "udp" : "tcp";
NeilBrown80212d52006-10-02 02:17:47 -0700289 int len;
290
Chuck Lever017cb472009-04-23 19:33:03 -0400291 switch (sk->sk_family) {
Chuck Levere7942b92009-04-23 19:32:48 -0400292 case PF_INET:
293 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400294 proto_name,
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000295 &inet_sk(sk)->inet_rcv_saddr,
296 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700297 break;
Eric Dumazetc2bb06d2013-10-09 03:05:48 -0700298#if IS_ENABLED(CONFIG_IPV6)
Chuck Lever58de2f82009-04-23 19:32:55 -0400299 case PF_INET6:
300 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400301 proto_name,
Eric Dumazetefe42082013-10-03 15:42:29 -0700302 &sk->sk_v6_rcv_saddr,
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000303 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700304 break;
Eric Dumazetc2bb06d2013-10-09 03:05:48 -0700305#endif
NeilBrown80212d52006-10-02 02:17:47 -0700306 default:
Chuck Levere7942b92009-04-23 19:32:48 -0400307 len = snprintf(buf, remaining, "*unknown-%d*\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400308 sk->sk_family);
NeilBrown80212d52006-10-02 02:17:47 -0700309 }
Chuck Levere7942b92009-04-23 19:32:48 -0400310
311 if (len >= remaining) {
312 *buf = '\0';
313 return -ENAMETOOLONG;
NeilBrown80212d52006-10-02 02:17:47 -0700314 }
315 return len;
316}
317
NeilBrown80212d52006-10-02 02:17:47 -0700318/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * Generic recvfrom routine.
320 */
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400321static ssize_t svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov,
322 unsigned int nr, size_t buflen, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600324 struct svc_sock *svsk =
325 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400326 struct msghdr msg = { NULL };
327 ssize_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Tom Tucker260c1d12007-12-30 21:08:29 -0600329 rqstp->rq_xprt_hlen = 0;
330
Trond Myklebustf8d1ff42014-08-03 13:03:13 -0400331 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
David Howellsaa563d72018-10-20 00:57:56 +0100332 iov_iter_kvec(&msg.msg_iter, READ, iov, nr, buflen);
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400333 if (base != 0) {
334 iov_iter_advance(&msg.msg_iter, base);
335 buflen -= base;
336 }
337 len = sock_recvmsg(svsk->sk_sock, &msg, MSG_DONTWAIT);
Trond Myklebustf8d1ff42014-08-03 13:03:13 -0400338 /* If we read a full record, then assume there may be more
339 * data to read (stream based sockets only!)
340 */
341 if (len == buflen)
342 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400344 dprintk("svc: socket %p recvfrom(%p, %zu) = %zd\n",
Chuck Lever1ba95102007-02-12 00:53:31 -0800345 svsk, iov[0].iov_base, iov[0].iov_len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return len;
347}
348
349/*
350 * Set socket snd and rcv buffer lengths
351 */
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500352static void svc_sock_setbufsize(struct svc_sock *svsk, unsigned int nreqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500354 unsigned int max_mesg = svsk->sk_xprt.xpt_server->sv_max_mesg;
355 struct socket *sock = svsk->sk_sock;
356
357 nreqs = min(nreqs, INT_MAX / 2 / max_mesg);
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 lock_sock(sock->sk);
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500360 sock->sk->sk_sndbuf = nreqs * max_mesg * 2;
361 sock->sk->sk_rcvbuf = nreqs * max_mesg * 2;
Trond Myklebust47fcb032009-05-18 17:47:56 -0400362 sock->sk->sk_write_space(sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 release_sock(sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
Chuck Lever16e4d932014-05-19 13:40:22 -0400365
Chuck Lever989f8812018-03-27 10:49:38 -0400366static void svc_sock_secure_port(struct svc_rqst *rqstp)
Chuck Lever16e4d932014-05-19 13:40:22 -0400367{
Chuck Lever989f8812018-03-27 10:49:38 -0400368 if (svc_port_is_privileged(svc_addr(rqstp)))
369 set_bit(RQ_SECURE, &rqstp->rq_flags);
370 else
371 clear_bit(RQ_SECURE, &rqstp->rq_flags);
Chuck Lever16e4d932014-05-19 13:40:22 -0400372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/*
375 * INET callback when data has been received on the socket.
376 */
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400377static void svc_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Neil Brown939bb7e2005-09-13 01:25:39 -0700379 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Neil Brown939bb7e2005-09-13 01:25:39 -0700381 if (svsk) {
David S. Miller676d2362014-04-11 16:15:36 -0400382 dprintk("svc: socket %p(inet %p), busy=%d\n",
383 svsk, sk,
Tom Tucker02fc6c32007-12-30 21:07:48 -0600384 test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400385
386 /* Refer to svc_setup_socket() for details. */
387 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400388 svsk->sk_odata(sk);
Trond Myklebust4720b072016-06-24 10:55:49 -0400389 if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags))
390 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394/*
395 * INET callback when space is newly available on the socket.
396 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600397static void svc_write_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
400
401 if (svsk) {
402 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600403 svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400404
405 /* Refer to svc_setup_socket() for details. */
406 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400407 svsk->sk_owspace(sk);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600408 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Trond Myklebustc7fb3f02014-07-24 23:59:32 -0400412static int svc_tcp_has_wspace(struct svc_xprt *xprt)
413{
Trond Myklebust637600f2016-06-24 10:55:51 -0400414 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Trond Myklebustc7fb3f02014-07-24 23:59:32 -0400415
416 if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
417 return 1;
Trond Myklebust637600f2016-06-24 10:55:51 -0400418 return !test_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Trond Myklebust51877682014-07-24 23:59:33 -0400419}
420
Scott Mayhewea08e392016-11-11 13:16:22 -0500421static void svc_tcp_kill_temp_xprt(struct svc_xprt *xprt)
422{
423 struct svc_sock *svsk;
424 struct socket *sock;
425 struct linger no_linger = {
426 .l_onoff = 1,
427 .l_linger = 0,
428 };
429
430 svsk = container_of(xprt, struct svc_sock, sk_xprt);
431 sock = svsk->sk_sock;
432 kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER,
433 (char *)&no_linger, sizeof(no_linger));
434}
435
Tom Tucker9dbc2402007-12-30 21:08:12 -0600436/*
Chuck Lever7702ce42009-07-13 10:54:26 -0400437 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
438 */
439static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
440 struct cmsghdr *cmh)
441{
442 struct in_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800443 struct sockaddr_in *daddr = svc_daddr_in(rqstp);
444
Chuck Lever7702ce42009-07-13 10:54:26 -0400445 if (cmh->cmsg_type != IP_PKTINFO)
446 return 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800447
448 daddr->sin_family = AF_INET;
449 daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
Chuck Lever7702ce42009-07-13 10:54:26 -0400450 return 1;
451}
452
453/*
Tom Parkin73df66f2013-01-31 01:02:24 +0000454 * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
Chuck Lever7702ce42009-07-13 10:54:26 -0400455 */
456static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
457 struct cmsghdr *cmh)
458{
459 struct in6_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800460 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
461
Chuck Lever7702ce42009-07-13 10:54:26 -0400462 if (cmh->cmsg_type != IPV6_PKTINFO)
463 return 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800464
465 daddr->sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000466 daddr->sin6_addr = pki->ipi6_addr;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800467 daddr->sin6_scope_id = pki->ipi6_ifindex;
Chuck Lever7702ce42009-07-13 10:54:26 -0400468 return 1;
469}
470
471/*
Tom Tucker9dbc2402007-12-30 21:08:12 -0600472 * Copy the UDP datagram's destination address to the rqstp structure.
473 * The 'destination' address in this case is the address to which the
474 * peer sent the datagram, i.e. our local address. For multihomed
475 * hosts, this can change from msg to msg. Note that only the IP
476 * address changes, the port number should remain the same.
477 */
Chuck Lever7702ce42009-07-13 10:54:26 -0400478static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
479 struct cmsghdr *cmh)
Chuck Lever95756482007-02-12 00:53:38 -0800480{
Chuck Lever7702ce42009-07-13 10:54:26 -0400481 switch (cmh->cmsg_level) {
482 case SOL_IP:
483 return svc_udp_get_dest_address4(rqstp, cmh);
484 case SOL_IPV6:
485 return svc_udp_get_dest_address6(rqstp, cmh);
Chuck Lever95756482007-02-12 00:53:38 -0800486 }
Chuck Lever7702ce42009-07-13 10:54:26 -0400487
488 return 0;
Chuck Lever95756482007-02-12 00:53:38 -0800489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/*
492 * Receive a datagram from a UDP socket.
493 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600494static int svc_udp_recvfrom(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600496 struct svc_sock *svsk =
497 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600498 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct sk_buff *skb;
David S. Millerbc375ea2007-04-12 13:35:59 -0700500 union {
501 struct cmsghdr hdr;
502 long all[SVC_PKTINFO_SPACE / sizeof(long)];
503 } buffer;
504 struct cmsghdr *cmh = &buffer.hdr;
NeilBrown7a37f572007-03-06 01:42:21 -0800505 struct msghdr msg = {
506 .msg_name = svc_addr(rqstp),
507 .msg_control = cmh,
508 .msg_controllen = sizeof(buffer),
509 .msg_flags = MSG_DONTWAIT,
510 };
Chuck Leverabc5c442009-04-23 19:31:25 -0400511 size_t len;
512 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Tom Tucker02fc6c32007-12-30 21:07:48 -0600514 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* udp sockets need large rcvbuf as all pending
516 * requests are still in that buffer. sndbuf must
517 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700518 * for one reply per thread. We count all threads
519 * rather than threads in a particular pool, which
520 * provides an upper bound on the number of threads
521 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 */
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500523 svc_sock_setbufsize(svsk, serv->sv_nrthreads + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Tom Tucker02fc6c32007-12-30 21:07:48 -0600525 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
NeilBrown05ed6902007-05-09 02:34:55 -0700526 skb = NULL;
527 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
528 0, 0, MSG_PEEK | MSG_DONTWAIT);
529 if (err >= 0)
Paolo Abeni7c13f972016-11-04 11:28:59 +0100530 skb = skb_recv_udp(svsk->sk_sk, 0, 1, &err);
NeilBrown05ed6902007-05-09 02:34:55 -0700531
532 if (skb == NULL) {
533 if (err != -EAGAIN) {
534 /* possibly an icmp error */
535 dprintk("svc: recvfrom returned error %d\n", -err);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600536 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -0400538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600540 len = svc_addr_len(svc_addr(rqstp));
Tom Tucker9dbc2402007-12-30 21:08:12 -0600541 rqstp->rq_addrlen = len;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100542 if (skb->tstamp == 0) {
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700543 skb->tstamp = ktime_get_real();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800544 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 need that much accuracy */
546 }
Deepa Dinamani3a0ed3e92018-12-27 18:55:09 -0800547 sock_write_timestamp(svsk->sk_sk, skb->tstamp);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600548 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Willem de Bruijn1da8c6812016-04-07 11:44:58 -0400550 len = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 rqstp->rq_arg.len = len;
552
Chuck Lever95756482007-02-12 00:53:38 -0800553 rqstp->rq_prot = IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Chuck Lever7702ce42009-07-13 10:54:26 -0400555 if (!svc_udp_get_dest_address(rqstp, cmh)) {
Joe Perchese87cc472012-05-13 21:56:26 +0000556 net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
557 cmh->cmsg_level, cmh->cmsg_type);
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400558 goto out_free;
NeilBrown7a37f572007-03-06 01:42:21 -0800559 }
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800560 rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 if (skb_is_nonlinear(skb)) {
563 /* we have to copy */
564 local_bh_disable();
565 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
566 local_bh_enable();
567 /* checksum error */
Eric Dumazet22911fc2012-06-27 00:23:44 +0000568 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570 local_bh_enable();
Paolo Abeni850cbad2016-10-21 13:55:47 +0200571 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 } else {
573 /* we can use it in-place */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -0400574 rqstp->rq_arg.head[0].iov_base = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 rqstp->rq_arg.head[0].iov_len = len;
Eric Dumazet22911fc2012-06-27 00:23:44 +0000576 if (skb_checksum_complete(skb))
577 goto out_free;
Tom Tucker5148bf42007-12-30 21:07:25 -0600578 rqstp->rq_xprt_ctxt = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
581 rqstp->rq_arg.page_base = 0;
582 if (len <= rqstp->rq_arg.head[0].iov_len) {
583 rqstp->rq_arg.head[0].iov_len = len;
584 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700585 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 } else {
587 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700588 rqstp->rq_respages = rqstp->rq_pages + 1 +
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700589 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500591 rqstp->rq_next_page = rqstp->rq_respages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 if (serv->sv_stats)
594 serv->sv_stats->netudpcnt++;
595
596 return len;
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400597out_free:
Paolo Abeni850cbad2016-10-21 13:55:47 +0200598 kfree_skb(skb);
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602static int
603svc_udp_sendto(struct svc_rqst *rqstp)
604{
605 int error;
606
607 error = svc_sendto(rqstp, &rqstp->rq_res);
608 if (error == -ECONNREFUSED)
609 /* ICMP error on earlier request. */
610 error = svc_sendto(rqstp, &rqstp->rq_res);
611
612 return error;
613}
614
Tom Tucker323bee32007-12-30 21:07:31 -0600615static int svc_udp_has_wspace(struct svc_xprt *xprt)
616{
617 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600618 struct svc_serv *serv = xprt->xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -0600619 unsigned long required;
620
621 /*
622 * Set the SOCK_NOSPACE flag before checking the available
623 * sock space.
624 */
625 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Tom Tucker7a90e8c2007-12-30 21:07:55 -0600626 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
Tom Tucker323bee32007-12-30 21:07:31 -0600627 if (required*2 > sock_wspace(svsk->sk_sk))
628 return 0;
629 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
630 return 1;
631}
632
Tom Tucker38a417c2007-12-30 21:07:36 -0600633static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
634{
635 BUG();
636 return NULL;
637}
638
Scott Mayhewea08e392016-11-11 13:16:22 -0500639static void svc_udp_kill_temp_xprt(struct svc_xprt *xprt)
640{
641}
642
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600643static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400644 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600645 struct sockaddr *sa, int salen,
646 int flags)
647{
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400648 return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600649}
650
Chuck Lever2412e922017-08-01 11:59:49 -0400651static const struct svc_xprt_ops svc_udp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600652 .xpo_create = svc_udp_create,
Tom Tucker5d137992007-12-30 21:07:23 -0600653 .xpo_recvfrom = svc_udp_recvfrom,
654 .xpo_sendto = svc_udp_sendto,
Paolo Abeni850cbad2016-10-21 13:55:47 +0200655 .xpo_release_rqst = svc_release_udp_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -0600656 .xpo_detach = svc_sock_detach,
657 .xpo_free = svc_sock_free,
Tom Tucker323bee32007-12-30 21:07:31 -0600658 .xpo_has_wspace = svc_udp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -0600659 .xpo_accept = svc_udp_accept,
Chuck Lever16e4d932014-05-19 13:40:22 -0400660 .xpo_secure_port = svc_sock_secure_port,
Scott Mayhewea08e392016-11-11 13:16:22 -0500661 .xpo_kill_temp_xprt = svc_udp_kill_temp_xprt,
Tom Tucker360d87382007-12-30 21:07:17 -0600662};
663
664static struct svc_xprt_class svc_udp_class = {
665 .xcl_name = "udp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600666 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -0600667 .xcl_ops = &svc_udp_ops,
Tom Tucker49023152007-12-30 21:07:21 -0600668 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
Chuck Lever3c45ddf2014-07-16 15:38:32 -0400669 .xcl_ident = XPRT_TRANSPORT_UDP,
Tom Tucker360d87382007-12-30 21:07:17 -0600670};
671
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600672static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Chuck Lever7702ce42009-07-13 10:54:26 -0400674 int err, level, optname, one = 1;
NeilBrown7a37f572007-03-06 01:42:21 -0800675
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +0300676 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
677 &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600678 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400679 svsk->sk_sk->sk_data_ready = svc_data_ready;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 svsk->sk_sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800683 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 * svc_udp_recvfrom will re-adjust if necessary
685 */
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500686 svc_sock_setbufsize(svsk, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Tom Tucker0f0257e2007-12-30 21:08:27 -0600688 /* data might have come in before data_ready set up */
689 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600690 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800691
NeilBrown7a37f572007-03-06 01:42:21 -0800692 /* make sure we get destination address info */
Chuck Lever7702ce42009-07-13 10:54:26 -0400693 switch (svsk->sk_sk->sk_family) {
694 case AF_INET:
695 level = SOL_IP;
696 optname = IP_PKTINFO;
697 break;
698 case AF_INET6:
699 level = SOL_IPV6;
700 optname = IPV6_RECVPKTINFO;
701 break;
702 default:
703 BUG();
704 }
705 err = kernel_setsockopt(svsk->sk_sock, level, optname,
706 (char *)&one, sizeof(one));
707 dprintk("svc: kernel_setsockopt returned %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710/*
711 * A data_ready event on a listening socket means there's a connection
712 * pending. Do not use state_change as a substitute for it.
713 */
David S. Miller676d2362014-04-11 16:15:36 -0400714static void svc_tcp_listen_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Neil Brown939bb7e2005-09-13 01:25:39 -0700716 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700719 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400721 if (svsk) {
722 /* Refer to svc_setup_socket() for details. */
723 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400724 svsk->sk_odata(sk);
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400725 }
726
Neil Brown939bb7e2005-09-13 01:25:39 -0700727 /*
728 * This callback may called twice when a new connection
729 * is established as a child socket inherits everything
730 * from a parent LISTEN socket.
731 * 1) data_ready method of the parent socket will be called
732 * when one of child sockets become ESTABLISHED.
733 * 2) data_ready method of the child socket may be called
734 * when it receives data before the socket is accepted.
735 * In case of 2, we should ignore it silently.
736 */
737 if (sk->sk_state == TCP_LISTEN) {
738 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600739 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600740 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700741 } else
742 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
746/*
747 * A state change on a connected socket means it's dying or dead.
748 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600749static void svc_tcp_state_change(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Neil Brown939bb7e2005-09-13 01:25:39 -0700751 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700754 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Neil Brown939bb7e2005-09-13 01:25:39 -0700756 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700758 else {
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400759 /* Refer to svc_setup_socket() for details. */
760 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400761 svsk->sk_ostate(sk);
Trond Myklebustb2f21f72016-07-26 09:51:19 -0400762 if (sk->sk_state != TCP_ESTABLISHED) {
763 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
764 svc_xprt_enqueue(&svsk->sk_xprt);
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769/*
770 * Accept a TCP connection
771 */
Tom Tucker38a417c2007-12-30 21:07:36 -0600772static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Tom Tucker38a417c2007-12-30 21:07:36 -0600774 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800775 struct sockaddr_storage addr;
776 struct sockaddr *sin = (struct sockaddr *) &addr;
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600777 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct socket *sock = svsk->sk_sock;
779 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct svc_sock *newsvsk;
781 int err, slen;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +0300782 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
785 if (!sock)
Tom Tucker38a417c2007-12-30 21:07:36 -0600786 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Tom Tucker02fc6c32007-12-30 21:07:48 -0600788 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700789 err = kernel_accept(sock, &newsock, O_NONBLOCK);
790 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (err == -ENOMEM)
792 printk(KERN_WARNING "%s: no more sockets!\n",
793 serv->sv_name);
Joe Perchese87cc472012-05-13 21:56:26 +0000794 else if (err != -EAGAIN)
795 net_warn_ratelimited("%s: accept failed (err %d)!\n",
796 serv->sv_name, -err);
Tom Tucker38a417c2007-12-30 21:07:36 -0600797 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Tom Tucker02fc6c32007-12-30 21:07:48 -0600799 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100801 err = kernel_getpeername(newsock, sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (err < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +0000803 net_warn_ratelimited("%s: peername failed (err %d)!\n",
804 serv->sv_name, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 goto failed; /* aborted connection or whatever */
806 }
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100807 slen = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /* Ideally, we would want to reject connections from unauthorized
Chuck Leverad06e4b2007-02-12 00:53:32 -0800810 * hosts here, but when we get encryption, the IP of the host won't
811 * tell us anything. For now just warn about unpriv connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800813 if (!svc_port_is_privileged(sin)) {
Kinglong Meea48fd0f2014-05-29 12:18:52 +0800814 dprintk("%s: connect from unprivileged port: %s\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800815 serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800816 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
Chuck Leverad06e4b2007-02-12 00:53:32 -0800818 dprintk("%s: connect from %s\n", 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
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400821 /* Reset the inherited callbacks before calling svc_setup_socket */
822 newsock->sk->sk_state_change = svsk->sk_ostate;
823 newsock->sk->sk_data_ready = svsk->sk_odata;
824 newsock->sk->sk_write_space = svsk->sk_owspace;
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* make sure that a write doesn't block forever when
827 * low on memory
828 */
829 newsock->sk->sk_sndtimeo = HZ*30;
830
J. Bruce Fields72c35372012-08-13 17:46:17 -0400831 newsvsk = svc_setup_socket(serv, newsock,
832 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
833 if (IS_ERR(newsvsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 goto failed;
Tom Tucker9dbc2402007-12-30 21:08:12 -0600835 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100836 err = kernel_getsockname(newsock, sin);
837 slen = err;
Frank van Maarseveena9747692007-07-09 22:21:39 +0200838 if (unlikely(err < 0)) {
839 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
840 slen = offsetof(struct sockaddr, sa_data);
841 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600842 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -0800843
NeilBrownef11ce22014-05-12 11:22:47 +1000844 if (sock_is_loopback(newsock->sk))
845 set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
846 else
847 clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600848 if (serv->sv_stats)
849 serv->sv_stats->nettcpconn++;
850
851 return &newsvsk->sk_xprt;
852
853failed:
854 sock_release(newsock);
855 return NULL;
856}
857
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800858static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
859{
860 unsigned int i, len, npages;
861
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500862 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800863 return 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500864 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800865 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
866 for (i = 0; i < npages; i++) {
867 if (rqstp->rq_pages[i] != NULL)
868 put_page(rqstp->rq_pages[i]);
869 BUG_ON(svsk->sk_pages[i] == NULL);
870 rqstp->rq_pages[i] = svsk->sk_pages[i];
871 svsk->sk_pages[i] = NULL;
872 }
873 rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
874 return len;
875}
876
877static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
878{
879 unsigned int i, len, npages;
880
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500881 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800882 return;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500883 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800884 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
885 for (i = 0; i < npages; i++) {
886 svsk->sk_pages[i] = rqstp->rq_pages[i];
887 rqstp->rq_pages[i] = NULL;
888 }
889}
890
891static void svc_tcp_clear_pages(struct svc_sock *svsk)
892{
893 unsigned int i, len, npages;
894
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500895 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800896 goto out;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500897 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800898 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
899 for (i = 0; i < npages; i++) {
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -0400900 if (svsk->sk_pages[i] == NULL) {
901 WARN_ON_ONCE(1);
902 continue;
903 }
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800904 put_page(svsk->sk_pages[i]);
905 svsk->sk_pages[i] = NULL;
906 }
907out:
908 svsk->sk_tcplen = 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500909 svsk->sk_datalen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800910}
911
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600912/*
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -0500913 * Receive fragment record header.
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300914 * If we haven't gotten the record length yet, get the next four bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 */
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300916static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600918 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400919 unsigned int want;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300920 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Chuck Leverc0401ea2008-04-14 12:27:30 -0400922 if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct kvec iov;
924
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400925 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
927 iov.iov_len = want;
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400928 len = svc_recvfrom(rqstp, &iov, 1, want, 0);
929 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 goto error;
931 svsk->sk_tcplen += len;
932
933 if (len < want) {
Chuck Leverc0401ea2008-04-14 12:27:30 -0400934 dprintk("svc: short recvfrom while reading record "
935 "length (%d of %d)\n", len, want);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800936 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500939 dprintk("svc: TCP record, %d bytes\n", svc_sock_reclen(svsk));
J. Bruce Fields836fbad2012-12-03 15:50:38 -0500940 if (svc_sock_reclen(svsk) + svsk->sk_datalen >
941 serv->sv_max_mesg) {
J. Bruce Fields3a28e332012-12-10 18:15:07 -0500942 net_notice_ratelimited("RPC: fragment too large: %d\n",
943 svc_sock_reclen(svsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 goto err_delete;
945 }
946 }
947
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500948 return svc_sock_reclen(svsk);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800949error:
950 dprintk("RPC: TCP recv_record got %d\n", len);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300951 return len;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800952err_delete:
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300953 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300954 return -EAGAIN;
955}
956
Trond Myklebust586c52c2009-05-18 17:47:56 -0400957static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300958{
Trond Myklebust586c52c2009-05-18 17:47:56 -0400959 struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300960 struct rpc_rqst *req = NULL;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400961 struct kvec *src, *dst;
962 __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
J. Bruce Fields48e65552011-02-14 14:52:03 -0500963 __be32 xid;
964 __be32 calldir;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300965
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300966 xid = *p++;
967 calldir = *p;
968
Trond Myklebust093a1462014-11-12 18:04:04 -0500969 if (!bc_xprt)
Trond Myklebust586c52c2009-05-18 17:47:56 -0400970 return -EAGAIN;
Trond Myklebust75c84152018-08-31 10:21:00 -0400971 spin_lock(&bc_xprt->queue_lock);
Trond Myklebust093a1462014-11-12 18:04:04 -0500972 req = xprt_lookup_rqst(bc_xprt, xid);
973 if (!req)
974 goto unlock_notfound;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400975
976 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
977 /*
978 * XXX!: cheating for now! Only copying HEAD.
979 * But we know this is good enough for now (in fact, for any
980 * callback reply in the forseeable future).
981 */
982 dst = &req->rq_private_buf.head[0];
983 src = &rqstp->rq_arg.head[0];
984 if (dst->iov_len < src->iov_len)
Trond Myklebust093a1462014-11-12 18:04:04 -0500985 goto unlock_eagain; /* whatever; just giving up. */
Trond Myklebust586c52c2009-05-18 17:47:56 -0400986 memcpy(dst->iov_base, src->iov_base, src->iov_len);
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500987 xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len);
Trond Myklebust586c52c2009-05-18 17:47:56 -0400988 rqstp->rq_arg.len = 0;
Trond Myklebust75c84152018-08-31 10:21:00 -0400989 spin_unlock(&bc_xprt->queue_lock);
Trond Myklebust586c52c2009-05-18 17:47:56 -0400990 return 0;
Trond Myklebust093a1462014-11-12 18:04:04 -0500991unlock_notfound:
992 printk(KERN_NOTICE
993 "%s: Got unrecognized reply: "
994 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
995 __func__, ntohl(calldir),
996 bc_xprt, ntohl(xid));
997unlock_eagain:
Trond Myklebust75c84152018-08-31 10:21:00 -0400998 spin_unlock(&bc_xprt->queue_lock);
Trond Myklebust093a1462014-11-12 18:04:04 -0500999 return -EAGAIN;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001000}
1001
1002static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len)
1003{
1004 int i = 0;
1005 int t = 0;
1006
1007 while (t < len) {
1008 vec[i].iov_base = page_address(pages[i]);
1009 vec[i].iov_len = PAGE_SIZE;
1010 i++;
1011 t += PAGE_SIZE;
1012 }
1013 return i;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001014}
1015
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001016static void svc_tcp_fragment_received(struct svc_sock *svsk)
1017{
1018 /* If we have more data, signal svc_xprt_enqueue() to try again */
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001019 dprintk("svc: TCP %s record (%d bytes)\n",
1020 svc_sock_final_rec(svsk) ? "final" : "nonfinal",
1021 svc_sock_reclen(svsk));
1022 svsk->sk_tcplen = 0;
1023 svsk->sk_reclen = 0;
1024}
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001025
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001026/*
1027 * Receive data from a TCP socket.
1028 */
1029static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
1030{
1031 struct svc_sock *svsk =
1032 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
1033 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1034 int len;
1035 struct kvec *vec;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001036 unsigned int want, base;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001037 __be32 *p;
1038 __be32 calldir;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001039 int pnum;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001040
1041 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1042 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
1043 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
1044 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
1045
1046 len = svc_tcp_recv_record(svsk, rqstp);
1047 if (len < 0)
1048 goto error;
1049
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001050 base = svc_tcp_restore_pages(svsk, rqstp);
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001051 want = svc_sock_reclen(svsk) - (svsk->sk_tcplen - sizeof(rpc_fraghdr));
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001052
NeilBrown3cc03b12006-10-04 02:15:47 -07001053 vec = rqstp->rq_vec;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001054
Trond Myklebust4c8e5532018-10-01 10:41:53 -04001055 pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0], base + want);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001056
NeilBrown44524352006-10-04 02:15:46 -07001057 rqstp->rq_respages = &rqstp->rq_pages[pnum];
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001058 rqstp->rq_next_page = rqstp->rq_respages + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 /* Now receive data */
Trond Myklebust4c8e5532018-10-01 10:41:53 -04001061 len = svc_recvfrom(rqstp, vec, pnum, base + want, base);
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001062 if (len >= 0) {
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001063 svsk->sk_tcplen += len;
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001064 svsk->sk_datalen += len;
1065 }
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001066 if (len != want || !svc_sock_final_rec(svsk)) {
J. Bruce Fieldsbe1e4442012-08-09 18:12:28 -04001067 svc_tcp_save_pages(svsk, rqstp);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001068 if (len < 0 && len != -EAGAIN)
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001069 goto err_delete;
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001070 if (len == want)
1071 svc_tcp_fragment_received(svsk);
1072 else
J. Bruce Fields3a28e332012-12-10 18:15:07 -05001073 dprintk("svc: incomplete TCP record (%d of %d)\n",
1074 (int)(svsk->sk_tcplen - sizeof(rpc_fraghdr)),
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001075 svc_sock_reclen(svsk));
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001076 goto err_noclose;
1077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
J. Bruce Fields1f691b02013-06-26 10:55:40 -04001079 if (svsk->sk_datalen < 8) {
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -04001080 svsk->sk_datalen = 0;
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001081 goto err_delete; /* client is nuts. */
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -04001082 }
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001083
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001084 rqstp->rq_arg.len = svsk->sk_datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 rqstp->rq_arg.page_base = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001086 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1087 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 rqstp->rq_arg.page_len = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001089 } else
1090 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Tom Tucker5148bf42007-12-30 21:07:25 -06001092 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 rqstp->rq_prot = IPPROTO_TCP;
Jeff Layton7501cc22014-11-19 07:51:15 -05001094 if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags))
1095 set_bit(RQ_LOCAL, &rqstp->rq_flags);
1096 else
1097 clear_bit(RQ_LOCAL, &rqstp->rq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Trond Myklebust586c52c2009-05-18 17:47:56 -04001099 p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1100 calldir = p[1];
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001101 if (calldir)
Trond Myklebust586c52c2009-05-18 17:47:56 -04001102 len = receive_cb_reply(svsk, rqstp);
Trond Myklebust586c52c2009-05-18 17:47:56 -04001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /* Reset TCP read info */
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001105 svsk->sk_datalen = 0;
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001106 svc_tcp_fragment_received(svsk);
Trond Myklebust0601f792009-05-18 17:47:56 -04001107
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001108 if (len < 0)
1109 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Tom Tucker9dbc2402007-12-30 21:08:12 -06001111 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (serv->sv_stats)
1113 serv->sv_stats->nettcpcnt++;
1114
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001115 return rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001117error:
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001118 if (len != -EAGAIN)
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001119 goto err_delete;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001120 dprintk("RPC: TCP recvfrom got EAGAIN\n");
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -04001121 return 0;
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001122err_delete:
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001123 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1124 svsk->sk_xprt.xpt_server->sv_name, -len);
1125 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1126err_noclose:
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -04001127 return 0; /* record not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130/*
1131 * Send out data on TCP socket.
1132 */
Tom Tucker0f0257e2007-12-30 21:08:27 -06001133static int svc_tcp_sendto(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 struct xdr_buf *xbufp = &rqstp->rq_res;
1136 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001137 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 /* Set up the first element of the reply kvec.
1140 * Any other kvecs that may be in use have been taken
1141 * care of by the server implementation itself.
1142 */
1143 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1144 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 sent = svc_sendto(rqstp, &rqstp->rq_res);
1147 if (sent != xbufp->len) {
Tom Tucker0f0257e2007-12-30 21:08:27 -06001148 printk(KERN_NOTICE
1149 "rpc-srv/tcp: %s: %s %d when sending %d bytes "
1150 "- shutting down socket\n",
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001151 rqstp->rq_xprt->xpt_server->sv_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 (sent<0)?"got error":"sent only",
1153 sent, xbufp->len);
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001154 set_bit(XPT_CLOSE, &rqstp->rq_xprt->xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -06001155 svc_xprt_enqueue(rqstp->rq_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 sent = -EAGAIN;
1157 }
1158 return sent;
1159}
1160
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001161static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001162 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001163 struct sockaddr *sa, int salen,
1164 int flags)
1165{
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001166 return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001167}
1168
Chuck Lever2412e922017-08-01 11:59:49 -04001169static const struct svc_xprt_ops svc_tcp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001170 .xpo_create = svc_tcp_create,
Tom Tucker5d137992007-12-30 21:07:23 -06001171 .xpo_recvfrom = svc_tcp_recvfrom,
1172 .xpo_sendto = svc_tcp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -06001173 .xpo_release_rqst = svc_release_skb,
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001174 .xpo_detach = svc_tcp_sock_detach,
Tom Tucker755ccea2007-12-30 21:07:27 -06001175 .xpo_free = svc_sock_free,
Tom Tucker323bee32007-12-30 21:07:31 -06001176 .xpo_has_wspace = svc_tcp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -06001177 .xpo_accept = svc_tcp_accept,
Chuck Lever16e4d932014-05-19 13:40:22 -04001178 .xpo_secure_port = svc_sock_secure_port,
Scott Mayhewea08e392016-11-11 13:16:22 -05001179 .xpo_kill_temp_xprt = svc_tcp_kill_temp_xprt,
Tom Tucker360d87382007-12-30 21:07:17 -06001180};
1181
1182static struct svc_xprt_class svc_tcp_class = {
1183 .xcl_name = "tcp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001184 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -06001185 .xcl_ops = &svc_tcp_ops,
Tom Tucker49023152007-12-30 21:07:21 -06001186 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
Chuck Lever3c45ddf2014-07-16 15:38:32 -04001187 .xcl_ident = XPRT_TRANSPORT_TCP,
Tom Tucker360d87382007-12-30 21:07:17 -06001188};
1189
1190void svc_init_xprt_sock(void)
1191{
1192 svc_reg_xprt_class(&svc_tcp_class);
1193 svc_reg_xprt_class(&svc_udp_class);
1194}
1195
1196void svc_cleanup_xprt_sock(void)
1197{
1198 svc_unreg_xprt_class(&svc_tcp_class);
1199 svc_unreg_xprt_class(&svc_udp_class);
1200}
1201
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001202static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 struct sock *sk = svsk->sk_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +03001206 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
1207 &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -06001208 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Jeff Layton362142b22017-02-24 13:25:23 -05001209 set_bit(XPT_CONG_CTRL, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (sk->sk_state == TCP_LISTEN) {
1211 dprintk("setting up TCP socket for listening\n");
Chuck Leverece200d2018-03-27 10:51:00 -04001212 strcpy(svsk->sk_xprt.xpt_remotebuf, "listener");
Tom Tucker02fc6c32007-12-30 21:07:48 -06001213 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 sk->sk_data_ready = svc_tcp_listen_data_ready;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001215 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 } else {
1217 dprintk("setting up TCP socket for reading\n");
1218 sk->sk_state_change = svc_tcp_state_change;
Trond Myklebustfa9251a2016-06-24 10:55:48 -04001219 sk->sk_data_ready = svc_data_ready;
Trond Myklebust637600f2016-06-24 10:55:51 -04001220 sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 svsk->sk_reclen = 0;
1223 svsk->sk_tcplen = 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001224 svsk->sk_datalen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001225 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Chuck Leverb7872fe2008-04-14 12:27:01 -04001227 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Tom Tucker02fc6c32007-12-30 21:07:48 -06001229 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Trond Myklebustb2f21f72016-07-26 09:51:19 -04001230 switch (sk->sk_state) {
1231 case TCP_SYN_RECV:
1232 case TCP_ESTABLISHED:
1233 break;
1234 default:
Tom Tucker02fc6c32007-12-30 21:07:48 -06001235 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Trond Myklebustb2f21f72016-07-26 09:51:19 -04001236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238}
1239
Tom Tucker0f0257e2007-12-30 21:08:27 -06001240void svc_sock_update_bufs(struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 /*
1243 * The number of server threads has changed. Update
1244 * rcvbuf and sndbuf accordingly on all sockets
1245 */
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001246 struct svc_sock *svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 spin_lock_bh(&serv->sv_lock);
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001249 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001250 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 spin_unlock_bh(&serv->sv_lock);
1252}
Trond Myklebust24c37672008-12-23 16:30:12 -05001253EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 * Initialize socket for RPC use and create svc_sock struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 */
Chuck Lever6b174332007-02-12 00:53:28 -08001258static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1259 struct socket *sock,
J. Bruce Fields72c35372012-08-13 17:46:17 -04001260 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 struct svc_sock *svsk;
1263 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001264 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001265 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 dprintk("svc: svc_setup_socket %p\n", sock);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001268 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1269 if (!svsk)
1270 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 inet = sock->sk;
1273
1274 /* Register socket with portmapper */
J. Bruce Fields72c35372012-08-13 17:46:17 -04001275 if (pmap_register)
1276 err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001277 inet->sk_protocol,
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001278 ntohs(inet_sk(inet)->inet_sport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
J. Bruce Fields72c35372012-08-13 17:46:17 -04001280 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 kfree(svsk);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001282 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 svsk->sk_sock = sock;
1286 svsk->sk_sk = inet;
1287 svsk->sk_ostate = inet->sk_state_change;
1288 svsk->sk_odata = inet->sk_data_ready;
1289 svsk->sk_owspace = inet->sk_write_space;
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -04001290 /*
1291 * This barrier is necessary in order to prevent race condition
1292 * with svc_data_ready(), svc_listen_data_ready() and others
1293 * when calling callbacks above.
1294 */
1295 wmb();
1296 inet->sk_user_data = svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 /* Initialize the socket */
1299 if (sock->type == SOCK_DGRAM)
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001300 svc_udp_init(svsk, serv);
Trond Myklebust637600f2016-06-24 10:55:51 -04001301 else
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001302 svc_tcp_init(svsk, serv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Trond Myklebustc7995f82016-07-26 09:51:20 -04001304 dprintk("svc: svc_setup_socket created %p (inet %p), "
1305 "listen %d close %d\n",
1306 svsk, svsk->sk_sk,
1307 test_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags),
1308 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return svsk;
1311}
1312
Stanislav Kinsbursky30646392014-02-26 16:50:01 +03001313bool svc_alien_sock(struct net *net, int fd)
1314{
1315 int err;
1316 struct socket *sock = sockfd_lookup(fd, &err);
1317 bool ret = false;
1318
1319 if (!sock)
1320 goto out;
1321 if (sock_net(sock->sk) != net)
1322 ret = true;
1323 sockfd_put(sock);
1324out:
1325 return ret;
1326}
1327EXPORT_SYMBOL_GPL(svc_alien_sock);
1328
Chuck Leverbfba9ab2009-04-23 19:32:33 -04001329/**
1330 * svc_addsock - add a listener socket to an RPC service
1331 * @serv: pointer to RPC service to which to add a new listener
1332 * @fd: file descriptor of the new listener
1333 * @name_return: pointer to buffer to fill in with name of listener
1334 * @len: size of the buffer
1335 *
1336 * Fills in socket name and returns positive length of name if successful.
1337 * Name is terminated with '\n'. On error, returns a negative errno
1338 * value.
1339 */
1340int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
1341 const size_t len)
NeilBrownb41b66d2006-10-02 02:17:48 -07001342{
1343 int err = 0;
1344 struct socket *so = sockfd_lookup(fd, &err);
1345 struct svc_sock *svsk = NULL;
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001346 struct sockaddr_storage addr;
1347 struct sockaddr *sin = (struct sockaddr *)&addr;
1348 int salen;
NeilBrownb41b66d2006-10-02 02:17:48 -07001349
1350 if (!so)
1351 return err;
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001352 err = -EAFNOSUPPORT;
Aime Le Rouzic205ba422010-01-26 14:03:56 -05001353 if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001354 goto out;
1355 err = -EPROTONOSUPPORT;
1356 if (so->sk->sk_protocol != IPPROTO_TCP &&
NeilBrownb41b66d2006-10-02 02:17:48 -07001357 so->sk->sk_protocol != IPPROTO_UDP)
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001358 goto out;
1359 err = -EISCONN;
1360 if (so->state > SS_UNCONNECTED)
1361 goto out;
1362 err = -ENOENT;
1363 if (!try_module_get(THIS_MODULE))
1364 goto out;
1365 svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1366 if (IS_ERR(svsk)) {
1367 module_put(THIS_MODULE);
1368 err = PTR_ERR(svsk);
1369 goto out;
NeilBrownb41b66d2006-10-02 02:17:48 -07001370 }
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001371 salen = kernel_getsockname(svsk->sk_sock, sin);
1372 if (salen >= 0)
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001373 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
J. Bruce Fields39b55302012-08-14 15:50:34 -04001374 svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
Chuck Levere7942b92009-04-23 19:32:48 -04001375 return svc_one_sock_name(svsk, name_return, len);
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001376out:
1377 sockfd_put(so);
1378 return err;
NeilBrownb41b66d2006-10-02 02:17:48 -07001379}
1380EXPORT_SYMBOL_GPL(svc_addsock);
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382/*
1383 * Create socket for RPC service.
1384 */
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001385static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1386 int protocol,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001387 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001388 struct sockaddr *sin, int len,
1389 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 struct svc_sock *svsk;
1392 struct socket *sock;
1393 int error;
1394 int type;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001395 struct sockaddr_storage addr;
1396 struct sockaddr *newsin = (struct sockaddr *)&addr;
1397 int newlen;
Trond Myklebustc69da772009-03-30 18:59:17 -04001398 int family;
1399 int val;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +03001400 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Chuck Leverad06e4b2007-02-12 00:53:32 -08001402 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1403 serv->sv_program->pg_name, protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001404 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1407 printk(KERN_WARNING "svc: only UDP and TCP "
1408 "sockets supported\n");
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001409 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Trond Myklebustc69da772009-03-30 18:59:17 -04001412 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1413 switch (sin->sa_family) {
1414 case AF_INET6:
1415 family = PF_INET6;
1416 break;
1417 case AF_INET:
1418 family = PF_INET;
1419 break;
1420 default:
1421 return ERR_PTR(-EINVAL);
1422 }
1423
Pavel Emelyanov14ec63c2010-09-29 16:06:57 +04001424 error = __sock_create(net, family, type, protocol, &sock, 1);
Chuck Lever77f1f672007-02-12 00:53:39 -08001425 if (error < 0)
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001426 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Peter Zijlstraed075362006-12-06 20:35:24 -08001428 svc_reclassify_socket(sock);
1429
Trond Myklebustc69da772009-03-30 18:59:17 -04001430 /*
1431 * If this is an PF_INET6 listener, we want to avoid
1432 * getting requests from IPv4 remotes. Those should
1433 * be shunted to a PF_INET listener via rpcbind.
1434 */
1435 val = 1;
1436 if (family == PF_INET6)
1437 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
1438 (char *)&val, sizeof(val));
1439
Eric Sesterhenn18114742006-09-28 14:37:07 -07001440 if (type == SOCK_STREAM)
Pavel Emelyanov4a17fd52012-04-19 03:39:36 +00001441 sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
Chuck Lever77f1f672007-02-12 00:53:39 -08001442 error = kernel_bind(sock, sin, len);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001443 if (error < 0)
1444 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001446 error = kernel_getsockname(sock, newsin);
Tom Tucker9dbc2402007-12-30 21:08:12 -06001447 if (error < 0)
1448 goto bummer;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001449 newlen = error;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001452 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 goto bummer;
1454 }
1455
J. Bruce Fields72c35372012-08-13 17:46:17 -04001456 svsk = svc_setup_socket(serv, sock, flags);
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001457 if (IS_ERR(svsk)) {
1458 error = PTR_ERR(svsk);
1459 goto bummer;
NeilBrowne79eff12007-02-12 00:53:30 -08001460 }
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001461 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1462 return (struct svc_xprt *)svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463bummer:
1464 dprintk("svc: svc_create_socket error = %d\n", -error);
1465 sock_release(sock);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001466 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
1469/*
Tom Tucker755ccea2007-12-30 21:07:27 -06001470 * Detach the svc_sock from the socket so that no
1471 * more callbacks occur.
1472 */
1473static void svc_sock_detach(struct svc_xprt *xprt)
1474{
1475 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1476 struct sock *sk = svsk->sk_sk;
1477
1478 dprintk("svc: svc_sock_detach(%p)\n", svsk);
1479
1480 /* put back the old socket callbacks */
Trond Myklebust069c2252016-06-24 10:55:47 -04001481 lock_sock(sk);
Tom Tucker755ccea2007-12-30 21:07:27 -06001482 sk->sk_state_change = svsk->sk_ostate;
1483 sk->sk_data_ready = svsk->sk_odata;
1484 sk->sk_write_space = svsk->sk_owspace;
Trond Myklebust069c2252016-06-24 10:55:47 -04001485 sk->sk_user_data = NULL;
1486 release_sock(sk);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001487}
1488
1489/*
1490 * Disconnect the socket, and reset the callbacks
1491 */
1492static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1493{
1494 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1495
1496 dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk);
1497
1498 svc_sock_detach(xprt);
1499
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001500 if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1501 svc_tcp_clear_pages(svsk);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001502 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001503 }
Tom Tucker755ccea2007-12-30 21:07:27 -06001504}
1505
1506/*
1507 * Free the svc_sock's socket resources and the svc_sock itself.
1508 */
1509static void svc_sock_free(struct svc_xprt *xprt)
1510{
1511 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1512 dprintk("svc: svc_sock_free(%p)\n", svsk);
1513
Tom Tucker755ccea2007-12-30 21:07:27 -06001514 if (svsk->sk_sock->file)
1515 sockfd_put(svsk->sk_sock);
1516 else
1517 sock_release(svsk->sk_sock);
1518 kfree(svsk);
1519}