blob: 0ef47dff89be00e85f6c0c97945f9482d4569738 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/callback.c
3 *
4 * Copyright (C) 2004 Trond Myklebust
5 *
6 * NFSv4 callback handling
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/completion.h>
10#include <linux/ip.h>
11#include <linux/module.h>
12#include <linux/smp_lock.h>
13#include <linux/sunrpc/svc.h>
14#include <linux/sunrpc/svcsock.h>
15#include <linux/nfs_fs.h>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080016#include <linux/mutex.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070017#include <linux/freezer.h>
Jeff Laytona277e332008-02-20 08:55:30 -050018#include <linux/kthread.h>
Olga Kornievskaia945b34a2008-12-23 16:18:34 -050019#include <linux/sunrpc/svcauth_gss.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020020
21#include <net/inet_sock.h>
22
Trond Myklebust4ce79712005-06-22 17:16:21 +000023#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "callback.h"
David Howells24c8dbb2006-08-22 20:06:10 -040025#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define NFSDBG_FACILITY NFSDBG_CALLBACK
28
29struct nfs_callback_data {
30 unsigned int users;
Jeff Layton5afc5972008-06-11 10:03:11 -040031 struct svc_rqst *rqst;
Jeff Laytona277e332008-02-20 08:55:30 -050032 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
35static struct nfs_callback_data nfs_callback_info;
Ingo Molnar353ab6e2006-03-26 01:37:12 -080036static DEFINE_MUTEX(nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static struct svc_program nfs4_callback_program;
38
Trond Myklebusta72b4422006-01-03 09:55:41 +010039unsigned int nfs_callback_set_tcpport;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040unsigned short nfs_callback_tcpport;
David Howells7d4e2742006-08-22 20:06:07 -040041static const int nfs_set_port_min = 0;
42static const int nfs_set_port_max = 65535;
43
44static int param_set_port(const char *val, struct kernel_param *kp)
45{
46 char *endp;
47 int num = simple_strtol(val, &endp, 0);
48 if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
49 return -EINVAL;
50 *((int *)kp->arg) = num;
51 return 0;
52}
53
54module_param_call(callback_tcpport, param_set_port, param_get_int,
55 &nfs_callback_set_tcpport, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
58 * This is the callback kernel thread.
59 */
Jeff Laytona277e332008-02-20 08:55:30 -050060static int
61nfs_callback_svc(void *vrqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Jeff Layton06e02d62008-04-08 15:40:07 -040063 int err, preverr = 0;
Jeff Laytona277e332008-02-20 08:55:30 -050064 struct svc_rqst *rqstp = vrqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Rafael J. Wysocki83144182007-07-17 04:03:35 -070066 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Jeff Laytona277e332008-02-20 08:55:30 -050068 /*
69 * FIXME: do we really need to run this under the BKL? If so, please
70 * add a comment about what it's intended to protect.
71 */
72 lock_kernel();
73 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 /*
75 * Listen for a request on the socket
76 */
NeilBrown6fb2b472006-10-02 02:17:50 -070077 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
Jeff Layton06e02d62008-04-08 15:40:07 -040078 if (err == -EAGAIN || err == -EINTR) {
79 preverr = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
Jeff Layton06e02d62008-04-08 15:40:07 -040082 if (err < 0) {
83 if (err != preverr) {
84 printk(KERN_WARNING "%s: unexpected error "
85 "from svc_recv (%d)\n", __func__, err);
86 preverr = err;
87 }
88 schedule_timeout_uninterruptible(HZ);
89 continue;
90 }
91 preverr = err;
NeilBrown6fb2b472006-10-02 02:17:50 -070092 svc_process(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 unlock_kernel();
Jeff Laytona277e332008-02-20 08:55:30 -050095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98/*
Jeff Layton5afc5972008-06-11 10:03:11 -040099 * Bring up the callback thread if it is not already up.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
101int nfs_callback_up(void)
102{
Jeff Layton8e600292008-02-11 10:00:20 -0500103 struct svc_serv *serv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 int ret = 0;
105
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800106 mutex_lock(&nfs_callback_mutex);
Jeff Laytona277e332008-02-20 08:55:30 -0500107 if (nfs_callback_info.users++ || nfs_callback_info.task != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 goto out;
Chuck Lever49a90722009-03-18 20:46:29 -0400109 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 ret = -ENOMEM;
111 if (!serv)
112 goto out_err;
Chuck Lever482fb942007-02-12 00:53:29 -0800113
Chuck Lever26298ca2009-03-18 20:46:36 -0400114 ret = svc_create_xprt(serv, "tcp", PF_INET,
Chuck Lever9652ada2009-03-18 20:46:21 -0400115 nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS);
Chuck Lever482fb942007-02-12 00:53:29 -0800116 if (ret <= 0)
Jeff Layton8e600292008-02-11 10:00:20 -0500117 goto out_err;
Chuck Lever482fb942007-02-12 00:53:29 -0800118 nfs_callback_tcpport = ret;
Chuck Lever18de9732008-10-16 17:41:11 -0400119 dprintk("NFS: Callback listener port = %u (af %u)\n",
Chuck Lever26298ca2009-03-18 20:46:36 -0400120 nfs_callback_tcpport, PF_INET);
Chuck Lever482fb942007-02-12 00:53:29 -0800121
Jeff Layton5afc5972008-06-11 10:03:11 -0400122 nfs_callback_info.rqst = svc_prepare_thread(serv, &serv->sv_pools[0]);
123 if (IS_ERR(nfs_callback_info.rqst)) {
124 ret = PTR_ERR(nfs_callback_info.rqst);
125 nfs_callback_info.rqst = NULL;
Jeff Layton8e600292008-02-11 10:00:20 -0500126 goto out_err;
Jeff Laytona277e332008-02-20 08:55:30 -0500127 }
128
129 svc_sock_update_bufs(serv);
Jeff Laytona277e332008-02-20 08:55:30 -0500130
Jeff Layton5afc5972008-06-11 10:03:11 -0400131 nfs_callback_info.task = kthread_run(nfs_callback_svc,
132 nfs_callback_info.rqst,
Jeff Laytona277e332008-02-20 08:55:30 -0500133 "nfsv4-svc");
134 if (IS_ERR(nfs_callback_info.task)) {
135 ret = PTR_ERR(nfs_callback_info.task);
Jeff Layton5afc5972008-06-11 10:03:11 -0400136 svc_exit_thread(nfs_callback_info.rqst);
137 nfs_callback_info.rqst = NULL;
Jeff Laytona277e332008-02-20 08:55:30 -0500138 nfs_callback_info.task = NULL;
Jeff Laytona277e332008-02-20 08:55:30 -0500139 goto out_err;
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141out:
Jeff Layton8e600292008-02-11 10:00:20 -0500142 /*
143 * svc_create creates the svc_serv with sv_nrthreads == 1, and then
Jeff Laytona277e332008-02-20 08:55:30 -0500144 * svc_prepare_thread increments that. So we need to call svc_destroy
Jeff Layton8e600292008-02-11 10:00:20 -0500145 * on both success and failure so that the refcount is 1 when the
146 * thread exits.
147 */
148 if (serv)
149 svc_destroy(serv);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800150 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return ret;
Jeff Layton8e600292008-02-11 10:00:20 -0500152out_err:
Chuck Lever18de9732008-10-16 17:41:11 -0400153 dprintk("NFS: Couldn't create callback socket or server thread; "
154 "err = %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 nfs_callback_info.users--;
156 goto out;
157}
158
159/*
Jeff Layton5afc5972008-06-11 10:03:11 -0400160 * Kill the callback thread if it's no longer being used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
David Howells5ae1fbc2006-08-22 20:06:08 -0400162void nfs_callback_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800164 mutex_lock(&nfs_callback_mutex);
Trond Myklebust1dd761e2006-03-20 13:44:49 -0500165 nfs_callback_info.users--;
Jeff Layton5afc5972008-06-11 10:03:11 -0400166 if (nfs_callback_info.users == 0 && nfs_callback_info.task != NULL) {
Jeff Laytona277e332008-02-20 08:55:30 -0500167 kthread_stop(nfs_callback_info.task);
Jeff Layton5afc5972008-06-11 10:03:11 -0400168 svc_exit_thread(nfs_callback_info.rqst);
169 nfs_callback_info.rqst = NULL;
170 nfs_callback_info.task = NULL;
171 }
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800172 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500175static int check_gss_callback_principal(struct nfs_client *clp,
176 struct svc_rqst *rqstp)
177{
178 struct rpc_clnt *r = clp->cl_rpcclient;
179 char *p = svc_gss_principal(rqstp);
180
181 /*
182 * It might just be a normal user principal, in which case
183 * userspace won't bother to tell us the name at all.
184 */
185 if (p == NULL)
186 return SVC_DENIED;
187
188 /* Expect a GSS_C_NT_HOSTBASED_NAME like "nfs@serverhostname" */
189
190 if (memcmp(p, "nfs@", 4) != 0)
191 return SVC_DENIED;
192 p += 4;
193 if (strcmp(p, r->cl_server) != 0)
194 return SVC_DENIED;
195 return SVC_OK;
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static int nfs_callback_authenticate(struct svc_rqst *rqstp)
199{
David Howellsadfa6f92006-08-22 20:06:08 -0400200 struct nfs_client *clp;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +0300201 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500202 int ret = SVC_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /* Don't talk to strangers */
Chuck Leverff052642007-12-10 14:58:44 -0500205 clp = nfs_find_client(svc_addr(rqstp), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (clp == NULL)
207 return SVC_DROP;
Chuck Leverad06e4b2007-02-12 00:53:32 -0800208
Harvey Harrison3110ff82008-05-02 13:42:44 -0700209 dprintk("%s: %s NFSv4 callback!\n", __func__,
Chuck Leverad06e4b2007-02-12 00:53:32 -0800210 svc_print_addr(rqstp, buf, sizeof(buf)));
Chuck Leverad06e4b2007-02-12 00:53:32 -0800211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 switch (rqstp->rq_authop->flavour) {
213 case RPC_AUTH_NULL:
214 if (rqstp->rq_proc != CB_NULL)
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500215 ret = SVC_DENIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
217 case RPC_AUTH_UNIX:
218 break;
219 case RPC_AUTH_GSS:
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500220 ret = check_gss_callback_principal(clp, rqstp);
221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 default:
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500223 ret = SVC_DENIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500225 nfs_put_client(clp);
226 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229/*
230 * Define NFS4 callback program
231 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static struct svc_version *nfs4_callback_version[] = {
233 [1] = &nfs4_callback_version1,
234};
235
236static struct svc_stat nfs4_callback_stats;
237
238static struct svc_program nfs4_callback_program = {
239 .pg_prog = NFS4_CALLBACK, /* RPC service number */
240 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
241 .pg_vers = nfs4_callback_version, /* version table */
242 .pg_name = "NFSv4 callback", /* service name */
243 .pg_class = "nfs", /* authentication class */
244 .pg_stats = &nfs4_callback_stats,
245 .pg_authenticate = nfs_callback_authenticate,
246};