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)
210 struct ptlrpc_ctx_ops {
211 int (*match) (struct ptlrpc_cli_ctx *ctx,
212 struct vfs_cred *vcred);
213 int (*refresh) (struct ptlrpc_cli_ctx *ctx);
214 int (*display) (struct ptlrpc_cli_ctx *ctx,
215 char *buf, int bufsize);
219 int (*sign) (struct ptlrpc_cli_ctx *ctx,
220 struct ptlrpc_request *req);
221 int (*verify) (struct ptlrpc_cli_ctx *ctx,
222 struct ptlrpc_request *req);
223 int (*seal) (struct ptlrpc_cli_ctx *ctx,
224 struct ptlrpc_request *req);
225 int (*unseal) (struct ptlrpc_cli_ctx *ctx,
226 struct ptlrpc_request *req);
230 int (*wrap_bulk) (struct ptlrpc_cli_ctx *ctx,
231 struct ptlrpc_request *req,
232 struct ptlrpc_bulk_desc *desc);
233 int (*unwrap_bulk) (struct ptlrpc_cli_ctx *ctx,
234 struct ptlrpc_request *req,
235 struct ptlrpc_bulk_desc *desc);
238 #define PTLRPC_CTX_UPTODATE_BIT (0) /* uptodate */
239 #define PTLRPC_CTX_DEAD_BIT (1) /* mark expired gracefully */
240 #define PTLRPC_CTX_ERROR_BIT (2) /* fatal error (refresh, etc.) */
241 #define PTLRPC_CTX_HASHED_BIT (8) /* in hash table */
242 #define PTLRPC_CTX_ETERNAL_BIT (9) /* always valid */
244 #define PTLRPC_CTX_UPTODATE (1 << PTLRPC_CTX_UPTODATE_BIT)
245 #define PTLRPC_CTX_DEAD (1 << PTLRPC_CTX_DEAD_BIT)
246 #define PTLRPC_CTX_ERROR (1 << PTLRPC_CTX_ERROR_BIT)
247 #define PTLRPC_CTX_HASHED (1 << PTLRPC_CTX_HASHED_BIT)
248 #define PTLRPC_CTX_ETERNAL (1 << PTLRPC_CTX_ETERNAL_BIT)
250 #define PTLRPC_CTX_STATUS_MASK (PTLRPC_CTX_UPTODATE | \
254 struct ptlrpc_cli_ctx {
255 struct hlist_node cc_hash; /* linked into hash table */
256 atomic_t cc_refcount;
257 struct ptlrpc_sec *cc_sec;
258 struct ptlrpc_ctx_ops *cc_ops;
259 cfs_time_t cc_expire; /* in seconds */
260 unsigned long cc_flags;
261 struct vfs_cred cc_vcred;
263 struct list_head cc_req_list; /* waiting reqs linked here */
266 struct ptlrpc_sec_cops {
268 * ptlrpc_sec constructor/destructor
270 struct ptlrpc_sec * (*create_sec) (struct obd_import *imp,
271 struct ptlrpc_svc_ctx *ctx,
273 unsigned long flags);
274 void (*destroy_sec) (struct ptlrpc_sec *sec);
276 * search ctx for a certain user, if this function is missing,
277 * a generic function will be invoked by caller. implement this
278 * for any special need.
280 struct ptlrpc_cli_ctx * (*lookup_ctx) (struct ptlrpc_sec *sec,
281 struct vfs_cred *vcred);
283 * ptlrpc_cli_ctx constructor/destructor
285 struct ptlrpc_cli_ctx * (*create_ctx) (struct ptlrpc_sec *sec,
286 struct vfs_cred *vcred);
287 void (*destroy_ctx) (struct ptlrpc_sec *sec,
288 struct ptlrpc_cli_ctx *ctx);
289 /* reverse service */
290 int (*install_rctx)(struct obd_import *imp,
291 struct ptlrpc_sec *sec,
292 struct ptlrpc_cli_ctx *ctx);
294 * request/reply buffer manipulation
296 int (*alloc_reqbuf)(struct ptlrpc_sec *sec,
297 struct ptlrpc_request *req,
298 int lustre_msg_size);
299 void (*free_reqbuf) (struct ptlrpc_sec *sec,
300 struct ptlrpc_request *req);
301 int (*alloc_repbuf)(struct ptlrpc_sec *sec,
302 struct ptlrpc_request *req,
303 int lustre_msg_size);
304 void (*free_repbuf) (struct ptlrpc_sec *sec,
305 struct ptlrpc_request *req);
306 int (*enlarge_reqbuf)
307 (struct ptlrpc_sec *sec,
308 struct ptlrpc_request *req,
309 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 */
341 #define PTLRPC_SEC_FL_PAG 0x0008 /* PAG enabled */
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;
365 #ifdef HAVE_LINUX_PAG
366 #define CURRENT_PAG ((uint64_t) current->pag)
368 #define CURRENT_PAG ((uint64_t) current->uid)
372 * user identity descriptor
374 #define LUSTRE_MAX_GROUPS (128)
376 struct ptlrpc_user_desc {
389 enum bulk_checksum_alg {
390 BULK_CSUM_ALG_NULL = 0,
394 BULK_CSUM_ALG_SHA256,
395 BULK_CSUM_ALG_SHA384,
396 BULK_CSUM_ALG_SHA512,
400 enum bulk_encrypt_alg {
401 BULK_PRIV_ALG_NULL = 0,
406 struct ptlrpc_bulk_sec_desc {
409 __u32 bsd_csum_alg; /* checksum algorithm */
410 __u32 bsd_priv_alg; /* encrypt algorithm */
411 __u8 bsd_iv[16]; /* encrypt iv */
415 const char * sptlrpc_bulk_csum_alg2name(__u32 csum_alg);
416 const char * sptlrpc_bulk_priv_alg2name(__u32 priv_alg);
421 struct proc_dir_entry;
422 extern struct proc_dir_entry *sptlrpc_proc_root;
425 * round size up to next power of 2, for slab allocation.
426 * @size must be sane (can't overflow after round up)
428 static inline int size_roundup_power2(int size)
441 * internal support libraries
443 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
444 int segment, int newsize);
449 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy);
450 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy);
452 __u32 sptlrpc_name2flavor(const char *name);
453 char *sptlrpc_flavor2name(__u32 flavor);
456 struct ptlrpc_sec_policy *sptlrpc_policy_get(struct ptlrpc_sec_policy *policy)
458 __module_get(policy->sp_owner);
463 void sptlrpc_policy_put(struct ptlrpc_sec_policy *policy)
465 module_put(policy->sp_owner);
471 struct ptlrpc_cli_ctx *sptlrpc_ctx_get(struct ptlrpc_cli_ctx *ctx);
472 void sptlrpc_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync);
473 void sptlrpc_ctx_expire(struct ptlrpc_cli_ctx *ctx);
474 void sptlrpc_ctx_replace(struct ptlrpc_sec *sec, struct ptlrpc_cli_ctx *new);
475 void sptlrpc_ctx_wakeup(struct ptlrpc_cli_ctx *ctx);
476 int sptlrpc_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize);
479 * client wrap/buffers
481 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req);
482 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req);
483 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize);
484 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req);
485 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize);
486 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req);
487 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
488 int segment, int newsize);
489 void sptlrpc_request_out_callback(struct ptlrpc_request *req);
492 * higher interface of import & request
494 int sptlrpc_import_get_sec(struct obd_import *imp, struct ptlrpc_svc_ctx *svc_ctx,
495 __u32 flavor, unsigned long flags);
496 void sptlrpc_import_put_sec(struct obd_import *imp);
497 int sptlrpc_import_check_ctx(struct obd_import *imp);
498 void sptlrpc_import_flush_root_ctx(struct obd_import *imp);
499 void sptlrpc_import_flush_my_ctx(struct obd_import *imp);
500 void sptlrpc_import_flush_all_ctx(struct obd_import *imp);
501 int sptlrpc_req_get_ctx(struct ptlrpc_request *req);
502 void sptlrpc_req_put_ctx(struct ptlrpc_request *req);
503 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout);
504 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode);
506 int sptlrpc_parse_flavor(enum lustre_part from, enum lustre_part to,
507 char *str, struct sec_flavor_config *conf);
509 const char * sec2target_str(struct ptlrpc_sec *sec);
510 int sptlrpc_lprocfs_rd(char *page, char **start, off_t off, int count,
511 int *eof, void *data);
516 enum secsvc_accept_res {
522 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req);
523 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen);
524 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req);
525 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs);
526 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req);
527 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req);
532 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
533 struct ptlrpc_svc_ctx *ctx);
534 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
535 struct ptlrpc_cli_ctx *ctx);
537 /* bulk security api */
538 int sptlrpc_enc_pool_add_user(void);
539 int sptlrpc_enc_pool_del_user(void);
540 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc);
541 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc);
543 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
544 struct ptlrpc_bulk_desc *desc);
545 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
546 int nob, obd_count pg_count,
547 struct brw_page **pga);
548 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
549 struct ptlrpc_bulk_desc *desc);
550 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
551 struct ptlrpc_bulk_desc *desc);
552 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
553 struct ptlrpc_bulk_desc *desc);
555 /* user descriptor helpers */
556 int sptlrpc_user_desc_size(void);
557 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset);
558 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset);
560 /* bulk helpers (internal use only by policies) */
561 int bulk_sec_desc_size(__u32 csum_alg, int request, int read);
562 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset);
564 int bulk_csum_cli_request(struct ptlrpc_bulk_desc *desc, int read,
565 __u32 alg, struct lustre_msg *rmsg, int roff);
566 int bulk_csum_cli_reply(struct ptlrpc_bulk_desc *desc, int read,
567 struct lustre_msg *rmsg, int roff,
568 struct lustre_msg *vmsg, int voff);
569 int bulk_csum_svc(struct ptlrpc_bulk_desc *desc, int read,
570 struct lustre_msg *vmsg, int voff,
571 struct lustre_msg *rmsg, int roff);
574 #endif /* _LUSTRE_SEC_H_ */