1 #include "ceph_debug.h"
3 #include <linux/module.h>
11 #include "messenger.h"
14 * get protocol handler
16 static u32 supported_protocols[] = {
20 int ceph_auth_init_protocol(struct ceph_auth_client *ac, int protocol)
24 return ceph_auth_none_init(ac);
33 struct ceph_auth_client *ceph_auth_init(const char *name, const char *secret)
35 struct ceph_auth_client *ac;
38 dout("auth_init name '%s' secret '%s'\n", name, secret);
41 ac = kzalloc(sizeof(*ac), GFP_NOFS);
45 ac->negotiating = true;
49 ac->name = CEPH_AUTH_NAME_DEFAULT;
50 dout("auth_init name %s secret %s\n", ac->name, secret);
58 void ceph_auth_destroy(struct ceph_auth_client *ac)
60 dout("auth_destroy %p\n", ac);
67 * Reset occurs when reconnecting to the monitor.
69 void ceph_auth_reset(struct ceph_auth_client *ac)
71 dout("auth_reset %p\n", ac);
72 if (ac->ops && !ac->negotiating)
74 ac->negotiating = true;
77 int ceph_entity_name_encode(const char *name, void **p, void *end)
79 int len = strlen(name);
81 if (*p + 2*sizeof(u32) + len > end)
83 ceph_encode_32(p, CEPH_ENTITY_TYPE_CLIENT);
84 ceph_encode_32(p, len);
85 ceph_encode_copy(p, name, len);
90 * Initiate protocol negotiation with monitor. Include entity name
91 * and list supported protocols.
93 int ceph_auth_build_hello(struct ceph_auth_client *ac, void *buf, size_t len)
95 struct ceph_mon_request_header *monhdr = buf;
96 void *p = monhdr + 1, *end = buf + len, *lenp;
100 dout("auth_build_hello\n");
101 monhdr->have_version = 0;
102 monhdr->session_mon = cpu_to_le16(-1);
103 monhdr->session_mon_tid = 0;
105 ceph_encode_32(&p, 0); /* no protocol, yet */
110 ceph_decode_need(&p, end, 1 + sizeof(u32), bad);
111 ceph_encode_8(&p, 1);
112 num = ARRAY_SIZE(supported_protocols);
113 ceph_encode_32(&p, num);
114 ceph_decode_need(&p, end, num * sizeof(u32), bad);
115 for (i = 0; i < num; i++)
116 ceph_encode_32(&p, supported_protocols[i]);
118 ret = ceph_entity_name_encode(ac->name, &p, end);
121 ceph_decode_need(&p, end, sizeof(u64), bad);
122 ceph_encode_64(&p, ac->global_id);
124 ceph_encode_32(&lenp, p - lenp - sizeof(u32));
131 int ceph_build_auth_request(struct ceph_auth_client *ac,
132 void *msg_buf, size_t msg_len)
134 struct ceph_mon_request_header *monhdr = msg_buf;
135 void *p = monhdr + 1;
136 void *end = msg_buf + msg_len;
139 monhdr->have_version = 0;
140 monhdr->session_mon = cpu_to_le16(-1);
141 monhdr->session_mon_tid = 0;
143 ceph_encode_32(&p, ac->protocol);
145 ret = ac->ops->build_request(ac, p + sizeof(u32), end);
147 pr_err("error %d building request\n", ret);
150 dout(" built request %d bytes\n", ret);
151 ceph_encode_32(&p, ret);
152 return p + ret - msg_buf;
156 * Handle auth message from monitor.
158 int ceph_handle_auth_reply(struct ceph_auth_client *ac,
159 void *buf, size_t len,
160 void *reply_buf, size_t reply_len)
163 void *end = buf + len;
167 void *payload, *payload_end;
173 dout("handle_auth_reply %p %p\n", p, end);
174 ceph_decode_need(&p, end, sizeof(u32) * 3 + sizeof(u64), bad);
175 protocol = ceph_decode_32(&p);
176 result = ceph_decode_32(&p);
177 global_id = ceph_decode_64(&p);
178 payload_len = ceph_decode_32(&p);
181 ceph_decode_need(&p, end, sizeof(u32), bad);
182 result_msg_len = ceph_decode_32(&p);
188 dout(" result %d '%.*s' gid %llu len %d\n", result, result_msg_len,
189 result_msg, global_id, payload_len);
191 payload_end = payload + payload_len;
193 if (global_id && ac->global_id != global_id) {
194 dout(" set global_id %lld -> %lld\n", ac->global_id, global_id);
195 ac->global_id = global_id;
198 if (ac->negotiating) {
199 /* server does not support our protocols? */
200 if (!protocol && result < 0) {
204 /* set up (new) protocol handler? */
205 if (ac->protocol && ac->protocol != protocol) {
206 ac->ops->destroy(ac);
210 if (ac->protocol != protocol) {
211 ret = ceph_auth_init_protocol(ac, protocol);
213 pr_err("error %d on auth protocol %d init\n",
219 ac->negotiating = false;
222 ret = ac->ops->handle_reply(ac, result, payload, payload_end);
223 if (ret == -EAGAIN) {
224 return ceph_build_auth_request(ac, reply_buf, reply_len);
226 pr_err("authentication error %d\n", ret);
232 pr_err("failed to decode auth msg\n");
237 int ceph_build_auth(struct ceph_auth_client *ac,
238 void *msg_buf, size_t msg_len)
241 return ceph_auth_build_hello(ac, msg_buf, msg_len);
243 if (!ac->ops->is_authenticated(ac))
244 return ceph_build_auth_request(ac, msg_buf, msg_len);
248 int ceph_auth_is_authenticated(struct ceph_auth_client *ac)
252 return ac->ops->is_authenticated(ac);