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