1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004-2006 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
22 #ifndef _LUSTRE_SEC_H_
23 #define _LUSTRE_SEC_H_
31 struct ptlrpc_request;
32 struct ptlrpc_reply_state;
33 struct ptlrpc_bulk_desc;
39 struct ptlrpc_sec_policy;
40 struct ptlrpc_sec_cops;
41 struct ptlrpc_sec_sops;
43 struct ptlrpc_svc_ctx;
44 struct ptlrpc_cli_ctx;
45 struct ptlrpc_ctx_ops;
51 SPTLRPC_POLICY_NULL = 0,
52 SPTLRPC_POLICY_PLAIN = 1,
53 SPTLRPC_POLICY_GSS = 2,
54 SPTLRPC_POLICY_GSS_PIPEFS = 3,
58 enum sptlrpc_mech_null {
59 SPTLRPC_MECH_NULL = 0,
60 SPTLRPC_MECH_NULL_MAX,
63 enum sptlrpc_mech_plain {
64 SPTLRPC_MECH_PLAIN = 0,
65 SPTLRPC_MECH_PLAIN_MAX,
68 enum sptlrpc_mech_gss {
69 SPTLRPC_MECH_GSS_NULL = 0,
70 SPTLRPC_MECH_GSS_KRB5 = 1,
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 */
83 * flavor compose/extract
86 typedef __u32 ptlrpc_sec_flavor_t;
89 * 8b (reserved) | 8b (flags)
90 * 4b (reserved) | 4b (svc) | 4b (mech) | 4b (policy)
92 #define SEC_FLAVOR_POLICY_OFFSET (0)
93 #define SEC_FLAVOR_MECH_OFFSET (4)
94 #define SEC_FLAVOR_SVC_OFFSET (8)
95 #define SEC_FLAVOR_RESERVE1_OFFSET (12)
96 #define SEC_FLAVOR_FLAGS_OFFSET (16)
98 #define SEC_MAKE_RPC_FLAVOR(policy, mech, svc) \
99 (((__u32)(policy) << SEC_FLAVOR_POLICY_OFFSET) | \
100 ((__u32)(mech) << SEC_FLAVOR_MECH_OFFSET) | \
101 ((__u32)(svc) << SEC_FLAVOR_SVC_OFFSET))
103 #define SEC_MAKE_RPC_SUBFLAVOR(mech, svc) \
106 (SEC_FLAVOR_SVC_OFFSET - SEC_FLAVOR_MECH_OFFSET)))
108 #define SEC_FLAVOR_SUB(flavor) \
109 ((((__u32)(flavor)) >> SEC_FLAVOR_MECH_OFFSET) & 0xFF)
111 #define SEC_FLAVOR_POLICY(flavor) \
112 ((((__u32)(flavor)) >> SEC_FLAVOR_POLICY_OFFSET) & 0xF)
113 #define SEC_FLAVOR_MECH(flavor) \
114 ((((__u32)(flavor)) >> SEC_FLAVOR_MECH_OFFSET) & 0xF)
115 #define SEC_FLAVOR_SVC(flavor) \
116 ((((__u32)(flavor)) >> SEC_FLAVOR_SVC_OFFSET) & 0xF)
118 #define SEC_FLAVOR_RPC(f) \
119 (((__u32) (f)) & ((1 << SEC_FLAVOR_RESERVE1_OFFSET) - 1))
124 #define SPTLRPC_SUBFLVR_KRB5N \
125 SEC_MAKE_RPC_SUBFLAVOR(SPTLRPC_MECH_GSS_KRB5, \
127 #define SPTLRPC_SUBFLVR_KRB5A \
128 SEC_MAKE_RPC_SUBFLAVOR(SPTLRPC_MECH_GSS_KRB5, \
130 #define SPTLRPC_SUBFLVR_KRB5I \
131 SEC_MAKE_RPC_SUBFLAVOR(SPTLRPC_MECH_GSS_KRB5, \
133 #define SPTLRPC_SUBFLVR_KRB5P \
134 SEC_MAKE_RPC_SUBFLAVOR(SPTLRPC_MECH_GSS_KRB5, \
140 #define SPTLRPC_FLVR_NULL \
141 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_NULL, \
144 #define SPTLRPC_FLVR_PLAIN \
145 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_PLAIN, \
146 SPTLRPC_MECH_PLAIN, \
148 #define SPTLRPC_FLVR_KRB5N \
149 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
150 SPTLRPC_MECH_GSS_KRB5, \
152 #define SPTLRPC_FLVR_KRB5A \
153 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
154 SPTLRPC_MECH_GSS_KRB5, \
156 #define SPTLRPC_FLVR_KRB5I \
157 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
158 SPTLRPC_MECH_GSS_KRB5, \
160 #define SPTLRPC_FLVR_KRB5P \
161 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
162 SPTLRPC_MECH_GSS_KRB5, \
165 #define SPTLRPC_FLVR_INVALID (-1)
167 #define SPTLRPC_FLVR_DEFAULT SPTLRPC_FLVR_NULL
170 * flavor flags (maximum 8 flags)
172 #define SEC_FLAVOR_FL_BULK (1 << (0 + SEC_FLAVOR_FLAGS_OFFSET))
173 #define SEC_FLAVOR_FL_USER (1 << (1 + SEC_FLAVOR_FLAGS_OFFSET))
175 #define SEC_FLAVOR_HAS_BULK(flavor) \
176 (((flavor) & SEC_FLAVOR_FL_BULK) != 0)
177 #define SEC_FLAVOR_HAS_USER(flavor) \
178 (((flavor) & SEC_FLAVOR_FL_USER) != 0)
181 struct sec_flavor_config {
182 __u32 sfc_rpc_flavor; /* main rpc flavor */
183 __u32 sfc_bulk_priv; /* bulk encryption algorithm */
184 __u32 sfc_bulk_csum; /* bulk checksum algorithm */
185 __u32 sfc_flags; /* extra flags */
196 /* The maximum length of security payload. 1024 is enough for Kerberos 5,
197 * and should be enough for other future mechanisms but not sure.
198 * Only used by pre-allocated request/reply pool.
200 #define SPTLRPC_MAX_PAYLOAD (1024)
208 struct ptlrpc_ctx_ops {
209 int (*match) (struct ptlrpc_cli_ctx *ctx,
210 struct vfs_cred *vcred);
211 int (*refresh) (struct ptlrpc_cli_ctx *ctx);
212 int (*validate) (struct ptlrpc_cli_ctx *ctx);
213 void (*die) (struct ptlrpc_cli_ctx *ctx,
215 int (*display) (struct ptlrpc_cli_ctx *ctx,
216 char *buf, int bufsize);
220 int (*sign) (struct ptlrpc_cli_ctx *ctx,
221 struct ptlrpc_request *req);
222 int (*verify) (struct ptlrpc_cli_ctx *ctx,
223 struct ptlrpc_request *req);
224 int (*seal) (struct ptlrpc_cli_ctx *ctx,
225 struct ptlrpc_request *req);
226 int (*unseal) (struct ptlrpc_cli_ctx *ctx,
227 struct ptlrpc_request *req);
231 int (*wrap_bulk) (struct ptlrpc_cli_ctx *ctx,
232 struct ptlrpc_request *req,
233 struct ptlrpc_bulk_desc *desc);
234 int (*unwrap_bulk) (struct ptlrpc_cli_ctx *ctx,
235 struct ptlrpc_request *req,
236 struct ptlrpc_bulk_desc *desc);
239 #define PTLRPC_CTX_NEW_BIT (0) /* newly created */
240 #define PTLRPC_CTX_UPTODATE_BIT (1) /* uptodate */
241 #define PTLRPC_CTX_DEAD_BIT (2) /* mark expired gracefully */
242 #define PTLRPC_CTX_ERROR_BIT (3) /* fatal error (refresh, etc.) */
243 #define PTLRPC_CTX_CACHED_BIT (8) /* in ctx cache (hash etc.) */
244 #define PTLRPC_CTX_ETERNAL_BIT (9) /* always valid */
246 #define PTLRPC_CTX_NEW (1 << PTLRPC_CTX_NEW_BIT)
247 #define PTLRPC_CTX_UPTODATE (1 << PTLRPC_CTX_UPTODATE_BIT)
248 #define PTLRPC_CTX_DEAD (1 << PTLRPC_CTX_DEAD_BIT)
249 #define PTLRPC_CTX_ERROR (1 << PTLRPC_CTX_ERROR_BIT)
250 #define PTLRPC_CTX_CACHED (1 << PTLRPC_CTX_CACHED_BIT)
251 #define PTLRPC_CTX_ETERNAL (1 << PTLRPC_CTX_ETERNAL_BIT)
253 #define PTLRPC_CTX_STATUS_MASK (PTLRPC_CTX_NEW_BIT | \
254 PTLRPC_CTX_UPTODATE | \
258 struct ptlrpc_cli_ctx {
259 struct hlist_node cc_cache; /* linked into ctx cache */
260 atomic_t cc_refcount;
261 struct ptlrpc_sec *cc_sec;
262 struct ptlrpc_ctx_ops *cc_ops;
263 cfs_time_t cc_expire; /* in seconds */
264 unsigned int cc_early_expire:1;
265 unsigned long cc_flags;
266 struct vfs_cred cc_vcred;
268 struct list_head cc_req_list; /* waiting reqs linked here */
269 struct list_head cc_gc_chain; /* linked to gc chain */
272 struct ptlrpc_sec_cops {
274 * ptlrpc_sec constructor/destructor
276 struct ptlrpc_sec * (*create_sec) (struct obd_import *imp,
277 struct ptlrpc_svc_ctx *ctx,
279 unsigned long flags);
280 void (*destroy_sec) (struct ptlrpc_sec *sec);
285 struct ptlrpc_cli_ctx * (*lookup_ctx) (struct ptlrpc_sec *sec,
286 struct vfs_cred *vcred,
289 void (*release_ctx) (struct ptlrpc_sec *sec,
290 struct ptlrpc_cli_ctx *ctx,
292 int (*flush_ctx_cache)
293 (struct ptlrpc_sec *sec,
297 void (*gc_ctx) (struct ptlrpc_sec *sec);
302 int (*install_rctx)(struct obd_import *imp,
303 struct ptlrpc_sec *sec,
304 struct ptlrpc_cli_ctx *ctx);
307 * request/reply buffer manipulation
309 int (*alloc_reqbuf)(struct ptlrpc_sec *sec,
310 struct ptlrpc_request *req,
311 int lustre_msg_size);
312 void (*free_reqbuf) (struct ptlrpc_sec *sec,
313 struct ptlrpc_request *req);
314 int (*alloc_repbuf)(struct ptlrpc_sec *sec,
315 struct ptlrpc_request *req,
316 int lustre_msg_size);
317 void (*free_repbuf) (struct ptlrpc_sec *sec,
318 struct ptlrpc_request *req);
319 int (*enlarge_reqbuf)
320 (struct ptlrpc_sec *sec,
321 struct ptlrpc_request *req,
322 int segment, int newsize);
326 int (*display) (struct ptlrpc_sec *sec,
327 char *buf, int buflen);
330 struct ptlrpc_sec_sops {
331 int (*accept) (struct ptlrpc_request *req);
332 int (*authorize) (struct ptlrpc_request *req);
333 void (*invalidate_ctx)
334 (struct ptlrpc_svc_ctx *ctx);
335 /* buffer manipulation */
336 int (*alloc_rs) (struct ptlrpc_request *req,
338 void (*free_rs) (struct ptlrpc_reply_state *rs);
339 void (*free_ctx) (struct ptlrpc_svc_ctx *ctx);
340 /* reverse credential */
341 int (*install_rctx)(struct obd_import *imp,
342 struct ptlrpc_svc_ctx *ctx);
344 int (*unwrap_bulk) (struct ptlrpc_request *req,
345 struct ptlrpc_bulk_desc *desc);
346 int (*wrap_bulk) (struct ptlrpc_request *req,
347 struct ptlrpc_bulk_desc *desc);
350 struct ptlrpc_sec_policy {
351 struct module *sp_owner;
353 __u32 sp_policy; /* policy number */
354 struct ptlrpc_sec_cops *sp_cops; /* client ops */
355 struct ptlrpc_sec_sops *sp_sops; /* server ops */
358 #define PTLRPC_SEC_FL_REVERSE 0x0001 /* reverse sec */
359 #define PTLRPC_SEC_FL_ROOTONLY 0x0002 /* treat everyone as root */
360 #define PTLRPC_SEC_FL_PAG 0x0004 /* PAG mode */
361 #define PTLRPC_SEC_FL_BULK 0x0008 /* intensive bulk i/o expected */
364 struct ptlrpc_sec_policy *ps_policy;
365 atomic_t ps_refcount;
366 __u32 ps_flavor; /* rpc flavor */
367 unsigned long ps_flags; /* PTLRPC_SEC_FL_XX */
368 struct obd_import *ps_import; /* owning import */
369 spinlock_t ps_lock; /* protect ccache */
370 atomic_t ps_busy; /* busy count */
374 struct list_head ps_gc_list;
375 cfs_time_t ps_gc_interval; /* in seconds */
376 cfs_time_t ps_gc_next; /* in seconds */
379 static inline int sec_is_reverse(struct ptlrpc_sec *sec)
381 return (sec->ps_flags & PTLRPC_SEC_FL_REVERSE);
384 static inline int sec_is_rootonly(struct ptlrpc_sec *sec)
386 return (sec->ps_flags & PTLRPC_SEC_FL_ROOTONLY);
390 struct ptlrpc_svc_ctx {
391 atomic_t sc_refcount;
392 struct ptlrpc_sec_policy *sc_policy;
396 * user identity descriptor
398 #define LUSTRE_MAX_GROUPS (128)
400 struct ptlrpc_user_desc {
413 enum bulk_checksum_alg {
414 BULK_CSUM_ALG_NULL = 0,
418 BULK_CSUM_ALG_SHA256,
419 BULK_CSUM_ALG_SHA384,
420 BULK_CSUM_ALG_SHA512,
424 enum bulk_encrypt_alg {
425 BULK_PRIV_ALG_NULL = 0,
430 struct ptlrpc_bulk_sec_desc {
433 __u32 bsd_csum_alg; /* checksum algorithm */
434 __u32 bsd_priv_alg; /* encrypt algorithm */
435 __u8 bsd_iv[16]; /* encrypt iv */
439 const char * sptlrpc_bulk_csum_alg2name(__u32 csum_alg);
440 const char * sptlrpc_bulk_priv_alg2name(__u32 priv_alg);
441 __u32 sptlrpc_bulk_priv_alg2flags(__u32 priv_alg);
446 struct proc_dir_entry;
447 extern struct proc_dir_entry *sptlrpc_proc_root;
450 * round size up to next power of 2, for slab allocation.
451 * @size must be sane (can't overflow after round up)
453 static inline int size_roundup_power2(int size)
466 * internal support libraries
468 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
469 int segment, int newsize);
474 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy);
475 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy);
477 __u32 sptlrpc_name2flavor(const char *name);
478 char *sptlrpc_flavor2name(__u32 flavor);
481 struct ptlrpc_sec_policy *sptlrpc_policy_get(struct ptlrpc_sec_policy *policy)
483 __module_get(policy->sp_owner);
488 void sptlrpc_policy_put(struct ptlrpc_sec_policy *policy)
490 module_put(policy->sp_owner);
497 unsigned long cli_ctx_status(struct ptlrpc_cli_ctx *ctx)
499 return (ctx->cc_flags & PTLRPC_CTX_STATUS_MASK);
503 int cli_ctx_is_ready(struct ptlrpc_cli_ctx *ctx)
505 return (cli_ctx_status(ctx) == PTLRPC_CTX_UPTODATE);
509 int cli_ctx_is_refreshed(struct ptlrpc_cli_ctx *ctx)
511 return (cli_ctx_status(ctx) != 0);
515 int cli_ctx_is_uptodate(struct ptlrpc_cli_ctx *ctx)
517 return ((ctx->cc_flags & PTLRPC_CTX_UPTODATE) != 0);
521 int cli_ctx_is_error(struct ptlrpc_cli_ctx *ctx)
523 return ((ctx->cc_flags & PTLRPC_CTX_ERROR) != 0);
527 int cli_ctx_is_dead(struct ptlrpc_cli_ctx *ctx)
529 return ((ctx->cc_flags & (PTLRPC_CTX_DEAD | PTLRPC_CTX_ERROR)) != 0);
533 int cli_ctx_is_eternal(struct ptlrpc_cli_ctx *ctx)
535 return ((ctx->cc_flags & PTLRPC_CTX_ETERNAL) != 0);
539 * internal apis which only used by policy impelentation
541 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec);
544 * exported client context api
546 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx);
547 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync);
548 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx);
549 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx);
550 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize);
553 * exported client context wrap/buffers
555 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req);
556 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req);
557 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize);
558 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req);
559 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize);
560 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req);
561 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
562 int segment, int newsize);
563 void sptlrpc_request_out_callback(struct ptlrpc_request *req);
566 * exported higher interface of import & request
568 int sptlrpc_import_get_sec(struct obd_import *imp, struct ptlrpc_svc_ctx *svc_ctx,
569 __u32 flavor, unsigned long flags);
570 void sptlrpc_import_put_sec(struct obd_import *imp);
571 int sptlrpc_import_check_ctx(struct obd_import *imp);
572 void sptlrpc_import_flush_root_ctx(struct obd_import *imp);
573 void sptlrpc_import_flush_my_ctx(struct obd_import *imp);
574 void sptlrpc_import_flush_all_ctx(struct obd_import *imp);
575 int sptlrpc_req_get_ctx(struct ptlrpc_request *req);
576 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync);
577 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout);
578 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req);
579 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode);
581 int sptlrpc_parse_flavor(enum lustre_part from, enum lustre_part to,
582 char *str, struct sec_flavor_config *conf);
585 void sptlrpc_gc_add_sec(struct ptlrpc_sec *sec);
586 void sptlrpc_gc_del_sec(struct ptlrpc_sec *sec);
587 void sptlrpc_gc_add_ctx(struct ptlrpc_cli_ctx *ctx);
590 const char * sec2target_str(struct ptlrpc_sec *sec);
591 int sptlrpc_lprocfs_rd(char *page, char **start, off_t off, int count,
592 int *eof, void *data);
597 enum secsvc_accept_res {
603 int sptlrpc_target_export_check(struct obd_export *exp,
604 struct ptlrpc_request *req);
605 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req);
606 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen);
607 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req);
608 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs);
609 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req);
610 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req);
611 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req);
616 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
617 struct ptlrpc_svc_ctx *ctx);
618 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
619 struct ptlrpc_cli_ctx *ctx);
621 /* bulk security api */
622 int sptlrpc_enc_pool_add_user(void);
623 int sptlrpc_enc_pool_del_user(void);
624 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc);
625 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc);
627 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
628 struct ptlrpc_bulk_desc *desc);
629 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
630 int nob, obd_count pg_count,
631 struct brw_page **pga);
632 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
633 struct ptlrpc_bulk_desc *desc);
634 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
635 struct ptlrpc_bulk_desc *desc);
636 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
637 struct ptlrpc_bulk_desc *desc);
639 /* user descriptor helpers */
640 static inline int sptlrpc_user_desc_size(int ngroups)
642 return sizeof(struct ptlrpc_user_desc) + ngroups * sizeof(__u32);
645 int sptlrpc_current_user_desc_size(void);
646 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset);
647 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset);
649 /* bulk helpers (internal use only by policies) */
650 int bulk_sec_desc_size(__u32 csum_alg, int request, int read);
651 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset);
653 int bulk_csum_cli_request(struct ptlrpc_bulk_desc *desc, int read,
654 __u32 alg, struct lustre_msg *rmsg, int roff);
655 int bulk_csum_cli_reply(struct ptlrpc_bulk_desc *desc, int read,
656 struct lustre_msg *rmsg, int roff,
657 struct lustre_msg *vmsg, int voff);
658 int bulk_csum_svc(struct ptlrpc_bulk_desc *desc, int read,
659 struct ptlrpc_bulk_sec_desc *bsdv, int vsize,
660 struct ptlrpc_bulk_sec_desc *bsdr, int rsize);
663 #endif /* _LUSTRE_SEC_H_ */