smp_l2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 1999-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /******************************************************************************
  19. *
  20. * This file contains functions for the SMP L2Cap interface
  21. *
  22. ******************************************************************************/
  23. #include "common/bt_target.h"
  24. #include "osi/allocator.h"
  25. #if SMP_INCLUDED == TRUE
  26. #include <string.h>
  27. #include "stack/btm_ble_api.h"
  28. #include "stack/l2c_api.h"
  29. #include "smp_int.h"
  30. static void smp_tx_complete_callback(UINT16 cid, UINT16 num_pkt);
  31. #if (BLE_INCLUDED == TRUE)
  32. static void smp_connect_callback(UINT16 channel, BD_ADDR bd_addr, BOOLEAN connected, UINT16 reason,
  33. tBT_TRANSPORT transport);
  34. static void smp_data_received(UINT16 channel, BD_ADDR bd_addr, BT_HDR *p_buf);
  35. #endif ///BLE_INCLUDED == TRUE
  36. #if (CLASSIC_BT_INCLUDED == TRUE)
  37. static void smp_br_connect_callback(UINT16 channel, BD_ADDR bd_addr, BOOLEAN connected, UINT16 reason,
  38. tBT_TRANSPORT transport);
  39. static void smp_br_data_received(UINT16 channel, BD_ADDR bd_addr, BT_HDR *p_buf);
  40. #endif ///CLASSIC_BT_INCLUDED == TRUE
  41. /*******************************************************************************
  42. **
  43. ** Function smp_l2cap_if_init
  44. **
  45. ** Description This function is called during the SMP task startup
  46. ** to register interface functions with L2CAP.
  47. **
  48. *******************************************************************************/
  49. void smp_l2cap_if_init (void)
  50. {
  51. tL2CAP_FIXED_CHNL_REG fixed_reg;
  52. SMP_TRACE_EVENT ("SMDBG l2c %s", __func__);
  53. fixed_reg.fixed_chnl_opts.mode = L2CAP_FCR_BASIC_MODE;
  54. fixed_reg.fixed_chnl_opts.max_transmit = 0;
  55. fixed_reg.fixed_chnl_opts.rtrans_tout = 0;
  56. fixed_reg.fixed_chnl_opts.mon_tout = 0;
  57. fixed_reg.fixed_chnl_opts.mps = 0;
  58. fixed_reg.fixed_chnl_opts.tx_win_sz = 0;
  59. fixed_reg.pL2CA_FixedTxComplete_Cb = smp_tx_complete_callback;
  60. fixed_reg.pL2CA_FixedCong_Cb = NULL; /* do not handle congestion on this channel */
  61. fixed_reg.default_idle_tout = 0; /* set 0 seconds timeout, 0xffff default idle timeout.
  62. This timeout is used to wait for the end of the pairing
  63. and then make a disconnect request, setting a larger value
  64. will cause the disconnect event to go back up for a long time.
  65. Set to 0 will be disconnected directly, and it will come up
  66. pairing failure, so it will not cause adverse effects. */
  67. #if (BLE_INCLUDED == TRUE)
  68. fixed_reg.pL2CA_FixedConn_Cb = smp_connect_callback;
  69. fixed_reg.pL2CA_FixedData_Cb = smp_data_received;
  70. L2CA_RegisterFixedChannel (L2CAP_SMP_CID, &fixed_reg);
  71. #endif ///BLE_INCLUDED == TRUE
  72. #if (CLASSIC_BT_INCLUDED == TRUE)
  73. fixed_reg.pL2CA_FixedConn_Cb = smp_br_connect_callback;
  74. fixed_reg.pL2CA_FixedData_Cb = smp_br_data_received;
  75. L2CA_RegisterFixedChannel (L2CAP_SMP_BR_CID, &fixed_reg);
  76. #endif ///CLASSIC_BT_INCLUDED == TRUE
  77. }
  78. #if (BLE_INCLUDED == TRUE)
  79. /*******************************************************************************
  80. **
  81. ** Function smp_connect_callback
  82. **
  83. ** Description This callback function is called by L2CAP to indicate that
  84. ** SMP channel is
  85. ** connected (conn = TRUE)/disconnected (conn = FALSE).
  86. **
  87. *******************************************************************************/
  88. static void smp_connect_callback (UINT16 channel, BD_ADDR bd_addr, BOOLEAN connected, UINT16 reason,
  89. tBT_TRANSPORT transport)
  90. {
  91. tSMP_CB *p_cb = &smp_cb;
  92. tSMP_INT_DATA int_data;
  93. BD_ADDR dummy_bda = {0};
  94. SMP_TRACE_EVENT ("SMDBG l2c %s\n", __FUNCTION__);
  95. if (transport == BT_TRANSPORT_BR_EDR || memcmp(bd_addr, dummy_bda, BD_ADDR_LEN) == 0) {
  96. return;
  97. }
  98. if(!connected && &p_cb->rsp_timer_ent) {
  99. //free timer
  100. btu_free_timer(&p_cb->rsp_timer_ent);
  101. }
  102. if (memcmp(bd_addr, p_cb->pairing_bda, BD_ADDR_LEN) == 0) {
  103. SMP_TRACE_EVENT ("%s() for pairing BDA: %08x%04x Event: %s\n",
  104. __FUNCTION__,
  105. (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
  106. (bd_addr[4] << 8) + bd_addr[5],
  107. (connected) ? "connected" : "disconnected");
  108. if (connected) {
  109. if (!p_cb->connect_initialized) {
  110. p_cb->connect_initialized = TRUE;
  111. /* initiating connection established */
  112. p_cb->role = L2CA_GetBleConnRole(bd_addr);
  113. /* initialize local i/r key to be default keys */
  114. p_cb->local_r_key = p_cb->local_i_key = SMP_SEC_DEFAULT_KEY;
  115. p_cb->loc_auth_req = p_cb->peer_auth_req = SMP_DEFAULT_AUTH_REQ;
  116. p_cb->cb_evt = SMP_IO_CAP_REQ_EVT;
  117. smp_sm_event(p_cb, SMP_L2CAP_CONN_EVT, NULL);
  118. }
  119. } else {
  120. int_data.reason = reason;
  121. /* Disconnected while doing security */
  122. smp_sm_event(p_cb, SMP_L2CAP_DISCONN_EVT, &int_data);
  123. }
  124. }
  125. }
  126. /*******************************************************************************
  127. **
  128. ** Function smp_data_received
  129. **
  130. ** Description This function is called when data is received from L2CAP on
  131. ** SMP channel.
  132. **
  133. **
  134. ** Returns void
  135. **
  136. *******************************************************************************/
  137. static void smp_data_received(UINT16 channel, BD_ADDR bd_addr, BT_HDR *p_buf)
  138. {
  139. tSMP_CB *p_cb = &smp_cb;
  140. UINT8 *p = (UINT8 *)(p_buf + 1) + p_buf->offset;
  141. UINT8 cmd ;
  142. SMP_TRACE_EVENT ("\nSMDBG l2c %s\n", __FUNCTION__);
  143. STREAM_TO_UINT8(cmd, p);
  144. /* sanity check */
  145. if ((SMP_OPCODE_MAX < cmd) || (SMP_OPCODE_MIN > cmd)) {
  146. SMP_TRACE_WARNING( "Ignore received command with RESERVED code 0x%02x\n", cmd);
  147. osi_free (p_buf);
  148. return;
  149. }
  150. /* reject the pairing request if there is an on-going SMP pairing */
  151. if (SMP_OPCODE_PAIRING_REQ == cmd || SMP_OPCODE_SEC_REQ == cmd) {
  152. if ((p_cb->state == SMP_STATE_IDLE) && (p_cb->br_state == SMP_BR_STATE_IDLE) &&
  153. !(p_cb->flags & SMP_PAIR_FLAGS_WE_STARTED_DD)) {
  154. p_cb->role = L2CA_GetBleConnRole(bd_addr);
  155. memcpy(&p_cb->pairing_bda[0], bd_addr, BD_ADDR_LEN);
  156. } else if (memcmp(&bd_addr[0], p_cb->pairing_bda, BD_ADDR_LEN)) {
  157. osi_free (p_buf);
  158. smp_reject_unexpected_pairing_command(bd_addr);
  159. return;
  160. }
  161. /* else, out of state pairing request/security request received, passed into SM */
  162. }
  163. if (memcmp(&bd_addr[0], p_cb->pairing_bda, BD_ADDR_LEN) == 0) {
  164. btu_stop_timer (&p_cb->rsp_timer_ent);
  165. btu_start_timer (&p_cb->rsp_timer_ent, BTU_TTYPE_SMP_PAIRING_CMD,
  166. SMP_WAIT_FOR_RSP_TOUT);
  167. if (cmd == SMP_OPCODE_CONFIRM) {
  168. SMP_TRACE_DEBUG ("in %s cmd = 0x%02x, peer_auth_req = 0x%02x,"
  169. "loc_auth_req = 0x%02x\n",
  170. __FUNCTION__, cmd, p_cb->peer_auth_req, p_cb->loc_auth_req);
  171. if ((p_cb->peer_auth_req & SMP_SC_SUPPORT_BIT) &&
  172. (p_cb->loc_auth_req & SMP_SC_SUPPORT_BIT)) {
  173. cmd = SMP_OPCODE_PAIR_COMMITM;
  174. }
  175. }
  176. p_cb->rcvd_cmd_code = cmd;
  177. p_cb->rcvd_cmd_len = (UINT8) p_buf->len;
  178. smp_sm_event(p_cb, cmd, p);
  179. }
  180. osi_free (p_buf);
  181. }
  182. #endif ///BLE_INCLUDED == TRUE
  183. /*******************************************************************************
  184. **
  185. ** Function smp_tx_complete_callback
  186. **
  187. ** Description SMP channel tx complete callback
  188. **
  189. *******************************************************************************/
  190. static void smp_tx_complete_callback (UINT16 cid, UINT16 num_pkt)
  191. {
  192. tSMP_CB *p_cb = &smp_cb;
  193. if (p_cb->total_tx_unacked >= num_pkt) {
  194. p_cb->total_tx_unacked -= num_pkt;
  195. } else {
  196. SMP_TRACE_ERROR("Unexpected %s: num_pkt = %d", __func__, num_pkt);
  197. }
  198. UINT8 reason = SMP_SUCCESS;
  199. if (p_cb->total_tx_unacked == 0 && p_cb->wait_for_authorization_complete) {
  200. if (cid == L2CAP_SMP_CID) {
  201. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  202. } else {
  203. #if (CLASSIC_BT_INCLUDED == TRUE)
  204. smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &reason);
  205. #endif ///CLASSIC_BT_INCLUDED == TRUE
  206. }
  207. }
  208. }
  209. /*******************************************************************************
  210. **
  211. ** Function smp_br_connect_callback
  212. **
  213. ** Description This callback function is called by L2CAP to indicate that
  214. ** SMP BR channel is
  215. ** connected (conn = TRUE)/disconnected (conn = FALSE).
  216. **
  217. *******************************************************************************/
  218. #if (CLASSIC_BT_INCLUDED == TRUE)
  219. static void smp_br_connect_callback(UINT16 channel, BD_ADDR bd_addr, BOOLEAN connected,
  220. UINT16 reason, tBT_TRANSPORT transport)
  221. {
  222. tSMP_CB *p_cb = &smp_cb;
  223. tSMP_INT_DATA int_data;
  224. SMP_TRACE_EVENT ("%s", __func__);
  225. if (transport != BT_TRANSPORT_BR_EDR) {
  226. SMP_TRACE_EVENT ("%s is called on unexpected transport %d\n",
  227. __func__, transport);
  228. return;
  229. }
  230. if (!(memcmp(bd_addr, p_cb->pairing_bda, BD_ADDR_LEN) == 0)) {
  231. return;
  232. }
  233. SMP_TRACE_EVENT ("%s for pairing BDA: %08x%04x Event: %s\n",
  234. __func__,
  235. (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
  236. (bd_addr[4] << 8) + bd_addr[5],
  237. (connected) ? "connected" : "disconnected");
  238. if (connected) {
  239. if (!p_cb->connect_initialized) {
  240. p_cb->connect_initialized = TRUE;
  241. /* initialize local i/r key to be default keys */
  242. p_cb->local_r_key = p_cb->local_i_key = SMP_BR_SEC_DEFAULT_KEY;
  243. p_cb->loc_auth_req = p_cb->peer_auth_req = 0;
  244. p_cb->cb_evt = SMP_BR_KEYS_REQ_EVT;
  245. smp_br_state_machine_event(p_cb, SMP_BR_L2CAP_CONN_EVT, NULL);
  246. }
  247. } else {
  248. int_data.reason = reason;
  249. /* Disconnected while doing security */
  250. smp_br_state_machine_event(p_cb, SMP_BR_L2CAP_DISCONN_EVT, &int_data);
  251. }
  252. }
  253. /*******************************************************************************
  254. **
  255. ** Function smp_br_data_received
  256. **
  257. ** Description This function is called when data is received from L2CAP on
  258. ** SMP BR channel.
  259. **
  260. ** Returns void
  261. **
  262. *******************************************************************************/
  263. static void smp_br_data_received(UINT16 channel, BD_ADDR bd_addr, BT_HDR *p_buf)
  264. {
  265. tSMP_CB *p_cb = &smp_cb;
  266. UINT8 *p = (UINT8 *)(p_buf + 1) + p_buf->offset;
  267. UINT8 cmd ;
  268. SMP_TRACE_EVENT ("SMDBG l2c %s\n", __func__);
  269. STREAM_TO_UINT8(cmd, p);
  270. /* sanity check */
  271. if ((SMP_OPCODE_MAX < cmd) || (SMP_OPCODE_MIN > cmd)) {
  272. SMP_TRACE_WARNING( "Ignore received command with RESERVED code 0x%02x", cmd);
  273. osi_free(p_buf);
  274. return;
  275. }
  276. /* reject the pairing request if there is an on-going SMP pairing */
  277. if (SMP_OPCODE_PAIRING_REQ == cmd) {
  278. if ((p_cb->state == SMP_STATE_IDLE) && (p_cb->br_state == SMP_BR_STATE_IDLE)) {
  279. p_cb->role = HCI_ROLE_SLAVE;
  280. p_cb->smp_over_br = TRUE;
  281. memcpy(&p_cb->pairing_bda[0], bd_addr, BD_ADDR_LEN);
  282. } else if (memcmp(&bd_addr[0], p_cb->pairing_bda, BD_ADDR_LEN)) {
  283. osi_free (p_buf);
  284. smp_reject_unexpected_pairing_command(bd_addr);
  285. return;
  286. }
  287. /* else, out of state pairing request received, passed into State Machine */
  288. }
  289. if (memcmp(&bd_addr[0], p_cb->pairing_bda, BD_ADDR_LEN) == 0) {
  290. btu_stop_timer (&p_cb->rsp_timer_ent);
  291. btu_start_timer (&p_cb->rsp_timer_ent, BTU_TTYPE_SMP_PAIRING_CMD,
  292. SMP_WAIT_FOR_RSP_TOUT);
  293. p_cb->rcvd_cmd_code = cmd;
  294. p_cb->rcvd_cmd_len = (UINT8) p_buf->len;
  295. smp_br_state_machine_event(p_cb, cmd, p);
  296. }
  297. osi_free (p_buf);
  298. }
  299. #endif /* CLASSIC_BT_INCLUDED == TRUE */
  300. #endif /* SMP_INCLUDED == TRUE */