1 /*  Bluetooth Mesh */
2 
3 /*
4  * Copyright (c) 2017 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <ble_os.h>
10 #include <bt_errno.h>
11 #include <misc/util.h>
12 
13 #include <net/buf.h>
14 #include <bluetooth/bluetooth.h>
15 #include <bluetooth/conn.h>
16 #include <api/mesh.h>
17 
18 #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_MESH_DEBUG_BEACON)
19 #include "common/log.h"
20 
21 #include "adv.h"
22 #include "mesh.h"
23 #include "net.h"
24 #include "prov.h"
25 #include "crypto.h"
26 #include "beacon.h"
27 #include "foundation.h"
28 
29 #define UNPROVISIONED_INTERVAL K_SECONDS(5)
30 #define PROVISIONED_INTERVAL K_SECONDS(10)
31 
32 /* 3 transmissions, 20ms interval */
33 #define UNPROV_XMIT BT_MESH_TRANSMIT(2, 20)
34 
35 /* 1 transmission, 20ms interval */
36 #define PROV_XMIT BT_MESH_TRANSMIT(0, 20)
37 
38 static struct k_delayed_work beacon_timer;
39 
40 static bt_u32_t unprov_beacon_interval = UNPROVISIONED_INTERVAL;
41 static bt_u32_t prov_beacon_interval = PROVISIONED_INTERVAL;
42 
cache_check(u8_t data[21])43 static struct bt_mesh_subnet *cache_check(u8_t data[21])
44 {
45     int i;
46 
47     for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
48         struct bt_mesh_subnet *sub = &bt_mesh.sub[i];
49 
50         if (sub->net_idx == BT_MESH_KEY_UNUSED) {
51             continue;
52         }
53 
54         if (!memcmp(sub->beacon_cache, data, 21)) {
55             return sub;
56         }
57     }
58 
59     return NULL;
60 }
61 
cache_add(u8_t data[21],struct bt_mesh_subnet * sub)62 static void cache_add(u8_t data[21], struct bt_mesh_subnet *sub)
63 {
64     memcpy(sub->beacon_cache, data, 21);
65 }
66 
beacon_complete(int err,void * user_data)67 static void beacon_complete(int err, void *user_data)
68 {
69     struct bt_mesh_subnet *sub = user_data;
70 
71     BT_DBG("err %d", err);
72 
73     sub->beacon_sent = k_uptime_get_32();
74 }
75 
unprov_beacon_interval_get()76 uint32_t unprov_beacon_interval_get()
77 {
78     return unprov_beacon_interval;
79 }
80 
genie_set_silent_unprov_beacon_interval(bool is_silent)81 void genie_set_silent_unprov_beacon_interval(bool is_silent)
82 {
83     if (is_silent) {
84         unprov_beacon_interval = K_SECONDS(60);
85     } else {
86         unprov_beacon_interval = K_SECONDS(5);
87     }
88 
89     return;
90 }
91 
bt_mesh_beacon_create(struct bt_mesh_subnet * sub,struct net_buf_simple * buf)92 void bt_mesh_beacon_create(struct bt_mesh_subnet *sub, struct net_buf_simple *buf)
93 {
94     u8_t flags = bt_mesh_net_flags(sub);
95     struct bt_mesh_subnet_keys *keys;
96 
97     net_buf_simple_add_u8(buf, BEACON_TYPE_SECURE);
98 
99     if (sub->kr_flag) {
100         keys = &sub->keys[1];
101     } else {
102         keys = &sub->keys[0];
103     }
104 
105     net_buf_simple_add_u8(buf, flags);
106 
107     /* Network ID */
108     net_buf_simple_add_mem(buf, keys->net_id, 8);
109 
110     /* IV Index */
111     net_buf_simple_add_be32(buf, bt_mesh.iv_index);
112 
113     net_buf_simple_add_mem(buf, sub->auth, 8);
114 
115     BT_DBG("net_idx 0x%04x flags 0x%02x NetID %s", sub->net_idx, flags, bt_hex(keys->net_id, 8));
116     BT_DBG("IV Index 0x%08x Auth %s", bt_mesh.iv_index, bt_hex(sub->auth, 8));
117 }
118 
119 /* If the interval has passed or is within 5 seconds from now send a beacon */
120 #define BEACON_THRESHOLD(sub) (K_SECONDS(10 * ((sub)->beacons_last + 1)) - K_SECONDS(5))
121 
secure_beacon_send(void)122 static int secure_beacon_send(void)
123 {
124     static const struct bt_mesh_send_cb send_cb = {
125         .end = beacon_complete,
126     };
127 #ifndef CONFIG_BT_BQB
128     bt_u32_t now = k_uptime_get_32();
129     bt_u32_t time_diff;
130 #endif
131 
132     int i;
133 
134     BT_DBG("");
135 
136     for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
137         struct bt_mesh_subnet *sub = &bt_mesh.sub[i];
138         struct net_buf *buf;
139 
140         if (sub->net_idx == BT_MESH_KEY_UNUSED) {
141             continue;
142         }
143 
144 #ifndef CONFIG_BT_BQB
145         time_diff = now - sub->beacon_sent;
146         /*[Genie begin] add by lgy at 2021-01-19*/
147         if (time_diff < K_SECONDS(600)) {
148 #ifdef CONFIG_GENIE_MESH_GLP
149             if (time_diff < K_SECONDS(30))
150 #else
151             if (time_diff < BEACON_THRESHOLD(sub))
152 #endif
153             {
154                 continue;
155             }
156         }
157 /*[Genie end] add by lgy at 2021-01-19*/
158 #endif
159 
160         buf = bt_mesh_adv_create(BT_MESH_ADV_BEACON, PROV_XMIT, K_NO_WAIT);
161         if (!buf) {
162             BT_ERR("Unable to allocate beacon buffer");
163             return -ENOBUFS;
164         }
165 
166         bt_mesh_beacon_create(sub, &buf->b);
167 
168         bt_mesh_adv_send(buf, &send_cb, sub);
169         net_buf_unref(buf);
170     }
171 
172     return 0;
173 }
174 
unprovisioned_beacon_send(void)175 static int unprovisioned_beacon_send(void)
176 {
177 #if defined(CONFIG_BT_MESH_PB_ADV)
178     const struct bt_mesh_prov *prov;
179     u8_t uri_hash[16] = { 0 };
180     struct net_buf *buf;
181     u16_t oob_info;
182 
183     BT_DBG("");
184 
185     buf = bt_mesh_adv_create(BT_MESH_ADV_BEACON, UNPROV_XMIT, K_NO_WAIT);
186     if (!buf) {
187         BT_ERR("Unable to allocate beacon buffer");
188         return -ENOBUFS;
189     }
190 
191     prov = bt_mesh_prov_get();
192 
193     net_buf_add_u8(buf, BEACON_TYPE_UNPROVISIONED);
194     net_buf_add_mem(buf, prov->uuid, 16);
195 
196     if (prov->uri && bt_mesh_s1(prov->uri, uri_hash) == 0) {
197         oob_info = prov->oob_info | BT_MESH_PROV_OOB_URI;
198     } else {
199         oob_info = prov->oob_info;
200     }
201 
202     net_buf_add_be16(buf, oob_info);
203     net_buf_add_mem(buf, uri_hash, 4);
204 
205     bt_mesh_adv_send(buf, NULL, NULL);
206     net_buf_unref(buf);
207 
208     if (prov->uri) {
209         size_t len;
210 
211         buf = bt_mesh_adv_create(BT_MESH_ADV_URI, UNPROV_XMIT, K_NO_WAIT);
212         if (!buf) {
213             BT_ERR("Unable to allocate URI buffer");
214             return -ENOBUFS;
215         }
216 
217         len = strlen(prov->uri);
218         if (net_buf_tailroom(buf) < len) {
219             BT_WARN("Too long URI to fit advertising data");
220         } else {
221             net_buf_add_mem(buf, prov->uri, len);
222             bt_mesh_adv_send(buf, NULL, NULL);
223         }
224 
225         net_buf_unref(buf);
226     }
227 
228 #endif /* CONFIG_BT_MESH_PB_ADV */
229     return 0;
230 }
231 
update_beacon_observation(void)232 static void update_beacon_observation(void)
233 {
234     static bool first_half;
235     int i;
236 
237     /* Observation period is 20 seconds, whereas the beacon timer
238 	 * runs every 10 seconds. We process what's happened during the
239 	 * window only after the seconnd half.
240 	 */
241     first_half = !first_half;
242     if (first_half) {
243         return;
244     }
245 
246     for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
247         struct bt_mesh_subnet *sub = &bt_mesh.sub[i];
248 
249         if (sub->net_idx == BT_MESH_KEY_UNUSED) {
250             continue;
251         }
252 
253         sub->beacons_last = sub->beacons_cur;
254         sub->beacons_cur = 0;
255     }
256 }
257 
beacon_send(struct k_work * work)258 static void beacon_send(struct k_work *work)
259 {
260     /* Don't send anything if we have an active provisioning link */
261     if (IS_ENABLED(CONFIG_BT_MESH_PROV) && bt_prov_active()) {
262         k_delayed_work_submit(&beacon_timer, bt_mesh_get_beacon_interval(BEACON_TYPE_UNPROVISIONED));
263         return;
264     }
265 
266     BT_DBG("");
267 
268     if (bt_mesh_is_provisioned()) {
269         update_beacon_observation();
270         secure_beacon_send();
271 
272         /* Only resubmit if beaconing is still enabled */
273         if (bt_mesh_beacon_get() == BT_MESH_BEACON_ENABLED || atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR)) {
274             k_delayed_work_submit(&beacon_timer, bt_mesh_get_beacon_interval(BEACON_TYPE_SECURE));
275         }
276     } else {
277         unprovisioned_beacon_send();
278         k_delayed_work_submit(&beacon_timer, bt_mesh_get_beacon_interval(BEACON_TYPE_UNPROVISIONED));
279     }
280 }
281 
secure_beacon_recv(struct net_buf_simple * buf)282 static void secure_beacon_recv(struct net_buf_simple *buf)
283 {
284     u8_t *data, *net_id, *auth;
285     struct bt_mesh_subnet *sub;
286     bt_u32_t iv_index;
287     bool new_key, kr_change, iv_change;
288     u8_t flags;
289 
290     if (buf->len < 21) {
291         BT_ERR("Too short secure beacon (len %u)", buf->len);
292         return;
293     }
294 
295     sub = cache_check(buf->data);
296     if (sub) {
297         /* We've seen this beacon before - just update the stats */
298         goto update_stats;
299     }
300 
301     /* So we can add to the cache if auth matches */
302     data = buf->data;
303 
304     flags = net_buf_simple_pull_u8(buf);
305     net_id = net_buf_simple_pull_mem(buf, 8);
306     iv_index = net_buf_simple_pull_be32(buf);
307     auth = buf->data;
308 
309     BT_DBG("flags 0x%02x id %s iv_index 0x%08x", flags, bt_hex(net_id, 8), iv_index);
310 
311     sub = bt_mesh_subnet_find(net_id, flags, iv_index, auth, &new_key);
312     if (!sub) {
313         BT_DBG("No subnet that matched beacon");
314         return;
315     }
316 
317     if (sub->kr_phase == BT_MESH_KR_PHASE_2 && !new_key) {
318         BT_WARN("Ignoring Phase 2 KR Update secured using old key");
319         return;
320     }
321 
322     cache_add(data, sub);
323 
324     /* If we have NetKey0 accept initiation only from it */
325     if (bt_mesh_subnet_get(BT_MESH_KEY_PRIMARY) && sub->net_idx != BT_MESH_KEY_PRIMARY) {
326         BT_WARN("Ignoring secure beacon on non-primary subnet");
327         goto update_stats;
328     }
329 
330     BT_DBG("net_idx 0x%04x iv_index 0x%08x, current iv_index 0x%08x", sub->net_idx, iv_index, bt_mesh.iv_index);
331 
332     if (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR) &&
333         (atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_IN_PROGRESS) == BT_MESH_IV_UPDATE(flags))) {
334         bt_mesh_beacon_ivu_initiator(false);
335     }
336 
337     iv_change = bt_mesh_net_iv_update(iv_index, BT_MESH_IV_UPDATE(flags));
338 
339     kr_change = bt_mesh_kr_update(sub, BT_MESH_KEY_REFRESH(flags), new_key);
340     if (kr_change) {
341         bt_mesh_net_beacon_update(sub);
342     }
343 
344     if (iv_change) {
345         /* Update all subnets */
346         bt_mesh_net_sec_update(NULL);
347     } else if (kr_change) {
348         /* Key Refresh without IV Update only impacts one subnet */
349         bt_mesh_net_sec_update(sub);
350     }
351 
352 update_stats:
353     if (bt_mesh_beacon_get() == BT_MESH_BEACON_ENABLED && sub->beacons_cur < 0xff) {
354         sub->beacons_cur++;
355     }
356 }
357 
bt_mesh_get_beacon_interval(u8_t type)358 bt_u32_t bt_mesh_get_beacon_interval(u8_t type)
359 {
360     switch (type) {
361     case BEACON_TYPE_UNPROVISIONED:
362         return unprov_beacon_interval;
363     case BEACON_TYPE_SECURE:
364         return prov_beacon_interval;
365     default:
366         return 0;
367     }
368 }
369 
bt_mesh_set_beacon_interval(u8_t type,bt_u32_t delay)370 void bt_mesh_set_beacon_interval(u8_t type, bt_u32_t delay)
371 {
372     switch (type) {
373     case BEACON_TYPE_UNPROVISIONED:
374         unprov_beacon_interval = delay;
375         return;
376     case BEACON_TYPE_SECURE:
377         prov_beacon_interval = delay;
378         return;
379     default:
380         return;
381     }
382 }
383 
bt_mesh_beacon_recv(struct net_buf_simple * buf)384 void bt_mesh_beacon_recv(struct net_buf_simple *buf)
385 {
386     u8_t type;
387 
388     BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len));
389 
390     if (buf->len < 1) {
391         BT_ERR("Too short beacon");
392         return;
393     }
394 
395     type = net_buf_simple_pull_u8(buf);
396     switch (type) {
397     case BEACON_TYPE_UNPROVISIONED:
398         BT_DBG("Ignoring unprovisioned device beacon");
399         break;
400     case BEACON_TYPE_SECURE:
401         secure_beacon_recv(buf);
402         break;
403     default:
404         BT_WARN("Unknown beacon type 0x%02x", type);
405         break;
406     }
407 }
408 
bt_mesh_beacon_init(void)409 void bt_mesh_beacon_init(void)
410 {
411     BT_INFO("enter %s\n", __func__);
412     k_delayed_work_init(&beacon_timer, beacon_send);
413 }
414 
bt_mesh_beacon_ivu_initiator(bool enable)415 void bt_mesh_beacon_ivu_initiator(bool enable)
416 {
417     atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_INITIATOR, enable);
418 
419     if (enable) {
420         k_work_submit(&beacon_timer.work);
421     } else if (bt_mesh_beacon_get() == BT_MESH_BEACON_DISABLED) {
422         k_delayed_work_cancel(&beacon_timer);
423     }
424 }
425 
bt_mesh_beacon_enable(void)426 void bt_mesh_beacon_enable(void)
427 {
428     int i;
429 
430     if (!bt_mesh_is_provisioned()) {
431         k_work_submit(&beacon_timer.work);
432         return;
433     }
434 
435     for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
436         struct bt_mesh_subnet *sub = &bt_mesh.sub[i];
437 
438         if (sub->net_idx == BT_MESH_KEY_UNUSED) {
439             continue;
440         }
441 
442         sub->beacons_last = 0;
443         sub->beacons_cur = 0;
444 
445         bt_mesh_net_beacon_update(sub);
446     }
447 
448     k_work_submit(&beacon_timer.work);
449 }
450 
bt_mesh_beacon_disable(void)451 void bt_mesh_beacon_disable(void)
452 {
453     if (!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR)) {
454         k_delayed_work_cancel(&beacon_timer);
455     }
456 }
457