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