xref: /openbmc/linux/net/sunrpc/svcsock.c (revision 623e9ef6)
1 /*
2  * linux/net/sunrpc/svcsock.c
3  *
4  * These are the RPC server socket internals.
5  *
6  * The server scheduling algorithm does not always distribute the load
7  * evenly when servicing a single client. May need to modify the
8  * svc_xprt_enqueue procedure...
9  *
10  * TCP support is largely untested and may be a little slow. The problem
11  * is that we currently do two separate recvfrom's, one for the 4-byte
12  * record length, and the second for the actual record. This could possibly
13  * be improved by always reading a minimum size of around 100 bytes and
14  * tucking any superfluous bytes away in a temporary store. Still, that
15  * leaves write requests out in the rain. An alternative may be to peek at
16  * the first skb in the queue, and if it matches the next TCP sequence
17  * number, to extract the record marker. Yuck.
18  *
19  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
20  */
21 
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/module.h>
25 #include <linux/errno.h>
26 #include <linux/fcntl.h>
27 #include <linux/net.h>
28 #include <linux/in.h>
29 #include <linux/inet.h>
30 #include <linux/udp.h>
31 #include <linux/tcp.h>
32 #include <linux/unistd.h>
33 #include <linux/slab.h>
34 #include <linux/netdevice.h>
35 #include <linux/skbuff.h>
36 #include <linux/file.h>
37 #include <linux/freezer.h>
38 #include <net/sock.h>
39 #include <net/checksum.h>
40 #include <net/ip.h>
41 #include <net/ipv6.h>
42 #include <net/tcp.h>
43 #include <net/tcp_states.h>
44 #include <asm/uaccess.h>
45 #include <asm/ioctls.h>
46 #include <trace/events/skb.h>
47 
48 #include <linux/sunrpc/types.h>
49 #include <linux/sunrpc/clnt.h>
50 #include <linux/sunrpc/xdr.h>
51 #include <linux/sunrpc/msg_prot.h>
52 #include <linux/sunrpc/svcsock.h>
53 #include <linux/sunrpc/stats.h>
54 #include <linux/sunrpc/xprt.h>
55 
56 #include "sunrpc.h"
57 
58 #define RPCDBG_FACILITY	RPCDBG_SVCXPRT
59 
60 
61 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
62 					 int flags);
63 static void		svc_udp_data_ready(struct sock *);
64 static int		svc_udp_recvfrom(struct svc_rqst *);
65 static int		svc_udp_sendto(struct svc_rqst *);
66 static void		svc_sock_detach(struct svc_xprt *);
67 static void		svc_tcp_sock_detach(struct svc_xprt *);
68 static void		svc_sock_free(struct svc_xprt *);
69 
70 static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
71 					  struct net *, struct sockaddr *,
72 					  int, int);
73 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
74 static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
75 					     struct net *, struct sockaddr *,
76 					     int, int);
77 static void svc_bc_sock_free(struct svc_xprt *xprt);
78 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
79 
80 #ifdef CONFIG_DEBUG_LOCK_ALLOC
81 static struct lock_class_key svc_key[2];
82 static struct lock_class_key svc_slock_key[2];
83 
84 static void svc_reclassify_socket(struct socket *sock)
85 {
86 	struct sock *sk = sock->sk;
87 
88 	if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
89 		return;
90 
91 	switch (sk->sk_family) {
92 	case AF_INET:
93 		sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
94 					      &svc_slock_key[0],
95 					      "sk_xprt.xpt_lock-AF_INET-NFSD",
96 					      &svc_key[0]);
97 		break;
98 
99 	case AF_INET6:
100 		sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
101 					      &svc_slock_key[1],
102 					      "sk_xprt.xpt_lock-AF_INET6-NFSD",
103 					      &svc_key[1]);
104 		break;
105 
106 	default:
107 		BUG();
108 	}
109 }
110 #else
111 static void svc_reclassify_socket(struct socket *sock)
112 {
113 }
114 #endif
115 
116 /*
117  * Release an skbuff after use
118  */
119 static void svc_release_skb(struct svc_rqst *rqstp)
120 {
121 	struct sk_buff *skb = rqstp->rq_xprt_ctxt;
122 
123 	if (skb) {
124 		struct svc_sock *svsk =
125 			container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
126 		rqstp->rq_xprt_ctxt = NULL;
127 
128 		dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
129 		skb_free_datagram_locked(svsk->sk_sk, skb);
130 	}
131 }
132 
133 union svc_pktinfo_u {
134 	struct in_pktinfo pkti;
135 	struct in6_pktinfo pkti6;
136 };
137 #define SVC_PKTINFO_SPACE \
138 	CMSG_SPACE(sizeof(union svc_pktinfo_u))
139 
140 static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
141 {
142 	struct svc_sock *svsk =
143 		container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
144 	switch (svsk->sk_sk->sk_family) {
145 	case AF_INET: {
146 			struct in_pktinfo *pki = CMSG_DATA(cmh);
147 
148 			cmh->cmsg_level = SOL_IP;
149 			cmh->cmsg_type = IP_PKTINFO;
150 			pki->ipi_ifindex = 0;
151 			pki->ipi_spec_dst.s_addr =
152 				 svc_daddr_in(rqstp)->sin_addr.s_addr;
153 			cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
154 		}
155 		break;
156 
157 	case AF_INET6: {
158 			struct in6_pktinfo *pki = CMSG_DATA(cmh);
159 			struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
160 
161 			cmh->cmsg_level = SOL_IPV6;
162 			cmh->cmsg_type = IPV6_PKTINFO;
163 			pki->ipi6_ifindex = daddr->sin6_scope_id;
164 			pki->ipi6_addr = daddr->sin6_addr;
165 			cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
166 		}
167 		break;
168 	}
169 }
170 
171 /*
172  * send routine intended to be shared by the fore- and back-channel
173  */
174 int svc_send_common(struct socket *sock, struct xdr_buf *xdr,
175 		    struct page *headpage, unsigned long headoffset,
176 		    struct page *tailpage, unsigned long tailoffset)
177 {
178 	int		result;
179 	int		size;
180 	struct page	**ppage = xdr->pages;
181 	size_t		base = xdr->page_base;
182 	unsigned int	pglen = xdr->page_len;
183 	unsigned int	flags = MSG_MORE | MSG_SENDPAGE_NOTLAST;
184 	int		slen;
185 	int		len = 0;
186 
187 	slen = xdr->len;
188 
189 	/* send head */
190 	if (slen == xdr->head[0].iov_len)
191 		flags = 0;
192 	len = kernel_sendpage(sock, headpage, headoffset,
193 				  xdr->head[0].iov_len, flags);
194 	if (len != xdr->head[0].iov_len)
195 		goto out;
196 	slen -= xdr->head[0].iov_len;
197 	if (slen == 0)
198 		goto out;
199 
200 	/* send page data */
201 	size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
202 	while (pglen > 0) {
203 		if (slen == size)
204 			flags = 0;
205 		result = kernel_sendpage(sock, *ppage, base, size, flags);
206 		if (result > 0)
207 			len += result;
208 		if (result != size)
209 			goto out;
210 		slen -= size;
211 		pglen -= size;
212 		size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
213 		base = 0;
214 		ppage++;
215 	}
216 
217 	/* send tail */
218 	if (xdr->tail[0].iov_len) {
219 		result = kernel_sendpage(sock, tailpage, tailoffset,
220 				   xdr->tail[0].iov_len, 0);
221 		if (result > 0)
222 			len += result;
223 	}
224 
225 out:
226 	return len;
227 }
228 
229 
230 /*
231  * Generic sendto routine
232  */
233 static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
234 {
235 	struct svc_sock	*svsk =
236 		container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
237 	struct socket	*sock = svsk->sk_sock;
238 	union {
239 		struct cmsghdr	hdr;
240 		long		all[SVC_PKTINFO_SPACE / sizeof(long)];
241 	} buffer;
242 	struct cmsghdr *cmh = &buffer.hdr;
243 	int		len = 0;
244 	unsigned long tailoff;
245 	unsigned long headoff;
246 	RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
247 
248 	if (rqstp->rq_prot == IPPROTO_UDP) {
249 		struct msghdr msg = {
250 			.msg_name	= &rqstp->rq_addr,
251 			.msg_namelen	= rqstp->rq_addrlen,
252 			.msg_control	= cmh,
253 			.msg_controllen	= sizeof(buffer),
254 			.msg_flags	= MSG_MORE,
255 		};
256 
257 		svc_set_cmsg_data(rqstp, cmh);
258 
259 		if (sock_sendmsg(sock, &msg) < 0)
260 			goto out;
261 	}
262 
263 	tailoff = ((unsigned long)xdr->tail[0].iov_base) & (PAGE_SIZE-1);
264 	headoff = 0;
265 	len = svc_send_common(sock, xdr, rqstp->rq_respages[0], headoff,
266 			       rqstp->rq_respages[0], tailoff);
267 
268 out:
269 	dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
270 		svsk, xdr->head[0].iov_base, xdr->head[0].iov_len,
271 		xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
272 
273 	return len;
274 }
275 
276 /*
277  * Report socket names for nfsdfs
278  */
279 static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
280 {
281 	const struct sock *sk = svsk->sk_sk;
282 	const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
283 							"udp" : "tcp";
284 	int len;
285 
286 	switch (sk->sk_family) {
287 	case PF_INET:
288 		len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
289 				proto_name,
290 				&inet_sk(sk)->inet_rcv_saddr,
291 				inet_sk(sk)->inet_num);
292 		break;
293 #if IS_ENABLED(CONFIG_IPV6)
294 	case PF_INET6:
295 		len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
296 				proto_name,
297 				&sk->sk_v6_rcv_saddr,
298 				inet_sk(sk)->inet_num);
299 		break;
300 #endif
301 	default:
302 		len = snprintf(buf, remaining, "*unknown-%d*\n",
303 				sk->sk_family);
304 	}
305 
306 	if (len >= remaining) {
307 		*buf = '\0';
308 		return -ENAMETOOLONG;
309 	}
310 	return len;
311 }
312 
313 /*
314  * Generic recvfrom routine.
315  */
316 static int svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr,
317 			int buflen)
318 {
319 	struct svc_sock *svsk =
320 		container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
321 	struct msghdr msg = {
322 		.msg_flags	= MSG_DONTWAIT,
323 	};
324 	int len;
325 
326 	rqstp->rq_xprt_hlen = 0;
327 
328 	clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
329 	len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
330 				msg.msg_flags);
331 	/* If we read a full record, then assume there may be more
332 	 * data to read (stream based sockets only!)
333 	 */
334 	if (len == buflen)
335 		set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
336 
337 	dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
338 		svsk, iov[0].iov_base, iov[0].iov_len, len);
339 	return len;
340 }
341 
342 static int svc_partial_recvfrom(struct svc_rqst *rqstp,
343 				struct kvec *iov, int nr,
344 				int buflen, unsigned int base)
345 {
346 	size_t save_iovlen;
347 	void *save_iovbase;
348 	unsigned int i;
349 	int ret;
350 
351 	if (base == 0)
352 		return svc_recvfrom(rqstp, iov, nr, buflen);
353 
354 	for (i = 0; i < nr; i++) {
355 		if (iov[i].iov_len > base)
356 			break;
357 		base -= iov[i].iov_len;
358 	}
359 	save_iovlen = iov[i].iov_len;
360 	save_iovbase = iov[i].iov_base;
361 	iov[i].iov_len -= base;
362 	iov[i].iov_base += base;
363 	ret = svc_recvfrom(rqstp, &iov[i], nr - i, buflen);
364 	iov[i].iov_len = save_iovlen;
365 	iov[i].iov_base = save_iovbase;
366 	return ret;
367 }
368 
369 /*
370  * Set socket snd and rcv buffer lengths
371  */
372 static void svc_sock_setbufsize(struct socket *sock, unsigned int snd,
373 				unsigned int rcv)
374 {
375 #if 0
376 	mm_segment_t	oldfs;
377 	oldfs = get_fs(); set_fs(KERNEL_DS);
378 	sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
379 			(char*)&snd, sizeof(snd));
380 	sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
381 			(char*)&rcv, sizeof(rcv));
382 #else
383 	/* sock_setsockopt limits use to sysctl_?mem_max,
384 	 * which isn't acceptable.  Until that is made conditional
385 	 * on not having CAP_SYS_RESOURCE or similar, we go direct...
386 	 * DaveM said I could!
387 	 */
388 	lock_sock(sock->sk);
389 	sock->sk->sk_sndbuf = snd * 2;
390 	sock->sk->sk_rcvbuf = rcv * 2;
391 	sock->sk->sk_write_space(sock->sk);
392 	release_sock(sock->sk);
393 #endif
394 }
395 
396 static int svc_sock_secure_port(struct svc_rqst *rqstp)
397 {
398 	return svc_port_is_privileged(svc_addr(rqstp));
399 }
400 
401 static bool sunrpc_waitqueue_active(wait_queue_head_t *wq)
402 {
403 	if (!wq)
404 		return false;
405 	/*
406 	 * There should normally be a memory * barrier here--see
407 	 * wq_has_sleeper().
408 	 *
409 	 * It appears that isn't currently necessary, though, basically
410 	 * because callers all appear to have sufficient memory barriers
411 	 * between the time the relevant change is made and the
412 	 * time they call these callbacks.
413 	 *
414 	 * The nfsd code itself doesn't actually explicitly wait on
415 	 * these waitqueues, but it may wait on them for example in
416 	 * sendpage() or sendmsg() calls.  (And those may be the only
417 	 * places, since it it uses nonblocking reads.)
418 	 *
419 	 * Maybe we should add the memory barriers anyway, but these are
420 	 * hot paths so we'd need to be convinced there's no sigificant
421 	 * penalty.
422 	 */
423 	return waitqueue_active(wq);
424 }
425 
426 /*
427  * INET callback when data has been received on the socket.
428  */
429 static void svc_udp_data_ready(struct sock *sk)
430 {
431 	struct svc_sock	*svsk = (struct svc_sock *)sk->sk_user_data;
432 	wait_queue_head_t *wq = sk_sleep(sk);
433 
434 	if (svsk) {
435 		dprintk("svc: socket %p(inet %p), busy=%d\n",
436 			svsk, sk,
437 			test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
438 		set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
439 		svc_xprt_enqueue(&svsk->sk_xprt);
440 	}
441 	if (sunrpc_waitqueue_active(wq))
442 		wake_up_interruptible(wq);
443 }
444 
445 /*
446  * INET callback when space is newly available on the socket.
447  */
448 static void svc_write_space(struct sock *sk)
449 {
450 	struct svc_sock	*svsk = (struct svc_sock *)(sk->sk_user_data);
451 	wait_queue_head_t *wq = sk_sleep(sk);
452 
453 	if (svsk) {
454 		dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
455 			svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
456 		svc_xprt_enqueue(&svsk->sk_xprt);
457 	}
458 
459 	if (sunrpc_waitqueue_active(wq)) {
460 		dprintk("RPC svc_write_space: someone sleeping on %p\n",
461 		       svsk);
462 		wake_up_interruptible(wq);
463 	}
464 }
465 
466 static int svc_tcp_has_wspace(struct svc_xprt *xprt)
467 {
468 	struct svc_sock *svsk =	container_of(xprt, struct svc_sock, sk_xprt);
469 	struct svc_serv *serv = svsk->sk_xprt.xpt_server;
470 	int required;
471 
472 	if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
473 		return 1;
474 	required = atomic_read(&xprt->xpt_reserved) + serv->sv_max_mesg;
475 	if (sk_stream_wspace(svsk->sk_sk) >= required ||
476 	    (sk_stream_min_wspace(svsk->sk_sk) == 0 &&
477 	     atomic_read(&xprt->xpt_reserved) == 0))
478 		return 1;
479 	set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
480 	return 0;
481 }
482 
483 static void svc_tcp_write_space(struct sock *sk)
484 {
485 	struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
486 	struct socket *sock = sk->sk_socket;
487 
488 	if (!sk_stream_is_writeable(sk) || !sock)
489 		return;
490 	if (!svsk || svc_tcp_has_wspace(&svsk->sk_xprt))
491 		clear_bit(SOCK_NOSPACE, &sock->flags);
492 	svc_write_space(sk);
493 }
494 
495 static void svc_tcp_adjust_wspace(struct svc_xprt *xprt)
496 {
497 	struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
498 
499 	if (svc_tcp_has_wspace(xprt))
500 		clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
501 }
502 
503 /*
504  * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
505  */
506 static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
507 				     struct cmsghdr *cmh)
508 {
509 	struct in_pktinfo *pki = CMSG_DATA(cmh);
510 	struct sockaddr_in *daddr = svc_daddr_in(rqstp);
511 
512 	if (cmh->cmsg_type != IP_PKTINFO)
513 		return 0;
514 
515 	daddr->sin_family = AF_INET;
516 	daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
517 	return 1;
518 }
519 
520 /*
521  * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
522  */
523 static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
524 				     struct cmsghdr *cmh)
525 {
526 	struct in6_pktinfo *pki = CMSG_DATA(cmh);
527 	struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
528 
529 	if (cmh->cmsg_type != IPV6_PKTINFO)
530 		return 0;
531 
532 	daddr->sin6_family = AF_INET6;
533 	daddr->sin6_addr = pki->ipi6_addr;
534 	daddr->sin6_scope_id = pki->ipi6_ifindex;
535 	return 1;
536 }
537 
538 /*
539  * Copy the UDP datagram's destination address to the rqstp structure.
540  * The 'destination' address in this case is the address to which the
541  * peer sent the datagram, i.e. our local address. For multihomed
542  * hosts, this can change from msg to msg. Note that only the IP
543  * address changes, the port number should remain the same.
544  */
545 static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
546 				    struct cmsghdr *cmh)
547 {
548 	switch (cmh->cmsg_level) {
549 	case SOL_IP:
550 		return svc_udp_get_dest_address4(rqstp, cmh);
551 	case SOL_IPV6:
552 		return svc_udp_get_dest_address6(rqstp, cmh);
553 	}
554 
555 	return 0;
556 }
557 
558 /*
559  * Receive a datagram from a UDP socket.
560  */
561 static int svc_udp_recvfrom(struct svc_rqst *rqstp)
562 {
563 	struct svc_sock	*svsk =
564 		container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
565 	struct svc_serv	*serv = svsk->sk_xprt.xpt_server;
566 	struct sk_buff	*skb;
567 	union {
568 		struct cmsghdr	hdr;
569 		long		all[SVC_PKTINFO_SPACE / sizeof(long)];
570 	} buffer;
571 	struct cmsghdr *cmh = &buffer.hdr;
572 	struct msghdr msg = {
573 		.msg_name = svc_addr(rqstp),
574 		.msg_control = cmh,
575 		.msg_controllen = sizeof(buffer),
576 		.msg_flags = MSG_DONTWAIT,
577 	};
578 	size_t len;
579 	int err;
580 
581 	if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
582 	    /* udp sockets need large rcvbuf as all pending
583 	     * requests are still in that buffer.  sndbuf must
584 	     * also be large enough that there is enough space
585 	     * for one reply per thread.  We count all threads
586 	     * rather than threads in a particular pool, which
587 	     * provides an upper bound on the number of threads
588 	     * which will access the socket.
589 	     */
590 	    svc_sock_setbufsize(svsk->sk_sock,
591 				(serv->sv_nrthreads+3) * serv->sv_max_mesg,
592 				(serv->sv_nrthreads+3) * serv->sv_max_mesg);
593 
594 	clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
595 	skb = NULL;
596 	err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
597 			     0, 0, MSG_PEEK | MSG_DONTWAIT);
598 	if (err >= 0)
599 		skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err);
600 
601 	if (skb == NULL) {
602 		if (err != -EAGAIN) {
603 			/* possibly an icmp error */
604 			dprintk("svc: recvfrom returned error %d\n", -err);
605 			set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
606 		}
607 		return 0;
608 	}
609 	len = svc_addr_len(svc_addr(rqstp));
610 	rqstp->rq_addrlen = len;
611 	if (skb->tstamp.tv64 == 0) {
612 		skb->tstamp = ktime_get_real();
613 		/* Don't enable netstamp, sunrpc doesn't
614 		   need that much accuracy */
615 	}
616 	svsk->sk_sk->sk_stamp = skb->tstamp;
617 	set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
618 
619 	len  = skb->len;
620 	rqstp->rq_arg.len = len;
621 
622 	rqstp->rq_prot = IPPROTO_UDP;
623 
624 	if (!svc_udp_get_dest_address(rqstp, cmh)) {
625 		net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
626 				     cmh->cmsg_level, cmh->cmsg_type);
627 		goto out_free;
628 	}
629 	rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
630 
631 	if (skb_is_nonlinear(skb)) {
632 		/* we have to copy */
633 		local_bh_disable();
634 		if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
635 			local_bh_enable();
636 			/* checksum error */
637 			goto out_free;
638 		}
639 		local_bh_enable();
640 		skb_free_datagram_locked(svsk->sk_sk, skb);
641 	} else {
642 		/* we can use it in-place */
643 		rqstp->rq_arg.head[0].iov_base = skb->data;
644 		rqstp->rq_arg.head[0].iov_len = len;
645 		if (skb_checksum_complete(skb))
646 			goto out_free;
647 		rqstp->rq_xprt_ctxt = skb;
648 	}
649 
650 	rqstp->rq_arg.page_base = 0;
651 	if (len <= rqstp->rq_arg.head[0].iov_len) {
652 		rqstp->rq_arg.head[0].iov_len = len;
653 		rqstp->rq_arg.page_len = 0;
654 		rqstp->rq_respages = rqstp->rq_pages+1;
655 	} else {
656 		rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
657 		rqstp->rq_respages = rqstp->rq_pages + 1 +
658 			DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
659 	}
660 	rqstp->rq_next_page = rqstp->rq_respages+1;
661 
662 	if (serv->sv_stats)
663 		serv->sv_stats->netudpcnt++;
664 
665 	return len;
666 out_free:
667 	trace_kfree_skb(skb, svc_udp_recvfrom);
668 	skb_free_datagram_locked(svsk->sk_sk, skb);
669 	return 0;
670 }
671 
672 static int
673 svc_udp_sendto(struct svc_rqst *rqstp)
674 {
675 	int		error;
676 
677 	error = svc_sendto(rqstp, &rqstp->rq_res);
678 	if (error == -ECONNREFUSED)
679 		/* ICMP error on earlier request. */
680 		error = svc_sendto(rqstp, &rqstp->rq_res);
681 
682 	return error;
683 }
684 
685 static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
686 {
687 }
688 
689 static int svc_udp_has_wspace(struct svc_xprt *xprt)
690 {
691 	struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
692 	struct svc_serv	*serv = xprt->xpt_server;
693 	unsigned long required;
694 
695 	/*
696 	 * Set the SOCK_NOSPACE flag before checking the available
697 	 * sock space.
698 	 */
699 	set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
700 	required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
701 	if (required*2 > sock_wspace(svsk->sk_sk))
702 		return 0;
703 	clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
704 	return 1;
705 }
706 
707 static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
708 {
709 	BUG();
710 	return NULL;
711 }
712 
713 static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
714 				       struct net *net,
715 				       struct sockaddr *sa, int salen,
716 				       int flags)
717 {
718 	return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
719 }
720 
721 static struct svc_xprt_ops svc_udp_ops = {
722 	.xpo_create = svc_udp_create,
723 	.xpo_recvfrom = svc_udp_recvfrom,
724 	.xpo_sendto = svc_udp_sendto,
725 	.xpo_release_rqst = svc_release_skb,
726 	.xpo_detach = svc_sock_detach,
727 	.xpo_free = svc_sock_free,
728 	.xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
729 	.xpo_has_wspace = svc_udp_has_wspace,
730 	.xpo_accept = svc_udp_accept,
731 	.xpo_secure_port = svc_sock_secure_port,
732 };
733 
734 static struct svc_xprt_class svc_udp_class = {
735 	.xcl_name = "udp",
736 	.xcl_owner = THIS_MODULE,
737 	.xcl_ops = &svc_udp_ops,
738 	.xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
739 	.xcl_ident = XPRT_TRANSPORT_UDP,
740 };
741 
742 static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
743 {
744 	int err, level, optname, one = 1;
745 
746 	svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
747 		      &svsk->sk_xprt, serv);
748 	clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
749 	svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
750 	svsk->sk_sk->sk_write_space = svc_write_space;
751 
752 	/* initialise setting must have enough space to
753 	 * receive and respond to one request.
754 	 * svc_udp_recvfrom will re-adjust if necessary
755 	 */
756 	svc_sock_setbufsize(svsk->sk_sock,
757 			    3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
758 			    3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
759 
760 	/* data might have come in before data_ready set up */
761 	set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
762 	set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
763 
764 	/* make sure we get destination address info */
765 	switch (svsk->sk_sk->sk_family) {
766 	case AF_INET:
767 		level = SOL_IP;
768 		optname = IP_PKTINFO;
769 		break;
770 	case AF_INET6:
771 		level = SOL_IPV6;
772 		optname = IPV6_RECVPKTINFO;
773 		break;
774 	default:
775 		BUG();
776 	}
777 	err = kernel_setsockopt(svsk->sk_sock, level, optname,
778 					(char *)&one, sizeof(one));
779 	dprintk("svc: kernel_setsockopt returned %d\n", err);
780 }
781 
782 /*
783  * A data_ready event on a listening socket means there's a connection
784  * pending. Do not use state_change as a substitute for it.
785  */
786 static void svc_tcp_listen_data_ready(struct sock *sk)
787 {
788 	struct svc_sock	*svsk = (struct svc_sock *)sk->sk_user_data;
789 	wait_queue_head_t *wq;
790 
791 	dprintk("svc: socket %p TCP (listen) state change %d\n",
792 		sk, sk->sk_state);
793 
794 	/*
795 	 * This callback may called twice when a new connection
796 	 * is established as a child socket inherits everything
797 	 * from a parent LISTEN socket.
798 	 * 1) data_ready method of the parent socket will be called
799 	 *    when one of child sockets become ESTABLISHED.
800 	 * 2) data_ready method of the child socket may be called
801 	 *    when it receives data before the socket is accepted.
802 	 * In case of 2, we should ignore it silently.
803 	 */
804 	if (sk->sk_state == TCP_LISTEN) {
805 		if (svsk) {
806 			set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
807 			svc_xprt_enqueue(&svsk->sk_xprt);
808 		} else
809 			printk("svc: socket %p: no user data\n", sk);
810 	}
811 
812 	wq = sk_sleep(sk);
813 	if (sunrpc_waitqueue_active(wq))
814 		wake_up_interruptible_all(wq);
815 }
816 
817 /*
818  * A state change on a connected socket means it's dying or dead.
819  */
820 static void svc_tcp_state_change(struct sock *sk)
821 {
822 	struct svc_sock	*svsk = (struct svc_sock *)sk->sk_user_data;
823 	wait_queue_head_t *wq = sk_sleep(sk);
824 
825 	dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
826 		sk, sk->sk_state, sk->sk_user_data);
827 
828 	if (!svsk)
829 		printk("svc: socket %p: no user data\n", sk);
830 	else {
831 		set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
832 		svc_xprt_enqueue(&svsk->sk_xprt);
833 	}
834 	if (sunrpc_waitqueue_active(wq))
835 		wake_up_interruptible_all(wq);
836 }
837 
838 static void svc_tcp_data_ready(struct sock *sk)
839 {
840 	struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
841 	wait_queue_head_t *wq = sk_sleep(sk);
842 
843 	dprintk("svc: socket %p TCP data ready (svsk %p)\n",
844 		sk, sk->sk_user_data);
845 	if (svsk) {
846 		set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
847 		svc_xprt_enqueue(&svsk->sk_xprt);
848 	}
849 	if (sunrpc_waitqueue_active(wq))
850 		wake_up_interruptible(wq);
851 }
852 
853 /*
854  * Accept a TCP connection
855  */
856 static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
857 {
858 	struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
859 	struct sockaddr_storage addr;
860 	struct sockaddr	*sin = (struct sockaddr *) &addr;
861 	struct svc_serv	*serv = svsk->sk_xprt.xpt_server;
862 	struct socket	*sock = svsk->sk_sock;
863 	struct socket	*newsock;
864 	struct svc_sock	*newsvsk;
865 	int		err, slen;
866 	RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
867 
868 	dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
869 	if (!sock)
870 		return NULL;
871 
872 	clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
873 	err = kernel_accept(sock, &newsock, O_NONBLOCK);
874 	if (err < 0) {
875 		if (err == -ENOMEM)
876 			printk(KERN_WARNING "%s: no more sockets!\n",
877 			       serv->sv_name);
878 		else if (err != -EAGAIN)
879 			net_warn_ratelimited("%s: accept failed (err %d)!\n",
880 					     serv->sv_name, -err);
881 		return NULL;
882 	}
883 	set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
884 
885 	err = kernel_getpeername(newsock, sin, &slen);
886 	if (err < 0) {
887 		net_warn_ratelimited("%s: peername failed (err %d)!\n",
888 				     serv->sv_name, -err);
889 		goto failed;		/* aborted connection or whatever */
890 	}
891 
892 	/* Ideally, we would want to reject connections from unauthorized
893 	 * hosts here, but when we get encryption, the IP of the host won't
894 	 * tell us anything.  For now just warn about unpriv connections.
895 	 */
896 	if (!svc_port_is_privileged(sin)) {
897 		dprintk("%s: connect from unprivileged port: %s\n",
898 			serv->sv_name,
899 			__svc_print_addr(sin, buf, sizeof(buf)));
900 	}
901 	dprintk("%s: connect from %s\n", serv->sv_name,
902 		__svc_print_addr(sin, buf, sizeof(buf)));
903 
904 	/* make sure that a write doesn't block forever when
905 	 * low on memory
906 	 */
907 	newsock->sk->sk_sndtimeo = HZ*30;
908 
909 	newsvsk = svc_setup_socket(serv, newsock,
910 				 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
911 	if (IS_ERR(newsvsk))
912 		goto failed;
913 	svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
914 	err = kernel_getsockname(newsock, sin, &slen);
915 	if (unlikely(err < 0)) {
916 		dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
917 		slen = offsetof(struct sockaddr, sa_data);
918 	}
919 	svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
920 
921 	if (sock_is_loopback(newsock->sk))
922 		set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
923 	else
924 		clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
925 	if (serv->sv_stats)
926 		serv->sv_stats->nettcpconn++;
927 
928 	return &newsvsk->sk_xprt;
929 
930 failed:
931 	sock_release(newsock);
932 	return NULL;
933 }
934 
935 static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
936 {
937 	unsigned int i, len, npages;
938 
939 	if (svsk->sk_datalen == 0)
940 		return 0;
941 	len = svsk->sk_datalen;
942 	npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
943 	for (i = 0; i < npages; i++) {
944 		if (rqstp->rq_pages[i] != NULL)
945 			put_page(rqstp->rq_pages[i]);
946 		BUG_ON(svsk->sk_pages[i] == NULL);
947 		rqstp->rq_pages[i] = svsk->sk_pages[i];
948 		svsk->sk_pages[i] = NULL;
949 	}
950 	rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
951 	return len;
952 }
953 
954 static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
955 {
956 	unsigned int i, len, npages;
957 
958 	if (svsk->sk_datalen == 0)
959 		return;
960 	len = svsk->sk_datalen;
961 	npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
962 	for (i = 0; i < npages; i++) {
963 		svsk->sk_pages[i] = rqstp->rq_pages[i];
964 		rqstp->rq_pages[i] = NULL;
965 	}
966 }
967 
968 static void svc_tcp_clear_pages(struct svc_sock *svsk)
969 {
970 	unsigned int i, len, npages;
971 
972 	if (svsk->sk_datalen == 0)
973 		goto out;
974 	len = svsk->sk_datalen;
975 	npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
976 	for (i = 0; i < npages; i++) {
977 		if (svsk->sk_pages[i] == NULL) {
978 			WARN_ON_ONCE(1);
979 			continue;
980 		}
981 		put_page(svsk->sk_pages[i]);
982 		svsk->sk_pages[i] = NULL;
983 	}
984 out:
985 	svsk->sk_tcplen = 0;
986 	svsk->sk_datalen = 0;
987 }
988 
989 /*
990  * Receive fragment record header.
991  * If we haven't gotten the record length yet, get the next four bytes.
992  */
993 static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp)
994 {
995 	struct svc_serv	*serv = svsk->sk_xprt.xpt_server;
996 	unsigned int want;
997 	int len;
998 
999 	if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
1000 		struct kvec	iov;
1001 
1002 		want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
1003 		iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
1004 		iov.iov_len  = want;
1005 		if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
1006 			goto error;
1007 		svsk->sk_tcplen += len;
1008 
1009 		if (len < want) {
1010 			dprintk("svc: short recvfrom while reading record "
1011 				"length (%d of %d)\n", len, want);
1012 			return -EAGAIN;
1013 		}
1014 
1015 		dprintk("svc: TCP record, %d bytes\n", svc_sock_reclen(svsk));
1016 		if (svc_sock_reclen(svsk) + svsk->sk_datalen >
1017 							serv->sv_max_mesg) {
1018 			net_notice_ratelimited("RPC: fragment too large: %d\n",
1019 					svc_sock_reclen(svsk));
1020 			goto err_delete;
1021 		}
1022 	}
1023 
1024 	return svc_sock_reclen(svsk);
1025 error:
1026 	dprintk("RPC: TCP recv_record got %d\n", len);
1027 	return len;
1028 err_delete:
1029 	set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1030 	return -EAGAIN;
1031 }
1032 
1033 static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
1034 {
1035 	struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
1036 	struct rpc_rqst *req = NULL;
1037 	struct kvec *src, *dst;
1038 	__be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1039 	__be32 xid;
1040 	__be32 calldir;
1041 
1042 	xid = *p++;
1043 	calldir = *p;
1044 
1045 	if (!bc_xprt)
1046 		return -EAGAIN;
1047 	spin_lock_bh(&bc_xprt->transport_lock);
1048 	req = xprt_lookup_rqst(bc_xprt, xid);
1049 	if (!req)
1050 		goto unlock_notfound;
1051 
1052 	memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
1053 	/*
1054 	 * XXX!: cheating for now!  Only copying HEAD.
1055 	 * But we know this is good enough for now (in fact, for any
1056 	 * callback reply in the forseeable future).
1057 	 */
1058 	dst = &req->rq_private_buf.head[0];
1059 	src = &rqstp->rq_arg.head[0];
1060 	if (dst->iov_len < src->iov_len)
1061 		goto unlock_eagain; /* whatever; just giving up. */
1062 	memcpy(dst->iov_base, src->iov_base, src->iov_len);
1063 	xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len);
1064 	rqstp->rq_arg.len = 0;
1065 	spin_unlock_bh(&bc_xprt->transport_lock);
1066 	return 0;
1067 unlock_notfound:
1068 	printk(KERN_NOTICE
1069 		"%s: Got unrecognized reply: "
1070 		"calldir 0x%x xpt_bc_xprt %p xid %08x\n",
1071 		__func__, ntohl(calldir),
1072 		bc_xprt, ntohl(xid));
1073 unlock_eagain:
1074 	spin_unlock_bh(&bc_xprt->transport_lock);
1075 	return -EAGAIN;
1076 }
1077 
1078 static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len)
1079 {
1080 	int i = 0;
1081 	int t = 0;
1082 
1083 	while (t < len) {
1084 		vec[i].iov_base = page_address(pages[i]);
1085 		vec[i].iov_len = PAGE_SIZE;
1086 		i++;
1087 		t += PAGE_SIZE;
1088 	}
1089 	return i;
1090 }
1091 
1092 static void svc_tcp_fragment_received(struct svc_sock *svsk)
1093 {
1094 	/* If we have more data, signal svc_xprt_enqueue() to try again */
1095 	dprintk("svc: TCP %s record (%d bytes)\n",
1096 		svc_sock_final_rec(svsk) ? "final" : "nonfinal",
1097 		svc_sock_reclen(svsk));
1098 	svsk->sk_tcplen = 0;
1099 	svsk->sk_reclen = 0;
1100 }
1101 
1102 /*
1103  * Receive data from a TCP socket.
1104  */
1105 static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
1106 {
1107 	struct svc_sock	*svsk =
1108 		container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
1109 	struct svc_serv	*serv = svsk->sk_xprt.xpt_server;
1110 	int		len;
1111 	struct kvec *vec;
1112 	unsigned int want, base;
1113 	__be32 *p;
1114 	__be32 calldir;
1115 	int pnum;
1116 
1117 	dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1118 		svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
1119 		test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
1120 		test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
1121 
1122 	len = svc_tcp_recv_record(svsk, rqstp);
1123 	if (len < 0)
1124 		goto error;
1125 
1126 	base = svc_tcp_restore_pages(svsk, rqstp);
1127 	want = svc_sock_reclen(svsk) - (svsk->sk_tcplen - sizeof(rpc_fraghdr));
1128 
1129 	vec = rqstp->rq_vec;
1130 
1131 	pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0],
1132 						svsk->sk_datalen + want);
1133 
1134 	rqstp->rq_respages = &rqstp->rq_pages[pnum];
1135 	rqstp->rq_next_page = rqstp->rq_respages + 1;
1136 
1137 	/* Now receive data */
1138 	len = svc_partial_recvfrom(rqstp, vec, pnum, want, base);
1139 	if (len >= 0) {
1140 		svsk->sk_tcplen += len;
1141 		svsk->sk_datalen += len;
1142 	}
1143 	if (len != want || !svc_sock_final_rec(svsk)) {
1144 		svc_tcp_save_pages(svsk, rqstp);
1145 		if (len < 0 && len != -EAGAIN)
1146 			goto err_delete;
1147 		if (len == want)
1148 			svc_tcp_fragment_received(svsk);
1149 		else
1150 			dprintk("svc: incomplete TCP record (%d of %d)\n",
1151 				(int)(svsk->sk_tcplen - sizeof(rpc_fraghdr)),
1152 				svc_sock_reclen(svsk));
1153 		goto err_noclose;
1154 	}
1155 
1156 	if (svsk->sk_datalen < 8) {
1157 		svsk->sk_datalen = 0;
1158 		goto err_delete; /* client is nuts. */
1159 	}
1160 
1161 	rqstp->rq_arg.len = svsk->sk_datalen;
1162 	rqstp->rq_arg.page_base = 0;
1163 	if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1164 		rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
1165 		rqstp->rq_arg.page_len = 0;
1166 	} else
1167 		rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1168 
1169 	rqstp->rq_xprt_ctxt   = NULL;
1170 	rqstp->rq_prot	      = IPPROTO_TCP;
1171 	if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags))
1172 		set_bit(RQ_LOCAL, &rqstp->rq_flags);
1173 	else
1174 		clear_bit(RQ_LOCAL, &rqstp->rq_flags);
1175 
1176 	p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1177 	calldir = p[1];
1178 	if (calldir)
1179 		len = receive_cb_reply(svsk, rqstp);
1180 
1181 	/* Reset TCP read info */
1182 	svsk->sk_datalen = 0;
1183 	svc_tcp_fragment_received(svsk);
1184 
1185 	if (len < 0)
1186 		goto error;
1187 
1188 	svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
1189 	if (serv->sv_stats)
1190 		serv->sv_stats->nettcpcnt++;
1191 
1192 	return rqstp->rq_arg.len;
1193 
1194 error:
1195 	if (len != -EAGAIN)
1196 		goto err_delete;
1197 	dprintk("RPC: TCP recvfrom got EAGAIN\n");
1198 	return 0;
1199 err_delete:
1200 	printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1201 	       svsk->sk_xprt.xpt_server->sv_name, -len);
1202 	set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1203 err_noclose:
1204 	return 0;	/* record not complete */
1205 }
1206 
1207 /*
1208  * Send out data on TCP socket.
1209  */
1210 static int svc_tcp_sendto(struct svc_rqst *rqstp)
1211 {
1212 	struct xdr_buf	*xbufp = &rqstp->rq_res;
1213 	int sent;
1214 	__be32 reclen;
1215 
1216 	/* Set up the first element of the reply kvec.
1217 	 * Any other kvecs that may be in use have been taken
1218 	 * care of by the server implementation itself.
1219 	 */
1220 	reclen = htonl(0x80000000|((xbufp->len ) - 4));
1221 	memcpy(xbufp->head[0].iov_base, &reclen, 4);
1222 
1223 	sent = svc_sendto(rqstp, &rqstp->rq_res);
1224 	if (sent != xbufp->len) {
1225 		printk(KERN_NOTICE
1226 		       "rpc-srv/tcp: %s: %s %d when sending %d bytes "
1227 		       "- shutting down socket\n",
1228 		       rqstp->rq_xprt->xpt_server->sv_name,
1229 		       (sent<0)?"got error":"sent only",
1230 		       sent, xbufp->len);
1231 		set_bit(XPT_CLOSE, &rqstp->rq_xprt->xpt_flags);
1232 		svc_xprt_enqueue(rqstp->rq_xprt);
1233 		sent = -EAGAIN;
1234 	}
1235 	return sent;
1236 }
1237 
1238 /*
1239  * Setup response header. TCP has a 4B record length field.
1240  */
1241 static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
1242 {
1243 	struct kvec *resv = &rqstp->rq_res.head[0];
1244 
1245 	/* tcp needs a space for the record length... */
1246 	svc_putnl(resv, 0);
1247 }
1248 
1249 static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
1250 				       struct net *net,
1251 				       struct sockaddr *sa, int salen,
1252 				       int flags)
1253 {
1254 	return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1255 }
1256 
1257 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1258 static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
1259 					     struct net *, struct sockaddr *,
1260 					     int, int);
1261 static void svc_bc_sock_free(struct svc_xprt *xprt);
1262 
1263 static struct svc_xprt *svc_bc_tcp_create(struct svc_serv *serv,
1264 				       struct net *net,
1265 				       struct sockaddr *sa, int salen,
1266 				       int flags)
1267 {
1268 	return svc_bc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1269 }
1270 
1271 static void svc_bc_tcp_sock_detach(struct svc_xprt *xprt)
1272 {
1273 }
1274 
1275 static struct svc_xprt_ops svc_tcp_bc_ops = {
1276 	.xpo_create = svc_bc_tcp_create,
1277 	.xpo_detach = svc_bc_tcp_sock_detach,
1278 	.xpo_free = svc_bc_sock_free,
1279 	.xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
1280 	.xpo_secure_port = svc_sock_secure_port,
1281 };
1282 
1283 static struct svc_xprt_class svc_tcp_bc_class = {
1284 	.xcl_name = "tcp-bc",
1285 	.xcl_owner = THIS_MODULE,
1286 	.xcl_ops = &svc_tcp_bc_ops,
1287 	.xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1288 };
1289 
1290 static void svc_init_bc_xprt_sock(void)
1291 {
1292 	svc_reg_xprt_class(&svc_tcp_bc_class);
1293 }
1294 
1295 static void svc_cleanup_bc_xprt_sock(void)
1296 {
1297 	svc_unreg_xprt_class(&svc_tcp_bc_class);
1298 }
1299 #else /* CONFIG_SUNRPC_BACKCHANNEL */
1300 static void svc_init_bc_xprt_sock(void)
1301 {
1302 }
1303 
1304 static void svc_cleanup_bc_xprt_sock(void)
1305 {
1306 }
1307 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1308 
1309 static struct svc_xprt_ops svc_tcp_ops = {
1310 	.xpo_create = svc_tcp_create,
1311 	.xpo_recvfrom = svc_tcp_recvfrom,
1312 	.xpo_sendto = svc_tcp_sendto,
1313 	.xpo_release_rqst = svc_release_skb,
1314 	.xpo_detach = svc_tcp_sock_detach,
1315 	.xpo_free = svc_sock_free,
1316 	.xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
1317 	.xpo_has_wspace = svc_tcp_has_wspace,
1318 	.xpo_accept = svc_tcp_accept,
1319 	.xpo_secure_port = svc_sock_secure_port,
1320 	.xpo_adjust_wspace = svc_tcp_adjust_wspace,
1321 };
1322 
1323 static struct svc_xprt_class svc_tcp_class = {
1324 	.xcl_name = "tcp",
1325 	.xcl_owner = THIS_MODULE,
1326 	.xcl_ops = &svc_tcp_ops,
1327 	.xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1328 	.xcl_ident = XPRT_TRANSPORT_TCP,
1329 };
1330 
1331 void svc_init_xprt_sock(void)
1332 {
1333 	svc_reg_xprt_class(&svc_tcp_class);
1334 	svc_reg_xprt_class(&svc_udp_class);
1335 	svc_init_bc_xprt_sock();
1336 }
1337 
1338 void svc_cleanup_xprt_sock(void)
1339 {
1340 	svc_unreg_xprt_class(&svc_tcp_class);
1341 	svc_unreg_xprt_class(&svc_udp_class);
1342 	svc_cleanup_bc_xprt_sock();
1343 }
1344 
1345 static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
1346 {
1347 	struct sock	*sk = svsk->sk_sk;
1348 
1349 	svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
1350 		      &svsk->sk_xprt, serv);
1351 	set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
1352 	if (sk->sk_state == TCP_LISTEN) {
1353 		dprintk("setting up TCP socket for listening\n");
1354 		set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
1355 		sk->sk_data_ready = svc_tcp_listen_data_ready;
1356 		set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
1357 	} else {
1358 		dprintk("setting up TCP socket for reading\n");
1359 		sk->sk_state_change = svc_tcp_state_change;
1360 		sk->sk_data_ready = svc_tcp_data_ready;
1361 		sk->sk_write_space = svc_tcp_write_space;
1362 
1363 		svsk->sk_reclen = 0;
1364 		svsk->sk_tcplen = 0;
1365 		svsk->sk_datalen = 0;
1366 		memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
1367 
1368 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1369 
1370 		set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1371 		if (sk->sk_state != TCP_ESTABLISHED)
1372 			set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1373 	}
1374 }
1375 
1376 void svc_sock_update_bufs(struct svc_serv *serv)
1377 {
1378 	/*
1379 	 * The number of server threads has changed. Update
1380 	 * rcvbuf and sndbuf accordingly on all sockets
1381 	 */
1382 	struct svc_sock *svsk;
1383 
1384 	spin_lock_bh(&serv->sv_lock);
1385 	list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
1386 		set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1387 	spin_unlock_bh(&serv->sv_lock);
1388 }
1389 EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
1390 
1391 /*
1392  * Initialize socket for RPC use and create svc_sock struct
1393  */
1394 static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1395 						struct socket *sock,
1396 						int flags)
1397 {
1398 	struct svc_sock	*svsk;
1399 	struct sock	*inet;
1400 	int		pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1401 	int		err = 0;
1402 
1403 	dprintk("svc: svc_setup_socket %p\n", sock);
1404 	svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1405 	if (!svsk)
1406 		return ERR_PTR(-ENOMEM);
1407 
1408 	inet = sock->sk;
1409 
1410 	/* Register socket with portmapper */
1411 	if (pmap_register)
1412 		err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
1413 				     inet->sk_protocol,
1414 				     ntohs(inet_sk(inet)->inet_sport));
1415 
1416 	if (err < 0) {
1417 		kfree(svsk);
1418 		return ERR_PTR(err);
1419 	}
1420 
1421 	inet->sk_user_data = svsk;
1422 	svsk->sk_sock = sock;
1423 	svsk->sk_sk = inet;
1424 	svsk->sk_ostate = inet->sk_state_change;
1425 	svsk->sk_odata = inet->sk_data_ready;
1426 	svsk->sk_owspace = inet->sk_write_space;
1427 
1428 	/* Initialize the socket */
1429 	if (sock->type == SOCK_DGRAM)
1430 		svc_udp_init(svsk, serv);
1431 	else {
1432 		/* initialise setting must have enough space to
1433 		 * receive and respond to one request.
1434 		 */
1435 		svc_sock_setbufsize(svsk->sk_sock, 4 * serv->sv_max_mesg,
1436 					4 * serv->sv_max_mesg);
1437 		svc_tcp_init(svsk, serv);
1438 	}
1439 
1440 	dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1441 				svsk, svsk->sk_sk);
1442 
1443 	return svsk;
1444 }
1445 
1446 bool svc_alien_sock(struct net *net, int fd)
1447 {
1448 	int err;
1449 	struct socket *sock = sockfd_lookup(fd, &err);
1450 	bool ret = false;
1451 
1452 	if (!sock)
1453 		goto out;
1454 	if (sock_net(sock->sk) != net)
1455 		ret = true;
1456 	sockfd_put(sock);
1457 out:
1458 	return ret;
1459 }
1460 EXPORT_SYMBOL_GPL(svc_alien_sock);
1461 
1462 /**
1463  * svc_addsock - add a listener socket to an RPC service
1464  * @serv: pointer to RPC service to which to add a new listener
1465  * @fd: file descriptor of the new listener
1466  * @name_return: pointer to buffer to fill in with name of listener
1467  * @len: size of the buffer
1468  *
1469  * Fills in socket name and returns positive length of name if successful.
1470  * Name is terminated with '\n'.  On error, returns a negative errno
1471  * value.
1472  */
1473 int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
1474 		const size_t len)
1475 {
1476 	int err = 0;
1477 	struct socket *so = sockfd_lookup(fd, &err);
1478 	struct svc_sock *svsk = NULL;
1479 	struct sockaddr_storage addr;
1480 	struct sockaddr *sin = (struct sockaddr *)&addr;
1481 	int salen;
1482 
1483 	if (!so)
1484 		return err;
1485 	err = -EAFNOSUPPORT;
1486 	if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
1487 		goto out;
1488 	err =  -EPROTONOSUPPORT;
1489 	if (so->sk->sk_protocol != IPPROTO_TCP &&
1490 	    so->sk->sk_protocol != IPPROTO_UDP)
1491 		goto out;
1492 	err = -EISCONN;
1493 	if (so->state > SS_UNCONNECTED)
1494 		goto out;
1495 	err = -ENOENT;
1496 	if (!try_module_get(THIS_MODULE))
1497 		goto out;
1498 	svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1499 	if (IS_ERR(svsk)) {
1500 		module_put(THIS_MODULE);
1501 		err = PTR_ERR(svsk);
1502 		goto out;
1503 	}
1504 	if (kernel_getsockname(svsk->sk_sock, sin, &salen) == 0)
1505 		svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
1506 	svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
1507 	return svc_one_sock_name(svsk, name_return, len);
1508 out:
1509 	sockfd_put(so);
1510 	return err;
1511 }
1512 EXPORT_SYMBOL_GPL(svc_addsock);
1513 
1514 /*
1515  * Create socket for RPC service.
1516  */
1517 static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1518 					  int protocol,
1519 					  struct net *net,
1520 					  struct sockaddr *sin, int len,
1521 					  int flags)
1522 {
1523 	struct svc_sock	*svsk;
1524 	struct socket	*sock;
1525 	int		error;
1526 	int		type;
1527 	struct sockaddr_storage addr;
1528 	struct sockaddr *newsin = (struct sockaddr *)&addr;
1529 	int		newlen;
1530 	int		family;
1531 	int		val;
1532 	RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
1533 
1534 	dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1535 			serv->sv_program->pg_name, protocol,
1536 			__svc_print_addr(sin, buf, sizeof(buf)));
1537 
1538 	if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1539 		printk(KERN_WARNING "svc: only UDP and TCP "
1540 				"sockets supported\n");
1541 		return ERR_PTR(-EINVAL);
1542 	}
1543 
1544 	type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1545 	switch (sin->sa_family) {
1546 	case AF_INET6:
1547 		family = PF_INET6;
1548 		break;
1549 	case AF_INET:
1550 		family = PF_INET;
1551 		break;
1552 	default:
1553 		return ERR_PTR(-EINVAL);
1554 	}
1555 
1556 	error = __sock_create(net, family, type, protocol, &sock, 1);
1557 	if (error < 0)
1558 		return ERR_PTR(error);
1559 
1560 	svc_reclassify_socket(sock);
1561 
1562 	/*
1563 	 * If this is an PF_INET6 listener, we want to avoid
1564 	 * getting requests from IPv4 remotes.  Those should
1565 	 * be shunted to a PF_INET listener via rpcbind.
1566 	 */
1567 	val = 1;
1568 	if (family == PF_INET6)
1569 		kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
1570 					(char *)&val, sizeof(val));
1571 
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 	newlen = len;
1579 	error = kernel_getsockname(sock, newsin, &newlen);
1580 	if (error < 0)
1581 		goto bummer;
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 	dprintk("svc: svc_create_socket error = %d\n", -error);
1597 	sock_release(sock);
1598 	return ERR_PTR(error);
1599 }
1600 
1601 /*
1602  * Detach the svc_sock from the socket so that no
1603  * more callbacks occur.
1604  */
1605 static void svc_sock_detach(struct svc_xprt *xprt)
1606 {
1607 	struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1608 	struct sock *sk = svsk->sk_sk;
1609 	wait_queue_head_t *wq;
1610 
1611 	dprintk("svc: svc_sock_detach(%p)\n", svsk);
1612 
1613 	/* put back the old socket callbacks */
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 
1618 	wq = sk_sleep(sk);
1619 	if (sunrpc_waitqueue_active(wq))
1620 		wake_up_interruptible(wq);
1621 }
1622 
1623 /*
1624  * Disconnect the socket, and reset the callbacks
1625  */
1626 static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1627 {
1628 	struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1629 
1630 	dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk);
1631 
1632 	svc_sock_detach(xprt);
1633 
1634 	if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1635 		svc_tcp_clear_pages(svsk);
1636 		kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
1637 	}
1638 }
1639 
1640 /*
1641  * Free the svc_sock's socket resources and the svc_sock itself.
1642  */
1643 static void svc_sock_free(struct svc_xprt *xprt)
1644 {
1645 	struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1646 	dprintk("svc: svc_sock_free(%p)\n", svsk);
1647 
1648 	if (svsk->sk_sock->file)
1649 		sockfd_put(svsk->sk_sock);
1650 	else
1651 		sock_release(svsk->sk_sock);
1652 	kfree(svsk);
1653 }
1654 
1655 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1656 /*
1657  * Create a back channel svc_xprt which shares the fore channel socket.
1658  */
1659 static struct svc_xprt *svc_bc_create_socket(struct svc_serv *serv,
1660 					     int protocol,
1661 					     struct net *net,
1662 					     struct sockaddr *sin, int len,
1663 					     int flags)
1664 {
1665 	struct svc_sock *svsk;
1666 	struct svc_xprt *xprt;
1667 
1668 	if (protocol != IPPROTO_TCP) {
1669 		printk(KERN_WARNING "svc: only TCP sockets"
1670 			" supported on shared back channel\n");
1671 		return ERR_PTR(-EINVAL);
1672 	}
1673 
1674 	svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1675 	if (!svsk)
1676 		return ERR_PTR(-ENOMEM);
1677 
1678 	xprt = &svsk->sk_xprt;
1679 	svc_xprt_init(net, &svc_tcp_bc_class, xprt, serv);
1680 
1681 	serv->sv_bc_xprt = xprt;
1682 
1683 	return xprt;
1684 }
1685 
1686 /*
1687  * Free a back channel svc_sock.
1688  */
1689 static void svc_bc_sock_free(struct svc_xprt *xprt)
1690 {
1691 	if (xprt)
1692 		kfree(container_of(xprt, struct svc_sock, sk_xprt));
1693 }
1694 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1695