blob: 0a2115084c3f2dbf0b759c262967f8d1c2dcecd4 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/sunrpc/svc.h>
13#include <linux/sunrpc/svcsock.h>
14#include <linux/nfs_fs.h>
Trond Myklebust758201e2012-10-01 16:33:18 -070015#include <linux/errno.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>
Ricardo Labiagaa43cde92009-04-01 09:23:08 -040020#include <linux/sunrpc/bc_xprt.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020021
22#include <net/inet_sock.h>
23
Trond Myklebust4ce79712005-06-22 17:16:21 +000024#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "callback.h"
David Howells24c8dbb2006-08-22 20:06:10 -040026#include "internal.h"
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +040027#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#define NFSDBG_FACILITY NFSDBG_CALLBACK
30
31struct nfs_callback_data {
32 unsigned int users;
Ricardo Labiagaa43cde92009-04-01 09:23:08 -040033 struct svc_serv *serv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
Andy Adamsone82dc222009-04-01 09:23:14 -040036static struct nfs_callback_data nfs_callback_info[NFS4_MAX_MINOR_VERSION + 1];
Ingo Molnar353ab6e2006-03-26 01:37:12 -080037static DEFINE_MUTEX(nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static struct svc_program nfs4_callback_program;
39
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +040040static int nfs4_callback_up_net(struct svc_serv *serv, struct net *net)
41{
42 int ret;
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +040043 struct nfs_net *nn = net_generic(net, nfs_net_id);
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +040044
45 ret = svc_create_xprt(serv, "tcp", net, PF_INET,
46 nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS);
47 if (ret <= 0)
48 goto out_err;
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +040049 nn->nfs_callback_tcpport = ret;
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +040050 dprintk("NFS: Callback listener port = %u (af %u, net %p)\n",
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +040051 nn->nfs_callback_tcpport, PF_INET, net);
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +040052
53 ret = svc_create_xprt(serv, "tcp", net, PF_INET6,
54 nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS);
55 if (ret > 0) {
Stanislav Kinsbursky29dcc162012-08-20 18:00:41 +040056 nn->nfs_callback_tcpport6 = ret;
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +040057 dprintk("NFS: Callback listener port = %u (af %u, net %p)\n",
Stanislav Kinsbursky29dcc162012-08-20 18:00:41 +040058 nn->nfs_callback_tcpport6, PF_INET6, net);
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +040059 } else if (ret != -EAFNOSUPPORT)
60 goto out_err;
61 return 0;
62
63out_err:
64 return (ret) ? ret : -ENOMEM;
65}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
Andy Adamsone82dc222009-04-01 09:23:14 -040068 * This is the NFSv4 callback kernel thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 */
Jeff Laytona277e332008-02-20 08:55:30 -050070static int
Benny Halevy71468512009-04-01 09:22:56 -040071nfs4_callback_svc(void *vrqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
J. Bruce Fields5b444cc2012-08-17 21:47:53 -040073 int err;
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 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 /*
80 * Listen for a request on the socket
81 */
NeilBrown6fb2b472006-10-02 02:17:50 -070082 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
J. Bruce Fields5b444cc2012-08-17 21:47:53 -040083 if (err == -EAGAIN || err == -EINTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 continue;
NeilBrown6fb2b472006-10-02 02:17:50 -070085 svc_process(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
Jeff Laytona277e332008-02-20 08:55:30 -050087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Ricardo Labiagaa43cde92009-04-01 09:23:08 -040090#if defined(CONFIG_NFS_V4_1)
91/*
92 * The callback service for NFSv4.1 callbacks
93 */
94static int
95nfs41_callback_svc(void *vrqstp)
96{
97 struct svc_rqst *rqstp = vrqstp;
98 struct svc_serv *serv = rqstp->rq_server;
99 struct rpc_rqst *req;
100 int error;
101 DEFINE_WAIT(wq);
102
103 set_freezable();
104
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400105 while (!kthread_should_stop()) {
Jeff Layton25d280a2013-03-25 07:59:57 -0400106 if (try_to_freeze())
107 continue;
108
Jeff Layton5d05e542015-03-20 15:15:14 -0400109 prepare_to_wait(&serv->sv_cb_waitq, &wq, TASK_INTERRUPTIBLE);
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400110 spin_lock_bh(&serv->sv_cb_lock);
111 if (!list_empty(&serv->sv_cb_list)) {
112 req = list_first_entry(&serv->sv_cb_list,
113 struct rpc_rqst, rq_bc_list);
114 list_del(&req->rq_bc_list);
115 spin_unlock_bh(&serv->sv_cb_lock);
Jeff Layton6ffa30d2015-01-14 13:08:57 -0500116 finish_wait(&serv->sv_cb_waitq, &wq);
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400117 dprintk("Invoking bc_svc_process()\n");
118 error = bc_svc_process(serv, req, rqstp);
119 dprintk("bc_svc_process() returned w/ error code= %d\n",
120 error);
121 } else {
122 spin_unlock_bh(&serv->sv_cb_lock);
Jeff Layton5d05e542015-03-20 15:15:14 -0400123 schedule();
Jeff Layton6ffa30d2015-01-14 13:08:57 -0500124 finish_wait(&serv->sv_cb_waitq, &wq);
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400125 }
Jeff Layton5d05e542015-03-20 15:15:14 -0400126 flush_signals(current);
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400127 }
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400128 return 0;
129}
130
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400131static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt,
Stanislav Kinsbursky691c4572012-08-20 18:00:21 +0400132 struct svc_serv *serv)
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400133{
134 if (minorversion)
Stanislav Kinsbursky691c4572012-08-20 18:00:21 +0400135 /*
136 * Save the svc_serv in the transport so that it can
137 * be referenced when the session backchannel is initialized
138 */
139 xprt->bc_serv = serv;
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400140}
141#else
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400142static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt,
Stanislav Kinsbursky691c4572012-08-20 18:00:21 +0400143 struct svc_serv *serv)
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400144{
145}
146#endif /* CONFIG_NFS_V4_1 */
147
Stanislav Kinsbursky8e246142012-08-20 18:00:26 +0400148static int nfs_callback_start_svc(int minorversion, struct rpc_xprt *xprt,
149 struct svc_serv *serv)
150{
Trond Myklebust5405fc42016-08-29 20:03:52 -0400151 int nrservs = nfs_callback_nr_threads;
Stanislav Kinsbursky8e246142012-08-20 18:00:26 +0400152 int ret;
Stanislav Kinsbursky8e246142012-08-20 18:00:26 +0400153
154 nfs_callback_bc_serv(minorversion, xprt, serv);
155
Trond Myklebust5405fc42016-08-29 20:03:52 -0400156 if (nrservs < NFS4_MIN_NR_CALLBACK_THREADS)
157 nrservs = NFS4_MIN_NR_CALLBACK_THREADS;
158
Trond Myklebustbb6aeba2016-08-29 20:03:51 -0400159 if (serv->sv_nrthreads-1 == nrservs)
Stanislav Kinsbursky23c20ec2012-08-20 18:00:31 +0400160 return 0;
161
Trond Myklebustbb6aeba2016-08-29 20:03:51 -0400162 ret = serv->sv_ops->svo_setup(serv, NULL, nrservs);
163 if (ret) {
164 serv->sv_ops->svo_setup(serv, NULL, 0);
Trond Myklebuste9b7e912012-10-16 12:30:44 -0400165 return ret;
Stanislav Kinsbursky8e246142012-08-20 18:00:26 +0400166 }
167 dprintk("nfs_callback_up: service started\n");
168 return 0;
169}
170
Stanislav Kinsburskyb3d19c52012-08-20 18:00:46 +0400171static void nfs_callback_down_net(u32 minorversion, struct svc_serv *serv, struct net *net)
172{
173 struct nfs_net *nn = net_generic(net, nfs_net_id);
174
175 if (--nn->cb_users[minorversion])
176 return;
177
178 dprintk("NFS: destroy per-net callback data; net=%p\n", net);
179 svc_shutdown_net(serv, net);
180}
181
Chuck Lever76566772015-10-24 17:28:32 -0400182static int nfs_callback_up_net(int minorversion, struct svc_serv *serv,
183 struct net *net, struct rpc_xprt *xprt)
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +0400184{
Stanislav Kinsburskyb3d19c52012-08-20 18:00:46 +0400185 struct nfs_net *nn = net_generic(net, nfs_net_id);
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +0400186 int ret;
187
Stanislav Kinsburskyb3d19c52012-08-20 18:00:46 +0400188 if (nn->cb_users[minorversion]++)
189 return 0;
190
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +0400191 dprintk("NFS: create per-net callback data; net=%p\n", net);
192
193 ret = svc_bind(serv, net);
194 if (ret < 0) {
195 printk(KERN_WARNING "NFS: bind callback service failed\n");
196 goto err_bind;
197 }
198
Chuck Lever76566772015-10-24 17:28:32 -0400199 ret = -EPROTONOSUPPORT;
Arnd Bergmannd55b3522016-11-22 21:50:52 +0100200 if (!IS_ENABLED(CONFIG_NFS_V4_1) || minorversion == 0)
Chuck Lever76566772015-10-24 17:28:32 -0400201 ret = nfs4_callback_up_net(serv, net);
202 else if (xprt->ops->bc_up)
203 ret = xprt->ops->bc_up(serv, net);
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +0400204
205 if (ret < 0) {
206 printk(KERN_ERR "NFS: callback service start failed\n");
207 goto err_socks;
208 }
209 return 0;
210
211err_socks:
212 svc_rpcb_cleanup(serv, net);
213err_bind:
Trond Myklebust98b0f802016-08-29 11:15:36 -0400214 nn->cb_users[minorversion]--;
Stanislav Kinsbursky23c20ec2012-08-20 18:00:31 +0400215 dprintk("NFS: Couldn't create callback socket: err = %d; "
216 "net = %p\n", ret, net);
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +0400217 return ret;
218}
219
Trond Myklebustf4b52bb2016-08-29 20:03:48 -0400220static struct svc_serv_ops nfs40_cb_sv_ops = {
221 .svo_function = nfs4_callback_svc,
Jeff Laytonb9e13cd2015-06-08 12:06:51 -0700222 .svo_enqueue_xprt = svc_xprt_do_enqueue,
Trond Myklebustbb6aeba2016-08-29 20:03:51 -0400223 .svo_setup = svc_set_num_threads,
Trond Myklebustf4b52bb2016-08-29 20:03:48 -0400224 .svo_module = THIS_MODULE,
Jeff Laytonea126e72015-06-08 12:03:32 -0700225};
Trond Myklebustf4b52bb2016-08-29 20:03:48 -0400226#if defined(CONFIG_NFS_V4_1)
227static struct svc_serv_ops nfs41_cb_sv_ops = {
228 .svo_function = nfs41_callback_svc,
229 .svo_enqueue_xprt = svc_xprt_do_enqueue,
Trond Myklebustbb6aeba2016-08-29 20:03:51 -0400230 .svo_setup = svc_set_num_threads,
Trond Myklebustf4b52bb2016-08-29 20:03:48 -0400231 .svo_module = THIS_MODULE,
232};
233
234struct svc_serv_ops *nfs4_cb_sv_ops[] = {
235 [0] = &nfs40_cb_sv_ops,
236 [1] = &nfs41_cb_sv_ops,
237};
238#else
239struct svc_serv_ops *nfs4_cb_sv_ops[] = {
240 [0] = &nfs40_cb_sv_ops,
241 [1] = NULL,
242};
243#endif
Jeff Laytonea126e72015-06-08 12:03:32 -0700244
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400245static struct svc_serv *nfs_callback_create_svc(int minorversion)
246{
247 struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
248 struct svc_serv *serv;
Trond Myklebustf4b52bb2016-08-29 20:03:48 -0400249 struct svc_serv_ops *sv_ops;
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400250
251 /*
252 * Check whether we're already up and running.
253 */
Trond Myklebust3b01c112016-08-29 20:03:50 -0400254 if (cb_info->serv) {
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400255 /*
256 * Note: increase service usage, because later in case of error
257 * svc_destroy() will be called.
258 */
259 svc_get(cb_info->serv);
260 return cb_info->serv;
261 }
262
Trond Myklebustf4b52bb2016-08-29 20:03:48 -0400263 switch (minorversion) {
264 case 0:
265 sv_ops = nfs4_cb_sv_ops[0];
266 break;
267 default:
268 sv_ops = nfs4_cb_sv_ops[1];
269 }
270
271 if (sv_ops == NULL)
272 return ERR_PTR(-ENOTSUPP);
273
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400274 /*
275 * Sanity check: if there's no task,
276 * we should be the first user ...
277 */
278 if (cb_info->users)
279 printk(KERN_WARNING "nfs_callback_create_svc: no kthread, %d users??\n",
280 cb_info->users);
281
Kinglong Mee4ebe28d2017-04-27 11:13:38 +0800282 serv = svc_create_pooled(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, sv_ops);
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400283 if (!serv) {
284 printk(KERN_ERR "nfs_callback_create_svc: create service failed\n");
285 return ERR_PTR(-ENOMEM);
286 }
Trond Myklebust3b01c112016-08-29 20:03:50 -0400287 cb_info->serv = serv;
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400288 /* As there is only one thread we need to over-ride the
289 * default maximum of 80 connections
290 */
291 serv->sv_maxconn = 1024;
292 dprintk("nfs_callback_create_svc: service created\n");
293 return serv;
294}
295
Benny Halevy71468512009-04-01 09:22:56 -0400296/*
297 * Bring up the callback thread if it is not already up.
298 */
299int nfs_callback_up(u32 minorversion, struct rpc_xprt *xprt)
300{
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400301 struct svc_serv *serv;
Andy Adamsone82dc222009-04-01 09:23:14 -0400302 struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
Stanislav Kinsbursky23c20ec2012-08-20 18:00:31 +0400303 int ret;
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +0400304 struct net *net = xprt->xprt_net;
Benny Halevy71468512009-04-01 09:22:56 -0400305
306 mutex_lock(&nfs_callback_mutex);
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400307
308 serv = nfs_callback_create_svc(minorversion);
309 if (IS_ERR(serv)) {
310 ret = PTR_ERR(serv);
311 goto err_create;
312 }
313
Chuck Lever76566772015-10-24 17:28:32 -0400314 ret = nfs_callback_up_net(minorversion, serv, net, xprt);
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +0400315 if (ret < 0)
316 goto err_net;
Stanislav Kinsbursky9793f7c2012-05-02 16:08:38 +0400317
Stanislav Kinsbursky8e246142012-08-20 18:00:26 +0400318 ret = nfs_callback_start_svc(minorversion, xprt, serv);
319 if (ret < 0)
320 goto err_start;
Stanislav Kinsbursky691c4572012-08-20 18:00:21 +0400321
Stanislav Kinsbursky23c20ec2012-08-20 18:00:31 +0400322 cb_info->users++;
Jeff Layton8e600292008-02-11 10:00:20 -0500323 /*
324 * svc_create creates the svc_serv with sv_nrthreads == 1, and then
Jeff Laytona277e332008-02-20 08:55:30 -0500325 * svc_prepare_thread increments that. So we need to call svc_destroy
Jeff Layton8e600292008-02-11 10:00:20 -0500326 * on both success and failure so that the refcount is 1 when the
327 * thread exits.
328 */
Stanislav Kinsbursky23c20ec2012-08-20 18:00:31 +0400329err_net:
Trond Myklebust3b01c112016-08-29 20:03:50 -0400330 if (!cb_info->users)
331 cb_info->serv = NULL;
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400332 svc_destroy(serv);
333err_create:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800334 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return ret;
Stanislav Kinsbursky8e246142012-08-20 18:00:26 +0400336
337err_start:
Stanislav Kinsburskyb3d19c52012-08-20 18:00:46 +0400338 nfs_callback_down_net(minorversion, serv, net);
Stanislav Kinsbursky23c20ec2012-08-20 18:00:31 +0400339 dprintk("NFS: Couldn't create server thread; err = %d\n", ret);
340 goto err_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343/*
Jeff Layton5afc5972008-06-11 10:03:11 -0400344 * Kill the callback thread if it's no longer being used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 */
Stanislav Kinsburskyc8ceb412012-08-20 18:00:06 +0400346void nfs_callback_down(int minorversion, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Andy Adamsone82dc222009-04-01 09:23:14 -0400348 struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
Trond Myklebustbb6aeba2016-08-29 20:03:51 -0400349 struct svc_serv *serv;
Andy Adamsone82dc222009-04-01 09:23:14 -0400350
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800351 mutex_lock(&nfs_callback_mutex);
Trond Myklebustbb6aeba2016-08-29 20:03:51 -0400352 serv = cb_info->serv;
353 nfs_callback_down_net(minorversion, serv, net);
Andy Adamsone82dc222009-04-01 09:23:14 -0400354 cb_info->users--;
Trond Myklebust3b01c112016-08-29 20:03:50 -0400355 if (cb_info->users == 0) {
Trond Myklebustbb6aeba2016-08-29 20:03:51 -0400356 svc_get(serv);
357 serv->sv_ops->svo_setup(serv, NULL, 0);
358 svc_destroy(serv);
Stanislav Kinsbursky1dc42e02012-08-20 18:00:51 +0400359 dprintk("nfs_callback_down: service destroyed\n");
Andy Adamsone82dc222009-04-01 09:23:14 -0400360 cb_info->serv = NULL;
Jeff Layton5afc5972008-06-11 10:03:11 -0400361 }
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800362 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Andy Adamson778be232011-01-25 15:38:01 +0000365/* Boolean check of RPC_AUTH_GSS principal */
366int
367check_gss_callback_principal(struct nfs_client *clp, struct svc_rqst *rqstp)
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500368{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -0400369 char *p = rqstp->rq_cred.cr_principal;
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500370
Andy Adamson778be232011-01-25 15:38:01 +0000371 if (rqstp->rq_authop->flavour != RPC_AUTH_GSS)
372 return 1;
373
Andy Adamsonece0de62011-01-06 02:04:33 +0000374 /* No RPC_AUTH_GSS on NFSv4.1 back channel yet */
375 if (clp->cl_minorversion != 0)
Andy Adamson778be232011-01-25 15:38:01 +0000376 return 0;
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500377 /*
378 * It might just be a normal user principal, in which case
379 * userspace won't bother to tell us the name at all.
380 */
381 if (p == NULL)
Andy Adamson778be232011-01-25 15:38:01 +0000382 return 0;
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500383
Jeff Laytonf11b2a12014-06-21 20:52:17 -0400384 /*
385 * Did we get the acceptor from userland during the SETCLIENID
386 * negotiation?
387 */
388 if (clp->cl_acceptor)
389 return !strcmp(p, clp->cl_acceptor);
390
391 /*
392 * Otherwise try to verify it using the cl_hostname. Note that this
393 * doesn't work if a non-canonical hostname was used in the devname.
394 */
395
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500396 /* Expect a GSS_C_NT_HOSTBASED_NAME like "nfs@serverhostname" */
397
398 if (memcmp(p, "nfs@", 4) != 0)
Andy Adamson778be232011-01-25 15:38:01 +0000399 return 0;
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500400 p += 4;
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500401 if (strcmp(p, clp->cl_hostname) != 0)
Andy Adamson778be232011-01-25 15:38:01 +0000402 return 0;
403 return 1;
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500404}
405
Andy Adamson778be232011-01-25 15:38:01 +0000406/*
407 * pg_authenticate method for nfsv4 callback threads.
408 *
409 * The authflavor has been negotiated, so an incorrect flavor is a server
Jeff Layton6f02dc82015-06-04 18:40:13 -0400410 * bug. Deny packets with incorrect authflavor.
Andy Adamson778be232011-01-25 15:38:01 +0000411 *
412 * All other checking done after NFS decoding where the nfs_client can be
413 * found in nfs4_callback_compound
414 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415static int nfs_callback_authenticate(struct svc_rqst *rqstp)
416{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 switch (rqstp->rq_authop->flavour) {
Andy Adamson778be232011-01-25 15:38:01 +0000418 case RPC_AUTH_NULL:
419 if (rqstp->rq_proc != CB_NULL)
Jeff Layton6f02dc82015-06-04 18:40:13 -0400420 return SVC_DENIED;
Andy Adamson778be232011-01-25 15:38:01 +0000421 break;
422 case RPC_AUTH_GSS:
423 /* No RPC_AUTH_GSS support yet in NFSv4.1 */
424 if (svc_is_backchannel(rqstp))
Jeff Layton6f02dc82015-06-04 18:40:13 -0400425 return SVC_DENIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Andy Adamson778be232011-01-25 15:38:01 +0000427 return SVC_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430/*
431 * Define NFS4 callback program
432 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static struct svc_version *nfs4_callback_version[] = {
434 [1] = &nfs4_callback_version1,
Alexandros Batsakis07bccc22009-12-05 13:19:01 -0500435 [4] = &nfs4_callback_version4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436};
437
438static struct svc_stat nfs4_callback_stats;
439
440static struct svc_program nfs4_callback_program = {
441 .pg_prog = NFS4_CALLBACK, /* RPC service number */
442 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
443 .pg_vers = nfs4_callback_version, /* version table */
444 .pg_name = "NFSv4 callback", /* service name */
445 .pg_class = "nfs", /* authentication class */
446 .pg_stats = &nfs4_callback_stats,
447 .pg_authenticate = nfs_callback_authenticate,
448};