Whamcloud - gitweb
b=23728 Doxygen style comment - sptlrpc api.
[fs/lustre-release.git] / lustre / include / lustre_sec.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef _LUSTRE_SEC_H_
38 #define _LUSTRE_SEC_H_
39
40 /** \defgroup sptlrpc sptlrpc
41  *
42  * @{
43  */
44
45 /*
46  * to avoid include
47  */
48 struct obd_import;
49 struct obd_export;
50 struct ptlrpc_request;
51 struct ptlrpc_reply_state;
52 struct ptlrpc_bulk_desc;
53 struct brw_page;
54 /* Linux specific */
55 struct key;
56 struct seq_file;
57
58 /*
59  * forward declaration
60  */
61 struct ptlrpc_sec_policy;
62 struct ptlrpc_sec_cops;
63 struct ptlrpc_sec_sops;
64 struct ptlrpc_sec;
65 struct ptlrpc_svc_ctx;
66 struct ptlrpc_cli_ctx;
67 struct ptlrpc_ctx_ops;
68
69 /**
70  * \addtogroup flavor flavor
71  *
72  * RPC flavor is represented by a 32 bits integer. Currently the high 12 bits
73  * are unused, must be set to 0 for future expansion.
74  * <pre>
75  * ------------------------------------------------------------------------
76  * | 4b (bulk svc) | 4b (bulk type) | 4b (svc) | 4b (mech)  | 4b (policy) |
77  * ------------------------------------------------------------------------
78  * </pre>
79  *
80  * @{
81  */
82
83 /*
84  * flavor constants
85  */
86 enum sptlrpc_policy {
87         SPTLRPC_POLICY_NULL             = 0,
88         SPTLRPC_POLICY_PLAIN            = 1,
89         SPTLRPC_POLICY_GSS              = 2,
90         SPTLRPC_POLICY_MAX,
91 };
92
93 enum sptlrpc_mech_null {
94         SPTLRPC_MECH_NULL               = 0,
95         SPTLRPC_MECH_NULL_MAX,
96 };
97
98 enum sptlrpc_mech_plain {
99         SPTLRPC_MECH_PLAIN              = 0,
100         SPTLRPC_MECH_PLAIN_MAX,
101 };
102
103 enum sptlrpc_mech_gss {
104         SPTLRPC_MECH_GSS_NULL           = 0,
105         SPTLRPC_MECH_GSS_KRB5           = 1,
106         SPTLRPC_MECH_GSS_MAX,
107 };
108
109 enum sptlrpc_service_type {
110         SPTLRPC_SVC_NULL                = 0,    /**< no security */
111         SPTLRPC_SVC_AUTH                = 1,    /**< authentication only */
112         SPTLRPC_SVC_INTG                = 2,    /**< integrity */
113         SPTLRPC_SVC_PRIV                = 3,    /**< privacy */
114         SPTLRPC_SVC_MAX,
115 };
116
117 enum sptlrpc_bulk_type {
118         SPTLRPC_BULK_DEFAULT            = 0,    /**< follow rpc flavor */
119         SPTLRPC_BULK_HASH               = 1,    /**< hash integrity */
120         SPTLRPC_BULK_MAX,
121 };
122
123 enum sptlrpc_bulk_service {
124         SPTLRPC_BULK_SVC_NULL           = 0,    /**< no security */
125         SPTLRPC_BULK_SVC_AUTH           = 1,    /**< authentication only */
126         SPTLRPC_BULK_SVC_INTG           = 2,    /**< integrity */
127         SPTLRPC_BULK_SVC_PRIV           = 3,    /**< privacy */
128         SPTLRPC_BULK_SVC_MAX,
129 };
130
131 /*
132  * compose/extract macros
133  */
134 #define FLVR_POLICY_OFFSET              (0)
135 #define FLVR_MECH_OFFSET                (4)
136 #define FLVR_SVC_OFFSET                 (8)
137 #define FLVR_BULK_TYPE_OFFSET           (12)
138 #define FLVR_BULK_SVC_OFFSET            (16)
139
140 #define MAKE_FLVR(policy, mech, svc, btype, bsvc)                       \
141         (((__u32)(policy) << FLVR_POLICY_OFFSET) |                      \
142          ((__u32)(mech) << FLVR_MECH_OFFSET) |                          \
143          ((__u32)(svc) << FLVR_SVC_OFFSET) |                            \
144          ((__u32)(btype) << FLVR_BULK_TYPE_OFFSET) |                    \
145          ((__u32)(bsvc) << FLVR_BULK_SVC_OFFSET))
146
147 /*
148  * extraction
149  */
150 #define SPTLRPC_FLVR_POLICY(flavor)                                     \
151         ((((__u32)(flavor)) >> FLVR_POLICY_OFFSET) & 0xF)
152 #define SPTLRPC_FLVR_MECH(flavor)                                       \
153         ((((__u32)(flavor)) >> FLVR_MECH_OFFSET) & 0xF)
154 #define SPTLRPC_FLVR_SVC(flavor)                                        \
155         ((((__u32)(flavor)) >> FLVR_SVC_OFFSET) & 0xF)
156 #define SPTLRPC_FLVR_BULK_TYPE(flavor)                                  \
157         ((((__u32)(flavor)) >> FLVR_BULK_TYPE_OFFSET) & 0xF)
158 #define SPTLRPC_FLVR_BULK_SVC(flavor)                                   \
159         ((((__u32)(flavor)) >> FLVR_BULK_SVC_OFFSET) & 0xF)
160
161 #define SPTLRPC_FLVR_BASE(flavor)                                       \
162         ((((__u32)(flavor)) >> FLVR_POLICY_OFFSET) & 0xFFF)
163 #define SPTLRPC_FLVR_BASE_SUB(flavor)                                   \
164         ((((__u32)(flavor)) >> FLVR_MECH_OFFSET) & 0xFF)
165
166 /*
167  * gss subflavors
168  */
169 #define MAKE_BASE_SUBFLVR(mech, svc)                                    \
170         ((__u32)(mech) |                                                \
171          ((__u32)(svc) << (FLVR_SVC_OFFSET - FLVR_MECH_OFFSET)))
172
173 #define SPTLRPC_SUBFLVR_KRB5N                                           \
174         MAKE_BASE_SUBFLVR(SPTLRPC_MECH_GSS_KRB5, SPTLRPC_SVC_NULL)
175 #define SPTLRPC_SUBFLVR_KRB5A                                           \
176         MAKE_BASE_SUBFLVR(SPTLRPC_MECH_GSS_KRB5, SPTLRPC_SVC_AUTH)
177 #define SPTLRPC_SUBFLVR_KRB5I                                           \
178         MAKE_BASE_SUBFLVR(SPTLRPC_MECH_GSS_KRB5, SPTLRPC_SVC_INTG)
179 #define SPTLRPC_SUBFLVR_KRB5P                                           \
180         MAKE_BASE_SUBFLVR(SPTLRPC_MECH_GSS_KRB5, SPTLRPC_SVC_PRIV)
181
182 /*
183  * "end user" flavors
184  */
185 #define SPTLRPC_FLVR_NULL                               \
186         MAKE_FLVR(SPTLRPC_POLICY_NULL,                  \
187                   SPTLRPC_MECH_NULL,                    \
188                   SPTLRPC_SVC_NULL,                     \
189                   SPTLRPC_BULK_DEFAULT,                 \
190                   SPTLRPC_BULK_SVC_NULL)
191 #define SPTLRPC_FLVR_PLAIN                              \
192         MAKE_FLVR(SPTLRPC_POLICY_PLAIN,                 \
193                   SPTLRPC_MECH_PLAIN,                   \
194                   SPTLRPC_SVC_NULL,                     \
195                   SPTLRPC_BULK_HASH,                    \
196                   SPTLRPC_BULK_SVC_INTG)
197 #define SPTLRPC_FLVR_KRB5N                              \
198         MAKE_FLVR(SPTLRPC_POLICY_GSS,                   \
199                   SPTLRPC_MECH_GSS_KRB5,                \
200                   SPTLRPC_SVC_NULL,                     \
201                   SPTLRPC_BULK_DEFAULT,                 \
202                   SPTLRPC_BULK_SVC_NULL)
203 #define SPTLRPC_FLVR_KRB5A                              \
204         MAKE_FLVR(SPTLRPC_POLICY_GSS,                   \
205                   SPTLRPC_MECH_GSS_KRB5,                \
206                   SPTLRPC_SVC_AUTH,                     \
207                   SPTLRPC_BULK_DEFAULT,                 \
208                   SPTLRPC_BULK_SVC_NULL)
209 #define SPTLRPC_FLVR_KRB5I                              \
210         MAKE_FLVR(SPTLRPC_POLICY_GSS,                   \
211                   SPTLRPC_MECH_GSS_KRB5,                \
212                   SPTLRPC_SVC_INTG,                     \
213                   SPTLRPC_BULK_DEFAULT,                 \
214                   SPTLRPC_BULK_SVC_INTG)
215 #define SPTLRPC_FLVR_KRB5P                              \
216         MAKE_FLVR(SPTLRPC_POLICY_GSS,                   \
217                   SPTLRPC_MECH_GSS_KRB5,                \
218                   SPTLRPC_SVC_PRIV,                     \
219                   SPTLRPC_BULK_DEFAULT,                 \
220                   SPTLRPC_BULK_SVC_PRIV)
221
222 #define SPTLRPC_FLVR_DEFAULT            SPTLRPC_FLVR_NULL
223
224 #define SPTLRPC_FLVR_INVALID            ((__u32) 0xFFFFFFFF)
225 #define SPTLRPC_FLVR_ANY                ((__u32) 0xFFF00000)
226
227 /**
228  * extract the useful part from wire flavor
229  */
230 #define WIRE_FLVR(wflvr)                (((__u32) (wflvr)) & 0x000FFFFF)
231
232 /** @} flavor */
233
234 static inline void flvr_set_svc(__u32 *flvr, __u32 svc)
235 {
236         LASSERT(svc < SPTLRPC_SVC_MAX);
237         *flvr = MAKE_FLVR(SPTLRPC_FLVR_POLICY(*flvr),
238                           SPTLRPC_FLVR_MECH(*flvr),
239                           svc,
240                           SPTLRPC_FLVR_BULK_TYPE(*flvr),
241                           SPTLRPC_FLVR_BULK_SVC(*flvr));
242 }
243
244 static inline void flvr_set_bulk_svc(__u32 *flvr, __u32 svc)
245 {
246         LASSERT(svc < SPTLRPC_BULK_SVC_MAX);
247         *flvr = MAKE_FLVR(SPTLRPC_FLVR_POLICY(*flvr),
248                           SPTLRPC_FLVR_MECH(*flvr),
249                           SPTLRPC_FLVR_SVC(*flvr),
250                           SPTLRPC_FLVR_BULK_TYPE(*flvr),
251                           svc);
252 }
253
254 struct bulk_spec_hash {
255         __u8    hash_alg;
256 };
257
258 /**
259  * Full description of flavors being used on a ptlrpc connection, include
260  * both regular RPC and bulk transfer parts.
261  */
262 struct sptlrpc_flavor {
263         /**
264          * wire flavor, should be renamed to sf_wire.
265          */
266         __u32   sf_rpc;
267         /**
268          * general flags of PTLRPC_SEC_FL_*
269          */
270         __u32   sf_flags;
271         /**
272          * rpc flavor specification
273          */
274         union {
275                 /* nothing for now */
276         } u_rpc;
277         /**
278          * bulk flavor specification
279          */
280         union {
281                 struct bulk_spec_hash hash;
282         } u_bulk;
283 };
284
285 /**
286  * identify the RPC is generated from what part of Lustre. It's encoded into
287  * RPC requests and to be checked by ptlrpc service.
288  */
289 enum lustre_sec_part {
290         LUSTRE_SP_CLI           = 0,
291         LUSTRE_SP_MDT,
292         LUSTRE_SP_OST,
293         LUSTRE_SP_MGC,
294         LUSTRE_SP_MGS,
295         LUSTRE_SP_ANY           = 0xFF
296 };
297
298 const char *sptlrpc_part2name(enum lustre_sec_part sp);
299 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd);
300
301 /**
302  * A rule specifies a flavor to be used by a ptlrpc connection between
303  * two Lustre parts.
304  */
305 struct sptlrpc_rule {
306         __u32                   sr_netid;   /* LNET network ID */
307         __u8                    sr_from;    /* sec_part */
308         __u8                    sr_to;      /* sec_part */
309         __u16                   sr_padding;
310         struct sptlrpc_flavor   sr_flvr;
311 };
312
313 /**
314  * A set of rules in memory.
315  *
316  * Rules are generated and stored on MGS, and propagated to MDT, OST,
317  * and client when needed.
318  */
319 struct sptlrpc_rule_set {
320         int                     srs_nslot;
321         int                     srs_nrule;
322         struct sptlrpc_rule    *srs_rules;
323 };
324
325 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr);
326 int sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr);
327
328 static inline void sptlrpc_rule_set_init(struct sptlrpc_rule_set *set)
329 {
330         memset(set, 0, sizeof(*set));
331 }
332
333 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *set);
334 int  sptlrpc_rule_set_expand(struct sptlrpc_rule_set *set);
335 int  sptlrpc_rule_set_merge(struct sptlrpc_rule_set *set,
336                             struct sptlrpc_rule *rule);
337 int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
338                             enum lustre_sec_part from,
339                             enum lustre_sec_part to,
340                             lnet_nid_t nid,
341                             struct sptlrpc_flavor *sf);
342 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *set);
343
344 int  sptlrpc_process_config(struct lustre_cfg *lcfg);
345 void sptlrpc_conf_log_start(const char *logname);
346 void sptlrpc_conf_log_stop(const char *logname);
347 void sptlrpc_conf_log_update_begin(const char *logname);
348 void sptlrpc_conf_log_update_end(const char *logname);
349 void sptlrpc_conf_client_adapt(struct obd_device *obd);
350 int  sptlrpc_conf_target_get_rules(struct obd_device *obd,
351                                    struct sptlrpc_rule_set *rset,
352                                    int initial);
353 void sptlrpc_target_choose_flavor(struct sptlrpc_rule_set *rset,
354                                   enum lustre_sec_part from,
355                                   lnet_nid_t nid,
356                                   struct sptlrpc_flavor *flavor);
357
358 /* The maximum length of security payload. 1024 is enough for Kerberos 5,
359  * and should be enough for other future mechanisms but not sure.
360  * Only used by pre-allocated request/reply pool.
361  */
362 #define SPTLRPC_MAX_PAYLOAD     (1024)
363
364
365 struct vfs_cred {
366         uint32_t        vc_uid;
367         uint32_t        vc_gid;
368 };
369
370 struct ptlrpc_ctx_ops {
371         /**
372          * To determine whether it's suitable to use the \a ctx for \a vcred.
373          */
374         int     (*match)       (struct ptlrpc_cli_ctx *ctx,
375                                 struct vfs_cred *vcred);
376
377         /**
378          * To bring the \a ctx uptodate.
379          */
380         int     (*refresh)     (struct ptlrpc_cli_ctx *ctx);
381
382         /**
383          * Validate the \a ctx.
384          */
385         int     (*validate)    (struct ptlrpc_cli_ctx *ctx);
386
387         /**
388          * Force the \a ctx to die.
389          */
390         void    (*die)         (struct ptlrpc_cli_ctx *ctx,
391                                 int grace);
392         int     (*display)     (struct ptlrpc_cli_ctx *ctx,
393                                 char *buf, int bufsize);
394
395         /**
396          * Sign the request message using \a ctx.
397          *
398          * \pre req->rq_reqmsg point to request message.
399          * \pre req->rq_reqlen is the request message length.
400          * \post req->rq_reqbuf point to request message with signature.
401          * \post req->rq_reqdata_len is set to the final request message size.
402          *
403          * \see null_ctx_sign(), plain_ctx_sign(), gss_cli_ctx_sign().
404          */
405         int     (*sign)        (struct ptlrpc_cli_ctx *ctx,
406                                 struct ptlrpc_request *req);
407
408         /**
409          * Verify the reply message using \a ctx.
410          *
411          * \pre req->rq_repdata point to reply message with signature.
412          * \pre req->rq_repdata_len is the total reply message length.
413          * \post req->rq_repmsg point to reply message without signature.
414          * \post req->rq_replen is the reply message length.
415          *
416          * \see null_ctx_verify(), plain_ctx_verify(), gss_cli_ctx_verify().
417          */
418         int     (*verify)      (struct ptlrpc_cli_ctx *ctx,
419                                 struct ptlrpc_request *req);
420
421         /**
422          * Encrypt the request message using \a ctx.
423          *
424          * \pre req->rq_reqmsg point to request message in clear text.
425          * \pre req->rq_reqlen is the request message length.
426          * \post req->rq_reqbuf point to request message.
427          * \post req->rq_reqdata_len is set to the final request message size.
428          *
429          * \see gss_cli_ctx_seal().
430          */
431         int     (*seal)        (struct ptlrpc_cli_ctx *ctx,
432                                 struct ptlrpc_request *req);
433
434         /**
435          * Decrypt the reply message using \a ctx.
436          *
437          * \pre req->rq_repdata point to encrypted reply message.
438          * \pre req->rq_repdata_len is the total cipher text length.
439          * \post req->rq_repmsg point to reply message in clear text.
440          * \post req->rq_replen is the reply message length in clear text.
441          *
442          * \see gss_cli_ctx_unseal().
443          */
444         int     (*unseal)      (struct ptlrpc_cli_ctx *ctx,
445                                 struct ptlrpc_request *req);
446
447         /**
448          * Wrap bulk request data. This is called before wrapping RPC
449          * request message.
450          *
451          * \pre bulk buffer is descripted by desc->bd_iov and
452          * desc->bd_iov_count. note for read it's just buffer, no data
453          * need to be sent;  for write it contains data in clear text.
454          * \post when necessary, ptlrpc_bulk_sec_desc was properly prepared
455          * (usually inside of RPC request message).
456          * - encryption: cipher text bulk buffer is descripted by
457          *   desc->bd_enc_iov and desc->bd_iov_count (currently assume iov
458          *   count remains the same).
459          * - otherwise: bulk buffer is still desc->bd_iov and
460          *   desc->bd_iov_count.
461          *
462          * \return 0: success.
463          * \return -ev: error code.
464          *
465          * \see plain_cli_wrap_bulk(), gss_cli_ctx_wrap_bulk().
466          */
467         int     (*wrap_bulk)   (struct ptlrpc_cli_ctx *ctx,
468                                 struct ptlrpc_request *req,
469                                 struct ptlrpc_bulk_desc *desc);
470
471         /**
472          * Unwrap bulk reply data. This is called after wrapping RPC
473          * reply message.
474          *
475          * \pre bulk buffer is descripted by desc->bd_iov/desc->bd_enc_iov and
476          * desc->bd_iov_count, according to wrap_bulk().
477          * \post final bulk data in clear text is placed in buffer described
478          * by desc->bd_iov and desc->bd_iov_count.
479          * \return +ve nob of actual bulk data in clear text.
480          * \return -ve error code.
481          *
482          * \see plain_cli_unwrap_bulk(), gss_cli_ctx_unwrap_bulk().
483          */
484         int     (*unwrap_bulk) (struct ptlrpc_cli_ctx *ctx,
485                                 struct ptlrpc_request *req,
486                                 struct ptlrpc_bulk_desc *desc);
487 };
488
489 #define PTLRPC_CTX_NEW_BIT             (0)  /* newly created */
490 #define PTLRPC_CTX_UPTODATE_BIT        (1)  /* uptodate */
491 #define PTLRPC_CTX_DEAD_BIT            (2)  /* mark expired gracefully */
492 #define PTLRPC_CTX_ERROR_BIT           (3)  /* fatal error (refresh, etc.) */
493 #define PTLRPC_CTX_CACHED_BIT          (8)  /* in ctx cache (hash etc.) */
494 #define PTLRPC_CTX_ETERNAL_BIT         (9)  /* always valid */
495
496 #define PTLRPC_CTX_NEW                 (1 << PTLRPC_CTX_NEW_BIT)
497 #define PTLRPC_CTX_UPTODATE            (1 << PTLRPC_CTX_UPTODATE_BIT)
498 #define PTLRPC_CTX_DEAD                (1 << PTLRPC_CTX_DEAD_BIT)
499 #define PTLRPC_CTX_ERROR               (1 << PTLRPC_CTX_ERROR_BIT)
500 #define PTLRPC_CTX_CACHED              (1 << PTLRPC_CTX_CACHED_BIT)
501 #define PTLRPC_CTX_ETERNAL             (1 << PTLRPC_CTX_ETERNAL_BIT)
502
503 #define PTLRPC_CTX_STATUS_MASK         (PTLRPC_CTX_NEW_BIT    |       \
504                                         PTLRPC_CTX_UPTODATE   |       \
505                                         PTLRPC_CTX_DEAD       |       \
506                                         PTLRPC_CTX_ERROR)
507
508 struct ptlrpc_cli_ctx {
509         cfs_hlist_node_t        cc_cache;      /* linked into ctx cache */
510         cfs_atomic_t            cc_refcount;
511         struct ptlrpc_sec      *cc_sec;
512         struct ptlrpc_ctx_ops  *cc_ops;
513         cfs_time_t              cc_expire;     /* in seconds */
514         unsigned int            cc_early_expire:1;
515         unsigned long           cc_flags;
516         struct vfs_cred         cc_vcred;
517         cfs_spinlock_t          cc_lock;
518         cfs_list_t              cc_req_list;   /* waiting reqs linked here */
519         cfs_list_t              cc_gc_chain;   /* linked to gc chain */
520 };
521
522 /**
523  * client side policy operation vector.
524  */
525 struct ptlrpc_sec_cops {
526         /**
527          * Given an \a imp, create and initialize a ptlrpc_sec structure.
528          * \param ctx service context:
529          * - regular import: \a ctx should be NULL;
530          * - reverse import: \a ctx is obtained from incoming request.
531          * \param flavor specify what flavor to use.
532          *
533          * When necessary, policy module is responsible for taking reference
534          * on the import.
535          *
536          * \see null_create_sec(), plain_create_sec(), gss_sec_create_kr().
537          */
538         struct ptlrpc_sec *     (*create_sec)  (struct obd_import *imp,
539                                                 struct ptlrpc_svc_ctx *ctx,
540                                                 struct sptlrpc_flavor *flavor);
541
542         /**
543          * Destructor of ptlrpc_sec. When called, refcount has been dropped
544          * to 0 and all contexts has been destroyed.
545          *
546          * \see null_destroy_sec(), plain_destroy_sec(), gss_sec_destroy_kr().
547          */
548         void                    (*destroy_sec) (struct ptlrpc_sec *sec);
549
550         /**
551          * Notify that this ptlrpc_sec is going to die. Optionally, policy
552          * module is supposed to set sec->ps_dying and whatever necessary
553          * actions.
554          *
555          * \see plain_kill_sec(), gss_sec_kill().
556          */
557         void                    (*kill_sec)    (struct ptlrpc_sec *sec);
558
559         /**
560          * Given \a vcred, lookup and/or create its context. The policy module
561          * is supposed to maintain its own context cache.
562          * XXX currently \a create and \a remove_dead is always 1, perhaps
563          * should be removed completely.
564          *
565          * \see null_lookup_ctx(), plain_lookup_ctx(), gss_sec_lookup_ctx_kr().
566          */
567         struct ptlrpc_cli_ctx * (*lookup_ctx)  (struct ptlrpc_sec *sec,
568                                                 struct vfs_cred *vcred,
569                                                 int create,
570                                                 int remove_dead);
571
572         /**
573          * Called then the reference of \a ctx dropped to 0. The policy module
574          * is supposed to destroy this context or whatever else according to
575          * its cache maintainance mechamism.
576          *
577          * \param sync if zero, we shouldn't wait for the context being
578          * destroyed completely.
579          *
580          * \see plain_release_ctx(), gss_sec_release_ctx_kr().
581          */
582         void                    (*release_ctx) (struct ptlrpc_sec *sec,
583                                                 struct ptlrpc_cli_ctx *ctx,
584                                                 int sync);
585
586         /**
587          * Flush the context cache.
588          *
589          * \param uid context of which user, -1 means all contexts.
590          * \param grace if zero, the PTLRPC_CTX_UPTODATE_BIT of affected
591          * contexts should be cleared immediately.
592          * \param force if zero, only idle contexts will be flushed.
593          *
594          * \see plain_flush_ctx_cache(), gss_sec_flush_ctx_cache_kr().
595          */
596         int                     (*flush_ctx_cache)
597                                                (struct ptlrpc_sec *sec,
598                                                 uid_t uid,
599                                                 int grace,
600                                                 int force);
601
602         /**
603          * Called periodically by garbage collector to remove dead contexts
604          * from cache.
605          *
606          * \see gss_sec_gc_ctx_kr().
607          */
608         void                    (*gc_ctx)      (struct ptlrpc_sec *sec);
609
610         /**
611          * Given an context \a ctx, install a corresponding reverse service
612          * context on client side.
613          * XXX currently it's only used by GSS module, maybe we should remove
614          * this from general API.
615          */
616         int                     (*install_rctx)(struct obd_import *imp,
617                                                 struct ptlrpc_sec *sec,
618                                                 struct ptlrpc_cli_ctx *ctx);
619
620         /**
621          * To allocate request buffer for \a req.
622          *
623          * \pre req->rq_reqmsg == NULL.
624          * \pre req->rq_reqbuf == NULL, otherwise it must be pre-allocated,
625          * we are not supposed to free it.
626          * \post if success, req->rq_reqmsg point to a buffer with size
627          * at least \a lustre_msg_size.
628          *
629          * \see null_alloc_reqbuf(), plain_alloc_reqbuf(), gss_alloc_reqbuf().
630          */
631         int                     (*alloc_reqbuf)(struct ptlrpc_sec *sec,
632                                                 struct ptlrpc_request *req,
633                                                 int lustre_msg_size);
634
635         /**
636          * To free request buffer for \a req.
637          *
638          * \pre req->rq_reqbuf != NULL.
639          *
640          * \see null_free_reqbuf(), plain_free_reqbuf(), gss_free_reqbuf().
641          */
642         void                    (*free_reqbuf) (struct ptlrpc_sec *sec,
643                                                 struct ptlrpc_request *req);
644
645         /**
646          * To allocate reply buffer for \a req.
647          *
648          * \pre req->rq_repbuf == NULL.
649          * \post if success, req->rq_repbuf point to a buffer with size
650          * req->rq_repbuf_len, the size should be large enough to receive
651          * reply which be transformed from \a lustre_msg_size of clear text.
652          *
653          * \see null_alloc_repbuf(), plain_alloc_repbuf(), gss_alloc_repbuf().
654          */
655         int                     (*alloc_repbuf)(struct ptlrpc_sec *sec,
656                                                 struct ptlrpc_request *req,
657                                                 int lustre_msg_size);
658
659         /**
660          * To free reply buffer for \a req.
661          *
662          * \pre req->rq_repbuf != NULL.
663          * \post req->rq_repbuf == NULL.
664          * \post req->rq_repbuf_len == 0.
665          *
666          * \see null_free_repbuf(), plain_free_repbuf(), gss_free_repbuf().
667          */
668         void                    (*free_repbuf) (struct ptlrpc_sec *sec,
669                                                 struct ptlrpc_request *req);
670
671         /**
672          * To expand the request buffer of \a req, thus the \a segment in
673          * the request message pointed by req->rq_reqmsg can accommodate
674          * at least \a newsize of data.
675          *
676          * \pre req->rq_reqmsg->lm_buflens[segment] < newsize.
677          *
678          * \see null_enlarge_reqbuf(), plain_enlarge_reqbuf(),
679          * gss_enlarge_reqbuf().
680          */
681         int                     (*enlarge_reqbuf)
682                                                (struct ptlrpc_sec *sec,
683                                                 struct ptlrpc_request *req,
684                                                 int segment, int newsize);
685         /*
686          * misc
687          */
688         int                     (*display)     (struct ptlrpc_sec *sec,
689                                                 struct seq_file *seq);
690 };
691
692 /**
693  * server side policy operation vector.
694  */
695 struct ptlrpc_sec_sops {
696         /**
697          * verify an incoming request.
698          *
699          * \pre request message is pointed by req->rq_reqbuf, size is
700          * req->rq_reqdata_len; and the message has been unpacked to
701          * host byte order.
702          *
703          * \retval SECSVC_OK success, req->rq_reqmsg point to request message
704          * in clear text, size is req->rq_reqlen; req->rq_svc_ctx is set;
705          * req->rq_sp_from is decoded from request.
706          * \retval SECSVC_COMPLETE success, the request has been fully
707          * processed, and reply message has been prepared; req->rq_sp_from is
708          * decoded from request.
709          * \retval SECSVC_DROP failed, this request should be dropped.
710          *
711          * \see null_accept(), plain_accept(), gss_svc_accept_kr().
712          */
713         int                     (*accept)      (struct ptlrpc_request *req);
714
715         /**
716          * Perform security transformation upon reply message.
717          *
718          * \pre reply message is pointed by req->rq_reply_state->rs_msg, size
719          * is req->rq_replen.
720          * \post req->rs_repdata_len is the final message size.
721          * \post req->rq_reply_off is set.
722          *
723          * \see null_authorize(), plain_authorize(), gss_svc_authorize().
724          */
725         int                     (*authorize)   (struct ptlrpc_request *req);
726
727         /**
728          * Invalidate server context \a ctx.
729          *
730          * \see gss_svc_invalidate_ctx().
731          */
732         void                    (*invalidate_ctx)
733                                                (struct ptlrpc_svc_ctx *ctx);
734
735         /**
736          * Allocate a ptlrpc_reply_state.
737          *
738          * \param msgsize size of the reply message in clear text.
739          * \pre if req->rq_reply_state != NULL, then it's pre-allocated, we
740          * should simply use it; otherwise we'll responsible for allocating
741          * a new one.
742          * \post req->rq_reply_state != NULL;
743          * \post req->rq_reply_state->rs_msg != NULL;
744          *
745          * \see null_alloc_rs(), plain_alloc_rs(), gss_svc_alloc_rs().
746          */
747         int                     (*alloc_rs)    (struct ptlrpc_request *req,
748                                                 int msgsize);
749
750         /**
751          * Free a ptlrpc_reply_state.
752          */
753         void                    (*free_rs)     (struct ptlrpc_reply_state *rs);
754
755         /**
756          * Release the server context \a ctx.
757          *
758          * \see gss_svc_free_ctx().
759          */
760         void                    (*free_ctx)    (struct ptlrpc_svc_ctx *ctx);
761
762         /**
763          * Install a reverse context based on the server context \a ctx.
764          *
765          * \see gss_svc_install_rctx_kr().
766          */
767         int                     (*install_rctx)(struct obd_import *imp,
768                                                 struct ptlrpc_svc_ctx *ctx);
769
770         /**
771          * Prepare buffer for incoming bulk write.
772          *
773          * \pre desc->bd_iov and desc->bd_iov_count describes the buffer
774          * intended to receive the write.
775          *
776          * \see gss_svc_prep_bulk().
777          */
778         int                     (*prep_bulk)   (struct ptlrpc_request *req,
779                                                 struct ptlrpc_bulk_desc *desc);
780
781         /**
782          * Unwrap the bulk write data.
783          *
784          * \see plain_svc_unwrap_bulk(), gss_svc_unwrap_bulk().
785          */
786         int                     (*unwrap_bulk) (struct ptlrpc_request *req,
787                                                 struct ptlrpc_bulk_desc *desc);
788
789         /**
790          * Wrap the bulk read data.
791          *
792          * \see plain_svc_wrap_bulk(), gss_svc_wrap_bulk().
793          */
794         int                     (*wrap_bulk)   (struct ptlrpc_request *req,
795                                                 struct ptlrpc_bulk_desc *desc);
796 };
797
798 struct ptlrpc_sec_policy {
799         cfs_module_t                   *sp_owner;
800         char                           *sp_name;
801         __u16                           sp_policy; /* policy number */
802         struct ptlrpc_sec_cops         *sp_cops;   /* client ops */
803         struct ptlrpc_sec_sops         *sp_sops;   /* server ops */
804 };
805
806 #define PTLRPC_SEC_FL_REVERSE           0x0001 /* reverse sec */
807 #define PTLRPC_SEC_FL_ROOTONLY          0x0002 /* treat everyone as root */
808 #define PTLRPC_SEC_FL_UDESC             0x0004 /* ship udesc */
809 #define PTLRPC_SEC_FL_BULK              0x0008 /* intensive bulk i/o expected */
810 #define PTLRPC_SEC_FL_PAG               0x0010 /* PAG mode */
811
812 /**
813  * The ptlrpc_sec represents the client side ptlrpc security facilities,
814  * each obd_import (both regular and reverse import) must associate with
815  * a ptlrpc_sec.
816  *
817  * \see sptlrpc_import_sec_adapt().
818  */
819 struct ptlrpc_sec {
820         struct ptlrpc_sec_policy       *ps_policy;
821         cfs_atomic_t                    ps_refcount;
822         /** statistic only */
823         cfs_atomic_t                    ps_nctx;
824         /** unique identifier */
825         int                             ps_id;
826         struct sptlrpc_flavor           ps_flvr;
827         enum lustre_sec_part            ps_part;
828         /** after set, no more new context will be created */
829         unsigned int                    ps_dying:1;
830         /** owning import */
831         struct obd_import              *ps_import;
832         cfs_spinlock_t                  ps_lock;
833
834         /*
835          * garbage collection
836          */
837         cfs_list_t                      ps_gc_list;
838         cfs_time_t                      ps_gc_interval; /* in seconds */
839         cfs_time_t                      ps_gc_next;     /* in seconds */
840 };
841
842 static inline int sec_is_reverse(struct ptlrpc_sec *sec)
843 {
844         return (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE);
845 }
846
847 static inline int sec_is_rootonly(struct ptlrpc_sec *sec)
848 {
849         return (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_ROOTONLY);
850 }
851
852
853 struct ptlrpc_svc_ctx {
854         cfs_atomic_t                    sc_refcount;
855         struct ptlrpc_sec_policy       *sc_policy;
856 };
857
858 /*
859  * user identity descriptor
860  */
861 #define LUSTRE_MAX_GROUPS               (128)
862
863 struct ptlrpc_user_desc {
864         __u32           pud_uid;
865         __u32           pud_gid;
866         __u32           pud_fsuid;
867         __u32           pud_fsgid;
868         __u32           pud_cap;
869         __u32           pud_ngroups;
870         __u32           pud_groups[0];
871 };
872
873 /*
874  * bulk flavors
875  */
876 enum sptlrpc_bulk_hash_alg {
877         BULK_HASH_ALG_NULL      = 0,
878         BULK_HASH_ALG_ADLER32,
879         BULK_HASH_ALG_CRC32,
880         BULK_HASH_ALG_MD5,
881         BULK_HASH_ALG_SHA1,
882         BULK_HASH_ALG_SHA256,
883         BULK_HASH_ALG_SHA384,
884         BULK_HASH_ALG_SHA512,
885         BULK_HASH_ALG_MAX
886 };
887
888 struct sptlrpc_hash_type {
889         char           *sht_name;
890         char           *sht_tfm_name;
891         unsigned int    sht_size;
892 };
893
894 const struct sptlrpc_hash_type *sptlrpc_get_hash_type(__u8 hash_alg);
895 const char * sptlrpc_get_hash_name(__u8 hash_alg);
896 __u8 sptlrpc_get_hash_alg(const char *algname);
897
898 enum {
899         BSD_FL_ERR      = 1,
900 };
901
902 struct ptlrpc_bulk_sec_desc {
903         __u8            bsd_version;    /* 0 */
904         __u8            bsd_type;       /* SPTLRPC_BULK_XXX */
905         __u8            bsd_svc;        /* SPTLRPC_BULK_SVC_XXXX */
906         __u8            bsd_flags;      /* flags */
907         __u32           bsd_nob;        /* nob of bulk data */
908         __u8            bsd_data[0];    /* policy-specific token */
909 };
910
911
912 /*
913  * lprocfs
914  */
915 struct proc_dir_entry;
916 extern struct proc_dir_entry *sptlrpc_proc_root;
917
918 /*
919  * round size up to next power of 2, for slab allocation.
920  * @size must be sane (can't overflow after round up)
921  */
922 static inline int size_roundup_power2(int size)
923 {
924         size--;
925         size |= size >> 1;
926         size |= size >> 2;
927         size |= size >> 4;
928         size |= size >> 8;
929         size |= size >> 16;
930         size++;
931         return size;
932 }
933
934 /*
935  * internal support libraries
936  */
937 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
938                                   int segment, int newsize);
939
940 /*
941  * security policies
942  */
943 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy);
944 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy);
945
946 __u32 sptlrpc_name2flavor_base(const char *name);
947 const char *sptlrpc_flavor2name_base(__u32 flvr);
948 char *sptlrpc_flavor2name_bulk(struct sptlrpc_flavor *sf,
949                                char *buf, int bufsize);
950 char *sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize);
951 char *sptlrpc_secflags2str(__u32 flags, char *buf, int bufsize);
952
953 static inline
954 struct ptlrpc_sec_policy *sptlrpc_policy_get(struct ptlrpc_sec_policy *policy)
955 {
956         __cfs_module_get(policy->sp_owner);
957         return policy;
958 }
959
960 static inline
961 void sptlrpc_policy_put(struct ptlrpc_sec_policy *policy)
962 {
963         cfs_module_put(policy->sp_owner);
964 }
965
966 /*
967  * client credential
968  */
969 static inline
970 unsigned long cli_ctx_status(struct ptlrpc_cli_ctx *ctx)
971 {
972         return (ctx->cc_flags & PTLRPC_CTX_STATUS_MASK);
973 }
974
975 static inline
976 int cli_ctx_is_ready(struct ptlrpc_cli_ctx *ctx)
977 {
978         return (cli_ctx_status(ctx) == PTLRPC_CTX_UPTODATE);
979 }
980
981 static inline
982 int cli_ctx_is_refreshed(struct ptlrpc_cli_ctx *ctx)
983 {
984         return (cli_ctx_status(ctx) != 0);
985 }
986
987 static inline
988 int cli_ctx_is_uptodate(struct ptlrpc_cli_ctx *ctx)
989 {
990         return ((ctx->cc_flags & PTLRPC_CTX_UPTODATE) != 0);
991 }
992
993 static inline
994 int cli_ctx_is_error(struct ptlrpc_cli_ctx *ctx)
995 {
996         return ((ctx->cc_flags & PTLRPC_CTX_ERROR) != 0);
997 }
998
999 static inline
1000 int cli_ctx_is_dead(struct ptlrpc_cli_ctx *ctx)
1001 {
1002         return ((ctx->cc_flags & (PTLRPC_CTX_DEAD | PTLRPC_CTX_ERROR)) != 0);
1003 }
1004
1005 static inline
1006 int cli_ctx_is_eternal(struct ptlrpc_cli_ctx *ctx)
1007 {
1008         return ((ctx->cc_flags & PTLRPC_CTX_ETERNAL) != 0);
1009 }
1010
1011 /*
1012  * sec get/put
1013  */
1014 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec);
1015 void sptlrpc_sec_put(struct ptlrpc_sec *sec);
1016
1017 /*
1018  * internal apis which only used by policy impelentation
1019  */
1020 int  sptlrpc_get_next_secid(void);
1021 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec);
1022
1023 /*
1024  * exported client context api
1025  */
1026 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx);
1027 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync);
1028 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx);
1029 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx);
1030 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize);
1031
1032 /*
1033  * exported client context wrap/buffers
1034  */
1035 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req);
1036 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req);
1037 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize);
1038 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req);
1039 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize);
1040 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req);
1041 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1042                                int segment, int newsize);
1043 int  sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
1044                                     struct ptlrpc_request **req_ret);
1045 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req);
1046
1047 void sptlrpc_request_out_callback(struct ptlrpc_request *req);
1048
1049 /*
1050  * exported higher interface of import & request
1051  */
1052 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1053                              struct ptlrpc_svc_ctx *ctx,
1054                              struct sptlrpc_flavor *flvr);
1055 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp);
1056 void sptlrpc_import_sec_put(struct obd_import *imp);
1057
1058 int  sptlrpc_import_check_ctx(struct obd_import *imp);
1059 void sptlrpc_import_flush_root_ctx(struct obd_import *imp);
1060 void sptlrpc_import_flush_my_ctx(struct obd_import *imp);
1061 void sptlrpc_import_flush_all_ctx(struct obd_import *imp);
1062 int  sptlrpc_req_get_ctx(struct ptlrpc_request *req);
1063 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync);
1064 int  sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout);
1065 int  sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req);
1066 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode);
1067
1068 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule);
1069
1070 /* gc */
1071 void sptlrpc_gc_add_sec(struct ptlrpc_sec *sec);
1072 void sptlrpc_gc_del_sec(struct ptlrpc_sec *sec);
1073 void sptlrpc_gc_add_ctx(struct ptlrpc_cli_ctx *ctx);
1074
1075 /* misc */
1076 const char * sec2target_str(struct ptlrpc_sec *sec);
1077 int sptlrpc_lprocfs_cliobd_attach(struct obd_device *dev);
1078
1079 /*
1080  * server side
1081  */
1082 enum secsvc_accept_res {
1083         SECSVC_OK       = 0,
1084         SECSVC_COMPLETE,
1085         SECSVC_DROP,
1086 };
1087
1088 int  sptlrpc_svc_unwrap_request(struct ptlrpc_request *req);
1089 int  sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen);
1090 int  sptlrpc_svc_wrap_reply(struct ptlrpc_request *req);
1091 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs);
1092 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req);
1093 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req);
1094 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req);
1095
1096 int  sptlrpc_target_export_check(struct obd_export *exp,
1097                                  struct ptlrpc_request *req);
1098 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
1099                                       struct sptlrpc_rule_set *rset);
1100
1101 /*
1102  * reverse context
1103  */
1104 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1105                                 struct ptlrpc_svc_ctx *ctx);
1106 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1107                                 struct ptlrpc_cli_ctx *ctx);
1108
1109 /* bulk security api */
1110 int sptlrpc_enc_pool_add_user(void);
1111 int sptlrpc_enc_pool_del_user(void);
1112 int  sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc);
1113 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc);
1114
1115 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
1116                           struct ptlrpc_bulk_desc *desc);
1117 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
1118                                  struct ptlrpc_bulk_desc *desc,
1119                                  int nob);
1120 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
1121                                   struct ptlrpc_bulk_desc *desc);
1122 int sptlrpc_svc_prep_bulk(struct ptlrpc_request *req,
1123                           struct ptlrpc_bulk_desc *desc);
1124 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
1125                           struct ptlrpc_bulk_desc *desc);
1126 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
1127                             struct ptlrpc_bulk_desc *desc);
1128
1129 /* bulk helpers (internal use only by policies) */
1130 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
1131                               void *buf, int buflen);
1132
1133 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed);
1134
1135 /* user descriptor helpers */
1136 static inline int sptlrpc_user_desc_size(int ngroups)
1137 {
1138         return sizeof(struct ptlrpc_user_desc) + ngroups * sizeof(__u32);
1139 }
1140
1141 int sptlrpc_current_user_desc_size(void);
1142 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset);
1143 int sptlrpc_unpack_user_desc(struct lustre_msg *req, int offset, int swabbed);
1144
1145
1146 #define CFS_CAP_CHOWN_MASK (1 << CFS_CAP_CHOWN)
1147 #define CFS_CAP_SYS_RESOURCE_MASK (1 << CFS_CAP_SYS_RESOURCE)
1148
1149 enum {
1150         LUSTRE_SEC_NONE         = 0,
1151         LUSTRE_SEC_REMOTE       = 1,
1152         LUSTRE_SEC_SPECIFY      = 2,
1153         LUSTRE_SEC_ALL          = 3
1154 };
1155
1156 /** @} sptlrpc */
1157
1158 #endif /* _LUSTRE_SEC_H_ */