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