hci_conn.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
  4. Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License version 2 as
  7. published by the Free Software Foundation;
  8. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  9. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  10. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  11. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  12. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  13. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  17. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  18. SOFTWARE IS DISCLAIMED.
  19. */
  20. /* Bluetooth HCI connection handling. */
  21. #include <linux/export.h>
  22. #include <linux/debugfs.h>
  23. #include <net/bluetooth/bluetooth.h>
  24. #include <net/bluetooth/hci_core.h>
  25. #include <net/bluetooth/l2cap.h>
  26. #include "hci_request.h"
  27. #include "smp.h"
  28. #include "a2mp.h"
  29. struct sco_param {
  30. u16 pkt_type;
  31. u16 max_latency;
  32. u8 retrans_effort;
  33. };
  34. static const struct sco_param esco_param_cvsd[] = {
  35. { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a, 0x01 }, /* S3 */
  36. { EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007, 0x01 }, /* S2 */
  37. { EDR_ESCO_MASK | ESCO_EV3, 0x0007, 0x01 }, /* S1 */
  38. { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0x01 }, /* D1 */
  39. { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0x01 }, /* D0 */
  40. };
  41. static const struct sco_param sco_param_cvsd[] = {
  42. { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0xff }, /* D1 */
  43. { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0xff }, /* D0 */
  44. };
  45. static const struct sco_param esco_param_msbc[] = {
  46. { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d, 0x02 }, /* T2 */
  47. { EDR_ESCO_MASK | ESCO_EV3, 0x0008, 0x02 }, /* T1 */
  48. };
  49. /* This function requires the caller holds hdev->lock */
  50. static void hci_connect_le_scan_cleanup(struct hci_conn *conn)
  51. {
  52. struct hci_conn_params *params;
  53. struct hci_dev *hdev = conn->hdev;
  54. struct smp_irk *irk;
  55. bdaddr_t *bdaddr;
  56. u8 bdaddr_type;
  57. bdaddr = &conn->dst;
  58. bdaddr_type = conn->dst_type;
  59. /* Check if we need to convert to identity address */
  60. irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
  61. if (irk) {
  62. bdaddr = &irk->bdaddr;
  63. bdaddr_type = irk->addr_type;
  64. }
  65. params = hci_pend_le_action_lookup(&hdev->pend_le_conns, bdaddr,
  66. bdaddr_type);
  67. if (!params || !params->explicit_connect)
  68. return;
  69. /* The connection attempt was doing scan for new RPA, and is
  70. * in scan phase. If params are not associated with any other
  71. * autoconnect action, remove them completely. If they are, just unmark
  72. * them as waiting for connection, by clearing explicit_connect field.
  73. */
  74. params->explicit_connect = false;
  75. list_del_init(&params->action);
  76. switch (params->auto_connect) {
  77. case HCI_AUTO_CONN_EXPLICIT:
  78. hci_conn_params_del(hdev, bdaddr, bdaddr_type);
  79. /* return instead of break to avoid duplicate scan update */
  80. return;
  81. case HCI_AUTO_CONN_DIRECT:
  82. case HCI_AUTO_CONN_ALWAYS:
  83. list_add(&params->action, &hdev->pend_le_conns);
  84. break;
  85. case HCI_AUTO_CONN_REPORT:
  86. list_add(&params->action, &hdev->pend_le_reports);
  87. break;
  88. default:
  89. break;
  90. }
  91. hci_update_background_scan(hdev);
  92. }
  93. static void hci_conn_cleanup(struct hci_conn *conn)
  94. {
  95. struct hci_dev *hdev = conn->hdev;
  96. if (test_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags))
  97. hci_conn_params_del(conn->hdev, &conn->dst, conn->dst_type);
  98. hci_chan_list_flush(conn);
  99. hci_conn_hash_del(hdev, conn);
  100. if (hdev->notify)
  101. hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
  102. hci_conn_del_sysfs(conn);
  103. debugfs_remove_recursive(conn->debugfs);
  104. hci_dev_put(hdev);
  105. hci_conn_put(conn);
  106. }
  107. static void le_scan_cleanup(struct work_struct *work)
  108. {
  109. struct hci_conn *conn = container_of(work, struct hci_conn,
  110. le_scan_cleanup);
  111. struct hci_dev *hdev = conn->hdev;
  112. struct hci_conn *c = NULL;
  113. BT_DBG("%s hcon %p", hdev->name, conn);
  114. hci_dev_lock(hdev);
  115. /* Check that the hci_conn is still around */
  116. rcu_read_lock();
  117. list_for_each_entry_rcu(c, &hdev->conn_hash.list, list) {
  118. if (c == conn)
  119. break;
  120. }
  121. rcu_read_unlock();
  122. if (c == conn) {
  123. hci_connect_le_scan_cleanup(conn);
  124. hci_conn_cleanup(conn);
  125. }
  126. hci_dev_unlock(hdev);
  127. hci_dev_put(hdev);
  128. hci_conn_put(conn);
  129. }
  130. static void hci_connect_le_scan_remove(struct hci_conn *conn)
  131. {
  132. BT_DBG("%s hcon %p", conn->hdev->name, conn);
  133. /* We can't call hci_conn_del/hci_conn_cleanup here since that
  134. * could deadlock with another hci_conn_del() call that's holding
  135. * hci_dev_lock and doing cancel_delayed_work_sync(&conn->disc_work).
  136. * Instead, grab temporary extra references to the hci_dev and
  137. * hci_conn and perform the necessary cleanup in a separate work
  138. * callback.
  139. */
  140. hci_dev_hold(conn->hdev);
  141. hci_conn_get(conn);
  142. /* Even though we hold a reference to the hdev, many other
  143. * things might get cleaned up meanwhile, including the hdev's
  144. * own workqueue, so we can't use that for scheduling.
  145. */
  146. schedule_work(&conn->le_scan_cleanup);
  147. }
  148. static void hci_acl_create_connection(struct hci_conn *conn)
  149. {
  150. struct hci_dev *hdev = conn->hdev;
  151. struct inquiry_entry *ie;
  152. struct hci_cp_create_conn cp;
  153. BT_DBG("hcon %p", conn);
  154. conn->state = BT_CONNECT;
  155. conn->out = true;
  156. conn->role = HCI_ROLE_MASTER;
  157. conn->attempt++;
  158. conn->link_policy = hdev->link_policy;
  159. memset(&cp, 0, sizeof(cp));
  160. bacpy(&cp.bdaddr, &conn->dst);
  161. cp.pscan_rep_mode = 0x02;
  162. ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
  163. if (ie) {
  164. if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
  165. cp.pscan_rep_mode = ie->data.pscan_rep_mode;
  166. cp.pscan_mode = ie->data.pscan_mode;
  167. cp.clock_offset = ie->data.clock_offset |
  168. cpu_to_le16(0x8000);
  169. }
  170. memcpy(conn->dev_class, ie->data.dev_class, 3);
  171. if (ie->data.ssp_mode > 0)
  172. set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
  173. }
  174. cp.pkt_type = cpu_to_le16(conn->pkt_type);
  175. if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
  176. cp.role_switch = 0x01;
  177. else
  178. cp.role_switch = 0x00;
  179. hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
  180. }
  181. int hci_disconnect(struct hci_conn *conn, __u8 reason)
  182. {
  183. BT_DBG("hcon %p", conn);
  184. /* When we are master of an established connection and it enters
  185. * the disconnect timeout, then go ahead and try to read the
  186. * current clock offset. Processing of the result is done
  187. * within the event handling and hci_clock_offset_evt function.
  188. */
  189. if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER &&
  190. (conn->state == BT_CONNECTED || conn->state == BT_CONFIG)) {
  191. struct hci_dev *hdev = conn->hdev;
  192. struct hci_cp_read_clock_offset clkoff_cp;
  193. clkoff_cp.handle = cpu_to_le16(conn->handle);
  194. hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp),
  195. &clkoff_cp);
  196. }
  197. return hci_abort_conn(conn, reason);
  198. }
  199. static void hci_add_sco(struct hci_conn *conn, __u16 handle)
  200. {
  201. struct hci_dev *hdev = conn->hdev;
  202. struct hci_cp_add_sco cp;
  203. BT_DBG("hcon %p", conn);
  204. conn->state = BT_CONNECT;
  205. conn->out = true;
  206. conn->attempt++;
  207. cp.handle = cpu_to_le16(handle);
  208. cp.pkt_type = cpu_to_le16(conn->pkt_type);
  209. hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
  210. }
  211. bool hci_setup_sync(struct hci_conn *conn, __u16 handle)
  212. {
  213. struct hci_dev *hdev = conn->hdev;
  214. struct hci_cp_setup_sync_conn cp;
  215. const struct sco_param *param;
  216. BT_DBG("hcon %p", conn);
  217. conn->state = BT_CONNECT;
  218. conn->out = true;
  219. conn->attempt++;
  220. cp.handle = cpu_to_le16(handle);
  221. cp.tx_bandwidth = cpu_to_le32(0x00001f40);
  222. cp.rx_bandwidth = cpu_to_le32(0x00001f40);
  223. cp.voice_setting = cpu_to_le16(conn->setting);
  224. switch (conn->setting & SCO_AIRMODE_MASK) {
  225. case SCO_AIRMODE_TRANSP:
  226. if (conn->attempt > ARRAY_SIZE(esco_param_msbc))
  227. return false;
  228. param = &esco_param_msbc[conn->attempt - 1];
  229. break;
  230. case SCO_AIRMODE_CVSD:
  231. if (lmp_esco_capable(conn->link)) {
  232. if (conn->attempt > ARRAY_SIZE(esco_param_cvsd))
  233. return false;
  234. param = &esco_param_cvsd[conn->attempt - 1];
  235. } else {
  236. if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
  237. return false;
  238. param = &sco_param_cvsd[conn->attempt - 1];
  239. }
  240. break;
  241. default:
  242. return false;
  243. }
  244. cp.retrans_effort = param->retrans_effort;
  245. cp.pkt_type = __cpu_to_le16(param->pkt_type);
  246. cp.max_latency = __cpu_to_le16(param->max_latency);
  247. if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
  248. return false;
  249. return true;
  250. }
  251. u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
  252. u16 to_multiplier)
  253. {
  254. struct hci_dev *hdev = conn->hdev;
  255. struct hci_conn_params *params;
  256. struct hci_cp_le_conn_update cp;
  257. hci_dev_lock(hdev);
  258. params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
  259. if (params) {
  260. params->conn_min_interval = min;
  261. params->conn_max_interval = max;
  262. params->conn_latency = latency;
  263. params->supervision_timeout = to_multiplier;
  264. }
  265. hci_dev_unlock(hdev);
  266. memset(&cp, 0, sizeof(cp));
  267. cp.handle = cpu_to_le16(conn->handle);
  268. cp.conn_interval_min = cpu_to_le16(min);
  269. cp.conn_interval_max = cpu_to_le16(max);
  270. cp.conn_latency = cpu_to_le16(latency);
  271. cp.supervision_timeout = cpu_to_le16(to_multiplier);
  272. cp.min_ce_len = cpu_to_le16(0x0000);
  273. cp.max_ce_len = cpu_to_le16(0x0000);
  274. hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
  275. if (params)
  276. return 0x01;
  277. return 0x00;
  278. }
  279. void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
  280. __u8 ltk[16], __u8 key_size)
  281. {
  282. struct hci_dev *hdev = conn->hdev;
  283. struct hci_cp_le_start_enc cp;
  284. BT_DBG("hcon %p", conn);
  285. memset(&cp, 0, sizeof(cp));
  286. cp.handle = cpu_to_le16(conn->handle);
  287. cp.rand = rand;
  288. cp.ediv = ediv;
  289. memcpy(cp.ltk, ltk, key_size);
  290. hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
  291. }
  292. /* Device _must_ be locked */
  293. void hci_sco_setup(struct hci_conn *conn, __u8 status)
  294. {
  295. struct hci_conn *sco = conn->link;
  296. if (!sco)
  297. return;
  298. BT_DBG("hcon %p", conn);
  299. if (!status) {
  300. if (lmp_esco_capable(conn->hdev))
  301. hci_setup_sync(sco, conn->handle);
  302. else
  303. hci_add_sco(sco, conn->handle);
  304. } else {
  305. hci_connect_cfm(sco, status);
  306. hci_conn_del(sco);
  307. }
  308. }
  309. static void hci_conn_timeout(struct work_struct *work)
  310. {
  311. struct hci_conn *conn = container_of(work, struct hci_conn,
  312. disc_work.work);
  313. int refcnt = atomic_read(&conn->refcnt);
  314. BT_DBG("hcon %p state %s", conn, state_to_string(conn->state));
  315. WARN_ON(refcnt < 0);
  316. /* FIXME: It was observed that in pairing failed scenario, refcnt
  317. * drops below 0. Probably this is because l2cap_conn_del calls
  318. * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is
  319. * dropped. After that loop hci_chan_del is called which also drops
  320. * conn. For now make sure that ACL is alive if refcnt is higher then 0,
  321. * otherwise drop it.
  322. */
  323. if (refcnt > 0)
  324. return;
  325. /* LE connections in scanning state need special handling */
  326. if (conn->state == BT_CONNECT && conn->type == LE_LINK &&
  327. test_bit(HCI_CONN_SCANNING, &conn->flags)) {
  328. hci_connect_le_scan_remove(conn);
  329. return;
  330. }
  331. hci_abort_conn(conn, hci_proto_disconn_ind(conn));
  332. }
  333. /* Enter sniff mode */
  334. static void hci_conn_idle(struct work_struct *work)
  335. {
  336. struct hci_conn *conn = container_of(work, struct hci_conn,
  337. idle_work.work);
  338. struct hci_dev *hdev = conn->hdev;
  339. BT_DBG("hcon %p mode %d", conn, conn->mode);
  340. if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
  341. return;
  342. if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
  343. return;
  344. if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
  345. struct hci_cp_sniff_subrate cp;
  346. cp.handle = cpu_to_le16(conn->handle);
  347. cp.max_latency = cpu_to_le16(0);
  348. cp.min_remote_timeout = cpu_to_le16(0);
  349. cp.min_local_timeout = cpu_to_le16(0);
  350. hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
  351. }
  352. if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
  353. struct hci_cp_sniff_mode cp;
  354. cp.handle = cpu_to_le16(conn->handle);
  355. cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
  356. cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
  357. cp.attempt = cpu_to_le16(4);
  358. cp.timeout = cpu_to_le16(1);
  359. hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
  360. }
  361. }
  362. static void hci_conn_auto_accept(struct work_struct *work)
  363. {
  364. struct hci_conn *conn = container_of(work, struct hci_conn,
  365. auto_accept_work.work);
  366. hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
  367. &conn->dst);
  368. }
  369. static void le_conn_timeout(struct work_struct *work)
  370. {
  371. struct hci_conn *conn = container_of(work, struct hci_conn,
  372. le_conn_timeout.work);
  373. struct hci_dev *hdev = conn->hdev;
  374. BT_DBG("");
  375. /* We could end up here due to having done directed advertising,
  376. * so clean up the state if necessary. This should however only
  377. * happen with broken hardware or if low duty cycle was used
  378. * (which doesn't have a timeout of its own).
  379. */
  380. if (conn->role == HCI_ROLE_SLAVE) {
  381. u8 enable = 0x00;
  382. hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
  383. &enable);
  384. hci_le_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT);
  385. return;
  386. }
  387. hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
  388. }
  389. struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
  390. u8 role)
  391. {
  392. struct hci_conn *conn;
  393. BT_DBG("%s dst %pMR", hdev->name, dst);
  394. conn = kzalloc(sizeof(*conn), GFP_KERNEL);
  395. if (!conn)
  396. return NULL;
  397. bacpy(&conn->dst, dst);
  398. bacpy(&conn->src, &hdev->bdaddr);
  399. conn->hdev = hdev;
  400. conn->type = type;
  401. conn->role = role;
  402. conn->mode = HCI_CM_ACTIVE;
  403. conn->state = BT_OPEN;
  404. conn->auth_type = HCI_AT_GENERAL_BONDING;
  405. conn->io_capability = hdev->io_capability;
  406. conn->remote_auth = 0xff;
  407. conn->key_type = 0xff;
  408. conn->rssi = HCI_RSSI_INVALID;
  409. conn->tx_power = HCI_TX_POWER_INVALID;
  410. conn->max_tx_power = HCI_TX_POWER_INVALID;
  411. set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
  412. conn->disc_timeout = HCI_DISCONN_TIMEOUT;
  413. if (conn->role == HCI_ROLE_MASTER)
  414. conn->out = true;
  415. switch (type) {
  416. case ACL_LINK:
  417. conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
  418. break;
  419. case LE_LINK:
  420. /* conn->src should reflect the local identity address */
  421. hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
  422. break;
  423. case SCO_LINK:
  424. if (lmp_esco_capable(hdev))
  425. conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
  426. (hdev->esco_type & EDR_ESCO_MASK);
  427. else
  428. conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
  429. break;
  430. case ESCO_LINK:
  431. conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK;
  432. break;
  433. }
  434. skb_queue_head_init(&conn->data_q);
  435. INIT_LIST_HEAD(&conn->chan_list);
  436. INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout);
  437. INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept);
  438. INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle);
  439. INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout);
  440. INIT_WORK(&conn->le_scan_cleanup, le_scan_cleanup);
  441. atomic_set(&conn->refcnt, 0);
  442. hci_dev_hold(hdev);
  443. hci_conn_hash_add(hdev, conn);
  444. if (hdev->notify)
  445. hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
  446. hci_conn_init_sysfs(conn);
  447. return conn;
  448. }
  449. int hci_conn_del(struct hci_conn *conn)
  450. {
  451. struct hci_dev *hdev = conn->hdev;
  452. BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle);
  453. cancel_delayed_work_sync(&conn->disc_work);
  454. cancel_delayed_work_sync(&conn->auto_accept_work);
  455. cancel_delayed_work_sync(&conn->idle_work);
  456. if (conn->type == ACL_LINK) {
  457. struct hci_conn *sco = conn->link;
  458. if (sco)
  459. sco->link = NULL;
  460. /* Unacked frames */
  461. hdev->acl_cnt += conn->sent;
  462. } else if (conn->type == LE_LINK) {
  463. cancel_delayed_work(&conn->le_conn_timeout);
  464. if (hdev->le_pkts)
  465. hdev->le_cnt += conn->sent;
  466. else
  467. hdev->acl_cnt += conn->sent;
  468. } else {
  469. struct hci_conn *acl = conn->link;
  470. if (acl) {
  471. acl->link = NULL;
  472. hci_conn_drop(acl);
  473. }
  474. }
  475. if (conn->amp_mgr)
  476. amp_mgr_put(conn->amp_mgr);
  477. skb_queue_purge(&conn->data_q);
  478. /* Remove the connection from the list and cleanup its remaining
  479. * state. This is a separate function since for some cases like
  480. * BT_CONNECT_SCAN we *only* want the cleanup part without the
  481. * rest of hci_conn_del.
  482. */
  483. hci_conn_cleanup(conn);
  484. return 0;
  485. }
  486. struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, uint8_t src_type)
  487. {
  488. int use_src = bacmp(src, BDADDR_ANY);
  489. struct hci_dev *hdev = NULL, *d;
  490. BT_DBG("%pMR -> %pMR", src, dst);
  491. read_lock(&hci_dev_list_lock);
  492. list_for_each_entry(d, &hci_dev_list, list) {
  493. if (!test_bit(HCI_UP, &d->flags) ||
  494. hci_dev_test_flag(d, HCI_USER_CHANNEL) ||
  495. d->dev_type != HCI_PRIMARY)
  496. continue;
  497. /* Simple routing:
  498. * No source address - find interface with bdaddr != dst
  499. * Source address - find interface with bdaddr == src
  500. */
  501. if (use_src) {
  502. bdaddr_t id_addr;
  503. u8 id_addr_type;
  504. if (src_type == BDADDR_BREDR) {
  505. if (!lmp_bredr_capable(d))
  506. continue;
  507. bacpy(&id_addr, &d->bdaddr);
  508. id_addr_type = BDADDR_BREDR;
  509. } else {
  510. if (!lmp_le_capable(d))
  511. continue;
  512. hci_copy_identity_address(d, &id_addr,
  513. &id_addr_type);
  514. /* Convert from HCI to three-value type */
  515. if (id_addr_type == ADDR_LE_DEV_PUBLIC)
  516. id_addr_type = BDADDR_LE_PUBLIC;
  517. else
  518. id_addr_type = BDADDR_LE_RANDOM;
  519. }
  520. if (!bacmp(&id_addr, src) && id_addr_type == src_type) {
  521. hdev = d; break;
  522. }
  523. } else {
  524. if (bacmp(&d->bdaddr, dst)) {
  525. hdev = d; break;
  526. }
  527. }
  528. }
  529. if (hdev)
  530. hdev = hci_dev_hold(hdev);
  531. read_unlock(&hci_dev_list_lock);
  532. return hdev;
  533. }
  534. EXPORT_SYMBOL(hci_get_route);
  535. /* This function requires the caller holds hdev->lock */
  536. void hci_le_conn_failed(struct hci_conn *conn, u8 status)
  537. {
  538. struct hci_dev *hdev = conn->hdev;
  539. struct hci_conn_params *params;
  540. params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
  541. conn->dst_type);
  542. if (params && params->conn) {
  543. hci_conn_drop(params->conn);
  544. hci_conn_put(params->conn);
  545. params->conn = NULL;
  546. }
  547. conn->state = BT_CLOSED;
  548. /* If the status indicates successful cancellation of
  549. * the attempt (i.e. Unkown Connection Id) there's no point of
  550. * notifying failure since we'll go back to keep trying to
  551. * connect. The only exception is explicit connect requests
  552. * where a timeout + cancel does indicate an actual failure.
  553. */
  554. if (status != HCI_ERROR_UNKNOWN_CONN_ID ||
  555. (params && params->explicit_connect))
  556. mgmt_connect_failed(hdev, &conn->dst, conn->type,
  557. conn->dst_type, status);
  558. hci_connect_cfm(conn, status);
  559. hci_conn_del(conn);
  560. /* Since we may have temporarily stopped the background scanning in
  561. * favor of connection establishment, we should restart it.
  562. */
  563. hci_update_background_scan(hdev);
  564. /* Re-enable advertising in case this was a failed connection
  565. * attempt as a peripheral.
  566. */
  567. hci_req_reenable_advertising(hdev);
  568. }
  569. static void create_le_conn_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  570. {
  571. struct hci_conn *conn;
  572. hci_dev_lock(hdev);
  573. conn = hci_lookup_le_connect(hdev);
  574. if (!status) {
  575. hci_connect_le_scan_cleanup(conn);
  576. goto done;
  577. }
  578. BT_ERR("HCI request failed to create LE connection: status 0x%2.2x",
  579. status);
  580. if (!conn)
  581. goto done;
  582. hci_le_conn_failed(conn, status);
  583. done:
  584. hci_dev_unlock(hdev);
  585. }
  586. static bool conn_use_rpa(struct hci_conn *conn)
  587. {
  588. struct hci_dev *hdev = conn->hdev;
  589. return hci_dev_test_flag(hdev, HCI_PRIVACY);
  590. }
  591. static void hci_req_add_le_create_conn(struct hci_request *req,
  592. struct hci_conn *conn,
  593. bdaddr_t *direct_rpa)
  594. {
  595. struct hci_cp_le_create_conn cp;
  596. struct hci_dev *hdev = conn->hdev;
  597. u8 own_addr_type;
  598. /* If direct address was provided we use it instead of current
  599. * address.
  600. */
  601. if (direct_rpa) {
  602. if (bacmp(&req->hdev->random_addr, direct_rpa))
  603. hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
  604. direct_rpa);
  605. /* direct address is always RPA */
  606. own_addr_type = ADDR_LE_DEV_RANDOM;
  607. } else {
  608. /* Update random address, but set require_privacy to false so
  609. * that we never connect with an non-resolvable address.
  610. */
  611. if (hci_update_random_address(req, false, conn_use_rpa(conn),
  612. &own_addr_type))
  613. return;
  614. }
  615. memset(&cp, 0, sizeof(cp));
  616. /* Set window to be the same value as the interval to enable
  617. * continuous scanning.
  618. */
  619. cp.scan_interval = cpu_to_le16(hdev->le_scan_interval);
  620. cp.scan_window = cp.scan_interval;
  621. bacpy(&cp.peer_addr, &conn->dst);
  622. cp.peer_addr_type = conn->dst_type;
  623. cp.own_address_type = own_addr_type;
  624. cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
  625. cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
  626. cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
  627. cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
  628. cp.min_ce_len = cpu_to_le16(0x0000);
  629. cp.max_ce_len = cpu_to_le16(0x0000);
  630. hci_req_add(req, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
  631. conn->state = BT_CONNECT;
  632. clear_bit(HCI_CONN_SCANNING, &conn->flags);
  633. }
  634. static void hci_req_directed_advertising(struct hci_request *req,
  635. struct hci_conn *conn)
  636. {
  637. struct hci_dev *hdev = req->hdev;
  638. struct hci_cp_le_set_adv_param cp;
  639. u8 own_addr_type;
  640. u8 enable;
  641. /* Clear the HCI_LE_ADV bit temporarily so that the
  642. * hci_update_random_address knows that it's safe to go ahead
  643. * and write a new random address. The flag will be set back on
  644. * as soon as the SET_ADV_ENABLE HCI command completes.
  645. */
  646. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  647. /* Set require_privacy to false so that the remote device has a
  648. * chance of identifying us.
  649. */
  650. if (hci_update_random_address(req, false, conn_use_rpa(conn),
  651. &own_addr_type) < 0)
  652. return;
  653. memset(&cp, 0, sizeof(cp));
  654. cp.type = LE_ADV_DIRECT_IND;
  655. cp.own_address_type = own_addr_type;
  656. cp.direct_addr_type = conn->dst_type;
  657. bacpy(&cp.direct_addr, &conn->dst);
  658. cp.channel_map = hdev->le_adv_channel_map;
  659. hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
  660. enable = 0x01;
  661. hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
  662. conn->state = BT_CONNECT;
  663. }
  664. struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
  665. u8 dst_type, u8 sec_level, u16 conn_timeout,
  666. u8 role, bdaddr_t *direct_rpa)
  667. {
  668. struct hci_conn_params *params;
  669. struct hci_conn *conn;
  670. struct smp_irk *irk;
  671. struct hci_request req;
  672. int err;
  673. /* Let's make sure that le is enabled.*/
  674. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
  675. if (lmp_le_capable(hdev))
  676. return ERR_PTR(-ECONNREFUSED);
  677. return ERR_PTR(-EOPNOTSUPP);
  678. }
  679. /* Since the controller supports only one LE connection attempt at a
  680. * time, we return -EBUSY if there is any connection attempt running.
  681. */
  682. if (hci_lookup_le_connect(hdev))
  683. return ERR_PTR(-EBUSY);
  684. /* If there's already a connection object but it's not in
  685. * scanning state it means it must already be established, in
  686. * which case we can't do anything else except report a failure
  687. * to connect.
  688. */
  689. conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
  690. if (conn && !test_bit(HCI_CONN_SCANNING, &conn->flags)) {
  691. return ERR_PTR(-EBUSY);
  692. }
  693. /* When given an identity address with existing identity
  694. * resolving key, the connection needs to be established
  695. * to a resolvable random address.
  696. *
  697. * Storing the resolvable random address is required here
  698. * to handle connection failures. The address will later
  699. * be resolved back into the original identity address
  700. * from the connect request.
  701. */
  702. irk = hci_find_irk_by_addr(hdev, dst, dst_type);
  703. if (irk && bacmp(&irk->rpa, BDADDR_ANY)) {
  704. dst = &irk->rpa;
  705. dst_type = ADDR_LE_DEV_RANDOM;
  706. }
  707. if (conn) {
  708. bacpy(&conn->dst, dst);
  709. } else {
  710. conn = hci_conn_add(hdev, LE_LINK, dst, role);
  711. if (!conn)
  712. return ERR_PTR(-ENOMEM);
  713. hci_conn_hold(conn);
  714. conn->pending_sec_level = sec_level;
  715. }
  716. conn->dst_type = dst_type;
  717. conn->sec_level = BT_SECURITY_LOW;
  718. conn->conn_timeout = conn_timeout;
  719. hci_req_init(&req, hdev);
  720. /* Disable advertising if we're active. For master role
  721. * connections most controllers will refuse to connect if
  722. * advertising is enabled, and for slave role connections we
  723. * anyway have to disable it in order to start directed
  724. * advertising.
  725. */
  726. if (hci_dev_test_flag(hdev, HCI_LE_ADV)) {
  727. u8 enable = 0x00;
  728. hci_req_add(&req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
  729. &enable);
  730. }
  731. /* If requested to connect as slave use directed advertising */
  732. if (conn->role == HCI_ROLE_SLAVE) {
  733. /* If we're active scanning most controllers are unable
  734. * to initiate advertising. Simply reject the attempt.
  735. */
  736. if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
  737. hdev->le_scan_type == LE_SCAN_ACTIVE) {
  738. skb_queue_purge(&req.cmd_q);
  739. hci_conn_del(conn);
  740. return ERR_PTR(-EBUSY);
  741. }
  742. hci_req_directed_advertising(&req, conn);
  743. goto create_conn;
  744. }
  745. params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
  746. if (params) {
  747. conn->le_conn_min_interval = params->conn_min_interval;
  748. conn->le_conn_max_interval = params->conn_max_interval;
  749. conn->le_conn_latency = params->conn_latency;
  750. conn->le_supv_timeout = params->supervision_timeout;
  751. } else {
  752. conn->le_conn_min_interval = hdev->le_conn_min_interval;
  753. conn->le_conn_max_interval = hdev->le_conn_max_interval;
  754. conn->le_conn_latency = hdev->le_conn_latency;
  755. conn->le_supv_timeout = hdev->le_supv_timeout;
  756. }
  757. /* If controller is scanning, we stop it since some controllers are
  758. * not able to scan and connect at the same time. Also set the
  759. * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
  760. * handler for scan disabling knows to set the correct discovery
  761. * state.
  762. */
  763. if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
  764. hci_req_add_le_scan_disable(&req);
  765. hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
  766. }
  767. hci_req_add_le_create_conn(&req, conn, direct_rpa);
  768. create_conn:
  769. err = hci_req_run(&req, create_le_conn_complete);
  770. if (err) {
  771. hci_conn_del(conn);
  772. return ERR_PTR(err);
  773. }
  774. return conn;
  775. }
  776. static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
  777. {
  778. struct hci_conn *conn;
  779. conn = hci_conn_hash_lookup_le(hdev, addr, type);
  780. if (!conn)
  781. return false;
  782. if (conn->state != BT_CONNECTED)
  783. return false;
  784. return true;
  785. }
  786. /* This function requires the caller holds hdev->lock */
  787. static int hci_explicit_conn_params_set(struct hci_dev *hdev,
  788. bdaddr_t *addr, u8 addr_type)
  789. {
  790. struct hci_conn_params *params;
  791. if (is_connected(hdev, addr, addr_type))
  792. return -EISCONN;
  793. params = hci_conn_params_lookup(hdev, addr, addr_type);
  794. if (!params) {
  795. params = hci_conn_params_add(hdev, addr, addr_type);
  796. if (!params)
  797. return -ENOMEM;
  798. /* If we created new params, mark them to be deleted in
  799. * hci_connect_le_scan_cleanup. It's different case than
  800. * existing disabled params, those will stay after cleanup.
  801. */
  802. params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
  803. }
  804. /* We're trying to connect, so make sure params are at pend_le_conns */
  805. if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
  806. params->auto_connect == HCI_AUTO_CONN_REPORT ||
  807. params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
  808. list_del_init(&params->action);
  809. list_add(&params->action, &hdev->pend_le_conns);
  810. }
  811. params->explicit_connect = true;
  812. BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
  813. params->auto_connect);
  814. return 0;
  815. }
  816. /* This function requires the caller holds hdev->lock */
  817. struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
  818. u8 dst_type, u8 sec_level,
  819. u16 conn_timeout)
  820. {
  821. struct hci_conn *conn;
  822. /* Let's make sure that le is enabled.*/
  823. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
  824. if (lmp_le_capable(hdev))
  825. return ERR_PTR(-ECONNREFUSED);
  826. return ERR_PTR(-EOPNOTSUPP);
  827. }
  828. /* Some devices send ATT messages as soon as the physical link is
  829. * established. To be able to handle these ATT messages, the user-
  830. * space first establishes the connection and then starts the pairing
  831. * process.
  832. *
  833. * So if a hci_conn object already exists for the following connection
  834. * attempt, we simply update pending_sec_level and auth_type fields
  835. * and return the object found.
  836. */
  837. conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
  838. if (conn) {
  839. if (conn->pending_sec_level < sec_level)
  840. conn->pending_sec_level = sec_level;
  841. goto done;
  842. }
  843. BT_DBG("requesting refresh of dst_addr");
  844. conn = hci_conn_add(hdev, LE_LINK, dst, HCI_ROLE_MASTER);
  845. if (!conn)
  846. return ERR_PTR(-ENOMEM);
  847. if (hci_explicit_conn_params_set(hdev, dst, dst_type) < 0)
  848. return ERR_PTR(-EBUSY);
  849. conn->state = BT_CONNECT;
  850. set_bit(HCI_CONN_SCANNING, &conn->flags);
  851. conn->dst_type = dst_type;
  852. conn->sec_level = BT_SECURITY_LOW;
  853. conn->pending_sec_level = sec_level;
  854. conn->conn_timeout = conn_timeout;
  855. hci_update_background_scan(hdev);
  856. done:
  857. hci_conn_hold(conn);
  858. return conn;
  859. }
  860. struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
  861. u8 sec_level, u8 auth_type)
  862. {
  863. struct hci_conn *acl;
  864. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
  865. if (lmp_bredr_capable(hdev))
  866. return ERR_PTR(-ECONNREFUSED);
  867. return ERR_PTR(-EOPNOTSUPP);
  868. }
  869. acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
  870. if (!acl) {
  871. acl = hci_conn_add(hdev, ACL_LINK, dst, HCI_ROLE_MASTER);
  872. if (!acl)
  873. return ERR_PTR(-ENOMEM);
  874. }
  875. hci_conn_hold(acl);
  876. if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
  877. acl->sec_level = BT_SECURITY_LOW;
  878. acl->pending_sec_level = sec_level;
  879. acl->auth_type = auth_type;
  880. hci_acl_create_connection(acl);
  881. }
  882. return acl;
  883. }
  884. struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
  885. __u16 setting)
  886. {
  887. struct hci_conn *acl;
  888. struct hci_conn *sco;
  889. acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING);
  890. if (IS_ERR(acl))
  891. return acl;
  892. sco = hci_conn_hash_lookup_ba(hdev, type, dst);
  893. if (!sco) {
  894. sco = hci_conn_add(hdev, type, dst, HCI_ROLE_MASTER);
  895. if (!sco) {
  896. hci_conn_drop(acl);
  897. return ERR_PTR(-ENOMEM);
  898. }
  899. }
  900. acl->link = sco;
  901. sco->link = acl;
  902. hci_conn_hold(sco);
  903. sco->setting = setting;
  904. if (acl->state == BT_CONNECTED &&
  905. (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
  906. set_bit(HCI_CONN_POWER_SAVE, &acl->flags);
  907. hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON);
  908. if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) {
  909. /* defer SCO setup until mode change completed */
  910. set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags);
  911. return sco;
  912. }
  913. hci_sco_setup(acl, 0x00);
  914. }
  915. return sco;
  916. }
  917. /* Check link security requirement */
  918. int hci_conn_check_link_mode(struct hci_conn *conn)
  919. {
  920. BT_DBG("hcon %p", conn);
  921. /* In Secure Connections Only mode, it is required that Secure
  922. * Connections is used and the link is encrypted with AES-CCM
  923. * using a P-256 authenticated combination key.
  924. */
  925. if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) {
  926. if (!hci_conn_sc_enabled(conn) ||
  927. !test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
  928. conn->key_type != HCI_LK_AUTH_COMBINATION_P256)
  929. return 0;
  930. }
  931. if (hci_conn_ssp_enabled(conn) &&
  932. !test_bit(HCI_CONN_ENCRYPT, &conn->flags))
  933. return 0;
  934. return 1;
  935. }
  936. /* Authenticate remote device */
  937. static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
  938. {
  939. BT_DBG("hcon %p", conn);
  940. if (conn->pending_sec_level > sec_level)
  941. sec_level = conn->pending_sec_level;
  942. if (sec_level > conn->sec_level)
  943. conn->pending_sec_level = sec_level;
  944. else if (test_bit(HCI_CONN_AUTH, &conn->flags))
  945. return 1;
  946. /* Make sure we preserve an existing MITM requirement*/
  947. auth_type |= (conn->auth_type & 0x01);
  948. conn->auth_type = auth_type;
  949. if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
  950. struct hci_cp_auth_requested cp;
  951. cp.handle = cpu_to_le16(conn->handle);
  952. hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
  953. sizeof(cp), &cp);
  954. /* If we're already encrypted set the REAUTH_PEND flag,
  955. * otherwise set the ENCRYPT_PEND.
  956. */
  957. if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
  958. set_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
  959. else
  960. set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
  961. }
  962. return 0;
  963. }
  964. /* Encrypt the the link */
  965. static void hci_conn_encrypt(struct hci_conn *conn)
  966. {
  967. BT_DBG("hcon %p", conn);
  968. if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
  969. struct hci_cp_set_conn_encrypt cp;
  970. cp.handle = cpu_to_le16(conn->handle);
  971. cp.encrypt = 0x01;
  972. hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
  973. &cp);
  974. }
  975. }
  976. /* Enable security */
  977. int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
  978. bool initiator)
  979. {
  980. BT_DBG("hcon %p", conn);
  981. if (conn->type == LE_LINK)
  982. return smp_conn_security(conn, sec_level);
  983. /* For sdp we don't need the link key. */
  984. if (sec_level == BT_SECURITY_SDP)
  985. return 1;
  986. /* For non 2.1 devices and low security level we don't need the link
  987. key. */
  988. if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn))
  989. return 1;
  990. /* For other security levels we need the link key. */
  991. if (!test_bit(HCI_CONN_AUTH, &conn->flags))
  992. goto auth;
  993. /* An authenticated FIPS approved combination key has sufficient
  994. * security for security level 4. */
  995. if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256 &&
  996. sec_level == BT_SECURITY_FIPS)
  997. goto encrypt;
  998. /* An authenticated combination key has sufficient security for
  999. security level 3. */
  1000. if ((conn->key_type == HCI_LK_AUTH_COMBINATION_P192 ||
  1001. conn->key_type == HCI_LK_AUTH_COMBINATION_P256) &&
  1002. sec_level == BT_SECURITY_HIGH)
  1003. goto encrypt;
  1004. /* An unauthenticated combination key has sufficient security for
  1005. security level 1 and 2. */
  1006. if ((conn->key_type == HCI_LK_UNAUTH_COMBINATION_P192 ||
  1007. conn->key_type == HCI_LK_UNAUTH_COMBINATION_P256) &&
  1008. (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW))
  1009. goto encrypt;
  1010. /* A combination key has always sufficient security for the security
  1011. levels 1 or 2. High security level requires the combination key
  1012. is generated using maximum PIN code length (16).
  1013. For pre 2.1 units. */
  1014. if (conn->key_type == HCI_LK_COMBINATION &&
  1015. (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW ||
  1016. conn->pin_length == 16))
  1017. goto encrypt;
  1018. auth:
  1019. if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
  1020. return 0;
  1021. if (initiator)
  1022. set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
  1023. if (!hci_conn_auth(conn, sec_level, auth_type))
  1024. return 0;
  1025. encrypt:
  1026. if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
  1027. return 1;
  1028. hci_conn_encrypt(conn);
  1029. return 0;
  1030. }
  1031. EXPORT_SYMBOL(hci_conn_security);
  1032. /* Check secure link requirement */
  1033. int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
  1034. {
  1035. BT_DBG("hcon %p", conn);
  1036. /* Accept if non-secure or higher security level is required */
  1037. if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS)
  1038. return 1;
  1039. /* Accept if secure or higher security level is already present */
  1040. if (conn->sec_level == BT_SECURITY_HIGH ||
  1041. conn->sec_level == BT_SECURITY_FIPS)
  1042. return 1;
  1043. /* Reject not secure link */
  1044. return 0;
  1045. }
  1046. EXPORT_SYMBOL(hci_conn_check_secure);
  1047. /* Switch role */
  1048. int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
  1049. {
  1050. BT_DBG("hcon %p", conn);
  1051. if (role == conn->role)
  1052. return 1;
  1053. if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) {
  1054. struct hci_cp_switch_role cp;
  1055. bacpy(&cp.bdaddr, &conn->dst);
  1056. cp.role = role;
  1057. hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
  1058. }
  1059. return 0;
  1060. }
  1061. EXPORT_SYMBOL(hci_conn_switch_role);
  1062. /* Enter active mode */
  1063. void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
  1064. {
  1065. struct hci_dev *hdev = conn->hdev;
  1066. BT_DBG("hcon %p mode %d", conn, conn->mode);
  1067. if (conn->mode != HCI_CM_SNIFF)
  1068. goto timer;
  1069. if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active)
  1070. goto timer;
  1071. if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
  1072. struct hci_cp_exit_sniff_mode cp;
  1073. cp.handle = cpu_to_le16(conn->handle);
  1074. hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
  1075. }
  1076. timer:
  1077. if (hdev->idle_timeout > 0)
  1078. queue_delayed_work(hdev->workqueue, &conn->idle_work,
  1079. msecs_to_jiffies(hdev->idle_timeout));
  1080. }
  1081. /* Drop all connection on the device */
  1082. void hci_conn_hash_flush(struct hci_dev *hdev)
  1083. {
  1084. struct hci_conn_hash *h = &hdev->conn_hash;
  1085. struct hci_conn *c, *n;
  1086. BT_DBG("hdev %s", hdev->name);
  1087. list_for_each_entry_safe(c, n, &h->list, list) {
  1088. c->state = BT_CLOSED;
  1089. hci_disconn_cfm(c, HCI_ERROR_LOCAL_HOST_TERM);
  1090. hci_conn_del(c);
  1091. }
  1092. }
  1093. /* Check pending connect attempts */
  1094. void hci_conn_check_pending(struct hci_dev *hdev)
  1095. {
  1096. struct hci_conn *conn;
  1097. BT_DBG("hdev %s", hdev->name);
  1098. hci_dev_lock(hdev);
  1099. conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
  1100. if (conn)
  1101. hci_acl_create_connection(conn);
  1102. hci_dev_unlock(hdev);
  1103. }
  1104. static u32 get_link_mode(struct hci_conn *conn)
  1105. {
  1106. u32 link_mode = 0;
  1107. if (conn->role == HCI_ROLE_MASTER)
  1108. link_mode |= HCI_LM_MASTER;
  1109. if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
  1110. link_mode |= HCI_LM_ENCRYPT;
  1111. if (test_bit(HCI_CONN_AUTH, &conn->flags))
  1112. link_mode |= HCI_LM_AUTH;
  1113. if (test_bit(HCI_CONN_SECURE, &conn->flags))
  1114. link_mode |= HCI_LM_SECURE;
  1115. if (test_bit(HCI_CONN_FIPS, &conn->flags))
  1116. link_mode |= HCI_LM_FIPS;
  1117. return link_mode;
  1118. }
  1119. int hci_get_conn_list(void __user *arg)
  1120. {
  1121. struct hci_conn *c;
  1122. struct hci_conn_list_req req, *cl;
  1123. struct hci_conn_info *ci;
  1124. struct hci_dev *hdev;
  1125. int n = 0, size, err;
  1126. if (copy_from_user(&req, arg, sizeof(req)))
  1127. return -EFAULT;
  1128. if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
  1129. return -EINVAL;
  1130. size = sizeof(req) + req.conn_num * sizeof(*ci);
  1131. cl = kmalloc(size, GFP_KERNEL);
  1132. if (!cl)
  1133. return -ENOMEM;
  1134. hdev = hci_dev_get(req.dev_id);
  1135. if (!hdev) {
  1136. kfree(cl);
  1137. return -ENODEV;
  1138. }
  1139. ci = cl->conn_info;
  1140. hci_dev_lock(hdev);
  1141. list_for_each_entry(c, &hdev->conn_hash.list, list) {
  1142. bacpy(&(ci + n)->bdaddr, &c->dst);
  1143. (ci + n)->handle = c->handle;
  1144. (ci + n)->type = c->type;
  1145. (ci + n)->out = c->out;
  1146. (ci + n)->state = c->state;
  1147. (ci + n)->link_mode = get_link_mode(c);
  1148. if (++n >= req.conn_num)
  1149. break;
  1150. }
  1151. hci_dev_unlock(hdev);
  1152. cl->dev_id = hdev->id;
  1153. cl->conn_num = n;
  1154. size = sizeof(req) + n * sizeof(*ci);
  1155. hci_dev_put(hdev);
  1156. err = copy_to_user(arg, cl, size);
  1157. kfree(cl);
  1158. return err ? -EFAULT : 0;
  1159. }
  1160. int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
  1161. {
  1162. struct hci_conn_info_req req;
  1163. struct hci_conn_info ci;
  1164. struct hci_conn *conn;
  1165. char __user *ptr = arg + sizeof(req);
  1166. if (copy_from_user(&req, arg, sizeof(req)))
  1167. return -EFAULT;
  1168. hci_dev_lock(hdev);
  1169. conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
  1170. if (conn) {
  1171. bacpy(&ci.bdaddr, &conn->dst);
  1172. ci.handle = conn->handle;
  1173. ci.type = conn->type;
  1174. ci.out = conn->out;
  1175. ci.state = conn->state;
  1176. ci.link_mode = get_link_mode(conn);
  1177. }
  1178. hci_dev_unlock(hdev);
  1179. if (!conn)
  1180. return -ENOENT;
  1181. return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
  1182. }
  1183. int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
  1184. {
  1185. struct hci_auth_info_req req;
  1186. struct hci_conn *conn;
  1187. if (copy_from_user(&req, arg, sizeof(req)))
  1188. return -EFAULT;
  1189. hci_dev_lock(hdev);
  1190. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
  1191. if (conn)
  1192. req.type = conn->auth_type;
  1193. hci_dev_unlock(hdev);
  1194. if (!conn)
  1195. return -ENOENT;
  1196. return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
  1197. }
  1198. struct hci_chan *hci_chan_create(struct hci_conn *conn)
  1199. {
  1200. struct hci_dev *hdev = conn->hdev;
  1201. struct hci_chan *chan;
  1202. BT_DBG("%s hcon %p", hdev->name, conn);
  1203. if (test_bit(HCI_CONN_DROP, &conn->flags)) {
  1204. BT_DBG("Refusing to create new hci_chan");
  1205. return NULL;
  1206. }
  1207. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  1208. if (!chan)
  1209. return NULL;
  1210. chan->conn = hci_conn_get(conn);
  1211. skb_queue_head_init(&chan->data_q);
  1212. chan->state = BT_CONNECTED;
  1213. list_add_rcu(&chan->list, &conn->chan_list);
  1214. return chan;
  1215. }
  1216. void hci_chan_del(struct hci_chan *chan)
  1217. {
  1218. struct hci_conn *conn = chan->conn;
  1219. struct hci_dev *hdev = conn->hdev;
  1220. BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan);
  1221. list_del_rcu(&chan->list);
  1222. synchronize_rcu();
  1223. /* Prevent new hci_chan's to be created for this hci_conn */
  1224. set_bit(HCI_CONN_DROP, &conn->flags);
  1225. hci_conn_put(conn);
  1226. skb_queue_purge(&chan->data_q);
  1227. kfree(chan);
  1228. }
  1229. void hci_chan_list_flush(struct hci_conn *conn)
  1230. {
  1231. struct hci_chan *chan, *n;
  1232. BT_DBG("hcon %p", conn);
  1233. list_for_each_entry_safe(chan, n, &conn->chan_list, list)
  1234. hci_chan_del(chan);
  1235. }
  1236. static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon,
  1237. __u16 handle)
  1238. {
  1239. struct hci_chan *hchan;
  1240. list_for_each_entry(hchan, &hcon->chan_list, list) {
  1241. if (hchan->handle == handle)
  1242. return hchan;
  1243. }
  1244. return NULL;
  1245. }
  1246. struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle)
  1247. {
  1248. struct hci_conn_hash *h = &hdev->conn_hash;
  1249. struct hci_conn *hcon;
  1250. struct hci_chan *hchan = NULL;
  1251. rcu_read_lock();
  1252. list_for_each_entry_rcu(hcon, &h->list, list) {
  1253. hchan = __hci_chan_lookup_handle(hcon, handle);
  1254. if (hchan)
  1255. break;
  1256. }
  1257. rcu_read_unlock();
  1258. return hchan;
  1259. }