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