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