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