blob: fe06acd6029b1ed9bcf7a7ce5ec2ebba7c2da3a2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * linux/net/sunrpc/auth_gss/auth_gss.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * RPCSEC_GSS client authentication.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (c) 2000 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Dug Song <dugsong@monkey.org>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
37
38
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/types.h>
42#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sched.h>
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -040044#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/sunrpc/auth.h>
47#include <linux/sunrpc/auth_gss.h>
48#include <linux/sunrpc/svcauth_gss.h>
49#include <linux/sunrpc/gss_err.h>
50#include <linux/workqueue.h>
51#include <linux/sunrpc/rpc_pipe_fs.h>
52#include <linux/sunrpc/gss_api.h>
53#include <asm/uaccess.h>
54
Trond Myklebustf1c0a862007-06-23 20:17:58 -040055static const struct rpc_authops authgss_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Trond Myklebustf1c0a862007-06-23 20:17:58 -040057static const struct rpc_credops gss_credops;
Trond Myklebust0df7fb72007-06-26 17:04:57 -040058static const struct rpc_credops gss_nullops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#ifdef RPC_DEBUG
61# define RPCDBG_FACILITY RPCDBG_AUTH
62#endif
63
\\\"J. Bruce Fields\\\d25a03c2008-06-09 16:51:34 -040064#define GSS_CRED_SLACK 1024
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* length of a krb5 verifier (48), plus data added before arguments when
66 * using integrity (two 4-byte integers): */
Olga Kornievskaiaadeb8132006-12-04 20:22:34 -050067#define GSS_VERF_SLACK 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069struct gss_auth {
Trond Myklebust0285ed12007-06-27 14:29:12 -040070 struct kref kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 struct rpc_auth rpc_auth;
72 struct gss_api_mech *mech;
73 enum rpc_gss_svc service;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 struct rpc_clnt *client;
75 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -050078/* pipe_version >= 0 if and only if someone has a pipe open. */
79static int pipe_version = -1;
\"J. Bruce Fields\cf819392008-12-23 16:10:19 -050080static atomic_t pipe_users = ATOMIC_INIT(0);
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -050081static DEFINE_SPINLOCK(pipe_version_lock);
82static struct rpc_wait_queue pipe_version_rpc_waitqueue;
83static DECLARE_WAIT_QUEUE_HEAD(pipe_version_waitqueue);
\"J. Bruce Fields\cf819392008-12-23 16:10:19 -050084
Trond Myklebust5d28dc82007-06-26 19:18:38 -040085static void gss_free_ctx(struct gss_cl_ctx *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static struct rpc_pipe_ops gss_upcall_ops;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static inline struct gss_cl_ctx *
89gss_get_ctx(struct gss_cl_ctx *ctx)
90{
91 atomic_inc(&ctx->count);
92 return ctx;
93}
94
95static inline void
96gss_put_ctx(struct gss_cl_ctx *ctx)
97{
98 if (atomic_dec_and_test(&ctx->count))
Trond Myklebust5d28dc82007-06-26 19:18:38 -040099 gss_free_ctx(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400102/* gss_cred_set_ctx:
103 * called by gss_upcall_callback and gss_create_upcall in order
104 * to set the gss context. The actual exchange of an old context
105 * and a new one is protected by the inode->i_lock.
106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static void
108gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
109{
110 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400111
Trond Myklebustcd019f72008-04-17 17:03:58 -0400112 if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
113 return;
Trond Myklebust7b6962b2008-04-17 16:53:01 -0400114 gss_get_ctx(ctx);
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400115 rcu_assign_pointer(gss_cred->gc_ctx, ctx);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400116 set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Trond Myklebustcd019f72008-04-17 17:03:58 -0400117 smp_mb__before_clear_bit();
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400118 clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121static const void *
122simple_get_bytes(const void *p, const void *end, void *res, size_t len)
123{
124 const void *q = (const void *)((const char *)p + len);
125 if (unlikely(q > end || q < p))
126 return ERR_PTR(-EFAULT);
127 memcpy(res, p, len);
128 return q;
129}
130
131static inline const void *
132simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
133{
134 const void *q;
135 unsigned int len;
136
137 p = simple_get_bytes(p, end, &len, sizeof(len));
138 if (IS_ERR(p))
139 return p;
140 q = (const void *)((const char *)p + len);
141 if (unlikely(q > end || q < p))
142 return ERR_PTR(-EFAULT);
Trond Myklebust0f38b872008-06-10 18:31:01 -0400143 dest->data = kmemdup(p, len, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (unlikely(dest->data == NULL))
145 return ERR_PTR(-ENOMEM);
146 dest->len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return q;
148}
149
150static struct gss_cl_ctx *
151gss_cred_get_ctx(struct rpc_cred *cred)
152{
153 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
154 struct gss_cl_ctx *ctx = NULL;
155
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400156 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (gss_cred->gc_ctx)
158 ctx = gss_get_ctx(gss_cred->gc_ctx);
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400159 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return ctx;
161}
162
163static struct gss_cl_ctx *
164gss_alloc_context(void)
165{
166 struct gss_cl_ctx *ctx;
167
Trond Myklebust0f38b872008-06-10 18:31:01 -0400168 ctx = kzalloc(sizeof(*ctx), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (ctx != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 ctx->gc_proc = RPC_GSS_PROC_DATA;
171 ctx->gc_seq = 1; /* NetApp 6.4R1 doesn't accept seq. no. 0 */
172 spin_lock_init(&ctx->gc_seq_lock);
173 atomic_set(&ctx->count,1);
174 }
175 return ctx;
176}
177
178#define GSSD_MIN_TIMEOUT (60 * 60)
179static const void *
180gss_fill_context(const void *p, const void *end, struct gss_cl_ctx *ctx, struct gss_api_mech *gm)
181{
182 const void *q;
183 unsigned int seclen;
184 unsigned int timeout;
185 u32 window_size;
186 int ret;
187
188 /* First unsigned int gives the lifetime (in seconds) of the cred */
189 p = simple_get_bytes(p, end, &timeout, sizeof(timeout));
190 if (IS_ERR(p))
191 goto err;
192 if (timeout == 0)
193 timeout = GSSD_MIN_TIMEOUT;
194 ctx->gc_expiry = jiffies + (unsigned long)timeout * HZ * 3 / 4;
195 /* Sequence number window. Determines the maximum number of simultaneous requests */
196 p = simple_get_bytes(p, end, &window_size, sizeof(window_size));
197 if (IS_ERR(p))
198 goto err;
199 ctx->gc_win = window_size;
200 /* gssd signals an error by passing ctx->gc_win = 0: */
201 if (ctx->gc_win == 0) {
202 /* in which case, p points to an error code which we ignore */
203 p = ERR_PTR(-EACCES);
204 goto err;
205 }
206 /* copy the opaque wire context */
207 p = simple_get_netobj(p, end, &ctx->gc_wire_ctx);
208 if (IS_ERR(p))
209 goto err;
210 /* import the opaque security context */
211 p = simple_get_bytes(p, end, &seclen, sizeof(seclen));
212 if (IS_ERR(p))
213 goto err;
214 q = (const void *)((const char *)p + seclen);
215 if (unlikely(q > end || q < p)) {
216 p = ERR_PTR(-EFAULT);
217 goto err;
218 }
219 ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx);
220 if (ret < 0) {
221 p = ERR_PTR(ret);
222 goto err;
223 }
224 return q;
225err:
Chuck Lever8885cb32007-01-31 12:14:05 -0500226 dprintk("RPC: gss_fill_context returning %ld\n", -PTR_ERR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return p;
228}
229
230
231struct gss_upcall_msg {
232 atomic_t count;
233 uid_t uid;
234 struct rpc_pipe_msg msg;
235 struct list_head list;
236 struct gss_auth *auth;
\"J. Bruce Fields\5b7ddd42008-12-23 16:15:44 -0500237 struct rpc_inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 struct rpc_wait_queue rpc_waitqueue;
239 wait_queue_head_t waitqueue;
240 struct gss_cl_ctx *ctx;
241};
242
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500243static int get_pipe_version(void)
244{
245 int ret;
246
247 spin_lock(&pipe_version_lock);
248 if (pipe_version >= 0) {
249 atomic_inc(&pipe_users);
250 ret = 0;
251 } else
252 ret = -EAGAIN;
253 spin_unlock(&pipe_version_lock);
254 return ret;
255}
256
257static void put_pipe_version(void)
258{
259 if (atomic_dec_and_lock(&pipe_users, &pipe_version_lock)) {
260 pipe_version = -1;
261 spin_unlock(&pipe_version_lock);
262 }
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static void
266gss_release_msg(struct gss_upcall_msg *gss_msg)
267{
268 if (!atomic_dec_and_test(&gss_msg->count))
269 return;
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500270 put_pipe_version();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 BUG_ON(!list_empty(&gss_msg->list));
272 if (gss_msg->ctx != NULL)
273 gss_put_ctx(gss_msg->ctx);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500274 rpc_destroy_wait_queue(&gss_msg->rpc_waitqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 kfree(gss_msg);
276}
277
278static struct gss_upcall_msg *
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400279__gss_find_upcall(struct rpc_inode *rpci, uid_t uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct gss_upcall_msg *pos;
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400282 list_for_each_entry(pos, &rpci->in_downcall, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (pos->uid != uid)
284 continue;
285 atomic_inc(&pos->count);
Chuck Lever8885cb32007-01-31 12:14:05 -0500286 dprintk("RPC: gss_find_upcall found msg %p\n", pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return pos;
288 }
Chuck Lever8885cb32007-01-31 12:14:05 -0500289 dprintk("RPC: gss_find_upcall found nothing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return NULL;
291}
292
\\\"J. Bruce Fields\\\720b8f22008-06-09 16:51:33 -0400293/* Try to add an upcall to the pipefs queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * If an upcall owned by our uid already exists, then we return a reference
295 * to that upcall instead of adding the new upcall.
296 */
297static inline struct gss_upcall_msg *
298gss_add_msg(struct gss_auth *gss_auth, struct gss_upcall_msg *gss_msg)
299{
\"J. Bruce Fields\5b7ddd42008-12-23 16:15:44 -0500300 struct rpc_inode *rpci = gss_msg->inode;
301 struct inode *inode = &rpci->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct gss_upcall_msg *old;
303
Trond Myklebustb185f832007-06-07 10:14:14 -0400304 spin_lock(&inode->i_lock);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400305 old = __gss_find_upcall(rpci, gss_msg->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (old == NULL) {
307 atomic_inc(&gss_msg->count);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400308 list_add(&gss_msg->list, &rpci->in_downcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 } else
310 gss_msg = old;
Trond Myklebustb185f832007-06-07 10:14:14 -0400311 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return gss_msg;
313}
314
315static void
316__gss_unhash_msg(struct gss_upcall_msg *gss_msg)
317{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 list_del_init(&gss_msg->list);
319 rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
320 wake_up_all(&gss_msg->waitqueue);
321 atomic_dec(&gss_msg->count);
322}
323
324static void
325gss_unhash_msg(struct gss_upcall_msg *gss_msg)
326{
\"J. Bruce Fields\5b7ddd42008-12-23 16:15:44 -0500327 struct inode *inode = &gss_msg->inode->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400329 if (list_empty(&gss_msg->list))
330 return;
Trond Myklebustb185f832007-06-07 10:14:14 -0400331 spin_lock(&inode->i_lock);
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400332 if (!list_empty(&gss_msg->list))
333 __gss_unhash_msg(gss_msg);
Trond Myklebustb185f832007-06-07 10:14:14 -0400334 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337static void
338gss_upcall_callback(struct rpc_task *task)
339{
340 struct gss_cred *gss_cred = container_of(task->tk_msg.rpc_cred,
341 struct gss_cred, gc_base);
342 struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
\"J. Bruce Fields\5b7ddd42008-12-23 16:15:44 -0500343 struct inode *inode = &gss_msg->inode->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400345 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (gss_msg->ctx)
Trond Myklebust7b6962b2008-04-17 16:53:01 -0400347 gss_cred_set_ctx(task->tk_msg.rpc_cred, gss_msg->ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 else
349 task->tk_status = gss_msg->msg.errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 gss_cred->gc_upcall = NULL;
351 rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
Trond Myklebustb185f832007-06-07 10:14:14 -0400352 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 gss_release_msg(gss_msg);
354}
355
356static inline struct gss_upcall_msg *
357gss_alloc_msg(struct gss_auth *gss_auth, uid_t uid)
358{
359 struct gss_upcall_msg *gss_msg;
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500360 int vers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Trond Myklebust0f38b872008-06-10 18:31:01 -0400362 gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
\"J. Bruce Fields\db75b3d2008-12-23 16:07:13 -0500363 if (gss_msg == NULL)
364 return ERR_PTR(-ENOMEM);
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500365 vers = get_pipe_version();
366 if (vers < 0) {
367 kfree(gss_msg);
368 return ERR_PTR(vers);
369 }
\"J. Bruce Fields\5b7ddd42008-12-23 16:15:44 -0500370 gss_msg->inode = RPC_I(gss_auth->dentry->d_inode);
\"J. Bruce Fields\db75b3d2008-12-23 16:07:13 -0500371 INIT_LIST_HEAD(&gss_msg->list);
372 rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
373 init_waitqueue_head(&gss_msg->waitqueue);
374 atomic_set(&gss_msg->count, 1);
375 gss_msg->msg.data = &gss_msg->uid;
376 gss_msg->msg.len = sizeof(gss_msg->uid);
377 gss_msg->uid = uid;
378 gss_msg->auth = gss_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return gss_msg;
380}
381
382static struct gss_upcall_msg *
383gss_setup_upcall(struct rpc_clnt *clnt, struct gss_auth *gss_auth, struct rpc_cred *cred)
384{
Trond Myklebust7c67db32008-04-07 20:50:11 -0400385 struct gss_cred *gss_cred = container_of(cred,
386 struct gss_cred, gc_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct gss_upcall_msg *gss_new, *gss_msg;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400388 uid_t uid = cred->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Trond Myklebust7c67db32008-04-07 20:50:11 -0400390 /* Special case: rpc.gssd assumes that uid == 0 implies machine creds */
391 if (gss_cred->gc_machine_cred != 0)
392 uid = 0;
393
394 gss_new = gss_alloc_msg(gss_auth, uid);
\"J. Bruce Fields\db75b3d2008-12-23 16:07:13 -0500395 if (IS_ERR(gss_new))
396 return gss_new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 gss_msg = gss_add_msg(gss_auth, gss_new);
398 if (gss_msg == gss_new) {
\"J. Bruce Fields\5b7ddd42008-12-23 16:15:44 -0500399 struct inode *inode = &gss_new->inode->vfs_inode;
400 int res = rpc_queue_upcall(inode, &gss_new->msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (res) {
402 gss_unhash_msg(gss_new);
403 gss_msg = ERR_PTR(res);
404 }
405 } else
406 gss_release_msg(gss_new);
407 return gss_msg;
408}
409
\"J. Bruce Fields\b03568c2008-12-23 16:06:55 -0500410static void warn_gssd(void)
411{
412 static unsigned long ratelimit;
413 unsigned long now = jiffies;
414
415 if (time_after(now, ratelimit)) {
416 printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
417 "Please check user daemon is running.\n");
418 ratelimit = now + 15*HZ;
419 }
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422static inline int
423gss_refresh_upcall(struct rpc_task *task)
424{
425 struct rpc_cred *cred = task->tk_msg.rpc_cred;
Trond Myklebust4a8c1342007-06-07 10:14:14 -0400426 struct gss_auth *gss_auth = container_of(cred->cr_auth,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct gss_auth, rpc_auth);
428 struct gss_cred *gss_cred = container_of(cred,
429 struct gss_cred, gc_base);
430 struct gss_upcall_msg *gss_msg;
\"J. Bruce Fields\5b7ddd42008-12-23 16:15:44 -0500431 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int err = 0;
433
Chuck Lever8885cb32007-01-31 12:14:05 -0500434 dprintk("RPC: %5u gss_refresh_upcall for uid %u\n", task->tk_pid,
435 cred->cr_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 gss_msg = gss_setup_upcall(task->tk_client, gss_auth, cred);
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500437 if (IS_ERR(gss_msg) == -EAGAIN) {
438 /* XXX: warning on the first, under the assumption we
439 * shouldn't normally hit this case on a refresh. */
440 warn_gssd();
441 task->tk_timeout = 15*HZ;
442 rpc_sleep_on(&pipe_version_rpc_waitqueue, task, NULL);
443 return 0;
444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (IS_ERR(gss_msg)) {
446 err = PTR_ERR(gss_msg);
447 goto out;
448 }
\"J. Bruce Fields\5b7ddd42008-12-23 16:15:44 -0500449 inode = &gss_msg->inode->vfs_inode;
Trond Myklebustb185f832007-06-07 10:14:14 -0400450 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (gss_cred->gc_upcall != NULL)
Trond Myklebust5d008372008-02-22 16:34:17 -0500452 rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL);
Trond Myklebust7b6962b2008-04-17 16:53:01 -0400453 else if (gss_msg->ctx != NULL) {
454 gss_cred_set_ctx(task->tk_msg.rpc_cred, gss_msg->ctx);
455 gss_cred->gc_upcall = NULL;
456 rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
457 } else if (gss_msg->msg.errno >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 task->tk_timeout = 0;
459 gss_cred->gc_upcall = gss_msg;
460 /* gss_upcall_callback will release the reference to gss_upcall_msg */
461 atomic_inc(&gss_msg->count);
Trond Myklebust5d008372008-02-22 16:34:17 -0500462 rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 } else
464 err = gss_msg->msg.errno;
Trond Myklebustb185f832007-06-07 10:14:14 -0400465 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 gss_release_msg(gss_msg);
467out:
Chuck Lever8885cb32007-01-31 12:14:05 -0500468 dprintk("RPC: %5u gss_refresh_upcall for uid %u result %d\n",
469 task->tk_pid, cred->cr_uid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return err;
471}
472
473static inline int
474gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
475{
\"J. Bruce Fields\5b7ddd42008-12-23 16:15:44 -0500476 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 struct rpc_cred *cred = &gss_cred->gc_base;
478 struct gss_upcall_msg *gss_msg;
479 DEFINE_WAIT(wait);
480 int err = 0;
481
Chuck Lever8885cb32007-01-31 12:14:05 -0500482 dprintk("RPC: gss_upcall for uid %u\n", cred->cr_uid);
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500483retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 gss_msg = gss_setup_upcall(gss_auth->client, gss_auth, cred);
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500485 if (PTR_ERR(gss_msg) == -EAGAIN) {
486 err = wait_event_interruptible_timeout(pipe_version_waitqueue,
487 pipe_version >= 0, 15*HZ);
488 if (err)
489 goto out;
490 if (pipe_version < 0)
491 warn_gssd();
492 goto retry;
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (IS_ERR(gss_msg)) {
495 err = PTR_ERR(gss_msg);
496 goto out;
497 }
\"J. Bruce Fields\5b7ddd42008-12-23 16:15:44 -0500498 inode = &gss_msg->inode->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 for (;;) {
500 prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_INTERRUPTIBLE);
Trond Myklebustb185f832007-06-07 10:14:14 -0400501 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
504 }
Trond Myklebustb185f832007-06-07 10:14:14 -0400505 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (signalled()) {
507 err = -ERESTARTSYS;
508 goto out_intr;
509 }
510 schedule();
511 }
512 if (gss_msg->ctx)
Trond Myklebust7b6962b2008-04-17 16:53:01 -0400513 gss_cred_set_ctx(cred, gss_msg->ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 else
515 err = gss_msg->msg.errno;
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400516 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517out_intr:
518 finish_wait(&gss_msg->waitqueue, &wait);
519 gss_release_msg(gss_msg);
520out:
Chuck Lever8885cb32007-01-31 12:14:05 -0500521 dprintk("RPC: gss_create_upcall for uid %u result %d\n",
522 cred->cr_uid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return err;
524}
525
526static ssize_t
527gss_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
528 char __user *dst, size_t buflen)
529{
530 char *data = (char *)msg->data + msg->copied;
Chuck Lever7df08992007-12-20 14:54:27 -0500531 size_t mlen = min(msg->len, buflen);
532 unsigned long left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 left = copy_to_user(dst, data, mlen);
Chuck Lever7df08992007-12-20 14:54:27 -0500535 if (left == mlen) {
536 msg->errno = -EFAULT;
537 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Chuck Lever7df08992007-12-20 14:54:27 -0500539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 mlen -= left;
541 msg->copied += mlen;
542 msg->errno = 0;
543 return mlen;
544}
545
546#define MSG_BUF_MAXSIZE 1024
547
548static ssize_t
549gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
550{
551 const void *p, *end;
552 void *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct gss_upcall_msg *gss_msg;
Trond Myklebustb185f832007-06-07 10:14:14 -0400554 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct gss_cl_ctx *ctx;
556 uid_t uid;
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400557 ssize_t err = -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 if (mlen > MSG_BUF_MAXSIZE)
560 goto out;
561 err = -ENOMEM;
Trond Myklebust0f38b872008-06-10 18:31:01 -0400562 buf = kmalloc(mlen, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (!buf)
564 goto out;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 err = -EFAULT;
567 if (copy_from_user(buf, src, mlen))
568 goto err;
569
570 end = (const void *)((char *)buf + mlen);
571 p = simple_get_bytes(buf, end, &uid, sizeof(uid));
572 if (IS_ERR(p)) {
573 err = PTR_ERR(p);
574 goto err;
575 }
576
577 err = -ENOMEM;
578 ctx = gss_alloc_context();
579 if (ctx == NULL)
580 goto err;
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400581
582 err = -ENOENT;
583 /* Find a matching upcall */
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400584 spin_lock(&inode->i_lock);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400585 gss_msg = __gss_find_upcall(RPC_I(inode), uid);
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400586 if (gss_msg == NULL) {
587 spin_unlock(&inode->i_lock);
588 goto err_put_ctx;
589 }
590 list_del_init(&gss_msg->list);
591 spin_unlock(&inode->i_lock);
592
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400593 p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (IS_ERR(p)) {
595 err = PTR_ERR(p);
Kevin Coffmanffc40f52007-11-09 18:42:04 -0500596 gss_msg->msg.errno = (err == -EAGAIN) ? -EAGAIN : -EACCES;
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400597 goto err_release_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400599 gss_msg->ctx = gss_get_ctx(ctx);
600 err = mlen;
601
602err_release_msg:
Trond Myklebustb185f832007-06-07 10:14:14 -0400603 spin_lock(&inode->i_lock);
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400604 __gss_unhash_msg(gss_msg);
605 spin_unlock(&inode->i_lock);
606 gss_release_msg(gss_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607err_put_ctx:
608 gss_put_ctx(ctx);
609err:
610 kfree(buf);
611out:
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400612 dprintk("RPC: gss_pipe_downcall returning %Zd\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return err;
614}
615
\"J. Bruce Fields\cf819392008-12-23 16:10:19 -0500616static int
617gss_pipe_open(struct inode *inode)
618{
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500619 spin_lock(&pipe_version_lock);
620 if (pipe_version < 0) {
621 pipe_version = 0;
622 rpc_wake_up(&pipe_version_rpc_waitqueue);
623 wake_up(&pipe_version_waitqueue);
624 }
\"J. Bruce Fields\cf819392008-12-23 16:10:19 -0500625 atomic_inc(&pipe_users);
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500626 spin_unlock(&pipe_version_lock);
\"J. Bruce Fields\cf819392008-12-23 16:10:19 -0500627 return 0;
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630static void
631gss_pipe_release(struct inode *inode)
632{
633 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400634 struct gss_upcall_msg *gss_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Trond Myklebustb185f832007-06-07 10:14:14 -0400636 spin_lock(&inode->i_lock);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400637 while (!list_empty(&rpci->in_downcall)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400639 gss_msg = list_entry(rpci->in_downcall.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct gss_upcall_msg, list);
641 gss_msg->msg.errno = -EPIPE;
642 atomic_inc(&gss_msg->count);
643 __gss_unhash_msg(gss_msg);
Trond Myklebustb185f832007-06-07 10:14:14 -0400644 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 gss_release_msg(gss_msg);
Trond Myklebustb185f832007-06-07 10:14:14 -0400646 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Trond Myklebustb185f832007-06-07 10:14:14 -0400648 spin_unlock(&inode->i_lock);
\"J. Bruce Fields\cf819392008-12-23 16:10:19 -0500649
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -0500650 put_pipe_version();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653static void
654gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
655{
656 struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 if (msg->errno < 0) {
Chuck Lever8885cb32007-01-31 12:14:05 -0500659 dprintk("RPC: gss_pipe_destroy_msg releasing msg %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 gss_msg);
661 atomic_inc(&gss_msg->count);
662 gss_unhash_msg(gss_msg);
\"J. Bruce Fields\b03568c2008-12-23 16:06:55 -0500663 if (msg->errno == -ETIMEDOUT)
664 warn_gssd();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 gss_release_msg(gss_msg);
666 }
667}
668
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800669/*
670 * NOTE: we have the opportunity to use different
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 * parameters based on the input flavor (which must be a pseudoflavor)
672 */
673static struct rpc_auth *
674gss_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
675{
676 struct gss_auth *gss_auth;
677 struct rpc_auth * auth;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000678 int err = -ENOMEM; /* XXX? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Chuck Lever8885cb32007-01-31 12:14:05 -0500680 dprintk("RPC: creating GSS authenticator for client %p\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 if (!try_module_get(THIS_MODULE))
J. Bruce Fields6a192752005-06-22 17:16:23 +0000683 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
685 goto out_dec;
686 gss_auth->client = clnt;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000687 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
689 if (!gss_auth->mech) {
James Morris3392c342007-12-26 11:20:43 +1100690 printk(KERN_WARNING "%s: Pseudoflavor %d not found!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800691 __func__, flavor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 goto err_free;
693 }
694 gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
J. Bruce Fields438b6fd2005-06-22 17:16:23 +0000695 if (gss_auth->service == 0)
696 goto err_put_mech;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 auth = &gss_auth->rpc_auth;
698 auth->au_cslack = GSS_CRED_SLACK >> 2;
699 auth->au_rslack = GSS_VERF_SLACK >> 2;
700 auth->au_ops = &authgss_ops;
701 auth->au_flavor = flavor;
702 atomic_set(&auth->au_count, 1);
Trond Myklebust0285ed12007-06-27 14:29:12 -0400703 kref_init(&gss_auth->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Trond Myklebust158998b2006-08-24 01:03:17 -0400705 gss_auth->dentry = rpc_mkpipe(clnt->cl_dentry, gss_auth->mech->gm_name,
706 clnt, &gss_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000707 if (IS_ERR(gss_auth->dentry)) {
708 err = PTR_ERR(gss_auth->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 goto err_put_mech;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Trond Myklebustf5c21872007-06-25 17:11:20 -0400712 err = rpcauth_init_credcache(auth);
Trond Myklebust07a2bf12007-06-09 15:42:01 -0400713 if (err)
714 goto err_unlink_pipe;
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return auth;
Trond Myklebust07a2bf12007-06-09 15:42:01 -0400717err_unlink_pipe:
718 rpc_unlink(gss_auth->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719err_put_mech:
720 gss_mech_put(gss_auth->mech);
721err_free:
722 kfree(gss_auth);
723out_dec:
724 module_put(THIS_MODULE);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000725 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728static void
Trond Myklebust0285ed12007-06-27 14:29:12 -0400729gss_free(struct gss_auth *gss_auth)
730{
731 rpc_unlink(gss_auth->dentry);
Trond Myklebust0285ed12007-06-27 14:29:12 -0400732 gss_mech_put(gss_auth->mech);
733
734 kfree(gss_auth);
735 module_put(THIS_MODULE);
736}
737
738static void
739gss_free_callback(struct kref *kref)
740{
741 struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
742
743 gss_free(gss_auth);
744}
745
746static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747gss_destroy(struct rpc_auth *auth)
748{
749 struct gss_auth *gss_auth;
750
Chuck Lever8885cb32007-01-31 12:14:05 -0500751 dprintk("RPC: destroying GSS authenticator %p flavor %d\n",
752 auth, auth->au_flavor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400754 rpcauth_destroy_credcache(auth);
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 gss_auth = container_of(auth, struct gss_auth, rpc_auth);
Trond Myklebust0285ed12007-06-27 14:29:12 -0400757 kref_put(&gss_auth->kref, gss_free_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Trond Myklebust0df7fb72007-06-26 17:04:57 -0400760/*
761 * gss_destroying_context will cause the RPCSEC_GSS to send a NULL RPC call
762 * to the server with the GSS control procedure field set to
763 * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
764 * all RPCSEC_GSS state associated with that context.
765 */
766static int
767gss_destroying_context(struct rpc_cred *cred)
768{
769 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
770 struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
771 struct rpc_task *task;
772
773 if (gss_cred->gc_ctx == NULL ||
Jeff Layton6dcd3922008-12-23 15:21:57 -0500774 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0)
Trond Myklebust0df7fb72007-06-26 17:04:57 -0400775 return 0;
776
777 gss_cred->gc_ctx->gc_proc = RPC_GSS_PROC_DESTROY;
778 cred->cr_ops = &gss_nullops;
779
780 /* Take a reference to ensure the cred will be destroyed either
781 * by the RPC call or by the put_rpccred() below */
782 get_rpccred(cred);
783
Trond Myklebust080a1f12008-04-19 14:22:31 -0400784 task = rpc_call_null(gss_auth->client, cred, RPC_TASK_ASYNC|RPC_TASK_SOFT);
Trond Myklebust0df7fb72007-06-26 17:04:57 -0400785 if (!IS_ERR(task))
786 rpc_put_task(task);
787
788 put_rpccred(cred);
789 return 1;
790}
791
792/* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 * to create a new cred or context, so they check that things have been
794 * allocated before freeing them. */
795static void
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400796gss_do_free_ctx(struct gss_cl_ctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400798 dprintk("RPC: gss_free_ctx\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 kfree(ctx->gc_wire_ctx.data);
801 kfree(ctx);
802}
803
804static void
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400805gss_free_ctx_callback(struct rcu_head *head)
806{
807 struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
808 gss_do_free_ctx(ctx);
809}
810
811static void
812gss_free_ctx(struct gss_cl_ctx *ctx)
813{
Trond Myklebusta4deb812007-08-06 12:21:13 -0400814 struct gss_ctx *gc_gss_ctx;
815
816 gc_gss_ctx = rcu_dereference(ctx->gc_gss_ctx);
817 rcu_assign_pointer(ctx->gc_gss_ctx, NULL);
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400818 call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
Trond Myklebusta4deb812007-08-06 12:21:13 -0400819 if (gc_gss_ctx)
820 gss_delete_sec_context(&gc_gss_ctx);
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400821}
822
823static void
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400824gss_free_cred(struct gss_cred *gss_cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400826 dprintk("RPC: gss_free_cred %p\n", gss_cred);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400827 kfree(gss_cred);
828}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400830static void
831gss_free_cred_callback(struct rcu_head *head)
832{
833 struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
834 gss_free_cred(gss_cred);
835}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400837static void
Jeff Layton6dcd3922008-12-23 15:21:57 -0500838gss_destroy_nullcred(struct rpc_cred *cred)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400839{
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400840 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
Trond Myklebust0285ed12007-06-27 14:29:12 -0400841 struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400842 struct gss_cl_ctx *ctx = gss_cred->gc_ctx;
843
844 rcu_assign_pointer(gss_cred->gc_ctx, NULL);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400845 call_rcu(&cred->cr_rcu, gss_free_cred_callback);
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400846 if (ctx)
847 gss_put_ctx(ctx);
Trond Myklebust0285ed12007-06-27 14:29:12 -0400848 kref_put(&gss_auth->kref, gss_free_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
Jeff Layton6dcd3922008-12-23 15:21:57 -0500851static void
852gss_destroy_cred(struct rpc_cred *cred)
853{
854
855 if (gss_destroying_context(cred))
856 return;
857 gss_destroy_nullcred(cred);
858}
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860/*
861 * Lookup RPCSEC_GSS cred for the current process
862 */
863static struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500864gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Trond Myklebust8a317762006-02-01 12:18:36 -0500866 return rpcauth_lookup_credcache(auth, acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
869static struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500870gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
873 struct gss_cred *cred = NULL;
874 int err = -ENOMEM;
875
Chuck Lever8885cb32007-01-31 12:14:05 -0500876 dprintk("RPC: gss_create_cred for uid %d, flavor %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 acred->uid, auth->au_flavor);
878
Trond Myklebust0f38b872008-06-10 18:31:01 -0400879 if (!(cred = kzalloc(sizeof(*cred), GFP_NOFS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 goto out_err;
881
Trond Myklebust5fe47552007-06-23 19:55:31 -0400882 rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /*
884 * Note: in order to force a call to call_refresh(), we deliberately
885 * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
886 */
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400887 cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 cred->gc_service = gss_auth->service;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400889 cred->gc_machine_cred = acred->machine_cred;
Trond Myklebust0285ed12007-06-27 14:29:12 -0400890 kref_get(&gss_auth->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return &cred->gc_base;
892
893out_err:
Chuck Lever8885cb32007-01-31 12:14:05 -0500894 dprintk("RPC: gss_create_cred failed with error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return ERR_PTR(err);
896}
897
898static int
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500899gss_cred_init(struct rpc_auth *auth, struct rpc_cred *cred)
900{
901 struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
902 struct gss_cred *gss_cred = container_of(cred,struct gss_cred, gc_base);
903 int err;
904
905 do {
906 err = gss_create_upcall(gss_auth, gss_cred);
907 } while (err == -EAGAIN);
908 return err;
909}
910
911static int
Trond Myklebust8a317762006-02-01 12:18:36 -0500912gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
915
Trond Myklebustcd019f72008-04-17 17:03:58 -0400916 if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
Trond Myklebust8a317762006-02-01 12:18:36 -0500917 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /* Don't match with creds that have expired. */
Trond Myklebustcd019f72008-04-17 17:03:58 -0400919 if (time_after(jiffies, gss_cred->gc_ctx->gc_expiry))
920 return 0;
921 if (!test_bit(RPCAUTH_CRED_UPTODATE, &rc->cr_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return 0;
Trond Myklebust8a317762006-02-01 12:18:36 -0500923out:
Trond Myklebust7c67db32008-04-07 20:50:11 -0400924 if (acred->machine_cred != gss_cred->gc_machine_cred)
925 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return (rc->cr_uid == acred->uid);
927}
928
929/*
930* Marshal credentials.
931* Maybe we should keep a cached credential for performance reasons.
932*/
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700933static __be32 *
934gss_marshal(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 struct rpc_cred *cred = task->tk_msg.rpc_cred;
937 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
938 gc_base);
939 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700940 __be32 *cred_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 struct rpc_rqst *req = task->tk_rqstp;
942 u32 maj_stat = 0;
943 struct xdr_netobj mic;
944 struct kvec iov;
945 struct xdr_buf verf_buf;
946
Chuck Lever8885cb32007-01-31 12:14:05 -0500947 dprintk("RPC: %5u gss_marshal\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 *p++ = htonl(RPC_AUTH_GSS);
950 cred_len = p++;
951
952 spin_lock(&ctx->gc_seq_lock);
953 req->rq_seqno = ctx->gc_seq++;
954 spin_unlock(&ctx->gc_seq_lock);
955
956 *p++ = htonl((u32) RPC_GSS_VERSION);
957 *p++ = htonl((u32) ctx->gc_proc);
958 *p++ = htonl((u32) req->rq_seqno);
959 *p++ = htonl((u32) gss_cred->gc_service);
960 p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
961 *cred_len = htonl((p - (cred_len + 1)) << 2);
962
963 /* We compute the checksum for the verifier over the xdr-encoded bytes
964 * starting with the xid and ending at the end of the credential: */
Chuck Lever808012f2005-08-25 16:25:49 -0700965 iov.iov_base = xprt_skip_transport_header(task->tk_xprt,
966 req->rq_snd_buf.head[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
968 xdr_buf_from_iov(&iov, &verf_buf);
969
970 /* set verifier flavor*/
971 *p++ = htonl(RPC_AUTH_GSS);
972
973 mic.data = (u8 *)(p + 1);
J. Bruce Fields00fd6e12005-10-13 16:55:18 -0400974 maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400976 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 } else if (maj_stat != 0) {
978 printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
979 goto out_put_ctx;
980 }
981 p = xdr_encode_opaque(p, NULL, mic.len);
982 gss_put_ctx(ctx);
983 return p;
984out_put_ctx:
985 gss_put_ctx(ctx);
986 return NULL;
987}
988
Trond Myklebustcd019f72008-04-17 17:03:58 -0400989static int gss_renew_cred(struct rpc_task *task)
990{
991 struct rpc_cred *oldcred = task->tk_msg.rpc_cred;
992 struct gss_cred *gss_cred = container_of(oldcred,
993 struct gss_cred,
994 gc_base);
995 struct rpc_auth *auth = oldcred->cr_auth;
996 struct auth_cred acred = {
997 .uid = oldcred->cr_uid,
998 .machine_cred = gss_cred->gc_machine_cred,
999 };
1000 struct rpc_cred *new;
1001
1002 new = gss_lookup_cred(auth, &acred, RPCAUTH_LOOKUP_NEW);
1003 if (IS_ERR(new))
1004 return PTR_ERR(new);
1005 task->tk_msg.rpc_cred = new;
1006 put_rpccred(oldcred);
1007 return 0;
1008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010/*
1011* Refresh credentials. XXX - finish
1012*/
1013static int
1014gss_refresh(struct rpc_task *task)
1015{
Trond Myklebustcd019f72008-04-17 17:03:58 -04001016 struct rpc_cred *cred = task->tk_msg.rpc_cred;
1017 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Trond Myklebustcd019f72008-04-17 17:03:58 -04001019 if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
1020 !test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags)) {
1021 ret = gss_renew_cred(task);
1022 if (ret < 0)
1023 goto out;
1024 cred = task->tk_msg.rpc_cred;
1025 }
1026
1027 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
1028 ret = gss_refresh_upcall(task);
1029out:
1030 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
Trond Myklebust0df7fb72007-06-26 17:04:57 -04001033/* Dummy refresh routine: used only when destroying the context */
1034static int
1035gss_refresh_null(struct rpc_task *task)
1036{
1037 return -EACCES;
1038}
1039
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001040static __be32 *
1041gss_validate(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 struct rpc_cred *cred = task->tk_msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001045 __be32 seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 struct kvec iov;
1047 struct xdr_buf verf_buf;
1048 struct xdr_netobj mic;
1049 u32 flav,len;
1050 u32 maj_stat;
1051
Chuck Lever8885cb32007-01-31 12:14:05 -05001052 dprintk("RPC: %5u gss_validate\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 flav = ntohl(*p++);
1055 if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001056 goto out_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (flav != RPC_AUTH_GSS)
1058 goto out_bad;
1059 seq = htonl(task->tk_rqstp->rq_seqno);
1060 iov.iov_base = &seq;
1061 iov.iov_len = sizeof(seq);
1062 xdr_buf_from_iov(&iov, &verf_buf);
1063 mic.data = (u8 *)p;
1064 mic.len = len;
1065
J. Bruce Fields00fd6e12005-10-13 16:55:18 -04001066 maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001068 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Trond Myklebust0df7fb72007-06-26 17:04:57 -04001069 if (maj_stat) {
Joe Perches014313a2007-11-19 17:53:43 -08001070 dprintk("RPC: %5u gss_validate: gss_verify_mic returned "
Trond Myklebust0df7fb72007-06-26 17:04:57 -04001071 "error 0x%08x\n", task->tk_pid, maj_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 goto out_bad;
Trond Myklebust0df7fb72007-06-26 17:04:57 -04001073 }
J. Bruce Fields24b26052005-10-13 16:54:53 -04001074 /* We leave it to unwrap to calculate au_rslack. For now we just
1075 * calculate the length of the verifier: */
Trond Myklebust1be27f32007-06-27 14:29:04 -04001076 cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 gss_put_ctx(ctx);
Chuck Lever8885cb32007-01-31 12:14:05 -05001078 dprintk("RPC: %5u gss_validate: gss_verify_mic succeeded.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 task->tk_pid);
1080 return p + XDR_QUADLEN(len);
1081out_bad:
1082 gss_put_ctx(ctx);
Chuck Lever8885cb32007-01-31 12:14:05 -05001083 dprintk("RPC: %5u gss_validate failed.\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return NULL;
1085}
1086
1087static inline int
1088gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001089 kxdrproc_t encode, struct rpc_rqst *rqstp, __be32 *p, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
1092 struct xdr_buf integ_buf;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001093 __be32 *integ_len = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct xdr_netobj mic;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001095 u32 offset;
1096 __be32 *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct kvec *iov;
1098 u32 maj_stat = 0;
1099 int status = -EIO;
1100
1101 integ_len = p++;
1102 offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
1103 *p++ = htonl(rqstp->rq_seqno);
1104
Trond Myklebust88a9fe82008-12-23 15:21:31 -05001105 status = encode(rqstp, p, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (status)
1107 return status;
1108
1109 if (xdr_buf_subsegment(snd_buf, &integ_buf,
1110 offset, snd_buf->len - offset))
1111 return status;
1112 *integ_len = htonl(integ_buf.len);
1113
1114 /* guess whether we're in the head or the tail: */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001115 if (snd_buf->page_len || snd_buf->tail[0].iov_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 iov = snd_buf->tail;
1117 else
1118 iov = snd_buf->head;
1119 p = iov->iov_base + iov->iov_len;
1120 mic.data = (u8 *)(p + 1);
1121
J. Bruce Fields00fd6e12005-10-13 16:55:18 -04001122 maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 status = -EIO; /* XXX? */
1124 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001125 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 else if (maj_stat)
1127 return status;
1128 q = xdr_encode_opaque(p, NULL, mic.len);
1129
1130 offset = (u8 *)q - (u8 *)p;
1131 iov->iov_len += offset;
1132 snd_buf->len += offset;
1133 return 0;
1134}
1135
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001136static void
1137priv_release_snd_buf(struct rpc_rqst *rqstp)
1138{
1139 int i;
1140
1141 for (i=0; i < rqstp->rq_enc_pages_num; i++)
1142 __free_page(rqstp->rq_enc_pages[i]);
1143 kfree(rqstp->rq_enc_pages);
1144}
1145
1146static int
1147alloc_enc_pages(struct rpc_rqst *rqstp)
1148{
1149 struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
1150 int first, last, i;
1151
1152 if (snd_buf->page_len == 0) {
1153 rqstp->rq_enc_pages_num = 0;
1154 return 0;
1155 }
1156
1157 first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
1158 last = (snd_buf->page_base + snd_buf->page_len - 1) >> PAGE_CACHE_SHIFT;
1159 rqstp->rq_enc_pages_num = last - first + 1 + 1;
1160 rqstp->rq_enc_pages
1161 = kmalloc(rqstp->rq_enc_pages_num * sizeof(struct page *),
1162 GFP_NOFS);
1163 if (!rqstp->rq_enc_pages)
1164 goto out;
1165 for (i=0; i < rqstp->rq_enc_pages_num; i++) {
1166 rqstp->rq_enc_pages[i] = alloc_page(GFP_NOFS);
1167 if (rqstp->rq_enc_pages[i] == NULL)
1168 goto out_free;
1169 }
1170 rqstp->rq_release_snd_buf = priv_release_snd_buf;
1171 return 0;
1172out_free:
1173 for (i--; i >= 0; i--) {
1174 __free_page(rqstp->rq_enc_pages[i]);
1175 }
1176out:
1177 return -EAGAIN;
1178}
1179
1180static inline int
1181gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001182 kxdrproc_t encode, struct rpc_rqst *rqstp, __be32 *p, void *obj)
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001183{
1184 struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
1185 u32 offset;
1186 u32 maj_stat;
1187 int status;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001188 __be32 *opaque_len;
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001189 struct page **inpages;
1190 int first;
1191 int pad;
1192 struct kvec *iov;
1193 char *tmp;
1194
1195 opaque_len = p++;
1196 offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
1197 *p++ = htonl(rqstp->rq_seqno);
1198
Trond Myklebust88a9fe82008-12-23 15:21:31 -05001199 status = encode(rqstp, p, obj);
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001200 if (status)
1201 return status;
1202
1203 status = alloc_enc_pages(rqstp);
1204 if (status)
1205 return status;
1206 first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
1207 inpages = snd_buf->pages + first;
1208 snd_buf->pages = rqstp->rq_enc_pages;
1209 snd_buf->page_base -= first << PAGE_CACHE_SHIFT;
1210 /* Give the tail its own page, in case we need extra space in the
1211 * head when wrapping: */
1212 if (snd_buf->page_len || snd_buf->tail[0].iov_len) {
1213 tmp = page_address(rqstp->rq_enc_pages[rqstp->rq_enc_pages_num - 1]);
1214 memcpy(tmp, snd_buf->tail[0].iov_base, snd_buf->tail[0].iov_len);
1215 snd_buf->tail[0].iov_base = tmp;
1216 }
J. Bruce Fields00fd6e12005-10-13 16:55:18 -04001217 maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages);
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001218 /* RPC_SLACK_SPACE should prevent this ever happening: */
1219 BUG_ON(snd_buf->len > snd_buf->buflen);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001220 status = -EIO;
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001221 /* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
1222 * done anyway, so it's safe to put the request on the wire: */
1223 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001224 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001225 else if (maj_stat)
1226 return status;
1227
1228 *opaque_len = htonl(snd_buf->len - offset);
1229 /* guess whether we're in the head or the tail: */
1230 if (snd_buf->page_len || snd_buf->tail[0].iov_len)
1231 iov = snd_buf->tail;
1232 else
1233 iov = snd_buf->head;
1234 p = iov->iov_base + iov->iov_len;
1235 pad = 3 - ((snd_buf->len - offset - 1) & 3);
1236 memset(p, 0, pad);
1237 iov->iov_len += pad;
1238 snd_buf->len += pad;
1239
1240 return 0;
1241}
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243static int
1244gss_wrap_req(struct rpc_task *task,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001245 kxdrproc_t encode, void *rqstp, __be32 *p, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 struct rpc_cred *cred = task->tk_msg.rpc_cred;
1248 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
1249 gc_base);
1250 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1251 int status = -EIO;
1252
Chuck Lever8885cb32007-01-31 12:14:05 -05001253 dprintk("RPC: %5u gss_wrap_req\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
1255 /* The spec seems a little ambiguous here, but I think that not
1256 * wrapping context destruction requests makes the most sense.
1257 */
Trond Myklebust88a9fe82008-12-23 15:21:31 -05001258 status = encode(rqstp, p, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 goto out;
1260 }
1261 switch (gss_cred->gc_service) {
1262 case RPC_GSS_SVC_NONE:
Trond Myklebust88a9fe82008-12-23 15:21:31 -05001263 status = encode(rqstp, p, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 break;
1265 case RPC_GSS_SVC_INTEGRITY:
1266 status = gss_wrap_req_integ(cred, ctx, encode,
1267 rqstp, p, obj);
1268 break;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001269 case RPC_GSS_SVC_PRIVACY:
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001270 status = gss_wrap_req_priv(cred, ctx, encode,
1271 rqstp, p, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 break;
1273 }
1274out:
1275 gss_put_ctx(ctx);
Chuck Lever8885cb32007-01-31 12:14:05 -05001276 dprintk("RPC: %5u gss_wrap_req returning %d\n", task->tk_pid, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return status;
1278}
1279
1280static inline int
1281gss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001282 struct rpc_rqst *rqstp, __be32 **p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
1285 struct xdr_buf integ_buf;
1286 struct xdr_netobj mic;
1287 u32 data_offset, mic_offset;
1288 u32 integ_len;
1289 u32 maj_stat;
1290 int status = -EIO;
1291
1292 integ_len = ntohl(*(*p)++);
1293 if (integ_len & 3)
1294 return status;
1295 data_offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
1296 mic_offset = integ_len + data_offset;
1297 if (mic_offset > rcv_buf->len)
1298 return status;
1299 if (ntohl(*(*p)++) != rqstp->rq_seqno)
1300 return status;
1301
1302 if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
1303 mic_offset - data_offset))
1304 return status;
1305
1306 if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
1307 return status;
1308
J. Bruce Fields00fd6e12005-10-13 16:55:18 -04001309 maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001311 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (maj_stat != GSS_S_COMPLETE)
1313 return status;
1314 return 0;
1315}
1316
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001317static inline int
1318gss_unwrap_resp_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001319 struct rpc_rqst *rqstp, __be32 **p)
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001320{
1321 struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
1322 u32 offset;
1323 u32 opaque_len;
1324 u32 maj_stat;
1325 int status = -EIO;
1326
1327 opaque_len = ntohl(*(*p)++);
1328 offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
1329 if (offset + opaque_len > rcv_buf->len)
1330 return status;
1331 /* remove padding: */
1332 rcv_buf->len = offset + opaque_len;
1333
J. Bruce Fields00fd6e12005-10-13 16:55:18 -04001334 maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf);
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001335 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001336 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001337 if (maj_stat != GSS_S_COMPLETE)
1338 return status;
1339 if (ntohl(*(*p)++) != rqstp->rq_seqno)
1340 return status;
1341
1342 return 0;
1343}
1344
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346static int
1347gss_unwrap_resp(struct rpc_task *task,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001348 kxdrproc_t decode, void *rqstp, __be32 *p, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 struct rpc_cred *cred = task->tk_msg.rpc_cred;
1351 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
1352 gc_base);
1353 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001354 __be32 *savedp = p;
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001355 struct kvec *head = ((struct rpc_rqst *)rqstp)->rq_rcv_buf.head;
1356 int savedlen = head->iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 int status = -EIO;
1358
1359 if (ctx->gc_proc != RPC_GSS_PROC_DATA)
1360 goto out_decode;
1361 switch (gss_cred->gc_service) {
1362 case RPC_GSS_SVC_NONE:
1363 break;
1364 case RPC_GSS_SVC_INTEGRITY:
1365 status = gss_unwrap_resp_integ(cred, ctx, rqstp, &p);
1366 if (status)
1367 goto out;
1368 break;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001369 case RPC_GSS_SVC_PRIVACY:
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001370 status = gss_unwrap_resp_priv(cred, ctx, rqstp, &p);
1371 if (status)
1372 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 break;
1374 }
J. Bruce Fields24b26052005-10-13 16:54:53 -04001375 /* take into account extra slack for integrity and privacy cases: */
Trond Myklebust1be27f32007-06-27 14:29:04 -04001376 cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + (p - savedp)
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001377 + (savedlen - head->iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378out_decode:
Trond Myklebust88a9fe82008-12-23 15:21:31 -05001379 status = decode(rqstp, p, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380out:
1381 gss_put_ctx(ctx);
Chuck Lever8885cb32007-01-31 12:14:05 -05001382 dprintk("RPC: %5u gss_unwrap_resp returning %d\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 status);
1384 return status;
1385}
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001386
Trond Myklebustf1c0a862007-06-23 20:17:58 -04001387static const struct rpc_authops authgss_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 .owner = THIS_MODULE,
1389 .au_flavor = RPC_AUTH_GSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 .au_name = "RPCSEC_GSS",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 .create = gss_create,
1392 .destroy = gss_destroy,
1393 .lookup_cred = gss_lookup_cred,
1394 .crcreate = gss_create_cred
1395};
1396
Trond Myklebustf1c0a862007-06-23 20:17:58 -04001397static const struct rpc_credops gss_credops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 .cr_name = "AUTH_GSS",
1399 .crdestroy = gss_destroy_cred,
Trond Myklebustfba3bad2006-02-01 12:19:27 -05001400 .cr_init = gss_cred_init,
Trond Myklebust5c691042008-03-12 16:21:07 -04001401 .crbind = rpcauth_generic_bind_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 .crmatch = gss_match,
1403 .crmarshal = gss_marshal,
1404 .crrefresh = gss_refresh,
1405 .crvalidate = gss_validate,
1406 .crwrap_req = gss_wrap_req,
1407 .crunwrap_resp = gss_unwrap_resp,
1408};
1409
Trond Myklebust0df7fb72007-06-26 17:04:57 -04001410static const struct rpc_credops gss_nullops = {
1411 .cr_name = "AUTH_GSS",
Jeff Layton6dcd3922008-12-23 15:21:57 -05001412 .crdestroy = gss_destroy_nullcred,
Trond Myklebust5c691042008-03-12 16:21:07 -04001413 .crbind = rpcauth_generic_bind_cred,
Trond Myklebust0df7fb72007-06-26 17:04:57 -04001414 .crmatch = gss_match,
1415 .crmarshal = gss_marshal,
1416 .crrefresh = gss_refresh_null,
1417 .crvalidate = gss_validate,
1418 .crwrap_req = gss_wrap_req,
1419 .crunwrap_resp = gss_unwrap_resp,
1420};
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422static struct rpc_pipe_ops gss_upcall_ops = {
1423 .upcall = gss_pipe_upcall,
1424 .downcall = gss_pipe_downcall,
1425 .destroy_msg = gss_pipe_destroy_msg,
\"J. Bruce Fields\cf819392008-12-23 16:10:19 -05001426 .open_pipe = gss_pipe_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 .release_pipe = gss_pipe_release,
1428};
1429
1430/*
1431 * Initialize RPCSEC_GSS module
1432 */
1433static int __init init_rpcsec_gss(void)
1434{
1435 int err = 0;
1436
1437 err = rpcauth_register(&authgss_ops);
1438 if (err)
1439 goto out;
1440 err = gss_svc_init();
1441 if (err)
1442 goto out_unregister;
\"J. Bruce Fields\79a3f202008-12-23 16:10:52 -05001443 rpc_init_wait_queue(&pipe_version_rpc_waitqueue, "gss pipe version");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return 0;
1445out_unregister:
1446 rpcauth_unregister(&authgss_ops);
1447out:
1448 return err;
1449}
1450
1451static void __exit exit_rpcsec_gss(void)
1452{
1453 gss_svc_shutdown();
1454 rpcauth_unregister(&authgss_ops);
1455}
1456
1457MODULE_LICENSE("GPL");
1458module_init(init_rpcsec_gss)
1459module_exit(exit_rpcsec_gss)