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