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