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_
29 struct ptlrpc_request;
30 struct ptlrpc_reply_state;
31 struct ptlrpc_bulk_desc;
37 struct ptlrpc_sec_policy;
38 struct ptlrpc_sec_cops;
39 struct ptlrpc_sec_sops;
41 struct ptlrpc_svc_ctx;
42 struct ptlrpc_cli_ctx;
43 struct ptlrpc_ctx_ops;
48 enum sptlrpc_policies {
49 SPTLRPC_POLICY_NULL = 0,
50 SPTLRPC_POLICY_PLAIN = 1,
51 SPTLRPC_POLICY_GSS = 2,
55 enum sptlrpc_subpolicy_null {
56 SPTLRPC_SUBPOLICY_NULL = 0,
57 SPTLRPC_SUBPOLICY_NULL_MAX,
60 enum sptlrpc_subpolicy_plain {
61 SPTLRPC_SUBPOLICY_PLAIN = 0,
62 SPTLRPC_SUBPOLICY_PLAIN_MAX,
65 enum sptlrpc_subpolicy_gss {
66 SPTLRPC_SUBPOLICY_GSS_NONE = 0,
67 SPTLRPC_SUBPOLICY_GSS_KRB5 = 1,
68 SPTLRPC_SUBPOLICY_GSS_MAX,
71 enum sptlrpc_service_type {
72 SPTLRPC_SVC_NONE = 0, /* no security */
73 SPTLRPC_SVC_AUTH = 1, /* authentication */
74 SPTLRPC_SVC_PRIV = 2, /* privacy */
79 * flavor compose/extract
82 typedef __u32 ptlrpc_sec_flavor_t;
85 * 8b (reserved) | 8b (flags) | 6b (policy) | 6b (subpolicy) | 4b (svc)
87 #define SEC_FLAVOR_FLAGS_OFFSET (16)
88 #define SEC_FLAVOR_POLICY_OFFSET (10)
89 #define SEC_FLAVOR_SUBPOLICY_OFFSET (4)
90 #define SEC_FLAVOR_SVC_OFFSET (0)
92 #define SEC_MAKE_RPC_FLAVOR(policy, subpolicy, svc) \
93 (((__u32)(policy) << SEC_FLAVOR_POLICY_OFFSET) | \
94 ((__u32)(subpolicy) << SEC_FLAVOR_SUBPOLICY_OFFSET) | \
95 ((__u32)(svc) << SEC_FLAVOR_SVC_OFFSET))
97 #define SEC_MAKE_RPC_SUBFLAVOR(subpolicy, svc) \
98 (((__u32)(subpolicy) << SEC_FLAVOR_SUBPOLICY_OFFSET) | \
99 ((__u32)(svc) << SEC_FLAVOR_SVC_OFFSET))
101 #define SEC_FLAVOR_POLICY(flavor) \
102 ((((__u32)(flavor)) >> SEC_FLAVOR_POLICY_OFFSET) & 0x3F)
103 #define SEC_FLAVOR_SUBPOLICY(flavor) \
104 ((((__u32)(flavor)) >> SEC_FLAVOR_SUBPOLICY_OFFSET) & 0x3F)
105 #define SEC_FLAVOR_SVC(flavor) \
106 ((((__u32)(flavor)) >> SEC_FLAVOR_SVC_OFFSET) & 0xF)
107 #define SEC_FLAVOR_SUB(flavor) \
108 ((((__u32)(flavor)) >> SEC_FLAVOR_SVC_OFFSET) & 0x3FF)
110 #define SEC_FLAVOR_RPC(f) \
111 (((__u32) f) & ((1 << SEC_FLAVOR_FLAGS_OFFSET) - 1))
114 * general gss flavors
116 #define SPTLRPC_FLVR_GSS_NONE \
117 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
118 SPTLRPC_SUBPOLICY_GSS_NONE, \
120 #define SPTLRPC_FLVR_GSS_AUTH \
121 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
122 SPTLRPC_SUBPOLICY_GSS_NONE, \
124 #define SPTLRPC_FLVR_GSS_PRIV \
125 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
126 SPTLRPC_SUBPOLICY_GSS_NONE, \
132 #define SPTLRPC_SUBFLVR_KRB5 \
133 SEC_MAKE_RPC_SUBFLAVOR(SPTLRPC_SUBPOLICY_GSS_KRB5, \
135 #define SPTLRPC_SUBFLVR_KRB5I \
136 SEC_MAKE_RPC_SUBFLAVOR(SPTLRPC_SUBPOLICY_GSS_KRB5, \
138 #define SPTLRPC_SUBFLVR_KRB5P \
139 SEC_MAKE_RPC_SUBFLAVOR(SPTLRPC_SUBPOLICY_GSS_KRB5, \
145 #define SPTLRPC_FLVR_NULL \
146 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_NULL, \
147 SPTLRPC_SUBPOLICY_NULL, \
149 #define SPTLRPC_FLVR_PLAIN \
150 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_PLAIN, \
151 SPTLRPC_SUBPOLICY_PLAIN, \
153 #define SPTLRPC_FLVR_KRB5 \
154 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
155 SPTLRPC_SUBPOLICY_GSS_KRB5, \
157 #define SPTLRPC_FLVR_KRB5I \
158 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
159 SPTLRPC_SUBPOLICY_GSS_KRB5, \
161 #define SPTLRPC_FLVR_KRB5P \
162 SEC_MAKE_RPC_FLAVOR(SPTLRPC_POLICY_GSS, \
163 SPTLRPC_SUBPOLICY_GSS_KRB5, \
166 #define SPTLRPC_FLVR_INVALID (-1)
168 #define SPTLRPC_FLVR_DEFAULT SPTLRPC_FLVR_NULL
171 * flavor flags (maximum 8 flags)
173 #define SEC_FLAVOR_FL_BULK (1 << (0 + SEC_FLAVOR_FLAGS_OFFSET))
174 #define SEC_FLAVOR_FL_USER (1 << (1 + SEC_FLAVOR_FLAGS_OFFSET))
176 #define SEC_FLAVOR_HAS_BULK(flavor) \
177 (((flavor) & SEC_FLAVOR_FL_BULK) != 0)
178 #define SEC_FLAVOR_HAS_USER(flavor) \
179 (((flavor) & SEC_FLAVOR_FL_USER) != 0)
182 struct sec_flavor_config {
183 __u32 sfc_rpc_flavor; /* main rpc flavor */
184 __u32 sfc_bulk_priv; /* bulk encryption algorithm */
185 __u32 sfc_bulk_csum; /* bulk checksum algorithm */
186 __u32 sfc_flags; /* extra flags */
197 /* The maximum length of security payload. 1024 is enough for Kerberos 5,
198 * and should be enough for other future mechanisms but not sure.
199 * Only used by pre-allocated request/reply pool.
201 #define SPTLRPC_MAX_PAYLOAD (1024)
209 struct ptlrpc_ctx_ops {
210 int (*match) (struct ptlrpc_cli_ctx *ctx,
211 struct vfs_cred *vcred);
212 int (*refresh) (struct ptlrpc_cli_ctx *ctx);
213 int (*display) (struct ptlrpc_cli_ctx *ctx,
214 char *buf, int bufsize);
218 int (*sign) (struct ptlrpc_cli_ctx *ctx,
219 struct ptlrpc_request *req);
220 int (*verify) (struct ptlrpc_cli_ctx *ctx,
221 struct ptlrpc_request *req);
222 int (*seal) (struct ptlrpc_cli_ctx *ctx,
223 struct ptlrpc_request *req);
224 int (*unseal) (struct ptlrpc_cli_ctx *ctx,
225 struct ptlrpc_request *req);
229 int (*wrap_bulk) (struct ptlrpc_cli_ctx *ctx,
230 struct ptlrpc_request *req,
231 struct ptlrpc_bulk_desc *desc);
232 int (*unwrap_bulk) (struct ptlrpc_cli_ctx *ctx,
233 struct ptlrpc_request *req,
234 struct ptlrpc_bulk_desc *desc);
237 #define PTLRPC_CTX_UPTODATE_BIT (0) /* uptodate */
238 #define PTLRPC_CTX_DEAD_BIT (1) /* mark expired gracefully */
239 #define PTLRPC_CTX_ERROR_BIT (2) /* fatal error (refresh, etc.) */
240 #define PTLRPC_CTX_HASHED_BIT (8) /* in hash table */
241 #define PTLRPC_CTX_ETERNAL_BIT (9) /* always valid */
243 #define PTLRPC_CTX_UPTODATE (1 << PTLRPC_CTX_UPTODATE_BIT)
244 #define PTLRPC_CTX_DEAD (1 << PTLRPC_CTX_DEAD_BIT)
245 #define PTLRPC_CTX_ERROR (1 << PTLRPC_CTX_ERROR_BIT)
246 #define PTLRPC_CTX_HASHED (1 << PTLRPC_CTX_HASHED_BIT)
247 #define PTLRPC_CTX_ETERNAL (1 << PTLRPC_CTX_ETERNAL_BIT)
249 #define PTLRPC_CTX_STATUS_MASK (PTLRPC_CTX_UPTODATE | \
253 struct ptlrpc_cli_ctx {
254 struct hlist_node cc_hash; /* linked into hash table */
255 atomic_t cc_refcount;
256 struct ptlrpc_sec *cc_sec;
257 struct ptlrpc_ctx_ops *cc_ops;
258 cfs_time_t cc_expire; /* in seconds */
259 unsigned long cc_flags;
260 struct vfs_cred cc_vcred;
262 struct list_head cc_req_list; /* waiting reqs linked here */
265 struct ptlrpc_sec_cops {
267 * ptlrpc_sec constructor/destructor
269 struct ptlrpc_sec * (*create_sec) (struct obd_import *imp,
270 struct ptlrpc_svc_ctx *ctx,
272 unsigned long flags);
273 void (*destroy_sec) (struct ptlrpc_sec *sec);
275 * search ctx for a certain user, if this function is missing,
276 * a generic function will be invoked by caller. implement this
277 * for any special need.
279 struct ptlrpc_cli_ctx * (*lookup_ctx) (struct ptlrpc_sec *sec,
280 struct vfs_cred *vcred);
282 * ptlrpc_cli_ctx constructor/destructor
284 struct ptlrpc_cli_ctx * (*create_ctx) (struct ptlrpc_sec *sec,
285 struct vfs_cred *vcred);
286 void (*destroy_ctx) (struct ptlrpc_sec *sec,
287 struct ptlrpc_cli_ctx *ctx);
288 /* reverse service */
289 int (*install_rctx)(struct obd_import *imp,
290 struct ptlrpc_sec *sec,
291 struct ptlrpc_cli_ctx *ctx);
293 * request/reply buffer manipulation
295 int (*alloc_reqbuf)(struct ptlrpc_sec *sec,
296 struct ptlrpc_request *req,
297 int lustre_msg_size);
298 void (*free_reqbuf) (struct ptlrpc_sec *sec,
299 struct ptlrpc_request *req);
300 int (*alloc_repbuf)(struct ptlrpc_sec *sec,
301 struct ptlrpc_request *req,
302 int lustre_msg_size);
303 void (*free_repbuf) (struct ptlrpc_sec *sec,
304 struct ptlrpc_request *req);
305 int (*enlarge_reqbuf)
306 (struct ptlrpc_sec *sec,
307 struct ptlrpc_request *req,
308 int segment, int newsize);
311 struct ptlrpc_sec_sops {
312 int (*accept) (struct ptlrpc_request *req);
313 int (*authorize) (struct ptlrpc_request *req);
314 void (*invalidate_ctx)
315 (struct ptlrpc_svc_ctx *ctx);
316 /* buffer manipulation */
317 int (*alloc_rs) (struct ptlrpc_request *req,
319 void (*free_rs) (struct ptlrpc_reply_state *rs);
320 void (*free_ctx) (struct ptlrpc_svc_ctx *ctx);
321 /* reverse credential */
322 int (*install_rctx)(struct obd_import *imp,
323 struct ptlrpc_svc_ctx *ctx);
325 int (*unwrap_bulk) (struct ptlrpc_request *req,
326 struct ptlrpc_bulk_desc *desc);
327 int (*wrap_bulk) (struct ptlrpc_request *req,
328 struct ptlrpc_bulk_desc *desc);
331 struct ptlrpc_sec_policy {
332 struct module *sp_owner;
334 __u32 sp_policy; /* policy number */
335 struct ptlrpc_sec_cops *sp_cops; /* client ops */
336 struct ptlrpc_sec_sops *sp_sops; /* server ops */
339 #define PTLRPC_SEC_FL_REVERSE 0x0001 /* reverse sec */
340 #define PTLRPC_SEC_FL_ROOTONLY 0x0002 /* treat everyone as root */
341 #define PTLRPC_SEC_FL_BULK 0x0004 /* intensive bulk i/o expected */
344 struct ptlrpc_sec_policy *ps_policy;
345 atomic_t ps_refcount;
346 __u32 ps_flavor; /* rpc flavor */
347 unsigned long ps_flags; /* PTLRPC_SEC_FL_XX */
348 struct obd_import *ps_import; /* owning import */
349 spinlock_t ps_lock; /* protect ccache */
350 int ps_ccache_size; /* must be 2^n */
351 struct hlist_head *ps_ccache; /* ctx cache hash */
352 atomic_t ps_busy; /* busy count */
353 cfs_time_t ps_gc_interval; /* in seconds */
354 cfs_time_t ps_gc_next; /* in seconds */
357 struct ptlrpc_svc_ctx {
358 atomic_t sc_refcount;
359 struct ptlrpc_sec_policy *sc_policy;
363 * user identity descriptor
365 #define LUSTRE_MAX_GROUPS (128)
367 struct ptlrpc_user_desc {
380 enum bulk_checksum_alg {
381 BULK_CSUM_ALG_NULL = 0,
385 BULK_CSUM_ALG_SHA256,
386 BULK_CSUM_ALG_SHA384,
387 BULK_CSUM_ALG_SHA512,
391 enum bulk_encrypt_alg {
392 BULK_PRIV_ALG_NULL = 0,
397 struct ptlrpc_bulk_sec_desc {
400 __u32 bsd_csum_alg; /* checksum algorithm */
401 __u32 bsd_priv_alg; /* encrypt algorithm */
402 __u8 bsd_iv[16]; /* encrypt iv */
406 const char * sptlrpc_bulk_csum_alg2name(__u32 csum_alg);
407 const char * sptlrpc_bulk_priv_alg2name(__u32 priv_alg);
408 __u32 sptlrpc_bulk_priv_alg2flags(__u32 priv_alg);
413 struct proc_dir_entry;
414 extern struct proc_dir_entry *sptlrpc_proc_root;
417 * round size up to next power of 2, for slab allocation.
418 * @size must be sane (can't overflow after round up)
420 static inline int size_roundup_power2(int size)
433 * internal support libraries
435 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
436 int segment, int newsize);
441 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy);
442 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy);
444 __u32 sptlrpc_name2flavor(const char *name);
445 char *sptlrpc_flavor2name(__u32 flavor);
448 struct ptlrpc_sec_policy *sptlrpc_policy_get(struct ptlrpc_sec_policy *policy)
450 __module_get(policy->sp_owner);
455 void sptlrpc_policy_put(struct ptlrpc_sec_policy *policy)
457 module_put(policy->sp_owner);
463 struct ptlrpc_cli_ctx *sptlrpc_ctx_get(struct ptlrpc_cli_ctx *ctx);
464 void sptlrpc_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync);
465 void sptlrpc_ctx_expire(struct ptlrpc_cli_ctx *ctx);
466 void sptlrpc_ctx_replace(struct ptlrpc_sec *sec, struct ptlrpc_cli_ctx *new);
467 void sptlrpc_ctx_wakeup(struct ptlrpc_cli_ctx *ctx);
468 int sptlrpc_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize);
471 * client wrap/buffers
473 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req);
474 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req);
475 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize);
476 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req);
477 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize);
478 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req);
479 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
480 int segment, int newsize);
481 void sptlrpc_request_out_callback(struct ptlrpc_request *req);
484 * higher interface of import & request
486 int sptlrpc_import_get_sec(struct obd_import *imp, struct ptlrpc_svc_ctx *svc_ctx,
487 __u32 flavor, unsigned long flags);
488 void sptlrpc_import_put_sec(struct obd_import *imp);
489 int sptlrpc_import_check_ctx(struct obd_import *imp);
490 void sptlrpc_import_flush_root_ctx(struct obd_import *imp);
491 void sptlrpc_import_flush_my_ctx(struct obd_import *imp);
492 void sptlrpc_import_flush_all_ctx(struct obd_import *imp);
493 int sptlrpc_req_get_ctx(struct ptlrpc_request *req);
494 void sptlrpc_req_put_ctx(struct ptlrpc_request *req);
495 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout);
496 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req);
497 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode);
499 int sptlrpc_parse_flavor(enum lustre_part from, enum lustre_part to,
500 char *str, struct sec_flavor_config *conf);
502 const char * sec2target_str(struct ptlrpc_sec *sec);
503 int sptlrpc_lprocfs_rd(char *page, char **start, off_t off, int count,
504 int *eof, void *data);
509 enum secsvc_accept_res {
515 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req);
516 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen);
517 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req);
518 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs);
519 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req);
520 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req);
521 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req);
526 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
527 struct ptlrpc_svc_ctx *ctx);
528 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
529 struct ptlrpc_cli_ctx *ctx);
531 /* bulk security api */
532 int sptlrpc_enc_pool_add_user(void);
533 int sptlrpc_enc_pool_del_user(void);
534 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc);
535 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc);
537 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
538 struct ptlrpc_bulk_desc *desc);
539 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
540 int nob, obd_count pg_count,
541 struct brw_page **pga);
542 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
543 struct ptlrpc_bulk_desc *desc);
544 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
545 struct ptlrpc_bulk_desc *desc);
546 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
547 struct ptlrpc_bulk_desc *desc);
549 /* user descriptor helpers */
550 static inline int sptlrpc_user_desc_size(int ngroups)
552 return sizeof(struct ptlrpc_user_desc) + ngroups * sizeof(__u32);
555 int sptlrpc_current_user_desc_size(void);
556 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset);
557 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset);
559 /* bulk helpers (internal use only by policies) */
560 int bulk_sec_desc_size(__u32 csum_alg, int request, int read);
561 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset);
563 int bulk_csum_cli_request(struct ptlrpc_bulk_desc *desc, int read,
564 __u32 alg, struct lustre_msg *rmsg, int roff);
565 int bulk_csum_cli_reply(struct ptlrpc_bulk_desc *desc, int read,
566 struct lustre_msg *rmsg, int roff,
567 struct lustre_msg *vmsg, int voff);
568 int bulk_csum_svc(struct ptlrpc_bulk_desc *desc, int read,
569 struct lustre_msg *vmsg, int voff,
570 struct lustre_msg *rmsg, int roff);
573 #endif /* _LUSTRE_SEC_H_ */