1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Modified from NFSv4 project for Lustre
5 * Copyright 2004 - 2006, Cluster File Systems, Inc.
7 * Author: Eric Mei <ericm@clusterfs.com>
10 #ifndef __PTLRPC_GSS_GSS_INTERNAL_H_
11 #define __PTLRPC_GSS_GSS_INTERNAL_H_
13 #include <lustre_sec.h>
18 typedef struct netobj_s {
23 #define NETOBJ_EMPTY ((netobj_t) { 0 })
25 typedef struct rawobj_s {
30 #define RAWOBJ_EMPTY ((rawobj_t) { 0, NULL })
32 typedef struct rawobj_buf_s {
39 int rawobj_empty(rawobj_t *obj);
40 int rawobj_alloc(rawobj_t *obj, char *buf, int len);
41 void rawobj_free(rawobj_t *obj);
42 int rawobj_equal(rawobj_t *a, rawobj_t *b);
43 int rawobj_dup(rawobj_t *dest, rawobj_t *src);
44 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen);
45 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen);
46 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen);
47 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen);
48 int rawobj_extract_local_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen);
49 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj);
50 int rawobj_from_netobj_alloc(rawobj_t *obj, netobj_t *netobj);
52 int buffer_extract_bytes(const void **buf, __u32 *buflen,
53 void *res, __u32 reslen);
56 * several timeout values. client refresh upcall timeout we using
57 * default in pipefs implemnetation.
59 #define __TIMEOUT_DELTA (10)
61 #define GSS_SECINIT_RPC_TIMEOUT \
62 (obd_timeout < __TIMEOUT_DELTA ? \
63 __TIMEOUT_DELTA : obd_timeout - __TIMEOUT_DELTA)
65 #define GSS_SECFINI_RPC_TIMEOUT (__TIMEOUT_DELTA)
66 #define GSS_SECSVC_UPCALL_TIMEOUT (GSS_SECINIT_RPC_TIMEOUT)
71 #define GSS_GC_INTERVAL (60 * 60) /* 60 minutes */
74 unsigned long gss_round_ctx_expiry(unsigned long expiry,
75 unsigned long sec_flags)
77 if (sec_flags & PTLRPC_SEC_FL_REVERSE)
80 if (get_seconds() + __TIMEOUT_DELTA <= expiry)
81 return expiry - __TIMEOUT_DELTA;
87 * Max encryption element in block cipher algorithms.
89 #define GSS_MAX_CIPHER_BLOCK (16)
92 * XXX make it visible of kernel and lgssd/lsvcgssd
94 #define GSSD_INTERFACE_VERSION (1)
96 #define PTLRPC_GSS_VERSION (1)
99 enum ptlrpc_gss_proc {
100 PTLRPC_GSS_PROC_DATA = 0,
101 PTLRPC_GSS_PROC_INIT = 1,
102 PTLRPC_GSS_PROC_CONTINUE_INIT = 2,
103 PTLRPC_GSS_PROC_DESTROY = 3,
104 PTLRPC_GSS_PROC_ERR = 4,
107 enum ptlrpc_gss_tgt {
108 LUSTRE_GSS_TGT_MDS = 0,
109 LUSTRE_GSS_TGT_OSS = 1,
110 LUSTRE_GSS_TGT_MGS = 2,
113 enum ptlrpc_gss_header_flags {
114 LUSTRE_GSS_PACK_BULK = 1,
115 LUSTRE_GSS_PACK_USER = 2,
119 __u32 import_to_gss_svc(struct obd_import *imp)
121 const char *name = imp->imp_obd->obd_type->typ_name;
123 if (!strcmp(name, LUSTRE_MDC_NAME))
124 return LUSTRE_GSS_TGT_MDS;
125 if (!strcmp(name, LUSTRE_OSC_NAME))
126 return LUSTRE_GSS_TGT_OSS;
132 * following 3 header must have the same size and offset
135 __u8 gh_version; /* gss version */
136 __u8 gh_sp; /* sec part */
138 __u32 gh_flags; /* wrap flags */
139 __u32 gh_proc; /* proc */
140 __u32 gh_seq; /* sequence */
141 __u32 gh_svc; /* service */
145 netobj_t gh_handle; /* context handle */
148 struct gss_rep_header {
162 struct gss_err_header {
177 * part of wire context information send from client which be saved and
178 * used later by server.
180 struct gss_wire_ctx {
188 #define PTLRPC_GSS_MAX_HANDLE_SIZE (8)
189 #define PTLRPC_GSS_HEADER_SIZE (sizeof(struct gss_header) + \
190 PTLRPC_GSS_MAX_HANDLE_SIZE)
193 static inline __u64 gss_handle_to_u64(rawobj_t *handle)
195 if (handle->len != PTLRPC_GSS_MAX_HANDLE_SIZE)
197 return *((__u64 *) handle->data);
200 #define GSS_SEQ_WIN (2048)
201 #define GSS_SEQ_WIN_MAIN GSS_SEQ_WIN
202 #define GSS_SEQ_WIN_BACK (128)
203 #define GSS_SEQ_REPACK_THRESHOLD (GSS_SEQ_WIN_MAIN / 2 + \
204 GSS_SEQ_WIN_MAIN / 4)
206 struct gss_svc_seq_data {
209 * highest sequence number seen so far, for main and back window
214 * main and back window
215 * for i such that ssd_max - GSS_SEQ_WIN < i <= ssd_max, the i-th bit
216 * of ssd_win is nonzero iff sequence number i has been seen already.
218 unsigned long ssd_win_main[GSS_SEQ_WIN_MAIN/BITS_PER_LONG];
219 unsigned long ssd_win_back[GSS_SEQ_WIN_BACK/BITS_PER_LONG];
223 struct gss_ctx *gsc_mechctx;
224 struct gss_svc_seq_data gsc_seqdata;
225 rawobj_t gsc_rvs_hdl;
229 uid_t gsc_mapped_uid;
230 unsigned int gsc_usr_root:1,
236 struct gss_svc_reqctx {
237 struct ptlrpc_svc_ctx src_base;
241 struct gss_wire_ctx src_wirectx;
242 struct gss_svc_ctx *src_ctx;
244 * record place of bulk_sec_desc in request/reply buffer
246 struct ptlrpc_bulk_sec_desc *src_reqbsd;
248 struct ptlrpc_bulk_sec_desc *src_repbsd;
253 unsigned int src_init:1,
260 struct ptlrpc_cli_ctx gc_base;
266 struct gss_ctx *gc_mechctx;
267 /* handle for the buddy svc ctx */
268 rawobj_t gc_svc_handle;
271 struct gss_cli_ctx_keyring {
272 struct gss_cli_ctx gck_base;
274 struct timer_list *gck_timer;
278 struct ptlrpc_sec gs_base;
279 struct gss_api_mech *gs_mech;
284 struct gss_sec_pipefs {
285 struct gss_sec gsp_base;
286 int gsp_chash_size; /* must be 2^n */
287 struct hlist_head gsp_chash[0];
291 * FIXME cleanup the keyring upcall mutexes
293 #define HAVE_KEYRING_UPCALL_SERIALIZED 1
295 struct gss_sec_keyring {
296 struct gss_sec gsk_base;
298 * all contexts listed here. access is protected by sec spinlock.
300 struct hlist_head gsk_clist;
302 * specially point to root ctx (only one at a time). access is
303 * protected by sec spinlock.
305 struct ptlrpc_cli_ctx *gsk_root_ctx;
307 * specially serialize upcalls for root context.
309 struct mutex gsk_root_uc_lock;
311 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
312 struct mutex gsk_uc_lock; /* serialize upcalls */
316 static inline struct gss_cli_ctx *ctx2gctx(struct ptlrpc_cli_ctx *ctx)
318 return container_of(ctx, struct gss_cli_ctx, gc_base);
322 struct gss_cli_ctx_keyring *ctx2gctx_keyring(struct ptlrpc_cli_ctx *ctx)
324 return container_of(ctx2gctx(ctx),
325 struct gss_cli_ctx_keyring, gck_base);
328 static inline struct gss_sec *sec2gsec(struct ptlrpc_sec *sec)
330 return container_of(sec, struct gss_sec, gs_base);
333 static inline struct gss_sec_pipefs *sec2gsec_pipefs(struct ptlrpc_sec *sec)
335 return container_of(sec2gsec(sec), struct gss_sec_pipefs, gsp_base);
338 static inline struct gss_sec_keyring *sec2gsec_keyring(struct ptlrpc_sec *sec)
340 return container_of(sec2gsec(sec), struct gss_sec_keyring, gsk_base);
344 #define GSS_CTX_INIT_MAX_LEN (1024)
347 * This only guaranteed be enough for current krb5 des-cbc-crc . We might
348 * adjust this when new enc type or mech added in.
350 #define GSS_PRIVBUF_PREFIX_LEN (32)
351 #define GSS_PRIVBUF_SUFFIX_LEN (32)
354 struct gss_svc_reqctx *gss_svc_ctx2reqctx(struct ptlrpc_svc_ctx *ctx)
357 return container_of(ctx, struct gss_svc_reqctx, src_base);
361 struct gss_svc_ctx *gss_svc_ctx2gssctx(struct ptlrpc_svc_ctx *ctx)
364 return gss_svc_ctx2reqctx(ctx)->src_ctx;
368 int gss_cli_ctx_match(struct ptlrpc_cli_ctx *ctx, struct vfs_cred *vcred);
369 int gss_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize);
370 int gss_cli_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
371 int gss_cli_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
372 int gss_cli_ctx_seal(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
373 int gss_cli_ctx_unseal(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
375 int gss_sec_install_rctx(struct obd_import *imp, struct ptlrpc_sec *sec,
376 struct ptlrpc_cli_ctx *ctx);
377 int gss_alloc_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
379 void gss_free_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req);
380 int gss_alloc_repbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
382 void gss_free_repbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req);
383 int gss_enlarge_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
384 int segment, int newsize);
386 int gss_svc_accept(struct ptlrpc_sec_policy *policy,
387 struct ptlrpc_request *req);
388 void gss_svc_invalidate_ctx(struct ptlrpc_svc_ctx *svc_ctx);
389 int gss_svc_alloc_rs(struct ptlrpc_request *req, int msglen);
390 int gss_svc_authorize(struct ptlrpc_request *req);
391 void gss_svc_free_rs(struct ptlrpc_reply_state *rs);
392 void gss_svc_free_ctx(struct ptlrpc_svc_ctx *ctx);
394 int cli_ctx_expire(struct ptlrpc_cli_ctx *ctx);
395 int cli_ctx_check_death(struct ptlrpc_cli_ctx *ctx);
397 int gss_copy_rvc_cli_ctx(struct ptlrpc_cli_ctx *cli_ctx,
398 struct ptlrpc_svc_ctx *svc_ctx);
400 struct gss_header *gss_swab_header(struct lustre_msg *msg, int segment);
401 netobj_t *gss_swab_netobj(struct lustre_msg *msg, int segment);
403 void gss_cli_ctx_uptodate(struct gss_cli_ctx *gctx);
404 int gss_pack_err_notify(struct ptlrpc_request *req, __u32 major, __u32 minor);
405 int gss_check_seq_num(struct gss_svc_seq_data *sd, __u32 seq_num, int set);
407 int gss_sec_create_common(struct gss_sec *gsec,
408 struct ptlrpc_sec_policy *policy,
409 struct obd_import *imp,
410 struct ptlrpc_svc_ctx *ctx,
411 struct sptlrpc_flavor *sf);
412 void gss_sec_destroy_common(struct gss_sec *gsec);
413 void gss_sec_kill(struct ptlrpc_sec *sec);
415 int gss_cli_ctx_init_common(struct ptlrpc_sec *sec,
416 struct ptlrpc_cli_ctx *ctx,
417 struct ptlrpc_ctx_ops *ctxops,
418 struct vfs_cred *vcred);
419 int gss_cli_ctx_fini_common(struct ptlrpc_sec *sec,
420 struct ptlrpc_cli_ctx *ctx);
422 void gss_cli_ctx_flags2str(unsigned long flags, char *buf, int bufsize);
425 int __init gss_init_keyring(void);
426 void __exit gss_exit_keyring(void);
429 int __init gss_init_pipefs(void);
430 void __exit gss_exit_pipefs(void);
433 int gss_cli_ctx_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
434 struct ptlrpc_request *req,
435 struct ptlrpc_bulk_desc *desc);
436 int gss_cli_ctx_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
437 struct ptlrpc_request *req,
438 struct ptlrpc_bulk_desc *desc);
439 int gss_svc_unwrap_bulk(struct ptlrpc_request *req,
440 struct ptlrpc_bulk_desc *desc);
441 int gss_svc_wrap_bulk(struct ptlrpc_request *req,
442 struct ptlrpc_bulk_desc *desc);
444 /* gss_mech_switch.c */
445 int init_kerberos_module(void);
446 void cleanup_kerberos_module(void);
448 /* gss_generic_token.c */
449 int g_token_size(rawobj_t *mech, unsigned int body_size);
450 void g_make_token_header(rawobj_t *mech, int body_size, unsigned char **buf);
451 __u32 g_verify_token_header(rawobj_t *mech, int *body_size,
452 unsigned char **buf_in, int toksize);
455 /* gss_cli_upcall.c */
456 int gss_do_ctx_init_rpc(char *buffer, unsigned long count);
457 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx);
459 int __init gss_init_cli_upcall(void);
460 void __exit gss_exit_cli_upcall(void);
462 /* gss_svc_upcall.c */
463 __u64 gss_get_next_ctx_index(void);
464 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
465 struct gss_sec *gsec,
466 struct gss_cli_ctx *gctx);
467 int gss_svc_upcall_expire_rvs_ctx(rawobj_t *handle);
468 int gss_svc_upcall_dup_handle(rawobj_t *handle, struct gss_svc_ctx *ctx);
469 int gss_svc_upcall_update_sequence(rawobj_t *handle, __u32 seq);
470 int gss_svc_upcall_handle_init(struct ptlrpc_request *req,
471 struct gss_svc_reqctx *grctx,
472 struct gss_wire_ctx *gw,
473 struct obd_device *target,
477 struct gss_svc_ctx *gss_svc_upcall_get_ctx(struct ptlrpc_request *req,
478 struct gss_wire_ctx *gw);
479 void gss_svc_upcall_put_ctx(struct gss_svc_ctx *ctx);
480 void gss_svc_upcall_destroy_ctx(struct gss_svc_ctx *ctx);
482 int __init gss_init_svc_upcall(void);
483 void __exit gss_exit_svc_upcall(void);
486 void gss_stat_oos_record_cli(int behind);
487 void gss_stat_oos_record_svc(int phase, int replay);
489 int __init gss_init_lproc(void);
490 void __exit gss_exit_lproc(void);
492 /* gss_krb5_mech.c */
493 int __init init_kerberos_module(void);
494 void __exit cleanup_kerberos_module(void);
499 void __dbg_memdump(char *name, void *ptr, int size)
501 char *buf, *p = (char *) ptr;
502 int bufsize = size * 2 + 1, i;
504 OBD_ALLOC(buf, bufsize);
506 printk("DUMP ERROR: can't alloc %d bytes\n", bufsize);
510 for (i = 0; i < size; i++)
511 sprintf(&buf[i+i], "%02x", (__u8) p[i]);
512 buf[size + size] = '\0';
513 printk("DUMP %s@%p(%d): %s\n", name, ptr, size, buf);
514 OBD_FREE(buf, bufsize);
517 #endif /* __PTLRPC_GSS_GSS_INTERNAL_H_ */