2 * Modified from NFSv4 project for Lustre
4 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
6 * Copyright (c) 2012, 2016, Intel Corporation.
8 * Author: Eric Mei <ericm@clusterfs.com>
11 #ifndef __PTLRPC_GSS_GSS_INTERNAL_H_
12 #define __PTLRPC_GSS_GSS_INTERNAL_H_
14 #include <crypto/hash.h>
15 #include <libcfs/libcfs_crypto.h>
16 #include <lustre_sec.h>
21 typedef struct netobj_s {
26 #define NETOBJ_EMPTY ((netobj_t) { 0 })
28 typedef struct rawobj_s {
33 #define RAWOBJ_EMPTY ((rawobj_t) { 0, NULL })
35 typedef struct rawobj_buf_s {
42 int rawobj_empty(rawobj_t *obj);
43 int rawobj_alloc(rawobj_t *obj, char *buf, int len);
44 void rawobj_free(rawobj_t *obj);
45 int rawobj_equal(rawobj_t *a, rawobj_t *b);
46 int rawobj_dup(rawobj_t *dest, rawobj_t *src);
47 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen);
48 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen);
49 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen);
50 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen);
51 int rawobj_extract_local_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen);
52 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj);
53 int rawobj_from_netobj_alloc(rawobj_t *obj, netobj_t *netobj);
55 int buffer_extract_bytes(const void **buf, __u32 *buflen,
56 void *res, __u32 reslen);
59 * several timeout values. client refresh upcall timeout we using
60 * default in pipefs implemnetation.
62 #define __TIMEOUT_DELTA (10)
64 #define GSS_SECINIT_RPC_TIMEOUT \
65 (obd_timeout < __TIMEOUT_DELTA ? \
66 __TIMEOUT_DELTA : obd_timeout - __TIMEOUT_DELTA)
68 #define GSS_SECFINI_RPC_TIMEOUT (__TIMEOUT_DELTA)
69 #define GSS_SECSVC_UPCALL_TIMEOUT (GSS_SECINIT_RPC_TIMEOUT)
74 #define GSS_GC_INTERVAL (60 * 60) /* 60 minutes */
76 static inline time64_t gss_round_ctx_expiry(time64_t expiry,
77 unsigned long sec_flags)
79 if (sec_flags & PTLRPC_SEC_FL_REVERSE)
82 if (ktime_get_real_seconds() + __TIMEOUT_DELTA <= expiry)
83 return expiry - __TIMEOUT_DELTA;
89 * Max encryption element in block cipher algorithms.
91 #define GSS_MAX_CIPHER_BLOCK (16)
94 * XXX make it visible of kernel and lgssd/lsvcgssd
97 GSSD_INTERFACE_VERSION_V1 = 1,
98 GSSD_INTERFACE_VERSION_V2 = 2,
99 GSSD_INTERFACE_VERSION = GSSD_INTERFACE_VERSION_V2,
102 #define PTLRPC_GSS_VERSION (1)
105 enum ptlrpc_gss_proc {
106 PTLRPC_GSS_PROC_DATA = 0,
107 PTLRPC_GSS_PROC_INIT = 1,
108 PTLRPC_GSS_PROC_CONTINUE_INIT = 2,
109 PTLRPC_GSS_PROC_DESTROY = 3,
110 PTLRPC_GSS_PROC_ERR = 4,
113 enum ptlrpc_gss_tgt {
114 LUSTRE_GSS_TGT_MGS = 0,
115 LUSTRE_GSS_TGT_MDS = 1,
116 LUSTRE_GSS_TGT_OSS = 2,
119 enum ptlrpc_gss_header_flags {
120 LUSTRE_GSS_PACK_BULK = 1,
121 LUSTRE_GSS_PACK_USER = 2,
122 LUSTRE_GSS_PACK_KCSUM = 4,
126 __u32 import_to_gss_svc(struct obd_import *imp)
128 int cl_sp_to = LUSTRE_SP_ANY;
131 cl_sp_to = imp->imp_obd->u.cli.cl_sp_to;
135 return LUSTRE_GSS_TGT_MDS;
137 return LUSTRE_GSS_TGT_OSS;
140 return LUSTRE_GSS_TGT_MGS;
149 * following 3 header must have the same size and offset
152 __u8 gh_version; /* gss version */
153 __u8 gh_sp; /* sec part */
155 __u32 gh_flags; /* wrap flags */
156 __u32 gh_proc; /* proc */
157 __u32 gh_seq; /* sequence */
158 __u32 gh_svc; /* service */
162 netobj_t gh_handle; /* context handle */
165 struct gss_rep_header {
179 struct gss_err_header {
194 * part of wire context information send from client which be saved and
195 * used later by server.
197 struct gss_wire_ctx {
205 #define PTLRPC_GSS_MAX_HANDLE_SIZE (8)
206 #define PTLRPC_GSS_HEADER_SIZE (sizeof(struct gss_header) + \
207 PTLRPC_GSS_MAX_HANDLE_SIZE)
210 static inline __u64 gss_handle_to_u64(rawobj_t *handle)
212 if (handle->len != PTLRPC_GSS_MAX_HANDLE_SIZE)
214 return *((__u64 *) handle->data);
217 #define GSS_SEQ_WIN (2048)
218 #define GSS_SEQ_WIN_MAIN GSS_SEQ_WIN
219 #define GSS_SEQ_WIN_BACK (128)
220 #define GSS_SEQ_REPACK_THRESHOLD (GSS_SEQ_WIN_MAIN / 2 + \
221 GSS_SEQ_WIN_MAIN / 4)
223 struct gss_svc_seq_data {
226 * highest sequence number seen so far, for main and back window
231 * main and back window
232 * for i such that ssd_max - GSS_SEQ_WIN < i <= ssd_max, the i-th bit
233 * of ssd_win is nonzero iff sequence number i has been seen already.
235 unsigned long ssd_win_main[GSS_SEQ_WIN_MAIN/BITS_PER_LONG];
236 unsigned long ssd_win_back[GSS_SEQ_WIN_BACK/BITS_PER_LONG];
240 struct gss_ctx *gsc_mechctx;
241 struct gss_svc_seq_data gsc_seqdata;
242 rawobj_t gsc_rvs_hdl;
246 uid_t gsc_mapped_uid;
247 unsigned int gsc_usr_root:1,
254 struct gss_svc_reqctx {
255 struct ptlrpc_svc_ctx src_base;
259 struct gss_wire_ctx src_wirectx;
260 struct gss_svc_ctx *src_ctx;
262 * record place of bulk_sec_desc in request/reply buffer
264 struct ptlrpc_bulk_sec_desc *src_reqbsd;
266 struct ptlrpc_bulk_sec_desc *src_repbsd;
271 unsigned int src_init:1,
278 struct ptlrpc_cli_ctx gc_base;
284 struct gss_ctx *gc_mechctx;
285 /* handle for the buddy svc ctx */
286 rawobj_t gc_svc_handle;
289 struct gss_cli_ctx_keyring {
290 struct gss_cli_ctx gck_base;
292 struct timer_list gck_timer;
296 struct ptlrpc_sec gs_base;
297 struct gss_api_mech *gs_mech;
302 struct gss_sec_pipefs {
303 struct gss_sec gsp_base;
304 int gsp_chash_size; /* must be 2^n */
305 struct hlist_head gsp_chash[0];
309 * FIXME cleanup the keyring upcall mutexes
311 #define HAVE_KEYRING_UPCALL_SERIALIZED 1
313 struct gss_sec_keyring {
314 struct gss_sec gsk_base;
316 * all contexts listed here. access is protected by sec spinlock.
318 struct hlist_head gsk_clist;
320 * specially point to root ctx (only one at a time). access is
321 * protected by sec spinlock.
323 struct ptlrpc_cli_ctx *gsk_root_ctx;
325 * specially serialize upcalls for root context.
327 struct mutex gsk_root_uc_lock;
329 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
330 struct mutex gsk_uc_lock; /* serialize upcalls */
334 static inline struct gss_cli_ctx *ctx2gctx(struct ptlrpc_cli_ctx *ctx)
336 return container_of(ctx, struct gss_cli_ctx, gc_base);
340 struct gss_cli_ctx_keyring *ctx2gctx_keyring(struct ptlrpc_cli_ctx *ctx)
342 return container_of(ctx2gctx(ctx),
343 struct gss_cli_ctx_keyring, gck_base);
346 static inline struct gss_sec *sec2gsec(struct ptlrpc_sec *sec)
348 return container_of(sec, struct gss_sec, gs_base);
351 static inline struct gss_sec_pipefs *sec2gsec_pipefs(struct ptlrpc_sec *sec)
353 return container_of(sec2gsec(sec), struct gss_sec_pipefs, gsp_base);
356 static inline struct gss_sec_keyring *sec2gsec_keyring(struct ptlrpc_sec *sec)
358 return container_of(sec2gsec(sec), struct gss_sec_keyring, gsk_base);
361 #ifdef HAVE_CACHE_HASH_SPINLOCK
362 # define sunrpc_cache_lookup(c, i, h) sunrpc_cache_lookup_rcu((c), (i), (h))
363 # define cache_read_lock(cdetail) spin_lock(&((cdetail)->hash_lock))
364 # define cache_read_unlock(cdetail) spin_unlock(&((cdetail)->hash_lock))
365 #else /* ! HAVE_CACHE_HASH_SPINLOCK */
366 # define cache_read_lock(cdetail) read_lock(&((cdetail)->hash_lock))
367 # define cache_read_unlock(cdetail) read_unlock(&((cdetail)->hash_lock))
370 #define GSS_CTX_INIT_MAX_LEN (1024)
373 * This only guaranteed be enough for current krb5 des-cbc-crc . We might
374 * adjust this when new enc type or mech added in.
376 #define GSS_PRIVBUF_PREFIX_LEN (32)
377 #define GSS_PRIVBUF_SUFFIX_LEN (32)
380 struct gss_svc_reqctx *gss_svc_ctx2reqctx(struct ptlrpc_svc_ctx *ctx)
383 return container_of(ctx, struct gss_svc_reqctx, src_base);
387 struct gss_svc_ctx *gss_svc_ctx2gssctx(struct ptlrpc_svc_ctx *ctx)
390 return gss_svc_ctx2reqctx(ctx)->src_ctx;
394 int gss_cli_ctx_match(struct ptlrpc_cli_ctx *ctx, struct vfs_cred *vcred);
395 int gss_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize);
396 int gss_cli_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
397 int gss_cli_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
398 int gss_cli_ctx_seal(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
399 int gss_cli_ctx_unseal(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
401 int gss_sec_install_rctx(struct obd_import *imp, struct ptlrpc_sec *sec,
402 struct ptlrpc_cli_ctx *ctx);
403 int gss_alloc_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
405 void gss_free_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req);
406 int gss_alloc_repbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
408 void gss_free_repbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req);
409 int gss_enlarge_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
410 int segment, int newsize);
412 int gss_svc_accept(struct ptlrpc_sec_policy *policy,
413 struct ptlrpc_request *req);
414 void gss_svc_invalidate_ctx(struct ptlrpc_svc_ctx *svc_ctx);
415 int gss_svc_alloc_rs(struct ptlrpc_request *req, int msglen);
416 int gss_svc_authorize(struct ptlrpc_request *req);
417 void gss_svc_free_rs(struct ptlrpc_reply_state *rs);
418 void gss_svc_free_ctx(struct ptlrpc_svc_ctx *ctx);
420 int cli_ctx_expire(struct ptlrpc_cli_ctx *ctx);
421 int cli_ctx_check_death(struct ptlrpc_cli_ctx *ctx);
423 int gss_copy_rvc_cli_ctx(struct ptlrpc_cli_ctx *cli_ctx,
424 struct ptlrpc_svc_ctx *svc_ctx);
426 struct gss_header *gss_swab_header(struct lustre_msg *msg, int segment,
428 netobj_t *gss_swab_netobj(struct lustre_msg *msg, int segment);
430 void gss_cli_ctx_uptodate(struct gss_cli_ctx *gctx);
431 int gss_pack_err_notify(struct ptlrpc_request *req, __u32 major, __u32 minor);
432 int gss_check_seq_num(struct gss_svc_seq_data *sd, __u32 seq_num, int set);
434 int gss_sec_create_common(struct gss_sec *gsec,
435 struct ptlrpc_sec_policy *policy,
436 struct obd_import *imp,
437 struct ptlrpc_svc_ctx *ctx,
438 struct sptlrpc_flavor *sf);
439 void gss_sec_destroy_common(struct gss_sec *gsec);
440 void gss_sec_kill(struct ptlrpc_sec *sec);
442 int gss_cli_ctx_init_common(struct ptlrpc_sec *sec,
443 struct ptlrpc_cli_ctx *ctx,
444 struct ptlrpc_ctx_ops *ctxops,
445 struct vfs_cred *vcred);
446 int gss_cli_ctx_fini_common(struct ptlrpc_sec *sec,
447 struct ptlrpc_cli_ctx *ctx);
449 void gss_cli_ctx_flags2str(unsigned long flags, char *buf, int bufsize);
452 #ifndef HAVE_GSS_KEYRING
453 static inline int __init gss_init_keyring(void) { return 0; }
454 static inline void __exit gss_exit_keyring(void) { return; }
456 int __init gss_init_keyring(void);
457 void __exit gss_exit_keyring(void);
461 #ifndef HAVE_GSS_PIPEFS
462 static inline int __init gss_init_pipefs(void) { return 0; }
463 static inline void __exit gss_exit_pipefs(void) { return; }
465 int __init gss_init_pipefs(void);
466 void __exit gss_exit_pipefs(void);
470 int gss_cli_prep_bulk(struct ptlrpc_request *req,
471 struct ptlrpc_bulk_desc *desc);
472 int gss_cli_ctx_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
473 struct ptlrpc_request *req,
474 struct ptlrpc_bulk_desc *desc);
475 int gss_cli_ctx_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
476 struct ptlrpc_request *req,
477 struct ptlrpc_bulk_desc *desc);
478 int gss_svc_prep_bulk(struct ptlrpc_request *req,
479 struct ptlrpc_bulk_desc *desc);
480 int gss_svc_unwrap_bulk(struct ptlrpc_request *req,
481 struct ptlrpc_bulk_desc *desc);
482 int gss_svc_wrap_bulk(struct ptlrpc_request *req,
483 struct ptlrpc_bulk_desc *desc);
485 /* gss_generic_token.c */
486 int g_token_size(rawobj_t *mech, unsigned int body_size);
487 void g_make_token_header(rawobj_t *mech, int body_size, unsigned char **buf);
488 __u32 g_verify_token_header(rawobj_t *mech, int *body_size,
489 unsigned char **buf_in, int toksize);
492 /* gss_cli_upcall.c */
493 int gss_do_ctx_init_rpc(char __user *buffer, unsigned long count);
494 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx);
496 int __init gss_init_cli_upcall(void);
497 void gss_exit_cli_upcall(void);
499 /* gss_svc_upcall.c */
500 __u64 gss_get_next_ctx_index(void);
501 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
502 struct gss_sec *gsec,
503 struct gss_cli_ctx *gctx);
504 int gss_svc_upcall_expire_rvs_ctx(rawobj_t *handle);
505 int gss_svc_upcall_dup_handle(rawobj_t *handle, struct gss_svc_ctx *ctx);
506 int gss_svc_upcall_update_sequence(rawobj_t *handle, __u32 seq);
507 int gss_svc_upcall_handle_init(struct ptlrpc_request *req,
508 struct gss_svc_reqctx *grctx,
509 struct gss_wire_ctx *gw,
510 struct obd_device *target,
514 struct gss_svc_ctx *gss_svc_upcall_get_ctx(struct ptlrpc_request *req,
515 struct gss_wire_ctx *gw);
516 void gss_svc_upcall_put_ctx(struct gss_svc_ctx *ctx);
517 void gss_svc_upcall_destroy_ctx(struct gss_svc_ctx *ctx);
519 int __init gss_init_svc_upcall(void);
520 void gss_exit_svc_upcall(void);
521 extern unsigned int krb5_allow_old_client_csum;
524 void gss_stat_oos_record_cli(int behind);
525 void gss_stat_oos_record_svc(int phase, int replay);
527 int __init gss_init_tunables(void);
528 void gss_exit_tunables(void);
530 /* gss_null_mech.c */
531 int __init init_null_module(void);
532 void cleanup_null_module(void);
534 /* gss_krb5_mech.c */
535 int __init init_kerberos_module(void);
536 void cleanup_kerberos_module(void);
539 #ifdef HAVE_OPENSSL_SSK
540 int __init init_sk_module(void);
541 void cleanup_sk_module(void);
543 static inline int init_sk_module(void) { return 0; }
544 static inline void cleanup_sk_module(void) { return; }
545 #endif /* HAVE_OPENSSL_SSK */
549 void __dbg_memdump(char *name, void *ptr, int size)
551 char *buf, *p = (char *) ptr;
552 int bufsize = size * 2 + 1, i;
554 OBD_ALLOC(buf, bufsize);
556 CDEBUG(D_ERROR, "DUMP ERROR: can't alloc %d bytes\n", bufsize);
560 for (i = 0; i < size; i++)
561 sprintf(&buf[i+i], "%02x", (__u8) p[i]);
562 buf[size + size] = '\0';
563 LCONSOLE_INFO("DUMP %s@%p(%d): %s\n", name, ptr, size, buf);
564 OBD_FREE(buf, bufsize);
567 static inline unsigned int ll_read_key_usage(struct key *key)
569 #ifdef HAVE_KEY_USAGE_REFCOUNT
570 return refcount_read(&key->usage);
572 return atomic_read(&key->usage);
576 #endif /* __PTLRPC_GSS_GSS_INTERNAL_H_ */