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