1 /*
2 * Copyright (c) 2017 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "adv.h"
8 #include "subnet.h"
9 #include <zephyr/bluetooth/mesh/sar_cfg.h>
10
11 #define BT_MESH_IV_UPDATE(flags) ((flags >> 1) & 0x01)
12 #define BT_MESH_KEY_REFRESH(flags) (flags & 0x01)
13
14 /* How many hours in between updating IVU duration */
15 #define BT_MESH_IVU_MIN_HOURS 96
16 #define BT_MESH_IVU_HOURS (BT_MESH_IVU_MIN_HOURS / \
17 CONFIG_BT_MESH_IVU_DIVIDER)
18 #define BT_MESH_IVU_TIMEOUT K_HOURS(BT_MESH_IVU_HOURS)
19
20 /* Minimum valid Mesh Network PDU length. The Network headers
21 * themselves take up 9 bytes. After that there is a minimum of 1 byte
22 * payload for both CTL=1 and CTL=0 PDUs (smallest OpCode is 1 byte). CTL=1
23 * PDUs must use a 64-bit (8 byte) NetMIC, whereas CTL=0 PDUs have at least
24 * a 32-bit (4 byte) NetMIC and AppMIC giving again a total of 8 bytes.
25 */
26 #define BT_MESH_NET_MIN_PDU_LEN (BT_MESH_NET_HDR_LEN + 1 + 8)
27 /* Maximum valid Mesh Network PDU length. The longest packet can either be a
28 * transport control message (CTL=1) of 12 bytes + 8 bytes of NetMIC, or an
29 * access message (CTL=0) of 16 bytes + 4 bytes of NetMIC.
30 */
31 #define BT_MESH_NET_MAX_PDU_LEN (BT_MESH_NET_HDR_LEN + 16 + 4)
32
33 struct bt_mesh_net_cred;
34 enum bt_mesh_nonce_type;
35
36 struct bt_mesh_node {
37 uint16_t addr;
38 uint16_t net_idx;
39 struct bt_mesh_key dev_key;
40 uint8_t num_elem;
41 };
42
43 #if defined(CONFIG_BT_MESH_FRIEND)
44 #define FRIEND_SEG_RX CONFIG_BT_MESH_FRIEND_SEG_RX
45 #define FRIEND_SUB_LIST_SIZE CONFIG_BT_MESH_FRIEND_SUB_LIST_SIZE
46 #else
47 #define FRIEND_SEG_RX 0
48 #define FRIEND_SUB_LIST_SIZE 0
49 #endif
50
51 struct bt_mesh_friend {
52 uint16_t lpn;
53 uint8_t recv_delay;
54 uint8_t fsn:1,
55 send_last:1,
56 pending_req:1,
57 pending_buf:1,
58 established:1;
59 int32_t poll_to;
60 uint8_t num_elem;
61 uint16_t lpn_counter;
62 uint16_t counter;
63
64 struct bt_mesh_subnet *subnet;
65
66 struct bt_mesh_net_cred cred[2];
67
68 uint16_t sub_list[FRIEND_SUB_LIST_SIZE];
69
70 struct k_work_delayable timer;
71
72 struct bt_mesh_friend_seg {
73 sys_slist_t queue;
74
75 /* The target number of segments, i.e. not necessarily
76 * the current number of segments, in the queue. This is
77 * used for Friend Queue free space calculations.
78 */
79 uint8_t seg_count;
80 } seg[FRIEND_SEG_RX];
81
82 struct net_buf *last;
83
84 sys_slist_t queue;
85 uint32_t queue_size;
86
87 /* Friend Clear Procedure */
88 struct {
89 uint32_t start; /* Clear Procedure start */
90 uint16_t frnd; /* Previous Friend's address */
91 uint16_t repeat_sec; /* Repeat timeout in seconds */
92 struct k_work_delayable timer; /* Repeat timer */
93 } clear;
94 };
95
96 #if defined(CONFIG_BT_MESH_LOW_POWER)
97 #define LPN_GROUPS CONFIG_BT_MESH_LPN_GROUPS
98 #else
99 #define LPN_GROUPS 0
100 #endif
101
102 /* Low Power Node state */
103 struct bt_mesh_lpn {
104 enum __packed {
105 BT_MESH_LPN_DISABLED, /* LPN feature is disabled */
106 BT_MESH_LPN_CLEAR, /* Clear in progress */
107 BT_MESH_LPN_TIMER, /* Waiting for auto timer expiry */
108 BT_MESH_LPN_ENABLED, /* LPN enabled, but no Friend */
109 BT_MESH_LPN_REQ_WAIT, /* Wait before scanning for offers */
110 BT_MESH_LPN_WAIT_OFFER, /* Friend Req sent */
111 BT_MESH_LPN_ESTABLISHED, /* Friendship established */
112 BT_MESH_LPN_RECV_DELAY, /* Poll sent, waiting ReceiveDelay */
113 BT_MESH_LPN_WAIT_UPDATE, /* Waiting for Update or message */
114 } state;
115
116 /* Transaction Number (used for subscription list) */
117 uint8_t xact_next;
118 uint8_t xact_pending;
119 uint8_t sent_req;
120
121 /* Address of our Friend when we're a LPN. Unassigned if we don't
122 * have a friend yet.
123 */
124 uint16_t frnd;
125
126 /* Value from the friend offer */
127 uint8_t recv_win;
128
129 uint8_t req_attempts; /* Number of Request attempts */
130
131 int32_t poll_timeout;
132
133 uint8_t groups_changed:1, /* Friend Subscription List needs updating */
134 pending_poll:1, /* Poll to be sent after subscription */
135 disable:1, /* Disable LPN after clearing */
136 fsn:1, /* Friend Sequence Number */
137 established:1, /* Friendship established */
138 clear_success:1; /* Friend Clear Confirm received */
139
140 /* Friend Queue Size */
141 uint8_t queue_size;
142
143 /* FriendCounter */
144 uint16_t frnd_counter;
145
146 /* LPNCounter */
147 uint16_t lpn_counter;
148
149 /* Previous Friend of this LPN */
150 uint16_t old_friend;
151
152 /* Duration reported for last advertising packet */
153 uint16_t adv_duration;
154
155 /* Advertising start time. */
156 uint32_t adv_start_time;
157
158 /* Next LPN related action timer */
159 struct k_work_delayable timer;
160
161 /* Subscribed groups */
162 uint16_t groups[LPN_GROUPS];
163
164 struct bt_mesh_subnet *sub;
165
166 struct bt_mesh_net_cred cred[2];
167
168 /* Bit fields for tracking which groups the Friend knows about */
169 ATOMIC_DEFINE(added, LPN_GROUPS);
170 ATOMIC_DEFINE(pending, LPN_GROUPS);
171 ATOMIC_DEFINE(to_remove, LPN_GROUPS);
172 };
173
174 /* bt_mesh_net.flags */
175 enum {
176 BT_MESH_INIT, /* We have been initialized */
177 BT_MESH_VALID, /* We have been provisioned */
178 BT_MESH_SUSPENDED, /* Network is temporarily suspended */
179 BT_MESH_IVU_IN_PROGRESS, /* IV Update in Progress */
180 BT_MESH_IVU_INITIATOR, /* IV Update initiated by us */
181 BT_MESH_IVU_TEST, /* IV Update test mode */
182 BT_MESH_IVU_PENDING, /* Update blocked by SDU in progress */
183 BT_MESH_COMP_DIRTY, /* Composition data is dirty */
184 BT_MESH_DEVKEY_CAND, /* Has device key candidate */
185 BT_MESH_METADATA_DIRTY, /* Models metadata is dirty */
186
187 /* Feature flags */
188 BT_MESH_RELAY,
189 BT_MESH_BEACON,
190 BT_MESH_GATT_PROXY,
191 BT_MESH_FRIEND,
192 BT_MESH_PRIV_BEACON,
193 BT_MESH_PRIV_GATT_PROXY,
194 BT_MESH_OD_PRIV_PROXY,
195
196 /* Don't touch - intentionally last */
197 BT_MESH_FLAG_COUNT,
198 };
199
200 struct bt_mesh_net {
201 uint32_t iv_index; /* Current IV Index */
202 uint32_t seq; /* Next outgoing sequence number (24 bits) */
203
204 ATOMIC_DEFINE(flags, BT_MESH_FLAG_COUNT);
205
206 /* Local network interface */
207 struct k_work local_work;
208 sys_slist_t local_queue;
209
210 #if defined(CONFIG_BT_MESH_FRIEND)
211 /* Friend state, unique for each LPN that we're Friends for */
212 struct bt_mesh_friend frnd[CONFIG_BT_MESH_FRIEND_LPN_COUNT];
213 #endif
214
215 #if defined(CONFIG_BT_MESH_LOW_POWER)
216 struct bt_mesh_lpn lpn; /* Low Power Node state */
217 #endif
218
219 /* Number of hours in current IV Update state */
220 uint8_t ivu_duration;
221
222 uint8_t net_xmit;
223 uint8_t relay_xmit;
224 uint8_t default_ttl;
225
226 #if defined(CONFIG_BT_MESH_PRIV_BEACONS)
227 uint8_t priv_beacon_int;
228 #endif
229
230 /* Timer to track duration in current IV Update state */
231 struct k_work_delayable ivu_timer;
232
233 struct bt_mesh_key dev_key;
234
235 #if defined(CONFIG_BT_MESH_RPR_SRV)
236 struct bt_mesh_key dev_key_cand;
237 #endif
238 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV)
239 uint8_t on_demand_state;
240 #endif
241 struct bt_mesh_sar_tx sar_tx; /* Transport SAR Transmitter configuration */
242 struct bt_mesh_sar_rx sar_rx; /* Transport SAR Receiver configuration */
243 };
244
245 /* Network interface */
246 enum bt_mesh_net_if {
247 BT_MESH_NET_IF_ADV,
248 BT_MESH_NET_IF_LOCAL,
249 BT_MESH_NET_IF_PROXY,
250 BT_MESH_NET_IF_PROXY_CFG,
251 };
252
253 /* Decoding context for Network/Transport data */
254 struct bt_mesh_net_rx {
255 struct bt_mesh_subnet *sub;
256 struct bt_mesh_msg_ctx ctx;
257 uint32_t seq; /* Sequence Number */
258 uint8_t old_iv:1, /* iv_index - 1 was used */
259 new_key:1, /* Data was encrypted with updated key */
260 friend_cred:1, /* Data was encrypted with friend cred */
261 ctl:1, /* Network Control */
262 net_if:2, /* Network interface */
263 local_match:1, /* Matched a local element */
264 friend_match:1; /* Matched an LPN we're friends for */
265 };
266
267 /* Encoding context for Network/Transport data */
268 struct bt_mesh_net_tx {
269 struct bt_mesh_subnet *sub;
270 struct bt_mesh_msg_ctx *ctx;
271 uint16_t src;
272 uint8_t xmit;
273 uint8_t friend_cred:1,
274 aszmic:1,
275 aid:6;
276 };
277
278 extern struct bt_mesh_net bt_mesh;
279
280 #define BT_MESH_NET_IVI_TX (bt_mesh.iv_index - \
281 atomic_test_bit(bt_mesh.flags, \
282 BT_MESH_IVU_IN_PROGRESS))
283 #define BT_MESH_NET_IVI_RX(rx) (bt_mesh.iv_index - (rx)->old_iv)
284
285 #define BT_MESH_NET_HDR_LEN 9
286
287 int bt_mesh_net_create(uint16_t idx, uint8_t flags, const struct bt_mesh_key *key,
288 uint32_t iv_index);
289
290 bool bt_mesh_net_iv_update(uint32_t iv_index, bool iv_update);
291
292 int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf,
293 enum bt_mesh_nonce_type type);
294
295 int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct bt_mesh_adv *adv,
296 const struct bt_mesh_send_cb *cb, void *cb_data);
297
298 int bt_mesh_net_decode(struct net_buf_simple *in, enum bt_mesh_net_if net_if,
299 struct bt_mesh_net_rx *rx, struct net_buf_simple *out);
300
301 void bt_mesh_net_recv(struct net_buf_simple *data, int8_t rssi,
302 enum bt_mesh_net_if net_if);
303
304 void bt_mesh_net_loopback_clear(uint16_t net_idx);
305
306 uint32_t bt_mesh_next_seq(void);
307 void bt_mesh_net_seq_store(bool force);
308
309 void bt_mesh_net_init(void);
310 void bt_mesh_net_header_parse(struct net_buf_simple *buf,
311 struct bt_mesh_net_rx *rx);
312 void bt_mesh_net_pending_net_store(void);
313 void bt_mesh_net_pending_iv_store(void);
314 void bt_mesh_net_pending_seq_store(void);
315
316 void bt_mesh_net_pending_dev_key_cand_store(void);
317 void bt_mesh_net_dev_key_cand_store(void);
318
319 void bt_mesh_net_store(void);
320 void bt_mesh_net_clear(void);
321 void bt_mesh_net_settings_commit(void);
322
send_cb_finalize(const struct bt_mesh_send_cb * cb,void * cb_data)323 static inline void send_cb_finalize(const struct bt_mesh_send_cb *cb,
324 void *cb_data)
325 {
326 if (!cb) {
327 return;
328 }
329
330 if (cb->start) {
331 cb->start(0, 0, cb_data);
332 }
333
334 if (cb->end) {
335 cb->end(0, cb_data);
336 }
337 }
338