Blob: att.c
Blob id: 77ca4aa249065f05d56dbc047b6d69641d1c5c8c
Size: 43.5 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 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 | // SPDX-License-Identifier: LGPL-2.1-or-later /* * * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2014 Google Inc. * * */ #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <stdlib.h> #include <unistd.h> #include <errno.h> #include "src/shared/io.h" #include "src/shared/queue.h" #include "src/shared/util.h" #include "src/shared/timeout.h" #include "bluetooth/bluetooth.h" #include "bluetooth/l2cap.h" #include "bluetooth/uuid.h" #include "src/shared/att.h" #include "src/shared/crypto.h" #define ATT_MIN_PDU_LEN 1 /* At least 1 byte for the opcode. */ #define ATT_OP_CMD_MASK 0x40 #define ATT_OP_SIGNED_MASK 0x80 #define ATT_TIMEOUT_INTERVAL 30000 /* 30000 ms */ /* Length of signature in write signed packet */ #define BT_ATT_SIGNATURE_LEN 12 struct att_send_op; struct bt_att_chan { struct bt_att *att; int fd; struct io *io; uint8_t type; int sec_level; /* Only used for non-L2CAP */ struct queue *queue; /* Channel dedicated queue */ struct att_send_op *pending_req; struct att_send_op *pending_ind; bool writer_active; bool in_req; /* There's a pending incoming request */ uint8_t *buf; uint16_t mtu; }; struct bt_att { int ref_count; bool close_on_unref; struct queue *chans; uint8_t enc_size; uint16_t mtu; /* Biggest possible MTU */ struct queue *notify_list; /* List of registered callbacks */ struct queue *disconn_list; /* List of disconnect handlers */ struct queue *exchange_list; /* List of MTU changed handlers */ unsigned int next_send_id; /* IDs for "send" ops */ unsigned int next_reg_id; /* IDs for registered callbacks */ struct queue *req_queue; /* Queued ATT protocol requests */ struct queue *ind_queue; /* Queued ATT protocol indications */ struct queue *write_queue; /* Queue of PDUs ready to send */ bool in_disc; /* Cleanup queues on disconnect_cb */ bt_att_timeout_func_t timeout_callback; bt_att_destroy_func_t timeout_destroy; void *timeout_data; uint8_t debug_level; bt_att_debug_func_t debug_callback; bt_att_destroy_func_t debug_destroy; void *debug_data; struct bt_crypto *crypto; struct sign_info *local_sign; struct sign_info *remote_sign; }; struct sign_info { uint8_t key[16]; bt_att_counter_func_t counter; void *user_data; }; enum att_op_type { ATT_OP_TYPE_REQ, ATT_OP_TYPE_RSP, ATT_OP_TYPE_CMD, ATT_OP_TYPE_IND, ATT_OP_TYPE_NFY, ATT_OP_TYPE_CONF, ATT_OP_TYPE_UNKNOWN, }; static const struct { uint8_t opcode; enum att_op_type type; } att_opcode_type_table[] = { { BT_ATT_OP_ERROR_RSP, ATT_OP_TYPE_RSP }, { BT_ATT_OP_MTU_REQ, ATT_OP_TYPE_REQ }, { BT_ATT_OP_MTU_RSP, ATT_OP_TYPE_RSP }, { BT_ATT_OP_FIND_INFO_REQ, ATT_OP_TYPE_REQ }, { BT_ATT_OP_FIND_INFO_RSP, ATT_OP_TYPE_RSP }, { BT_ATT_OP_FIND_BY_TYPE_REQ, ATT_OP_TYPE_REQ }, { BT_ATT_OP_FIND_BY_TYPE_RSP, ATT_OP_TYPE_RSP }, { BT_ATT_OP_READ_BY_TYPE_REQ, ATT_OP_TYPE_REQ }, { BT_ATT_OP_READ_BY_TYPE_RSP, ATT_OP_TYPE_RSP }, { BT_ATT_OP_READ_REQ, ATT_OP_TYPE_REQ }, { BT_ATT_OP_READ_RSP, ATT_OP_TYPE_RSP }, { BT_ATT_OP_READ_BLOB_REQ, ATT_OP_TYPE_REQ }, { BT_ATT_OP_READ_BLOB_RSP, ATT_OP_TYPE_RSP }, { BT_ATT_OP_READ_MULT_REQ, ATT_OP_TYPE_REQ }, { BT_ATT_OP_READ_MULT_RSP, ATT_OP_TYPE_RSP }, { BT_ATT_OP_READ_BY_GRP_TYPE_REQ, ATT_OP_TYPE_REQ }, { BT_ATT_OP_READ_BY_GRP_TYPE_RSP, ATT_OP_TYPE_RSP }, { BT_ATT_OP_WRITE_REQ, ATT_OP_TYPE_REQ }, { BT_ATT_OP_WRITE_RSP, ATT_OP_TYPE_RSP }, { BT_ATT_OP_WRITE_CMD, ATT_OP_TYPE_CMD }, { BT_ATT_OP_SIGNED_WRITE_CMD, ATT_OP_TYPE_CMD }, { BT_ATT_OP_PREP_WRITE_REQ, ATT_OP_TYPE_REQ }, { BT_ATT_OP_PREP_WRITE_RSP, ATT_OP_TYPE_RSP }, { BT_ATT_OP_EXEC_WRITE_REQ, ATT_OP_TYPE_REQ }, { BT_ATT_OP_EXEC_WRITE_RSP, ATT_OP_TYPE_RSP }, { BT_ATT_OP_HANDLE_NFY, ATT_OP_TYPE_NFY }, { BT_ATT_OP_HANDLE_NFY_MULT, ATT_OP_TYPE_NFY }, { BT_ATT_OP_HANDLE_IND, ATT_OP_TYPE_IND }, { BT_ATT_OP_HANDLE_CONF, ATT_OP_TYPE_CONF }, { } }; static enum att_op_type get_op_type(uint8_t opcode) { int i; for (i = 0; att_opcode_type_table[i].opcode; i++) { if (att_opcode_type_table[i].opcode == opcode) return att_opcode_type_table[i].type; } if (opcode & ATT_OP_CMD_MASK) return ATT_OP_TYPE_CMD; return ATT_OP_TYPE_UNKNOWN; } static const struct { uint8_t req_opcode; uint8_t rsp_opcode; } att_req_rsp_mapping_table[] = { { BT_ATT_OP_MTU_REQ, BT_ATT_OP_MTU_RSP }, { BT_ATT_OP_FIND_INFO_REQ, BT_ATT_OP_FIND_INFO_RSP}, { BT_ATT_OP_FIND_BY_TYPE_REQ, BT_ATT_OP_FIND_BY_TYPE_RSP }, { BT_ATT_OP_READ_BY_TYPE_REQ, BT_ATT_OP_READ_BY_TYPE_RSP }, { BT_ATT_OP_READ_REQ, BT_ATT_OP_READ_RSP }, { BT_ATT_OP_READ_BLOB_REQ, BT_ATT_OP_READ_BLOB_RSP }, { BT_ATT_OP_READ_MULT_REQ, BT_ATT_OP_READ_MULT_RSP }, { BT_ATT_OP_READ_BY_GRP_TYPE_REQ, BT_ATT_OP_READ_BY_GRP_TYPE_RSP }, { BT_ATT_OP_WRITE_REQ, BT_ATT_OP_WRITE_RSP }, { BT_ATT_OP_PREP_WRITE_REQ, BT_ATT_OP_PREP_WRITE_RSP }, { BT_ATT_OP_EXEC_WRITE_REQ, BT_ATT_OP_EXEC_WRITE_RSP }, { } }; static uint8_t get_req_opcode(uint8_t rsp_opcode) { int i; for (i = 0; att_req_rsp_mapping_table[i].rsp_opcode; i++) { if (att_req_rsp_mapping_table[i].rsp_opcode == rsp_opcode) return att_req_rsp_mapping_table[i].req_opcode; } return 0; } struct att_send_op { unsigned int id; unsigned int timeout_id; enum att_op_type type; uint8_t opcode; void *pdu; uint16_t len; bool retry; bt_att_response_func_t callback; bt_att_destroy_func_t destroy; void *user_data; }; static void destroy_att_send_op(void *data) { struct att_send_op *op = data; if (op->timeout_id) timeout_remove(op->timeout_id); if (op->destroy) op->destroy(op->user_data); free(op->pdu); free(op); } static void cancel_att_send_op(void *data) { struct att_send_op *op = data; if (op->destroy) op->destroy(op->user_data); op->user_data = NULL; op->callback = NULL; op->destroy = NULL; } struct att_notify { unsigned int id; uint16_t opcode; bt_att_notify_func_t callback; bt_att_destroy_func_t destroy; void *user_data; }; static void destroy_att_notify(void *data) { struct att_notify *notify = data; if (notify->destroy) notify->destroy(notify->user_data); free(notify); } static bool match_notify_id(const void *a, const void *b) { const struct att_notify *notify = a; unsigned int id = PTR_TO_UINT(b); return notify->id == id; } struct att_disconn { unsigned int id; bool removed; bt_att_disconnect_func_t callback; bt_att_destroy_func_t destroy; void *user_data; }; struct att_exchange { unsigned int id; bool removed; bt_att_exchange_func_t callback; bt_att_destroy_func_t destroy; void *user_data; }; static void destroy_att_disconn(void *data) { struct att_disconn *disconn = data; if (disconn->destroy) disconn->destroy(disconn->user_data); free(disconn); } static void destroy_att_exchange(void *data) { struct att_exchange *exchange = data; if (exchange->destroy) exchange->destroy(exchange->user_data); free(exchange); } static bool match_disconn_id(const void *a, const void *b) { const struct att_disconn *disconn = a; unsigned int id = PTR_TO_UINT(b); return disconn->id == id; } static void att_log(struct bt_att *att, uint8_t level, const char *format, ...) { va_list va; if (att->debug_level < level) return; va_start(va, format); util_debug_va(att->debug_callback, att->debug_data, format, va); va_end(va); } #define DBG(_att, _format, _arg...) \ att_log(_att, BT_ATT_DEBUG, "%s:%s() " _format, __FILE__, __func__,\ ## _arg) #define VERBOSE(_att, _format, _arg...) \ att_log(_att, BT_ATT_DEBUG_VERBOSE, "%s:%s() " _format, __FILE__, \ __func__, ## _arg) static void att_hexdump(struct bt_att *att, char dir, const void *data, size_t len) { if (att->debug_level < 2) return; util_hexdump(dir, data, len, att->debug_callback, att->debug_data); } static bool encode_pdu(struct bt_att *att, struct att_send_op *op, const void *pdu, uint16_t length) { uint16_t pdu_len = 1; struct sign_info *sign = att->local_sign; uint32_t sign_cnt; if (sign && (op->opcode & ATT_OP_SIGNED_MASK)) pdu_len += BT_ATT_SIGNATURE_LEN; if (length && pdu) pdu_len += length; if (pdu_len > att->mtu) return false; op->len = pdu_len; op->pdu = malloc(op->len); if (!op->pdu) return false; ((uint8_t *) op->pdu)[0] = op->opcode; if (pdu_len > 1) memcpy(op->pdu + 1, pdu, length); if (!sign || !(op->opcode & ATT_OP_SIGNED_MASK) || !att->crypto) return true; if (!sign->counter(&sign_cnt, sign->user_data)) goto fail; if ((bt_crypto_sign_att(att->crypto, sign->key, op->pdu, 1 + length, sign_cnt, &((uint8_t *) op->pdu)[1 + length]))) return true; DBG(att, "ATT unable to generate signature"); fail: free(op->pdu); return false; } static struct att_send_op *create_att_send_op(struct bt_att *att, uint8_t opcode, const void *pdu, uint16_t length, bt_att_response_func_t callback, void *user_data, bt_att_destroy_func_t destroy) { struct att_send_op *op; enum att_op_type type; if (length && !pdu) return NULL; type = get_op_type(opcode); if (type == ATT_OP_TYPE_UNKNOWN) return NULL; /* If the opcode corresponds to an operation type that does not elicit a * response from the remote end, then no callback should have been * provided, since it will never be called. */ if (callback && type != ATT_OP_TYPE_REQ && type != ATT_OP_TYPE_IND) return NULL; /* Similarly, if the operation does elicit a response then a callback * must be provided. */ if (!callback && (type == ATT_OP_TYPE_REQ || type == ATT_OP_TYPE_IND)) return NULL; op = new0(struct att_send_op, 1); op->type = type; op->opcode = opcode; op->callback = callback; op->destroy = destroy; op->user_data = user_data; if (!encode_pdu(att, op, pdu, length)) { free(op); return NULL; } return op; } static struct att_send_op *pick_next_send_op(struct bt_att_chan *chan) { struct bt_att *att = chan->att; struct att_send_op *op; /* Check if there is anything queued on the channel */ op = queue_pop_head(chan->queue); if (op) return op; /* See if any operations are already in the write queue */ op = queue_peek_head(att->write_queue); if (op && op->len <= chan->mtu) return queue_pop_head(att->write_queue); /* If there is no pending request, pick an operation from the * request queue. */ if (!chan->pending_req) { op = queue_peek_head(att->req_queue); if (op && op->len <= chan->mtu) { /* Don't send Exchange MTU over EATT */ if (op->opcode == BT_ATT_OP_MTU_REQ && chan->type == BT_ATT_EATT) goto indicate; return queue_pop_head(att->req_queue); } } indicate: /* There is either a request pending or no requests queued. If there is * no pending indication, pick an operation from the indication queue. */ if (!chan->pending_ind) { op = queue_peek_head(att->ind_queue); if (op && op->len <= chan->mtu) return queue_pop_head(att->ind_queue); } return NULL; } static void disc_att_send_op(void *data) { struct att_send_op *op = data; if (op->callback) op->callback(BT_ATT_OP_ERROR_RSP, NULL, 0, op->user_data); destroy_att_send_op(op); } struct timeout_data { struct bt_att_chan *chan; unsigned int id; }; static bool timeout_cb(void *user_data) { struct timeout_data *timeout = user_data; struct bt_att_chan *chan = timeout->chan; struct bt_att *att = chan->att; struct att_send_op *op = NULL; if (chan->pending_req && chan->pending_req->id == timeout->id) { op = chan->pending_req; chan->pending_req = NULL; } else if (chan->pending_ind && chan->pending_ind->id == timeout->id) { op = chan->pending_ind; chan->pending_ind = NULL; } if (!op) return false; DBG(att, "(chan %p) Operation timed out: 0x%02x", chan, op->opcode); if (att->timeout_callback) att->timeout_callback(op->id, op->opcode, att->timeout_data); op->timeout_id = 0; disc_att_send_op(op); /* * Directly terminate the connection as required by the ATT protocol. * This should trigger an io disconnect event which will clean up the * io and notify the upper layer. */ io_shutdown(chan->io); return false; } static void write_watch_destroy(void *user_data) { struct bt_att_chan *chan = user_data; chan->writer_active = false; } static ssize_t bt_att_chan_write(struct bt_att_chan *chan, uint8_t opcode, const void *pdu, uint16_t len) { struct bt_att *att = chan->att; ssize_t ret; struct iovec iov; iov.iov_base = (void *) pdu; iov.iov_len = len; VERBOSE(att, "(chan %p) ATT op 0x%02x", chan, opcode); ret = io_send(chan->io, &iov, 1); if (ret < 0) { DBG(att, "(chan %p) write failed: %s", chan, strerror(-ret)); return ret; } if (att->debug_level) util_hexdump('<', pdu, ret, att->debug_callback, att->debug_data); return ret; } static bool can_write_data(struct io *io, void *user_data) { struct bt_att_chan *chan = user_data; struct att_send_op *op; struct timeout_data *timeout; op = pick_next_send_op(chan); if (!op) return false; if (bt_att_chan_write(chan, op->opcode, op->pdu, op->len) < 0) { if (op->callback) op->callback(BT_ATT_OP_ERROR_RSP, NULL, 0, op->user_data); destroy_att_send_op(op); return true; } /* Based on the operation type, set either the pending request or the * pending indication. If it came from the write queue, then there is * no need to keep it around. */ switch (op->type) { case ATT_OP_TYPE_REQ: chan->pending_req = op; break; case ATT_OP_TYPE_IND: chan->pending_ind = op; break; case ATT_OP_TYPE_RSP: /* Set in_req to false to indicate that no request is pending */ chan->in_req = false; /* fall through */ case ATT_OP_TYPE_CMD: case ATT_OP_TYPE_NFY: case ATT_OP_TYPE_CONF: case ATT_OP_TYPE_UNKNOWN: default: destroy_att_send_op(op); return true; } timeout = new0(struct timeout_data, 1); timeout->chan = chan; timeout->id = op->id; op->timeout_id = timeout_add(ATT_TIMEOUT_INTERVAL, timeout_cb, timeout, free); /* Return true as there may be more operations ready to write. */ return true; } static void wakeup_chan_writer(void *data, void *user_data) { struct bt_att_chan *chan = data; struct bt_att *att = chan->att; if (chan->writer_active) return; /* Set the write handler only if there is anything that can be sent * at all. */ if (queue_isempty(chan->queue) && queue_isempty(att->write_queue)) { if ((chan->pending_req || queue_isempty(att->req_queue)) && (chan->pending_ind || queue_isempty(att->ind_queue))) return; } if (!io_set_write_handler(chan->io, can_write_data, chan, write_watch_destroy)) return; chan->writer_active = true; } static void wakeup_writer(struct bt_att *att) { queue_foreach(att->chans, wakeup_chan_writer, NULL); } static void disconn_handler(void *data, void *user_data) { struct att_disconn *disconn = data; int err = PTR_TO_INT(user_data); if (disconn->removed) return; if (disconn->callback) disconn->callback(err, disconn->user_data); } static void bt_att_chan_free(void *data) { struct bt_att_chan *chan = data; if (chan->pending_req) destroy_att_send_op(chan->pending_req); if (chan->pending_ind) destroy_att_send_op(chan->pending_ind); queue_destroy(chan->queue, destroy_att_send_op); io_destroy(chan->io); free(chan->buf); free(chan); } static bool disconnect_cb(struct io *io, void *user_data) { struct bt_att_chan *chan = user_data; struct bt_att *att = chan->att; int err; socklen_t len; len = sizeof(err); if (getsockopt(chan->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0) { DBG(att, "(chan %p) Failed to obtain disconnect " "error: %s", chan, strerror(errno)); err = 0; } DBG(att, "Channel %p disconnected: %s", chan, strerror(err)); /* Detach channel */ queue_remove(att->chans, chan); if (chan->pending_req) { disc_att_send_op(chan->pending_req); chan->pending_req = NULL; } if (chan->pending_ind) { disc_att_send_op(chan->pending_ind); chan->pending_ind = NULL; } bt_att_chan_free(chan); /* Don't run disconnect callback if there are channels left */ if (!queue_isempty(att->chans)) return false; bt_att_ref(att); att->in_disc = true; /* Notify request callbacks */ queue_remove_all(att->req_queue, NULL, NULL, disc_att_send_op); queue_remove_all(att->ind_queue, NULL, NULL, disc_att_send_op); queue_remove_all(att->write_queue, NULL, NULL, disc_att_send_op); att->in_disc = false; queue_foreach(att->disconn_list, disconn_handler, INT_TO_PTR(err)); bt_att_unregister_all(att); bt_att_unref(att); return false; } static int bt_att_chan_get_security(struct bt_att_chan *chan) { struct bt_security sec; socklen_t len; if (chan->type == BT_ATT_LOCAL) return chan->sec_level; memset(&sec, 0, sizeof(sec)); len = sizeof(sec); if (getsockopt(chan->fd, SOL_BLUETOOTH, BT_SECURITY, &sec, &len) < 0) return -EIO; return sec.level; } static bool bt_att_chan_set_security(struct bt_att_chan *chan, int level) { struct bt_security sec; if (chan->type == BT_ATT_LOCAL) { chan->sec_level = level; return true; } /* Check if security level has already been set, if the security level * is higher it shall satisfy the request since we never want to * downgrade security. */ if (level <= bt_att_chan_get_security(chan)) return true; memset(&sec, 0, sizeof(sec)); sec.level = level; if (setsockopt(chan->fd, SOL_BLUETOOTH, BT_SECURITY, &sec, sizeof(sec)) < 0) return false; return true; } static bool change_security(struct bt_att_chan *chan, uint8_t ecode) { int security; if (chan->sec_level != BT_ATT_SECURITY_AUTO) return false; security = bt_att_chan_get_security(chan); if (ecode == BT_ATT_ERROR_INSUFFICIENT_ENCRYPTION && security < BT_ATT_SECURITY_MEDIUM) { security = BT_ATT_SECURITY_MEDIUM; } else if (ecode == BT_ATT_ERROR_AUTHENTICATION) { if (security < BT_ATT_SECURITY_MEDIUM) security = BT_ATT_SECURITY_MEDIUM; else if (security < BT_ATT_SECURITY_HIGH) security = BT_ATT_SECURITY_HIGH; else if (security < BT_ATT_SECURITY_FIPS) security = BT_ATT_SECURITY_FIPS; else return false; } else { return false; } return bt_att_chan_set_security(chan, security); } static bool handle_error_rsp(struct bt_att_chan *chan, uint8_t *pdu, ssize_t pdu_len, uint8_t *opcode) { struct bt_att *att = chan->att; const struct bt_att_pdu_error_rsp *rsp; struct att_send_op *op = chan->pending_req; if (pdu_len != sizeof(*rsp)) { *opcode = 0; return false; } rsp = (void *) pdu; *opcode = rsp->opcode; /* If operation has already been marked as retry don't attempt to change * the security again. */ if (op->retry) return false; /* Attempt to change security */ if (!change_security(chan, rsp->ecode)) return false; /* Remove timeout_id if outstanding */ if (op->timeout_id) { timeout_remove(op->timeout_id); op->timeout_id = 0; } DBG(att, "(chan %p) Retrying operation %p", chan, op); chan->pending_req = NULL; op->retry = true; /* Push operation back to channel queue */ return queue_push_head(chan->queue, op); } static void handle_rsp(struct bt_att_chan *chan, uint8_t opcode, uint8_t *pdu, ssize_t pdu_len) { struct bt_att *att = chan->att; struct att_send_op *op = chan->pending_req; uint8_t req_opcode; uint8_t rsp_opcode; uint8_t *rsp_pdu = NULL; uint16_t rsp_pdu_len = 0; /* * If no request is pending, then the response is unexpected. Disconnect * the bearer. */ if (!op) { DBG(att, "(chan %p) Received unexpected ATT response", chan); io_shutdown(chan->io); return; } /* * If the received response doesn't match the pending request, or if * the request is malformed, end the current request with failure. */ if (opcode == BT_ATT_OP_ERROR_RSP) { /* Return if error response cause a retry */ if (handle_error_rsp(chan, pdu, pdu_len, &req_opcode)) { wakeup_chan_writer(chan, NULL); return; } } else if (!(req_opcode = get_req_opcode(opcode))) goto fail; if (req_opcode != op->opcode) goto fail; rsp_opcode = opcode; if (pdu_len > 0) { rsp_pdu = pdu; rsp_pdu_len = pdu_len; } goto done; fail: DBG(att, "(chan %p) Failed to handle response PDU; opcode: " "0x%02x", chan, opcode); rsp_opcode = BT_ATT_OP_ERROR_RSP; done: if (op->callback) op->callback(rsp_opcode, rsp_pdu, rsp_pdu_len, op->user_data); destroy_att_send_op(op); chan->pending_req = NULL; wakeup_chan_writer(chan, NULL); } static void handle_conf(struct bt_att_chan *chan, uint8_t *pdu, ssize_t pdu_len) { struct bt_att *att = chan->att; struct att_send_op *op = chan->pending_ind; /* * Disconnect the bearer if the confirmation is unexpected or the PDU is * invalid. */ if (!op || pdu_len) { DBG(att, "(chan %p) Received unexpected/invalid ATT " "confirmation", chan); io_shutdown(chan->io); return; } if (op->callback) op->callback(BT_ATT_OP_HANDLE_CONF, NULL, 0, op->user_data); destroy_att_send_op(op); chan->pending_ind = NULL; wakeup_chan_writer(chan, NULL); } struct notify_data { uint8_t opcode; uint8_t *pdu; ssize_t pdu_len; bool handler_found; }; static bool opcode_match(uint8_t opcode, uint8_t test_opcode) { enum att_op_type op_type = get_op_type(test_opcode); if (opcode == BT_ATT_ALL_REQUESTS && (op_type == ATT_OP_TYPE_REQ || op_type == ATT_OP_TYPE_CMD)) return true; return opcode == test_opcode; } static void respond_not_supported(struct bt_att *att, uint8_t opcode) { struct bt_att_pdu_error_rsp pdu; pdu.opcode = opcode; pdu.handle = 0x0000; pdu.ecode = BT_ATT_ERROR_REQUEST_NOT_SUPPORTED; bt_att_send(att, BT_ATT_OP_ERROR_RSP, &pdu, sizeof(pdu), NULL, NULL, NULL); } static bool handle_signed(struct bt_att *att, uint8_t *pdu, ssize_t pdu_len) { uint8_t *signature; uint32_t sign_cnt; struct sign_info *sign; uint8_t opcode = pdu[0]; /* Check if there is enough data for a signature */ if (pdu_len < 3 + BT_ATT_SIGNATURE_LEN) goto fail; sign = att->remote_sign; if (!sign) goto fail; signature = pdu + (pdu_len - BT_ATT_SIGNATURE_LEN); sign_cnt = get_le32(signature); /* Validate counter */ if (!sign->counter(&sign_cnt, sign->user_data)) goto fail; /* Verify received signature */ if (!bt_crypto_verify_att_sign(att->crypto, sign->key, pdu, pdu_len)) goto fail; return true; fail: DBG(att, "ATT failed to verify signature: 0x%02x", opcode); return false; } static void handle_notify(struct bt_att_chan *chan, uint8_t *pdu, ssize_t pdu_len) { struct bt_att *att = chan->att; const struct queue_entry *entry; bool found; uint8_t opcode = pdu[0]; bt_att_ref(att); found = false; entry = queue_get_entries(att->notify_list); while (entry) { struct att_notify *notify = entry->data; entry = entry->next; if (!opcode_match(notify->opcode, opcode)) continue; if ((opcode & ATT_OP_SIGNED_MASK) && att->crypto) { if (!handle_signed(att, pdu, pdu_len)) return; pdu_len -= BT_ATT_SIGNATURE_LEN; } /* BLUETOOTH CORE SPECIFICATION Version 5.1 | Vol 3, Part G * page 2370 * * 4.3.1 Exchange MTU * * This sub-procedure shall not be used on a BR/EDR physical * link since the MTU size is negotiated using L2CAP channel * configuration procedures. */ if (bt_att_get_link_type(att) == BT_ATT_BREDR || chan->type == BT_ATT_EATT) { switch (opcode) { case BT_ATT_OP_MTU_REQ: goto not_supported; } } found = true; if (notify->callback) notify->callback(chan, chan->mtu, opcode, pdu + 1, pdu_len - 1, notify->user_data); /* callback could remove all entries from notify list */ if (queue_isempty(att->notify_list)) break; } not_supported: /* * If this was not a command and no handler was registered for it, * respond with "Not Supported" */ if (!found && get_op_type(opcode) != ATT_OP_TYPE_CMD) respond_not_supported(att, opcode); bt_att_unref(att); } static bool can_read_data(struct io *io, void *user_data) { struct bt_att_chan *chan = user_data; struct bt_att *att = chan->att; uint8_t opcode; uint8_t *pdu; ssize_t bytes_read; bytes_read = read(chan->fd, chan->buf, chan->mtu); if (bytes_read < 0) return false; VERBOSE(att, "(chan %p) ATT received: %zd", chan, bytes_read); att_hexdump(att, '>', chan->buf, bytes_read); if (bytes_read < ATT_MIN_PDU_LEN) return true; pdu = chan->buf; opcode = pdu[0]; bt_att_ref(att); /* Act on the received PDU based on the opcode type */ switch (get_op_type(opcode)) { case ATT_OP_TYPE_RSP: VERBOSE(att, "(chan %p) ATT response received: 0x%02x", chan, opcode); handle_rsp(chan, opcode, pdu + 1, bytes_read - 1); break; case ATT_OP_TYPE_CONF: VERBOSE(att, "(chan %p) ATT confirmation received: 0x%02x", chan, opcode); handle_conf(chan, pdu + 1, bytes_read - 1); break; case ATT_OP_TYPE_REQ: /* * If a request is currently pending, then the sequential * protocol was violated. Disconnect the bearer, which will * promptly notify the upper layer via disconnect handlers. */ if (chan->in_req) { DBG(att, "(chan %p) Received request while " "another is pending: 0x%02x", chan, opcode); io_shutdown(chan->io); bt_att_unref(chan->att); return false; } chan->in_req = true; /* fall through */ case ATT_OP_TYPE_CMD: case ATT_OP_TYPE_NFY: case ATT_OP_TYPE_UNKNOWN: case ATT_OP_TYPE_IND: /* fall through */ default: /* For all other opcodes notify the upper layer of the PDU and * let them act on it. */ DBG(att, "(chan %p) ATT PDU received: 0x%02x", chan, opcode); handle_notify(chan, pdu, bytes_read); break; } bt_att_unref(att); return true; } static bool is_io_l2cap_based(int fd) { int domain; int proto; int err; socklen_t len; domain = 0; len = sizeof(domain); err = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &domain, &len); if (err < 0) return false; if (domain != AF_BLUETOOTH) return false; proto = 0; len = sizeof(proto); err = getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &proto, &len); if (err < 0) return false; return proto == BTPROTO_L2CAP; } static void bt_att_free(struct bt_att *att) { bt_crypto_unref(att->crypto); if (att->timeout_destroy) att->timeout_destroy(att->timeout_data); if (att->debug_destroy) att->debug_destroy(att->debug_data); free(att->local_sign); free(att->remote_sign); queue_destroy(att->req_queue, NULL); queue_destroy(att->ind_queue, NULL); queue_destroy(att->write_queue, NULL); queue_destroy(att->notify_list, NULL); queue_destroy(att->disconn_list, NULL); queue_destroy(att->exchange_list, NULL); queue_destroy(att->chans, bt_att_chan_free); free(att); } static uint16_t io_get_mtu(int fd) { socklen_t len; struct l2cap_options l2o; len = sizeof(l2o); if (!getsockopt(fd, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &len)) return l2o.omtu; if (!getsockopt(fd, SOL_BLUETOOTH, BT_SNDMTU, &l2o.omtu, &len)) return l2o.omtu; return 0; } static uint8_t io_get_type(int fd) { struct sockaddr_l2 src; socklen_t len; if (!is_io_l2cap_based(fd)) return BT_ATT_LOCAL; len = sizeof(src); memset(&src, 0, len); if (getsockname(fd, (void *)&src, &len) < 0) return -errno; if (src.l2_bdaddr_type == BDADDR_BREDR) return BT_ATT_BREDR; return BT_ATT_LE; } static struct bt_att_chan *bt_att_chan_new(int fd, uint8_t type) { struct bt_att_chan *chan; if (fd < 0) return NULL; chan = new0(struct bt_att_chan, 1); chan->fd = fd; chan->io = io_new(fd); if (!chan->io) goto fail; if (!io_set_read_handler(chan->io, can_read_data, chan, NULL)) goto fail; if (!io_set_disconnect_handler(chan->io, disconnect_cb, chan, NULL)) goto fail; chan->type = type; switch (chan->type) { case BT_ATT_LOCAL: chan->sec_level = BT_ATT_SECURITY_LOW; /* fall through */ case BT_ATT_LE: chan->mtu = BT_ATT_DEFAULT_LE_MTU; break; default: chan->mtu = io_get_mtu(chan->fd); } if (chan->mtu < BT_ATT_DEFAULT_LE_MTU) goto fail; chan->buf = malloc(chan->mtu); if (!chan->buf) goto fail; chan->queue = queue_new(); return chan; fail: bt_att_chan_free(chan); return NULL; } static void bt_att_attach_chan(struct bt_att *att, struct bt_att_chan *chan) { /* Push to head as EATT channels have higher priority */ queue_push_head(att->chans, chan); chan->att = att; if (chan->mtu > att->mtu) att->mtu = chan->mtu; io_set_close_on_destroy(chan->io, att->close_on_unref); DBG(att, "Channel %p attached", chan); wakeup_chan_writer(chan, NULL); } struct bt_att *bt_att_new(int fd, bool ext_signed) { struct bt_att *att; struct bt_att_chan *chan; chan = bt_att_chan_new(fd, io_get_type(fd)); if (!chan) return NULL; att = new0(struct bt_att, 1); att->chans = queue_new(); att->mtu = chan->mtu; /* crypto is optional, if not available leave it NULL */ if (!ext_signed) att->crypto = bt_crypto_new(); att->req_queue = queue_new(); att->ind_queue = queue_new(); att->write_queue = queue_new(); att->notify_list = queue_new(); att->disconn_list = queue_new(); att->exchange_list = queue_new(); bt_att_attach_chan(att, chan); return bt_att_ref(att); } struct bt_att *bt_att_ref(struct bt_att *att) { if (!att) return NULL; __sync_fetch_and_add(&att->ref_count, 1); return att; } void bt_att_unref(struct bt_att *att) { if (!att) return; if (__sync_sub_and_fetch(&att->ref_count, 1)) return; bt_att_unregister_all(att); bt_att_cancel_all(att); bt_att_free(att); } bool bt_att_set_close_on_unref(struct bt_att *att, bool do_close) { const struct queue_entry *entry; if (!att) return false; att->close_on_unref = do_close; for (entry = queue_get_entries(att->chans); entry; entry = entry->next) { struct bt_att_chan *chan = entry->data; if (!io_set_close_on_destroy(chan->io, do_close)) return false; } return true; } int bt_att_attach_fd(struct bt_att *att, int fd) { struct bt_att_chan *chan; if (!att || fd < 0) return -EINVAL; chan = bt_att_chan_new(fd, BT_ATT_EATT); if (!chan) return -EINVAL; bt_att_attach_chan(att, chan); return 0; } int bt_att_get_fd(struct bt_att *att) { struct bt_att_chan *chan; if (!att) return -1; if (queue_isempty(att->chans)) return -ENOTCONN; chan = queue_peek_tail(att->chans); return chan->fd; } int bt_att_get_channels(struct bt_att *att) { if (!att) return 0; return queue_length(att->chans); } bool bt_att_set_debug(struct bt_att *att, uint8_t level, bt_att_debug_func_t callback, void *user_data, bt_att_destroy_func_t destroy) { if (!att) return false; if (att->debug_destroy) att->debug_destroy(att->debug_data); att->debug_level = level; att->debug_callback = callback; att->debug_destroy = destroy; att->debug_data = user_data; return true; } uint16_t bt_att_get_mtu(struct bt_att *att) { if (!att) return 0; return att->mtu; } static void exchange_handler(void *data, void *user_data) { struct att_exchange *exchange = data; uint16_t mtu = PTR_TO_INT(user_data); if (exchange->removed) return; if (exchange->callback) exchange->callback(mtu, exchange->user_data); } bool bt_att_set_mtu(struct bt_att *att, uint16_t mtu) { struct bt_att_chan *chan; void *buf; if (!att) return false; if (mtu < BT_ATT_DEFAULT_LE_MTU) return false; /* Original channel is always the last */ chan = queue_peek_tail(att->chans); if (!chan) return -ENOTCONN; buf = malloc(mtu); if (!buf) return false; free(chan->buf); chan->mtu = mtu; chan->buf = buf; if (chan->mtu > att->mtu) { att->mtu = chan->mtu; queue_foreach(att->exchange_list, exchange_handler, INT_TO_PTR(att->mtu)); } return true; } uint8_t bt_att_get_link_type(struct bt_att *att) { struct bt_att_chan *chan; if (!att) return -EINVAL; chan = queue_peek_tail(att->chans); if (!chan) return -ENOTCONN; return chan->type; } bool bt_att_set_timeout_cb(struct bt_att *att, bt_att_timeout_func_t callback, void *user_data, bt_att_destroy_func_t destroy) { if (!att) return false; if (att->timeout_destroy) att->timeout_destroy(att->timeout_data); att->timeout_callback = callback; att->timeout_destroy = destroy; att->timeout_data = user_data; return true; } unsigned int bt_att_register_disconnect(struct bt_att *att, bt_att_disconnect_func_t callback, void *user_data, bt_att_destroy_func_t destroy) { struct att_disconn *disconn; if (!att || queue_isempty(att->chans)) return 0; disconn = new0(struct att_disconn, 1); disconn->callback = callback; disconn->destroy = destroy; disconn->user_data = user_data; if (att->next_reg_id < 1) att->next_reg_id = 1; disconn->id = att->next_reg_id++; if (!queue_push_tail(att->disconn_list, disconn)) { free(disconn); return 0; } return disconn->id; } bool bt_att_unregister_disconnect(struct bt_att *att, unsigned int id) { struct att_disconn *disconn; if (!att || !id) return false; /* Check if disconnect is running */ if (queue_isempty(att->chans)) { disconn = queue_find(att->disconn_list, match_disconn_id, UINT_TO_PTR(id)); if (!disconn) return false; disconn->removed = true; return true; } disconn = queue_remove_if(att->disconn_list, match_disconn_id, UINT_TO_PTR(id)); if (!disconn) return false; destroy_att_disconn(disconn); return true; } unsigned int bt_att_register_exchange(struct bt_att *att, bt_att_exchange_func_t callback, void *user_data, bt_att_destroy_func_t destroy) { struct att_exchange *mtu; if (!att || queue_isempty(att->chans)) return 0; mtu = new0(struct att_exchange, 1); mtu->callback = callback; mtu->destroy = destroy; mtu->user_data = user_data; if (att->next_reg_id < 1) att->next_reg_id = 1; mtu->id = att->next_reg_id++; if (!queue_push_tail(att->exchange_list, mtu)) { free(att); return 0; } return mtu->id; } bool bt_att_unregister_exchange(struct bt_att *att, unsigned int id) { struct att_exchange *mtu; if (!att || !id) return false; /* Check if disconnect is running */ if (queue_isempty(att->chans)) { mtu = queue_find(att->exchange_list, match_disconn_id, UINT_TO_PTR(id)); if (!mtu) return false; mtu->removed = true; return true; } mtu = queue_remove_if(att->exchange_list, match_disconn_id, UINT_TO_PTR(id)); if (!mtu) return false; destroy_att_exchange(mtu); return true; } unsigned int bt_att_send(struct bt_att *att, uint8_t opcode, const void *pdu, uint16_t length, bt_att_response_func_t callback, void *user_data, bt_att_destroy_func_t destroy) { struct att_send_op *op; bool result; if (!att || queue_isempty(att->chans)) return 0; op = create_att_send_op(att, opcode, pdu, length, callback, user_data, destroy); if (!op) return 0; if (att->next_send_id < 1) att->next_send_id = 1; op->id = att->next_send_id++; /* Always use fixed channel for BT_ATT_OP_MTU_REQ */ if (opcode == BT_ATT_OP_MTU_REQ) { struct bt_att_chan *chan = queue_peek_tail(att->chans); result = queue_push_tail(chan->queue, op); goto done; } /* Add the op to the correct queue based on its type */ switch (op->type) { case ATT_OP_TYPE_REQ: result = queue_push_tail(att->req_queue, op); break; case ATT_OP_TYPE_IND: result = queue_push_tail(att->ind_queue, op); break; case ATT_OP_TYPE_CMD: case ATT_OP_TYPE_NFY: case ATT_OP_TYPE_UNKNOWN: case ATT_OP_TYPE_RSP: case ATT_OP_TYPE_CONF: default: result = queue_push_tail(att->write_queue, op); break; } done: if (!result) { free(op->pdu); free(op); return 0; } wakeup_writer(att); return op->id; } int bt_att_resend(struct bt_att *att, unsigned int id, uint8_t opcode, const void *pdu, uint16_t length, bt_att_response_func_t callback, void *user_data, bt_att_destroy_func_t destroy) { const struct queue_entry *entry; struct att_send_op *op; bool result; if (!att || !id) return -EINVAL; /* Lookup request on each channel */ for (entry = queue_get_entries(att->chans); entry; entry = entry->next) { struct bt_att_chan *chan = entry->data; if (chan->pending_req && chan->pending_req->id == id) break; } if (!entry) return -ENOENT; /* Only allow requests to be resend */ if (get_op_type(opcode) != ATT_OP_TYPE_REQ) return -EOPNOTSUPP; op = create_att_send_op(att, opcode, pdu, length, callback, user_data, destroy); if (!op) return -ENOMEM; op->id = id; switch (opcode) { /* Only prepend requests that could be a continuation */ case BT_ATT_OP_READ_BLOB_REQ: case BT_ATT_OP_PREP_WRITE_REQ: case BT_ATT_OP_EXEC_WRITE_REQ: result = queue_push_head(att->req_queue, op); break; default: result = queue_push_tail(att->req_queue, op); break; } if (!result) { free(op->pdu); free(op); return -ENOMEM; } wakeup_writer(att); return 0; } unsigned int bt_att_chan_send(struct bt_att_chan *chan, uint8_t opcode, const void *pdu, uint16_t len, bt_att_response_func_t callback, void *user_data, bt_att_destroy_func_t destroy) { struct att_send_op *op; if (!chan || !chan->att) return -EINVAL; op = create_att_send_op(chan->att, opcode, pdu, len, callback, user_data, destroy); if (!op) return -EINVAL; if (!queue_push_tail(chan->queue, op)) { free(op->pdu); free(op); return 0; } wakeup_chan_writer(chan, NULL); return op->id; } static bool match_op_id(const void *a, const void *b) { const struct att_send_op *op = a; unsigned int id = PTR_TO_UINT(b); return op->id == id; } bool bt_att_chan_cancel(struct bt_att_chan *chan, unsigned int id) { struct att_send_op *op; if (chan->pending_req && chan->pending_req->id == id) { /* Don't cancel the pending request; remove it's handlers */ cancel_att_send_op(chan->pending_req); return true; } if (chan->pending_ind && chan->pending_ind->id == id) { /* Don't cancel the pending indication; remove it's handlers. */ cancel_att_send_op(chan->pending_ind); return true; } op = queue_remove_if(chan->queue, match_op_id, UINT_TO_PTR(id)); if (!op) return false; destroy_att_send_op(op); wakeup_chan_writer(chan, NULL); return true; } static bool bt_att_disc_cancel(struct bt_att *att, unsigned int id) { struct att_send_op *op; op = queue_find(att->req_queue, match_op_id, UINT_TO_PTR(id)); if (op) goto done; op = queue_find(att->ind_queue, match_op_id, UINT_TO_PTR(id)); if (op) goto done; op = queue_find(att->write_queue, match_op_id, UINT_TO_PTR(id)); done: if (!op) return false; /* Just cancel since disconnect_cb will be cleaning up */ cancel_att_send_op(op); return true; } bool bt_att_cancel(struct bt_att *att, unsigned int id) { const struct queue_entry *entry; struct att_send_op *op; if (!att || !id) return false; /* Lookuo request on each channel first */ for (entry = queue_get_entries(att->chans); entry; entry = entry->next) { struct bt_att_chan *chan = entry->data; if (bt_att_chan_cancel(chan, id)) return true; } if (att->in_disc) return bt_att_disc_cancel(att, id); op = queue_remove_if(att->req_queue, match_op_id, UINT_TO_PTR(id)); if (op) goto done; op = queue_remove_if(att->ind_queue, match_op_id, UINT_TO_PTR(id)); if (op) goto done; op = queue_remove_if(att->write_queue, match_op_id, UINT_TO_PTR(id)); if (op) goto done; if (!op) return false; done: destroy_att_send_op(op); wakeup_writer(att); return true; } bool bt_att_cancel_all(struct bt_att *att) { const struct queue_entry *entry; if (!att) return false; queue_remove_all(att->req_queue, NULL, NULL, destroy_att_send_op); queue_remove_all(att->ind_queue, NULL, NULL, destroy_att_send_op); queue_remove_all(att->write_queue, NULL, NULL, destroy_att_send_op); for (entry = queue_get_entries(att->chans); entry; entry = entry->next) { struct bt_att_chan *chan = entry->data; if (chan->pending_req) /* Don't cancel the pending request; remove it's * handlers */ cancel_att_send_op(chan->pending_req); if (chan->pending_ind) /* Don't cancel the pending request; remove it's * handlers */ cancel_att_send_op(chan->pending_ind); } return true; } static uint8_t att_ecode_from_error(int err) { /* * If the error fits in a single byte, treat it as an ATT protocol * error as is. Since "0" is not a valid ATT protocol error code, we map * that to UNLIKELY below. */ if (err > 0 && err < UINT8_MAX) return err; /* * Since we allow UNIX errnos, map them to appropriate ATT protocol * and "Common Profile and Service" error codes. */ switch (err) { case -ENOENT: return BT_ATT_ERROR_INVALID_HANDLE; case -ENOMEM: return BT_ATT_ERROR_INSUFFICIENT_RESOURCES; case -EALREADY: return BT_ERROR_ALREADY_IN_PROGRESS; case -EOVERFLOW: return BT_ERROR_OUT_OF_RANGE; } return BT_ATT_ERROR_UNLIKELY; } int bt_att_chan_send_error_rsp(struct bt_att_chan *chan, uint8_t opcode, uint16_t handle, int error) { struct bt_att_pdu_error_rsp pdu; uint8_t ecode; if (!chan || !chan->att || !opcode) return -EINVAL; ecode = att_ecode_from_error(error); memset(&pdu, 0, sizeof(pdu)); pdu.opcode = opcode; put_le16(handle, &pdu.handle); pdu.ecode = ecode; return bt_att_chan_send_rsp(chan, BT_ATT_OP_ERROR_RSP, &pdu, sizeof(pdu)); } unsigned int bt_att_register(struct bt_att *att, uint8_t opcode, bt_att_notify_func_t callback, void *user_data, bt_att_destroy_func_t destroy) { struct att_notify *notify; if (!att || !callback || queue_isempty(att->chans)) return 0; notify = new0(struct att_notify, 1); notify->opcode = opcode; notify->callback = callback; notify->destroy = destroy; notify->user_data = user_data; if (att->next_reg_id < 1) att->next_reg_id = 1; notify->id = att->next_reg_id++; if (!queue_push_tail(att->notify_list, notify)) { free(notify); return 0; } return notify->id; } bool bt_att_unregister(struct bt_att *att, unsigned int id) { struct att_notify *notify; if (!att || !id) return false; notify = queue_remove_if(att->notify_list, match_notify_id, UINT_TO_PTR(id)); if (!notify) return false; destroy_att_notify(notify); return true; } bool bt_att_unregister_all(struct bt_att *att) { if (!att) return false; queue_remove_all(att->notify_list, NULL, NULL, destroy_att_notify); queue_remove_all(att->disconn_list, NULL, NULL, destroy_att_disconn); queue_remove_all(att->exchange_list, NULL, NULL, destroy_att_exchange); return true; } int bt_att_get_security(struct bt_att *att, uint8_t *enc_size) { struct bt_att_chan *chan; int ret; if (!att) return -EINVAL; chan = queue_peek_tail(att->chans); if (!chan) return -ENOTCONN; ret = bt_att_chan_get_security(chan); if (ret < 0) return ret; if (enc_size) *enc_size = att->enc_size; return ret; } bool bt_att_set_security(struct bt_att *att, int level) { struct bt_att_chan *chan; if (!att || level < BT_ATT_SECURITY_AUTO || level > BT_ATT_SECURITY_HIGH) return false; chan = queue_peek_tail(att->chans); if (!chan) return -ENOTCONN; return bt_att_chan_set_security(chan, level); } void bt_att_set_enc_key_size(struct bt_att *att, uint8_t enc_size) { if (!att) return; att->enc_size = enc_size; } static bool sign_set_key(struct sign_info **sign, uint8_t key[16], bt_att_counter_func_t func, void *user_data) { if (!(*sign)) *sign = new0(struct sign_info, 1); (*sign)->counter = func; (*sign)->user_data = user_data; memcpy((*sign)->key, key, 16); return true; } bool bt_att_set_local_key(struct bt_att *att, uint8_t sign_key[16], bt_att_counter_func_t func, void *user_data) { if (!att) return false; return sign_set_key(&att->local_sign, sign_key, func, user_data); } bool bt_att_set_remote_key(struct bt_att *att, uint8_t sign_key[16], bt_att_counter_func_t func, void *user_data) { if (!att) return false; return sign_set_key(&att->remote_sign, sign_key, func, user_data); } bool bt_att_has_crypto(struct bt_att *att) { if (!att) return false; return att->crypto ? true : false; } bool bt_att_set_retry(struct bt_att *att, unsigned int id, bool retry) { struct att_send_op *op; if (!id) return false; op = queue_find(att->req_queue, match_op_id, UINT_TO_PTR(id)); if (op) goto done; op = queue_find(att->ind_queue, match_op_id, UINT_TO_PTR(id)); if (op) goto done; op = queue_find(att->write_queue, match_op_id, UINT_TO_PTR(id)); done: if (!op) return false; op->retry = !retry; return true; } |