1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Ceph msgr2 protocol implementation 4 * 5 * Copyright (C) 2020 Ilya Dryomov <idryomov@gmail.com> 6 */ 7 8 #include <linux/ceph/ceph_debug.h> 9 10 #include <crypto/aead.h> 11 #include <crypto/sha2.h> 12 #include <crypto/utils.h> 13 #include <linux/bvec.h> 14 #include <linux/crc32c.h> 15 #include <linux/net.h> 16 #include <linux/scatterlist.h> 17 #include <linux/socket.h> 18 #include <linux/sched/mm.h> 19 #include <net/sock.h> 20 #include <net/tcp.h> 21 22 #include <linux/ceph/ceph_features.h> 23 #include <linux/ceph/decode.h> 24 #include <linux/ceph/libceph.h> 25 #include <linux/ceph/messenger.h> 26 27 #include "crypto.h" /* for CEPH_KEY_LEN and CEPH_MAX_CON_SECRET_LEN */ 28 29 #define FRAME_TAG_HELLO 1 30 #define FRAME_TAG_AUTH_REQUEST 2 31 #define FRAME_TAG_AUTH_BAD_METHOD 3 32 #define FRAME_TAG_AUTH_REPLY_MORE 4 33 #define FRAME_TAG_AUTH_REQUEST_MORE 5 34 #define FRAME_TAG_AUTH_DONE 6 35 #define FRAME_TAG_AUTH_SIGNATURE 7 36 #define FRAME_TAG_CLIENT_IDENT 8 37 #define FRAME_TAG_SERVER_IDENT 9 38 #define FRAME_TAG_IDENT_MISSING_FEATURES 10 39 #define FRAME_TAG_SESSION_RECONNECT 11 40 #define FRAME_TAG_SESSION_RESET 12 41 #define FRAME_TAG_SESSION_RETRY 13 42 #define FRAME_TAG_SESSION_RETRY_GLOBAL 14 43 #define FRAME_TAG_SESSION_RECONNECT_OK 15 44 #define FRAME_TAG_WAIT 16 45 #define FRAME_TAG_MESSAGE 17 46 #define FRAME_TAG_KEEPALIVE2 18 47 #define FRAME_TAG_KEEPALIVE2_ACK 19 48 #define FRAME_TAG_ACK 20 49 50 #define FRAME_LATE_STATUS_ABORTED 0x1 51 #define FRAME_LATE_STATUS_COMPLETE 0xe 52 #define FRAME_LATE_STATUS_ABORTED_MASK 0xf 53 54 #define IN_S_HANDLE_PREAMBLE 1 55 #define IN_S_HANDLE_CONTROL 2 56 #define IN_S_HANDLE_CONTROL_REMAINDER 3 57 #define IN_S_PREPARE_READ_DATA 4 58 #define IN_S_PREPARE_READ_DATA_CONT 5 59 #define IN_S_PREPARE_READ_ENC_PAGE 6 60 #define IN_S_PREPARE_SPARSE_DATA 7 61 #define IN_S_PREPARE_SPARSE_DATA_CONT 8 62 #define IN_S_HANDLE_EPILOGUE 9 63 #define IN_S_FINISH_SKIP 10 64 65 #define OUT_S_QUEUE_DATA 1 66 #define OUT_S_QUEUE_DATA_CONT 2 67 #define OUT_S_QUEUE_ENC_PAGE 3 68 #define OUT_S_QUEUE_ZEROS 4 69 #define OUT_S_FINISH_MESSAGE 5 70 #define OUT_S_GET_NEXT 6 71 72 #define CTRL_BODY(p) ((void *)(p) + CEPH_PREAMBLE_LEN) 73 #define FRONT_PAD(p) ((void *)(p) + CEPH_EPILOGUE_SECURE_LEN) 74 #define MIDDLE_PAD(p) (FRONT_PAD(p) + CEPH_GCM_BLOCK_LEN) 75 #define DATA_PAD(p) (MIDDLE_PAD(p) + CEPH_GCM_BLOCK_LEN) 76 77 #define CEPH_MSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 78 79 static int do_recvmsg(struct socket *sock, struct iov_iter *it) 80 { 81 struct msghdr msg = { .msg_flags = CEPH_MSG_FLAGS }; 82 int ret; 83 84 msg.msg_iter = *it; 85 while (iov_iter_count(it)) { 86 ret = sock_recvmsg(sock, &msg, msg.msg_flags); 87 if (ret <= 0) { 88 if (ret == -EAGAIN) 89 ret = 0; 90 return ret; 91 } 92 93 iov_iter_advance(it, ret); 94 } 95 96 WARN_ON(msg_data_left(&msg)); 97 return 1; 98 } 99 100 /* 101 * Read as much as possible. 102 * 103 * Return: 104 * 1 - done, nothing (else) to read 105 * 0 - socket is empty, need to wait 106 * <0 - error 107 */ 108 static int ceph_tcp_recv(struct ceph_connection *con) 109 { 110 int ret; 111 112 dout("%s con %p %s %zu\n", __func__, con, 113 iov_iter_is_discard(&con->v2.in_iter) ? "discard" : "need", 114 iov_iter_count(&con->v2.in_iter)); 115 ret = do_recvmsg(con->sock, &con->v2.in_iter); 116 dout("%s con %p ret %d left %zu\n", __func__, con, ret, 117 iov_iter_count(&con->v2.in_iter)); 118 return ret; 119 } 120 121 static int do_sendmsg(struct socket *sock, struct iov_iter *it) 122 { 123 struct msghdr msg = { .msg_flags = CEPH_MSG_FLAGS }; 124 int ret; 125 126 msg.msg_iter = *it; 127 while (iov_iter_count(it)) { 128 ret = sock_sendmsg(sock, &msg); 129 if (ret <= 0) { 130 if (ret == -EAGAIN) 131 ret = 0; 132 return ret; 133 } 134 135 iov_iter_advance(it, ret); 136 } 137 138 WARN_ON(msg_data_left(&msg)); 139 return 1; 140 } 141 142 static int do_try_sendpage(struct socket *sock, struct iov_iter *it) 143 { 144 struct msghdr msg = { .msg_flags = CEPH_MSG_FLAGS }; 145 struct bio_vec bv; 146 int ret; 147 148 if (WARN_ON(!iov_iter_is_bvec(it))) 149 return -EINVAL; 150 151 while (iov_iter_count(it)) { 152 /* iov_iter_iovec() for ITER_BVEC */ 153 bvec_set_page(&bv, it->bvec->bv_page, 154 min(iov_iter_count(it), 155 it->bvec->bv_len - it->iov_offset), 156 it->bvec->bv_offset + it->iov_offset); 157 158 /* 159 * MSG_SPLICE_PAGES cannot properly handle pages with 160 * page_count == 0, we need to fall back to sendmsg if 161 * that's the case. 162 * 163 * Same goes for slab pages: skb_can_coalesce() allows 164 * coalescing neighboring slab objects into a single frag 165 * which triggers one of hardened usercopy checks. 166 */ 167 if (sendpage_ok(bv.bv_page)) 168 msg.msg_flags |= MSG_SPLICE_PAGES; 169 else 170 msg.msg_flags &= ~MSG_SPLICE_PAGES; 171 172 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bv, 1, bv.bv_len); 173 ret = sock_sendmsg(sock, &msg); 174 if (ret <= 0) { 175 if (ret == -EAGAIN) 176 ret = 0; 177 return ret; 178 } 179 180 iov_iter_advance(it, ret); 181 } 182 183 return 1; 184 } 185 186 /* 187 * Write as much as possible. The socket is expected to be corked, 188 * so we don't bother with MSG_MORE here. 189 * 190 * Return: 191 * 1 - done, nothing (else) to write 192 * 0 - socket is full, need to wait 193 * <0 - error 194 */ 195 static int ceph_tcp_send(struct ceph_connection *con) 196 { 197 int ret; 198 199 dout("%s con %p have %zu try_sendpage %d\n", __func__, con, 200 iov_iter_count(&con->v2.out_iter), con->v2.out_iter_sendpage); 201 if (con->v2.out_iter_sendpage) 202 ret = do_try_sendpage(con->sock, &con->v2.out_iter); 203 else 204 ret = do_sendmsg(con->sock, &con->v2.out_iter); 205 dout("%s con %p ret %d left %zu\n", __func__, con, ret, 206 iov_iter_count(&con->v2.out_iter)); 207 return ret; 208 } 209 210 static void add_in_kvec(struct ceph_connection *con, void *buf, int len) 211 { 212 BUG_ON(con->v2.in_kvec_cnt >= ARRAY_SIZE(con->v2.in_kvecs)); 213 WARN_ON(!iov_iter_is_kvec(&con->v2.in_iter)); 214 215 con->v2.in_kvecs[con->v2.in_kvec_cnt].iov_base = buf; 216 con->v2.in_kvecs[con->v2.in_kvec_cnt].iov_len = len; 217 con->v2.in_kvec_cnt++; 218 219 con->v2.in_iter.nr_segs++; 220 con->v2.in_iter.count += len; 221 } 222 223 static void reset_in_kvecs(struct ceph_connection *con) 224 { 225 WARN_ON(iov_iter_count(&con->v2.in_iter)); 226 227 con->v2.in_kvec_cnt = 0; 228 iov_iter_kvec(&con->v2.in_iter, ITER_DEST, con->v2.in_kvecs, 0, 0); 229 } 230 231 static void set_in_bvec(struct ceph_connection *con, const struct bio_vec *bv) 232 { 233 WARN_ON(iov_iter_count(&con->v2.in_iter)); 234 235 con->v2.in_bvec = *bv; 236 iov_iter_bvec(&con->v2.in_iter, ITER_DEST, &con->v2.in_bvec, 1, bv->bv_len); 237 } 238 239 static void set_in_skip(struct ceph_connection *con, int len) 240 { 241 WARN_ON(iov_iter_count(&con->v2.in_iter)); 242 243 dout("%s con %p len %d\n", __func__, con, len); 244 iov_iter_discard(&con->v2.in_iter, ITER_DEST, len); 245 } 246 247 static void add_out_kvec(struct ceph_connection *con, void *buf, int len) 248 { 249 BUG_ON(con->v2.out_kvec_cnt >= ARRAY_SIZE(con->v2.out_kvecs)); 250 WARN_ON(!iov_iter_is_kvec(&con->v2.out_iter)); 251 WARN_ON(con->v2.out_zero); 252 253 con->v2.out_kvecs[con->v2.out_kvec_cnt].iov_base = buf; 254 con->v2.out_kvecs[con->v2.out_kvec_cnt].iov_len = len; 255 con->v2.out_kvec_cnt++; 256 257 con->v2.out_iter.nr_segs++; 258 con->v2.out_iter.count += len; 259 } 260 261 static void reset_out_kvecs(struct ceph_connection *con) 262 { 263 WARN_ON(iov_iter_count(&con->v2.out_iter)); 264 WARN_ON(con->v2.out_zero); 265 266 con->v2.out_kvec_cnt = 0; 267 268 iov_iter_kvec(&con->v2.out_iter, ITER_SOURCE, con->v2.out_kvecs, 0, 0); 269 con->v2.out_iter_sendpage = false; 270 } 271 272 static void set_out_bvec(struct ceph_connection *con, const struct bio_vec *bv, 273 bool zerocopy) 274 { 275 WARN_ON(iov_iter_count(&con->v2.out_iter)); 276 WARN_ON(con->v2.out_zero); 277 278 con->v2.out_bvec = *bv; 279 con->v2.out_iter_sendpage = zerocopy; 280 iov_iter_bvec(&con->v2.out_iter, ITER_SOURCE, &con->v2.out_bvec, 1, 281 con->v2.out_bvec.bv_len); 282 } 283 284 static void set_out_bvec_zero(struct ceph_connection *con) 285 { 286 WARN_ON(iov_iter_count(&con->v2.out_iter)); 287 WARN_ON(!con->v2.out_zero); 288 289 bvec_set_page(&con->v2.out_bvec, ceph_zero_page, 290 min(con->v2.out_zero, (int)PAGE_SIZE), 0); 291 con->v2.out_iter_sendpage = true; 292 iov_iter_bvec(&con->v2.out_iter, ITER_SOURCE, &con->v2.out_bvec, 1, 293 con->v2.out_bvec.bv_len); 294 } 295 296 static void out_zero_add(struct ceph_connection *con, int len) 297 { 298 dout("%s con %p len %d\n", __func__, con, len); 299 con->v2.out_zero += len; 300 } 301 302 static void *alloc_conn_buf(struct ceph_connection *con, int len) 303 { 304 void *buf; 305 306 dout("%s con %p len %d\n", __func__, con, len); 307 308 if (WARN_ON(con->v2.conn_buf_cnt >= ARRAY_SIZE(con->v2.conn_bufs))) 309 return NULL; 310 311 buf = kvmalloc(len, GFP_NOIO); 312 if (!buf) 313 return NULL; 314 315 con->v2.conn_bufs[con->v2.conn_buf_cnt++] = buf; 316 return buf; 317 } 318 319 static void free_conn_bufs(struct ceph_connection *con) 320 { 321 while (con->v2.conn_buf_cnt) 322 kvfree(con->v2.conn_bufs[--con->v2.conn_buf_cnt]); 323 } 324 325 static void add_in_sign_kvec(struct ceph_connection *con, void *buf, int len) 326 { 327 BUG_ON(con->v2.in_sign_kvec_cnt >= ARRAY_SIZE(con->v2.in_sign_kvecs)); 328 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++; 332 } 333 334 static void clear_in_sign_kvecs(struct ceph_connection *con) 335 { 336 con->v2.in_sign_kvec_cnt = 0; 337 } 338 339 static void add_out_sign_kvec(struct ceph_connection *con, void *buf, int len) 340 { 341 BUG_ON(con->v2.out_sign_kvec_cnt >= ARRAY_SIZE(con->v2.out_sign_kvecs)); 342 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++; 346 } 347 348 static void clear_out_sign_kvecs(struct ceph_connection *con) 349 { 350 con->v2.out_sign_kvec_cnt = 0; 351 } 352 353 static bool con_secure(struct ceph_connection *con) 354 { 355 return con->v2.con_mode == CEPH_CON_MODE_SECURE; 356 } 357 358 static int front_len(const struct ceph_msg *msg) 359 { 360 return le32_to_cpu(msg->hdr.front_len); 361 } 362 363 static int middle_len(const struct ceph_msg *msg) 364 { 365 return le32_to_cpu(msg->hdr.middle_len); 366 } 367 368 static int data_len(const struct ceph_msg *msg) 369 { 370 return le32_to_cpu(msg->hdr.data_len); 371 } 372 373 static bool need_padding(int len) 374 { 375 return !IS_ALIGNED(len, CEPH_GCM_BLOCK_LEN); 376 } 377 378 static int padded_len(int len) 379 { 380 return ALIGN(len, CEPH_GCM_BLOCK_LEN); 381 } 382 383 static int padding_len(int len) 384 { 385 return padded_len(len) - len; 386 } 387 388 /* preamble + control segment */ 389 static int head_onwire_len(int ctrl_len, bool secure) 390 { 391 int head_len; 392 int rem_len; 393 394 BUG_ON(ctrl_len < 1 || ctrl_len > CEPH_MSG_MAX_CONTROL_LEN); 395 396 if (secure) { 397 head_len = CEPH_PREAMBLE_SECURE_LEN; 398 if (ctrl_len > CEPH_PREAMBLE_INLINE_LEN) { 399 rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN; 400 head_len += padded_len(rem_len) + CEPH_GCM_TAG_LEN; 401 } 402 } else { 403 head_len = CEPH_PREAMBLE_PLAIN_LEN + ctrl_len + CEPH_CRC_LEN; 404 } 405 return head_len; 406 } 407 408 /* front, middle and data segments + epilogue */ 409 static int __tail_onwire_len(int front_len, int middle_len, int data_len, 410 bool secure) 411 { 412 BUG_ON(front_len < 0 || front_len > CEPH_MSG_MAX_FRONT_LEN || 413 middle_len < 0 || middle_len > CEPH_MSG_MAX_MIDDLE_LEN || 414 data_len < 0 || data_len > CEPH_MSG_MAX_DATA_LEN); 415 416 if (!front_len && !middle_len && !data_len) 417 return 0; 418 419 if (!secure) 420 return front_len + middle_len + data_len + 421 CEPH_EPILOGUE_PLAIN_LEN; 422 423 return padded_len(front_len) + padded_len(middle_len) + 424 padded_len(data_len) + CEPH_EPILOGUE_SECURE_LEN; 425 } 426 427 static int tail_onwire_len(const struct ceph_msg *msg, bool secure) 428 { 429 return __tail_onwire_len(front_len(msg), middle_len(msg), 430 data_len(msg), secure); 431 } 432 433 /* head_onwire_len(sizeof(struct ceph_msg_header2), false) */ 434 #define MESSAGE_HEAD_PLAIN_LEN (CEPH_PREAMBLE_PLAIN_LEN + \ 435 sizeof(struct ceph_msg_header2) + \ 436 CEPH_CRC_LEN) 437 438 static const int frame_aligns[] = { 439 sizeof(void *), 440 sizeof(void *), 441 sizeof(void *), 442 PAGE_SIZE 443 }; 444 445 /* 446 * Discards trailing empty segments, unless there is just one segment. 447 * A frame always has at least one (possibly empty) segment. 448 */ 449 static int calc_segment_count(const int *lens, int len_cnt) 450 { 451 int i; 452 453 for (i = len_cnt - 1; i >= 0; i--) { 454 if (lens[i]) 455 return i + 1; 456 } 457 458 return 1; 459 } 460 461 static void init_frame_desc(struct ceph_frame_desc *desc, int tag, 462 const int *lens, int len_cnt) 463 { 464 int i; 465 466 memset(desc, 0, sizeof(*desc)); 467 468 desc->fd_tag = tag; 469 desc->fd_seg_cnt = calc_segment_count(lens, len_cnt); 470 BUG_ON(desc->fd_seg_cnt > CEPH_FRAME_MAX_SEGMENT_COUNT); 471 for (i = 0; i < desc->fd_seg_cnt; i++) { 472 desc->fd_lens[i] = lens[i]; 473 desc->fd_aligns[i] = frame_aligns[i]; 474 } 475 } 476 477 /* 478 * Preamble crc covers everything up to itself (28 bytes) and 479 * is calculated and verified irrespective of the connection mode 480 * (i.e. even if the frame is encrypted). 481 */ 482 static void encode_preamble(const struct ceph_frame_desc *desc, void *p) 483 { 484 void *crcp = p + CEPH_PREAMBLE_LEN - CEPH_CRC_LEN; 485 void *start = p; 486 int i; 487 488 memset(p, 0, CEPH_PREAMBLE_LEN); 489 490 ceph_encode_8(&p, desc->fd_tag); 491 ceph_encode_8(&p, desc->fd_seg_cnt); 492 for (i = 0; i < desc->fd_seg_cnt; i++) { 493 ceph_encode_32(&p, desc->fd_lens[i]); 494 ceph_encode_16(&p, desc->fd_aligns[i]); 495 } 496 497 put_unaligned_le32(crc32c(0, start, crcp - start), crcp); 498 } 499 500 static int decode_preamble(void *p, struct ceph_frame_desc *desc) 501 { 502 void *crcp = p + CEPH_PREAMBLE_LEN - CEPH_CRC_LEN; 503 u32 crc, expected_crc; 504 int i; 505 506 crc = crc32c(0, p, crcp - p); 507 expected_crc = get_unaligned_le32(crcp); 508 if (crc != expected_crc) { 509 pr_err("bad preamble crc, calculated %u, expected %u\n", 510 crc, expected_crc); 511 return -EBADMSG; 512 } 513 514 memset(desc, 0, sizeof(*desc)); 515 516 desc->fd_tag = ceph_decode_8(&p); 517 desc->fd_seg_cnt = ceph_decode_8(&p); 518 if (desc->fd_seg_cnt < 1 || 519 desc->fd_seg_cnt > CEPH_FRAME_MAX_SEGMENT_COUNT) { 520 pr_err("bad segment count %d\n", desc->fd_seg_cnt); 521 return -EINVAL; 522 } 523 for (i = 0; i < desc->fd_seg_cnt; i++) { 524 desc->fd_lens[i] = ceph_decode_32(&p); 525 desc->fd_aligns[i] = ceph_decode_16(&p); 526 } 527 528 /* 529 * This would fire for FRAME_TAG_WAIT (it has one empty 530 * segment), but we should never get it as client. 531 */ 532 if (desc->fd_lens[0] < 1 || 533 desc->fd_lens[0] > CEPH_MSG_MAX_CONTROL_LEN) { 534 pr_err("bad control segment length %d\n", desc->fd_lens[0]); 535 return -EINVAL; 536 } 537 538 if (desc->fd_lens[1] < 0 || 539 desc->fd_lens[1] > CEPH_MSG_MAX_FRONT_LEN) { 540 pr_err("bad front segment length %d\n", desc->fd_lens[1]); 541 return -EINVAL; 542 } 543 if (desc->fd_lens[2] < 0 || 544 desc->fd_lens[2] > CEPH_MSG_MAX_MIDDLE_LEN) { 545 pr_err("bad middle segment length %d\n", desc->fd_lens[2]); 546 return -EINVAL; 547 } 548 if (desc->fd_lens[3] < 0 || 549 desc->fd_lens[3] > CEPH_MSG_MAX_DATA_LEN) { 550 pr_err("bad data segment length %d\n", desc->fd_lens[3]); 551 return -EINVAL; 552 } 553 554 if (!desc->fd_lens[desc->fd_seg_cnt - 1]) { 555 pr_err("last segment empty, segment count %d\n", 556 desc->fd_seg_cnt); 557 return -EINVAL; 558 } 559 560 return 0; 561 } 562 563 static void encode_epilogue_plain(struct ceph_connection *con, bool aborted) 564 { 565 con->v2.out_epil.late_status = aborted ? FRAME_LATE_STATUS_ABORTED : 566 FRAME_LATE_STATUS_COMPLETE; 567 cpu_to_le32s(&con->v2.out_epil.front_crc); 568 cpu_to_le32s(&con->v2.out_epil.middle_crc); 569 cpu_to_le32s(&con->v2.out_epil.data_crc); 570 } 571 572 static void encode_epilogue_secure(struct ceph_connection *con, bool aborted) 573 { 574 memset(&con->v2.out_epil, 0, sizeof(con->v2.out_epil)); 575 con->v2.out_epil.late_status = aborted ? FRAME_LATE_STATUS_ABORTED : 576 FRAME_LATE_STATUS_COMPLETE; 577 } 578 579 static int decode_epilogue(void *p, u32 *front_crc, u32 *middle_crc, 580 u32 *data_crc) 581 { 582 u8 late_status; 583 584 late_status = ceph_decode_8(&p); 585 if ((late_status & FRAME_LATE_STATUS_ABORTED_MASK) != 586 FRAME_LATE_STATUS_COMPLETE) { 587 /* we should never get an aborted message as client */ 588 pr_err("bad late_status 0x%x\n", late_status); 589 return -EINVAL; 590 } 591 592 if (front_crc && middle_crc && data_crc) { 593 *front_crc = ceph_decode_32(&p); 594 *middle_crc = ceph_decode_32(&p); 595 *data_crc = ceph_decode_32(&p); 596 } 597 598 return 0; 599 } 600 601 static void fill_header(struct ceph_msg_header *hdr, 602 const struct ceph_msg_header2 *hdr2, 603 int front_len, int middle_len, int data_len, 604 const struct ceph_entity_name *peer_name) 605 { 606 hdr->seq = hdr2->seq; 607 hdr->tid = hdr2->tid; 608 hdr->type = hdr2->type; 609 hdr->priority = hdr2->priority; 610 hdr->version = hdr2->version; 611 hdr->front_len = cpu_to_le32(front_len); 612 hdr->middle_len = cpu_to_le32(middle_len); 613 hdr->data_len = cpu_to_le32(data_len); 614 hdr->data_off = hdr2->data_off; 615 hdr->src = *peer_name; 616 hdr->compat_version = hdr2->compat_version; 617 hdr->reserved = 0; 618 hdr->crc = 0; 619 } 620 621 static void fill_header2(struct ceph_msg_header2 *hdr2, 622 const struct ceph_msg_header *hdr, u64 ack_seq) 623 { 624 hdr2->seq = hdr->seq; 625 hdr2->tid = hdr->tid; 626 hdr2->type = hdr->type; 627 hdr2->priority = hdr->priority; 628 hdr2->version = hdr->version; 629 hdr2->data_pre_padding_len = 0; 630 hdr2->data_off = hdr->data_off; 631 hdr2->ack_seq = cpu_to_le64(ack_seq); 632 hdr2->flags = 0; 633 hdr2->compat_version = hdr->compat_version; 634 hdr2->reserved = 0; 635 } 636 637 static int verify_control_crc(struct ceph_connection *con) 638 { 639 int ctrl_len = con->v2.in_desc.fd_lens[0]; 640 u32 crc, expected_crc; 641 642 WARN_ON(con->v2.in_kvecs[0].iov_len != ctrl_len); 643 WARN_ON(con->v2.in_kvecs[1].iov_len != CEPH_CRC_LEN); 644 645 crc = crc32c(-1, con->v2.in_kvecs[0].iov_base, ctrl_len); 646 expected_crc = get_unaligned_le32(con->v2.in_kvecs[1].iov_base); 647 if (crc != expected_crc) { 648 pr_err("bad control crc, calculated %u, expected %u\n", 649 crc, expected_crc); 650 return -EBADMSG; 651 } 652 653 return 0; 654 } 655 656 static int verify_epilogue_crcs(struct ceph_connection *con, u32 front_crc, 657 u32 middle_crc, u32 data_crc) 658 { 659 if (front_len(con->in_msg)) { 660 con->in_front_crc = crc32c(-1, con->in_msg->front.iov_base, 661 front_len(con->in_msg)); 662 } else { 663 WARN_ON(!middle_len(con->in_msg) && !data_len(con->in_msg)); 664 con->in_front_crc = -1; 665 } 666 667 if (middle_len(con->in_msg)) 668 con->in_middle_crc = crc32c(-1, 669 con->in_msg->middle->vec.iov_base, 670 middle_len(con->in_msg)); 671 else if (data_len(con->in_msg)) 672 con->in_middle_crc = -1; 673 else 674 con->in_middle_crc = 0; 675 676 if (!data_len(con->in_msg)) 677 con->in_data_crc = 0; 678 679 dout("%s con %p msg %p crcs %u %u %u\n", __func__, con, con->in_msg, 680 con->in_front_crc, con->in_middle_crc, con->in_data_crc); 681 682 if (con->in_front_crc != front_crc) { 683 pr_err("bad front crc, calculated %u, expected %u\n", 684 con->in_front_crc, front_crc); 685 return -EBADMSG; 686 } 687 if (con->in_middle_crc != middle_crc) { 688 pr_err("bad middle crc, calculated %u, expected %u\n", 689 con->in_middle_crc, middle_crc); 690 return -EBADMSG; 691 } 692 if (con->in_data_crc != data_crc) { 693 pr_err("bad data crc, calculated %u, expected %u\n", 694 con->in_data_crc, data_crc); 695 return -EBADMSG; 696 } 697 698 return 0; 699 } 700 701 static int setup_crypto(struct ceph_connection *con, 702 const u8 *session_key, int session_key_len, 703 const u8 *con_secret, int con_secret_len) 704 { 705 unsigned int noio_flag; 706 int ret; 707 708 dout("%s con %p con_mode %d session_key_len %d con_secret_len %d\n", 709 __func__, con, con->v2.con_mode, session_key_len, con_secret_len); 710 WARN_ON(con->v2.hmac_key_set || con->v2.gcm_tfm || con->v2.gcm_req); 711 712 if (con->v2.con_mode != CEPH_CON_MODE_CRC && 713 con->v2.con_mode != CEPH_CON_MODE_SECURE) { 714 pr_err("bad con_mode %d\n", con->v2.con_mode); 715 return -EINVAL; 716 } 717 718 if (!session_key_len) { 719 WARN_ON(con->v2.con_mode != CEPH_CON_MODE_CRC); 720 WARN_ON(con_secret_len); 721 return 0; /* auth_none */ 722 } 723 724 hmac_sha256_preparekey(&con->v2.hmac_key, session_key, session_key_len); 725 con->v2.hmac_key_set = true; 726 727 if (con->v2.con_mode == CEPH_CON_MODE_CRC) { 728 WARN_ON(con_secret_len); 729 return 0; /* auth_x, plain mode */ 730 } 731 732 if (con_secret_len < CEPH_GCM_KEY_LEN + 2 * CEPH_GCM_IV_LEN) { 733 pr_err("con_secret too small %d\n", con_secret_len); 734 return -EINVAL; 735 } 736 737 noio_flag = memalloc_noio_save(); 738 con->v2.gcm_tfm = crypto_alloc_aead("gcm(aes)", 0, 0); 739 memalloc_noio_restore(noio_flag); 740 if (IS_ERR(con->v2.gcm_tfm)) { 741 ret = PTR_ERR(con->v2.gcm_tfm); 742 con->v2.gcm_tfm = NULL; 743 pr_err("failed to allocate gcm tfm context: %d\n", ret); 744 return ret; 745 } 746 747 WARN_ON((unsigned long)con_secret & 748 crypto_aead_alignmask(con->v2.gcm_tfm)); 749 ret = crypto_aead_setkey(con->v2.gcm_tfm, con_secret, CEPH_GCM_KEY_LEN); 750 if (ret) { 751 pr_err("failed to set gcm key: %d\n", ret); 752 return ret; 753 } 754 755 WARN_ON(crypto_aead_ivsize(con->v2.gcm_tfm) != CEPH_GCM_IV_LEN); 756 ret = crypto_aead_setauthsize(con->v2.gcm_tfm, CEPH_GCM_TAG_LEN); 757 if (ret) { 758 pr_err("failed to set gcm tag size: %d\n", ret); 759 return ret; 760 } 761 762 con->v2.gcm_req = aead_request_alloc(con->v2.gcm_tfm, GFP_NOIO); 763 if (!con->v2.gcm_req) { 764 pr_err("failed to allocate gcm request\n"); 765 return -ENOMEM; 766 } 767 768 crypto_init_wait(&con->v2.gcm_wait); 769 aead_request_set_callback(con->v2.gcm_req, CRYPTO_TFM_REQ_MAY_BACKLOG, 770 crypto_req_done, &con->v2.gcm_wait); 771 772 memcpy(&con->v2.in_gcm_nonce, con_secret + CEPH_GCM_KEY_LEN, 773 CEPH_GCM_IV_LEN); 774 memcpy(&con->v2.out_gcm_nonce, 775 con_secret + CEPH_GCM_KEY_LEN + CEPH_GCM_IV_LEN, 776 CEPH_GCM_IV_LEN); 777 return 0; /* auth_x, secure mode */ 778 } 779 780 static void con_hmac_sha256(struct ceph_connection *con, 781 const struct kvec *kvecs, int kvec_cnt, 782 u8 hmac[SHA256_DIGEST_SIZE]) 783 { 784 struct hmac_sha256_ctx ctx; 785 int i; 786 787 dout("%s con %p hmac_key_set %d kvec_cnt %d\n", __func__, con, 788 con->v2.hmac_key_set, kvec_cnt); 789 790 if (!con->v2.hmac_key_set) { 791 memset(hmac, 0, SHA256_DIGEST_SIZE); 792 return; /* auth_none */ 793 } 794 795 /* auth_x, both plain and secure modes */ 796 hmac_sha256_init(&ctx, &con->v2.hmac_key); 797 for (i = 0; i < kvec_cnt; i++) 798 hmac_sha256_update(&ctx, kvecs[i].iov_base, kvecs[i].iov_len); 799 hmac_sha256_final(&ctx, hmac); 800 } 801 802 static void gcm_inc_nonce(struct ceph_gcm_nonce *nonce) 803 { 804 u64 counter; 805 806 counter = le64_to_cpu(nonce->counter); 807 nonce->counter = cpu_to_le64(counter + 1); 808 } 809 810 static int gcm_crypt(struct ceph_connection *con, bool encrypt, 811 struct scatterlist *src, struct scatterlist *dst, 812 int src_len) 813 { 814 struct ceph_gcm_nonce *nonce; 815 int ret; 816 817 nonce = encrypt ? &con->v2.out_gcm_nonce : &con->v2.in_gcm_nonce; 818 819 aead_request_set_ad(con->v2.gcm_req, 0); /* no AAD */ 820 aead_request_set_crypt(con->v2.gcm_req, src, dst, src_len, (u8 *)nonce); 821 ret = crypto_wait_req(encrypt ? crypto_aead_encrypt(con->v2.gcm_req) : 822 crypto_aead_decrypt(con->v2.gcm_req), 823 &con->v2.gcm_wait); 824 if (ret) 825 return ret; 826 827 gcm_inc_nonce(nonce); 828 return 0; 829 } 830 831 static void get_bvec_at(struct ceph_msg_data_cursor *cursor, 832 struct bio_vec *bv) 833 { 834 struct page *page; 835 size_t off, len; 836 837 WARN_ON(!cursor->total_resid); 838 839 /* skip zero-length data items */ 840 while (!cursor->resid) 841 ceph_msg_data_advance(cursor, 0); 842 843 /* get a piece of data, cursor isn't advanced */ 844 page = ceph_msg_data_next(cursor, &off, &len); 845 bvec_set_page(bv, page, len, off); 846 } 847 848 static int calc_sg_cnt(void *buf, int buf_len) 849 { 850 int sg_cnt; 851 852 if (!buf_len) 853 return 0; 854 855 sg_cnt = need_padding(buf_len) ? 1 : 0; 856 if (is_vmalloc_addr(buf)) { 857 WARN_ON(offset_in_page(buf)); 858 sg_cnt += PAGE_ALIGN(buf_len) >> PAGE_SHIFT; 859 } else { 860 sg_cnt++; 861 } 862 863 return sg_cnt; 864 } 865 866 static int calc_sg_cnt_cursor(struct ceph_msg_data_cursor *cursor) 867 { 868 int data_len = cursor->total_resid; 869 struct bio_vec bv; 870 int sg_cnt; 871 872 if (!data_len) 873 return 0; 874 875 sg_cnt = need_padding(data_len) ? 1 : 0; 876 do { 877 get_bvec_at(cursor, &bv); 878 sg_cnt++; 879 880 ceph_msg_data_advance(cursor, bv.bv_len); 881 } while (cursor->total_resid); 882 883 return sg_cnt; 884 } 885 886 static void init_sgs(struct scatterlist **sg, void *buf, int buf_len, u8 *pad) 887 { 888 void *end = buf + buf_len; 889 struct page *page; 890 int len; 891 void *p; 892 893 if (!buf_len) 894 return; 895 896 if (is_vmalloc_addr(buf)) { 897 p = buf; 898 do { 899 page = vmalloc_to_page(p); 900 len = min_t(int, end - p, PAGE_SIZE); 901 WARN_ON(!page || !len || offset_in_page(p)); 902 sg_set_page(*sg, page, len, 0); 903 *sg = sg_next(*sg); 904 p += len; 905 } while (p != end); 906 } else { 907 sg_set_buf(*sg, buf, buf_len); 908 *sg = sg_next(*sg); 909 } 910 911 if (need_padding(buf_len)) { 912 sg_set_buf(*sg, pad, padding_len(buf_len)); 913 *sg = sg_next(*sg); 914 } 915 } 916 917 static void init_sgs_cursor(struct scatterlist **sg, 918 struct ceph_msg_data_cursor *cursor, u8 *pad) 919 { 920 int data_len = cursor->total_resid; 921 struct bio_vec bv; 922 923 if (!data_len) 924 return; 925 926 do { 927 get_bvec_at(cursor, &bv); 928 sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset); 929 *sg = sg_next(*sg); 930 931 ceph_msg_data_advance(cursor, bv.bv_len); 932 } while (cursor->total_resid); 933 934 if (need_padding(data_len)) { 935 sg_set_buf(*sg, pad, padding_len(data_len)); 936 *sg = sg_next(*sg); 937 } 938 } 939 940 /** 941 * init_sgs_pages: set up scatterlist on an array of page pointers 942 * @sg: scatterlist to populate 943 * @pages: pointer to page array 944 * @dpos: position in the array to start (bytes) 945 * @dlen: len to add to sg (bytes) 946 * @pad: pointer to pad destination (if any) 947 * 948 * Populate the scatterlist from the page array, starting at an arbitrary 949 * byte in the array and running for a specified length. 950 */ 951 static void init_sgs_pages(struct scatterlist **sg, struct page **pages, 952 int dpos, int dlen, u8 *pad) 953 { 954 int idx = dpos >> PAGE_SHIFT; 955 int off = offset_in_page(dpos); 956 int resid = dlen; 957 958 do { 959 int len = min(resid, (int)PAGE_SIZE - off); 960 961 sg_set_page(*sg, pages[idx], len, off); 962 *sg = sg_next(*sg); 963 off = 0; 964 ++idx; 965 resid -= len; 966 } while (resid); 967 968 if (need_padding(dlen)) { 969 sg_set_buf(*sg, pad, padding_len(dlen)); 970 *sg = sg_next(*sg); 971 } 972 } 973 974 static int setup_message_sgs(struct sg_table *sgt, struct ceph_msg *msg, 975 u8 *front_pad, u8 *middle_pad, u8 *data_pad, 976 void *epilogue, struct page **pages, int dpos, 977 bool add_tag) 978 { 979 struct ceph_msg_data_cursor cursor; 980 struct scatterlist *cur_sg; 981 int dlen = data_len(msg); 982 int sg_cnt; 983 int ret; 984 985 if (!front_len(msg) && !middle_len(msg) && !data_len(msg)) 986 return 0; 987 988 sg_cnt = 1; /* epilogue + [auth tag] */ 989 if (front_len(msg)) 990 sg_cnt += calc_sg_cnt(msg->front.iov_base, 991 front_len(msg)); 992 if (middle_len(msg)) 993 sg_cnt += calc_sg_cnt(msg->middle->vec.iov_base, 994 middle_len(msg)); 995 if (dlen) { 996 if (pages) { 997 sg_cnt += calc_pages_for(dpos, dlen); 998 if (need_padding(dlen)) 999 sg_cnt++; 1000 } else { 1001 ceph_msg_data_cursor_init(&cursor, msg, dlen); 1002 sg_cnt += calc_sg_cnt_cursor(&cursor); 1003 } 1004 } 1005 1006 ret = sg_alloc_table(sgt, sg_cnt, GFP_NOIO); 1007 if (ret) 1008 return ret; 1009 1010 cur_sg = sgt->sgl; 1011 if (front_len(msg)) 1012 init_sgs(&cur_sg, msg->front.iov_base, front_len(msg), 1013 front_pad); 1014 if (middle_len(msg)) 1015 init_sgs(&cur_sg, msg->middle->vec.iov_base, middle_len(msg), 1016 middle_pad); 1017 if (dlen) { 1018 if (pages) { 1019 init_sgs_pages(&cur_sg, pages, dpos, dlen, data_pad); 1020 } else { 1021 ceph_msg_data_cursor_init(&cursor, msg, dlen); 1022 init_sgs_cursor(&cur_sg, &cursor, data_pad); 1023 } 1024 } 1025 1026 WARN_ON(!sg_is_last(cur_sg)); 1027 sg_set_buf(cur_sg, epilogue, 1028 CEPH_GCM_BLOCK_LEN + (add_tag ? CEPH_GCM_TAG_LEN : 0)); 1029 return 0; 1030 } 1031 1032 static int decrypt_preamble(struct ceph_connection *con) 1033 { 1034 struct scatterlist sg; 1035 1036 sg_init_one(&sg, con->v2.in_buf, CEPH_PREAMBLE_SECURE_LEN); 1037 return gcm_crypt(con, false, &sg, &sg, CEPH_PREAMBLE_SECURE_LEN); 1038 } 1039 1040 static int decrypt_control_remainder(struct ceph_connection *con) 1041 { 1042 int ctrl_len = con->v2.in_desc.fd_lens[0]; 1043 int rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN; 1044 int pt_len = padding_len(rem_len) + CEPH_GCM_TAG_LEN; 1045 struct scatterlist sgs[2]; 1046 1047 WARN_ON(con->v2.in_kvecs[0].iov_len != rem_len); 1048 WARN_ON(con->v2.in_kvecs[1].iov_len != pt_len); 1049 1050 sg_init_table(sgs, 2); 1051 sg_set_buf(&sgs[0], con->v2.in_kvecs[0].iov_base, rem_len); 1052 sg_set_buf(&sgs[1], con->v2.in_buf, pt_len); 1053 1054 return gcm_crypt(con, false, sgs, sgs, 1055 padded_len(rem_len) + CEPH_GCM_TAG_LEN); 1056 } 1057 1058 /* Process sparse read data that lives in a buffer */ 1059 static int process_v2_sparse_read(struct ceph_connection *con, 1060 struct page **pages, int spos) 1061 { 1062 struct ceph_msg_data_cursor cursor; 1063 int ret; 1064 1065 ceph_msg_data_cursor_init(&cursor, con->in_msg, 1066 con->in_msg->sparse_read_total); 1067 1068 for (;;) { 1069 char *buf = NULL; 1070 1071 ret = con->ops->sparse_read(con, &cursor, &buf); 1072 if (ret <= 0) 1073 return ret; 1074 1075 dout("%s: sparse_read return %x buf %p\n", __func__, ret, buf); 1076 1077 do { 1078 int idx = spos >> PAGE_SHIFT; 1079 int soff = offset_in_page(spos); 1080 struct page *spage = con->v2.in_enc_pages[idx]; 1081 int len = min_t(int, ret, PAGE_SIZE - soff); 1082 1083 if (buf) { 1084 memcpy_from_page(buf, spage, soff, len); 1085 buf += len; 1086 } else { 1087 struct bio_vec bv; 1088 1089 get_bvec_at(&cursor, &bv); 1090 len = min_t(int, len, bv.bv_len); 1091 memcpy_page(bv.bv_page, bv.bv_offset, 1092 spage, soff, len); 1093 ceph_msg_data_advance(&cursor, len); 1094 } 1095 spos += len; 1096 ret -= len; 1097 } while (ret); 1098 } 1099 } 1100 1101 static int decrypt_tail(struct ceph_connection *con) 1102 { 1103 struct sg_table enc_sgt = {}; 1104 struct sg_table sgt = {}; 1105 struct page **pages = NULL; 1106 bool sparse = !!con->in_msg->sparse_read_total; 1107 int dpos = 0; 1108 int tail_len; 1109 int ret; 1110 1111 tail_len = tail_onwire_len(con->in_msg, true); 1112 ret = sg_alloc_table_from_pages(&enc_sgt, con->v2.in_enc_pages, 1113 con->v2.in_enc_page_cnt, 0, tail_len, 1114 GFP_NOIO); 1115 if (ret) 1116 goto out; 1117 1118 if (sparse) { 1119 dpos = padded_len(front_len(con->in_msg) + padded_len(middle_len(con->in_msg))); 1120 pages = con->v2.in_enc_pages; 1121 } 1122 1123 ret = setup_message_sgs(&sgt, con->in_msg, FRONT_PAD(con->v2.in_buf), 1124 MIDDLE_PAD(con->v2.in_buf), DATA_PAD(con->v2.in_buf), 1125 con->v2.in_buf, pages, dpos, true); 1126 if (ret) 1127 goto out; 1128 1129 dout("%s con %p msg %p enc_page_cnt %d sg_cnt %d\n", __func__, con, 1130 con->in_msg, con->v2.in_enc_page_cnt, sgt.orig_nents); 1131 ret = gcm_crypt(con, false, enc_sgt.sgl, sgt.sgl, tail_len); 1132 if (ret) 1133 goto out; 1134 1135 if (sparse && data_len(con->in_msg)) { 1136 ret = process_v2_sparse_read(con, con->v2.in_enc_pages, dpos); 1137 if (ret) 1138 goto out; 1139 } 1140 1141 WARN_ON(!con->v2.in_enc_page_cnt); 1142 ceph_release_page_vector(con->v2.in_enc_pages, 1143 con->v2.in_enc_page_cnt); 1144 con->v2.in_enc_pages = NULL; 1145 con->v2.in_enc_page_cnt = 0; 1146 1147 out: 1148 sg_free_table(&sgt); 1149 sg_free_table(&enc_sgt); 1150 return ret; 1151 } 1152 1153 static int prepare_banner(struct ceph_connection *con) 1154 { 1155 int buf_len = CEPH_BANNER_V2_LEN + 2 + 8 + 8; 1156 void *buf, *p; 1157 1158 buf = alloc_conn_buf(con, buf_len); 1159 if (!buf) 1160 return -ENOMEM; 1161 1162 p = buf; 1163 ceph_encode_copy(&p, CEPH_BANNER_V2, CEPH_BANNER_V2_LEN); 1164 ceph_encode_16(&p, sizeof(u64) + sizeof(u64)); 1165 ceph_encode_64(&p, CEPH_MSGR2_SUPPORTED_FEATURES); 1166 ceph_encode_64(&p, CEPH_MSGR2_REQUIRED_FEATURES); 1167 WARN_ON(p != buf + buf_len); 1168 1169 add_out_kvec(con, buf, buf_len); 1170 add_out_sign_kvec(con, buf, buf_len); 1171 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING); 1172 return 0; 1173 } 1174 1175 /* 1176 * base: 1177 * preamble 1178 * control body (ctrl_len bytes) 1179 * space for control crc 1180 * 1181 * extdata (optional): 1182 * control body (extdata_len bytes) 1183 * 1184 * Compute control crc and gather base and extdata into: 1185 * 1186 * preamble 1187 * control body (ctrl_len + extdata_len bytes) 1188 * control crc 1189 * 1190 * Preamble should already be encoded at the start of base. 1191 */ 1192 static void prepare_head_plain(struct ceph_connection *con, void *base, 1193 int ctrl_len, void *extdata, int extdata_len, 1194 bool to_be_signed) 1195 { 1196 int base_len = CEPH_PREAMBLE_LEN + ctrl_len + CEPH_CRC_LEN; 1197 void *crcp = base + base_len - CEPH_CRC_LEN; 1198 u32 crc; 1199 1200 crc = crc32c(-1, CTRL_BODY(base), ctrl_len); 1201 if (extdata_len) 1202 crc = crc32c(crc, extdata, extdata_len); 1203 put_unaligned_le32(crc, crcp); 1204 1205 if (!extdata_len) { 1206 add_out_kvec(con, base, base_len); 1207 if (to_be_signed) 1208 add_out_sign_kvec(con, base, base_len); 1209 return; 1210 } 1211 1212 add_out_kvec(con, base, crcp - base); 1213 add_out_kvec(con, extdata, extdata_len); 1214 add_out_kvec(con, crcp, CEPH_CRC_LEN); 1215 if (to_be_signed) { 1216 add_out_sign_kvec(con, base, crcp - base); 1217 add_out_sign_kvec(con, extdata, extdata_len); 1218 add_out_sign_kvec(con, crcp, CEPH_CRC_LEN); 1219 } 1220 } 1221 1222 static int prepare_head_secure_small(struct ceph_connection *con, 1223 void *base, int ctrl_len) 1224 { 1225 struct scatterlist sg; 1226 int ret; 1227 1228 /* inline buffer padding? */ 1229 if (ctrl_len < CEPH_PREAMBLE_INLINE_LEN) 1230 memset(CTRL_BODY(base) + ctrl_len, 0, 1231 CEPH_PREAMBLE_INLINE_LEN - ctrl_len); 1232 1233 sg_init_one(&sg, base, CEPH_PREAMBLE_SECURE_LEN); 1234 ret = gcm_crypt(con, true, &sg, &sg, 1235 CEPH_PREAMBLE_SECURE_LEN - CEPH_GCM_TAG_LEN); 1236 if (ret) 1237 return ret; 1238 1239 add_out_kvec(con, base, CEPH_PREAMBLE_SECURE_LEN); 1240 return 0; 1241 } 1242 1243 /* 1244 * base: 1245 * preamble 1246 * control body (ctrl_len bytes) 1247 * space for padding, if needed 1248 * space for control remainder auth tag 1249 * space for preamble auth tag 1250 * 1251 * Encrypt preamble and the inline portion, then encrypt the remainder 1252 * and gather into: 1253 * 1254 * preamble 1255 * control body (48 bytes) 1256 * preamble auth tag 1257 * control body (ctrl_len - 48 bytes) 1258 * zero padding, if needed 1259 * control remainder auth tag 1260 * 1261 * Preamble should already be encoded at the start of base. 1262 */ 1263 static int prepare_head_secure_big(struct ceph_connection *con, 1264 void *base, int ctrl_len) 1265 { 1266 int rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN; 1267 void *rem = CTRL_BODY(base) + CEPH_PREAMBLE_INLINE_LEN; 1268 void *rem_tag = rem + padded_len(rem_len); 1269 void *pmbl_tag = rem_tag + CEPH_GCM_TAG_LEN; 1270 struct scatterlist sgs[2]; 1271 int ret; 1272 1273 sg_init_table(sgs, 2); 1274 sg_set_buf(&sgs[0], base, rem - base); 1275 sg_set_buf(&sgs[1], pmbl_tag, CEPH_GCM_TAG_LEN); 1276 ret = gcm_crypt(con, true, sgs, sgs, rem - base); 1277 if (ret) 1278 return ret; 1279 1280 /* control remainder padding? */ 1281 if (need_padding(rem_len)) 1282 memset(rem + rem_len, 0, padding_len(rem_len)); 1283 1284 sg_init_one(&sgs[0], rem, pmbl_tag - rem); 1285 ret = gcm_crypt(con, true, sgs, sgs, rem_tag - rem); 1286 if (ret) 1287 return ret; 1288 1289 add_out_kvec(con, base, rem - base); 1290 add_out_kvec(con, pmbl_tag, CEPH_GCM_TAG_LEN); 1291 add_out_kvec(con, rem, pmbl_tag - rem); 1292 return 0; 1293 } 1294 1295 static int __prepare_control(struct ceph_connection *con, int tag, 1296 void *base, int ctrl_len, void *extdata, 1297 int extdata_len, bool to_be_signed) 1298 { 1299 int total_len = ctrl_len + extdata_len; 1300 struct ceph_frame_desc desc; 1301 int ret; 1302 1303 dout("%s con %p tag %d len %d (%d+%d)\n", __func__, con, tag, 1304 total_len, ctrl_len, extdata_len); 1305 1306 /* extdata may be vmalloc'ed but not base */ 1307 if (WARN_ON(is_vmalloc_addr(base) || !ctrl_len)) 1308 return -EINVAL; 1309 1310 init_frame_desc(&desc, tag, &total_len, 1); 1311 encode_preamble(&desc, base); 1312 1313 if (con_secure(con)) { 1314 if (WARN_ON(extdata_len || to_be_signed)) 1315 return -EINVAL; 1316 1317 if (ctrl_len <= CEPH_PREAMBLE_INLINE_LEN) 1318 /* fully inlined, inline buffer may need padding */ 1319 ret = prepare_head_secure_small(con, base, ctrl_len); 1320 else 1321 /* partially inlined, inline buffer is full */ 1322 ret = prepare_head_secure_big(con, base, ctrl_len); 1323 if (ret) 1324 return ret; 1325 } else { 1326 prepare_head_plain(con, base, ctrl_len, extdata, extdata_len, 1327 to_be_signed); 1328 } 1329 1330 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING); 1331 return 0; 1332 } 1333 1334 static int prepare_control(struct ceph_connection *con, int tag, 1335 void *base, int ctrl_len) 1336 { 1337 return __prepare_control(con, tag, base, ctrl_len, NULL, 0, false); 1338 } 1339 1340 static int prepare_hello(struct ceph_connection *con) 1341 { 1342 void *buf, *p; 1343 int ctrl_len; 1344 1345 ctrl_len = 1 + ceph_entity_addr_encoding_len(&con->peer_addr); 1346 buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, false)); 1347 if (!buf) 1348 return -ENOMEM; 1349 1350 p = CTRL_BODY(buf); 1351 ceph_encode_8(&p, CEPH_ENTITY_TYPE_CLIENT); 1352 ceph_encode_entity_addr(&p, &con->peer_addr); 1353 WARN_ON(p != CTRL_BODY(buf) + ctrl_len); 1354 1355 return __prepare_control(con, FRAME_TAG_HELLO, buf, ctrl_len, 1356 NULL, 0, true); 1357 } 1358 1359 /* so that head_onwire_len(AUTH_BUF_LEN, false) is 512 */ 1360 #define AUTH_BUF_LEN (512 - CEPH_CRC_LEN - CEPH_PREAMBLE_PLAIN_LEN) 1361 1362 static int prepare_auth_request(struct ceph_connection *con) 1363 { 1364 void *authorizer, *authorizer_copy; 1365 int ctrl_len, authorizer_len; 1366 void *buf; 1367 int ret; 1368 1369 ctrl_len = AUTH_BUF_LEN; 1370 buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, false)); 1371 if (!buf) 1372 return -ENOMEM; 1373 1374 mutex_unlock(&con->mutex); 1375 ret = con->ops->get_auth_request(con, CTRL_BODY(buf), &ctrl_len, 1376 &authorizer, &authorizer_len); 1377 mutex_lock(&con->mutex); 1378 if (con->state != CEPH_CON_S_V2_HELLO) { 1379 dout("%s con %p state changed to %d\n", __func__, con, 1380 con->state); 1381 return -EAGAIN; 1382 } 1383 1384 dout("%s con %p get_auth_request ret %d\n", __func__, con, ret); 1385 if (ret) 1386 return ret; 1387 1388 authorizer_copy = alloc_conn_buf(con, authorizer_len); 1389 if (!authorizer_copy) 1390 return -ENOMEM; 1391 1392 memcpy(authorizer_copy, authorizer, authorizer_len); 1393 1394 return __prepare_control(con, FRAME_TAG_AUTH_REQUEST, buf, ctrl_len, 1395 authorizer_copy, authorizer_len, true); 1396 } 1397 1398 static int prepare_auth_request_more(struct ceph_connection *con, 1399 void *reply, int reply_len) 1400 { 1401 int ctrl_len, authorizer_len; 1402 void *authorizer; 1403 void *buf; 1404 int ret; 1405 1406 ctrl_len = AUTH_BUF_LEN; 1407 buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, false)); 1408 if (!buf) 1409 return -ENOMEM; 1410 1411 mutex_unlock(&con->mutex); 1412 ret = con->ops->handle_auth_reply_more(con, reply, reply_len, 1413 CTRL_BODY(buf), &ctrl_len, 1414 &authorizer, &authorizer_len); 1415 mutex_lock(&con->mutex); 1416 if (con->state != CEPH_CON_S_V2_AUTH) { 1417 dout("%s con %p state changed to %d\n", __func__, con, 1418 con->state); 1419 return -EAGAIN; 1420 } 1421 1422 dout("%s con %p handle_auth_reply_more ret %d\n", __func__, con, ret); 1423 if (ret) 1424 return ret; 1425 1426 return __prepare_control(con, FRAME_TAG_AUTH_REQUEST_MORE, buf, 1427 ctrl_len, authorizer, authorizer_len, true); 1428 } 1429 1430 static int prepare_auth_signature(struct ceph_connection *con) 1431 { 1432 void *buf; 1433 1434 buf = alloc_conn_buf(con, head_onwire_len(SHA256_DIGEST_SIZE, 1435 con_secure(con))); 1436 if (!buf) 1437 return -ENOMEM; 1438 1439 con_hmac_sha256(con, con->v2.in_sign_kvecs, con->v2.in_sign_kvec_cnt, 1440 CTRL_BODY(buf)); 1441 1442 return prepare_control(con, FRAME_TAG_AUTH_SIGNATURE, buf, 1443 SHA256_DIGEST_SIZE); 1444 } 1445 1446 static int prepare_client_ident(struct ceph_connection *con) 1447 { 1448 struct ceph_entity_addr *my_addr = &con->msgr->inst.addr; 1449 struct ceph_client *client = from_msgr(con->msgr); 1450 u64 global_id = ceph_client_gid(client); 1451 void *buf, *p; 1452 int ctrl_len; 1453 1454 WARN_ON(con->v2.server_cookie); 1455 WARN_ON(con->v2.connect_seq); 1456 WARN_ON(con->v2.peer_global_seq); 1457 1458 if (!con->v2.client_cookie) { 1459 do { 1460 get_random_bytes(&con->v2.client_cookie, 1461 sizeof(con->v2.client_cookie)); 1462 } while (!con->v2.client_cookie); 1463 dout("%s con %p generated cookie 0x%llx\n", __func__, con, 1464 con->v2.client_cookie); 1465 } else { 1466 dout("%s con %p cookie already set 0x%llx\n", __func__, con, 1467 con->v2.client_cookie); 1468 } 1469 1470 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", 1471 __func__, con, ceph_pr_addr(my_addr), le32_to_cpu(my_addr->nonce), 1472 ceph_pr_addr(&con->peer_addr), le32_to_cpu(con->peer_addr.nonce), 1473 global_id, con->v2.global_seq, client->supported_features, 1474 client->required_features, con->v2.client_cookie); 1475 1476 ctrl_len = 1 + 4 + ceph_entity_addr_encoding_len(my_addr) + 1477 ceph_entity_addr_encoding_len(&con->peer_addr) + 6 * 8; 1478 buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, con_secure(con))); 1479 if (!buf) 1480 return -ENOMEM; 1481 1482 p = CTRL_BODY(buf); 1483 ceph_encode_8(&p, 2); /* addrvec marker */ 1484 ceph_encode_32(&p, 1); /* addr_cnt */ 1485 ceph_encode_entity_addr(&p, my_addr); 1486 ceph_encode_entity_addr(&p, &con->peer_addr); 1487 ceph_encode_64(&p, global_id); 1488 ceph_encode_64(&p, con->v2.global_seq); 1489 ceph_encode_64(&p, client->supported_features); 1490 ceph_encode_64(&p, client->required_features); 1491 ceph_encode_64(&p, 0); /* flags */ 1492 ceph_encode_64(&p, con->v2.client_cookie); 1493 WARN_ON(p != CTRL_BODY(buf) + ctrl_len); 1494 1495 return prepare_control(con, FRAME_TAG_CLIENT_IDENT, buf, ctrl_len); 1496 } 1497 1498 static int prepare_session_reconnect(struct ceph_connection *con) 1499 { 1500 struct ceph_entity_addr *my_addr = &con->msgr->inst.addr; 1501 void *buf, *p; 1502 int ctrl_len; 1503 1504 WARN_ON(!con->v2.client_cookie); 1505 WARN_ON(!con->v2.server_cookie); 1506 WARN_ON(!con->v2.connect_seq); 1507 WARN_ON(!con->v2.peer_global_seq); 1508 1509 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", 1510 __func__, con, ceph_pr_addr(my_addr), le32_to_cpu(my_addr->nonce), 1511 con->v2.client_cookie, con->v2.server_cookie, con->v2.global_seq, 1512 con->v2.connect_seq, con->in_seq); 1513 1514 ctrl_len = 1 + 4 + ceph_entity_addr_encoding_len(my_addr) + 5 * 8; 1515 buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, con_secure(con))); 1516 if (!buf) 1517 return -ENOMEM; 1518 1519 p = CTRL_BODY(buf); 1520 ceph_encode_8(&p, 2); /* entity_addrvec_t marker */ 1521 ceph_encode_32(&p, 1); /* my_addrs len */ 1522 ceph_encode_entity_addr(&p, my_addr); 1523 ceph_encode_64(&p, con->v2.client_cookie); 1524 ceph_encode_64(&p, con->v2.server_cookie); 1525 ceph_encode_64(&p, con->v2.global_seq); 1526 ceph_encode_64(&p, con->v2.connect_seq); 1527 ceph_encode_64(&p, con->in_seq); 1528 WARN_ON(p != CTRL_BODY(buf) + ctrl_len); 1529 1530 return prepare_control(con, FRAME_TAG_SESSION_RECONNECT, buf, ctrl_len); 1531 } 1532 1533 static int prepare_keepalive2(struct ceph_connection *con) 1534 { 1535 struct ceph_timespec *ts = CTRL_BODY(con->v2.out_buf); 1536 struct timespec64 now; 1537 1538 ktime_get_real_ts64(&now); 1539 dout("%s con %p timestamp %ptSp\n", __func__, con, &now); 1540 1541 ceph_encode_timespec64(ts, &now); 1542 1543 reset_out_kvecs(con); 1544 return prepare_control(con, FRAME_TAG_KEEPALIVE2, con->v2.out_buf, 1545 sizeof(struct ceph_timespec)); 1546 } 1547 1548 static int prepare_ack(struct ceph_connection *con) 1549 { 1550 void *p; 1551 1552 dout("%s con %p in_seq_acked %llu -> %llu\n", __func__, con, 1553 con->in_seq_acked, con->in_seq); 1554 con->in_seq_acked = con->in_seq; 1555 1556 p = CTRL_BODY(con->v2.out_buf); 1557 ceph_encode_64(&p, con->in_seq_acked); 1558 1559 reset_out_kvecs(con); 1560 return prepare_control(con, FRAME_TAG_ACK, con->v2.out_buf, 8); 1561 } 1562 1563 static void prepare_epilogue_plain(struct ceph_connection *con, 1564 struct ceph_msg *msg, bool aborted) 1565 { 1566 dout("%s con %p msg %p aborted %d crcs %u %u %u\n", __func__, con, 1567 msg, aborted, con->v2.out_epil.front_crc, 1568 con->v2.out_epil.middle_crc, con->v2.out_epil.data_crc); 1569 1570 encode_epilogue_plain(con, aborted); 1571 add_out_kvec(con, &con->v2.out_epil, CEPH_EPILOGUE_PLAIN_LEN); 1572 } 1573 1574 /* 1575 * For "used" empty segments, crc is -1. For unused (trailing) 1576 * segments, crc is 0. 1577 */ 1578 static void prepare_message_plain(struct ceph_connection *con, 1579 struct ceph_msg *msg) 1580 { 1581 prepare_head_plain(con, con->v2.out_buf, 1582 sizeof(struct ceph_msg_header2), NULL, 0, false); 1583 1584 if (!front_len(msg) && !middle_len(msg)) { 1585 if (!data_len(msg)) { 1586 /* 1587 * Empty message: once the head is written, 1588 * we are done -- there is no epilogue. 1589 */ 1590 con->v2.out_state = OUT_S_FINISH_MESSAGE; 1591 return; 1592 } 1593 1594 con->v2.out_epil.front_crc = -1; 1595 con->v2.out_epil.middle_crc = -1; 1596 con->v2.out_state = OUT_S_QUEUE_DATA; 1597 return; 1598 } 1599 1600 if (front_len(msg)) { 1601 con->v2.out_epil.front_crc = crc32c(-1, msg->front.iov_base, 1602 front_len(msg)); 1603 add_out_kvec(con, msg->front.iov_base, front_len(msg)); 1604 } else { 1605 /* middle (at least) is there, checked above */ 1606 con->v2.out_epil.front_crc = -1; 1607 } 1608 1609 if (middle_len(msg)) { 1610 con->v2.out_epil.middle_crc = 1611 crc32c(-1, msg->middle->vec.iov_base, middle_len(msg)); 1612 add_out_kvec(con, msg->middle->vec.iov_base, middle_len(msg)); 1613 } else { 1614 con->v2.out_epil.middle_crc = data_len(msg) ? -1 : 0; 1615 } 1616 1617 if (data_len(msg)) { 1618 con->v2.out_state = OUT_S_QUEUE_DATA; 1619 } else { 1620 con->v2.out_epil.data_crc = 0; 1621 prepare_epilogue_plain(con, msg, false); 1622 con->v2.out_state = OUT_S_FINISH_MESSAGE; 1623 } 1624 } 1625 1626 /* 1627 * Unfortunately the kernel crypto API doesn't support streaming 1628 * (piecewise) operation for AEAD algorithms, so we can't get away 1629 * with a fixed size buffer and a couple sgs. Instead, we have to 1630 * allocate pages for the entire tail of the message (currently up 1631 * to ~32M) and two sgs arrays (up to ~256K each)... 1632 */ 1633 static int prepare_message_secure(struct ceph_connection *con, 1634 struct ceph_msg *msg) 1635 { 1636 void *zerop = page_address(ceph_zero_page); 1637 struct sg_table enc_sgt = {}; 1638 struct sg_table sgt = {}; 1639 struct page **enc_pages; 1640 int enc_page_cnt; 1641 int tail_len; 1642 int ret; 1643 1644 ret = prepare_head_secure_small(con, con->v2.out_buf, 1645 sizeof(struct ceph_msg_header2)); 1646 if (ret) 1647 return ret; 1648 1649 tail_len = tail_onwire_len(msg, true); 1650 if (!tail_len) { 1651 /* 1652 * Empty message: once the head is written, 1653 * we are done -- there is no epilogue. 1654 */ 1655 con->v2.out_state = OUT_S_FINISH_MESSAGE; 1656 return 0; 1657 } 1658 1659 encode_epilogue_secure(con, false); 1660 ret = setup_message_sgs(&sgt, msg, zerop, zerop, zerop, 1661 &con->v2.out_epil, NULL, 0, false); 1662 if (ret) 1663 goto out; 1664 1665 enc_page_cnt = calc_pages_for(0, tail_len); 1666 enc_pages = ceph_alloc_page_vector(enc_page_cnt, GFP_NOIO); 1667 if (IS_ERR(enc_pages)) { 1668 ret = PTR_ERR(enc_pages); 1669 goto out; 1670 } 1671 1672 WARN_ON(con->v2.out_enc_pages || con->v2.out_enc_page_cnt); 1673 con->v2.out_enc_pages = enc_pages; 1674 con->v2.out_enc_page_cnt = enc_page_cnt; 1675 con->v2.out_enc_resid = tail_len; 1676 con->v2.out_enc_i = 0; 1677 1678 ret = sg_alloc_table_from_pages(&enc_sgt, enc_pages, enc_page_cnt, 1679 0, tail_len, GFP_NOIO); 1680 if (ret) 1681 goto out; 1682 1683 ret = gcm_crypt(con, true, sgt.sgl, enc_sgt.sgl, 1684 tail_len - CEPH_GCM_TAG_LEN); 1685 if (ret) 1686 goto out; 1687 1688 dout("%s con %p msg %p sg_cnt %d enc_page_cnt %d\n", __func__, con, 1689 msg, sgt.orig_nents, enc_page_cnt); 1690 con->v2.out_state = OUT_S_QUEUE_ENC_PAGE; 1691 1692 out: 1693 sg_free_table(&sgt); 1694 sg_free_table(&enc_sgt); 1695 return ret; 1696 } 1697 1698 static int prepare_message(struct ceph_connection *con, struct ceph_msg *msg) 1699 { 1700 int lens[] = { 1701 sizeof(struct ceph_msg_header2), 1702 front_len(msg), 1703 middle_len(msg), 1704 data_len(msg) 1705 }; 1706 struct ceph_frame_desc desc; 1707 int ret; 1708 1709 dout("%s con %p msg %p logical %d+%d+%d+%d\n", __func__, con, 1710 msg, lens[0], lens[1], lens[2], lens[3]); 1711 1712 if (con->in_seq > con->in_seq_acked) { 1713 dout("%s con %p in_seq_acked %llu -> %llu\n", __func__, con, 1714 con->in_seq_acked, con->in_seq); 1715 con->in_seq_acked = con->in_seq; 1716 } 1717 1718 reset_out_kvecs(con); 1719 init_frame_desc(&desc, FRAME_TAG_MESSAGE, lens, 4); 1720 encode_preamble(&desc, con->v2.out_buf); 1721 fill_header2(CTRL_BODY(con->v2.out_buf), &msg->hdr, 1722 con->in_seq_acked); 1723 1724 if (con_secure(con)) { 1725 ret = prepare_message_secure(con, msg); 1726 if (ret) 1727 return ret; 1728 } else { 1729 prepare_message_plain(con, msg); 1730 } 1731 1732 ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING); 1733 return 0; 1734 } 1735 1736 static int prepare_read_banner_prefix(struct ceph_connection *con) 1737 { 1738 void *buf; 1739 1740 buf = alloc_conn_buf(con, CEPH_BANNER_V2_PREFIX_LEN); 1741 if (!buf) 1742 return -ENOMEM; 1743 1744 reset_in_kvecs(con); 1745 add_in_kvec(con, buf, CEPH_BANNER_V2_PREFIX_LEN); 1746 add_in_sign_kvec(con, buf, CEPH_BANNER_V2_PREFIX_LEN); 1747 con->state = CEPH_CON_S_V2_BANNER_PREFIX; 1748 return 0; 1749 } 1750 1751 static int prepare_read_banner_payload(struct ceph_connection *con, 1752 int payload_len) 1753 { 1754 void *buf; 1755 1756 buf = alloc_conn_buf(con, payload_len); 1757 if (!buf) 1758 return -ENOMEM; 1759 1760 reset_in_kvecs(con); 1761 add_in_kvec(con, buf, payload_len); 1762 add_in_sign_kvec(con, buf, payload_len); 1763 con->state = CEPH_CON_S_V2_BANNER_PAYLOAD; 1764 return 0; 1765 } 1766 1767 static void prepare_read_preamble(struct ceph_connection *con) 1768 { 1769 reset_in_kvecs(con); 1770 add_in_kvec(con, con->v2.in_buf, 1771 con_secure(con) ? CEPH_PREAMBLE_SECURE_LEN : 1772 CEPH_PREAMBLE_PLAIN_LEN); 1773 con->v2.in_state = IN_S_HANDLE_PREAMBLE; 1774 } 1775 1776 static int prepare_read_control(struct ceph_connection *con) 1777 { 1778 int ctrl_len = con->v2.in_desc.fd_lens[0]; 1779 int head_len; 1780 void *buf; 1781 1782 reset_in_kvecs(con); 1783 if (con->state == CEPH_CON_S_V2_HELLO || 1784 con->state == CEPH_CON_S_V2_AUTH) { 1785 head_len = head_onwire_len(ctrl_len, false); 1786 buf = alloc_conn_buf(con, head_len); 1787 if (!buf) 1788 return -ENOMEM; 1789 1790 /* preserve preamble */ 1791 memcpy(buf, con->v2.in_buf, CEPH_PREAMBLE_LEN); 1792 1793 add_in_kvec(con, CTRL_BODY(buf), ctrl_len); 1794 add_in_kvec(con, CTRL_BODY(buf) + ctrl_len, CEPH_CRC_LEN); 1795 add_in_sign_kvec(con, buf, head_len); 1796 } else { 1797 if (ctrl_len > CEPH_PREAMBLE_INLINE_LEN) { 1798 buf = alloc_conn_buf(con, ctrl_len); 1799 if (!buf) 1800 return -ENOMEM; 1801 1802 add_in_kvec(con, buf, ctrl_len); 1803 } else { 1804 add_in_kvec(con, CTRL_BODY(con->v2.in_buf), ctrl_len); 1805 } 1806 add_in_kvec(con, con->v2.in_buf, CEPH_CRC_LEN); 1807 } 1808 con->v2.in_state = IN_S_HANDLE_CONTROL; 1809 return 0; 1810 } 1811 1812 static int prepare_read_control_remainder(struct ceph_connection *con) 1813 { 1814 int ctrl_len = con->v2.in_desc.fd_lens[0]; 1815 int rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN; 1816 void *buf; 1817 1818 buf = alloc_conn_buf(con, ctrl_len); 1819 if (!buf) 1820 return -ENOMEM; 1821 1822 memcpy(buf, CTRL_BODY(con->v2.in_buf), CEPH_PREAMBLE_INLINE_LEN); 1823 1824 reset_in_kvecs(con); 1825 add_in_kvec(con, buf + CEPH_PREAMBLE_INLINE_LEN, rem_len); 1826 add_in_kvec(con, con->v2.in_buf, 1827 padding_len(rem_len) + CEPH_GCM_TAG_LEN); 1828 con->v2.in_state = IN_S_HANDLE_CONTROL_REMAINDER; 1829 return 0; 1830 } 1831 1832 static int prepare_read_data(struct ceph_connection *con) 1833 { 1834 struct bio_vec bv; 1835 1836 con->in_data_crc = -1; 1837 ceph_msg_data_cursor_init(&con->v2.in_cursor, con->in_msg, 1838 data_len(con->in_msg)); 1839 1840 get_bvec_at(&con->v2.in_cursor, &bv); 1841 if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) { 1842 if (unlikely(!con->bounce_page)) { 1843 con->bounce_page = alloc_page(GFP_NOIO); 1844 if (!con->bounce_page) { 1845 pr_err("failed to allocate bounce page\n"); 1846 return -ENOMEM; 1847 } 1848 } 1849 1850 bv.bv_page = con->bounce_page; 1851 bv.bv_offset = 0; 1852 } 1853 set_in_bvec(con, &bv); 1854 con->v2.in_state = IN_S_PREPARE_READ_DATA_CONT; 1855 return 0; 1856 } 1857 1858 static void prepare_read_data_cont(struct ceph_connection *con) 1859 { 1860 struct bio_vec bv; 1861 1862 if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) { 1863 con->in_data_crc = crc32c(con->in_data_crc, 1864 page_address(con->bounce_page), 1865 con->v2.in_bvec.bv_len); 1866 1867 get_bvec_at(&con->v2.in_cursor, &bv); 1868 memcpy_to_page(bv.bv_page, bv.bv_offset, 1869 page_address(con->bounce_page), 1870 con->v2.in_bvec.bv_len); 1871 } else { 1872 con->in_data_crc = ceph_crc32c_page(con->in_data_crc, 1873 con->v2.in_bvec.bv_page, 1874 con->v2.in_bvec.bv_offset, 1875 con->v2.in_bvec.bv_len); 1876 } 1877 1878 ceph_msg_data_advance(&con->v2.in_cursor, con->v2.in_bvec.bv_len); 1879 if (con->v2.in_cursor.total_resid) { 1880 get_bvec_at(&con->v2.in_cursor, &bv); 1881 if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) { 1882 bv.bv_page = con->bounce_page; 1883 bv.bv_offset = 0; 1884 } 1885 set_in_bvec(con, &bv); 1886 WARN_ON(con->v2.in_state != IN_S_PREPARE_READ_DATA_CONT); 1887 return; 1888 } 1889 1890 /* 1891 * We've read all data. Prepare to read epilogue. 1892 */ 1893 reset_in_kvecs(con); 1894 add_in_kvec(con, con->v2.in_buf, CEPH_EPILOGUE_PLAIN_LEN); 1895 con->v2.in_state = IN_S_HANDLE_EPILOGUE; 1896 } 1897 1898 static int prepare_sparse_read_cont(struct ceph_connection *con) 1899 { 1900 int ret; 1901 struct bio_vec bv; 1902 char *buf = NULL; 1903 struct ceph_msg_data_cursor *cursor = &con->v2.in_cursor; 1904 1905 WARN_ON(con->v2.in_state != IN_S_PREPARE_SPARSE_DATA_CONT); 1906 1907 if (iov_iter_is_bvec(&con->v2.in_iter)) { 1908 if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) { 1909 con->in_data_crc = crc32c(con->in_data_crc, 1910 page_address(con->bounce_page), 1911 con->v2.in_bvec.bv_len); 1912 get_bvec_at(cursor, &bv); 1913 memcpy_to_page(bv.bv_page, bv.bv_offset, 1914 page_address(con->bounce_page), 1915 con->v2.in_bvec.bv_len); 1916 } else { 1917 con->in_data_crc = ceph_crc32c_page(con->in_data_crc, 1918 con->v2.in_bvec.bv_page, 1919 con->v2.in_bvec.bv_offset, 1920 con->v2.in_bvec.bv_len); 1921 } 1922 1923 ceph_msg_data_advance(cursor, con->v2.in_bvec.bv_len); 1924 cursor->sr_resid -= con->v2.in_bvec.bv_len; 1925 dout("%s: advance by 0x%x sr_resid 0x%x\n", __func__, 1926 con->v2.in_bvec.bv_len, cursor->sr_resid); 1927 WARN_ON_ONCE(cursor->sr_resid > cursor->total_resid); 1928 if (cursor->sr_resid) { 1929 get_bvec_at(cursor, &bv); 1930 if (bv.bv_len > cursor->sr_resid) 1931 bv.bv_len = cursor->sr_resid; 1932 if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) { 1933 bv.bv_page = con->bounce_page; 1934 bv.bv_offset = 0; 1935 } 1936 set_in_bvec(con, &bv); 1937 con->v2.data_len_remain -= bv.bv_len; 1938 return 0; 1939 } 1940 } else if (iov_iter_is_kvec(&con->v2.in_iter)) { 1941 /* On first call, we have no kvec so don't compute crc */ 1942 if (con->v2.in_kvec_cnt) { 1943 WARN_ON_ONCE(con->v2.in_kvec_cnt > 1); 1944 con->in_data_crc = crc32c(con->in_data_crc, 1945 con->v2.in_kvecs[0].iov_base, 1946 con->v2.in_kvecs[0].iov_len); 1947 } 1948 } else { 1949 return -EIO; 1950 } 1951 1952 /* get next extent */ 1953 ret = con->ops->sparse_read(con, cursor, &buf); 1954 if (ret <= 0) { 1955 if (ret < 0) 1956 return ret; 1957 1958 reset_in_kvecs(con); 1959 add_in_kvec(con, con->v2.in_buf, CEPH_EPILOGUE_PLAIN_LEN); 1960 con->v2.in_state = IN_S_HANDLE_EPILOGUE; 1961 return 0; 1962 } 1963 1964 if (buf) { 1965 /* receive into buffer */ 1966 reset_in_kvecs(con); 1967 add_in_kvec(con, buf, ret); 1968 con->v2.data_len_remain -= ret; 1969 return 0; 1970 } 1971 1972 if (ret > cursor->total_resid) { 1973 pr_warn("%s: ret 0x%x total_resid 0x%zx resid 0x%zx\n", 1974 __func__, ret, cursor->total_resid, cursor->resid); 1975 return -EIO; 1976 } 1977 get_bvec_at(cursor, &bv); 1978 if (bv.bv_len > cursor->sr_resid) 1979 bv.bv_len = cursor->sr_resid; 1980 if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) { 1981 if (unlikely(!con->bounce_page)) { 1982 con->bounce_page = alloc_page(GFP_NOIO); 1983 if (!con->bounce_page) { 1984 pr_err("failed to allocate bounce page\n"); 1985 return -ENOMEM; 1986 } 1987 } 1988 1989 bv.bv_page = con->bounce_page; 1990 bv.bv_offset = 0; 1991 } 1992 set_in_bvec(con, &bv); 1993 con->v2.data_len_remain -= ret; 1994 return ret; 1995 } 1996 1997 static int prepare_sparse_read_data(struct ceph_connection *con) 1998 { 1999 struct ceph_msg *msg = con->in_msg; 2000 2001 dout("%s: starting sparse read\n", __func__); 2002 2003 if (WARN_ON_ONCE(!con->ops->sparse_read)) 2004 return -EOPNOTSUPP; 2005 2006 if (!con_secure(con)) 2007 con->in_data_crc = -1; 2008 2009 ceph_msg_data_cursor_init(&con->v2.in_cursor, msg, 2010 msg->sparse_read_total); 2011 2012 reset_in_kvecs(con); 2013 con->v2.in_state = IN_S_PREPARE_SPARSE_DATA_CONT; 2014 con->v2.data_len_remain = data_len(msg); 2015 return prepare_sparse_read_cont(con); 2016 } 2017 2018 static int prepare_read_tail_plain(struct ceph_connection *con) 2019 { 2020 struct ceph_msg *msg = con->in_msg; 2021 2022 if (!front_len(msg) && !middle_len(msg)) { 2023 WARN_ON(!data_len(msg)); 2024 return prepare_read_data(con); 2025 } 2026 2027 reset_in_kvecs(con); 2028 if (front_len(msg)) { 2029 add_in_kvec(con, msg->front.iov_base, front_len(msg)); 2030 WARN_ON(msg->front.iov_len != front_len(msg)); 2031 } 2032 if (middle_len(msg)) { 2033 add_in_kvec(con, msg->middle->vec.iov_base, middle_len(msg)); 2034 WARN_ON(msg->middle->vec.iov_len != middle_len(msg)); 2035 } 2036 2037 if (data_len(msg)) { 2038 if (msg->sparse_read_total) 2039 con->v2.in_state = IN_S_PREPARE_SPARSE_DATA; 2040 else 2041 con->v2.in_state = IN_S_PREPARE_READ_DATA; 2042 } else { 2043 add_in_kvec(con, con->v2.in_buf, CEPH_EPILOGUE_PLAIN_LEN); 2044 con->v2.in_state = IN_S_HANDLE_EPILOGUE; 2045 } 2046 return 0; 2047 } 2048 2049 static void prepare_read_enc_page(struct ceph_connection *con) 2050 { 2051 struct bio_vec bv; 2052 2053 dout("%s con %p i %d resid %d\n", __func__, con, con->v2.in_enc_i, 2054 con->v2.in_enc_resid); 2055 WARN_ON(!con->v2.in_enc_resid); 2056 2057 bvec_set_page(&bv, con->v2.in_enc_pages[con->v2.in_enc_i], 2058 min(con->v2.in_enc_resid, (int)PAGE_SIZE), 0); 2059 2060 set_in_bvec(con, &bv); 2061 con->v2.in_enc_i++; 2062 con->v2.in_enc_resid -= bv.bv_len; 2063 2064 if (con->v2.in_enc_resid) { 2065 con->v2.in_state = IN_S_PREPARE_READ_ENC_PAGE; 2066 return; 2067 } 2068 2069 /* 2070 * We are set to read the last piece of ciphertext (ending 2071 * with epilogue) + auth tag. 2072 */ 2073 WARN_ON(con->v2.in_enc_i != con->v2.in_enc_page_cnt); 2074 con->v2.in_state = IN_S_HANDLE_EPILOGUE; 2075 } 2076 2077 static int prepare_read_tail_secure(struct ceph_connection *con) 2078 { 2079 struct page **enc_pages; 2080 int enc_page_cnt; 2081 int tail_len; 2082 2083 tail_len = tail_onwire_len(con->in_msg, true); 2084 WARN_ON(!tail_len); 2085 2086 enc_page_cnt = calc_pages_for(0, tail_len); 2087 enc_pages = ceph_alloc_page_vector(enc_page_cnt, GFP_NOIO); 2088 if (IS_ERR(enc_pages)) 2089 return PTR_ERR(enc_pages); 2090 2091 WARN_ON(con->v2.in_enc_pages || con->v2.in_enc_page_cnt); 2092 con->v2.in_enc_pages = enc_pages; 2093 con->v2.in_enc_page_cnt = enc_page_cnt; 2094 con->v2.in_enc_resid = tail_len; 2095 con->v2.in_enc_i = 0; 2096 2097 prepare_read_enc_page(con); 2098 return 0; 2099 } 2100 2101 static void __finish_skip(struct ceph_connection *con) 2102 { 2103 con->in_seq++; 2104 prepare_read_preamble(con); 2105 } 2106 2107 static void prepare_skip_message(struct ceph_connection *con) 2108 { 2109 struct ceph_frame_desc *desc = &con->v2.in_desc; 2110 int tail_len; 2111 2112 dout("%s con %p %d+%d+%d\n", __func__, con, desc->fd_lens[1], 2113 desc->fd_lens[2], desc->fd_lens[3]); 2114 2115 tail_len = __tail_onwire_len(desc->fd_lens[1], desc->fd_lens[2], 2116 desc->fd_lens[3], con_secure(con)); 2117 if (!tail_len) { 2118 __finish_skip(con); 2119 } else { 2120 set_in_skip(con, tail_len); 2121 con->v2.in_state = IN_S_FINISH_SKIP; 2122 } 2123 } 2124 2125 static int process_banner_prefix(struct ceph_connection *con) 2126 { 2127 int payload_len; 2128 void *p; 2129 2130 WARN_ON(con->v2.in_kvecs[0].iov_len != CEPH_BANNER_V2_PREFIX_LEN); 2131 2132 p = con->v2.in_kvecs[0].iov_base; 2133 if (memcmp(p, CEPH_BANNER_V2, CEPH_BANNER_V2_LEN)) { 2134 if (!memcmp(p, CEPH_BANNER, CEPH_BANNER_LEN)) 2135 con->error_msg = "server is speaking msgr1 protocol"; 2136 else 2137 con->error_msg = "protocol error, bad banner"; 2138 return -EINVAL; 2139 } 2140 2141 p += CEPH_BANNER_V2_LEN; 2142 payload_len = ceph_decode_16(&p); 2143 dout("%s con %p payload_len %d\n", __func__, con, payload_len); 2144 2145 return prepare_read_banner_payload(con, payload_len); 2146 } 2147 2148 static int process_banner_payload(struct ceph_connection *con) 2149 { 2150 void *end = con->v2.in_kvecs[0].iov_base + con->v2.in_kvecs[0].iov_len; 2151 u64 feat = CEPH_MSGR2_SUPPORTED_FEATURES; 2152 u64 req_feat = CEPH_MSGR2_REQUIRED_FEATURES; 2153 u64 server_feat, server_req_feat; 2154 void *p; 2155 int ret; 2156 2157 p = con->v2.in_kvecs[0].iov_base; 2158 ceph_decode_64_safe(&p, end, server_feat, bad); 2159 ceph_decode_64_safe(&p, end, server_req_feat, bad); 2160 2161 dout("%s con %p server_feat 0x%llx server_req_feat 0x%llx\n", 2162 __func__, con, server_feat, server_req_feat); 2163 2164 if (req_feat & ~server_feat) { 2165 pr_err("msgr2 feature set mismatch: my required > server's supported 0x%llx, need 0x%llx\n", 2166 server_feat, req_feat & ~server_feat); 2167 con->error_msg = "missing required protocol features"; 2168 return -EINVAL; 2169 } 2170 if (server_req_feat & ~feat) { 2171 pr_err("msgr2 feature set mismatch: server's required > my supported 0x%llx, missing 0x%llx\n", 2172 feat, server_req_feat & ~feat); 2173 con->error_msg = "missing required protocol features"; 2174 return -EINVAL; 2175 } 2176 2177 /* no reset_out_kvecs() as our banner may still be pending */ 2178 ret = prepare_hello(con); 2179 if (ret) { 2180 pr_err("prepare_hello failed: %d\n", ret); 2181 return ret; 2182 } 2183 2184 con->state = CEPH_CON_S_V2_HELLO; 2185 prepare_read_preamble(con); 2186 return 0; 2187 2188 bad: 2189 pr_err("failed to decode banner payload\n"); 2190 return -EINVAL; 2191 } 2192 2193 static int process_hello(struct ceph_connection *con, void *p, void *end) 2194 { 2195 struct ceph_entity_addr *my_addr = &con->msgr->inst.addr; 2196 struct ceph_entity_addr addr_for_me; 2197 u8 entity_type; 2198 int ret; 2199 2200 if (con->state != CEPH_CON_S_V2_HELLO) { 2201 con->error_msg = "protocol error, unexpected hello"; 2202 return -EINVAL; 2203 } 2204 2205 ceph_decode_8_safe(&p, end, entity_type, bad); 2206 ret = ceph_decode_entity_addr(&p, end, &addr_for_me); 2207 if (ret) { 2208 pr_err("failed to decode addr_for_me: %d\n", ret); 2209 return ret; 2210 } 2211 2212 dout("%s con %p entity_type %d addr_for_me %s\n", __func__, con, 2213 entity_type, ceph_pr_addr(&addr_for_me)); 2214 2215 if (entity_type != con->peer_name.type) { 2216 pr_err("bad peer type, want %d, got %d\n", 2217 con->peer_name.type, entity_type); 2218 con->error_msg = "wrong peer at address"; 2219 return -EINVAL; 2220 } 2221 2222 /* 2223 * Set our address to the address our first peer (i.e. monitor) 2224 * sees that we are connecting from. If we are behind some sort 2225 * of NAT and want to be identified by some private (not NATed) 2226 * address, ip option should be used. 2227 */ 2228 if (ceph_addr_is_blank(my_addr)) { 2229 memcpy(&my_addr->in_addr, &addr_for_me.in_addr, 2230 sizeof(my_addr->in_addr)); 2231 ceph_addr_set_port(my_addr, 0); 2232 dout("%s con %p set my addr %s, as seen by peer %s\n", 2233 __func__, con, ceph_pr_addr(my_addr), 2234 ceph_pr_addr(&con->peer_addr)); 2235 } else { 2236 dout("%s con %p my addr already set %s\n", 2237 __func__, con, ceph_pr_addr(my_addr)); 2238 } 2239 2240 WARN_ON(ceph_addr_is_blank(my_addr) || ceph_addr_port(my_addr)); 2241 WARN_ON(my_addr->type != CEPH_ENTITY_ADDR_TYPE_ANY); 2242 WARN_ON(!my_addr->nonce); 2243 2244 /* no reset_out_kvecs() as our hello may still be pending */ 2245 ret = prepare_auth_request(con); 2246 if (ret) { 2247 if (ret != -EAGAIN) 2248 pr_err("prepare_auth_request failed: %d\n", ret); 2249 return ret; 2250 } 2251 2252 con->state = CEPH_CON_S_V2_AUTH; 2253 return 0; 2254 2255 bad: 2256 pr_err("failed to decode hello\n"); 2257 return -EINVAL; 2258 } 2259 2260 static int process_auth_bad_method(struct ceph_connection *con, 2261 void *p, void *end) 2262 { 2263 int allowed_protos[8], allowed_modes[8]; 2264 int allowed_proto_cnt, allowed_mode_cnt; 2265 int used_proto, result; 2266 int ret; 2267 int i; 2268 2269 if (con->state != CEPH_CON_S_V2_AUTH) { 2270 con->error_msg = "protocol error, unexpected auth_bad_method"; 2271 return -EINVAL; 2272 } 2273 2274 ceph_decode_32_safe(&p, end, used_proto, bad); 2275 ceph_decode_32_safe(&p, end, result, bad); 2276 dout("%s con %p used_proto %d result %d\n", __func__, con, used_proto, 2277 result); 2278 2279 ceph_decode_32_safe(&p, end, allowed_proto_cnt, bad); 2280 if (allowed_proto_cnt > ARRAY_SIZE(allowed_protos)) { 2281 pr_err("allowed_protos too big %d\n", allowed_proto_cnt); 2282 return -EINVAL; 2283 } 2284 for (i = 0; i < allowed_proto_cnt; i++) { 2285 ceph_decode_32_safe(&p, end, allowed_protos[i], bad); 2286 dout("%s con %p allowed_protos[%d] %d\n", __func__, con, 2287 i, allowed_protos[i]); 2288 } 2289 2290 ceph_decode_32_safe(&p, end, allowed_mode_cnt, bad); 2291 if (allowed_mode_cnt > ARRAY_SIZE(allowed_modes)) { 2292 pr_err("allowed_modes too big %d\n", allowed_mode_cnt); 2293 return -EINVAL; 2294 } 2295 for (i = 0; i < allowed_mode_cnt; i++) { 2296 ceph_decode_32_safe(&p, end, allowed_modes[i], bad); 2297 dout("%s con %p allowed_modes[%d] %d\n", __func__, con, 2298 i, allowed_modes[i]); 2299 } 2300 2301 mutex_unlock(&con->mutex); 2302 ret = con->ops->handle_auth_bad_method(con, used_proto, result, 2303 allowed_protos, 2304 allowed_proto_cnt, 2305 allowed_modes, 2306 allowed_mode_cnt); 2307 mutex_lock(&con->mutex); 2308 if (con->state != CEPH_CON_S_V2_AUTH) { 2309 dout("%s con %p state changed to %d\n", __func__, con, 2310 con->state); 2311 return -EAGAIN; 2312 } 2313 2314 dout("%s con %p handle_auth_bad_method ret %d\n", __func__, con, ret); 2315 return ret; 2316 2317 bad: 2318 pr_err("failed to decode auth_bad_method\n"); 2319 return -EINVAL; 2320 } 2321 2322 static int process_auth_reply_more(struct ceph_connection *con, 2323 void *p, void *end) 2324 { 2325 int payload_len; 2326 int ret; 2327 2328 if (con->state != CEPH_CON_S_V2_AUTH) { 2329 con->error_msg = "protocol error, unexpected auth_reply_more"; 2330 return -EINVAL; 2331 } 2332 2333 ceph_decode_32_safe(&p, end, payload_len, bad); 2334 ceph_decode_need(&p, end, payload_len, bad); 2335 2336 dout("%s con %p payload_len %d\n", __func__, con, payload_len); 2337 2338 reset_out_kvecs(con); 2339 ret = prepare_auth_request_more(con, p, payload_len); 2340 if (ret) { 2341 if (ret != -EAGAIN) 2342 pr_err("prepare_auth_request_more failed: %d\n", ret); 2343 return ret; 2344 } 2345 2346 return 0; 2347 2348 bad: 2349 pr_err("failed to decode auth_reply_more\n"); 2350 return -EINVAL; 2351 } 2352 2353 /* 2354 * Align con_secret to avoid GFP_ATOMIC allocation inside 2355 * crypto_aead_setkey() called from setup_crypto(). __aligned(16) 2356 * isn't guaranteed to work for stack objects, so do it by hand. 2357 */ 2358 static int process_auth_done(struct ceph_connection *con, void *p, void *end) 2359 { 2360 u8 session_key[CEPH_MAX_KEY_LEN]; 2361 u8 con_secret_buf[CEPH_MAX_CON_SECRET_LEN + 16]; 2362 u8 *con_secret = PTR_ALIGN(&con_secret_buf[0], 16); 2363 int session_key_len, con_secret_len; 2364 int payload_len; 2365 u64 global_id; 2366 int ret; 2367 2368 if (con->state != CEPH_CON_S_V2_AUTH) { 2369 con->error_msg = "protocol error, unexpected auth_done"; 2370 return -EINVAL; 2371 } 2372 2373 ceph_decode_64_safe(&p, end, global_id, bad); 2374 ceph_decode_32_safe(&p, end, con->v2.con_mode, bad); 2375 2376 ceph_decode_32_safe(&p, end, payload_len, bad); 2377 ceph_decode_need(&p, end, payload_len, bad); 2378 2379 dout("%s con %p global_id %llu con_mode %d payload_len %d\n", 2380 __func__, con, global_id, con->v2.con_mode, payload_len); 2381 2382 mutex_unlock(&con->mutex); 2383 session_key_len = 0; 2384 con_secret_len = 0; 2385 ret = con->ops->handle_auth_done(con, global_id, p, payload_len, 2386 session_key, &session_key_len, 2387 con_secret, &con_secret_len); 2388 mutex_lock(&con->mutex); 2389 if (con->state != CEPH_CON_S_V2_AUTH) { 2390 dout("%s con %p state changed to %d\n", __func__, con, 2391 con->state); 2392 ret = -EAGAIN; 2393 goto out; 2394 } 2395 2396 dout("%s con %p handle_auth_done ret %d\n", __func__, con, ret); 2397 if (ret) 2398 goto out; 2399 2400 ret = setup_crypto(con, session_key, session_key_len, con_secret, 2401 con_secret_len); 2402 if (ret) 2403 goto out; 2404 2405 reset_out_kvecs(con); 2406 ret = prepare_auth_signature(con); 2407 if (ret) { 2408 pr_err("prepare_auth_signature failed: %d\n", ret); 2409 goto out; 2410 } 2411 2412 con->state = CEPH_CON_S_V2_AUTH_SIGNATURE; 2413 2414 out: 2415 memzero_explicit(session_key, sizeof(session_key)); 2416 memzero_explicit(con_secret_buf, sizeof(con_secret_buf)); 2417 return ret; 2418 2419 bad: 2420 pr_err("failed to decode auth_done\n"); 2421 return -EINVAL; 2422 } 2423 2424 static int process_auth_signature(struct ceph_connection *con, 2425 void *p, void *end) 2426 { 2427 u8 hmac[SHA256_DIGEST_SIZE]; 2428 int ret; 2429 2430 if (con->state != CEPH_CON_S_V2_AUTH_SIGNATURE) { 2431 con->error_msg = "protocol error, unexpected auth_signature"; 2432 return -EINVAL; 2433 } 2434 2435 con_hmac_sha256(con, con->v2.out_sign_kvecs, con->v2.out_sign_kvec_cnt, 2436 hmac); 2437 2438 ceph_decode_need(&p, end, SHA256_DIGEST_SIZE, bad); 2439 if (crypto_memneq(p, hmac, SHA256_DIGEST_SIZE)) { 2440 con->error_msg = "integrity error, bad auth signature"; 2441 return -EBADMSG; 2442 } 2443 2444 dout("%s con %p auth signature ok\n", __func__, con); 2445 2446 /* no reset_out_kvecs() as our auth_signature may still be pending */ 2447 if (!con->v2.server_cookie) { 2448 ret = prepare_client_ident(con); 2449 if (ret) { 2450 pr_err("prepare_client_ident failed: %d\n", ret); 2451 return ret; 2452 } 2453 2454 con->state = CEPH_CON_S_V2_SESSION_CONNECT; 2455 } else { 2456 ret = prepare_session_reconnect(con); 2457 if (ret) { 2458 pr_err("prepare_session_reconnect failed: %d\n", ret); 2459 return ret; 2460 } 2461 2462 con->state = CEPH_CON_S_V2_SESSION_RECONNECT; 2463 } 2464 2465 return 0; 2466 2467 bad: 2468 pr_err("failed to decode auth_signature\n"); 2469 return -EINVAL; 2470 } 2471 2472 static int process_server_ident(struct ceph_connection *con, 2473 void *p, void *end) 2474 { 2475 struct ceph_client *client = from_msgr(con->msgr); 2476 u64 features, required_features; 2477 struct ceph_entity_addr addr; 2478 u64 global_seq; 2479 u64 global_id; 2480 u64 cookie; 2481 u64 flags; 2482 int ret; 2483 2484 if (con->state != CEPH_CON_S_V2_SESSION_CONNECT) { 2485 con->error_msg = "protocol error, unexpected server_ident"; 2486 return -EINVAL; 2487 } 2488 2489 ret = ceph_decode_entity_addrvec(&p, end, true, &addr); 2490 if (ret) { 2491 pr_err("failed to decode server addrs: %d\n", ret); 2492 return ret; 2493 } 2494 2495 ceph_decode_64_safe(&p, end, global_id, bad); 2496 ceph_decode_64_safe(&p, end, global_seq, bad); 2497 ceph_decode_64_safe(&p, end, features, bad); 2498 ceph_decode_64_safe(&p, end, required_features, bad); 2499 ceph_decode_64_safe(&p, end, flags, bad); 2500 ceph_decode_64_safe(&p, end, cookie, bad); 2501 2502 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", 2503 __func__, con, ceph_pr_addr(&addr), le32_to_cpu(addr.nonce), 2504 global_id, global_seq, features, required_features, flags, cookie); 2505 2506 /* is this who we intended to talk to? */ 2507 if (memcmp(&addr, &con->peer_addr, sizeof(con->peer_addr))) { 2508 pr_err("bad peer addr/nonce, want %s/%u, got %s/%u\n", 2509 ceph_pr_addr(&con->peer_addr), 2510 le32_to_cpu(con->peer_addr.nonce), 2511 ceph_pr_addr(&addr), le32_to_cpu(addr.nonce)); 2512 con->error_msg = "wrong peer at address"; 2513 return -EINVAL; 2514 } 2515 2516 if (client->required_features & ~features) { 2517 pr_err("RADOS feature set mismatch: my required > server's supported 0x%llx, need 0x%llx\n", 2518 features, client->required_features & ~features); 2519 con->error_msg = "missing required protocol features"; 2520 return -EINVAL; 2521 } 2522 2523 /* 2524 * Both name->type and name->num are set in ceph_con_open() but 2525 * name->num may be bogus in the initial monmap. name->type is 2526 * verified in handle_hello(). 2527 */ 2528 WARN_ON(!con->peer_name.type); 2529 con->peer_name.num = cpu_to_le64(global_id); 2530 con->v2.peer_global_seq = global_seq; 2531 con->peer_features = features; 2532 WARN_ON(required_features & ~client->supported_features); 2533 con->v2.server_cookie = cookie; 2534 2535 if (flags & CEPH_MSG_CONNECT_LOSSY) { 2536 ceph_con_flag_set(con, CEPH_CON_F_LOSSYTX); 2537 WARN_ON(con->v2.server_cookie); 2538 } else { 2539 WARN_ON(!con->v2.server_cookie); 2540 } 2541 2542 clear_in_sign_kvecs(con); 2543 clear_out_sign_kvecs(con); 2544 free_conn_bufs(con); 2545 con->delay = 0; /* reset backoff memory */ 2546 2547 con->state = CEPH_CON_S_OPEN; 2548 con->v2.out_state = OUT_S_GET_NEXT; 2549 return 0; 2550 2551 bad: 2552 pr_err("failed to decode server_ident\n"); 2553 return -EINVAL; 2554 } 2555 2556 static int process_ident_missing_features(struct ceph_connection *con, 2557 void *p, void *end) 2558 { 2559 struct ceph_client *client = from_msgr(con->msgr); 2560 u64 missing_features; 2561 2562 if (con->state != CEPH_CON_S_V2_SESSION_CONNECT) { 2563 con->error_msg = "protocol error, unexpected ident_missing_features"; 2564 return -EINVAL; 2565 } 2566 2567 ceph_decode_64_safe(&p, end, missing_features, bad); 2568 pr_err("RADOS feature set mismatch: server's required > my supported 0x%llx, missing 0x%llx\n", 2569 client->supported_features, missing_features); 2570 con->error_msg = "missing required protocol features"; 2571 return -EINVAL; 2572 2573 bad: 2574 pr_err("failed to decode ident_missing_features\n"); 2575 return -EINVAL; 2576 } 2577 2578 static int process_session_reconnect_ok(struct ceph_connection *con, 2579 void *p, void *end) 2580 { 2581 u64 seq; 2582 2583 if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) { 2584 con->error_msg = "protocol error, unexpected session_reconnect_ok"; 2585 return -EINVAL; 2586 } 2587 2588 ceph_decode_64_safe(&p, end, seq, bad); 2589 2590 dout("%s con %p seq %llu\n", __func__, con, seq); 2591 ceph_con_discard_requeued(con, seq); 2592 2593 clear_in_sign_kvecs(con); 2594 clear_out_sign_kvecs(con); 2595 free_conn_bufs(con); 2596 con->delay = 0; /* reset backoff memory */ 2597 2598 con->state = CEPH_CON_S_OPEN; 2599 con->v2.out_state = OUT_S_GET_NEXT; 2600 return 0; 2601 2602 bad: 2603 pr_err("failed to decode session_reconnect_ok\n"); 2604 return -EINVAL; 2605 } 2606 2607 static int process_session_retry(struct ceph_connection *con, 2608 void *p, void *end) 2609 { 2610 u64 connect_seq; 2611 int ret; 2612 2613 if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) { 2614 con->error_msg = "protocol error, unexpected session_retry"; 2615 return -EINVAL; 2616 } 2617 2618 ceph_decode_64_safe(&p, end, connect_seq, bad); 2619 2620 dout("%s con %p connect_seq %llu\n", __func__, con, connect_seq); 2621 WARN_ON(connect_seq <= con->v2.connect_seq); 2622 con->v2.connect_seq = connect_seq + 1; 2623 2624 free_conn_bufs(con); 2625 2626 reset_out_kvecs(con); 2627 ret = prepare_session_reconnect(con); 2628 if (ret) { 2629 pr_err("prepare_session_reconnect (cseq) failed: %d\n", ret); 2630 return ret; 2631 } 2632 2633 return 0; 2634 2635 bad: 2636 pr_err("failed to decode session_retry\n"); 2637 return -EINVAL; 2638 } 2639 2640 static int process_session_retry_global(struct ceph_connection *con, 2641 void *p, void *end) 2642 { 2643 u64 global_seq; 2644 int ret; 2645 2646 if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) { 2647 con->error_msg = "protocol error, unexpected session_retry_global"; 2648 return -EINVAL; 2649 } 2650 2651 ceph_decode_64_safe(&p, end, global_seq, bad); 2652 2653 dout("%s con %p global_seq %llu\n", __func__, con, global_seq); 2654 WARN_ON(global_seq <= con->v2.global_seq); 2655 con->v2.global_seq = ceph_get_global_seq(con->msgr, global_seq); 2656 2657 free_conn_bufs(con); 2658 2659 reset_out_kvecs(con); 2660 ret = prepare_session_reconnect(con); 2661 if (ret) { 2662 pr_err("prepare_session_reconnect (gseq) failed: %d\n", ret); 2663 return ret; 2664 } 2665 2666 return 0; 2667 2668 bad: 2669 pr_err("failed to decode session_retry_global\n"); 2670 return -EINVAL; 2671 } 2672 2673 static int process_session_reset(struct ceph_connection *con, 2674 void *p, void *end) 2675 { 2676 bool full; 2677 int ret; 2678 2679 if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) { 2680 con->error_msg = "protocol error, unexpected session_reset"; 2681 return -EINVAL; 2682 } 2683 2684 ceph_decode_8_safe(&p, end, full, bad); 2685 if (!full) { 2686 con->error_msg = "protocol error, bad session_reset"; 2687 return -EINVAL; 2688 } 2689 2690 pr_info("%s%lld %s session reset\n", ENTITY_NAME(con->peer_name), 2691 ceph_pr_addr(&con->peer_addr)); 2692 ceph_con_reset_session(con); 2693 2694 mutex_unlock(&con->mutex); 2695 if (con->ops->peer_reset) 2696 con->ops->peer_reset(con); 2697 mutex_lock(&con->mutex); 2698 if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) { 2699 dout("%s con %p state changed to %d\n", __func__, con, 2700 con->state); 2701 return -EAGAIN; 2702 } 2703 2704 free_conn_bufs(con); 2705 2706 reset_out_kvecs(con); 2707 ret = prepare_client_ident(con); 2708 if (ret) { 2709 pr_err("prepare_client_ident (rst) failed: %d\n", ret); 2710 return ret; 2711 } 2712 2713 con->state = CEPH_CON_S_V2_SESSION_CONNECT; 2714 return 0; 2715 2716 bad: 2717 pr_err("failed to decode session_reset\n"); 2718 return -EINVAL; 2719 } 2720 2721 static int process_keepalive2_ack(struct ceph_connection *con, 2722 void *p, void *end) 2723 { 2724 if (con->state != CEPH_CON_S_OPEN) { 2725 con->error_msg = "protocol error, unexpected keepalive2_ack"; 2726 return -EINVAL; 2727 } 2728 2729 ceph_decode_need(&p, end, sizeof(struct ceph_timespec), bad); 2730 ceph_decode_timespec64(&con->last_keepalive_ack, p); 2731 2732 dout("%s con %p timestamp %ptSp\n", __func__, con, &con->last_keepalive_ack); 2733 2734 return 0; 2735 2736 bad: 2737 pr_err("failed to decode keepalive2_ack\n"); 2738 return -EINVAL; 2739 } 2740 2741 static int process_ack(struct ceph_connection *con, void *p, void *end) 2742 { 2743 u64 seq; 2744 2745 if (con->state != CEPH_CON_S_OPEN) { 2746 con->error_msg = "protocol error, unexpected ack"; 2747 return -EINVAL; 2748 } 2749 2750 ceph_decode_64_safe(&p, end, seq, bad); 2751 2752 dout("%s con %p seq %llu\n", __func__, con, seq); 2753 ceph_con_discard_sent(con, seq); 2754 return 0; 2755 2756 bad: 2757 pr_err("failed to decode ack\n"); 2758 return -EINVAL; 2759 } 2760 2761 static int process_control(struct ceph_connection *con, void *p, void *end) 2762 { 2763 int tag = con->v2.in_desc.fd_tag; 2764 int ret; 2765 2766 dout("%s con %p tag %d len %d\n", __func__, con, tag, (int)(end - p)); 2767 2768 switch (tag) { 2769 case FRAME_TAG_HELLO: 2770 ret = process_hello(con, p, end); 2771 break; 2772 case FRAME_TAG_AUTH_BAD_METHOD: 2773 ret = process_auth_bad_method(con, p, end); 2774 break; 2775 case FRAME_TAG_AUTH_REPLY_MORE: 2776 ret = process_auth_reply_more(con, p, end); 2777 break; 2778 case FRAME_TAG_AUTH_DONE: 2779 ret = process_auth_done(con, p, end); 2780 break; 2781 case FRAME_TAG_AUTH_SIGNATURE: 2782 ret = process_auth_signature(con, p, end); 2783 break; 2784 case FRAME_TAG_SERVER_IDENT: 2785 ret = process_server_ident(con, p, end); 2786 break; 2787 case FRAME_TAG_IDENT_MISSING_FEATURES: 2788 ret = process_ident_missing_features(con, p, end); 2789 break; 2790 case FRAME_TAG_SESSION_RECONNECT_OK: 2791 ret = process_session_reconnect_ok(con, p, end); 2792 break; 2793 case FRAME_TAG_SESSION_RETRY: 2794 ret = process_session_retry(con, p, end); 2795 break; 2796 case FRAME_TAG_SESSION_RETRY_GLOBAL: 2797 ret = process_session_retry_global(con, p, end); 2798 break; 2799 case FRAME_TAG_SESSION_RESET: 2800 ret = process_session_reset(con, p, end); 2801 break; 2802 case FRAME_TAG_KEEPALIVE2_ACK: 2803 ret = process_keepalive2_ack(con, p, end); 2804 break; 2805 case FRAME_TAG_ACK: 2806 ret = process_ack(con, p, end); 2807 break; 2808 default: 2809 pr_err("bad tag %d\n", tag); 2810 con->error_msg = "protocol error, bad tag"; 2811 return -EINVAL; 2812 } 2813 if (ret) { 2814 dout("%s con %p error %d\n", __func__, con, ret); 2815 return ret; 2816 } 2817 2818 prepare_read_preamble(con); 2819 return 0; 2820 } 2821 2822 /* 2823 * Return: 2824 * 1 - con->in_msg set, read message 2825 * 0 - skip message 2826 * <0 - error 2827 */ 2828 static int process_message_header(struct ceph_connection *con, 2829 void *p, void *end) 2830 { 2831 struct ceph_frame_desc *desc = &con->v2.in_desc; 2832 struct ceph_msg_header2 *hdr2; 2833 struct ceph_msg_header hdr; 2834 int skip; 2835 int ret; 2836 u64 seq; 2837 2838 ceph_decode_need(&p, end, sizeof(*hdr2), bad); 2839 hdr2 = p; 2840 2841 /* verify seq# */ 2842 seq = le64_to_cpu(hdr2->seq); 2843 if ((s64)seq - (s64)con->in_seq < 1) { 2844 pr_info("%s%lld %s skipping old message: seq %llu, expected %llu\n", 2845 ENTITY_NAME(con->peer_name), 2846 ceph_pr_addr(&con->peer_addr), 2847 seq, con->in_seq + 1); 2848 return 0; 2849 } 2850 if ((s64)seq - (s64)con->in_seq > 1) { 2851 pr_err("bad seq %llu, expected %llu\n", seq, con->in_seq + 1); 2852 con->error_msg = "bad message sequence # for incoming message"; 2853 return -EBADE; 2854 } 2855 2856 ceph_con_discard_sent(con, le64_to_cpu(hdr2->ack_seq)); 2857 2858 fill_header(&hdr, hdr2, desc->fd_lens[1], desc->fd_lens[2], 2859 desc->fd_lens[3], &con->peer_name); 2860 ret = ceph_con_in_msg_alloc(con, &hdr, &skip); 2861 if (ret) 2862 return ret; 2863 2864 WARN_ON(!con->in_msg ^ skip); 2865 if (skip) 2866 return 0; 2867 2868 WARN_ON(!con->in_msg); 2869 WARN_ON(con->in_msg->con != con); 2870 return 1; 2871 2872 bad: 2873 pr_err("failed to decode message header\n"); 2874 return -EINVAL; 2875 } 2876 2877 static int process_message(struct ceph_connection *con) 2878 { 2879 ceph_con_process_message(con); 2880 2881 /* 2882 * We could have been closed by ceph_con_close() because 2883 * ceph_con_process_message() temporarily drops con->mutex. 2884 */ 2885 if (con->state != CEPH_CON_S_OPEN) { 2886 dout("%s con %p state changed to %d\n", __func__, con, 2887 con->state); 2888 return -EAGAIN; 2889 } 2890 2891 prepare_read_preamble(con); 2892 return 0; 2893 } 2894 2895 static int __handle_control(struct ceph_connection *con, void *p) 2896 { 2897 void *end = p + con->v2.in_desc.fd_lens[0]; 2898 struct ceph_msg *msg; 2899 int ret; 2900 2901 if (con->v2.in_desc.fd_tag != FRAME_TAG_MESSAGE) 2902 return process_control(con, p, end); 2903 2904 if (con->state != CEPH_CON_S_OPEN) { 2905 con->error_msg = "protocol error, unexpected message"; 2906 return -EINVAL; 2907 } 2908 2909 ret = process_message_header(con, p, end); 2910 if (ret < 0) 2911 return ret; 2912 if (ret == 0) { 2913 prepare_skip_message(con); 2914 return 0; 2915 } 2916 2917 msg = con->in_msg; /* set in process_message_header() */ 2918 if (front_len(msg)) { 2919 WARN_ON(front_len(msg) > msg->front_alloc_len); 2920 msg->front.iov_len = front_len(msg); 2921 } else { 2922 msg->front.iov_len = 0; 2923 } 2924 if (middle_len(msg)) { 2925 WARN_ON(middle_len(msg) > msg->middle->alloc_len); 2926 msg->middle->vec.iov_len = middle_len(msg); 2927 } else if (msg->middle) { 2928 msg->middle->vec.iov_len = 0; 2929 } 2930 2931 if (!front_len(msg) && !middle_len(msg) && !data_len(msg)) 2932 return process_message(con); 2933 2934 if (con_secure(con)) 2935 return prepare_read_tail_secure(con); 2936 2937 return prepare_read_tail_plain(con); 2938 } 2939 2940 static int handle_preamble(struct ceph_connection *con) 2941 { 2942 struct ceph_frame_desc *desc = &con->v2.in_desc; 2943 int ret; 2944 2945 if (con_secure(con)) { 2946 ret = decrypt_preamble(con); 2947 if (ret) { 2948 if (ret == -EBADMSG) 2949 con->error_msg = "integrity error, bad preamble auth tag"; 2950 return ret; 2951 } 2952 } 2953 2954 ret = decode_preamble(con->v2.in_buf, desc); 2955 if (ret) { 2956 if (ret == -EBADMSG) 2957 con->error_msg = "integrity error, bad crc"; 2958 else 2959 con->error_msg = "protocol error, bad preamble"; 2960 return ret; 2961 } 2962 2963 dout("%s con %p tag %d seg_cnt %d %d+%d+%d+%d\n", __func__, 2964 con, desc->fd_tag, desc->fd_seg_cnt, desc->fd_lens[0], 2965 desc->fd_lens[1], desc->fd_lens[2], desc->fd_lens[3]); 2966 2967 if (!con_secure(con)) 2968 return prepare_read_control(con); 2969 2970 if (desc->fd_lens[0] > CEPH_PREAMBLE_INLINE_LEN) 2971 return prepare_read_control_remainder(con); 2972 2973 return __handle_control(con, CTRL_BODY(con->v2.in_buf)); 2974 } 2975 2976 static int handle_control(struct ceph_connection *con) 2977 { 2978 int ctrl_len = con->v2.in_desc.fd_lens[0]; 2979 void *buf; 2980 int ret; 2981 2982 WARN_ON(con_secure(con)); 2983 2984 ret = verify_control_crc(con); 2985 if (ret) { 2986 con->error_msg = "integrity error, bad crc"; 2987 return ret; 2988 } 2989 2990 if (con->state == CEPH_CON_S_V2_AUTH) { 2991 buf = alloc_conn_buf(con, ctrl_len); 2992 if (!buf) 2993 return -ENOMEM; 2994 2995 memcpy(buf, con->v2.in_kvecs[0].iov_base, ctrl_len); 2996 return __handle_control(con, buf); 2997 } 2998 2999 return __handle_control(con, con->v2.in_kvecs[0].iov_base); 3000 } 3001 3002 static int handle_control_remainder(struct ceph_connection *con) 3003 { 3004 int ret; 3005 3006 WARN_ON(!con_secure(con)); 3007 3008 ret = decrypt_control_remainder(con); 3009 if (ret) { 3010 if (ret == -EBADMSG) 3011 con->error_msg = "integrity error, bad control remainder auth tag"; 3012 return ret; 3013 } 3014 3015 return __handle_control(con, con->v2.in_kvecs[0].iov_base - 3016 CEPH_PREAMBLE_INLINE_LEN); 3017 } 3018 3019 static int handle_epilogue(struct ceph_connection *con) 3020 { 3021 u32 front_crc, middle_crc, data_crc; 3022 int ret; 3023 3024 if (con_secure(con)) { 3025 ret = decrypt_tail(con); 3026 if (ret) { 3027 if (ret == -EBADMSG) 3028 con->error_msg = "integrity error, bad epilogue auth tag"; 3029 return ret; 3030 } 3031 3032 /* just late_status */ 3033 ret = decode_epilogue(con->v2.in_buf, NULL, NULL, NULL); 3034 if (ret) { 3035 con->error_msg = "protocol error, bad epilogue"; 3036 return ret; 3037 } 3038 } else { 3039 ret = decode_epilogue(con->v2.in_buf, &front_crc, 3040 &middle_crc, &data_crc); 3041 if (ret) { 3042 con->error_msg = "protocol error, bad epilogue"; 3043 return ret; 3044 } 3045 3046 ret = verify_epilogue_crcs(con, front_crc, middle_crc, 3047 data_crc); 3048 if (ret) { 3049 con->error_msg = "integrity error, bad crc"; 3050 return ret; 3051 } 3052 } 3053 3054 return process_message(con); 3055 } 3056 3057 static void finish_skip(struct ceph_connection *con) 3058 { 3059 dout("%s con %p\n", __func__, con); 3060 3061 if (con_secure(con)) 3062 gcm_inc_nonce(&con->v2.in_gcm_nonce); 3063 3064 __finish_skip(con); 3065 } 3066 3067 static int populate_in_iter(struct ceph_connection *con) 3068 { 3069 int ret; 3070 3071 dout("%s con %p state %d in_state %d\n", __func__, con, con->state, 3072 con->v2.in_state); 3073 WARN_ON(iov_iter_count(&con->v2.in_iter)); 3074 3075 if (con->state == CEPH_CON_S_V2_BANNER_PREFIX) { 3076 ret = process_banner_prefix(con); 3077 } else if (con->state == CEPH_CON_S_V2_BANNER_PAYLOAD) { 3078 ret = process_banner_payload(con); 3079 } else if ((con->state >= CEPH_CON_S_V2_HELLO && 3080 con->state <= CEPH_CON_S_V2_SESSION_RECONNECT) || 3081 con->state == CEPH_CON_S_OPEN) { 3082 switch (con->v2.in_state) { 3083 case IN_S_HANDLE_PREAMBLE: 3084 ret = handle_preamble(con); 3085 break; 3086 case IN_S_HANDLE_CONTROL: 3087 ret = handle_control(con); 3088 break; 3089 case IN_S_HANDLE_CONTROL_REMAINDER: 3090 ret = handle_control_remainder(con); 3091 break; 3092 case IN_S_PREPARE_READ_DATA: 3093 ret = prepare_read_data(con); 3094 break; 3095 case IN_S_PREPARE_READ_DATA_CONT: 3096 prepare_read_data_cont(con); 3097 ret = 0; 3098 break; 3099 case IN_S_PREPARE_READ_ENC_PAGE: 3100 prepare_read_enc_page(con); 3101 ret = 0; 3102 break; 3103 case IN_S_PREPARE_SPARSE_DATA: 3104 ret = prepare_sparse_read_data(con); 3105 break; 3106 case IN_S_PREPARE_SPARSE_DATA_CONT: 3107 ret = prepare_sparse_read_cont(con); 3108 break; 3109 case IN_S_HANDLE_EPILOGUE: 3110 ret = handle_epilogue(con); 3111 break; 3112 case IN_S_FINISH_SKIP: 3113 finish_skip(con); 3114 ret = 0; 3115 break; 3116 default: 3117 WARN(1, "bad in_state %d", con->v2.in_state); 3118 return -EINVAL; 3119 } 3120 } else { 3121 WARN(1, "bad state %d", con->state); 3122 return -EINVAL; 3123 } 3124 if (ret) { 3125 dout("%s con %p error %d\n", __func__, con, ret); 3126 return ret; 3127 } 3128 3129 if (WARN_ON(!iov_iter_count(&con->v2.in_iter))) 3130 return -ENODATA; 3131 dout("%s con %p populated %zu\n", __func__, con, 3132 iov_iter_count(&con->v2.in_iter)); 3133 return 1; 3134 } 3135 3136 int ceph_con_v2_try_read(struct ceph_connection *con) 3137 { 3138 int ret; 3139 3140 dout("%s con %p state %d need %zu\n", __func__, con, con->state, 3141 iov_iter_count(&con->v2.in_iter)); 3142 3143 if (con->state == CEPH_CON_S_PREOPEN) 3144 return 0; 3145 3146 /* 3147 * We should always have something pending here. If not, 3148 * avoid calling populate_in_iter() as if we read something 3149 * (ceph_tcp_recv() would immediately return 1). 3150 */ 3151 if (WARN_ON(!iov_iter_count(&con->v2.in_iter))) 3152 return -ENODATA; 3153 3154 for (;;) { 3155 ret = ceph_tcp_recv(con); 3156 if (ret <= 0) 3157 return ret; 3158 3159 ret = populate_in_iter(con); 3160 if (ret <= 0) { 3161 if (ret && ret != -EAGAIN && !con->error_msg) 3162 con->error_msg = "read processing error"; 3163 return ret; 3164 } 3165 } 3166 } 3167 3168 static void queue_data(struct ceph_connection *con, struct ceph_msg *msg) 3169 { 3170 struct bio_vec bv; 3171 3172 con->v2.out_epil.data_crc = -1; 3173 ceph_msg_data_cursor_init(&con->v2.out_cursor, msg, 3174 data_len(msg)); 3175 3176 get_bvec_at(&con->v2.out_cursor, &bv); 3177 set_out_bvec(con, &bv, true); 3178 con->v2.out_state = OUT_S_QUEUE_DATA_CONT; 3179 } 3180 3181 static void queue_data_cont(struct ceph_connection *con, struct ceph_msg *msg) 3182 { 3183 struct bio_vec bv; 3184 3185 con->v2.out_epil.data_crc = ceph_crc32c_page( 3186 con->v2.out_epil.data_crc, con->v2.out_bvec.bv_page, 3187 con->v2.out_bvec.bv_offset, con->v2.out_bvec.bv_len); 3188 3189 ceph_msg_data_advance(&con->v2.out_cursor, con->v2.out_bvec.bv_len); 3190 if (con->v2.out_cursor.total_resid) { 3191 get_bvec_at(&con->v2.out_cursor, &bv); 3192 set_out_bvec(con, &bv, true); 3193 WARN_ON(con->v2.out_state != OUT_S_QUEUE_DATA_CONT); 3194 return; 3195 } 3196 3197 /* 3198 * We've written all data. Queue epilogue. Once it's written, 3199 * we are done. 3200 */ 3201 reset_out_kvecs(con); 3202 prepare_epilogue_plain(con, msg, false); 3203 con->v2.out_state = OUT_S_FINISH_MESSAGE; 3204 } 3205 3206 static void queue_enc_page(struct ceph_connection *con) 3207 { 3208 struct bio_vec bv; 3209 3210 dout("%s con %p i %d resid %d\n", __func__, con, con->v2.out_enc_i, 3211 con->v2.out_enc_resid); 3212 WARN_ON(!con->v2.out_enc_resid); 3213 3214 bvec_set_page(&bv, con->v2.out_enc_pages[con->v2.out_enc_i], 3215 min(con->v2.out_enc_resid, (int)PAGE_SIZE), 0); 3216 3217 set_out_bvec(con, &bv, false); 3218 con->v2.out_enc_i++; 3219 con->v2.out_enc_resid -= bv.bv_len; 3220 3221 if (con->v2.out_enc_resid) { 3222 WARN_ON(con->v2.out_state != OUT_S_QUEUE_ENC_PAGE); 3223 return; 3224 } 3225 3226 /* 3227 * We've queued the last piece of ciphertext (ending with 3228 * epilogue) + auth tag. Once it's written, we are done. 3229 */ 3230 WARN_ON(con->v2.out_enc_i != con->v2.out_enc_page_cnt); 3231 con->v2.out_state = OUT_S_FINISH_MESSAGE; 3232 } 3233 3234 static void queue_zeros(struct ceph_connection *con, struct ceph_msg *msg) 3235 { 3236 dout("%s con %p out_zero %d\n", __func__, con, con->v2.out_zero); 3237 3238 if (con->v2.out_zero) { 3239 set_out_bvec_zero(con); 3240 con->v2.out_zero -= con->v2.out_bvec.bv_len; 3241 con->v2.out_state = OUT_S_QUEUE_ZEROS; 3242 return; 3243 } 3244 3245 /* 3246 * We've zero-filled everything up to epilogue. Queue epilogue 3247 * with late_status set to ABORTED and crcs adjusted for zeros. 3248 * Once it's written, we are done patching up for the revoke. 3249 */ 3250 reset_out_kvecs(con); 3251 prepare_epilogue_plain(con, msg, true); 3252 con->v2.out_state = OUT_S_FINISH_MESSAGE; 3253 } 3254 3255 static void finish_message(struct ceph_connection *con) 3256 { 3257 dout("%s con %p msg %p\n", __func__, con, con->out_msg); 3258 3259 /* we end up here both plain and secure modes */ 3260 if (con->v2.out_enc_pages) { 3261 WARN_ON(!con->v2.out_enc_page_cnt); 3262 ceph_release_page_vector(con->v2.out_enc_pages, 3263 con->v2.out_enc_page_cnt); 3264 con->v2.out_enc_pages = NULL; 3265 con->v2.out_enc_page_cnt = 0; 3266 } 3267 /* message may have been revoked */ 3268 if (con->out_msg) { 3269 ceph_msg_put(con->out_msg); 3270 con->out_msg = NULL; 3271 } 3272 3273 con->v2.out_state = OUT_S_GET_NEXT; 3274 } 3275 3276 static int populate_out_iter(struct ceph_connection *con) 3277 { 3278 struct ceph_msg *msg; 3279 int ret; 3280 3281 dout("%s con %p state %d out_state %d\n", __func__, con, con->state, 3282 con->v2.out_state); 3283 WARN_ON(iov_iter_count(&con->v2.out_iter)); 3284 3285 if (con->state != CEPH_CON_S_OPEN) { 3286 WARN_ON(con->state < CEPH_CON_S_V2_BANNER_PREFIX || 3287 con->state > CEPH_CON_S_V2_SESSION_RECONNECT); 3288 goto nothing_pending; 3289 } 3290 3291 switch (con->v2.out_state) { 3292 case OUT_S_QUEUE_DATA: 3293 WARN_ON(!con->out_msg); 3294 queue_data(con, con->out_msg); 3295 goto populated; 3296 case OUT_S_QUEUE_DATA_CONT: 3297 WARN_ON(!con->out_msg); 3298 queue_data_cont(con, con->out_msg); 3299 goto populated; 3300 case OUT_S_QUEUE_ENC_PAGE: 3301 queue_enc_page(con); 3302 goto populated; 3303 case OUT_S_QUEUE_ZEROS: 3304 WARN_ON(con->out_msg); /* revoked */ 3305 queue_zeros(con, con->out_msg); 3306 goto populated; 3307 case OUT_S_FINISH_MESSAGE: 3308 finish_message(con); 3309 break; 3310 case OUT_S_GET_NEXT: 3311 break; 3312 default: 3313 WARN(1, "bad out_state %d", con->v2.out_state); 3314 return -EINVAL; 3315 } 3316 3317 WARN_ON(con->v2.out_state != OUT_S_GET_NEXT); 3318 if (ceph_con_flag_test_and_clear(con, CEPH_CON_F_KEEPALIVE_PENDING)) { 3319 ret = prepare_keepalive2(con); 3320 if (ret) { 3321 pr_err("prepare_keepalive2 failed: %d\n", ret); 3322 return ret; 3323 } 3324 } else if ((msg = ceph_con_get_out_msg(con)) != NULL) { 3325 ret = prepare_message(con, msg); 3326 if (ret) { 3327 pr_err("prepare_message failed: %d\n", ret); 3328 return ret; 3329 } 3330 } else if (con->in_seq > con->in_seq_acked) { 3331 ret = prepare_ack(con); 3332 if (ret) { 3333 pr_err("prepare_ack failed: %d\n", ret); 3334 return ret; 3335 } 3336 } else { 3337 goto nothing_pending; 3338 } 3339 3340 populated: 3341 if (WARN_ON(!iov_iter_count(&con->v2.out_iter))) 3342 return -ENODATA; 3343 dout("%s con %p populated %zu\n", __func__, con, 3344 iov_iter_count(&con->v2.out_iter)); 3345 return 1; 3346 3347 nothing_pending: 3348 WARN_ON(iov_iter_count(&con->v2.out_iter)); 3349 dout("%s con %p nothing pending\n", __func__, con); 3350 ceph_con_flag_clear(con, CEPH_CON_F_WRITE_PENDING); 3351 return 0; 3352 } 3353 3354 int ceph_con_v2_try_write(struct ceph_connection *con) 3355 { 3356 int ret; 3357 3358 dout("%s con %p state %d have %zu\n", __func__, con, con->state, 3359 iov_iter_count(&con->v2.out_iter)); 3360 3361 /* open the socket first? */ 3362 if (con->state == CEPH_CON_S_PREOPEN) { 3363 WARN_ON(con->peer_addr.type != CEPH_ENTITY_ADDR_TYPE_MSGR2); 3364 3365 /* 3366 * Always bump global_seq. Bump connect_seq only if 3367 * there is a session (i.e. we are reconnecting and will 3368 * send session_reconnect instead of client_ident). 3369 */ 3370 con->v2.global_seq = ceph_get_global_seq(con->msgr, 0); 3371 if (con->v2.server_cookie) 3372 con->v2.connect_seq++; 3373 3374 ret = prepare_read_banner_prefix(con); 3375 if (ret) { 3376 pr_err("prepare_read_banner_prefix failed: %d\n", ret); 3377 con->error_msg = "connect error"; 3378 return ret; 3379 } 3380 3381 reset_out_kvecs(con); 3382 ret = prepare_banner(con); 3383 if (ret) { 3384 pr_err("prepare_banner failed: %d\n", ret); 3385 con->error_msg = "connect error"; 3386 return ret; 3387 } 3388 3389 ret = ceph_tcp_connect(con); 3390 if (ret) { 3391 pr_err("ceph_tcp_connect failed: %d\n", ret); 3392 con->error_msg = "connect error"; 3393 return ret; 3394 } 3395 } 3396 3397 if (!iov_iter_count(&con->v2.out_iter)) { 3398 ret = populate_out_iter(con); 3399 if (ret <= 0) { 3400 if (ret && ret != -EAGAIN && !con->error_msg) 3401 con->error_msg = "write processing error"; 3402 return ret; 3403 } 3404 } 3405 3406 tcp_sock_set_cork(con->sock->sk, true); 3407 for (;;) { 3408 ret = ceph_tcp_send(con); 3409 if (ret <= 0) 3410 break; 3411 3412 ret = populate_out_iter(con); 3413 if (ret <= 0) { 3414 if (ret && ret != -EAGAIN && !con->error_msg) 3415 con->error_msg = "write processing error"; 3416 break; 3417 } 3418 } 3419 3420 tcp_sock_set_cork(con->sock->sk, false); 3421 return ret; 3422 } 3423 3424 static u32 crc32c_zeros(u32 crc, int zero_len) 3425 { 3426 int len; 3427 3428 while (zero_len) { 3429 len = min(zero_len, (int)PAGE_SIZE); 3430 crc = crc32c(crc, page_address(ceph_zero_page), len); 3431 zero_len -= len; 3432 } 3433 3434 return crc; 3435 } 3436 3437 static void prepare_zero_front(struct ceph_connection *con, 3438 struct ceph_msg *msg, int resid) 3439 { 3440 int sent; 3441 3442 WARN_ON(!resid || resid > front_len(msg)); 3443 sent = front_len(msg) - resid; 3444 dout("%s con %p sent %d resid %d\n", __func__, con, sent, resid); 3445 3446 if (sent) { 3447 con->v2.out_epil.front_crc = 3448 crc32c(-1, msg->front.iov_base, sent); 3449 con->v2.out_epil.front_crc = 3450 crc32c_zeros(con->v2.out_epil.front_crc, resid); 3451 } else { 3452 con->v2.out_epil.front_crc = crc32c_zeros(-1, resid); 3453 } 3454 3455 con->v2.out_iter.count -= resid; 3456 out_zero_add(con, resid); 3457 } 3458 3459 static void prepare_zero_middle(struct ceph_connection *con, 3460 struct ceph_msg *msg, int resid) 3461 { 3462 int sent; 3463 3464 WARN_ON(!resid || resid > middle_len(msg)); 3465 sent = middle_len(msg) - resid; 3466 dout("%s con %p sent %d resid %d\n", __func__, con, sent, resid); 3467 3468 if (sent) { 3469 con->v2.out_epil.middle_crc = 3470 crc32c(-1, msg->middle->vec.iov_base, sent); 3471 con->v2.out_epil.middle_crc = 3472 crc32c_zeros(con->v2.out_epil.middle_crc, resid); 3473 } else { 3474 con->v2.out_epil.middle_crc = crc32c_zeros(-1, resid); 3475 } 3476 3477 con->v2.out_iter.count -= resid; 3478 out_zero_add(con, resid); 3479 } 3480 3481 static void prepare_zero_data(struct ceph_connection *con, 3482 struct ceph_msg *msg) 3483 { 3484 dout("%s con %p\n", __func__, con); 3485 con->v2.out_epil.data_crc = crc32c_zeros(-1, data_len(msg)); 3486 out_zero_add(con, data_len(msg)); 3487 } 3488 3489 static void revoke_at_queue_data(struct ceph_connection *con, 3490 struct ceph_msg *msg) 3491 { 3492 int boundary; 3493 int resid; 3494 3495 WARN_ON(!data_len(msg)); 3496 WARN_ON(!iov_iter_is_kvec(&con->v2.out_iter)); 3497 resid = iov_iter_count(&con->v2.out_iter); 3498 3499 boundary = front_len(msg) + middle_len(msg); 3500 if (resid > boundary) { 3501 resid -= boundary; 3502 WARN_ON(resid > MESSAGE_HEAD_PLAIN_LEN); 3503 dout("%s con %p was sending head\n", __func__, con); 3504 if (front_len(msg)) 3505 prepare_zero_front(con, msg, front_len(msg)); 3506 if (middle_len(msg)) 3507 prepare_zero_middle(con, msg, middle_len(msg)); 3508 prepare_zero_data(con, msg); 3509 WARN_ON(iov_iter_count(&con->v2.out_iter) != resid); 3510 con->v2.out_state = OUT_S_QUEUE_ZEROS; 3511 return; 3512 } 3513 3514 boundary = middle_len(msg); 3515 if (resid > boundary) { 3516 resid -= boundary; 3517 dout("%s con %p was sending front\n", __func__, con); 3518 prepare_zero_front(con, msg, resid); 3519 if (middle_len(msg)) 3520 prepare_zero_middle(con, msg, middle_len(msg)); 3521 prepare_zero_data(con, msg); 3522 queue_zeros(con, msg); 3523 return; 3524 } 3525 3526 WARN_ON(!resid); 3527 dout("%s con %p was sending middle\n", __func__, con); 3528 prepare_zero_middle(con, msg, resid); 3529 prepare_zero_data(con, msg); 3530 queue_zeros(con, msg); 3531 } 3532 3533 static void revoke_at_queue_data_cont(struct ceph_connection *con, 3534 struct ceph_msg *msg) 3535 { 3536 int sent, resid; /* current piece of data */ 3537 3538 WARN_ON(!data_len(msg)); 3539 WARN_ON(!iov_iter_is_bvec(&con->v2.out_iter)); 3540 resid = iov_iter_count(&con->v2.out_iter); 3541 WARN_ON(!resid || resid > con->v2.out_bvec.bv_len); 3542 sent = con->v2.out_bvec.bv_len - resid; 3543 dout("%s con %p sent %d resid %d\n", __func__, con, sent, resid); 3544 3545 if (sent) { 3546 con->v2.out_epil.data_crc = ceph_crc32c_page( 3547 con->v2.out_epil.data_crc, con->v2.out_bvec.bv_page, 3548 con->v2.out_bvec.bv_offset, sent); 3549 ceph_msg_data_advance(&con->v2.out_cursor, sent); 3550 } 3551 WARN_ON(resid > con->v2.out_cursor.total_resid); 3552 con->v2.out_epil.data_crc = crc32c_zeros(con->v2.out_epil.data_crc, 3553 con->v2.out_cursor.total_resid); 3554 3555 con->v2.out_iter.count -= resid; 3556 out_zero_add(con, con->v2.out_cursor.total_resid); 3557 queue_zeros(con, msg); 3558 } 3559 3560 static void revoke_at_finish_message(struct ceph_connection *con, 3561 struct ceph_msg *msg) 3562 { 3563 int boundary; 3564 int resid; 3565 3566 WARN_ON(!iov_iter_is_kvec(&con->v2.out_iter)); 3567 resid = iov_iter_count(&con->v2.out_iter); 3568 3569 if (!front_len(msg) && !middle_len(msg) && 3570 !data_len(msg)) { 3571 WARN_ON(!resid || resid > MESSAGE_HEAD_PLAIN_LEN); 3572 dout("%s con %p was sending head (empty message) - noop\n", 3573 __func__, con); 3574 return; 3575 } 3576 3577 boundary = front_len(msg) + middle_len(msg) + 3578 CEPH_EPILOGUE_PLAIN_LEN; 3579 if (resid > boundary) { 3580 resid -= boundary; 3581 WARN_ON(resid > MESSAGE_HEAD_PLAIN_LEN); 3582 dout("%s con %p was sending head\n", __func__, con); 3583 if (front_len(msg)) 3584 prepare_zero_front(con, msg, front_len(msg)); 3585 if (middle_len(msg)) 3586 prepare_zero_middle(con, msg, middle_len(msg)); 3587 con->v2.out_iter.count -= CEPH_EPILOGUE_PLAIN_LEN; 3588 WARN_ON(iov_iter_count(&con->v2.out_iter) != resid); 3589 con->v2.out_state = OUT_S_QUEUE_ZEROS; 3590 return; 3591 } 3592 3593 boundary = middle_len(msg) + CEPH_EPILOGUE_PLAIN_LEN; 3594 if (resid > boundary) { 3595 resid -= boundary; 3596 dout("%s con %p was sending front\n", __func__, con); 3597 prepare_zero_front(con, msg, resid); 3598 if (middle_len(msg)) 3599 prepare_zero_middle(con, msg, middle_len(msg)); 3600 con->v2.out_iter.count -= CEPH_EPILOGUE_PLAIN_LEN; 3601 queue_zeros(con, msg); 3602 return; 3603 } 3604 3605 boundary = CEPH_EPILOGUE_PLAIN_LEN; 3606 if (resid > boundary) { 3607 resid -= boundary; 3608 dout("%s con %p was sending middle\n", __func__, con); 3609 prepare_zero_middle(con, msg, resid); 3610 con->v2.out_iter.count -= CEPH_EPILOGUE_PLAIN_LEN; 3611 queue_zeros(con, msg); 3612 return; 3613 } 3614 3615 WARN_ON(!resid); 3616 dout("%s con %p was sending epilogue - noop\n", __func__, con); 3617 } 3618 3619 void ceph_con_v2_revoke(struct ceph_connection *con, struct ceph_msg *msg) 3620 { 3621 WARN_ON(con->v2.out_zero); 3622 3623 if (con_secure(con)) { 3624 WARN_ON(con->v2.out_state != OUT_S_QUEUE_ENC_PAGE && 3625 con->v2.out_state != OUT_S_FINISH_MESSAGE); 3626 dout("%s con %p secure - noop\n", __func__, con); 3627 return; 3628 } 3629 3630 switch (con->v2.out_state) { 3631 case OUT_S_QUEUE_DATA: 3632 revoke_at_queue_data(con, msg); 3633 break; 3634 case OUT_S_QUEUE_DATA_CONT: 3635 revoke_at_queue_data_cont(con, msg); 3636 break; 3637 case OUT_S_FINISH_MESSAGE: 3638 revoke_at_finish_message(con, msg); 3639 break; 3640 default: 3641 WARN(1, "bad out_state %d", con->v2.out_state); 3642 break; 3643 } 3644 } 3645 3646 static void revoke_at_prepare_read_data(struct ceph_connection *con) 3647 { 3648 int remaining; 3649 int resid; 3650 3651 WARN_ON(con_secure(con)); 3652 WARN_ON(!data_len(con->in_msg)); 3653 WARN_ON(!iov_iter_is_kvec(&con->v2.in_iter)); 3654 resid = iov_iter_count(&con->v2.in_iter); 3655 WARN_ON(!resid); 3656 3657 remaining = data_len(con->in_msg) + CEPH_EPILOGUE_PLAIN_LEN; 3658 dout("%s con %p resid %d remaining %d\n", __func__, con, resid, 3659 remaining); 3660 con->v2.in_iter.count -= resid; 3661 set_in_skip(con, resid + remaining); 3662 con->v2.in_state = IN_S_FINISH_SKIP; 3663 } 3664 3665 static void revoke_at_prepare_read_data_cont(struct ceph_connection *con) 3666 { 3667 int recved, resid; /* current piece of data */ 3668 int remaining; 3669 3670 WARN_ON(con_secure(con)); 3671 WARN_ON(!data_len(con->in_msg)); 3672 WARN_ON(!iov_iter_is_bvec(&con->v2.in_iter)); 3673 resid = iov_iter_count(&con->v2.in_iter); 3674 WARN_ON(!resid || resid > con->v2.in_bvec.bv_len); 3675 recved = con->v2.in_bvec.bv_len - resid; 3676 dout("%s con %p recved %d resid %d\n", __func__, con, recved, resid); 3677 3678 if (recved) 3679 ceph_msg_data_advance(&con->v2.in_cursor, recved); 3680 WARN_ON(resid > con->v2.in_cursor.total_resid); 3681 3682 remaining = CEPH_EPILOGUE_PLAIN_LEN; 3683 dout("%s con %p total_resid %zu remaining %d\n", __func__, con, 3684 con->v2.in_cursor.total_resid, remaining); 3685 con->v2.in_iter.count -= resid; 3686 set_in_skip(con, con->v2.in_cursor.total_resid + remaining); 3687 con->v2.in_state = IN_S_FINISH_SKIP; 3688 } 3689 3690 static void revoke_at_prepare_read_enc_page(struct ceph_connection *con) 3691 { 3692 int resid; /* current enc page (not necessarily data) */ 3693 3694 WARN_ON(!con_secure(con)); 3695 WARN_ON(!iov_iter_is_bvec(&con->v2.in_iter)); 3696 resid = iov_iter_count(&con->v2.in_iter); 3697 WARN_ON(!resid || resid > con->v2.in_bvec.bv_len); 3698 3699 dout("%s con %p resid %d enc_resid %d\n", __func__, con, resid, 3700 con->v2.in_enc_resid); 3701 con->v2.in_iter.count -= resid; 3702 set_in_skip(con, resid + con->v2.in_enc_resid); 3703 con->v2.in_state = IN_S_FINISH_SKIP; 3704 } 3705 3706 static void revoke_at_prepare_sparse_data(struct ceph_connection *con) 3707 { 3708 int resid; /* current piece of data */ 3709 int remaining; 3710 3711 WARN_ON(con_secure(con)); 3712 WARN_ON(!data_len(con->in_msg)); 3713 WARN_ON(!iov_iter_is_bvec(&con->v2.in_iter)); 3714 resid = iov_iter_count(&con->v2.in_iter); 3715 dout("%s con %p resid %d\n", __func__, con, resid); 3716 3717 remaining = CEPH_EPILOGUE_PLAIN_LEN + con->v2.data_len_remain; 3718 con->v2.in_iter.count -= resid; 3719 set_in_skip(con, resid + remaining); 3720 con->v2.in_state = IN_S_FINISH_SKIP; 3721 } 3722 3723 static void revoke_at_handle_epilogue(struct ceph_connection *con) 3724 { 3725 int resid; 3726 3727 resid = iov_iter_count(&con->v2.in_iter); 3728 WARN_ON(!resid); 3729 3730 dout("%s con %p resid %d\n", __func__, con, resid); 3731 con->v2.in_iter.count -= resid; 3732 set_in_skip(con, resid); 3733 con->v2.in_state = IN_S_FINISH_SKIP; 3734 } 3735 3736 void ceph_con_v2_revoke_incoming(struct ceph_connection *con) 3737 { 3738 switch (con->v2.in_state) { 3739 case IN_S_PREPARE_SPARSE_DATA: 3740 case IN_S_PREPARE_READ_DATA: 3741 revoke_at_prepare_read_data(con); 3742 break; 3743 case IN_S_PREPARE_READ_DATA_CONT: 3744 revoke_at_prepare_read_data_cont(con); 3745 break; 3746 case IN_S_PREPARE_READ_ENC_PAGE: 3747 revoke_at_prepare_read_enc_page(con); 3748 break; 3749 case IN_S_PREPARE_SPARSE_DATA_CONT: 3750 revoke_at_prepare_sparse_data(con); 3751 break; 3752 case IN_S_HANDLE_EPILOGUE: 3753 revoke_at_handle_epilogue(con); 3754 break; 3755 default: 3756 WARN(1, "bad in_state %d", con->v2.in_state); 3757 break; 3758 } 3759 } 3760 3761 bool ceph_con_v2_opened(struct ceph_connection *con) 3762 { 3763 return con->v2.peer_global_seq; 3764 } 3765 3766 void ceph_con_v2_reset_session(struct ceph_connection *con) 3767 { 3768 con->v2.client_cookie = 0; 3769 con->v2.server_cookie = 0; 3770 con->v2.global_seq = 0; 3771 con->v2.connect_seq = 0; 3772 con->v2.peer_global_seq = 0; 3773 } 3774 3775 void ceph_con_v2_reset_protocol(struct ceph_connection *con) 3776 { 3777 iov_iter_truncate(&con->v2.in_iter, 0); 3778 iov_iter_truncate(&con->v2.out_iter, 0); 3779 con->v2.out_zero = 0; 3780 3781 clear_in_sign_kvecs(con); 3782 clear_out_sign_kvecs(con); 3783 free_conn_bufs(con); 3784 3785 if (con->v2.in_enc_pages) { 3786 WARN_ON(!con->v2.in_enc_page_cnt); 3787 ceph_release_page_vector(con->v2.in_enc_pages, 3788 con->v2.in_enc_page_cnt); 3789 con->v2.in_enc_pages = NULL; 3790 con->v2.in_enc_page_cnt = 0; 3791 } 3792 if (con->v2.out_enc_pages) { 3793 WARN_ON(!con->v2.out_enc_page_cnt); 3794 ceph_release_page_vector(con->v2.out_enc_pages, 3795 con->v2.out_enc_page_cnt); 3796 con->v2.out_enc_pages = NULL; 3797 con->v2.out_enc_page_cnt = 0; 3798 } 3799 3800 con->v2.con_mode = CEPH_CON_MODE_UNKNOWN; 3801 memzero_explicit(&con->v2.in_gcm_nonce, CEPH_GCM_IV_LEN); 3802 memzero_explicit(&con->v2.out_gcm_nonce, CEPH_GCM_IV_LEN); 3803 3804 memzero_explicit(&con->v2.hmac_key, sizeof(con->v2.hmac_key)); 3805 con->v2.hmac_key_set = false; 3806 if (con->v2.gcm_req) { 3807 aead_request_free(con->v2.gcm_req); 3808 con->v2.gcm_req = NULL; 3809 } 3810 if (con->v2.gcm_tfm) { 3811 crypto_free_aead(con->v2.gcm_tfm); 3812 con->v2.gcm_tfm = NULL; 3813 } 3814 } 3815