hci_request.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (C) 2014 Intel Corporation
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License version 2 as
  6. published by the Free Software Foundation;
  7. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  8. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  10. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  11. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  12. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  16. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  17. SOFTWARE IS DISCLAIMED.
  18. */
  19. #include <net/bluetooth/bluetooth.h>
  20. #include <net/bluetooth/hci_core.h>
  21. #include <net/bluetooth/mgmt.h>
  22. #include "smp.h"
  23. #include "hci_request.h"
  24. #define HCI_REQ_DONE 0
  25. #define HCI_REQ_PEND 1
  26. #define HCI_REQ_CANCELED 2
  27. void hci_req_init(struct hci_request *req, struct hci_dev *hdev)
  28. {
  29. skb_queue_head_init(&req->cmd_q);
  30. req->hdev = hdev;
  31. req->err = 0;
  32. }
  33. static int req_run(struct hci_request *req, hci_req_complete_t complete,
  34. hci_req_complete_skb_t complete_skb)
  35. {
  36. struct hci_dev *hdev = req->hdev;
  37. struct sk_buff *skb;
  38. unsigned long flags;
  39. BT_DBG("length %u", skb_queue_len(&req->cmd_q));
  40. /* If an error occurred during request building, remove all HCI
  41. * commands queued on the HCI request queue.
  42. */
  43. if (req->err) {
  44. skb_queue_purge(&req->cmd_q);
  45. return req->err;
  46. }
  47. /* Do not allow empty requests */
  48. if (skb_queue_empty(&req->cmd_q))
  49. return -ENODATA;
  50. skb = skb_peek_tail(&req->cmd_q);
  51. if (complete) {
  52. bt_cb(skb)->hci.req_complete = complete;
  53. } else if (complete_skb) {
  54. bt_cb(skb)->hci.req_complete_skb = complete_skb;
  55. bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
  56. }
  57. spin_lock_irqsave(&hdev->cmd_q.lock, flags);
  58. skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
  59. spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
  60. queue_work(hdev->workqueue, &hdev->cmd_work);
  61. return 0;
  62. }
  63. int hci_req_run(struct hci_request *req, hci_req_complete_t complete)
  64. {
  65. return req_run(req, complete, NULL);
  66. }
  67. int hci_req_run_skb(struct hci_request *req, hci_req_complete_skb_t complete)
  68. {
  69. return req_run(req, NULL, complete);
  70. }
  71. static void hci_req_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
  72. struct sk_buff *skb)
  73. {
  74. BT_DBG("%s result 0x%2.2x", hdev->name, result);
  75. if (hdev->req_status == HCI_REQ_PEND) {
  76. hdev->req_result = result;
  77. hdev->req_status = HCI_REQ_DONE;
  78. if (skb)
  79. hdev->req_skb = skb_get(skb);
  80. wake_up_interruptible(&hdev->req_wait_q);
  81. }
  82. }
  83. void hci_req_sync_cancel(struct hci_dev *hdev, int err)
  84. {
  85. BT_DBG("%s err 0x%2.2x", hdev->name, err);
  86. if (hdev->req_status == HCI_REQ_PEND) {
  87. hdev->req_result = err;
  88. hdev->req_status = HCI_REQ_CANCELED;
  89. wake_up_interruptible(&hdev->req_wait_q);
  90. }
  91. }
  92. struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
  93. const void *param, u8 event, u32 timeout)
  94. {
  95. DECLARE_WAITQUEUE(wait, current);
  96. struct hci_request req;
  97. struct sk_buff *skb;
  98. int err = 0;
  99. BT_DBG("%s", hdev->name);
  100. hci_req_init(&req, hdev);
  101. hci_req_add_ev(&req, opcode, plen, param, event);
  102. hdev->req_status = HCI_REQ_PEND;
  103. add_wait_queue(&hdev->req_wait_q, &wait);
  104. set_current_state(TASK_INTERRUPTIBLE);
  105. err = hci_req_run_skb(&req, hci_req_sync_complete);
  106. if (err < 0) {
  107. remove_wait_queue(&hdev->req_wait_q, &wait);
  108. set_current_state(TASK_RUNNING);
  109. return ERR_PTR(err);
  110. }
  111. schedule_timeout(timeout);
  112. remove_wait_queue(&hdev->req_wait_q, &wait);
  113. if (signal_pending(current))
  114. return ERR_PTR(-EINTR);
  115. switch (hdev->req_status) {
  116. case HCI_REQ_DONE:
  117. err = -bt_to_errno(hdev->req_result);
  118. break;
  119. case HCI_REQ_CANCELED:
  120. err = -hdev->req_result;
  121. break;
  122. default:
  123. err = -ETIMEDOUT;
  124. break;
  125. }
  126. hdev->req_status = hdev->req_result = 0;
  127. skb = hdev->req_skb;
  128. hdev->req_skb = NULL;
  129. BT_DBG("%s end: err %d", hdev->name, err);
  130. if (err < 0) {
  131. kfree_skb(skb);
  132. return ERR_PTR(err);
  133. }
  134. if (!skb)
  135. return ERR_PTR(-ENODATA);
  136. return skb;
  137. }
  138. EXPORT_SYMBOL(__hci_cmd_sync_ev);
  139. struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
  140. const void *param, u32 timeout)
  141. {
  142. return __hci_cmd_sync_ev(hdev, opcode, plen, param, 0, timeout);
  143. }
  144. EXPORT_SYMBOL(__hci_cmd_sync);
  145. /* Execute request and wait for completion. */
  146. int __hci_req_sync(struct hci_dev *hdev, int (*func)(struct hci_request *req,
  147. unsigned long opt),
  148. unsigned long opt, u32 timeout, u8 *hci_status)
  149. {
  150. struct hci_request req;
  151. DECLARE_WAITQUEUE(wait, current);
  152. int err = 0;
  153. BT_DBG("%s start", hdev->name);
  154. hci_req_init(&req, hdev);
  155. hdev->req_status = HCI_REQ_PEND;
  156. err = func(&req, opt);
  157. if (err) {
  158. if (hci_status)
  159. *hci_status = HCI_ERROR_UNSPECIFIED;
  160. return err;
  161. }
  162. add_wait_queue(&hdev->req_wait_q, &wait);
  163. set_current_state(TASK_INTERRUPTIBLE);
  164. err = hci_req_run_skb(&req, hci_req_sync_complete);
  165. if (err < 0) {
  166. hdev->req_status = 0;
  167. remove_wait_queue(&hdev->req_wait_q, &wait);
  168. set_current_state(TASK_RUNNING);
  169. /* ENODATA means the HCI request command queue is empty.
  170. * This can happen when a request with conditionals doesn't
  171. * trigger any commands to be sent. This is normal behavior
  172. * and should not trigger an error return.
  173. */
  174. if (err == -ENODATA) {
  175. if (hci_status)
  176. *hci_status = 0;
  177. return 0;
  178. }
  179. if (hci_status)
  180. *hci_status = HCI_ERROR_UNSPECIFIED;
  181. return err;
  182. }
  183. schedule_timeout(timeout);
  184. remove_wait_queue(&hdev->req_wait_q, &wait);
  185. if (signal_pending(current))
  186. return -EINTR;
  187. switch (hdev->req_status) {
  188. case HCI_REQ_DONE:
  189. err = -bt_to_errno(hdev->req_result);
  190. if (hci_status)
  191. *hci_status = hdev->req_result;
  192. break;
  193. case HCI_REQ_CANCELED:
  194. err = -hdev->req_result;
  195. if (hci_status)
  196. *hci_status = HCI_ERROR_UNSPECIFIED;
  197. break;
  198. default:
  199. err = -ETIMEDOUT;
  200. if (hci_status)
  201. *hci_status = HCI_ERROR_UNSPECIFIED;
  202. break;
  203. }
  204. hdev->req_status = hdev->req_result = 0;
  205. BT_DBG("%s end: err %d", hdev->name, err);
  206. return err;
  207. }
  208. int hci_req_sync(struct hci_dev *hdev, int (*req)(struct hci_request *req,
  209. unsigned long opt),
  210. unsigned long opt, u32 timeout, u8 *hci_status)
  211. {
  212. int ret;
  213. if (!test_bit(HCI_UP, &hdev->flags))
  214. return -ENETDOWN;
  215. /* Serialize all requests */
  216. hci_req_sync_lock(hdev);
  217. ret = __hci_req_sync(hdev, req, opt, timeout, hci_status);
  218. hci_req_sync_unlock(hdev);
  219. return ret;
  220. }
  221. struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, u32 plen,
  222. const void *param)
  223. {
  224. int len = HCI_COMMAND_HDR_SIZE + plen;
  225. struct hci_command_hdr *hdr;
  226. struct sk_buff *skb;
  227. skb = bt_skb_alloc(len, GFP_ATOMIC);
  228. if (!skb)
  229. return NULL;
  230. hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE);
  231. hdr->opcode = cpu_to_le16(opcode);
  232. hdr->plen = plen;
  233. if (plen)
  234. memcpy(skb_put(skb, plen), param, plen);
  235. BT_DBG("skb len %d", skb->len);
  236. hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
  237. hci_skb_opcode(skb) = opcode;
  238. return skb;
  239. }
  240. /* Queue a command to an asynchronous HCI request */
  241. void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen,
  242. const void *param, u8 event)
  243. {
  244. struct hci_dev *hdev = req->hdev;
  245. struct sk_buff *skb;
  246. BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen);
  247. /* If an error occurred during request building, there is no point in
  248. * queueing the HCI command. We can simply return.
  249. */
  250. if (req->err)
  251. return;
  252. skb = hci_prepare_cmd(hdev, opcode, plen, param);
  253. if (!skb) {
  254. BT_ERR("%s no memory for command (opcode 0x%4.4x)",
  255. hdev->name, opcode);
  256. req->err = -ENOMEM;
  257. return;
  258. }
  259. if (skb_queue_empty(&req->cmd_q))
  260. bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
  261. bt_cb(skb)->hci.req_event = event;
  262. skb_queue_tail(&req->cmd_q, skb);
  263. }
  264. void hci_req_add(struct hci_request *req, u16 opcode, u32 plen,
  265. const void *param)
  266. {
  267. hci_req_add_ev(req, opcode, plen, param, 0);
  268. }
  269. /* This function controls the background scanning based on hdev->pend_le_conns
  270. * list. If there are pending LE connection we start the background scanning,
  271. * otherwise we stop it.
  272. *
  273. * This function requires the caller holds hdev->lock.
  274. */
  275. static void __hci_update_background_scan(struct hci_request *req)
  276. {
  277. struct hci_dev *hdev = req->hdev;
  278. if (!test_bit(HCI_UP, &hdev->flags) ||
  279. test_bit(HCI_INIT, &hdev->flags) ||
  280. hci_dev_test_flag(hdev, HCI_SETUP) ||
  281. hci_dev_test_flag(hdev, HCI_CONFIG) ||
  282. hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
  283. hci_dev_test_flag(hdev, HCI_UNREGISTER))
  284. return;
  285. /* No point in doing scanning if LE support hasn't been enabled */
  286. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  287. return;
  288. /* If discovery is active don't interfere with it */
  289. if (hdev->discovery.state != DISCOVERY_STOPPED)
  290. return;
  291. /* Reset RSSI and UUID filters when starting background scanning
  292. * since these filters are meant for service discovery only.
  293. *
  294. * The Start Discovery and Start Service Discovery operations
  295. * ensure to set proper values for RSSI threshold and UUID
  296. * filter list. So it is safe to just reset them here.
  297. */
  298. hci_discovery_filter_clear(hdev);
  299. if (list_empty(&hdev->pend_le_conns) &&
  300. list_empty(&hdev->pend_le_reports)) {
  301. /* If there is no pending LE connections or devices
  302. * to be scanned for, we should stop the background
  303. * scanning.
  304. */
  305. /* If controller is not scanning we are done. */
  306. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
  307. return;
  308. hci_req_add_le_scan_disable(req);
  309. BT_DBG("%s stopping background scanning", hdev->name);
  310. } else {
  311. /* If there is at least one pending LE connection, we should
  312. * keep the background scan running.
  313. */
  314. /* If controller is connecting, we should not start scanning
  315. * since some controllers are not able to scan and connect at
  316. * the same time.
  317. */
  318. if (hci_lookup_le_connect(hdev))
  319. return;
  320. /* If controller is currently scanning, we stop it to ensure we
  321. * don't miss any advertising (due to duplicates filter).
  322. */
  323. if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
  324. hci_req_add_le_scan_disable(req);
  325. hci_req_add_le_passive_scan(req);
  326. BT_DBG("%s starting background scanning", hdev->name);
  327. }
  328. }
  329. void hci_req_add_le_scan_disable(struct hci_request *req)
  330. {
  331. struct hci_cp_le_set_scan_enable cp;
  332. memset(&cp, 0, sizeof(cp));
  333. cp.enable = LE_SCAN_DISABLE;
  334. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
  335. }
  336. static void add_to_white_list(struct hci_request *req,
  337. struct hci_conn_params *params)
  338. {
  339. struct hci_cp_le_add_to_white_list cp;
  340. cp.bdaddr_type = params->addr_type;
  341. bacpy(&cp.bdaddr, &params->addr);
  342. hci_req_add(req, HCI_OP_LE_ADD_TO_WHITE_LIST, sizeof(cp), &cp);
  343. }
  344. static u8 update_white_list(struct hci_request *req)
  345. {
  346. struct hci_dev *hdev = req->hdev;
  347. struct hci_conn_params *params;
  348. struct bdaddr_list *b;
  349. uint8_t white_list_entries = 0;
  350. /* Go through the current white list programmed into the
  351. * controller one by one and check if that address is still
  352. * in the list of pending connections or list of devices to
  353. * report. If not present in either list, then queue the
  354. * command to remove it from the controller.
  355. */
  356. list_for_each_entry(b, &hdev->le_white_list, list) {
  357. struct hci_cp_le_del_from_white_list cp;
  358. if (hci_pend_le_action_lookup(&hdev->pend_le_conns,
  359. &b->bdaddr, b->bdaddr_type) ||
  360. hci_pend_le_action_lookup(&hdev->pend_le_reports,
  361. &b->bdaddr, b->bdaddr_type)) {
  362. white_list_entries++;
  363. continue;
  364. }
  365. cp.bdaddr_type = b->bdaddr_type;
  366. bacpy(&cp.bdaddr, &b->bdaddr);
  367. hci_req_add(req, HCI_OP_LE_DEL_FROM_WHITE_LIST,
  368. sizeof(cp), &cp);
  369. }
  370. /* Since all no longer valid white list entries have been
  371. * removed, walk through the list of pending connections
  372. * and ensure that any new device gets programmed into
  373. * the controller.
  374. *
  375. * If the list of the devices is larger than the list of
  376. * available white list entries in the controller, then
  377. * just abort and return filer policy value to not use the
  378. * white list.
  379. */
  380. list_for_each_entry(params, &hdev->pend_le_conns, action) {
  381. if (hci_bdaddr_list_lookup(&hdev->le_white_list,
  382. &params->addr, params->addr_type))
  383. continue;
  384. if (white_list_entries >= hdev->le_white_list_size) {
  385. /* Select filter policy to accept all advertising */
  386. return 0x00;
  387. }
  388. if (hci_find_irk_by_addr(hdev, &params->addr,
  389. params->addr_type)) {
  390. /* White list can not be used with RPAs */
  391. return 0x00;
  392. }
  393. white_list_entries++;
  394. add_to_white_list(req, params);
  395. }
  396. /* After adding all new pending connections, walk through
  397. * the list of pending reports and also add these to the
  398. * white list if there is still space.
  399. */
  400. list_for_each_entry(params, &hdev->pend_le_reports, action) {
  401. if (hci_bdaddr_list_lookup(&hdev->le_white_list,
  402. &params->addr, params->addr_type))
  403. continue;
  404. if (white_list_entries >= hdev->le_white_list_size) {
  405. /* Select filter policy to accept all advertising */
  406. return 0x00;
  407. }
  408. if (hci_find_irk_by_addr(hdev, &params->addr,
  409. params->addr_type)) {
  410. /* White list can not be used with RPAs */
  411. return 0x00;
  412. }
  413. white_list_entries++;
  414. add_to_white_list(req, params);
  415. }
  416. /* Select filter policy to use white list */
  417. return 0x01;
  418. }
  419. void hci_req_add_le_passive_scan(struct hci_request *req)
  420. {
  421. struct hci_cp_le_set_scan_param param_cp;
  422. struct hci_cp_le_set_scan_enable enable_cp;
  423. struct hci_dev *hdev = req->hdev;
  424. u8 own_addr_type;
  425. u8 filter_policy;
  426. /* Set require_privacy to false since no SCAN_REQ are send
  427. * during passive scanning. Not using an non-resolvable address
  428. * here is important so that peer devices using direct
  429. * advertising with our address will be correctly reported
  430. * by the controller.
  431. */
  432. if (hci_update_random_address(req, false, &own_addr_type))
  433. return;
  434. /* Adding or removing entries from the white list must
  435. * happen before enabling scanning. The controller does
  436. * not allow white list modification while scanning.
  437. */
  438. filter_policy = update_white_list(req);
  439. /* When the controller is using random resolvable addresses and
  440. * with that having LE privacy enabled, then controllers with
  441. * Extended Scanner Filter Policies support can now enable support
  442. * for handling directed advertising.
  443. *
  444. * So instead of using filter polices 0x00 (no whitelist)
  445. * and 0x01 (whitelist enabled) use the new filter policies
  446. * 0x02 (no whitelist) and 0x03 (whitelist enabled).
  447. */
  448. if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
  449. (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
  450. filter_policy |= 0x02;
  451. memset(&param_cp, 0, sizeof(param_cp));
  452. param_cp.type = LE_SCAN_PASSIVE;
  453. param_cp.interval = cpu_to_le16(hdev->le_scan_interval);
  454. param_cp.window = cpu_to_le16(hdev->le_scan_window);
  455. param_cp.own_address_type = own_addr_type;
  456. param_cp.filter_policy = filter_policy;
  457. hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
  458. &param_cp);
  459. memset(&enable_cp, 0, sizeof(enable_cp));
  460. enable_cp.enable = LE_SCAN_ENABLE;
  461. enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
  462. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
  463. &enable_cp);
  464. }
  465. static u8 get_current_adv_instance(struct hci_dev *hdev)
  466. {
  467. /* The "Set Advertising" setting supersedes the "Add Advertising"
  468. * setting. Here we set the advertising data based on which
  469. * setting was set. When neither apply, default to the global settings,
  470. * represented by instance "0".
  471. */
  472. if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
  473. !hci_dev_test_flag(hdev, HCI_ADVERTISING))
  474. return hdev->cur_adv_instance;
  475. return 0x00;
  476. }
  477. static u8 get_cur_adv_instance_scan_rsp_len(struct hci_dev *hdev)
  478. {
  479. u8 instance = get_current_adv_instance(hdev);
  480. struct adv_info *adv_instance;
  481. /* Ignore instance 0 */
  482. if (instance == 0x00)
  483. return 0;
  484. adv_instance = hci_find_adv_instance(hdev, instance);
  485. if (!adv_instance)
  486. return 0;
  487. /* TODO: Take into account the "appearance" and "local-name" flags here.
  488. * These are currently being ignored as they are not supported.
  489. */
  490. return adv_instance->scan_rsp_len;
  491. }
  492. void __hci_req_disable_advertising(struct hci_request *req)
  493. {
  494. u8 enable = 0x00;
  495. hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
  496. }
  497. static u32 get_adv_instance_flags(struct hci_dev *hdev, u8 instance)
  498. {
  499. u32 flags;
  500. struct adv_info *adv_instance;
  501. if (instance == 0x00) {
  502. /* Instance 0 always manages the "Tx Power" and "Flags"
  503. * fields
  504. */
  505. flags = MGMT_ADV_FLAG_TX_POWER | MGMT_ADV_FLAG_MANAGED_FLAGS;
  506. /* For instance 0, the HCI_ADVERTISING_CONNECTABLE setting
  507. * corresponds to the "connectable" instance flag.
  508. */
  509. if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE))
  510. flags |= MGMT_ADV_FLAG_CONNECTABLE;
  511. return flags;
  512. }
  513. adv_instance = hci_find_adv_instance(hdev, instance);
  514. /* Return 0 when we got an invalid instance identifier. */
  515. if (!adv_instance)
  516. return 0;
  517. return adv_instance->flags;
  518. }
  519. void __hci_req_enable_advertising(struct hci_request *req)
  520. {
  521. struct hci_dev *hdev = req->hdev;
  522. struct hci_cp_le_set_adv_param cp;
  523. u8 own_addr_type, enable = 0x01;
  524. bool connectable;
  525. u8 instance;
  526. u32 flags;
  527. if (hci_conn_num(hdev, LE_LINK) > 0)
  528. return;
  529. if (hci_dev_test_flag(hdev, HCI_LE_ADV))
  530. __hci_req_disable_advertising(req);
  531. /* Clear the HCI_LE_ADV bit temporarily so that the
  532. * hci_update_random_address knows that it's safe to go ahead
  533. * and write a new random address. The flag will be set back on
  534. * as soon as the SET_ADV_ENABLE HCI command completes.
  535. */
  536. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  537. instance = get_current_adv_instance(hdev);
  538. flags = get_adv_instance_flags(hdev, instance);
  539. /* If the "connectable" instance flag was not set, then choose between
  540. * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
  541. */
  542. connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
  543. mgmt_get_connectable(hdev);
  544. /* Set require_privacy to true only when non-connectable
  545. * advertising is used. In that case it is fine to use a
  546. * non-resolvable private address.
  547. */
  548. if (hci_update_random_address(req, !connectable, &own_addr_type) < 0)
  549. return;
  550. memset(&cp, 0, sizeof(cp));
  551. cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval);
  552. cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval);
  553. if (connectable)
  554. cp.type = LE_ADV_IND;
  555. else if (get_cur_adv_instance_scan_rsp_len(hdev))
  556. cp.type = LE_ADV_SCAN_IND;
  557. else
  558. cp.type = LE_ADV_NONCONN_IND;
  559. cp.own_address_type = own_addr_type;
  560. cp.channel_map = hdev->le_adv_channel_map;
  561. hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
  562. hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
  563. }
  564. static u8 create_default_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
  565. {
  566. u8 ad_len = 0;
  567. size_t name_len;
  568. name_len = strlen(hdev->dev_name);
  569. if (name_len > 0) {
  570. size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2;
  571. if (name_len > max_len) {
  572. name_len = max_len;
  573. ptr[1] = EIR_NAME_SHORT;
  574. } else
  575. ptr[1] = EIR_NAME_COMPLETE;
  576. ptr[0] = name_len + 1;
  577. memcpy(ptr + 2, hdev->dev_name, name_len);
  578. ad_len += (name_len + 2);
  579. ptr += (name_len + 2);
  580. }
  581. return ad_len;
  582. }
  583. static u8 create_instance_scan_rsp_data(struct hci_dev *hdev, u8 instance,
  584. u8 *ptr)
  585. {
  586. struct adv_info *adv_instance;
  587. adv_instance = hci_find_adv_instance(hdev, instance);
  588. if (!adv_instance)
  589. return 0;
  590. /* TODO: Set the appropriate entries based on advertising instance flags
  591. * here once flags other than 0 are supported.
  592. */
  593. memcpy(ptr, adv_instance->scan_rsp_data,
  594. adv_instance->scan_rsp_len);
  595. return adv_instance->scan_rsp_len;
  596. }
  597. static void update_inst_scan_rsp_data(struct hci_request *req, u8 instance)
  598. {
  599. struct hci_dev *hdev = req->hdev;
  600. struct hci_cp_le_set_scan_rsp_data cp;
  601. u8 len;
  602. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  603. return;
  604. memset(&cp, 0, sizeof(cp));
  605. if (instance)
  606. len = create_instance_scan_rsp_data(hdev, instance, cp.data);
  607. else
  608. len = create_default_scan_rsp_data(hdev, cp.data);
  609. if (hdev->scan_rsp_data_len == len &&
  610. !memcmp(cp.data, hdev->scan_rsp_data, len))
  611. return;
  612. memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
  613. hdev->scan_rsp_data_len = len;
  614. cp.length = len;
  615. hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp);
  616. }
  617. void __hci_req_update_scan_rsp_data(struct hci_request *req, int instance)
  618. {
  619. if (instance == HCI_ADV_CURRENT)
  620. instance = get_current_adv_instance(req->hdev);
  621. update_inst_scan_rsp_data(req, get_current_adv_instance(req->hdev));
  622. }
  623. static u8 create_instance_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
  624. {
  625. struct adv_info *adv_instance = NULL;
  626. u8 ad_len = 0, flags = 0;
  627. u32 instance_flags;
  628. /* Return 0 when the current instance identifier is invalid. */
  629. if (instance) {
  630. adv_instance = hci_find_adv_instance(hdev, instance);
  631. if (!adv_instance)
  632. return 0;
  633. }
  634. instance_flags = get_adv_instance_flags(hdev, instance);
  635. /* The Add Advertising command allows userspace to set both the general
  636. * and limited discoverable flags.
  637. */
  638. if (instance_flags & MGMT_ADV_FLAG_DISCOV)
  639. flags |= LE_AD_GENERAL;
  640. if (instance_flags & MGMT_ADV_FLAG_LIMITED_DISCOV)
  641. flags |= LE_AD_LIMITED;
  642. if (flags || (instance_flags & MGMT_ADV_FLAG_MANAGED_FLAGS)) {
  643. /* If a discovery flag wasn't provided, simply use the global
  644. * settings.
  645. */
  646. if (!flags)
  647. flags |= mgmt_get_adv_discov_flags(hdev);
  648. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  649. flags |= LE_AD_NO_BREDR;
  650. /* If flags would still be empty, then there is no need to
  651. * include the "Flags" AD field".
  652. */
  653. if (flags) {
  654. ptr[0] = 0x02;
  655. ptr[1] = EIR_FLAGS;
  656. ptr[2] = flags;
  657. ad_len += 3;
  658. ptr += 3;
  659. }
  660. }
  661. if (adv_instance) {
  662. memcpy(ptr, adv_instance->adv_data,
  663. adv_instance->adv_data_len);
  664. ad_len += adv_instance->adv_data_len;
  665. ptr += adv_instance->adv_data_len;
  666. }
  667. /* Provide Tx Power only if we can provide a valid value for it */
  668. if (hdev->adv_tx_power != HCI_TX_POWER_INVALID &&
  669. (instance_flags & MGMT_ADV_FLAG_TX_POWER)) {
  670. ptr[0] = 0x02;
  671. ptr[1] = EIR_TX_POWER;
  672. ptr[2] = (u8)hdev->adv_tx_power;
  673. ad_len += 3;
  674. ptr += 3;
  675. }
  676. return ad_len;
  677. }
  678. static void update_inst_adv_data(struct hci_request *req, u8 instance)
  679. {
  680. struct hci_dev *hdev = req->hdev;
  681. struct hci_cp_le_set_adv_data cp;
  682. u8 len;
  683. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  684. return;
  685. memset(&cp, 0, sizeof(cp));
  686. len = create_instance_adv_data(hdev, instance, cp.data);
  687. /* There's nothing to do if the data hasn't changed */
  688. if (hdev->adv_data_len == len &&
  689. memcmp(cp.data, hdev->adv_data, len) == 0)
  690. return;
  691. memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
  692. hdev->adv_data_len = len;
  693. cp.length = len;
  694. hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
  695. }
  696. void __hci_req_update_adv_data(struct hci_request *req, int instance)
  697. {
  698. if (instance == HCI_ADV_CURRENT)
  699. instance = get_current_adv_instance(req->hdev);
  700. update_inst_adv_data(req, instance);
  701. }
  702. int hci_req_update_adv_data(struct hci_dev *hdev, int instance)
  703. {
  704. struct hci_request req;
  705. hci_req_init(&req, hdev);
  706. __hci_req_update_adv_data(&req, instance);
  707. return hci_req_run(&req, NULL);
  708. }
  709. static void adv_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  710. {
  711. BT_DBG("%s status %u", hdev->name, status);
  712. }
  713. void hci_req_reenable_advertising(struct hci_dev *hdev)
  714. {
  715. struct hci_request req;
  716. u8 instance;
  717. if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
  718. !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
  719. return;
  720. instance = get_current_adv_instance(hdev);
  721. hci_req_init(&req, hdev);
  722. if (instance) {
  723. __hci_req_schedule_adv_instance(&req, instance, true);
  724. } else {
  725. __hci_req_update_adv_data(&req, HCI_ADV_CURRENT);
  726. __hci_req_update_scan_rsp_data(&req, HCI_ADV_CURRENT);
  727. __hci_req_enable_advertising(&req);
  728. }
  729. hci_req_run(&req, adv_enable_complete);
  730. }
  731. static void adv_timeout_expire(struct work_struct *work)
  732. {
  733. struct hci_dev *hdev = container_of(work, struct hci_dev,
  734. adv_instance_expire.work);
  735. struct hci_request req;
  736. u8 instance;
  737. BT_DBG("%s", hdev->name);
  738. hci_dev_lock(hdev);
  739. hdev->adv_instance_timeout = 0;
  740. instance = get_current_adv_instance(hdev);
  741. if (instance == 0x00)
  742. goto unlock;
  743. hci_req_init(&req, hdev);
  744. hci_req_clear_adv_instance(hdev, &req, instance, false);
  745. if (list_empty(&hdev->adv_instances))
  746. __hci_req_disable_advertising(&req);
  747. if (!skb_queue_empty(&req.cmd_q))
  748. hci_req_run(&req, NULL);
  749. unlock:
  750. hci_dev_unlock(hdev);
  751. }
  752. int __hci_req_schedule_adv_instance(struct hci_request *req, u8 instance,
  753. bool force)
  754. {
  755. struct hci_dev *hdev = req->hdev;
  756. struct adv_info *adv_instance = NULL;
  757. u16 timeout;
  758. if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
  759. !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
  760. return -EPERM;
  761. if (hdev->adv_instance_timeout)
  762. return -EBUSY;
  763. adv_instance = hci_find_adv_instance(hdev, instance);
  764. if (!adv_instance)
  765. return -ENOENT;
  766. /* A zero timeout means unlimited advertising. As long as there is
  767. * only one instance, duration should be ignored. We still set a timeout
  768. * in case further instances are being added later on.
  769. *
  770. * If the remaining lifetime of the instance is more than the duration
  771. * then the timeout corresponds to the duration, otherwise it will be
  772. * reduced to the remaining instance lifetime.
  773. */
  774. if (adv_instance->timeout == 0 ||
  775. adv_instance->duration <= adv_instance->remaining_time)
  776. timeout = adv_instance->duration;
  777. else
  778. timeout = adv_instance->remaining_time;
  779. /* The remaining time is being reduced unless the instance is being
  780. * advertised without time limit.
  781. */
  782. if (adv_instance->timeout)
  783. adv_instance->remaining_time =
  784. adv_instance->remaining_time - timeout;
  785. hdev->adv_instance_timeout = timeout;
  786. queue_delayed_work(hdev->req_workqueue,
  787. &hdev->adv_instance_expire,
  788. msecs_to_jiffies(timeout * 1000));
  789. /* If we're just re-scheduling the same instance again then do not
  790. * execute any HCI commands. This happens when a single instance is
  791. * being advertised.
  792. */
  793. if (!force && hdev->cur_adv_instance == instance &&
  794. hci_dev_test_flag(hdev, HCI_LE_ADV))
  795. return 0;
  796. hdev->cur_adv_instance = instance;
  797. __hci_req_update_adv_data(req, HCI_ADV_CURRENT);
  798. __hci_req_update_scan_rsp_data(req, HCI_ADV_CURRENT);
  799. __hci_req_enable_advertising(req);
  800. return 0;
  801. }
  802. static void cancel_adv_timeout(struct hci_dev *hdev)
  803. {
  804. if (hdev->adv_instance_timeout) {
  805. hdev->adv_instance_timeout = 0;
  806. cancel_delayed_work(&hdev->adv_instance_expire);
  807. }
  808. }
  809. /* For a single instance:
  810. * - force == true: The instance will be removed even when its remaining
  811. * lifetime is not zero.
  812. * - force == false: the instance will be deactivated but kept stored unless
  813. * the remaining lifetime is zero.
  814. *
  815. * For instance == 0x00:
  816. * - force == true: All instances will be removed regardless of their timeout
  817. * setting.
  818. * - force == false: Only instances that have a timeout will be removed.
  819. */
  820. void hci_req_clear_adv_instance(struct hci_dev *hdev, struct hci_request *req,
  821. u8 instance, bool force)
  822. {
  823. struct adv_info *adv_instance, *n, *next_instance = NULL;
  824. int err;
  825. u8 rem_inst;
  826. /* Cancel any timeout concerning the removed instance(s). */
  827. if (!instance || hdev->cur_adv_instance == instance)
  828. cancel_adv_timeout(hdev);
  829. /* Get the next instance to advertise BEFORE we remove
  830. * the current one. This can be the same instance again
  831. * if there is only one instance.
  832. */
  833. if (instance && hdev->cur_adv_instance == instance)
  834. next_instance = hci_get_next_instance(hdev, instance);
  835. if (instance == 0x00) {
  836. list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
  837. list) {
  838. if (!(force || adv_instance->timeout))
  839. continue;
  840. rem_inst = adv_instance->instance;
  841. err = hci_remove_adv_instance(hdev, rem_inst);
  842. if (!err)
  843. mgmt_advertising_removed(NULL, hdev, rem_inst);
  844. }
  845. hdev->cur_adv_instance = 0x00;
  846. } else {
  847. adv_instance = hci_find_adv_instance(hdev, instance);
  848. if (force || (adv_instance && adv_instance->timeout &&
  849. !adv_instance->remaining_time)) {
  850. /* Don't advertise a removed instance. */
  851. if (next_instance &&
  852. next_instance->instance == instance)
  853. next_instance = NULL;
  854. err = hci_remove_adv_instance(hdev, instance);
  855. if (!err)
  856. mgmt_advertising_removed(NULL, hdev, instance);
  857. }
  858. }
  859. if (list_empty(&hdev->adv_instances)) {
  860. hdev->cur_adv_instance = 0x00;
  861. hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
  862. }
  863. if (!req || !hdev_is_powered(hdev) ||
  864. hci_dev_test_flag(hdev, HCI_ADVERTISING))
  865. return;
  866. if (next_instance)
  867. __hci_req_schedule_adv_instance(req, next_instance->instance,
  868. false);
  869. }
  870. static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
  871. {
  872. struct hci_dev *hdev = req->hdev;
  873. /* If we're advertising or initiating an LE connection we can't
  874. * go ahead and change the random address at this time. This is
  875. * because the eventual initiator address used for the
  876. * subsequently created connection will be undefined (some
  877. * controllers use the new address and others the one we had
  878. * when the operation started).
  879. *
  880. * In this kind of scenario skip the update and let the random
  881. * address be updated at the next cycle.
  882. */
  883. if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
  884. hci_lookup_le_connect(hdev)) {
  885. BT_DBG("Deferring random address update");
  886. hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
  887. return;
  888. }
  889. hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, rpa);
  890. }
  891. int hci_update_random_address(struct hci_request *req, bool require_privacy,
  892. u8 *own_addr_type)
  893. {
  894. struct hci_dev *hdev = req->hdev;
  895. int err;
  896. /* If privacy is enabled use a resolvable private address. If
  897. * current RPA has expired or there is something else than
  898. * the current RPA in use, then generate a new one.
  899. */
  900. if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
  901. int to;
  902. *own_addr_type = ADDR_LE_DEV_RANDOM;
  903. if (!hci_dev_test_and_clear_flag(hdev, HCI_RPA_EXPIRED) &&
  904. !bacmp(&hdev->random_addr, &hdev->rpa))
  905. return 0;
  906. err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
  907. if (err < 0) {
  908. BT_ERR("%s failed to generate new RPA", hdev->name);
  909. return err;
  910. }
  911. set_random_addr(req, &hdev->rpa);
  912. to = msecs_to_jiffies(hdev->rpa_timeout * 1000);
  913. queue_delayed_work(hdev->workqueue, &hdev->rpa_expired, to);
  914. return 0;
  915. }
  916. /* In case of required privacy without resolvable private address,
  917. * use an non-resolvable private address. This is useful for active
  918. * scanning and non-connectable advertising.
  919. */
  920. if (require_privacy) {
  921. bdaddr_t nrpa;
  922. while (true) {
  923. /* The non-resolvable private address is generated
  924. * from random six bytes with the two most significant
  925. * bits cleared.
  926. */
  927. get_random_bytes(&nrpa, 6);
  928. nrpa.b[5] &= 0x3f;
  929. /* The non-resolvable private address shall not be
  930. * equal to the public address.
  931. */
  932. if (bacmp(&hdev->bdaddr, &nrpa))
  933. break;
  934. }
  935. *own_addr_type = ADDR_LE_DEV_RANDOM;
  936. set_random_addr(req, &nrpa);
  937. return 0;
  938. }
  939. /* If forcing static address is in use or there is no public
  940. * address use the static address as random address (but skip
  941. * the HCI command if the current random address is already the
  942. * static one.
  943. *
  944. * In case BR/EDR has been disabled on a dual-mode controller
  945. * and a static address has been configured, then use that
  946. * address instead of the public BR/EDR address.
  947. */
  948. if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
  949. !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
  950. (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
  951. bacmp(&hdev->static_addr, BDADDR_ANY))) {
  952. *own_addr_type = ADDR_LE_DEV_RANDOM;
  953. if (bacmp(&hdev->static_addr, &hdev->random_addr))
  954. hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
  955. &hdev->static_addr);
  956. return 0;
  957. }
  958. /* Neither privacy nor static address is being used so use a
  959. * public address.
  960. */
  961. *own_addr_type = ADDR_LE_DEV_PUBLIC;
  962. return 0;
  963. }
  964. static bool disconnected_whitelist_entries(struct hci_dev *hdev)
  965. {
  966. struct bdaddr_list *b;
  967. list_for_each_entry(b, &hdev->whitelist, list) {
  968. struct hci_conn *conn;
  969. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
  970. if (!conn)
  971. return true;
  972. if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
  973. return true;
  974. }
  975. return false;
  976. }
  977. void __hci_req_update_scan(struct hci_request *req)
  978. {
  979. struct hci_dev *hdev = req->hdev;
  980. u8 scan;
  981. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  982. return;
  983. if (!hdev_is_powered(hdev))
  984. return;
  985. if (mgmt_powering_down(hdev))
  986. return;
  987. if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
  988. disconnected_whitelist_entries(hdev))
  989. scan = SCAN_PAGE;
  990. else
  991. scan = SCAN_DISABLED;
  992. if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
  993. scan |= SCAN_INQUIRY;
  994. if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
  995. test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
  996. return;
  997. hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
  998. }
  999. static int update_scan(struct hci_request *req, unsigned long opt)
  1000. {
  1001. hci_dev_lock(req->hdev);
  1002. __hci_req_update_scan(req);
  1003. hci_dev_unlock(req->hdev);
  1004. return 0;
  1005. }
  1006. static void scan_update_work(struct work_struct *work)
  1007. {
  1008. struct hci_dev *hdev = container_of(work, struct hci_dev, scan_update);
  1009. hci_req_sync(hdev, update_scan, 0, HCI_CMD_TIMEOUT, NULL);
  1010. }
  1011. void __hci_abort_conn(struct hci_request *req, struct hci_conn *conn,
  1012. u8 reason)
  1013. {
  1014. switch (conn->state) {
  1015. case BT_CONNECTED:
  1016. case BT_CONFIG:
  1017. if (conn->type == AMP_LINK) {
  1018. struct hci_cp_disconn_phy_link cp;
  1019. cp.phy_handle = HCI_PHY_HANDLE(conn->handle);
  1020. cp.reason = reason;
  1021. hci_req_add(req, HCI_OP_DISCONN_PHY_LINK, sizeof(cp),
  1022. &cp);
  1023. } else {
  1024. struct hci_cp_disconnect dc;
  1025. dc.handle = cpu_to_le16(conn->handle);
  1026. dc.reason = reason;
  1027. hci_req_add(req, HCI_OP_DISCONNECT, sizeof(dc), &dc);
  1028. }
  1029. conn->state = BT_DISCONN;
  1030. break;
  1031. case BT_CONNECT:
  1032. if (conn->type == LE_LINK) {
  1033. if (test_bit(HCI_CONN_SCANNING, &conn->flags))
  1034. break;
  1035. hci_req_add(req, HCI_OP_LE_CREATE_CONN_CANCEL,
  1036. 0, NULL);
  1037. } else if (conn->type == ACL_LINK) {
  1038. if (req->hdev->hci_ver < BLUETOOTH_VER_1_2)
  1039. break;
  1040. hci_req_add(req, HCI_OP_CREATE_CONN_CANCEL,
  1041. 6, &conn->dst);
  1042. }
  1043. break;
  1044. case BT_CONNECT2:
  1045. if (conn->type == ACL_LINK) {
  1046. struct hci_cp_reject_conn_req rej;
  1047. bacpy(&rej.bdaddr, &conn->dst);
  1048. rej.reason = reason;
  1049. hci_req_add(req, HCI_OP_REJECT_CONN_REQ,
  1050. sizeof(rej), &rej);
  1051. } else if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
  1052. struct hci_cp_reject_sync_conn_req rej;
  1053. bacpy(&rej.bdaddr, &conn->dst);
  1054. /* SCO rejection has its own limited set of
  1055. * allowed error values (0x0D-0x0F) which isn't
  1056. * compatible with most values passed to this
  1057. * function. To be safe hard-code one of the
  1058. * values that's suitable for SCO.
  1059. */
  1060. rej.reason = HCI_ERROR_REMOTE_LOW_RESOURCES;
  1061. hci_req_add(req, HCI_OP_REJECT_SYNC_CONN_REQ,
  1062. sizeof(rej), &rej);
  1063. }
  1064. break;
  1065. default:
  1066. conn->state = BT_CLOSED;
  1067. break;
  1068. }
  1069. }
  1070. static void abort_conn_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  1071. {
  1072. if (status)
  1073. BT_DBG("Failed to abort connection: status 0x%2.2x", status);
  1074. }
  1075. int hci_abort_conn(struct hci_conn *conn, u8 reason)
  1076. {
  1077. struct hci_request req;
  1078. int err;
  1079. hci_req_init(&req, conn->hdev);
  1080. __hci_abort_conn(&req, conn, reason);
  1081. err = hci_req_run(&req, abort_conn_complete);
  1082. if (err && err != -ENODATA) {
  1083. BT_ERR("Failed to run HCI request: err %d", err);
  1084. return err;
  1085. }
  1086. return 0;
  1087. }
  1088. static int update_bg_scan(struct hci_request *req, unsigned long opt)
  1089. {
  1090. hci_dev_lock(req->hdev);
  1091. __hci_update_background_scan(req);
  1092. hci_dev_unlock(req->hdev);
  1093. return 0;
  1094. }
  1095. static void bg_scan_update(struct work_struct *work)
  1096. {
  1097. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1098. bg_scan_update);
  1099. struct hci_conn *conn;
  1100. u8 status;
  1101. int err;
  1102. err = hci_req_sync(hdev, update_bg_scan, 0, HCI_CMD_TIMEOUT, &status);
  1103. if (!err)
  1104. return;
  1105. hci_dev_lock(hdev);
  1106. conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
  1107. if (conn)
  1108. hci_le_conn_failed(conn, status);
  1109. hci_dev_unlock(hdev);
  1110. }
  1111. static int le_scan_disable(struct hci_request *req, unsigned long opt)
  1112. {
  1113. hci_req_add_le_scan_disable(req);
  1114. return 0;
  1115. }
  1116. static int bredr_inquiry(struct hci_request *req, unsigned long opt)
  1117. {
  1118. u8 length = opt;
  1119. /* General inquiry access code (GIAC) */
  1120. u8 lap[3] = { 0x33, 0x8b, 0x9e };
  1121. struct hci_cp_inquiry cp;
  1122. BT_DBG("%s", req->hdev->name);
  1123. hci_dev_lock(req->hdev);
  1124. hci_inquiry_cache_flush(req->hdev);
  1125. hci_dev_unlock(req->hdev);
  1126. memset(&cp, 0, sizeof(cp));
  1127. memcpy(&cp.lap, lap, sizeof(cp.lap));
  1128. cp.length = length;
  1129. hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp);
  1130. return 0;
  1131. }
  1132. static void le_scan_disable_work(struct work_struct *work)
  1133. {
  1134. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1135. le_scan_disable.work);
  1136. u8 status;
  1137. BT_DBG("%s", hdev->name);
  1138. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
  1139. return;
  1140. cancel_delayed_work(&hdev->le_scan_restart);
  1141. hci_req_sync(hdev, le_scan_disable, 0, HCI_CMD_TIMEOUT, &status);
  1142. if (status) {
  1143. BT_ERR("Failed to disable LE scan: status 0x%02x", status);
  1144. return;
  1145. }
  1146. hdev->discovery.scan_start = 0;
  1147. /* If we were running LE only scan, change discovery state. If
  1148. * we were running both LE and BR/EDR inquiry simultaneously,
  1149. * and BR/EDR inquiry is already finished, stop discovery,
  1150. * otherwise BR/EDR inquiry will stop discovery when finished.
  1151. * If we will resolve remote device name, do not change
  1152. * discovery state.
  1153. */
  1154. if (hdev->discovery.type == DISCOV_TYPE_LE)
  1155. goto discov_stopped;
  1156. if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED)
  1157. return;
  1158. if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) {
  1159. if (!test_bit(HCI_INQUIRY, &hdev->flags) &&
  1160. hdev->discovery.state != DISCOVERY_RESOLVING)
  1161. goto discov_stopped;
  1162. return;
  1163. }
  1164. hci_req_sync(hdev, bredr_inquiry, DISCOV_INTERLEAVED_INQUIRY_LEN,
  1165. HCI_CMD_TIMEOUT, &status);
  1166. if (status) {
  1167. BT_ERR("Inquiry failed: status 0x%02x", status);
  1168. goto discov_stopped;
  1169. }
  1170. return;
  1171. discov_stopped:
  1172. hci_dev_lock(hdev);
  1173. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  1174. hci_dev_unlock(hdev);
  1175. }
  1176. static int le_scan_restart(struct hci_request *req, unsigned long opt)
  1177. {
  1178. struct hci_dev *hdev = req->hdev;
  1179. struct hci_cp_le_set_scan_enable cp;
  1180. /* If controller is not scanning we are done. */
  1181. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
  1182. return 0;
  1183. hci_req_add_le_scan_disable(req);
  1184. memset(&cp, 0, sizeof(cp));
  1185. cp.enable = LE_SCAN_ENABLE;
  1186. cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
  1187. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
  1188. return 0;
  1189. }
  1190. static void le_scan_restart_work(struct work_struct *work)
  1191. {
  1192. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1193. le_scan_restart.work);
  1194. unsigned long timeout, duration, scan_start, now;
  1195. u8 status;
  1196. BT_DBG("%s", hdev->name);
  1197. hci_req_sync(hdev, le_scan_restart, 0, HCI_CMD_TIMEOUT, &status);
  1198. if (status) {
  1199. BT_ERR("Failed to restart LE scan: status %d", status);
  1200. return;
  1201. }
  1202. hci_dev_lock(hdev);
  1203. if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) ||
  1204. !hdev->discovery.scan_start)
  1205. goto unlock;
  1206. /* When the scan was started, hdev->le_scan_disable has been queued
  1207. * after duration from scan_start. During scan restart this job
  1208. * has been canceled, and we need to queue it again after proper
  1209. * timeout, to make sure that scan does not run indefinitely.
  1210. */
  1211. duration = hdev->discovery.scan_duration;
  1212. scan_start = hdev->discovery.scan_start;
  1213. now = jiffies;
  1214. if (now - scan_start <= duration) {
  1215. int elapsed;
  1216. if (now >= scan_start)
  1217. elapsed = now - scan_start;
  1218. else
  1219. elapsed = ULONG_MAX - scan_start + now;
  1220. timeout = duration - elapsed;
  1221. } else {
  1222. timeout = 0;
  1223. }
  1224. queue_delayed_work(hdev->req_workqueue,
  1225. &hdev->le_scan_disable, timeout);
  1226. unlock:
  1227. hci_dev_unlock(hdev);
  1228. }
  1229. static void disable_advertising(struct hci_request *req)
  1230. {
  1231. u8 enable = 0x00;
  1232. hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
  1233. }
  1234. static int active_scan(struct hci_request *req, unsigned long opt)
  1235. {
  1236. uint16_t interval = opt;
  1237. struct hci_dev *hdev = req->hdev;
  1238. struct hci_cp_le_set_scan_param param_cp;
  1239. struct hci_cp_le_set_scan_enable enable_cp;
  1240. u8 own_addr_type;
  1241. int err;
  1242. BT_DBG("%s", hdev->name);
  1243. if (hci_dev_test_flag(hdev, HCI_LE_ADV)) {
  1244. hci_dev_lock(hdev);
  1245. /* Don't let discovery abort an outgoing connection attempt
  1246. * that's using directed advertising.
  1247. */
  1248. if (hci_lookup_le_connect(hdev)) {
  1249. hci_dev_unlock(hdev);
  1250. return -EBUSY;
  1251. }
  1252. cancel_adv_timeout(hdev);
  1253. hci_dev_unlock(hdev);
  1254. disable_advertising(req);
  1255. }
  1256. /* If controller is scanning, it means the background scanning is
  1257. * running. Thus, we should temporarily stop it in order to set the
  1258. * discovery scanning parameters.
  1259. */
  1260. if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
  1261. hci_req_add_le_scan_disable(req);
  1262. /* All active scans will be done with either a resolvable private
  1263. * address (when privacy feature has been enabled) or non-resolvable
  1264. * private address.
  1265. */
  1266. err = hci_update_random_address(req, true, &own_addr_type);
  1267. if (err < 0)
  1268. own_addr_type = ADDR_LE_DEV_PUBLIC;
  1269. memset(&param_cp, 0, sizeof(param_cp));
  1270. param_cp.type = LE_SCAN_ACTIVE;
  1271. param_cp.interval = cpu_to_le16(interval);
  1272. param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN);
  1273. param_cp.own_address_type = own_addr_type;
  1274. hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
  1275. &param_cp);
  1276. memset(&enable_cp, 0, sizeof(enable_cp));
  1277. enable_cp.enable = LE_SCAN_ENABLE;
  1278. enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
  1279. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
  1280. &enable_cp);
  1281. return 0;
  1282. }
  1283. static int interleaved_discov(struct hci_request *req, unsigned long opt)
  1284. {
  1285. int err;
  1286. BT_DBG("%s", req->hdev->name);
  1287. err = active_scan(req, opt);
  1288. if (err)
  1289. return err;
  1290. return bredr_inquiry(req, DISCOV_BREDR_INQUIRY_LEN);
  1291. }
  1292. static void start_discovery(struct hci_dev *hdev, u8 *status)
  1293. {
  1294. unsigned long timeout;
  1295. BT_DBG("%s type %u", hdev->name, hdev->discovery.type);
  1296. switch (hdev->discovery.type) {
  1297. case DISCOV_TYPE_BREDR:
  1298. if (!hci_dev_test_flag(hdev, HCI_INQUIRY))
  1299. hci_req_sync(hdev, bredr_inquiry,
  1300. DISCOV_BREDR_INQUIRY_LEN, HCI_CMD_TIMEOUT,
  1301. status);
  1302. return;
  1303. case DISCOV_TYPE_INTERLEAVED:
  1304. /* When running simultaneous discovery, the LE scanning time
  1305. * should occupy the whole discovery time sine BR/EDR inquiry
  1306. * and LE scanning are scheduled by the controller.
  1307. *
  1308. * For interleaving discovery in comparison, BR/EDR inquiry
  1309. * and LE scanning are done sequentially with separate
  1310. * timeouts.
  1311. */
  1312. if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
  1313. &hdev->quirks)) {
  1314. timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
  1315. /* During simultaneous discovery, we double LE scan
  1316. * interval. We must leave some time for the controller
  1317. * to do BR/EDR inquiry.
  1318. */
  1319. hci_req_sync(hdev, interleaved_discov,
  1320. DISCOV_LE_SCAN_INT * 2, HCI_CMD_TIMEOUT,
  1321. status);
  1322. break;
  1323. }
  1324. timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
  1325. hci_req_sync(hdev, active_scan, DISCOV_LE_SCAN_INT,
  1326. HCI_CMD_TIMEOUT, status);
  1327. break;
  1328. case DISCOV_TYPE_LE:
  1329. timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
  1330. hci_req_sync(hdev, active_scan, DISCOV_LE_SCAN_INT,
  1331. HCI_CMD_TIMEOUT, status);
  1332. break;
  1333. default:
  1334. *status = HCI_ERROR_UNSPECIFIED;
  1335. return;
  1336. }
  1337. if (*status)
  1338. return;
  1339. BT_DBG("%s timeout %u ms", hdev->name, jiffies_to_msecs(timeout));
  1340. /* When service discovery is used and the controller has a
  1341. * strict duplicate filter, it is important to remember the
  1342. * start and duration of the scan. This is required for
  1343. * restarting scanning during the discovery phase.
  1344. */
  1345. if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
  1346. hdev->discovery.result_filtering) {
  1347. hdev->discovery.scan_start = jiffies;
  1348. hdev->discovery.scan_duration = timeout;
  1349. }
  1350. queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
  1351. timeout);
  1352. }
  1353. bool hci_req_stop_discovery(struct hci_request *req)
  1354. {
  1355. struct hci_dev *hdev = req->hdev;
  1356. struct discovery_state *d = &hdev->discovery;
  1357. struct hci_cp_remote_name_req_cancel cp;
  1358. struct inquiry_entry *e;
  1359. bool ret = false;
  1360. BT_DBG("%s state %u", hdev->name, hdev->discovery.state);
  1361. if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
  1362. if (test_bit(HCI_INQUIRY, &hdev->flags))
  1363. hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL);
  1364. if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
  1365. cancel_delayed_work(&hdev->le_scan_disable);
  1366. hci_req_add_le_scan_disable(req);
  1367. }
  1368. ret = true;
  1369. } else {
  1370. /* Passive scanning */
  1371. if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
  1372. hci_req_add_le_scan_disable(req);
  1373. ret = true;
  1374. }
  1375. }
  1376. /* No further actions needed for LE-only discovery */
  1377. if (d->type == DISCOV_TYPE_LE)
  1378. return ret;
  1379. if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
  1380. e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
  1381. NAME_PENDING);
  1382. if (!e)
  1383. return ret;
  1384. bacpy(&cp.bdaddr, &e->data.bdaddr);
  1385. hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp),
  1386. &cp);
  1387. ret = true;
  1388. }
  1389. return ret;
  1390. }
  1391. static int stop_discovery(struct hci_request *req, unsigned long opt)
  1392. {
  1393. hci_dev_lock(req->hdev);
  1394. hci_req_stop_discovery(req);
  1395. hci_dev_unlock(req->hdev);
  1396. return 0;
  1397. }
  1398. static void discov_update(struct work_struct *work)
  1399. {
  1400. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1401. discov_update);
  1402. u8 status = 0;
  1403. switch (hdev->discovery.state) {
  1404. case DISCOVERY_STARTING:
  1405. start_discovery(hdev, &status);
  1406. mgmt_start_discovery_complete(hdev, status);
  1407. if (status)
  1408. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  1409. else
  1410. hci_discovery_set_state(hdev, DISCOVERY_FINDING);
  1411. break;
  1412. case DISCOVERY_STOPPING:
  1413. hci_req_sync(hdev, stop_discovery, 0, HCI_CMD_TIMEOUT, &status);
  1414. mgmt_stop_discovery_complete(hdev, status);
  1415. if (!status)
  1416. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  1417. break;
  1418. case DISCOVERY_STOPPED:
  1419. default:
  1420. return;
  1421. }
  1422. }
  1423. void hci_request_setup(struct hci_dev *hdev)
  1424. {
  1425. INIT_WORK(&hdev->discov_update, discov_update);
  1426. INIT_WORK(&hdev->bg_scan_update, bg_scan_update);
  1427. INIT_WORK(&hdev->scan_update, scan_update_work);
  1428. INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work);
  1429. INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart_work);
  1430. INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
  1431. }
  1432. void hci_request_cancel_all(struct hci_dev *hdev)
  1433. {
  1434. hci_req_sync_cancel(hdev, ENODEV);
  1435. cancel_work_sync(&hdev->discov_update);
  1436. cancel_work_sync(&hdev->bg_scan_update);
  1437. cancel_work_sync(&hdev->scan_update);
  1438. cancel_delayed_work_sync(&hdev->le_scan_disable);
  1439. cancel_delayed_work_sync(&hdev->le_scan_restart);
  1440. if (hdev->adv_instance_timeout) {
  1441. cancel_delayed_work_sync(&hdev->adv_instance_expire);
  1442. hdev->adv_instance_timeout = 0;
  1443. }
  1444. }