1 /*
2  * Copyright (c) 2020 Demant
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 
9 #include <zephyr/bluetooth/hci_types.h>
10 #include <zephyr/sys/byteorder.h>
11 #include <zephyr/sys/slist.h>
12 #include <zephyr/sys/util.h>
13 
14 #include "hal/ecb.h"
15 #include "hal/ccm.h"
16 
17 #include "util/util.h"
18 #include "util/mem.h"
19 #include "util/memq.h"
20 #include "util/dbuf.h"
21 
22 #include "pdu_df.h"
23 #include "lll/pdu_vendor.h"
24 #include "pdu.h"
25 
26 #include "ll.h"
27 #include "ll_feat.h"
28 #include "ll_settings.h"
29 
30 #include "lll.h"
31 #include "lll/lll_df_types.h"
32 #include "lll_conn.h"
33 #include "lll_conn_iso.h"
34 
35 #include "ull_tx_queue.h"
36 
37 #include "isoal.h"
38 #include "ull_iso_types.h"
39 #include "ull_conn_iso_types.h"
40 #include "ull_conn_iso_internal.h"
41 
42 #include "ull_conn_types.h"
43 #include "ull_internal.h"
44 #include "ull_llcp.h"
45 #include "ull_llcp_internal.h"
46 #include "ull_llcp_features.h"
47 #include "ull_conn_internal.h"
48 
49 #include <soc.h>
50 #include "hal/debug.h"
51 
52 #if defined(CONFIG_BT_CENTRAL)
53 /* LLCP Local Procedure Encryption FSM states */
54 enum {
55 	/* Start Procedure */
56 	LP_ENC_STATE_UNENCRYPTED,
57 	LP_ENC_STATE_WAIT_TX_ENC_REQ,
58 	LP_ENC_STATE_WAIT_RX_ENC_RSP,
59 	LP_ENC_STATE_WAIT_RX_START_ENC_REQ,
60 	LP_ENC_STATE_WAIT_TX_START_ENC_RSP,
61 	LP_ENC_STATE_WAIT_RX_START_ENC_RSP,
62 	/* Pause Procedure */
63 	LP_ENC_STATE_ENCRYPTED,
64 	LP_ENC_STATE_WAIT_TX_PAUSE_ENC_REQ,
65 	LP_ENC_STATE_WAIT_RX_PAUSE_ENC_RSP,
66 	LP_ENC_STATE_WAIT_TX_PAUSE_ENC_RSP,
67 };
68 
69 /* LLCP Local Procedure Encryption FSM events */
70 enum {
71 	/* Procedure prepared */
72 	LP_ENC_EVT_RUN,
73 
74 	/* Response received */
75 	LP_ENC_EVT_ENC_RSP,
76 
77 	/* Request received */
78 	LP_ENC_EVT_START_ENC_REQ,
79 
80 	/* Response received */
81 	LP_ENC_EVT_START_ENC_RSP,
82 
83 	/* Reject response received */
84 	LP_ENC_EVT_REJECT,
85 
86 	/* Unknown response received */
87 	LP_ENC_EVT_UNKNOWN,
88 
89 	/* Response received */
90 	LP_ENC_EVT_PAUSE_ENC_RSP,
91 };
92 #endif /* CONFIG_BT_CENTRAL */
93 
94 #if defined(CONFIG_BT_PERIPHERAL)
95 /* LLCP Remote Procedure Encryption FSM states */
96 enum {
97 	/* Start Procedure */
98 	RP_ENC_STATE_UNENCRYPTED,
99 	RP_ENC_STATE_WAIT_RX_ENC_REQ,
100 	RP_ENC_STATE_WAIT_TX_ENC_RSP,
101 	RP_ENC_STATE_WAIT_LTK_REPLY,
102 	RP_ENC_STATE_WAIT_TX_START_ENC_REQ,
103 	RP_ENC_STATE_WAIT_TX_REJECT_IND,
104 	RP_ENC_STATE_WAIT_RX_START_ENC_RSP,
105 	RP_ENC_STATE_WAIT_TX_START_ENC_RSP,
106 	/* Pause Procedure */
107 	RP_ENC_STATE_ENCRYPTED,
108 	RP_ENC_STATE_WAIT_RX_PAUSE_ENC_REQ,
109 	RP_ENC_STATE_WAIT_TX_PAUSE_ENC_RSP,
110 	RP_ENC_STATE_WAIT_RX_PAUSE_ENC_RSP,
111 };
112 
113 /* LLCP Remote Procedure Encryption FSM events */
114 enum {
115 	/* Procedure prepared */
116 	RP_ENC_EVT_RUN,
117 
118 	/* Request received */
119 	RP_ENC_EVT_ENC_REQ,
120 
121 	/* Response received */
122 	RP_ENC_EVT_START_ENC_RSP,
123 
124 	/* LTK request reply */
125 	RP_ENC_EVT_LTK_REQ_REPLY,
126 
127 	/* LTK request negative reply */
128 	RP_ENC_EVT_LTK_REQ_NEG_REPLY,
129 
130 	/* Reject response received */
131 	RP_ENC_EVT_REJECT,
132 
133 	/* Unknown response received */
134 	RP_ENC_EVT_UNKNOWN,
135 
136 	/* Request received */
137 	RP_ENC_EVT_PAUSE_ENC_REQ,
138 
139 	/* Response received */
140 	RP_ENC_EVT_PAUSE_ENC_RSP,
141 };
142 #endif /* CONFIG_BT_PERIPHERAL */
143 
144 
enc_setup_lll(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t role)145 static void enc_setup_lll(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t role)
146 {
147 	/* TODO(thoh): Move LLL/CCM manipulation to ULL? */
148 
149 	/* Calculate the Session Key */
150 	ecb_encrypt(&ctx->data.enc.ltk[0], &ctx->data.enc.skd[0], NULL, &conn->lll.ccm_rx.key[0]);
151 
152 	/* Copy the Session Key */
153 	memcpy(&conn->lll.ccm_tx.key[0], &conn->lll.ccm_rx.key[0], sizeof(conn->lll.ccm_tx.key));
154 
155 	/* Copy the IV */
156 	memcpy(&conn->lll.ccm_tx.iv[0], &conn->lll.ccm_rx.iv[0], sizeof(conn->lll.ccm_tx.iv));
157 
158 	/* Reset CCM counter */
159 	conn->lll.ccm_tx.counter = 0U;
160 	conn->lll.ccm_rx.counter = 0U;
161 
162 	/* Set CCM direction:
163 	 *	periph to central = 0,
164 	 *	central to periph = 1
165 	 */
166 	if (role == BT_HCI_ROLE_PERIPHERAL) {
167 		conn->lll.ccm_tx.direction = 0U;
168 		conn->lll.ccm_rx.direction = 1U;
169 	} else {
170 		conn->lll.ccm_tx.direction = 1U;
171 		conn->lll.ccm_rx.direction = 0U;
172 	}
173 }
174 
175 #if defined(CONFIG_BT_CENTRAL)
176 /*
177  * LLCP Local Procedure Encryption FSM
178  */
179 
llcp_lp_enc_tx(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t opcode)180 static struct node_tx *llcp_lp_enc_tx(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t opcode)
181 {
182 	struct node_tx *tx;
183 	struct pdu_data *pdu;
184 
185 	/* Allocate tx node */
186 	tx = llcp_tx_alloc(conn, ctx);
187 	LL_ASSERT(tx);
188 
189 	pdu = (struct pdu_data *)tx->pdu;
190 
191 	/* Encode LL Control PDU */
192 	switch (opcode) {
193 	case PDU_DATA_LLCTRL_TYPE_ENC_REQ:
194 		llcp_pdu_encode_enc_req(ctx, pdu);
195 		break;
196 	case PDU_DATA_LLCTRL_TYPE_START_ENC_RSP:
197 		llcp_pdu_encode_start_enc_rsp(pdu);
198 		break;
199 	case PDU_DATA_LLCTRL_TYPE_PAUSE_ENC_REQ:
200 		llcp_pdu_encode_pause_enc_req(pdu);
201 		break;
202 	case PDU_DATA_LLCTRL_TYPE_PAUSE_ENC_RSP:
203 		llcp_pdu_encode_pause_enc_rsp(pdu);
204 		break;
205 	default:
206 		LL_ASSERT(0);
207 	}
208 
209 	ctx->tx_opcode = pdu->llctrl.opcode;
210 
211 	/* Enqueue LL Control PDU towards LLL */
212 	llcp_tx_enqueue(conn, tx);
213 
214 	/* Restart procedure response timeout timer */
215 	llcp_lr_prt_restart(conn);
216 
217 	return tx;
218 }
219 
lp_enc_ntf(struct ll_conn * conn,struct proc_ctx * ctx)220 static void lp_enc_ntf(struct ll_conn *conn, struct proc_ctx *ctx)
221 {
222 	struct node_rx_pdu *ntf;
223 	struct pdu_data *pdu;
224 
225 	/* Piggy-back on RX node */
226 	ntf = ctx->node_ref.rx;
227 	LL_ASSERT(ntf);
228 
229 	ntf->hdr.type = NODE_RX_TYPE_DC_PDU;
230 	ntf->hdr.handle = conn->lll.handle;
231 	pdu = (struct pdu_data *)ntf->pdu;
232 
233 	if (ctx->data.enc.error == BT_HCI_ERR_SUCCESS) {
234 		if (ctx->proc == PROC_ENCRYPTION_START) {
235 			/* Encryption Change Event */
236 			/* TODO(thoh): is this correct? */
237 			llcp_pdu_encode_start_enc_rsp(pdu);
238 		} else if (ctx->proc == PROC_ENCRYPTION_PAUSE) {
239 			/* Encryption Key Refresh Complete Event */
240 			ntf->hdr.type = NODE_RX_TYPE_ENC_REFRESH;
241 		} else {
242 			/* Should never happen */
243 			LL_ASSERT(0);
244 		}
245 	} else {
246 		llcp_pdu_encode_reject_ind(pdu, ctx->data.enc.error);
247 	}
248 }
249 
lp_enc_complete(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)250 static void lp_enc_complete(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
251 {
252 	lp_enc_ntf(conn, ctx);
253 	llcp_lr_complete(conn);
254 	ctx->state = LP_ENC_STATE_UNENCRYPTED;
255 }
256 
lp_enc_store_m(struct ll_conn * conn,struct proc_ctx * ctx,struct pdu_data * pdu)257 static void lp_enc_store_m(struct ll_conn *conn, struct proc_ctx *ctx, struct pdu_data *pdu)
258 {
259 	/* Store SKDm */
260 	memcpy(&ctx->data.enc.skd[0], pdu->llctrl.enc_req.skdm, sizeof(pdu->llctrl.enc_req.skdm));
261 	/* Store IVm in the LLL CCM RX
262 	 * TODO(thoh): Should this be made into a ULL function, as it
263 	 * interacts with data outside of LLCP?
264 	 */
265 	memcpy(&conn->lll.ccm_rx.iv[0], pdu->llctrl.enc_req.ivm, sizeof(pdu->llctrl.enc_req.ivm));
266 }
267 
lp_enc_send_enc_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)268 static void lp_enc_send_enc_req(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
269 				void *param)
270 {
271 	struct node_tx *tx;
272 
273 	if (!llcp_tx_alloc_peek(conn, ctx)) {
274 		ctx->state = LP_ENC_STATE_WAIT_TX_ENC_REQ;
275 	} else {
276 		tx = llcp_lp_enc_tx(conn, ctx, PDU_DATA_LLCTRL_TYPE_ENC_REQ);
277 		lp_enc_store_m(conn, ctx, (struct pdu_data *)tx->pdu);
278 		/* Wait for the LL_ENC_RSP */
279 		ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_ENC_RSP;
280 		ctx->state = LP_ENC_STATE_WAIT_RX_ENC_RSP;
281 
282 		/* Pause possibly ongoing remote procedure */
283 		llcp_rr_pause(conn);
284 
285 	}
286 }
287 
lp_enc_send_pause_enc_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)288 static void lp_enc_send_pause_enc_req(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
289 				      void *param)
290 {
291 	if (!llcp_tx_alloc_peek(conn, ctx)) {
292 		ctx->state = LP_ENC_STATE_WAIT_TX_PAUSE_ENC_REQ;
293 	} else {
294 		llcp_lp_enc_tx(conn, ctx, PDU_DATA_LLCTRL_TYPE_PAUSE_ENC_REQ);
295 		/* Wait for the LL_PAUSE_ENC_RSP */
296 		ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_PAUSE_ENC_RSP;
297 		ctx->state = LP_ENC_STATE_WAIT_RX_PAUSE_ENC_RSP;
298 	}
299 }
300 
lp_enc_send_pause_enc_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)301 static void lp_enc_send_pause_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
302 				      void *param)
303 {
304 	if (!llcp_tx_alloc_peek(conn, ctx)) {
305 		ctx->state = LP_ENC_STATE_WAIT_TX_PAUSE_ENC_RSP;
306 	} else {
307 		llcp_lp_enc_tx(conn, ctx, PDU_DATA_LLCTRL_TYPE_PAUSE_ENC_RSP);
308 
309 		ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_UNUSED;
310 		/* Continue with an encapsulated Start Procedure */
311 		ctx->state = LP_ENC_STATE_UNENCRYPTED;
312 
313 		/* Tx Encryption disabled */
314 		conn->lll.enc_tx = 0U;
315 
316 		/* Rx Decryption disabled */
317 		conn->lll.enc_rx = 0U;
318 	}
319 }
320 
lp_enc_send_start_enc_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)321 static void lp_enc_send_start_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
322 				      void *param)
323 {
324 	if (!llcp_tx_alloc_peek(conn, ctx)) {
325 		ctx->state = LP_ENC_STATE_WAIT_TX_START_ENC_RSP;
326 	} else {
327 		enc_setup_lll(conn, ctx, BT_HCI_ROLE_CENTRAL);
328 		llcp_lp_enc_tx(conn, ctx, PDU_DATA_LLCTRL_TYPE_START_ENC_RSP);
329 
330 		/* Wait for LL_START_ENC_RSP */
331 		ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_START_ENC_RSP;
332 		ctx->state = LP_ENC_STATE_WAIT_RX_START_ENC_RSP;
333 
334 		/* Tx Encryption enabled */
335 		conn->lll.enc_tx = 1U;
336 
337 		/* Rx Decryption enabled */
338 		conn->lll.enc_rx = 1U;
339 	}
340 }
341 
lp_enc_st_unencrypted(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)342 static void lp_enc_st_unencrypted(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
343 				  void *param)
344 {
345 	switch (evt) {
346 	case LP_ENC_EVT_RUN:
347 		/* Pause Tx data */
348 		llcp_tx_pause_data(conn, LLCP_TX_QUEUE_PAUSE_DATA_ENCRYPTION);
349 		lp_enc_send_enc_req(conn, ctx, evt, param);
350 		break;
351 	default:
352 		/* Ignore other evts */
353 		break;
354 	}
355 }
356 
lp_enc_st_wait_tx_enc_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)357 static void lp_enc_st_wait_tx_enc_req(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
358 				      void *param)
359 {
360 	switch (evt) {
361 	case LP_ENC_EVT_RUN:
362 		lp_enc_send_enc_req(conn, ctx, evt, param);
363 		break;
364 	default:
365 		/* Ignore other evts */
366 		break;
367 	}
368 }
369 
lp_enc_store_s(struct ll_conn * conn,struct proc_ctx * ctx,struct pdu_data * pdu)370 static void lp_enc_store_s(struct ll_conn *conn, struct proc_ctx *ctx, struct pdu_data *pdu)
371 {
372 	/* Store SKDs */
373 	memcpy(&ctx->data.enc.skd[8], pdu->llctrl.enc_rsp.skds, sizeof(pdu->llctrl.enc_rsp.skds));
374 	/* Store IVs in the LLL CCM RX
375 	 * TODO(thoh): Should this be made into a ULL function, as it
376 	 * interacts with data outside of LLCP?
377 	 */
378 	memcpy(&conn->lll.ccm_rx.iv[4], pdu->llctrl.enc_rsp.ivs, sizeof(pdu->llctrl.enc_rsp.ivs));
379 }
380 
reject_error_code(struct pdu_data * pdu)381 static inline uint8_t reject_error_code(struct pdu_data *pdu)
382 {
383 	if (pdu->llctrl.opcode == PDU_DATA_LLCTRL_TYPE_REJECT_IND) {
384 		return pdu->llctrl.reject_ind.error_code;
385 #if defined(CONFIG_BT_CTLR_EXT_REJ_IND)
386 	} else if (pdu->llctrl.opcode == PDU_DATA_LLCTRL_TYPE_REJECT_EXT_IND) {
387 		return pdu->llctrl.reject_ext_ind.error_code;
388 #endif /* CONFIG_BT_CTLR_EXT_REJ_IND */
389 	} else {
390 		/* Called with an invalid PDU */
391 		LL_ASSERT(0);
392 
393 		/* Keep compiler happy */
394 		return BT_HCI_ERR_UNSPECIFIED;
395 	}
396 }
397 
lp_enc_st_wait_rx_enc_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)398 static void lp_enc_st_wait_rx_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
399 				      void *param)
400 {
401 	struct pdu_data *pdu = (struct pdu_data *)param;
402 
403 	switch (evt) {
404 	case LP_ENC_EVT_ENC_RSP:
405 		/* Pause Rx data */
406 		ull_conn_pause_rx_data(conn);
407 		lp_enc_store_s(conn, ctx, pdu);
408 
409 		/* After the Central has received the LL_ENC_RSP PDU,
410 		 * only PDUs related to this procedure are valid, and invalids should
411 		 * result in disconnect.
412 		 * to achieve this enable the greedy RX behaviour, such that
413 		 * all PDU's end up in this FSM.
414 		 */
415 		ctx->rx_greedy = 1U;
416 
417 		/* Wait for LL_START_ENC_REQ */
418 		ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_START_ENC_REQ;
419 		ctx->state = LP_ENC_STATE_WAIT_RX_START_ENC_REQ;
420 		break;
421 	case LP_ENC_EVT_REJECT:
422 		/* Encryption is not supported by the Link Layer of the Peripheral */
423 
424 		/* Resume Tx data */
425 		llcp_tx_resume_data(conn, LLCP_TX_QUEUE_PAUSE_DATA_ENCRYPTION);
426 
427 		/* Store the error reason */
428 		ctx->data.enc.error = reject_error_code(pdu);
429 
430 		/* Resume possibly paused remote procedure */
431 		llcp_rr_resume(conn);
432 
433 		/* Complete the procedure */
434 		lp_enc_complete(conn, ctx, evt, param);
435 		break;
436 	default:
437 		/* Ignore other evts */
438 		break;
439 	}
440 }
441 
lp_enc_st_wait_rx_start_enc_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)442 static void lp_enc_st_wait_rx_start_enc_req(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
443 					    void *param)
444 {
445 	struct pdu_data *pdu = (struct pdu_data *)param;
446 
447 	switch (evt) {
448 	case LP_ENC_EVT_START_ENC_REQ:
449 		lp_enc_send_start_enc_rsp(conn, ctx, evt, param);
450 		break;
451 	case LP_ENC_EVT_REJECT:
452 		/* Resume Tx data */
453 		llcp_tx_resume_data(conn, LLCP_TX_QUEUE_PAUSE_DATA_ENCRYPTION);
454 		/* Resume Rx data */
455 		ull_conn_resume_rx_data(conn);
456 
457 		/* Store the error reason */
458 		ctx->data.enc.error = reject_error_code(pdu);
459 
460 		/* Resume possibly paused remote procedure */
461 		llcp_rr_resume(conn);
462 
463 		/* Disable the greedy behaviour */
464 		ctx->rx_greedy = 0U;
465 
466 		lp_enc_complete(conn, ctx, evt, param);
467 		break;
468 	default:
469 		/* Ignore other evts */
470 		break;
471 	}
472 }
473 
lp_enc_st_wait_tx_start_enc_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)474 static void lp_enc_st_wait_tx_start_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
475 					    void *param)
476 {
477 	switch (evt) {
478 	case LP_ENC_EVT_RUN:
479 		lp_enc_send_start_enc_rsp(conn, ctx, evt, param);
480 		break;
481 	default:
482 		/* Ignore other evts */
483 		break;
484 	}
485 }
486 
lp_enc_st_wait_rx_start_enc_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)487 static void lp_enc_st_wait_rx_start_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
488 					    void *param)
489 {
490 	switch (evt) {
491 	case LP_ENC_EVT_START_ENC_RSP:
492 		/* Resume Tx data */
493 		llcp_tx_resume_data(conn, LLCP_TX_QUEUE_PAUSE_DATA_ENCRYPTION);
494 		/* Resume Rx data */
495 		ull_conn_resume_rx_data(conn);
496 		ctx->data.enc.error = BT_HCI_ERR_SUCCESS;
497 
498 		/* Resume possibly paused remote procedure */
499 		llcp_rr_resume(conn);
500 
501 		/* Disable the greedy behaviour */
502 		ctx->rx_greedy = 0U;
503 
504 		lp_enc_complete(conn, ctx, evt, param);
505 		break;
506 	default:
507 		/* Ignore other evts */
508 		break;
509 	}
510 }
511 
lp_enc_state_encrypted(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)512 static void lp_enc_state_encrypted(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
513 				   void *param)
514 {
515 	switch (evt) {
516 	case LP_ENC_EVT_RUN:
517 		/* Pause Tx data */
518 		llcp_tx_pause_data(conn, LLCP_TX_QUEUE_PAUSE_DATA_ENCRYPTION);
519 		lp_enc_send_pause_enc_req(conn, ctx, evt, param);
520 		break;
521 	default:
522 		/* Ignore other evts */
523 		break;
524 	}
525 }
526 
lp_enc_state_wait_tx_pause_enc_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)527 static void lp_enc_state_wait_tx_pause_enc_req(struct ll_conn *conn, struct proc_ctx *ctx,
528 					       uint8_t evt, void *param)
529 {
530 	switch (evt) {
531 	case LP_ENC_EVT_RUN:
532 		lp_enc_send_pause_enc_req(conn, ctx, evt, param);
533 		break;
534 	default:
535 		/* Ignore other evts */
536 		break;
537 	}
538 }
539 
lp_enc_state_wait_rx_pause_enc_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)540 static void lp_enc_state_wait_rx_pause_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx,
541 					       uint8_t evt, void *param)
542 {
543 	switch (evt) {
544 	case LP_ENC_EVT_PAUSE_ENC_RSP:
545 		/*
546 		 * Pause Rx data; will be resumed when the encapsulated
547 		 * Start Procedure is done.
548 		 */
549 		ull_conn_pause_rx_data(conn);
550 		lp_enc_send_pause_enc_rsp(conn, ctx, evt, param);
551 		break;
552 	default:
553 		/* Ignore other evts */
554 		break;
555 	}
556 }
557 
lp_enc_state_wait_tx_pause_enc_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)558 static void lp_enc_state_wait_tx_pause_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx,
559 					       uint8_t evt, void *param)
560 {
561 	switch (evt) {
562 	case LP_ENC_EVT_RUN:
563 		lp_enc_send_pause_enc_rsp(conn, ctx, evt, param);
564 		break;
565 	default:
566 		/* Ignore other evts */
567 		break;
568 	}
569 }
570 
lp_enc_execute_fsm(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)571 static void lp_enc_execute_fsm(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
572 {
573 	switch (ctx->state) {
574 	/* Start Procedure */
575 	case LP_ENC_STATE_UNENCRYPTED:
576 		lp_enc_st_unencrypted(conn, ctx, evt, param);
577 		break;
578 	case LP_ENC_STATE_WAIT_TX_ENC_REQ:
579 		lp_enc_st_wait_tx_enc_req(conn, ctx, evt, param);
580 		break;
581 	case LP_ENC_STATE_WAIT_RX_ENC_RSP:
582 		lp_enc_st_wait_rx_enc_rsp(conn, ctx, evt, param);
583 		break;
584 	case LP_ENC_STATE_WAIT_RX_START_ENC_REQ:
585 		lp_enc_st_wait_rx_start_enc_req(conn, ctx, evt, param);
586 		break;
587 	case LP_ENC_STATE_WAIT_TX_START_ENC_RSP:
588 		lp_enc_st_wait_tx_start_enc_rsp(conn, ctx, evt, param);
589 		break;
590 	case LP_ENC_STATE_WAIT_RX_START_ENC_RSP:
591 		lp_enc_st_wait_rx_start_enc_rsp(conn, ctx, evt, param);
592 		break;
593 	/* Pause Procedure */
594 	case LP_ENC_STATE_ENCRYPTED:
595 		lp_enc_state_encrypted(conn, ctx, evt, param);
596 		break;
597 	case LP_ENC_STATE_WAIT_TX_PAUSE_ENC_REQ:
598 		lp_enc_state_wait_tx_pause_enc_req(conn, ctx, evt, param);
599 		break;
600 	case LP_ENC_STATE_WAIT_RX_PAUSE_ENC_RSP:
601 		lp_enc_state_wait_rx_pause_enc_rsp(conn, ctx, evt, param);
602 		break;
603 	case LP_ENC_STATE_WAIT_TX_PAUSE_ENC_RSP:
604 		lp_enc_state_wait_tx_pause_enc_rsp(conn, ctx, evt, param);
605 		break;
606 	default:
607 		/* Unknown state */
608 		LL_ASSERT(0);
609 	}
610 }
611 
llcp_lp_enc_rx(struct ll_conn * conn,struct proc_ctx * ctx,struct node_rx_pdu * rx)612 void llcp_lp_enc_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx)
613 {
614 	struct pdu_data *pdu = (struct pdu_data *)rx->pdu;
615 
616 	switch (pdu->llctrl.opcode) {
617 	case PDU_DATA_LLCTRL_TYPE_ENC_RSP:
618 		lp_enc_execute_fsm(conn, ctx, LP_ENC_EVT_ENC_RSP, pdu);
619 		break;
620 	case PDU_DATA_LLCTRL_TYPE_START_ENC_REQ:
621 		lp_enc_execute_fsm(conn, ctx, LP_ENC_EVT_START_ENC_REQ, pdu);
622 		break;
623 	case PDU_DATA_LLCTRL_TYPE_START_ENC_RSP:
624 		lp_enc_execute_fsm(conn, ctx, LP_ENC_EVT_START_ENC_RSP, pdu);
625 		break;
626 	case PDU_DATA_LLCTRL_TYPE_REJECT_IND:
627 	case PDU_DATA_LLCTRL_TYPE_REJECT_EXT_IND:
628 		lp_enc_execute_fsm(conn, ctx, LP_ENC_EVT_REJECT, pdu);
629 		break;
630 	case PDU_DATA_LLCTRL_TYPE_PAUSE_ENC_RSP:
631 		lp_enc_execute_fsm(conn, ctx, LP_ENC_EVT_PAUSE_ENC_RSP, pdu);
632 		break;
633 	default:
634 		/* Unknown opcode */
635 
636 		/*
637 		 * BLUETOOTH CORE SPECIFICATION Version 5.3
638 		 * Vol 6, Part B, 5.1.3.1 Encryption Start procedure
639 		 *
640 		 * [...]
641 		 *
642 		 * If, at any time during the encryption start procedure after the Peripheral has
643 		 * received the LL_ENC_REQ PDU or the Central has received the
644 		 * LL_ENC_RSP PDU, the Link Layer of the Central or the Peripheral receives an
645 		 * unexpected Data Physical Channel PDU from the peer Link Layer, it shall
646 		 * immediately exit the Connection state, and shall transition to the Standby state.
647 		 * The Host shall be notified that the link has been disconnected with the error
648 		 * code Connection Terminated Due to MIC Failure (0x3D).
649 		 */
650 
651 		conn->llcp_terminate.reason_final = BT_HCI_ERR_TERM_DUE_TO_MIC_FAIL;
652 	}
653 }
654 
llcp_lp_enc_init_proc(struct proc_ctx * ctx)655 void llcp_lp_enc_init_proc(struct proc_ctx *ctx)
656 {
657 	switch (ctx->proc) {
658 	case PROC_ENCRYPTION_START:
659 		ctx->state = LP_ENC_STATE_UNENCRYPTED;
660 		break;
661 	case PROC_ENCRYPTION_PAUSE:
662 		ctx->state = LP_ENC_STATE_ENCRYPTED;
663 		break;
664 	default:
665 		LL_ASSERT(0);
666 	}
667 }
668 
llcp_lp_enc_run(struct ll_conn * conn,struct proc_ctx * ctx,void * param)669 void llcp_lp_enc_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param)
670 {
671 	lp_enc_execute_fsm(conn, ctx, LP_ENC_EVT_RUN, param);
672 }
673 
674 #endif /* CONFIG_BT_CENTRAL */
675 
676 #if defined(CONFIG_BT_PERIPHERAL)
677 /*
678  * LLCP Remote Procedure Encryption FSM
679  */
680 
llcp_rp_enc_tx(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t opcode)681 static struct node_tx *llcp_rp_enc_tx(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t opcode)
682 {
683 	struct node_tx *tx;
684 	struct pdu_data *pdu;
685 
686 	/* Allocate tx node */
687 	tx = llcp_tx_alloc(conn, ctx);
688 	LL_ASSERT(tx);
689 
690 	pdu = (struct pdu_data *)tx->pdu;
691 
692 	/* Encode LL Control PDU */
693 	switch (opcode) {
694 	case PDU_DATA_LLCTRL_TYPE_ENC_RSP:
695 		llcp_pdu_encode_enc_rsp(pdu);
696 		break;
697 	case PDU_DATA_LLCTRL_TYPE_START_ENC_REQ:
698 		llcp_pdu_encode_start_enc_req(pdu);
699 		break;
700 	case PDU_DATA_LLCTRL_TYPE_START_ENC_RSP:
701 		llcp_pdu_encode_start_enc_rsp(pdu);
702 		break;
703 	case PDU_DATA_LLCTRL_TYPE_PAUSE_ENC_RSP:
704 		llcp_pdu_encode_pause_enc_rsp(pdu);
705 		break;
706 	case PDU_DATA_LLCTRL_TYPE_REJECT_IND:
707 		if (conn->llcp.fex.valid && feature_ext_rej_ind(conn)) {
708 			llcp_pdu_encode_reject_ext_ind(pdu, PDU_DATA_LLCTRL_TYPE_ENC_REQ,
709 						       BT_HCI_ERR_PIN_OR_KEY_MISSING);
710 		} else {
711 			llcp_pdu_encode_reject_ind(pdu, BT_HCI_ERR_PIN_OR_KEY_MISSING);
712 		}
713 		break;
714 	default:
715 		LL_ASSERT(0);
716 	}
717 
718 	ctx->tx_opcode = pdu->llctrl.opcode;
719 
720 	/* Enqueue LL Control PDU towards LLL */
721 	llcp_tx_enqueue(conn, tx);
722 
723 	/* Restart procedure response timeout timer */
724 	llcp_rr_prt_restart(conn);
725 
726 	return tx;
727 }
728 
rp_enc_ntf_ltk(struct ll_conn * conn,struct proc_ctx * ctx)729 static void rp_enc_ntf_ltk(struct ll_conn *conn, struct proc_ctx *ctx)
730 {
731 	struct node_rx_pdu *ntf;
732 	struct pdu_data *pdu;
733 	uint8_t piggy_back;
734 
735 	/* Piggy-back on RX node */
736 	ntf = ctx->node_ref.rx;
737 	ctx->node_ref.rx = NULL;
738 	LL_ASSERT(ntf);
739 
740 	piggy_back = (ntf->hdr.type != NODE_RX_TYPE_RETAIN);
741 
742 	ntf->hdr.type = NODE_RX_TYPE_DC_PDU;
743 	ntf->hdr.handle = conn->lll.handle;
744 	pdu = (struct pdu_data *)ntf->pdu;
745 
746 	llcp_ntf_encode_enc_req(ctx, pdu);
747 
748 	if (!piggy_back) {
749 		/* Enqueue notification towards LL unless it's piggybacked */
750 		ll_rx_put_sched(ntf->hdr.link, ntf);
751 	}
752 
753 }
754 
rp_enc_ntf(struct ll_conn * conn,struct proc_ctx * ctx)755 static void rp_enc_ntf(struct ll_conn *conn, struct proc_ctx *ctx)
756 {
757 	struct node_rx_pdu *ntf;
758 	struct pdu_data *pdu;
759 
760 	/* Piggy-back on RX node */
761 	ntf = ctx->node_ref.rx;
762 	ctx->node_ref.rx = NULL;
763 	LL_ASSERT(ntf);
764 
765 	ntf->hdr.type = NODE_RX_TYPE_DC_PDU;
766 	ntf->hdr.handle = conn->lll.handle;
767 	pdu = (struct pdu_data *)ntf->pdu;
768 
769 	if (ctx->proc == PROC_ENCRYPTION_START) {
770 		/* Encryption Change Event */
771 		/* TODO(thoh): is this correct? */
772 		llcp_pdu_encode_start_enc_rsp(pdu);
773 	} else if (ctx->proc == PROC_ENCRYPTION_PAUSE) {
774 		/* Encryption Key Refresh Complete Event */
775 		ntf->hdr.type = NODE_RX_TYPE_ENC_REFRESH;
776 	} else {
777 		/* Should never happen */
778 		LL_ASSERT(0);
779 	}
780 }
781 
782 static void rp_enc_send_start_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
783 				      void *param);
784 
rp_enc_complete(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)785 static void rp_enc_complete(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
786 {
787 	rp_enc_ntf(conn, ctx);
788 	rp_enc_send_start_enc_rsp(conn, ctx, evt, param);
789 }
790 
rp_enc_store_s(struct ll_conn * conn,struct proc_ctx * ctx,struct pdu_data * pdu)791 static void rp_enc_store_s(struct ll_conn *conn, struct proc_ctx *ctx, struct pdu_data *pdu)
792 {
793 	/* Store SKDs */
794 	memcpy(&ctx->data.enc.skds, pdu->llctrl.enc_rsp.skds, sizeof(pdu->llctrl.enc_rsp.skds));
795 	/* Store IVs in the LLL CCM RX
796 	 * TODO(thoh): Should this be made into a ULL function, as it
797 	 * interacts with data outside of LLCP?
798 	 */
799 	memcpy(&conn->lll.ccm_rx.iv[4], pdu->llctrl.enc_rsp.ivs, sizeof(pdu->llctrl.enc_rsp.ivs));
800 }
801 
rp_enc_send_enc_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)802 static void rp_enc_send_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
803 				void *param)
804 {
805 	struct node_tx *tx;
806 
807 	if (!llcp_tx_alloc_peek(conn, ctx)) {
808 		/* Mark RX node to not release, needed for LTK NTF */
809 		llcp_rx_node_retain(ctx);
810 		ctx->state = RP_ENC_STATE_WAIT_TX_ENC_RSP;
811 	} else {
812 		tx = llcp_rp_enc_tx(conn, ctx, PDU_DATA_LLCTRL_TYPE_ENC_RSP);
813 		rp_enc_store_s(conn, ctx, (struct pdu_data *)tx->pdu);
814 
815 		rp_enc_ntf_ltk(conn, ctx);
816 		ctx->state = RP_ENC_STATE_WAIT_LTK_REPLY;
817 	}
818 }
819 
rp_enc_send_start_enc_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)820 static void rp_enc_send_start_enc_req(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
821 				      void *param)
822 {
823 	if (!llcp_tx_alloc_peek(conn, ctx)) {
824 		ctx->state = RP_ENC_STATE_WAIT_TX_START_ENC_REQ;
825 	} else {
826 		enc_setup_lll(conn, ctx, BT_HCI_ROLE_PERIPHERAL);
827 		llcp_rp_enc_tx(conn, ctx, PDU_DATA_LLCTRL_TYPE_START_ENC_REQ);
828 		/* Wait for the LL_START_ENC_RSP */
829 		ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_START_ENC_RSP;
830 		ctx->state = RP_ENC_STATE_WAIT_RX_START_ENC_RSP;
831 
832 		/* Rx Decryption enabled */
833 		conn->lll.enc_rx = 1U;
834 	}
835 }
836 
rp_enc_send_reject_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)837 static void rp_enc_send_reject_ind(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
838 				   void *param)
839 {
840 	if (!llcp_tx_alloc_peek(conn, ctx)) {
841 		ctx->state = RP_ENC_STATE_WAIT_TX_REJECT_IND;
842 	} else {
843 		llcp_rp_enc_tx(conn, ctx, PDU_DATA_LLCTRL_TYPE_REJECT_IND);
844 		llcp_rr_complete(conn);
845 		ctx->state = RP_ENC_STATE_UNENCRYPTED;
846 
847 		/* Resume Tx data */
848 		llcp_tx_resume_data(conn, LLCP_TX_QUEUE_PAUSE_DATA_ENCRYPTION);
849 		/* Resume Rx data */
850 		ull_conn_resume_rx_data(conn);
851 		/* Resume possibly paused local procedure */
852 		llcp_lr_resume(conn);
853 	}
854 }
855 
rp_enc_send_start_enc_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)856 static void rp_enc_send_start_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
857 				      void *param)
858 {
859 	if (!llcp_tx_alloc_peek(conn, ctx)) {
860 		ctx->state = RP_ENC_STATE_WAIT_TX_START_ENC_RSP;
861 	} else {
862 		llcp_rp_enc_tx(conn, ctx, PDU_DATA_LLCTRL_TYPE_START_ENC_RSP);
863 		llcp_rr_complete(conn);
864 		ctx->state = RP_ENC_STATE_UNENCRYPTED;
865 
866 		/* Resume Tx data */
867 		llcp_tx_resume_data(conn, LLCP_TX_QUEUE_PAUSE_DATA_ENCRYPTION);
868 		/* Resume Rx data */
869 		ull_conn_resume_rx_data(conn);
870 
871 		/* Resume possibly paused local procedure */
872 		llcp_lr_resume(conn);
873 
874 		/* Tx Encryption enabled */
875 		conn->lll.enc_tx = 1U;
876 	}
877 }
878 
rp_enc_send_pause_enc_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)879 static void rp_enc_send_pause_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
880 				      void *param)
881 {
882 	if (!llcp_tx_alloc_peek(conn, ctx)) {
883 		ctx->state = RP_ENC_STATE_WAIT_TX_PAUSE_ENC_RSP;
884 	} else {
885 		llcp_rp_enc_tx(conn, ctx, PDU_DATA_LLCTRL_TYPE_PAUSE_ENC_RSP);
886 		/* Wait for the LL_PAUSE_ENC_RSP */
887 		ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_PAUSE_ENC_RSP;
888 		ctx->state = RP_ENC_STATE_WAIT_RX_PAUSE_ENC_RSP;
889 
890 		/* Rx Decryption disabled */
891 		conn->lll.enc_rx = 0U;
892 	}
893 }
894 
rp_enc_state_unencrypted(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)895 static void rp_enc_state_unencrypted(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
896 				     void *param)
897 {
898 	switch (evt) {
899 	case RP_ENC_EVT_RUN:
900 		ctx->state = RP_ENC_STATE_WAIT_RX_ENC_REQ;
901 		break;
902 	default:
903 		/* Ignore other evts */
904 		break;
905 	}
906 }
907 
rp_enc_store_m(struct ll_conn * conn,struct proc_ctx * ctx,struct pdu_data * pdu)908 static void rp_enc_store_m(struct ll_conn *conn, struct proc_ctx *ctx, struct pdu_data *pdu)
909 {
910 	/* Store Rand */
911 	memcpy(ctx->data.enc.rand, pdu->llctrl.enc_req.rand, sizeof(ctx->data.enc.rand));
912 
913 	/* Store EDIV */
914 	ctx->data.enc.ediv[0] = pdu->llctrl.enc_req.ediv[0];
915 	ctx->data.enc.ediv[1] = pdu->llctrl.enc_req.ediv[1];
916 
917 	/* Store SKDm */
918 	memcpy(&ctx->data.enc.skdm, pdu->llctrl.enc_req.skdm, sizeof(ctx->data.enc.skdm));
919 
920 	/* Store IVm in the LLL CCM RX
921 	 * TODO(thoh): Should this be made into a ULL function, as it
922 	 * interacts with data outside of LLCP?
923 	 */
924 	memcpy(&conn->lll.ccm_rx.iv[0], pdu->llctrl.enc_req.ivm, sizeof(pdu->llctrl.enc_req.ivm));
925 }
926 
rp_enc_state_wait_rx_enc_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)927 static void rp_enc_state_wait_rx_enc_req(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
928 					 void *param)
929 {
930 	switch (evt) {
931 	case RP_ENC_EVT_ENC_REQ:
932 		/* Pause Tx data */
933 		llcp_tx_pause_data(conn, LLCP_TX_QUEUE_PAUSE_DATA_ENCRYPTION);
934 		/* Pause Rx data */
935 		ull_conn_pause_rx_data(conn);
936 
937 		/* Pause possibly paused local procedure */
938 		llcp_lr_pause(conn);
939 
940 		rp_enc_store_m(conn, ctx, param);
941 
942 		rp_enc_send_enc_rsp(conn, ctx, evt, param);
943 		break;
944 	default:
945 		/* Ignore other evts */
946 		break;
947 	}
948 }
949 
rp_enc_state_wait_tx_enc_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)950 static void rp_enc_state_wait_tx_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
951 					 void *param)
952 {
953 	switch (evt) {
954 	case RP_ENC_EVT_RUN:
955 		rp_enc_send_enc_rsp(conn, ctx, evt, param);
956 		break;
957 	default:
958 		/* Ignore other evts */
959 		break;
960 	}
961 }
962 
rp_enc_state_wait_ltk_reply(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)963 static void rp_enc_state_wait_ltk_reply(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
964 					void *param)
965 {
966 	switch (evt) {
967 	case RP_ENC_EVT_LTK_REQ_REPLY:
968 		rp_enc_send_start_enc_req(conn, ctx, evt, param);
969 		break;
970 	case RP_ENC_EVT_LTK_REQ_NEG_REPLY:
971 		rp_enc_send_reject_ind(conn, ctx, evt, param);
972 		break;
973 	default:
974 		/* Ignore other evts */
975 		break;
976 	}
977 }
978 
rp_enc_state_wait_tx_start_enc_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)979 static void rp_enc_state_wait_tx_start_enc_req(struct ll_conn *conn, struct proc_ctx *ctx,
980 					       uint8_t evt, void *param)
981 {
982 	switch (evt) {
983 	case RP_ENC_EVT_RUN:
984 		rp_enc_send_start_enc_req(conn, ctx, evt, param);
985 		break;
986 	default:
987 		/* Ignore other evts */
988 		break;
989 	}
990 }
991 
rp_enc_state_wait_tx_reject_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)992 static void rp_enc_state_wait_tx_reject_ind(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
993 					    void *param)
994 {
995 	switch (evt) {
996 	case RP_ENC_EVT_RUN:
997 		rp_enc_send_reject_ind(conn, ctx, evt, param);
998 		break;
999 	default:
1000 		/* Ignore other evts */
1001 		break;
1002 	}
1003 }
1004 
rp_enc_state_wait_rx_start_enc_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)1005 static void rp_enc_state_wait_rx_start_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx,
1006 					       uint8_t evt, void *param)
1007 {
1008 	switch (evt) {
1009 	case RP_ENC_EVT_START_ENC_RSP:
1010 		rp_enc_complete(conn, ctx, evt, param);
1011 		break;
1012 	default:
1013 		/* Ignore other evts */
1014 		break;
1015 	}
1016 }
1017 
rp_enc_state_wait_tx_start_enc_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)1018 static void rp_enc_state_wait_tx_start_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx,
1019 					       uint8_t evt, void *param)
1020 {
1021 	switch (evt) {
1022 	case RP_ENC_EVT_RUN:
1023 		rp_enc_send_start_enc_rsp(conn, ctx, evt, param);
1024 		break;
1025 	default:
1026 		/* Ignore other evts */
1027 		break;
1028 	}
1029 }
1030 
rp_enc_state_encrypted(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)1031 static void rp_enc_state_encrypted(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
1032 				   void *param)
1033 {
1034 	switch (evt) {
1035 	case RP_ENC_EVT_RUN:
1036 		ctx->state = RP_ENC_STATE_WAIT_RX_PAUSE_ENC_REQ;
1037 		break;
1038 	default:
1039 		/* Ignore other evts */
1040 		break;
1041 	}
1042 }
1043 
rp_enc_state_wait_rx_pause_enc_req(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)1044 static void rp_enc_state_wait_rx_pause_enc_req(struct ll_conn *conn, struct proc_ctx *ctx,
1045 					       uint8_t evt, void *param)
1046 {
1047 	switch (evt) {
1048 	case RP_ENC_EVT_PAUSE_ENC_REQ:
1049 		/* Pause Tx data */
1050 		llcp_tx_pause_data(conn, LLCP_TX_QUEUE_PAUSE_DATA_ENCRYPTION);
1051 		/*
1052 		 * Pause Rx data; will be resumed when the encapsulated
1053 		 * Start Procedure is done.
1054 		 */
1055 		ull_conn_pause_rx_data(conn);
1056 		rp_enc_send_pause_enc_rsp(conn, ctx, evt, param);
1057 		break;
1058 	default:
1059 		/* Ignore other evts */
1060 		break;
1061 	}
1062 }
1063 
rp_enc_state_wait_tx_pause_enc_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)1064 static void rp_enc_state_wait_tx_pause_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx,
1065 					       uint8_t evt, void *param)
1066 {
1067 	switch (evt) {
1068 	case RP_ENC_EVT_RUN:
1069 		rp_enc_send_pause_enc_rsp(conn, ctx, evt, param);
1070 		break;
1071 	default:
1072 		/* Ignore other evts */
1073 		break;
1074 	}
1075 }
1076 
rp_enc_state_wait_rx_pause_enc_rsp(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)1077 static void rp_enc_state_wait_rx_pause_enc_rsp(struct ll_conn *conn, struct proc_ctx *ctx,
1078 					       uint8_t evt, void *param)
1079 {
1080 	switch (evt) {
1081 	case RP_ENC_EVT_PAUSE_ENC_RSP:
1082 		/* Continue with an encapsulated Start Procedure */
1083 		/* Wait for the LL_ENC_REQ */
1084 		ctx->rx_opcode = PDU_DATA_LLCTRL_TYPE_ENC_REQ;
1085 		ctx->state = RP_ENC_STATE_WAIT_RX_ENC_REQ;
1086 
1087 		/* Tx Encryption disabled */
1088 		conn->lll.enc_tx = 0U;
1089 		break;
1090 	default:
1091 		/* Ignore other evts */
1092 		break;
1093 	}
1094 }
1095 
rp_enc_execute_fsm(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)1096 static void rp_enc_execute_fsm(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
1097 {
1098 	switch (ctx->state) {
1099 	/* Start Procedure */
1100 	case RP_ENC_STATE_UNENCRYPTED:
1101 		rp_enc_state_unencrypted(conn, ctx, evt, param);
1102 		break;
1103 	case RP_ENC_STATE_WAIT_RX_ENC_REQ:
1104 		rp_enc_state_wait_rx_enc_req(conn, ctx, evt, param);
1105 		break;
1106 	case RP_ENC_STATE_WAIT_TX_ENC_RSP:
1107 		rp_enc_state_wait_tx_enc_rsp(conn, ctx, evt, param);
1108 		break;
1109 	case RP_ENC_STATE_WAIT_LTK_REPLY:
1110 		rp_enc_state_wait_ltk_reply(conn, ctx, evt, param);
1111 		break;
1112 	case RP_ENC_STATE_WAIT_TX_START_ENC_REQ:
1113 		rp_enc_state_wait_tx_start_enc_req(conn, ctx, evt, param);
1114 		break;
1115 	case RP_ENC_STATE_WAIT_TX_REJECT_IND:
1116 		rp_enc_state_wait_tx_reject_ind(conn, ctx, evt, param);
1117 		break;
1118 	case RP_ENC_STATE_WAIT_RX_START_ENC_RSP:
1119 		rp_enc_state_wait_rx_start_enc_rsp(conn, ctx, evt, param);
1120 		break;
1121 	case RP_ENC_STATE_WAIT_TX_START_ENC_RSP:
1122 		rp_enc_state_wait_tx_start_enc_rsp(conn, ctx, evt, param);
1123 		break;
1124 	/* Pause Procedure */
1125 	case RP_ENC_STATE_ENCRYPTED:
1126 		rp_enc_state_encrypted(conn, ctx, evt, param);
1127 		break;
1128 	case RP_ENC_STATE_WAIT_RX_PAUSE_ENC_REQ:
1129 		rp_enc_state_wait_rx_pause_enc_req(conn, ctx, evt, param);
1130 		break;
1131 	case RP_ENC_STATE_WAIT_TX_PAUSE_ENC_RSP:
1132 		rp_enc_state_wait_tx_pause_enc_rsp(conn, ctx, evt, param);
1133 		break;
1134 	case RP_ENC_STATE_WAIT_RX_PAUSE_ENC_RSP:
1135 		rp_enc_state_wait_rx_pause_enc_rsp(conn, ctx, evt, param);
1136 		break;
1137 	default:
1138 		/* Unknown state */
1139 		LL_ASSERT(0);
1140 	}
1141 }
1142 
llcp_rp_enc_rx(struct ll_conn * conn,struct proc_ctx * ctx,struct node_rx_pdu * rx)1143 void llcp_rp_enc_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx)
1144 {
1145 	struct pdu_data *pdu = (struct pdu_data *)rx->pdu;
1146 
1147 	switch (pdu->llctrl.opcode) {
1148 	case PDU_DATA_LLCTRL_TYPE_ENC_REQ:
1149 		rp_enc_execute_fsm(conn, ctx, RP_ENC_EVT_ENC_REQ, pdu);
1150 		break;
1151 	case PDU_DATA_LLCTRL_TYPE_START_ENC_RSP:
1152 		rp_enc_execute_fsm(conn, ctx, RP_ENC_EVT_START_ENC_RSP, pdu);
1153 		break;
1154 	case PDU_DATA_LLCTRL_TYPE_PAUSE_ENC_REQ:
1155 		rp_enc_execute_fsm(conn, ctx, RP_ENC_EVT_PAUSE_ENC_REQ, pdu);
1156 		break;
1157 	case PDU_DATA_LLCTRL_TYPE_PAUSE_ENC_RSP:
1158 		rp_enc_execute_fsm(conn, ctx, RP_ENC_EVT_PAUSE_ENC_RSP, pdu);
1159 		break;
1160 	default:
1161 		/* Unknown opcode */
1162 
1163 		/*
1164 		 * BLUETOOTH CORE SPECIFICATION Version 5.3
1165 		 * Vol 6, Part B, 5.1.3.1 Encryption Start procedure
1166 		 *
1167 		 * [...]
1168 		 *
1169 		 * If, at any time during the encryption start procedure after the Peripheral has
1170 		 * received the LL_ENC_REQ PDU or the Central has received the
1171 		 * LL_ENC_RSP PDU, the Link Layer of the Central or the Peripheral receives an
1172 		 * unexpected Data Physical Channel PDU from the peer Link Layer, it shall
1173 		 * immediately exit the Connection state, and shall transition to the Standby state.
1174 		 * The Host shall be notified that the link has been disconnected with the error
1175 		 * code Connection Terminated Due to MIC Failure (0x3D).
1176 		 */
1177 
1178 		conn->llcp_terminate.reason_final = BT_HCI_ERR_TERM_DUE_TO_MIC_FAIL;
1179 	}
1180 }
1181 
llcp_rp_enc_init_proc(struct proc_ctx * ctx)1182 void llcp_rp_enc_init_proc(struct proc_ctx *ctx)
1183 {
1184 	switch (ctx->proc) {
1185 	case PROC_ENCRYPTION_START:
1186 		ctx->state = RP_ENC_STATE_UNENCRYPTED;
1187 		break;
1188 	case PROC_ENCRYPTION_PAUSE:
1189 		ctx->state = RP_ENC_STATE_ENCRYPTED;
1190 		break;
1191 	default:
1192 		LL_ASSERT(0);
1193 	}
1194 }
1195 
llcp_rp_enc_ltk_req_reply(struct ll_conn * conn,struct proc_ctx * ctx)1196 void llcp_rp_enc_ltk_req_reply(struct ll_conn *conn, struct proc_ctx *ctx)
1197 {
1198 	rp_enc_execute_fsm(conn, ctx, RP_ENC_EVT_LTK_REQ_REPLY, NULL);
1199 }
1200 
llcp_rp_enc_ltk_req_neg_reply(struct ll_conn * conn,struct proc_ctx * ctx)1201 void llcp_rp_enc_ltk_req_neg_reply(struct ll_conn *conn, struct proc_ctx *ctx)
1202 {
1203 	rp_enc_execute_fsm(conn, ctx, RP_ENC_EVT_LTK_REQ_NEG_REPLY, NULL);
1204 }
1205 
llcp_rp_enc_ltk_req_reply_allowed(struct ll_conn * conn,struct proc_ctx * ctx)1206 bool llcp_rp_enc_ltk_req_reply_allowed(struct ll_conn *conn, struct proc_ctx *ctx)
1207 {
1208 	return (ctx->state == RP_ENC_STATE_WAIT_LTK_REPLY);
1209 }
1210 
llcp_rp_enc_run(struct ll_conn * conn,struct proc_ctx * ctx,void * param)1211 void llcp_rp_enc_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param)
1212 {
1213 	rp_enc_execute_fsm(conn, ctx, RP_ENC_EVT_RUN, param);
1214 }
1215 #endif /* CONFIG_BT_PERIPHERAL */
1216