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_
30 struct ptlrpc_request;
31 struct ptlrpc_reply_state;
32 struct ptlrpc_bulk_desc;
38 struct ptlrpc_sec_policy;
39 struct ptlrpc_sec_cops;
40 struct ptlrpc_sec_sops;
42 struct ptlrpc_svc_ctx;
43 struct ptlrpc_cli_ctx;
44 struct ptlrpc_ctx_ops;
49 enum sptlrpc_policies {
50 SPTLRPC_POLICY_NULL = 0,
51 SPTLRPC_POLICY_PLAIN = 1,
52 SPTLRPC_POLICY_GSS = 2,
53 SPTLRPC_POLICY_GSS_PIPEFS = 3,
57 enum sptlrpc_subpolicy_null {
58 SPTLRPC_SUBPOLICY_NULL = 0,
59 SPTLRPC_SUBPOLICY_NULL_MAX,
62 enum sptlrpc_subpolicy_plain {
63 SPTLRPC_SUBPOLICY_PLAIN = 0,
64 SPTLRPC_SUBPOLICY_PLAIN_MAX,
67 enum sptlrpc_subpolicy_gss {
68 SPTLRPC_SUBPOLICY_GSS_NONE = 0,
69 SPTLRPC_SUBPOLICY_GSS_KRB5 = 1,
70 SPTLRPC_SUBPOLICY_GSS_MAX,
73 enum sptlrpc_service_type {
74 SPTLRPC_SVC_NONE = 0, /* no security */
75 SPTLRPC_SVC_AUTH = 1, /* authentication */
76 SPTLRPC_SVC_PRIV = 2, /* privacy */
81 * flavor compose/extract
84 typedef __u32 ptlrpc_sec_flavor_t;
87 * 8b (reserved) | 8b (flags) | 6b (policy) | 6b (subpolicy) | 4b (svc)
89 #define SEC_FLAVOR_FLAGS_OFFSET (16)
90 #define SEC_FLAVOR_POLICY_OFFSET (10)
91 #define SEC_FLAVOR_SUBPOLICY_OFFSET (4)
92 #define SEC_FLAVOR_SVC_OFFSET (0)
94 #define SEC_MAKE_RPC_FLAVOR(policy, subpolicy, svc) \
95 (((__u32)(policy) << SEC_FLAVOR_POLICY_OFFSET) | \
96 ((__u32)(subpolicy) << SEC_FLAVOR_SUBPOLICY_OFFSET) | \
97 ((__u32)(svc) << SEC_FLAVOR_SVC_OFFSET))
99 #define SEC_MAKE_RPC_SUBFLAVOR(subpolicy, svc) \
100 (((__u32)(subpolicy) << SEC_FLAVOR_SUBPOLICY_OFFSET) | \
101 ((__u32)(svc) << SEC_FLAVOR_SVC_OFFSET))
103 #define SEC_FLAVOR_POLICY(flavor) \
104 ((((__u32)(flavor)) >> SEC_FLAVOR_POLICY_OFFSET) & 0x3F)
105 #define SEC_FLAVOR_SUBPOLICY(flavor) \
106 ((((__u32)(flavor)) >> SEC_FLAVOR_SUBPOLICY_OFFSET) & 0x3F)
107 #define SEC_FLAVOR_SVC(flavor) \
108 ((((__u32)(flavor)) >> SEC_FLAVOR_SVC_OFFSET) & 0xF)
109 #define SEC_FLAVOR_SUB(flavor) \
110 ((((__u32)(flavor)) >> SEC_FLAVOR_SVC_OFFSET) & 0x3FF)
112 #define SEC_FLAVOR_RPC(f) \
113 (((__u32) f) & ((1 << SEC_FLAVOR_FLAGS_OFFSET) - 1))
116 * general gss flavors
118 #define SPTLRPC_FLVR_GSS_NONE \
119 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
120 SPTLRPC_SUBPOLICY_GSS_NONE, \
122 #define SPTLRPC_FLVR_GSS_AUTH \
123 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
124 SPTLRPC_SUBPOLICY_GSS_NONE, \
126 #define SPTLRPC_FLVR_GSS_PRIV \
127 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
128 SPTLRPC_SUBPOLICY_GSS_NONE, \
134 #define SPTLRPC_SUBFLVR_KRB5 \
135 SEC_MAKE_RPC_SUBFLAVOR(SPTLRPC_SUBPOLICY_GSS_KRB5, \
137 #define SPTLRPC_SUBFLVR_KRB5I \
138 SEC_MAKE_RPC_SUBFLAVOR(SPTLRPC_SUBPOLICY_GSS_KRB5, \
140 #define SPTLRPC_SUBFLVR_KRB5P \
141 SEC_MAKE_RPC_SUBFLAVOR(SPTLRPC_SUBPOLICY_GSS_KRB5, \
147 #define SPTLRPC_FLVR_NULL \
148 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_NULL, \
149 SPTLRPC_SUBPOLICY_NULL, \
151 #define SPTLRPC_FLVR_PLAIN \
152 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_PLAIN, \
153 SPTLRPC_SUBPOLICY_PLAIN, \
155 #define SPTLRPC_FLVR_KRB5 \
156 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
157 SPTLRPC_SUBPOLICY_GSS_KRB5, \
159 #define SPTLRPC_FLVR_KRB5I \
160 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
161 SPTLRPC_SUBPOLICY_GSS_KRB5, \
163 #define SPTLRPC_FLVR_KRB5P \
164 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
165 SPTLRPC_SUBPOLICY_GSS_KRB5, \
168 #define SPTLRPC_FLVR_INVALID (-1)
170 #define SPTLRPC_FLVR_DEFAULT SPTLRPC_FLVR_NULL
173 * flavor flags (maximum 8 flags)
175 #define SEC_FLAVOR_FL_BULK (1 << (0 + SEC_FLAVOR_FLAGS_OFFSET))
176 #define SEC_FLAVOR_FL_USER (1 << (1 + SEC_FLAVOR_FLAGS_OFFSET))
178 #define SEC_FLAVOR_HAS_BULK(flavor) \
179 (((flavor) & SEC_FLAVOR_FL_BULK) != 0)
180 #define SEC_FLAVOR_HAS_USER(flavor) \
181 (((flavor) & SEC_FLAVOR_FL_USER) != 0)
184 struct sec_flavor_config {
185 __u32 sfc_rpc_flavor; /* main rpc flavor */
186 __u32 sfc_bulk_priv; /* bulk encryption algorithm */
187 __u32 sfc_bulk_csum; /* bulk checksum algorithm */
188 __u32 sfc_flags; /* extra flags */
199 /* The maximum length of security payload. 1024 is enough for Kerberos 5,
200 * and should be enough for other future mechanisms but not sure.
201 * Only used by pre-allocated request/reply pool.
203 #define SPTLRPC_MAX_PAYLOAD (1024)
211 struct ptlrpc_ctx_ops {
212 int (*match) (struct ptlrpc_cli_ctx *ctx,
213 struct vfs_cred *vcred);
214 int (*refresh) (struct ptlrpc_cli_ctx *ctx);
215 int (*validate) (struct ptlrpc_cli_ctx *ctx);
216 void (*die) (struct ptlrpc_cli_ctx *ctx,
218 int (*display) (struct ptlrpc_cli_ctx *ctx,
219 char *buf, int bufsize);
223 int (*sign) (struct ptlrpc_cli_ctx *ctx,
224 struct ptlrpc_request *req);
225 int (*verify) (struct ptlrpc_cli_ctx *ctx,
226 struct ptlrpc_request *req);
227 int (*seal) (struct ptlrpc_cli_ctx *ctx,
228 struct ptlrpc_request *req);
229 int (*unseal) (struct ptlrpc_cli_ctx *ctx,
230 struct ptlrpc_request *req);
234 int (*wrap_bulk) (struct ptlrpc_cli_ctx *ctx,
235 struct ptlrpc_request *req,
236 struct ptlrpc_bulk_desc *desc);
237 int (*unwrap_bulk) (struct ptlrpc_cli_ctx *ctx,
238 struct ptlrpc_request *req,
239 struct ptlrpc_bulk_desc *desc);
242 #define PTLRPC_CTX_NEW_BIT (0) /* newly created */
243 #define PTLRPC_CTX_UPTODATE_BIT (1) /* uptodate */
244 #define PTLRPC_CTX_DEAD_BIT (2) /* mark expired gracefully */
245 #define PTLRPC_CTX_ERROR_BIT (3) /* fatal error (refresh, etc.) */
246 #define PTLRPC_CTX_CACHED_BIT (8) /* in ctx cache (hash etc.) */
247 #define PTLRPC_CTX_ETERNAL_BIT (9) /* always valid */
249 #define PTLRPC_CTX_NEW (1 << PTLRPC_CTX_NEW_BIT)
250 #define PTLRPC_CTX_UPTODATE (1 << PTLRPC_CTX_UPTODATE_BIT)
251 #define PTLRPC_CTX_DEAD (1 << PTLRPC_CTX_DEAD_BIT)
252 #define PTLRPC_CTX_ERROR (1 << PTLRPC_CTX_ERROR_BIT)
253 #define PTLRPC_CTX_CACHED (1 << PTLRPC_CTX_CACHED_BIT)
254 #define PTLRPC_CTX_ETERNAL (1 << PTLRPC_CTX_ETERNAL_BIT)
256 #define PTLRPC_CTX_STATUS_MASK (PTLRPC_CTX_NEW_BIT | \
257 PTLRPC_CTX_UPTODATE | \
261 struct ptlrpc_cli_ctx {
262 struct hlist_node cc_hash; /* linked into hash table */
263 atomic_t cc_refcount;
264 struct ptlrpc_sec *cc_sec;
265 struct ptlrpc_ctx_ops *cc_ops;
266 cfs_time_t cc_expire; /* in seconds */
267 unsigned long cc_flags;
268 struct vfs_cred cc_vcred;
270 struct list_head cc_req_list; /* waiting reqs linked here */
273 struct ptlrpc_sec_cops {
275 * ptlrpc_sec constructor/destructor
277 struct ptlrpc_sec * (*create_sec) (struct obd_import *imp,
278 struct ptlrpc_svc_ctx *ctx,
280 unsigned long flags);
281 void (*destroy_sec) (struct ptlrpc_sec *sec);
286 struct ptlrpc_cli_ctx * (*lookup_ctx) (struct ptlrpc_sec *sec,
287 struct vfs_cred *vcred,
290 void (*release_ctx) (struct ptlrpc_sec *sec,
291 struct ptlrpc_cli_ctx *ctx,
293 int (*flush_ctx_cache)
294 (struct ptlrpc_sec *sec,
298 void (*gc_ctx) (struct ptlrpc_sec *sec);
303 int (*install_rctx)(struct obd_import *imp,
304 struct ptlrpc_sec *sec,
305 struct ptlrpc_cli_ctx *ctx);
308 * request/reply buffer manipulation
310 int (*alloc_reqbuf)(struct ptlrpc_sec *sec,
311 struct ptlrpc_request *req,
312 int lustre_msg_size);
313 void (*free_reqbuf) (struct ptlrpc_sec *sec,
314 struct ptlrpc_request *req);
315 int (*alloc_repbuf)(struct ptlrpc_sec *sec,
316 struct ptlrpc_request *req,
317 int lustre_msg_size);
318 void (*free_repbuf) (struct ptlrpc_sec *sec,
319 struct ptlrpc_request *req);
320 int (*enlarge_reqbuf)
321 (struct ptlrpc_sec *sec,
322 struct ptlrpc_request *req,
323 int segment, int newsize);
327 int (*display) (struct ptlrpc_sec *sec,
328 char *buf, int buflen);
331 struct ptlrpc_sec_sops {
332 int (*accept) (struct ptlrpc_request *req);
333 int (*authorize) (struct ptlrpc_request *req);
334 void (*invalidate_ctx)
335 (struct ptlrpc_svc_ctx *ctx);
336 /* buffer manipulation */
337 int (*alloc_rs) (struct ptlrpc_request *req,
339 void (*free_rs) (struct ptlrpc_reply_state *rs);
340 void (*free_ctx) (struct ptlrpc_svc_ctx *ctx);
341 /* reverse credential */
342 int (*install_rctx)(struct obd_import *imp,
343 struct ptlrpc_svc_ctx *ctx);
345 int (*unwrap_bulk) (struct ptlrpc_request *req,
346 struct ptlrpc_bulk_desc *desc);
347 int (*wrap_bulk) (struct ptlrpc_request *req,
348 struct ptlrpc_bulk_desc *desc);
351 struct ptlrpc_sec_policy {
352 struct module *sp_owner;
354 __u32 sp_policy; /* policy number */
355 struct ptlrpc_sec_cops *sp_cops; /* client ops */
356 struct ptlrpc_sec_sops *sp_sops; /* server ops */
359 #define PTLRPC_SEC_FL_REVERSE 0x0001 /* reverse sec */
360 #define PTLRPC_SEC_FL_ROOTONLY 0x0002 /* treat everyone as root */
361 #define PTLRPC_SEC_FL_PAG 0x0004 /* PAG mode */
362 #define PTLRPC_SEC_FL_BULK 0x0008 /* intensive bulk i/o expected */
365 struct ptlrpc_sec_policy *ps_policy;
366 atomic_t ps_refcount;
367 __u32 ps_flavor; /* rpc flavor */
368 unsigned long ps_flags; /* PTLRPC_SEC_FL_XX */
369 struct obd_import *ps_import; /* owning import */
370 spinlock_t ps_lock; /* protect ccache */
371 atomic_t ps_busy; /* busy count */
375 struct list_head ps_gc_list;
376 cfs_time_t ps_gc_interval; /* in seconds */
377 cfs_time_t ps_gc_next; /* in seconds */
380 struct ptlrpc_svc_ctx {
381 atomic_t sc_refcount;
382 struct ptlrpc_sec_policy *sc_policy;
386 * user identity descriptor
388 #define LUSTRE_MAX_GROUPS (128)
390 struct ptlrpc_user_desc {
403 enum bulk_checksum_alg {
404 BULK_CSUM_ALG_NULL = 0,
408 BULK_CSUM_ALG_SHA256,
409 BULK_CSUM_ALG_SHA384,
410 BULK_CSUM_ALG_SHA512,
414 enum bulk_encrypt_alg {
415 BULK_PRIV_ALG_NULL = 0,
420 struct ptlrpc_bulk_sec_desc {
423 __u32 bsd_csum_alg; /* checksum algorithm */
424 __u32 bsd_priv_alg; /* encrypt algorithm */
425 __u8 bsd_iv[16]; /* encrypt iv */
429 const char * sptlrpc_bulk_csum_alg2name(__u32 csum_alg);
430 const char * sptlrpc_bulk_priv_alg2name(__u32 priv_alg);
431 __u32 sptlrpc_bulk_priv_alg2flags(__u32 priv_alg);
436 struct proc_dir_entry;
437 extern struct proc_dir_entry *sptlrpc_proc_root;
440 * round size up to next power of 2, for slab allocation.
441 * @size must be sane (can't overflow after round up)
443 static inline int size_roundup_power2(int size)
456 * internal support libraries
458 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
459 int segment, int newsize);
464 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy);
465 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy);
467 __u32 sptlrpc_name2flavor(const char *name);
468 char *sptlrpc_flavor2name(__u32 flavor);
471 struct ptlrpc_sec_policy *sptlrpc_policy_get(struct ptlrpc_sec_policy *policy)
473 __module_get(policy->sp_owner);
478 void sptlrpc_policy_put(struct ptlrpc_sec_policy *policy)
480 module_put(policy->sp_owner);
487 unsigned long cli_ctx_status(struct ptlrpc_cli_ctx *ctx)
489 return (ctx->cc_flags & PTLRPC_CTX_STATUS_MASK);
493 int cli_ctx_is_uptodate(struct ptlrpc_cli_ctx *ctx)
495 return (cli_ctx_status(ctx) == PTLRPC_CTX_UPTODATE);
499 int cli_ctx_is_refreshed(struct ptlrpc_cli_ctx *ctx)
501 return (cli_ctx_status(ctx) != 0);
505 int cli_ctx_is_dead(struct ptlrpc_cli_ctx *ctx)
507 return ((ctx->cc_flags & (PTLRPC_CTX_DEAD | PTLRPC_CTX_ERROR)) != 0);
511 int cli_ctx_is_eternal(struct ptlrpc_cli_ctx *ctx)
513 return ((ctx->cc_flags & PTLRPC_CTX_ETERNAL) != 0);
517 * internal apis which only used by policy impelentation
519 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec);
522 * exported client context api
524 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx);
525 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync);
526 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx);
527 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx);
528 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize);
531 * exported client context wrap/buffers
533 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req);
534 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req);
535 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize);
536 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req);
537 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize);
538 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req);
539 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
540 int segment, int newsize);
541 void sptlrpc_request_out_callback(struct ptlrpc_request *req);
544 * exported higher interface of import & request
546 int sptlrpc_import_get_sec(struct obd_import *imp, struct ptlrpc_svc_ctx *svc_ctx,
547 __u32 flavor, unsigned long flags);
548 void sptlrpc_import_put_sec(struct obd_import *imp);
549 int sptlrpc_import_check_ctx(struct obd_import *imp);
550 void sptlrpc_import_flush_root_ctx(struct obd_import *imp);
551 void sptlrpc_import_flush_my_ctx(struct obd_import *imp);
552 void sptlrpc_import_flush_all_ctx(struct obd_import *imp);
553 int sptlrpc_req_get_ctx(struct ptlrpc_request *req);
554 void sptlrpc_req_put_ctx(struct ptlrpc_request *req);
555 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout);
556 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req);
557 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode);
559 int sptlrpc_parse_flavor(enum lustre_part from, enum lustre_part to,
560 char *str, struct sec_flavor_config *conf);
563 const char * sec2target_str(struct ptlrpc_sec *sec);
564 int sptlrpc_lprocfs_rd(char *page, char **start, off_t off, int count,
565 int *eof, void *data);
570 enum secsvc_accept_res {
576 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req);
577 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen);
578 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req);
579 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs);
580 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req);
581 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req);
582 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req);
587 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
588 struct ptlrpc_svc_ctx *ctx);
589 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
590 struct ptlrpc_cli_ctx *ctx);
592 /* bulk security api */
593 int sptlrpc_enc_pool_add_user(void);
594 int sptlrpc_enc_pool_del_user(void);
595 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc);
596 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc);
598 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
599 struct ptlrpc_bulk_desc *desc);
600 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
601 int nob, obd_count pg_count,
602 struct brw_page **pga);
603 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
604 struct ptlrpc_bulk_desc *desc);
605 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
606 struct ptlrpc_bulk_desc *desc);
607 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
608 struct ptlrpc_bulk_desc *desc);
610 /* user descriptor helpers */
611 static inline int sptlrpc_user_desc_size(int ngroups)
613 return sizeof(struct ptlrpc_user_desc) + ngroups * sizeof(__u32);
616 int sptlrpc_current_user_desc_size(void);
617 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset);
618 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset);
620 /* bulk helpers (internal use only by policies) */
621 int bulk_sec_desc_size(__u32 csum_alg, int request, int read);
622 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset);
624 int bulk_csum_cli_request(struct ptlrpc_bulk_desc *desc, int read,
625 __u32 alg, struct lustre_msg *rmsg, int roff);
626 int bulk_csum_cli_reply(struct ptlrpc_bulk_desc *desc, int read,
627 struct lustre_msg *rmsg, int roff,
628 struct lustre_msg *vmsg, int voff);
629 int bulk_csum_svc(struct ptlrpc_bulk_desc *desc, int read,
630 struct lustre_msg *vmsg, int voff,
631 struct lustre_msg *rmsg, int roff);
634 #endif /* _LUSTRE_SEC_H_ */