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