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_HANDLE_EPILOGUE 6
61 #define IN_S_FINISH_SKIP 7
63 #define OUT_S_QUEUE_DATA 1
64 #define OUT_S_QUEUE_DATA_CONT 2
65 #define OUT_S_QUEUE_ENC_PAGE 3
66 #define OUT_S_QUEUE_ZEROS 4
67 #define OUT_S_FINISH_MESSAGE 5
68 #define OUT_S_GET_NEXT 6
70 #define CTRL_BODY(p) ((void *)(p) + CEPH_PREAMBLE_LEN)
71 #define FRONT_PAD(p) ((void *)(p) + CEPH_EPILOGUE_SECURE_LEN)
72 #define MIDDLE_PAD(p) (FRONT_PAD(p) + CEPH_GCM_BLOCK_LEN)
73 #define DATA_PAD(p) (MIDDLE_PAD(p) + CEPH_GCM_BLOCK_LEN)
75 #define CEPH_MSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
77 static int do_recvmsg(struct socket *sock, struct iov_iter *it)
79 struct msghdr msg = { .msg_flags = CEPH_MSG_FLAGS };
83 while (iov_iter_count(it)) {
84 ret = sock_recvmsg(sock, &msg, msg.msg_flags);
91 iov_iter_advance(it, ret);
94 WARN_ON(msg_data_left(&msg));
99 * Read as much as possible.
102 * 1 - done, nothing (else) to read
103 * 0 - socket is empty, need to wait
106 static int ceph_tcp_recv(struct ceph_connection *con)
110 dout("%s con %p %s %zu\n", __func__, con,
111 iov_iter_is_discard(&con->v2.in_iter) ? "discard" : "need",
112 iov_iter_count(&con->v2.in_iter));
113 ret = do_recvmsg(con->sock, &con->v2.in_iter);
114 dout("%s con %p ret %d left %zu\n", __func__, con, ret,
115 iov_iter_count(&con->v2.in_iter));
119 static int do_sendmsg(struct socket *sock, struct iov_iter *it)
121 struct msghdr msg = { .msg_flags = CEPH_MSG_FLAGS };
125 while (iov_iter_count(it)) {
126 ret = sock_sendmsg(sock, &msg);
133 iov_iter_advance(it, ret);
136 WARN_ON(msg_data_left(&msg));
140 static int do_try_sendpage(struct socket *sock, struct iov_iter *it)
142 struct msghdr msg = { .msg_flags = CEPH_MSG_FLAGS };
146 if (WARN_ON(!iov_iter_is_bvec(it)))
149 while (iov_iter_count(it)) {
150 /* iov_iter_iovec() for ITER_BVEC */
151 bv.bv_page = it->bvec->bv_page;
152 bv.bv_offset = it->bvec->bv_offset + it->iov_offset;
153 bv.bv_len = min(iov_iter_count(it),
154 it->bvec->bv_len - it->iov_offset);
157 * sendpage cannot properly handle pages with
158 * page_count == 0, we need to fall back to sendmsg if
161 * Same goes for slab pages: skb_can_coalesce() allows
162 * coalescing neighboring slab objects into a single frag
163 * which triggers one of hardened usercopy checks.
165 if (sendpage_ok(bv.bv_page)) {
166 ret = sock->ops->sendpage(sock, bv.bv_page,
167 bv.bv_offset, bv.bv_len,
170 iov_iter_bvec(&msg.msg_iter, WRITE, &bv, 1, bv.bv_len);
171 ret = sock_sendmsg(sock, &msg);
179 iov_iter_advance(it, ret);
186 * Write as much as possible. The socket is expected to be corked,
187 * so we don't bother with MSG_MORE/MSG_SENDPAGE_NOTLAST here.
190 * 1 - done, nothing (else) to write
191 * 0 - socket is full, need to wait
194 static int ceph_tcp_send(struct ceph_connection *con)
198 dout("%s con %p have %zu try_sendpage %d\n", __func__, con,
199 iov_iter_count(&con->v2.out_iter), con->v2.out_iter_sendpage);
200 if (con->v2.out_iter_sendpage)
201 ret = do_try_sendpage(con->sock, &con->v2.out_iter);
203 ret = do_sendmsg(con->sock, &con->v2.out_iter);
204 dout("%s con %p ret %d left %zu\n", __func__, con, ret,
205 iov_iter_count(&con->v2.out_iter));
209 static void add_in_kvec(struct ceph_connection *con, void *buf, int len)
211 BUG_ON(con->v2.in_kvec_cnt >= ARRAY_SIZE(con->v2.in_kvecs));
212 WARN_ON(!iov_iter_is_kvec(&con->v2.in_iter));
214 con->v2.in_kvecs[con->v2.in_kvec_cnt].iov_base = buf;
215 con->v2.in_kvecs[con->v2.in_kvec_cnt].iov_len = len;
216 con->v2.in_kvec_cnt++;
218 con->v2.in_iter.nr_segs++;
219 con->v2.in_iter.count += len;
222 static void reset_in_kvecs(struct ceph_connection *con)
224 WARN_ON(iov_iter_count(&con->v2.in_iter));
226 con->v2.in_kvec_cnt = 0;
227 iov_iter_kvec(&con->v2.in_iter, READ, con->v2.in_kvecs, 0, 0);
230 static void set_in_bvec(struct ceph_connection *con, const struct bio_vec *bv)
232 WARN_ON(iov_iter_count(&con->v2.in_iter));
234 con->v2.in_bvec = *bv;
235 iov_iter_bvec(&con->v2.in_iter, READ, &con->v2.in_bvec, 1, bv->bv_len);
238 static void set_in_skip(struct ceph_connection *con, int len)
240 WARN_ON(iov_iter_count(&con->v2.in_iter));
242 dout("%s con %p len %d\n", __func__, con, len);
243 iov_iter_discard(&con->v2.in_iter, READ, len);
246 static void add_out_kvec(struct ceph_connection *con, void *buf, int len)
248 BUG_ON(con->v2.out_kvec_cnt >= ARRAY_SIZE(con->v2.out_kvecs));
249 WARN_ON(!iov_iter_is_kvec(&con->v2.out_iter));
250 WARN_ON(con->v2.out_zero);
252 con->v2.out_kvecs[con->v2.out_kvec_cnt].iov_base = buf;
253 con->v2.out_kvecs[con->v2.out_kvec_cnt].iov_len = len;
254 con->v2.out_kvec_cnt++;
256 con->v2.out_iter.nr_segs++;
257 con->v2.out_iter.count += len;
260 static void reset_out_kvecs(struct ceph_connection *con)
262 WARN_ON(iov_iter_count(&con->v2.out_iter));
263 WARN_ON(con->v2.out_zero);
265 con->v2.out_kvec_cnt = 0;
267 iov_iter_kvec(&con->v2.out_iter, WRITE, con->v2.out_kvecs, 0, 0);
268 con->v2.out_iter_sendpage = false;
271 static void set_out_bvec(struct ceph_connection *con, const struct bio_vec *bv,
274 WARN_ON(iov_iter_count(&con->v2.out_iter));
275 WARN_ON(con->v2.out_zero);
277 con->v2.out_bvec = *bv;
278 con->v2.out_iter_sendpage = zerocopy;
279 iov_iter_bvec(&con->v2.out_iter, WRITE, &con->v2.out_bvec, 1,
280 con->v2.out_bvec.bv_len);
283 static void set_out_bvec_zero(struct ceph_connection *con)
285 WARN_ON(iov_iter_count(&con->v2.out_iter));
286 WARN_ON(!con->v2.out_zero);
288 con->v2.out_bvec.bv_page = ceph_zero_page;
289 con->v2.out_bvec.bv_offset = 0;
290 con->v2.out_bvec.bv_len = min(con->v2.out_zero, (int)PAGE_SIZE);
291 con->v2.out_iter_sendpage = true;
292 iov_iter_bvec(&con->v2.out_iter, WRITE, &con->v2.out_bvec, 1,
293 con->v2.out_bvec.bv_len);
296 static void out_zero_add(struct ceph_connection *con, int len)
298 dout("%s con %p len %d\n", __func__, con, len);
299 con->v2.out_zero += len;
302 static void *alloc_conn_buf(struct ceph_connection *con, int len)
306 dout("%s con %p len %d\n", __func__, con, len);
308 if (WARN_ON(con->v2.conn_buf_cnt >= ARRAY_SIZE(con->v2.conn_bufs)))
311 buf = ceph_kvmalloc(len, GFP_NOIO);
315 con->v2.conn_bufs[con->v2.conn_buf_cnt++] = buf;
319 static void free_conn_bufs(struct ceph_connection *con)
321 while (con->v2.conn_buf_cnt)
322 kvfree(con->v2.conn_bufs[--con->v2.conn_buf_cnt]);
325 static void add_in_sign_kvec(struct ceph_connection *con, void *buf, int len)
327 BUG_ON(con->v2.in_sign_kvec_cnt >= ARRAY_SIZE(con->v2.in_sign_kvecs));
329 con->v2.in_sign_kvecs[con->v2.in_sign_kvec_cnt].iov_base = buf;
330 con->v2.in_sign_kvecs[con->v2.in_sign_kvec_cnt].iov_len = len;
331 con->v2.in_sign_kvec_cnt++;
334 static void clear_in_sign_kvecs(struct ceph_connection *con)
336 con->v2.in_sign_kvec_cnt = 0;
339 static void add_out_sign_kvec(struct ceph_connection *con, void *buf, int len)
341 BUG_ON(con->v2.out_sign_kvec_cnt >= ARRAY_SIZE(con->v2.out_sign_kvecs));
343 con->v2.out_sign_kvecs[con->v2.out_sign_kvec_cnt].iov_base = buf;
344 con->v2.out_sign_kvecs[con->v2.out_sign_kvec_cnt].iov_len = len;
345 con->v2.out_sign_kvec_cnt++;
348 static void clear_out_sign_kvecs(struct ceph_connection *con)
350 con->v2.out_sign_kvec_cnt = 0;
353 static bool con_secure(struct ceph_connection *con)
355 return con->v2.con_mode == CEPH_CON_MODE_SECURE;
358 static int front_len(const struct ceph_msg *msg)
360 return le32_to_cpu(msg->hdr.front_len);
363 static int middle_len(const struct ceph_msg *msg)
365 return le32_to_cpu(msg->hdr.middle_len);
368 static int data_len(const struct ceph_msg *msg)
370 return le32_to_cpu(msg->hdr.data_len);
373 static bool need_padding(int len)
375 return !IS_ALIGNED(len, CEPH_GCM_BLOCK_LEN);
378 static int padded_len(int len)
380 return ALIGN(len, CEPH_GCM_BLOCK_LEN);
383 static int padding_len(int len)
385 return padded_len(len) - len;
388 /* preamble + control segment */
389 static int head_onwire_len(int ctrl_len, bool secure)
395 head_len = CEPH_PREAMBLE_SECURE_LEN;
396 if (ctrl_len > CEPH_PREAMBLE_INLINE_LEN) {
397 rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
398 head_len += padded_len(rem_len) + CEPH_GCM_TAG_LEN;
401 head_len = CEPH_PREAMBLE_PLAIN_LEN;
403 head_len += ctrl_len + CEPH_CRC_LEN;
408 /* front, middle and data segments + epilogue */
409 static int __tail_onwire_len(int front_len, int middle_len, int data_len,
412 if (!front_len && !middle_len && !data_len)
416 return front_len + middle_len + data_len +
417 CEPH_EPILOGUE_PLAIN_LEN;
419 return padded_len(front_len) + padded_len(middle_len) +
420 padded_len(data_len) + CEPH_EPILOGUE_SECURE_LEN;
423 static int tail_onwire_len(const struct ceph_msg *msg, bool secure)
425 return __tail_onwire_len(front_len(msg), middle_len(msg),
426 data_len(msg), secure);
429 /* head_onwire_len(sizeof(struct ceph_msg_header2), false) */
430 #define MESSAGE_HEAD_PLAIN_LEN (CEPH_PREAMBLE_PLAIN_LEN + \
431 sizeof(struct ceph_msg_header2) + \
434 static const int frame_aligns[] = {
442 * Discards trailing empty segments, unless there is just one segment.
443 * A frame always has at least one (possibly empty) segment.
445 static int calc_segment_count(const int *lens, int len_cnt)
449 for (i = len_cnt - 1; i >= 0; i--) {
457 static void init_frame_desc(struct ceph_frame_desc *desc, int tag,
458 const int *lens, int len_cnt)
462 memset(desc, 0, sizeof(*desc));
465 desc->fd_seg_cnt = calc_segment_count(lens, len_cnt);
466 BUG_ON(desc->fd_seg_cnt > CEPH_FRAME_MAX_SEGMENT_COUNT);
467 for (i = 0; i < desc->fd_seg_cnt; i++) {
468 desc->fd_lens[i] = lens[i];
469 desc->fd_aligns[i] = frame_aligns[i];
474 * Preamble crc covers everything up to itself (28 bytes) and
475 * is calculated and verified irrespective of the connection mode
476 * (i.e. even if the frame is encrypted).
478 static void encode_preamble(const struct ceph_frame_desc *desc, void *p)
480 void *crcp = p + CEPH_PREAMBLE_LEN - CEPH_CRC_LEN;
484 memset(p, 0, CEPH_PREAMBLE_LEN);
486 ceph_encode_8(&p, desc->fd_tag);
487 ceph_encode_8(&p, desc->fd_seg_cnt);
488 for (i = 0; i < desc->fd_seg_cnt; i++) {
489 ceph_encode_32(&p, desc->fd_lens[i]);
490 ceph_encode_16(&p, desc->fd_aligns[i]);
493 put_unaligned_le32(crc32c(0, start, crcp - start), crcp);
496 static int decode_preamble(void *p, struct ceph_frame_desc *desc)
498 void *crcp = p + CEPH_PREAMBLE_LEN - CEPH_CRC_LEN;
499 u32 crc, expected_crc;
502 crc = crc32c(0, p, crcp - p);
503 expected_crc = get_unaligned_le32(crcp);
504 if (crc != expected_crc) {
505 pr_err("bad preamble crc, calculated %u, expected %u\n",
510 memset(desc, 0, sizeof(*desc));
512 desc->fd_tag = ceph_decode_8(&p);
513 desc->fd_seg_cnt = ceph_decode_8(&p);
514 if (desc->fd_seg_cnt < 1 ||
515 desc->fd_seg_cnt > CEPH_FRAME_MAX_SEGMENT_COUNT) {
516 pr_err("bad segment count %d\n", desc->fd_seg_cnt);
519 for (i = 0; i < desc->fd_seg_cnt; i++) {
520 desc->fd_lens[i] = ceph_decode_32(&p);
521 desc->fd_aligns[i] = ceph_decode_16(&p);
525 * This would fire for FRAME_TAG_WAIT (it has one empty
526 * segment), but we should never get it as client.
528 if (!desc->fd_lens[desc->fd_seg_cnt - 1]) {
529 pr_err("last segment empty\n");
533 if (desc->fd_lens[0] > CEPH_MSG_MAX_CONTROL_LEN) {
534 pr_err("control segment too big %d\n", desc->fd_lens[0]);
537 if (desc->fd_lens[1] > CEPH_MSG_MAX_FRONT_LEN) {
538 pr_err("front segment too big %d\n", desc->fd_lens[1]);
541 if (desc->fd_lens[2] > CEPH_MSG_MAX_MIDDLE_LEN) {
542 pr_err("middle segment too big %d\n", desc->fd_lens[2]);
545 if (desc->fd_lens[3] > CEPH_MSG_MAX_DATA_LEN) {
546 pr_err("data segment too big %d\n", desc->fd_lens[3]);
553 static void encode_epilogue_plain(struct ceph_connection *con, bool aborted)
555 con->v2.out_epil.late_status = aborted ? FRAME_LATE_STATUS_ABORTED :
556 FRAME_LATE_STATUS_COMPLETE;
557 cpu_to_le32s(&con->v2.out_epil.front_crc);
558 cpu_to_le32s(&con->v2.out_epil.middle_crc);
559 cpu_to_le32s(&con->v2.out_epil.data_crc);
562 static void encode_epilogue_secure(struct ceph_connection *con, bool aborted)
564 memset(&con->v2.out_epil, 0, sizeof(con->v2.out_epil));
565 con->v2.out_epil.late_status = aborted ? FRAME_LATE_STATUS_ABORTED :
566 FRAME_LATE_STATUS_COMPLETE;
569 static int decode_epilogue(void *p, u32 *front_crc, u32 *middle_crc,
574 late_status = ceph_decode_8(&p);
575 if ((late_status & FRAME_LATE_STATUS_ABORTED_MASK) !=
576 FRAME_LATE_STATUS_COMPLETE) {
577 /* we should never get an aborted message as client */
578 pr_err("bad late_status 0x%x\n", late_status);
582 if (front_crc && middle_crc && data_crc) {
583 *front_crc = ceph_decode_32(&p);
584 *middle_crc = ceph_decode_32(&p);
585 *data_crc = ceph_decode_32(&p);
591 static void fill_header(struct ceph_msg_header *hdr,
592 const struct ceph_msg_header2 *hdr2,
593 int front_len, int middle_len, int data_len,
594 const struct ceph_entity_name *peer_name)
596 hdr->seq = hdr2->seq;
597 hdr->tid = hdr2->tid;
598 hdr->type = hdr2->type;
599 hdr->priority = hdr2->priority;
600 hdr->version = hdr2->version;
601 hdr->front_len = cpu_to_le32(front_len);
602 hdr->middle_len = cpu_to_le32(middle_len);
603 hdr->data_len = cpu_to_le32(data_len);
604 hdr->data_off = hdr2->data_off;
605 hdr->src = *peer_name;
606 hdr->compat_version = hdr2->compat_version;
611 static void fill_header2(struct ceph_msg_header2 *hdr2,
612 const struct ceph_msg_header *hdr, u64 ack_seq)
614 hdr2->seq = hdr->seq;
615 hdr2->tid = hdr->tid;
616 hdr2->type = hdr->type;
617 hdr2->priority = hdr->priority;
618 hdr2->version = hdr->version;
619 hdr2->data_pre_padding_len = 0;
620 hdr2->data_off = hdr->data_off;
621 hdr2->ack_seq = cpu_to_le64(ack_seq);
623 hdr2->compat_version = hdr->compat_version;
627 static int verify_control_crc(struct ceph_connection *con)
629 int ctrl_len = con->v2.in_desc.fd_lens[0];
630 u32 crc, expected_crc;
632 WARN_ON(con->v2.in_kvecs[0].iov_len != ctrl_len);
633 WARN_ON(con->v2.in_kvecs[1].iov_len != CEPH_CRC_LEN);
635 crc = crc32c(-1, con->v2.in_kvecs[0].iov_base, ctrl_len);
636 expected_crc = get_unaligned_le32(con->v2.in_kvecs[1].iov_base);
637 if (crc != expected_crc) {
638 pr_err("bad control crc, calculated %u, expected %u\n",
646 static int verify_epilogue_crcs(struct ceph_connection *con, u32 front_crc,
647 u32 middle_crc, u32 data_crc)
649 if (front_len(con->in_msg)) {
650 con->in_front_crc = crc32c(-1, con->in_msg->front.iov_base,
651 front_len(con->in_msg));
653 WARN_ON(!middle_len(con->in_msg) && !data_len(con->in_msg));
654 con->in_front_crc = -1;
657 if (middle_len(con->in_msg))
658 con->in_middle_crc = crc32c(-1,
659 con->in_msg->middle->vec.iov_base,
660 middle_len(con->in_msg));
661 else if (data_len(con->in_msg))
662 con->in_middle_crc = -1;
664 con->in_middle_crc = 0;
666 if (!data_len(con->in_msg))
667 con->in_data_crc = 0;
669 dout("%s con %p msg %p crcs %u %u %u\n", __func__, con, con->in_msg,
670 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
672 if (con->in_front_crc != front_crc) {
673 pr_err("bad front crc, calculated %u, expected %u\n",
674 con->in_front_crc, front_crc);
677 if (con->in_middle_crc != middle_crc) {
678 pr_err("bad middle crc, calculated %u, expected %u\n",
679 con->in_middle_crc, middle_crc);
682 if (con->in_data_crc != data_crc) {
683 pr_err("bad data crc, calculated %u, expected %u\n",
684 con->in_data_crc, data_crc);
691 static int setup_crypto(struct ceph_connection *con,
692 const u8 *session_key, int session_key_len,
693 const u8 *con_secret, int con_secret_len)
695 unsigned int noio_flag;
698 dout("%s con %p con_mode %d session_key_len %d con_secret_len %d\n",
699 __func__, con, con->v2.con_mode, session_key_len, con_secret_len);
700 WARN_ON(con->v2.hmac_tfm || con->v2.gcm_tfm || con->v2.gcm_req);
702 if (con->v2.con_mode != CEPH_CON_MODE_CRC &&
703 con->v2.con_mode != CEPH_CON_MODE_SECURE) {
704 pr_err("bad con_mode %d\n", con->v2.con_mode);
708 if (!session_key_len) {
709 WARN_ON(con->v2.con_mode != CEPH_CON_MODE_CRC);
710 WARN_ON(con_secret_len);
711 return 0; /* auth_none */
714 noio_flag = memalloc_noio_save();
715 con->v2.hmac_tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
716 memalloc_noio_restore(noio_flag);
717 if (IS_ERR(con->v2.hmac_tfm)) {
718 ret = PTR_ERR(con->v2.hmac_tfm);
719 con->v2.hmac_tfm = NULL;
720 pr_err("failed to allocate hmac tfm context: %d\n", ret);
724 WARN_ON((unsigned long)session_key &
725 crypto_shash_alignmask(con->v2.hmac_tfm));
726 ret = crypto_shash_setkey(con->v2.hmac_tfm, session_key,
729 pr_err("failed to set hmac key: %d\n", ret);
733 if (con->v2.con_mode == CEPH_CON_MODE_CRC) {
734 WARN_ON(con_secret_len);
735 return 0; /* auth_x, plain mode */
738 if (con_secret_len < CEPH_GCM_KEY_LEN + 2 * CEPH_GCM_IV_LEN) {
739 pr_err("con_secret too small %d\n", con_secret_len);
743 noio_flag = memalloc_noio_save();
744 con->v2.gcm_tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
745 memalloc_noio_restore(noio_flag);
746 if (IS_ERR(con->v2.gcm_tfm)) {
747 ret = PTR_ERR(con->v2.gcm_tfm);
748 con->v2.gcm_tfm = NULL;
749 pr_err("failed to allocate gcm tfm context: %d\n", ret);
753 WARN_ON((unsigned long)con_secret &
754 crypto_aead_alignmask(con->v2.gcm_tfm));
755 ret = crypto_aead_setkey(con->v2.gcm_tfm, con_secret, CEPH_GCM_KEY_LEN);
757 pr_err("failed to set gcm key: %d\n", ret);
761 WARN_ON(crypto_aead_ivsize(con->v2.gcm_tfm) != CEPH_GCM_IV_LEN);
762 ret = crypto_aead_setauthsize(con->v2.gcm_tfm, CEPH_GCM_TAG_LEN);
764 pr_err("failed to set gcm tag size: %d\n", ret);
768 con->v2.gcm_req = aead_request_alloc(con->v2.gcm_tfm, GFP_NOIO);
769 if (!con->v2.gcm_req) {
770 pr_err("failed to allocate gcm request\n");
774 crypto_init_wait(&con->v2.gcm_wait);
775 aead_request_set_callback(con->v2.gcm_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
776 crypto_req_done, &con->v2.gcm_wait);
778 memcpy(&con->v2.in_gcm_nonce, con_secret + CEPH_GCM_KEY_LEN,
780 memcpy(&con->v2.out_gcm_nonce,
781 con_secret + CEPH_GCM_KEY_LEN + CEPH_GCM_IV_LEN,
783 return 0; /* auth_x, secure mode */
786 static int hmac_sha256(struct ceph_connection *con, const struct kvec *kvecs,
787 int kvec_cnt, u8 *hmac)
789 SHASH_DESC_ON_STACK(desc, con->v2.hmac_tfm); /* tfm arg is ignored */
793 dout("%s con %p hmac_tfm %p kvec_cnt %d\n", __func__, con,
794 con->v2.hmac_tfm, kvec_cnt);
796 if (!con->v2.hmac_tfm) {
797 memset(hmac, 0, SHA256_DIGEST_SIZE);
798 return 0; /* auth_none */
801 desc->tfm = con->v2.hmac_tfm;
802 ret = crypto_shash_init(desc);
806 for (i = 0; i < kvec_cnt; i++) {
807 WARN_ON((unsigned long)kvecs[i].iov_base &
808 crypto_shash_alignmask(con->v2.hmac_tfm));
809 ret = crypto_shash_update(desc, kvecs[i].iov_base,
815 ret = crypto_shash_final(desc, hmac);
818 shash_desc_zero(desc);
819 return ret; /* auth_x, both plain and secure modes */
822 static void gcm_inc_nonce(struct ceph_gcm_nonce *nonce)
826 counter = le64_to_cpu(nonce->counter);
827 nonce->counter = cpu_to_le64(counter + 1);
830 static int gcm_crypt(struct ceph_connection *con, bool encrypt,
831 struct scatterlist *src, struct scatterlist *dst,
834 struct ceph_gcm_nonce *nonce;
837 nonce = encrypt ? &con->v2.out_gcm_nonce : &con->v2.in_gcm_nonce;
839 aead_request_set_ad(con->v2.gcm_req, 0); /* no AAD */
840 aead_request_set_crypt(con->v2.gcm_req, src, dst, src_len, (u8 *)nonce);
841 ret = crypto_wait_req(encrypt ? crypto_aead_encrypt(con->v2.gcm_req) :
842 crypto_aead_decrypt(con->v2.gcm_req),
847 gcm_inc_nonce(nonce);
851 static void get_bvec_at(struct ceph_msg_data_cursor *cursor,
857 WARN_ON(!cursor->total_resid);
859 /* skip zero-length data items */
860 while (!cursor->resid)
861 ceph_msg_data_advance(cursor, 0);
863 /* get a piece of data, cursor isn't advanced */
864 page = ceph_msg_data_next(cursor, &off, &len, NULL);
871 static int calc_sg_cnt(void *buf, int buf_len)
878 sg_cnt = need_padding(buf_len) ? 1 : 0;
879 if (is_vmalloc_addr(buf)) {
880 WARN_ON(offset_in_page(buf));
881 sg_cnt += PAGE_ALIGN(buf_len) >> PAGE_SHIFT;
889 static int calc_sg_cnt_cursor(struct ceph_msg_data_cursor *cursor)
891 int data_len = cursor->total_resid;
898 sg_cnt = need_padding(data_len) ? 1 : 0;
900 get_bvec_at(cursor, &bv);
903 ceph_msg_data_advance(cursor, bv.bv_len);
904 } while (cursor->total_resid);
909 static void init_sgs(struct scatterlist **sg, void *buf, int buf_len, u8 *pad)
911 void *end = buf + buf_len;
919 if (is_vmalloc_addr(buf)) {
922 page = vmalloc_to_page(p);
923 len = min_t(int, end - p, PAGE_SIZE);
924 WARN_ON(!page || !len || offset_in_page(p));
925 sg_set_page(*sg, page, len, 0);
930 sg_set_buf(*sg, buf, buf_len);
934 if (need_padding(buf_len)) {
935 sg_set_buf(*sg, pad, padding_len(buf_len));
940 static void init_sgs_cursor(struct scatterlist **sg,
941 struct ceph_msg_data_cursor *cursor, u8 *pad)
943 int data_len = cursor->total_resid;
950 get_bvec_at(cursor, &bv);
951 sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset);
954 ceph_msg_data_advance(cursor, bv.bv_len);
955 } while (cursor->total_resid);
957 if (need_padding(data_len)) {
958 sg_set_buf(*sg, pad, padding_len(data_len));
963 static int setup_message_sgs(struct sg_table *sgt, struct ceph_msg *msg,
964 u8 *front_pad, u8 *middle_pad, u8 *data_pad,
965 void *epilogue, bool add_tag)
967 struct ceph_msg_data_cursor cursor;
968 struct scatterlist *cur_sg;
972 if (!front_len(msg) && !middle_len(msg) && !data_len(msg))
975 sg_cnt = 1; /* epilogue + [auth tag] */
977 sg_cnt += calc_sg_cnt(msg->front.iov_base,
980 sg_cnt += calc_sg_cnt(msg->middle->vec.iov_base,
983 ceph_msg_data_cursor_init(&cursor, msg, data_len(msg));
984 sg_cnt += calc_sg_cnt_cursor(&cursor);
987 ret = sg_alloc_table(sgt, sg_cnt, GFP_NOIO);
993 init_sgs(&cur_sg, msg->front.iov_base, front_len(msg),
996 init_sgs(&cur_sg, msg->middle->vec.iov_base, middle_len(msg),
999 ceph_msg_data_cursor_init(&cursor, msg, data_len(msg));
1000 init_sgs_cursor(&cur_sg, &cursor, data_pad);
1003 WARN_ON(!sg_is_last(cur_sg));
1004 sg_set_buf(cur_sg, epilogue,
1005 CEPH_GCM_BLOCK_LEN + (add_tag ? CEPH_GCM_TAG_LEN : 0));
1009 static int decrypt_preamble(struct ceph_connection *con)
1011 struct scatterlist sg;
1013 sg_init_one(&sg, con->v2.in_buf, CEPH_PREAMBLE_SECURE_LEN);
1014 return gcm_crypt(con, false, &sg, &sg, CEPH_PREAMBLE_SECURE_LEN);
1017 static int decrypt_control_remainder(struct ceph_connection *con)
1019 int ctrl_len = con->v2.in_desc.fd_lens[0];
1020 int rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
1021 int pt_len = padding_len(rem_len) + CEPH_GCM_TAG_LEN;
1022 struct scatterlist sgs[2];
1024 WARN_ON(con->v2.in_kvecs[0].iov_len != rem_len);
1025 WARN_ON(con->v2.in_kvecs[1].iov_len != pt_len);
1027 sg_init_table(sgs, 2);
1028 sg_set_buf(&sgs[0], con->v2.in_kvecs[0].iov_base, rem_len);
1029 sg_set_buf(&sgs[1], con->v2.in_buf, pt_len);
1031 return gcm_crypt(con, false, sgs, sgs,
1032 padded_len(rem_len) + CEPH_GCM_TAG_LEN);
1035 static int decrypt_message(struct ceph_connection *con)
1037 struct sg_table sgt = {};
1040 ret = setup_message_sgs(&sgt, con->in_msg, FRONT_PAD(con->v2.in_buf),
1041 MIDDLE_PAD(con->v2.in_buf), DATA_PAD(con->v2.in_buf),
1042 con->v2.in_buf, true);
1046 ret = gcm_crypt(con, false, sgt.sgl, sgt.sgl,
1047 tail_onwire_len(con->in_msg, true));
1050 sg_free_table(&sgt);
1054 static int prepare_banner(struct ceph_connection *con)
1056 int buf_len = CEPH_BANNER_V2_LEN + 2 + 8 + 8;
1059 buf = alloc_conn_buf(con, buf_len);
1064 ceph_encode_copy(&p, CEPH_BANNER_V2, CEPH_BANNER_V2_LEN);
1065 ceph_encode_16(&p, sizeof(u64) + sizeof(u64));
1066 ceph_encode_64(&p, CEPH_MSGR2_SUPPORTED_FEATURES);
1067 ceph_encode_64(&p, CEPH_MSGR2_REQUIRED_FEATURES);
1068 WARN_ON(p != buf + buf_len);
1070 add_out_kvec(con, buf, buf_len);
1071 add_out_sign_kvec(con, buf, buf_len);
1072 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
1079 * control body (ctrl_len bytes)
1080 * space for control crc
1082 * extdata (optional):
1083 * control body (extdata_len bytes)
1085 * Compute control crc and gather base and extdata into:
1088 * control body (ctrl_len + extdata_len bytes)
1091 * Preamble should already be encoded at the start of base.
1093 static void prepare_head_plain(struct ceph_connection *con, void *base,
1094 int ctrl_len, void *extdata, int extdata_len,
1097 int base_len = CEPH_PREAMBLE_LEN + ctrl_len + CEPH_CRC_LEN;
1098 void *crcp = base + base_len - CEPH_CRC_LEN;
1101 crc = crc32c(-1, CTRL_BODY(base), ctrl_len);
1103 crc = crc32c(crc, extdata, extdata_len);
1104 put_unaligned_le32(crc, crcp);
1107 add_out_kvec(con, base, base_len);
1109 add_out_sign_kvec(con, base, base_len);
1113 add_out_kvec(con, base, crcp - base);
1114 add_out_kvec(con, extdata, extdata_len);
1115 add_out_kvec(con, crcp, CEPH_CRC_LEN);
1117 add_out_sign_kvec(con, base, crcp - base);
1118 add_out_sign_kvec(con, extdata, extdata_len);
1119 add_out_sign_kvec(con, crcp, CEPH_CRC_LEN);
1123 static int prepare_head_secure_small(struct ceph_connection *con,
1124 void *base, int ctrl_len)
1126 struct scatterlist sg;
1129 /* inline buffer padding? */
1130 if (ctrl_len < CEPH_PREAMBLE_INLINE_LEN)
1131 memset(CTRL_BODY(base) + ctrl_len, 0,
1132 CEPH_PREAMBLE_INLINE_LEN - ctrl_len);
1134 sg_init_one(&sg, base, CEPH_PREAMBLE_SECURE_LEN);
1135 ret = gcm_crypt(con, true, &sg, &sg,
1136 CEPH_PREAMBLE_SECURE_LEN - CEPH_GCM_TAG_LEN);
1140 add_out_kvec(con, base, CEPH_PREAMBLE_SECURE_LEN);
1147 * control body (ctrl_len bytes)
1148 * space for padding, if needed
1149 * space for control remainder auth tag
1150 * space for preamble auth tag
1152 * Encrypt preamble and the inline portion, then encrypt the remainder
1156 * control body (48 bytes)
1158 * control body (ctrl_len - 48 bytes)
1159 * zero padding, if needed
1160 * control remainder auth tag
1162 * Preamble should already be encoded at the start of base.
1164 static int prepare_head_secure_big(struct ceph_connection *con,
1165 void *base, int ctrl_len)
1167 int rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
1168 void *rem = CTRL_BODY(base) + CEPH_PREAMBLE_INLINE_LEN;
1169 void *rem_tag = rem + padded_len(rem_len);
1170 void *pmbl_tag = rem_tag + CEPH_GCM_TAG_LEN;
1171 struct scatterlist sgs[2];
1174 sg_init_table(sgs, 2);
1175 sg_set_buf(&sgs[0], base, rem - base);
1176 sg_set_buf(&sgs[1], pmbl_tag, CEPH_GCM_TAG_LEN);
1177 ret = gcm_crypt(con, true, sgs, sgs, rem - base);
1181 /* control remainder padding? */
1182 if (need_padding(rem_len))
1183 memset(rem + rem_len, 0, padding_len(rem_len));
1185 sg_init_one(&sgs[0], rem, pmbl_tag - rem);
1186 ret = gcm_crypt(con, true, sgs, sgs, rem_tag - rem);
1190 add_out_kvec(con, base, rem - base);
1191 add_out_kvec(con, pmbl_tag, CEPH_GCM_TAG_LEN);
1192 add_out_kvec(con, rem, pmbl_tag - rem);
1196 static int __prepare_control(struct ceph_connection *con, int tag,
1197 void *base, int ctrl_len, void *extdata,
1198 int extdata_len, bool to_be_signed)
1200 int total_len = ctrl_len + extdata_len;
1201 struct ceph_frame_desc desc;
1204 dout("%s con %p tag %d len %d (%d+%d)\n", __func__, con, tag,
1205 total_len, ctrl_len, extdata_len);
1207 /* extdata may be vmalloc'ed but not base */
1208 if (WARN_ON(is_vmalloc_addr(base) || !ctrl_len))
1211 init_frame_desc(&desc, tag, &total_len, 1);
1212 encode_preamble(&desc, base);
1214 if (con_secure(con)) {
1215 if (WARN_ON(extdata_len || to_be_signed))
1218 if (ctrl_len <= CEPH_PREAMBLE_INLINE_LEN)
1219 /* fully inlined, inline buffer may need padding */
1220 ret = prepare_head_secure_small(con, base, ctrl_len);
1222 /* partially inlined, inline buffer is full */
1223 ret = prepare_head_secure_big(con, base, ctrl_len);
1227 prepare_head_plain(con, base, ctrl_len, extdata, extdata_len,
1231 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
1235 static int prepare_control(struct ceph_connection *con, int tag,
1236 void *base, int ctrl_len)
1238 return __prepare_control(con, tag, base, ctrl_len, NULL, 0, false);
1241 static int prepare_hello(struct ceph_connection *con)
1246 ctrl_len = 1 + ceph_entity_addr_encoding_len(&con->peer_addr);
1247 buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, false));
1252 ceph_encode_8(&p, CEPH_ENTITY_TYPE_CLIENT);
1253 ceph_encode_entity_addr(&p, &con->peer_addr);
1254 WARN_ON(p != CTRL_BODY(buf) + ctrl_len);
1256 return __prepare_control(con, FRAME_TAG_HELLO, buf, ctrl_len,
1260 /* so that head_onwire_len(AUTH_BUF_LEN, false) is 512 */
1261 #define AUTH_BUF_LEN (512 - CEPH_CRC_LEN - CEPH_PREAMBLE_PLAIN_LEN)
1263 static int prepare_auth_request(struct ceph_connection *con)
1265 void *authorizer, *authorizer_copy;
1266 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->get_auth_request(con, CTRL_BODY(buf), &ctrl_len,
1277 &authorizer, &authorizer_len);
1278 mutex_lock(&con->mutex);
1279 if (con->state != CEPH_CON_S_V2_HELLO) {
1280 dout("%s con %p state changed to %d\n", __func__, con,
1285 dout("%s con %p get_auth_request ret %d\n", __func__, con, ret);
1289 authorizer_copy = alloc_conn_buf(con, authorizer_len);
1290 if (!authorizer_copy)
1293 memcpy(authorizer_copy, authorizer, authorizer_len);
1295 return __prepare_control(con, FRAME_TAG_AUTH_REQUEST, buf, ctrl_len,
1296 authorizer_copy, authorizer_len, true);
1299 static int prepare_auth_request_more(struct ceph_connection *con,
1300 void *reply, int reply_len)
1302 int ctrl_len, authorizer_len;
1307 ctrl_len = AUTH_BUF_LEN;
1308 buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, false));
1312 mutex_unlock(&con->mutex);
1313 ret = con->ops->handle_auth_reply_more(con, reply, reply_len,
1314 CTRL_BODY(buf), &ctrl_len,
1315 &authorizer, &authorizer_len);
1316 mutex_lock(&con->mutex);
1317 if (con->state != CEPH_CON_S_V2_AUTH) {
1318 dout("%s con %p state changed to %d\n", __func__, con,
1323 dout("%s con %p handle_auth_reply_more ret %d\n", __func__, con, ret);
1327 return __prepare_control(con, FRAME_TAG_AUTH_REQUEST_MORE, buf,
1328 ctrl_len, authorizer, authorizer_len, true);
1331 static int prepare_auth_signature(struct ceph_connection *con)
1336 buf = alloc_conn_buf(con, head_onwire_len(SHA256_DIGEST_SIZE,
1341 ret = hmac_sha256(con, con->v2.in_sign_kvecs, con->v2.in_sign_kvec_cnt,
1346 return prepare_control(con, FRAME_TAG_AUTH_SIGNATURE, buf,
1347 SHA256_DIGEST_SIZE);
1350 static int prepare_client_ident(struct ceph_connection *con)
1352 struct ceph_entity_addr *my_addr = &con->msgr->inst.addr;
1353 struct ceph_client *client = from_msgr(con->msgr);
1354 u64 global_id = ceph_client_gid(client);
1358 WARN_ON(con->v2.server_cookie);
1359 WARN_ON(con->v2.connect_seq);
1360 WARN_ON(con->v2.peer_global_seq);
1362 if (!con->v2.client_cookie) {
1364 get_random_bytes(&con->v2.client_cookie,
1365 sizeof(con->v2.client_cookie));
1366 } while (!con->v2.client_cookie);
1367 dout("%s con %p generated cookie 0x%llx\n", __func__, con,
1368 con->v2.client_cookie);
1370 dout("%s con %p cookie already set 0x%llx\n", __func__, con,
1371 con->v2.client_cookie);
1374 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",
1375 __func__, con, ceph_pr_addr(my_addr), le32_to_cpu(my_addr->nonce),
1376 ceph_pr_addr(&con->peer_addr), le32_to_cpu(con->peer_addr.nonce),
1377 global_id, con->v2.global_seq, client->supported_features,
1378 client->required_features, con->v2.client_cookie);
1380 ctrl_len = 1 + 4 + ceph_entity_addr_encoding_len(my_addr) +
1381 ceph_entity_addr_encoding_len(&con->peer_addr) + 6 * 8;
1382 buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, con_secure(con)));
1387 ceph_encode_8(&p, 2); /* addrvec marker */
1388 ceph_encode_32(&p, 1); /* addr_cnt */
1389 ceph_encode_entity_addr(&p, my_addr);
1390 ceph_encode_entity_addr(&p, &con->peer_addr);
1391 ceph_encode_64(&p, global_id);
1392 ceph_encode_64(&p, con->v2.global_seq);
1393 ceph_encode_64(&p, client->supported_features);
1394 ceph_encode_64(&p, client->required_features);
1395 ceph_encode_64(&p, 0); /* flags */
1396 ceph_encode_64(&p, con->v2.client_cookie);
1397 WARN_ON(p != CTRL_BODY(buf) + ctrl_len);
1399 return prepare_control(con, FRAME_TAG_CLIENT_IDENT, buf, ctrl_len);
1402 static int prepare_session_reconnect(struct ceph_connection *con)
1404 struct ceph_entity_addr *my_addr = &con->msgr->inst.addr;
1408 WARN_ON(!con->v2.client_cookie);
1409 WARN_ON(!con->v2.server_cookie);
1410 WARN_ON(!con->v2.connect_seq);
1411 WARN_ON(!con->v2.peer_global_seq);
1413 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",
1414 __func__, con, ceph_pr_addr(my_addr), le32_to_cpu(my_addr->nonce),
1415 con->v2.client_cookie, con->v2.server_cookie, con->v2.global_seq,
1416 con->v2.connect_seq, con->in_seq);
1418 ctrl_len = 1 + 4 + ceph_entity_addr_encoding_len(my_addr) + 5 * 8;
1419 buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, con_secure(con)));
1424 ceph_encode_8(&p, 2); /* entity_addrvec_t marker */
1425 ceph_encode_32(&p, 1); /* my_addrs len */
1426 ceph_encode_entity_addr(&p, my_addr);
1427 ceph_encode_64(&p, con->v2.client_cookie);
1428 ceph_encode_64(&p, con->v2.server_cookie);
1429 ceph_encode_64(&p, con->v2.global_seq);
1430 ceph_encode_64(&p, con->v2.connect_seq);
1431 ceph_encode_64(&p, con->in_seq);
1432 WARN_ON(p != CTRL_BODY(buf) + ctrl_len);
1434 return prepare_control(con, FRAME_TAG_SESSION_RECONNECT, buf, ctrl_len);
1437 static int prepare_keepalive2(struct ceph_connection *con)
1439 struct ceph_timespec *ts = CTRL_BODY(con->v2.out_buf);
1440 struct timespec64 now;
1442 ktime_get_real_ts64(&now);
1443 dout("%s con %p timestamp %lld.%09ld\n", __func__, con, now.tv_sec,
1446 ceph_encode_timespec64(ts, &now);
1448 reset_out_kvecs(con);
1449 return prepare_control(con, FRAME_TAG_KEEPALIVE2, con->v2.out_buf,
1450 sizeof(struct ceph_timespec));
1453 static int prepare_ack(struct ceph_connection *con)
1457 dout("%s con %p in_seq_acked %llu -> %llu\n", __func__, con,
1458 con->in_seq_acked, con->in_seq);
1459 con->in_seq_acked = con->in_seq;
1461 p = CTRL_BODY(con->v2.out_buf);
1462 ceph_encode_64(&p, con->in_seq_acked);
1464 reset_out_kvecs(con);
1465 return prepare_control(con, FRAME_TAG_ACK, con->v2.out_buf, 8);
1468 static void prepare_epilogue_plain(struct ceph_connection *con, bool aborted)
1470 dout("%s con %p msg %p aborted %d crcs %u %u %u\n", __func__, con,
1471 con->out_msg, aborted, con->v2.out_epil.front_crc,
1472 con->v2.out_epil.middle_crc, con->v2.out_epil.data_crc);
1474 encode_epilogue_plain(con, aborted);
1475 add_out_kvec(con, &con->v2.out_epil, CEPH_EPILOGUE_PLAIN_LEN);
1479 * For "used" empty segments, crc is -1. For unused (trailing)
1480 * segments, crc is 0.
1482 static void prepare_message_plain(struct ceph_connection *con)
1484 struct ceph_msg *msg = con->out_msg;
1486 prepare_head_plain(con, con->v2.out_buf,
1487 sizeof(struct ceph_msg_header2), NULL, 0, false);
1489 if (!front_len(msg) && !middle_len(msg)) {
1490 if (!data_len(msg)) {
1492 * Empty message: once the head is written,
1493 * we are done -- there is no epilogue.
1495 con->v2.out_state = OUT_S_FINISH_MESSAGE;
1499 con->v2.out_epil.front_crc = -1;
1500 con->v2.out_epil.middle_crc = -1;
1501 con->v2.out_state = OUT_S_QUEUE_DATA;
1505 if (front_len(msg)) {
1506 con->v2.out_epil.front_crc = crc32c(-1, msg->front.iov_base,
1508 add_out_kvec(con, msg->front.iov_base, front_len(msg));
1510 /* middle (at least) is there, checked above */
1511 con->v2.out_epil.front_crc = -1;
1514 if (middle_len(msg)) {
1515 con->v2.out_epil.middle_crc =
1516 crc32c(-1, msg->middle->vec.iov_base, middle_len(msg));
1517 add_out_kvec(con, msg->middle->vec.iov_base, middle_len(msg));
1519 con->v2.out_epil.middle_crc = data_len(msg) ? -1 : 0;
1522 if (data_len(msg)) {
1523 con->v2.out_state = OUT_S_QUEUE_DATA;
1525 con->v2.out_epil.data_crc = 0;
1526 prepare_epilogue_plain(con, false);
1527 con->v2.out_state = OUT_S_FINISH_MESSAGE;
1532 * Unfortunately the kernel crypto API doesn't support streaming
1533 * (piecewise) operation for AEAD algorithms, so we can't get away
1534 * with a fixed size buffer and a couple sgs. Instead, we have to
1535 * allocate pages for the entire tail of the message (currently up
1536 * to ~32M) and two sgs arrays (up to ~256K each)...
1538 static int prepare_message_secure(struct ceph_connection *con)
1540 void *zerop = page_address(ceph_zero_page);
1541 struct sg_table enc_sgt = {};
1542 struct sg_table sgt = {};
1543 struct page **enc_pages;
1548 ret = prepare_head_secure_small(con, con->v2.out_buf,
1549 sizeof(struct ceph_msg_header2));
1553 tail_len = tail_onwire_len(con->out_msg, true);
1556 * Empty message: once the head is written,
1557 * we are done -- there is no epilogue.
1559 con->v2.out_state = OUT_S_FINISH_MESSAGE;
1563 encode_epilogue_secure(con, false);
1564 ret = setup_message_sgs(&sgt, con->out_msg, zerop, zerop, zerop,
1565 &con->v2.out_epil, false);
1569 enc_page_cnt = calc_pages_for(0, tail_len);
1570 enc_pages = ceph_alloc_page_vector(enc_page_cnt, GFP_NOIO);
1571 if (IS_ERR(enc_pages)) {
1572 ret = PTR_ERR(enc_pages);
1576 WARN_ON(con->v2.out_enc_pages || con->v2.out_enc_page_cnt);
1577 con->v2.out_enc_pages = enc_pages;
1578 con->v2.out_enc_page_cnt = enc_page_cnt;
1579 con->v2.out_enc_resid = tail_len;
1580 con->v2.out_enc_i = 0;
1582 ret = sg_alloc_table_from_pages(&enc_sgt, enc_pages, enc_page_cnt,
1583 0, tail_len, GFP_NOIO);
1587 ret = gcm_crypt(con, true, sgt.sgl, enc_sgt.sgl,
1588 tail_len - CEPH_GCM_TAG_LEN);
1592 dout("%s con %p msg %p sg_cnt %d enc_page_cnt %d\n", __func__, con,
1593 con->out_msg, sgt.orig_nents, enc_page_cnt);
1594 con->v2.out_state = OUT_S_QUEUE_ENC_PAGE;
1597 sg_free_table(&sgt);
1598 sg_free_table(&enc_sgt);
1602 static int prepare_message(struct ceph_connection *con)
1605 sizeof(struct ceph_msg_header2),
1606 front_len(con->out_msg),
1607 middle_len(con->out_msg),
1608 data_len(con->out_msg)
1610 struct ceph_frame_desc desc;
1613 dout("%s con %p msg %p logical %d+%d+%d+%d\n", __func__, con,
1614 con->out_msg, lens[0], lens[1], lens[2], lens[3]);
1616 if (con->in_seq > con->in_seq_acked) {
1617 dout("%s con %p in_seq_acked %llu -> %llu\n", __func__, con,
1618 con->in_seq_acked, con->in_seq);
1619 con->in_seq_acked = con->in_seq;
1622 reset_out_kvecs(con);
1623 init_frame_desc(&desc, FRAME_TAG_MESSAGE, lens, 4);
1624 encode_preamble(&desc, con->v2.out_buf);
1625 fill_header2(CTRL_BODY(con->v2.out_buf), &con->out_msg->hdr,
1628 if (con_secure(con)) {
1629 ret = prepare_message_secure(con);
1633 prepare_message_plain(con);
1636 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
1640 static int prepare_read_banner_prefix(struct ceph_connection *con)
1644 buf = alloc_conn_buf(con, CEPH_BANNER_V2_PREFIX_LEN);
1648 reset_in_kvecs(con);
1649 add_in_kvec(con, buf, CEPH_BANNER_V2_PREFIX_LEN);
1650 add_in_sign_kvec(con, buf, CEPH_BANNER_V2_PREFIX_LEN);
1651 con->state = CEPH_CON_S_V2_BANNER_PREFIX;
1655 static int prepare_read_banner_payload(struct ceph_connection *con,
1660 buf = alloc_conn_buf(con, payload_len);
1664 reset_in_kvecs(con);
1665 add_in_kvec(con, buf, payload_len);
1666 add_in_sign_kvec(con, buf, payload_len);
1667 con->state = CEPH_CON_S_V2_BANNER_PAYLOAD;
1671 static void prepare_read_preamble(struct ceph_connection *con)
1673 reset_in_kvecs(con);
1674 add_in_kvec(con, con->v2.in_buf,
1675 con_secure(con) ? CEPH_PREAMBLE_SECURE_LEN :
1676 CEPH_PREAMBLE_PLAIN_LEN);
1677 con->v2.in_state = IN_S_HANDLE_PREAMBLE;
1680 static int prepare_read_control(struct ceph_connection *con)
1682 int ctrl_len = con->v2.in_desc.fd_lens[0];
1686 reset_in_kvecs(con);
1687 if (con->state == CEPH_CON_S_V2_HELLO ||
1688 con->state == CEPH_CON_S_V2_AUTH) {
1689 head_len = head_onwire_len(ctrl_len, false);
1690 buf = alloc_conn_buf(con, head_len);
1694 /* preserve preamble */
1695 memcpy(buf, con->v2.in_buf, CEPH_PREAMBLE_LEN);
1697 add_in_kvec(con, CTRL_BODY(buf), ctrl_len);
1698 add_in_kvec(con, CTRL_BODY(buf) + ctrl_len, CEPH_CRC_LEN);
1699 add_in_sign_kvec(con, buf, head_len);
1701 if (ctrl_len > CEPH_PREAMBLE_INLINE_LEN) {
1702 buf = alloc_conn_buf(con, ctrl_len);
1706 add_in_kvec(con, buf, ctrl_len);
1708 add_in_kvec(con, CTRL_BODY(con->v2.in_buf), ctrl_len);
1710 add_in_kvec(con, con->v2.in_buf, CEPH_CRC_LEN);
1712 con->v2.in_state = IN_S_HANDLE_CONTROL;
1716 static int prepare_read_control_remainder(struct ceph_connection *con)
1718 int ctrl_len = con->v2.in_desc.fd_lens[0];
1719 int rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
1722 buf = alloc_conn_buf(con, ctrl_len);
1726 memcpy(buf, CTRL_BODY(con->v2.in_buf), CEPH_PREAMBLE_INLINE_LEN);
1728 reset_in_kvecs(con);
1729 add_in_kvec(con, buf + CEPH_PREAMBLE_INLINE_LEN, rem_len);
1730 add_in_kvec(con, con->v2.in_buf,
1731 padding_len(rem_len) + CEPH_GCM_TAG_LEN);
1732 con->v2.in_state = IN_S_HANDLE_CONTROL_REMAINDER;
1736 static void prepare_read_data(struct ceph_connection *con)
1740 if (!con_secure(con))
1741 con->in_data_crc = -1;
1742 ceph_msg_data_cursor_init(&con->v2.in_cursor, con->in_msg,
1743 data_len(con->in_msg));
1745 get_bvec_at(&con->v2.in_cursor, &bv);
1746 set_in_bvec(con, &bv);
1747 con->v2.in_state = IN_S_PREPARE_READ_DATA_CONT;
1750 static void prepare_read_data_cont(struct ceph_connection *con)
1754 if (!con_secure(con))
1755 con->in_data_crc = ceph_crc32c_page(con->in_data_crc,
1756 con->v2.in_bvec.bv_page,
1757 con->v2.in_bvec.bv_offset,
1758 con->v2.in_bvec.bv_len);
1760 ceph_msg_data_advance(&con->v2.in_cursor, con->v2.in_bvec.bv_len);
1761 if (con->v2.in_cursor.total_resid) {
1762 get_bvec_at(&con->v2.in_cursor, &bv);
1763 set_in_bvec(con, &bv);
1764 WARN_ON(con->v2.in_state != IN_S_PREPARE_READ_DATA_CONT);
1769 * We've read all data. Prepare to read data padding (if any)
1772 reset_in_kvecs(con);
1773 if (con_secure(con)) {
1774 if (need_padding(data_len(con->in_msg)))
1775 add_in_kvec(con, DATA_PAD(con->v2.in_buf),
1776 padding_len(data_len(con->in_msg)));
1777 add_in_kvec(con, con->v2.in_buf, CEPH_EPILOGUE_SECURE_LEN);
1779 add_in_kvec(con, con->v2.in_buf, CEPH_EPILOGUE_PLAIN_LEN);
1781 con->v2.in_state = IN_S_HANDLE_EPILOGUE;
1784 static void __finish_skip(struct ceph_connection *con)
1787 prepare_read_preamble(con);
1790 static void prepare_skip_message(struct ceph_connection *con)
1792 struct ceph_frame_desc *desc = &con->v2.in_desc;
1795 dout("%s con %p %d+%d+%d\n", __func__, con, desc->fd_lens[1],
1796 desc->fd_lens[2], desc->fd_lens[3]);
1798 tail_len = __tail_onwire_len(desc->fd_lens[1], desc->fd_lens[2],
1799 desc->fd_lens[3], con_secure(con));
1803 set_in_skip(con, tail_len);
1804 con->v2.in_state = IN_S_FINISH_SKIP;
1808 static int process_banner_prefix(struct ceph_connection *con)
1813 WARN_ON(con->v2.in_kvecs[0].iov_len != CEPH_BANNER_V2_PREFIX_LEN);
1815 p = con->v2.in_kvecs[0].iov_base;
1816 if (memcmp(p, CEPH_BANNER_V2, CEPH_BANNER_V2_LEN)) {
1817 if (!memcmp(p, CEPH_BANNER, CEPH_BANNER_LEN))
1818 con->error_msg = "server is speaking msgr1 protocol";
1820 con->error_msg = "protocol error, bad banner";
1824 p += CEPH_BANNER_V2_LEN;
1825 payload_len = ceph_decode_16(&p);
1826 dout("%s con %p payload_len %d\n", __func__, con, payload_len);
1828 return prepare_read_banner_payload(con, payload_len);
1831 static int process_banner_payload(struct ceph_connection *con)
1833 void *end = con->v2.in_kvecs[0].iov_base + con->v2.in_kvecs[0].iov_len;
1834 u64 feat = CEPH_MSGR2_SUPPORTED_FEATURES;
1835 u64 req_feat = CEPH_MSGR2_REQUIRED_FEATURES;
1836 u64 server_feat, server_req_feat;
1840 p = con->v2.in_kvecs[0].iov_base;
1841 ceph_decode_64_safe(&p, end, server_feat, bad);
1842 ceph_decode_64_safe(&p, end, server_req_feat, bad);
1844 dout("%s con %p server_feat 0x%llx server_req_feat 0x%llx\n",
1845 __func__, con, server_feat, server_req_feat);
1847 if (req_feat & ~server_feat) {
1848 pr_err("msgr2 feature set mismatch: my required > server's supported 0x%llx, need 0x%llx\n",
1849 server_feat, req_feat & ~server_feat);
1850 con->error_msg = "missing required protocol features";
1853 if (server_req_feat & ~feat) {
1854 pr_err("msgr2 feature set mismatch: server's required > my supported 0x%llx, missing 0x%llx\n",
1855 feat, server_req_feat & ~feat);
1856 con->error_msg = "missing required protocol features";
1860 /* no reset_out_kvecs() as our banner may still be pending */
1861 ret = prepare_hello(con);
1863 pr_err("prepare_hello failed: %d\n", ret);
1867 con->state = CEPH_CON_S_V2_HELLO;
1868 prepare_read_preamble(con);
1872 pr_err("failed to decode banner payload\n");
1876 static int process_hello(struct ceph_connection *con, void *p, void *end)
1878 struct ceph_entity_addr *my_addr = &con->msgr->inst.addr;
1879 struct ceph_entity_addr addr_for_me;
1883 if (con->state != CEPH_CON_S_V2_HELLO) {
1884 con->error_msg = "protocol error, unexpected hello";
1888 ceph_decode_8_safe(&p, end, entity_type, bad);
1889 ret = ceph_decode_entity_addr(&p, end, &addr_for_me);
1891 pr_err("failed to decode addr_for_me: %d\n", ret);
1895 dout("%s con %p entity_type %d addr_for_me %s\n", __func__, con,
1896 entity_type, ceph_pr_addr(&addr_for_me));
1898 if (entity_type != con->peer_name.type) {
1899 pr_err("bad peer type, want %d, got %d\n",
1900 con->peer_name.type, entity_type);
1901 con->error_msg = "wrong peer at address";
1906 * Set our address to the address our first peer (i.e. monitor)
1907 * sees that we are connecting from. If we are behind some sort
1908 * of NAT and want to be identified by some private (not NATed)
1909 * address, ip option should be used.
1911 if (ceph_addr_is_blank(my_addr)) {
1912 memcpy(&my_addr->in_addr, &addr_for_me.in_addr,
1913 sizeof(my_addr->in_addr));
1914 ceph_addr_set_port(my_addr, 0);
1915 dout("%s con %p set my addr %s, as seen by peer %s\n",
1916 __func__, con, ceph_pr_addr(my_addr),
1917 ceph_pr_addr(&con->peer_addr));
1919 dout("%s con %p my addr already set %s\n",
1920 __func__, con, ceph_pr_addr(my_addr));
1923 WARN_ON(ceph_addr_is_blank(my_addr) || ceph_addr_port(my_addr));
1924 WARN_ON(my_addr->type != CEPH_ENTITY_ADDR_TYPE_ANY);
1925 WARN_ON(!my_addr->nonce);
1927 /* no reset_out_kvecs() as our hello may still be pending */
1928 ret = prepare_auth_request(con);
1931 pr_err("prepare_auth_request failed: %d\n", ret);
1935 con->state = CEPH_CON_S_V2_AUTH;
1939 pr_err("failed to decode hello\n");
1943 static int process_auth_bad_method(struct ceph_connection *con,
1946 int allowed_protos[8], allowed_modes[8];
1947 int allowed_proto_cnt, allowed_mode_cnt;
1948 int used_proto, result;
1952 if (con->state != CEPH_CON_S_V2_AUTH) {
1953 con->error_msg = "protocol error, unexpected auth_bad_method";
1957 ceph_decode_32_safe(&p, end, used_proto, bad);
1958 ceph_decode_32_safe(&p, end, result, bad);
1959 dout("%s con %p used_proto %d result %d\n", __func__, con, used_proto,
1962 ceph_decode_32_safe(&p, end, allowed_proto_cnt, bad);
1963 if (allowed_proto_cnt > ARRAY_SIZE(allowed_protos)) {
1964 pr_err("allowed_protos too big %d\n", allowed_proto_cnt);
1967 for (i = 0; i < allowed_proto_cnt; i++) {
1968 ceph_decode_32_safe(&p, end, allowed_protos[i], bad);
1969 dout("%s con %p allowed_protos[%d] %d\n", __func__, con,
1970 i, allowed_protos[i]);
1973 ceph_decode_32_safe(&p, end, allowed_mode_cnt, bad);
1974 if (allowed_mode_cnt > ARRAY_SIZE(allowed_modes)) {
1975 pr_err("allowed_modes too big %d\n", allowed_mode_cnt);
1978 for (i = 0; i < allowed_mode_cnt; i++) {
1979 ceph_decode_32_safe(&p, end, allowed_modes[i], bad);
1980 dout("%s con %p allowed_modes[%d] %d\n", __func__, con,
1981 i, allowed_modes[i]);
1984 mutex_unlock(&con->mutex);
1985 ret = con->ops->handle_auth_bad_method(con, used_proto, result,
1990 mutex_lock(&con->mutex);
1991 if (con->state != CEPH_CON_S_V2_AUTH) {
1992 dout("%s con %p state changed to %d\n", __func__, con,
1997 dout("%s con %p handle_auth_bad_method ret %d\n", __func__, con, ret);
2001 pr_err("failed to decode auth_bad_method\n");
2005 static int process_auth_reply_more(struct ceph_connection *con,
2011 if (con->state != CEPH_CON_S_V2_AUTH) {
2012 con->error_msg = "protocol error, unexpected auth_reply_more";
2016 ceph_decode_32_safe(&p, end, payload_len, bad);
2017 ceph_decode_need(&p, end, payload_len, bad);
2019 dout("%s con %p payload_len %d\n", __func__, con, payload_len);
2021 reset_out_kvecs(con);
2022 ret = prepare_auth_request_more(con, p, payload_len);
2025 pr_err("prepare_auth_request_more failed: %d\n", ret);
2032 pr_err("failed to decode auth_reply_more\n");
2037 * Align session_key and con_secret to avoid GFP_ATOMIC allocation
2038 * inside crypto_shash_setkey() and crypto_aead_setkey() called from
2039 * setup_crypto(). __aligned(16) isn't guaranteed to work for stack
2040 * objects, so do it by hand.
2042 static int process_auth_done(struct ceph_connection *con, void *p, void *end)
2044 u8 session_key_buf[CEPH_KEY_LEN + 16];
2045 u8 con_secret_buf[CEPH_MAX_CON_SECRET_LEN + 16];
2046 u8 *session_key = PTR_ALIGN(&session_key_buf[0], 16);
2047 u8 *con_secret = PTR_ALIGN(&con_secret_buf[0], 16);
2048 int session_key_len, con_secret_len;
2053 if (con->state != CEPH_CON_S_V2_AUTH) {
2054 con->error_msg = "protocol error, unexpected auth_done";
2058 ceph_decode_64_safe(&p, end, global_id, bad);
2059 ceph_decode_32_safe(&p, end, con->v2.con_mode, bad);
2060 ceph_decode_32_safe(&p, end, payload_len, bad);
2062 dout("%s con %p global_id %llu con_mode %d payload_len %d\n",
2063 __func__, con, global_id, con->v2.con_mode, payload_len);
2065 mutex_unlock(&con->mutex);
2066 session_key_len = 0;
2068 ret = con->ops->handle_auth_done(con, global_id, p, payload_len,
2069 session_key, &session_key_len,
2070 con_secret, &con_secret_len);
2071 mutex_lock(&con->mutex);
2072 if (con->state != CEPH_CON_S_V2_AUTH) {
2073 dout("%s con %p state changed to %d\n", __func__, con,
2079 dout("%s con %p handle_auth_done ret %d\n", __func__, con, ret);
2083 ret = setup_crypto(con, session_key, session_key_len, con_secret,
2088 reset_out_kvecs(con);
2089 ret = prepare_auth_signature(con);
2091 pr_err("prepare_auth_signature failed: %d\n", ret);
2095 con->state = CEPH_CON_S_V2_AUTH_SIGNATURE;
2098 memzero_explicit(session_key_buf, sizeof(session_key_buf));
2099 memzero_explicit(con_secret_buf, sizeof(con_secret_buf));
2103 pr_err("failed to decode auth_done\n");
2107 static int process_auth_signature(struct ceph_connection *con,
2110 u8 hmac[SHA256_DIGEST_SIZE];
2113 if (con->state != CEPH_CON_S_V2_AUTH_SIGNATURE) {
2114 con->error_msg = "protocol error, unexpected auth_signature";
2118 ret = hmac_sha256(con, con->v2.out_sign_kvecs,
2119 con->v2.out_sign_kvec_cnt, hmac);
2123 ceph_decode_need(&p, end, SHA256_DIGEST_SIZE, bad);
2124 if (crypto_memneq(p, hmac, SHA256_DIGEST_SIZE)) {
2125 con->error_msg = "integrity error, bad auth signature";
2129 dout("%s con %p auth signature ok\n", __func__, con);
2131 /* no reset_out_kvecs() as our auth_signature may still be pending */
2132 if (!con->v2.server_cookie) {
2133 ret = prepare_client_ident(con);
2135 pr_err("prepare_client_ident failed: %d\n", ret);
2139 con->state = CEPH_CON_S_V2_SESSION_CONNECT;
2141 ret = prepare_session_reconnect(con);
2143 pr_err("prepare_session_reconnect failed: %d\n", ret);
2147 con->state = CEPH_CON_S_V2_SESSION_RECONNECT;
2153 pr_err("failed to decode auth_signature\n");
2157 static int process_server_ident(struct ceph_connection *con,
2160 struct ceph_client *client = from_msgr(con->msgr);
2161 u64 features, required_features;
2162 struct ceph_entity_addr addr;
2169 if (con->state != CEPH_CON_S_V2_SESSION_CONNECT) {
2170 con->error_msg = "protocol error, unexpected server_ident";
2174 ret = ceph_decode_entity_addrvec(&p, end, true, &addr);
2176 pr_err("failed to decode server addrs: %d\n", ret);
2180 ceph_decode_64_safe(&p, end, global_id, bad);
2181 ceph_decode_64_safe(&p, end, global_seq, bad);
2182 ceph_decode_64_safe(&p, end, features, bad);
2183 ceph_decode_64_safe(&p, end, required_features, bad);
2184 ceph_decode_64_safe(&p, end, flags, bad);
2185 ceph_decode_64_safe(&p, end, cookie, bad);
2187 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",
2188 __func__, con, ceph_pr_addr(&addr), le32_to_cpu(addr.nonce),
2189 global_id, global_seq, features, required_features, flags, cookie);
2191 /* is this who we intended to talk to? */
2192 if (memcmp(&addr, &con->peer_addr, sizeof(con->peer_addr))) {
2193 pr_err("bad peer addr/nonce, want %s/%u, got %s/%u\n",
2194 ceph_pr_addr(&con->peer_addr),
2195 le32_to_cpu(con->peer_addr.nonce),
2196 ceph_pr_addr(&addr), le32_to_cpu(addr.nonce));
2197 con->error_msg = "wrong peer at address";
2201 if (client->required_features & ~features) {
2202 pr_err("RADOS feature set mismatch: my required > server's supported 0x%llx, need 0x%llx\n",
2203 features, client->required_features & ~features);
2204 con->error_msg = "missing required protocol features";
2209 * Both name->type and name->num are set in ceph_con_open() but
2210 * name->num may be bogus in the initial monmap. name->type is
2211 * verified in handle_hello().
2213 WARN_ON(!con->peer_name.type);
2214 con->peer_name.num = cpu_to_le64(global_id);
2215 con->v2.peer_global_seq = global_seq;
2216 con->peer_features = features;
2217 WARN_ON(required_features & ~client->supported_features);
2218 con->v2.server_cookie = cookie;
2220 if (flags & CEPH_MSG_CONNECT_LOSSY) {
2221 ceph_con_flag_set(con, CEPH_CON_F_LOSSYTX);
2222 WARN_ON(con->v2.server_cookie);
2224 WARN_ON(!con->v2.server_cookie);
2227 clear_in_sign_kvecs(con);
2228 clear_out_sign_kvecs(con);
2229 free_conn_bufs(con);
2230 con->delay = 0; /* reset backoff memory */
2232 con->state = CEPH_CON_S_OPEN;
2233 con->v2.out_state = OUT_S_GET_NEXT;
2237 pr_err("failed to decode server_ident\n");
2241 static int process_ident_missing_features(struct ceph_connection *con,
2244 struct ceph_client *client = from_msgr(con->msgr);
2245 u64 missing_features;
2247 if (con->state != CEPH_CON_S_V2_SESSION_CONNECT) {
2248 con->error_msg = "protocol error, unexpected ident_missing_features";
2252 ceph_decode_64_safe(&p, end, missing_features, bad);
2253 pr_err("RADOS feature set mismatch: server's required > my supported 0x%llx, missing 0x%llx\n",
2254 client->supported_features, missing_features);
2255 con->error_msg = "missing required protocol features";
2259 pr_err("failed to decode ident_missing_features\n");
2263 static int process_session_reconnect_ok(struct ceph_connection *con,
2268 if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
2269 con->error_msg = "protocol error, unexpected session_reconnect_ok";
2273 ceph_decode_64_safe(&p, end, seq, bad);
2275 dout("%s con %p seq %llu\n", __func__, con, seq);
2276 ceph_con_discard_requeued(con, seq);
2278 clear_in_sign_kvecs(con);
2279 clear_out_sign_kvecs(con);
2280 free_conn_bufs(con);
2281 con->delay = 0; /* reset backoff memory */
2283 con->state = CEPH_CON_S_OPEN;
2284 con->v2.out_state = OUT_S_GET_NEXT;
2288 pr_err("failed to decode session_reconnect_ok\n");
2292 static int process_session_retry(struct ceph_connection *con,
2298 if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
2299 con->error_msg = "protocol error, unexpected session_retry";
2303 ceph_decode_64_safe(&p, end, connect_seq, bad);
2305 dout("%s con %p connect_seq %llu\n", __func__, con, connect_seq);
2306 WARN_ON(connect_seq <= con->v2.connect_seq);
2307 con->v2.connect_seq = connect_seq + 1;
2309 free_conn_bufs(con);
2311 reset_out_kvecs(con);
2312 ret = prepare_session_reconnect(con);
2314 pr_err("prepare_session_reconnect (cseq) failed: %d\n", ret);
2321 pr_err("failed to decode session_retry\n");
2325 static int process_session_retry_global(struct ceph_connection *con,
2331 if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
2332 con->error_msg = "protocol error, unexpected session_retry_global";
2336 ceph_decode_64_safe(&p, end, global_seq, bad);
2338 dout("%s con %p global_seq %llu\n", __func__, con, global_seq);
2339 WARN_ON(global_seq <= con->v2.global_seq);
2340 con->v2.global_seq = ceph_get_global_seq(con->msgr, global_seq);
2342 free_conn_bufs(con);
2344 reset_out_kvecs(con);
2345 ret = prepare_session_reconnect(con);
2347 pr_err("prepare_session_reconnect (gseq) failed: %d\n", ret);
2354 pr_err("failed to decode session_retry_global\n");
2358 static int process_session_reset(struct ceph_connection *con,
2364 if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
2365 con->error_msg = "protocol error, unexpected session_reset";
2369 ceph_decode_8_safe(&p, end, full, bad);
2371 con->error_msg = "protocol error, bad session_reset";
2375 pr_info("%s%lld %s session reset\n", ENTITY_NAME(con->peer_name),
2376 ceph_pr_addr(&con->peer_addr));
2377 ceph_con_reset_session(con);
2379 mutex_unlock(&con->mutex);
2380 if (con->ops->peer_reset)
2381 con->ops->peer_reset(con);
2382 mutex_lock(&con->mutex);
2383 if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
2384 dout("%s con %p state changed to %d\n", __func__, con,
2389 free_conn_bufs(con);
2391 reset_out_kvecs(con);
2392 ret = prepare_client_ident(con);
2394 pr_err("prepare_client_ident (rst) failed: %d\n", ret);
2398 con->state = CEPH_CON_S_V2_SESSION_CONNECT;
2402 pr_err("failed to decode session_reset\n");
2406 static int process_keepalive2_ack(struct ceph_connection *con,
2409 if (con->state != CEPH_CON_S_OPEN) {
2410 con->error_msg = "protocol error, unexpected keepalive2_ack";
2414 ceph_decode_need(&p, end, sizeof(struct ceph_timespec), bad);
2415 ceph_decode_timespec64(&con->last_keepalive_ack, p);
2417 dout("%s con %p timestamp %lld.%09ld\n", __func__, con,
2418 con->last_keepalive_ack.tv_sec, con->last_keepalive_ack.tv_nsec);
2423 pr_err("failed to decode keepalive2_ack\n");
2427 static int process_ack(struct ceph_connection *con, void *p, void *end)
2431 if (con->state != CEPH_CON_S_OPEN) {
2432 con->error_msg = "protocol error, unexpected ack";
2436 ceph_decode_64_safe(&p, end, seq, bad);
2438 dout("%s con %p seq %llu\n", __func__, con, seq);
2439 ceph_con_discard_sent(con, seq);
2443 pr_err("failed to decode ack\n");
2447 static int process_control(struct ceph_connection *con, void *p, void *end)
2449 int tag = con->v2.in_desc.fd_tag;
2452 dout("%s con %p tag %d len %d\n", __func__, con, tag, (int)(end - p));
2455 case FRAME_TAG_HELLO:
2456 ret = process_hello(con, p, end);
2458 case FRAME_TAG_AUTH_BAD_METHOD:
2459 ret = process_auth_bad_method(con, p, end);
2461 case FRAME_TAG_AUTH_REPLY_MORE:
2462 ret = process_auth_reply_more(con, p, end);
2464 case FRAME_TAG_AUTH_DONE:
2465 ret = process_auth_done(con, p, end);
2467 case FRAME_TAG_AUTH_SIGNATURE:
2468 ret = process_auth_signature(con, p, end);
2470 case FRAME_TAG_SERVER_IDENT:
2471 ret = process_server_ident(con, p, end);
2473 case FRAME_TAG_IDENT_MISSING_FEATURES:
2474 ret = process_ident_missing_features(con, p, end);
2476 case FRAME_TAG_SESSION_RECONNECT_OK:
2477 ret = process_session_reconnect_ok(con, p, end);
2479 case FRAME_TAG_SESSION_RETRY:
2480 ret = process_session_retry(con, p, end);
2482 case FRAME_TAG_SESSION_RETRY_GLOBAL:
2483 ret = process_session_retry_global(con, p, end);
2485 case FRAME_TAG_SESSION_RESET:
2486 ret = process_session_reset(con, p, end);
2488 case FRAME_TAG_KEEPALIVE2_ACK:
2489 ret = process_keepalive2_ack(con, p, end);
2492 ret = process_ack(con, p, end);
2495 pr_err("bad tag %d\n", tag);
2496 con->error_msg = "protocol error, bad tag";
2500 dout("%s con %p error %d\n", __func__, con, ret);
2504 prepare_read_preamble(con);
2510 * 1 - con->in_msg set, read message
2514 static int process_message_header(struct ceph_connection *con,
2517 struct ceph_frame_desc *desc = &con->v2.in_desc;
2518 struct ceph_msg_header2 *hdr2 = p;
2519 struct ceph_msg_header hdr;
2525 seq = le64_to_cpu(hdr2->seq);
2526 if ((s64)seq - (s64)con->in_seq < 1) {
2527 pr_info("%s%lld %s skipping old message: seq %llu, expected %llu\n",
2528 ENTITY_NAME(con->peer_name),
2529 ceph_pr_addr(&con->peer_addr),
2530 seq, con->in_seq + 1);
2533 if ((s64)seq - (s64)con->in_seq > 1) {
2534 pr_err("bad seq %llu, expected %llu\n", seq, con->in_seq + 1);
2535 con->error_msg = "bad message sequence # for incoming message";
2539 ceph_con_discard_sent(con, le64_to_cpu(hdr2->ack_seq));
2541 fill_header(&hdr, hdr2, desc->fd_lens[1], desc->fd_lens[2],
2542 desc->fd_lens[3], &con->peer_name);
2543 ret = ceph_con_in_msg_alloc(con, &hdr, &skip);
2547 WARN_ON(!con->in_msg ^ skip);
2551 WARN_ON(!con->in_msg);
2552 WARN_ON(con->in_msg->con != con);
2556 static int process_message(struct ceph_connection *con)
2558 ceph_con_process_message(con);
2561 * We could have been closed by ceph_con_close() because
2562 * ceph_con_process_message() temporarily drops con->mutex.
2564 if (con->state != CEPH_CON_S_OPEN) {
2565 dout("%s con %p state changed to %d\n", __func__, con,
2570 prepare_read_preamble(con);
2574 static int __handle_control(struct ceph_connection *con, void *p)
2576 void *end = p + con->v2.in_desc.fd_lens[0];
2577 struct ceph_msg *msg;
2580 if (con->v2.in_desc.fd_tag != FRAME_TAG_MESSAGE)
2581 return process_control(con, p, end);
2583 ret = process_message_header(con, p, end);
2587 prepare_skip_message(con);
2591 msg = con->in_msg; /* set in process_message_header() */
2592 if (!front_len(msg) && !middle_len(msg)) {
2594 return process_message(con);
2596 prepare_read_data(con);
2600 reset_in_kvecs(con);
2601 if (front_len(msg)) {
2602 WARN_ON(front_len(msg) > msg->front_alloc_len);
2603 add_in_kvec(con, msg->front.iov_base, front_len(msg));
2604 msg->front.iov_len = front_len(msg);
2606 if (con_secure(con) && need_padding(front_len(msg)))
2607 add_in_kvec(con, FRONT_PAD(con->v2.in_buf),
2608 padding_len(front_len(msg)));
2610 msg->front.iov_len = 0;
2612 if (middle_len(msg)) {
2613 WARN_ON(middle_len(msg) > msg->middle->alloc_len);
2614 add_in_kvec(con, msg->middle->vec.iov_base, middle_len(msg));
2615 msg->middle->vec.iov_len = middle_len(msg);
2617 if (con_secure(con) && need_padding(middle_len(msg)))
2618 add_in_kvec(con, MIDDLE_PAD(con->v2.in_buf),
2619 padding_len(middle_len(msg)));
2620 } else if (msg->middle) {
2621 msg->middle->vec.iov_len = 0;
2624 if (data_len(msg)) {
2625 con->v2.in_state = IN_S_PREPARE_READ_DATA;
2627 add_in_kvec(con, con->v2.in_buf,
2628 con_secure(con) ? CEPH_EPILOGUE_SECURE_LEN :
2629 CEPH_EPILOGUE_PLAIN_LEN);
2630 con->v2.in_state = IN_S_HANDLE_EPILOGUE;
2635 static int handle_preamble(struct ceph_connection *con)
2637 struct ceph_frame_desc *desc = &con->v2.in_desc;
2640 if (con_secure(con)) {
2641 ret = decrypt_preamble(con);
2643 if (ret == -EBADMSG)
2644 con->error_msg = "integrity error, bad preamble auth tag";
2649 ret = decode_preamble(con->v2.in_buf, desc);
2651 if (ret == -EBADMSG)
2652 con->error_msg = "integrity error, bad crc";
2654 con->error_msg = "protocol error, bad preamble";
2658 dout("%s con %p tag %d seg_cnt %d %d+%d+%d+%d\n", __func__,
2659 con, desc->fd_tag, desc->fd_seg_cnt, desc->fd_lens[0],
2660 desc->fd_lens[1], desc->fd_lens[2], desc->fd_lens[3]);
2662 if (!con_secure(con))
2663 return prepare_read_control(con);
2665 if (desc->fd_lens[0] > CEPH_PREAMBLE_INLINE_LEN)
2666 return prepare_read_control_remainder(con);
2668 return __handle_control(con, CTRL_BODY(con->v2.in_buf));
2671 static int handle_control(struct ceph_connection *con)
2673 int ctrl_len = con->v2.in_desc.fd_lens[0];
2677 WARN_ON(con_secure(con));
2679 ret = verify_control_crc(con);
2681 con->error_msg = "integrity error, bad crc";
2685 if (con->state == CEPH_CON_S_V2_AUTH) {
2686 buf = alloc_conn_buf(con, ctrl_len);
2690 memcpy(buf, con->v2.in_kvecs[0].iov_base, ctrl_len);
2691 return __handle_control(con, buf);
2694 return __handle_control(con, con->v2.in_kvecs[0].iov_base);
2697 static int handle_control_remainder(struct ceph_connection *con)
2701 WARN_ON(!con_secure(con));
2703 ret = decrypt_control_remainder(con);
2705 if (ret == -EBADMSG)
2706 con->error_msg = "integrity error, bad control remainder auth tag";
2710 return __handle_control(con, con->v2.in_kvecs[0].iov_base -
2711 CEPH_PREAMBLE_INLINE_LEN);
2714 static int handle_epilogue(struct ceph_connection *con)
2716 u32 front_crc, middle_crc, data_crc;
2719 if (con_secure(con)) {
2720 ret = decrypt_message(con);
2722 if (ret == -EBADMSG)
2723 con->error_msg = "integrity error, bad epilogue auth tag";
2727 /* just late_status */
2728 ret = decode_epilogue(con->v2.in_buf, NULL, NULL, NULL);
2730 con->error_msg = "protocol error, bad epilogue";
2734 ret = decode_epilogue(con->v2.in_buf, &front_crc,
2735 &middle_crc, &data_crc);
2737 con->error_msg = "protocol error, bad epilogue";
2741 ret = verify_epilogue_crcs(con, front_crc, middle_crc,
2744 con->error_msg = "integrity error, bad crc";
2749 return process_message(con);
2752 static void finish_skip(struct ceph_connection *con)
2754 dout("%s con %p\n", __func__, con);
2756 if (con_secure(con))
2757 gcm_inc_nonce(&con->v2.in_gcm_nonce);
2762 static int populate_in_iter(struct ceph_connection *con)
2766 dout("%s con %p state %d in_state %d\n", __func__, con, con->state,
2768 WARN_ON(iov_iter_count(&con->v2.in_iter));
2770 if (con->state == CEPH_CON_S_V2_BANNER_PREFIX) {
2771 ret = process_banner_prefix(con);
2772 } else if (con->state == CEPH_CON_S_V2_BANNER_PAYLOAD) {
2773 ret = process_banner_payload(con);
2774 } else if ((con->state >= CEPH_CON_S_V2_HELLO &&
2775 con->state <= CEPH_CON_S_V2_SESSION_RECONNECT) ||
2776 con->state == CEPH_CON_S_OPEN) {
2777 switch (con->v2.in_state) {
2778 case IN_S_HANDLE_PREAMBLE:
2779 ret = handle_preamble(con);
2781 case IN_S_HANDLE_CONTROL:
2782 ret = handle_control(con);
2784 case IN_S_HANDLE_CONTROL_REMAINDER:
2785 ret = handle_control_remainder(con);
2787 case IN_S_PREPARE_READ_DATA:
2788 prepare_read_data(con);
2791 case IN_S_PREPARE_READ_DATA_CONT:
2792 prepare_read_data_cont(con);
2795 case IN_S_HANDLE_EPILOGUE:
2796 ret = handle_epilogue(con);
2798 case IN_S_FINISH_SKIP:
2803 WARN(1, "bad in_state %d", con->v2.in_state);
2807 WARN(1, "bad state %d", con->state);
2811 dout("%s con %p error %d\n", __func__, con, ret);
2815 if (WARN_ON(!iov_iter_count(&con->v2.in_iter)))
2817 dout("%s con %p populated %zu\n", __func__, con,
2818 iov_iter_count(&con->v2.in_iter));
2822 int ceph_con_v2_try_read(struct ceph_connection *con)
2826 dout("%s con %p state %d need %zu\n", __func__, con, con->state,
2827 iov_iter_count(&con->v2.in_iter));
2829 if (con->state == CEPH_CON_S_PREOPEN)
2833 * We should always have something pending here. If not,
2834 * avoid calling populate_in_iter() as if we read something
2835 * (ceph_tcp_recv() would immediately return 1).
2837 if (WARN_ON(!iov_iter_count(&con->v2.in_iter)))
2841 ret = ceph_tcp_recv(con);
2845 ret = populate_in_iter(con);
2847 if (ret && ret != -EAGAIN && !con->error_msg)
2848 con->error_msg = "read processing error";
2854 static void queue_data(struct ceph_connection *con)
2858 con->v2.out_epil.data_crc = -1;
2859 ceph_msg_data_cursor_init(&con->v2.out_cursor, con->out_msg,
2860 data_len(con->out_msg));
2862 get_bvec_at(&con->v2.out_cursor, &bv);
2863 set_out_bvec(con, &bv, true);
2864 con->v2.out_state = OUT_S_QUEUE_DATA_CONT;
2867 static void queue_data_cont(struct ceph_connection *con)
2871 con->v2.out_epil.data_crc = ceph_crc32c_page(
2872 con->v2.out_epil.data_crc, con->v2.out_bvec.bv_page,
2873 con->v2.out_bvec.bv_offset, con->v2.out_bvec.bv_len);
2875 ceph_msg_data_advance(&con->v2.out_cursor, con->v2.out_bvec.bv_len);
2876 if (con->v2.out_cursor.total_resid) {
2877 get_bvec_at(&con->v2.out_cursor, &bv);
2878 set_out_bvec(con, &bv, true);
2879 WARN_ON(con->v2.out_state != OUT_S_QUEUE_DATA_CONT);
2884 * We've written all data. Queue epilogue. Once it's written,
2887 reset_out_kvecs(con);
2888 prepare_epilogue_plain(con, false);
2889 con->v2.out_state = OUT_S_FINISH_MESSAGE;
2892 static void queue_enc_page(struct ceph_connection *con)
2896 dout("%s con %p i %d resid %d\n", __func__, con, con->v2.out_enc_i,
2897 con->v2.out_enc_resid);
2898 WARN_ON(!con->v2.out_enc_resid);
2900 bv.bv_page = con->v2.out_enc_pages[con->v2.out_enc_i];
2902 bv.bv_len = min(con->v2.out_enc_resid, (int)PAGE_SIZE);
2904 set_out_bvec(con, &bv, false);
2905 con->v2.out_enc_i++;
2906 con->v2.out_enc_resid -= bv.bv_len;
2908 if (con->v2.out_enc_resid) {
2909 WARN_ON(con->v2.out_state != OUT_S_QUEUE_ENC_PAGE);
2914 * We've queued the last piece of ciphertext (ending with
2915 * epilogue) + auth tag. Once it's written, we are done.
2917 WARN_ON(con->v2.out_enc_i != con->v2.out_enc_page_cnt);
2918 con->v2.out_state = OUT_S_FINISH_MESSAGE;
2921 static void queue_zeros(struct ceph_connection *con)
2923 dout("%s con %p out_zero %d\n", __func__, con, con->v2.out_zero);
2925 if (con->v2.out_zero) {
2926 set_out_bvec_zero(con);
2927 con->v2.out_zero -= con->v2.out_bvec.bv_len;
2928 con->v2.out_state = OUT_S_QUEUE_ZEROS;
2933 * We've zero-filled everything up to epilogue. Queue epilogue
2934 * with late_status set to ABORTED and crcs adjusted for zeros.
2935 * Once it's written, we are done patching up for the revoke.
2937 reset_out_kvecs(con);
2938 prepare_epilogue_plain(con, true);
2939 con->v2.out_state = OUT_S_FINISH_MESSAGE;
2942 static void finish_message(struct ceph_connection *con)
2944 dout("%s con %p msg %p\n", __func__, con, con->out_msg);
2946 /* we end up here both plain and secure modes */
2947 if (con->v2.out_enc_pages) {
2948 WARN_ON(!con->v2.out_enc_page_cnt);
2949 ceph_release_page_vector(con->v2.out_enc_pages,
2950 con->v2.out_enc_page_cnt);
2951 con->v2.out_enc_pages = NULL;
2952 con->v2.out_enc_page_cnt = 0;
2954 /* message may have been revoked */
2956 ceph_msg_put(con->out_msg);
2957 con->out_msg = NULL;
2960 con->v2.out_state = OUT_S_GET_NEXT;
2963 static int populate_out_iter(struct ceph_connection *con)
2967 dout("%s con %p state %d out_state %d\n", __func__, con, con->state,
2969 WARN_ON(iov_iter_count(&con->v2.out_iter));
2971 if (con->state != CEPH_CON_S_OPEN) {
2972 WARN_ON(con->state < CEPH_CON_S_V2_BANNER_PREFIX ||
2973 con->state > CEPH_CON_S_V2_SESSION_RECONNECT);
2974 goto nothing_pending;
2977 switch (con->v2.out_state) {
2978 case OUT_S_QUEUE_DATA:
2979 WARN_ON(!con->out_msg);
2982 case OUT_S_QUEUE_DATA_CONT:
2983 WARN_ON(!con->out_msg);
2984 queue_data_cont(con);
2986 case OUT_S_QUEUE_ENC_PAGE:
2987 queue_enc_page(con);
2989 case OUT_S_QUEUE_ZEROS:
2990 WARN_ON(con->out_msg); /* revoked */
2993 case OUT_S_FINISH_MESSAGE:
2994 finish_message(con);
2996 case OUT_S_GET_NEXT:
2999 WARN(1, "bad out_state %d", con->v2.out_state);
3003 WARN_ON(con->v2.out_state != OUT_S_GET_NEXT);
3004 if (ceph_con_flag_test_and_clear(con, CEPH_CON_F_KEEPALIVE_PENDING)) {
3005 ret = prepare_keepalive2(con);
3007 pr_err("prepare_keepalive2 failed: %d\n", ret);
3010 } else if (!list_empty(&con->out_queue)) {
3011 ceph_con_get_out_msg(con);
3012 ret = prepare_message(con);
3014 pr_err("prepare_message failed: %d\n", ret);
3017 } else if (con->in_seq > con->in_seq_acked) {
3018 ret = prepare_ack(con);
3020 pr_err("prepare_ack failed: %d\n", ret);
3024 goto nothing_pending;
3028 if (WARN_ON(!iov_iter_count(&con->v2.out_iter)))
3030 dout("%s con %p populated %zu\n", __func__, con,
3031 iov_iter_count(&con->v2.out_iter));
3035 WARN_ON(iov_iter_count(&con->v2.out_iter));
3036 dout("%s con %p nothing pending\n", __func__, con);
3037 ceph_con_flag_clear(con, CEPH_CON_F_WRITE_PENDING);
3041 int ceph_con_v2_try_write(struct ceph_connection *con)
3045 dout("%s con %p state %d have %zu\n", __func__, con, con->state,
3046 iov_iter_count(&con->v2.out_iter));
3048 /* open the socket first? */
3049 if (con->state == CEPH_CON_S_PREOPEN) {
3050 WARN_ON(con->peer_addr.type != CEPH_ENTITY_ADDR_TYPE_MSGR2);
3053 * Always bump global_seq. Bump connect_seq only if
3054 * there is a session (i.e. we are reconnecting and will
3055 * send session_reconnect instead of client_ident).
3057 con->v2.global_seq = ceph_get_global_seq(con->msgr, 0);
3058 if (con->v2.server_cookie)
3059 con->v2.connect_seq++;
3061 ret = prepare_read_banner_prefix(con);
3063 pr_err("prepare_read_banner_prefix failed: %d\n", ret);
3064 con->error_msg = "connect error";
3068 reset_out_kvecs(con);
3069 ret = prepare_banner(con);
3071 pr_err("prepare_banner failed: %d\n", ret);
3072 con->error_msg = "connect error";
3076 ret = ceph_tcp_connect(con);
3078 pr_err("ceph_tcp_connect failed: %d\n", ret);
3079 con->error_msg = "connect error";
3084 if (!iov_iter_count(&con->v2.out_iter)) {
3085 ret = populate_out_iter(con);
3087 if (ret && ret != -EAGAIN && !con->error_msg)
3088 con->error_msg = "write processing error";
3093 tcp_sock_set_cork(con->sock->sk, true);
3095 ret = ceph_tcp_send(con);
3099 ret = populate_out_iter(con);
3101 if (ret && ret != -EAGAIN && !con->error_msg)
3102 con->error_msg = "write processing error";
3107 tcp_sock_set_cork(con->sock->sk, false);
3111 static u32 crc32c_zeros(u32 crc, int zero_len)
3116 len = min(zero_len, (int)PAGE_SIZE);
3117 crc = crc32c(crc, page_address(ceph_zero_page), len);
3124 static void prepare_zero_front(struct ceph_connection *con, int resid)
3128 WARN_ON(!resid || resid > front_len(con->out_msg));
3129 sent = front_len(con->out_msg) - resid;
3130 dout("%s con %p sent %d resid %d\n", __func__, con, sent, resid);
3133 con->v2.out_epil.front_crc =
3134 crc32c(-1, con->out_msg->front.iov_base, sent);
3135 con->v2.out_epil.front_crc =
3136 crc32c_zeros(con->v2.out_epil.front_crc, resid);
3138 con->v2.out_epil.front_crc = crc32c_zeros(-1, resid);
3141 con->v2.out_iter.count -= resid;
3142 out_zero_add(con, resid);
3145 static void prepare_zero_middle(struct ceph_connection *con, int resid)
3149 WARN_ON(!resid || resid > middle_len(con->out_msg));
3150 sent = middle_len(con->out_msg) - resid;
3151 dout("%s con %p sent %d resid %d\n", __func__, con, sent, resid);
3154 con->v2.out_epil.middle_crc =
3155 crc32c(-1, con->out_msg->middle->vec.iov_base, sent);
3156 con->v2.out_epil.middle_crc =
3157 crc32c_zeros(con->v2.out_epil.middle_crc, resid);
3159 con->v2.out_epil.middle_crc = crc32c_zeros(-1, resid);
3162 con->v2.out_iter.count -= resid;
3163 out_zero_add(con, resid);
3166 static void prepare_zero_data(struct ceph_connection *con)
3168 dout("%s con %p\n", __func__, con);
3169 con->v2.out_epil.data_crc = crc32c_zeros(-1, data_len(con->out_msg));
3170 out_zero_add(con, data_len(con->out_msg));
3173 static void revoke_at_queue_data(struct ceph_connection *con)
3178 WARN_ON(!data_len(con->out_msg));
3179 WARN_ON(!iov_iter_is_kvec(&con->v2.out_iter));
3180 resid = iov_iter_count(&con->v2.out_iter);
3182 boundary = front_len(con->out_msg) + middle_len(con->out_msg);
3183 if (resid > boundary) {
3185 WARN_ON(resid > MESSAGE_HEAD_PLAIN_LEN);
3186 dout("%s con %p was sending head\n", __func__, con);
3187 if (front_len(con->out_msg))
3188 prepare_zero_front(con, front_len(con->out_msg));
3189 if (middle_len(con->out_msg))
3190 prepare_zero_middle(con, middle_len(con->out_msg));
3191 prepare_zero_data(con);
3192 WARN_ON(iov_iter_count(&con->v2.out_iter) != resid);
3193 con->v2.out_state = OUT_S_QUEUE_ZEROS;
3197 boundary = middle_len(con->out_msg);
3198 if (resid > boundary) {
3200 dout("%s con %p was sending front\n", __func__, con);
3201 prepare_zero_front(con, resid);
3202 if (middle_len(con->out_msg))
3203 prepare_zero_middle(con, middle_len(con->out_msg));
3204 prepare_zero_data(con);
3210 dout("%s con %p was sending middle\n", __func__, con);
3211 prepare_zero_middle(con, resid);
3212 prepare_zero_data(con);
3216 static void revoke_at_queue_data_cont(struct ceph_connection *con)
3218 int sent, resid; /* current piece of data */
3220 WARN_ON(!data_len(con->out_msg));
3221 WARN_ON(!iov_iter_is_bvec(&con->v2.out_iter));
3222 resid = iov_iter_count(&con->v2.out_iter);
3223 WARN_ON(!resid || resid > con->v2.out_bvec.bv_len);
3224 sent = con->v2.out_bvec.bv_len - resid;
3225 dout("%s con %p sent %d resid %d\n", __func__, con, sent, resid);
3228 con->v2.out_epil.data_crc = ceph_crc32c_page(
3229 con->v2.out_epil.data_crc, con->v2.out_bvec.bv_page,
3230 con->v2.out_bvec.bv_offset, sent);
3231 ceph_msg_data_advance(&con->v2.out_cursor, sent);
3233 WARN_ON(resid > con->v2.out_cursor.total_resid);
3234 con->v2.out_epil.data_crc = crc32c_zeros(con->v2.out_epil.data_crc,
3235 con->v2.out_cursor.total_resid);
3237 con->v2.out_iter.count -= resid;
3238 out_zero_add(con, con->v2.out_cursor.total_resid);
3242 static void revoke_at_finish_message(struct ceph_connection *con)
3247 WARN_ON(!iov_iter_is_kvec(&con->v2.out_iter));
3248 resid = iov_iter_count(&con->v2.out_iter);
3250 if (!front_len(con->out_msg) && !middle_len(con->out_msg) &&
3251 !data_len(con->out_msg)) {
3252 WARN_ON(!resid || resid > MESSAGE_HEAD_PLAIN_LEN);
3253 dout("%s con %p was sending head (empty message) - noop\n",
3258 boundary = front_len(con->out_msg) + middle_len(con->out_msg) +
3259 CEPH_EPILOGUE_PLAIN_LEN;
3260 if (resid > boundary) {
3262 WARN_ON(resid > MESSAGE_HEAD_PLAIN_LEN);
3263 dout("%s con %p was sending head\n", __func__, con);
3264 if (front_len(con->out_msg))
3265 prepare_zero_front(con, front_len(con->out_msg));
3266 if (middle_len(con->out_msg))
3267 prepare_zero_middle(con, middle_len(con->out_msg));
3268 con->v2.out_iter.count -= CEPH_EPILOGUE_PLAIN_LEN;
3269 WARN_ON(iov_iter_count(&con->v2.out_iter) != resid);
3270 con->v2.out_state = OUT_S_QUEUE_ZEROS;
3274 boundary = middle_len(con->out_msg) + CEPH_EPILOGUE_PLAIN_LEN;
3275 if (resid > boundary) {
3277 dout("%s con %p was sending front\n", __func__, con);
3278 prepare_zero_front(con, resid);
3279 if (middle_len(con->out_msg))
3280 prepare_zero_middle(con, middle_len(con->out_msg));
3281 con->v2.out_iter.count -= CEPH_EPILOGUE_PLAIN_LEN;
3286 boundary = CEPH_EPILOGUE_PLAIN_LEN;
3287 if (resid > boundary) {
3289 dout("%s con %p was sending middle\n", __func__, con);
3290 prepare_zero_middle(con, resid);
3291 con->v2.out_iter.count -= CEPH_EPILOGUE_PLAIN_LEN;
3297 dout("%s con %p was sending epilogue - noop\n", __func__, con);
3300 void ceph_con_v2_revoke(struct ceph_connection *con)
3302 WARN_ON(con->v2.out_zero);
3304 if (con_secure(con)) {
3305 WARN_ON(con->v2.out_state != OUT_S_QUEUE_ENC_PAGE &&
3306 con->v2.out_state != OUT_S_FINISH_MESSAGE);
3307 dout("%s con %p secure - noop\n", __func__, con);
3311 switch (con->v2.out_state) {
3312 case OUT_S_QUEUE_DATA:
3313 revoke_at_queue_data(con);
3315 case OUT_S_QUEUE_DATA_CONT:
3316 revoke_at_queue_data_cont(con);
3318 case OUT_S_FINISH_MESSAGE:
3319 revoke_at_finish_message(con);
3322 WARN(1, "bad out_state %d", con->v2.out_state);
3327 static void revoke_at_prepare_read_data(struct ceph_connection *con)
3329 int remaining; /* data + [data padding] + epilogue */
3332 WARN_ON(!data_len(con->in_msg));
3333 WARN_ON(!iov_iter_is_kvec(&con->v2.in_iter));
3334 resid = iov_iter_count(&con->v2.in_iter);
3337 if (con_secure(con))
3338 remaining = padded_len(data_len(con->in_msg)) +
3339 CEPH_EPILOGUE_SECURE_LEN;
3341 remaining = data_len(con->in_msg) + CEPH_EPILOGUE_PLAIN_LEN;
3343 dout("%s con %p resid %d remaining %d\n", __func__, con, resid,
3345 con->v2.in_iter.count -= resid;
3346 set_in_skip(con, resid + remaining);
3347 con->v2.in_state = IN_S_FINISH_SKIP;
3350 static void revoke_at_prepare_read_data_cont(struct ceph_connection *con)
3352 int recved, resid; /* current piece of data */
3353 int remaining; /* [data padding] + epilogue */
3355 WARN_ON(!data_len(con->in_msg));
3356 WARN_ON(!iov_iter_is_bvec(&con->v2.in_iter));
3357 resid = iov_iter_count(&con->v2.in_iter);
3358 WARN_ON(!resid || resid > con->v2.in_bvec.bv_len);
3359 recved = con->v2.in_bvec.bv_len - resid;
3360 dout("%s con %p recved %d resid %d\n", __func__, con, recved, resid);
3363 ceph_msg_data_advance(&con->v2.in_cursor, recved);
3364 WARN_ON(resid > con->v2.in_cursor.total_resid);
3366 if (con_secure(con))
3367 remaining = padding_len(data_len(con->in_msg)) +
3368 CEPH_EPILOGUE_SECURE_LEN;
3370 remaining = CEPH_EPILOGUE_PLAIN_LEN;
3372 dout("%s con %p total_resid %zu remaining %d\n", __func__, con,
3373 con->v2.in_cursor.total_resid, remaining);
3374 con->v2.in_iter.count -= resid;
3375 set_in_skip(con, con->v2.in_cursor.total_resid + remaining);
3376 con->v2.in_state = IN_S_FINISH_SKIP;
3379 static void revoke_at_handle_epilogue(struct ceph_connection *con)
3383 WARN_ON(!iov_iter_is_kvec(&con->v2.in_iter));
3384 resid = iov_iter_count(&con->v2.in_iter);
3387 dout("%s con %p resid %d\n", __func__, con, resid);
3388 con->v2.in_iter.count -= resid;
3389 set_in_skip(con, resid);
3390 con->v2.in_state = IN_S_FINISH_SKIP;
3393 void ceph_con_v2_revoke_incoming(struct ceph_connection *con)
3395 switch (con->v2.in_state) {
3396 case IN_S_PREPARE_READ_DATA:
3397 revoke_at_prepare_read_data(con);
3399 case IN_S_PREPARE_READ_DATA_CONT:
3400 revoke_at_prepare_read_data_cont(con);
3402 case IN_S_HANDLE_EPILOGUE:
3403 revoke_at_handle_epilogue(con);
3406 WARN(1, "bad in_state %d", con->v2.in_state);
3411 bool ceph_con_v2_opened(struct ceph_connection *con)
3413 return con->v2.peer_global_seq;
3416 void ceph_con_v2_reset_session(struct ceph_connection *con)
3418 con->v2.client_cookie = 0;
3419 con->v2.server_cookie = 0;
3420 con->v2.global_seq = 0;
3421 con->v2.connect_seq = 0;
3422 con->v2.peer_global_seq = 0;
3425 void ceph_con_v2_reset_protocol(struct ceph_connection *con)
3427 iov_iter_truncate(&con->v2.in_iter, 0);
3428 iov_iter_truncate(&con->v2.out_iter, 0);
3429 con->v2.out_zero = 0;
3431 clear_in_sign_kvecs(con);
3432 clear_out_sign_kvecs(con);
3433 free_conn_bufs(con);
3435 if (con->v2.out_enc_pages) {
3436 WARN_ON(!con->v2.out_enc_page_cnt);
3437 ceph_release_page_vector(con->v2.out_enc_pages,
3438 con->v2.out_enc_page_cnt);
3439 con->v2.out_enc_pages = NULL;
3440 con->v2.out_enc_page_cnt = 0;
3443 con->v2.con_mode = CEPH_CON_MODE_UNKNOWN;
3444 memzero_explicit(&con->v2.in_gcm_nonce, CEPH_GCM_IV_LEN);
3445 memzero_explicit(&con->v2.out_gcm_nonce, CEPH_GCM_IV_LEN);
3447 if (con->v2.hmac_tfm) {
3448 crypto_free_shash(con->v2.hmac_tfm);
3449 con->v2.hmac_tfm = NULL;
3451 if (con->v2.gcm_req) {
3452 aead_request_free(con->v2.gcm_req);
3453 con->v2.gcm_req = NULL;
3455 if (con->v2.gcm_tfm) {
3456 crypto_free_aead(con->v2.gcm_tfm);
3457 con->v2.gcm_tfm = NULL;