1 #include "ceph_debug.h"
3 #include <linux/module.h>
12 #include "messenger.h"
15 * get protocol handler
17 static u32 supported_protocols
[] = {
22 int ceph_auth_init_protocol(struct ceph_auth_client
*ac
, int protocol
)
26 return ceph_auth_none_init(ac
);
28 return ceph_x_init(ac
);
37 struct ceph_auth_client
*ceph_auth_init(const char *name
, const char *secret
)
39 struct ceph_auth_client
*ac
;
42 dout("auth_init name '%s' secret '%s'\n", name
, secret
);
45 ac
= kzalloc(sizeof(*ac
), GFP_NOFS
);
49 ac
->negotiating
= true;
53 ac
->name
= CEPH_AUTH_NAME_DEFAULT
;
54 dout("auth_init name %s secret %s\n", ac
->name
, secret
);
62 void ceph_auth_destroy(struct ceph_auth_client
*ac
)
64 dout("auth_destroy %p\n", ac
);
71 * Reset occurs when reconnecting to the monitor.
73 void ceph_auth_reset(struct ceph_auth_client
*ac
)
75 dout("auth_reset %p\n", ac
);
76 if (ac
->ops
&& !ac
->negotiating
)
78 ac
->negotiating
= true;
81 int ceph_entity_name_encode(const char *name
, void **p
, void *end
)
83 int len
= strlen(name
);
85 if (*p
+ 2*sizeof(u32
) + len
> end
)
87 ceph_encode_32(p
, CEPH_ENTITY_TYPE_CLIENT
);
88 ceph_encode_32(p
, len
);
89 ceph_encode_copy(p
, name
, len
);
94 * Initiate protocol negotiation with monitor. Include entity name
95 * and list supported protocols.
97 int ceph_auth_build_hello(struct ceph_auth_client
*ac
, void *buf
, size_t len
)
99 struct ceph_mon_request_header
*monhdr
= buf
;
100 void *p
= monhdr
+ 1, *end
= buf
+ len
, *lenp
;
104 dout("auth_build_hello\n");
105 monhdr
->have_version
= 0;
106 monhdr
->session_mon
= cpu_to_le16(-1);
107 monhdr
->session_mon_tid
= 0;
109 ceph_encode_32(&p
, 0); /* no protocol, yet */
114 ceph_decode_need(&p
, end
, 1 + sizeof(u32
), bad
);
115 ceph_encode_8(&p
, 1);
116 num
= ARRAY_SIZE(supported_protocols
);
117 ceph_encode_32(&p
, num
);
118 ceph_decode_need(&p
, end
, num
* sizeof(u32
), bad
);
119 for (i
= 0; i
< num
; i
++)
120 ceph_encode_32(&p
, supported_protocols
[i
]);
122 ret
= ceph_entity_name_encode(ac
->name
, &p
, end
);
125 ceph_decode_need(&p
, end
, sizeof(u64
), bad
);
126 ceph_encode_64(&p
, ac
->global_id
);
128 ceph_encode_32(&lenp
, p
- lenp
- sizeof(u32
));
135 int ceph_build_auth_request(struct ceph_auth_client
*ac
,
136 void *msg_buf
, size_t msg_len
)
138 struct ceph_mon_request_header
*monhdr
= msg_buf
;
139 void *p
= monhdr
+ 1;
140 void *end
= msg_buf
+ msg_len
;
143 monhdr
->have_version
= 0;
144 monhdr
->session_mon
= cpu_to_le16(-1);
145 monhdr
->session_mon_tid
= 0;
147 ceph_encode_32(&p
, ac
->protocol
);
149 ret
= ac
->ops
->build_request(ac
, p
+ sizeof(u32
), end
);
151 pr_err("error %d building request\n", ret
);
154 dout(" built request %d bytes\n", ret
);
155 ceph_encode_32(&p
, ret
);
156 return p
+ ret
- msg_buf
;
160 * Handle auth message from monitor.
162 int ceph_handle_auth_reply(struct ceph_auth_client
*ac
,
163 void *buf
, size_t len
,
164 void *reply_buf
, size_t reply_len
)
167 void *end
= buf
+ len
;
171 void *payload
, *payload_end
;
177 dout("handle_auth_reply %p %p\n", p
, end
);
178 ceph_decode_need(&p
, end
, sizeof(u32
) * 3 + sizeof(u64
), bad
);
179 protocol
= ceph_decode_32(&p
);
180 result
= ceph_decode_32(&p
);
181 global_id
= ceph_decode_64(&p
);
182 payload_len
= ceph_decode_32(&p
);
185 ceph_decode_need(&p
, end
, sizeof(u32
), bad
);
186 result_msg_len
= ceph_decode_32(&p
);
192 dout(" result %d '%.*s' gid %llu len %d\n", result
, result_msg_len
,
193 result_msg
, global_id
, payload_len
);
195 payload_end
= payload
+ payload_len
;
197 if (global_id
&& ac
->global_id
!= global_id
) {
198 dout(" set global_id %lld -> %lld\n", ac
->global_id
, global_id
);
199 ac
->global_id
= global_id
;
202 if (ac
->negotiating
) {
203 /* server does not support our protocols? */
204 if (!protocol
&& result
< 0) {
208 /* set up (new) protocol handler? */
209 if (ac
->protocol
&& ac
->protocol
!= protocol
) {
210 ac
->ops
->destroy(ac
);
214 if (ac
->protocol
!= protocol
) {
215 ret
= ceph_auth_init_protocol(ac
, protocol
);
217 pr_err("error %d on auth protocol %d init\n",
223 ac
->negotiating
= false;
226 ret
= ac
->ops
->handle_reply(ac
, result
, payload
, payload_end
);
227 if (ret
== -EAGAIN
) {
228 return ceph_build_auth_request(ac
, reply_buf
, reply_len
);
230 pr_err("authentication error %d\n", ret
);
236 pr_err("failed to decode auth msg\n");
241 int ceph_build_auth(struct ceph_auth_client
*ac
,
242 void *msg_buf
, size_t msg_len
)
245 return ceph_auth_build_hello(ac
, msg_buf
, msg_len
);
247 if (!ac
->ops
->is_authenticated(ac
))
248 return ceph_build_auth_request(ac
, msg_buf
, msg_len
);
252 int ceph_auth_is_authenticated(struct ceph_auth_client
*ac
)
256 return ac
->ops
->is_authenticated(ac
);