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