1 /*
2  * Copyright (c) 2018-2019 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #if defined(CONFIG_BT_CTLR_FAL_SIZE) && defined(CONFIG_BT_CTLR_RL_SIZE)
8 #define LLL_FILTER_SIZE MAX(CONFIG_BT_CTLR_FAL_SIZE, CONFIG_BT_CTLR_RL_SIZE)
9 #elif defined(CONFIG_BT_CTLR_FAL_SIZE)
10 #define LLL_FILTER_SIZE CONFIG_BT_CTLR_FAL_SIZE
11 #elif defined(CONFIG_BT_CTLR_RL_SIZE)
12 #define LLL_FILTER_SIZE CONFIG_BT_CTLR_RL_SIZE
13 #else
14 #define LLL_FILTER_SIZE 8
15 #endif /* CONFIG_BT_CTLR_FAL_SIZE && CONFIG_BT_CTLR_RL_SIZE */
16 
17 #define IRK_SIZE               16
18 #define FILTER_IDX_NONE        0xFF
19 #define LLL_FILTER_BITMASK_ALL (BIT_MASK(LLL_FILTER_SIZE))
20 
21 #define LLL_FILTER_SIZE_MAX_BITMASK8	(1 * CHAR_BIT)
22 #define LLL_FILTER_SIZE_MAX_BITMASK16	(2 * CHAR_BIT)
23 
24 struct lll_filter {
25 #if (LLL_FILTER_SIZE <= LLL_FILTER_SIZE_MAX_BITMASK8)
26 	uint8_t  enable_bitmask;
27 	uint8_t  addr_type_bitmask;
28 #elif (LLL_FILTER_SIZE <= LLL_FILTER_SIZE_MAX_BITMASK16)
29 	uint16_t enable_bitmask;
30 	uint16_t addr_type_bitmask;
31 #else
32 #error LLL_FILTER_SIZE must be <= LLL_FILTER_SIZE_MAX_BITMASK16
33 #endif
34 	uint8_t  bdaddr[LLL_FILTER_SIZE][BDADDR_SIZE];
35 };
36 
37 /* Filter Accept List peer list */
38 struct lll_fal {
39 	uint8_t   taken:1;
40 	uint8_t   id_addr_type:1;
41 	uint8_t   rl_idx;
42 	bt_addr_t id_addr;
43 };
44 
45 /* Periodic Advertising List */
46 struct lll_pal {
47 	bt_addr_t id_addr;
48 	uint8_t   taken:1;
49 	uint8_t   id_addr_type:1;
50 	uint8_t   sid;
51 
52 #if defined(CONFIG_BT_CTLR_PRIVACY)
53 	uint8_t   rl_idx;
54 #endif /* CONFIG_BT_CTLR_PRIVACY */
55 };
56 
57 /* Resolve list */
58 struct lll_resolve_list {
59 	uint8_t   taken:1;
60 	uint8_t   rpas_ready:1;
61 	uint8_t   pirk:1;
62 	uint8_t   lirk:1;
63 	uint8_t   dev:1;
64 	uint8_t   fal:1;
65 
66 	uint8_t   id_addr_type:1;
67 	bt_addr_t id_addr;
68 
69 	uint8_t   local_irk[IRK_SIZE];
70 	uint8_t   pirk_idx;
71 	bt_addr_t curr_rpa;
72 	bt_addr_t peer_rpa;
73 	bt_addr_t *local_rpa;
74 #if defined(CONFIG_BT_CTLR_SW_DEFERRED_PRIVACY)
75 	bt_addr_t target_rpa;
76 #endif
77 
78 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC_ADV_LIST)
79 	uint16_t   pal:9; /* 0 - not present, 1 to 256 - lll_pal entry index */
80 #endif /* CONFIG_BT_CTLR_SYNC_PERIODIC_ADV_LIST */
81 };
82 
83 #if defined(CONFIG_BT_CTLR_SW_DEFERRED_PRIVACY)
84 /* Cache of known unknown peer RPAs */
85 struct lll_prpa_cache {
86 	uint8_t   taken:1;
87 	bt_addr_t rpa;
88 };
89 
90 /* Cache of known unknown target RPAs */
91 struct lll_trpa_cache {
92 	uint8_t   rl_idx;
93 	bt_addr_t rpa;
94 };
95 #endif
96 
97 extern uint8_t ull_filter_lll_fal_match(struct lll_filter const *const filter,
98 					uint8_t addr_type,
99 					uint8_t const *const addr,
100 					uint8_t *devmatch_id);
101 extern bool ull_filter_lll_lrpa_used(uint8_t rl_idx);
102 extern bt_addr_t *ull_filter_lll_lrpa_get(uint8_t rl_idx);
103 extern uint8_t *ull_filter_lll_irks_get(uint8_t *count);
104 extern uint8_t ull_filter_lll_rl_idx(bool fal, uint8_t devmatch_id);
105 extern uint8_t ull_filter_lll_rl_irk_idx(uint8_t irkmatch_id);
106 extern bool ull_filter_lll_irk_in_fal(uint8_t rl_idx);
107 extern struct lll_filter *ull_filter_lll_get(bool fal);
108 extern struct lll_fal *ull_filter_lll_fal_get(void);
109 extern struct lll_resolve_list *ull_filter_lll_resolve_list_get(void);
110 extern bool ull_filter_lll_rl_idx_allowed(uint8_t irkmatch_ok, uint8_t rl_idx);
111 extern bool ull_filter_lll_rl_addr_allowed(uint8_t id_addr_type,
112 					   const uint8_t *id_addr,
113 					   uint8_t *const rl_idx);
114 extern bool ull_filter_lll_rl_addr_resolve(uint8_t id_addr_type,
115 					   const uint8_t *id_addr,
116 					   uint8_t rl_idx);
117 extern bool ull_filter_lll_rl_enabled(void);
118 #if defined(CONFIG_BT_CTLR_SW_DEFERRED_PRIVACY)
119 extern const struct lll_prpa_cache *ull_filter_lll_prpa_cache_get(void);
120 extern const struct lll_trpa_cache *ull_filter_lll_trpa_cache_get(void);
121 typedef void (*resolve_callback_t)(void *param);
122 extern uint8_t ull_filter_deferred_resolve(bt_addr_t *rpa,
123 					resolve_callback_t cb);
124 extern uint8_t ull_filter_deferred_targeta_resolve(bt_addr_t *rpa,
125 						uint8_t rl_idx,
126 						resolve_callback_t cb);
127 #endif
128