Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/callback.c |
| 3 | * |
| 4 | * Copyright (C) 2004 Trond Myklebust |
| 5 | * |
| 6 | * NFSv4 callback handling |
| 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 16 | #include <linux/mutex.h> |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 17 | #include <linux/freezer.h> |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 18 | #include <linux/kthread.h> |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 19 | #include <linux/sunrpc/svcauth_gss.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 20 | |
| 21 | #include <net/inet_sock.h> |
| 22 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 23 | #include "nfs4_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "callback.h" |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 25 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #define NFSDBG_FACILITY NFSDBG_CALLBACK |
| 28 | |
| 29 | struct nfs_callback_data { |
| 30 | unsigned int users; |
Jeff Layton | 5afc597 | 2008-06-11 10:03:11 -0400 | [diff] [blame] | 31 | struct svc_rqst *rqst; |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 32 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static struct nfs_callback_data nfs_callback_info; |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 36 | static DEFINE_MUTEX(nfs_callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | static struct svc_program nfs4_callback_program; |
| 38 | |
Trond Myklebust | a72b442 | 2006-01-03 09:55:41 +0100 | [diff] [blame] | 39 | unsigned int nfs_callback_set_tcpport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | unsigned short nfs_callback_tcpport; |
Chuck Lever | f738f51 | 2009-03-18 20:48:06 -0400 | [diff] [blame] | 41 | unsigned short nfs_callback_tcpport6; |
David Howells | 7d4e274 | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 42 | static const int nfs_set_port_min = 0; |
| 43 | static const int nfs_set_port_max = 65535; |
| 44 | |
| 45 | static int param_set_port(const char *val, struct kernel_param *kp) |
| 46 | { |
| 47 | char *endp; |
| 48 | int num = simple_strtol(val, &endp, 0); |
| 49 | if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max) |
| 50 | return -EINVAL; |
| 51 | *((int *)kp->arg) = num; |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | module_param_call(callback_tcpport, param_set_port, param_get_int, |
| 56 | &nfs_callback_set_tcpport, 0644); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * This is the callback kernel thread. |
| 60 | */ |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 61 | static int |
| 62 | nfs_callback_svc(void *vrqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Jeff Layton | 06e02d6 | 2008-04-08 15:40:07 -0400 | [diff] [blame] | 64 | int err, preverr = 0; |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 65 | struct svc_rqst *rqstp = vrqstp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 67 | set_freezable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 69 | /* |
| 70 | * FIXME: do we really need to run this under the BKL? If so, please |
| 71 | * add a comment about what it's intended to protect. |
| 72 | */ |
| 73 | lock_kernel(); |
| 74 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* |
| 76 | * Listen for a request on the socket |
| 77 | */ |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 78 | err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT); |
Jeff Layton | 06e02d6 | 2008-04-08 15:40:07 -0400 | [diff] [blame] | 79 | if (err == -EAGAIN || err == -EINTR) { |
| 80 | preverr = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
Jeff Layton | 06e02d6 | 2008-04-08 15:40:07 -0400 | [diff] [blame] | 83 | if (err < 0) { |
| 84 | if (err != preverr) { |
| 85 | printk(KERN_WARNING "%s: unexpected error " |
| 86 | "from svc_recv (%d)\n", __func__, err); |
| 87 | preverr = err; |
| 88 | } |
| 89 | schedule_timeout_uninterruptible(HZ); |
| 90 | continue; |
| 91 | } |
| 92 | preverr = err; |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 93 | svc_process(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | unlock_kernel(); |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 96 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
Jeff Layton | 5afc597 | 2008-06-11 10:03:11 -0400 | [diff] [blame] | 100 | * Bring up the callback thread if it is not already up. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | */ |
| 102 | int nfs_callback_up(void) |
| 103 | { |
Jeff Layton | 8e60029 | 2008-02-11 10:00:20 -0500 | [diff] [blame] | 104 | struct svc_serv *serv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | int ret = 0; |
| 106 | |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 107 | mutex_lock(&nfs_callback_mutex); |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 108 | if (nfs_callback_info.users++ || nfs_callback_info.task != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | goto out; |
Chuck Lever | 49a9072 | 2009-03-18 20:46:29 -0400 | [diff] [blame] | 110 | serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | ret = -ENOMEM; |
| 112 | if (!serv) |
| 113 | goto out_err; |
Chuck Lever | 482fb94 | 2007-02-12 00:53:29 -0800 | [diff] [blame] | 114 | |
Chuck Lever | 26298ca | 2009-03-18 20:46:36 -0400 | [diff] [blame] | 115 | ret = svc_create_xprt(serv, "tcp", PF_INET, |
Chuck Lever | 9652ada | 2009-03-18 20:46:21 -0400 | [diff] [blame] | 116 | nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS); |
Chuck Lever | 482fb94 | 2007-02-12 00:53:29 -0800 | [diff] [blame] | 117 | if (ret <= 0) |
Jeff Layton | 8e60029 | 2008-02-11 10:00:20 -0500 | [diff] [blame] | 118 | goto out_err; |
Chuck Lever | 482fb94 | 2007-02-12 00:53:29 -0800 | [diff] [blame] | 119 | nfs_callback_tcpport = ret; |
Chuck Lever | 18de973 | 2008-10-16 17:41:11 -0400 | [diff] [blame] | 120 | dprintk("NFS: Callback listener port = %u (af %u)\n", |
Chuck Lever | 26298ca | 2009-03-18 20:46:36 -0400 | [diff] [blame] | 121 | nfs_callback_tcpport, PF_INET); |
Chuck Lever | 482fb94 | 2007-02-12 00:53:29 -0800 | [diff] [blame] | 122 | |
Chuck Lever | f738f51 | 2009-03-18 20:48:06 -0400 | [diff] [blame] | 123 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 124 | ret = svc_create_xprt(serv, "tcp", PF_INET6, |
| 125 | nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS); |
| 126 | if (ret > 0) { |
| 127 | nfs_callback_tcpport6 = ret; |
| 128 | dprintk("NFS: Callback listener port = %u (af %u)\n", |
| 129 | nfs_callback_tcpport6, PF_INET6); |
Chuck Lever | 18fc316 | 2009-06-17 18:02:10 -0700 | [diff] [blame^] | 130 | } else if (ret == -EAFNOSUPPORT) |
| 131 | ret = 0; |
| 132 | else |
Chuck Lever | f738f51 | 2009-03-18 20:48:06 -0400 | [diff] [blame] | 133 | goto out_err; |
| 134 | #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */ |
| 135 | |
Jeff Layton | 5afc597 | 2008-06-11 10:03:11 -0400 | [diff] [blame] | 136 | nfs_callback_info.rqst = svc_prepare_thread(serv, &serv->sv_pools[0]); |
| 137 | if (IS_ERR(nfs_callback_info.rqst)) { |
| 138 | ret = PTR_ERR(nfs_callback_info.rqst); |
| 139 | nfs_callback_info.rqst = NULL; |
Jeff Layton | 8e60029 | 2008-02-11 10:00:20 -0500 | [diff] [blame] | 140 | goto out_err; |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | svc_sock_update_bufs(serv); |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 144 | |
Jeff Layton | 5afc597 | 2008-06-11 10:03:11 -0400 | [diff] [blame] | 145 | nfs_callback_info.task = kthread_run(nfs_callback_svc, |
| 146 | nfs_callback_info.rqst, |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 147 | "nfsv4-svc"); |
| 148 | if (IS_ERR(nfs_callback_info.task)) { |
| 149 | ret = PTR_ERR(nfs_callback_info.task); |
Jeff Layton | 5afc597 | 2008-06-11 10:03:11 -0400 | [diff] [blame] | 150 | svc_exit_thread(nfs_callback_info.rqst); |
| 151 | nfs_callback_info.rqst = NULL; |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 152 | nfs_callback_info.task = NULL; |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 153 | goto out_err; |
| 154 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | out: |
Jeff Layton | 8e60029 | 2008-02-11 10:00:20 -0500 | [diff] [blame] | 156 | /* |
| 157 | * svc_create creates the svc_serv with sv_nrthreads == 1, and then |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 158 | * svc_prepare_thread increments that. So we need to call svc_destroy |
Jeff Layton | 8e60029 | 2008-02-11 10:00:20 -0500 | [diff] [blame] | 159 | * on both success and failure so that the refcount is 1 when the |
| 160 | * thread exits. |
| 161 | */ |
| 162 | if (serv) |
| 163 | svc_destroy(serv); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 164 | mutex_unlock(&nfs_callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | return ret; |
Jeff Layton | 8e60029 | 2008-02-11 10:00:20 -0500 | [diff] [blame] | 166 | out_err: |
Chuck Lever | 18de973 | 2008-10-16 17:41:11 -0400 | [diff] [blame] | 167 | dprintk("NFS: Couldn't create callback socket or server thread; " |
| 168 | "err = %d\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | nfs_callback_info.users--; |
| 170 | goto out; |
| 171 | } |
| 172 | |
| 173 | /* |
Jeff Layton | 5afc597 | 2008-06-11 10:03:11 -0400 | [diff] [blame] | 174 | * Kill the callback thread if it's no longer being used. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | */ |
David Howells | 5ae1fbc | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 176 | void nfs_callback_down(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 178 | mutex_lock(&nfs_callback_mutex); |
Trond Myklebust | 1dd761e | 2006-03-20 13:44:49 -0500 | [diff] [blame] | 179 | nfs_callback_info.users--; |
Jeff Layton | 5afc597 | 2008-06-11 10:03:11 -0400 | [diff] [blame] | 180 | if (nfs_callback_info.users == 0 && nfs_callback_info.task != NULL) { |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 181 | kthread_stop(nfs_callback_info.task); |
Jeff Layton | 5afc597 | 2008-06-11 10:03:11 -0400 | [diff] [blame] | 182 | svc_exit_thread(nfs_callback_info.rqst); |
| 183 | nfs_callback_info.rqst = NULL; |
| 184 | nfs_callback_info.task = NULL; |
| 185 | } |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 186 | mutex_unlock(&nfs_callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 189 | static int check_gss_callback_principal(struct nfs_client *clp, |
| 190 | struct svc_rqst *rqstp) |
| 191 | { |
| 192 | struct rpc_clnt *r = clp->cl_rpcclient; |
| 193 | char *p = svc_gss_principal(rqstp); |
| 194 | |
| 195 | /* |
| 196 | * It might just be a normal user principal, in which case |
| 197 | * userspace won't bother to tell us the name at all. |
| 198 | */ |
| 199 | if (p == NULL) |
| 200 | return SVC_DENIED; |
| 201 | |
| 202 | /* Expect a GSS_C_NT_HOSTBASED_NAME like "nfs@serverhostname" */ |
| 203 | |
| 204 | if (memcmp(p, "nfs@", 4) != 0) |
| 205 | return SVC_DENIED; |
| 206 | p += 4; |
| 207 | if (strcmp(p, r->cl_server) != 0) |
| 208 | return SVC_DENIED; |
| 209 | return SVC_OK; |
| 210 | } |
| 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | static int nfs_callback_authenticate(struct svc_rqst *rqstp) |
| 213 | { |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 214 | struct nfs_client *clp; |
Pavel Emelyanov | 5216a8e | 2008-02-21 10:57:45 +0300 | [diff] [blame] | 215 | RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]); |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 216 | int ret = SVC_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
| 218 | /* Don't talk to strangers */ |
Chuck Lever | ff05264 | 2007-12-10 14:58:44 -0500 | [diff] [blame] | 219 | clp = nfs_find_client(svc_addr(rqstp), 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | if (clp == NULL) |
| 221 | return SVC_DROP; |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 222 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 223 | dprintk("%s: %s NFSv4 callback!\n", __func__, |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 224 | svc_print_addr(rqstp, buf, sizeof(buf))); |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | switch (rqstp->rq_authop->flavour) { |
| 227 | case RPC_AUTH_NULL: |
| 228 | if (rqstp->rq_proc != CB_NULL) |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 229 | ret = SVC_DENIED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | break; |
| 231 | case RPC_AUTH_UNIX: |
| 232 | break; |
| 233 | case RPC_AUTH_GSS: |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 234 | ret = check_gss_callback_principal(clp, rqstp); |
| 235 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | default: |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 237 | ret = SVC_DENIED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 239 | nfs_put_client(clp); |
| 240 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Define NFS4 callback program |
| 245 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | static struct svc_version *nfs4_callback_version[] = { |
| 247 | [1] = &nfs4_callback_version1, |
| 248 | }; |
| 249 | |
| 250 | static struct svc_stat nfs4_callback_stats; |
| 251 | |
| 252 | static struct svc_program nfs4_callback_program = { |
| 253 | .pg_prog = NFS4_CALLBACK, /* RPC service number */ |
| 254 | .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */ |
| 255 | .pg_vers = nfs4_callback_version, /* version table */ |
| 256 | .pg_name = "NFSv4 callback", /* service name */ |
| 257 | .pg_class = "nfs", /* authentication class */ |
| 258 | .pg_stats = &nfs4_callback_stats, |
| 259 | .pg_authenticate = nfs_callback_authenticate, |
| 260 | }; |