1 #include "ceph_debug.h"
3 #include <linux/module.h>
4 #include <linux/slab.h>
13 #include "messenger.h"
16 * get protocol handler
18 static u32 supported_protocols
[] = {
23 int ceph_auth_init_protocol(struct ceph_auth_client
*ac
, int protocol
)
27 return ceph_auth_none_init(ac
);
29 return ceph_x_init(ac
);
38 struct ceph_auth_client
*ceph_auth_init(const char *name
, const char *secret
)
40 struct ceph_auth_client
*ac
;
43 dout("auth_init name '%s' secret '%s'\n", name
, secret
);
46 ac
= kzalloc(sizeof(*ac
), GFP_NOFS
);
50 ac
->negotiating
= true;
54 ac
->name
= CEPH_AUTH_NAME_DEFAULT
;
55 dout("auth_init name %s secret %s\n", ac
->name
, secret
);
63 void ceph_auth_destroy(struct ceph_auth_client
*ac
)
65 dout("auth_destroy %p\n", ac
);
72 * Reset occurs when reconnecting to the monitor.
74 void ceph_auth_reset(struct ceph_auth_client
*ac
)
76 dout("auth_reset %p\n", ac
);
77 if (ac
->ops
&& !ac
->negotiating
)
79 ac
->negotiating
= true;
82 int ceph_entity_name_encode(const char *name
, void **p
, void *end
)
84 int len
= strlen(name
);
86 if (*p
+ 2*sizeof(u32
) + len
> end
)
88 ceph_encode_32(p
, CEPH_ENTITY_TYPE_CLIENT
);
89 ceph_encode_32(p
, len
);
90 ceph_encode_copy(p
, name
, len
);
95 * Initiate protocol negotiation with monitor. Include entity name
96 * and list supported protocols.
98 int ceph_auth_build_hello(struct ceph_auth_client
*ac
, void *buf
, size_t len
)
100 struct ceph_mon_request_header
*monhdr
= buf
;
101 void *p
= monhdr
+ 1, *end
= buf
+ len
, *lenp
;
105 dout("auth_build_hello\n");
106 monhdr
->have_version
= 0;
107 monhdr
->session_mon
= cpu_to_le16(-1);
108 monhdr
->session_mon_tid
= 0;
110 ceph_encode_32(&p
, 0); /* no protocol, yet */
115 ceph_decode_need(&p
, end
, 1 + sizeof(u32
), bad
);
116 ceph_encode_8(&p
, 1);
117 num
= ARRAY_SIZE(supported_protocols
);
118 ceph_encode_32(&p
, num
);
119 ceph_decode_need(&p
, end
, num
* sizeof(u32
), bad
);
120 for (i
= 0; i
< num
; i
++)
121 ceph_encode_32(&p
, supported_protocols
[i
]);
123 ret
= ceph_entity_name_encode(ac
->name
, &p
, end
);
126 ceph_decode_need(&p
, end
, sizeof(u64
), bad
);
127 ceph_encode_64(&p
, ac
->global_id
);
129 ceph_encode_32(&lenp
, p
- lenp
- sizeof(u32
));
136 int ceph_build_auth_request(struct ceph_auth_client
*ac
,
137 void *msg_buf
, size_t msg_len
)
139 struct ceph_mon_request_header
*monhdr
= msg_buf
;
140 void *p
= monhdr
+ 1;
141 void *end
= msg_buf
+ msg_len
;
144 monhdr
->have_version
= 0;
145 monhdr
->session_mon
= cpu_to_le16(-1);
146 monhdr
->session_mon_tid
= 0;
148 ceph_encode_32(&p
, ac
->protocol
);
150 ret
= ac
->ops
->build_request(ac
, p
+ sizeof(u32
), end
);
152 pr_err("error %d building request\n", ret
);
155 dout(" built request %d bytes\n", ret
);
156 ceph_encode_32(&p
, ret
);
157 return p
+ ret
- msg_buf
;
161 * Handle auth message from monitor.
163 int ceph_handle_auth_reply(struct ceph_auth_client
*ac
,
164 void *buf
, size_t len
,
165 void *reply_buf
, size_t reply_len
)
168 void *end
= buf
+ len
;
172 void *payload
, *payload_end
;
178 dout("handle_auth_reply %p %p\n", p
, end
);
179 ceph_decode_need(&p
, end
, sizeof(u32
) * 3 + sizeof(u64
), bad
);
180 protocol
= ceph_decode_32(&p
);
181 result
= ceph_decode_32(&p
);
182 global_id
= ceph_decode_64(&p
);
183 payload_len
= ceph_decode_32(&p
);
186 ceph_decode_need(&p
, end
, sizeof(u32
), bad
);
187 result_msg_len
= ceph_decode_32(&p
);
193 dout(" result %d '%.*s' gid %llu len %d\n", result
, result_msg_len
,
194 result_msg
, global_id
, payload_len
);
196 payload_end
= payload
+ payload_len
;
198 if (global_id
&& ac
->global_id
!= global_id
) {
199 dout(" set global_id %lld -> %lld\n", ac
->global_id
, global_id
);
200 ac
->global_id
= global_id
;
203 if (ac
->negotiating
) {
204 /* server does not support our protocols? */
205 if (!protocol
&& result
< 0) {
209 /* set up (new) protocol handler? */
210 if (ac
->protocol
&& ac
->protocol
!= protocol
) {
211 ac
->ops
->destroy(ac
);
215 if (ac
->protocol
!= protocol
) {
216 ret
= ceph_auth_init_protocol(ac
, protocol
);
218 pr_err("error %d on auth protocol %d init\n",
224 ac
->negotiating
= false;
227 ret
= ac
->ops
->handle_reply(ac
, result
, payload
, payload_end
);
228 if (ret
== -EAGAIN
) {
229 return ceph_build_auth_request(ac
, reply_buf
, reply_len
);
231 pr_err("authentication error %d\n", ret
);
237 pr_err("failed to decode auth msg\n");
242 int ceph_build_auth(struct ceph_auth_client
*ac
,
243 void *msg_buf
, size_t msg_len
)
246 return ceph_auth_build_hello(ac
, msg_buf
, msg_len
);
248 if (!ac
->ops
->is_authenticated(ac
))
249 return ceph_build_auth_request(ac
, msg_buf
, msg_len
);
253 int ceph_auth_is_authenticated(struct ceph_auth_client
*ac
)
257 return ac
->ops
->is_authenticated(ac
);