4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/obdfilter/filter_capa.c
36 * Author: Lai Siyao <lsy@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_FILTER
42 #include <linux/version.h>
43 #include <asm/uaccess.h>
44 #include <linux/file.h>
45 #include <linux/kmod.h>
47 #include <lustre_fsfilt.h>
48 #include <lustre_capa.h>
50 #include "filter_internal.h"
52 static inline __u32 filter_ck_keyid(struct filter_capa_key *key)
54 return key->k_key.lk_keyid;
57 int filter_update_capa_key(struct obd_device *obd, struct lustre_capa_key *new)
59 struct filter_obd *filter = &obd->u.filter;
60 struct filter_capa_key *k, *keys[2] = { NULL, NULL };
63 cfs_spin_lock(&capa_lock);
64 cfs_list_for_each_entry(k, &filter->fo_capa_keys, k_list) {
65 if (k->k_key.lk_seq != new->lk_seq)
70 if (filter_ck_keyid(keys[1]) > filter_ck_keyid(keys[0]))
71 keys[1] = keys[0], keys[0] = k;
76 cfs_spin_unlock(&capa_lock);
78 for (i = 0; i < 2; i++) {
81 if (filter_ck_keyid(keys[i]) != new->lk_keyid)
83 /* maybe because of recovery or other reasons, MDS sent the
84 * the old capability key again.
86 cfs_spin_lock(&capa_lock);
87 keys[i]->k_key = *new;
88 cfs_spin_unlock(&capa_lock);
94 /* if OSS already have two keys, update the old one */
100 CFS_INIT_LIST_HEAD(&k->k_list);
103 cfs_spin_lock(&capa_lock);
105 if (cfs_list_empty(&k->k_list))
106 cfs_list_add(&k->k_list, &filter->fo_capa_keys);
107 cfs_spin_unlock(&capa_lock);
109 DEBUG_CAPA_KEY(D_SEC, new, "new");
113 int filter_auth_capa(struct obd_export *exp, struct lu_fid *fid, obd_seq seq,
114 struct lustre_capa *capa, __u64 opc)
116 struct obd_device *obd = exp->exp_obd;
117 struct filter_obd *filter = &obd->u.filter;
118 struct filter_capa_key *k;
119 struct lustre_capa_key key;
122 int keys_ready = 0, key_found = 0, rc = 0;
125 /* skip capa check for llog and obdecho */
126 if (!fid_seq_is_mdt(seq))
129 /* capability is disabled */
130 if (!filter->fo_fl_oss_capa)
133 if (!(exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
138 CERROR("seq/fid/opc "LPU64"/"DFID"/"LPX64
139 ": no capability has been passed\n",
140 seq, PFID(fid), opc);
142 CERROR("seq/opc "LPU64"/"LPX64
143 ": no capability has been passed\n",
148 if (opc == CAPA_OPC_OSS_READ) {
149 if (!(capa->lc_opc & CAPA_OPC_OSS_RW))
151 } else if (!capa_opc_supported(capa, opc)) {
155 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
159 oc = capa_lookup(filter->fo_capa_hash, capa, 0);
161 cfs_spin_lock(&oc->c_lock);
162 if (capa_is_expired(oc)) {
163 DEBUG_CAPA(D_ERROR, capa, "expired");
166 cfs_spin_unlock(&oc->c_lock);
172 if (capa_is_expired_sec(capa)) {
173 DEBUG_CAPA(D_ERROR, capa, "expired");
177 cfs_spin_lock(&capa_lock);
178 cfs_list_for_each_entry(k, &filter->fo_capa_keys, k_list) {
179 if (k->k_key.lk_seq == seq) {
181 if (k->k_key.lk_keyid == capa_keyid(capa)) {
188 cfs_spin_unlock(&capa_lock);
191 CDEBUG(D_SEC, "MDS hasn't propagated capability keys yet, "
197 DEBUG_CAPA(D_ERROR, capa, "no matched capability key for");
201 OBD_ALLOC(hmac, CAPA_HMAC_MAX_LEN);
205 rc = capa_hmac(hmac, capa, key.lk_key);
207 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: rc %d", rc);
208 OBD_FREE(hmac, CAPA_HMAC_MAX_LEN);
212 rc = memcmp(hmac, capa->lc_hmac, CAPA_HMAC_MAX_LEN);
213 OBD_FREE(hmac, CAPA_HMAC_MAX_LEN);
215 DEBUG_CAPA_KEY(D_ERROR, &key, "calculate HMAC with ");
216 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
220 /* store in capa hash */
221 oc = capa_add(filter->fo_capa_hash, capa);
226 int filter_capa_fixoa(struct obd_export *exp, struct obdo *oa, obd_seq seq,
227 struct lustre_capa *capa)
232 /* skip capa check for llog and obdecho */
233 if (!fid_seq_is_mdt(seq))
236 if (!(exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
242 if (capa_flags(capa) == LC_ID_CONVERT) {
243 struct obd_device *obd = exp->exp_obd;
244 struct filter_obd *filter = &obd->u.filter;
245 struct filter_capa_key *k;
248 cfs_spin_lock(&capa_lock);
249 cfs_list_for_each_entry(k, &filter->fo_capa_keys, k_list) {
250 if (k->k_key.lk_seq == seq &&
251 k->k_key.lk_keyid == capa_keyid(capa)) {
256 cfs_spin_unlock(&capa_lock);
265 uid.id64 = capa_uid(capa);
266 gid.id64 = capa_gid(capa);
272 rc = capa_decrypt_id(d, s, k->k_key.lk_key,
273 CAPA_HMAC_KEY_MAX_LEN);
280 DEBUG_CAPA(D_ERROR, capa, "no matched capability key for");
288 void filter_free_capa_keys(struct filter_obd *filter)
290 struct filter_capa_key *key, *n;
292 cfs_spin_lock(&capa_lock);
293 cfs_list_for_each_entry_safe(key, n, &filter->fo_capa_keys, k_list) {
294 cfs_list_del_init(&key->k_list);
295 OBD_FREE(key, sizeof(*key));
297 cfs_spin_unlock(&capa_lock);