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,
312 struct ptlrpc_sec_sops {
313 int (*accept) (struct ptlrpc_request *req);
314 int (*authorize) (struct ptlrpc_request *req);
315 /* buffer manipulation */
316 int (*alloc_rs) (struct ptlrpc_request *req,
318 void (*free_rs) (struct ptlrpc_reply_state *rs);
319 void (*free_ctx) (struct ptlrpc_svc_ctx *ctx);
320 /* reverse credential */
321 int (*install_rctx)(struct obd_import *imp,
322 struct ptlrpc_svc_ctx *ctx);
324 int (*unwrap_bulk) (struct ptlrpc_request *req,
325 struct ptlrpc_bulk_desc *desc);
326 int (*wrap_bulk) (struct ptlrpc_request *req,
327 struct ptlrpc_bulk_desc *desc);
330 struct ptlrpc_sec_policy {
331 struct module *sp_owner;
333 __u32 sp_policy; /* policy number */
334 struct ptlrpc_sec_cops *sp_cops; /* client ops */
335 struct ptlrpc_sec_sops *sp_sops; /* server ops */
338 #define PTLRPC_SEC_FL_REVERSE 0x0001 /* reverse sec */
339 #define PTLRPC_SEC_FL_ROOTONLY 0x0002 /* treat everyone as root */
340 #define PTLRPC_SEC_FL_BULK 0x0004 /* intensive bulk i/o expected */
343 struct ptlrpc_sec_policy *ps_policy;
344 atomic_t ps_refcount;
345 __u32 ps_flavor; /* rpc flavor */
346 unsigned long ps_flags; /* PTLRPC_SEC_FL_XX */
347 struct obd_import *ps_import; /* owning import */
348 spinlock_t ps_lock; /* protect ccache */
349 int ps_ccache_size; /* must be 2^n */
350 struct hlist_head *ps_ccache; /* ctx cache hash */
351 atomic_t ps_busy; /* busy count */
352 cfs_time_t ps_gc_interval; /* in seconds */
353 cfs_time_t ps_gc_next; /* in seconds */
356 struct ptlrpc_svc_ctx {
357 atomic_t sc_refcount;
358 struct ptlrpc_sec_policy *sc_policy;
362 * user identity descriptor
364 #define LUSTRE_MAX_GROUPS (128)
366 struct ptlrpc_user_desc {
379 enum bulk_checksum_alg {
380 BULK_CSUM_ALG_NULL = 0,
384 BULK_CSUM_ALG_SHA256,
385 BULK_CSUM_ALG_SHA384,
386 BULK_CSUM_ALG_SHA512,
390 enum bulk_encrypt_alg {
391 BULK_PRIV_ALG_NULL = 0,
396 struct ptlrpc_bulk_sec_desc {
399 __u32 bsd_csum_alg; /* checksum algorithm */
400 __u32 bsd_priv_alg; /* encrypt algorithm */
401 __u8 bsd_iv[16]; /* encrypt iv */
405 const char * sptlrpc_bulk_csum_alg2name(__u32 csum_alg);
406 const char * sptlrpc_bulk_priv_alg2name(__u32 priv_alg);
411 struct proc_dir_entry;
412 extern struct proc_dir_entry *sptlrpc_proc_root;
417 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy);
418 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy);
420 __u32 sptlrpc_name2flavor(const char *name);
421 char *sptlrpc_flavor2name(__u32 flavor);
424 struct ptlrpc_sec_policy *sptlrpc_policy_get(struct ptlrpc_sec_policy *policy)
426 __module_get(policy->sp_owner);
431 void sptlrpc_policy_put(struct ptlrpc_sec_policy *policy)
433 module_put(policy->sp_owner);
439 struct ptlrpc_cli_ctx *sptlrpc_ctx_get(struct ptlrpc_cli_ctx *ctx);
440 void sptlrpc_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync);
441 void sptlrpc_ctx_expire(struct ptlrpc_cli_ctx *ctx);
442 void sptlrpc_ctx_replace(struct ptlrpc_sec *sec, struct ptlrpc_cli_ctx *new);
443 void sptlrpc_ctx_wakeup(struct ptlrpc_cli_ctx *ctx);
444 int sptlrpc_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize);
447 * client wrap/buffers
449 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req);
450 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req);
451 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize);
452 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req);
453 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize);
454 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req);
455 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
456 int segment, int newsize, int movedata);
457 void sptlrpc_request_out_callback(struct ptlrpc_request *req);
460 * higher interface of import & request
462 int sptlrpc_import_get_sec(struct obd_import *imp, struct ptlrpc_svc_ctx *svc_ctx,
463 __u32 flavor, unsigned long flags);
464 void sptlrpc_import_put_sec(struct obd_import *imp);
465 int sptlrpc_import_check_ctx(struct obd_import *imp);
466 void sptlrpc_import_flush_root_ctx(struct obd_import *imp);
467 void sptlrpc_import_flush_my_ctx(struct obd_import *imp);
468 void sptlrpc_import_flush_all_ctx(struct obd_import *imp);
469 int sptlrpc_req_get_ctx(struct ptlrpc_request *req);
470 void sptlrpc_req_put_ctx(struct ptlrpc_request *req);
471 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout);
472 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode);
474 int sptlrpc_parse_flavor(enum lustre_part from, enum lustre_part to,
475 char *str, struct sec_flavor_config *conf);
477 const char * sec2target_str(struct ptlrpc_sec *sec);
478 int sptlrpc_lprocfs_rd(char *page, char **start, off_t off, int count,
479 int *eof, void *data);
484 enum secsvc_accept_res {
490 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req);
491 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen);
492 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req);
493 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs);
494 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req);
495 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req);
500 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
501 struct ptlrpc_svc_ctx *ctx);
502 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
503 struct ptlrpc_cli_ctx *ctx);
505 /* bulk security api */
506 int sptlrpc_enc_pool_add_user(void);
507 int sptlrpc_enc_pool_del_user(void);
508 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc);
509 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc);
511 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
512 struct ptlrpc_bulk_desc *desc);
513 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
514 int nob, obd_count pg_count,
515 struct brw_page **pga);
516 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
517 struct ptlrpc_bulk_desc *desc);
518 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
519 struct ptlrpc_bulk_desc *desc);
520 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
521 struct ptlrpc_bulk_desc *desc);
523 /* user descriptor helpers */
524 int sptlrpc_user_desc_size(void);
525 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset);
526 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset);
528 /* bulk helpers (internal use only by policies) */
529 int bulk_sec_desc_size(__u32 csum_alg, int request, int read);
530 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset);
532 int bulk_csum_cli_request(struct ptlrpc_bulk_desc *desc, int read,
533 __u32 alg, struct lustre_msg *rmsg, int roff);
534 int bulk_csum_cli_reply(struct ptlrpc_bulk_desc *desc, int read,
535 struct lustre_msg *rmsg, int roff,
536 struct lustre_msg *vmsg, int voff);
537 int bulk_csum_svc(struct ptlrpc_bulk_desc *desc, int read,
538 struct lustre_msg *vmsg, int voff,
539 struct lustre_msg *rmsg, int roff);
542 #endif /* _LUSTRE_SEC_H_ */