1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_SEC
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
31 #include <liblustre.h>
34 #include <libcfs/kp30.h>
35 #include <linux/obd_support.h>
36 #include <linux/lustre_idl.h>
37 #include <linux/lustre_net.h>
38 #include <linux/lustre_sec.h>
40 static spinlock_t svcsecs_lock = SPIN_LOCK_UNLOCKED;
41 static struct ptlrpc_svcsec *svcsecs[PTLRPCS_FLVR_MAJOR_MAX] = {
45 int svcsec_register(struct ptlrpc_svcsec *sec)
47 __u32 major = sec->pss_flavor;
49 if (major >= PTLRPCS_FLVR_MAJOR_MAX)
52 spin_lock(&svcsecs_lock);
54 spin_unlock(&svcsecs_lock);
58 spin_unlock(&svcsecs_lock);
60 CWARN("%s: registered\n", sec->pss_name);
64 int svcsec_unregister(struct ptlrpc_svcsec *sec)
66 __u32 major = sec->pss_flavor;
68 LASSERT(major < PTLRPCS_FLVR_MAJOR_MAX);
70 spin_lock(&svcsecs_lock);
71 if (!svcsecs[major]) {
72 spin_unlock(&svcsecs_lock);
76 LASSERT(svcsecs[major] == sec);
78 svcsecs[major] = NULL;
79 spin_unlock(&svcsecs_lock);
81 CWARN("%s: unregistered\n", sec->pss_name);
86 struct ptlrpc_svcsec * flavor2svcsec(__u32 flavor)
88 struct ptlrpc_svcsec *sec;
89 __u32 major = SEC_FLAVOR_MAJOR(flavor);
91 if (major >= PTLRPCS_FLVR_MAJOR_MAX)
94 spin_lock(&svcsecs_lock);
96 if (sec && !try_module_get(sec->pss_owner))
98 spin_unlock(&svcsecs_lock);
102 struct ptlrpc_svcsec * svcsec_get(struct ptlrpc_svcsec *sec)
106 rc = try_module_get(sec->pss_owner);
111 void svcsec_put(struct ptlrpc_svcsec *sec)
113 module_put(sec->pss_owner);
117 * common code to allocate reply_state buffer.
119 int svcsec_alloc_reply_state(struct ptlrpc_request *req,
120 int msgsize, int secsize)
122 struct ptlrpc_reply_state *rs;
124 int repsize, bufsize;
127 LASSERT(msgsize % 8 == 0);
128 LASSERT(secsize % 8 == 0);
130 repsize = sizeof(struct ptlrpcs_wire_hdr) + msgsize + secsize;
131 bufsize = repsize + sizeof(struct ptlrpc_reply_state);
133 OBD_ALLOC(buf, bufsize);
135 CERROR("can't alloc %d\n", bufsize);
139 /* req->rq_repbuf is not used on server side */
140 rs = (struct ptlrpc_reply_state *) (buf + repsize);
142 rs->rs_buf_len = bufsize;
144 rs->rs_repbuf_len = repsize;
145 /* current known data length is hdr + msg, security payload
146 * will be added on later.
148 rs->rs_repdata_len = sizeof(struct ptlrpcs_wire_hdr) + msgsize;
149 req->rq_repmsg = rs->rs_msg = (struct lustre_msg *)
150 (rs->rs_repbuf + sizeof(struct ptlrpcs_wire_hdr));
152 req->rq_reply_state = rs;
154 CDEBUG(D_SEC, "alloc rs buf at %p, len %d; repbuf at %p, len %d\n",
155 rs->rs_buf, rs->rs_buf_len, rs->rs_repbuf, rs->rs_repbuf_len);
160 void svcsec_free_reply_state(struct ptlrpc_reply_state *rs)
165 /* for work around memory-alloc debug poison */
168 OBD_FREE(p, rs->rs_buf_len);
172 int svcsec_alloc_repbuf(struct ptlrpc_svcsec *svcsec,
173 struct ptlrpc_request *req,
177 LASSERT(msgsize % 8 == 0);
179 if (svcsec->alloc_repbuf)
180 return svcsec->alloc_repbuf(svcsec, req, msgsize);
182 return svcsec_alloc_reply_state(req, msgsize, 0);
185 int svcsec_accept(struct ptlrpc_request *req, enum ptlrpcs_error *res)
187 struct ptlrpc_svcsec *sec;
188 struct ptlrpcs_wire_hdr *sec_hdr;
192 LASSERT(req->rq_reqbuf);
193 LASSERT(!req->rq_reqmsg);
194 LASSERT(!req->rq_svcsec);
196 *res = PTLRPCS_BADCRED;
197 if (req->rq_reqbuf_len < sizeof(*sec_hdr)) {
198 CERROR("drop too short msg (length: %d)\n", req->rq_reqbuf_len);
202 sec_hdr = (struct ptlrpcs_wire_hdr *) req->rq_reqbuf;
203 sec_hdr->flavor = le32_to_cpu(sec_hdr->flavor);
204 sec_hdr->msg_len = le32_to_cpu(sec_hdr->msg_len);
205 sec_hdr->sec_len = le32_to_cpu(sec_hdr->sec_len);
208 switch (SEC_FLAVOR_SVC(sec_hdr->flavor)) {
209 case PTLRPCS_SVC_NONE:
210 case PTLRPCS_SVC_AUTH:
211 case PTLRPCS_SVC_PRIV:
214 CERROR("unknown security svc %d\n",
215 SEC_FLAVOR_SVC(sec_hdr->flavor));
219 if (sizeof(*sec_hdr) + sec_hdr->msg_len + sec_hdr->sec_len >
220 req->rq_reqbuf_len) {
221 CERROR("received %d, msg %d, sec %d\n",
222 req->rq_reqbuf_len, sec_hdr->msg_len, sec_hdr->sec_len);
226 req->rq_svcsec = sec = flavor2svcsec(sec_hdr->flavor);
228 CERROR("drop msg: unsupported flavor %d\n", sec_hdr->flavor);
232 /* flavor in the wire header might not be the real "end user" flavor,
233 * we set it here anyway, later accept() might override with correct
236 req->rq_req_secflvr = sec_hdr->flavor;
238 LASSERT(sec->accept);
239 rc = sec->accept(req, res);
244 req->rq_svcsec = NULL;
249 LASSERT(req->rq_reqmsg);
256 int svcsec_authorize(struct ptlrpc_request *req)
258 LASSERT(req->rq_svcsec);
259 LASSERT(req->rq_svcsec->authorize);
261 return (req->rq_svcsec->authorize(req));
264 void svcsec_cleanup_req(struct ptlrpc_request *req)
266 struct ptlrpc_svcsec *svcsec = req->rq_svcsec;
270 LASSERT(svcsec->cleanup_req || !req->rq_svcsec_data);
272 if (svcsec->cleanup_req)
273 svcsec->cleanup_req(svcsec, req);