1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/net/sunrpc/svcsock.c
5 * These are the RPC server socket internals.
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...
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.
20 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
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>
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>
40 #include <net/checksum.h>
45 #include <net/tcp_states.h>
47 #include <net/handshake.h>
48 #include <linux/uaccess.h>
49 #include <linux/highmem.h>
50 #include <asm/ioctls.h>
51 #include <linux/key.h>
53 #include <linux/sunrpc/types.h>
54 #include <linux/sunrpc/clnt.h>
55 #include <linux/sunrpc/xdr.h>
56 #include <linux/sunrpc/msg_prot.h>
57 #include <linux/sunrpc/svcsock.h>
58 #include <linux/sunrpc/stats.h>
59 #include <linux/sunrpc/xprt.h>
61 #include <trace/events/sock.h>
62 #include <trace/events/sunrpc.h>
67 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
69 /* To-do: to avoid tying up an nfsd thread while waiting for a
70 * handshake request, the request could instead be deferred.
73 SVC_HANDSHAKE_TO = 5U * HZ
76 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
78 static int svc_udp_recvfrom(struct svc_rqst *);
79 static int svc_udp_sendto(struct svc_rqst *);
80 static void svc_sock_detach(struct svc_xprt *);
81 static void svc_tcp_sock_detach(struct svc_xprt *);
82 static void svc_sock_free(struct svc_xprt *);
84 static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
85 struct net *, struct sockaddr *,
87 #ifdef CONFIG_DEBUG_LOCK_ALLOC
88 static struct lock_class_key svc_key[2];
89 static struct lock_class_key svc_slock_key[2];
91 static void svc_reclassify_socket(struct socket *sock)
93 struct sock *sk = sock->sk;
95 if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
98 switch (sk->sk_family) {
100 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
102 "sk_xprt.xpt_lock-AF_INET-NFSD",
107 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
109 "sk_xprt.xpt_lock-AF_INET6-NFSD",
118 static void svc_reclassify_socket(struct socket *sock)
124 * svc_tcp_release_rqst - Release transport-related resources
125 * @rqstp: request structure with resources to be released
128 static void svc_tcp_release_rqst(struct svc_rqst *rqstp)
133 * svc_udp_release_rqst - Release transport-related resources
134 * @rqstp: request structure with resources to be released
137 static void svc_udp_release_rqst(struct svc_rqst *rqstp)
139 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
142 rqstp->rq_xprt_ctxt = NULL;
147 union svc_pktinfo_u {
148 struct in_pktinfo pkti;
149 struct in6_pktinfo pkti6;
151 #define SVC_PKTINFO_SPACE \
152 CMSG_SPACE(sizeof(union svc_pktinfo_u))
154 static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
156 struct svc_sock *svsk =
157 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
158 switch (svsk->sk_sk->sk_family) {
160 struct in_pktinfo *pki = CMSG_DATA(cmh);
162 cmh->cmsg_level = SOL_IP;
163 cmh->cmsg_type = IP_PKTINFO;
164 pki->ipi_ifindex = 0;
165 pki->ipi_spec_dst.s_addr =
166 svc_daddr_in(rqstp)->sin_addr.s_addr;
167 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
172 struct in6_pktinfo *pki = CMSG_DATA(cmh);
173 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
175 cmh->cmsg_level = SOL_IPV6;
176 cmh->cmsg_type = IPV6_PKTINFO;
177 pki->ipi6_ifindex = daddr->sin6_scope_id;
178 pki->ipi6_addr = daddr->sin6_addr;
179 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
185 static int svc_sock_result_payload(struct svc_rqst *rqstp, unsigned int offset,
192 * Report socket names for nfsdfs
194 static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
196 const struct sock *sk = svsk->sk_sk;
197 const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
201 switch (sk->sk_family) {
203 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
205 &inet_sk(sk)->inet_rcv_saddr,
206 inet_sk(sk)->inet_num);
208 #if IS_ENABLED(CONFIG_IPV6)
210 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
212 &sk->sk_v6_rcv_saddr,
213 inet_sk(sk)->inet_num);
217 len = snprintf(buf, remaining, "*unknown-%d*\n",
221 if (len >= remaining) {
223 return -ENAMETOOLONG;
229 svc_tcp_sock_process_cmsg(struct svc_sock *svsk, struct msghdr *msg,
230 struct cmsghdr *cmsg, int ret)
232 if (cmsg->cmsg_level == SOL_TLS &&
233 cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
234 u8 content_type = *((u8 *)CMSG_DATA(cmsg));
236 switch (content_type) {
237 case TLS_RECORD_TYPE_DATA:
238 /* TLS sets EOR at the end of each application data
239 * record, even though there might be more frames
240 * waiting to be decrypted.
242 msg->msg_flags &= ~MSG_EOR;
244 case TLS_RECORD_TYPE_ALERT:
255 svc_tcp_sock_recv_cmsg(struct svc_sock *svsk, struct msghdr *msg)
259 u8 buf[CMSG_SPACE(sizeof(u8))];
263 msg->msg_control = &u;
264 msg->msg_controllen = sizeof(u);
265 ret = sock_recvmsg(svsk->sk_sock, msg, MSG_DONTWAIT);
266 if (unlikely(msg->msg_controllen != sizeof(u)))
267 ret = svc_tcp_sock_process_cmsg(svsk, msg, &u.cmsg, ret);
271 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
272 static void svc_flush_bvec(const struct bio_vec *bvec, size_t size, size_t seek)
274 struct bvec_iter bi = {
275 .bi_size = size + seek,
279 bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
280 for_each_bvec(bv, bvec, bi, bi)
281 flush_dcache_page(bv.bv_page);
284 static inline void svc_flush_bvec(const struct bio_vec *bvec, size_t size,
291 * Read from @rqstp's transport socket. The incoming message fills whole
292 * pages in @rqstp's rq_pages array until the last page of the message
293 * has been received into a partial page.
295 static ssize_t svc_tcp_read_msg(struct svc_rqst *rqstp, size_t buflen,
298 struct svc_sock *svsk =
299 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
300 struct bio_vec *bvec = rqstp->rq_bvec;
301 struct msghdr msg = { NULL };
306 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
308 for (i = 0, t = 0; t < buflen; i++, t += PAGE_SIZE)
309 bvec_set_page(&bvec[i], rqstp->rq_pages[i], PAGE_SIZE, 0);
310 rqstp->rq_respages = &rqstp->rq_pages[i];
311 rqstp->rq_next_page = rqstp->rq_respages + 1;
313 iov_iter_bvec(&msg.msg_iter, ITER_DEST, bvec, i, buflen);
315 iov_iter_advance(&msg.msg_iter, seek);
318 len = svc_tcp_sock_recv_cmsg(svsk, &msg);
320 svc_flush_bvec(bvec, len, seek);
322 /* If we read a full record, then assume there may be more
323 * data to read (stream based sockets only!)
326 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
332 * Set socket snd and rcv buffer lengths
334 static void svc_sock_setbufsize(struct svc_sock *svsk, unsigned int nreqs)
336 unsigned int max_mesg = svsk->sk_xprt.xpt_server->sv_max_mesg;
337 struct socket *sock = svsk->sk_sock;
339 nreqs = min(nreqs, INT_MAX / 2 / max_mesg);
342 sock->sk->sk_sndbuf = nreqs * max_mesg * 2;
343 sock->sk->sk_rcvbuf = nreqs * max_mesg * 2;
344 sock->sk->sk_write_space(sock->sk);
345 release_sock(sock->sk);
348 static void svc_sock_secure_port(struct svc_rqst *rqstp)
350 if (svc_port_is_privileged(svc_addr(rqstp)))
351 set_bit(RQ_SECURE, &rqstp->rq_flags);
353 clear_bit(RQ_SECURE, &rqstp->rq_flags);
357 * INET callback when data has been received on the socket.
359 static void svc_data_ready(struct sock *sk)
361 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
363 trace_sk_data_ready(sk);
366 /* Refer to svc_setup_socket() for details. */
369 trace_svcsock_data_ready(&svsk->sk_xprt, 0);
370 if (test_bit(XPT_HANDSHAKE, &svsk->sk_xprt.xpt_flags))
372 if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags))
373 svc_xprt_enqueue(&svsk->sk_xprt);
378 * INET callback when space is newly available on the socket.
380 static void svc_write_space(struct sock *sk)
382 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
385 /* Refer to svc_setup_socket() for details. */
387 trace_svcsock_write_space(&svsk->sk_xprt, 0);
388 svsk->sk_owspace(sk);
389 svc_xprt_enqueue(&svsk->sk_xprt);
393 static int svc_tcp_has_wspace(struct svc_xprt *xprt)
395 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
397 if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
399 return !test_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
402 static void svc_tcp_kill_temp_xprt(struct svc_xprt *xprt)
404 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
406 sock_no_linger(svsk->sk_sock->sk);
410 * svc_tcp_handshake_done - Handshake completion handler
411 * @data: address of xprt to wake
412 * @status: status of handshake
413 * @peerid: serial number of key containing the remote peer's identity
415 * If a security policy is specified as an export option, we don't
416 * have a specific export here to check. So we set a "TLS session
417 * is present" flag on the xprt and let an upper layer enforce local
420 static void svc_tcp_handshake_done(void *data, int status, key_serial_t peerid)
422 struct svc_xprt *xprt = data;
423 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
426 if (peerid != TLS_NO_PEERID)
427 set_bit(XPT_PEER_AUTH, &xprt->xpt_flags);
428 set_bit(XPT_TLS_SESSION, &xprt->xpt_flags);
430 clear_bit(XPT_HANDSHAKE, &xprt->xpt_flags);
431 complete_all(&svsk->sk_handshake_done);
435 * svc_tcp_handshake - Perform a transport-layer security handshake
436 * @xprt: connected transport endpoint
439 static void svc_tcp_handshake(struct svc_xprt *xprt)
441 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
442 struct sock *sk = svsk->sk_sock->sk;
443 struct tls_handshake_args args = {
444 .ta_sock = svsk->sk_sock,
445 .ta_done = svc_tcp_handshake_done,
450 trace_svc_tls_upcall(xprt);
452 clear_bit(XPT_TLS_SESSION, &xprt->xpt_flags);
453 init_completion(&svsk->sk_handshake_done);
455 ret = tls_server_hello_x509(&args, GFP_KERNEL);
457 trace_svc_tls_not_started(xprt);
461 ret = wait_for_completion_interruptible_timeout(&svsk->sk_handshake_done,
464 if (tls_handshake_cancel(sk)) {
465 trace_svc_tls_timed_out(xprt);
470 if (!test_bit(XPT_TLS_SESSION, &xprt->xpt_flags)) {
471 trace_svc_tls_unavailable(xprt);
475 /* Mark the transport ready in case the remote sent RPC
476 * traffic before the kernel received the handshake
477 * completion downcall.
479 set_bit(XPT_DATA, &xprt->xpt_flags);
480 svc_xprt_enqueue(xprt);
484 set_bit(XPT_CLOSE, &xprt->xpt_flags);
486 clear_bit(XPT_HANDSHAKE, &xprt->xpt_flags);
487 set_bit(XPT_DATA, &xprt->xpt_flags);
488 svc_xprt_enqueue(xprt);
492 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
494 static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
497 struct in_pktinfo *pki = CMSG_DATA(cmh);
498 struct sockaddr_in *daddr = svc_daddr_in(rqstp);
500 if (cmh->cmsg_type != IP_PKTINFO)
503 daddr->sin_family = AF_INET;
504 daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
509 * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
511 static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
514 struct in6_pktinfo *pki = CMSG_DATA(cmh);
515 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
517 if (cmh->cmsg_type != IPV6_PKTINFO)
520 daddr->sin6_family = AF_INET6;
521 daddr->sin6_addr = pki->ipi6_addr;
522 daddr->sin6_scope_id = pki->ipi6_ifindex;
527 * Copy the UDP datagram's destination address to the rqstp structure.
528 * The 'destination' address in this case is the address to which the
529 * peer sent the datagram, i.e. our local address. For multihomed
530 * hosts, this can change from msg to msg. Note that only the IP
531 * address changes, the port number should remain the same.
533 static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
536 switch (cmh->cmsg_level) {
538 return svc_udp_get_dest_address4(rqstp, cmh);
540 return svc_udp_get_dest_address6(rqstp, cmh);
547 * svc_udp_recvfrom - Receive a datagram from a UDP socket.
548 * @rqstp: request structure into which to receive an RPC Call
550 * Called in a loop when XPT_DATA has been set.
553 * On success, the number of bytes in a received RPC Call, or
554 * %0 if a complete RPC Call message was not ready to return
556 static int svc_udp_recvfrom(struct svc_rqst *rqstp)
558 struct svc_sock *svsk =
559 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
560 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
564 long all[SVC_PKTINFO_SPACE / sizeof(long)];
566 struct cmsghdr *cmh = &buffer.hdr;
567 struct msghdr msg = {
568 .msg_name = svc_addr(rqstp),
570 .msg_controllen = sizeof(buffer),
571 .msg_flags = MSG_DONTWAIT,
576 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
577 /* udp sockets need large rcvbuf as all pending
578 * requests are still in that buffer. sndbuf must
579 * also be large enough that there is enough space
580 * for one reply per thread. We count all threads
581 * rather than threads in a particular pool, which
582 * provides an upper bound on the number of threads
583 * which will access the socket.
585 svc_sock_setbufsize(svsk, serv->sv_nrthreads + 3);
587 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
588 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
589 0, 0, MSG_PEEK | MSG_DONTWAIT);
592 skb = skb_recv_udp(svsk->sk_sk, MSG_DONTWAIT, &err);
596 len = svc_addr_len(svc_addr(rqstp));
597 rqstp->rq_addrlen = len;
598 if (skb->tstamp == 0) {
599 skb->tstamp = ktime_get_real();
600 /* Don't enable netstamp, sunrpc doesn't
601 need that much accuracy */
603 sock_write_timestamp(svsk->sk_sk, skb->tstamp);
604 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
607 rqstp->rq_arg.len = len;
608 trace_svcsock_udp_recv(&svsk->sk_xprt, len);
610 rqstp->rq_prot = IPPROTO_UDP;
612 if (!svc_udp_get_dest_address(rqstp, cmh))
614 rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
616 if (skb_is_nonlinear(skb)) {
617 /* we have to copy */
619 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb))
624 /* we can use it in-place */
625 rqstp->rq_arg.head[0].iov_base = skb->data;
626 rqstp->rq_arg.head[0].iov_len = len;
627 if (skb_checksum_complete(skb))
629 rqstp->rq_xprt_ctxt = skb;
632 rqstp->rq_arg.page_base = 0;
633 if (len <= rqstp->rq_arg.head[0].iov_len) {
634 rqstp->rq_arg.head[0].iov_len = len;
635 rqstp->rq_arg.page_len = 0;
636 rqstp->rq_respages = rqstp->rq_pages+1;
638 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
639 rqstp->rq_respages = rqstp->rq_pages + 1 +
640 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
642 rqstp->rq_next_page = rqstp->rq_respages+1;
645 serv->sv_stats->netudpcnt++;
647 svc_sock_secure_port(rqstp);
648 svc_xprt_received(rqstp->rq_xprt);
652 if (err != -EAGAIN) {
653 /* possibly an icmp error */
654 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
656 trace_svcsock_udp_recv_err(&svsk->sk_xprt, err);
659 net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
660 cmh->cmsg_level, cmh->cmsg_type);
667 svc_xprt_received(rqstp->rq_xprt);
672 * svc_udp_sendto - Send out a reply on a UDP socket
673 * @rqstp: completed svc_rqst
675 * xpt_mutex ensures @rqstp's whole message is written to the socket
676 * without interruption.
678 * Returns the number of bytes sent, or a negative errno.
680 static int svc_udp_sendto(struct svc_rqst *rqstp)
682 struct svc_xprt *xprt = rqstp->rq_xprt;
683 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
684 struct xdr_buf *xdr = &rqstp->rq_res;
687 long all[SVC_PKTINFO_SPACE / sizeof(long)];
689 struct cmsghdr *cmh = &buffer.hdr;
690 struct msghdr msg = {
691 .msg_name = &rqstp->rq_addr,
692 .msg_namelen = rqstp->rq_addrlen,
694 .msg_controllen = sizeof(buffer),
699 svc_udp_release_rqst(rqstp);
701 svc_set_cmsg_data(rqstp, cmh);
703 mutex_lock(&xprt->xpt_mutex);
705 if (svc_xprt_is_dead(xprt))
708 err = xdr_alloc_bvec(xdr, GFP_KERNEL);
712 err = xprt_sock_sendmsg(svsk->sk_sock, &msg, xdr, 0, 0, &sent);
713 if (err == -ECONNREFUSED) {
714 /* ICMP error on earlier request. */
715 err = xprt_sock_sendmsg(svsk->sk_sock, &msg, xdr, 0, 0, &sent);
718 trace_svcsock_udp_send(xprt, err);
720 mutex_unlock(&xprt->xpt_mutex);
726 mutex_unlock(&xprt->xpt_mutex);
730 static int svc_udp_has_wspace(struct svc_xprt *xprt)
732 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
733 struct svc_serv *serv = xprt->xpt_server;
734 unsigned long required;
737 * Set the SOCK_NOSPACE flag before checking the available
740 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
741 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
742 if (required*2 > sock_wspace(svsk->sk_sk))
744 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
748 static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
754 static void svc_udp_kill_temp_xprt(struct svc_xprt *xprt)
758 static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
760 struct sockaddr *sa, int salen,
763 return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
766 static const struct svc_xprt_ops svc_udp_ops = {
767 .xpo_create = svc_udp_create,
768 .xpo_recvfrom = svc_udp_recvfrom,
769 .xpo_sendto = svc_udp_sendto,
770 .xpo_result_payload = svc_sock_result_payload,
771 .xpo_release_rqst = svc_udp_release_rqst,
772 .xpo_detach = svc_sock_detach,
773 .xpo_free = svc_sock_free,
774 .xpo_has_wspace = svc_udp_has_wspace,
775 .xpo_accept = svc_udp_accept,
776 .xpo_kill_temp_xprt = svc_udp_kill_temp_xprt,
779 static struct svc_xprt_class svc_udp_class = {
781 .xcl_owner = THIS_MODULE,
782 .xcl_ops = &svc_udp_ops,
783 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
784 .xcl_ident = XPRT_TRANSPORT_UDP,
787 static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
789 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
790 &svsk->sk_xprt, serv);
791 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
792 svsk->sk_sk->sk_data_ready = svc_data_ready;
793 svsk->sk_sk->sk_write_space = svc_write_space;
795 /* initialise setting must have enough space to
796 * receive and respond to one request.
797 * svc_udp_recvfrom will re-adjust if necessary
799 svc_sock_setbufsize(svsk, 3);
801 /* data might have come in before data_ready set up */
802 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
803 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
805 /* make sure we get destination address info */
806 switch (svsk->sk_sk->sk_family) {
808 ip_sock_set_pktinfo(svsk->sk_sock->sk);
811 ip6_sock_set_recvpktinfo(svsk->sk_sock->sk);
819 * A data_ready event on a listening socket means there's a connection
820 * pending. Do not use state_change as a substitute for it.
822 static void svc_tcp_listen_data_ready(struct sock *sk)
824 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
826 trace_sk_data_ready(sk);
829 /* Refer to svc_setup_socket() for details. */
835 * This callback may called twice when a new connection
836 * is established as a child socket inherits everything
837 * from a parent LISTEN socket.
838 * 1) data_ready method of the parent socket will be called
839 * when one of child sockets become ESTABLISHED.
840 * 2) data_ready method of the child socket may be called
841 * when it receives data before the socket is accepted.
842 * In case of 2, we should ignore it silently.
844 if (sk->sk_state == TCP_LISTEN) {
846 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
847 svc_xprt_enqueue(&svsk->sk_xprt);
853 * A state change on a connected socket means it's dying or dead.
855 static void svc_tcp_state_change(struct sock *sk)
857 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
860 /* Refer to svc_setup_socket() for details. */
863 trace_svcsock_tcp_state(&svsk->sk_xprt, svsk->sk_sock);
864 if (sk->sk_state != TCP_ESTABLISHED)
865 svc_xprt_deferred_close(&svsk->sk_xprt);
870 * Accept a TCP connection
872 static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
874 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
875 struct sockaddr_storage addr;
876 struct sockaddr *sin = (struct sockaddr *) &addr;
877 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
878 struct socket *sock = svsk->sk_sock;
879 struct socket *newsock;
880 struct svc_sock *newsvsk;
886 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
887 err = kernel_accept(sock, &newsock, O_NONBLOCK);
890 printk(KERN_WARNING "%s: no more sockets!\n",
892 else if (err != -EAGAIN)
893 net_warn_ratelimited("%s: accept failed (err %d)!\n",
894 serv->sv_name, -err);
895 trace_svcsock_accept_err(xprt, serv->sv_name, err);
898 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
900 err = kernel_getpeername(newsock, sin);
902 trace_svcsock_getpeername_err(xprt, serv->sv_name, err);
903 goto failed; /* aborted connection or whatever */
907 /* Reset the inherited callbacks before calling svc_setup_socket */
908 newsock->sk->sk_state_change = svsk->sk_ostate;
909 newsock->sk->sk_data_ready = svsk->sk_odata;
910 newsock->sk->sk_write_space = svsk->sk_owspace;
912 /* make sure that a write doesn't block forever when
915 newsock->sk->sk_sndtimeo = HZ*30;
917 newsvsk = svc_setup_socket(serv, newsock,
918 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
921 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
922 err = kernel_getsockname(newsock, sin);
924 if (unlikely(err < 0))
925 slen = offsetof(struct sockaddr, sa_data);
926 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
928 if (sock_is_loopback(newsock->sk))
929 set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
931 clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
933 serv->sv_stats->nettcpconn++;
935 return &newsvsk->sk_xprt;
938 sock_release(newsock);
942 static size_t svc_tcp_restore_pages(struct svc_sock *svsk,
943 struct svc_rqst *rqstp)
945 size_t len = svsk->sk_datalen;
946 unsigned int i, npages;
950 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
951 for (i = 0; i < npages; i++) {
952 if (rqstp->rq_pages[i] != NULL)
953 put_page(rqstp->rq_pages[i]);
954 BUG_ON(svsk->sk_pages[i] == NULL);
955 rqstp->rq_pages[i] = svsk->sk_pages[i];
956 svsk->sk_pages[i] = NULL;
958 rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
962 static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
964 unsigned int i, len, npages;
966 if (svsk->sk_datalen == 0)
968 len = svsk->sk_datalen;
969 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
970 for (i = 0; i < npages; i++) {
971 svsk->sk_pages[i] = rqstp->rq_pages[i];
972 rqstp->rq_pages[i] = NULL;
976 static void svc_tcp_clear_pages(struct svc_sock *svsk)
978 unsigned int i, len, npages;
980 if (svsk->sk_datalen == 0)
982 len = svsk->sk_datalen;
983 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
984 for (i = 0; i < npages; i++) {
985 if (svsk->sk_pages[i] == NULL) {
989 put_page(svsk->sk_pages[i]);
990 svsk->sk_pages[i] = NULL;
994 svsk->sk_datalen = 0;
998 * Receive fragment record header into sk_marker.
1000 static ssize_t svc_tcp_read_marker(struct svc_sock *svsk,
1001 struct svc_rqst *rqstp)
1005 /* If we haven't gotten the record length yet,
1006 * get the next four bytes.
1008 if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
1009 struct msghdr msg = { NULL };
1012 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
1013 iov.iov_base = ((char *)&svsk->sk_marker) + svsk->sk_tcplen;
1015 iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, want);
1016 len = svc_tcp_sock_recv_cmsg(svsk, &msg);
1019 svsk->sk_tcplen += len;
1021 /* call again to read the remaining bytes */
1024 trace_svcsock_marker(&svsk->sk_xprt, svsk->sk_marker);
1025 if (svc_sock_reclen(svsk) + svsk->sk_datalen >
1026 svsk->sk_xprt.xpt_server->sv_max_mesg)
1029 return svc_sock_reclen(svsk);
1032 net_notice_ratelimited("svc: %s %s RPC fragment too large: %d\n",
1033 __func__, svsk->sk_xprt.xpt_server->sv_name,
1034 svc_sock_reclen(svsk));
1035 svc_xprt_deferred_close(&svsk->sk_xprt);
1040 static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
1042 struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
1043 struct rpc_rqst *req = NULL;
1044 struct kvec *src, *dst;
1045 __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1054 spin_lock(&bc_xprt->queue_lock);
1055 req = xprt_lookup_rqst(bc_xprt, xid);
1057 goto unlock_notfound;
1059 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
1061 * XXX!: cheating for now! Only copying HEAD.
1062 * But we know this is good enough for now (in fact, for any
1063 * callback reply in the forseeable future).
1065 dst = &req->rq_private_buf.head[0];
1066 src = &rqstp->rq_arg.head[0];
1067 if (dst->iov_len < src->iov_len)
1068 goto unlock_eagain; /* whatever; just giving up. */
1069 memcpy(dst->iov_base, src->iov_base, src->iov_len);
1070 xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len);
1071 rqstp->rq_arg.len = 0;
1072 spin_unlock(&bc_xprt->queue_lock);
1076 "%s: Got unrecognized reply: "
1077 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
1078 __func__, ntohl(calldir),
1079 bc_xprt, ntohl(xid));
1081 spin_unlock(&bc_xprt->queue_lock);
1085 static void svc_tcp_fragment_received(struct svc_sock *svsk)
1087 /* If we have more data, signal svc_xprt_enqueue() to try again */
1088 svsk->sk_tcplen = 0;
1089 svsk->sk_marker = xdr_zero;
1093 * svc_tcp_recvfrom - Receive data from a TCP socket
1094 * @rqstp: request structure into which to receive an RPC Call
1096 * Called in a loop when XPT_DATA has been set.
1098 * Read the 4-byte stream record marker, then use the record length
1099 * in that marker to set up exactly the resources needed to receive
1100 * the next RPC message into @rqstp.
1103 * On success, the number of bytes in a received RPC Call, or
1104 * %0 if a complete RPC Call message was not ready to return
1106 * The zero return case handles partial receives and callback Replies.
1107 * The state of a partial receive is preserved in the svc_sock for
1108 * the next call to svc_tcp_recvfrom.
1110 static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
1112 struct svc_sock *svsk =
1113 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
1114 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1120 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1121 len = svc_tcp_read_marker(svsk, rqstp);
1125 base = svc_tcp_restore_pages(svsk, rqstp);
1126 want = len - (svsk->sk_tcplen - sizeof(rpc_fraghdr));
1127 len = svc_tcp_read_msg(rqstp, base + want, base);
1129 trace_svcsock_tcp_recv(&svsk->sk_xprt, len);
1130 svsk->sk_tcplen += len;
1131 svsk->sk_datalen += len;
1133 if (len != want || !svc_sock_final_rec(svsk))
1134 goto err_incomplete;
1135 if (svsk->sk_datalen < 8)
1138 rqstp->rq_arg.len = svsk->sk_datalen;
1139 rqstp->rq_arg.page_base = 0;
1140 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1141 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
1142 rqstp->rq_arg.page_len = 0;
1144 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1146 rqstp->rq_xprt_ctxt = NULL;
1147 rqstp->rq_prot = IPPROTO_TCP;
1148 if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags))
1149 set_bit(RQ_LOCAL, &rqstp->rq_flags);
1151 clear_bit(RQ_LOCAL, &rqstp->rq_flags);
1153 p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1156 len = receive_cb_reply(svsk, rqstp);
1158 /* Reset TCP read info */
1159 svsk->sk_datalen = 0;
1160 svc_tcp_fragment_received(svsk);
1165 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
1167 serv->sv_stats->nettcpcnt++;
1169 svc_sock_secure_port(rqstp);
1170 svc_xprt_received(rqstp->rq_xprt);
1171 return rqstp->rq_arg.len;
1174 svc_tcp_save_pages(svsk, rqstp);
1175 if (len < 0 && len != -EAGAIN)
1178 svc_tcp_fragment_received(svsk);
1180 trace_svcsock_tcp_recv_short(&svsk->sk_xprt,
1181 svc_sock_reclen(svsk),
1182 svsk->sk_tcplen - sizeof(rpc_fraghdr));
1187 trace_svcsock_tcp_recv_eagain(&svsk->sk_xprt, 0);
1190 svsk->sk_datalen = 0;
1192 trace_svcsock_tcp_recv_err(&svsk->sk_xprt, len);
1193 svc_xprt_deferred_close(&svsk->sk_xprt);
1195 svc_xprt_received(rqstp->rq_xprt);
1196 return 0; /* record not complete */
1199 static int svc_tcp_send_kvec(struct socket *sock, const struct kvec *vec,
1202 return kernel_sendpage(sock, virt_to_page(vec->iov_base),
1203 offset_in_page(vec->iov_base),
1204 vec->iov_len, flags);
1208 * kernel_sendpage() is used exclusively to reduce the number of
1209 * copy operations in this path. Therefore the caller must ensure
1210 * that the pages backing @xdr are unchanging.
1212 * In addition, the logic assumes that * .bv_len is never larger
1215 static int svc_tcp_sendmsg(struct socket *sock, struct xdr_buf *xdr,
1216 rpc_fraghdr marker, unsigned int *sentp)
1218 const struct kvec *head = xdr->head;
1219 const struct kvec *tail = xdr->tail;
1221 .iov_base = &marker,
1222 .iov_len = sizeof(marker),
1224 struct msghdr msg = {
1230 ret = xdr_alloc_bvec(xdr, GFP_KERNEL);
1234 ret = kernel_sendmsg(sock, &msg, &rm, 1, rm.iov_len);
1238 if (ret != rm.iov_len)
1241 ret = svc_tcp_send_kvec(sock, head, 0);
1245 if (ret != head->iov_len)
1248 if (xdr->page_len) {
1249 unsigned int offset, len, remaining;
1250 struct bio_vec *bvec;
1252 bvec = xdr->bvec + (xdr->page_base >> PAGE_SHIFT);
1253 offset = offset_in_page(xdr->page_base);
1254 remaining = xdr->page_len;
1255 while (remaining > 0) {
1256 len = min(remaining, bvec->bv_len - offset);
1257 ret = kernel_sendpage(sock, bvec->bv_page,
1258 bvec->bv_offset + offset,
1271 if (tail->iov_len) {
1272 ret = svc_tcp_send_kvec(sock, tail, 0);
1283 * svc_tcp_sendto - Send out a reply on a TCP socket
1284 * @rqstp: completed svc_rqst
1286 * xpt_mutex ensures @rqstp's whole message is written to the socket
1287 * without interruption.
1289 * Returns the number of bytes sent, or a negative errno.
1291 static int svc_tcp_sendto(struct svc_rqst *rqstp)
1293 struct svc_xprt *xprt = rqstp->rq_xprt;
1294 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1295 struct xdr_buf *xdr = &rqstp->rq_res;
1296 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
1301 svc_tcp_release_rqst(rqstp);
1303 atomic_inc(&svsk->sk_sendqlen);
1304 mutex_lock(&xprt->xpt_mutex);
1305 if (svc_xprt_is_dead(xprt))
1307 tcp_sock_set_cork(svsk->sk_sk, true);
1308 err = svc_tcp_sendmsg(svsk->sk_sock, xdr, marker, &sent);
1310 trace_svcsock_tcp_send(xprt, err < 0 ? (long)err : sent);
1311 if (err < 0 || sent != (xdr->len + sizeof(marker)))
1313 if (atomic_dec_and_test(&svsk->sk_sendqlen))
1314 tcp_sock_set_cork(svsk->sk_sk, false);
1315 mutex_unlock(&xprt->xpt_mutex);
1319 atomic_dec(&svsk->sk_sendqlen);
1320 mutex_unlock(&xprt->xpt_mutex);
1323 pr_notice("rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1324 xprt->xpt_server->sv_name,
1325 (err < 0) ? "got error" : "sent",
1326 (err < 0) ? err : sent, xdr->len);
1327 svc_xprt_deferred_close(xprt);
1328 atomic_dec(&svsk->sk_sendqlen);
1329 mutex_unlock(&xprt->xpt_mutex);
1333 static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
1335 struct sockaddr *sa, int salen,
1338 return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1341 static const struct svc_xprt_ops svc_tcp_ops = {
1342 .xpo_create = svc_tcp_create,
1343 .xpo_recvfrom = svc_tcp_recvfrom,
1344 .xpo_sendto = svc_tcp_sendto,
1345 .xpo_result_payload = svc_sock_result_payload,
1346 .xpo_release_rqst = svc_tcp_release_rqst,
1347 .xpo_detach = svc_tcp_sock_detach,
1348 .xpo_free = svc_sock_free,
1349 .xpo_has_wspace = svc_tcp_has_wspace,
1350 .xpo_accept = svc_tcp_accept,
1351 .xpo_kill_temp_xprt = svc_tcp_kill_temp_xprt,
1352 .xpo_handshake = svc_tcp_handshake,
1355 static struct svc_xprt_class svc_tcp_class = {
1357 .xcl_owner = THIS_MODULE,
1358 .xcl_ops = &svc_tcp_ops,
1359 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1360 .xcl_ident = XPRT_TRANSPORT_TCP,
1363 void svc_init_xprt_sock(void)
1365 svc_reg_xprt_class(&svc_tcp_class);
1366 svc_reg_xprt_class(&svc_udp_class);
1369 void svc_cleanup_xprt_sock(void)
1371 svc_unreg_xprt_class(&svc_tcp_class);
1372 svc_unreg_xprt_class(&svc_udp_class);
1375 static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
1377 struct sock *sk = svsk->sk_sk;
1379 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
1380 &svsk->sk_xprt, serv);
1381 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
1382 set_bit(XPT_CONG_CTRL, &svsk->sk_xprt.xpt_flags);
1383 if (sk->sk_state == TCP_LISTEN) {
1384 strcpy(svsk->sk_xprt.xpt_remotebuf, "listener");
1385 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
1386 sk->sk_data_ready = svc_tcp_listen_data_ready;
1387 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
1389 sk->sk_state_change = svc_tcp_state_change;
1390 sk->sk_data_ready = svc_data_ready;
1391 sk->sk_write_space = svc_write_space;
1393 svsk->sk_marker = xdr_zero;
1394 svsk->sk_tcplen = 0;
1395 svsk->sk_datalen = 0;
1396 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
1398 tcp_sock_set_nodelay(sk);
1400 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1401 switch (sk->sk_state) {
1403 case TCP_ESTABLISHED:
1406 svc_xprt_deferred_close(&svsk->sk_xprt);
1411 void svc_sock_update_bufs(struct svc_serv *serv)
1414 * The number of server threads has changed. Update
1415 * rcvbuf and sndbuf accordingly on all sockets
1417 struct svc_sock *svsk;
1419 spin_lock_bh(&serv->sv_lock);
1420 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
1421 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1422 spin_unlock_bh(&serv->sv_lock);
1424 EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
1427 * Initialize socket for RPC use and create svc_sock struct
1429 static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1430 struct socket *sock,
1433 struct file *filp = NULL;
1434 struct svc_sock *svsk;
1436 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1438 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1440 return ERR_PTR(-ENOMEM);
1443 filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1446 return ERR_CAST(filp);
1452 if (pmap_register) {
1455 err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
1457 ntohs(inet_sk(inet)->inet_sport));
1462 return ERR_PTR(err);
1466 svsk->sk_sock = sock;
1468 svsk->sk_ostate = inet->sk_state_change;
1469 svsk->sk_odata = inet->sk_data_ready;
1470 svsk->sk_owspace = inet->sk_write_space;
1472 * This barrier is necessary in order to prevent race condition
1473 * with svc_data_ready(), svc_listen_data_ready() and others
1474 * when calling callbacks above.
1477 inet->sk_user_data = svsk;
1479 /* Initialize the socket */
1480 if (sock->type == SOCK_DGRAM)
1481 svc_udp_init(svsk, serv);
1483 svc_tcp_init(svsk, serv);
1485 trace_svcsock_new_socket(sock);
1489 bool svc_alien_sock(struct net *net, int fd)
1492 struct socket *sock = sockfd_lookup(fd, &err);
1497 if (sock_net(sock->sk) != net)
1503 EXPORT_SYMBOL_GPL(svc_alien_sock);
1506 * svc_addsock - add a listener socket to an RPC service
1507 * @serv: pointer to RPC service to which to add a new listener
1508 * @fd: file descriptor of the new listener
1509 * @name_return: pointer to buffer to fill in with name of listener
1510 * @len: size of the buffer
1513 * Fills in socket name and returns positive length of name if successful.
1514 * Name is terminated with '\n'. On error, returns a negative errno
1517 int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
1518 const size_t len, const struct cred *cred)
1521 struct socket *so = sockfd_lookup(fd, &err);
1522 struct svc_sock *svsk = NULL;
1523 struct sockaddr_storage addr;
1524 struct sockaddr *sin = (struct sockaddr *)&addr;
1529 err = -EAFNOSUPPORT;
1530 if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
1532 err = -EPROTONOSUPPORT;
1533 if (so->sk->sk_protocol != IPPROTO_TCP &&
1534 so->sk->sk_protocol != IPPROTO_UDP)
1537 if (so->state > SS_UNCONNECTED)
1540 if (!try_module_get(THIS_MODULE))
1542 svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1544 module_put(THIS_MODULE);
1545 err = PTR_ERR(svsk);
1548 salen = kernel_getsockname(svsk->sk_sock, sin);
1550 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
1551 svsk->sk_xprt.xpt_cred = get_cred(cred);
1552 svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
1553 return svc_one_sock_name(svsk, name_return, len);
1558 EXPORT_SYMBOL_GPL(svc_addsock);
1561 * Create socket for RPC service.
1563 static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1566 struct sockaddr *sin, int len,
1569 struct svc_sock *svsk;
1570 struct socket *sock;
1573 struct sockaddr_storage addr;
1574 struct sockaddr *newsin = (struct sockaddr *)&addr;
1578 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1579 printk(KERN_WARNING "svc: only UDP and TCP "
1580 "sockets supported\n");
1581 return ERR_PTR(-EINVAL);
1584 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1585 switch (sin->sa_family) {
1593 return ERR_PTR(-EINVAL);
1596 error = __sock_create(net, family, type, protocol, &sock, 1);
1598 return ERR_PTR(error);
1600 svc_reclassify_socket(sock);
1603 * If this is an PF_INET6 listener, we want to avoid
1604 * getting requests from IPv4 remotes. Those should
1605 * be shunted to a PF_INET listener via rpcbind.
1607 if (family == PF_INET6)
1608 ip6_sock_set_v6only(sock->sk);
1609 if (type == SOCK_STREAM)
1610 sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
1611 error = kernel_bind(sock, sin, len);
1615 error = kernel_getsockname(sock, newsin);
1620 if (protocol == IPPROTO_TCP) {
1621 if ((error = kernel_listen(sock, 64)) < 0)
1625 svsk = svc_setup_socket(serv, sock, flags);
1627 error = PTR_ERR(svsk);
1630 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1631 return (struct svc_xprt *)svsk;
1634 return ERR_PTR(error);
1638 * Detach the svc_sock from the socket so that no
1639 * more callbacks occur.
1641 static void svc_sock_detach(struct svc_xprt *xprt)
1643 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1644 struct sock *sk = svsk->sk_sk;
1646 /* put back the old socket callbacks */
1648 sk->sk_state_change = svsk->sk_ostate;
1649 sk->sk_data_ready = svsk->sk_odata;
1650 sk->sk_write_space = svsk->sk_owspace;
1651 sk->sk_user_data = NULL;
1656 * Disconnect the socket, and reset the callbacks
1658 static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1660 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1662 svc_sock_detach(xprt);
1664 if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1665 svc_tcp_clear_pages(svsk);
1666 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
1671 * Free the svc_sock's socket resources and the svc_sock itself.
1673 static void svc_sock_free(struct svc_xprt *xprt)
1675 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1676 struct socket *sock = svsk->sk_sock;
1678 tls_handshake_cancel(sock->sk);