Whamcloud - gitweb
branch: HEAD
[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  * Copyright (C) 2004-2007 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef _LUSTRE_SEC_H_
23 #define _LUSTRE_SEC_H_
24
25 /*
26  * to avoid include
27  */
28 struct key;
29 struct obd_import;
30 struct obd_export;
31 struct ptlrpc_request;
32 struct ptlrpc_reply_state;
33 struct ptlrpc_bulk_desc;
34 struct brw_page;
35 struct seq_file;
36
37 /*
38  * forward declaration
39  */
40 struct ptlrpc_sec_policy;
41 struct ptlrpc_sec_cops;
42 struct ptlrpc_sec_sops;
43 struct ptlrpc_sec;
44 struct ptlrpc_svc_ctx;
45 struct ptlrpc_cli_ctx;
46 struct ptlrpc_ctx_ops;
47
48 /*
49  * flavor constants
50  */
51 enum sptlrpc_policy {
52         SPTLRPC_POLICY_NULL             = 0,
53         SPTLRPC_POLICY_PLAIN            = 1,
54         SPTLRPC_POLICY_GSS              = 2,
55         SPTLRPC_POLICY_MAX,
56 };
57
58 enum sptlrpc_mech_null {
59         SPTLRPC_MECH_NULL               = 0,
60         SPTLRPC_MECH_NULL_MAX,
61 };
62
63 enum sptlrpc_mech_plain {
64         SPTLRPC_MECH_PLAIN              = 0,
65         SPTLRPC_MECH_PLAIN_MAX,
66 };
67
68 enum sptlrpc_mech_gss {
69         SPTLRPC_MECH_GSS_NULL           = 0,
70         SPTLRPC_MECH_GSS_KRB5           = 1,
71         SPTLRPC_MECH_GSS_MAX,
72 };
73
74 enum sptlrpc_service_type {
75         SPTLRPC_SVC_NULL                = 0,    /* no security */
76         SPTLRPC_SVC_AUTH                = 1,    /* auth only */
77         SPTLRPC_SVC_INTG                = 2,    /* integrity */
78         SPTLRPC_SVC_PRIV                = 3,    /* privacy */
79         SPTLRPC_SVC_MAX,
80 };
81
82 /*
83  * rpc flavor compose/extract, represented as 16 bits
84  *
85  * 4b (reserved) | 4b (svc) | 4b (mech)  | 4b (policy)
86  */
87 #define RPC_FLVR_POLICY_OFFSET        (0)
88 #define RPC_FLVR_MECH_OFFSET          (4)
89 #define RPC_FLVR_SVC_OFFSET           (8)
90
91 #define MAKE_RPC_FLVR(policy, mech, svc)                                \
92         (((__u16)(policy) << RPC_FLVR_POLICY_OFFSET) |                  \
93          ((__u16)(mech) << RPC_FLVR_MECH_OFFSET) |                      \
94          ((__u16)(svc) << RPC_FLVR_SVC_OFFSET))
95
96 #define MAKE_RPC_SUBFLVR(mech, svc)                                     \
97         ((__u16)(mech) |                                                \
98          ((__u16)(svc) << (RPC_FLVR_SVC_OFFSET - RPC_FLVR_MECH_OFFSET)))
99
100 #define RPC_FLVR_SUB(flavor)                                            \
101         ((((__u16)(flavor)) >> RPC_FLVR_MECH_OFFSET) & 0xFF)
102
103 #define RPC_FLVR_POLICY(flavor)                                         \
104         ((((__u16)(flavor)) >> RPC_FLVR_POLICY_OFFSET) & 0xF)
105 #define RPC_FLVR_MECH(flavor)                                           \
106         ((((__u16)(flavor)) >> RPC_FLVR_MECH_OFFSET) & 0xF)
107 #define RPC_FLVR_SVC(flavor)                                            \
108         ((((__u16)(flavor)) >> RPC_FLVR_SVC_OFFSET) & 0xF)
109
110 /*
111  * gss subflavors
112  */
113 #define SPTLRPC_SUBFLVR_KRB5N                                           \
114         MAKE_RPC_SUBFLVR(SPTLRPC_MECH_GSS_KRB5, SPTLRPC_SVC_NULL)
115 #define SPTLRPC_SUBFLVR_KRB5A                                           \
116         MAKE_RPC_SUBFLVR(SPTLRPC_MECH_GSS_KRB5, SPTLRPC_SVC_AUTH)
117 #define SPTLRPC_SUBFLVR_KRB5I                                           \
118         MAKE_RPC_SUBFLVR(SPTLRPC_MECH_GSS_KRB5, SPTLRPC_SVC_INTG)
119 #define SPTLRPC_SUBFLVR_KRB5P                                           \
120         MAKE_RPC_SUBFLVR(SPTLRPC_MECH_GSS_KRB5, SPTLRPC_SVC_PRIV)
121
122 /*
123  * "end user" flavors
124  */
125 #define SPTLRPC_FLVR_NULL                               \
126         MAKE_RPC_FLVR(SPTLRPC_POLICY_NULL,              \
127                       SPTLRPC_MECH_NULL,                \
128                       SPTLRPC_SVC_NULL)
129 #define SPTLRPC_FLVR_PLAIN                              \
130         MAKE_RPC_FLVR(SPTLRPC_POLICY_PLAIN,             \
131                       SPTLRPC_MECH_PLAIN,               \
132                       SPTLRPC_SVC_NULL)
133 #define SPTLRPC_FLVR_KRB5N                              \
134         MAKE_RPC_FLVR(SPTLRPC_POLICY_GSS,               \
135                       SPTLRPC_MECH_GSS_KRB5,            \
136                       SPTLRPC_SVC_NULL)
137 #define SPTLRPC_FLVR_KRB5A                              \
138         MAKE_RPC_FLVR(SPTLRPC_POLICY_GSS,               \
139                       SPTLRPC_MECH_GSS_KRB5,            \
140                       SPTLRPC_SVC_AUTH)
141 #define SPTLRPC_FLVR_KRB5I                              \
142         MAKE_RPC_FLVR(SPTLRPC_POLICY_GSS,               \
143                       SPTLRPC_MECH_GSS_KRB5,            \
144                       SPTLRPC_SVC_INTG)
145 #define SPTLRPC_FLVR_KRB5P                              \
146         MAKE_RPC_FLVR(SPTLRPC_POLICY_GSS,               \
147                       SPTLRPC_MECH_GSS_KRB5,            \
148                       SPTLRPC_SVC_PRIV)
149
150 #define SPTLRPC_FLVR_INVALID            ((__u16) -1)
151
152 #define SPTLRPC_FLVR_DEFAULT            SPTLRPC_FLVR_NULL
153
154 /*
155  * 32 bits wire flavor (msg->lm_secflvr), lower 12 bits is the rpc flavor,
156  * higher 20 bits is not defined right now.
157  */
158 #define WIRE_FLVR_RPC(wflvr)            (((__u16) (wflvr)) & 0x0FFF)
159
160 static inline void rpc_flvr_set_svc(__u16 *flvr, __u16 svc)
161 {
162         LASSERT(svc < SPTLRPC_SVC_MAX);
163         *flvr = MAKE_RPC_FLVR(RPC_FLVR_POLICY(*flvr),
164                               RPC_FLVR_MECH(*flvr),
165                               svc);
166 }
167
168
169 struct sptlrpc_flavor {
170         __u16   sf_rpc;         /* rpc flavor */
171         __u8    sf_bulk_ciph;   /* bulk cipher alg */
172         __u8    sf_bulk_hash;   /* bulk hash alg */
173         __u32   sf_flags;       /* general flags */
174 };
175
176 enum lustre_sec_part {
177         LUSTRE_SP_CLI           = 0,
178         LUSTRE_SP_MDT,
179         LUSTRE_SP_OST,
180         LUSTRE_SP_MGS,
181         LUSTRE_SP_ANY           = 0xFF
182 };
183
184 struct sptlrpc_rule {
185         __u32                   sr_netid;   /* LNET network ID */
186         __u8                    sr_from;    /* sec_part */
187         __u8                    sr_to;      /* sec_part */
188         __u16                   sr_padding;
189         struct sptlrpc_flavor   sr_flvr;
190 };
191
192 struct sptlrpc_rule_set {
193         int                     srs_nslot;
194         int                     srs_nrule;
195         struct sptlrpc_rule    *srs_rules;
196 };
197
198 #define SPTLRPC_CONF_LOG_MAX    (64)
199
200 struct sptlrpc_conf_log {
201         __u32               scl_max;            /* maximum rules # */
202         __u32               scl_nrule;          /* rules # */
203         __u8                scl_part;           /* which part am i */
204         __u8                scl_pad0;
205         __u16               scl_pad1;
206         __u32               scl_pad2;
207         struct sptlrpc_rule scl_rules[SPTLRPC_CONF_LOG_MAX];
208 };
209
210 static inline void sptlrpc_rule_set_init(struct sptlrpc_rule_set *set)
211 {
212         memset(set, 0, sizeof(*set));
213 }
214
215 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *set);
216 int  sptlrpc_rule_set_expand(struct sptlrpc_rule_set *set, int expand);
217 int  sptlrpc_rule_set_merge(struct sptlrpc_rule_set *set,
218                             struct sptlrpc_rule *rule,
219                             int expand);
220 int sptlrpc_rule_set_from_log(struct sptlrpc_rule_set *rset,
221                               struct sptlrpc_conf_log *log);
222 void sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
223                              enum lustre_sec_part from,
224                              lnet_nid_t nid,
225                              struct sptlrpc_flavor *flavor);
226 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *set);
227
228 struct sptlrpc_conf_log *sptlrpc_conf_log_alloc(void);
229 void sptlrpc_conf_log_free(struct sptlrpc_conf_log *log);
230 int sptlrpc_conf_log_populate(struct sptlrpc_rule_set *gen,
231                               struct sptlrpc_rule_set *tgt,
232                               enum lustre_sec_part from,
233                               enum lustre_sec_part to,
234                               unsigned int fl_udesc,
235                               struct sptlrpc_conf_log *log);
236 struct sptlrpc_conf_log *sptlrpc_conf_log_extract(struct lustre_cfg *lcfg);
237 void sptlrpc_conf_log_cleanup(struct sptlrpc_conf_log *log);
238 void sptlrpc_conf_log_dump(struct sptlrpc_conf_log *log);
239
240 const char *sptlrpc_part2name(enum lustre_sec_part part);
241 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd);
242
243 int sptlrpc_cliobd_process_config(struct obd_device *obd,
244                                   struct lustre_cfg *lcfg);
245
246 /* The maximum length of security payload. 1024 is enough for Kerberos 5,
247  * and should be enough for other future mechanisms but not sure.
248  * Only used by pre-allocated request/reply pool.
249  */
250 #define SPTLRPC_MAX_PAYLOAD     (1024)
251
252
253 struct vfs_cred {
254         uint32_t        vc_uid;
255         uint32_t        vc_gid;
256 };
257
258 struct ptlrpc_ctx_ops {
259         int     (*match)       (struct ptlrpc_cli_ctx *ctx,
260                                 struct vfs_cred *vcred);
261         int     (*refresh)     (struct ptlrpc_cli_ctx *ctx);
262         int     (*validate)    (struct ptlrpc_cli_ctx *ctx);
263         void    (*die)         (struct ptlrpc_cli_ctx *ctx,
264                                 int grace);
265         int     (*display)     (struct ptlrpc_cli_ctx *ctx,
266                                 char *buf, int bufsize);
267         /*
268          * rpc data transform
269          */
270         int     (*sign)        (struct ptlrpc_cli_ctx *ctx,
271                                 struct ptlrpc_request *req);
272         int     (*verify)      (struct ptlrpc_cli_ctx *ctx,
273                                 struct ptlrpc_request *req);
274         int     (*seal)        (struct ptlrpc_cli_ctx *ctx,
275                                 struct ptlrpc_request *req);
276         int     (*unseal)      (struct ptlrpc_cli_ctx *ctx,
277                                 struct ptlrpc_request *req);
278         /*
279          * bulk transform
280          */
281         int     (*wrap_bulk)   (struct ptlrpc_cli_ctx *ctx,
282                                 struct ptlrpc_request *req,
283                                 struct ptlrpc_bulk_desc *desc);
284         int     (*unwrap_bulk) (struct ptlrpc_cli_ctx *ctx,
285                                 struct ptlrpc_request *req,
286                                 struct ptlrpc_bulk_desc *desc);
287 };
288
289 #define PTLRPC_CTX_NEW_BIT             (0)  /* newly created */
290 #define PTLRPC_CTX_UPTODATE_BIT        (1)  /* uptodate */
291 #define PTLRPC_CTX_DEAD_BIT            (2)  /* mark expired gracefully */
292 #define PTLRPC_CTX_ERROR_BIT           (3)  /* fatal error (refresh, etc.) */
293 #define PTLRPC_CTX_CACHED_BIT          (8)  /* in ctx cache (hash etc.) */
294 #define PTLRPC_CTX_ETERNAL_BIT         (9)  /* always valid */
295
296 #define PTLRPC_CTX_NEW                 (1 << PTLRPC_CTX_NEW_BIT)
297 #define PTLRPC_CTX_UPTODATE            (1 << PTLRPC_CTX_UPTODATE_BIT)
298 #define PTLRPC_CTX_DEAD                (1 << PTLRPC_CTX_DEAD_BIT)
299 #define PTLRPC_CTX_ERROR               (1 << PTLRPC_CTX_ERROR_BIT)
300 #define PTLRPC_CTX_CACHED              (1 << PTLRPC_CTX_CACHED_BIT)
301 #define PTLRPC_CTX_ETERNAL             (1 << PTLRPC_CTX_ETERNAL_BIT)
302
303 #define PTLRPC_CTX_STATUS_MASK         (PTLRPC_CTX_NEW_BIT    |       \
304                                         PTLRPC_CTX_UPTODATE   |       \
305                                         PTLRPC_CTX_DEAD       |       \
306                                         PTLRPC_CTX_ERROR)
307
308 struct ptlrpc_cli_ctx {
309         struct hlist_node       cc_cache;      /* linked into ctx cache */
310         atomic_t                cc_refcount;
311         struct ptlrpc_sec      *cc_sec;
312         struct ptlrpc_ctx_ops  *cc_ops;
313         cfs_time_t              cc_expire;     /* in seconds */
314         unsigned int            cc_early_expire:1;
315         unsigned long           cc_flags;
316         struct vfs_cred         cc_vcred;
317         spinlock_t              cc_lock;
318         struct list_head        cc_req_list;   /* waiting reqs linked here */
319         struct list_head        cc_gc_chain;   /* linked to gc chain */
320 };
321
322 struct ptlrpc_sec_cops {
323         /*
324          * ptlrpc_sec constructor/destructor
325          */
326         struct ptlrpc_sec *     (*create_sec)  (struct obd_import *imp,
327                                                 struct ptlrpc_svc_ctx *ctx,
328                                                 struct sptlrpc_flavor *flavor);
329         void                    (*destroy_sec) (struct ptlrpc_sec *sec);
330
331         /*
332          * notify to-be-dead
333          */
334         void                    (*kill_sec)    (struct ptlrpc_sec *sec);
335
336         /*
337          * context
338          */
339         struct ptlrpc_cli_ctx * (*lookup_ctx)  (struct ptlrpc_sec *sec,
340                                                 struct vfs_cred *vcred,
341                                                 int create,
342                                                 int remove_dead);
343         void                    (*release_ctx) (struct ptlrpc_sec *sec,
344                                                 struct ptlrpc_cli_ctx *ctx,
345                                                 int sync);
346         int                     (*flush_ctx_cache)
347                                                (struct ptlrpc_sec *sec,
348                                                 uid_t uid,
349                                                 int grace,
350                                                 int force);
351         void                    (*gc_ctx)      (struct ptlrpc_sec *sec);
352
353         /*
354          * reverse context
355          */
356         int                     (*install_rctx)(struct obd_import *imp,
357                                                 struct ptlrpc_sec *sec,
358                                                 struct ptlrpc_cli_ctx *ctx);
359
360         /*
361          * request/reply buffer manipulation
362          */
363         int                     (*alloc_reqbuf)(struct ptlrpc_sec *sec,
364                                                 struct ptlrpc_request *req,
365                                                 int lustre_msg_size);
366         void                    (*free_reqbuf) (struct ptlrpc_sec *sec,
367                                                 struct ptlrpc_request *req);
368         int                     (*alloc_repbuf)(struct ptlrpc_sec *sec,
369                                                 struct ptlrpc_request *req,
370                                                 int lustre_msg_size);
371         void                    (*free_repbuf) (struct ptlrpc_sec *sec,
372                                                 struct ptlrpc_request *req);
373         int                     (*enlarge_reqbuf)
374                                                (struct ptlrpc_sec *sec,
375                                                 struct ptlrpc_request *req,
376                                                 int segment, int newsize);
377         /*
378          * misc
379          */
380         int                     (*display)     (struct ptlrpc_sec *sec,
381                                                 struct seq_file *seq);
382 };
383
384 struct ptlrpc_sec_sops {
385         int                     (*accept)      (struct ptlrpc_request *req);
386         int                     (*authorize)   (struct ptlrpc_request *req);
387         void                    (*invalidate_ctx)
388                                                (struct ptlrpc_svc_ctx *ctx);
389         /* buffer manipulation */
390         int                     (*alloc_rs)    (struct ptlrpc_request *req,
391                                                 int msgsize);
392         void                    (*free_rs)     (struct ptlrpc_reply_state *rs);
393         void                    (*free_ctx)    (struct ptlrpc_svc_ctx *ctx);
394         /* reverse credential */
395         int                     (*install_rctx)(struct obd_import *imp,
396                                                 struct ptlrpc_svc_ctx *ctx);
397         /* bulk transform */
398         int                     (*unwrap_bulk) (struct ptlrpc_request *req,
399                                                 struct ptlrpc_bulk_desc *desc);
400         int                     (*wrap_bulk)   (struct ptlrpc_request *req,
401                                                 struct ptlrpc_bulk_desc *desc);
402 };
403
404 struct ptlrpc_sec_policy {
405         struct module                  *sp_owner;
406         char                           *sp_name;
407         __u16                           sp_policy; /* policy number */
408         struct ptlrpc_sec_cops         *sp_cops;   /* client ops */
409         struct ptlrpc_sec_sops         *sp_sops;   /* server ops */
410 };
411
412 #define PTLRPC_SEC_FL_REVERSE           0x0001 /* reverse sec */
413 #define PTLRPC_SEC_FL_ROOTONLY          0x0002 /* treat everyone as root */
414 #define PTLRPC_SEC_FL_UDESC             0x0004 /* ship udesc */
415 #define PTLRPC_SEC_FL_BULK              0x0008 /* intensive bulk i/o expected */
416 #define PTLRPC_SEC_FL_PAG               0x0010 /* PAG mode */
417
418 struct ptlrpc_sec {
419         struct ptlrpc_sec_policy       *ps_policy;
420         atomic_t                        ps_refcount;
421         atomic_t                        ps_nctx;        /* statistic only */
422         int                             ps_id;          /* unique identifier */
423         struct sptlrpc_flavor           ps_flvr;        /* flavor */
424         enum lustre_sec_part            ps_part;
425         unsigned int                    ps_dying:1;
426         struct obd_import              *ps_import;      /* owning import */
427         spinlock_t                      ps_lock;        /* protect ccache */
428         /*
429          * garbage collection
430          */
431         struct list_head                ps_gc_list;
432         cfs_time_t                      ps_gc_interval; /* in seconds */
433         cfs_time_t                      ps_gc_next;     /* in seconds */
434 };
435
436 static inline int sec_is_reverse(struct ptlrpc_sec *sec)
437 {
438         return (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE);
439 }
440
441 static inline int sec_is_rootonly(struct ptlrpc_sec *sec)
442 {
443         return (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_ROOTONLY);
444 }
445
446
447 struct ptlrpc_svc_ctx {
448         atomic_t                        sc_refcount;
449         struct ptlrpc_sec_policy       *sc_policy;
450 };
451
452 /*
453  * user identity descriptor
454  */
455 #define LUSTRE_MAX_GROUPS               (128)
456
457 struct ptlrpc_user_desc {
458         __u32           pud_uid;
459         __u32           pud_gid;
460         __u32           pud_fsuid;
461         __u32           pud_fsgid;
462         __u32           pud_cap;
463         __u32           pud_ngroups;
464         __u32           pud_groups[0];
465 };
466
467 /*
468  * bulk flavors
469  */
470 enum sptlrpc_bulk_hash_alg {
471         BULK_HASH_ALG_NULL      = 0,
472         BULK_HASH_ALG_ADLER32,
473         BULK_HASH_ALG_CRC32,
474         BULK_HASH_ALG_MD5,
475         BULK_HASH_ALG_SHA1,
476         BULK_HASH_ALG_SHA256,
477         BULK_HASH_ALG_SHA384,
478         BULK_HASH_ALG_SHA512,
479         BULK_HASH_ALG_WP256,
480         BULK_HASH_ALG_WP384,
481         BULK_HASH_ALG_WP512,
482         BULK_HASH_ALG_MAX
483 };
484
485 enum sptlrpc_bulk_cipher_alg {
486         BULK_CIPH_ALG_NULL      = 0,
487         BULK_CIPH_ALG_ARC4,
488         BULK_CIPH_ALG_AES128,
489         BULK_CIPH_ALG_AES192,
490         BULK_CIPH_ALG_AES256,
491         BULK_CIPH_ALG_CAST128,
492         BULK_CIPH_ALG_CAST256,
493         BULK_CIPH_ALG_TWOFISH128,
494         BULK_CIPH_ALG_TWOFISH256,
495         BULK_CIPH_ALG_MAX
496 };
497
498 struct sptlrpc_hash_type {
499         char           *sht_name;
500         char           *sht_tfm_name;
501         unsigned int    sht_size;
502 };
503
504 struct sptlrpc_ciph_type {
505         char           *sct_name;
506         char           *sct_tfm_name;
507         __u32           sct_tfm_flags;
508         unsigned int    sct_ivsize;
509         unsigned int    sct_keysize;
510 };
511
512 const struct sptlrpc_hash_type *sptlrpc_get_hash_type(__u8 hash_alg);
513 const char * sptlrpc_get_hash_name(__u8 hash_alg);
514 const struct sptlrpc_ciph_type *sptlrpc_get_ciph_type(__u8 ciph_alg);
515 const char *sptlrpc_get_ciph_name(__u8 ciph_alg);
516
517 #define CIPHER_MAX_BLKSIZE      (16)
518 #define CIPHER_MAX_KEYSIZE      (64)
519
520 struct ptlrpc_bulk_sec_desc {
521         __u8            bsd_version;
522         __u8            bsd_flags;
523         __u8            bsd_pad[4];
524         __u8            bsd_hash_alg;                /* hash algorithm */
525         __u8            bsd_ciph_alg;                /* cipher algorithm */
526         __u8            bsd_key[CIPHER_MAX_KEYSIZE]; /* encrypt key seed */
527         __u8            bsd_csum[0];
528 };
529
530
531 /*
532  * lprocfs
533  */
534 struct proc_dir_entry;
535 extern struct proc_dir_entry *sptlrpc_proc_root;
536
537 /*
538  * round size up to next power of 2, for slab allocation.
539  * @size must be sane (can't overflow after round up)
540  */
541 static inline int size_roundup_power2(int size)
542 {
543         size--;
544         size |= size >> 1;
545         size |= size >> 2;
546         size |= size >> 4;
547         size |= size >> 8;
548         size |= size >> 16;
549         size++;
550         return size;
551 }
552
553 /*
554  * internal support libraries
555  */
556 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
557                                   int segment, int newsize);
558
559 /*
560  * security type
561  */
562 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy);
563 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy);
564
565 __u16 sptlrpc_name2rpcflavor(const char *name);
566 const char *sptlrpc_rpcflavor2name(__u16 flavor);
567 int sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize);
568
569 static inline
570 struct ptlrpc_sec_policy *sptlrpc_policy_get(struct ptlrpc_sec_policy *policy)
571 {
572         __module_get(policy->sp_owner);
573         return policy;
574 }
575
576 static inline
577 void sptlrpc_policy_put(struct ptlrpc_sec_policy *policy)
578 {
579         module_put(policy->sp_owner);
580 }
581
582 /*
583  * client credential
584  */
585 static inline
586 unsigned long cli_ctx_status(struct ptlrpc_cli_ctx *ctx)
587 {
588         return (ctx->cc_flags & PTLRPC_CTX_STATUS_MASK);
589 }
590
591 static inline
592 int cli_ctx_is_ready(struct ptlrpc_cli_ctx *ctx)
593 {
594         return (cli_ctx_status(ctx) == PTLRPC_CTX_UPTODATE);
595 }
596
597 static inline
598 int cli_ctx_is_refreshed(struct ptlrpc_cli_ctx *ctx)
599 {
600         return (cli_ctx_status(ctx) != 0);
601 }
602
603 static inline
604 int cli_ctx_is_uptodate(struct ptlrpc_cli_ctx *ctx)
605 {
606         return ((ctx->cc_flags & PTLRPC_CTX_UPTODATE) != 0);
607 }
608
609 static inline
610 int cli_ctx_is_error(struct ptlrpc_cli_ctx *ctx)
611 {
612         return ((ctx->cc_flags & PTLRPC_CTX_ERROR) != 0);
613 }
614
615 static inline
616 int cli_ctx_is_dead(struct ptlrpc_cli_ctx *ctx)
617 {
618         return ((ctx->cc_flags & (PTLRPC_CTX_DEAD | PTLRPC_CTX_ERROR)) != 0);
619 }
620
621 static inline
622 int cli_ctx_is_eternal(struct ptlrpc_cli_ctx *ctx)
623 {
624         return ((ctx->cc_flags & PTLRPC_CTX_ETERNAL) != 0);
625 }
626
627 /*
628  * sec get/put
629  */
630 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec);
631 void sptlrpc_sec_put(struct ptlrpc_sec *sec);
632
633 /*
634  * internal apis which only used by policy impelentation
635  */
636 int  sptlrpc_get_next_secid(void);
637 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec);
638
639 /*
640  * exported client context api
641  */
642 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx);
643 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync);
644 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx);
645 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx);
646 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize);
647
648 /*
649  * exported client context wrap/buffers
650  */
651 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req);
652 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req);
653 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize);
654 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req);
655 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize);
656 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req);
657 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
658                                int segment, int newsize);
659 void sptlrpc_request_out_callback(struct ptlrpc_request *req);
660
661 /*
662  * exported higher interface of import & request
663  */
664 int sptlrpc_import_sec_adapt(struct obd_import *imp,
665                              struct ptlrpc_svc_ctx *ctx,
666                              __u16 rpc_flavor);
667 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp);
668 void sptlrpc_import_sec_put(struct obd_import *imp);
669
670 int  sptlrpc_import_check_ctx(struct obd_import *imp);
671 void sptlrpc_import_inval_all_ctx(struct obd_import *imp);
672 void sptlrpc_import_flush_root_ctx(struct obd_import *imp);
673 void sptlrpc_import_flush_my_ctx(struct obd_import *imp);
674 void sptlrpc_import_flush_all_ctx(struct obd_import *imp);
675 int  sptlrpc_req_get_ctx(struct ptlrpc_request *req);
676 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync);
677 int  sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout);
678 int  sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req);
679 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode);
680
681 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule);
682
683 /* gc */
684 void sptlrpc_gc_add_sec(struct ptlrpc_sec *sec);
685 void sptlrpc_gc_del_sec(struct ptlrpc_sec *sec);
686 void sptlrpc_gc_add_ctx(struct ptlrpc_cli_ctx *ctx);
687
688 /* misc */
689 const char * sec2target_str(struct ptlrpc_sec *sec);
690 int sptlrpc_lprocfs_cliobd_attach(struct obd_device *dev);
691
692 /*
693  * server side
694  */
695 enum secsvc_accept_res {
696         SECSVC_OK       = 0,
697         SECSVC_COMPLETE,
698         SECSVC_DROP,
699 };
700
701 int  sptlrpc_svc_unwrap_request(struct ptlrpc_request *req);
702 int  sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen);
703 int  sptlrpc_svc_wrap_reply(struct ptlrpc_request *req);
704 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs);
705 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req);
706 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req);
707 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req);
708
709 int  sptlrpc_target_export_check(struct obd_export *exp,
710                                  struct ptlrpc_request *req);
711 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
712                                       struct sptlrpc_rule_set *rset);
713
714 /*
715  * reverse context
716  */
717 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
718                                 struct ptlrpc_svc_ctx *ctx);
719 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
720                                 struct ptlrpc_cli_ctx *ctx);
721
722 /* bulk security api */
723 int sptlrpc_enc_pool_add_user(void);
724 int sptlrpc_enc_pool_del_user(void);
725 int  sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc);
726 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc);
727
728 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
729                           struct ptlrpc_bulk_desc *desc);
730 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
731                                  int nob, obd_count pg_count,
732                                  struct brw_page **pga);
733 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
734                                   struct ptlrpc_bulk_desc *desc);
735 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
736                           struct ptlrpc_bulk_desc *desc);
737 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
738                             struct ptlrpc_bulk_desc *desc);
739
740 /* user descriptor helpers */
741 static inline int sptlrpc_user_desc_size(int ngroups)
742 {
743         return sizeof(struct ptlrpc_user_desc) + ngroups * sizeof(__u32);
744 }
745
746 int sptlrpc_current_user_desc_size(void);
747 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset);
748 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset);
749
750 /* bulk helpers (internal use only by policies) */
751 int bulk_sec_desc_size(__u8 hash_alg, int request, int read);
752 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset);
753
754 int bulk_csum_cli_request(struct ptlrpc_bulk_desc *desc, int read,
755                           __u32 alg, struct lustre_msg *rmsg, int roff);
756 int bulk_csum_cli_reply(struct ptlrpc_bulk_desc *desc, int read,
757                         struct lustre_msg *rmsg, int roff,
758                         struct lustre_msg *vmsg, int voff);
759 int bulk_csum_svc(struct ptlrpc_bulk_desc *desc, int read,
760                   struct ptlrpc_bulk_sec_desc *bsdv, int vsize,
761                   struct ptlrpc_bulk_sec_desc *bsdr, int rsize);
762
763
764 #endif /* _LUSTRE_SEC_H_ */