Whamcloud - gitweb
branch: HEAD
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_internal.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Modified from NFSv4 project for Lustre
5  * Copyright 2004 - 2006, Cluster File Systems, Inc.
6  * All rights reserved
7  * Author: Eric Mei <ericm@clusterfs.com>
8  */
9
10 #ifndef __PTLRPC_GSS_GSS_INTERNAL_H_
11 #define __PTLRPC_GSS_GSS_INTERNAL_H_
12
13 #include <lustre_sec.h>
14
15 /*
16  * rawobj stuff
17  */
18 typedef struct netobj_s {
19         __u32           len;
20         __u8            data[0];
21 } netobj_t;
22
23 #define NETOBJ_EMPTY    ((netobj_t) { 0 })
24
25 typedef struct rawobj_s {
26         __u32           len;
27         __u8           *data;
28 } rawobj_t;
29
30 #define RAWOBJ_EMPTY    ((rawobj_t) { 0, NULL })
31
32 typedef struct rawobj_buf_s {
33         __u32           dataoff;
34         __u32           datalen;
35         __u32           buflen;
36         __u8           *buf;
37 } rawobj_buf_t;
38
39 int rawobj_alloc(rawobj_t *obj, char *buf, int len);
40 void rawobj_free(rawobj_t *obj);
41 int rawobj_equal(rawobj_t *a, rawobj_t *b);
42 int rawobj_dup(rawobj_t *dest, rawobj_t *src);
43 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen);
44 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen);
45 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen);
46 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen);
47 int rawobj_extract_local_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen);
48 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj);
49 int rawobj_from_netobj_alloc(rawobj_t *obj, netobj_t *netobj);
50
51 int buffer_extract_bytes(const void **buf, __u32 *buflen,
52                          void *res, __u32 reslen);
53
54 /*
55  * several timeout values. client refresh upcall timeout we using
56  * default in pipefs implemnetation.
57  */
58 #define __TIMEOUT_DELTA                 (10)
59
60 #define GSS_SECINIT_RPC_TIMEOUT                                         \
61         (obd_timeout < __TIMEOUT_DELTA ?                                \
62          __TIMEOUT_DELTA : obd_timeout - __TIMEOUT_DELTA)
63
64 #define GSS_SECFINI_RPC_TIMEOUT         (__TIMEOUT_DELTA)
65 #define GSS_SECSVC_UPCALL_TIMEOUT       (GSS_SECINIT_RPC_TIMEOUT)
66
67 /*
68  * default gc interval
69  */
70 #define GSS_GC_INTERVAL                 (60 * 60) /* 60 minutes */
71
72 static inline
73 unsigned long gss_round_ctx_expiry(unsigned long expiry,
74                                    unsigned long sec_flags)
75 {
76         if (sec_flags & PTLRPC_SEC_FL_REVERSE)
77                 return expiry;
78
79         if (get_seconds() + __TIMEOUT_DELTA <= expiry)
80                 return expiry - __TIMEOUT_DELTA;
81
82         return expiry;
83 }
84
85 /*
86  * Max encryption element in block cipher algorithms.
87  */
88 #define GSS_MAX_CIPHER_BLOCK               (16)
89
90 /*
91  * XXX make it visible of kernel and lgssd/lsvcgssd
92  */
93 #define GSSD_INTERFACE_VERSION          (1)
94
95 #define PTLRPC_GSS_VERSION              (1)
96
97
98 enum ptlrpc_gss_proc {
99         PTLRPC_GSS_PROC_DATA            = 0,
100         PTLRPC_GSS_PROC_INIT            = 1,
101         PTLRPC_GSS_PROC_CONTINUE_INIT   = 2,
102         PTLRPC_GSS_PROC_DESTROY         = 3,
103         PTLRPC_GSS_PROC_ERR             = 4,
104 };
105
106 enum ptlrpc_gss_tgt {
107         LUSTRE_GSS_TGT_MDS              = 0,
108         LUSTRE_GSS_TGT_OSS              = 1,
109         LUSTRE_GSS_TGT_MGS              = 2,
110 };
111
112 static inline
113 __u32 import_to_gss_svc(struct obd_import *imp)
114 {
115         const char *name = imp->imp_obd->obd_type->typ_name;
116
117         if (!strcmp(name, LUSTRE_MDC_NAME))
118                 return LUSTRE_GSS_TGT_MDS;
119         if (!strcmp(name, LUSTRE_OSC_NAME))
120                 return LUSTRE_GSS_TGT_OSS;
121         LBUG();
122         return 0;
123 }
124
125 /*
126  * following 3 header must have the same size and offset
127  */
128 struct gss_header {
129         __u32                   gh_version;     /* gss version */
130         __u32                   gh_flags;       /* wrap flags */
131         __u32                   gh_proc;        /* proc */
132         __u32                   gh_seq;         /* sequence */
133         __u32                   gh_svc;         /* service */
134         __u32                   gh_pad1;
135         __u32                   gh_pad2;
136         __u32                   gh_pad3;
137         netobj_t                gh_handle;      /* context handle */
138 };
139
140 struct gss_rep_header {
141         __u32                   gh_version;
142         __u32                   gh_flags;
143         __u32                   gh_proc;
144         __u32                   gh_major;
145         __u32                   gh_minor;
146         __u32                   gh_seqwin;
147         __u32                   gh_pad2;
148         __u32                   gh_pad3;
149         netobj_t                gh_handle;
150 };
151
152 struct gss_err_header {
153         __u32                   gh_version;
154         __u32                   gh_flags;
155         __u32                   gh_proc;
156         __u32                   gh_major;
157         __u32                   gh_minor;
158         __u32                   gh_pad1;
159         __u32                   gh_pad2;
160         __u32                   gh_pad3;
161         netobj_t                gh_handle;
162 };
163
164 /*
165  * part of wire context information send from client which be saved and
166  * used later by server.
167  */
168 struct gss_wire_ctx {
169         __u32                   gw_proc;
170         __u32                   gw_seq;
171         __u32                   gw_svc;
172         rawobj_t                gw_handle;
173 };
174
175 #define PTLRPC_GSS_MAX_HANDLE_SIZE      (8)
176 #define PTLRPC_GSS_HEADER_SIZE          (sizeof(struct gss_header) + \
177                                          PTLRPC_GSS_MAX_HANDLE_SIZE)
178
179
180 #define GSS_SEQ_WIN                     (2048)
181 #define GSS_SEQ_WIN_MAIN                GSS_SEQ_WIN
182 #define GSS_SEQ_WIN_BACK                (128)
183 #define GSS_SEQ_REPACK_THRESHOLD        (GSS_SEQ_WIN_MAIN / 2 + \
184                                          GSS_SEQ_WIN_MAIN / 4)
185
186 struct gss_svc_seq_data {
187         spinlock_t              ssd_lock;
188         /*
189          * highest sequence number seen so far, for main and back window
190          */
191         __u32                   ssd_max_main;
192         __u32                   ssd_max_back;
193         /*
194          * main and back window
195          * for i such that ssd_max - GSS_SEQ_WIN < i <= ssd_max, the i-th bit
196          * of ssd_win is nonzero iff sequence number i has been seen already.
197          */
198         unsigned long           ssd_win_main[GSS_SEQ_WIN_MAIN/BITS_PER_LONG];
199         unsigned long           ssd_win_back[GSS_SEQ_WIN_BACK/BITS_PER_LONG];
200 };
201
202 struct gss_svc_ctx {
203         unsigned int            gsc_usr_root:1,
204                                 gsc_usr_mds:1,
205                                 gsc_remote:1;
206         uid_t                   gsc_uid;
207         gid_t                   gsc_gid;
208         uid_t                   gsc_mapped_uid;
209         rawobj_t                gsc_rvs_hdl;
210         struct gss_svc_seq_data gsc_seqdata;
211         struct gss_ctx         *gsc_mechctx;
212 };
213
214 struct gss_svc_reqctx {
215         struct ptlrpc_svc_ctx           src_base;
216         /*
217          * context
218          */
219         struct gss_wire_ctx             src_wirectx;
220         struct gss_svc_ctx             *src_ctx;
221         /*
222          * record place of bulk_sec_desc in request/reply buffer
223          */
224         struct ptlrpc_bulk_sec_desc    *src_reqbsd;
225         int                             src_reqbsd_size;
226         struct ptlrpc_bulk_sec_desc    *src_repbsd;
227         int                             src_repbsd_size;
228         /*
229          * flags
230          */
231         unsigned int                    src_init:1,
232                                         src_init_continue:1,
233                                         src_err_notify:1;
234         int                             src_reserve_len;
235 };
236
237 struct gss_cli_ctx {
238         struct ptlrpc_cli_ctx   gc_base;
239         __u32                   gc_flavor;
240         __u32                   gc_proc;
241         __u32                   gc_win;
242         atomic_t                gc_seq;
243         rawobj_t                gc_handle;
244         struct gss_ctx         *gc_mechctx;
245 };
246
247 struct gss_cli_ctx_keyring {
248         struct gss_cli_ctx      gck_base;
249         struct key             *gck_key;
250         struct timer_list      *gck_timer;
251 };
252
253 struct gss_sec {
254         struct ptlrpc_sec       gs_base;
255         struct gss_api_mech    *gs_mech;
256         spinlock_t              gs_lock;
257         __u64                   gs_rvs_hdl;
258 };
259
260 struct gss_sec_pipefs {
261         struct gss_sec          gsp_base;
262         int                     gsp_chash_size;  /* must be 2^n */
263         struct hlist_head       gsp_chash[0];
264 };
265
266 /*
267  * FIXME cleanup the keyring upcall mutexes
268  */
269 #define HAVE_KEYRING_UPCALL_SERIALIZED  1
270
271 struct gss_sec_keyring {
272         struct gss_sec          gsk_base;
273         /*
274          * unique sec_id.
275          */
276         int                     gsk_id;
277         /*
278          * all contexts listed here. access is protected by sec spinlock.
279          */
280         struct hlist_head       gsk_clist;
281         /*
282          * specially point to root ctx (only one at a time). access is
283          * protected by sec spinlock.
284          */
285         struct ptlrpc_cli_ctx  *gsk_root_ctx;
286         /*
287          * specially serialize upcalls for root context.
288          */
289         struct mutex            gsk_root_uc_lock;
290
291 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
292         struct mutex            gsk_uc_lock;        /* serialize upcalls */
293 #endif
294 };
295
296 static inline struct gss_cli_ctx *ctx2gctx(struct ptlrpc_cli_ctx *ctx)
297 {
298         return container_of(ctx, struct gss_cli_ctx, gc_base);
299 }
300
301 static inline
302 struct gss_cli_ctx_keyring *ctx2gctx_keyring(struct ptlrpc_cli_ctx *ctx)
303 {
304         return container_of(ctx2gctx(ctx),
305                             struct gss_cli_ctx_keyring, gck_base);
306 }
307
308 static inline struct gss_sec *sec2gsec(struct ptlrpc_sec *sec)
309 {
310         return container_of(sec, struct gss_sec, gs_base);
311 }
312
313 static inline struct gss_sec_pipefs *sec2gsec_pipefs(struct ptlrpc_sec *sec)
314 {
315         return container_of(sec2gsec(sec), struct gss_sec_pipefs, gsp_base);
316 }
317
318 static inline struct gss_sec_keyring *sec2gsec_keyring(struct ptlrpc_sec *sec)
319 {
320         return container_of(sec2gsec(sec), struct gss_sec_keyring, gsk_base);
321 }
322
323
324 #define GSS_CTX_INIT_MAX_LEN            (1024)
325
326 /*
327  * This only guaranteed be enough for current krb5 des-cbc-crc . We might
328  * adjust this when new enc type or mech added in.
329  */
330 #define GSS_PRIVBUF_PREFIX_LEN         (32)
331 #define GSS_PRIVBUF_SUFFIX_LEN         (32)
332
333 static inline
334 struct gss_svc_reqctx *gss_svc_ctx2reqctx(struct ptlrpc_svc_ctx *ctx)
335 {
336         LASSERT(ctx);
337         return container_of(ctx, struct gss_svc_reqctx, src_base);
338 }
339
340 /* sec_gss.c */
341 int gss_cli_ctx_match(struct ptlrpc_cli_ctx *ctx, struct vfs_cred *vcred);
342 int gss_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize);
343 int gss_cli_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
344 int gss_cli_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
345 int gss_cli_ctx_seal(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
346 int gss_cli_ctx_unseal(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
347
348 int  gss_sec_install_rctx(struct obd_import *imp, struct ptlrpc_sec *sec,
349                           struct ptlrpc_cli_ctx *ctx);
350 int  gss_alloc_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
351                       int msgsize);
352 void gss_free_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req);
353 int  gss_alloc_repbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
354                       int msgsize);
355 void gss_free_repbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req);
356 int  gss_enlarge_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
357                         int segment, int newsize);
358
359 int  gss_svc_accept(struct ptlrpc_sec_policy *policy,
360                     struct ptlrpc_request *req);
361 void gss_svc_invalidate_ctx(struct ptlrpc_svc_ctx *svc_ctx);
362 int  gss_svc_alloc_rs(struct ptlrpc_request *req, int msglen);
363 int  gss_svc_authorize(struct ptlrpc_request *req);
364 void gss_svc_free_rs(struct ptlrpc_reply_state *rs);
365 void gss_svc_free_ctx(struct ptlrpc_svc_ctx *ctx);
366
367 int cli_ctx_expire(struct ptlrpc_cli_ctx *ctx);
368 int cli_ctx_check_death(struct ptlrpc_cli_ctx *ctx);
369
370 int gss_copy_rvc_cli_ctx(struct ptlrpc_cli_ctx *cli_ctx,
371                          struct ptlrpc_svc_ctx *svc_ctx);
372
373 struct gss_header *gss_swab_header(struct lustre_msg *msg, int segment);
374 netobj_t *gss_swab_netobj(struct lustre_msg *msg, int segment);
375
376 void gss_cli_ctx_uptodate(struct gss_cli_ctx *gctx);
377 int gss_pack_err_notify(struct ptlrpc_request *req, __u32 major, __u32 minor);
378 int gss_check_seq_num(struct gss_svc_seq_data *sd, __u32 seq_num, int set);
379
380 int gss_sec_create_common(struct gss_sec *gsec,
381                           struct ptlrpc_sec_policy *policy,
382                           struct obd_import *imp,
383                           struct ptlrpc_svc_ctx *ctx,
384                           __u32 flavor,
385                           unsigned long flags);
386 void gss_sec_destroy_common(struct gss_sec *gsec);
387
388 int gss_cli_ctx_init_common(struct ptlrpc_sec *sec,
389                             struct ptlrpc_cli_ctx *ctx,
390                             struct ptlrpc_ctx_ops *ctxops,
391                             struct vfs_cred *vcred);
392 int gss_cli_ctx_fini_common(struct ptlrpc_sec *sec,
393                             struct ptlrpc_cli_ctx *ctx);
394
395 void gss_cli_ctx_flags2str(unsigned long flags, char *buf, int bufsize);
396
397 /* gss_keyring.c */
398 extern struct ptlrpc_sec_policy gss_policy_keyring;
399 int  __init gss_init_keyring(void);
400 void __exit gss_exit_keyring(void);
401
402 /* gss_pipefs.c */
403 int  __init gss_init_pipefs(void);
404 void __exit gss_exit_pipefs(void);
405
406 /* gss_bulk.c */
407 int gss_cli_ctx_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
408                           struct ptlrpc_request *req,
409                           struct ptlrpc_bulk_desc *desc);
410 int gss_cli_ctx_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
411                             struct ptlrpc_request *req,
412                             struct ptlrpc_bulk_desc *desc);
413 int gss_svc_unwrap_bulk(struct ptlrpc_request *req,
414                         struct ptlrpc_bulk_desc *desc);
415 int gss_svc_wrap_bulk(struct ptlrpc_request *req,
416                       struct ptlrpc_bulk_desc *desc);
417
418 /* gss_mech_switch.c */
419 int init_kerberos_module(void);
420 void cleanup_kerberos_module(void);
421
422 /* gss_generic_token.c */
423 int g_token_size(rawobj_t *mech, unsigned int body_size);
424 void g_make_token_header(rawobj_t *mech, int body_size, unsigned char **buf);
425 __u32 g_verify_token_header(rawobj_t *mech, int *body_size,
426                             unsigned char **buf_in, int toksize);
427
428
429 /* gss_cli_upcall.c */
430 int gss_do_ctx_init_rpc(char *buffer, unsigned long count);
431 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx);
432
433 int  __init gss_init_cli_upcall(void);
434 void __exit gss_exit_cli_upcall(void);
435
436 /* gss_svc_upcall.c */
437 __u64 gss_get_next_ctx_index(void);
438 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
439                                    struct gss_sec *gsec,
440                                    struct gss_cli_ctx *gctx);
441 int gss_svc_upcall_handle_init(struct ptlrpc_request *req,
442                                struct gss_svc_reqctx *grctx,
443                                struct gss_wire_ctx *gw,
444                                struct obd_device *target,
445                                __u32 lustre_svc,
446                                rawobj_t *rvs_hdl,
447                                rawobj_t *in_token);
448 struct gss_svc_ctx *gss_svc_upcall_get_ctx(struct ptlrpc_request *req,
449                                            struct gss_wire_ctx *gw);
450 void gss_svc_upcall_put_ctx(struct gss_svc_ctx *ctx);
451 void gss_svc_upcall_destroy_ctx(struct gss_svc_ctx *ctx);
452
453 int  __init gss_init_svc_upcall(void);
454 void __exit gss_exit_svc_upcall(void);
455
456 /* lproc_gss.c */
457 void gss_stat_oos_record_cli(int behind);
458 void gss_stat_oos_record_svc(int phase, int replay);
459
460 int  __init gss_init_lproc(void);
461 void __exit gss_exit_lproc(void);
462
463 /* gss_krb5_mech.c */
464 int __init init_kerberos_module(void);
465 void __exit cleanup_kerberos_module(void);
466
467
468 /* debug */
469 static inline
470 void __dbg_memdump(char *name, void *ptr, int size)
471 {
472         char *buf, *p = (char *) ptr;
473         int bufsize = size * 2 + 1, i;
474
475         OBD_ALLOC(buf, bufsize);
476         if (!buf) {
477                 printk("DUMP ERROR: can't alloc %d bytes\n", bufsize);
478                 return;
479         }
480
481         for (i = 0; i < size; i++)
482                 sprintf(&buf[i+i], "%02x", (__u8) p[i]);
483         buf[size + size] = '\0';
484         printk("DUMP %s@%p(%d): %s\n", name, ptr, size, buf);
485         OBD_FREE(buf, bufsize);
486 }
487
488 #endif /* __PTLRPC_GSS_GSS_INTERNAL_H_ */