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