Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/shaggy...
[linux-2.6] / 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/smp_lock.h>
13 #include <linux/sunrpc/svc.h>
14 #include <linux/sunrpc/svcsock.h>
15 #include <linux/nfs_fs.h>
16 #include <linux/mutex.h>
17 #include <linux/freezer.h>
18 #include <linux/kthread.h>
19 #include <linux/sunrpc/svcauth_gss.h>
20
21 #include <net/inet_sock.h>
22
23 #include "nfs4_fs.h"
24 #include "callback.h"
25 #include "internal.h"
26
27 #define NFSDBG_FACILITY NFSDBG_CALLBACK
28
29 struct nfs_callback_data {
30         unsigned int users;
31         struct svc_rqst *rqst;
32         struct task_struct *task;
33 };
34
35 static struct nfs_callback_data nfs_callback_info;
36 static DEFINE_MUTEX(nfs_callback_mutex);
37 static struct svc_program nfs4_callback_program;
38
39 unsigned int nfs_callback_set_tcpport;
40 unsigned short nfs_callback_tcpport;
41 static const int nfs_set_port_min = 0;
42 static const int nfs_set_port_max = 65535;
43
44 /*
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)
49 static const sa_family_t        nfs_callback_family = AF_INET6;
50 #else
51 static const sa_family_t        nfs_callback_family = AF_INET;
52 #endif
53
54 static 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
64 module_param_call(callback_tcpport, param_set_port, param_get_int,
65                  &nfs_callback_set_tcpport, 0644);
66
67 /*
68  * This is the callback kernel thread.
69  */
70 static int
71 nfs_callback_svc(void *vrqstp)
72 {
73         int err, preverr = 0;
74         struct svc_rqst *rqstp = vrqstp;
75
76         set_freezable();
77
78         /*
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()) {
84                 /*
85                  * Listen for a request on the socket
86                  */
87                 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
88                 if (err == -EAGAIN || err == -EINTR) {
89                         preverr = err;
90                         continue;
91                 }
92                 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;
102                 svc_process(rqstp);
103         }
104         unlock_kernel();
105         return 0;
106 }
107
108 /*
109  * Bring up the callback thread if it is not already up.
110  */
111 int nfs_callback_up(void)
112 {
113         struct svc_serv *serv = NULL;
114         int ret = 0;
115
116         mutex_lock(&nfs_callback_mutex);
117         if (nfs_callback_info.users++ || nfs_callback_info.task != NULL)
118                 goto out;
119         serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE,
120                                 nfs_callback_family, NULL);
121         ret = -ENOMEM;
122         if (!serv)
123                 goto out_err;
124
125         ret = svc_create_xprt(serv, "tcp", nfs_callback_set_tcpport,
126                               SVC_SOCK_ANONYMOUS);
127         if (ret <= 0)
128                 goto out_err;
129         nfs_callback_tcpport = ret;
130         dprintk("NFS: Callback listener port = %u (af %u)\n",
131                         nfs_callback_tcpport, nfs_callback_family);
132
133         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;
137                 goto out_err;
138         }
139
140         svc_sock_update_bufs(serv);
141
142         nfs_callback_info.task = kthread_run(nfs_callback_svc,
143                                              nfs_callback_info.rqst,
144                                              "nfsv4-svc");
145         if (IS_ERR(nfs_callback_info.task)) {
146                 ret = PTR_ERR(nfs_callback_info.task);
147                 svc_exit_thread(nfs_callback_info.rqst);
148                 nfs_callback_info.rqst = NULL;
149                 nfs_callback_info.task = NULL;
150                 goto out_err;
151         }
152 out:
153         /*
154          * svc_create creates the svc_serv with sv_nrthreads == 1, and then
155          * svc_prepare_thread increments that. So we need to call svc_destroy
156          * on both success and failure so that the refcount is 1 when the
157          * thread exits.
158          */
159         if (serv)
160                 svc_destroy(serv);
161         mutex_unlock(&nfs_callback_mutex);
162         return ret;
163 out_err:
164         dprintk("NFS: Couldn't create callback socket or server thread; "
165                 "err = %d\n", ret);
166         nfs_callback_info.users--;
167         goto out;
168 }
169
170 /*
171  * Kill the callback thread if it's no longer being used.
172  */
173 void nfs_callback_down(void)
174 {
175         mutex_lock(&nfs_callback_mutex);
176         nfs_callback_info.users--;
177         if (nfs_callback_info.users == 0 && nfs_callback_info.task != NULL) {
178                 kthread_stop(nfs_callback_info.task);
179                 svc_exit_thread(nfs_callback_info.rqst);
180                 nfs_callback_info.rqst = NULL;
181                 nfs_callback_info.task = NULL;
182         }
183         mutex_unlock(&nfs_callback_mutex);
184 }
185
186 static 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
209 static int nfs_callback_authenticate(struct svc_rqst *rqstp)
210 {
211         struct nfs_client *clp;
212         RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
213         int ret = SVC_OK;
214
215         /* Don't talk to strangers */
216         clp = nfs_find_client(svc_addr(rqstp), 4);
217         if (clp == NULL)
218                 return SVC_DROP;
219
220         dprintk("%s: %s NFSv4 callback!\n", __func__,
221                         svc_print_addr(rqstp, buf, sizeof(buf)));
222
223         switch (rqstp->rq_authop->flavour) {
224                 case RPC_AUTH_NULL:
225                         if (rqstp->rq_proc != CB_NULL)
226                                 ret = SVC_DENIED;
227                         break;
228                 case RPC_AUTH_UNIX:
229                         break;
230                 case RPC_AUTH_GSS:
231                         ret = check_gss_callback_principal(clp, rqstp);
232                         break;
233                 default:
234                         ret = SVC_DENIED;
235         }
236         nfs_put_client(clp);
237         return ret;
238 }
239
240 /*
241  * Define NFS4 callback program
242  */
243 static struct svc_version *nfs4_callback_version[] = {
244         [1] = &nfs4_callback_version1,
245 };
246
247 static struct svc_stat nfs4_callback_stats;
248
249 static 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 };