Whamcloud - gitweb
b=22470 Put header file contents into a doxygen group.
[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  2008 Sun Microsystems, Inc. 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 sec sec
41  *
42  * @{
43  */
44
45 /*
46  * to avoid include
47  */
48 struct key;
49 struct obd_import;
50 struct obd_export;
51 struct ptlrpc_request;
52 struct ptlrpc_reply_state;
53 struct ptlrpc_bulk_desc;
54 struct brw_page;
55 struct seq_file;
56
57 /*
58  * forward declaration
59  */
60 struct ptlrpc_sec_policy;
61 struct ptlrpc_sec_cops;
62 struct ptlrpc_sec_sops;
63 struct ptlrpc_sec;
64 struct ptlrpc_svc_ctx;
65 struct ptlrpc_cli_ctx;
66 struct ptlrpc_ctx_ops;
67
68 /*
69  * flavor constants
70  */
71 enum sptlrpc_policy {
72         SPTLRPC_POLICY_NULL             = 0,
73         SPTLRPC_POLICY_PLAIN            = 1,
74         SPTLRPC_POLICY_GSS              = 2,
75         SPTLRPC_POLICY_MAX,
76 };
77
78 enum sptlrpc_mech_null {
79         SPTLRPC_MECH_NULL               = 0,
80         SPTLRPC_MECH_NULL_MAX,
81 };
82
83 enum sptlrpc_mech_plain {
84         SPTLRPC_MECH_PLAIN              = 0,
85         SPTLRPC_MECH_PLAIN_MAX,
86 };
87
88 enum sptlrpc_mech_gss {
89         SPTLRPC_MECH_GSS_NULL           = 0,
90         SPTLRPC_MECH_GSS_KRB5           = 1,
91         SPTLRPC_MECH_GSS_MAX,
92 };
93
94 enum sptlrpc_service_type {
95         SPTLRPC_SVC_NULL                = 0,    /* no security */
96         SPTLRPC_SVC_AUTH                = 1,    /* auth only */
97         SPTLRPC_SVC_INTG                = 2,    /* integrity */
98         SPTLRPC_SVC_PRIV                = 3,    /* privacy */
99         SPTLRPC_SVC_MAX,
100 };
101
102 enum sptlrpc_bulk_type {
103         SPTLRPC_BULK_DEFAULT            = 0,    /* follow rpc flavor */
104         SPTLRPC_BULK_HASH               = 1,    /* hash integrity */
105         SPTLRPC_BULK_MAX,
106 };
107
108 enum sptlrpc_bulk_service {
109         SPTLRPC_BULK_SVC_NULL           = 0,
110         SPTLRPC_BULK_SVC_AUTH           = 1,
111         SPTLRPC_BULK_SVC_INTG           = 2,
112         SPTLRPC_BULK_SVC_PRIV           = 3,
113         SPTLRPC_BULK_SVC_MAX,
114 };
115
116 /*
117  * rpc flavor compose/extract, represented as 32 bits. currently the
118  * high 12 bits are unused, must be set as 0.
119  *
120  * 4b (bulk svc) | 4b (bulk type) | 4b (svc) | 4b (mech)  | 4b (policy)
121  */
122 #define FLVR_POLICY_OFFSET              (0)
123 #define FLVR_MECH_OFFSET                (4)
124 #define FLVR_SVC_OFFSET                 (8)
125 #define FLVR_BULK_TYPE_OFFSET           (12)
126 #define FLVR_BULK_SVC_OFFSET            (16)
127
128 #define MAKE_FLVR(policy, mech, svc, btype, bsvc)                       \
129         (((__u32)(policy) << FLVR_POLICY_OFFSET) |                      \
130          ((__u32)(mech) << FLVR_MECH_OFFSET) |                          \
131          ((__u32)(svc) << FLVR_SVC_OFFSET) |                            \
132          ((__u32)(btype) << FLVR_BULK_TYPE_OFFSET) |                    \
133          ((__u32)(bsvc) << FLVR_BULK_SVC_OFFSET))
134
135 /*
136  * extraction
137  */
138 #define SPTLRPC_FLVR_POLICY(flavor)                                     \
139         ((((__u32)(flavor)) >> FLVR_POLICY_OFFSET) & 0xF)
140 #define SPTLRPC_FLVR_MECH(flavor)                                       \
141         ((((__u32)(flavor)) >> FLVR_MECH_OFFSET) & 0xF)
142 #define SPTLRPC_FLVR_SVC(flavor)                                        \
143         ((((__u32)(flavor)) >> FLVR_SVC_OFFSET) & 0xF)
144 #define SPTLRPC_FLVR_BULK_TYPE(flavor)                                  \
145         ((((__u32)(flavor)) >> FLVR_BULK_TYPE_OFFSET) & 0xF)
146 #define SPTLRPC_FLVR_BULK_SVC(flavor)                                   \
147         ((((__u32)(flavor)) >> FLVR_BULK_SVC_OFFSET) & 0xF)
148
149 #define SPTLRPC_FLVR_BASE(flavor)                                       \
150         ((((__u32)(flavor)) >> FLVR_POLICY_OFFSET) & 0xFFF)
151 #define SPTLRPC_FLVR_BASE_SUB(flavor)                                   \
152         ((((__u32)(flavor)) >> FLVR_MECH_OFFSET) & 0xFF)
153
154 /*
155  * gss subflavors
156  */
157 #define MAKE_BASE_SUBFLVR(mech, svc)                                    \
158         ((__u32)(mech) |                                                \
159          ((__u32)(svc) << (FLVR_SVC_OFFSET - FLVR_MECH_OFFSET)))
160
161 #define SPTLRPC_SUBFLVR_KRB5N                                           \
162         MAKE_BASE_SUBFLVR(SPTLRPC_MECH_GSS_KRB5, SPTLRPC_SVC_NULL)
163 #define SPTLRPC_SUBFLVR_KRB5A                                           \
164         MAKE_BASE_SUBFLVR(SPTLRPC_MECH_GSS_KRB5, SPTLRPC_SVC_AUTH)
165 #define SPTLRPC_SUBFLVR_KRB5I                                           \
166         MAKE_BASE_SUBFLVR(SPTLRPC_MECH_GSS_KRB5, SPTLRPC_SVC_INTG)
167 #define SPTLRPC_SUBFLVR_KRB5P                                           \
168         MAKE_BASE_SUBFLVR(SPTLRPC_MECH_GSS_KRB5, SPTLRPC_SVC_PRIV)
169
170 /*
171  * "end user" flavors
172  */
173 #define SPTLRPC_FLVR_NULL                               \
174         MAKE_FLVR(SPTLRPC_POLICY_NULL,                  \
175                   SPTLRPC_MECH_NULL,                    \
176                   SPTLRPC_SVC_NULL,                     \
177                   SPTLRPC_BULK_DEFAULT,                 \
178                   SPTLRPC_BULK_SVC_NULL)
179 #define SPTLRPC_FLVR_PLAIN                              \
180         MAKE_FLVR(SPTLRPC_POLICY_PLAIN,                 \
181                   SPTLRPC_MECH_PLAIN,                   \
182                   SPTLRPC_SVC_NULL,                     \
183                   SPTLRPC_BULK_HASH,                    \
184                   SPTLRPC_BULK_SVC_INTG)
185 #define SPTLRPC_FLVR_KRB5N                              \
186         MAKE_FLVR(SPTLRPC_POLICY_GSS,                   \
187                   SPTLRPC_MECH_GSS_KRB5,                \
188                   SPTLRPC_SVC_NULL,                     \
189                   SPTLRPC_BULK_DEFAULT,                 \
190                   SPTLRPC_BULK_SVC_NULL)
191 #define SPTLRPC_FLVR_KRB5A                              \
192         MAKE_FLVR(SPTLRPC_POLICY_GSS,                   \
193                   SPTLRPC_MECH_GSS_KRB5,                \
194                   SPTLRPC_SVC_AUTH,                     \
195                   SPTLRPC_BULK_DEFAULT,                 \
196                   SPTLRPC_BULK_SVC_NULL)
197 #define SPTLRPC_FLVR_KRB5I                              \
198         MAKE_FLVR(SPTLRPC_POLICY_GSS,                   \
199                   SPTLRPC_MECH_GSS_KRB5,                \
200                   SPTLRPC_SVC_INTG,                     \
201                   SPTLRPC_BULK_DEFAULT,                 \
202                   SPTLRPC_BULK_SVC_INTG)
203 #define SPTLRPC_FLVR_KRB5P                              \
204         MAKE_FLVR(SPTLRPC_POLICY_GSS,                   \
205                   SPTLRPC_MECH_GSS_KRB5,                \
206                   SPTLRPC_SVC_PRIV,                     \
207                   SPTLRPC_BULK_DEFAULT,                 \
208                   SPTLRPC_BULK_SVC_PRIV)
209
210 #define SPTLRPC_FLVR_DEFAULT            SPTLRPC_FLVR_NULL
211
212 #define SPTLRPC_FLVR_INVALID            ((__u32) 0xFFFFFFFF)
213 #define SPTLRPC_FLVR_ANY                ((__u32) 0xFFF00000)
214
215 /*
216  * extract the useful part from wire flavor
217  */
218 #define WIRE_FLVR(wflvr)                (((__u32) (wflvr)) & 0x000FFFFF)
219
220 static inline void flvr_set_svc(__u32 *flvr, __u32 svc)
221 {
222         LASSERT(svc < SPTLRPC_SVC_MAX);
223         *flvr = MAKE_FLVR(SPTLRPC_FLVR_POLICY(*flvr),
224                           SPTLRPC_FLVR_MECH(*flvr),
225                           svc,
226                           SPTLRPC_FLVR_BULK_TYPE(*flvr),
227                           SPTLRPC_FLVR_BULK_SVC(*flvr));
228 }
229
230 static inline void flvr_set_bulk_svc(__u32 *flvr, __u32 svc)
231 {
232         LASSERT(svc < SPTLRPC_BULK_SVC_MAX);
233         *flvr = MAKE_FLVR(SPTLRPC_FLVR_POLICY(*flvr),
234                           SPTLRPC_FLVR_MECH(*flvr),
235                           SPTLRPC_FLVR_SVC(*flvr),
236                           SPTLRPC_FLVR_BULK_TYPE(*flvr),
237                           svc);
238 }
239
240 struct bulk_spec_hash {
241         __u8    hash_alg;
242 };
243
244 struct sptlrpc_flavor {
245         __u32   sf_rpc;         /* wire flavor - should be renamed to sf_wire */
246         __u32   sf_flags;       /* general flags */
247         /*
248          * rpc flavor specification
249          */
250         union {
251                 /* nothing for now */
252         } u_rpc;
253         /*
254          * bulk flavor specification
255          */
256         union {
257                 struct bulk_spec_hash hash;
258         } u_bulk;
259 };
260
261 enum lustre_sec_part {
262         LUSTRE_SP_CLI           = 0,
263         LUSTRE_SP_MDT,
264         LUSTRE_SP_OST,
265         LUSTRE_SP_MGC,
266         LUSTRE_SP_MGS,
267         LUSTRE_SP_ANY           = 0xFF
268 };
269
270 const char *sptlrpc_part2name(enum lustre_sec_part sp);
271 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd);
272
273 struct sptlrpc_rule {
274         __u32                   sr_netid;   /* LNET network ID */
275         __u8                    sr_from;    /* sec_part */
276         __u8                    sr_to;      /* sec_part */
277         __u16                   sr_padding;
278         struct sptlrpc_flavor   sr_flvr;
279 };
280
281 struct sptlrpc_rule_set {
282         int                     srs_nslot;
283         int                     srs_nrule;
284         struct sptlrpc_rule    *srs_rules;
285 };
286
287 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr);
288 int sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr);
289
290 static inline void sptlrpc_rule_set_init(struct sptlrpc_rule_set *set)
291 {
292         memset(set, 0, sizeof(*set));
293 }
294
295 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *set);
296 int  sptlrpc_rule_set_expand(struct sptlrpc_rule_set *set);
297 int  sptlrpc_rule_set_merge(struct sptlrpc_rule_set *set,
298                             struct sptlrpc_rule *rule);
299 int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
300                             enum lustre_sec_part from,
301                             enum lustre_sec_part to,
302                             lnet_nid_t nid,
303                             struct sptlrpc_flavor *sf);
304 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *set);
305
306 int  sptlrpc_process_config(struct lustre_cfg *lcfg);
307 void sptlrpc_conf_log_start(const char *logname);
308 void sptlrpc_conf_log_stop(const char *logname);
309 void sptlrpc_conf_log_update_begin(const char *logname);
310 void sptlrpc_conf_log_update_end(const char *logname);
311 void sptlrpc_conf_client_adapt(struct obd_device *obd);
312 int  sptlrpc_conf_target_get_rules(struct obd_device *obd,
313                                    struct sptlrpc_rule_set *rset,
314                                    int initial);
315 void sptlrpc_target_choose_flavor(struct sptlrpc_rule_set *rset,
316                                   enum lustre_sec_part from,
317                                   lnet_nid_t nid,
318                                   struct sptlrpc_flavor *flavor);
319
320 /* The maximum length of security payload. 1024 is enough for Kerberos 5,
321  * and should be enough for other future mechanisms but not sure.
322  * Only used by pre-allocated request/reply pool.
323  */
324 #define SPTLRPC_MAX_PAYLOAD     (1024)
325
326
327 struct vfs_cred {
328         uint32_t        vc_uid;
329         uint32_t        vc_gid;
330 };
331
332 struct ptlrpc_ctx_ops {
333         int     (*match)       (struct ptlrpc_cli_ctx *ctx,
334                                 struct vfs_cred *vcred);
335         int     (*refresh)     (struct ptlrpc_cli_ctx *ctx);
336         int     (*validate)    (struct ptlrpc_cli_ctx *ctx);
337         void    (*die)         (struct ptlrpc_cli_ctx *ctx,
338                                 int grace);
339         int     (*display)     (struct ptlrpc_cli_ctx *ctx,
340                                 char *buf, int bufsize);
341         /*
342          * rpc data transform
343          */
344         int     (*sign)        (struct ptlrpc_cli_ctx *ctx,
345                                 struct ptlrpc_request *req);
346         int     (*verify)      (struct ptlrpc_cli_ctx *ctx,
347                                 struct ptlrpc_request *req);
348         int     (*seal)        (struct ptlrpc_cli_ctx *ctx,
349                                 struct ptlrpc_request *req);
350         int     (*unseal)      (struct ptlrpc_cli_ctx *ctx,
351                                 struct ptlrpc_request *req);
352         /*
353          * bulk transform
354          */
355         int     (*wrap_bulk)   (struct ptlrpc_cli_ctx *ctx,
356                                 struct ptlrpc_request *req,
357                                 struct ptlrpc_bulk_desc *desc);
358         int     (*unwrap_bulk) (struct ptlrpc_cli_ctx *ctx,
359                                 struct ptlrpc_request *req,
360                                 struct ptlrpc_bulk_desc *desc);
361 };
362
363 #define PTLRPC_CTX_NEW_BIT             (0)  /* newly created */
364 #define PTLRPC_CTX_UPTODATE_BIT        (1)  /* uptodate */
365 #define PTLRPC_CTX_DEAD_BIT            (2)  /* mark expired gracefully */
366 #define PTLRPC_CTX_ERROR_BIT           (3)  /* fatal error (refresh, etc.) */
367 #define PTLRPC_CTX_CACHED_BIT          (8)  /* in ctx cache (hash etc.) */
368 #define PTLRPC_CTX_ETERNAL_BIT         (9)  /* always valid */
369
370 #define PTLRPC_CTX_NEW                 (1 << PTLRPC_CTX_NEW_BIT)
371 #define PTLRPC_CTX_UPTODATE            (1 << PTLRPC_CTX_UPTODATE_BIT)
372 #define PTLRPC_CTX_DEAD                (1 << PTLRPC_CTX_DEAD_BIT)
373 #define PTLRPC_CTX_ERROR               (1 << PTLRPC_CTX_ERROR_BIT)
374 #define PTLRPC_CTX_CACHED              (1 << PTLRPC_CTX_CACHED_BIT)
375 #define PTLRPC_CTX_ETERNAL             (1 << PTLRPC_CTX_ETERNAL_BIT)
376
377 #define PTLRPC_CTX_STATUS_MASK         (PTLRPC_CTX_NEW_BIT    |       \
378                                         PTLRPC_CTX_UPTODATE   |       \
379                                         PTLRPC_CTX_DEAD       |       \
380                                         PTLRPC_CTX_ERROR)
381
382 struct ptlrpc_cli_ctx {
383         cfs_hlist_node_t        cc_cache;      /* linked into ctx cache */
384         cfs_atomic_t            cc_refcount;
385         struct ptlrpc_sec      *cc_sec;
386         struct ptlrpc_ctx_ops  *cc_ops;
387         cfs_time_t              cc_expire;     /* in seconds */
388         unsigned int            cc_early_expire:1;
389         unsigned long           cc_flags;
390         struct vfs_cred         cc_vcred;
391         cfs_spinlock_t          cc_lock;
392         cfs_list_t              cc_req_list;   /* waiting reqs linked here */
393         cfs_list_t              cc_gc_chain;   /* linked to gc chain */
394 };
395
396 struct ptlrpc_sec_cops {
397         /*
398          * ptlrpc_sec constructor/destructor
399          */
400         struct ptlrpc_sec *     (*create_sec)  (struct obd_import *imp,
401                                                 struct ptlrpc_svc_ctx *ctx,
402                                                 struct sptlrpc_flavor *flavor);
403         void                    (*destroy_sec) (struct ptlrpc_sec *sec);
404
405         /*
406          * notify to-be-dead
407          */
408         void                    (*kill_sec)    (struct ptlrpc_sec *sec);
409
410         /*
411          * context
412          */
413         struct ptlrpc_cli_ctx * (*lookup_ctx)  (struct ptlrpc_sec *sec,
414                                                 struct vfs_cred *vcred,
415                                                 int create,
416                                                 int remove_dead);
417         void                    (*release_ctx) (struct ptlrpc_sec *sec,
418                                                 struct ptlrpc_cli_ctx *ctx,
419                                                 int sync);
420         int                     (*flush_ctx_cache)
421                                                (struct ptlrpc_sec *sec,
422                                                 uid_t uid,
423                                                 int grace,
424                                                 int force);
425         void                    (*gc_ctx)      (struct ptlrpc_sec *sec);
426
427         /*
428          * reverse context
429          */
430         int                     (*install_rctx)(struct obd_import *imp,
431                                                 struct ptlrpc_sec *sec,
432                                                 struct ptlrpc_cli_ctx *ctx);
433
434         /*
435          * request/reply buffer manipulation
436          */
437         int                     (*alloc_reqbuf)(struct ptlrpc_sec *sec,
438                                                 struct ptlrpc_request *req,
439                                                 int lustre_msg_size);
440         void                    (*free_reqbuf) (struct ptlrpc_sec *sec,
441                                                 struct ptlrpc_request *req);
442         int                     (*alloc_repbuf)(struct ptlrpc_sec *sec,
443                                                 struct ptlrpc_request *req,
444                                                 int lustre_msg_size);
445         void                    (*free_repbuf) (struct ptlrpc_sec *sec,
446                                                 struct ptlrpc_request *req);
447         int                     (*enlarge_reqbuf)
448                                                (struct ptlrpc_sec *sec,
449                                                 struct ptlrpc_request *req,
450                                                 int segment, int newsize);
451         /*
452          * misc
453          */
454         int                     (*display)     (struct ptlrpc_sec *sec,
455                                                 struct seq_file *seq);
456 };
457
458 struct ptlrpc_sec_sops {
459         int                     (*accept)      (struct ptlrpc_request *req);
460         int                     (*authorize)   (struct ptlrpc_request *req);
461         void                    (*invalidate_ctx)
462                                                (struct ptlrpc_svc_ctx *ctx);
463         /* buffer manipulation */
464         int                     (*alloc_rs)    (struct ptlrpc_request *req,
465                                                 int msgsize);
466         void                    (*free_rs)     (struct ptlrpc_reply_state *rs);
467         void                    (*free_ctx)    (struct ptlrpc_svc_ctx *ctx);
468         /* reverse context */
469         int                     (*install_rctx)(struct obd_import *imp,
470                                                 struct ptlrpc_svc_ctx *ctx);
471         /* bulk transform */
472         int                     (*prep_bulk)   (struct ptlrpc_request *req,
473                                                 struct ptlrpc_bulk_desc *desc);
474         int                     (*unwrap_bulk) (struct ptlrpc_request *req,
475                                                 struct ptlrpc_bulk_desc *desc);
476         int                     (*wrap_bulk)   (struct ptlrpc_request *req,
477                                                 struct ptlrpc_bulk_desc *desc);
478 };
479
480 struct ptlrpc_sec_policy {
481         cfs_module_t                   *sp_owner;
482         char                           *sp_name;
483         __u16                           sp_policy; /* policy number */
484         struct ptlrpc_sec_cops         *sp_cops;   /* client ops */
485         struct ptlrpc_sec_sops         *sp_sops;   /* server ops */
486 };
487
488 #define PTLRPC_SEC_FL_REVERSE           0x0001 /* reverse sec */
489 #define PTLRPC_SEC_FL_ROOTONLY          0x0002 /* treat everyone as root */
490 #define PTLRPC_SEC_FL_UDESC             0x0004 /* ship udesc */
491 #define PTLRPC_SEC_FL_BULK              0x0008 /* intensive bulk i/o expected */
492 #define PTLRPC_SEC_FL_PAG               0x0010 /* PAG mode */
493
494 struct ptlrpc_sec {
495         struct ptlrpc_sec_policy       *ps_policy;
496         cfs_atomic_t                    ps_refcount;
497         cfs_atomic_t                    ps_nctx;        /* statistic only */
498         int                             ps_id;          /* unique identifier */
499         struct sptlrpc_flavor           ps_flvr;        /* flavor */
500         enum lustre_sec_part            ps_part;
501         unsigned int                    ps_dying:1;
502         struct obd_import              *ps_import;      /* owning import */
503         cfs_spinlock_t                  ps_lock;        /* protect ccache */
504         /*
505          * garbage collection
506          */
507         cfs_list_t                      ps_gc_list;
508         cfs_time_t                      ps_gc_interval; /* in seconds */
509         cfs_time_t                      ps_gc_next;     /* in seconds */
510 };
511
512 static inline int sec_is_reverse(struct ptlrpc_sec *sec)
513 {
514         return (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE);
515 }
516
517 static inline int sec_is_rootonly(struct ptlrpc_sec *sec)
518 {
519         return (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_ROOTONLY);
520 }
521
522
523 struct ptlrpc_svc_ctx {
524         cfs_atomic_t                    sc_refcount;
525         struct ptlrpc_sec_policy       *sc_policy;
526 };
527
528 /*
529  * user identity descriptor
530  */
531 #define LUSTRE_MAX_GROUPS               (128)
532
533 struct ptlrpc_user_desc {
534         __u32           pud_uid;
535         __u32           pud_gid;
536         __u32           pud_fsuid;
537         __u32           pud_fsgid;
538         __u32           pud_cap;
539         __u32           pud_ngroups;
540         __u32           pud_groups[0];
541 };
542
543 /*
544  * bulk flavors
545  */
546 enum sptlrpc_bulk_hash_alg {
547         BULK_HASH_ALG_NULL      = 0,
548         BULK_HASH_ALG_ADLER32,
549         BULK_HASH_ALG_CRC32,
550         BULK_HASH_ALG_MD5,
551         BULK_HASH_ALG_SHA1,
552         BULK_HASH_ALG_SHA256,
553         BULK_HASH_ALG_SHA384,
554         BULK_HASH_ALG_SHA512,
555         BULK_HASH_ALG_MAX
556 };
557
558 struct sptlrpc_hash_type {
559         char           *sht_name;
560         char           *sht_tfm_name;
561         unsigned int    sht_size;
562 };
563
564 const struct sptlrpc_hash_type *sptlrpc_get_hash_type(__u8 hash_alg);
565 const char * sptlrpc_get_hash_name(__u8 hash_alg);
566 __u8 sptlrpc_get_hash_alg(const char *algname);
567
568 enum {
569         BSD_FL_ERR      = 1,
570 };
571
572 struct ptlrpc_bulk_sec_desc {
573         __u8            bsd_version;    /* 0 */
574         __u8            bsd_type;       /* SPTLRPC_BULK_XXX */
575         __u8            bsd_svc;        /* SPTLRPC_BULK_SVC_XXXX */
576         __u8            bsd_flags;      /* flags */
577         __u32           bsd_nob;        /* nob of bulk data */
578         __u8            bsd_data[0];    /* policy-specific token */
579 };
580
581
582 /*
583  * lprocfs
584  */
585 struct proc_dir_entry;
586 extern struct proc_dir_entry *sptlrpc_proc_root;
587
588 /*
589  * round size up to next power of 2, for slab allocation.
590  * @size must be sane (can't overflow after round up)
591  */
592 static inline int size_roundup_power2(int size)
593 {
594         size--;
595         size |= size >> 1;
596         size |= size >> 2;
597         size |= size >> 4;
598         size |= size >> 8;
599         size |= size >> 16;
600         size++;
601         return size;
602 }
603
604 /*
605  * internal support libraries
606  */
607 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
608                                   int segment, int newsize);
609
610 /*
611  * security type
612  */
613 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy);
614 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy);
615
616 __u32 sptlrpc_name2flavor_base(const char *name);
617 const char *sptlrpc_flavor2name_base(__u32 flvr);
618 char *sptlrpc_flavor2name_bulk(struct sptlrpc_flavor *sf,
619                                char *buf, int bufsize);
620 char *sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize);
621 char *sptlrpc_secflags2str(__u32 flags, char *buf, int bufsize);
622
623 static inline
624 struct ptlrpc_sec_policy *sptlrpc_policy_get(struct ptlrpc_sec_policy *policy)
625 {
626         __cfs_module_get(policy->sp_owner);
627         return policy;
628 }
629
630 static inline
631 void sptlrpc_policy_put(struct ptlrpc_sec_policy *policy)
632 {
633         cfs_module_put(policy->sp_owner);
634 }
635
636 /*
637  * client credential
638  */
639 static inline
640 unsigned long cli_ctx_status(struct ptlrpc_cli_ctx *ctx)
641 {
642         return (ctx->cc_flags & PTLRPC_CTX_STATUS_MASK);
643 }
644
645 static inline
646 int cli_ctx_is_ready(struct ptlrpc_cli_ctx *ctx)
647 {
648         return (cli_ctx_status(ctx) == PTLRPC_CTX_UPTODATE);
649 }
650
651 static inline
652 int cli_ctx_is_refreshed(struct ptlrpc_cli_ctx *ctx)
653 {
654         return (cli_ctx_status(ctx) != 0);
655 }
656
657 static inline
658 int cli_ctx_is_uptodate(struct ptlrpc_cli_ctx *ctx)
659 {
660         return ((ctx->cc_flags & PTLRPC_CTX_UPTODATE) != 0);
661 }
662
663 static inline
664 int cli_ctx_is_error(struct ptlrpc_cli_ctx *ctx)
665 {
666         return ((ctx->cc_flags & PTLRPC_CTX_ERROR) != 0);
667 }
668
669 static inline
670 int cli_ctx_is_dead(struct ptlrpc_cli_ctx *ctx)
671 {
672         return ((ctx->cc_flags & (PTLRPC_CTX_DEAD | PTLRPC_CTX_ERROR)) != 0);
673 }
674
675 static inline
676 int cli_ctx_is_eternal(struct ptlrpc_cli_ctx *ctx)
677 {
678         return ((ctx->cc_flags & PTLRPC_CTX_ETERNAL) != 0);
679 }
680
681 /*
682  * sec get/put
683  */
684 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec);
685 void sptlrpc_sec_put(struct ptlrpc_sec *sec);
686
687 /*
688  * internal apis which only used by policy impelentation
689  */
690 int  sptlrpc_get_next_secid(void);
691 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec);
692
693 /*
694  * exported client context api
695  */
696 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx);
697 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync);
698 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx);
699 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx);
700 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize);
701
702 /*
703  * exported client context wrap/buffers
704  */
705 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req);
706 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req);
707 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize);
708 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req);
709 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize);
710 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req);
711 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
712                                int segment, int newsize);
713 int  sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
714                                     struct ptlrpc_request **req_ret);
715 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req);
716
717 void sptlrpc_request_out_callback(struct ptlrpc_request *req);
718
719 /*
720  * exported higher interface of import & request
721  */
722 int sptlrpc_import_sec_adapt(struct obd_import *imp,
723                              struct ptlrpc_svc_ctx *ctx,
724                              struct sptlrpc_flavor *flvr);
725 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp);
726 void sptlrpc_import_sec_put(struct obd_import *imp);
727
728 int  sptlrpc_import_check_ctx(struct obd_import *imp);
729 void sptlrpc_import_flush_root_ctx(struct obd_import *imp);
730 void sptlrpc_import_flush_my_ctx(struct obd_import *imp);
731 void sptlrpc_import_flush_all_ctx(struct obd_import *imp);
732 int  sptlrpc_req_get_ctx(struct ptlrpc_request *req);
733 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync);
734 int  sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout);
735 int  sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req);
736 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode);
737
738 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule);
739
740 /* gc */
741 void sptlrpc_gc_add_sec(struct ptlrpc_sec *sec);
742 void sptlrpc_gc_del_sec(struct ptlrpc_sec *sec);
743 void sptlrpc_gc_add_ctx(struct ptlrpc_cli_ctx *ctx);
744
745 /* misc */
746 const char * sec2target_str(struct ptlrpc_sec *sec);
747 int sptlrpc_lprocfs_cliobd_attach(struct obd_device *dev);
748
749 /*
750  * server side
751  */
752 enum secsvc_accept_res {
753         SECSVC_OK       = 0,
754         SECSVC_COMPLETE,
755         SECSVC_DROP,
756 };
757
758 int  sptlrpc_svc_unwrap_request(struct ptlrpc_request *req);
759 int  sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen);
760 int  sptlrpc_svc_wrap_reply(struct ptlrpc_request *req);
761 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs);
762 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req);
763 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req);
764 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req);
765
766 int  sptlrpc_target_export_check(struct obd_export *exp,
767                                  struct ptlrpc_request *req);
768 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
769                                       struct sptlrpc_rule_set *rset);
770
771 /*
772  * reverse context
773  */
774 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
775                                 struct ptlrpc_svc_ctx *ctx);
776 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
777                                 struct ptlrpc_cli_ctx *ctx);
778
779 /* bulk security api */
780 int sptlrpc_enc_pool_add_user(void);
781 int sptlrpc_enc_pool_del_user(void);
782 int  sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc);
783 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc);
784
785 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
786                           struct ptlrpc_bulk_desc *desc);
787 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
788                                  struct ptlrpc_bulk_desc *desc,
789                                  int nob);
790 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
791                                   struct ptlrpc_bulk_desc *desc);
792 int sptlrpc_svc_prep_bulk(struct ptlrpc_request *req,
793                           struct ptlrpc_bulk_desc *desc);
794 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
795                           struct ptlrpc_bulk_desc *desc);
796 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
797                             struct ptlrpc_bulk_desc *desc);
798
799 /* bulk helpers (internal use only by policies) */
800 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
801                               void *buf, int buflen);
802
803 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed);
804
805 /* user descriptor helpers */
806 static inline int sptlrpc_user_desc_size(int ngroups)
807 {
808         return sizeof(struct ptlrpc_user_desc) + ngroups * sizeof(__u32);
809 }
810
811 int sptlrpc_current_user_desc_size(void);
812 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset);
813 int sptlrpc_unpack_user_desc(struct lustre_msg *req, int offset, int swabbed);
814
815
816 #define CFS_CAP_CHOWN_MASK (1 << CFS_CAP_CHOWN)
817 #define CFS_CAP_SYS_RESOURCE_MASK (1 << CFS_CAP_SYS_RESOURCE)
818
819 enum {
820         LUSTRE_SEC_NONE         = 0,
821         LUSTRE_SEC_REMOTE       = 1,
822         LUSTRE_SEC_SPECIFY      = 2,
823         LUSTRE_SEC_ALL          = 3
824 };
825
826 /** @} sec */
827
828 #endif /* _LUSTRE_SEC_H_ */