2 * Modified from NFSv4 project for Lustre
4 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
6 * Copyright (c) 2012, 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 <lustre_sec.h>
19 typedef struct netobj_s {
24 #define NETOBJ_EMPTY ((netobj_t) { 0 })
26 typedef struct rawobj_s {
31 #define RAWOBJ_EMPTY ((rawobj_t) { 0, NULL })
33 typedef struct rawobj_buf_s {
40 int rawobj_empty(rawobj_t *obj);
41 int rawobj_alloc(rawobj_t *obj, char *buf, int len);
42 void rawobj_free(rawobj_t *obj);
43 int rawobj_equal(rawobj_t *a, rawobj_t *b);
44 int rawobj_dup(rawobj_t *dest, rawobj_t *src);
45 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen);
46 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen);
47 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen);
48 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen);
49 int rawobj_extract_local_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen);
50 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj);
51 int rawobj_from_netobj_alloc(rawobj_t *obj, netobj_t *netobj);
53 int buffer_extract_bytes(const void **buf, __u32 *buflen,
54 void *res, __u32 reslen);
57 * several timeout values. client refresh upcall timeout we using
58 * default in pipefs implemnetation.
60 #define __TIMEOUT_DELTA (10)
62 #define GSS_SECINIT_RPC_TIMEOUT \
63 (obd_timeout < __TIMEOUT_DELTA ? \
64 __TIMEOUT_DELTA : obd_timeout - __TIMEOUT_DELTA)
66 #define GSS_SECFINI_RPC_TIMEOUT (__TIMEOUT_DELTA)
67 #define GSS_SECSVC_UPCALL_TIMEOUT (GSS_SECINIT_RPC_TIMEOUT)
72 #define GSS_GC_INTERVAL (60 * 60) /* 60 minutes */
75 unsigned long gss_round_ctx_expiry(unsigned long expiry,
76 unsigned long sec_flags)
78 if (sec_flags & PTLRPC_SEC_FL_REVERSE)
81 if (get_seconds() + __TIMEOUT_DELTA <= expiry)
82 return expiry - __TIMEOUT_DELTA;
88 * Max encryption element in block cipher algorithms.
90 #define GSS_MAX_CIPHER_BLOCK (16)
93 * XXX make it visible of kernel and lgssd/lsvcgssd
95 #define GSSD_INTERFACE_VERSION (1)
97 #define PTLRPC_GSS_VERSION (1)
100 enum ptlrpc_gss_proc {
101 PTLRPC_GSS_PROC_DATA = 0,
102 PTLRPC_GSS_PROC_INIT = 1,
103 PTLRPC_GSS_PROC_CONTINUE_INIT = 2,
104 PTLRPC_GSS_PROC_DESTROY = 3,
105 PTLRPC_GSS_PROC_ERR = 4,
108 enum ptlrpc_gss_tgt {
109 LUSTRE_GSS_TGT_MGS = 0,
110 LUSTRE_GSS_TGT_MDS = 1,
111 LUSTRE_GSS_TGT_OSS = 2,
114 enum ptlrpc_gss_header_flags {
115 LUSTRE_GSS_PACK_BULK = 1,
116 LUSTRE_GSS_PACK_USER = 2,
120 __u32 import_to_gss_svc(struct obd_import *imp)
122 const char *name = imp->imp_obd->obd_type->typ_name;
124 if (!strcmp(name, LUSTRE_MGC_NAME))
125 return LUSTRE_GSS_TGT_MGS;
126 if (!strcmp(name, LUSTRE_MDC_NAME) ||
127 !strcmp(name, LUSTRE_LWP_NAME))
128 return LUSTRE_GSS_TGT_MDS;
129 if (!strcmp(name, LUSTRE_OSC_NAME) ||
130 !strcmp(name, LUSTRE_OSP_NAME))
131 return LUSTRE_GSS_TGT_OSS;
137 * following 3 header must have the same size and offset
140 __u8 gh_version; /* gss version */
141 __u8 gh_sp; /* sec part */
143 __u32 gh_flags; /* wrap flags */
144 __u32 gh_proc; /* proc */
145 __u32 gh_seq; /* sequence */
146 __u32 gh_svc; /* service */
150 netobj_t gh_handle; /* context handle */
153 struct gss_rep_header {
167 struct gss_err_header {
182 * part of wire context information send from client which be saved and
183 * used later by server.
185 struct gss_wire_ctx {
193 #define PTLRPC_GSS_MAX_HANDLE_SIZE (8)
194 #define PTLRPC_GSS_HEADER_SIZE (sizeof(struct gss_header) + \
195 PTLRPC_GSS_MAX_HANDLE_SIZE)
198 static inline __u64 gss_handle_to_u64(rawobj_t *handle)
200 if (handle->len != PTLRPC_GSS_MAX_HANDLE_SIZE)
202 return *((__u64 *) handle->data);
205 #define GSS_SEQ_WIN (2048)
206 #define GSS_SEQ_WIN_MAIN GSS_SEQ_WIN
207 #define GSS_SEQ_WIN_BACK (128)
208 #define GSS_SEQ_REPACK_THRESHOLD (GSS_SEQ_WIN_MAIN / 2 + \
209 GSS_SEQ_WIN_MAIN / 4)
211 struct gss_svc_seq_data {
214 * highest sequence number seen so far, for main and back window
219 * main and back window
220 * for i such that ssd_max - GSS_SEQ_WIN < i <= ssd_max, the i-th bit
221 * of ssd_win is nonzero iff sequence number i has been seen already.
223 unsigned long ssd_win_main[GSS_SEQ_WIN_MAIN/BITS_PER_LONG];
224 unsigned long ssd_win_back[GSS_SEQ_WIN_BACK/BITS_PER_LONG];
228 struct gss_ctx *gsc_mechctx;
229 struct gss_svc_seq_data gsc_seqdata;
230 rawobj_t gsc_rvs_hdl;
234 uid_t gsc_mapped_uid;
235 unsigned int gsc_usr_root:1,
242 struct gss_svc_reqctx {
243 struct ptlrpc_svc_ctx src_base;
247 struct gss_wire_ctx src_wirectx;
248 struct gss_svc_ctx *src_ctx;
250 * record place of bulk_sec_desc in request/reply buffer
252 struct ptlrpc_bulk_sec_desc *src_reqbsd;
254 struct ptlrpc_bulk_sec_desc *src_repbsd;
259 unsigned int src_init:1,
266 struct ptlrpc_cli_ctx gc_base;
272 struct gss_ctx *gc_mechctx;
273 /* handle for the buddy svc ctx */
274 rawobj_t gc_svc_handle;
277 struct gss_cli_ctx_keyring {
278 struct gss_cli_ctx gck_base;
280 struct timer_list *gck_timer;
284 struct ptlrpc_sec gs_base;
285 struct gss_api_mech *gs_mech;
290 struct gss_sec_pipefs {
291 struct gss_sec gsp_base;
292 int gsp_chash_size; /* must be 2^n */
293 struct hlist_head gsp_chash[0];
297 * FIXME cleanup the keyring upcall mutexes
299 #define HAVE_KEYRING_UPCALL_SERIALIZED 1
301 struct gss_sec_keyring {
302 struct gss_sec gsk_base;
304 * all contexts listed here. access is protected by sec spinlock.
306 struct hlist_head gsk_clist;
308 * specially point to root ctx (only one at a time). access is
309 * protected by sec spinlock.
311 struct ptlrpc_cli_ctx *gsk_root_ctx;
313 * specially serialize upcalls for root context.
315 struct mutex gsk_root_uc_lock;
317 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
318 struct mutex gsk_uc_lock; /* serialize upcalls */
322 static inline struct gss_cli_ctx *ctx2gctx(struct ptlrpc_cli_ctx *ctx)
324 return container_of(ctx, struct gss_cli_ctx, gc_base);
328 struct gss_cli_ctx_keyring *ctx2gctx_keyring(struct ptlrpc_cli_ctx *ctx)
330 return container_of(ctx2gctx(ctx),
331 struct gss_cli_ctx_keyring, gck_base);
334 static inline struct gss_sec *sec2gsec(struct ptlrpc_sec *sec)
336 return container_of(sec, struct gss_sec, gs_base);
339 static inline struct gss_sec_pipefs *sec2gsec_pipefs(struct ptlrpc_sec *sec)
341 return container_of(sec2gsec(sec), struct gss_sec_pipefs, gsp_base);
344 static inline struct gss_sec_keyring *sec2gsec_keyring(struct ptlrpc_sec *sec)
346 return container_of(sec2gsec(sec), struct gss_sec_keyring, gsk_base);
350 #define GSS_CTX_INIT_MAX_LEN (1024)
353 * This only guaranteed be enough for current krb5 des-cbc-crc . We might
354 * adjust this when new enc type or mech added in.
356 #define GSS_PRIVBUF_PREFIX_LEN (32)
357 #define GSS_PRIVBUF_SUFFIX_LEN (32)
360 struct gss_svc_reqctx *gss_svc_ctx2reqctx(struct ptlrpc_svc_ctx *ctx)
363 return container_of(ctx, struct gss_svc_reqctx, src_base);
367 struct gss_svc_ctx *gss_svc_ctx2gssctx(struct ptlrpc_svc_ctx *ctx)
370 return gss_svc_ctx2reqctx(ctx)->src_ctx;
374 int gss_cli_ctx_match(struct ptlrpc_cli_ctx *ctx, struct vfs_cred *vcred);
375 int gss_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize);
376 int gss_cli_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
377 int gss_cli_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
378 int gss_cli_ctx_seal(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
379 int gss_cli_ctx_unseal(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
381 int gss_sec_install_rctx(struct obd_import *imp, struct ptlrpc_sec *sec,
382 struct ptlrpc_cli_ctx *ctx);
383 int gss_alloc_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
385 void gss_free_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req);
386 int gss_alloc_repbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
388 void gss_free_repbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req);
389 int gss_enlarge_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
390 int segment, int newsize);
392 int gss_svc_accept(struct ptlrpc_sec_policy *policy,
393 struct ptlrpc_request *req);
394 void gss_svc_invalidate_ctx(struct ptlrpc_svc_ctx *svc_ctx);
395 int gss_svc_alloc_rs(struct ptlrpc_request *req, int msglen);
396 int gss_svc_authorize(struct ptlrpc_request *req);
397 void gss_svc_free_rs(struct ptlrpc_reply_state *rs);
398 void gss_svc_free_ctx(struct ptlrpc_svc_ctx *ctx);
400 int cli_ctx_expire(struct ptlrpc_cli_ctx *ctx);
401 int cli_ctx_check_death(struct ptlrpc_cli_ctx *ctx);
403 int gss_copy_rvc_cli_ctx(struct ptlrpc_cli_ctx *cli_ctx,
404 struct ptlrpc_svc_ctx *svc_ctx);
406 struct gss_header *gss_swab_header(struct lustre_msg *msg, int segment,
408 netobj_t *gss_swab_netobj(struct lustre_msg *msg, int segment);
410 void gss_cli_ctx_uptodate(struct gss_cli_ctx *gctx);
411 int gss_pack_err_notify(struct ptlrpc_request *req, __u32 major, __u32 minor);
412 int gss_check_seq_num(struct gss_svc_seq_data *sd, __u32 seq_num, int set);
414 int gss_sec_create_common(struct gss_sec *gsec,
415 struct ptlrpc_sec_policy *policy,
416 struct obd_import *imp,
417 struct ptlrpc_svc_ctx *ctx,
418 struct sptlrpc_flavor *sf);
419 void gss_sec_destroy_common(struct gss_sec *gsec);
420 void gss_sec_kill(struct ptlrpc_sec *sec);
422 int gss_cli_ctx_init_common(struct ptlrpc_sec *sec,
423 struct ptlrpc_cli_ctx *ctx,
424 struct ptlrpc_ctx_ops *ctxops,
425 struct vfs_cred *vcred);
426 int gss_cli_ctx_fini_common(struct ptlrpc_sec *sec,
427 struct ptlrpc_cli_ctx *ctx);
429 void gss_cli_ctx_flags2str(unsigned long flags, char *buf, int bufsize);
432 #ifndef HAVE_GSS_KEYRING
433 static inline int __init gss_init_keyring(void) { return 0; }
434 static inline void __exit gss_exit_keyring(void) { return; }
436 int __init gss_init_keyring(void);
437 void __exit gss_exit_keyring(void);
441 #ifndef HAVE_GSS_PIPEFS
442 static inline int __init gss_init_pipefs(void) { return 0; }
443 static inline void __exit gss_exit_pipefs(void) { return; }
445 int __init gss_init_pipefs(void);
446 void __exit gss_exit_pipefs(void);
450 int gss_cli_prep_bulk(struct ptlrpc_request *req,
451 struct ptlrpc_bulk_desc *desc);
452 int gss_cli_ctx_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
453 struct ptlrpc_request *req,
454 struct ptlrpc_bulk_desc *desc);
455 int gss_cli_ctx_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
456 struct ptlrpc_request *req,
457 struct ptlrpc_bulk_desc *desc);
458 int gss_svc_prep_bulk(struct ptlrpc_request *req,
459 struct ptlrpc_bulk_desc *desc);
460 int gss_svc_unwrap_bulk(struct ptlrpc_request *req,
461 struct ptlrpc_bulk_desc *desc);
462 int gss_svc_wrap_bulk(struct ptlrpc_request *req,
463 struct ptlrpc_bulk_desc *desc);
465 /* gss_generic_token.c */
466 int g_token_size(rawobj_t *mech, unsigned int body_size);
467 void g_make_token_header(rawobj_t *mech, int body_size, unsigned char **buf);
468 __u32 g_verify_token_header(rawobj_t *mech, int *body_size,
469 unsigned char **buf_in, int toksize);
472 /* gss_cli_upcall.c */
473 int gss_do_ctx_init_rpc(char *buffer, unsigned long count);
474 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx);
476 int __init gss_init_cli_upcall(void);
477 void gss_exit_cli_upcall(void);
479 /* gss_svc_upcall.c */
480 __u64 gss_get_next_ctx_index(void);
481 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
482 struct gss_sec *gsec,
483 struct gss_cli_ctx *gctx);
484 int gss_svc_upcall_expire_rvs_ctx(rawobj_t *handle);
485 int gss_svc_upcall_dup_handle(rawobj_t *handle, struct gss_svc_ctx *ctx);
486 int gss_svc_upcall_update_sequence(rawobj_t *handle, __u32 seq);
487 int gss_svc_upcall_handle_init(struct ptlrpc_request *req,
488 struct gss_svc_reqctx *grctx,
489 struct gss_wire_ctx *gw,
490 struct obd_device *target,
494 struct gss_svc_ctx *gss_svc_upcall_get_ctx(struct ptlrpc_request *req,
495 struct gss_wire_ctx *gw);
496 void gss_svc_upcall_put_ctx(struct gss_svc_ctx *ctx);
497 void gss_svc_upcall_destroy_ctx(struct gss_svc_ctx *ctx);
499 int __init gss_init_svc_upcall(void);
500 void gss_exit_svc_upcall(void);
503 void gss_stat_oos_record_cli(int behind);
504 void gss_stat_oos_record_svc(int phase, int replay);
506 int __init gss_init_lproc(void);
507 void gss_exit_lproc(void);
509 /* gss_null_mech.c */
510 int __init init_null_module(void);
511 void cleanup_null_module(void);
513 /* gss_krb5_mech.c */
514 int __init init_kerberos_module(void);
515 void cleanup_kerberos_module(void);
518 int __init init_sk_module(void);
519 void cleanup_sk_module(void);
523 void __dbg_memdump(char *name, void *ptr, int size)
525 char *buf, *p = (char *) ptr;
526 int bufsize = size * 2 + 1, i;
528 OBD_ALLOC(buf, bufsize);
530 CDEBUG(D_ERROR, "DUMP ERROR: can't alloc %d bytes\n", bufsize);
534 for (i = 0; i < size; i++)
535 sprintf(&buf[i+i], "%02x", (__u8) p[i]);
536 buf[size + size] = '\0';
537 LCONSOLE_INFO("DUMP %s@%p(%d): %s\n", name, ptr, size, buf);
538 OBD_FREE(buf, bufsize);
541 #endif /* __PTLRPC_GSS_GSS_INTERNAL_H_ */