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