1 // SPDX-License-Identifier: GPL-2.0
3 * Ceph msgr2 protocol implementation
5 * Copyright (C) 2020 Ilya Dryomov <idryomov@gmail.com>
8 #include <linux/ceph/ceph_debug.h>
10 #include <crypto/aead.h>
11 #include <crypto/algapi.h> /* for crypto_memneq() */
12 #include <crypto/hash.h>
13 #include <crypto/sha2.h>
14 #include <linux/bvec.h>
15 #include <linux/crc32c.h>
16 #include <linux/net.h>
17 #include <linux/scatterlist.h>
18 #include <linux/socket.h>
19 #include <linux/sched/mm.h>
23 #include <linux/ceph/ceph_features.h>
24 #include <linux/ceph/decode.h>
25 #include <linux/ceph/libceph.h>
26 #include <linux/ceph/messenger.h>
28 #include "crypto.h" /* for CEPH_KEY_LEN and CEPH_MAX_CON_SECRET_LEN */
30 #define FRAME_TAG_HELLO 1
31 #define FRAME_TAG_AUTH_REQUEST 2
32 #define FRAME_TAG_AUTH_BAD_METHOD 3
33 #define FRAME_TAG_AUTH_REPLY_MORE 4
34 #define FRAME_TAG_AUTH_REQUEST_MORE 5
35 #define FRAME_TAG_AUTH_DONE 6
36 #define FRAME_TAG_AUTH_SIGNATURE 7
37 #define FRAME_TAG_CLIENT_IDENT 8
38 #define FRAME_TAG_SERVER_IDENT 9
39 #define FRAME_TAG_IDENT_MISSING_FEATURES 10
40 #define FRAME_TAG_SESSION_RECONNECT 11
41 #define FRAME_TAG_SESSION_RESET 12
42 #define FRAME_TAG_SESSION_RETRY 13
43 #define FRAME_TAG_SESSION_RETRY_GLOBAL 14
44 #define FRAME_TAG_SESSION_RECONNECT_OK 15
45 #define FRAME_TAG_WAIT 16
46 #define FRAME_TAG_MESSAGE 17
47 #define FRAME_TAG_KEEPALIVE2 18
48 #define FRAME_TAG_KEEPALIVE2_ACK 19
49 #define FRAME_TAG_ACK 20
51 #define FRAME_LATE_STATUS_ABORTED 0x1
52 #define FRAME_LATE_STATUS_COMPLETE 0xe
53 #define FRAME_LATE_STATUS_ABORTED_MASK 0xf
55 #define IN_S_HANDLE_PREAMBLE 1
56 #define IN_S_HANDLE_CONTROL 2
57 #define IN_S_HANDLE_CONTROL_REMAINDER 3
58 #define IN_S_PREPARE_READ_DATA 4
59 #define IN_S_PREPARE_READ_DATA_CONT 5
60 #define IN_S_PREPARE_READ_ENC_PAGE 6
61 #define IN_S_HANDLE_EPILOGUE 7
62 #define IN_S_FINISH_SKIP 8
64 #define OUT_S_QUEUE_DATA 1
65 #define OUT_S_QUEUE_DATA_CONT 2
66 #define OUT_S_QUEUE_ENC_PAGE 3
67 #define OUT_S_QUEUE_ZEROS 4
68 #define OUT_S_FINISH_MESSAGE 5
69 #define OUT_S_GET_NEXT 6
71 #define CTRL_BODY(p) ((void *)(p) + CEPH_PREAMBLE_LEN)
72 #define FRONT_PAD(p) ((void *)(p) + CEPH_EPILOGUE_SECURE_LEN)
73 #define MIDDLE_PAD(p) (FRONT_PAD(p) + CEPH_GCM_BLOCK_LEN)
74 #define DATA_PAD(p) (MIDDLE_PAD(p) + CEPH_GCM_BLOCK_LEN)
76 #define CEPH_MSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
78 static int do_recvmsg(struct socket *sock, struct iov_iter *it)
80 struct msghdr msg = { .msg_flags = CEPH_MSG_FLAGS };
84 while (iov_iter_count(it)) {
85 ret = sock_recvmsg(sock, &msg, msg.msg_flags);
92 iov_iter_advance(it, ret);
95 WARN_ON(msg_data_left(&msg));
100 * Read as much as possible.
103 * 1 - done, nothing (else) to read
104 * 0 - socket is empty, need to wait
107 static int ceph_tcp_recv(struct ceph_connection *con)
111 dout("%s con %p %s %zu\n", __func__, con,
112 iov_iter_is_discard(&con->v2.in_iter) ? "discard" : "need",
113 iov_iter_count(&con->v2.in_iter));
114 ret = do_recvmsg(con->sock, &con->v2.in_iter);
115 dout("%s con %p ret %d left %zu\n", __func__, con, ret,
116 iov_iter_count(&con->v2.in_iter));
121 * Write as much as possible. The socket is expected to be corked,
122 * so we don't bother with MSG_MORE here.
125 * >0 - done, nothing (else) to write
126 * 0 - socket is full, need to wait
129 static int ceph_tcp_send(struct ceph_connection *con)
131 struct msghdr msg = {
132 .msg_iter = con->v2.out_iter,
133 .msg_flags = CEPH_MSG_FLAGS,
137 if (WARN_ON(!iov_iter_is_bvec(&con->v2.out_iter)))
140 if (con->v2.out_iter_sendpage)
141 msg.msg_flags |= MSG_SPLICE_PAGES;
143 dout("%s con %p have %zu try_sendpage %d\n", __func__, con,
144 iov_iter_count(&con->v2.out_iter), con->v2.out_iter_sendpage);
146 ret = sock_sendmsg(con->sock, &msg);
148 iov_iter_advance(&con->v2.out_iter, ret);
149 else if (ret == -EAGAIN)
152 dout("%s con %p ret %d left %zu\n", __func__, con, ret,
153 iov_iter_count(&con->v2.out_iter));
157 static void add_in_kvec(struct ceph_connection *con, void *buf, int len)
159 BUG_ON(con->v2.in_kvec_cnt >= ARRAY_SIZE(con->v2.in_kvecs));
160 WARN_ON(!iov_iter_is_kvec(&con->v2.in_iter));
162 con->v2.in_kvecs[con->v2.in_kvec_cnt].iov_base = buf;
163 con->v2.in_kvecs[con->v2.in_kvec_cnt].iov_len = len;
164 con->v2.in_kvec_cnt++;
166 con->v2.in_iter.nr_segs++;
167 con->v2.in_iter.count += len;
170 static void reset_in_kvecs(struct ceph_connection *con)
172 WARN_ON(iov_iter_count(&con->v2.in_iter));
174 con->v2.in_kvec_cnt = 0;
175 iov_iter_kvec(&con->v2.in_iter, ITER_DEST, con->v2.in_kvecs, 0, 0);
178 static void set_in_bvec(struct ceph_connection *con, const struct bio_vec *bv)
180 WARN_ON(iov_iter_count(&con->v2.in_iter));
182 con->v2.in_bvec = *bv;
183 iov_iter_bvec(&con->v2.in_iter, ITER_DEST, &con->v2.in_bvec, 1, bv->bv_len);
186 static void set_in_skip(struct ceph_connection *con, int len)
188 WARN_ON(iov_iter_count(&con->v2.in_iter));
190 dout("%s con %p len %d\n", __func__, con, len);
191 iov_iter_discard(&con->v2.in_iter, ITER_DEST, len);
194 static void add_out_kvec(struct ceph_connection *con, void *buf, int len)
196 BUG_ON(con->v2.out_kvec_cnt >= ARRAY_SIZE(con->v2.out_kvecs));
197 WARN_ON(!iov_iter_is_kvec(&con->v2.out_iter));
198 WARN_ON(con->v2.out_zero);
200 con->v2.out_kvecs[con->v2.out_kvec_cnt].iov_base = buf;
201 con->v2.out_kvecs[con->v2.out_kvec_cnt].iov_len = len;
202 con->v2.out_kvec_cnt++;
204 con->v2.out_iter.nr_segs++;
205 con->v2.out_iter.count += len;
208 static void reset_out_kvecs(struct ceph_connection *con)
210 WARN_ON(iov_iter_count(&con->v2.out_iter));
211 WARN_ON(con->v2.out_zero);
213 con->v2.out_kvec_cnt = 0;
215 iov_iter_kvec(&con->v2.out_iter, ITER_SOURCE, con->v2.out_kvecs, 0, 0);
216 con->v2.out_iter_sendpage = false;
219 static void set_out_bvec(struct ceph_connection *con, const struct bio_vec *bv,
222 WARN_ON(iov_iter_count(&con->v2.out_iter));
223 WARN_ON(con->v2.out_zero);
225 con->v2.out_bvec = *bv;
226 con->v2.out_iter_sendpage = zerocopy;
227 iov_iter_bvec(&con->v2.out_iter, ITER_SOURCE, &con->v2.out_bvec, 1,
228 con->v2.out_bvec.bv_len);
231 static void set_out_bvec_zero(struct ceph_connection *con)
233 WARN_ON(iov_iter_count(&con->v2.out_iter));
234 WARN_ON(!con->v2.out_zero);
236 bvec_set_page(&con->v2.out_bvec, ceph_zero_page,
237 min(con->v2.out_zero, (int)PAGE_SIZE), 0);
238 con->v2.out_iter_sendpage = true;
239 iov_iter_bvec(&con->v2.out_iter, ITER_SOURCE, &con->v2.out_bvec, 1,
240 con->v2.out_bvec.bv_len);
243 static void out_zero_add(struct ceph_connection *con, int len)
245 dout("%s con %p len %d\n", __func__, con, len);
246 con->v2.out_zero += len;
249 static void *alloc_conn_buf(struct ceph_connection *con, int len)
253 dout("%s con %p len %d\n", __func__, con, len);
255 if (WARN_ON(con->v2.conn_buf_cnt >= ARRAY_SIZE(con->v2.conn_bufs)))
258 buf = kvmalloc(len, GFP_NOIO);
262 con->v2.conn_bufs[con->v2.conn_buf_cnt++] = buf;
266 static void free_conn_bufs(struct ceph_connection *con)
268 while (con->v2.conn_buf_cnt)
269 kvfree(con->v2.conn_bufs[--con->v2.conn_buf_cnt]);
272 static void add_in_sign_kvec(struct ceph_connection *con, void *buf, int len)
274 BUG_ON(con->v2.in_sign_kvec_cnt >= ARRAY_SIZE(con->v2.in_sign_kvecs));
276 con->v2.in_sign_kvecs[con->v2.in_sign_kvec_cnt].iov_base = buf;
277 con->v2.in_sign_kvecs[con->v2.in_sign_kvec_cnt].iov_len = len;
278 con->v2.in_sign_kvec_cnt++;
281 static void clear_in_sign_kvecs(struct ceph_connection *con)
283 con->v2.in_sign_kvec_cnt = 0;
286 static void add_out_sign_kvec(struct ceph_connection *con, void *buf, int len)
288 BUG_ON(con->v2.out_sign_kvec_cnt >= ARRAY_SIZE(con->v2.out_sign_kvecs));
290 con->v2.out_sign_kvecs[con->v2.out_sign_kvec_cnt].iov_base = buf;
291 con->v2.out_sign_kvecs[con->v2.out_sign_kvec_cnt].iov_len = len;
292 con->v2.out_sign_kvec_cnt++;
295 static void clear_out_sign_kvecs(struct ceph_connection *con)
297 con->v2.out_sign_kvec_cnt = 0;
300 static bool con_secure(struct ceph_connection *con)
302 return con->v2.con_mode == CEPH_CON_MODE_SECURE;
305 static int front_len(const struct ceph_msg *msg)
307 return le32_to_cpu(msg->hdr.front_len);
310 static int middle_len(const struct ceph_msg *msg)
312 return le32_to_cpu(msg->hdr.middle_len);
315 static int data_len(const struct ceph_msg *msg)
317 return le32_to_cpu(msg->hdr.data_len);
320 static bool need_padding(int len)
322 return !IS_ALIGNED(len, CEPH_GCM_BLOCK_LEN);
325 static int padded_len(int len)
327 return ALIGN(len, CEPH_GCM_BLOCK_LEN);
330 static int padding_len(int len)
332 return padded_len(len) - len;
335 /* preamble + control segment */
336 static int head_onwire_len(int ctrl_len, bool secure)
342 head_len = CEPH_PREAMBLE_SECURE_LEN;
343 if (ctrl_len > CEPH_PREAMBLE_INLINE_LEN) {
344 rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
345 head_len += padded_len(rem_len) + CEPH_GCM_TAG_LEN;
348 head_len = CEPH_PREAMBLE_PLAIN_LEN;
350 head_len += ctrl_len + CEPH_CRC_LEN;
355 /* front, middle and data segments + epilogue */
356 static int __tail_onwire_len(int front_len, int middle_len, int data_len,
359 if (!front_len && !middle_len && !data_len)
363 return front_len + middle_len + data_len +
364 CEPH_EPILOGUE_PLAIN_LEN;
366 return padded_len(front_len) + padded_len(middle_len) +
367 padded_len(data_len) + CEPH_EPILOGUE_SECURE_LEN;
370 static int tail_onwire_len(const struct ceph_msg *msg, bool secure)
372 return __tail_onwire_len(front_len(msg), middle_len(msg),
373 data_len(msg), secure);
376 /* head_onwire_len(sizeof(struct ceph_msg_header2), false) */
377 #define MESSAGE_HEAD_PLAIN_LEN (CEPH_PREAMBLE_PLAIN_LEN + \
378 sizeof(struct ceph_msg_header2) + \
381 static const int frame_aligns[] = {
389 * Discards trailing empty segments, unless there is just one segment.
390 * A frame always has at least one (possibly empty) segment.
392 static int calc_segment_count(const int *lens, int len_cnt)
396 for (i = len_cnt - 1; i >= 0; i--) {
404 static void init_frame_desc(struct ceph_frame_desc *desc, int tag,
405 const int *lens, int len_cnt)
409 memset(desc, 0, sizeof(*desc));
412 desc->fd_seg_cnt = calc_segment_count(lens, len_cnt);
413 BUG_ON(desc->fd_seg_cnt > CEPH_FRAME_MAX_SEGMENT_COUNT);
414 for (i = 0; i < desc->fd_seg_cnt; i++) {
415 desc->fd_lens[i] = lens[i];
416 desc->fd_aligns[i] = frame_aligns[i];
421 * Preamble crc covers everything up to itself (28 bytes) and
422 * is calculated and verified irrespective of the connection mode
423 * (i.e. even if the frame is encrypted).
425 static void encode_preamble(const struct ceph_frame_desc *desc, void *p)
427 void *crcp = p + CEPH_PREAMBLE_LEN - CEPH_CRC_LEN;
431 memset(p, 0, CEPH_PREAMBLE_LEN);
433 ceph_encode_8(&p, desc->fd_tag);
434 ceph_encode_8(&p, desc->fd_seg_cnt);
435 for (i = 0; i < desc->fd_seg_cnt; i++) {
436 ceph_encode_32(&p, desc->fd_lens[i]);
437 ceph_encode_16(&p, desc->fd_aligns[i]);
440 put_unaligned_le32(crc32c(0, start, crcp - start), crcp);
443 static int decode_preamble(void *p, struct ceph_frame_desc *desc)
445 void *crcp = p + CEPH_PREAMBLE_LEN - CEPH_CRC_LEN;
446 u32 crc, expected_crc;
449 crc = crc32c(0, p, crcp - p);
450 expected_crc = get_unaligned_le32(crcp);
451 if (crc != expected_crc) {
452 pr_err("bad preamble crc, calculated %u, expected %u\n",
457 memset(desc, 0, sizeof(*desc));
459 desc->fd_tag = ceph_decode_8(&p);
460 desc->fd_seg_cnt = ceph_decode_8(&p);
461 if (desc->fd_seg_cnt < 1 ||
462 desc->fd_seg_cnt > CEPH_FRAME_MAX_SEGMENT_COUNT) {
463 pr_err("bad segment count %d\n", desc->fd_seg_cnt);
466 for (i = 0; i < desc->fd_seg_cnt; i++) {
467 desc->fd_lens[i] = ceph_decode_32(&p);
468 desc->fd_aligns[i] = ceph_decode_16(&p);
472 * This would fire for FRAME_TAG_WAIT (it has one empty
473 * segment), but we should never get it as client.
475 if (!desc->fd_lens[desc->fd_seg_cnt - 1]) {
476 pr_err("last segment empty\n");
480 if (desc->fd_lens[0] > CEPH_MSG_MAX_CONTROL_LEN) {
481 pr_err("control segment too big %d\n", desc->fd_lens[0]);
484 if (desc->fd_lens[1] > CEPH_MSG_MAX_FRONT_LEN) {
485 pr_err("front segment too big %d\n", desc->fd_lens[1]);
488 if (desc->fd_lens[2] > CEPH_MSG_MAX_MIDDLE_LEN) {
489 pr_err("middle segment too big %d\n", desc->fd_lens[2]);
492 if (desc->fd_lens[3] > CEPH_MSG_MAX_DATA_LEN) {
493 pr_err("data segment too big %d\n", desc->fd_lens[3]);
500 static void encode_epilogue_plain(struct ceph_connection *con, bool aborted)
502 con->v2.out_epil.late_status = aborted ? FRAME_LATE_STATUS_ABORTED :
503 FRAME_LATE_STATUS_COMPLETE;
504 cpu_to_le32s(&con->v2.out_epil.front_crc);
505 cpu_to_le32s(&con->v2.out_epil.middle_crc);
506 cpu_to_le32s(&con->v2.out_epil.data_crc);
509 static void encode_epilogue_secure(struct ceph_connection *con, bool aborted)
511 memset(&con->v2.out_epil, 0, sizeof(con->v2.out_epil));
512 con->v2.out_epil.late_status = aborted ? FRAME_LATE_STATUS_ABORTED :
513 FRAME_LATE_STATUS_COMPLETE;
516 static int decode_epilogue(void *p, u32 *front_crc, u32 *middle_crc,
521 late_status = ceph_decode_8(&p);
522 if ((late_status & FRAME_LATE_STATUS_ABORTED_MASK) !=
523 FRAME_LATE_STATUS_COMPLETE) {
524 /* we should never get an aborted message as client */
525 pr_err("bad late_status 0x%x\n", late_status);
529 if (front_crc && middle_crc && data_crc) {
530 *front_crc = ceph_decode_32(&p);
531 *middle_crc = ceph_decode_32(&p);
532 *data_crc = ceph_decode_32(&p);
538 static void fill_header(struct ceph_msg_header *hdr,
539 const struct ceph_msg_header2 *hdr2,
540 int front_len, int middle_len, int data_len,
541 const struct ceph_entity_name *peer_name)
543 hdr->seq = hdr2->seq;
544 hdr->tid = hdr2->tid;
545 hdr->type = hdr2->type;
546 hdr->priority = hdr2->priority;
547 hdr->version = hdr2->version;
548 hdr->front_len = cpu_to_le32(front_len);
549 hdr->middle_len = cpu_to_le32(middle_len);
550 hdr->data_len = cpu_to_le32(data_len);
551 hdr->data_off = hdr2->data_off;
552 hdr->src = *peer_name;
553 hdr->compat_version = hdr2->compat_version;
558 static void fill_header2(struct ceph_msg_header2 *hdr2,
559 const struct ceph_msg_header *hdr, u64 ack_seq)
561 hdr2->seq = hdr->seq;
562 hdr2->tid = hdr->tid;
563 hdr2->type = hdr->type;
564 hdr2->priority = hdr->priority;
565 hdr2->version = hdr->version;
566 hdr2->data_pre_padding_len = 0;
567 hdr2->data_off = hdr->data_off;
568 hdr2->ack_seq = cpu_to_le64(ack_seq);
570 hdr2->compat_version = hdr->compat_version;
574 static int verify_control_crc(struct ceph_connection *con)
576 int ctrl_len = con->v2.in_desc.fd_lens[0];
577 u32 crc, expected_crc;
579 WARN_ON(con->v2.in_kvecs[0].iov_len != ctrl_len);
580 WARN_ON(con->v2.in_kvecs[1].iov_len != CEPH_CRC_LEN);
582 crc = crc32c(-1, con->v2.in_kvecs[0].iov_base, ctrl_len);
583 expected_crc = get_unaligned_le32(con->v2.in_kvecs[1].iov_base);
584 if (crc != expected_crc) {
585 pr_err("bad control crc, calculated %u, expected %u\n",
593 static int verify_epilogue_crcs(struct ceph_connection *con, u32 front_crc,
594 u32 middle_crc, u32 data_crc)
596 if (front_len(con->in_msg)) {
597 con->in_front_crc = crc32c(-1, con->in_msg->front.iov_base,
598 front_len(con->in_msg));
600 WARN_ON(!middle_len(con->in_msg) && !data_len(con->in_msg));
601 con->in_front_crc = -1;
604 if (middle_len(con->in_msg))
605 con->in_middle_crc = crc32c(-1,
606 con->in_msg->middle->vec.iov_base,
607 middle_len(con->in_msg));
608 else if (data_len(con->in_msg))
609 con->in_middle_crc = -1;
611 con->in_middle_crc = 0;
613 if (!data_len(con->in_msg))
614 con->in_data_crc = 0;
616 dout("%s con %p msg %p crcs %u %u %u\n", __func__, con, con->in_msg,
617 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
619 if (con->in_front_crc != front_crc) {
620 pr_err("bad front crc, calculated %u, expected %u\n",
621 con->in_front_crc, front_crc);
624 if (con->in_middle_crc != middle_crc) {
625 pr_err("bad middle crc, calculated %u, expected %u\n",
626 con->in_middle_crc, middle_crc);
629 if (con->in_data_crc != data_crc) {
630 pr_err("bad data crc, calculated %u, expected %u\n",
631 con->in_data_crc, data_crc);
638 static int setup_crypto(struct ceph_connection *con,
639 const u8 *session_key, int session_key_len,
640 const u8 *con_secret, int con_secret_len)
642 unsigned int noio_flag;
645 dout("%s con %p con_mode %d session_key_len %d con_secret_len %d\n",
646 __func__, con, con->v2.con_mode, session_key_len, con_secret_len);
647 WARN_ON(con->v2.hmac_tfm || con->v2.gcm_tfm || con->v2.gcm_req);
649 if (con->v2.con_mode != CEPH_CON_MODE_CRC &&
650 con->v2.con_mode != CEPH_CON_MODE_SECURE) {
651 pr_err("bad con_mode %d\n", con->v2.con_mode);
655 if (!session_key_len) {
656 WARN_ON(con->v2.con_mode != CEPH_CON_MODE_CRC);
657 WARN_ON(con_secret_len);
658 return 0; /* auth_none */
661 noio_flag = memalloc_noio_save();
662 con->v2.hmac_tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
663 memalloc_noio_restore(noio_flag);
664 if (IS_ERR(con->v2.hmac_tfm)) {
665 ret = PTR_ERR(con->v2.hmac_tfm);
666 con->v2.hmac_tfm = NULL;
667 pr_err("failed to allocate hmac tfm context: %d\n", ret);
671 WARN_ON((unsigned long)session_key &
672 crypto_shash_alignmask(con->v2.hmac_tfm));
673 ret = crypto_shash_setkey(con->v2.hmac_tfm, session_key,
676 pr_err("failed to set hmac key: %d\n", ret);
680 if (con->v2.con_mode == CEPH_CON_MODE_CRC) {
681 WARN_ON(con_secret_len);
682 return 0; /* auth_x, plain mode */
685 if (con_secret_len < CEPH_GCM_KEY_LEN + 2 * CEPH_GCM_IV_LEN) {
686 pr_err("con_secret too small %d\n", con_secret_len);
690 noio_flag = memalloc_noio_save();
691 con->v2.gcm_tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
692 memalloc_noio_restore(noio_flag);
693 if (IS_ERR(con->v2.gcm_tfm)) {
694 ret = PTR_ERR(con->v2.gcm_tfm);
695 con->v2.gcm_tfm = NULL;
696 pr_err("failed to allocate gcm tfm context: %d\n", ret);
700 WARN_ON((unsigned long)con_secret &
701 crypto_aead_alignmask(con->v2.gcm_tfm));
702 ret = crypto_aead_setkey(con->v2.gcm_tfm, con_secret, CEPH_GCM_KEY_LEN);
704 pr_err("failed to set gcm key: %d\n", ret);
708 WARN_ON(crypto_aead_ivsize(con->v2.gcm_tfm) != CEPH_GCM_IV_LEN);
709 ret = crypto_aead_setauthsize(con->v2.gcm_tfm, CEPH_GCM_TAG_LEN);
711 pr_err("failed to set gcm tag size: %d\n", ret);
715 con->v2.gcm_req = aead_request_alloc(con->v2.gcm_tfm, GFP_NOIO);
716 if (!con->v2.gcm_req) {
717 pr_err("failed to allocate gcm request\n");
721 crypto_init_wait(&con->v2.gcm_wait);
722 aead_request_set_callback(con->v2.gcm_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
723 crypto_req_done, &con->v2.gcm_wait);
725 memcpy(&con->v2.in_gcm_nonce, con_secret + CEPH_GCM_KEY_LEN,
727 memcpy(&con->v2.out_gcm_nonce,
728 con_secret + CEPH_GCM_KEY_LEN + CEPH_GCM_IV_LEN,
730 return 0; /* auth_x, secure mode */
733 static int hmac_sha256(struct ceph_connection *con, const struct kvec *kvecs,
734 int kvec_cnt, u8 *hmac)
736 SHASH_DESC_ON_STACK(desc, con->v2.hmac_tfm); /* tfm arg is ignored */
740 dout("%s con %p hmac_tfm %p kvec_cnt %d\n", __func__, con,
741 con->v2.hmac_tfm, kvec_cnt);
743 if (!con->v2.hmac_tfm) {
744 memset(hmac, 0, SHA256_DIGEST_SIZE);
745 return 0; /* auth_none */
748 desc->tfm = con->v2.hmac_tfm;
749 ret = crypto_shash_init(desc);
753 for (i = 0; i < kvec_cnt; i++) {
754 WARN_ON((unsigned long)kvecs[i].iov_base &
755 crypto_shash_alignmask(con->v2.hmac_tfm));
756 ret = crypto_shash_update(desc, kvecs[i].iov_base,
762 ret = crypto_shash_final(desc, hmac);
765 shash_desc_zero(desc);
766 return ret; /* auth_x, both plain and secure modes */
769 static void gcm_inc_nonce(struct ceph_gcm_nonce *nonce)
773 counter = le64_to_cpu(nonce->counter);
774 nonce->counter = cpu_to_le64(counter + 1);
777 static int gcm_crypt(struct ceph_connection *con, bool encrypt,
778 struct scatterlist *src, struct scatterlist *dst,
781 struct ceph_gcm_nonce *nonce;
784 nonce = encrypt ? &con->v2.out_gcm_nonce : &con->v2.in_gcm_nonce;
786 aead_request_set_ad(con->v2.gcm_req, 0); /* no AAD */
787 aead_request_set_crypt(con->v2.gcm_req, src, dst, src_len, (u8 *)nonce);
788 ret = crypto_wait_req(encrypt ? crypto_aead_encrypt(con->v2.gcm_req) :
789 crypto_aead_decrypt(con->v2.gcm_req),
794 gcm_inc_nonce(nonce);
798 static void get_bvec_at(struct ceph_msg_data_cursor *cursor,
804 WARN_ON(!cursor->total_resid);
806 /* skip zero-length data items */
807 while (!cursor->resid)
808 ceph_msg_data_advance(cursor, 0);
810 /* get a piece of data, cursor isn't advanced */
811 page = ceph_msg_data_next(cursor, &off, &len);
812 bvec_set_page(bv, page, len, off);
815 static int calc_sg_cnt(void *buf, int buf_len)
822 sg_cnt = need_padding(buf_len) ? 1 : 0;
823 if (is_vmalloc_addr(buf)) {
824 WARN_ON(offset_in_page(buf));
825 sg_cnt += PAGE_ALIGN(buf_len) >> PAGE_SHIFT;
833 static int calc_sg_cnt_cursor(struct ceph_msg_data_cursor *cursor)
835 int data_len = cursor->total_resid;
842 sg_cnt = need_padding(data_len) ? 1 : 0;
844 get_bvec_at(cursor, &bv);
847 ceph_msg_data_advance(cursor, bv.bv_len);
848 } while (cursor->total_resid);
853 static void init_sgs(struct scatterlist **sg, void *buf, int buf_len, u8 *pad)
855 void *end = buf + buf_len;
863 if (is_vmalloc_addr(buf)) {
866 page = vmalloc_to_page(p);
867 len = min_t(int, end - p, PAGE_SIZE);
868 WARN_ON(!page || !len || offset_in_page(p));
869 sg_set_page(*sg, page, len, 0);
874 sg_set_buf(*sg, buf, buf_len);
878 if (need_padding(buf_len)) {
879 sg_set_buf(*sg, pad, padding_len(buf_len));
884 static void init_sgs_cursor(struct scatterlist **sg,
885 struct ceph_msg_data_cursor *cursor, u8 *pad)
887 int data_len = cursor->total_resid;
894 get_bvec_at(cursor, &bv);
895 sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset);
898 ceph_msg_data_advance(cursor, bv.bv_len);
899 } while (cursor->total_resid);
901 if (need_padding(data_len)) {
902 sg_set_buf(*sg, pad, padding_len(data_len));
907 static int setup_message_sgs(struct sg_table *sgt, struct ceph_msg *msg,
908 u8 *front_pad, u8 *middle_pad, u8 *data_pad,
909 void *epilogue, bool add_tag)
911 struct ceph_msg_data_cursor cursor;
912 struct scatterlist *cur_sg;
916 if (!front_len(msg) && !middle_len(msg) && !data_len(msg))
919 sg_cnt = 1; /* epilogue + [auth tag] */
921 sg_cnt += calc_sg_cnt(msg->front.iov_base,
924 sg_cnt += calc_sg_cnt(msg->middle->vec.iov_base,
927 ceph_msg_data_cursor_init(&cursor, msg, data_len(msg));
928 sg_cnt += calc_sg_cnt_cursor(&cursor);
931 ret = sg_alloc_table(sgt, sg_cnt, GFP_NOIO);
937 init_sgs(&cur_sg, msg->front.iov_base, front_len(msg),
940 init_sgs(&cur_sg, msg->middle->vec.iov_base, middle_len(msg),
943 ceph_msg_data_cursor_init(&cursor, msg, data_len(msg));
944 init_sgs_cursor(&cur_sg, &cursor, data_pad);
947 WARN_ON(!sg_is_last(cur_sg));
948 sg_set_buf(cur_sg, epilogue,
949 CEPH_GCM_BLOCK_LEN + (add_tag ? CEPH_GCM_TAG_LEN : 0));
953 static int decrypt_preamble(struct ceph_connection *con)
955 struct scatterlist sg;
957 sg_init_one(&sg, con->v2.in_buf, CEPH_PREAMBLE_SECURE_LEN);
958 return gcm_crypt(con, false, &sg, &sg, CEPH_PREAMBLE_SECURE_LEN);
961 static int decrypt_control_remainder(struct ceph_connection *con)
963 int ctrl_len = con->v2.in_desc.fd_lens[0];
964 int rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
965 int pt_len = padding_len(rem_len) + CEPH_GCM_TAG_LEN;
966 struct scatterlist sgs[2];
968 WARN_ON(con->v2.in_kvecs[0].iov_len != rem_len);
969 WARN_ON(con->v2.in_kvecs[1].iov_len != pt_len);
971 sg_init_table(sgs, 2);
972 sg_set_buf(&sgs[0], con->v2.in_kvecs[0].iov_base, rem_len);
973 sg_set_buf(&sgs[1], con->v2.in_buf, pt_len);
975 return gcm_crypt(con, false, sgs, sgs,
976 padded_len(rem_len) + CEPH_GCM_TAG_LEN);
979 static int decrypt_tail(struct ceph_connection *con)
981 struct sg_table enc_sgt = {};
982 struct sg_table sgt = {};
986 tail_len = tail_onwire_len(con->in_msg, true);
987 ret = sg_alloc_table_from_pages(&enc_sgt, con->v2.in_enc_pages,
988 con->v2.in_enc_page_cnt, 0, tail_len,
993 ret = setup_message_sgs(&sgt, con->in_msg, FRONT_PAD(con->v2.in_buf),
994 MIDDLE_PAD(con->v2.in_buf), DATA_PAD(con->v2.in_buf),
995 con->v2.in_buf, true);
999 dout("%s con %p msg %p enc_page_cnt %d sg_cnt %d\n", __func__, con,
1000 con->in_msg, con->v2.in_enc_page_cnt, sgt.orig_nents);
1001 ret = gcm_crypt(con, false, enc_sgt.sgl, sgt.sgl, tail_len);
1005 WARN_ON(!con->v2.in_enc_page_cnt);
1006 ceph_release_page_vector(con->v2.in_enc_pages,
1007 con->v2.in_enc_page_cnt);
1008 con->v2.in_enc_pages = NULL;
1009 con->v2.in_enc_page_cnt = 0;
1012 sg_free_table(&sgt);
1013 sg_free_table(&enc_sgt);
1017 static int prepare_banner(struct ceph_connection *con)
1019 int buf_len = CEPH_BANNER_V2_LEN + 2 + 8 + 8;
1022 buf = alloc_conn_buf(con, buf_len);
1027 ceph_encode_copy(&p, CEPH_BANNER_V2, CEPH_BANNER_V2_LEN);
1028 ceph_encode_16(&p, sizeof(u64) + sizeof(u64));
1029 ceph_encode_64(&p, CEPH_MSGR2_SUPPORTED_FEATURES);
1030 ceph_encode_64(&p, CEPH_MSGR2_REQUIRED_FEATURES);
1031 WARN_ON(p != buf + buf_len);
1033 add_out_kvec(con, buf, buf_len);
1034 add_out_sign_kvec(con, buf, buf_len);
1035 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
1042 * control body (ctrl_len bytes)
1043 * space for control crc
1045 * extdata (optional):
1046 * control body (extdata_len bytes)
1048 * Compute control crc and gather base and extdata into:
1051 * control body (ctrl_len + extdata_len bytes)
1054 * Preamble should already be encoded at the start of base.
1056 static void prepare_head_plain(struct ceph_connection *con, void *base,
1057 int ctrl_len, void *extdata, int extdata_len,
1060 int base_len = CEPH_PREAMBLE_LEN + ctrl_len + CEPH_CRC_LEN;
1061 void *crcp = base + base_len - CEPH_CRC_LEN;
1064 crc = crc32c(-1, CTRL_BODY(base), ctrl_len);
1066 crc = crc32c(crc, extdata, extdata_len);
1067 put_unaligned_le32(crc, crcp);
1070 add_out_kvec(con, base, base_len);
1072 add_out_sign_kvec(con, base, base_len);
1076 add_out_kvec(con, base, crcp - base);
1077 add_out_kvec(con, extdata, extdata_len);
1078 add_out_kvec(con, crcp, CEPH_CRC_LEN);
1080 add_out_sign_kvec(con, base, crcp - base);
1081 add_out_sign_kvec(con, extdata, extdata_len);
1082 add_out_sign_kvec(con, crcp, CEPH_CRC_LEN);
1086 static int prepare_head_secure_small(struct ceph_connection *con,
1087 void *base, int ctrl_len)
1089 struct scatterlist sg;
1092 /* inline buffer padding? */
1093 if (ctrl_len < CEPH_PREAMBLE_INLINE_LEN)
1094 memset(CTRL_BODY(base) + ctrl_len, 0,
1095 CEPH_PREAMBLE_INLINE_LEN - ctrl_len);
1097 sg_init_one(&sg, base, CEPH_PREAMBLE_SECURE_LEN);
1098 ret = gcm_crypt(con, true, &sg, &sg,
1099 CEPH_PREAMBLE_SECURE_LEN - CEPH_GCM_TAG_LEN);
1103 add_out_kvec(con, base, CEPH_PREAMBLE_SECURE_LEN);
1110 * control body (ctrl_len bytes)
1111 * space for padding, if needed
1112 * space for control remainder auth tag
1113 * space for preamble auth tag
1115 * Encrypt preamble and the inline portion, then encrypt the remainder
1119 * control body (48 bytes)
1121 * control body (ctrl_len - 48 bytes)
1122 * zero padding, if needed
1123 * control remainder auth tag
1125 * Preamble should already be encoded at the start of base.
1127 static int prepare_head_secure_big(struct ceph_connection *con,
1128 void *base, int ctrl_len)
1130 int rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
1131 void *rem = CTRL_BODY(base) + CEPH_PREAMBLE_INLINE_LEN;
1132 void *rem_tag = rem + padded_len(rem_len);
1133 void *pmbl_tag = rem_tag + CEPH_GCM_TAG_LEN;
1134 struct scatterlist sgs[2];
1137 sg_init_table(sgs, 2);
1138 sg_set_buf(&sgs[0], base, rem - base);
1139 sg_set_buf(&sgs[1], pmbl_tag, CEPH_GCM_TAG_LEN);
1140 ret = gcm_crypt(con, true, sgs, sgs, rem - base);
1144 /* control remainder padding? */
1145 if (need_padding(rem_len))
1146 memset(rem + rem_len, 0, padding_len(rem_len));
1148 sg_init_one(&sgs[0], rem, pmbl_tag - rem);
1149 ret = gcm_crypt(con, true, sgs, sgs, rem_tag - rem);
1153 add_out_kvec(con, base, rem - base);
1154 add_out_kvec(con, pmbl_tag, CEPH_GCM_TAG_LEN);
1155 add_out_kvec(con, rem, pmbl_tag - rem);
1159 static int __prepare_control(struct ceph_connection *con, int tag,
1160 void *base, int ctrl_len, void *extdata,
1161 int extdata_len, bool to_be_signed)
1163 int total_len = ctrl_len + extdata_len;
1164 struct ceph_frame_desc desc;
1167 dout("%s con %p tag %d len %d (%d+%d)\n", __func__, con, tag,
1168 total_len, ctrl_len, extdata_len);
1170 /* extdata may be vmalloc'ed but not base */
1171 if (WARN_ON(is_vmalloc_addr(base) || !ctrl_len))
1174 init_frame_desc(&desc, tag, &total_len, 1);
1175 encode_preamble(&desc, base);
1177 if (con_secure(con)) {
1178 if (WARN_ON(extdata_len || to_be_signed))
1181 if (ctrl_len <= CEPH_PREAMBLE_INLINE_LEN)
1182 /* fully inlined, inline buffer may need padding */
1183 ret = prepare_head_secure_small(con, base, ctrl_len);
1185 /* partially inlined, inline buffer is full */
1186 ret = prepare_head_secure_big(con, base, ctrl_len);
1190 prepare_head_plain(con, base, ctrl_len, extdata, extdata_len,
1194 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
1198 static int prepare_control(struct ceph_connection *con, int tag,
1199 void *base, int ctrl_len)
1201 return __prepare_control(con, tag, base, ctrl_len, NULL, 0, false);
1204 static int prepare_hello(struct ceph_connection *con)
1209 ctrl_len = 1 + ceph_entity_addr_encoding_len(&con->peer_addr);
1210 buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, false));
1215 ceph_encode_8(&p, CEPH_ENTITY_TYPE_CLIENT);
1216 ceph_encode_entity_addr(&p, &con->peer_addr);
1217 WARN_ON(p != CTRL_BODY(buf) + ctrl_len);
1219 return __prepare_control(con, FRAME_TAG_HELLO, buf, ctrl_len,
1223 /* so that head_onwire_len(AUTH_BUF_LEN, false) is 512 */
1224 #define AUTH_BUF_LEN (512 - CEPH_CRC_LEN - CEPH_PREAMBLE_PLAIN_LEN)
1226 static int prepare_auth_request(struct ceph_connection *con)
1228 void *authorizer, *authorizer_copy;
1229 int ctrl_len, authorizer_len;
1233 ctrl_len = AUTH_BUF_LEN;
1234 buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, false));
1238 mutex_unlock(&con->mutex);
1239 ret = con->ops->get_auth_request(con, CTRL_BODY(buf), &ctrl_len,
1240 &authorizer, &authorizer_len);
1241 mutex_lock(&con->mutex);
1242 if (con->state != CEPH_CON_S_V2_HELLO) {
1243 dout("%s con %p state changed to %d\n", __func__, con,
1248 dout("%s con %p get_auth_request ret %d\n", __func__, con, ret);
1252 authorizer_copy = alloc_conn_buf(con, authorizer_len);
1253 if (!authorizer_copy)
1256 memcpy(authorizer_copy, authorizer, authorizer_len);
1258 return __prepare_control(con, FRAME_TAG_AUTH_REQUEST, buf, ctrl_len,
1259 authorizer_copy, authorizer_len, true);
1262 static int prepare_auth_request_more(struct ceph_connection *con,
1263 void *reply, int reply_len)
1265 int ctrl_len, authorizer_len;
1270 ctrl_len = AUTH_BUF_LEN;
1271 buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, false));
1275 mutex_unlock(&con->mutex);
1276 ret = con->ops->handle_auth_reply_more(con, reply, reply_len,
1277 CTRL_BODY(buf), &ctrl_len,
1278 &authorizer, &authorizer_len);
1279 mutex_lock(&con->mutex);
1280 if (con->state != CEPH_CON_S_V2_AUTH) {
1281 dout("%s con %p state changed to %d\n", __func__, con,
1286 dout("%s con %p handle_auth_reply_more ret %d\n", __func__, con, ret);
1290 return __prepare_control(con, FRAME_TAG_AUTH_REQUEST_MORE, buf,
1291 ctrl_len, authorizer, authorizer_len, true);
1294 static int prepare_auth_signature(struct ceph_connection *con)
1299 buf = alloc_conn_buf(con, head_onwire_len(SHA256_DIGEST_SIZE,
1304 ret = hmac_sha256(con, con->v2.in_sign_kvecs, con->v2.in_sign_kvec_cnt,
1309 return prepare_control(con, FRAME_TAG_AUTH_SIGNATURE, buf,
1310 SHA256_DIGEST_SIZE);
1313 static int prepare_client_ident(struct ceph_connection *con)
1315 struct ceph_entity_addr *my_addr = &con->msgr->inst.addr;
1316 struct ceph_client *client = from_msgr(con->msgr);
1317 u64 global_id = ceph_client_gid(client);
1321 WARN_ON(con->v2.server_cookie);
1322 WARN_ON(con->v2.connect_seq);
1323 WARN_ON(con->v2.peer_global_seq);
1325 if (!con->v2.client_cookie) {
1327 get_random_bytes(&con->v2.client_cookie,
1328 sizeof(con->v2.client_cookie));
1329 } while (!con->v2.client_cookie);
1330 dout("%s con %p generated cookie 0x%llx\n", __func__, con,
1331 con->v2.client_cookie);
1333 dout("%s con %p cookie already set 0x%llx\n", __func__, con,
1334 con->v2.client_cookie);
1337 dout("%s con %p my_addr %s/%u peer_addr %s/%u global_id %llu global_seq %llu features 0x%llx required_features 0x%llx cookie 0x%llx\n",
1338 __func__, con, ceph_pr_addr(my_addr), le32_to_cpu(my_addr->nonce),
1339 ceph_pr_addr(&con->peer_addr), le32_to_cpu(con->peer_addr.nonce),
1340 global_id, con->v2.global_seq, client->supported_features,
1341 client->required_features, con->v2.client_cookie);
1343 ctrl_len = 1 + 4 + ceph_entity_addr_encoding_len(my_addr) +
1344 ceph_entity_addr_encoding_len(&con->peer_addr) + 6 * 8;
1345 buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, con_secure(con)));
1350 ceph_encode_8(&p, 2); /* addrvec marker */
1351 ceph_encode_32(&p, 1); /* addr_cnt */
1352 ceph_encode_entity_addr(&p, my_addr);
1353 ceph_encode_entity_addr(&p, &con->peer_addr);
1354 ceph_encode_64(&p, global_id);
1355 ceph_encode_64(&p, con->v2.global_seq);
1356 ceph_encode_64(&p, client->supported_features);
1357 ceph_encode_64(&p, client->required_features);
1358 ceph_encode_64(&p, 0); /* flags */
1359 ceph_encode_64(&p, con->v2.client_cookie);
1360 WARN_ON(p != CTRL_BODY(buf) + ctrl_len);
1362 return prepare_control(con, FRAME_TAG_CLIENT_IDENT, buf, ctrl_len);
1365 static int prepare_session_reconnect(struct ceph_connection *con)
1367 struct ceph_entity_addr *my_addr = &con->msgr->inst.addr;
1371 WARN_ON(!con->v2.client_cookie);
1372 WARN_ON(!con->v2.server_cookie);
1373 WARN_ON(!con->v2.connect_seq);
1374 WARN_ON(!con->v2.peer_global_seq);
1376 dout("%s con %p my_addr %s/%u client_cookie 0x%llx server_cookie 0x%llx global_seq %llu connect_seq %llu in_seq %llu\n",
1377 __func__, con, ceph_pr_addr(my_addr), le32_to_cpu(my_addr->nonce),
1378 con->v2.client_cookie, con->v2.server_cookie, con->v2.global_seq,
1379 con->v2.connect_seq, con->in_seq);
1381 ctrl_len = 1 + 4 + ceph_entity_addr_encoding_len(my_addr) + 5 * 8;
1382 buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, con_secure(con)));
1387 ceph_encode_8(&p, 2); /* entity_addrvec_t marker */
1388 ceph_encode_32(&p, 1); /* my_addrs len */
1389 ceph_encode_entity_addr(&p, my_addr);
1390 ceph_encode_64(&p, con->v2.client_cookie);
1391 ceph_encode_64(&p, con->v2.server_cookie);
1392 ceph_encode_64(&p, con->v2.global_seq);
1393 ceph_encode_64(&p, con->v2.connect_seq);
1394 ceph_encode_64(&p, con->in_seq);
1395 WARN_ON(p != CTRL_BODY(buf) + ctrl_len);
1397 return prepare_control(con, FRAME_TAG_SESSION_RECONNECT, buf, ctrl_len);
1400 static int prepare_keepalive2(struct ceph_connection *con)
1402 struct ceph_timespec *ts = CTRL_BODY(con->v2.out_buf);
1403 struct timespec64 now;
1405 ktime_get_real_ts64(&now);
1406 dout("%s con %p timestamp %lld.%09ld\n", __func__, con, now.tv_sec,
1409 ceph_encode_timespec64(ts, &now);
1411 reset_out_kvecs(con);
1412 return prepare_control(con, FRAME_TAG_KEEPALIVE2, con->v2.out_buf,
1413 sizeof(struct ceph_timespec));
1416 static int prepare_ack(struct ceph_connection *con)
1420 dout("%s con %p in_seq_acked %llu -> %llu\n", __func__, con,
1421 con->in_seq_acked, con->in_seq);
1422 con->in_seq_acked = con->in_seq;
1424 p = CTRL_BODY(con->v2.out_buf);
1425 ceph_encode_64(&p, con->in_seq_acked);
1427 reset_out_kvecs(con);
1428 return prepare_control(con, FRAME_TAG_ACK, con->v2.out_buf, 8);
1431 static void prepare_epilogue_plain(struct ceph_connection *con, bool aborted)
1433 dout("%s con %p msg %p aborted %d crcs %u %u %u\n", __func__, con,
1434 con->out_msg, aborted, con->v2.out_epil.front_crc,
1435 con->v2.out_epil.middle_crc, con->v2.out_epil.data_crc);
1437 encode_epilogue_plain(con, aborted);
1438 add_out_kvec(con, &con->v2.out_epil, CEPH_EPILOGUE_PLAIN_LEN);
1442 * For "used" empty segments, crc is -1. For unused (trailing)
1443 * segments, crc is 0.
1445 static void prepare_message_plain(struct ceph_connection *con)
1447 struct ceph_msg *msg = con->out_msg;
1449 prepare_head_plain(con, con->v2.out_buf,
1450 sizeof(struct ceph_msg_header2), NULL, 0, false);
1452 if (!front_len(msg) && !middle_len(msg)) {
1453 if (!data_len(msg)) {
1455 * Empty message: once the head is written,
1456 * we are done -- there is no epilogue.
1458 con->v2.out_state = OUT_S_FINISH_MESSAGE;
1462 con->v2.out_epil.front_crc = -1;
1463 con->v2.out_epil.middle_crc = -1;
1464 con->v2.out_state = OUT_S_QUEUE_DATA;
1468 if (front_len(msg)) {
1469 con->v2.out_epil.front_crc = crc32c(-1, msg->front.iov_base,
1471 add_out_kvec(con, msg->front.iov_base, front_len(msg));
1473 /* middle (at least) is there, checked above */
1474 con->v2.out_epil.front_crc = -1;
1477 if (middle_len(msg)) {
1478 con->v2.out_epil.middle_crc =
1479 crc32c(-1, msg->middle->vec.iov_base, middle_len(msg));
1480 add_out_kvec(con, msg->middle->vec.iov_base, middle_len(msg));
1482 con->v2.out_epil.middle_crc = data_len(msg) ? -1 : 0;
1485 if (data_len(msg)) {
1486 con->v2.out_state = OUT_S_QUEUE_DATA;
1488 con->v2.out_epil.data_crc = 0;
1489 prepare_epilogue_plain(con, false);
1490 con->v2.out_state = OUT_S_FINISH_MESSAGE;
1495 * Unfortunately the kernel crypto API doesn't support streaming
1496 * (piecewise) operation for AEAD algorithms, so we can't get away
1497 * with a fixed size buffer and a couple sgs. Instead, we have to
1498 * allocate pages for the entire tail of the message (currently up
1499 * to ~32M) and two sgs arrays (up to ~256K each)...
1501 static int prepare_message_secure(struct ceph_connection *con)
1503 void *zerop = page_address(ceph_zero_page);
1504 struct sg_table enc_sgt = {};
1505 struct sg_table sgt = {};
1506 struct page **enc_pages;
1511 ret = prepare_head_secure_small(con, con->v2.out_buf,
1512 sizeof(struct ceph_msg_header2));
1516 tail_len = tail_onwire_len(con->out_msg, true);
1519 * Empty message: once the head is written,
1520 * we are done -- there is no epilogue.
1522 con->v2.out_state = OUT_S_FINISH_MESSAGE;
1526 encode_epilogue_secure(con, false);
1527 ret = setup_message_sgs(&sgt, con->out_msg, zerop, zerop, zerop,
1528 &con->v2.out_epil, false);
1532 enc_page_cnt = calc_pages_for(0, tail_len);
1533 enc_pages = ceph_alloc_page_vector(enc_page_cnt, GFP_NOIO);
1534 if (IS_ERR(enc_pages)) {
1535 ret = PTR_ERR(enc_pages);
1539 WARN_ON(con->v2.out_enc_pages || con->v2.out_enc_page_cnt);
1540 con->v2.out_enc_pages = enc_pages;
1541 con->v2.out_enc_page_cnt = enc_page_cnt;
1542 con->v2.out_enc_resid = tail_len;
1543 con->v2.out_enc_i = 0;
1545 ret = sg_alloc_table_from_pages(&enc_sgt, enc_pages, enc_page_cnt,
1546 0, tail_len, GFP_NOIO);
1550 ret = gcm_crypt(con, true, sgt.sgl, enc_sgt.sgl,
1551 tail_len - CEPH_GCM_TAG_LEN);
1555 dout("%s con %p msg %p sg_cnt %d enc_page_cnt %d\n", __func__, con,
1556 con->out_msg, sgt.orig_nents, enc_page_cnt);
1557 con->v2.out_state = OUT_S_QUEUE_ENC_PAGE;
1560 sg_free_table(&sgt);
1561 sg_free_table(&enc_sgt);
1565 static int prepare_message(struct ceph_connection *con)
1568 sizeof(struct ceph_msg_header2),
1569 front_len(con->out_msg),
1570 middle_len(con->out_msg),
1571 data_len(con->out_msg)
1573 struct ceph_frame_desc desc;
1576 dout("%s con %p msg %p logical %d+%d+%d+%d\n", __func__, con,
1577 con->out_msg, lens[0], lens[1], lens[2], lens[3]);
1579 if (con->in_seq > con->in_seq_acked) {
1580 dout("%s con %p in_seq_acked %llu -> %llu\n", __func__, con,
1581 con->in_seq_acked, con->in_seq);
1582 con->in_seq_acked = con->in_seq;
1585 reset_out_kvecs(con);
1586 init_frame_desc(&desc, FRAME_TAG_MESSAGE, lens, 4);
1587 encode_preamble(&desc, con->v2.out_buf);
1588 fill_header2(CTRL_BODY(con->v2.out_buf), &con->out_msg->hdr,
1591 if (con_secure(con)) {
1592 ret = prepare_message_secure(con);
1596 prepare_message_plain(con);
1599 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
1603 static int prepare_read_banner_prefix(struct ceph_connection *con)
1607 buf = alloc_conn_buf(con, CEPH_BANNER_V2_PREFIX_LEN);
1611 reset_in_kvecs(con);
1612 add_in_kvec(con, buf, CEPH_BANNER_V2_PREFIX_LEN);
1613 add_in_sign_kvec(con, buf, CEPH_BANNER_V2_PREFIX_LEN);
1614 con->state = CEPH_CON_S_V2_BANNER_PREFIX;
1618 static int prepare_read_banner_payload(struct ceph_connection *con,
1623 buf = alloc_conn_buf(con, payload_len);
1627 reset_in_kvecs(con);
1628 add_in_kvec(con, buf, payload_len);
1629 add_in_sign_kvec(con, buf, payload_len);
1630 con->state = CEPH_CON_S_V2_BANNER_PAYLOAD;
1634 static void prepare_read_preamble(struct ceph_connection *con)
1636 reset_in_kvecs(con);
1637 add_in_kvec(con, con->v2.in_buf,
1638 con_secure(con) ? CEPH_PREAMBLE_SECURE_LEN :
1639 CEPH_PREAMBLE_PLAIN_LEN);
1640 con->v2.in_state = IN_S_HANDLE_PREAMBLE;
1643 static int prepare_read_control(struct ceph_connection *con)
1645 int ctrl_len = con->v2.in_desc.fd_lens[0];
1649 reset_in_kvecs(con);
1650 if (con->state == CEPH_CON_S_V2_HELLO ||
1651 con->state == CEPH_CON_S_V2_AUTH) {
1652 head_len = head_onwire_len(ctrl_len, false);
1653 buf = alloc_conn_buf(con, head_len);
1657 /* preserve preamble */
1658 memcpy(buf, con->v2.in_buf, CEPH_PREAMBLE_LEN);
1660 add_in_kvec(con, CTRL_BODY(buf), ctrl_len);
1661 add_in_kvec(con, CTRL_BODY(buf) + ctrl_len, CEPH_CRC_LEN);
1662 add_in_sign_kvec(con, buf, head_len);
1664 if (ctrl_len > CEPH_PREAMBLE_INLINE_LEN) {
1665 buf = alloc_conn_buf(con, ctrl_len);
1669 add_in_kvec(con, buf, ctrl_len);
1671 add_in_kvec(con, CTRL_BODY(con->v2.in_buf), ctrl_len);
1673 add_in_kvec(con, con->v2.in_buf, CEPH_CRC_LEN);
1675 con->v2.in_state = IN_S_HANDLE_CONTROL;
1679 static int prepare_read_control_remainder(struct ceph_connection *con)
1681 int ctrl_len = con->v2.in_desc.fd_lens[0];
1682 int rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
1685 buf = alloc_conn_buf(con, ctrl_len);
1689 memcpy(buf, CTRL_BODY(con->v2.in_buf), CEPH_PREAMBLE_INLINE_LEN);
1691 reset_in_kvecs(con);
1692 add_in_kvec(con, buf + CEPH_PREAMBLE_INLINE_LEN, rem_len);
1693 add_in_kvec(con, con->v2.in_buf,
1694 padding_len(rem_len) + CEPH_GCM_TAG_LEN);
1695 con->v2.in_state = IN_S_HANDLE_CONTROL_REMAINDER;
1699 static int prepare_read_data(struct ceph_connection *con)
1703 con->in_data_crc = -1;
1704 ceph_msg_data_cursor_init(&con->v2.in_cursor, con->in_msg,
1705 data_len(con->in_msg));
1707 get_bvec_at(&con->v2.in_cursor, &bv);
1708 if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) {
1709 if (unlikely(!con->bounce_page)) {
1710 con->bounce_page = alloc_page(GFP_NOIO);
1711 if (!con->bounce_page) {
1712 pr_err("failed to allocate bounce page\n");
1717 bv.bv_page = con->bounce_page;
1720 set_in_bvec(con, &bv);
1721 con->v2.in_state = IN_S_PREPARE_READ_DATA_CONT;
1725 static void prepare_read_data_cont(struct ceph_connection *con)
1729 if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) {
1730 con->in_data_crc = crc32c(con->in_data_crc,
1731 page_address(con->bounce_page),
1732 con->v2.in_bvec.bv_len);
1734 get_bvec_at(&con->v2.in_cursor, &bv);
1735 memcpy_to_page(bv.bv_page, bv.bv_offset,
1736 page_address(con->bounce_page),
1737 con->v2.in_bvec.bv_len);
1739 con->in_data_crc = ceph_crc32c_page(con->in_data_crc,
1740 con->v2.in_bvec.bv_page,
1741 con->v2.in_bvec.bv_offset,
1742 con->v2.in_bvec.bv_len);
1745 ceph_msg_data_advance(&con->v2.in_cursor, con->v2.in_bvec.bv_len);
1746 if (con->v2.in_cursor.total_resid) {
1747 get_bvec_at(&con->v2.in_cursor, &bv);
1748 if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) {
1749 bv.bv_page = con->bounce_page;
1752 set_in_bvec(con, &bv);
1753 WARN_ON(con->v2.in_state != IN_S_PREPARE_READ_DATA_CONT);
1758 * We've read all data. Prepare to read epilogue.
1760 reset_in_kvecs(con);
1761 add_in_kvec(con, con->v2.in_buf, CEPH_EPILOGUE_PLAIN_LEN);
1762 con->v2.in_state = IN_S_HANDLE_EPILOGUE;
1765 static int prepare_read_tail_plain(struct ceph_connection *con)
1767 struct ceph_msg *msg = con->in_msg;
1769 if (!front_len(msg) && !middle_len(msg)) {
1770 WARN_ON(!data_len(msg));
1771 return prepare_read_data(con);
1774 reset_in_kvecs(con);
1775 if (front_len(msg)) {
1776 add_in_kvec(con, msg->front.iov_base, front_len(msg));
1777 WARN_ON(msg->front.iov_len != front_len(msg));
1779 if (middle_len(msg)) {
1780 add_in_kvec(con, msg->middle->vec.iov_base, middle_len(msg));
1781 WARN_ON(msg->middle->vec.iov_len != middle_len(msg));
1784 if (data_len(msg)) {
1785 con->v2.in_state = IN_S_PREPARE_READ_DATA;
1787 add_in_kvec(con, con->v2.in_buf, CEPH_EPILOGUE_PLAIN_LEN);
1788 con->v2.in_state = IN_S_HANDLE_EPILOGUE;
1793 static void prepare_read_enc_page(struct ceph_connection *con)
1797 dout("%s con %p i %d resid %d\n", __func__, con, con->v2.in_enc_i,
1798 con->v2.in_enc_resid);
1799 WARN_ON(!con->v2.in_enc_resid);
1801 bvec_set_page(&bv, con->v2.in_enc_pages[con->v2.in_enc_i],
1802 min(con->v2.in_enc_resid, (int)PAGE_SIZE), 0);
1804 set_in_bvec(con, &bv);
1806 con->v2.in_enc_resid -= bv.bv_len;
1808 if (con->v2.in_enc_resid) {
1809 con->v2.in_state = IN_S_PREPARE_READ_ENC_PAGE;
1814 * We are set to read the last piece of ciphertext (ending
1815 * with epilogue) + auth tag.
1817 WARN_ON(con->v2.in_enc_i != con->v2.in_enc_page_cnt);
1818 con->v2.in_state = IN_S_HANDLE_EPILOGUE;
1821 static int prepare_read_tail_secure(struct ceph_connection *con)
1823 struct page **enc_pages;
1827 tail_len = tail_onwire_len(con->in_msg, true);
1830 enc_page_cnt = calc_pages_for(0, tail_len);
1831 enc_pages = ceph_alloc_page_vector(enc_page_cnt, GFP_NOIO);
1832 if (IS_ERR(enc_pages))
1833 return PTR_ERR(enc_pages);
1835 WARN_ON(con->v2.in_enc_pages || con->v2.in_enc_page_cnt);
1836 con->v2.in_enc_pages = enc_pages;
1837 con->v2.in_enc_page_cnt = enc_page_cnt;
1838 con->v2.in_enc_resid = tail_len;
1839 con->v2.in_enc_i = 0;
1841 prepare_read_enc_page(con);
1845 static void __finish_skip(struct ceph_connection *con)
1848 prepare_read_preamble(con);
1851 static void prepare_skip_message(struct ceph_connection *con)
1853 struct ceph_frame_desc *desc = &con->v2.in_desc;
1856 dout("%s con %p %d+%d+%d\n", __func__, con, desc->fd_lens[1],
1857 desc->fd_lens[2], desc->fd_lens[3]);
1859 tail_len = __tail_onwire_len(desc->fd_lens[1], desc->fd_lens[2],
1860 desc->fd_lens[3], con_secure(con));
1864 set_in_skip(con, tail_len);
1865 con->v2.in_state = IN_S_FINISH_SKIP;
1869 static int process_banner_prefix(struct ceph_connection *con)
1874 WARN_ON(con->v2.in_kvecs[0].iov_len != CEPH_BANNER_V2_PREFIX_LEN);
1876 p = con->v2.in_kvecs[0].iov_base;
1877 if (memcmp(p, CEPH_BANNER_V2, CEPH_BANNER_V2_LEN)) {
1878 if (!memcmp(p, CEPH_BANNER, CEPH_BANNER_LEN))
1879 con->error_msg = "server is speaking msgr1 protocol";
1881 con->error_msg = "protocol error, bad banner";
1885 p += CEPH_BANNER_V2_LEN;
1886 payload_len = ceph_decode_16(&p);
1887 dout("%s con %p payload_len %d\n", __func__, con, payload_len);
1889 return prepare_read_banner_payload(con, payload_len);
1892 static int process_banner_payload(struct ceph_connection *con)
1894 void *end = con->v2.in_kvecs[0].iov_base + con->v2.in_kvecs[0].iov_len;
1895 u64 feat = CEPH_MSGR2_SUPPORTED_FEATURES;
1896 u64 req_feat = CEPH_MSGR2_REQUIRED_FEATURES;
1897 u64 server_feat, server_req_feat;
1901 p = con->v2.in_kvecs[0].iov_base;
1902 ceph_decode_64_safe(&p, end, server_feat, bad);
1903 ceph_decode_64_safe(&p, end, server_req_feat, bad);
1905 dout("%s con %p server_feat 0x%llx server_req_feat 0x%llx\n",
1906 __func__, con, server_feat, server_req_feat);
1908 if (req_feat & ~server_feat) {
1909 pr_err("msgr2 feature set mismatch: my required > server's supported 0x%llx, need 0x%llx\n",
1910 server_feat, req_feat & ~server_feat);
1911 con->error_msg = "missing required protocol features";
1914 if (server_req_feat & ~feat) {
1915 pr_err("msgr2 feature set mismatch: server's required > my supported 0x%llx, missing 0x%llx\n",
1916 feat, server_req_feat & ~feat);
1917 con->error_msg = "missing required protocol features";
1921 /* no reset_out_kvecs() as our banner may still be pending */
1922 ret = prepare_hello(con);
1924 pr_err("prepare_hello failed: %d\n", ret);
1928 con->state = CEPH_CON_S_V2_HELLO;
1929 prepare_read_preamble(con);
1933 pr_err("failed to decode banner payload\n");
1937 static int process_hello(struct ceph_connection *con, void *p, void *end)
1939 struct ceph_entity_addr *my_addr = &con->msgr->inst.addr;
1940 struct ceph_entity_addr addr_for_me;
1944 if (con->state != CEPH_CON_S_V2_HELLO) {
1945 con->error_msg = "protocol error, unexpected hello";
1949 ceph_decode_8_safe(&p, end, entity_type, bad);
1950 ret = ceph_decode_entity_addr(&p, end, &addr_for_me);
1952 pr_err("failed to decode addr_for_me: %d\n", ret);
1956 dout("%s con %p entity_type %d addr_for_me %s\n", __func__, con,
1957 entity_type, ceph_pr_addr(&addr_for_me));
1959 if (entity_type != con->peer_name.type) {
1960 pr_err("bad peer type, want %d, got %d\n",
1961 con->peer_name.type, entity_type);
1962 con->error_msg = "wrong peer at address";
1967 * Set our address to the address our first peer (i.e. monitor)
1968 * sees that we are connecting from. If we are behind some sort
1969 * of NAT and want to be identified by some private (not NATed)
1970 * address, ip option should be used.
1972 if (ceph_addr_is_blank(my_addr)) {
1973 memcpy(&my_addr->in_addr, &addr_for_me.in_addr,
1974 sizeof(my_addr->in_addr));
1975 ceph_addr_set_port(my_addr, 0);
1976 dout("%s con %p set my addr %s, as seen by peer %s\n",
1977 __func__, con, ceph_pr_addr(my_addr),
1978 ceph_pr_addr(&con->peer_addr));
1980 dout("%s con %p my addr already set %s\n",
1981 __func__, con, ceph_pr_addr(my_addr));
1984 WARN_ON(ceph_addr_is_blank(my_addr) || ceph_addr_port(my_addr));
1985 WARN_ON(my_addr->type != CEPH_ENTITY_ADDR_TYPE_ANY);
1986 WARN_ON(!my_addr->nonce);
1988 /* no reset_out_kvecs() as our hello may still be pending */
1989 ret = prepare_auth_request(con);
1992 pr_err("prepare_auth_request failed: %d\n", ret);
1996 con->state = CEPH_CON_S_V2_AUTH;
2000 pr_err("failed to decode hello\n");
2004 static int process_auth_bad_method(struct ceph_connection *con,
2007 int allowed_protos[8], allowed_modes[8];
2008 int allowed_proto_cnt, allowed_mode_cnt;
2009 int used_proto, result;
2013 if (con->state != CEPH_CON_S_V2_AUTH) {
2014 con->error_msg = "protocol error, unexpected auth_bad_method";
2018 ceph_decode_32_safe(&p, end, used_proto, bad);
2019 ceph_decode_32_safe(&p, end, result, bad);
2020 dout("%s con %p used_proto %d result %d\n", __func__, con, used_proto,
2023 ceph_decode_32_safe(&p, end, allowed_proto_cnt, bad);
2024 if (allowed_proto_cnt > ARRAY_SIZE(allowed_protos)) {
2025 pr_err("allowed_protos too big %d\n", allowed_proto_cnt);
2028 for (i = 0; i < allowed_proto_cnt; i++) {
2029 ceph_decode_32_safe(&p, end, allowed_protos[i], bad);
2030 dout("%s con %p allowed_protos[%d] %d\n", __func__, con,
2031 i, allowed_protos[i]);
2034 ceph_decode_32_safe(&p, end, allowed_mode_cnt, bad);
2035 if (allowed_mode_cnt > ARRAY_SIZE(allowed_modes)) {
2036 pr_err("allowed_modes too big %d\n", allowed_mode_cnt);
2039 for (i = 0; i < allowed_mode_cnt; i++) {
2040 ceph_decode_32_safe(&p, end, allowed_modes[i], bad);
2041 dout("%s con %p allowed_modes[%d] %d\n", __func__, con,
2042 i, allowed_modes[i]);
2045 mutex_unlock(&con->mutex);
2046 ret = con->ops->handle_auth_bad_method(con, used_proto, result,
2051 mutex_lock(&con->mutex);
2052 if (con->state != CEPH_CON_S_V2_AUTH) {
2053 dout("%s con %p state changed to %d\n", __func__, con,
2058 dout("%s con %p handle_auth_bad_method ret %d\n", __func__, con, ret);
2062 pr_err("failed to decode auth_bad_method\n");
2066 static int process_auth_reply_more(struct ceph_connection *con,
2072 if (con->state != CEPH_CON_S_V2_AUTH) {
2073 con->error_msg = "protocol error, unexpected auth_reply_more";
2077 ceph_decode_32_safe(&p, end, payload_len, bad);
2078 ceph_decode_need(&p, end, payload_len, bad);
2080 dout("%s con %p payload_len %d\n", __func__, con, payload_len);
2082 reset_out_kvecs(con);
2083 ret = prepare_auth_request_more(con, p, payload_len);
2086 pr_err("prepare_auth_request_more failed: %d\n", ret);
2093 pr_err("failed to decode auth_reply_more\n");
2098 * Align session_key and con_secret to avoid GFP_ATOMIC allocation
2099 * inside crypto_shash_setkey() and crypto_aead_setkey() called from
2100 * setup_crypto(). __aligned(16) isn't guaranteed to work for stack
2101 * objects, so do it by hand.
2103 static int process_auth_done(struct ceph_connection *con, void *p, void *end)
2105 u8 session_key_buf[CEPH_KEY_LEN + 16];
2106 u8 con_secret_buf[CEPH_MAX_CON_SECRET_LEN + 16];
2107 u8 *session_key = PTR_ALIGN(&session_key_buf[0], 16);
2108 u8 *con_secret = PTR_ALIGN(&con_secret_buf[0], 16);
2109 int session_key_len, con_secret_len;
2114 if (con->state != CEPH_CON_S_V2_AUTH) {
2115 con->error_msg = "protocol error, unexpected auth_done";
2119 ceph_decode_64_safe(&p, end, global_id, bad);
2120 ceph_decode_32_safe(&p, end, con->v2.con_mode, bad);
2121 ceph_decode_32_safe(&p, end, payload_len, bad);
2123 dout("%s con %p global_id %llu con_mode %d payload_len %d\n",
2124 __func__, con, global_id, con->v2.con_mode, payload_len);
2126 mutex_unlock(&con->mutex);
2127 session_key_len = 0;
2129 ret = con->ops->handle_auth_done(con, global_id, p, payload_len,
2130 session_key, &session_key_len,
2131 con_secret, &con_secret_len);
2132 mutex_lock(&con->mutex);
2133 if (con->state != CEPH_CON_S_V2_AUTH) {
2134 dout("%s con %p state changed to %d\n", __func__, con,
2140 dout("%s con %p handle_auth_done ret %d\n", __func__, con, ret);
2144 ret = setup_crypto(con, session_key, session_key_len, con_secret,
2149 reset_out_kvecs(con);
2150 ret = prepare_auth_signature(con);
2152 pr_err("prepare_auth_signature failed: %d\n", ret);
2156 con->state = CEPH_CON_S_V2_AUTH_SIGNATURE;
2159 memzero_explicit(session_key_buf, sizeof(session_key_buf));
2160 memzero_explicit(con_secret_buf, sizeof(con_secret_buf));
2164 pr_err("failed to decode auth_done\n");
2168 static int process_auth_signature(struct ceph_connection *con,
2171 u8 hmac[SHA256_DIGEST_SIZE];
2174 if (con->state != CEPH_CON_S_V2_AUTH_SIGNATURE) {
2175 con->error_msg = "protocol error, unexpected auth_signature";
2179 ret = hmac_sha256(con, con->v2.out_sign_kvecs,
2180 con->v2.out_sign_kvec_cnt, hmac);
2184 ceph_decode_need(&p, end, SHA256_DIGEST_SIZE, bad);
2185 if (crypto_memneq(p, hmac, SHA256_DIGEST_SIZE)) {
2186 con->error_msg = "integrity error, bad auth signature";
2190 dout("%s con %p auth signature ok\n", __func__, con);
2192 /* no reset_out_kvecs() as our auth_signature may still be pending */
2193 if (!con->v2.server_cookie) {
2194 ret = prepare_client_ident(con);
2196 pr_err("prepare_client_ident failed: %d\n", ret);
2200 con->state = CEPH_CON_S_V2_SESSION_CONNECT;
2202 ret = prepare_session_reconnect(con);
2204 pr_err("prepare_session_reconnect failed: %d\n", ret);
2208 con->state = CEPH_CON_S_V2_SESSION_RECONNECT;
2214 pr_err("failed to decode auth_signature\n");
2218 static int process_server_ident(struct ceph_connection *con,
2221 struct ceph_client *client = from_msgr(con->msgr);
2222 u64 features, required_features;
2223 struct ceph_entity_addr addr;
2230 if (con->state != CEPH_CON_S_V2_SESSION_CONNECT) {
2231 con->error_msg = "protocol error, unexpected server_ident";
2235 ret = ceph_decode_entity_addrvec(&p, end, true, &addr);
2237 pr_err("failed to decode server addrs: %d\n", ret);
2241 ceph_decode_64_safe(&p, end, global_id, bad);
2242 ceph_decode_64_safe(&p, end, global_seq, bad);
2243 ceph_decode_64_safe(&p, end, features, bad);
2244 ceph_decode_64_safe(&p, end, required_features, bad);
2245 ceph_decode_64_safe(&p, end, flags, bad);
2246 ceph_decode_64_safe(&p, end, cookie, bad);
2248 dout("%s con %p addr %s/%u global_id %llu global_seq %llu features 0x%llx required_features 0x%llx flags 0x%llx cookie 0x%llx\n",
2249 __func__, con, ceph_pr_addr(&addr), le32_to_cpu(addr.nonce),
2250 global_id, global_seq, features, required_features, flags, cookie);
2252 /* is this who we intended to talk to? */
2253 if (memcmp(&addr, &con->peer_addr, sizeof(con->peer_addr))) {
2254 pr_err("bad peer addr/nonce, want %s/%u, got %s/%u\n",
2255 ceph_pr_addr(&con->peer_addr),
2256 le32_to_cpu(con->peer_addr.nonce),
2257 ceph_pr_addr(&addr), le32_to_cpu(addr.nonce));
2258 con->error_msg = "wrong peer at address";
2262 if (client->required_features & ~features) {
2263 pr_err("RADOS feature set mismatch: my required > server's supported 0x%llx, need 0x%llx\n",
2264 features, client->required_features & ~features);
2265 con->error_msg = "missing required protocol features";
2270 * Both name->type and name->num are set in ceph_con_open() but
2271 * name->num may be bogus in the initial monmap. name->type is
2272 * verified in handle_hello().
2274 WARN_ON(!con->peer_name.type);
2275 con->peer_name.num = cpu_to_le64(global_id);
2276 con->v2.peer_global_seq = global_seq;
2277 con->peer_features = features;
2278 WARN_ON(required_features & ~client->supported_features);
2279 con->v2.server_cookie = cookie;
2281 if (flags & CEPH_MSG_CONNECT_LOSSY) {
2282 ceph_con_flag_set(con, CEPH_CON_F_LOSSYTX);
2283 WARN_ON(con->v2.server_cookie);
2285 WARN_ON(!con->v2.server_cookie);
2288 clear_in_sign_kvecs(con);
2289 clear_out_sign_kvecs(con);
2290 free_conn_bufs(con);
2291 con->delay = 0; /* reset backoff memory */
2293 con->state = CEPH_CON_S_OPEN;
2294 con->v2.out_state = OUT_S_GET_NEXT;
2298 pr_err("failed to decode server_ident\n");
2302 static int process_ident_missing_features(struct ceph_connection *con,
2305 struct ceph_client *client = from_msgr(con->msgr);
2306 u64 missing_features;
2308 if (con->state != CEPH_CON_S_V2_SESSION_CONNECT) {
2309 con->error_msg = "protocol error, unexpected ident_missing_features";
2313 ceph_decode_64_safe(&p, end, missing_features, bad);
2314 pr_err("RADOS feature set mismatch: server's required > my supported 0x%llx, missing 0x%llx\n",
2315 client->supported_features, missing_features);
2316 con->error_msg = "missing required protocol features";
2320 pr_err("failed to decode ident_missing_features\n");
2324 static int process_session_reconnect_ok(struct ceph_connection *con,
2329 if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
2330 con->error_msg = "protocol error, unexpected session_reconnect_ok";
2334 ceph_decode_64_safe(&p, end, seq, bad);
2336 dout("%s con %p seq %llu\n", __func__, con, seq);
2337 ceph_con_discard_requeued(con, seq);
2339 clear_in_sign_kvecs(con);
2340 clear_out_sign_kvecs(con);
2341 free_conn_bufs(con);
2342 con->delay = 0; /* reset backoff memory */
2344 con->state = CEPH_CON_S_OPEN;
2345 con->v2.out_state = OUT_S_GET_NEXT;
2349 pr_err("failed to decode session_reconnect_ok\n");
2353 static int process_session_retry(struct ceph_connection *con,
2359 if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
2360 con->error_msg = "protocol error, unexpected session_retry";
2364 ceph_decode_64_safe(&p, end, connect_seq, bad);
2366 dout("%s con %p connect_seq %llu\n", __func__, con, connect_seq);
2367 WARN_ON(connect_seq <= con->v2.connect_seq);
2368 con->v2.connect_seq = connect_seq + 1;
2370 free_conn_bufs(con);
2372 reset_out_kvecs(con);
2373 ret = prepare_session_reconnect(con);
2375 pr_err("prepare_session_reconnect (cseq) failed: %d\n", ret);
2382 pr_err("failed to decode session_retry\n");
2386 static int process_session_retry_global(struct ceph_connection *con,
2392 if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
2393 con->error_msg = "protocol error, unexpected session_retry_global";
2397 ceph_decode_64_safe(&p, end, global_seq, bad);
2399 dout("%s con %p global_seq %llu\n", __func__, con, global_seq);
2400 WARN_ON(global_seq <= con->v2.global_seq);
2401 con->v2.global_seq = ceph_get_global_seq(con->msgr, global_seq);
2403 free_conn_bufs(con);
2405 reset_out_kvecs(con);
2406 ret = prepare_session_reconnect(con);
2408 pr_err("prepare_session_reconnect (gseq) failed: %d\n", ret);
2415 pr_err("failed to decode session_retry_global\n");
2419 static int process_session_reset(struct ceph_connection *con,
2425 if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
2426 con->error_msg = "protocol error, unexpected session_reset";
2430 ceph_decode_8_safe(&p, end, full, bad);
2432 con->error_msg = "protocol error, bad session_reset";
2436 pr_info("%s%lld %s session reset\n", ENTITY_NAME(con->peer_name),
2437 ceph_pr_addr(&con->peer_addr));
2438 ceph_con_reset_session(con);
2440 mutex_unlock(&con->mutex);
2441 if (con->ops->peer_reset)
2442 con->ops->peer_reset(con);
2443 mutex_lock(&con->mutex);
2444 if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
2445 dout("%s con %p state changed to %d\n", __func__, con,
2450 free_conn_bufs(con);
2452 reset_out_kvecs(con);
2453 ret = prepare_client_ident(con);
2455 pr_err("prepare_client_ident (rst) failed: %d\n", ret);
2459 con->state = CEPH_CON_S_V2_SESSION_CONNECT;
2463 pr_err("failed to decode session_reset\n");
2467 static int process_keepalive2_ack(struct ceph_connection *con,
2470 if (con->state != CEPH_CON_S_OPEN) {
2471 con->error_msg = "protocol error, unexpected keepalive2_ack";
2475 ceph_decode_need(&p, end, sizeof(struct ceph_timespec), bad);
2476 ceph_decode_timespec64(&con->last_keepalive_ack, p);
2478 dout("%s con %p timestamp %lld.%09ld\n", __func__, con,
2479 con->last_keepalive_ack.tv_sec, con->last_keepalive_ack.tv_nsec);
2484 pr_err("failed to decode keepalive2_ack\n");
2488 static int process_ack(struct ceph_connection *con, void *p, void *end)
2492 if (con->state != CEPH_CON_S_OPEN) {
2493 con->error_msg = "protocol error, unexpected ack";
2497 ceph_decode_64_safe(&p, end, seq, bad);
2499 dout("%s con %p seq %llu\n", __func__, con, seq);
2500 ceph_con_discard_sent(con, seq);
2504 pr_err("failed to decode ack\n");
2508 static int process_control(struct ceph_connection *con, void *p, void *end)
2510 int tag = con->v2.in_desc.fd_tag;
2513 dout("%s con %p tag %d len %d\n", __func__, con, tag, (int)(end - p));
2516 case FRAME_TAG_HELLO:
2517 ret = process_hello(con, p, end);
2519 case FRAME_TAG_AUTH_BAD_METHOD:
2520 ret = process_auth_bad_method(con, p, end);
2522 case FRAME_TAG_AUTH_REPLY_MORE:
2523 ret = process_auth_reply_more(con, p, end);
2525 case FRAME_TAG_AUTH_DONE:
2526 ret = process_auth_done(con, p, end);
2528 case FRAME_TAG_AUTH_SIGNATURE:
2529 ret = process_auth_signature(con, p, end);
2531 case FRAME_TAG_SERVER_IDENT:
2532 ret = process_server_ident(con, p, end);
2534 case FRAME_TAG_IDENT_MISSING_FEATURES:
2535 ret = process_ident_missing_features(con, p, end);
2537 case FRAME_TAG_SESSION_RECONNECT_OK:
2538 ret = process_session_reconnect_ok(con, p, end);
2540 case FRAME_TAG_SESSION_RETRY:
2541 ret = process_session_retry(con, p, end);
2543 case FRAME_TAG_SESSION_RETRY_GLOBAL:
2544 ret = process_session_retry_global(con, p, end);
2546 case FRAME_TAG_SESSION_RESET:
2547 ret = process_session_reset(con, p, end);
2549 case FRAME_TAG_KEEPALIVE2_ACK:
2550 ret = process_keepalive2_ack(con, p, end);
2553 ret = process_ack(con, p, end);
2556 pr_err("bad tag %d\n", tag);
2557 con->error_msg = "protocol error, bad tag";
2561 dout("%s con %p error %d\n", __func__, con, ret);
2565 prepare_read_preamble(con);
2571 * 1 - con->in_msg set, read message
2575 static int process_message_header(struct ceph_connection *con,
2578 struct ceph_frame_desc *desc = &con->v2.in_desc;
2579 struct ceph_msg_header2 *hdr2 = p;
2580 struct ceph_msg_header hdr;
2586 seq = le64_to_cpu(hdr2->seq);
2587 if ((s64)seq - (s64)con->in_seq < 1) {
2588 pr_info("%s%lld %s skipping old message: seq %llu, expected %llu\n",
2589 ENTITY_NAME(con->peer_name),
2590 ceph_pr_addr(&con->peer_addr),
2591 seq, con->in_seq + 1);
2594 if ((s64)seq - (s64)con->in_seq > 1) {
2595 pr_err("bad seq %llu, expected %llu\n", seq, con->in_seq + 1);
2596 con->error_msg = "bad message sequence # for incoming message";
2600 ceph_con_discard_sent(con, le64_to_cpu(hdr2->ack_seq));
2602 fill_header(&hdr, hdr2, desc->fd_lens[1], desc->fd_lens[2],
2603 desc->fd_lens[3], &con->peer_name);
2604 ret = ceph_con_in_msg_alloc(con, &hdr, &skip);
2608 WARN_ON(!con->in_msg ^ skip);
2612 WARN_ON(!con->in_msg);
2613 WARN_ON(con->in_msg->con != con);
2617 static int process_message(struct ceph_connection *con)
2619 ceph_con_process_message(con);
2622 * We could have been closed by ceph_con_close() because
2623 * ceph_con_process_message() temporarily drops con->mutex.
2625 if (con->state != CEPH_CON_S_OPEN) {
2626 dout("%s con %p state changed to %d\n", __func__, con,
2631 prepare_read_preamble(con);
2635 static int __handle_control(struct ceph_connection *con, void *p)
2637 void *end = p + con->v2.in_desc.fd_lens[0];
2638 struct ceph_msg *msg;
2641 if (con->v2.in_desc.fd_tag != FRAME_TAG_MESSAGE)
2642 return process_control(con, p, end);
2644 ret = process_message_header(con, p, end);
2648 prepare_skip_message(con);
2652 msg = con->in_msg; /* set in process_message_header() */
2653 if (front_len(msg)) {
2654 WARN_ON(front_len(msg) > msg->front_alloc_len);
2655 msg->front.iov_len = front_len(msg);
2657 msg->front.iov_len = 0;
2659 if (middle_len(msg)) {
2660 WARN_ON(middle_len(msg) > msg->middle->alloc_len);
2661 msg->middle->vec.iov_len = middle_len(msg);
2662 } else if (msg->middle) {
2663 msg->middle->vec.iov_len = 0;
2666 if (!front_len(msg) && !middle_len(msg) && !data_len(msg))
2667 return process_message(con);
2669 if (con_secure(con))
2670 return prepare_read_tail_secure(con);
2672 return prepare_read_tail_plain(con);
2675 static int handle_preamble(struct ceph_connection *con)
2677 struct ceph_frame_desc *desc = &con->v2.in_desc;
2680 if (con_secure(con)) {
2681 ret = decrypt_preamble(con);
2683 if (ret == -EBADMSG)
2684 con->error_msg = "integrity error, bad preamble auth tag";
2689 ret = decode_preamble(con->v2.in_buf, desc);
2691 if (ret == -EBADMSG)
2692 con->error_msg = "integrity error, bad crc";
2694 con->error_msg = "protocol error, bad preamble";
2698 dout("%s con %p tag %d seg_cnt %d %d+%d+%d+%d\n", __func__,
2699 con, desc->fd_tag, desc->fd_seg_cnt, desc->fd_lens[0],
2700 desc->fd_lens[1], desc->fd_lens[2], desc->fd_lens[3]);
2702 if (!con_secure(con))
2703 return prepare_read_control(con);
2705 if (desc->fd_lens[0] > CEPH_PREAMBLE_INLINE_LEN)
2706 return prepare_read_control_remainder(con);
2708 return __handle_control(con, CTRL_BODY(con->v2.in_buf));
2711 static int handle_control(struct ceph_connection *con)
2713 int ctrl_len = con->v2.in_desc.fd_lens[0];
2717 WARN_ON(con_secure(con));
2719 ret = verify_control_crc(con);
2721 con->error_msg = "integrity error, bad crc";
2725 if (con->state == CEPH_CON_S_V2_AUTH) {
2726 buf = alloc_conn_buf(con, ctrl_len);
2730 memcpy(buf, con->v2.in_kvecs[0].iov_base, ctrl_len);
2731 return __handle_control(con, buf);
2734 return __handle_control(con, con->v2.in_kvecs[0].iov_base);
2737 static int handle_control_remainder(struct ceph_connection *con)
2741 WARN_ON(!con_secure(con));
2743 ret = decrypt_control_remainder(con);
2745 if (ret == -EBADMSG)
2746 con->error_msg = "integrity error, bad control remainder auth tag";
2750 return __handle_control(con, con->v2.in_kvecs[0].iov_base -
2751 CEPH_PREAMBLE_INLINE_LEN);
2754 static int handle_epilogue(struct ceph_connection *con)
2756 u32 front_crc, middle_crc, data_crc;
2759 if (con_secure(con)) {
2760 ret = decrypt_tail(con);
2762 if (ret == -EBADMSG)
2763 con->error_msg = "integrity error, bad epilogue auth tag";
2767 /* just late_status */
2768 ret = decode_epilogue(con->v2.in_buf, NULL, NULL, NULL);
2770 con->error_msg = "protocol error, bad epilogue";
2774 ret = decode_epilogue(con->v2.in_buf, &front_crc,
2775 &middle_crc, &data_crc);
2777 con->error_msg = "protocol error, bad epilogue";
2781 ret = verify_epilogue_crcs(con, front_crc, middle_crc,
2784 con->error_msg = "integrity error, bad crc";
2789 return process_message(con);
2792 static void finish_skip(struct ceph_connection *con)
2794 dout("%s con %p\n", __func__, con);
2796 if (con_secure(con))
2797 gcm_inc_nonce(&con->v2.in_gcm_nonce);
2802 static int populate_in_iter(struct ceph_connection *con)
2806 dout("%s con %p state %d in_state %d\n", __func__, con, con->state,
2808 WARN_ON(iov_iter_count(&con->v2.in_iter));
2810 if (con->state == CEPH_CON_S_V2_BANNER_PREFIX) {
2811 ret = process_banner_prefix(con);
2812 } else if (con->state == CEPH_CON_S_V2_BANNER_PAYLOAD) {
2813 ret = process_banner_payload(con);
2814 } else if ((con->state >= CEPH_CON_S_V2_HELLO &&
2815 con->state <= CEPH_CON_S_V2_SESSION_RECONNECT) ||
2816 con->state == CEPH_CON_S_OPEN) {
2817 switch (con->v2.in_state) {
2818 case IN_S_HANDLE_PREAMBLE:
2819 ret = handle_preamble(con);
2821 case IN_S_HANDLE_CONTROL:
2822 ret = handle_control(con);
2824 case IN_S_HANDLE_CONTROL_REMAINDER:
2825 ret = handle_control_remainder(con);
2827 case IN_S_PREPARE_READ_DATA:
2828 ret = prepare_read_data(con);
2830 case IN_S_PREPARE_READ_DATA_CONT:
2831 prepare_read_data_cont(con);
2834 case IN_S_PREPARE_READ_ENC_PAGE:
2835 prepare_read_enc_page(con);
2838 case IN_S_HANDLE_EPILOGUE:
2839 ret = handle_epilogue(con);
2841 case IN_S_FINISH_SKIP:
2846 WARN(1, "bad in_state %d", con->v2.in_state);
2850 WARN(1, "bad state %d", con->state);
2854 dout("%s con %p error %d\n", __func__, con, ret);
2858 if (WARN_ON(!iov_iter_count(&con->v2.in_iter)))
2860 dout("%s con %p populated %zu\n", __func__, con,
2861 iov_iter_count(&con->v2.in_iter));
2865 int ceph_con_v2_try_read(struct ceph_connection *con)
2869 dout("%s con %p state %d need %zu\n", __func__, con, con->state,
2870 iov_iter_count(&con->v2.in_iter));
2872 if (con->state == CEPH_CON_S_PREOPEN)
2876 * We should always have something pending here. If not,
2877 * avoid calling populate_in_iter() as if we read something
2878 * (ceph_tcp_recv() would immediately return 1).
2880 if (WARN_ON(!iov_iter_count(&con->v2.in_iter)))
2884 ret = ceph_tcp_recv(con);
2888 ret = populate_in_iter(con);
2890 if (ret && ret != -EAGAIN && !con->error_msg)
2891 con->error_msg = "read processing error";
2897 static void queue_data(struct ceph_connection *con)
2901 con->v2.out_epil.data_crc = -1;
2902 ceph_msg_data_cursor_init(&con->v2.out_cursor, con->out_msg,
2903 data_len(con->out_msg));
2905 get_bvec_at(&con->v2.out_cursor, &bv);
2906 set_out_bvec(con, &bv, true);
2907 con->v2.out_state = OUT_S_QUEUE_DATA_CONT;
2910 static void queue_data_cont(struct ceph_connection *con)
2914 con->v2.out_epil.data_crc = ceph_crc32c_page(
2915 con->v2.out_epil.data_crc, con->v2.out_bvec.bv_page,
2916 con->v2.out_bvec.bv_offset, con->v2.out_bvec.bv_len);
2918 ceph_msg_data_advance(&con->v2.out_cursor, con->v2.out_bvec.bv_len);
2919 if (con->v2.out_cursor.total_resid) {
2920 get_bvec_at(&con->v2.out_cursor, &bv);
2921 set_out_bvec(con, &bv, true);
2922 WARN_ON(con->v2.out_state != OUT_S_QUEUE_DATA_CONT);
2927 * We've written all data. Queue epilogue. Once it's written,
2930 reset_out_kvecs(con);
2931 prepare_epilogue_plain(con, false);
2932 con->v2.out_state = OUT_S_FINISH_MESSAGE;
2935 static void queue_enc_page(struct ceph_connection *con)
2939 dout("%s con %p i %d resid %d\n", __func__, con, con->v2.out_enc_i,
2940 con->v2.out_enc_resid);
2941 WARN_ON(!con->v2.out_enc_resid);
2943 bvec_set_page(&bv, con->v2.out_enc_pages[con->v2.out_enc_i],
2944 min(con->v2.out_enc_resid, (int)PAGE_SIZE), 0);
2946 set_out_bvec(con, &bv, false);
2947 con->v2.out_enc_i++;
2948 con->v2.out_enc_resid -= bv.bv_len;
2950 if (con->v2.out_enc_resid) {
2951 WARN_ON(con->v2.out_state != OUT_S_QUEUE_ENC_PAGE);
2956 * We've queued the last piece of ciphertext (ending with
2957 * epilogue) + auth tag. Once it's written, we are done.
2959 WARN_ON(con->v2.out_enc_i != con->v2.out_enc_page_cnt);
2960 con->v2.out_state = OUT_S_FINISH_MESSAGE;
2963 static void queue_zeros(struct ceph_connection *con)
2965 dout("%s con %p out_zero %d\n", __func__, con, con->v2.out_zero);
2967 if (con->v2.out_zero) {
2968 set_out_bvec_zero(con);
2969 con->v2.out_zero -= con->v2.out_bvec.bv_len;
2970 con->v2.out_state = OUT_S_QUEUE_ZEROS;
2975 * We've zero-filled everything up to epilogue. Queue epilogue
2976 * with late_status set to ABORTED and crcs adjusted for zeros.
2977 * Once it's written, we are done patching up for the revoke.
2979 reset_out_kvecs(con);
2980 prepare_epilogue_plain(con, true);
2981 con->v2.out_state = OUT_S_FINISH_MESSAGE;
2984 static void finish_message(struct ceph_connection *con)
2986 dout("%s con %p msg %p\n", __func__, con, con->out_msg);
2988 /* we end up here both plain and secure modes */
2989 if (con->v2.out_enc_pages) {
2990 WARN_ON(!con->v2.out_enc_page_cnt);
2991 ceph_release_page_vector(con->v2.out_enc_pages,
2992 con->v2.out_enc_page_cnt);
2993 con->v2.out_enc_pages = NULL;
2994 con->v2.out_enc_page_cnt = 0;
2996 /* message may have been revoked */
2998 ceph_msg_put(con->out_msg);
2999 con->out_msg = NULL;
3002 con->v2.out_state = OUT_S_GET_NEXT;
3005 static int populate_out_iter(struct ceph_connection *con)
3009 dout("%s con %p state %d out_state %d\n", __func__, con, con->state,
3011 WARN_ON(iov_iter_count(&con->v2.out_iter));
3013 if (con->state != CEPH_CON_S_OPEN) {
3014 WARN_ON(con->state < CEPH_CON_S_V2_BANNER_PREFIX ||
3015 con->state > CEPH_CON_S_V2_SESSION_RECONNECT);
3016 goto nothing_pending;
3019 switch (con->v2.out_state) {
3020 case OUT_S_QUEUE_DATA:
3021 WARN_ON(!con->out_msg);
3024 case OUT_S_QUEUE_DATA_CONT:
3025 WARN_ON(!con->out_msg);
3026 queue_data_cont(con);
3028 case OUT_S_QUEUE_ENC_PAGE:
3029 queue_enc_page(con);
3031 case OUT_S_QUEUE_ZEROS:
3032 WARN_ON(con->out_msg); /* revoked */
3035 case OUT_S_FINISH_MESSAGE:
3036 finish_message(con);
3038 case OUT_S_GET_NEXT:
3041 WARN(1, "bad out_state %d", con->v2.out_state);
3045 WARN_ON(con->v2.out_state != OUT_S_GET_NEXT);
3046 if (ceph_con_flag_test_and_clear(con, CEPH_CON_F_KEEPALIVE_PENDING)) {
3047 ret = prepare_keepalive2(con);
3049 pr_err("prepare_keepalive2 failed: %d\n", ret);
3052 } else if (!list_empty(&con->out_queue)) {
3053 ceph_con_get_out_msg(con);
3054 ret = prepare_message(con);
3056 pr_err("prepare_message failed: %d\n", ret);
3059 } else if (con->in_seq > con->in_seq_acked) {
3060 ret = prepare_ack(con);
3062 pr_err("prepare_ack failed: %d\n", ret);
3066 goto nothing_pending;
3070 if (WARN_ON(!iov_iter_count(&con->v2.out_iter)))
3072 dout("%s con %p populated %zu\n", __func__, con,
3073 iov_iter_count(&con->v2.out_iter));
3077 WARN_ON(iov_iter_count(&con->v2.out_iter));
3078 dout("%s con %p nothing pending\n", __func__, con);
3079 ceph_con_flag_clear(con, CEPH_CON_F_WRITE_PENDING);
3083 int ceph_con_v2_try_write(struct ceph_connection *con)
3087 dout("%s con %p state %d have %zu\n", __func__, con, con->state,
3088 iov_iter_count(&con->v2.out_iter));
3090 /* open the socket first? */
3091 if (con->state == CEPH_CON_S_PREOPEN) {
3092 WARN_ON(con->peer_addr.type != CEPH_ENTITY_ADDR_TYPE_MSGR2);
3095 * Always bump global_seq. Bump connect_seq only if
3096 * there is a session (i.e. we are reconnecting and will
3097 * send session_reconnect instead of client_ident).
3099 con->v2.global_seq = ceph_get_global_seq(con->msgr, 0);
3100 if (con->v2.server_cookie)
3101 con->v2.connect_seq++;
3103 ret = prepare_read_banner_prefix(con);
3105 pr_err("prepare_read_banner_prefix failed: %d\n", ret);
3106 con->error_msg = "connect error";
3110 reset_out_kvecs(con);
3111 ret = prepare_banner(con);
3113 pr_err("prepare_banner failed: %d\n", ret);
3114 con->error_msg = "connect error";
3118 ret = ceph_tcp_connect(con);
3120 pr_err("ceph_tcp_connect failed: %d\n", ret);
3121 con->error_msg = "connect error";
3126 if (!iov_iter_count(&con->v2.out_iter)) {
3127 ret = populate_out_iter(con);
3129 if (ret && ret != -EAGAIN && !con->error_msg)
3130 con->error_msg = "write processing error";
3135 tcp_sock_set_cork(con->sock->sk, true);
3137 ret = ceph_tcp_send(con);
3141 ret = populate_out_iter(con);
3143 if (ret && ret != -EAGAIN && !con->error_msg)
3144 con->error_msg = "write processing error";
3149 tcp_sock_set_cork(con->sock->sk, false);
3153 static u32 crc32c_zeros(u32 crc, int zero_len)
3158 len = min(zero_len, (int)PAGE_SIZE);
3159 crc = crc32c(crc, page_address(ceph_zero_page), len);
3166 static void prepare_zero_front(struct ceph_connection *con, int resid)
3170 WARN_ON(!resid || resid > front_len(con->out_msg));
3171 sent = front_len(con->out_msg) - resid;
3172 dout("%s con %p sent %d resid %d\n", __func__, con, sent, resid);
3175 con->v2.out_epil.front_crc =
3176 crc32c(-1, con->out_msg->front.iov_base, sent);
3177 con->v2.out_epil.front_crc =
3178 crc32c_zeros(con->v2.out_epil.front_crc, resid);
3180 con->v2.out_epil.front_crc = crc32c_zeros(-1, resid);
3183 con->v2.out_iter.count -= resid;
3184 out_zero_add(con, resid);
3187 static void prepare_zero_middle(struct ceph_connection *con, int resid)
3191 WARN_ON(!resid || resid > middle_len(con->out_msg));
3192 sent = middle_len(con->out_msg) - resid;
3193 dout("%s con %p sent %d resid %d\n", __func__, con, sent, resid);
3196 con->v2.out_epil.middle_crc =
3197 crc32c(-1, con->out_msg->middle->vec.iov_base, sent);
3198 con->v2.out_epil.middle_crc =
3199 crc32c_zeros(con->v2.out_epil.middle_crc, resid);
3201 con->v2.out_epil.middle_crc = crc32c_zeros(-1, resid);
3204 con->v2.out_iter.count -= resid;
3205 out_zero_add(con, resid);
3208 static void prepare_zero_data(struct ceph_connection *con)
3210 dout("%s con %p\n", __func__, con);
3211 con->v2.out_epil.data_crc = crc32c_zeros(-1, data_len(con->out_msg));
3212 out_zero_add(con, data_len(con->out_msg));
3215 static void revoke_at_queue_data(struct ceph_connection *con)
3220 WARN_ON(!data_len(con->out_msg));
3221 WARN_ON(!iov_iter_is_kvec(&con->v2.out_iter));
3222 resid = iov_iter_count(&con->v2.out_iter);
3224 boundary = front_len(con->out_msg) + middle_len(con->out_msg);
3225 if (resid > boundary) {
3227 WARN_ON(resid > MESSAGE_HEAD_PLAIN_LEN);
3228 dout("%s con %p was sending head\n", __func__, con);
3229 if (front_len(con->out_msg))
3230 prepare_zero_front(con, front_len(con->out_msg));
3231 if (middle_len(con->out_msg))
3232 prepare_zero_middle(con, middle_len(con->out_msg));
3233 prepare_zero_data(con);
3234 WARN_ON(iov_iter_count(&con->v2.out_iter) != resid);
3235 con->v2.out_state = OUT_S_QUEUE_ZEROS;
3239 boundary = middle_len(con->out_msg);
3240 if (resid > boundary) {
3242 dout("%s con %p was sending front\n", __func__, con);
3243 prepare_zero_front(con, resid);
3244 if (middle_len(con->out_msg))
3245 prepare_zero_middle(con, middle_len(con->out_msg));
3246 prepare_zero_data(con);
3252 dout("%s con %p was sending middle\n", __func__, con);
3253 prepare_zero_middle(con, resid);
3254 prepare_zero_data(con);
3258 static void revoke_at_queue_data_cont(struct ceph_connection *con)
3260 int sent, resid; /* current piece of data */
3262 WARN_ON(!data_len(con->out_msg));
3263 WARN_ON(!iov_iter_is_bvec(&con->v2.out_iter));
3264 resid = iov_iter_count(&con->v2.out_iter);
3265 WARN_ON(!resid || resid > con->v2.out_bvec.bv_len);
3266 sent = con->v2.out_bvec.bv_len - resid;
3267 dout("%s con %p sent %d resid %d\n", __func__, con, sent, resid);
3270 con->v2.out_epil.data_crc = ceph_crc32c_page(
3271 con->v2.out_epil.data_crc, con->v2.out_bvec.bv_page,
3272 con->v2.out_bvec.bv_offset, sent);
3273 ceph_msg_data_advance(&con->v2.out_cursor, sent);
3275 WARN_ON(resid > con->v2.out_cursor.total_resid);
3276 con->v2.out_epil.data_crc = crc32c_zeros(con->v2.out_epil.data_crc,
3277 con->v2.out_cursor.total_resid);
3279 con->v2.out_iter.count -= resid;
3280 out_zero_add(con, con->v2.out_cursor.total_resid);
3284 static void revoke_at_finish_message(struct ceph_connection *con)
3289 WARN_ON(!iov_iter_is_kvec(&con->v2.out_iter));
3290 resid = iov_iter_count(&con->v2.out_iter);
3292 if (!front_len(con->out_msg) && !middle_len(con->out_msg) &&
3293 !data_len(con->out_msg)) {
3294 WARN_ON(!resid || resid > MESSAGE_HEAD_PLAIN_LEN);
3295 dout("%s con %p was sending head (empty message) - noop\n",
3300 boundary = front_len(con->out_msg) + middle_len(con->out_msg) +
3301 CEPH_EPILOGUE_PLAIN_LEN;
3302 if (resid > boundary) {
3304 WARN_ON(resid > MESSAGE_HEAD_PLAIN_LEN);
3305 dout("%s con %p was sending head\n", __func__, con);
3306 if (front_len(con->out_msg))
3307 prepare_zero_front(con, front_len(con->out_msg));
3308 if (middle_len(con->out_msg))
3309 prepare_zero_middle(con, middle_len(con->out_msg));
3310 con->v2.out_iter.count -= CEPH_EPILOGUE_PLAIN_LEN;
3311 WARN_ON(iov_iter_count(&con->v2.out_iter) != resid);
3312 con->v2.out_state = OUT_S_QUEUE_ZEROS;
3316 boundary = middle_len(con->out_msg) + CEPH_EPILOGUE_PLAIN_LEN;
3317 if (resid > boundary) {
3319 dout("%s con %p was sending front\n", __func__, con);
3320 prepare_zero_front(con, resid);
3321 if (middle_len(con->out_msg))
3322 prepare_zero_middle(con, middle_len(con->out_msg));
3323 con->v2.out_iter.count -= CEPH_EPILOGUE_PLAIN_LEN;
3328 boundary = CEPH_EPILOGUE_PLAIN_LEN;
3329 if (resid > boundary) {
3331 dout("%s con %p was sending middle\n", __func__, con);
3332 prepare_zero_middle(con, resid);
3333 con->v2.out_iter.count -= CEPH_EPILOGUE_PLAIN_LEN;
3339 dout("%s con %p was sending epilogue - noop\n", __func__, con);
3342 void ceph_con_v2_revoke(struct ceph_connection *con)
3344 WARN_ON(con->v2.out_zero);
3346 if (con_secure(con)) {
3347 WARN_ON(con->v2.out_state != OUT_S_QUEUE_ENC_PAGE &&
3348 con->v2.out_state != OUT_S_FINISH_MESSAGE);
3349 dout("%s con %p secure - noop\n", __func__, con);
3353 switch (con->v2.out_state) {
3354 case OUT_S_QUEUE_DATA:
3355 revoke_at_queue_data(con);
3357 case OUT_S_QUEUE_DATA_CONT:
3358 revoke_at_queue_data_cont(con);
3360 case OUT_S_FINISH_MESSAGE:
3361 revoke_at_finish_message(con);
3364 WARN(1, "bad out_state %d", con->v2.out_state);
3369 static void revoke_at_prepare_read_data(struct ceph_connection *con)
3374 WARN_ON(con_secure(con));
3375 WARN_ON(!data_len(con->in_msg));
3376 WARN_ON(!iov_iter_is_kvec(&con->v2.in_iter));
3377 resid = iov_iter_count(&con->v2.in_iter);
3380 remaining = data_len(con->in_msg) + CEPH_EPILOGUE_PLAIN_LEN;
3381 dout("%s con %p resid %d remaining %d\n", __func__, con, resid,
3383 con->v2.in_iter.count -= resid;
3384 set_in_skip(con, resid + remaining);
3385 con->v2.in_state = IN_S_FINISH_SKIP;
3388 static void revoke_at_prepare_read_data_cont(struct ceph_connection *con)
3390 int recved, resid; /* current piece of data */
3393 WARN_ON(con_secure(con));
3394 WARN_ON(!data_len(con->in_msg));
3395 WARN_ON(!iov_iter_is_bvec(&con->v2.in_iter));
3396 resid = iov_iter_count(&con->v2.in_iter);
3397 WARN_ON(!resid || resid > con->v2.in_bvec.bv_len);
3398 recved = con->v2.in_bvec.bv_len - resid;
3399 dout("%s con %p recved %d resid %d\n", __func__, con, recved, resid);
3402 ceph_msg_data_advance(&con->v2.in_cursor, recved);
3403 WARN_ON(resid > con->v2.in_cursor.total_resid);
3405 remaining = CEPH_EPILOGUE_PLAIN_LEN;
3406 dout("%s con %p total_resid %zu remaining %d\n", __func__, con,
3407 con->v2.in_cursor.total_resid, remaining);
3408 con->v2.in_iter.count -= resid;
3409 set_in_skip(con, con->v2.in_cursor.total_resid + remaining);
3410 con->v2.in_state = IN_S_FINISH_SKIP;
3413 static void revoke_at_prepare_read_enc_page(struct ceph_connection *con)
3415 int resid; /* current enc page (not necessarily data) */
3417 WARN_ON(!con_secure(con));
3418 WARN_ON(!iov_iter_is_bvec(&con->v2.in_iter));
3419 resid = iov_iter_count(&con->v2.in_iter);
3420 WARN_ON(!resid || resid > con->v2.in_bvec.bv_len);
3422 dout("%s con %p resid %d enc_resid %d\n", __func__, con, resid,
3423 con->v2.in_enc_resid);
3424 con->v2.in_iter.count -= resid;
3425 set_in_skip(con, resid + con->v2.in_enc_resid);
3426 con->v2.in_state = IN_S_FINISH_SKIP;
3429 static void revoke_at_handle_epilogue(struct ceph_connection *con)
3433 resid = iov_iter_count(&con->v2.in_iter);
3436 dout("%s con %p resid %d\n", __func__, con, resid);
3437 con->v2.in_iter.count -= resid;
3438 set_in_skip(con, resid);
3439 con->v2.in_state = IN_S_FINISH_SKIP;
3442 void ceph_con_v2_revoke_incoming(struct ceph_connection *con)
3444 switch (con->v2.in_state) {
3445 case IN_S_PREPARE_READ_DATA:
3446 revoke_at_prepare_read_data(con);
3448 case IN_S_PREPARE_READ_DATA_CONT:
3449 revoke_at_prepare_read_data_cont(con);
3451 case IN_S_PREPARE_READ_ENC_PAGE:
3452 revoke_at_prepare_read_enc_page(con);
3454 case IN_S_HANDLE_EPILOGUE:
3455 revoke_at_handle_epilogue(con);
3458 WARN(1, "bad in_state %d", con->v2.in_state);
3463 bool ceph_con_v2_opened(struct ceph_connection *con)
3465 return con->v2.peer_global_seq;
3468 void ceph_con_v2_reset_session(struct ceph_connection *con)
3470 con->v2.client_cookie = 0;
3471 con->v2.server_cookie = 0;
3472 con->v2.global_seq = 0;
3473 con->v2.connect_seq = 0;
3474 con->v2.peer_global_seq = 0;
3477 void ceph_con_v2_reset_protocol(struct ceph_connection *con)
3479 iov_iter_truncate(&con->v2.in_iter, 0);
3480 iov_iter_truncate(&con->v2.out_iter, 0);
3481 con->v2.out_zero = 0;
3483 clear_in_sign_kvecs(con);
3484 clear_out_sign_kvecs(con);
3485 free_conn_bufs(con);
3487 if (con->v2.in_enc_pages) {
3488 WARN_ON(!con->v2.in_enc_page_cnt);
3489 ceph_release_page_vector(con->v2.in_enc_pages,
3490 con->v2.in_enc_page_cnt);
3491 con->v2.in_enc_pages = NULL;
3492 con->v2.in_enc_page_cnt = 0;
3494 if (con->v2.out_enc_pages) {
3495 WARN_ON(!con->v2.out_enc_page_cnt);
3496 ceph_release_page_vector(con->v2.out_enc_pages,
3497 con->v2.out_enc_page_cnt);
3498 con->v2.out_enc_pages = NULL;
3499 con->v2.out_enc_page_cnt = 0;
3502 con->v2.con_mode = CEPH_CON_MODE_UNKNOWN;
3503 memzero_explicit(&con->v2.in_gcm_nonce, CEPH_GCM_IV_LEN);
3504 memzero_explicit(&con->v2.out_gcm_nonce, CEPH_GCM_IV_LEN);
3506 if (con->v2.hmac_tfm) {
3507 crypto_free_shash(con->v2.hmac_tfm);
3508 con->v2.hmac_tfm = NULL;
3510 if (con->v2.gcm_req) {
3511 aead_request_free(con->v2.gcm_req);
3512 con->v2.gcm_req = NULL;
3514 if (con->v2.gcm_tfm) {
3515 crypto_free_aead(con->v2.gcm_tfm);
3516 con->v2.gcm_tfm = NULL;