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