blob: 73a1f928226c05706bab82a37468973bff7b185c [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>
Ingo Molnar3f07c012017-02-08 18:51:30 +010012#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/sunrpc/svc.h>
14#include <linux/sunrpc/svcsock.h>
15#include <linux/nfs_fs.h>
Trond Myklebust758201e2012-10-01 16:33:18 -070016#include <linux/errno.h>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080017#include <linux/mutex.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070018#include <linux/freezer.h>
Jeff Laytona277e332008-02-20 08:55:30 -050019#include <linux/kthread.h>
Olga Kornievskaia945b34a2008-12-23 16:18:34 -050020#include <linux/sunrpc/svcauth_gss.h>
Ricardo Labiagaa43cde92009-04-01 09:23:08 -040021#include <linux/sunrpc/bc_xprt.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020022
23#include <net/inet_sock.h>
24
Trond Myklebust4ce79712005-06-22 17:16:21 +000025#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "callback.h"
David Howells24c8dbb2006-08-22 20:06:10 -040027#include "internal.h"
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +040028#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#define NFSDBG_FACILITY NFSDBG_CALLBACK
31
32struct nfs_callback_data {
33 unsigned int users;
Ricardo Labiagaa43cde92009-04-01 09:23:08 -040034 struct svc_serv *serv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
Andy Adamsone82dc222009-04-01 09:23:14 -040037static struct nfs_callback_data nfs_callback_info[NFS4_MAX_MINOR_VERSION + 1];
Ingo Molnar353ab6e2006-03-26 01:37:12 -080038static DEFINE_MUTEX(nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static struct svc_program nfs4_callback_program;
40
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +040041static int nfs4_callback_up_net(struct svc_serv *serv, struct net *net)
42{
43 int ret;
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +040044 struct nfs_net *nn = net_generic(net, nfs_net_id);
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +040045
46 ret = svc_create_xprt(serv, "tcp", net, PF_INET,
47 nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS);
48 if (ret <= 0)
49 goto out_err;
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +040050 nn->nfs_callback_tcpport = ret;
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +040051 dprintk("NFS: Callback listener port = %u (af %u, net %p)\n",
Stanislav Kinsburskybbe0a3a2012-08-20 18:00:36 +040052 nn->nfs_callback_tcpport, PF_INET, net);
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +040053
54 ret = svc_create_xprt(serv, "tcp", net, PF_INET6,
55 nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS);
56 if (ret > 0) {
Stanislav Kinsbursky29dcc162012-08-20 18:00:41 +040057 nn->nfs_callback_tcpport6 = ret;
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +040058 dprintk("NFS: Callback listener port = %u (af %u, net %p)\n",
Stanislav Kinsbursky29dcc162012-08-20 18:00:41 +040059 nn->nfs_callback_tcpport6, PF_INET6, net);
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +040060 } else if (ret != -EAFNOSUPPORT)
61 goto out_err;
62 return 0;
63
64out_err:
65 return (ret) ? ret : -ENOMEM;
66}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
Andy Adamsone82dc222009-04-01 09:23:14 -040069 * This is the NFSv4 callback kernel thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 */
Jeff Laytona277e332008-02-20 08:55:30 -050071static int
Benny Halevy71468512009-04-01 09:22:56 -040072nfs4_callback_svc(void *vrqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
J. Bruce Fields5b444cc2012-08-17 21:47:53 -040074 int err;
Jeff Laytona277e332008-02-20 08:55:30 -050075 struct svc_rqst *rqstp = vrqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Rafael J. Wysocki83144182007-07-17 04:03:35 -070077 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Trond Myklebusted6473d2017-04-26 11:55:27 -040079 while (!kthread_freezable_should_stop(NULL)) {
80
81 if (signal_pending(current))
82 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 /*
84 * Listen for a request on the socket
85 */
NeilBrown6fb2b472006-10-02 02:17:50 -070086 err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
J. Bruce Fields5b444cc2012-08-17 21:47:53 -040087 if (err == -EAGAIN || err == -EINTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 continue;
NeilBrown6fb2b472006-10-02 02:17:50 -070089 svc_process(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Trond Myklebusted6473d2017-04-26 11:55:27 -040091 svc_exit_thread(rqstp);
92 module_put_and_exit(0);
Jeff Laytona277e332008-02-20 08:55:30 -050093 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Ricardo Labiagaa43cde92009-04-01 09:23:08 -040096#if defined(CONFIG_NFS_V4_1)
97/*
98 * The callback service for NFSv4.1 callbacks
99 */
100static int
101nfs41_callback_svc(void *vrqstp)
102{
103 struct svc_rqst *rqstp = vrqstp;
104 struct svc_serv *serv = rqstp->rq_server;
105 struct rpc_rqst *req;
106 int error;
107 DEFINE_WAIT(wq);
108
109 set_freezable();
110
Trond Myklebusted6473d2017-04-26 11:55:27 -0400111 while (!kthread_freezable_should_stop(NULL)) {
112
113 if (signal_pending(current))
114 flush_signals(current);
Jeff Layton25d280a2013-03-25 07:59:57 -0400115
Jeff Layton5d05e542015-03-20 15:15:14 -0400116 prepare_to_wait(&serv->sv_cb_waitq, &wq, TASK_INTERRUPTIBLE);
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400117 spin_lock_bh(&serv->sv_cb_lock);
118 if (!list_empty(&serv->sv_cb_list)) {
119 req = list_first_entry(&serv->sv_cb_list,
120 struct rpc_rqst, rq_bc_list);
121 list_del(&req->rq_bc_list);
122 spin_unlock_bh(&serv->sv_cb_lock);
Jeff Layton6ffa30d2015-01-14 13:08:57 -0500123 finish_wait(&serv->sv_cb_waitq, &wq);
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400124 dprintk("Invoking bc_svc_process()\n");
125 error = bc_svc_process(serv, req, rqstp);
126 dprintk("bc_svc_process() returned w/ error code= %d\n",
127 error);
128 } else {
129 spin_unlock_bh(&serv->sv_cb_lock);
Trond Myklebusted6473d2017-04-26 11:55:27 -0400130 if (!kthread_should_stop())
131 schedule();
Jeff Layton6ffa30d2015-01-14 13:08:57 -0500132 finish_wait(&serv->sv_cb_waitq, &wq);
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400133 }
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400134 }
Trond Myklebusted6473d2017-04-26 11:55:27 -0400135 svc_exit_thread(rqstp);
136 module_put_and_exit(0);
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400137 return 0;
138}
139
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400140static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt,
Stanislav Kinsbursky691c4572012-08-20 18:00:21 +0400141 struct svc_serv *serv)
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400142{
143 if (minorversion)
Stanislav Kinsbursky691c4572012-08-20 18:00:21 +0400144 /*
145 * Save the svc_serv in the transport so that it can
146 * be referenced when the session backchannel is initialized
147 */
148 xprt->bc_serv = serv;
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400149}
150#else
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400151static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt,
Stanislav Kinsbursky691c4572012-08-20 18:00:21 +0400152 struct svc_serv *serv)
Ricardo Labiagaa43cde92009-04-01 09:23:08 -0400153{
154}
155#endif /* CONFIG_NFS_V4_1 */
156
Stanislav Kinsbursky8e246142012-08-20 18:00:26 +0400157static int nfs_callback_start_svc(int minorversion, struct rpc_xprt *xprt,
158 struct svc_serv *serv)
159{
Trond Myklebust5405fc42016-08-29 20:03:52 -0400160 int nrservs = nfs_callback_nr_threads;
Stanislav Kinsbursky8e246142012-08-20 18:00:26 +0400161 int ret;
Stanislav Kinsbursky8e246142012-08-20 18:00:26 +0400162
163 nfs_callback_bc_serv(minorversion, xprt, serv);
164
Trond Myklebust5405fc42016-08-29 20:03:52 -0400165 if (nrservs < NFS4_MIN_NR_CALLBACK_THREADS)
166 nrservs = NFS4_MIN_NR_CALLBACK_THREADS;
167
Trond Myklebustbb6aeba2016-08-29 20:03:51 -0400168 if (serv->sv_nrthreads-1 == nrservs)
Stanislav Kinsbursky23c20ec2012-08-20 18:00:31 +0400169 return 0;
170
Trond Myklebustbb6aeba2016-08-29 20:03:51 -0400171 ret = serv->sv_ops->svo_setup(serv, NULL, nrservs);
172 if (ret) {
173 serv->sv_ops->svo_setup(serv, NULL, 0);
Trond Myklebuste9b7e912012-10-16 12:30:44 -0400174 return ret;
Stanislav Kinsbursky8e246142012-08-20 18:00:26 +0400175 }
176 dprintk("nfs_callback_up: service started\n");
177 return 0;
178}
179
Stanislav Kinsburskyb3d19c52012-08-20 18:00:46 +0400180static void nfs_callback_down_net(u32 minorversion, struct svc_serv *serv, struct net *net)
181{
182 struct nfs_net *nn = net_generic(net, nfs_net_id);
183
184 if (--nn->cb_users[minorversion])
185 return;
186
187 dprintk("NFS: destroy per-net callback data; net=%p\n", net);
188 svc_shutdown_net(serv, net);
189}
190
Chuck Lever76566772015-10-24 17:28:32 -0400191static int nfs_callback_up_net(int minorversion, struct svc_serv *serv,
192 struct net *net, struct rpc_xprt *xprt)
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +0400193{
Stanislav Kinsburskyb3d19c52012-08-20 18:00:46 +0400194 struct nfs_net *nn = net_generic(net, nfs_net_id);
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +0400195 int ret;
196
Stanislav Kinsburskyb3d19c52012-08-20 18:00:46 +0400197 if (nn->cb_users[minorversion]++)
198 return 0;
199
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +0400200 dprintk("NFS: create per-net callback data; net=%p\n", net);
201
202 ret = svc_bind(serv, net);
203 if (ret < 0) {
204 printk(KERN_WARNING "NFS: bind callback service failed\n");
205 goto err_bind;
206 }
207
Chuck Lever76566772015-10-24 17:28:32 -0400208 ret = -EPROTONOSUPPORT;
Arnd Bergmannd55b3522016-11-22 21:50:52 +0100209 if (!IS_ENABLED(CONFIG_NFS_V4_1) || minorversion == 0)
Chuck Lever76566772015-10-24 17:28:32 -0400210 ret = nfs4_callback_up_net(serv, net);
211 else if (xprt->ops->bc_up)
212 ret = xprt->ops->bc_up(serv, net);
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +0400213
214 if (ret < 0) {
215 printk(KERN_ERR "NFS: callback service start failed\n");
216 goto err_socks;
217 }
218 return 0;
219
220err_socks:
221 svc_rpcb_cleanup(serv, net);
222err_bind:
Trond Myklebust98b0f802016-08-29 11:15:36 -0400223 nn->cb_users[minorversion]--;
Stanislav Kinsbursky23c20ec2012-08-20 18:00:31 +0400224 dprintk("NFS: Couldn't create callback socket: err = %d; "
225 "net = %p\n", ret, net);
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +0400226 return ret;
227}
228
Trond Myklebustf4b52bb2016-08-29 20:03:48 -0400229static struct svc_serv_ops nfs40_cb_sv_ops = {
230 .svo_function = nfs4_callback_svc,
Jeff Laytonb9e13cd2015-06-08 12:06:51 -0700231 .svo_enqueue_xprt = svc_xprt_do_enqueue,
Trond Myklebusted6473d2017-04-26 11:55:27 -0400232 .svo_setup = svc_set_num_threads_sync,
Trond Myklebustf4b52bb2016-08-29 20:03:48 -0400233 .svo_module = THIS_MODULE,
Jeff Laytonea126e72015-06-08 12:03:32 -0700234};
Trond Myklebustf4b52bb2016-08-29 20:03:48 -0400235#if defined(CONFIG_NFS_V4_1)
236static struct svc_serv_ops nfs41_cb_sv_ops = {
237 .svo_function = nfs41_callback_svc,
238 .svo_enqueue_xprt = svc_xprt_do_enqueue,
Trond Myklebusted6473d2017-04-26 11:55:27 -0400239 .svo_setup = svc_set_num_threads_sync,
Trond Myklebustf4b52bb2016-08-29 20:03:48 -0400240 .svo_module = THIS_MODULE,
241};
242
Jason Yan05fae7b2017-03-10 10:48:13 +0800243static struct svc_serv_ops *nfs4_cb_sv_ops[] = {
Trond Myklebustf4b52bb2016-08-29 20:03:48 -0400244 [0] = &nfs40_cb_sv_ops,
245 [1] = &nfs41_cb_sv_ops,
246};
247#else
Jason Yan05fae7b2017-03-10 10:48:13 +0800248static struct svc_serv_ops *nfs4_cb_sv_ops[] = {
Trond Myklebustf4b52bb2016-08-29 20:03:48 -0400249 [0] = &nfs40_cb_sv_ops,
250 [1] = NULL,
251};
252#endif
Jeff Laytonea126e72015-06-08 12:03:32 -0700253
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400254static struct svc_serv *nfs_callback_create_svc(int minorversion)
255{
256 struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
257 struct svc_serv *serv;
Trond Myklebustf4b52bb2016-08-29 20:03:48 -0400258 struct svc_serv_ops *sv_ops;
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400259
260 /*
261 * Check whether we're already up and running.
262 */
Trond Myklebust3b01c112016-08-29 20:03:50 -0400263 if (cb_info->serv) {
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400264 /*
265 * Note: increase service usage, because later in case of error
266 * svc_destroy() will be called.
267 */
268 svc_get(cb_info->serv);
269 return cb_info->serv;
270 }
271
Trond Myklebustf4b52bb2016-08-29 20:03:48 -0400272 switch (minorversion) {
273 case 0:
274 sv_ops = nfs4_cb_sv_ops[0];
275 break;
276 default:
277 sv_ops = nfs4_cb_sv_ops[1];
278 }
279
280 if (sv_ops == NULL)
281 return ERR_PTR(-ENOTSUPP);
282
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400283 /*
284 * Sanity check: if there's no task,
285 * we should be the first user ...
286 */
287 if (cb_info->users)
288 printk(KERN_WARNING "nfs_callback_create_svc: no kthread, %d users??\n",
289 cb_info->users);
290
Kinglong Meedf807ff2017-04-27 11:13:38 +0800291 serv = svc_create_pooled(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, sv_ops);
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400292 if (!serv) {
293 printk(KERN_ERR "nfs_callback_create_svc: create service failed\n");
294 return ERR_PTR(-ENOMEM);
295 }
Trond Myklebust3b01c112016-08-29 20:03:50 -0400296 cb_info->serv = serv;
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400297 /* As there is only one thread we need to over-ride the
298 * default maximum of 80 connections
299 */
300 serv->sv_maxconn = 1024;
301 dprintk("nfs_callback_create_svc: service created\n");
302 return serv;
303}
304
Benny Halevy71468512009-04-01 09:22:56 -0400305/*
306 * Bring up the callback thread if it is not already up.
307 */
308int nfs_callback_up(u32 minorversion, struct rpc_xprt *xprt)
309{
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400310 struct svc_serv *serv;
Andy Adamsone82dc222009-04-01 09:23:14 -0400311 struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
Stanislav Kinsbursky23c20ec2012-08-20 18:00:31 +0400312 int ret;
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +0400313 struct net *net = xprt->xprt_net;
Benny Halevy71468512009-04-01 09:22:56 -0400314
315 mutex_lock(&nfs_callback_mutex);
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400316
317 serv = nfs_callback_create_svc(minorversion);
318 if (IS_ERR(serv)) {
319 ret = PTR_ERR(serv);
320 goto err_create;
321 }
322
Chuck Lever76566772015-10-24 17:28:32 -0400323 ret = nfs_callback_up_net(minorversion, serv, net, xprt);
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +0400324 if (ret < 0)
325 goto err_net;
Stanislav Kinsbursky9793f7c2012-05-02 16:08:38 +0400326
Stanislav Kinsbursky8e246142012-08-20 18:00:26 +0400327 ret = nfs_callback_start_svc(minorversion, xprt, serv);
328 if (ret < 0)
329 goto err_start;
Stanislav Kinsbursky691c4572012-08-20 18:00:21 +0400330
Stanislav Kinsbursky23c20ec2012-08-20 18:00:31 +0400331 cb_info->users++;
Jeff Layton8e600292008-02-11 10:00:20 -0500332 /*
333 * svc_create creates the svc_serv with sv_nrthreads == 1, and then
Jeff Laytona277e332008-02-20 08:55:30 -0500334 * svc_prepare_thread increments that. So we need to call svc_destroy
Jeff Layton8e600292008-02-11 10:00:20 -0500335 * on both success and failure so that the refcount is 1 when the
336 * thread exits.
337 */
Stanislav Kinsbursky23c20ec2012-08-20 18:00:31 +0400338err_net:
Trond Myklebust3b01c112016-08-29 20:03:50 -0400339 if (!cb_info->users)
340 cb_info->serv = NULL;
Stanislav Kinsburskydd018422012-08-20 18:00:11 +0400341 svc_destroy(serv);
342err_create:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800343 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return ret;
Stanislav Kinsbursky8e246142012-08-20 18:00:26 +0400345
346err_start:
Stanislav Kinsburskyb3d19c52012-08-20 18:00:46 +0400347 nfs_callback_down_net(minorversion, serv, net);
Stanislav Kinsbursky23c20ec2012-08-20 18:00:31 +0400348 dprintk("NFS: Couldn't create server thread; err = %d\n", ret);
349 goto err_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352/*
Jeff Layton5afc5972008-06-11 10:03:11 -0400353 * Kill the callback thread if it's no longer being used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
Stanislav Kinsburskyc8ceb412012-08-20 18:00:06 +0400355void nfs_callback_down(int minorversion, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Andy Adamsone82dc222009-04-01 09:23:14 -0400357 struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
Trond Myklebustbb6aeba2016-08-29 20:03:51 -0400358 struct svc_serv *serv;
Andy Adamsone82dc222009-04-01 09:23:14 -0400359
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800360 mutex_lock(&nfs_callback_mutex);
Trond Myklebustbb6aeba2016-08-29 20:03:51 -0400361 serv = cb_info->serv;
362 nfs_callback_down_net(minorversion, serv, net);
Andy Adamsone82dc222009-04-01 09:23:14 -0400363 cb_info->users--;
Trond Myklebust3b01c112016-08-29 20:03:50 -0400364 if (cb_info->users == 0) {
Trond Myklebustbb6aeba2016-08-29 20:03:51 -0400365 svc_get(serv);
366 serv->sv_ops->svo_setup(serv, NULL, 0);
367 svc_destroy(serv);
Stanislav Kinsbursky1dc42e02012-08-20 18:00:51 +0400368 dprintk("nfs_callback_down: service destroyed\n");
Andy Adamsone82dc222009-04-01 09:23:14 -0400369 cb_info->serv = NULL;
Jeff Layton5afc5972008-06-11 10:03:11 -0400370 }
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800371 mutex_unlock(&nfs_callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Andy Adamson778be232011-01-25 15:38:01 +0000374/* Boolean check of RPC_AUTH_GSS principal */
375int
376check_gss_callback_principal(struct nfs_client *clp, struct svc_rqst *rqstp)
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500377{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -0400378 char *p = rqstp->rq_cred.cr_principal;
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500379
Andy Adamson778be232011-01-25 15:38:01 +0000380 if (rqstp->rq_authop->flavour != RPC_AUTH_GSS)
381 return 1;
382
Andy Adamsonece0de62011-01-06 02:04:33 +0000383 /* No RPC_AUTH_GSS on NFSv4.1 back channel yet */
384 if (clp->cl_minorversion != 0)
Andy Adamson778be232011-01-25 15:38:01 +0000385 return 0;
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500386 /*
387 * It might just be a normal user principal, in which case
388 * userspace won't bother to tell us the name at all.
389 */
390 if (p == NULL)
Andy Adamson778be232011-01-25 15:38:01 +0000391 return 0;
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500392
Jeff Laytonf11b2a12014-06-21 20:52:17 -0400393 /*
394 * Did we get the acceptor from userland during the SETCLIENID
395 * negotiation?
396 */
397 if (clp->cl_acceptor)
398 return !strcmp(p, clp->cl_acceptor);
399
400 /*
401 * Otherwise try to verify it using the cl_hostname. Note that this
402 * doesn't work if a non-canonical hostname was used in the devname.
403 */
404
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500405 /* Expect a GSS_C_NT_HOSTBASED_NAME like "nfs@serverhostname" */
406
407 if (memcmp(p, "nfs@", 4) != 0)
Andy Adamson778be232011-01-25 15:38:01 +0000408 return 0;
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500409 p += 4;
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500410 if (strcmp(p, clp->cl_hostname) != 0)
Andy Adamson778be232011-01-25 15:38:01 +0000411 return 0;
412 return 1;
Olga Kornievskaia945b34a2008-12-23 16:18:34 -0500413}
414
Andy Adamson778be232011-01-25 15:38:01 +0000415/*
416 * pg_authenticate method for nfsv4 callback threads.
417 *
418 * The authflavor has been negotiated, so an incorrect flavor is a server
Jeff Layton6f02dc82015-06-04 18:40:13 -0400419 * bug. Deny packets with incorrect authflavor.
Andy Adamson778be232011-01-25 15:38:01 +0000420 *
421 * All other checking done after NFS decoding where the nfs_client can be
422 * found in nfs4_callback_compound
423 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424static int nfs_callback_authenticate(struct svc_rqst *rqstp)
425{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 switch (rqstp->rq_authop->flavour) {
Andy Adamson778be232011-01-25 15:38:01 +0000427 case RPC_AUTH_NULL:
428 if (rqstp->rq_proc != CB_NULL)
Jeff Layton6f02dc82015-06-04 18:40:13 -0400429 return SVC_DENIED;
Andy Adamson778be232011-01-25 15:38:01 +0000430 break;
431 case RPC_AUTH_GSS:
432 /* No RPC_AUTH_GSS support yet in NFSv4.1 */
433 if (svc_is_backchannel(rqstp))
Jeff Layton6f02dc82015-06-04 18:40:13 -0400434 return SVC_DENIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Andy Adamson778be232011-01-25 15:38:01 +0000436 return SVC_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439/*
440 * Define NFS4 callback program
441 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442static struct svc_version *nfs4_callback_version[] = {
443 [1] = &nfs4_callback_version1,
Alexandros Batsakis07bccc22009-12-05 13:19:01 -0500444 [4] = &nfs4_callback_version4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445};
446
447static struct svc_stat nfs4_callback_stats;
448
449static struct svc_program nfs4_callback_program = {
450 .pg_prog = NFS4_CALLBACK, /* RPC service number */
451 .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
452 .pg_vers = nfs4_callback_version, /* version table */
453 .pg_name = "NFSv4 callback", /* service name */
454 .pg_class = "nfs", /* authentication class */
455 .pg_stats = &nfs4_callback_stats,
456 .pg_authenticate = nfs_callback_authenticate,
457};