blob: 758ab10690dec12210f91d0eda72f87bd9c53c6a [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/net/sunrpc/svcsock.c
4 *
5 * These are the RPC server socket internals.
6 *
7 * The server scheduling algorithm does not always distribute the load
8 * evenly when servicing a single client. May need to modify the
Tom Tuckerf6150c32007-12-30 21:07:57 -06009 * svc_xprt_enqueue procedure...
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * TCP support is largely untested and may be a little slow. The problem
12 * is that we currently do two separate recvfrom's, one for the 4-byte
13 * record length, and the second for the actual record. This could possibly
14 * be improved by always reading a minimum size of around 100 bytes and
15 * tucking any superfluous bytes away in a temporary store. Still, that
16 * leaves write requests out in the rain. An alternative may be to peek at
17 * the first skb in the queue, and if it matches the next TCP sequence
18 * number, to extract the record marker. Yuck.
19 *
20 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
21 */
22
Ilpo Järvinen172589c2007-08-28 15:50:33 -070023#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/sched.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040025#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/errno.h>
27#include <linux/fcntl.h>
28#include <linux/net.h>
29#include <linux/in.h>
30#include <linux/inet.h>
31#include <linux/udp.h>
Andrew Morton91483c42005-08-09 20:20:07 -070032#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/unistd.h>
34#include <linux/slab.h>
35#include <linux/netdevice.h>
36#include <linux/skbuff.h>
NeilBrownb41b66d2006-10-02 02:17:48 -070037#include <linux/file.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080038#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <net/sock.h>
40#include <net/checksum.h>
41#include <net/ip.h>
Chuck Leverb92503b2007-02-12 00:53:36 -080042#include <net/ipv6.h>
Paolo Abeni850cbad2016-10-21 13:55:47 +020043#include <net/udp.h>
Chuck Leverb7872fe2008-04-14 12:27:01 -040044#include <net/tcp.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070045#include <net/tcp_states.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080046#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/ioctls.h>
Eric Dumazet22911fc2012-06-27 00:23:44 +000048#include <trace/events/skb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <linux/sunrpc/types.h>
Chuck Leverad06e4b2007-02-12 00:53:32 -080051#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/sunrpc/xdr.h>
Chuck Leverc0401ea2008-04-14 12:27:30 -040053#include <linux/sunrpc/msg_prot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/sunrpc/svcsock.h>
55#include <linux/sunrpc/stats.h>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030056#include <linux/sunrpc/xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
H Hartley Sweeten177e4f92011-06-20 17:54:51 -070058#include "sunrpc.h"
59
Tom Tucker360d87382007-12-30 21:07:17 -060060#define RPCDBG_FACILITY RPCDBG_SVCXPRT
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62
63static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
J. Bruce Fields72c35372012-08-13 17:46:17 -040064 int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int svc_udp_recvfrom(struct svc_rqst *);
66static int svc_udp_sendto(struct svc_rqst *);
Tom Tucker755ccea2007-12-30 21:07:27 -060067static void svc_sock_detach(struct svc_xprt *);
Trond Myklebust69b6ba32008-12-23 16:30:11 -050068static void svc_tcp_sock_detach(struct svc_xprt *);
Tom Tucker755ccea2007-12-30 21:07:27 -060069static void svc_sock_free(struct svc_xprt *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Tom Tuckerb700cbb2007-12-30 21:07:42 -060071static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
Pavel Emelyanov62832c02010-09-29 16:04:18 +040072 struct net *, struct sockaddr *,
73 int, int);
Peter Zijlstraed075362006-12-06 20:35:24 -080074#ifdef CONFIG_DEBUG_LOCK_ALLOC
75static struct lock_class_key svc_key[2];
76static struct lock_class_key svc_slock_key[2];
77
Tom Tucker0f0257e2007-12-30 21:08:27 -060078static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -080079{
80 struct sock *sk = sock->sk;
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -040081
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +020082 if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -040083 return;
84
Peter Zijlstraed075362006-12-06 20:35:24 -080085 switch (sk->sk_family) {
86 case AF_INET:
87 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -060088 &svc_slock_key[0],
89 "sk_xprt.xpt_lock-AF_INET-NFSD",
90 &svc_key[0]);
Peter Zijlstraed075362006-12-06 20:35:24 -080091 break;
92
93 case AF_INET6:
94 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -060095 &svc_slock_key[1],
96 "sk_xprt.xpt_lock-AF_INET6-NFSD",
97 &svc_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -080098 break;
99
100 default:
101 BUG();
102 }
103}
104#else
Tom Tucker0f0257e2007-12-30 21:08:27 -0600105static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -0800106{
107}
108#endif
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
111 * Release an skbuff after use
112 */
Tom Tucker5148bf42007-12-30 21:07:25 -0600113static void svc_release_skb(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Tom Tucker5148bf42007-12-30 21:07:25 -0600115 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 if (skb) {
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600118 struct svc_sock *svsk =
119 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tucker5148bf42007-12-30 21:07:25 -0600120 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
Eric Dumazet9d410c72009-10-30 05:03:53 +0000123 skb_free_datagram_locked(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Paolo Abeni850cbad2016-10-21 13:55:47 +0200127static void svc_release_udp_skb(struct svc_rqst *rqstp)
128{
129 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
130
131 if (skb) {
132 rqstp->rq_xprt_ctxt = NULL;
133
134 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
135 consume_skb(skb);
136 }
137}
138
Chuck Leverb92503b2007-02-12 00:53:36 -0800139union svc_pktinfo_u {
140 struct in_pktinfo pkti;
Chuck Leverb92503b2007-02-12 00:53:36 -0800141 struct in6_pktinfo pkti6;
Chuck Leverb92503b2007-02-12 00:53:36 -0800142};
David S. Millerbc375ea2007-04-12 13:35:59 -0700143#define SVC_PKTINFO_SPACE \
144 CMSG_SPACE(sizeof(union svc_pktinfo_u))
Chuck Leverb92503b2007-02-12 00:53:36 -0800145
146static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
147{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600148 struct svc_sock *svsk =
149 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
150 switch (svsk->sk_sk->sk_family) {
Chuck Leverb92503b2007-02-12 00:53:36 -0800151 case AF_INET: {
152 struct in_pktinfo *pki = CMSG_DATA(cmh);
153
154 cmh->cmsg_level = SOL_IP;
155 cmh->cmsg_type = IP_PKTINFO;
156 pki->ipi_ifindex = 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800157 pki->ipi_spec_dst.s_addr =
158 svc_daddr_in(rqstp)->sin_addr.s_addr;
Chuck Leverb92503b2007-02-12 00:53:36 -0800159 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
160 }
161 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800162
Chuck Leverb92503b2007-02-12 00:53:36 -0800163 case AF_INET6: {
164 struct in6_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800165 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
Chuck Leverb92503b2007-02-12 00:53:36 -0800166
167 cmh->cmsg_level = SOL_IPV6;
168 cmh->cmsg_type = IPV6_PKTINFO;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800169 pki->ipi6_ifindex = daddr->sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000170 pki->ipi6_addr = daddr->sin6_addr;
Chuck Leverb92503b2007-02-12 00:53:36 -0800171 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
172 }
173 break;
Chuck Leverb92503b2007-02-12 00:53:36 -0800174 }
Chuck Leverb92503b2007-02-12 00:53:36 -0800175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/*
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300178 * send routine intended to be shared by the fore- and back-channel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300180int svc_send_common(struct socket *sock, struct xdr_buf *xdr,
181 struct page *headpage, unsigned long headoffset,
182 struct page *tailpage, unsigned long tailoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int result;
185 int size;
186 struct page **ppage = xdr->pages;
187 size_t base = xdr->page_base;
188 unsigned int pglen = xdr->page_len;
Trond Myklebust226453d2015-10-07 13:54:42 -0400189 unsigned int flags = MSG_MORE | MSG_SENDPAGE_NOTLAST;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300190 int slen;
191 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 slen = xdr->len;
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* send head */
196 if (slen == xdr->head[0].iov_len)
197 flags = 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300198 len = kernel_sendpage(sock, headpage, headoffset,
NeilBrown44524352006-10-04 02:15:46 -0700199 xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (len != xdr->head[0].iov_len)
201 goto out;
202 slen -= xdr->head[0].iov_len;
203 if (slen == 0)
204 goto out;
205
206 /* send page data */
207 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
208 while (pglen > 0) {
209 if (slen == size)
210 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700211 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (result > 0)
213 len += result;
214 if (result != size)
215 goto out;
216 slen -= size;
217 pglen -= size;
218 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
219 base = 0;
220 ppage++;
221 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* send tail */
224 if (xdr->tail[0].iov_len) {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300225 result = kernel_sendpage(sock, tailpage, tailoffset,
226 xdr->tail[0].iov_len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (result > 0)
228 len += result;
229 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300230
231out:
232 return len;
233}
234
235
236/*
237 * Generic sendto routine
238 */
239static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
240{
241 struct svc_sock *svsk =
242 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
243 struct socket *sock = svsk->sk_sock;
244 union {
245 struct cmsghdr hdr;
246 long all[SVC_PKTINFO_SPACE / sizeof(long)];
247 } buffer;
248 struct cmsghdr *cmh = &buffer.hdr;
249 int len = 0;
250 unsigned long tailoff;
251 unsigned long headoff;
252 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
253
254 if (rqstp->rq_prot == IPPROTO_UDP) {
255 struct msghdr msg = {
256 .msg_name = &rqstp->rq_addr,
257 .msg_namelen = rqstp->rq_addrlen,
258 .msg_control = cmh,
259 .msg_controllen = sizeof(buffer),
260 .msg_flags = MSG_MORE,
261 };
262
263 svc_set_cmsg_data(rqstp, cmh);
264
Al Virod8725c82014-12-11 00:02:50 -0500265 if (sock_sendmsg(sock, &msg) < 0)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300266 goto out;
267 }
268
269 tailoff = ((unsigned long)xdr->tail[0].iov_base) & (PAGE_SIZE-1);
270 headoff = 0;
271 len = svc_send_common(sock, xdr, rqstp->rq_respages[0], headoff,
272 rqstp->rq_respages[0], tailoff);
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274out:
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800275 dprintk("svc: socket %p sendto([%p %zu... ], %d) = %d (addr %s)\n",
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600276 svsk, xdr->head[0].iov_base, xdr->head[0].iov_len,
Chuck Leverad06e4b2007-02-12 00:53:32 -0800277 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 return len;
280}
281
Chuck Lever41205532020-03-02 14:45:53 -0500282static int svc_sock_read_payload(struct svc_rqst *rqstp, unsigned int offset,
283 unsigned int length)
284{
285 return 0;
286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/*
NeilBrown80212d52006-10-02 02:17:47 -0700289 * Report socket names for nfsdfs
290 */
Chuck Levere7942b92009-04-23 19:32:48 -0400291static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
NeilBrown80212d52006-10-02 02:17:47 -0700292{
Chuck Lever017cb472009-04-23 19:33:03 -0400293 const struct sock *sk = svsk->sk_sk;
294 const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
295 "udp" : "tcp";
NeilBrown80212d52006-10-02 02:17:47 -0700296 int len;
297
Chuck Lever017cb472009-04-23 19:33:03 -0400298 switch (sk->sk_family) {
Chuck Levere7942b92009-04-23 19:32:48 -0400299 case PF_INET:
300 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400301 proto_name,
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000302 &inet_sk(sk)->inet_rcv_saddr,
303 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700304 break;
Eric Dumazetc2bb06d2013-10-09 03:05:48 -0700305#if IS_ENABLED(CONFIG_IPV6)
Chuck Lever58de2f82009-04-23 19:32:55 -0400306 case PF_INET6:
307 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400308 proto_name,
Eric Dumazetefe42082013-10-03 15:42:29 -0700309 &sk->sk_v6_rcv_saddr,
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000310 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700311 break;
Eric Dumazetc2bb06d2013-10-09 03:05:48 -0700312#endif
NeilBrown80212d52006-10-02 02:17:47 -0700313 default:
Chuck Levere7942b92009-04-23 19:32:48 -0400314 len = snprintf(buf, remaining, "*unknown-%d*\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400315 sk->sk_family);
NeilBrown80212d52006-10-02 02:17:47 -0700316 }
Chuck Levere7942b92009-04-23 19:32:48 -0400317
318 if (len >= remaining) {
319 *buf = '\0';
320 return -ENAMETOOLONG;
NeilBrown80212d52006-10-02 02:17:47 -0700321 }
322 return len;
323}
324
NeilBrown80212d52006-10-02 02:17:47 -0700325/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 * Generic recvfrom routine.
327 */
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400328static ssize_t svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov,
329 unsigned int nr, size_t buflen, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600331 struct svc_sock *svsk =
332 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400333 struct msghdr msg = { NULL };
334 ssize_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Tom Tucker260c1d12007-12-30 21:08:29 -0600336 rqstp->rq_xprt_hlen = 0;
337
Trond Myklebustf8d1ff42014-08-03 13:03:13 -0400338 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
David Howellsaa563d72018-10-20 00:57:56 +0100339 iov_iter_kvec(&msg.msg_iter, READ, iov, nr, buflen);
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400340 if (base != 0) {
341 iov_iter_advance(&msg.msg_iter, base);
342 buflen -= base;
343 }
344 len = sock_recvmsg(svsk->sk_sock, &msg, MSG_DONTWAIT);
Trond Myklebustf8d1ff42014-08-03 13:03:13 -0400345 /* If we read a full record, then assume there may be more
346 * data to read (stream based sockets only!)
347 */
348 if (len == buflen)
349 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400351 dprintk("svc: socket %p recvfrom(%p, %zu) = %zd\n",
Chuck Lever1ba95102007-02-12 00:53:31 -0800352 svsk, iov[0].iov_base, iov[0].iov_len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return len;
354}
355
356/*
357 * Set socket snd and rcv buffer lengths
358 */
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500359static void svc_sock_setbufsize(struct svc_sock *svsk, unsigned int nreqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500361 unsigned int max_mesg = svsk->sk_xprt.xpt_server->sv_max_mesg;
362 struct socket *sock = svsk->sk_sock;
363
364 nreqs = min(nreqs, INT_MAX / 2 / max_mesg);
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 lock_sock(sock->sk);
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500367 sock->sk->sk_sndbuf = nreqs * max_mesg * 2;
368 sock->sk->sk_rcvbuf = nreqs * max_mesg * 2;
Trond Myklebust47fcb032009-05-18 17:47:56 -0400369 sock->sk->sk_write_space(sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 release_sock(sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
Chuck Lever16e4d932014-05-19 13:40:22 -0400372
Chuck Lever989f8812018-03-27 10:49:38 -0400373static void svc_sock_secure_port(struct svc_rqst *rqstp)
Chuck Lever16e4d932014-05-19 13:40:22 -0400374{
Chuck Lever989f8812018-03-27 10:49:38 -0400375 if (svc_port_is_privileged(svc_addr(rqstp)))
376 set_bit(RQ_SECURE, &rqstp->rq_flags);
377 else
378 clear_bit(RQ_SECURE, &rqstp->rq_flags);
Chuck Lever16e4d932014-05-19 13:40:22 -0400379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/*
382 * INET callback when data has been received on the socket.
383 */
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400384static void svc_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Neil Brown939bb7e2005-09-13 01:25:39 -0700386 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Neil Brown939bb7e2005-09-13 01:25:39 -0700388 if (svsk) {
David S. Miller676d2362014-04-11 16:15:36 -0400389 dprintk("svc: socket %p(inet %p), busy=%d\n",
390 svsk, sk,
Tom Tucker02fc6c32007-12-30 21:07:48 -0600391 test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400392
393 /* Refer to svc_setup_socket() for details. */
394 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400395 svsk->sk_odata(sk);
Trond Myklebust4720b072016-06-24 10:55:49 -0400396 if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags))
397 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401/*
402 * INET callback when space is newly available on the socket.
403 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600404static void svc_write_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
407
408 if (svsk) {
409 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600410 svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400411
412 /* Refer to svc_setup_socket() for details. */
413 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400414 svsk->sk_owspace(sk);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600415 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Trond Myklebustc7fb3f02014-07-24 23:59:32 -0400419static int svc_tcp_has_wspace(struct svc_xprt *xprt)
420{
Trond Myklebust637600f2016-06-24 10:55:51 -0400421 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Trond Myklebustc7fb3f02014-07-24 23:59:32 -0400422
423 if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
424 return 1;
Trond Myklebust637600f2016-06-24 10:55:51 -0400425 return !test_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Trond Myklebust51877682014-07-24 23:59:33 -0400426}
427
Scott Mayhewea08e392016-11-11 13:16:22 -0500428static void svc_tcp_kill_temp_xprt(struct svc_xprt *xprt)
429{
430 struct svc_sock *svsk;
431 struct socket *sock;
432 struct linger no_linger = {
433 .l_onoff = 1,
434 .l_linger = 0,
435 };
436
437 svsk = container_of(xprt, struct svc_sock, sk_xprt);
438 sock = svsk->sk_sock;
439 kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER,
440 (char *)&no_linger, sizeof(no_linger));
441}
442
Tom Tucker9dbc2402007-12-30 21:08:12 -0600443/*
Chuck Lever7702ce42009-07-13 10:54:26 -0400444 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
445 */
446static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
447 struct cmsghdr *cmh)
448{
449 struct in_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800450 struct sockaddr_in *daddr = svc_daddr_in(rqstp);
451
Chuck Lever7702ce42009-07-13 10:54:26 -0400452 if (cmh->cmsg_type != IP_PKTINFO)
453 return 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800454
455 daddr->sin_family = AF_INET;
456 daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
Chuck Lever7702ce42009-07-13 10:54:26 -0400457 return 1;
458}
459
460/*
Tom Parkin73df66f2013-01-31 01:02:24 +0000461 * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
Chuck Lever7702ce42009-07-13 10:54:26 -0400462 */
463static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
464 struct cmsghdr *cmh)
465{
466 struct in6_pktinfo *pki = CMSG_DATA(cmh);
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800467 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
468
Chuck Lever7702ce42009-07-13 10:54:26 -0400469 if (cmh->cmsg_type != IPV6_PKTINFO)
470 return 0;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800471
472 daddr->sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000473 daddr->sin6_addr = pki->ipi6_addr;
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800474 daddr->sin6_scope_id = pki->ipi6_ifindex;
Chuck Lever7702ce42009-07-13 10:54:26 -0400475 return 1;
476}
477
478/*
Tom Tucker9dbc2402007-12-30 21:08:12 -0600479 * Copy the UDP datagram's destination address to the rqstp structure.
480 * The 'destination' address in this case is the address to which the
481 * peer sent the datagram, i.e. our local address. For multihomed
482 * hosts, this can change from msg to msg. Note that only the IP
483 * address changes, the port number should remain the same.
484 */
Chuck Lever7702ce42009-07-13 10:54:26 -0400485static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
486 struct cmsghdr *cmh)
Chuck Lever95756482007-02-12 00:53:38 -0800487{
Chuck Lever7702ce42009-07-13 10:54:26 -0400488 switch (cmh->cmsg_level) {
489 case SOL_IP:
490 return svc_udp_get_dest_address4(rqstp, cmh);
491 case SOL_IPV6:
492 return svc_udp_get_dest_address6(rqstp, cmh);
Chuck Lever95756482007-02-12 00:53:38 -0800493 }
Chuck Lever7702ce42009-07-13 10:54:26 -0400494
495 return 0;
Chuck Lever95756482007-02-12 00:53:38 -0800496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/*
499 * Receive a datagram from a UDP socket.
500 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600501static int svc_udp_recvfrom(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600503 struct svc_sock *svsk =
504 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600505 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct sk_buff *skb;
David S. Millerbc375ea2007-04-12 13:35:59 -0700507 union {
508 struct cmsghdr hdr;
509 long all[SVC_PKTINFO_SPACE / sizeof(long)];
510 } buffer;
511 struct cmsghdr *cmh = &buffer.hdr;
NeilBrown7a37f572007-03-06 01:42:21 -0800512 struct msghdr msg = {
513 .msg_name = svc_addr(rqstp),
514 .msg_control = cmh,
515 .msg_controllen = sizeof(buffer),
516 .msg_flags = MSG_DONTWAIT,
517 };
Chuck Leverabc5c442009-04-23 19:31:25 -0400518 size_t len;
519 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Tom Tucker02fc6c32007-12-30 21:07:48 -0600521 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* udp sockets need large rcvbuf as all pending
523 * requests are still in that buffer. sndbuf must
524 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700525 * for one reply per thread. We count all threads
526 * rather than threads in a particular pool, which
527 * provides an upper bound on the number of threads
528 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 */
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500530 svc_sock_setbufsize(svsk, serv->sv_nrthreads + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Tom Tucker02fc6c32007-12-30 21:07:48 -0600532 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
NeilBrown05ed6902007-05-09 02:34:55 -0700533 skb = NULL;
534 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
535 0, 0, MSG_PEEK | MSG_DONTWAIT);
536 if (err >= 0)
Paolo Abeni7c13f972016-11-04 11:28:59 +0100537 skb = skb_recv_udp(svsk->sk_sk, 0, 1, &err);
NeilBrown05ed6902007-05-09 02:34:55 -0700538
539 if (skb == NULL) {
540 if (err != -EAGAIN) {
541 /* possibly an icmp error */
542 dprintk("svc: recvfrom returned error %d\n", -err);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600543 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -0400545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600547 len = svc_addr_len(svc_addr(rqstp));
Tom Tucker9dbc2402007-12-30 21:08:12 -0600548 rqstp->rq_addrlen = len;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100549 if (skb->tstamp == 0) {
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700550 skb->tstamp = ktime_get_real();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800551 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 need that much accuracy */
553 }
Deepa Dinamani3a0ed3e92018-12-27 18:55:09 -0800554 sock_write_timestamp(svsk->sk_sk, skb->tstamp);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600555 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Willem de Bruijn1da8c6812016-04-07 11:44:58 -0400557 len = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 rqstp->rq_arg.len = len;
559
Chuck Lever95756482007-02-12 00:53:38 -0800560 rqstp->rq_prot = IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Chuck Lever7702ce42009-07-13 10:54:26 -0400562 if (!svc_udp_get_dest_address(rqstp, cmh)) {
Joe Perchese87cc472012-05-13 21:56:26 +0000563 net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
564 cmh->cmsg_level, cmh->cmsg_type);
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400565 goto out_free;
NeilBrown7a37f572007-03-06 01:42:21 -0800566 }
Mi Jinlong849a1cf2011-08-30 17:18:41 +0800567 rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 if (skb_is_nonlinear(skb)) {
570 /* we have to copy */
571 local_bh_disable();
572 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
573 local_bh_enable();
574 /* checksum error */
Eric Dumazet22911fc2012-06-27 00:23:44 +0000575 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577 local_bh_enable();
Paolo Abeni850cbad2016-10-21 13:55:47 +0200578 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 } else {
580 /* we can use it in-place */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -0400581 rqstp->rq_arg.head[0].iov_base = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 rqstp->rq_arg.head[0].iov_len = len;
Eric Dumazet22911fc2012-06-27 00:23:44 +0000583 if (skb_checksum_complete(skb))
584 goto out_free;
Tom Tucker5148bf42007-12-30 21:07:25 -0600585 rqstp->rq_xprt_ctxt = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
588 rqstp->rq_arg.page_base = 0;
589 if (len <= rqstp->rq_arg.head[0].iov_len) {
590 rqstp->rq_arg.head[0].iov_len = len;
591 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700592 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 } else {
594 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700595 rqstp->rq_respages = rqstp->rq_pages + 1 +
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700596 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500598 rqstp->rq_next_page = rqstp->rq_respages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 if (serv->sv_stats)
601 serv->sv_stats->netudpcnt++;
602
603 return len;
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400604out_free:
Paolo Abeni850cbad2016-10-21 13:55:47 +0200605 kfree_skb(skb);
J. Bruce Fieldsf23abfd2012-08-17 20:32:27 -0400606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
609static int
610svc_udp_sendto(struct svc_rqst *rqstp)
611{
612 int error;
613
614 error = svc_sendto(rqstp, &rqstp->rq_res);
615 if (error == -ECONNREFUSED)
616 /* ICMP error on earlier request. */
617 error = svc_sendto(rqstp, &rqstp->rq_res);
618
619 return error;
620}
621
Tom Tucker323bee32007-12-30 21:07:31 -0600622static int svc_udp_has_wspace(struct svc_xprt *xprt)
623{
624 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600625 struct svc_serv *serv = xprt->xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -0600626 unsigned long required;
627
628 /*
629 * Set the SOCK_NOSPACE flag before checking the available
630 * sock space.
631 */
632 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Tom Tucker7a90e8c2007-12-30 21:07:55 -0600633 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
Tom Tucker323bee32007-12-30 21:07:31 -0600634 if (required*2 > sock_wspace(svsk->sk_sk))
635 return 0;
636 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
637 return 1;
638}
639
Tom Tucker38a417c2007-12-30 21:07:36 -0600640static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
641{
642 BUG();
643 return NULL;
644}
645
Scott Mayhewea08e392016-11-11 13:16:22 -0500646static void svc_udp_kill_temp_xprt(struct svc_xprt *xprt)
647{
648}
649
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600650static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400651 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600652 struct sockaddr *sa, int salen,
653 int flags)
654{
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400655 return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600656}
657
Chuck Lever2412e922017-08-01 11:59:49 -0400658static const struct svc_xprt_ops svc_udp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600659 .xpo_create = svc_udp_create,
Tom Tucker5d137992007-12-30 21:07:23 -0600660 .xpo_recvfrom = svc_udp_recvfrom,
661 .xpo_sendto = svc_udp_sendto,
Chuck Lever41205532020-03-02 14:45:53 -0500662 .xpo_read_payload = svc_sock_read_payload,
Paolo Abeni850cbad2016-10-21 13:55:47 +0200663 .xpo_release_rqst = svc_release_udp_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -0600664 .xpo_detach = svc_sock_detach,
665 .xpo_free = svc_sock_free,
Tom Tucker323bee32007-12-30 21:07:31 -0600666 .xpo_has_wspace = svc_udp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -0600667 .xpo_accept = svc_udp_accept,
Chuck Lever16e4d932014-05-19 13:40:22 -0400668 .xpo_secure_port = svc_sock_secure_port,
Scott Mayhewea08e392016-11-11 13:16:22 -0500669 .xpo_kill_temp_xprt = svc_udp_kill_temp_xprt,
Tom Tucker360d87382007-12-30 21:07:17 -0600670};
671
672static struct svc_xprt_class svc_udp_class = {
673 .xcl_name = "udp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600674 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -0600675 .xcl_ops = &svc_udp_ops,
Tom Tucker49023152007-12-30 21:07:21 -0600676 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
Chuck Lever3c45ddf2014-07-16 15:38:32 -0400677 .xcl_ident = XPRT_TRANSPORT_UDP,
Tom Tucker360d87382007-12-30 21:07:17 -0600678};
679
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600680static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Chuck Lever7702ce42009-07-13 10:54:26 -0400682 int err, level, optname, one = 1;
NeilBrown7a37f572007-03-06 01:42:21 -0800683
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +0300684 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
685 &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600686 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400687 svsk->sk_sk->sk_data_ready = svc_data_ready;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 svsk->sk_sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800691 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * svc_udp_recvfrom will re-adjust if necessary
693 */
J. Bruce Fieldsb7e50342019-02-20 12:54:50 -0500694 svc_sock_setbufsize(svsk, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Tom Tucker0f0257e2007-12-30 21:08:27 -0600696 /* data might have come in before data_ready set up */
697 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600698 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800699
NeilBrown7a37f572007-03-06 01:42:21 -0800700 /* make sure we get destination address info */
Chuck Lever7702ce42009-07-13 10:54:26 -0400701 switch (svsk->sk_sk->sk_family) {
702 case AF_INET:
703 level = SOL_IP;
704 optname = IP_PKTINFO;
705 break;
706 case AF_INET6:
707 level = SOL_IPV6;
708 optname = IPV6_RECVPKTINFO;
709 break;
710 default:
711 BUG();
712 }
713 err = kernel_setsockopt(svsk->sk_sock, level, optname,
714 (char *)&one, sizeof(one));
715 dprintk("svc: kernel_setsockopt returned %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718/*
719 * A data_ready event on a listening socket means there's a connection
720 * pending. Do not use state_change as a substitute for it.
721 */
David S. Miller676d2362014-04-11 16:15:36 -0400722static void svc_tcp_listen_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Neil Brown939bb7e2005-09-13 01:25:39 -0700724 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700727 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400729 if (svsk) {
730 /* Refer to svc_setup_socket() for details. */
731 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400732 svsk->sk_odata(sk);
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400733 }
734
Neil Brown939bb7e2005-09-13 01:25:39 -0700735 /*
736 * This callback may called twice when a new connection
737 * is established as a child socket inherits everything
738 * from a parent LISTEN socket.
739 * 1) data_ready method of the parent socket will be called
740 * when one of child sockets become ESTABLISHED.
741 * 2) data_ready method of the child socket may be called
742 * when it receives data before the socket is accepted.
743 * In case of 2, we should ignore it silently.
744 */
745 if (sk->sk_state == TCP_LISTEN) {
746 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600747 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600748 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700749 } else
750 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754/*
755 * A state change on a connected socket means it's dying or dead.
756 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600757static void svc_tcp_state_change(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Neil Brown939bb7e2005-09-13 01:25:39 -0700759 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700762 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Neil Brown939bb7e2005-09-13 01:25:39 -0700764 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700766 else {
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -0400767 /* Refer to svc_setup_socket() for details. */
768 rmb();
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400769 svsk->sk_ostate(sk);
Trond Myklebustb2f21f72016-07-26 09:51:19 -0400770 if (sk->sk_state != TCP_ESTABLISHED) {
771 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
772 svc_xprt_enqueue(&svsk->sk_xprt);
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777/*
778 * Accept a TCP connection
779 */
Tom Tucker38a417c2007-12-30 21:07:36 -0600780static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Tom Tucker38a417c2007-12-30 21:07:36 -0600782 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800783 struct sockaddr_storage addr;
784 struct sockaddr *sin = (struct sockaddr *) &addr;
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600785 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct socket *sock = svsk->sk_sock;
787 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct svc_sock *newsvsk;
789 int err, slen;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +0300790 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
793 if (!sock)
Tom Tucker38a417c2007-12-30 21:07:36 -0600794 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Tom Tucker02fc6c32007-12-30 21:07:48 -0600796 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700797 err = kernel_accept(sock, &newsock, O_NONBLOCK);
798 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (err == -ENOMEM)
800 printk(KERN_WARNING "%s: no more sockets!\n",
801 serv->sv_name);
Joe Perchese87cc472012-05-13 21:56:26 +0000802 else if (err != -EAGAIN)
803 net_warn_ratelimited("%s: accept failed (err %d)!\n",
804 serv->sv_name, -err);
Tom Tucker38a417c2007-12-30 21:07:36 -0600805 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
Tom Tucker02fc6c32007-12-30 21:07:48 -0600807 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100809 err = kernel_getpeername(newsock, sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (err < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +0000811 net_warn_ratelimited("%s: peername failed (err %d)!\n",
812 serv->sv_name, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 goto failed; /* aborted connection or whatever */
814 }
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100815 slen = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /* Ideally, we would want to reject connections from unauthorized
Chuck Leverad06e4b2007-02-12 00:53:32 -0800818 * hosts here, but when we get encryption, the IP of the host won't
819 * tell us anything. For now just warn about unpriv connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800821 if (!svc_port_is_privileged(sin)) {
Kinglong Meea48fd0f2014-05-29 12:18:52 +0800822 dprintk("%s: connect from unprivileged port: %s\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800823 serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800824 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
Chuck Leverad06e4b2007-02-12 00:53:32 -0800826 dprintk("%s: connect from %s\n", serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800827 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Trond Myklebustfa9251a2016-06-24 10:55:48 -0400829 /* Reset the inherited callbacks before calling svc_setup_socket */
830 newsock->sk->sk_state_change = svsk->sk_ostate;
831 newsock->sk->sk_data_ready = svsk->sk_odata;
832 newsock->sk->sk_write_space = svsk->sk_owspace;
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* make sure that a write doesn't block forever when
835 * low on memory
836 */
837 newsock->sk->sk_sndtimeo = HZ*30;
838
J. Bruce Fields72c35372012-08-13 17:46:17 -0400839 newsvsk = svc_setup_socket(serv, newsock,
840 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
841 if (IS_ERR(newsvsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto failed;
Tom Tucker9dbc2402007-12-30 21:08:12 -0600843 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100844 err = kernel_getsockname(newsock, sin);
845 slen = err;
Frank van Maarseveena9747692007-07-09 22:21:39 +0200846 if (unlikely(err < 0)) {
847 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
848 slen = offsetof(struct sockaddr, sa_data);
849 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600850 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -0800851
NeilBrownef11ce22014-05-12 11:22:47 +1000852 if (sock_is_loopback(newsock->sk))
853 set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
854 else
855 clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600856 if (serv->sv_stats)
857 serv->sv_stats->nettcpconn++;
858
859 return &newsvsk->sk_xprt;
860
861failed:
862 sock_release(newsock);
863 return NULL;
864}
865
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800866static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
867{
868 unsigned int i, len, npages;
869
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500870 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800871 return 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500872 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800873 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
874 for (i = 0; i < npages; i++) {
875 if (rqstp->rq_pages[i] != NULL)
876 put_page(rqstp->rq_pages[i]);
877 BUG_ON(svsk->sk_pages[i] == NULL);
878 rqstp->rq_pages[i] = svsk->sk_pages[i];
879 svsk->sk_pages[i] = NULL;
880 }
881 rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
882 return len;
883}
884
885static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
886{
887 unsigned int i, len, npages;
888
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500889 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800890 return;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500891 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800892 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
893 for (i = 0; i < npages; i++) {
894 svsk->sk_pages[i] = rqstp->rq_pages[i];
895 rqstp->rq_pages[i] = NULL;
896 }
897}
898
899static void svc_tcp_clear_pages(struct svc_sock *svsk)
900{
901 unsigned int i, len, npages;
902
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500903 if (svsk->sk_datalen == 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800904 goto out;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500905 len = svsk->sk_datalen;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800906 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
907 for (i = 0; i < npages; i++) {
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -0400908 if (svsk->sk_pages[i] == NULL) {
909 WARN_ON_ONCE(1);
910 continue;
911 }
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800912 put_page(svsk->sk_pages[i]);
913 svsk->sk_pages[i] = NULL;
914 }
915out:
916 svsk->sk_tcplen = 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -0500917 svsk->sk_datalen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800918}
919
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600920/*
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -0500921 * Receive fragment record header.
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300922 * If we haven't gotten the record length yet, get the next four bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 */
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300924static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600926 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400927 unsigned int want;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300928 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Chuck Leverc0401ea2008-04-14 12:27:30 -0400930 if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 struct kvec iov;
932
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400933 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
935 iov.iov_len = want;
Trond Myklebust4c8e5532018-10-01 10:41:53 -0400936 len = svc_recvfrom(rqstp, &iov, 1, want, 0);
937 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 goto error;
939 svsk->sk_tcplen += len;
940
941 if (len < want) {
Chuck Leverc0401ea2008-04-14 12:27:30 -0400942 dprintk("svc: short recvfrom while reading record "
943 "length (%d of %d)\n", len, want);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800944 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500947 dprintk("svc: TCP record, %d bytes\n", svc_sock_reclen(svsk));
J. Bruce Fields836fbad2012-12-03 15:50:38 -0500948 if (svc_sock_reclen(svsk) + svsk->sk_datalen >
949 serv->sv_max_mesg) {
J. Bruce Fields3a28e332012-12-10 18:15:07 -0500950 net_notice_ratelimited("RPC: fragment too large: %d\n",
951 svc_sock_reclen(svsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 goto err_delete;
953 }
954 }
955
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500956 return svc_sock_reclen(svsk);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800957error:
958 dprintk("RPC: TCP recv_record got %d\n", len);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300959 return len;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800960err_delete:
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300961 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300962 return -EAGAIN;
963}
964
Trond Myklebust586c52c2009-05-18 17:47:56 -0400965static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300966{
Trond Myklebust586c52c2009-05-18 17:47:56 -0400967 struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300968 struct rpc_rqst *req = NULL;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400969 struct kvec *src, *dst;
970 __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
J. Bruce Fields48e65552011-02-14 14:52:03 -0500971 __be32 xid;
972 __be32 calldir;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300973
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300974 xid = *p++;
975 calldir = *p;
976
Trond Myklebust093a1462014-11-12 18:04:04 -0500977 if (!bc_xprt)
Trond Myklebust586c52c2009-05-18 17:47:56 -0400978 return -EAGAIN;
Trond Myklebust75c84152018-08-31 10:21:00 -0400979 spin_lock(&bc_xprt->queue_lock);
Trond Myklebust093a1462014-11-12 18:04:04 -0500980 req = xprt_lookup_rqst(bc_xprt, xid);
981 if (!req)
982 goto unlock_notfound;
Trond Myklebust586c52c2009-05-18 17:47:56 -0400983
984 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
985 /*
986 * XXX!: cheating for now! Only copying HEAD.
987 * But we know this is good enough for now (in fact, for any
988 * callback reply in the forseeable future).
989 */
990 dst = &req->rq_private_buf.head[0];
991 src = &rqstp->rq_arg.head[0];
992 if (dst->iov_len < src->iov_len)
Trond Myklebust093a1462014-11-12 18:04:04 -0500993 goto unlock_eagain; /* whatever; just giving up. */
Trond Myklebust586c52c2009-05-18 17:47:56 -0400994 memcpy(dst->iov_base, src->iov_base, src->iov_len);
J. Bruce Fieldscc248d42012-12-03 16:11:13 -0500995 xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len);
Trond Myklebust586c52c2009-05-18 17:47:56 -0400996 rqstp->rq_arg.len = 0;
Trond Myklebust75c84152018-08-31 10:21:00 -0400997 spin_unlock(&bc_xprt->queue_lock);
Trond Myklebust586c52c2009-05-18 17:47:56 -0400998 return 0;
Trond Myklebust093a1462014-11-12 18:04:04 -0500999unlock_notfound:
1000 printk(KERN_NOTICE
1001 "%s: Got unrecognized reply: "
1002 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
1003 __func__, ntohl(calldir),
1004 bc_xprt, ntohl(xid));
1005unlock_eagain:
Trond Myklebust75c84152018-08-31 10:21:00 -04001006 spin_unlock(&bc_xprt->queue_lock);
Trond Myklebust093a1462014-11-12 18:04:04 -05001007 return -EAGAIN;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001008}
1009
1010static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len)
1011{
1012 int i = 0;
1013 int t = 0;
1014
1015 while (t < len) {
1016 vec[i].iov_base = page_address(pages[i]);
1017 vec[i].iov_len = PAGE_SIZE;
1018 i++;
1019 t += PAGE_SIZE;
1020 }
1021 return i;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001022}
1023
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001024static void svc_tcp_fragment_received(struct svc_sock *svsk)
1025{
1026 /* If we have more data, signal svc_xprt_enqueue() to try again */
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001027 dprintk("svc: TCP %s record (%d bytes)\n",
1028 svc_sock_final_rec(svsk) ? "final" : "nonfinal",
1029 svc_sock_reclen(svsk));
1030 svsk->sk_tcplen = 0;
1031 svsk->sk_reclen = 0;
1032}
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001033
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001034/*
1035 * Receive data from a TCP socket.
1036 */
1037static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
1038{
1039 struct svc_sock *svsk =
1040 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
1041 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1042 int len;
1043 struct kvec *vec;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001044 unsigned int want, base;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001045 __be32 *p;
1046 __be32 calldir;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001047 int pnum;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001048
1049 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1050 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
1051 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
1052 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
1053
1054 len = svc_tcp_recv_record(svsk, rqstp);
1055 if (len < 0)
1056 goto error;
1057
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001058 base = svc_tcp_restore_pages(svsk, rqstp);
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001059 want = svc_sock_reclen(svsk) - (svsk->sk_tcplen - sizeof(rpc_fraghdr));
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001060
NeilBrown3cc03b12006-10-04 02:15:47 -07001061 vec = rqstp->rq_vec;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001062
Trond Myklebust4c8e5532018-10-01 10:41:53 -04001063 pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0], base + want);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001064
NeilBrown44524352006-10-04 02:15:46 -07001065 rqstp->rq_respages = &rqstp->rq_pages[pnum];
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001066 rqstp->rq_next_page = rqstp->rq_respages + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 /* Now receive data */
Trond Myklebust4c8e5532018-10-01 10:41:53 -04001069 len = svc_recvfrom(rqstp, vec, pnum, base + want, base);
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001070 if (len >= 0) {
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001071 svsk->sk_tcplen += len;
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001072 svsk->sk_datalen += len;
1073 }
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001074 if (len != want || !svc_sock_final_rec(svsk)) {
J. Bruce Fieldsbe1e4442012-08-09 18:12:28 -04001075 svc_tcp_save_pages(svsk, rqstp);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001076 if (len < 0 && len != -EAGAIN)
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001077 goto err_delete;
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001078 if (len == want)
1079 svc_tcp_fragment_received(svsk);
1080 else
J. Bruce Fields3a28e332012-12-10 18:15:07 -05001081 dprintk("svc: incomplete TCP record (%d of %d)\n",
1082 (int)(svsk->sk_tcplen - sizeof(rpc_fraghdr)),
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001083 svc_sock_reclen(svsk));
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001084 goto err_noclose;
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
J. Bruce Fields1f691b02013-06-26 10:55:40 -04001087 if (svsk->sk_datalen < 8) {
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -04001088 svsk->sk_datalen = 0;
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001089 goto err_delete; /* client is nuts. */
J. Bruce Fieldscf3aa022013-06-26 11:09:06 -04001090 }
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001091
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001092 rqstp->rq_arg.len = svsk->sk_datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 rqstp->rq_arg.page_base = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001094 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1095 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 rqstp->rq_arg.page_len = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001097 } else
1098 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Tom Tucker5148bf42007-12-30 21:07:25 -06001100 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 rqstp->rq_prot = IPPROTO_TCP;
Jeff Layton7501cc22014-11-19 07:51:15 -05001102 if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags))
1103 set_bit(RQ_LOCAL, &rqstp->rq_flags);
1104 else
1105 clear_bit(RQ_LOCAL, &rqstp->rq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Trond Myklebust586c52c2009-05-18 17:47:56 -04001107 p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1108 calldir = p[1];
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001109 if (calldir)
Trond Myklebust586c52c2009-05-18 17:47:56 -04001110 len = receive_cb_reply(svsk, rqstp);
Trond Myklebust586c52c2009-05-18 17:47:56 -04001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 /* Reset TCP read info */
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001113 svsk->sk_datalen = 0;
J. Bruce Fields836fbad2012-12-03 15:50:38 -05001114 svc_tcp_fragment_received(svsk);
Trond Myklebust0601f792009-05-18 17:47:56 -04001115
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001116 if (len < 0)
1117 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Tom Tucker9dbc2402007-12-30 21:08:12 -06001119 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (serv->sv_stats)
1121 serv->sv_stats->nettcpcnt++;
1122
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001123 return rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001125error:
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001126 if (len != -EAGAIN)
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001127 goto err_delete;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001128 dprintk("RPC: TCP recvfrom got EAGAIN\n");
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -04001129 return 0;
J. Bruce Fieldsad46ccf2012-12-03 16:30:42 -05001130err_delete:
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001131 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1132 svsk->sk_xprt.xpt_server->sv_name, -len);
1133 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1134err_noclose:
J. Bruce Fields9f9d2eb2012-08-17 21:35:24 -04001135 return 0; /* record not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
1138/*
1139 * Send out data on TCP socket.
1140 */
Tom Tucker0f0257e2007-12-30 21:08:27 -06001141static int svc_tcp_sendto(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 struct xdr_buf *xbufp = &rqstp->rq_res;
1144 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001145 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 /* Set up the first element of the reply kvec.
1148 * Any other kvecs that may be in use have been taken
1149 * care of by the server implementation itself.
1150 */
1151 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1152 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 sent = svc_sendto(rqstp, &rqstp->rq_res);
1155 if (sent != xbufp->len) {
Tom Tucker0f0257e2007-12-30 21:08:27 -06001156 printk(KERN_NOTICE
1157 "rpc-srv/tcp: %s: %s %d when sending %d bytes "
1158 "- shutting down socket\n",
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001159 rqstp->rq_xprt->xpt_server->sv_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 (sent<0)?"got error":"sent only",
1161 sent, xbufp->len);
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001162 set_bit(XPT_CLOSE, &rqstp->rq_xprt->xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -06001163 svc_xprt_enqueue(rqstp->rq_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 sent = -EAGAIN;
1165 }
1166 return sent;
1167}
1168
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001169static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001170 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001171 struct sockaddr *sa, int salen,
1172 int flags)
1173{
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001174 return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001175}
1176
Chuck Lever2412e922017-08-01 11:59:49 -04001177static const struct svc_xprt_ops svc_tcp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001178 .xpo_create = svc_tcp_create,
Tom Tucker5d137992007-12-30 21:07:23 -06001179 .xpo_recvfrom = svc_tcp_recvfrom,
1180 .xpo_sendto = svc_tcp_sendto,
Chuck Lever41205532020-03-02 14:45:53 -05001181 .xpo_read_payload = svc_sock_read_payload,
Tom Tucker5148bf42007-12-30 21:07:25 -06001182 .xpo_release_rqst = svc_release_skb,
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001183 .xpo_detach = svc_tcp_sock_detach,
Tom Tucker755ccea2007-12-30 21:07:27 -06001184 .xpo_free = svc_sock_free,
Tom Tucker323bee32007-12-30 21:07:31 -06001185 .xpo_has_wspace = svc_tcp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -06001186 .xpo_accept = svc_tcp_accept,
Chuck Lever16e4d932014-05-19 13:40:22 -04001187 .xpo_secure_port = svc_sock_secure_port,
Scott Mayhewea08e392016-11-11 13:16:22 -05001188 .xpo_kill_temp_xprt = svc_tcp_kill_temp_xprt,
Tom Tucker360d87382007-12-30 21:07:17 -06001189};
1190
1191static struct svc_xprt_class svc_tcp_class = {
1192 .xcl_name = "tcp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001193 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -06001194 .xcl_ops = &svc_tcp_ops,
Tom Tucker49023152007-12-30 21:07:21 -06001195 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
Chuck Lever3c45ddf2014-07-16 15:38:32 -04001196 .xcl_ident = XPRT_TRANSPORT_TCP,
Tom Tucker360d87382007-12-30 21:07:17 -06001197};
1198
1199void svc_init_xprt_sock(void)
1200{
1201 svc_reg_xprt_class(&svc_tcp_class);
1202 svc_reg_xprt_class(&svc_udp_class);
1203}
1204
1205void svc_cleanup_xprt_sock(void)
1206{
1207 svc_unreg_xprt_class(&svc_tcp_class);
1208 svc_unreg_xprt_class(&svc_udp_class);
1209}
1210
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001211static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 struct sock *sk = svsk->sk_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +03001215 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
1216 &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -06001217 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Jeff Layton362142b22017-02-24 13:25:23 -05001218 set_bit(XPT_CONG_CTRL, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (sk->sk_state == TCP_LISTEN) {
1220 dprintk("setting up TCP socket for listening\n");
Chuck Leverece200d2018-03-27 10:51:00 -04001221 strcpy(svsk->sk_xprt.xpt_remotebuf, "listener");
Tom Tucker02fc6c32007-12-30 21:07:48 -06001222 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 sk->sk_data_ready = svc_tcp_listen_data_ready;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001224 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 } else {
1226 dprintk("setting up TCP socket for reading\n");
1227 sk->sk_state_change = svc_tcp_state_change;
Trond Myklebustfa9251a2016-06-24 10:55:48 -04001228 sk->sk_data_ready = svc_data_ready;
Trond Myklebust637600f2016-06-24 10:55:51 -04001229 sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 svsk->sk_reclen = 0;
1232 svsk->sk_tcplen = 0;
J. Bruce Fields8af345f2012-12-03 16:45:35 -05001233 svsk->sk_datalen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001234 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Chuck Leverb7872fe2008-04-14 12:27:01 -04001236 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Tom Tucker02fc6c32007-12-30 21:07:48 -06001238 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Trond Myklebustb2f21f72016-07-26 09:51:19 -04001239 switch (sk->sk_state) {
1240 case TCP_SYN_RECV:
1241 case TCP_ESTABLISHED:
1242 break;
1243 default:
Tom Tucker02fc6c32007-12-30 21:07:48 -06001244 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Trond Myklebustb2f21f72016-07-26 09:51:19 -04001245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247}
1248
Tom Tucker0f0257e2007-12-30 21:08:27 -06001249void svc_sock_update_bufs(struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 /*
1252 * The number of server threads has changed. Update
1253 * rcvbuf and sndbuf accordingly on all sockets
1254 */
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001255 struct svc_sock *svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 spin_lock_bh(&serv->sv_lock);
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001258 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001259 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 spin_unlock_bh(&serv->sv_lock);
1261}
Trond Myklebust24c37672008-12-23 16:30:12 -05001262EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 * Initialize socket for RPC use and create svc_sock struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 */
Chuck Lever6b174332007-02-12 00:53:28 -08001267static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1268 struct socket *sock,
J. Bruce Fields72c35372012-08-13 17:46:17 -04001269 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 struct svc_sock *svsk;
1272 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001273 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001274 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 dprintk("svc: svc_setup_socket %p\n", sock);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001277 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1278 if (!svsk)
1279 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 inet = sock->sk;
1282
1283 /* Register socket with portmapper */
J. Bruce Fields72c35372012-08-13 17:46:17 -04001284 if (pmap_register)
1285 err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001286 inet->sk_protocol,
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001287 ntohs(inet_sk(inet)->inet_sport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
J. Bruce Fields72c35372012-08-13 17:46:17 -04001289 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 kfree(svsk);
J. Bruce Fields72c35372012-08-13 17:46:17 -04001291 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 svsk->sk_sock = sock;
1295 svsk->sk_sk = inet;
1296 svsk->sk_ostate = inet->sk_state_change;
1297 svsk->sk_odata = inet->sk_data_ready;
1298 svsk->sk_owspace = inet->sk_write_space;
Vadim Lomovtseveebe53e2017-08-21 07:23:07 -04001299 /*
1300 * This barrier is necessary in order to prevent race condition
1301 * with svc_data_ready(), svc_listen_data_ready() and others
1302 * when calling callbacks above.
1303 */
1304 wmb();
1305 inet->sk_user_data = svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 /* Initialize the socket */
1308 if (sock->type == SOCK_DGRAM)
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001309 svc_udp_init(svsk, serv);
Trond Myklebust637600f2016-06-24 10:55:51 -04001310 else
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001311 svc_tcp_init(svsk, serv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Trond Myklebustc7995f82016-07-26 09:51:20 -04001313 dprintk("svc: svc_setup_socket created %p (inet %p), "
1314 "listen %d close %d\n",
1315 svsk, svsk->sk_sk,
1316 test_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags),
1317 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return svsk;
1320}
1321
Stanislav Kinsbursky30646392014-02-26 16:50:01 +03001322bool svc_alien_sock(struct net *net, int fd)
1323{
1324 int err;
1325 struct socket *sock = sockfd_lookup(fd, &err);
1326 bool ret = false;
1327
1328 if (!sock)
1329 goto out;
1330 if (sock_net(sock->sk) != net)
1331 ret = true;
1332 sockfd_put(sock);
1333out:
1334 return ret;
1335}
1336EXPORT_SYMBOL_GPL(svc_alien_sock);
1337
Chuck Leverbfba9ab2009-04-23 19:32:33 -04001338/**
1339 * svc_addsock - add a listener socket to an RPC service
1340 * @serv: pointer to RPC service to which to add a new listener
1341 * @fd: file descriptor of the new listener
1342 * @name_return: pointer to buffer to fill in with name of listener
1343 * @len: size of the buffer
Trond Myklebust4df493a2019-04-09 12:13:37 -04001344 * @cred: credential
Chuck Leverbfba9ab2009-04-23 19:32:33 -04001345 *
1346 * Fills in socket name and returns positive length of name if successful.
1347 * Name is terminated with '\n'. On error, returns a negative errno
1348 * value.
1349 */
1350int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
Trond Myklebust4df493a2019-04-09 12:13:37 -04001351 const size_t len, const struct cred *cred)
NeilBrownb41b66d2006-10-02 02:17:48 -07001352{
1353 int err = 0;
1354 struct socket *so = sockfd_lookup(fd, &err);
1355 struct svc_sock *svsk = NULL;
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001356 struct sockaddr_storage addr;
1357 struct sockaddr *sin = (struct sockaddr *)&addr;
1358 int salen;
NeilBrownb41b66d2006-10-02 02:17:48 -07001359
1360 if (!so)
1361 return err;
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001362 err = -EAFNOSUPPORT;
Aime Le Rouzic205ba422010-01-26 14:03:56 -05001363 if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001364 goto out;
1365 err = -EPROTONOSUPPORT;
1366 if (so->sk->sk_protocol != IPPROTO_TCP &&
NeilBrownb41b66d2006-10-02 02:17:48 -07001367 so->sk->sk_protocol != IPPROTO_UDP)
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001368 goto out;
1369 err = -EISCONN;
1370 if (so->state > SS_UNCONNECTED)
1371 goto out;
1372 err = -ENOENT;
1373 if (!try_module_get(THIS_MODULE))
1374 goto out;
1375 svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1376 if (IS_ERR(svsk)) {
1377 module_put(THIS_MODULE);
1378 err = PTR_ERR(svsk);
1379 goto out;
NeilBrownb41b66d2006-10-02 02:17:48 -07001380 }
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001381 salen = kernel_getsockname(svsk->sk_sock, sin);
1382 if (salen >= 0)
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001383 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
Trond Myklebust4df493a2019-04-09 12:13:37 -04001384 svsk->sk_xprt.xpt_cred = get_cred(cred);
J. Bruce Fields39b55302012-08-14 15:50:34 -04001385 svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
Chuck Levere7942b92009-04-23 19:32:48 -04001386 return svc_one_sock_name(svsk, name_return, len);
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001387out:
1388 sockfd_put(so);
1389 return err;
NeilBrownb41b66d2006-10-02 02:17:48 -07001390}
1391EXPORT_SYMBOL_GPL(svc_addsock);
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393/*
1394 * Create socket for RPC service.
1395 */
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001396static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1397 int protocol,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001398 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001399 struct sockaddr *sin, int len,
1400 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct svc_sock *svsk;
1403 struct socket *sock;
1404 int error;
1405 int type;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001406 struct sockaddr_storage addr;
1407 struct sockaddr *newsin = (struct sockaddr *)&addr;
1408 int newlen;
Trond Myklebustc69da772009-03-30 18:59:17 -04001409 int family;
1410 int val;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +03001411 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Chuck Leverad06e4b2007-02-12 00:53:32 -08001413 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1414 serv->sv_program->pg_name, protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001415 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1418 printk(KERN_WARNING "svc: only UDP and TCP "
1419 "sockets supported\n");
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001420 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Trond Myklebustc69da772009-03-30 18:59:17 -04001423 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1424 switch (sin->sa_family) {
1425 case AF_INET6:
1426 family = PF_INET6;
1427 break;
1428 case AF_INET:
1429 family = PF_INET;
1430 break;
1431 default:
1432 return ERR_PTR(-EINVAL);
1433 }
1434
Pavel Emelyanov14ec63c2010-09-29 16:06:57 +04001435 error = __sock_create(net, family, type, protocol, &sock, 1);
Chuck Lever77f1f672007-02-12 00:53:39 -08001436 if (error < 0)
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001437 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Peter Zijlstraed075362006-12-06 20:35:24 -08001439 svc_reclassify_socket(sock);
1440
Trond Myklebustc69da772009-03-30 18:59:17 -04001441 /*
1442 * If this is an PF_INET6 listener, we want to avoid
1443 * getting requests from IPv4 remotes. Those should
1444 * be shunted to a PF_INET listener via rpcbind.
1445 */
1446 val = 1;
1447 if (family == PF_INET6)
1448 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
1449 (char *)&val, sizeof(val));
1450
Eric Sesterhenn18114742006-09-28 14:37:07 -07001451 if (type == SOCK_STREAM)
Pavel Emelyanov4a17fd52012-04-19 03:39:36 +00001452 sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
Chuck Lever77f1f672007-02-12 00:53:39 -08001453 error = kernel_bind(sock, sin, len);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001454 if (error < 0)
1455 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001457 error = kernel_getsockname(sock, newsin);
Tom Tucker9dbc2402007-12-30 21:08:12 -06001458 if (error < 0)
1459 goto bummer;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001460 newlen = error;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001463 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 goto bummer;
1465 }
1466
J. Bruce Fields72c35372012-08-13 17:46:17 -04001467 svsk = svc_setup_socket(serv, sock, flags);
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001468 if (IS_ERR(svsk)) {
1469 error = PTR_ERR(svsk);
1470 goto bummer;
NeilBrowne79eff12007-02-12 00:53:30 -08001471 }
J. Bruce Fieldsa8e10072012-08-13 18:01:03 -04001472 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1473 return (struct svc_xprt *)svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474bummer:
1475 dprintk("svc: svc_create_socket error = %d\n", -error);
1476 sock_release(sock);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001477 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480/*
Tom Tucker755ccea2007-12-30 21:07:27 -06001481 * Detach the svc_sock from the socket so that no
1482 * more callbacks occur.
1483 */
1484static void svc_sock_detach(struct svc_xprt *xprt)
1485{
1486 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1487 struct sock *sk = svsk->sk_sk;
1488
1489 dprintk("svc: svc_sock_detach(%p)\n", svsk);
1490
1491 /* put back the old socket callbacks */
Trond Myklebust069c2252016-06-24 10:55:47 -04001492 lock_sock(sk);
Tom Tucker755ccea2007-12-30 21:07:27 -06001493 sk->sk_state_change = svsk->sk_ostate;
1494 sk->sk_data_ready = svsk->sk_odata;
1495 sk->sk_write_space = svsk->sk_owspace;
Trond Myklebust069c2252016-06-24 10:55:47 -04001496 sk->sk_user_data = NULL;
1497 release_sock(sk);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001498}
1499
1500/*
1501 * Disconnect the socket, and reset the callbacks
1502 */
1503static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1504{
1505 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1506
1507 dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk);
1508
1509 svc_sock_detach(xprt);
1510
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001511 if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1512 svc_tcp_clear_pages(svsk);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001513 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001514 }
Tom Tucker755ccea2007-12-30 21:07:27 -06001515}
1516
1517/*
1518 * Free the svc_sock's socket resources and the svc_sock itself.
1519 */
1520static void svc_sock_free(struct svc_xprt *xprt)
1521{
1522 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1523 dprintk("svc: svc_sock_free(%p)\n", svsk);
1524
Tom Tucker755ccea2007-12-30 21:07:27 -06001525 if (svsk->sk_sock->file)
1526 sockfd_put(svsk->sk_sock);
1527 else
1528 sock_release(svsk->sk_sock);
1529 kfree(svsk);
1530}