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