Blob: pb-adv.c
Blob id: 1b80b97ad31c80bb1ed05624c098f5813bf6a70a
Size: 12.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | // SPDX-License-Identifier: LGPL-2.1-or-later /* * * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2018-2019 Intel Corporation. All rights reserved. * * */ #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <time.h> #include <ell/ell.h> #include "src/shared/ad.h" #include "mesh/mesh-defs.h" #include "mesh/crypto.h" #include "mesh/net.h" #include "mesh/mesh-io.h" #include "mesh/mesh.h" #include "mesh/prov.h" #include "mesh/provision.h" #include "mesh/pb-adv.h" #include "mesh/util.h" struct pb_adv_session { mesh_prov_open_func_t open_cb; mesh_prov_close_func_t close_cb; mesh_prov_receive_func_t rx_cb; mesh_prov_ack_func_t ack_cb; struct l_timeout *tx_timeout; struct pb_adv_session *loop; uint32_t link_id; uint16_t exp_len; uint8_t exp_fcs; uint8_t exp_segs; uint8_t got_segs; uint8_t trans_num; uint8_t local_acked; uint8_t local_trans_num; uint8_t peer_trans_num; uint8_t last_peer_trans_num; uint8_t sar[80]; uint8_t uuid[16]; bool initiator; bool opened; void *user_data; }; #define PB_ADV_ACK 0x01 #define PB_ADV_OPEN_REQ 0x03 #define PB_ADV_OPEN_CFM 0x07 #define PB_ADV_CLOSE 0x0B #define PB_ADV_MTU 24 struct pb_ack { uint8_t ad_type; uint32_t link_id; uint8_t trans_num; uint8_t opcode; } __packed; struct pb_open_req{ uint8_t ad_type; uint32_t link_id; uint8_t trans_num; uint8_t opcode; uint8_t uuid[16]; } __packed; struct pb_open_cfm{ uint8_t ad_type; uint32_t link_id; uint8_t trans_num; uint8_t opcode; } __packed; struct pb_close_ind { uint8_t ad_type; uint32_t link_id; uint8_t trans_num; uint8_t opcode; uint8_t reason; } __packed; struct idle_rx { struct pb_adv_session *session; uint16_t len; uint8_t data[PB_ADV_MTU + 6]; }; static struct l_queue *pb_sessions = NULL; static const uint8_t filter[1] = { BT_AD_MESH_PROV }; static void pb_adv_packet(void *user_data, const uint8_t *pkt, uint16_t len); static void idle_rx_adv(void *user_data) { struct idle_rx *rx = user_data; pb_adv_packet(rx->session, rx->data, rx->len); l_free(rx); } static void pb_adv_send(struct pb_adv_session *session, uint8_t count, uint16_t interval, void *data, uint16_t len) { struct idle_rx *rx; if (session->loop) { rx = l_new(struct idle_rx, 1); rx->session = session->loop; rx->len = len; memcpy(rx->data, data, len); l_idle_oneshot(idle_rx_adv, rx, NULL); } else mesh_send_pkt(count, interval, data, len); } static void send_adv_segs(struct pb_adv_session *session, const uint8_t *data, uint16_t size) { uint16_t init_size; uint8_t buf[PB_ADV_MTU + 6] = { BT_AD_MESH_PROV }; uint8_t max_seg; uint8_t consumed; int i; if (!size) return; mesh_send_cancel(filter, sizeof(filter)); l_put_be32(session->link_id, buf + 1); buf[1 + 4] = ++session->local_trans_num; if (size > PB_ADV_MTU - 4) { max_seg = 1 + (((size - (PB_ADV_MTU - 4)) - 1) / (PB_ADV_MTU - 1)); init_size = PB_ADV_MTU - 4; } else { max_seg = 0; init_size = size; } l_debug("Sending %u fragments for %u octets", max_seg + 1, size); buf[6] = max_seg << 2; l_put_be16(size, buf + 7); buf[9] = mesh_crypto_compute_fcs(data, size); memcpy(buf + 10, data, init_size); l_debug("max_seg: %2.2x", max_seg); l_debug("size: %2.2x, CRC: %2.2x", size, buf[9]); pb_adv_send(session, MESH_IO_TX_COUNT_UNLIMITED, 500, buf, init_size + 10); consumed = init_size; for (i = 1; i <= max_seg; i++) { size_t seg_size; /* Amount of payload data being sent */ if (size - consumed > PB_ADV_MTU - 1) seg_size = PB_ADV_MTU - 1; else seg_size = size - consumed; buf[6] = (i << 2) | 0x02; memcpy(buf + 7, data + consumed, seg_size); pb_adv_send(session, MESH_IO_TX_COUNT_UNLIMITED, 500, buf, seg_size + 7); consumed += seg_size; } } static bool session_match (const void *a, const void *b) { return a == b; } static bool uuid_match (const void *a, const void *b) { const struct pb_adv_session *session = a; const uint8_t *uuid = b; return !memcmp(session->uuid, uuid, sizeof(session->uuid)); } static bool user_match (const void *a, const void *b) { const struct pb_adv_session *session = a; return session->user_data == b; } static void tx_timeout(struct l_timeout *timeout, void *user_data) { struct pb_adv_session *session = user_data; mesh_prov_close_func_t cb; if (!l_queue_find(pb_sessions, session_match, session)) return; mesh_send_cancel(filter, sizeof(filter)); l_debug("TX timeout"); cb = session->close_cb; user_data = session->user_data; cb(user_data, 1); } static void pb_adv_tx(void *user_data, const void *data, uint16_t len) { struct pb_adv_session *session = user_data; if (!l_queue_find(pb_sessions, session_match, session)) return; l_timeout_remove(session->tx_timeout); session->tx_timeout = l_timeout_create(30, tx_timeout, session, NULL); send_adv_segs(session, data, len); } static void send_open_req(struct pb_adv_session *session) { struct pb_open_req open_req = { BT_AD_MESH_PROV }; l_put_be32(session->link_id, &open_req.link_id); open_req.trans_num = 0; open_req.opcode = PB_ADV_OPEN_REQ; memcpy(open_req.uuid, session->uuid, 16); mesh_send_cancel(filter, sizeof(filter)); pb_adv_send(session, MESH_IO_TX_COUNT_UNLIMITED, 500, &open_req, sizeof(open_req)); } static void send_open_cfm(struct pb_adv_session *session) { struct pb_open_cfm open_cfm = { BT_AD_MESH_PROV }; l_put_be32(session->link_id, &open_cfm.link_id); open_cfm.trans_num = 0; open_cfm.opcode = PB_ADV_OPEN_CFM; mesh_send_cancel(filter, sizeof(filter)); pb_adv_send(session, MESH_IO_TX_COUNT_UNLIMITED, 500, &open_cfm, sizeof(open_cfm)); } static void send_ack(struct pb_adv_session *session, uint8_t trans_num) { struct pb_ack ack = { BT_AD_MESH_PROV }; if (!l_queue_find(pb_sessions, session_match, session)) return; l_put_be32(session->link_id, &ack.link_id); ack.trans_num = trans_num; ack.opcode = PB_ADV_ACK; pb_adv_send(session, MESH_IO_TX_COUNT_UNLIMITED, 500, &ack, sizeof(ack)); } static void send_close_ind(struct pb_adv_session *session, uint8_t reason) { struct pb_close_ind close_ind = { BT_AD_MESH_PROV }; if (!l_queue_find(pb_sessions, session_match, session)) return; l_put_be32(session->link_id, &close_ind.link_id); close_ind.trans_num = 0; close_ind.opcode = PB_ADV_CLOSE; close_ind.reason = reason; mesh_send_cancel(filter, sizeof(filter)); pb_adv_send(session, 10, 100, &close_ind, sizeof(close_ind)); } static void pb_adv_packet(void *user_data, const uint8_t *pkt, uint16_t len) { struct pb_adv_session *session = user_data; uint32_t link_id; size_t offset; uint8_t trans_num; uint8_t type; bool first; if (!l_queue_find(pb_sessions, session_match, session)) return; link_id = l_get_be32(pkt + 1); type = l_get_u8(pkt + 6); /* Validate new or existing Connection ID */ if (session->link_id) { if (session->link_id != link_id) return; } else if (type != 0x03) return; else if (!link_id) return; trans_num = l_get_u8(pkt + 5); pkt += 7; len -= 7; switch (type) { case PB_ADV_OPEN_CFM: /* * Ignore if: * 1. We are acceptor * 2. We are already provisioning on different link_id */ if (!session->initiator) return; first = !session->opened; session->opened = true; /* Only call Open callback once */ if (first) { l_debug("PB-ADV open confirmed"); session->local_trans_num = 0xFF; session->open_cb(session->user_data, pb_adv_tx, session, PB_ADV); } return; case PB_ADV_OPEN_REQ: /* * Ignore if: * 1. We are initiator * 2. Open request not addressed to us * 3. We are already provisioning on different link_id */ if (session->initiator) return; if (memcmp(pkt, session->uuid, 16)) return; first = !session->link_id; session->link_id = link_id; session->last_peer_trans_num = 0xFF; session->peer_trans_num = 0x00; session->local_trans_num = 0x7F; session->opened = true; /* Only call Open callback once */ if (first) { l_debug("PB-ADV open requested"); session->open_cb(session->user_data, pb_adv_tx, session, PB_ADV); } /* Send CFM once per received request */ send_open_cfm(session); break; case PB_ADV_CLOSE: l_debug("Link closed notification: %2.2x", pkt[0]); session->close_cb(session->user_data, pkt[0]); break; case PB_ADV_ACK: if (!session->opened) return; if (trans_num != session->local_trans_num) return; if (session->local_acked > trans_num) return; mesh_send_cancel(filter, sizeof(filter)); session->local_acked = trans_num; session->ack_cb(session->user_data, trans_num); break; default: /* DATA SEGMENT */ if (!session->opened) return; if (trans_num == session->last_peer_trans_num) { send_ack(session, trans_num); return; } switch(type & 0x03) { case 0x00: session->peer_trans_num = trans_num; session->exp_len = l_get_be16(pkt); l_debug("PB-ADV start with %u fragments, %d octets", type >> 2, session->exp_len); if (session->exp_len > sizeof(session->sar)) { l_debug("Incoming length exceeded: %d", session->exp_len); return; } session->exp_fcs = l_get_u8(pkt + 2); session->exp_segs = 0xff >> (7 - (type >> 2)); /* Save first segment */ memcpy(session->sar, pkt + 3, len - 3); session->got_segs |= 1; break; case 0x02: session->peer_trans_num = trans_num; offset = 20 + (((type >> 2) - 1) * 23); if (offset + len - 3 > sizeof(session->sar)) { l_debug("Length exceeded: %d", session->exp_len); return; } l_debug("Processing fragment %u", type >> 2); memcpy(session->sar + offset, pkt, len); session->got_segs |= 1 << (type >> 2); break; default: /* Malformed or unrecognized */ return; } if (session->got_segs != session->exp_segs) return; /* Validate RXed packet and pass up to Provisioning */ if (!mesh_crypto_check_fcs(session->sar, session->exp_len, session->exp_fcs)) { /* This can be a false negative if first * segment missed, and can almost always * be ignored. */ l_debug("Invalid FCS"); return; } send_ack(session, session->peer_trans_num); if (session->last_peer_trans_num != session->peer_trans_num) { session->got_segs = 0; session->last_peer_trans_num = session->peer_trans_num; session->rx_cb(session->user_data, session->sar, session->exp_len); } } } bool pb_adv_reg(bool initiator, mesh_prov_open_func_t open_cb, mesh_prov_close_func_t close_cb, mesh_prov_receive_func_t rx_cb, mesh_prov_ack_func_t ack_cb, const uint8_t *uuid, void *user_data) { struct pb_adv_session *session, *old_session; if (!pb_sessions) pb_sessions = l_queue_new(); old_session = l_queue_find(pb_sessions, uuid_match, uuid); /* Reject 2nd session if not looping back */ if (l_queue_length(pb_sessions) && !old_session) return false; /* Reject looping to more than one session or with same role*/ if (old_session && (old_session->loop || old_session->initiator == initiator)) return false; session = l_new(struct pb_adv_session, 1); session->open_cb = open_cb; session->close_cb = close_cb; session->rx_cb = rx_cb; session->ack_cb = ack_cb; session->user_data = user_data; session->initiator = initiator; memcpy(session->uuid, uuid, 16); l_queue_push_head(pb_sessions, session); if (initiator) { l_getrandom(&session->link_id, sizeof(session->link_id)); session->tx_timeout = l_timeout_create(60, tx_timeout, session, NULL); } /* Setup Loop-back if complementary session with same UUID */ if (old_session) { session->loop = old_session; old_session->loop = session; mesh_unreg_prov_rx(pb_adv_packet); if (initiator) send_open_req(session); else send_open_req(old_session); return true; } mesh_reg_prov_rx(pb_adv_packet, session); if (initiator) send_open_req(session); return true; } void pb_adv_unreg(void *user_data) { struct pb_adv_session *session = l_queue_find(pb_sessions, user_match, user_data); if (!session) return; l_timeout_remove(session->tx_timeout); session->tx_timeout = NULL; send_close_ind(session, 0); l_queue_remove(pb_sessions, session); l_free(session); if (!l_queue_length(pb_sessions)) { l_queue_destroy(pb_sessions, l_free); pb_sessions = NULL; } } |