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