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