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