nfsd4: move principal name into svc_cred
[linux-3.10.git] / fs / nfs / callback.c
1 /*
2  * linux/fs/nfs/callback.c
3  *
4  * Copyright (C) 2004 Trond Myklebust
5  *
6  * NFSv4 callback handling
7  */
8
9 #include <linux/completion.h>
10 #include <linux/ip.h>
11 #include <linux/module.h>
12 #include <linux/sunrpc/svc.h>
13 #include <linux/sunrpc/svcsock.h>
14 #include <linux/nfs_fs.h>
15 #include <linux/mutex.h>
16 #include <linux/freezer.h>
17 #include <linux/kthread.h>
18 #include <linux/sunrpc/svcauth_gss.h>
19 #include <linux/sunrpc/bc_xprt.h>
20 #include <linux/nsproxy.h>
21
22 #include <net/inet_sock.h>
23
24 #include "nfs4_fs.h"
25 #include "callback.h"
26 #include "internal.h"
27
28 #define NFSDBG_FACILITY NFSDBG_CALLBACK
29
30 struct nfs_callback_data {
31         unsigned int users;
32         struct svc_serv *serv;
33         struct svc_rqst *rqst;
34         struct task_struct *task;
35 };
36
37 static struct nfs_callback_data nfs_callback_info[NFS4_MAX_MINOR_VERSION + 1];
38 static DEFINE_MUTEX(nfs_callback_mutex);
39 static struct svc_program nfs4_callback_program;
40
41 unsigned int nfs_callback_set_tcpport;
42 unsigned short nfs_callback_tcpport;
43 unsigned short nfs_callback_tcpport6;
44 #define NFS_CALLBACK_MAXPORTNR (65535U)
45
46 static int param_set_portnr(const char *val, const struct kernel_param *kp)
47 {
48         unsigned long num;
49         int ret;
50
51         if (!val)
52                 return -EINVAL;
53         ret = strict_strtoul(val, 0, &num);
54         if (ret == -EINVAL || num > NFS_CALLBACK_MAXPORTNR)
55                 return -EINVAL;
56         *((unsigned int *)kp->arg) = num;
57         return 0;
58 }
59 static struct kernel_param_ops param_ops_portnr = {
60         .set = param_set_portnr,
61         .get = param_get_uint,
62 };
63 #define param_check_portnr(name, p) __param_check(name, p, unsigned int);
64
65 module_param_named(callback_tcpport, nfs_callback_set_tcpport, portnr, 0644);
66
67 /*
68  * This is the NFSv4 callback kernel thread.
69  */
70 static int
71 nfs4_callback_svc(void *vrqstp)
72 {
73         int err, preverr = 0;
74         struct svc_rqst *rqstp = vrqstp;
75
76         set_freezable();
77
78         while (!kthread_should_stop()) {
79                 /*
80                  * Listen for a request on the socket
81                  */
82                 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
83                 if (err == -EAGAIN || err == -EINTR) {
84                         preverr = err;
85                         continue;
86                 }
87                 if (err < 0) {
88                         if (err != preverr) {
89                                 printk(KERN_WARNING "NFS: %s: unexpected error "
90                                         "from svc_recv (%d)\n", __func__, err);
91                                 preverr = err;
92                         }
93                         schedule_timeout_uninterruptible(HZ);
94                         continue;
95                 }
96                 preverr = err;
97                 svc_process(rqstp);
98         }
99         return 0;
100 }
101
102 /*
103  * Prepare to bring up the NFSv4 callback service
104  */
105 static struct svc_rqst *
106 nfs4_callback_up(struct svc_serv *serv, struct rpc_xprt *xprt)
107 {
108         int ret;
109
110         ret = svc_create_xprt(serv, "tcp", xprt->xprt_net, PF_INET,
111                                 nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS);
112         if (ret <= 0)
113                 goto out_err;
114         nfs_callback_tcpport = ret;
115         dprintk("NFS: Callback listener port = %u (af %u)\n",
116                         nfs_callback_tcpport, PF_INET);
117
118         ret = svc_create_xprt(serv, "tcp", xprt->xprt_net, PF_INET6,
119                                 nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS);
120         if (ret > 0) {
121                 nfs_callback_tcpport6 = ret;
122                 dprintk("NFS: Callback listener port = %u (af %u)\n",
123                                 nfs_callback_tcpport6, PF_INET6);
124         } else if (ret == -EAFNOSUPPORT)
125                 ret = 0;
126         else
127                 goto out_err;
128
129         return svc_prepare_thread(serv, &serv->sv_pools[0], NUMA_NO_NODE);
130
131 out_err:
132         if (ret == 0)
133                 ret = -ENOMEM;
134         return ERR_PTR(ret);
135 }
136
137 #if defined(CONFIG_NFS_V4_1)
138 /*
139  * The callback service for NFSv4.1 callbacks
140  */
141 static int
142 nfs41_callback_svc(void *vrqstp)
143 {
144         struct svc_rqst *rqstp = vrqstp;
145         struct svc_serv *serv = rqstp->rq_server;
146         struct rpc_rqst *req;
147         int error;
148         DEFINE_WAIT(wq);
149
150         set_freezable();
151
152         while (!kthread_should_stop()) {
153                 prepare_to_wait(&serv->sv_cb_waitq, &wq, TASK_INTERRUPTIBLE);
154                 spin_lock_bh(&serv->sv_cb_lock);
155                 if (!list_empty(&serv->sv_cb_list)) {
156                         req = list_first_entry(&serv->sv_cb_list,
157                                         struct rpc_rqst, rq_bc_list);
158                         list_del(&req->rq_bc_list);
159                         spin_unlock_bh(&serv->sv_cb_lock);
160                         dprintk("Invoking bc_svc_process()\n");
161                         error = bc_svc_process(serv, req, rqstp);
162                         dprintk("bc_svc_process() returned w/ error code= %d\n",
163                                 error);
164                 } else {
165                         spin_unlock_bh(&serv->sv_cb_lock);
166                         schedule();
167                 }
168                 finish_wait(&serv->sv_cb_waitq, &wq);
169         }
170         return 0;
171 }
172
173 /*
174  * Bring up the NFSv4.1 callback service
175  */
176 static struct svc_rqst *
177 nfs41_callback_up(struct svc_serv *serv, struct rpc_xprt *xprt)
178 {
179         struct svc_rqst *rqstp;
180         int ret;
181
182         /*
183          * Create an svc_sock for the back channel service that shares the
184          * fore channel connection.
185          * Returns the input port (0) and sets the svc_serv bc_xprt on success
186          */
187         ret = svc_create_xprt(serv, "tcp-bc", xprt->xprt_net, PF_INET, 0,
188                               SVC_SOCK_ANONYMOUS);
189         if (ret < 0) {
190                 rqstp = ERR_PTR(ret);
191                 goto out;
192         }
193
194         /*
195          * Save the svc_serv in the transport so that it can
196          * be referenced when the session backchannel is initialized
197          */
198         xprt->bc_serv = serv;
199
200         INIT_LIST_HEAD(&serv->sv_cb_list);
201         spin_lock_init(&serv->sv_cb_lock);
202         init_waitqueue_head(&serv->sv_cb_waitq);
203         rqstp = svc_prepare_thread(serv, &serv->sv_pools[0], NUMA_NO_NODE);
204         if (IS_ERR(rqstp)) {
205                 svc_xprt_put(serv->sv_bc_xprt);
206                 serv->sv_bc_xprt = NULL;
207         }
208 out:
209         dprintk("--> %s return %ld\n", __func__,
210                 IS_ERR(rqstp) ? PTR_ERR(rqstp) : 0);
211         return rqstp;
212 }
213
214 static inline int nfs_minorversion_callback_svc_setup(u32 minorversion,
215                 struct svc_serv *serv, struct rpc_xprt *xprt,
216                 struct svc_rqst **rqstpp, int (**callback_svc)(void *vrqstp))
217 {
218         if (minorversion) {
219                 *rqstpp = nfs41_callback_up(serv, xprt);
220                 *callback_svc = nfs41_callback_svc;
221         }
222         return minorversion;
223 }
224
225 static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt,
226                 struct nfs_callback_data *cb_info)
227 {
228         if (minorversion)
229                 xprt->bc_serv = cb_info->serv;
230 }
231 #else
232 static inline int nfs_minorversion_callback_svc_setup(u32 minorversion,
233                 struct svc_serv *serv, struct rpc_xprt *xprt,
234                 struct svc_rqst **rqstpp, int (**callback_svc)(void *vrqstp))
235 {
236         return 0;
237 }
238
239 static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt,
240                 struct nfs_callback_data *cb_info)
241 {
242 }
243 #endif /* CONFIG_NFS_V4_1 */
244
245 /*
246  * Bring up the callback thread if it is not already up.
247  */
248 int nfs_callback_up(u32 minorversion, struct rpc_xprt *xprt)
249 {
250         struct svc_serv *serv = NULL;
251         struct svc_rqst *rqstp;
252         int (*callback_svc)(void *vrqstp);
253         struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
254         char svc_name[12];
255         int ret = 0;
256         int minorversion_setup;
257         struct net *net = current->nsproxy->net_ns;
258
259         mutex_lock(&nfs_callback_mutex);
260         if (cb_info->users++ || cb_info->task != NULL) {
261                 nfs_callback_bc_serv(minorversion, xprt, cb_info);
262                 goto out;
263         }
264         serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL);
265         if (!serv) {
266                 ret = -ENOMEM;
267                 goto out_err;
268         }
269
270         ret = svc_bind(serv, net);
271         if (ret < 0) {
272                 printk(KERN_WARNING "NFS: bind callback service failed\n");
273                 goto out_err;
274         }
275
276         minorversion_setup =  nfs_minorversion_callback_svc_setup(minorversion,
277                                         serv, xprt, &rqstp, &callback_svc);
278         if (!minorversion_setup) {
279                 /* v4.0 callback setup */
280                 rqstp = nfs4_callback_up(serv, xprt);
281                 callback_svc = nfs4_callback_svc;
282         }
283
284         if (IS_ERR(rqstp)) {
285                 ret = PTR_ERR(rqstp);
286                 goto out_err;
287         }
288
289         svc_sock_update_bufs(serv);
290
291         sprintf(svc_name, "nfsv4.%u-svc", minorversion);
292         cb_info->serv = serv;
293         cb_info->rqst = rqstp;
294         cb_info->task = kthread_run(callback_svc, cb_info->rqst, svc_name);
295         if (IS_ERR(cb_info->task)) {
296                 ret = PTR_ERR(cb_info->task);
297                 svc_exit_thread(cb_info->rqst);
298                 cb_info->rqst = NULL;
299                 cb_info->task = NULL;
300                 goto out_err;
301         }
302 out:
303         /*
304          * svc_create creates the svc_serv with sv_nrthreads == 1, and then
305          * svc_prepare_thread increments that. So we need to call svc_destroy
306          * on both success and failure so that the refcount is 1 when the
307          * thread exits.
308          */
309         if (serv)
310                 svc_destroy(serv);
311         mutex_unlock(&nfs_callback_mutex);
312         return ret;
313 out_err:
314         dprintk("NFS: Couldn't create callback socket or server thread; "
315                 "err = %d\n", ret);
316         cb_info->users--;
317         if (serv)
318                 svc_shutdown_net(serv, net);
319         goto out;
320 }
321
322 /*
323  * Kill the callback thread if it's no longer being used.
324  */
325 void nfs_callback_down(int minorversion)
326 {
327         struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
328
329         mutex_lock(&nfs_callback_mutex);
330         cb_info->users--;
331         if (cb_info->users == 0 && cb_info->task != NULL) {
332                 kthread_stop(cb_info->task);
333                 svc_shutdown_net(cb_info->serv, current->nsproxy->net_ns);
334                 svc_exit_thread(cb_info->rqst);
335                 cb_info->serv = NULL;
336                 cb_info->rqst = NULL;
337                 cb_info->task = NULL;
338         }
339         mutex_unlock(&nfs_callback_mutex);
340 }
341
342 /* Boolean check of RPC_AUTH_GSS principal */
343 int
344 check_gss_callback_principal(struct nfs_client *clp, struct svc_rqst *rqstp)
345 {
346         char *p = rqstp->rq_cred.cr_principal;
347
348         if (rqstp->rq_authop->flavour != RPC_AUTH_GSS)
349                 return 1;
350
351         /* No RPC_AUTH_GSS on NFSv4.1 back channel yet */
352         if (clp->cl_minorversion != 0)
353                 return 0;
354         /*
355          * It might just be a normal user principal, in which case
356          * userspace won't bother to tell us the name at all.
357          */
358         if (p == NULL)
359                 return 0;
360
361         /* Expect a GSS_C_NT_HOSTBASED_NAME like "nfs@serverhostname" */
362
363         if (memcmp(p, "nfs@", 4) != 0)
364                 return 0;
365         p += 4;
366         if (strcmp(p, clp->cl_hostname) != 0)
367                 return 0;
368         return 1;
369 }
370
371 /*
372  * pg_authenticate method for nfsv4 callback threads.
373  *
374  * The authflavor has been negotiated, so an incorrect flavor is a server
375  * bug. Drop packets with incorrect authflavor.
376  *
377  * All other checking done after NFS decoding where the nfs_client can be
378  * found in nfs4_callback_compound
379  */
380 static int nfs_callback_authenticate(struct svc_rqst *rqstp)
381 {
382         switch (rqstp->rq_authop->flavour) {
383         case RPC_AUTH_NULL:
384                 if (rqstp->rq_proc != CB_NULL)
385                         return SVC_DROP;
386                 break;
387         case RPC_AUTH_GSS:
388                 /* No RPC_AUTH_GSS support yet in NFSv4.1 */
389                  if (svc_is_backchannel(rqstp))
390                         return SVC_DROP;
391         }
392         return SVC_OK;
393 }
394
395 /*
396  * Define NFS4 callback program
397  */
398 static struct svc_version *nfs4_callback_version[] = {
399         [1] = &nfs4_callback_version1,
400         [4] = &nfs4_callback_version4,
401 };
402
403 static struct svc_stat nfs4_callback_stats;
404
405 static struct svc_program nfs4_callback_program = {
406         .pg_prog = NFS4_CALLBACK,                       /* RPC service number */
407         .pg_nvers = ARRAY_SIZE(nfs4_callback_version),  /* Number of entries */
408         .pg_vers = nfs4_callback_version,               /* version table */
409         .pg_name = "NFSv4 callback",                    /* service name */
410         .pg_class = "nfs",                              /* authentication class */
411         .pg_stats = &nfs4_callback_stats,
412         .pg_authenticate = nfs_callback_authenticate,
413 };