blob: af04f779ce9f1afb624c3dd20d99941fa6c1e60e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/svcsock.c
3 *
4 * These are the RPC server socket internals.
5 *
6 * The server scheduling algorithm does not always distribute the load
7 * evenly when servicing a single client. May need to modify the
Tom Tuckerf6150c32007-12-30 21:07:57 -06008 * svc_xprt_enqueue procedure...
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * TCP support is largely untested and may be a little slow. The problem
11 * is that we currently do two separate recvfrom's, one for the 4-byte
12 * record length, and the second for the actual record. This could possibly
13 * be improved by always reading a minimum size of around 100 bytes and
14 * tucking any superfluous bytes away in a temporary store. Still, that
15 * leaves write requests out in the rain. An alternative may be to peek at
16 * the first skb in the queue, and if it matches the next TCP sequence
17 * number, to extract the record marker. Yuck.
18 *
19 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
20 */
21
Ilpo Järvinen172589c2007-08-28 15:50:33 -070022#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/sched.h>
24#include <linux/errno.h>
25#include <linux/fcntl.h>
26#include <linux/net.h>
27#include <linux/in.h>
28#include <linux/inet.h>
29#include <linux/udp.h>
Andrew Morton91483c42005-08-09 20:20:07 -070030#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/unistd.h>
32#include <linux/slab.h>
33#include <linux/netdevice.h>
34#include <linux/skbuff.h>
NeilBrownb41b66d2006-10-02 02:17:48 -070035#include <linux/file.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <net/sock.h>
38#include <net/checksum.h>
39#include <net/ip.h>
Chuck Leverb92503b2007-02-12 00:53:36 -080040#include <net/ipv6.h>
Chuck Leverb7872fe2008-04-14 12:27:01 -040041#include <net/tcp.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070042#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/uaccess.h>
44#include <asm/ioctls.h>
45
46#include <linux/sunrpc/types.h>
Chuck Leverad06e4b2007-02-12 00:53:32 -080047#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/sunrpc/xdr.h>
Chuck Leverc0401ea2008-04-14 12:27:30 -040049#include <linux/sunrpc/msg_prot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/sunrpc/svcsock.h>
51#include <linux/sunrpc/stats.h>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030052#include <linux/sunrpc/xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Tom Tucker360d8732007-12-30 21:07:17 -060054#define RPCDBG_FACILITY RPCDBG_SVCXPRT
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56
57static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
Chuck Lever6b174332007-02-12 00:53:28 -080058 int *errp, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static void svc_udp_data_ready(struct sock *, int);
60static int svc_udp_recvfrom(struct svc_rqst *);
61static int svc_udp_sendto(struct svc_rqst *);
Tom Tucker755ccea2007-12-30 21:07:27 -060062static void svc_sock_detach(struct svc_xprt *);
Trond Myklebust69b6ba32008-12-23 16:30:11 -050063static void svc_tcp_sock_detach(struct svc_xprt *);
Tom Tucker755ccea2007-12-30 21:07:27 -060064static void svc_sock_free(struct svc_xprt *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Tom Tuckerb700cbb2007-12-30 21:07:42 -060066static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
Pavel Emelyanov62832c02010-09-29 16:04:18 +040067 struct net *, struct sockaddr *,
68 int, int);
Andy Adamson1f11a032011-01-06 02:04:26 +000069#if defined(CONFIG_NFS_V4_1)
70static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
71 struct net *, struct sockaddr *,
72 int, int);
73static void svc_bc_sock_free(struct svc_xprt *xprt);
74#endif /* CONFIG_NFS_V4_1 */
75
Peter Zijlstraed075362006-12-06 20:35:24 -080076#ifdef CONFIG_DEBUG_LOCK_ALLOC
77static struct lock_class_key svc_key[2];
78static struct lock_class_key svc_slock_key[2];
79
Tom Tucker0f0257e2007-12-30 21:08:27 -060080static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -080081{
82 struct sock *sk = sock->sk;
John Heffner02b3d342007-09-12 10:42:12 +020083 BUG_ON(sock_owned_by_user(sk));
Peter Zijlstraed075362006-12-06 20:35:24 -080084 switch (sk->sk_family) {
85 case AF_INET:
86 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -060087 &svc_slock_key[0],
88 "sk_xprt.xpt_lock-AF_INET-NFSD",
89 &svc_key[0]);
Peter Zijlstraed075362006-12-06 20:35:24 -080090 break;
91
92 case AF_INET6:
93 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -060094 &svc_slock_key[1],
95 "sk_xprt.xpt_lock-AF_INET6-NFSD",
96 &svc_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -080097 break;
98
99 default:
100 BUG();
101 }
102}
103#else
Tom Tucker0f0257e2007-12-30 21:08:27 -0600104static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -0800105{
106}
107#endif
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * Release an skbuff after use
111 */
Tom Tucker5148bf42007-12-30 21:07:25 -0600112static void svc_release_skb(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Tom Tucker5148bf42007-12-30 21:07:25 -0600114 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (skb) {
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600117 struct svc_sock *svsk =
118 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tucker5148bf42007-12-30 21:07:25 -0600119 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
Eric Dumazet9d410c72009-10-30 05:03:53 +0000122 skb_free_datagram_locked(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Chuck Leverb92503b2007-02-12 00:53:36 -0800126union svc_pktinfo_u {
127 struct in_pktinfo pkti;
Chuck Leverb92503b2007-02-12 00:53:36 -0800128 struct in6_pktinfo pkti6;
Chuck Leverb92503b2007-02-12 00:53:36 -0800129};
David S. Millerbc375ea2007-04-12 13:35:59 -0700130#define SVC_PKTINFO_SPACE \
131 CMSG_SPACE(sizeof(union svc_pktinfo_u))
Chuck Leverb92503b2007-02-12 00:53:36 -0800132
133static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
134{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600135 struct svc_sock *svsk =
136 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
137 switch (svsk->sk_sk->sk_family) {
Chuck Leverb92503b2007-02-12 00:53:36 -0800138 case AF_INET: {
139 struct in_pktinfo *pki = CMSG_DATA(cmh);
140
141 cmh->cmsg_level = SOL_IP;
142 cmh->cmsg_type = IP_PKTINFO;
143 pki->ipi_ifindex = 0;
144 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr.addr.s_addr;
145 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
146 }
147 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800148
Chuck Leverb92503b2007-02-12 00:53:36 -0800149 case AF_INET6: {
150 struct in6_pktinfo *pki = CMSG_DATA(cmh);
151
152 cmh->cmsg_level = SOL_IPV6;
153 cmh->cmsg_type = IPV6_PKTINFO;
154 pki->ipi6_ifindex = 0;
155 ipv6_addr_copy(&pki->ipi6_addr,
156 &rqstp->rq_daddr.addr6);
157 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
158 }
159 break;
Chuck Leverb92503b2007-02-12 00:53:36 -0800160 }
Chuck Leverb92503b2007-02-12 00:53:36 -0800161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300164 * send routine intended to be shared by the fore- and back-channel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300166int svc_send_common(struct socket *sock, struct xdr_buf *xdr,
167 struct page *headpage, unsigned long headoffset,
168 struct page *tailpage, unsigned long tailoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 int result;
171 int size;
172 struct page **ppage = xdr->pages;
173 size_t base = xdr->page_base;
174 unsigned int pglen = xdr->page_len;
175 unsigned int flags = MSG_MORE;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300176 int slen;
177 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 slen = xdr->len;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /* send head */
182 if (slen == xdr->head[0].iov_len)
183 flags = 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300184 len = kernel_sendpage(sock, headpage, headoffset,
NeilBrown44524352006-10-04 02:15:46 -0700185 xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (len != xdr->head[0].iov_len)
187 goto out;
188 slen -= xdr->head[0].iov_len;
189 if (slen == 0)
190 goto out;
191
192 /* send page data */
193 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
194 while (pglen > 0) {
195 if (slen == size)
196 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700197 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (result > 0)
199 len += result;
200 if (result != size)
201 goto out;
202 slen -= size;
203 pglen -= size;
204 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
205 base = 0;
206 ppage++;
207 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* send tail */
210 if (xdr->tail[0].iov_len) {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300211 result = kernel_sendpage(sock, tailpage, tailoffset,
212 xdr->tail[0].iov_len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (result > 0)
214 len += result;
215 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300216
217out:
218 return len;
219}
220
221
222/*
223 * Generic sendto routine
224 */
225static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
226{
227 struct svc_sock *svsk =
228 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
229 struct socket *sock = svsk->sk_sock;
230 union {
231 struct cmsghdr hdr;
232 long all[SVC_PKTINFO_SPACE / sizeof(long)];
233 } buffer;
234 struct cmsghdr *cmh = &buffer.hdr;
235 int len = 0;
236 unsigned long tailoff;
237 unsigned long headoff;
238 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
239
240 if (rqstp->rq_prot == IPPROTO_UDP) {
241 struct msghdr msg = {
242 .msg_name = &rqstp->rq_addr,
243 .msg_namelen = rqstp->rq_addrlen,
244 .msg_control = cmh,
245 .msg_controllen = sizeof(buffer),
246 .msg_flags = MSG_MORE,
247 };
248
249 svc_set_cmsg_data(rqstp, cmh);
250
251 if (sock_sendmsg(sock, &msg, 0) < 0)
252 goto out;
253 }
254
255 tailoff = ((unsigned long)xdr->tail[0].iov_base) & (PAGE_SIZE-1);
256 headoff = 0;
257 len = svc_send_common(sock, xdr, rqstp->rq_respages[0], headoff,
258 rqstp->rq_respages[0], tailoff);
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260out:
Chuck Leverad06e4b2007-02-12 00:53:32 -0800261 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600262 svsk, xdr->head[0].iov_base, xdr->head[0].iov_len,
Chuck Leverad06e4b2007-02-12 00:53:32 -0800263 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 return len;
266}
267
268/*
NeilBrown80212d52006-10-02 02:17:47 -0700269 * Report socket names for nfsdfs
270 */
Chuck Levere7942b92009-04-23 19:32:48 -0400271static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
NeilBrown80212d52006-10-02 02:17:47 -0700272{
Chuck Lever017cb472009-04-23 19:33:03 -0400273 const struct sock *sk = svsk->sk_sk;
274 const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
275 "udp" : "tcp";
NeilBrown80212d52006-10-02 02:17:47 -0700276 int len;
277
Chuck Lever017cb472009-04-23 19:33:03 -0400278 switch (sk->sk_family) {
Chuck Levere7942b92009-04-23 19:32:48 -0400279 case PF_INET:
280 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400281 proto_name,
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000282 &inet_sk(sk)->inet_rcv_saddr,
283 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700284 break;
Chuck Lever58de2f82009-04-23 19:32:55 -0400285 case PF_INET6:
286 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400287 proto_name,
288 &inet6_sk(sk)->rcv_saddr,
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000289 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700290 break;
291 default:
Chuck Levere7942b92009-04-23 19:32:48 -0400292 len = snprintf(buf, remaining, "*unknown-%d*\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400293 sk->sk_family);
NeilBrown80212d52006-10-02 02:17:47 -0700294 }
Chuck Levere7942b92009-04-23 19:32:48 -0400295
296 if (len >= remaining) {
297 *buf = '\0';
298 return -ENAMETOOLONG;
NeilBrown80212d52006-10-02 02:17:47 -0700299 }
300 return len;
301}
302
Chuck Lever8435d342009-04-23 19:32:40 -0400303/**
304 * svc_sock_names - construct a list of listener names in a string
305 * @serv: pointer to RPC service
306 * @buf: pointer to a buffer to fill in with socket names
307 * @buflen: size of the buffer to be filled
308 * @toclose: pointer to '\0'-terminated C string containing the name
309 * of a listener to be closed
310 *
311 * Fills in @buf with a '\n'-separated list of names of listener
312 * sockets. If @toclose is not NULL, the socket named by @toclose
313 * is closed, and is not included in the output list.
314 *
315 * Returns positive length of the socket name string, or a negative
316 * errno value on error.
317 */
318int svc_sock_names(struct svc_serv *serv, char *buf, const size_t buflen,
319 const char *toclose)
NeilBrown80212d52006-10-02 02:17:47 -0700320{
NeilBrownb41b66d2006-10-02 02:17:48 -0700321 struct svc_sock *svsk, *closesk = NULL;
NeilBrown80212d52006-10-02 02:17:47 -0700322 int len = 0;
323
324 if (!serv)
325 return 0;
Chuck Levere7942b92009-04-23 19:32:48 -0400326
NeilBrownaaf68cf2007-02-08 14:20:30 -0800327 spin_lock_bh(&serv->sv_lock);
Tom Tucker7a182082007-12-30 21:07:53 -0600328 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list) {
Chuck Levere7942b92009-04-23 19:32:48 -0400329 int onelen = svc_one_sock_name(svsk, buf + len, buflen - len);
330 if (onelen < 0) {
331 len = onelen;
332 break;
333 }
NeilBrown39423022010-11-15 11:27:01 +1100334 if (toclose && strcmp(toclose, buf + len) == 0) {
NeilBrownb41b66d2006-10-02 02:17:48 -0700335 closesk = svsk;
NeilBrown39423022010-11-15 11:27:01 +1100336 svc_xprt_get(&closesk->sk_xprt);
337 } else
NeilBrownb41b66d2006-10-02 02:17:48 -0700338 len += onelen;
NeilBrown80212d52006-10-02 02:17:47 -0700339 }
NeilBrownaaf68cf2007-02-08 14:20:30 -0800340 spin_unlock_bh(&serv->sv_lock);
Chuck Levere7942b92009-04-23 19:32:48 -0400341
NeilBrown39423022010-11-15 11:27:01 +1100342 if (closesk) {
NeilBrown5680c442006-10-04 02:15:45 -0700343 /* Should unregister with portmap, but you cannot
344 * unregister just one protocol...
345 */
Tom Tucker7a182082007-12-30 21:07:53 -0600346 svc_close_xprt(&closesk->sk_xprt);
NeilBrown39423022010-11-15 11:27:01 +1100347 svc_xprt_put(&closesk->sk_xprt);
348 } else if (toclose)
NeilBrown37a03472006-10-04 02:15:44 -0700349 return -ENOENT;
NeilBrown80212d52006-10-02 02:17:47 -0700350 return len;
351}
Trond Myklebust24c37672008-12-23 16:30:12 -0500352EXPORT_SYMBOL_GPL(svc_sock_names);
NeilBrown80212d52006-10-02 02:17:47 -0700353
354/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * Check input queue length
356 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600357static int svc_recv_available(struct svc_sock *svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 struct socket *sock = svsk->sk_sock;
360 int avail, err;
361
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700362 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 return (err >= 0)? avail : err;
365}
366
367/*
368 * Generic recvfrom routine.
369 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600370static int svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr,
371 int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600373 struct svc_sock *svsk =
374 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Chuck Lever1ba95102007-02-12 00:53:31 -0800375 struct msghdr msg = {
376 .msg_flags = MSG_DONTWAIT,
377 };
378 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Tom Tucker260c1d12007-12-30 21:08:29 -0600380 rqstp->rq_xprt_hlen = 0;
381
Chuck Lever1ba95102007-02-12 00:53:31 -0800382 len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
383 msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
Chuck Lever1ba95102007-02-12 00:53:31 -0800386 svsk, iov[0].iov_base, iov[0].iov_len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return len;
388}
389
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800390static int svc_partial_recvfrom(struct svc_rqst *rqstp,
391 struct kvec *iov, int nr,
392 int buflen, unsigned int base)
393{
394 size_t save_iovlen;
395 void __user *save_iovbase;
396 unsigned int i;
397 int ret;
398
399 if (base == 0)
400 return svc_recvfrom(rqstp, iov, nr, buflen);
401
402 for (i = 0; i < nr; i++) {
403 if (iov[i].iov_len > base)
404 break;
405 base -= iov[i].iov_len;
406 }
407 save_iovlen = iov[i].iov_len;
408 save_iovbase = iov[i].iov_base;
409 iov[i].iov_len -= base;
410 iov[i].iov_base += base;
411 ret = svc_recvfrom(rqstp, &iov[i], nr - i, buflen);
412 iov[i].iov_len = save_iovlen;
413 iov[i].iov_base = save_iovbase;
414 return ret;
415}
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/*
418 * Set socket snd and rcv buffer lengths
419 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600420static void svc_sock_setbufsize(struct socket *sock, unsigned int snd,
421 unsigned int rcv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423#if 0
424 mm_segment_t oldfs;
425 oldfs = get_fs(); set_fs(KERNEL_DS);
426 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
427 (char*)&snd, sizeof(snd));
428 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
429 (char*)&rcv, sizeof(rcv));
430#else
431 /* sock_setsockopt limits use to sysctl_?mem_max,
432 * which isn't acceptable. Until that is made conditional
433 * on not having CAP_SYS_RESOURCE or similar, we go direct...
434 * DaveM said I could!
435 */
436 lock_sock(sock->sk);
437 sock->sk->sk_sndbuf = snd * 2;
438 sock->sk->sk_rcvbuf = rcv * 2;
Trond Myklebust47fcb032009-05-18 17:47:56 -0400439 sock->sk->sk_write_space(sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 release_sock(sock->sk);
441#endif
442}
443/*
444 * INET callback when data has been received on the socket.
445 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600446static void svc_udp_data_ready(struct sock *sk, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Neil Brown939bb7e2005-09-13 01:25:39 -0700448 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000449 wait_queue_head_t *wq = sk_sleep(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Neil Brown939bb7e2005-09-13 01:25:39 -0700451 if (svsk) {
452 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600453 svsk, sk, count,
454 test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
455 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600456 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700457 }
Eric Dumazeteaefd112011-02-18 03:26:36 +0000458 if (wq && waitqueue_active(wq))
459 wake_up_interruptible(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462/*
463 * INET callback when space is newly available on the socket.
464 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600465static void svc_write_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
Eric Dumazeteaefd112011-02-18 03:26:36 +0000468 wait_queue_head_t *wq = sk_sleep(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 if (svsk) {
471 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600472 svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Tom Tuckerf6150c32007-12-30 21:07:57 -0600473 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475
Eric Dumazeteaefd112011-02-18 03:26:36 +0000476 if (wq && waitqueue_active(wq)) {
Neil Brown939bb7e2005-09-13 01:25:39 -0700477 dprintk("RPC svc_write_space: someone sleeping on %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 svsk);
Eric Dumazeteaefd112011-02-18 03:26:36 +0000479 wake_up_interruptible(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481}
482
Trond Myklebust47fcb032009-05-18 17:47:56 -0400483static void svc_tcp_write_space(struct sock *sk)
484{
485 struct socket *sock = sk->sk_socket;
486
487 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) && sock)
488 clear_bit(SOCK_NOSPACE, &sock->flags);
489 svc_write_space(sk);
490}
491
Tom Tucker9dbc2402007-12-30 21:08:12 -0600492/*
Chuck Lever7702ce42009-07-13 10:54:26 -0400493 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
494 */
495static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
496 struct cmsghdr *cmh)
497{
498 struct in_pktinfo *pki = CMSG_DATA(cmh);
499 if (cmh->cmsg_type != IP_PKTINFO)
500 return 0;
501 rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr;
502 return 1;
503}
504
505/*
506 * See net/ipv6/datagram.c : datagram_recv_ctl
507 */
508static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
509 struct cmsghdr *cmh)
510{
511 struct in6_pktinfo *pki = CMSG_DATA(cmh);
512 if (cmh->cmsg_type != IPV6_PKTINFO)
513 return 0;
514 ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr);
515 return 1;
516}
517
518/*
Tom Tucker9dbc2402007-12-30 21:08:12 -0600519 * Copy the UDP datagram's destination address to the rqstp structure.
520 * The 'destination' address in this case is the address to which the
521 * peer sent the datagram, i.e. our local address. For multihomed
522 * hosts, this can change from msg to msg. Note that only the IP
523 * address changes, the port number should remain the same.
524 */
Chuck Lever7702ce42009-07-13 10:54:26 -0400525static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
526 struct cmsghdr *cmh)
Chuck Lever95756482007-02-12 00:53:38 -0800527{
Chuck Lever7702ce42009-07-13 10:54:26 -0400528 switch (cmh->cmsg_level) {
529 case SOL_IP:
530 return svc_udp_get_dest_address4(rqstp, cmh);
531 case SOL_IPV6:
532 return svc_udp_get_dest_address6(rqstp, cmh);
Chuck Lever95756482007-02-12 00:53:38 -0800533 }
Chuck Lever7702ce42009-07-13 10:54:26 -0400534
535 return 0;
Chuck Lever95756482007-02-12 00:53:38 -0800536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538/*
539 * Receive a datagram from a UDP socket.
540 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600541static int svc_udp_recvfrom(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600543 struct svc_sock *svsk =
544 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600545 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct sk_buff *skb;
David S. Millerbc375ea2007-04-12 13:35:59 -0700547 union {
548 struct cmsghdr hdr;
549 long all[SVC_PKTINFO_SPACE / sizeof(long)];
550 } buffer;
551 struct cmsghdr *cmh = &buffer.hdr;
NeilBrown7a37f572007-03-06 01:42:21 -0800552 struct msghdr msg = {
553 .msg_name = svc_addr(rqstp),
554 .msg_control = cmh,
555 .msg_controllen = sizeof(buffer),
556 .msg_flags = MSG_DONTWAIT,
557 };
Chuck Leverabc5c442009-04-23 19:31:25 -0400558 size_t len;
559 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Tom Tucker02fc6c32007-12-30 21:07:48 -0600561 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* udp sockets need large rcvbuf as all pending
563 * requests are still in that buffer. sndbuf must
564 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700565 * for one reply per thread. We count all threads
566 * rather than threads in a particular pool, which
567 * provides an upper bound on the number of threads
568 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
570 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700571 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
572 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Tom Tucker02fc6c32007-12-30 21:07:48 -0600574 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
NeilBrown05ed6902007-05-09 02:34:55 -0700575 skb = NULL;
576 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
577 0, 0, MSG_PEEK | MSG_DONTWAIT);
578 if (err >= 0)
579 skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err);
580
581 if (skb == NULL) {
582 if (err != -EAGAIN) {
583 /* possibly an icmp error */
584 dprintk("svc: recvfrom returned error %d\n", -err);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600585 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
NeilBrown05ed6902007-05-09 02:34:55 -0700587 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600589 len = svc_addr_len(svc_addr(rqstp));
Chuck Leverabc5c442009-04-23 19:31:25 -0400590 if (len == 0)
591 return -EAFNOSUPPORT;
Tom Tucker9dbc2402007-12-30 21:08:12 -0600592 rqstp->rq_addrlen = len;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700593 if (skb->tstamp.tv64 == 0) {
594 skb->tstamp = ktime_get_real();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800595 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 need that much accuracy */
597 }
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700598 svsk->sk_sk->sk_stamp = skb->tstamp;
Tom Tucker02fc6c32007-12-30 21:07:48 -0600599 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 len = skb->len - sizeof(struct udphdr);
602 rqstp->rq_arg.len = len;
603
Chuck Lever95756482007-02-12 00:53:38 -0800604 rqstp->rq_prot = IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Chuck Lever7702ce42009-07-13 10:54:26 -0400606 if (!svc_udp_get_dest_address(rqstp, cmh)) {
NeilBrown7a37f572007-03-06 01:42:21 -0800607 if (net_ratelimit())
Chuck Lever7702ce42009-07-13 10:54:26 -0400608 printk(KERN_WARNING
609 "svc: received unknown control message %d/%d; "
610 "dropping RPC reply datagram\n",
611 cmh->cmsg_level, cmh->cmsg_type);
Eric Dumazet9d410c72009-10-30 05:03:53 +0000612 skb_free_datagram_locked(svsk->sk_sk, skb);
NeilBrown7a37f572007-03-06 01:42:21 -0800613 return 0;
614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 if (skb_is_nonlinear(skb)) {
617 /* we have to copy */
618 local_bh_disable();
619 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
620 local_bh_enable();
621 /* checksum error */
Eric Dumazet9d410c72009-10-30 05:03:53 +0000622 skb_free_datagram_locked(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624 }
625 local_bh_enable();
Eric Dumazet9d410c72009-10-30 05:03:53 +0000626 skb_free_datagram_locked(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 } else {
628 /* we can use it in-place */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600629 rqstp->rq_arg.head[0].iov_base = skb->data +
630 sizeof(struct udphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 rqstp->rq_arg.head[0].iov_len = len;
Herbert Xufb286bb2005-11-10 13:01:24 -0800632 if (skb_checksum_complete(skb)) {
Eric Dumazet9d410c72009-10-30 05:03:53 +0000633 skb_free_datagram_locked(svsk->sk_sk, skb);
Herbert Xufb286bb2005-11-10 13:01:24 -0800634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
Tom Tucker5148bf42007-12-30 21:07:25 -0600636 rqstp->rq_xprt_ctxt = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638
639 rqstp->rq_arg.page_base = 0;
640 if (len <= rqstp->rq_arg.head[0].iov_len) {
641 rqstp->rq_arg.head[0].iov_len = len;
642 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700643 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 } else {
645 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700646 rqstp->rq_respages = rqstp->rq_pages + 1 +
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700647 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
650 if (serv->sv_stats)
651 serv->sv_stats->netudpcnt++;
652
653 return len;
654}
655
656static int
657svc_udp_sendto(struct svc_rqst *rqstp)
658{
659 int error;
660
661 error = svc_sendto(rqstp, &rqstp->rq_res);
662 if (error == -ECONNREFUSED)
663 /* ICMP error on earlier request. */
664 error = svc_sendto(rqstp, &rqstp->rq_res);
665
666 return error;
667}
668
Tom Tuckere831fe62007-12-30 21:07:29 -0600669static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
670{
671}
672
Tom Tucker323bee32007-12-30 21:07:31 -0600673static int svc_udp_has_wspace(struct svc_xprt *xprt)
674{
675 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600676 struct svc_serv *serv = xprt->xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -0600677 unsigned long required;
678
679 /*
680 * Set the SOCK_NOSPACE flag before checking the available
681 * sock space.
682 */
683 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Tom Tucker7a90e8c2007-12-30 21:07:55 -0600684 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
Tom Tucker323bee32007-12-30 21:07:31 -0600685 if (required*2 > sock_wspace(svsk->sk_sk))
686 return 0;
687 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
688 return 1;
689}
690
Tom Tucker38a417c2007-12-30 21:07:36 -0600691static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
692{
693 BUG();
694 return NULL;
695}
696
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600697static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400698 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600699 struct sockaddr *sa, int salen,
700 int flags)
701{
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400702 return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600703}
704
Tom Tucker360d8732007-12-30 21:07:17 -0600705static struct svc_xprt_ops svc_udp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600706 .xpo_create = svc_udp_create,
Tom Tucker5d137992007-12-30 21:07:23 -0600707 .xpo_recvfrom = svc_udp_recvfrom,
708 .xpo_sendto = svc_udp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -0600709 .xpo_release_rqst = svc_release_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -0600710 .xpo_detach = svc_sock_detach,
711 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -0600712 .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -0600713 .xpo_has_wspace = svc_udp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -0600714 .xpo_accept = svc_udp_accept,
Tom Tucker360d8732007-12-30 21:07:17 -0600715};
716
717static struct svc_xprt_class svc_udp_class = {
718 .xcl_name = "udp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600719 .xcl_owner = THIS_MODULE,
Tom Tucker360d8732007-12-30 21:07:17 -0600720 .xcl_ops = &svc_udp_ops,
Tom Tucker49023152007-12-30 21:07:21 -0600721 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
Tom Tucker360d8732007-12-30 21:07:17 -0600722};
723
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600724static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Chuck Lever7702ce42009-07-13 10:54:26 -0400726 int err, level, optname, one = 1;
NeilBrown7a37f572007-03-06 01:42:21 -0800727
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600728 svc_xprt_init(&svc_udp_class, &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600729 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
731 svsk->sk_sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800734 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * svc_udp_recvfrom will re-adjust if necessary
736 */
737 svc_sock_setbufsize(svsk->sk_sock,
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600738 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
739 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Tom Tucker0f0257e2007-12-30 21:08:27 -0600741 /* data might have come in before data_ready set up */
742 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600743 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800744
NeilBrown7a37f572007-03-06 01:42:21 -0800745 /* make sure we get destination address info */
Chuck Lever7702ce42009-07-13 10:54:26 -0400746 switch (svsk->sk_sk->sk_family) {
747 case AF_INET:
748 level = SOL_IP;
749 optname = IP_PKTINFO;
750 break;
751 case AF_INET6:
752 level = SOL_IPV6;
753 optname = IPV6_RECVPKTINFO;
754 break;
755 default:
756 BUG();
757 }
758 err = kernel_setsockopt(svsk->sk_sock, level, optname,
759 (char *)&one, sizeof(one));
760 dprintk("svc: kernel_setsockopt returned %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
763/*
764 * A data_ready event on a listening socket means there's a connection
765 * pending. Do not use state_change as a substitute for it.
766 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600767static void svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Neil Brown939bb7e2005-09-13 01:25:39 -0700769 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000770 wait_queue_head_t *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700773 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Neil Brown939bb7e2005-09-13 01:25:39 -0700775 /*
776 * This callback may called twice when a new connection
777 * is established as a child socket inherits everything
778 * from a parent LISTEN socket.
779 * 1) data_ready method of the parent socket will be called
780 * when one of child sockets become ESTABLISHED.
781 * 2) data_ready method of the child socket may be called
782 * when it receives data before the socket is accepted.
783 * In case of 2, we should ignore it silently.
784 */
785 if (sk->sk_state == TCP_LISTEN) {
786 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600787 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600788 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700789 } else
790 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
Neil Brown939bb7e2005-09-13 01:25:39 -0700792
Eric Dumazeteaefd112011-02-18 03:26:36 +0000793 wq = sk_sleep(sk);
794 if (wq && waitqueue_active(wq))
795 wake_up_interruptible_all(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
798/*
799 * A state change on a connected socket means it's dying or dead.
800 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600801static void svc_tcp_state_change(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Neil Brown939bb7e2005-09-13 01:25:39 -0700803 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000804 wait_queue_head_t *wq = sk_sleep(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700807 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Neil Brown939bb7e2005-09-13 01:25:39 -0700809 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700811 else {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600812 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600813 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
Eric Dumazeteaefd112011-02-18 03:26:36 +0000815 if (wq && waitqueue_active(wq))
816 wake_up_interruptible_all(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
Tom Tucker0f0257e2007-12-30 21:08:27 -0600819static void svc_tcp_data_ready(struct sock *sk, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Neil Brown939bb7e2005-09-13 01:25:39 -0700821 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000822 wait_queue_head_t *wq = sk_sleep(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700825 sk, sk->sk_user_data);
826 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600827 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600828 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700829 }
Eric Dumazeteaefd112011-02-18 03:26:36 +0000830 if (wq && waitqueue_active(wq))
831 wake_up_interruptible(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834/*
835 * Accept a TCP connection
836 */
Tom Tucker38a417c2007-12-30 21:07:36 -0600837static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Tom Tucker38a417c2007-12-30 21:07:36 -0600839 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800840 struct sockaddr_storage addr;
841 struct sockaddr *sin = (struct sockaddr *) &addr;
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600842 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 struct socket *sock = svsk->sk_sock;
844 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 struct svc_sock *newsvsk;
846 int err, slen;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +0300847 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
850 if (!sock)
Tom Tucker38a417c2007-12-30 21:07:36 -0600851 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Tom Tucker02fc6c32007-12-30 21:07:48 -0600853 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700854 err = kernel_accept(sock, &newsock, O_NONBLOCK);
855 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (err == -ENOMEM)
857 printk(KERN_WARNING "%s: no more sockets!\n",
858 serv->sv_name);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700859 else if (err != -EAGAIN && net_ratelimit())
860 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
861 serv->sv_name, -err);
Tom Tucker38a417c2007-12-30 21:07:36 -0600862 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Tom Tucker02fc6c32007-12-30 21:07:48 -0600864 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800866 err = kernel_getpeername(newsock, sin, &slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (err < 0) {
868 if (net_ratelimit())
869 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
870 serv->sv_name, -err);
871 goto failed; /* aborted connection or whatever */
872 }
873
874 /* Ideally, we would want to reject connections from unauthorized
Chuck Leverad06e4b2007-02-12 00:53:32 -0800875 * hosts here, but when we get encryption, the IP of the host won't
876 * tell us anything. For now just warn about unpriv connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 */
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800878 if (!svc_port_is_privileged(sin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 dprintk(KERN_WARNING
Chuck Leverad06e4b2007-02-12 00:53:32 -0800880 "%s: connect from unprivileged port: %s\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800881 serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800882 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
Chuck Leverad06e4b2007-02-12 00:53:32 -0800884 dprintk("%s: connect from %s\n", serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800885 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /* make sure that a write doesn't block forever when
888 * low on memory
889 */
890 newsock->sk->sk_sndtimeo = HZ*30;
891
Chuck Lever6b174332007-02-12 00:53:28 -0800892 if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
893 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 goto failed;
Tom Tucker9dbc2402007-12-30 21:08:12 -0600895 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
Frank van Maarseveena9747692007-07-09 22:21:39 +0200896 err = kernel_getsockname(newsock, sin, &slen);
897 if (unlikely(err < 0)) {
898 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
899 slen = offsetof(struct sockaddr, sa_data);
900 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600901 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -0800902
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600903 if (serv->sv_stats)
904 serv->sv_stats->nettcpconn++;
905
906 return &newsvsk->sk_xprt;
907
908failed:
909 sock_release(newsock);
910 return NULL;
911}
912
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800913static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
914{
915 unsigned int i, len, npages;
916
917 if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
918 return 0;
919 len = svsk->sk_tcplen - sizeof(rpc_fraghdr);
920 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
921 for (i = 0; i < npages; i++) {
922 if (rqstp->rq_pages[i] != NULL)
923 put_page(rqstp->rq_pages[i]);
924 BUG_ON(svsk->sk_pages[i] == NULL);
925 rqstp->rq_pages[i] = svsk->sk_pages[i];
926 svsk->sk_pages[i] = NULL;
927 }
928 rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
929 return len;
930}
931
932static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
933{
934 unsigned int i, len, npages;
935
936 if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
937 return;
938 len = svsk->sk_tcplen - sizeof(rpc_fraghdr);
939 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
940 for (i = 0; i < npages; i++) {
941 svsk->sk_pages[i] = rqstp->rq_pages[i];
942 rqstp->rq_pages[i] = NULL;
943 }
944}
945
946static void svc_tcp_clear_pages(struct svc_sock *svsk)
947{
948 unsigned int i, len, npages;
949
950 if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
951 goto out;
952 len = svsk->sk_tcplen - sizeof(rpc_fraghdr);
953 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
954 for (i = 0; i < npages; i++) {
955 BUG_ON(svsk->sk_pages[i] == NULL);
956 put_page(svsk->sk_pages[i]);
957 svsk->sk_pages[i] = NULL;
958 }
959out:
960 svsk->sk_tcplen = 0;
961}
962
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600963/*
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300964 * Receive data.
965 * If we haven't gotten the record length yet, get the next four bytes.
966 * Otherwise try to gobble up as much as possible up to the complete
967 * record length.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 */
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300969static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600971 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400972 unsigned int want;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300973 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Tom Tucker02fc6c32007-12-30 21:07:48 -0600975 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Chuck Leverc0401ea2008-04-14 12:27:30 -0400977 if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 struct kvec iov;
979
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400980 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
982 iov.iov_len = want;
983 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
984 goto error;
985 svsk->sk_tcplen += len;
986
987 if (len < want) {
Chuck Leverc0401ea2008-04-14 12:27:30 -0400988 dprintk("svc: short recvfrom while reading record "
989 "length (%d of %d)\n", len, want);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800990 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992
993 svsk->sk_reclen = ntohl(svsk->sk_reclen);
Chuck Leverc0401ea2008-04-14 12:27:30 -0400994 if (!(svsk->sk_reclen & RPC_LAST_STREAM_FRAGMENT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /* FIXME: technically, a record can be fragmented,
996 * and non-terminal fragments will not have the top
997 * bit set in the fragment length header.
998 * But apparently no known nfs clients send fragmented
999 * records. */
NeilBrown34e9a632007-01-29 13:19:52 -08001000 if (net_ratelimit())
Chuck Leverc0401ea2008-04-14 12:27:30 -04001001 printk(KERN_NOTICE "RPC: multiple fragments "
1002 "per record not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 goto err_delete;
1004 }
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001005
Chuck Leverc0401ea2008-04-14 12:27:30 -04001006 svsk->sk_reclen &= RPC_FRAGMENT_SIZE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001008 if (svsk->sk_reclen > serv->sv_max_mesg) {
NeilBrown34e9a632007-01-29 13:19:52 -08001009 if (net_ratelimit())
Chuck Leverc0401ea2008-04-14 12:27:30 -04001010 printk(KERN_NOTICE "RPC: "
1011 "fragment too large: 0x%08lx\n",
1012 (unsigned long)svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 goto err_delete;
1014 }
1015 }
1016
J. Bruce Fieldscc6c2122011-02-14 15:03:35 -05001017 if (svsk->sk_reclen < 8)
1018 goto err_delete; /* client is nuts. */
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 len = svsk->sk_reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001022 return len;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001023error:
1024 dprintk("RPC: TCP recv_record got %d\n", len);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001025 return len;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001026err_delete:
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001027 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001028 return -EAGAIN;
1029}
1030
Trond Myklebust586c52c2009-05-18 17:47:56 -04001031static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001032{
Trond Myklebust586c52c2009-05-18 17:47:56 -04001033 struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001034 struct rpc_rqst *req = NULL;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001035 struct kvec *src, *dst;
1036 __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
J. Bruce Fields48e65552011-02-14 14:52:03 -05001037 __be32 xid;
1038 __be32 calldir;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001039
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001040 xid = *p++;
1041 calldir = *p;
1042
Trond Myklebust586c52c2009-05-18 17:47:56 -04001043 if (bc_xprt)
1044 req = xprt_lookup_rqst(bc_xprt, xid);
J. Bruce Fieldsd75faea2010-11-30 19:15:01 -05001045
Trond Myklebust586c52c2009-05-18 17:47:56 -04001046 if (!req) {
1047 printk(KERN_NOTICE
1048 "%s: Got unrecognized reply: "
1049 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
1050 __func__, ntohl(calldir),
1051 bc_xprt, xid);
1052 return -EAGAIN;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001053 }
Trond Myklebust586c52c2009-05-18 17:47:56 -04001054
1055 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
1056 /*
1057 * XXX!: cheating for now! Only copying HEAD.
1058 * But we know this is good enough for now (in fact, for any
1059 * callback reply in the forseeable future).
1060 */
1061 dst = &req->rq_private_buf.head[0];
1062 src = &rqstp->rq_arg.head[0];
1063 if (dst->iov_len < src->iov_len)
1064 return -EAGAIN; /* whatever; just giving up. */
1065 memcpy(dst->iov_base, src->iov_base, src->iov_len);
1066 xprt_complete_rqst(req->rq_task, svsk->sk_reclen);
1067 rqstp->rq_arg.len = 0;
1068 return 0;
1069}
1070
1071static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len)
1072{
1073 int i = 0;
1074 int t = 0;
1075
1076 while (t < len) {
1077 vec[i].iov_base = page_address(pages[i]);
1078 vec[i].iov_len = PAGE_SIZE;
1079 i++;
1080 t += PAGE_SIZE;
1081 }
1082 return i;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001083}
1084
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001085
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001086/*
1087 * Receive data from a TCP socket.
1088 */
1089static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
1090{
1091 struct svc_sock *svsk =
1092 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
1093 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1094 int len;
1095 struct kvec *vec;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001096 unsigned int want, base;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001097 __be32 *p;
1098 __be32 calldir;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001099 int pnum;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001100
1101 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1102 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
1103 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
1104 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
1105
1106 len = svc_tcp_recv_record(svsk, rqstp);
1107 if (len < 0)
1108 goto error;
1109
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001110 base = svc_tcp_restore_pages(svsk, rqstp);
1111 want = svsk->sk_reclen - base;
1112
NeilBrown3cc03b12006-10-04 02:15:47 -07001113 vec = rqstp->rq_vec;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001114
Trond Myklebust586c52c2009-05-18 17:47:56 -04001115 pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0],
1116 svsk->sk_reclen);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001117
NeilBrown44524352006-10-04 02:15:46 -07001118 rqstp->rq_respages = &rqstp->rq_pages[pnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 /* Now receive data */
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001121 len = svc_partial_recvfrom(rqstp, vec, pnum, want, base);
1122 if (len >= 0)
1123 svsk->sk_tcplen += len;
1124 if (len != want) {
1125 if (len < 0 && len != -EAGAIN)
1126 goto err_other;
1127 svc_tcp_save_pages(svsk, rqstp);
1128 dprintk("svc: incomplete TCP record (%d of %d)\n",
1129 svsk->sk_tcplen, svsk->sk_reclen);
1130 goto err_noclose;
1131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001133 rqstp->rq_arg.len = svsk->sk_reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 rqstp->rq_arg.page_base = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001135 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1136 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 rqstp->rq_arg.page_len = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001138 } else
1139 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Tom Tucker5148bf42007-12-30 21:07:25 -06001141 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 rqstp->rq_prot = IPPROTO_TCP;
1143
Trond Myklebust586c52c2009-05-18 17:47:56 -04001144 p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1145 calldir = p[1];
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001146 if (calldir)
Trond Myklebust586c52c2009-05-18 17:47:56 -04001147 len = receive_cb_reply(svsk, rqstp);
Trond Myklebust586c52c2009-05-18 17:47:56 -04001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /* Reset TCP read info */
1150 svsk->sk_reclen = 0;
1151 svsk->sk_tcplen = 0;
Trond Myklebust0601f792009-05-18 17:47:56 -04001152 /* If we have more data, signal svc_xprt_enqueue() to try again */
1153 if (svc_recv_available(svsk) > sizeof(rpc_fraghdr))
1154 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1155
J. Bruce Fields8985ef02011-04-09 10:03:10 -04001156 if (len < 0)
1157 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Tom Tucker9dbc2402007-12-30 21:08:12 -06001159 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (serv->sv_stats)
1161 serv->sv_stats->nettcpcnt++;
1162
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001163 dprintk("svc: TCP complete record (%d bytes)\n", rqstp->rq_arg.len);
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001164 return rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001166error:
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001167 if (len != -EAGAIN)
1168 goto err_other;
1169 dprintk("RPC: TCP recvfrom got EAGAIN\n");
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001170 return -EAGAIN;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001171err_other:
1172 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1173 svsk->sk_xprt.xpt_server->sv_name, -len);
1174 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1175err_noclose:
1176 return -EAGAIN; /* record not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
1179/*
1180 * Send out data on TCP socket.
1181 */
Tom Tucker0f0257e2007-12-30 21:08:27 -06001182static int svc_tcp_sendto(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 struct xdr_buf *xbufp = &rqstp->rq_res;
1185 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001186 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 /* Set up the first element of the reply kvec.
1189 * Any other kvecs that may be in use have been taken
1190 * care of by the server implementation itself.
1191 */
1192 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1193 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 sent = svc_sendto(rqstp, &rqstp->rq_res);
1196 if (sent != xbufp->len) {
Tom Tucker0f0257e2007-12-30 21:08:27 -06001197 printk(KERN_NOTICE
1198 "rpc-srv/tcp: %s: %s %d when sending %d bytes "
1199 "- shutting down socket\n",
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001200 rqstp->rq_xprt->xpt_server->sv_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 (sent<0)?"got error":"sent only",
1202 sent, xbufp->len);
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001203 set_bit(XPT_CLOSE, &rqstp->rq_xprt->xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -06001204 svc_xprt_enqueue(rqstp->rq_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 sent = -EAGAIN;
1206 }
1207 return sent;
1208}
1209
Tom Tuckere831fe62007-12-30 21:07:29 -06001210/*
1211 * Setup response header. TCP has a 4B record length field.
1212 */
1213static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
1214{
1215 struct kvec *resv = &rqstp->rq_res.head[0];
1216
1217 /* tcp needs a space for the record length... */
1218 svc_putnl(resv, 0);
1219}
1220
Tom Tucker323bee32007-12-30 21:07:31 -06001221static int svc_tcp_has_wspace(struct svc_xprt *xprt)
1222{
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001223 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Trond Myklebust47fcb032009-05-18 17:47:56 -04001224 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -06001225 int required;
Tom Tucker323bee32007-12-30 21:07:31 -06001226
Trond Myklebust47fcb032009-05-18 17:47:56 -04001227 if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
1228 return 1;
1229 required = atomic_read(&xprt->xpt_reserved) + serv->sv_max_mesg;
1230 if (sk_stream_wspace(svsk->sk_sk) >= required)
1231 return 1;
Tom Tucker323bee32007-12-30 21:07:31 -06001232 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Trond Myklebust47fcb032009-05-18 17:47:56 -04001233 return 0;
Tom Tucker323bee32007-12-30 21:07:31 -06001234}
1235
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001236static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001237 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001238 struct sockaddr *sa, int salen,
1239 int flags)
1240{
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001241 return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001242}
1243
Andy Adamson1f11a032011-01-06 02:04:26 +00001244#if defined(CONFIG_NFS_V4_1)
1245static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
1246 struct net *, struct sockaddr *,
1247 int, int);
1248static void svc_bc_sock_free(struct svc_xprt *xprt);
1249
1250static struct svc_xprt *svc_bc_tcp_create(struct svc_serv *serv,
1251 struct net *net,
1252 struct sockaddr *sa, int salen,
1253 int flags)
1254{
1255 return svc_bc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1256}
1257
1258static void svc_bc_tcp_sock_detach(struct svc_xprt *xprt)
1259{
1260}
1261
1262static struct svc_xprt_ops svc_tcp_bc_ops = {
1263 .xpo_create = svc_bc_tcp_create,
1264 .xpo_detach = svc_bc_tcp_sock_detach,
1265 .xpo_free = svc_bc_sock_free,
1266 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
1267};
1268
1269static struct svc_xprt_class svc_tcp_bc_class = {
1270 .xcl_name = "tcp-bc",
1271 .xcl_owner = THIS_MODULE,
1272 .xcl_ops = &svc_tcp_bc_ops,
1273 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1274};
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001275
1276static void svc_init_bc_xprt_sock(void)
1277{
1278 svc_reg_xprt_class(&svc_tcp_bc_class);
1279}
1280
1281static void svc_cleanup_bc_xprt_sock(void)
1282{
1283 svc_unreg_xprt_class(&svc_tcp_bc_class);
1284}
1285#else /* CONFIG_NFS_V4_1 */
1286static void svc_init_bc_xprt_sock(void)
1287{
1288}
1289
1290static void svc_cleanup_bc_xprt_sock(void)
1291{
1292}
Andy Adamson1f11a032011-01-06 02:04:26 +00001293#endif /* CONFIG_NFS_V4_1 */
1294
Tom Tucker360d8732007-12-30 21:07:17 -06001295static struct svc_xprt_ops svc_tcp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001296 .xpo_create = svc_tcp_create,
Tom Tucker5d137992007-12-30 21:07:23 -06001297 .xpo_recvfrom = svc_tcp_recvfrom,
1298 .xpo_sendto = svc_tcp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -06001299 .xpo_release_rqst = svc_release_skb,
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001300 .xpo_detach = svc_tcp_sock_detach,
Tom Tucker755ccea2007-12-30 21:07:27 -06001301 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -06001302 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -06001303 .xpo_has_wspace = svc_tcp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -06001304 .xpo_accept = svc_tcp_accept,
Tom Tucker360d8732007-12-30 21:07:17 -06001305};
1306
1307static struct svc_xprt_class svc_tcp_class = {
1308 .xcl_name = "tcp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001309 .xcl_owner = THIS_MODULE,
Tom Tucker360d8732007-12-30 21:07:17 -06001310 .xcl_ops = &svc_tcp_ops,
Tom Tucker49023152007-12-30 21:07:21 -06001311 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
Tom Tucker360d8732007-12-30 21:07:17 -06001312};
1313
1314void svc_init_xprt_sock(void)
1315{
1316 svc_reg_xprt_class(&svc_tcp_class);
1317 svc_reg_xprt_class(&svc_udp_class);
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001318 svc_init_bc_xprt_sock();
Tom Tucker360d8732007-12-30 21:07:17 -06001319}
1320
1321void svc_cleanup_xprt_sock(void)
1322{
1323 svc_unreg_xprt_class(&svc_tcp_class);
1324 svc_unreg_xprt_class(&svc_udp_class);
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001325 svc_cleanup_bc_xprt_sock();
Tom Tucker360d8732007-12-30 21:07:17 -06001326}
1327
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001328static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 struct sock *sk = svsk->sk_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001332 svc_xprt_init(&svc_tcp_class, &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -06001333 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (sk->sk_state == TCP_LISTEN) {
1335 dprintk("setting up TCP socket for listening\n");
Tom Tucker02fc6c32007-12-30 21:07:48 -06001336 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 sk->sk_data_ready = svc_tcp_listen_data_ready;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001338 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 } else {
1340 dprintk("setting up TCP socket for reading\n");
1341 sk->sk_state_change = svc_tcp_state_change;
1342 sk->sk_data_ready = svc_tcp_data_ready;
Trond Myklebust47fcb032009-05-18 17:47:56 -04001343 sk->sk_write_space = svc_tcp_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 svsk->sk_reclen = 0;
1346 svsk->sk_tcplen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001347 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Chuck Leverb7872fe2008-04-14 12:27:01 -04001349 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Tom Tucker02fc6c32007-12-30 21:07:48 -06001351 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001352 if (sk->sk_state != TCP_ESTABLISHED)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001353 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355}
1356
Tom Tucker0f0257e2007-12-30 21:08:27 -06001357void svc_sock_update_bufs(struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
1359 /*
1360 * The number of server threads has changed. Update
1361 * rcvbuf and sndbuf accordingly on all sockets
1362 */
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001363 struct svc_sock *svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 spin_lock_bh(&serv->sv_lock);
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001366 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001367 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001368 list_for_each_entry(svsk, &serv->sv_tempsocks, sk_xprt.xpt_list)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001369 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 spin_unlock_bh(&serv->sv_lock);
1371}
Trond Myklebust24c37672008-12-23 16:30:12 -05001372EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 * Initialize socket for RPC use and create svc_sock struct
1376 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1377 */
Chuck Lever6b174332007-02-12 00:53:28 -08001378static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1379 struct socket *sock,
1380 int *errp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 struct svc_sock *svsk;
1383 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001384 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 dprintk("svc: svc_setup_socket %p\n", sock);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001387 if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 *errp = -ENOMEM;
1389 return NULL;
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 inet = sock->sk;
1393
1394 /* Register socket with portmapper */
1395 if (*errp >= 0 && pmap_register)
Chuck Leverbaf01ca2009-03-18 20:46:13 -04001396 *errp = svc_register(serv, inet->sk_family, inet->sk_protocol,
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001397 ntohs(inet_sk(inet)->inet_sport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 if (*errp < 0) {
1400 kfree(svsk);
1401 return NULL;
1402 }
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 inet->sk_user_data = svsk;
1405 svsk->sk_sock = sock;
1406 svsk->sk_sk = inet;
1407 svsk->sk_ostate = inet->sk_state_change;
1408 svsk->sk_odata = inet->sk_data_ready;
1409 svsk->sk_owspace = inet->sk_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 /* Initialize the socket */
1412 if (sock->type == SOCK_DGRAM)
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001413 svc_udp_init(svsk, serv);
Olga Kornievskaia96604392008-10-21 14:13:47 -04001414 else {
1415 /* initialise setting must have enough space to
1416 * receive and respond to one request.
1417 */
1418 svc_sock_setbufsize(svsk->sk_sock, 4 * serv->sv_max_mesg,
1419 4 * serv->sv_max_mesg);
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001420 svc_tcp_init(svsk, serv);
Olga Kornievskaia96604392008-10-21 14:13:47 -04001421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1424 svsk, svsk->sk_sk);
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return svsk;
1427}
1428
Chuck Leverbfba9ab2009-04-23 19:32:33 -04001429/**
1430 * svc_addsock - add a listener socket to an RPC service
1431 * @serv: pointer to RPC service to which to add a new listener
1432 * @fd: file descriptor of the new listener
1433 * @name_return: pointer to buffer to fill in with name of listener
1434 * @len: size of the buffer
1435 *
1436 * Fills in socket name and returns positive length of name if successful.
1437 * Name is terminated with '\n'. On error, returns a negative errno
1438 * value.
1439 */
1440int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
1441 const size_t len)
NeilBrownb41b66d2006-10-02 02:17:48 -07001442{
1443 int err = 0;
1444 struct socket *so = sockfd_lookup(fd, &err);
1445 struct svc_sock *svsk = NULL;
1446
1447 if (!so)
1448 return err;
Aime Le Rouzic205ba422010-01-26 14:03:56 -05001449 if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
NeilBrownb41b66d2006-10-02 02:17:48 -07001450 err = -EAFNOSUPPORT;
1451 else if (so->sk->sk_protocol != IPPROTO_TCP &&
1452 so->sk->sk_protocol != IPPROTO_UDP)
1453 err = -EPROTONOSUPPORT;
1454 else if (so->state > SS_UNCONNECTED)
1455 err = -EISCONN;
1456 else {
Tom Tucker2da2c212008-11-23 09:58:08 -06001457 if (!try_module_get(THIS_MODULE))
1458 err = -ENOENT;
1459 else
1460 svsk = svc_setup_socket(serv, so, &err,
1461 SVC_SOCK_DEFAULTS);
NeilBrowne79eff12007-02-12 00:53:30 -08001462 if (svsk) {
Tom Tucker9dbc2402007-12-30 21:08:12 -06001463 struct sockaddr_storage addr;
1464 struct sockaddr *sin = (struct sockaddr *)&addr;
1465 int salen;
1466 if (kernel_getsockname(svsk->sk_sock, sin, &salen) == 0)
1467 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
Tom Tucker4e5caaa2007-12-30 21:08:20 -06001468 clear_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags);
1469 spin_lock_bh(&serv->sv_lock);
1470 list_add(&svsk->sk_xprt.xpt_list, &serv->sv_permsocks);
1471 spin_unlock_bh(&serv->sv_lock);
Tom Tuckera6046f72007-12-30 21:08:01 -06001472 svc_xprt_received(&svsk->sk_xprt);
NeilBrownb41b66d2006-10-02 02:17:48 -07001473 err = 0;
Tom Tucker2da2c212008-11-23 09:58:08 -06001474 } else
1475 module_put(THIS_MODULE);
NeilBrownb41b66d2006-10-02 02:17:48 -07001476 }
1477 if (err) {
1478 sockfd_put(so);
1479 return err;
1480 }
Chuck Levere7942b92009-04-23 19:32:48 -04001481 return svc_one_sock_name(svsk, name_return, len);
NeilBrownb41b66d2006-10-02 02:17:48 -07001482}
1483EXPORT_SYMBOL_GPL(svc_addsock);
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485/*
1486 * Create socket for RPC service.
1487 */
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001488static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1489 int protocol,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001490 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001491 struct sockaddr *sin, int len,
1492 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 struct svc_sock *svsk;
1495 struct socket *sock;
1496 int error;
1497 int type;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001498 struct sockaddr_storage addr;
1499 struct sockaddr *newsin = (struct sockaddr *)&addr;
1500 int newlen;
Trond Myklebustc69da772009-03-30 18:59:17 -04001501 int family;
1502 int val;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +03001503 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Chuck Leverad06e4b2007-02-12 00:53:32 -08001505 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1506 serv->sv_program->pg_name, protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001507 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1510 printk(KERN_WARNING "svc: only UDP and TCP "
1511 "sockets supported\n");
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001512 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Trond Myklebustc69da772009-03-30 18:59:17 -04001515 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1516 switch (sin->sa_family) {
1517 case AF_INET6:
1518 family = PF_INET6;
1519 break;
1520 case AF_INET:
1521 family = PF_INET;
1522 break;
1523 default:
1524 return ERR_PTR(-EINVAL);
1525 }
1526
Pavel Emelyanov14ec63c2010-09-29 16:06:57 +04001527 error = __sock_create(net, family, type, protocol, &sock, 1);
Chuck Lever77f1f672007-02-12 00:53:39 -08001528 if (error < 0)
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001529 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Peter Zijlstraed075362006-12-06 20:35:24 -08001531 svc_reclassify_socket(sock);
1532
Trond Myklebustc69da772009-03-30 18:59:17 -04001533 /*
1534 * If this is an PF_INET6 listener, we want to avoid
1535 * getting requests from IPv4 remotes. Those should
1536 * be shunted to a PF_INET listener via rpcbind.
1537 */
1538 val = 1;
1539 if (family == PF_INET6)
1540 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
1541 (char *)&val, sizeof(val));
1542
Eric Sesterhenn18114742006-09-28 14:37:07 -07001543 if (type == SOCK_STREAM)
Chuck Lever77f1f672007-02-12 00:53:39 -08001544 sock->sk->sk_reuse = 1; /* allow address reuse */
1545 error = kernel_bind(sock, sin, len);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001546 if (error < 0)
1547 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Tom Tucker9dbc2402007-12-30 21:08:12 -06001549 newlen = len;
1550 error = kernel_getsockname(sock, newsin, &newlen);
1551 if (error < 0)
1552 goto bummer;
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001555 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 goto bummer;
1557 }
1558
NeilBrowne79eff12007-02-12 00:53:30 -08001559 if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
Tom Tucker9dbc2402007-12-30 21:08:12 -06001560 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001561 return (struct svc_xprt *)svsk;
NeilBrowne79eff12007-02-12 00:53:30 -08001562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564bummer:
1565 dprintk("svc: svc_create_socket error = %d\n", -error);
1566 sock_release(sock);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001567 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
1570/*
Tom Tucker755ccea2007-12-30 21:07:27 -06001571 * Detach the svc_sock from the socket so that no
1572 * more callbacks occur.
1573 */
1574static void svc_sock_detach(struct svc_xprt *xprt)
1575{
1576 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1577 struct sock *sk = svsk->sk_sk;
Eric Dumazeteaefd112011-02-18 03:26:36 +00001578 wait_queue_head_t *wq;
Tom Tucker755ccea2007-12-30 21:07:27 -06001579
1580 dprintk("svc: svc_sock_detach(%p)\n", svsk);
1581
1582 /* put back the old socket callbacks */
1583 sk->sk_state_change = svsk->sk_ostate;
1584 sk->sk_data_ready = svsk->sk_odata;
1585 sk->sk_write_space = svsk->sk_owspace;
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001586
Eric Dumazeteaefd112011-02-18 03:26:36 +00001587 wq = sk_sleep(sk);
1588 if (wq && waitqueue_active(wq))
1589 wake_up_interruptible(wq);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001590}
1591
1592/*
1593 * Disconnect the socket, and reset the callbacks
1594 */
1595static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1596{
1597 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1598
1599 dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk);
1600
1601 svc_sock_detach(xprt);
1602
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001603 if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1604 svc_tcp_clear_pages(svsk);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001605 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001606 }
Tom Tucker755ccea2007-12-30 21:07:27 -06001607}
1608
1609/*
1610 * Free the svc_sock's socket resources and the svc_sock itself.
1611 */
1612static void svc_sock_free(struct svc_xprt *xprt)
1613{
1614 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1615 dprintk("svc: svc_sock_free(%p)\n", svsk);
1616
Tom Tucker755ccea2007-12-30 21:07:27 -06001617 if (svsk->sk_sock->file)
1618 sockfd_put(svsk->sk_sock);
1619 else
1620 sock_release(svsk->sk_sock);
1621 kfree(svsk);
1622}
Benny Halevy7652e5a2009-04-01 09:23:09 -04001623
Andy Adamson1f11a032011-01-06 02:04:26 +00001624#if defined(CONFIG_NFS_V4_1)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001625/*
Andy Adamson1f11a032011-01-06 02:04:26 +00001626 * Create a back channel svc_xprt which shares the fore channel socket.
Benny Halevy7652e5a2009-04-01 09:23:09 -04001627 */
Andy Adamson1f11a032011-01-06 02:04:26 +00001628static struct svc_xprt *svc_bc_create_socket(struct svc_serv *serv,
1629 int protocol,
1630 struct net *net,
1631 struct sockaddr *sin, int len,
1632 int flags)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001633{
1634 struct svc_sock *svsk;
Andy Adamson1f11a032011-01-06 02:04:26 +00001635 struct svc_xprt *xprt;
Benny Halevy7652e5a2009-04-01 09:23:09 -04001636
Andy Adamson1f11a032011-01-06 02:04:26 +00001637 if (protocol != IPPROTO_TCP) {
1638 printk(KERN_WARNING "svc: only TCP sockets"
1639 " supported on shared back channel\n");
1640 return ERR_PTR(-EINVAL);
1641 }
1642
Benny Halevy7652e5a2009-04-01 09:23:09 -04001643 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1644 if (!svsk)
Andy Adamson1f11a032011-01-06 02:04:26 +00001645 return ERR_PTR(-ENOMEM);
Benny Halevy7652e5a2009-04-01 09:23:09 -04001646
1647 xprt = &svsk->sk_xprt;
Andy Adamson1f11a032011-01-06 02:04:26 +00001648 svc_xprt_init(&svc_tcp_bc_class, xprt, serv);
1649
Andy Adamson4a19de02011-01-06 02:04:35 +00001650 serv->sv_bc_xprt = xprt;
Andy Adamson1f11a032011-01-06 02:04:26 +00001651
Benny Halevy7652e5a2009-04-01 09:23:09 -04001652 return xprt;
1653}
Benny Halevy7652e5a2009-04-01 09:23:09 -04001654
1655/*
Andy Adamson1f11a032011-01-06 02:04:26 +00001656 * Free a back channel svc_sock.
Benny Halevy7652e5a2009-04-01 09:23:09 -04001657 */
Andy Adamson1f11a032011-01-06 02:04:26 +00001658static void svc_bc_sock_free(struct svc_xprt *xprt)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001659{
Andy Adamson778be232011-01-25 15:38:01 +00001660 if (xprt)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001661 kfree(container_of(xprt, struct svc_sock, sk_xprt));
1662}
Andy Adamson1f11a032011-01-06 02:04:26 +00001663#endif /* CONFIG_NFS_V4_1 */