xref: /linux/net/sunrpc/svcsock.c (revision 4e2866b2baaddfff6069a2f18fc134c1d5a08f2b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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
9  * svc_xprt_enqueue procedure...
10  *
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 
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/module.h>
26 #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>
32 #include <linux/tcp.h>
33 #include <linux/unistd.h>
34 #include <linux/slab.h>
35 #include <linux/netdevice.h>
36 #include <linux/skbuff.h>
37 #include <linux/file.h>
38 #include <linux/freezer.h>
39 #include <linux/bvec.h>
40 
41 #include <net/sock.h>
42 #include <net/checksum.h>
43 #include <net/ip.h>
44 #include <net/ipv6.h>
45 #include <net/udp.h>
46 #include <net/tcp.h>
47 #include <net/tcp_states.h>
48 #include <net/tls_prot.h>
49 #include <net/handshake.h>
50 #include <linux/uaccess.h>
51 #include <linux/highmem.h>
52 #include <asm/ioctls.h>
53 #include <linux/key.h>
54 
55 #include <linux/sunrpc/types.h>
56 #include <linux/sunrpc/clnt.h>
57 #include <linux/sunrpc/xdr.h>
58 #include <linux/sunrpc/msg_prot.h>
59 #include <linux/sunrpc/svcsock.h>
60 #include <linux/sunrpc/stats.h>
61 #include <linux/sunrpc/xprt.h>
62 
63 #include <trace/events/sock.h>
64 #include <trace/events/sunrpc.h>
65 
66 #include "socklib.h"
67 #include "sunrpc.h"
68 
69 #define RPCDBG_FACILITY	RPCDBG_SVCXPRT
70 
71 /*
72  * For UDP:
73  * 1 for header page
74  * enough pages for RPCSVC_MAXPAYLOAD_UDP
75  * 1 in case payload is not aligned
76  * 1 for tail page
77  */
78 enum {
79 	SUNRPC_MAX_UDP_SENDPAGES = 1 + RPCSVC_MAXPAYLOAD_UDP / PAGE_SIZE + 1 + 1
80 };
81 
82 /* To-do: to avoid tying up an nfsd thread while waiting for a
83  * handshake request, the request could instead be deferred.
84  */
85 enum {
86 	SVC_HANDSHAKE_TO	= 5U * HZ
87 };
88 
89 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
90 					 int flags);
91 static int		svc_udp_recvfrom(struct svc_rqst *);
92 static int		svc_udp_sendto(struct svc_rqst *);
93 static void		svc_sock_detach(struct svc_xprt *);
94 static void		svc_tcp_sock_detach(struct svc_xprt *);
95 static void		svc_sock_free(struct svc_xprt *);
96 
97 static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
98 					  struct net *, struct sockaddr *,
99 					  int, int);
100 #ifdef CONFIG_DEBUG_LOCK_ALLOC
101 static struct lock_class_key svc_key[2];
102 static struct lock_class_key svc_slock_key[2];
103 
104 static void svc_reclassify_socket(struct socket *sock)
105 {
106 	struct sock *sk = sock->sk;
107 
108 	if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
109 		return;
110 
111 	switch (sk->sk_family) {
112 	case AF_INET:
113 		sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
114 					      &svc_slock_key[0],
115 					      "sk_xprt.xpt_lock-AF_INET-NFSD",
116 					      &svc_key[0]);
117 		break;
118 
119 	case AF_INET6:
120 		sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
121 					      &svc_slock_key[1],
122 					      "sk_xprt.xpt_lock-AF_INET6-NFSD",
123 					      &svc_key[1]);
124 		break;
125 
126 	default:
127 		BUG();
128 	}
129 }
130 #else
131 static void svc_reclassify_socket(struct socket *sock)
132 {
133 }
134 #endif
135 
136 /**
137  * svc_tcp_release_ctxt - Release transport-related resources
138  * @xprt: the transport which owned the context
139  * @ctxt: the context from rqstp->rq_xprt_ctxt or dr->xprt_ctxt
140  *
141  */
142 static void svc_tcp_release_ctxt(struct svc_xprt *xprt, void *ctxt)
143 {
144 }
145 
146 /**
147  * svc_udp_release_ctxt - Release transport-related resources
148  * @xprt: the transport which owned the context
149  * @ctxt: the context from rqstp->rq_xprt_ctxt or dr->xprt_ctxt
150  *
151  */
152 static void svc_udp_release_ctxt(struct svc_xprt *xprt, void *ctxt)
153 {
154 	struct sk_buff *skb = ctxt;
155 
156 	if (skb)
157 		consume_skb(skb);
158 }
159 
160 union svc_pktinfo_u {
161 	struct in_pktinfo pkti;
162 	struct in6_pktinfo pkti6;
163 };
164 #define SVC_PKTINFO_SPACE \
165 	CMSG_SPACE(sizeof(union svc_pktinfo_u))
166 
167 static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
168 {
169 	struct svc_sock *svsk =
170 		container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
171 	switch (svsk->sk_sk->sk_family) {
172 	case AF_INET: {
173 			struct in_pktinfo *pki = CMSG_DATA(cmh);
174 
175 			cmh->cmsg_level = SOL_IP;
176 			cmh->cmsg_type = IP_PKTINFO;
177 			pki->ipi_ifindex = 0;
178 			pki->ipi_spec_dst.s_addr =
179 				 svc_daddr_in(rqstp)->sin_addr.s_addr;
180 			cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
181 		}
182 		break;
183 
184 	case AF_INET6: {
185 			struct in6_pktinfo *pki = CMSG_DATA(cmh);
186 			struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
187 
188 			cmh->cmsg_level = SOL_IPV6;
189 			cmh->cmsg_type = IPV6_PKTINFO;
190 			pki->ipi6_ifindex = daddr->sin6_scope_id;
191 			pki->ipi6_addr = daddr->sin6_addr;
192 			cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
193 		}
194 		break;
195 	}
196 }
197 
198 static int svc_sock_result_payload(struct svc_rqst *rqstp, unsigned int offset,
199 				   unsigned int length)
200 {
201 	return 0;
202 }
203 
204 /*
205  * Report socket names for nfsdfs
206  */
207 static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
208 {
209 	const struct sock *sk = svsk->sk_sk;
210 	const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
211 							"udp" : "tcp";
212 	int len;
213 
214 	switch (sk->sk_family) {
215 	case PF_INET:
216 		len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
217 				proto_name,
218 				&inet_sk(sk)->inet_rcv_saddr,
219 				inet_sk(sk)->inet_num);
220 		break;
221 #if IS_ENABLED(CONFIG_IPV6)
222 	case PF_INET6:
223 		len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
224 				proto_name,
225 				&sk->sk_v6_rcv_saddr,
226 				inet_sk(sk)->inet_num);
227 		break;
228 #endif
229 	default:
230 		len = snprintf(buf, remaining, "*unknown-%d*\n",
231 				sk->sk_family);
232 	}
233 
234 	if (len >= remaining) {
235 		*buf = '\0';
236 		return -ENAMETOOLONG;
237 	}
238 	return len;
239 }
240 
241 static int
242 svc_tcp_sock_process_cmsg(struct socket *sock, struct msghdr *msg,
243 			  struct cmsghdr *cmsg, int ret)
244 {
245 	u8 content_type = tls_get_record_type(sock->sk, cmsg);
246 	u8 level, description;
247 
248 	switch (content_type) {
249 	case 0:
250 		break;
251 	case TLS_RECORD_TYPE_DATA:
252 		/* TLS sets EOR at the end of each application data
253 		 * record, even though there might be more frames
254 		 * waiting to be decrypted.
255 		 */
256 		msg->msg_flags &= ~MSG_EOR;
257 		break;
258 	case TLS_RECORD_TYPE_ALERT:
259 		tls_alert_recv(sock->sk, msg, &level, &description);
260 		ret = (level == TLS_ALERT_LEVEL_FATAL) ?
261 			-ENOTCONN : -EAGAIN;
262 		break;
263 	default:
264 		/* discard this record type */
265 		ret = -EAGAIN;
266 	}
267 	return ret;
268 }
269 
270 static int
271 svc_tcp_sock_recv_cmsg(struct socket *sock, unsigned int *msg_flags)
272 {
273 	union {
274 		struct cmsghdr	cmsg;
275 		u8		buf[CMSG_SPACE(sizeof(u8))];
276 	} u;
277 	u8 alert[2];
278 	struct kvec alert_kvec = {
279 		.iov_base = alert,
280 		.iov_len = sizeof(alert),
281 	};
282 	struct msghdr msg = {
283 		.msg_flags = *msg_flags,
284 		.msg_control = &u,
285 		.msg_controllen = sizeof(u),
286 	};
287 	int ret;
288 
289 	iov_iter_kvec(&msg.msg_iter, ITER_DEST, &alert_kvec, 1,
290 		      alert_kvec.iov_len);
291 	ret = sock_recvmsg(sock, &msg, MSG_DONTWAIT);
292 	if (ret > 0 &&
293 	    tls_get_record_type(sock->sk, &u.cmsg) == TLS_RECORD_TYPE_ALERT) {
294 		iov_iter_revert(&msg.msg_iter, ret);
295 		ret = svc_tcp_sock_process_cmsg(sock, &msg, &u.cmsg, -EAGAIN);
296 	}
297 	return ret;
298 }
299 
300 static int
301 svc_tcp_sock_recvmsg(struct svc_sock *svsk, struct msghdr *msg)
302 {
303 	int ret;
304 	struct socket *sock = svsk->sk_sock;
305 
306 	ret = sock_recvmsg(sock, msg, MSG_DONTWAIT);
307 	if (msg->msg_flags & MSG_CTRUNC) {
308 		msg->msg_flags &= ~(MSG_CTRUNC | MSG_EOR);
309 		if (ret == 0 || ret == -EIO)
310 			ret = svc_tcp_sock_recv_cmsg(sock, &msg->msg_flags);
311 	}
312 	return ret;
313 }
314 
315 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
316 static void svc_flush_bvec(const struct bio_vec *bvec, size_t size, size_t seek)
317 {
318 	struct bvec_iter bi = {
319 		.bi_size	= size + seek,
320 	};
321 	struct bio_vec bv;
322 
323 	bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
324 	for_each_bvec(bv, bvec, bi, bi)
325 		flush_dcache_page(bv.bv_page);
326 }
327 #else
328 static inline void svc_flush_bvec(const struct bio_vec *bvec, size_t size,
329 				  size_t seek)
330 {
331 }
332 #endif
333 
334 /*
335  * Read from @rqstp's transport socket. The incoming message fills whole
336  * pages in @rqstp's rq_pages array until the last page of the message
337  * has been received into a partial page.
338  */
339 static ssize_t svc_tcp_read_msg(struct svc_rqst *rqstp, size_t buflen,
340 				size_t seek)
341 {
342 	struct svc_sock *svsk =
343 		container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
344 	struct bio_vec *bvec = rqstp->rq_bvec;
345 	struct msghdr msg = { NULL };
346 	unsigned int i;
347 	ssize_t len;
348 	size_t t;
349 
350 	clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
351 
352 	for (i = 0, t = 0; t < buflen; i++, t += PAGE_SIZE)
353 		bvec_set_page(&bvec[i], rqstp->rq_pages[i], PAGE_SIZE, 0);
354 
355 	iov_iter_bvec(&msg.msg_iter, ITER_DEST, bvec, i, buflen);
356 	if (seek) {
357 		iov_iter_advance(&msg.msg_iter, seek);
358 		buflen -= seek;
359 	}
360 	len = svc_tcp_sock_recvmsg(svsk, &msg);
361 	if (len > 0)
362 		svc_flush_bvec(bvec, len, seek);
363 
364 	/* If we read a full record, then assume there may be more
365 	 * data to read (stream based sockets only!)
366 	 */
367 	if (len == buflen)
368 		set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
369 
370 	return len;
371 }
372 
373 /*
374  * Set socket snd and rcv buffer lengths
375  */
376 static void svc_sock_setbufsize(struct svc_sock *svsk, unsigned int nreqs)
377 {
378 	unsigned int max_mesg = svsk->sk_xprt.xpt_server->sv_max_mesg;
379 	struct socket *sock = svsk->sk_sock;
380 
381 	nreqs = min(nreqs, INT_MAX / 2 / max_mesg);
382 
383 	lock_sock(sock->sk);
384 	sock->sk->sk_sndbuf = nreqs * max_mesg * 2;
385 	sock->sk->sk_rcvbuf = nreqs * max_mesg * 2;
386 	sock->sk->sk_write_space(sock->sk);
387 	release_sock(sock->sk);
388 }
389 
390 static void svc_sock_secure_port(struct svc_rqst *rqstp)
391 {
392 	if (svc_port_is_privileged(svc_addr(rqstp)))
393 		set_bit(RQ_SECURE, &rqstp->rq_flags);
394 	else
395 		clear_bit(RQ_SECURE, &rqstp->rq_flags);
396 }
397 
398 /*
399  * INET callback when data has been received on the socket.
400  */
401 static void svc_data_ready(struct sock *sk)
402 {
403 	struct svc_sock	*svsk = (struct svc_sock *)sk->sk_user_data;
404 
405 	trace_sk_data_ready(sk);
406 
407 	if (svsk) {
408 		/* Refer to svc_setup_socket() for details. */
409 		rmb();
410 		svsk->sk_odata(sk);
411 		trace_svcsock_data_ready(&svsk->sk_xprt, 0);
412 		if (test_bit(XPT_HANDSHAKE, &svsk->sk_xprt.xpt_flags))
413 			return;
414 		if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags))
415 			svc_xprt_enqueue(&svsk->sk_xprt);
416 	}
417 }
418 
419 /*
420  * INET callback when space is newly available on the socket.
421  */
422 static void svc_write_space(struct sock *sk)
423 {
424 	struct svc_sock	*svsk = (struct svc_sock *)(sk->sk_user_data);
425 
426 	if (svsk) {
427 		/* Refer to svc_setup_socket() for details. */
428 		rmb();
429 		trace_svcsock_write_space(&svsk->sk_xprt, 0);
430 		svsk->sk_owspace(sk);
431 		svc_xprt_enqueue(&svsk->sk_xprt);
432 	}
433 }
434 
435 static int svc_tcp_has_wspace(struct svc_xprt *xprt)
436 {
437 	struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
438 
439 	if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
440 		return 1;
441 	return !test_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
442 }
443 
444 static void svc_tcp_kill_temp_xprt(struct svc_xprt *xprt)
445 {
446 	struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
447 
448 	sock_no_linger(svsk->sk_sock->sk);
449 }
450 
451 /**
452  * svc_tcp_handshake_done - Handshake completion handler
453  * @data: address of xprt to wake
454  * @status: status of handshake
455  * @peerid: serial number of key containing the remote peer's identity
456  *
457  * If a security policy is specified as an export option, we don't
458  * have a specific export here to check. So we set a "TLS session
459  * is present" flag on the xprt and let an upper layer enforce local
460  * security policy.
461  */
462 static void svc_tcp_handshake_done(void *data, int status, key_serial_t peerid)
463 {
464 	struct svc_xprt *xprt = data;
465 	struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
466 
467 	if (!status) {
468 		if (peerid != TLS_NO_PEERID)
469 			set_bit(XPT_PEER_AUTH, &xprt->xpt_flags);
470 		set_bit(XPT_TLS_SESSION, &xprt->xpt_flags);
471 	}
472 	clear_bit(XPT_HANDSHAKE, &xprt->xpt_flags);
473 	complete_all(&svsk->sk_handshake_done);
474 }
475 
476 /**
477  * svc_tcp_handshake - Perform a transport-layer security handshake
478  * @xprt: connected transport endpoint
479  *
480  */
481 static void svc_tcp_handshake(struct svc_xprt *xprt)
482 {
483 	struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
484 	struct sock *sk = svsk->sk_sock->sk;
485 	struct tls_handshake_args args = {
486 		.ta_sock	= svsk->sk_sock,
487 		.ta_done	= svc_tcp_handshake_done,
488 		.ta_data	= xprt,
489 	};
490 	int ret;
491 
492 	trace_svc_tls_upcall(xprt);
493 
494 	clear_bit(XPT_TLS_SESSION, &xprt->xpt_flags);
495 	init_completion(&svsk->sk_handshake_done);
496 
497 	ret = tls_server_hello_x509(&args, GFP_KERNEL);
498 	if (ret) {
499 		trace_svc_tls_not_started(xprt);
500 		goto out_failed;
501 	}
502 
503 	ret = wait_for_completion_interruptible_timeout(&svsk->sk_handshake_done,
504 							SVC_HANDSHAKE_TO);
505 	if (ret <= 0) {
506 		if (tls_handshake_cancel(sk)) {
507 			trace_svc_tls_timed_out(xprt);
508 			goto out_close;
509 		}
510 	}
511 
512 	if (!test_bit(XPT_TLS_SESSION, &xprt->xpt_flags)) {
513 		trace_svc_tls_unavailable(xprt);
514 		goto out_close;
515 	}
516 
517 	/* Mark the transport ready in case the remote sent RPC
518 	 * traffic before the kernel received the handshake
519 	 * completion downcall.
520 	 */
521 	set_bit(XPT_DATA, &xprt->xpt_flags);
522 	svc_xprt_enqueue(xprt);
523 	return;
524 
525 out_close:
526 	set_bit(XPT_CLOSE, &xprt->xpt_flags);
527 out_failed:
528 	clear_bit(XPT_HANDSHAKE, &xprt->xpt_flags);
529 	set_bit(XPT_DATA, &xprt->xpt_flags);
530 	svc_xprt_enqueue(xprt);
531 }
532 
533 /*
534  * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
535  */
536 static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
537 				     struct cmsghdr *cmh)
538 {
539 	struct in_pktinfo *pki = CMSG_DATA(cmh);
540 	struct sockaddr_in *daddr = svc_daddr_in(rqstp);
541 
542 	if (cmh->cmsg_type != IP_PKTINFO)
543 		return 0;
544 
545 	daddr->sin_family = AF_INET;
546 	daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
547 	return 1;
548 }
549 
550 /*
551  * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
552  */
553 static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
554 				     struct cmsghdr *cmh)
555 {
556 	struct in6_pktinfo *pki = CMSG_DATA(cmh);
557 	struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
558 
559 	if (cmh->cmsg_type != IPV6_PKTINFO)
560 		return 0;
561 
562 	daddr->sin6_family = AF_INET6;
563 	daddr->sin6_addr = pki->ipi6_addr;
564 	daddr->sin6_scope_id = pki->ipi6_ifindex;
565 	return 1;
566 }
567 
568 /*
569  * Copy the UDP datagram's destination address to the rqstp structure.
570  * The 'destination' address in this case is the address to which the
571  * peer sent the datagram, i.e. our local address. For multihomed
572  * hosts, this can change from msg to msg. Note that only the IP
573  * address changes, the port number should remain the same.
574  */
575 static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
576 				    struct cmsghdr *cmh)
577 {
578 	switch (cmh->cmsg_level) {
579 	case SOL_IP:
580 		return svc_udp_get_dest_address4(rqstp, cmh);
581 	case SOL_IPV6:
582 		return svc_udp_get_dest_address6(rqstp, cmh);
583 	}
584 
585 	return 0;
586 }
587 
588 /**
589  * svc_udp_recvfrom - Receive a datagram from a UDP socket.
590  * @rqstp: request structure into which to receive an RPC Call
591  *
592  * Called in a loop when XPT_DATA has been set.
593  *
594  * Returns:
595  *   On success, the number of bytes in a received RPC Call, or
596  *   %0 if a complete RPC Call message was not ready to return
597  */
598 static int svc_udp_recvfrom(struct svc_rqst *rqstp)
599 {
600 	struct svc_sock	*svsk =
601 		container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
602 	struct svc_serv	*serv = svsk->sk_xprt.xpt_server;
603 	struct sk_buff	*skb;
604 	union {
605 		struct cmsghdr	hdr;
606 		long		all[SVC_PKTINFO_SPACE / sizeof(long)];
607 	} buffer;
608 	struct cmsghdr *cmh = &buffer.hdr;
609 	struct msghdr msg = {
610 		.msg_name = svc_addr(rqstp),
611 		.msg_control = cmh,
612 		.msg_controllen = sizeof(buffer),
613 		.msg_flags = MSG_DONTWAIT,
614 	};
615 	size_t len;
616 	int err;
617 
618 	if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
619 	    /* udp sockets need large rcvbuf as all pending
620 	     * requests are still in that buffer.  sndbuf must
621 	     * also be large enough that there is enough space
622 	     * for one reply per thread.  We count all threads
623 	     * rather than threads in a particular pool, which
624 	     * provides an upper bound on the number of threads
625 	     * which will access the socket.
626 	     */
627 	    svc_sock_setbufsize(svsk, serv->sv_nrthreads + 3);
628 
629 	clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
630 	err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
631 			     0, 0, MSG_PEEK | MSG_DONTWAIT);
632 	if (err < 0)
633 		goto out_recv_err;
634 	skb = skb_recv_udp(svsk->sk_sk, MSG_DONTWAIT, &err);
635 	if (!skb)
636 		goto out_recv_err;
637 
638 	len = svc_addr_len(svc_addr(rqstp));
639 	rqstp->rq_addrlen = len;
640 	if (skb->tstamp == 0) {
641 		skb->tstamp = ktime_get_real();
642 		/* Don't enable netstamp, sunrpc doesn't
643 		   need that much accuracy */
644 	}
645 	sock_write_timestamp(svsk->sk_sk, skb->tstamp);
646 	set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
647 
648 	len = skb->len;
649 	rqstp->rq_arg.len = len;
650 	trace_svcsock_udp_recv(&svsk->sk_xprt, len);
651 
652 	rqstp->rq_prot = IPPROTO_UDP;
653 
654 	if (!svc_udp_get_dest_address(rqstp, cmh))
655 		goto out_cmsg_err;
656 	rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
657 
658 	if (skb_is_nonlinear(skb)) {
659 		/* we have to copy */
660 		local_bh_disable();
661 		if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb))
662 			goto out_bh_enable;
663 		local_bh_enable();
664 		consume_skb(skb);
665 	} else {
666 		/* we can use it in-place */
667 		rqstp->rq_arg.head[0].iov_base = skb->data;
668 		rqstp->rq_arg.head[0].iov_len = len;
669 		if (skb_checksum_complete(skb))
670 			goto out_free;
671 		rqstp->rq_xprt_ctxt = skb;
672 	}
673 
674 	rqstp->rq_arg.page_base = 0;
675 	if (len <= rqstp->rq_arg.head[0].iov_len) {
676 		rqstp->rq_arg.head[0].iov_len = len;
677 		rqstp->rq_arg.page_len = 0;
678 	} else {
679 		rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
680 	}
681 
682 	if (serv->sv_stats)
683 		serv->sv_stats->netudpcnt++;
684 
685 	svc_sock_secure_port(rqstp);
686 	svc_xprt_received(rqstp->rq_xprt);
687 	return len;
688 
689 out_recv_err:
690 	if (err != -EAGAIN) {
691 		/* possibly an icmp error */
692 		set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
693 	}
694 	trace_svcsock_udp_recv_err(&svsk->sk_xprt, err);
695 	goto out_clear_busy;
696 out_cmsg_err:
697 	net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
698 			     cmh->cmsg_level, cmh->cmsg_type);
699 	goto out_free;
700 out_bh_enable:
701 	local_bh_enable();
702 out_free:
703 	kfree_skb(skb);
704 out_clear_busy:
705 	svc_xprt_received(rqstp->rq_xprt);
706 	return 0;
707 }
708 
709 /**
710  * svc_udp_sendto - Send out a reply on a UDP socket
711  * @rqstp: completed svc_rqst
712  *
713  * xpt_mutex ensures @rqstp's whole message is written to the socket
714  * without interruption.
715  *
716  * Returns the number of bytes sent, or a negative errno.
717  */
718 static int svc_udp_sendto(struct svc_rqst *rqstp)
719 {
720 	struct svc_xprt *xprt = rqstp->rq_xprt;
721 	struct svc_sock	*svsk = container_of(xprt, struct svc_sock, sk_xprt);
722 	struct xdr_buf *xdr = &rqstp->rq_res;
723 	union {
724 		struct cmsghdr	hdr;
725 		long		all[SVC_PKTINFO_SPACE / sizeof(long)];
726 	} buffer;
727 	struct cmsghdr *cmh = &buffer.hdr;
728 	struct msghdr msg = {
729 		.msg_name	= &rqstp->rq_addr,
730 		.msg_namelen	= rqstp->rq_addrlen,
731 		.msg_control	= cmh,
732 		.msg_flags	= MSG_SPLICE_PAGES,
733 		.msg_controllen	= sizeof(buffer),
734 	};
735 	unsigned int count;
736 	int err;
737 
738 	svc_udp_release_ctxt(xprt, rqstp->rq_xprt_ctxt);
739 	rqstp->rq_xprt_ctxt = NULL;
740 
741 	svc_set_cmsg_data(rqstp, cmh);
742 
743 	mutex_lock(&xprt->xpt_mutex);
744 
745 	if (svc_xprt_is_dead(xprt))
746 		goto out_notconn;
747 
748 	count = xdr_buf_to_bvec(svsk->sk_bvec, SUNRPC_MAX_UDP_SENDPAGES, xdr);
749 
750 	iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, svsk->sk_bvec,
751 		      count, rqstp->rq_res.len);
752 	err = sock_sendmsg(svsk->sk_sock, &msg);
753 	if (err == -ECONNREFUSED) {
754 		/* ICMP error on earlier request. */
755 		iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, svsk->sk_bvec,
756 			      count, rqstp->rq_res.len);
757 		err = sock_sendmsg(svsk->sk_sock, &msg);
758 	}
759 
760 	trace_svcsock_udp_send(xprt, err);
761 
762 	mutex_unlock(&xprt->xpt_mutex);
763 	return err;
764 
765 out_notconn:
766 	mutex_unlock(&xprt->xpt_mutex);
767 	return -ENOTCONN;
768 }
769 
770 static int svc_udp_has_wspace(struct svc_xprt *xprt)
771 {
772 	struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
773 	struct svc_serv	*serv = xprt->xpt_server;
774 	unsigned long required;
775 
776 	/*
777 	 * Set the SOCK_NOSPACE flag before checking the available
778 	 * sock space.
779 	 */
780 	set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
781 	required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
782 	if (required*2 > sock_wspace(svsk->sk_sk))
783 		return 0;
784 	clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
785 	return 1;
786 }
787 
788 static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
789 {
790 	BUG();
791 	return NULL;
792 }
793 
794 static void svc_udp_kill_temp_xprt(struct svc_xprt *xprt)
795 {
796 }
797 
798 static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
799 				       struct net *net,
800 				       struct sockaddr *sa, int salen,
801 				       int flags)
802 {
803 	return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
804 }
805 
806 static const struct svc_xprt_ops svc_udp_ops = {
807 	.xpo_create = svc_udp_create,
808 	.xpo_recvfrom = svc_udp_recvfrom,
809 	.xpo_sendto = svc_udp_sendto,
810 	.xpo_result_payload = svc_sock_result_payload,
811 	.xpo_release_ctxt = svc_udp_release_ctxt,
812 	.xpo_detach = svc_sock_detach,
813 	.xpo_free = svc_sock_free,
814 	.xpo_has_wspace = svc_udp_has_wspace,
815 	.xpo_accept = svc_udp_accept,
816 	.xpo_kill_temp_xprt = svc_udp_kill_temp_xprt,
817 };
818 
819 static struct svc_xprt_class svc_udp_class = {
820 	.xcl_name = "udp",
821 	.xcl_owner = THIS_MODULE,
822 	.xcl_ops = &svc_udp_ops,
823 	.xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
824 	.xcl_ident = XPRT_TRANSPORT_UDP,
825 };
826 
827 static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
828 {
829 	svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
830 		      &svsk->sk_xprt, serv);
831 	clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
832 	svsk->sk_sk->sk_data_ready = svc_data_ready;
833 	svsk->sk_sk->sk_write_space = svc_write_space;
834 
835 	/* initialise setting must have enough space to
836 	 * receive and respond to one request.
837 	 * svc_udp_recvfrom will re-adjust if necessary
838 	 */
839 	svc_sock_setbufsize(svsk, 3);
840 
841 	/* data might have come in before data_ready set up */
842 	set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
843 	set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
844 	set_bit(XPT_RPCB_UNREG, &svsk->sk_xprt.xpt_flags);
845 
846 	/* make sure we get destination address info */
847 	switch (svsk->sk_sk->sk_family) {
848 	case AF_INET:
849 		ip_sock_set_pktinfo(svsk->sk_sock->sk);
850 		break;
851 	case AF_INET6:
852 		ip6_sock_set_recvpktinfo(svsk->sk_sock->sk);
853 		break;
854 	default:
855 		BUG();
856 	}
857 }
858 
859 /*
860  * A data_ready event on a listening socket means there's a connection
861  * pending. Do not use state_change as a substitute for it.
862  */
863 static void svc_tcp_listen_data_ready(struct sock *sk)
864 {
865 	struct svc_sock	*svsk = (struct svc_sock *)sk->sk_user_data;
866 
867 	trace_sk_data_ready(sk);
868 
869 	/*
870 	 * This callback may called twice when a new connection
871 	 * is established as a child socket inherits everything
872 	 * from a parent LISTEN socket.
873 	 * 1) data_ready method of the parent socket will be called
874 	 *    when one of child sockets become ESTABLISHED.
875 	 * 2) data_ready method of the child socket may be called
876 	 *    when it receives data before the socket is accepted.
877 	 * In case of 2, we should ignore it silently and DO NOT
878 	 * dereference svsk.
879 	 */
880 	if (sk->sk_state != TCP_LISTEN)
881 		return;
882 
883 	if (svsk) {
884 		/* Refer to svc_setup_socket() for details. */
885 		rmb();
886 		svsk->sk_odata(sk);
887 		set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
888 		svc_xprt_enqueue(&svsk->sk_xprt);
889 	}
890 }
891 
892 /*
893  * A state change on a connected socket means it's dying or dead.
894  */
895 static void svc_tcp_state_change(struct sock *sk)
896 {
897 	struct svc_sock	*svsk = (struct svc_sock *)sk->sk_user_data;
898 
899 	if (svsk) {
900 		/* Refer to svc_setup_socket() for details. */
901 		rmb();
902 		svsk->sk_ostate(sk);
903 		trace_svcsock_tcp_state(&svsk->sk_xprt, svsk->sk_sock);
904 		if (sk->sk_state != TCP_ESTABLISHED)
905 			svc_xprt_deferred_close(&svsk->sk_xprt);
906 	}
907 }
908 
909 /*
910  * Accept a TCP connection
911  */
912 static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
913 {
914 	struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
915 	struct sockaddr_storage addr;
916 	struct sockaddr	*sin = (struct sockaddr *) &addr;
917 	struct svc_serv	*serv = svsk->sk_xprt.xpt_server;
918 	struct socket	*sock = svsk->sk_sock;
919 	struct socket	*newsock;
920 	struct svc_sock	*newsvsk;
921 	int		err, slen;
922 
923 	if (!sock)
924 		return NULL;
925 
926 	clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
927 	err = kernel_accept(sock, &newsock, O_NONBLOCK);
928 	if (err < 0) {
929 		if (err != -EAGAIN)
930 			trace_svcsock_accept_err(xprt, serv->sv_name, err);
931 		return NULL;
932 	}
933 	if (IS_ERR(sock_alloc_file(newsock, O_NONBLOCK, NULL)))
934 		return NULL;
935 
936 	set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
937 
938 	err = kernel_getpeername(newsock, sin);
939 	if (err < 0) {
940 		trace_svcsock_getpeername_err(xprt, serv->sv_name, err);
941 		goto failed;		/* aborted connection or whatever */
942 	}
943 	slen = err;
944 
945 	/* Reset the inherited callbacks before calling svc_setup_socket */
946 	newsock->sk->sk_state_change = svsk->sk_ostate;
947 	newsock->sk->sk_data_ready = svsk->sk_odata;
948 	newsock->sk->sk_write_space = svsk->sk_owspace;
949 
950 	/* make sure that a write doesn't block forever when
951 	 * low on memory
952 	 */
953 	newsock->sk->sk_sndtimeo = HZ*30;
954 
955 	newsvsk = svc_setup_socket(serv, newsock,
956 				 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
957 	if (IS_ERR(newsvsk))
958 		goto failed;
959 	svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
960 	err = kernel_getsockname(newsock, sin);
961 	slen = err;
962 	if (unlikely(err < 0))
963 		slen = offsetof(struct sockaddr, sa_data);
964 	svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
965 
966 	if (sock_is_loopback(newsock->sk))
967 		set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
968 	else
969 		clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
970 	if (serv->sv_stats)
971 		serv->sv_stats->nettcpconn++;
972 
973 	return &newsvsk->sk_xprt;
974 
975 failed:
976 	sockfd_put(newsock);
977 	return NULL;
978 }
979 
980 static size_t svc_tcp_restore_pages(struct svc_sock *svsk,
981 				    struct svc_rqst *rqstp)
982 {
983 	size_t len = svsk->sk_datalen;
984 	unsigned int i, npages;
985 
986 	if (!len)
987 		return 0;
988 	npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
989 	for (i = 0; i < npages; i++) {
990 		if (rqstp->rq_pages[i] != NULL)
991 			svc_rqst_page_release(rqstp, rqstp->rq_pages[i]);
992 		BUG_ON(svsk->sk_pages[i] == NULL);
993 		rqstp->rq_pages[i] = svsk->sk_pages[i];
994 		svsk->sk_pages[i] = NULL;
995 	}
996 	rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
997 	return len;
998 }
999 
1000 static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
1001 {
1002 	unsigned int i, len, npages;
1003 
1004 	if (svsk->sk_datalen == 0)
1005 		return;
1006 	len = svsk->sk_datalen;
1007 	npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1008 	for (i = 0; i < npages; i++) {
1009 		svsk->sk_pages[i] = rqstp->rq_pages[i];
1010 		rqstp->rq_pages[i] = NULL;
1011 	}
1012 	rqstp->rq_pages_nfree = npages;
1013 }
1014 
1015 static void svc_tcp_clear_pages(struct svc_sock *svsk)
1016 {
1017 	unsigned int i, len, npages;
1018 
1019 	if (svsk->sk_datalen == 0)
1020 		goto out;
1021 	len = svsk->sk_datalen;
1022 	npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1023 	for (i = 0; i < npages; i++) {
1024 		if (svsk->sk_pages[i] == NULL) {
1025 			WARN_ON_ONCE(1);
1026 			continue;
1027 		}
1028 		put_page(svsk->sk_pages[i]);
1029 		svsk->sk_pages[i] = NULL;
1030 	}
1031 out:
1032 	svsk->sk_tcplen = 0;
1033 	svsk->sk_datalen = 0;
1034 }
1035 
1036 /*
1037  * Receive fragment record header into sk_marker.
1038  */
1039 static ssize_t svc_tcp_read_marker(struct svc_sock *svsk,
1040 				   struct svc_rqst *rqstp)
1041 {
1042 	ssize_t want, len;
1043 
1044 	/* If we haven't gotten the record length yet,
1045 	 * get the next four bytes.
1046 	 */
1047 	if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
1048 		struct msghdr	msg = { NULL };
1049 		struct kvec	iov;
1050 
1051 		want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
1052 		iov.iov_base = ((char *)&svsk->sk_marker) + svsk->sk_tcplen;
1053 		iov.iov_len  = want;
1054 		iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, want);
1055 		len = svc_tcp_sock_recvmsg(svsk, &msg);
1056 		if (len < 0)
1057 			return len;
1058 		svsk->sk_tcplen += len;
1059 		if (len < want) {
1060 			/* call again to read the remaining bytes */
1061 			goto err_short;
1062 		}
1063 		trace_svcsock_marker(&svsk->sk_xprt, svsk->sk_marker);
1064 		if (svc_sock_reclen(svsk) + svsk->sk_datalen >
1065 		    svsk->sk_xprt.xpt_server->sv_max_mesg)
1066 			goto err_too_large;
1067 	}
1068 	return svc_sock_reclen(svsk);
1069 
1070 err_too_large:
1071 	net_notice_ratelimited("svc: %s oversized RPC fragment (%u octets) from %pISpc\n",
1072 			       svsk->sk_xprt.xpt_server->sv_name,
1073 			       svc_sock_reclen(svsk),
1074 			       (struct sockaddr *)&svsk->sk_xprt.xpt_remote);
1075 	svc_xprt_deferred_close(&svsk->sk_xprt);
1076 err_short:
1077 	return -EAGAIN;
1078 }
1079 
1080 static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
1081 {
1082 	struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
1083 	struct rpc_rqst *req = NULL;
1084 	struct kvec *src, *dst;
1085 	__be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1086 	__be32 xid = *p;
1087 
1088 	if (!bc_xprt)
1089 		return -EAGAIN;
1090 	spin_lock(&bc_xprt->queue_lock);
1091 	req = xprt_lookup_rqst(bc_xprt, xid);
1092 	if (!req)
1093 		goto unlock_eagain;
1094 
1095 	memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
1096 	/*
1097 	 * XXX!: cheating for now!  Only copying HEAD.
1098 	 * But we know this is good enough for now (in fact, for any
1099 	 * callback reply in the forseeable future).
1100 	 */
1101 	dst = &req->rq_private_buf.head[0];
1102 	src = &rqstp->rq_arg.head[0];
1103 	if (dst->iov_len < src->iov_len)
1104 		goto unlock_eagain; /* whatever; just giving up. */
1105 	memcpy(dst->iov_base, src->iov_base, src->iov_len);
1106 	xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len);
1107 	rqstp->rq_arg.len = 0;
1108 	spin_unlock(&bc_xprt->queue_lock);
1109 	return 0;
1110 unlock_eagain:
1111 	spin_unlock(&bc_xprt->queue_lock);
1112 	return -EAGAIN;
1113 }
1114 
1115 static void svc_tcp_fragment_received(struct svc_sock *svsk)
1116 {
1117 	/* If we have more data, signal svc_xprt_enqueue() to try again */
1118 	svsk->sk_tcplen = 0;
1119 	svsk->sk_marker = xdr_zero;
1120 }
1121 
1122 /**
1123  * svc_tcp_recvfrom - Receive data from a TCP socket
1124  * @rqstp: request structure into which to receive an RPC Call
1125  *
1126  * Called in a loop when XPT_DATA has been set.
1127  *
1128  * Read the 4-byte stream record marker, then use the record length
1129  * in that marker to set up exactly the resources needed to receive
1130  * the next RPC message into @rqstp.
1131  *
1132  * Returns:
1133  *   On success, the number of bytes in a received RPC Call, or
1134  *   %0 if a complete RPC Call message was not ready to return
1135  *
1136  * The zero return case handles partial receives and callback Replies.
1137  * The state of a partial receive is preserved in the svc_sock for
1138  * the next call to svc_tcp_recvfrom.
1139  */
1140 static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
1141 {
1142 	struct svc_sock	*svsk =
1143 		container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
1144 	struct svc_serv	*serv = svsk->sk_xprt.xpt_server;
1145 	size_t want, base;
1146 	ssize_t len;
1147 	__be32 *p;
1148 	__be32 calldir;
1149 
1150 	clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1151 	len = svc_tcp_read_marker(svsk, rqstp);
1152 	if (len < 0)
1153 		goto error;
1154 
1155 	base = svc_tcp_restore_pages(svsk, rqstp);
1156 	want = len - (svsk->sk_tcplen - sizeof(rpc_fraghdr));
1157 	len = svc_tcp_read_msg(rqstp, base + want, base);
1158 	if (len >= 0) {
1159 		trace_svcsock_tcp_recv(&svsk->sk_xprt, len);
1160 		svsk->sk_tcplen += len;
1161 		svsk->sk_datalen += len;
1162 	}
1163 	if (len != want || !svc_sock_final_rec(svsk))
1164 		goto err_incomplete;
1165 	if (svsk->sk_datalen < 8)
1166 		goto err_nuts;
1167 
1168 	rqstp->rq_arg.len = svsk->sk_datalen;
1169 	rqstp->rq_arg.page_base = 0;
1170 	if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1171 		rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
1172 		rqstp->rq_arg.page_len = 0;
1173 	} else
1174 		rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1175 
1176 	rqstp->rq_xprt_ctxt   = NULL;
1177 	rqstp->rq_prot	      = IPPROTO_TCP;
1178 	if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags))
1179 		set_bit(RQ_LOCAL, &rqstp->rq_flags);
1180 	else
1181 		clear_bit(RQ_LOCAL, &rqstp->rq_flags);
1182 
1183 	p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1184 	calldir = p[1];
1185 	if (calldir)
1186 		len = receive_cb_reply(svsk, rqstp);
1187 
1188 	/* Reset TCP read info */
1189 	svsk->sk_datalen = 0;
1190 	svc_tcp_fragment_received(svsk);
1191 
1192 	if (len < 0)
1193 		goto error;
1194 
1195 	svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
1196 	if (serv->sv_stats)
1197 		serv->sv_stats->nettcpcnt++;
1198 
1199 	svc_sock_secure_port(rqstp);
1200 	svc_xprt_received(rqstp->rq_xprt);
1201 	return rqstp->rq_arg.len;
1202 
1203 err_incomplete:
1204 	svc_tcp_save_pages(svsk, rqstp);
1205 	if (len < 0 && len != -EAGAIN)
1206 		goto err_delete;
1207 	if (len == want)
1208 		svc_tcp_fragment_received(svsk);
1209 	else
1210 		trace_svcsock_tcp_recv_short(&svsk->sk_xprt,
1211 				svc_sock_reclen(svsk),
1212 				svsk->sk_tcplen - sizeof(rpc_fraghdr));
1213 	goto err_noclose;
1214 error:
1215 	if (len != -EAGAIN)
1216 		goto err_delete;
1217 	trace_svcsock_tcp_recv_eagain(&svsk->sk_xprt, 0);
1218 	goto err_noclose;
1219 err_nuts:
1220 	svsk->sk_datalen = 0;
1221 err_delete:
1222 	trace_svcsock_tcp_recv_err(&svsk->sk_xprt, len);
1223 	svc_xprt_deferred_close(&svsk->sk_xprt);
1224 err_noclose:
1225 	svc_xprt_received(rqstp->rq_xprt);
1226 	return 0;	/* record not complete */
1227 }
1228 
1229 /*
1230  * MSG_SPLICE_PAGES is used exclusively to reduce the number of
1231  * copy operations in this path. Therefore the caller must ensure
1232  * that the pages backing @xdr are unchanging.
1233  */
1234 static int svc_tcp_sendmsg(struct svc_sock *svsk, struct svc_rqst *rqstp,
1235 			   rpc_fraghdr marker)
1236 {
1237 	struct msghdr msg = {
1238 		.msg_flags	= MSG_SPLICE_PAGES,
1239 	};
1240 	unsigned int count;
1241 	void *buf;
1242 	int ret;
1243 
1244 	/* The stream record marker is copied into a temporary page
1245 	 * fragment buffer so that it can be included in sk_bvec.
1246 	 */
1247 	buf = page_frag_alloc(&svsk->sk_frag_cache, sizeof(marker),
1248 			      GFP_KERNEL);
1249 	if (!buf)
1250 		return -ENOMEM;
1251 	memcpy(buf, &marker, sizeof(marker));
1252 	bvec_set_virt(svsk->sk_bvec, buf, sizeof(marker));
1253 
1254 	count = xdr_buf_to_bvec(svsk->sk_bvec + 1, rqstp->rq_maxpages,
1255 				&rqstp->rq_res);
1256 
1257 	iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, svsk->sk_bvec,
1258 		      1 + count, sizeof(marker) + rqstp->rq_res.len);
1259 	ret = sock_sendmsg(svsk->sk_sock, &msg);
1260 	page_frag_free(buf);
1261 	return ret;
1262 }
1263 
1264 /**
1265  * svc_tcp_sendto - Send out a reply on a TCP socket
1266  * @rqstp: completed svc_rqst
1267  *
1268  * xpt_mutex ensures @rqstp's whole message is written to the socket
1269  * without interruption.
1270  *
1271  * Returns the number of bytes sent, or a negative errno.
1272  */
1273 static int svc_tcp_sendto(struct svc_rqst *rqstp)
1274 {
1275 	struct svc_xprt *xprt = rqstp->rq_xprt;
1276 	struct svc_sock	*svsk = container_of(xprt, struct svc_sock, sk_xprt);
1277 	struct xdr_buf *xdr = &rqstp->rq_res;
1278 	rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
1279 					 (u32)xdr->len);
1280 	int sent;
1281 
1282 	svc_tcp_release_ctxt(xprt, rqstp->rq_xprt_ctxt);
1283 	rqstp->rq_xprt_ctxt = NULL;
1284 
1285 	mutex_lock(&xprt->xpt_mutex);
1286 	if (svc_xprt_is_dead(xprt))
1287 		goto out_notconn;
1288 	sent = svc_tcp_sendmsg(svsk, rqstp, marker);
1289 	trace_svcsock_tcp_send(xprt, sent);
1290 	if (sent < 0 || sent != (xdr->len + sizeof(marker)))
1291 		goto out_close;
1292 	mutex_unlock(&xprt->xpt_mutex);
1293 	return sent;
1294 
1295 out_notconn:
1296 	mutex_unlock(&xprt->xpt_mutex);
1297 	return -ENOTCONN;
1298 out_close:
1299 	pr_notice("rpc-srv/tcp: %s: %s %d when sending %zu bytes - shutting down socket\n",
1300 		  xprt->xpt_server->sv_name,
1301 		  (sent < 0) ? "got error" : "sent",
1302 		  sent, xdr->len + sizeof(marker));
1303 	svc_xprt_deferred_close(xprt);
1304 	mutex_unlock(&xprt->xpt_mutex);
1305 	return -EAGAIN;
1306 }
1307 
1308 static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
1309 				       struct net *net,
1310 				       struct sockaddr *sa, int salen,
1311 				       int flags)
1312 {
1313 	return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1314 }
1315 
1316 static const struct svc_xprt_ops svc_tcp_ops = {
1317 	.xpo_create = svc_tcp_create,
1318 	.xpo_recvfrom = svc_tcp_recvfrom,
1319 	.xpo_sendto = svc_tcp_sendto,
1320 	.xpo_result_payload = svc_sock_result_payload,
1321 	.xpo_release_ctxt = svc_tcp_release_ctxt,
1322 	.xpo_detach = svc_tcp_sock_detach,
1323 	.xpo_free = svc_sock_free,
1324 	.xpo_has_wspace = svc_tcp_has_wspace,
1325 	.xpo_accept = svc_tcp_accept,
1326 	.xpo_kill_temp_xprt = svc_tcp_kill_temp_xprt,
1327 	.xpo_handshake = svc_tcp_handshake,
1328 };
1329 
1330 static struct svc_xprt_class svc_tcp_class = {
1331 	.xcl_name = "tcp",
1332 	.xcl_owner = THIS_MODULE,
1333 	.xcl_ops = &svc_tcp_ops,
1334 	.xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1335 	.xcl_ident = XPRT_TRANSPORT_TCP,
1336 };
1337 
1338 void svc_init_xprt_sock(void)
1339 {
1340 	svc_reg_xprt_class(&svc_tcp_class);
1341 	svc_reg_xprt_class(&svc_udp_class);
1342 }
1343 
1344 void svc_cleanup_xprt_sock(void)
1345 {
1346 	svc_unreg_xprt_class(&svc_tcp_class);
1347 	svc_unreg_xprt_class(&svc_udp_class);
1348 }
1349 
1350 static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
1351 {
1352 	struct sock	*sk = svsk->sk_sk;
1353 
1354 	svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
1355 		      &svsk->sk_xprt, serv);
1356 	set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
1357 	set_bit(XPT_CONG_CTRL, &svsk->sk_xprt.xpt_flags);
1358 	if (sk->sk_state == TCP_LISTEN) {
1359 		strcpy(svsk->sk_xprt.xpt_remotebuf, "listener");
1360 		set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
1361 		set_bit(XPT_RPCB_UNREG, &svsk->sk_xprt.xpt_flags);
1362 		sk->sk_data_ready = svc_tcp_listen_data_ready;
1363 		set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
1364 	} else {
1365 		sk->sk_state_change = svc_tcp_state_change;
1366 		sk->sk_data_ready = svc_data_ready;
1367 		sk->sk_write_space = svc_write_space;
1368 
1369 		svsk->sk_marker = xdr_zero;
1370 		svsk->sk_tcplen = 0;
1371 		svsk->sk_datalen = 0;
1372 		memset(&svsk->sk_pages[0], 0,
1373 		       svsk->sk_maxpages * sizeof(struct page *));
1374 
1375 		tcp_sock_set_nodelay(sk);
1376 
1377 		set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1378 		switch (sk->sk_state) {
1379 		case TCP_SYN_RECV:
1380 		case TCP_ESTABLISHED:
1381 			break;
1382 		default:
1383 			svc_xprt_deferred_close(&svsk->sk_xprt);
1384 		}
1385 	}
1386 }
1387 
1388 void svc_sock_update_bufs(struct svc_serv *serv)
1389 {
1390 	/*
1391 	 * The number of server threads has changed. Update
1392 	 * rcvbuf and sndbuf accordingly on all sockets
1393 	 */
1394 	struct svc_sock *svsk;
1395 
1396 	spin_lock_bh(&serv->sv_lock);
1397 	list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
1398 		set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1399 	spin_unlock_bh(&serv->sv_lock);
1400 }
1401 
1402 static int svc_sock_sendpages(struct svc_serv *serv, struct socket *sock, int flags)
1403 {
1404 	switch (sock->type) {
1405 	case SOCK_STREAM:
1406 		/* +1 for TCP record marker */
1407 		if (flags & SVC_SOCK_TEMPORARY)
1408 			return svc_serv_maxpages(serv) + 1;
1409 		return 0;
1410 	case SOCK_DGRAM:
1411 		return SUNRPC_MAX_UDP_SENDPAGES;
1412 	}
1413 	return -EINVAL;
1414 }
1415 
1416 /*
1417  * Initialize socket for RPC use and create svc_sock struct
1418  */
1419 static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1420 						struct socket *sock,
1421 						int flags)
1422 {
1423 	struct svc_sock	*svsk;
1424 	struct sock	*inet;
1425 	int		pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1426 	int		sendpages;
1427 	unsigned long	pages;
1428 
1429 	sendpages = svc_sock_sendpages(serv, sock, flags);
1430 	if (sendpages < 0)
1431 		return ERR_PTR(sendpages);
1432 
1433 	pages = svc_serv_maxpages(serv);
1434 	svsk = kzalloc_flex(*svsk, sk_pages, pages);
1435 	if (!svsk)
1436 		return ERR_PTR(-ENOMEM);
1437 
1438 	if (sendpages) {
1439 		svsk->sk_bvec = kzalloc_objs(*svsk->sk_bvec, sendpages);
1440 		if (!svsk->sk_bvec) {
1441 			kfree(svsk);
1442 			return ERR_PTR(-ENOMEM);
1443 		}
1444 	}
1445 
1446 	svsk->sk_maxpages = pages;
1447 
1448 	inet = sock->sk;
1449 
1450 	if (pmap_register) {
1451 		int err;
1452 
1453 		err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
1454 				     inet->sk_protocol,
1455 				     ntohs(inet_sk(inet)->inet_sport));
1456 		if (err < 0) {
1457 			kfree(svsk->sk_bvec);
1458 			kfree(svsk);
1459 			return ERR_PTR(err);
1460 		}
1461 	}
1462 
1463 	svsk->sk_sock = sock;
1464 	svsk->sk_sk = inet;
1465 	svsk->sk_ostate = inet->sk_state_change;
1466 	svsk->sk_odata = inet->sk_data_ready;
1467 	svsk->sk_owspace = inet->sk_write_space;
1468 	/*
1469 	 * This barrier is necessary in order to prevent race condition
1470 	 * with svc_data_ready(), svc_tcp_listen_data_ready(), and others
1471 	 * when calling callbacks above.
1472 	 */
1473 	wmb();
1474 	inet->sk_user_data = svsk;
1475 
1476 	/* Initialize the socket */
1477 	if (sock->type == SOCK_DGRAM)
1478 		svc_udp_init(svsk, serv);
1479 	else
1480 		svc_tcp_init(svsk, serv);
1481 
1482 	trace_svcsock_new(svsk, sock);
1483 	return svsk;
1484 }
1485 
1486 /**
1487  * svc_addsock - add a listener socket to an RPC service
1488  * @serv: pointer to RPC service to which to add a new listener
1489  * @net: caller's network namespace
1490  * @fd: file descriptor of the new listener
1491  * @name_return: pointer to buffer to fill in with name of listener
1492  * @len: size of the buffer
1493  * @cred: credential
1494  *
1495  * Fills in socket name and returns positive length of name if successful.
1496  * Name is terminated with '\n'.  On error, returns a negative errno
1497  * value.
1498  */
1499 int svc_addsock(struct svc_serv *serv, struct net *net, const int fd,
1500 		char *name_return, const size_t len, const struct cred *cred)
1501 {
1502 	int err = 0;
1503 	struct socket *so = sockfd_lookup(fd, &err);
1504 	struct svc_sock *svsk = NULL;
1505 	struct sockaddr_storage addr;
1506 	struct sockaddr *sin = (struct sockaddr *)&addr;
1507 	int salen;
1508 
1509 	if (!so)
1510 		return err;
1511 	err = -EINVAL;
1512 	if (sock_net(so->sk) != net)
1513 		goto out;
1514 	err = -EAFNOSUPPORT;
1515 	if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
1516 		goto out;
1517 	err =  -EPROTONOSUPPORT;
1518 	if (so->sk->sk_protocol != IPPROTO_TCP &&
1519 	    so->sk->sk_protocol != IPPROTO_UDP)
1520 		goto out;
1521 	err = -EISCONN;
1522 	if (so->state > SS_UNCONNECTED)
1523 		goto out;
1524 	err = -ENOENT;
1525 	if (!try_module_get(THIS_MODULE))
1526 		goto out;
1527 	svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1528 	if (IS_ERR(svsk)) {
1529 		module_put(THIS_MODULE);
1530 		err = PTR_ERR(svsk);
1531 		goto out;
1532 	}
1533 	salen = kernel_getsockname(svsk->sk_sock, sin);
1534 	if (salen >= 0)
1535 		svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
1536 	svsk->sk_xprt.xpt_cred = get_cred(cred);
1537 	svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
1538 	return svc_one_sock_name(svsk, name_return, len);
1539 out:
1540 	sockfd_put(so);
1541 	return err;
1542 }
1543 EXPORT_SYMBOL_GPL(svc_addsock);
1544 
1545 /*
1546  * Create socket for RPC service.
1547  */
1548 static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1549 					  int protocol,
1550 					  struct net *net,
1551 					  struct sockaddr *sin, int len,
1552 					  int flags)
1553 {
1554 	struct svc_sock	*svsk;
1555 	struct socket	*sock;
1556 	int		error;
1557 	int		type;
1558 	struct sockaddr_storage addr;
1559 	struct sockaddr *newsin = (struct sockaddr *)&addr;
1560 	int		newlen;
1561 	int		family;
1562 
1563 	if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1564 		printk(KERN_WARNING "svc: only UDP and TCP "
1565 				"sockets supported\n");
1566 		return ERR_PTR(-EINVAL);
1567 	}
1568 
1569 	type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1570 	switch (sin->sa_family) {
1571 	case AF_INET6:
1572 		family = PF_INET6;
1573 		break;
1574 	case AF_INET:
1575 		family = PF_INET;
1576 		break;
1577 	default:
1578 		return ERR_PTR(-EINVAL);
1579 	}
1580 
1581 	error = __sock_create(net, family, type, protocol, &sock, 1);
1582 	if (error < 0)
1583 		return ERR_PTR(error);
1584 
1585 	svc_reclassify_socket(sock);
1586 
1587 	/*
1588 	 * If this is an PF_INET6 listener, we want to avoid
1589 	 * getting requests from IPv4 remotes.  Those should
1590 	 * be shunted to a PF_INET listener via rpcbind.
1591 	 */
1592 	if (family == PF_INET6)
1593 		ip6_sock_set_v6only(sock->sk);
1594 	if (type == SOCK_STREAM)
1595 		sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
1596 	error = kernel_bind(sock, (struct sockaddr_unsized *)sin, len);
1597 	if (error < 0)
1598 		goto bummer;
1599 
1600 	error = kernel_getsockname(sock, newsin);
1601 	if (error < 0)
1602 		goto bummer;
1603 	newlen = error;
1604 
1605 	if (protocol == IPPROTO_TCP) {
1606 		sk_net_refcnt_upgrade(sock->sk);
1607 		if ((error = kernel_listen(sock, SOMAXCONN)) < 0)
1608 			goto bummer;
1609 	}
1610 
1611 	svsk = svc_setup_socket(serv, sock, flags);
1612 	if (IS_ERR(svsk)) {
1613 		error = PTR_ERR(svsk);
1614 		goto bummer;
1615 	}
1616 	svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1617 	return (struct svc_xprt *)svsk;
1618 bummer:
1619 	sock_release(sock);
1620 	return ERR_PTR(error);
1621 }
1622 
1623 /*
1624  * Detach the svc_sock from the socket so that no
1625  * more callbacks occur.
1626  */
1627 static void svc_sock_detach(struct svc_xprt *xprt)
1628 {
1629 	struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1630 	struct sock *sk = svsk->sk_sk;
1631 
1632 	/* put back the old socket callbacks */
1633 	lock_sock(sk);
1634 	sk->sk_state_change = svsk->sk_ostate;
1635 	sk->sk_data_ready = svsk->sk_odata;
1636 	sk->sk_write_space = svsk->sk_owspace;
1637 	sk->sk_user_data = NULL;
1638 	release_sock(sk);
1639 }
1640 
1641 /*
1642  * Disconnect the socket, and reset the callbacks
1643  */
1644 static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1645 {
1646 	struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1647 
1648 	tls_handshake_close(svsk->sk_sock);
1649 
1650 	svc_sock_detach(xprt);
1651 
1652 	if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1653 		svc_tcp_clear_pages(svsk);
1654 		kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
1655 	}
1656 }
1657 
1658 /*
1659  * Free the svc_sock's socket resources and the svc_sock itself.
1660  */
1661 static void svc_sock_free(struct svc_xprt *xprt)
1662 {
1663 	struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1664 	struct socket *sock = svsk->sk_sock;
1665 
1666 	trace_svcsock_free(svsk, sock);
1667 
1668 	tls_handshake_cancel(sock->sk);
1669 	if (sock->file)
1670 		sockfd_put(sock);
1671 	else
1672 		sock_release(sock);
1673 
1674 	page_frag_cache_drain(&svsk->sk_frag_cache);
1675 	kfree(svsk->sk_bvec);
1676 	kfree(svsk);
1677 }
1678