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