blob: 3e634f2a1083cf556e9c3429320e0cd458d3c5b1 [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
Chuck Lever18de9732008-10-16 17:41:11 -040044/*
45 * If the kernel has IPv6 support available, always listen for
46 * both AF_INET and AF_INET6 requests.
47 */
48#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
49static const sa_family_t nfs_callback_family = AF_INET6;
50#else
51static const sa_family_t nfs_callback_family = AF_INET;
52#endif
53
David Howells7d4e2742006-08-22 20:06:07 -040054static int param_set_port(const char *val, struct kernel_param *kp)
55{
56 char *endp;
57 int num = simple_strtol(val, &endp, 0);
58 if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
59 return -EINVAL;
60 *((int *)kp->arg) = num;
61 return 0;
62}
63
64module_param_call(callback_tcpport, param_set_port, param_get_int,
65 &nfs_callback_set_tcpport, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/*
68 * This is the callback kernel thread.
69 */
Jeff Laytona277e332008-02-20 08:55:30 -050070static int
71nfs_callback_svc(void *vrqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Jeff Layton06e02d62008-04-08 15:40:07 -040073 int err, preverr = 0;
Jeff Laytona277e332008-02-20 08:55:30 -050074 struct svc_rqst *rqstp = vrqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Rafael J. Wysocki83144182007-07-17 04:03:35 -070076 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Jeff Laytona277e332008-02-20 08:55:30 -050078 /*
79 * FIXME: do we really need to run this under the BKL? If so, please
80 * add a comment about what it's intended to protect.
81 */
82 lock_kernel();
83 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 /*
85 * Listen for a request on the socket
86 */
NeilBrown6fb2b472006-10-02 02:17:50 -070087 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
Jeff Layton06e02d62008-04-08 15:40:07 -040088 if (err == -EAGAIN || err == -EINTR) {
89 preverr = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
Jeff Layton06e02d62008-04-08 15:40:07 -040092 if (err < 0) {
93 if (err != preverr) {
94 printk(KERN_WARNING "%s: unexpected error "
95 "from svc_recv (%d)\n", __func__, err);
96 preverr = err;
97 }
98 schedule_timeout_uninterruptible(HZ);
99 continue;
100 }
101 preverr = err;
NeilBrown6fb2b472006-10-02 02:17:50 -0700102 svc_process(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unlock_kernel();
Jeff Laytona277e332008-02-20 08:55:30 -0500105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108/*
Jeff Layton5afc5972008-06-11 10:03:11 -0400109 * Bring up the callback thread if it is not already up.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 */
111int nfs_callback_up(void)
112{
Jeff Layton8e600292008-02-11 10:00:20 -0500113 struct svc_serv *serv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 int ret = 0;
115
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800116 mutex_lock(&nfs_callback_mutex);
Jeff Laytona277e332008-02-20 08:55:30 -0500117 if (nfs_callback_info.users++ || nfs_callback_info.task != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 goto out;
Chuck Levere851db52008-06-30 18:45:30 -0400119 serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE,
Chuck Lever18de9732008-10-16 17:41:11 -0400120 nfs_callback_family, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 ret = -ENOMEM;
122 if (!serv)
123 goto out_err;
Chuck Lever482fb942007-02-12 00:53:29 -0800124
Tom Tuckerd7c9f1e2007-12-30 21:07:44 -0600125 ret = svc_create_xprt(serv, "tcp", nfs_callback_set_tcpport,
126 SVC_SOCK_ANONYMOUS);
Chuck Lever482fb942007-02-12 00:53:29 -0800127 if (ret <= 0)
Jeff Layton8e600292008-02-11 10:00:20 -0500128 goto out_err;
Chuck Lever482fb942007-02-12 00:53:29 -0800129 nfs_callback_tcpport = ret;
Chuck Lever18de9732008-10-16 17:41:11 -0400130 dprintk("NFS: Callback listener port = %u (af %u)\n",
131 nfs_callback_tcpport, nfs_callback_family);
Chuck Lever482fb942007-02-12 00:53:29 -0800132
Jeff Layton5afc5972008-06-11 10:03:11 -0400133 nfs_callback_info.rqst = svc_prepare_thread(serv, &serv->sv_pools[0]);
134 if (IS_ERR(nfs_callback_info.rqst)) {
135 ret = PTR_ERR(nfs_callback_info.rqst);
136 nfs_callback_info.rqst = NULL;
Jeff Layton8e600292008-02-11 10:00:20 -0500137 goto out_err;
Jeff Laytona277e332008-02-20 08:55:30 -0500138 }
139
140 svc_sock_update_bufs(serv);
Jeff Laytona277e332008-02-20 08:55:30 -0500141
Jeff Layton5afc5972008-06-11 10:03:11 -0400142 nfs_callback_info.task = kthread_run(nfs_callback_svc,
143 nfs_callback_info.rqst,
Jeff Laytona277e332008-02-20 08:55:30 -0500144 "nfsv4-svc");
145 if (IS_ERR(nfs_callback_info.task)) {
146 ret = PTR_ERR(nfs_callback_info.task);
Jeff Layton5afc5972008-06-11 10:03:11 -0400147 svc_exit_thread(nfs_callback_info.rqst);
148 nfs_callback_info.rqst = NULL;
Jeff Laytona277e332008-02-20 08:55:30 -0500149 nfs_callback_info.task = NULL;
Jeff Laytona277e332008-02-20 08:55:30 -0500150 goto out_err;
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152out:
Jeff Layton8e600292008-02-11 10:00:20 -0500153 /*
154 * svc_create creates the svc_serv with sv_nrthreads == 1, and then
Jeff Laytona277e332008-02-20 08:55:30 -0500155 * svc_prepare_thread increments that. So we need to call svc_destroy
Jeff Layton8e600292008-02-11 10:00:20 -0500156 * on both success and failure so that the refcount is 1 when the
157 * thread exits.
158 */
159 if (serv)
160 svc_destroy(serv);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800161 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return ret;
Jeff Layton8e600292008-02-11 10:00:20 -0500163out_err:
Chuck Lever18de9732008-10-16 17:41:11 -0400164 dprintk("NFS: Couldn't create callback socket or server thread; "
165 "err = %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 nfs_callback_info.users--;
167 goto out;
168}
169
170/*
Jeff Layton5afc5972008-06-11 10:03:11 -0400171 * Kill the callback thread if it's no longer being used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
David Howells5ae1fbc2006-08-22 20:06:08 -0400173void nfs_callback_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800175 mutex_lock(&nfs_callback_mutex);
Trond Myklebust1dd761e2006-03-20 13:44:49 -0500176 nfs_callback_info.users--;
Jeff Layton5afc5972008-06-11 10:03:11 -0400177 if (nfs_callback_info.users == 0 && nfs_callback_info.task != NULL) {
Jeff Laytona277e332008-02-20 08:55:30 -0500178 kthread_stop(nfs_callback_info.task);
Jeff Layton5afc5972008-06-11 10:03:11 -0400179 svc_exit_thread(nfs_callback_info.rqst);
180 nfs_callback_info.rqst = NULL;
181 nfs_callback_info.task = NULL;
182 }
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800183 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500186static int check_gss_callback_principal(struct nfs_client *clp,
187 struct svc_rqst *rqstp)
188{
189 struct rpc_clnt *r = clp->cl_rpcclient;
190 char *p = svc_gss_principal(rqstp);
191
192 /*
193 * It might just be a normal user principal, in which case
194 * userspace won't bother to tell us the name at all.
195 */
196 if (p == NULL)
197 return SVC_DENIED;
198
199 /* Expect a GSS_C_NT_HOSTBASED_NAME like "nfs@serverhostname" */
200
201 if (memcmp(p, "nfs@", 4) != 0)
202 return SVC_DENIED;
203 p += 4;
204 if (strcmp(p, r->cl_server) != 0)
205 return SVC_DENIED;
206 return SVC_OK;
207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static int nfs_callback_authenticate(struct svc_rqst *rqstp)
210{
David Howellsadfa6f92006-08-22 20:06:08 -0400211 struct nfs_client *clp;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +0300212 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500213 int ret = SVC_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* Don't talk to strangers */
Chuck Leverff052642007-12-10 14:58:44 -0500216 clp = nfs_find_client(svc_addr(rqstp), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (clp == NULL)
218 return SVC_DROP;
Chuck Leverad06e4b2007-02-12 00:53:32 -0800219
Harvey Harrison3110ff82008-05-02 13:42:44 -0700220 dprintk("%s: %s NFSv4 callback!\n", __func__,
Chuck Leverad06e4b2007-02-12 00:53:32 -0800221 svc_print_addr(rqstp, buf, sizeof(buf)));
Chuck Leverad06e4b2007-02-12 00:53:32 -0800222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 switch (rqstp->rq_authop->flavour) {
224 case RPC_AUTH_NULL:
225 if (rqstp->rq_proc != CB_NULL)
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500226 ret = SVC_DENIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 break;
228 case RPC_AUTH_UNIX:
229 break;
230 case RPC_AUTH_GSS:
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500231 ret = check_gss_callback_principal(clp, rqstp);
232 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 default:
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500234 ret = SVC_DENIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500236 nfs_put_client(clp);
237 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240/*
241 * Define NFS4 callback program
242 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static struct svc_version *nfs4_callback_version[] = {
244 [1] = &nfs4_callback_version1,
245};
246
247static struct svc_stat nfs4_callback_stats;
248
249static struct svc_program nfs4_callback_program = {
250 .pg_prog = NFS4_CALLBACK, /* RPC service number */
251 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
252 .pg_vers = nfs4_callback_version, /* version table */
253 .pg_name = "NFSv4 callback", /* service name */
254 .pg_class = "nfs", /* authentication class */
255 .pg_stats = &nfs4_callback_stats,
256 .pg_authenticate = nfs_callback_authenticate,
257};