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