hci_request.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836
  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 <linux/sched/signal.h>
  20. #include <net/bluetooth/bluetooth.h>
  21. #include <net/bluetooth/hci_core.h>
  22. #include <net/bluetooth/mgmt.h>
  23. #include "smp.h"
  24. #include "hci_request.h"
  25. #define HCI_REQ_DONE 0
  26. #define HCI_REQ_PEND 1
  27. #define HCI_REQ_CANCELED 2
  28. void hci_req_init(struct hci_request *req, struct hci_dev *hdev)
  29. {
  30. skb_queue_head_init(&req->cmd_q);
  31. req->hdev = hdev;
  32. req->err = 0;
  33. }
  34. void hci_req_purge(struct hci_request *req)
  35. {
  36. skb_queue_purge(&req->cmd_q);
  37. }
  38. bool hci_req_status_pend(struct hci_dev *hdev)
  39. {
  40. return hdev->req_status == HCI_REQ_PEND;
  41. }
  42. static int req_run(struct hci_request *req, hci_req_complete_t complete,
  43. hci_req_complete_skb_t complete_skb)
  44. {
  45. struct hci_dev *hdev = req->hdev;
  46. struct sk_buff *skb;
  47. unsigned long flags;
  48. BT_DBG("length %u", skb_queue_len(&req->cmd_q));
  49. /* If an error occurred during request building, remove all HCI
  50. * commands queued on the HCI request queue.
  51. */
  52. if (req->err) {
  53. skb_queue_purge(&req->cmd_q);
  54. return req->err;
  55. }
  56. /* Do not allow empty requests */
  57. if (skb_queue_empty(&req->cmd_q))
  58. return -ENODATA;
  59. skb = skb_peek_tail(&req->cmd_q);
  60. if (complete) {
  61. bt_cb(skb)->hci.req_complete = complete;
  62. } else if (complete_skb) {
  63. bt_cb(skb)->hci.req_complete_skb = complete_skb;
  64. bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
  65. }
  66. spin_lock_irqsave(&hdev->cmd_q.lock, flags);
  67. skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
  68. spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
  69. queue_work(hdev->workqueue, &hdev->cmd_work);
  70. return 0;
  71. }
  72. int hci_req_run(struct hci_request *req, hci_req_complete_t complete)
  73. {
  74. return req_run(req, complete, NULL);
  75. }
  76. int hci_req_run_skb(struct hci_request *req, hci_req_complete_skb_t complete)
  77. {
  78. return req_run(req, NULL, complete);
  79. }
  80. static void hci_req_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
  81. struct sk_buff *skb)
  82. {
  83. BT_DBG("%s result 0x%2.2x", hdev->name, result);
  84. if (hdev->req_status == HCI_REQ_PEND) {
  85. hdev->req_result = result;
  86. hdev->req_status = HCI_REQ_DONE;
  87. if (skb)
  88. hdev->req_skb = skb_get(skb);
  89. wake_up_interruptible(&hdev->req_wait_q);
  90. }
  91. }
  92. void hci_req_sync_cancel(struct hci_dev *hdev, int err)
  93. {
  94. BT_DBG("%s err 0x%2.2x", hdev->name, err);
  95. if (hdev->req_status == HCI_REQ_PEND) {
  96. hdev->req_result = err;
  97. hdev->req_status = HCI_REQ_CANCELED;
  98. wake_up_interruptible(&hdev->req_wait_q);
  99. }
  100. }
  101. struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
  102. const void *param, u8 event, u32 timeout)
  103. {
  104. struct hci_request req;
  105. struct sk_buff *skb;
  106. int err = 0;
  107. BT_DBG("%s", hdev->name);
  108. hci_req_init(&req, hdev);
  109. hci_req_add_ev(&req, opcode, plen, param, event);
  110. hdev->req_status = HCI_REQ_PEND;
  111. err = hci_req_run_skb(&req, hci_req_sync_complete);
  112. if (err < 0)
  113. return ERR_PTR(err);
  114. err = wait_event_interruptible_timeout(hdev->req_wait_q,
  115. hdev->req_status != HCI_REQ_PEND, timeout);
  116. if (err == -ERESTARTSYS)
  117. return ERR_PTR(-EINTR);
  118. switch (hdev->req_status) {
  119. case HCI_REQ_DONE:
  120. err = -bt_to_errno(hdev->req_result);
  121. break;
  122. case HCI_REQ_CANCELED:
  123. err = -hdev->req_result;
  124. break;
  125. default:
  126. err = -ETIMEDOUT;
  127. break;
  128. }
  129. hdev->req_status = hdev->req_result = 0;
  130. skb = hdev->req_skb;
  131. hdev->req_skb = NULL;
  132. BT_DBG("%s end: err %d", hdev->name, err);
  133. if (err < 0) {
  134. kfree_skb(skb);
  135. return ERR_PTR(err);
  136. }
  137. if (!skb)
  138. return ERR_PTR(-ENODATA);
  139. return skb;
  140. }
  141. EXPORT_SYMBOL(__hci_cmd_sync_ev);
  142. struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
  143. const void *param, u32 timeout)
  144. {
  145. return __hci_cmd_sync_ev(hdev, opcode, plen, param, 0, timeout);
  146. }
  147. EXPORT_SYMBOL(__hci_cmd_sync);
  148. /* Execute request and wait for completion. */
  149. int __hci_req_sync(struct hci_dev *hdev, int (*func)(struct hci_request *req,
  150. unsigned long opt),
  151. unsigned long opt, u32 timeout, u8 *hci_status)
  152. {
  153. struct hci_request req;
  154. int err = 0;
  155. BT_DBG("%s start", hdev->name);
  156. hci_req_init(&req, hdev);
  157. hdev->req_status = HCI_REQ_PEND;
  158. err = func(&req, opt);
  159. if (err) {
  160. if (hci_status)
  161. *hci_status = HCI_ERROR_UNSPECIFIED;
  162. return err;
  163. }
  164. err = hci_req_run_skb(&req, hci_req_sync_complete);
  165. if (err < 0) {
  166. hdev->req_status = 0;
  167. /* ENODATA means the HCI request command queue is empty.
  168. * This can happen when a request with conditionals doesn't
  169. * trigger any commands to be sent. This is normal behavior
  170. * and should not trigger an error return.
  171. */
  172. if (err == -ENODATA) {
  173. if (hci_status)
  174. *hci_status = 0;
  175. return 0;
  176. }
  177. if (hci_status)
  178. *hci_status = HCI_ERROR_UNSPECIFIED;
  179. return err;
  180. }
  181. err = wait_event_interruptible_timeout(hdev->req_wait_q,
  182. hdev->req_status != HCI_REQ_PEND, timeout);
  183. if (err == -ERESTARTSYS)
  184. return -EINTR;
  185. switch (hdev->req_status) {
  186. case HCI_REQ_DONE:
  187. err = -bt_to_errno(hdev->req_result);
  188. if (hci_status)
  189. *hci_status = hdev->req_result;
  190. break;
  191. case HCI_REQ_CANCELED:
  192. err = -hdev->req_result;
  193. if (hci_status)
  194. *hci_status = HCI_ERROR_UNSPECIFIED;
  195. break;
  196. default:
  197. err = -ETIMEDOUT;
  198. if (hci_status)
  199. *hci_status = HCI_ERROR_UNSPECIFIED;
  200. break;
  201. }
  202. kfree_skb(hdev->req_skb);
  203. hdev->req_skb = NULL;
  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. /* Serialize all requests */
  214. hci_req_sync_lock(hdev);
  215. /* check the state after obtaing the lock to protect the HCI_UP
  216. * against any races from hci_dev_do_close when the controller
  217. * gets removed.
  218. */
  219. if (test_bit(HCI_UP, &hdev->flags))
  220. ret = __hci_req_sync(hdev, req, opt, timeout, hci_status);
  221. else
  222. ret = -ENETDOWN;
  223. hci_req_sync_unlock(hdev);
  224. return ret;
  225. }
  226. struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, u32 plen,
  227. const void *param)
  228. {
  229. int len = HCI_COMMAND_HDR_SIZE + plen;
  230. struct hci_command_hdr *hdr;
  231. struct sk_buff *skb;
  232. skb = bt_skb_alloc(len, GFP_ATOMIC);
  233. if (!skb)
  234. return NULL;
  235. hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
  236. hdr->opcode = cpu_to_le16(opcode);
  237. hdr->plen = plen;
  238. if (plen)
  239. skb_put_data(skb, param, plen);
  240. BT_DBG("skb len %d", skb->len);
  241. hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
  242. hci_skb_opcode(skb) = opcode;
  243. return skb;
  244. }
  245. /* Queue a command to an asynchronous HCI request */
  246. void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen,
  247. const void *param, u8 event)
  248. {
  249. struct hci_dev *hdev = req->hdev;
  250. struct sk_buff *skb;
  251. BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen);
  252. /* If an error occurred during request building, there is no point in
  253. * queueing the HCI command. We can simply return.
  254. */
  255. if (req->err)
  256. return;
  257. skb = hci_prepare_cmd(hdev, opcode, plen, param);
  258. if (!skb) {
  259. bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
  260. opcode);
  261. req->err = -ENOMEM;
  262. return;
  263. }
  264. if (skb_queue_empty(&req->cmd_q))
  265. bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
  266. bt_cb(skb)->hci.req_event = event;
  267. skb_queue_tail(&req->cmd_q, skb);
  268. }
  269. void hci_req_add(struct hci_request *req, u16 opcode, u32 plen,
  270. const void *param)
  271. {
  272. hci_req_add_ev(req, opcode, plen, param, 0);
  273. }
  274. void __hci_req_write_fast_connectable(struct hci_request *req, bool enable)
  275. {
  276. struct hci_dev *hdev = req->hdev;
  277. struct hci_cp_write_page_scan_activity acp;
  278. u8 type;
  279. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  280. return;
  281. if (hdev->hci_ver < BLUETOOTH_VER_1_2)
  282. return;
  283. if (enable) {
  284. type = PAGE_SCAN_TYPE_INTERLACED;
  285. /* 160 msec page scan interval */
  286. acp.interval = cpu_to_le16(0x0100);
  287. } else {
  288. type = PAGE_SCAN_TYPE_STANDARD; /* default */
  289. /* default 1.28 sec page scan */
  290. acp.interval = cpu_to_le16(0x0800);
  291. }
  292. acp.window = cpu_to_le16(0x0012);
  293. if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval ||
  294. __cpu_to_le16(hdev->page_scan_window) != acp.window)
  295. hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
  296. sizeof(acp), &acp);
  297. if (hdev->page_scan_type != type)
  298. hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
  299. }
  300. /* This function controls the background scanning based on hdev->pend_le_conns
  301. * list. If there are pending LE connection we start the background scanning,
  302. * otherwise we stop it.
  303. *
  304. * This function requires the caller holds hdev->lock.
  305. */
  306. static void __hci_update_background_scan(struct hci_request *req)
  307. {
  308. struct hci_dev *hdev = req->hdev;
  309. if (!test_bit(HCI_UP, &hdev->flags) ||
  310. test_bit(HCI_INIT, &hdev->flags) ||
  311. hci_dev_test_flag(hdev, HCI_SETUP) ||
  312. hci_dev_test_flag(hdev, HCI_CONFIG) ||
  313. hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
  314. hci_dev_test_flag(hdev, HCI_UNREGISTER))
  315. return;
  316. /* No point in doing scanning if LE support hasn't been enabled */
  317. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  318. return;
  319. /* If discovery is active don't interfere with it */
  320. if (hdev->discovery.state != DISCOVERY_STOPPED)
  321. return;
  322. /* Reset RSSI and UUID filters when starting background scanning
  323. * since these filters are meant for service discovery only.
  324. *
  325. * The Start Discovery and Start Service Discovery operations
  326. * ensure to set proper values for RSSI threshold and UUID
  327. * filter list. So it is safe to just reset them here.
  328. */
  329. hci_discovery_filter_clear(hdev);
  330. if (list_empty(&hdev->pend_le_conns) &&
  331. list_empty(&hdev->pend_le_reports)) {
  332. /* If there is no pending LE connections or devices
  333. * to be scanned for, we should stop the background
  334. * scanning.
  335. */
  336. /* If controller is not scanning we are done. */
  337. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
  338. return;
  339. hci_req_add_le_scan_disable(req);
  340. BT_DBG("%s stopping background scanning", hdev->name);
  341. } else {
  342. /* If there is at least one pending LE connection, we should
  343. * keep the background scan running.
  344. */
  345. /* If controller is connecting, we should not start scanning
  346. * since some controllers are not able to scan and connect at
  347. * the same time.
  348. */
  349. if (hci_lookup_le_connect(hdev))
  350. return;
  351. /* If controller is currently scanning, we stop it to ensure we
  352. * don't miss any advertising (due to duplicates filter).
  353. */
  354. if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
  355. hci_req_add_le_scan_disable(req);
  356. hci_req_add_le_passive_scan(req);
  357. BT_DBG("%s starting background scanning", hdev->name);
  358. }
  359. }
  360. void __hci_req_update_name(struct hci_request *req)
  361. {
  362. struct hci_dev *hdev = req->hdev;
  363. struct hci_cp_write_local_name cp;
  364. memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
  365. hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
  366. }
  367. #define PNP_INFO_SVCLASS_ID 0x1200
  368. static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
  369. {
  370. u8 *ptr = data, *uuids_start = NULL;
  371. struct bt_uuid *uuid;
  372. if (len < 4)
  373. return ptr;
  374. list_for_each_entry(uuid, &hdev->uuids, list) {
  375. u16 uuid16;
  376. if (uuid->size != 16)
  377. continue;
  378. uuid16 = get_unaligned_le16(&uuid->uuid[12]);
  379. if (uuid16 < 0x1100)
  380. continue;
  381. if (uuid16 == PNP_INFO_SVCLASS_ID)
  382. continue;
  383. if (!uuids_start) {
  384. uuids_start = ptr;
  385. uuids_start[0] = 1;
  386. uuids_start[1] = EIR_UUID16_ALL;
  387. ptr += 2;
  388. }
  389. /* Stop if not enough space to put next UUID */
  390. if ((ptr - data) + sizeof(u16) > len) {
  391. uuids_start[1] = EIR_UUID16_SOME;
  392. break;
  393. }
  394. *ptr++ = (uuid16 & 0x00ff);
  395. *ptr++ = (uuid16 & 0xff00) >> 8;
  396. uuids_start[0] += sizeof(uuid16);
  397. }
  398. return ptr;
  399. }
  400. static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
  401. {
  402. u8 *ptr = data, *uuids_start = NULL;
  403. struct bt_uuid *uuid;
  404. if (len < 6)
  405. return ptr;
  406. list_for_each_entry(uuid, &hdev->uuids, list) {
  407. if (uuid->size != 32)
  408. continue;
  409. if (!uuids_start) {
  410. uuids_start = ptr;
  411. uuids_start[0] = 1;
  412. uuids_start[1] = EIR_UUID32_ALL;
  413. ptr += 2;
  414. }
  415. /* Stop if not enough space to put next UUID */
  416. if ((ptr - data) + sizeof(u32) > len) {
  417. uuids_start[1] = EIR_UUID32_SOME;
  418. break;
  419. }
  420. memcpy(ptr, &uuid->uuid[12], sizeof(u32));
  421. ptr += sizeof(u32);
  422. uuids_start[0] += sizeof(u32);
  423. }
  424. return ptr;
  425. }
  426. static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
  427. {
  428. u8 *ptr = data, *uuids_start = NULL;
  429. struct bt_uuid *uuid;
  430. if (len < 18)
  431. return ptr;
  432. list_for_each_entry(uuid, &hdev->uuids, list) {
  433. if (uuid->size != 128)
  434. continue;
  435. if (!uuids_start) {
  436. uuids_start = ptr;
  437. uuids_start[0] = 1;
  438. uuids_start[1] = EIR_UUID128_ALL;
  439. ptr += 2;
  440. }
  441. /* Stop if not enough space to put next UUID */
  442. if ((ptr - data) + 16 > len) {
  443. uuids_start[1] = EIR_UUID128_SOME;
  444. break;
  445. }
  446. memcpy(ptr, uuid->uuid, 16);
  447. ptr += 16;
  448. uuids_start[0] += 16;
  449. }
  450. return ptr;
  451. }
  452. static void create_eir(struct hci_dev *hdev, u8 *data)
  453. {
  454. u8 *ptr = data;
  455. size_t name_len;
  456. name_len = strlen(hdev->dev_name);
  457. if (name_len > 0) {
  458. /* EIR Data type */
  459. if (name_len > 48) {
  460. name_len = 48;
  461. ptr[1] = EIR_NAME_SHORT;
  462. } else
  463. ptr[1] = EIR_NAME_COMPLETE;
  464. /* EIR Data length */
  465. ptr[0] = name_len + 1;
  466. memcpy(ptr + 2, hdev->dev_name, name_len);
  467. ptr += (name_len + 2);
  468. }
  469. if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
  470. ptr[0] = 2;
  471. ptr[1] = EIR_TX_POWER;
  472. ptr[2] = (u8) hdev->inq_tx_power;
  473. ptr += 3;
  474. }
  475. if (hdev->devid_source > 0) {
  476. ptr[0] = 9;
  477. ptr[1] = EIR_DEVICE_ID;
  478. put_unaligned_le16(hdev->devid_source, ptr + 2);
  479. put_unaligned_le16(hdev->devid_vendor, ptr + 4);
  480. put_unaligned_le16(hdev->devid_product, ptr + 6);
  481. put_unaligned_le16(hdev->devid_version, ptr + 8);
  482. ptr += 10;
  483. }
  484. ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
  485. ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
  486. ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
  487. }
  488. void __hci_req_update_eir(struct hci_request *req)
  489. {
  490. struct hci_dev *hdev = req->hdev;
  491. struct hci_cp_write_eir cp;
  492. if (!hdev_is_powered(hdev))
  493. return;
  494. if (!lmp_ext_inq_capable(hdev))
  495. return;
  496. if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
  497. return;
  498. if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
  499. return;
  500. memset(&cp, 0, sizeof(cp));
  501. create_eir(hdev, cp.data);
  502. if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
  503. return;
  504. memcpy(hdev->eir, cp.data, sizeof(cp.data));
  505. hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
  506. }
  507. void hci_req_add_le_scan_disable(struct hci_request *req)
  508. {
  509. struct hci_dev *hdev = req->hdev;
  510. if (use_ext_scan(hdev)) {
  511. struct hci_cp_le_set_ext_scan_enable cp;
  512. memset(&cp, 0, sizeof(cp));
  513. cp.enable = LE_SCAN_DISABLE;
  514. hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_ENABLE, sizeof(cp),
  515. &cp);
  516. } else {
  517. struct hci_cp_le_set_scan_enable cp;
  518. memset(&cp, 0, sizeof(cp));
  519. cp.enable = LE_SCAN_DISABLE;
  520. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
  521. }
  522. }
  523. static void add_to_white_list(struct hci_request *req,
  524. struct hci_conn_params *params)
  525. {
  526. struct hci_cp_le_add_to_white_list cp;
  527. cp.bdaddr_type = params->addr_type;
  528. bacpy(&cp.bdaddr, &params->addr);
  529. hci_req_add(req, HCI_OP_LE_ADD_TO_WHITE_LIST, sizeof(cp), &cp);
  530. }
  531. static u8 update_white_list(struct hci_request *req)
  532. {
  533. struct hci_dev *hdev = req->hdev;
  534. struct hci_conn_params *params;
  535. struct bdaddr_list *b;
  536. uint8_t white_list_entries = 0;
  537. /* Go through the current white list programmed into the
  538. * controller one by one and check if that address is still
  539. * in the list of pending connections or list of devices to
  540. * report. If not present in either list, then queue the
  541. * command to remove it from the controller.
  542. */
  543. list_for_each_entry(b, &hdev->le_white_list, list) {
  544. /* If the device is neither in pend_le_conns nor
  545. * pend_le_reports then remove it from the whitelist.
  546. */
  547. if (!hci_pend_le_action_lookup(&hdev->pend_le_conns,
  548. &b->bdaddr, b->bdaddr_type) &&
  549. !hci_pend_le_action_lookup(&hdev->pend_le_reports,
  550. &b->bdaddr, b->bdaddr_type)) {
  551. struct hci_cp_le_del_from_white_list cp;
  552. cp.bdaddr_type = b->bdaddr_type;
  553. bacpy(&cp.bdaddr, &b->bdaddr);
  554. hci_req_add(req, HCI_OP_LE_DEL_FROM_WHITE_LIST,
  555. sizeof(cp), &cp);
  556. continue;
  557. }
  558. if (hci_find_irk_by_addr(hdev, &b->bdaddr, b->bdaddr_type)) {
  559. /* White list can not be used with RPAs */
  560. return 0x00;
  561. }
  562. white_list_entries++;
  563. }
  564. /* Since all no longer valid white list entries have been
  565. * removed, walk through the list of pending connections
  566. * and ensure that any new device gets programmed into
  567. * the controller.
  568. *
  569. * If the list of the devices is larger than the list of
  570. * available white list entries in the controller, then
  571. * just abort and return filer policy value to not use the
  572. * white list.
  573. */
  574. list_for_each_entry(params, &hdev->pend_le_conns, action) {
  575. if (hci_bdaddr_list_lookup(&hdev->le_white_list,
  576. &params->addr, params->addr_type))
  577. continue;
  578. if (white_list_entries >= hdev->le_white_list_size) {
  579. /* Select filter policy to accept all advertising */
  580. return 0x00;
  581. }
  582. if (hci_find_irk_by_addr(hdev, &params->addr,
  583. params->addr_type)) {
  584. /* White list can not be used with RPAs */
  585. return 0x00;
  586. }
  587. white_list_entries++;
  588. add_to_white_list(req, params);
  589. }
  590. /* After adding all new pending connections, walk through
  591. * the list of pending reports and also add these to the
  592. * white list if there is still space.
  593. */
  594. list_for_each_entry(params, &hdev->pend_le_reports, action) {
  595. if (hci_bdaddr_list_lookup(&hdev->le_white_list,
  596. &params->addr, params->addr_type))
  597. continue;
  598. if (white_list_entries >= hdev->le_white_list_size) {
  599. /* Select filter policy to accept all advertising */
  600. return 0x00;
  601. }
  602. if (hci_find_irk_by_addr(hdev, &params->addr,
  603. params->addr_type)) {
  604. /* White list can not be used with RPAs */
  605. return 0x00;
  606. }
  607. white_list_entries++;
  608. add_to_white_list(req, params);
  609. }
  610. /* Select filter policy to use white list */
  611. return 0x01;
  612. }
  613. static bool scan_use_rpa(struct hci_dev *hdev)
  614. {
  615. return hci_dev_test_flag(hdev, HCI_PRIVACY);
  616. }
  617. static void hci_req_start_scan(struct hci_request *req, u8 type, u16 interval,
  618. u16 window, u8 own_addr_type, u8 filter_policy)
  619. {
  620. struct hci_dev *hdev = req->hdev;
  621. /* Use ext scanning if set ext scan param and ext scan enable is
  622. * supported
  623. */
  624. if (use_ext_scan(hdev)) {
  625. struct hci_cp_le_set_ext_scan_params *ext_param_cp;
  626. struct hci_cp_le_set_ext_scan_enable ext_enable_cp;
  627. struct hci_cp_le_scan_phy_params *phy_params;
  628. u8 data[sizeof(*ext_param_cp) + sizeof(*phy_params) * 2];
  629. u32 plen;
  630. ext_param_cp = (void *)data;
  631. phy_params = (void *)ext_param_cp->data;
  632. memset(ext_param_cp, 0, sizeof(*ext_param_cp));
  633. ext_param_cp->own_addr_type = own_addr_type;
  634. ext_param_cp->filter_policy = filter_policy;
  635. plen = sizeof(*ext_param_cp);
  636. if (scan_1m(hdev) || scan_2m(hdev)) {
  637. ext_param_cp->scanning_phys |= LE_SCAN_PHY_1M;
  638. memset(phy_params, 0, sizeof(*phy_params));
  639. phy_params->type = type;
  640. phy_params->interval = cpu_to_le16(interval);
  641. phy_params->window = cpu_to_le16(window);
  642. plen += sizeof(*phy_params);
  643. phy_params++;
  644. }
  645. if (scan_coded(hdev)) {
  646. ext_param_cp->scanning_phys |= LE_SCAN_PHY_CODED;
  647. memset(phy_params, 0, sizeof(*phy_params));
  648. phy_params->type = type;
  649. phy_params->interval = cpu_to_le16(interval);
  650. phy_params->window = cpu_to_le16(window);
  651. plen += sizeof(*phy_params);
  652. phy_params++;
  653. }
  654. hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
  655. plen, ext_param_cp);
  656. memset(&ext_enable_cp, 0, sizeof(ext_enable_cp));
  657. ext_enable_cp.enable = LE_SCAN_ENABLE;
  658. ext_enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
  659. hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
  660. sizeof(ext_enable_cp), &ext_enable_cp);
  661. } else {
  662. struct hci_cp_le_set_scan_param param_cp;
  663. struct hci_cp_le_set_scan_enable enable_cp;
  664. memset(&param_cp, 0, sizeof(param_cp));
  665. param_cp.type = type;
  666. param_cp.interval = cpu_to_le16(interval);
  667. param_cp.window = cpu_to_le16(window);
  668. param_cp.own_address_type = own_addr_type;
  669. param_cp.filter_policy = filter_policy;
  670. hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
  671. &param_cp);
  672. memset(&enable_cp, 0, sizeof(enable_cp));
  673. enable_cp.enable = LE_SCAN_ENABLE;
  674. enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
  675. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
  676. &enable_cp);
  677. }
  678. }
  679. void hci_req_add_le_passive_scan(struct hci_request *req)
  680. {
  681. struct hci_dev *hdev = req->hdev;
  682. u8 own_addr_type;
  683. u8 filter_policy;
  684. /* Set require_privacy to false since no SCAN_REQ are send
  685. * during passive scanning. Not using an non-resolvable address
  686. * here is important so that peer devices using direct
  687. * advertising with our address will be correctly reported
  688. * by the controller.
  689. */
  690. if (hci_update_random_address(req, false, scan_use_rpa(hdev),
  691. &own_addr_type))
  692. return;
  693. /* Adding or removing entries from the white list must
  694. * happen before enabling scanning. The controller does
  695. * not allow white list modification while scanning.
  696. */
  697. filter_policy = update_white_list(req);
  698. /* When the controller is using random resolvable addresses and
  699. * with that having LE privacy enabled, then controllers with
  700. * Extended Scanner Filter Policies support can now enable support
  701. * for handling directed advertising.
  702. *
  703. * So instead of using filter polices 0x00 (no whitelist)
  704. * and 0x01 (whitelist enabled) use the new filter policies
  705. * 0x02 (no whitelist) and 0x03 (whitelist enabled).
  706. */
  707. if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
  708. (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
  709. filter_policy |= 0x02;
  710. hci_req_start_scan(req, LE_SCAN_PASSIVE, hdev->le_scan_interval,
  711. hdev->le_scan_window, own_addr_type, filter_policy);
  712. }
  713. static u8 get_adv_instance_scan_rsp_len(struct hci_dev *hdev, u8 instance)
  714. {
  715. struct adv_info *adv_instance;
  716. /* Ignore instance 0 */
  717. if (instance == 0x00)
  718. return 0;
  719. adv_instance = hci_find_adv_instance(hdev, instance);
  720. if (!adv_instance)
  721. return 0;
  722. /* TODO: Take into account the "appearance" and "local-name" flags here.
  723. * These are currently being ignored as they are not supported.
  724. */
  725. return adv_instance->scan_rsp_len;
  726. }
  727. static u8 get_cur_adv_instance_scan_rsp_len(struct hci_dev *hdev)
  728. {
  729. u8 instance = hdev->cur_adv_instance;
  730. struct adv_info *adv_instance;
  731. /* Ignore instance 0 */
  732. if (instance == 0x00)
  733. return 0;
  734. adv_instance = hci_find_adv_instance(hdev, instance);
  735. if (!adv_instance)
  736. return 0;
  737. /* TODO: Take into account the "appearance" and "local-name" flags here.
  738. * These are currently being ignored as they are not supported.
  739. */
  740. return adv_instance->scan_rsp_len;
  741. }
  742. void __hci_req_disable_advertising(struct hci_request *req)
  743. {
  744. if (ext_adv_capable(req->hdev)) {
  745. struct hci_cp_le_set_ext_adv_enable cp;
  746. cp.enable = 0x00;
  747. /* Disable all sets since we only support one set at the moment */
  748. cp.num_of_sets = 0x00;
  749. hci_req_add(req, HCI_OP_LE_SET_EXT_ADV_ENABLE, sizeof(cp), &cp);
  750. } else {
  751. u8 enable = 0x00;
  752. hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
  753. }
  754. }
  755. static u32 get_adv_instance_flags(struct hci_dev *hdev, u8 instance)
  756. {
  757. u32 flags;
  758. struct adv_info *adv_instance;
  759. if (instance == 0x00) {
  760. /* Instance 0 always manages the "Tx Power" and "Flags"
  761. * fields
  762. */
  763. flags = MGMT_ADV_FLAG_TX_POWER | MGMT_ADV_FLAG_MANAGED_FLAGS;
  764. /* For instance 0, the HCI_ADVERTISING_CONNECTABLE setting
  765. * corresponds to the "connectable" instance flag.
  766. */
  767. if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE))
  768. flags |= MGMT_ADV_FLAG_CONNECTABLE;
  769. if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
  770. flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
  771. else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
  772. flags |= MGMT_ADV_FLAG_DISCOV;
  773. return flags;
  774. }
  775. adv_instance = hci_find_adv_instance(hdev, instance);
  776. /* Return 0 when we got an invalid instance identifier. */
  777. if (!adv_instance)
  778. return 0;
  779. return adv_instance->flags;
  780. }
  781. static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
  782. {
  783. /* If privacy is not enabled don't use RPA */
  784. if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
  785. return false;
  786. /* If basic privacy mode is enabled use RPA */
  787. if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
  788. return true;
  789. /* If limited privacy mode is enabled don't use RPA if we're
  790. * both discoverable and bondable.
  791. */
  792. if ((flags & MGMT_ADV_FLAG_DISCOV) &&
  793. hci_dev_test_flag(hdev, HCI_BONDABLE))
  794. return false;
  795. /* We're neither bondable nor discoverable in the limited
  796. * privacy mode, therefore use RPA.
  797. */
  798. return true;
  799. }
  800. static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
  801. {
  802. /* If there is no connection we are OK to advertise. */
  803. if (hci_conn_num(hdev, LE_LINK) == 0)
  804. return true;
  805. /* Check le_states if there is any connection in slave role. */
  806. if (hdev->conn_hash.le_num_slave > 0) {
  807. /* Slave connection state and non connectable mode bit 20. */
  808. if (!connectable && !(hdev->le_states[2] & 0x10))
  809. return false;
  810. /* Slave connection state and connectable mode bit 38
  811. * and scannable bit 21.
  812. */
  813. if (connectable && (!(hdev->le_states[4] & 0x40) ||
  814. !(hdev->le_states[2] & 0x20)))
  815. return false;
  816. }
  817. /* Check le_states if there is any connection in master role. */
  818. if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_slave) {
  819. /* Master connection state and non connectable mode bit 18. */
  820. if (!connectable && !(hdev->le_states[2] & 0x02))
  821. return false;
  822. /* Master connection state and connectable mode bit 35 and
  823. * scannable 19.
  824. */
  825. if (connectable && (!(hdev->le_states[4] & 0x08) ||
  826. !(hdev->le_states[2] & 0x08)))
  827. return false;
  828. }
  829. return true;
  830. }
  831. void __hci_req_enable_advertising(struct hci_request *req)
  832. {
  833. struct hci_dev *hdev = req->hdev;
  834. struct hci_cp_le_set_adv_param cp;
  835. u8 own_addr_type, enable = 0x01;
  836. bool connectable;
  837. u32 flags;
  838. flags = get_adv_instance_flags(hdev, hdev->cur_adv_instance);
  839. /* If the "connectable" instance flag was not set, then choose between
  840. * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
  841. */
  842. connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
  843. mgmt_get_connectable(hdev);
  844. if (!is_advertising_allowed(hdev, connectable))
  845. return;
  846. if (hci_dev_test_flag(hdev, HCI_LE_ADV))
  847. __hci_req_disable_advertising(req);
  848. /* Clear the HCI_LE_ADV bit temporarily so that the
  849. * hci_update_random_address knows that it's safe to go ahead
  850. * and write a new random address. The flag will be set back on
  851. * as soon as the SET_ADV_ENABLE HCI command completes.
  852. */
  853. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  854. /* Set require_privacy to true only when non-connectable
  855. * advertising is used. In that case it is fine to use a
  856. * non-resolvable private address.
  857. */
  858. if (hci_update_random_address(req, !connectable,
  859. adv_use_rpa(hdev, flags),
  860. &own_addr_type) < 0)
  861. return;
  862. memset(&cp, 0, sizeof(cp));
  863. cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval);
  864. cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval);
  865. if (connectable)
  866. cp.type = LE_ADV_IND;
  867. else if (get_cur_adv_instance_scan_rsp_len(hdev))
  868. cp.type = LE_ADV_SCAN_IND;
  869. else
  870. cp.type = LE_ADV_NONCONN_IND;
  871. cp.own_address_type = own_addr_type;
  872. cp.channel_map = hdev->le_adv_channel_map;
  873. hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
  874. hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
  875. }
  876. u8 append_local_name(struct hci_dev *hdev, u8 *ptr, u8 ad_len)
  877. {
  878. size_t short_len;
  879. size_t complete_len;
  880. /* no space left for name (+ NULL + type + len) */
  881. if ((HCI_MAX_AD_LENGTH - ad_len) < HCI_MAX_SHORT_NAME_LENGTH + 3)
  882. return ad_len;
  883. /* use complete name if present and fits */
  884. complete_len = strlen(hdev->dev_name);
  885. if (complete_len && complete_len <= HCI_MAX_SHORT_NAME_LENGTH)
  886. return eir_append_data(ptr, ad_len, EIR_NAME_COMPLETE,
  887. hdev->dev_name, complete_len + 1);
  888. /* use short name if present */
  889. short_len = strlen(hdev->short_name);
  890. if (short_len)
  891. return eir_append_data(ptr, ad_len, EIR_NAME_SHORT,
  892. hdev->short_name, short_len + 1);
  893. /* use shortened full name if present, we already know that name
  894. * is longer then HCI_MAX_SHORT_NAME_LENGTH
  895. */
  896. if (complete_len) {
  897. u8 name[HCI_MAX_SHORT_NAME_LENGTH + 1];
  898. memcpy(name, hdev->dev_name, HCI_MAX_SHORT_NAME_LENGTH);
  899. name[HCI_MAX_SHORT_NAME_LENGTH] = '\0';
  900. return eir_append_data(ptr, ad_len, EIR_NAME_SHORT, name,
  901. sizeof(name));
  902. }
  903. return ad_len;
  904. }
  905. static u8 append_appearance(struct hci_dev *hdev, u8 *ptr, u8 ad_len)
  906. {
  907. return eir_append_le16(ptr, ad_len, EIR_APPEARANCE, hdev->appearance);
  908. }
  909. static u8 create_default_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
  910. {
  911. u8 scan_rsp_len = 0;
  912. if (hdev->appearance) {
  913. scan_rsp_len = append_appearance(hdev, ptr, scan_rsp_len);
  914. }
  915. return append_local_name(hdev, ptr, scan_rsp_len);
  916. }
  917. static u8 create_instance_scan_rsp_data(struct hci_dev *hdev, u8 instance,
  918. u8 *ptr)
  919. {
  920. struct adv_info *adv_instance;
  921. u32 instance_flags;
  922. u8 scan_rsp_len = 0;
  923. adv_instance = hci_find_adv_instance(hdev, instance);
  924. if (!adv_instance)
  925. return 0;
  926. instance_flags = adv_instance->flags;
  927. if ((instance_flags & MGMT_ADV_FLAG_APPEARANCE) && hdev->appearance) {
  928. scan_rsp_len = append_appearance(hdev, ptr, scan_rsp_len);
  929. }
  930. memcpy(&ptr[scan_rsp_len], adv_instance->scan_rsp_data,
  931. adv_instance->scan_rsp_len);
  932. scan_rsp_len += adv_instance->scan_rsp_len;
  933. if (instance_flags & MGMT_ADV_FLAG_LOCAL_NAME)
  934. scan_rsp_len = append_local_name(hdev, ptr, scan_rsp_len);
  935. return scan_rsp_len;
  936. }
  937. void __hci_req_update_scan_rsp_data(struct hci_request *req, u8 instance)
  938. {
  939. struct hci_dev *hdev = req->hdev;
  940. u8 len;
  941. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  942. return;
  943. if (ext_adv_capable(hdev)) {
  944. struct hci_cp_le_set_ext_scan_rsp_data cp;
  945. memset(&cp, 0, sizeof(cp));
  946. if (instance)
  947. len = create_instance_scan_rsp_data(hdev, instance,
  948. cp.data);
  949. else
  950. len = create_default_scan_rsp_data(hdev, cp.data);
  951. if (hdev->scan_rsp_data_len == len &&
  952. !memcmp(cp.data, hdev->scan_rsp_data, len))
  953. return;
  954. memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
  955. hdev->scan_rsp_data_len = len;
  956. cp.handle = 0;
  957. cp.length = len;
  958. cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
  959. cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
  960. hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA, sizeof(cp),
  961. &cp);
  962. } else {
  963. struct hci_cp_le_set_scan_rsp_data cp;
  964. memset(&cp, 0, sizeof(cp));
  965. if (instance)
  966. len = create_instance_scan_rsp_data(hdev, instance,
  967. cp.data);
  968. else
  969. len = create_default_scan_rsp_data(hdev, cp.data);
  970. if (hdev->scan_rsp_data_len == len &&
  971. !memcmp(cp.data, hdev->scan_rsp_data, len))
  972. return;
  973. memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
  974. hdev->scan_rsp_data_len = len;
  975. cp.length = len;
  976. hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp);
  977. }
  978. }
  979. static u8 create_instance_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
  980. {
  981. struct adv_info *adv_instance = NULL;
  982. u8 ad_len = 0, flags = 0;
  983. u32 instance_flags;
  984. /* Return 0 when the current instance identifier is invalid. */
  985. if (instance) {
  986. adv_instance = hci_find_adv_instance(hdev, instance);
  987. if (!adv_instance)
  988. return 0;
  989. }
  990. instance_flags = get_adv_instance_flags(hdev, instance);
  991. /* If instance already has the flags set skip adding it once
  992. * again.
  993. */
  994. if (adv_instance && eir_get_data(adv_instance->adv_data,
  995. adv_instance->adv_data_len, EIR_FLAGS,
  996. NULL))
  997. goto skip_flags;
  998. /* The Add Advertising command allows userspace to set both the general
  999. * and limited discoverable flags.
  1000. */
  1001. if (instance_flags & MGMT_ADV_FLAG_DISCOV)
  1002. flags |= LE_AD_GENERAL;
  1003. if (instance_flags & MGMT_ADV_FLAG_LIMITED_DISCOV)
  1004. flags |= LE_AD_LIMITED;
  1005. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  1006. flags |= LE_AD_NO_BREDR;
  1007. if (flags || (instance_flags & MGMT_ADV_FLAG_MANAGED_FLAGS)) {
  1008. /* If a discovery flag wasn't provided, simply use the global
  1009. * settings.
  1010. */
  1011. if (!flags)
  1012. flags |= mgmt_get_adv_discov_flags(hdev);
  1013. /* If flags would still be empty, then there is no need to
  1014. * include the "Flags" AD field".
  1015. */
  1016. if (flags) {
  1017. ptr[0] = 0x02;
  1018. ptr[1] = EIR_FLAGS;
  1019. ptr[2] = flags;
  1020. ad_len += 3;
  1021. ptr += 3;
  1022. }
  1023. }
  1024. skip_flags:
  1025. if (adv_instance) {
  1026. memcpy(ptr, adv_instance->adv_data,
  1027. adv_instance->adv_data_len);
  1028. ad_len += adv_instance->adv_data_len;
  1029. ptr += adv_instance->adv_data_len;
  1030. }
  1031. if (instance_flags & MGMT_ADV_FLAG_TX_POWER) {
  1032. s8 adv_tx_power;
  1033. if (ext_adv_capable(hdev)) {
  1034. if (adv_instance)
  1035. adv_tx_power = adv_instance->tx_power;
  1036. else
  1037. adv_tx_power = hdev->adv_tx_power;
  1038. } else {
  1039. adv_tx_power = hdev->adv_tx_power;
  1040. }
  1041. /* Provide Tx Power only if we can provide a valid value for it */
  1042. if (adv_tx_power != HCI_TX_POWER_INVALID) {
  1043. ptr[0] = 0x02;
  1044. ptr[1] = EIR_TX_POWER;
  1045. ptr[2] = (u8)adv_tx_power;
  1046. ad_len += 3;
  1047. ptr += 3;
  1048. }
  1049. }
  1050. return ad_len;
  1051. }
  1052. void __hci_req_update_adv_data(struct hci_request *req, u8 instance)
  1053. {
  1054. struct hci_dev *hdev = req->hdev;
  1055. u8 len;
  1056. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  1057. return;
  1058. if (ext_adv_capable(hdev)) {
  1059. struct hci_cp_le_set_ext_adv_data cp;
  1060. memset(&cp, 0, sizeof(cp));
  1061. len = create_instance_adv_data(hdev, instance, cp.data);
  1062. /* There's nothing to do if the data hasn't changed */
  1063. if (hdev->adv_data_len == len &&
  1064. memcmp(cp.data, hdev->adv_data, len) == 0)
  1065. return;
  1066. memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
  1067. hdev->adv_data_len = len;
  1068. cp.length = len;
  1069. cp.handle = 0;
  1070. cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
  1071. cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
  1072. hci_req_add(req, HCI_OP_LE_SET_EXT_ADV_DATA, sizeof(cp), &cp);
  1073. } else {
  1074. struct hci_cp_le_set_adv_data cp;
  1075. memset(&cp, 0, sizeof(cp));
  1076. len = create_instance_adv_data(hdev, instance, cp.data);
  1077. /* There's nothing to do if the data hasn't changed */
  1078. if (hdev->adv_data_len == len &&
  1079. memcmp(cp.data, hdev->adv_data, len) == 0)
  1080. return;
  1081. memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
  1082. hdev->adv_data_len = len;
  1083. cp.length = len;
  1084. hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
  1085. }
  1086. }
  1087. int hci_req_update_adv_data(struct hci_dev *hdev, u8 instance)
  1088. {
  1089. struct hci_request req;
  1090. hci_req_init(&req, hdev);
  1091. __hci_req_update_adv_data(&req, instance);
  1092. return hci_req_run(&req, NULL);
  1093. }
  1094. static void adv_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  1095. {
  1096. BT_DBG("%s status %u", hdev->name, status);
  1097. }
  1098. void hci_req_reenable_advertising(struct hci_dev *hdev)
  1099. {
  1100. struct hci_request req;
  1101. if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
  1102. list_empty(&hdev->adv_instances))
  1103. return;
  1104. hci_req_init(&req, hdev);
  1105. if (hdev->cur_adv_instance) {
  1106. __hci_req_schedule_adv_instance(&req, hdev->cur_adv_instance,
  1107. true);
  1108. } else {
  1109. if (ext_adv_capable(hdev)) {
  1110. __hci_req_start_ext_adv(&req, 0x00);
  1111. } else {
  1112. __hci_req_update_adv_data(&req, 0x00);
  1113. __hci_req_update_scan_rsp_data(&req, 0x00);
  1114. __hci_req_enable_advertising(&req);
  1115. }
  1116. }
  1117. hci_req_run(&req, adv_enable_complete);
  1118. }
  1119. static void adv_timeout_expire(struct work_struct *work)
  1120. {
  1121. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1122. adv_instance_expire.work);
  1123. struct hci_request req;
  1124. u8 instance;
  1125. BT_DBG("%s", hdev->name);
  1126. hci_dev_lock(hdev);
  1127. hdev->adv_instance_timeout = 0;
  1128. instance = hdev->cur_adv_instance;
  1129. if (instance == 0x00)
  1130. goto unlock;
  1131. hci_req_init(&req, hdev);
  1132. hci_req_clear_adv_instance(hdev, NULL, &req, instance, false);
  1133. if (list_empty(&hdev->adv_instances))
  1134. __hci_req_disable_advertising(&req);
  1135. hci_req_run(&req, NULL);
  1136. unlock:
  1137. hci_dev_unlock(hdev);
  1138. }
  1139. int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
  1140. bool use_rpa, struct adv_info *adv_instance,
  1141. u8 *own_addr_type, bdaddr_t *rand_addr)
  1142. {
  1143. int err;
  1144. bacpy(rand_addr, BDADDR_ANY);
  1145. /* If privacy is enabled use a resolvable private address. If
  1146. * current RPA has expired then generate a new one.
  1147. */
  1148. if (use_rpa) {
  1149. int to;
  1150. *own_addr_type = ADDR_LE_DEV_RANDOM;
  1151. if (adv_instance) {
  1152. if (!adv_instance->rpa_expired &&
  1153. !bacmp(&adv_instance->random_addr, &hdev->rpa))
  1154. return 0;
  1155. adv_instance->rpa_expired = false;
  1156. } else {
  1157. if (!hci_dev_test_and_clear_flag(hdev, HCI_RPA_EXPIRED) &&
  1158. !bacmp(&hdev->random_addr, &hdev->rpa))
  1159. return 0;
  1160. }
  1161. err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
  1162. if (err < 0) {
  1163. BT_ERR("%s failed to generate new RPA", hdev->name);
  1164. return err;
  1165. }
  1166. bacpy(rand_addr, &hdev->rpa);
  1167. to = msecs_to_jiffies(hdev->rpa_timeout * 1000);
  1168. if (adv_instance)
  1169. queue_delayed_work(hdev->workqueue,
  1170. &adv_instance->rpa_expired_cb, to);
  1171. else
  1172. queue_delayed_work(hdev->workqueue,
  1173. &hdev->rpa_expired, to);
  1174. return 0;
  1175. }
  1176. /* In case of required privacy without resolvable private address,
  1177. * use an non-resolvable private address. This is useful for
  1178. * non-connectable advertising.
  1179. */
  1180. if (require_privacy) {
  1181. bdaddr_t nrpa;
  1182. while (true) {
  1183. /* The non-resolvable private address is generated
  1184. * from random six bytes with the two most significant
  1185. * bits cleared.
  1186. */
  1187. get_random_bytes(&nrpa, 6);
  1188. nrpa.b[5] &= 0x3f;
  1189. /* The non-resolvable private address shall not be
  1190. * equal to the public address.
  1191. */
  1192. if (bacmp(&hdev->bdaddr, &nrpa))
  1193. break;
  1194. }
  1195. *own_addr_type = ADDR_LE_DEV_RANDOM;
  1196. bacpy(rand_addr, &nrpa);
  1197. return 0;
  1198. }
  1199. /* No privacy so use a public address. */
  1200. *own_addr_type = ADDR_LE_DEV_PUBLIC;
  1201. return 0;
  1202. }
  1203. void __hci_req_clear_ext_adv_sets(struct hci_request *req)
  1204. {
  1205. hci_req_add(req, HCI_OP_LE_CLEAR_ADV_SETS, 0, NULL);
  1206. }
  1207. int __hci_req_setup_ext_adv_instance(struct hci_request *req, u8 instance)
  1208. {
  1209. struct hci_cp_le_set_ext_adv_params cp;
  1210. struct hci_dev *hdev = req->hdev;
  1211. bool connectable;
  1212. u32 flags;
  1213. bdaddr_t random_addr;
  1214. u8 own_addr_type;
  1215. int err;
  1216. struct adv_info *adv_instance;
  1217. bool secondary_adv;
  1218. /* In ext adv set param interval is 3 octets */
  1219. const u8 adv_interval[3] = { 0x00, 0x08, 0x00 };
  1220. if (instance > 0) {
  1221. adv_instance = hci_find_adv_instance(hdev, instance);
  1222. if (!adv_instance)
  1223. return -EINVAL;
  1224. } else {
  1225. adv_instance = NULL;
  1226. }
  1227. flags = get_adv_instance_flags(hdev, instance);
  1228. /* If the "connectable" instance flag was not set, then choose between
  1229. * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
  1230. */
  1231. connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
  1232. mgmt_get_connectable(hdev);
  1233. if (!is_advertising_allowed(hdev, connectable))
  1234. return -EPERM;
  1235. /* Set require_privacy to true only when non-connectable
  1236. * advertising is used. In that case it is fine to use a
  1237. * non-resolvable private address.
  1238. */
  1239. err = hci_get_random_address(hdev, !connectable,
  1240. adv_use_rpa(hdev, flags), adv_instance,
  1241. &own_addr_type, &random_addr);
  1242. if (err < 0)
  1243. return err;
  1244. memset(&cp, 0, sizeof(cp));
  1245. memcpy(cp.min_interval, adv_interval, sizeof(cp.min_interval));
  1246. memcpy(cp.max_interval, adv_interval, sizeof(cp.max_interval));
  1247. secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
  1248. if (connectable) {
  1249. if (secondary_adv)
  1250. cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
  1251. else
  1252. cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
  1253. } else if (get_adv_instance_scan_rsp_len(hdev, instance)) {
  1254. if (secondary_adv)
  1255. cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
  1256. else
  1257. cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
  1258. } else {
  1259. if (secondary_adv)
  1260. cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
  1261. else
  1262. cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
  1263. }
  1264. cp.own_addr_type = own_addr_type;
  1265. cp.channel_map = hdev->le_adv_channel_map;
  1266. cp.tx_power = 127;
  1267. cp.handle = 0;
  1268. if (flags & MGMT_ADV_FLAG_SEC_2M) {
  1269. cp.primary_phy = HCI_ADV_PHY_1M;
  1270. cp.secondary_phy = HCI_ADV_PHY_2M;
  1271. } else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
  1272. cp.primary_phy = HCI_ADV_PHY_CODED;
  1273. cp.secondary_phy = HCI_ADV_PHY_CODED;
  1274. } else {
  1275. /* In all other cases use 1M */
  1276. cp.primary_phy = HCI_ADV_PHY_1M;
  1277. cp.secondary_phy = HCI_ADV_PHY_1M;
  1278. }
  1279. hci_req_add(req, HCI_OP_LE_SET_EXT_ADV_PARAMS, sizeof(cp), &cp);
  1280. if (own_addr_type == ADDR_LE_DEV_RANDOM &&
  1281. bacmp(&random_addr, BDADDR_ANY)) {
  1282. struct hci_cp_le_set_adv_set_rand_addr cp;
  1283. /* Check if random address need to be updated */
  1284. if (adv_instance) {
  1285. if (!bacmp(&random_addr, &adv_instance->random_addr))
  1286. return 0;
  1287. } else {
  1288. if (!bacmp(&random_addr, &hdev->random_addr))
  1289. return 0;
  1290. }
  1291. memset(&cp, 0, sizeof(cp));
  1292. cp.handle = 0;
  1293. bacpy(&cp.bdaddr, &random_addr);
  1294. hci_req_add(req,
  1295. HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
  1296. sizeof(cp), &cp);
  1297. }
  1298. return 0;
  1299. }
  1300. void __hci_req_enable_ext_advertising(struct hci_request *req)
  1301. {
  1302. struct hci_cp_le_set_ext_adv_enable *cp;
  1303. struct hci_cp_ext_adv_set *adv_set;
  1304. u8 data[sizeof(*cp) + sizeof(*adv_set) * 1];
  1305. cp = (void *) data;
  1306. adv_set = (void *) cp->data;
  1307. memset(cp, 0, sizeof(*cp));
  1308. cp->enable = 0x01;
  1309. cp->num_of_sets = 0x01;
  1310. memset(adv_set, 0, sizeof(*adv_set));
  1311. adv_set->handle = 0;
  1312. hci_req_add(req, HCI_OP_LE_SET_EXT_ADV_ENABLE,
  1313. sizeof(*cp) + sizeof(*adv_set) * cp->num_of_sets,
  1314. data);
  1315. }
  1316. int __hci_req_start_ext_adv(struct hci_request *req, u8 instance)
  1317. {
  1318. struct hci_dev *hdev = req->hdev;
  1319. int err;
  1320. if (hci_dev_test_flag(hdev, HCI_LE_ADV))
  1321. __hci_req_disable_advertising(req);
  1322. err = __hci_req_setup_ext_adv_instance(req, instance);
  1323. if (err < 0)
  1324. return err;
  1325. __hci_req_update_scan_rsp_data(req, instance);
  1326. __hci_req_enable_ext_advertising(req);
  1327. return 0;
  1328. }
  1329. int __hci_req_schedule_adv_instance(struct hci_request *req, u8 instance,
  1330. bool force)
  1331. {
  1332. struct hci_dev *hdev = req->hdev;
  1333. struct adv_info *adv_instance = NULL;
  1334. u16 timeout;
  1335. if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
  1336. list_empty(&hdev->adv_instances))
  1337. return -EPERM;
  1338. if (hdev->adv_instance_timeout)
  1339. return -EBUSY;
  1340. adv_instance = hci_find_adv_instance(hdev, instance);
  1341. if (!adv_instance)
  1342. return -ENOENT;
  1343. /* A zero timeout means unlimited advertising. As long as there is
  1344. * only one instance, duration should be ignored. We still set a timeout
  1345. * in case further instances are being added later on.
  1346. *
  1347. * If the remaining lifetime of the instance is more than the duration
  1348. * then the timeout corresponds to the duration, otherwise it will be
  1349. * reduced to the remaining instance lifetime.
  1350. */
  1351. if (adv_instance->timeout == 0 ||
  1352. adv_instance->duration <= adv_instance->remaining_time)
  1353. timeout = adv_instance->duration;
  1354. else
  1355. timeout = adv_instance->remaining_time;
  1356. /* The remaining time is being reduced unless the instance is being
  1357. * advertised without time limit.
  1358. */
  1359. if (adv_instance->timeout)
  1360. adv_instance->remaining_time =
  1361. adv_instance->remaining_time - timeout;
  1362. hdev->adv_instance_timeout = timeout;
  1363. queue_delayed_work(hdev->req_workqueue,
  1364. &hdev->adv_instance_expire,
  1365. msecs_to_jiffies(timeout * 1000));
  1366. /* If we're just re-scheduling the same instance again then do not
  1367. * execute any HCI commands. This happens when a single instance is
  1368. * being advertised.
  1369. */
  1370. if (!force && hdev->cur_adv_instance == instance &&
  1371. hci_dev_test_flag(hdev, HCI_LE_ADV))
  1372. return 0;
  1373. hdev->cur_adv_instance = instance;
  1374. if (ext_adv_capable(hdev)) {
  1375. __hci_req_start_ext_adv(req, instance);
  1376. } else {
  1377. __hci_req_update_adv_data(req, instance);
  1378. __hci_req_update_scan_rsp_data(req, instance);
  1379. __hci_req_enable_advertising(req);
  1380. }
  1381. return 0;
  1382. }
  1383. static void cancel_adv_timeout(struct hci_dev *hdev)
  1384. {
  1385. if (hdev->adv_instance_timeout) {
  1386. hdev->adv_instance_timeout = 0;
  1387. cancel_delayed_work(&hdev->adv_instance_expire);
  1388. }
  1389. }
  1390. /* For a single instance:
  1391. * - force == true: The instance will be removed even when its remaining
  1392. * lifetime is not zero.
  1393. * - force == false: the instance will be deactivated but kept stored unless
  1394. * the remaining lifetime is zero.
  1395. *
  1396. * For instance == 0x00:
  1397. * - force == true: All instances will be removed regardless of their timeout
  1398. * setting.
  1399. * - force == false: Only instances that have a timeout will be removed.
  1400. */
  1401. void hci_req_clear_adv_instance(struct hci_dev *hdev, struct sock *sk,
  1402. struct hci_request *req, u8 instance,
  1403. bool force)
  1404. {
  1405. struct adv_info *adv_instance, *n, *next_instance = NULL;
  1406. int err;
  1407. u8 rem_inst;
  1408. /* Cancel any timeout concerning the removed instance(s). */
  1409. if (!instance || hdev->cur_adv_instance == instance)
  1410. cancel_adv_timeout(hdev);
  1411. /* Get the next instance to advertise BEFORE we remove
  1412. * the current one. This can be the same instance again
  1413. * if there is only one instance.
  1414. */
  1415. if (instance && hdev->cur_adv_instance == instance)
  1416. next_instance = hci_get_next_instance(hdev, instance);
  1417. if (instance == 0x00) {
  1418. list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
  1419. list) {
  1420. if (!(force || adv_instance->timeout))
  1421. continue;
  1422. rem_inst = adv_instance->instance;
  1423. err = hci_remove_adv_instance(hdev, rem_inst);
  1424. if (!err)
  1425. mgmt_advertising_removed(sk, hdev, rem_inst);
  1426. }
  1427. } else {
  1428. adv_instance = hci_find_adv_instance(hdev, instance);
  1429. if (force || (adv_instance && adv_instance->timeout &&
  1430. !adv_instance->remaining_time)) {
  1431. /* Don't advertise a removed instance. */
  1432. if (next_instance &&
  1433. next_instance->instance == instance)
  1434. next_instance = NULL;
  1435. err = hci_remove_adv_instance(hdev, instance);
  1436. if (!err)
  1437. mgmt_advertising_removed(sk, hdev, instance);
  1438. }
  1439. }
  1440. if (!req || !hdev_is_powered(hdev) ||
  1441. hci_dev_test_flag(hdev, HCI_ADVERTISING))
  1442. return;
  1443. if (next_instance)
  1444. __hci_req_schedule_adv_instance(req, next_instance->instance,
  1445. false);
  1446. }
  1447. static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
  1448. {
  1449. struct hci_dev *hdev = req->hdev;
  1450. /* If we're advertising or initiating an LE connection we can't
  1451. * go ahead and change the random address at this time. This is
  1452. * because the eventual initiator address used for the
  1453. * subsequently created connection will be undefined (some
  1454. * controllers use the new address and others the one we had
  1455. * when the operation started).
  1456. *
  1457. * In this kind of scenario skip the update and let the random
  1458. * address be updated at the next cycle.
  1459. */
  1460. if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
  1461. hci_lookup_le_connect(hdev)) {
  1462. BT_DBG("Deferring random address update");
  1463. hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
  1464. return;
  1465. }
  1466. hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, rpa);
  1467. }
  1468. int hci_update_random_address(struct hci_request *req, bool require_privacy,
  1469. bool use_rpa, u8 *own_addr_type)
  1470. {
  1471. struct hci_dev *hdev = req->hdev;
  1472. int err;
  1473. /* If privacy is enabled use a resolvable private address. If
  1474. * current RPA has expired or there is something else than
  1475. * the current RPA in use, then generate a new one.
  1476. */
  1477. if (use_rpa) {
  1478. int to;
  1479. *own_addr_type = ADDR_LE_DEV_RANDOM;
  1480. if (!hci_dev_test_and_clear_flag(hdev, HCI_RPA_EXPIRED) &&
  1481. !bacmp(&hdev->random_addr, &hdev->rpa))
  1482. return 0;
  1483. err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
  1484. if (err < 0) {
  1485. bt_dev_err(hdev, "failed to generate new RPA");
  1486. return err;
  1487. }
  1488. set_random_addr(req, &hdev->rpa);
  1489. to = msecs_to_jiffies(hdev->rpa_timeout * 1000);
  1490. queue_delayed_work(hdev->workqueue, &hdev->rpa_expired, to);
  1491. return 0;
  1492. }
  1493. /* In case of required privacy without resolvable private address,
  1494. * use an non-resolvable private address. This is useful for active
  1495. * scanning and non-connectable advertising.
  1496. */
  1497. if (require_privacy) {
  1498. bdaddr_t nrpa;
  1499. while (true) {
  1500. /* The non-resolvable private address is generated
  1501. * from random six bytes with the two most significant
  1502. * bits cleared.
  1503. */
  1504. get_random_bytes(&nrpa, 6);
  1505. nrpa.b[5] &= 0x3f;
  1506. /* The non-resolvable private address shall not be
  1507. * equal to the public address.
  1508. */
  1509. if (bacmp(&hdev->bdaddr, &nrpa))
  1510. break;
  1511. }
  1512. *own_addr_type = ADDR_LE_DEV_RANDOM;
  1513. set_random_addr(req, &nrpa);
  1514. return 0;
  1515. }
  1516. /* If forcing static address is in use or there is no public
  1517. * address use the static address as random address (but skip
  1518. * the HCI command if the current random address is already the
  1519. * static one.
  1520. *
  1521. * In case BR/EDR has been disabled on a dual-mode controller
  1522. * and a static address has been configured, then use that
  1523. * address instead of the public BR/EDR address.
  1524. */
  1525. if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
  1526. !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
  1527. (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
  1528. bacmp(&hdev->static_addr, BDADDR_ANY))) {
  1529. *own_addr_type = ADDR_LE_DEV_RANDOM;
  1530. if (bacmp(&hdev->static_addr, &hdev->random_addr))
  1531. hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
  1532. &hdev->static_addr);
  1533. return 0;
  1534. }
  1535. /* Neither privacy nor static address is being used so use a
  1536. * public address.
  1537. */
  1538. *own_addr_type = ADDR_LE_DEV_PUBLIC;
  1539. return 0;
  1540. }
  1541. static bool disconnected_whitelist_entries(struct hci_dev *hdev)
  1542. {
  1543. struct bdaddr_list *b;
  1544. list_for_each_entry(b, &hdev->whitelist, list) {
  1545. struct hci_conn *conn;
  1546. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
  1547. if (!conn)
  1548. return true;
  1549. if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
  1550. return true;
  1551. }
  1552. return false;
  1553. }
  1554. void __hci_req_update_scan(struct hci_request *req)
  1555. {
  1556. struct hci_dev *hdev = req->hdev;
  1557. u8 scan;
  1558. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  1559. return;
  1560. if (!hdev_is_powered(hdev))
  1561. return;
  1562. if (mgmt_powering_down(hdev))
  1563. return;
  1564. if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
  1565. disconnected_whitelist_entries(hdev))
  1566. scan = SCAN_PAGE;
  1567. else
  1568. scan = SCAN_DISABLED;
  1569. if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
  1570. scan |= SCAN_INQUIRY;
  1571. if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
  1572. test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
  1573. return;
  1574. hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
  1575. }
  1576. static int update_scan(struct hci_request *req, unsigned long opt)
  1577. {
  1578. hci_dev_lock(req->hdev);
  1579. __hci_req_update_scan(req);
  1580. hci_dev_unlock(req->hdev);
  1581. return 0;
  1582. }
  1583. static void scan_update_work(struct work_struct *work)
  1584. {
  1585. struct hci_dev *hdev = container_of(work, struct hci_dev, scan_update);
  1586. hci_req_sync(hdev, update_scan, 0, HCI_CMD_TIMEOUT, NULL);
  1587. }
  1588. static int connectable_update(struct hci_request *req, unsigned long opt)
  1589. {
  1590. struct hci_dev *hdev = req->hdev;
  1591. hci_dev_lock(hdev);
  1592. __hci_req_update_scan(req);
  1593. /* If BR/EDR is not enabled and we disable advertising as a
  1594. * by-product of disabling connectable, we need to update the
  1595. * advertising flags.
  1596. */
  1597. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  1598. __hci_req_update_adv_data(req, hdev->cur_adv_instance);
  1599. /* Update the advertising parameters if necessary */
  1600. if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
  1601. !list_empty(&hdev->adv_instances)) {
  1602. if (ext_adv_capable(hdev))
  1603. __hci_req_start_ext_adv(req, hdev->cur_adv_instance);
  1604. else
  1605. __hci_req_enable_advertising(req);
  1606. }
  1607. __hci_update_background_scan(req);
  1608. hci_dev_unlock(hdev);
  1609. return 0;
  1610. }
  1611. static void connectable_update_work(struct work_struct *work)
  1612. {
  1613. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1614. connectable_update);
  1615. u8 status;
  1616. hci_req_sync(hdev, connectable_update, 0, HCI_CMD_TIMEOUT, &status);
  1617. mgmt_set_connectable_complete(hdev, status);
  1618. }
  1619. static u8 get_service_classes(struct hci_dev *hdev)
  1620. {
  1621. struct bt_uuid *uuid;
  1622. u8 val = 0;
  1623. list_for_each_entry(uuid, &hdev->uuids, list)
  1624. val |= uuid->svc_hint;
  1625. return val;
  1626. }
  1627. void __hci_req_update_class(struct hci_request *req)
  1628. {
  1629. struct hci_dev *hdev = req->hdev;
  1630. u8 cod[3];
  1631. BT_DBG("%s", hdev->name);
  1632. if (!hdev_is_powered(hdev))
  1633. return;
  1634. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  1635. return;
  1636. if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
  1637. return;
  1638. cod[0] = hdev->minor_class;
  1639. cod[1] = hdev->major_class;
  1640. cod[2] = get_service_classes(hdev);
  1641. if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
  1642. cod[1] |= 0x20;
  1643. if (memcmp(cod, hdev->dev_class, 3) == 0)
  1644. return;
  1645. hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
  1646. }
  1647. static void write_iac(struct hci_request *req)
  1648. {
  1649. struct hci_dev *hdev = req->hdev;
  1650. struct hci_cp_write_current_iac_lap cp;
  1651. if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
  1652. return;
  1653. if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
  1654. /* Limited discoverable mode */
  1655. cp.num_iac = min_t(u8, hdev->num_iac, 2);
  1656. cp.iac_lap[0] = 0x00; /* LIAC */
  1657. cp.iac_lap[1] = 0x8b;
  1658. cp.iac_lap[2] = 0x9e;
  1659. cp.iac_lap[3] = 0x33; /* GIAC */
  1660. cp.iac_lap[4] = 0x8b;
  1661. cp.iac_lap[5] = 0x9e;
  1662. } else {
  1663. /* General discoverable mode */
  1664. cp.num_iac = 1;
  1665. cp.iac_lap[0] = 0x33; /* GIAC */
  1666. cp.iac_lap[1] = 0x8b;
  1667. cp.iac_lap[2] = 0x9e;
  1668. }
  1669. hci_req_add(req, HCI_OP_WRITE_CURRENT_IAC_LAP,
  1670. (cp.num_iac * 3) + 1, &cp);
  1671. }
  1672. static int discoverable_update(struct hci_request *req, unsigned long opt)
  1673. {
  1674. struct hci_dev *hdev = req->hdev;
  1675. hci_dev_lock(hdev);
  1676. if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
  1677. write_iac(req);
  1678. __hci_req_update_scan(req);
  1679. __hci_req_update_class(req);
  1680. }
  1681. /* Advertising instances don't use the global discoverable setting, so
  1682. * only update AD if advertising was enabled using Set Advertising.
  1683. */
  1684. if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
  1685. __hci_req_update_adv_data(req, 0x00);
  1686. /* Discoverable mode affects the local advertising
  1687. * address in limited privacy mode.
  1688. */
  1689. if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
  1690. if (ext_adv_capable(hdev))
  1691. __hci_req_start_ext_adv(req, 0x00);
  1692. else
  1693. __hci_req_enable_advertising(req);
  1694. }
  1695. }
  1696. hci_dev_unlock(hdev);
  1697. return 0;
  1698. }
  1699. static void discoverable_update_work(struct work_struct *work)
  1700. {
  1701. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1702. discoverable_update);
  1703. u8 status;
  1704. hci_req_sync(hdev, discoverable_update, 0, HCI_CMD_TIMEOUT, &status);
  1705. mgmt_set_discoverable_complete(hdev, status);
  1706. }
  1707. void __hci_abort_conn(struct hci_request *req, struct hci_conn *conn,
  1708. u8 reason)
  1709. {
  1710. switch (conn->state) {
  1711. case BT_CONNECTED:
  1712. case BT_CONFIG:
  1713. if (conn->type == AMP_LINK) {
  1714. struct hci_cp_disconn_phy_link cp;
  1715. cp.phy_handle = HCI_PHY_HANDLE(conn->handle);
  1716. cp.reason = reason;
  1717. hci_req_add(req, HCI_OP_DISCONN_PHY_LINK, sizeof(cp),
  1718. &cp);
  1719. } else {
  1720. struct hci_cp_disconnect dc;
  1721. dc.handle = cpu_to_le16(conn->handle);
  1722. dc.reason = reason;
  1723. hci_req_add(req, HCI_OP_DISCONNECT, sizeof(dc), &dc);
  1724. }
  1725. conn->state = BT_DISCONN;
  1726. break;
  1727. case BT_CONNECT:
  1728. if (conn->type == LE_LINK) {
  1729. if (test_bit(HCI_CONN_SCANNING, &conn->flags))
  1730. break;
  1731. hci_req_add(req, HCI_OP_LE_CREATE_CONN_CANCEL,
  1732. 0, NULL);
  1733. } else if (conn->type == ACL_LINK) {
  1734. if (req->hdev->hci_ver < BLUETOOTH_VER_1_2)
  1735. break;
  1736. hci_req_add(req, HCI_OP_CREATE_CONN_CANCEL,
  1737. 6, &conn->dst);
  1738. }
  1739. break;
  1740. case BT_CONNECT2:
  1741. if (conn->type == ACL_LINK) {
  1742. struct hci_cp_reject_conn_req rej;
  1743. bacpy(&rej.bdaddr, &conn->dst);
  1744. rej.reason = reason;
  1745. hci_req_add(req, HCI_OP_REJECT_CONN_REQ,
  1746. sizeof(rej), &rej);
  1747. } else if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
  1748. struct hci_cp_reject_sync_conn_req rej;
  1749. bacpy(&rej.bdaddr, &conn->dst);
  1750. /* SCO rejection has its own limited set of
  1751. * allowed error values (0x0D-0x0F) which isn't
  1752. * compatible with most values passed to this
  1753. * function. To be safe hard-code one of the
  1754. * values that's suitable for SCO.
  1755. */
  1756. rej.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
  1757. hci_req_add(req, HCI_OP_REJECT_SYNC_CONN_REQ,
  1758. sizeof(rej), &rej);
  1759. }
  1760. break;
  1761. default:
  1762. conn->state = BT_CLOSED;
  1763. break;
  1764. }
  1765. }
  1766. static void abort_conn_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  1767. {
  1768. if (status)
  1769. BT_DBG("Failed to abort connection: status 0x%2.2x", status);
  1770. }
  1771. int hci_abort_conn(struct hci_conn *conn, u8 reason)
  1772. {
  1773. struct hci_request req;
  1774. int err;
  1775. hci_req_init(&req, conn->hdev);
  1776. __hci_abort_conn(&req, conn, reason);
  1777. err = hci_req_run(&req, abort_conn_complete);
  1778. if (err && err != -ENODATA) {
  1779. bt_dev_err(conn->hdev, "failed to run HCI request: err %d", err);
  1780. return err;
  1781. }
  1782. return 0;
  1783. }
  1784. static int update_bg_scan(struct hci_request *req, unsigned long opt)
  1785. {
  1786. hci_dev_lock(req->hdev);
  1787. __hci_update_background_scan(req);
  1788. hci_dev_unlock(req->hdev);
  1789. return 0;
  1790. }
  1791. static void bg_scan_update(struct work_struct *work)
  1792. {
  1793. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1794. bg_scan_update);
  1795. struct hci_conn *conn;
  1796. u8 status;
  1797. int err;
  1798. err = hci_req_sync(hdev, update_bg_scan, 0, HCI_CMD_TIMEOUT, &status);
  1799. if (!err)
  1800. return;
  1801. hci_dev_lock(hdev);
  1802. conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
  1803. if (conn)
  1804. hci_le_conn_failed(conn, status);
  1805. hci_dev_unlock(hdev);
  1806. }
  1807. static int le_scan_disable(struct hci_request *req, unsigned long opt)
  1808. {
  1809. hci_req_add_le_scan_disable(req);
  1810. return 0;
  1811. }
  1812. static int bredr_inquiry(struct hci_request *req, unsigned long opt)
  1813. {
  1814. u8 length = opt;
  1815. const u8 giac[3] = { 0x33, 0x8b, 0x9e };
  1816. const u8 liac[3] = { 0x00, 0x8b, 0x9e };
  1817. struct hci_cp_inquiry cp;
  1818. BT_DBG("%s", req->hdev->name);
  1819. hci_dev_lock(req->hdev);
  1820. hci_inquiry_cache_flush(req->hdev);
  1821. hci_dev_unlock(req->hdev);
  1822. memset(&cp, 0, sizeof(cp));
  1823. if (req->hdev->discovery.limited)
  1824. memcpy(&cp.lap, liac, sizeof(cp.lap));
  1825. else
  1826. memcpy(&cp.lap, giac, sizeof(cp.lap));
  1827. cp.length = length;
  1828. hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp);
  1829. return 0;
  1830. }
  1831. static void le_scan_disable_work(struct work_struct *work)
  1832. {
  1833. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1834. le_scan_disable.work);
  1835. u8 status;
  1836. BT_DBG("%s", hdev->name);
  1837. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
  1838. return;
  1839. cancel_delayed_work(&hdev->le_scan_restart);
  1840. hci_req_sync(hdev, le_scan_disable, 0, HCI_CMD_TIMEOUT, &status);
  1841. if (status) {
  1842. bt_dev_err(hdev, "failed to disable LE scan: status 0x%02x",
  1843. status);
  1844. return;
  1845. }
  1846. hdev->discovery.scan_start = 0;
  1847. /* If we were running LE only scan, change discovery state. If
  1848. * we were running both LE and BR/EDR inquiry simultaneously,
  1849. * and BR/EDR inquiry is already finished, stop discovery,
  1850. * otherwise BR/EDR inquiry will stop discovery when finished.
  1851. * If we will resolve remote device name, do not change
  1852. * discovery state.
  1853. */
  1854. if (hdev->discovery.type == DISCOV_TYPE_LE)
  1855. goto discov_stopped;
  1856. if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED)
  1857. return;
  1858. if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) {
  1859. if (!test_bit(HCI_INQUIRY, &hdev->flags) &&
  1860. hdev->discovery.state != DISCOVERY_RESOLVING)
  1861. goto discov_stopped;
  1862. return;
  1863. }
  1864. hci_req_sync(hdev, bredr_inquiry, DISCOV_INTERLEAVED_INQUIRY_LEN,
  1865. HCI_CMD_TIMEOUT, &status);
  1866. if (status) {
  1867. bt_dev_err(hdev, "inquiry failed: status 0x%02x", status);
  1868. goto discov_stopped;
  1869. }
  1870. return;
  1871. discov_stopped:
  1872. hci_dev_lock(hdev);
  1873. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  1874. hci_dev_unlock(hdev);
  1875. }
  1876. static int le_scan_restart(struct hci_request *req, unsigned long opt)
  1877. {
  1878. struct hci_dev *hdev = req->hdev;
  1879. /* If controller is not scanning we are done. */
  1880. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
  1881. return 0;
  1882. hci_req_add_le_scan_disable(req);
  1883. if (use_ext_scan(hdev)) {
  1884. struct hci_cp_le_set_ext_scan_enable ext_enable_cp;
  1885. memset(&ext_enable_cp, 0, sizeof(ext_enable_cp));
  1886. ext_enable_cp.enable = LE_SCAN_ENABLE;
  1887. ext_enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
  1888. hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
  1889. sizeof(ext_enable_cp), &ext_enable_cp);
  1890. } else {
  1891. struct hci_cp_le_set_scan_enable cp;
  1892. memset(&cp, 0, sizeof(cp));
  1893. cp.enable = LE_SCAN_ENABLE;
  1894. cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
  1895. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
  1896. }
  1897. return 0;
  1898. }
  1899. static void le_scan_restart_work(struct work_struct *work)
  1900. {
  1901. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1902. le_scan_restart.work);
  1903. unsigned long timeout, duration, scan_start, now;
  1904. u8 status;
  1905. BT_DBG("%s", hdev->name);
  1906. hci_req_sync(hdev, le_scan_restart, 0, HCI_CMD_TIMEOUT, &status);
  1907. if (status) {
  1908. bt_dev_err(hdev, "failed to restart LE scan: status %d",
  1909. status);
  1910. return;
  1911. }
  1912. hci_dev_lock(hdev);
  1913. if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) ||
  1914. !hdev->discovery.scan_start)
  1915. goto unlock;
  1916. /* When the scan was started, hdev->le_scan_disable has been queued
  1917. * after duration from scan_start. During scan restart this job
  1918. * has been canceled, and we need to queue it again after proper
  1919. * timeout, to make sure that scan does not run indefinitely.
  1920. */
  1921. duration = hdev->discovery.scan_duration;
  1922. scan_start = hdev->discovery.scan_start;
  1923. now = jiffies;
  1924. if (now - scan_start <= duration) {
  1925. int elapsed;
  1926. if (now >= scan_start)
  1927. elapsed = now - scan_start;
  1928. else
  1929. elapsed = ULONG_MAX - scan_start + now;
  1930. timeout = duration - elapsed;
  1931. } else {
  1932. timeout = 0;
  1933. }
  1934. queue_delayed_work(hdev->req_workqueue,
  1935. &hdev->le_scan_disable, timeout);
  1936. unlock:
  1937. hci_dev_unlock(hdev);
  1938. }
  1939. static int active_scan(struct hci_request *req, unsigned long opt)
  1940. {
  1941. uint16_t interval = opt;
  1942. struct hci_dev *hdev = req->hdev;
  1943. u8 own_addr_type;
  1944. int err;
  1945. BT_DBG("%s", hdev->name);
  1946. if (hci_dev_test_flag(hdev, HCI_LE_ADV)) {
  1947. hci_dev_lock(hdev);
  1948. /* Don't let discovery abort an outgoing connection attempt
  1949. * that's using directed advertising.
  1950. */
  1951. if (hci_lookup_le_connect(hdev)) {
  1952. hci_dev_unlock(hdev);
  1953. return -EBUSY;
  1954. }
  1955. cancel_adv_timeout(hdev);
  1956. hci_dev_unlock(hdev);
  1957. __hci_req_disable_advertising(req);
  1958. }
  1959. /* If controller is scanning, it means the background scanning is
  1960. * running. Thus, we should temporarily stop it in order to set the
  1961. * discovery scanning parameters.
  1962. */
  1963. if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
  1964. hci_req_add_le_scan_disable(req);
  1965. /* All active scans will be done with either a resolvable private
  1966. * address (when privacy feature has been enabled) or non-resolvable
  1967. * private address.
  1968. */
  1969. err = hci_update_random_address(req, true, scan_use_rpa(hdev),
  1970. &own_addr_type);
  1971. if (err < 0)
  1972. own_addr_type = ADDR_LE_DEV_PUBLIC;
  1973. hci_req_start_scan(req, LE_SCAN_ACTIVE, interval, DISCOV_LE_SCAN_WIN,
  1974. own_addr_type, 0);
  1975. return 0;
  1976. }
  1977. static int interleaved_discov(struct hci_request *req, unsigned long opt)
  1978. {
  1979. int err;
  1980. BT_DBG("%s", req->hdev->name);
  1981. err = active_scan(req, opt);
  1982. if (err)
  1983. return err;
  1984. return bredr_inquiry(req, DISCOV_BREDR_INQUIRY_LEN);
  1985. }
  1986. static void start_discovery(struct hci_dev *hdev, u8 *status)
  1987. {
  1988. unsigned long timeout;
  1989. BT_DBG("%s type %u", hdev->name, hdev->discovery.type);
  1990. switch (hdev->discovery.type) {
  1991. case DISCOV_TYPE_BREDR:
  1992. if (!hci_dev_test_flag(hdev, HCI_INQUIRY))
  1993. hci_req_sync(hdev, bredr_inquiry,
  1994. DISCOV_BREDR_INQUIRY_LEN, HCI_CMD_TIMEOUT,
  1995. status);
  1996. return;
  1997. case DISCOV_TYPE_INTERLEAVED:
  1998. /* When running simultaneous discovery, the LE scanning time
  1999. * should occupy the whole discovery time sine BR/EDR inquiry
  2000. * and LE scanning are scheduled by the controller.
  2001. *
  2002. * For interleaving discovery in comparison, BR/EDR inquiry
  2003. * and LE scanning are done sequentially with separate
  2004. * timeouts.
  2005. */
  2006. if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
  2007. &hdev->quirks)) {
  2008. timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
  2009. /* During simultaneous discovery, we double LE scan
  2010. * interval. We must leave some time for the controller
  2011. * to do BR/EDR inquiry.
  2012. */
  2013. hci_req_sync(hdev, interleaved_discov,
  2014. DISCOV_LE_SCAN_INT * 2, HCI_CMD_TIMEOUT,
  2015. status);
  2016. break;
  2017. }
  2018. timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
  2019. hci_req_sync(hdev, active_scan, DISCOV_LE_SCAN_INT,
  2020. HCI_CMD_TIMEOUT, status);
  2021. break;
  2022. case DISCOV_TYPE_LE:
  2023. timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
  2024. hci_req_sync(hdev, active_scan, DISCOV_LE_SCAN_INT,
  2025. HCI_CMD_TIMEOUT, status);
  2026. break;
  2027. default:
  2028. *status = HCI_ERROR_UNSPECIFIED;
  2029. return;
  2030. }
  2031. if (*status)
  2032. return;
  2033. BT_DBG("%s timeout %u ms", hdev->name, jiffies_to_msecs(timeout));
  2034. /* When service discovery is used and the controller has a
  2035. * strict duplicate filter, it is important to remember the
  2036. * start and duration of the scan. This is required for
  2037. * restarting scanning during the discovery phase.
  2038. */
  2039. if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
  2040. hdev->discovery.result_filtering) {
  2041. hdev->discovery.scan_start = jiffies;
  2042. hdev->discovery.scan_duration = timeout;
  2043. }
  2044. queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
  2045. timeout);
  2046. }
  2047. bool hci_req_stop_discovery(struct hci_request *req)
  2048. {
  2049. struct hci_dev *hdev = req->hdev;
  2050. struct discovery_state *d = &hdev->discovery;
  2051. struct hci_cp_remote_name_req_cancel cp;
  2052. struct inquiry_entry *e;
  2053. bool ret = false;
  2054. BT_DBG("%s state %u", hdev->name, hdev->discovery.state);
  2055. if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
  2056. if (test_bit(HCI_INQUIRY, &hdev->flags))
  2057. hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL);
  2058. if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
  2059. cancel_delayed_work(&hdev->le_scan_disable);
  2060. hci_req_add_le_scan_disable(req);
  2061. }
  2062. ret = true;
  2063. } else {
  2064. /* Passive scanning */
  2065. if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
  2066. hci_req_add_le_scan_disable(req);
  2067. ret = true;
  2068. }
  2069. }
  2070. /* No further actions needed for LE-only discovery */
  2071. if (d->type == DISCOV_TYPE_LE)
  2072. return ret;
  2073. if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
  2074. e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
  2075. NAME_PENDING);
  2076. if (!e)
  2077. return ret;
  2078. bacpy(&cp.bdaddr, &e->data.bdaddr);
  2079. hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp),
  2080. &cp);
  2081. ret = true;
  2082. }
  2083. return ret;
  2084. }
  2085. static int stop_discovery(struct hci_request *req, unsigned long opt)
  2086. {
  2087. hci_dev_lock(req->hdev);
  2088. hci_req_stop_discovery(req);
  2089. hci_dev_unlock(req->hdev);
  2090. return 0;
  2091. }
  2092. static void discov_update(struct work_struct *work)
  2093. {
  2094. struct hci_dev *hdev = container_of(work, struct hci_dev,
  2095. discov_update);
  2096. u8 status = 0;
  2097. switch (hdev->discovery.state) {
  2098. case DISCOVERY_STARTING:
  2099. start_discovery(hdev, &status);
  2100. mgmt_start_discovery_complete(hdev, status);
  2101. if (status)
  2102. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  2103. else
  2104. hci_discovery_set_state(hdev, DISCOVERY_FINDING);
  2105. break;
  2106. case DISCOVERY_STOPPING:
  2107. hci_req_sync(hdev, stop_discovery, 0, HCI_CMD_TIMEOUT, &status);
  2108. mgmt_stop_discovery_complete(hdev, status);
  2109. if (!status)
  2110. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  2111. break;
  2112. case DISCOVERY_STOPPED:
  2113. default:
  2114. return;
  2115. }
  2116. }
  2117. static void discov_off(struct work_struct *work)
  2118. {
  2119. struct hci_dev *hdev = container_of(work, struct hci_dev,
  2120. discov_off.work);
  2121. BT_DBG("%s", hdev->name);
  2122. hci_dev_lock(hdev);
  2123. /* When discoverable timeout triggers, then just make sure
  2124. * the limited discoverable flag is cleared. Even in the case
  2125. * of a timeout triggered from general discoverable, it is
  2126. * safe to unconditionally clear the flag.
  2127. */
  2128. hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
  2129. hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
  2130. hdev->discov_timeout = 0;
  2131. hci_dev_unlock(hdev);
  2132. hci_req_sync(hdev, discoverable_update, 0, HCI_CMD_TIMEOUT, NULL);
  2133. mgmt_new_settings(hdev);
  2134. }
  2135. static int powered_update_hci(struct hci_request *req, unsigned long opt)
  2136. {
  2137. struct hci_dev *hdev = req->hdev;
  2138. u8 link_sec;
  2139. hci_dev_lock(hdev);
  2140. if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
  2141. !lmp_host_ssp_capable(hdev)) {
  2142. u8 mode = 0x01;
  2143. hci_req_add(req, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
  2144. if (bredr_sc_enabled(hdev) && !lmp_host_sc_capable(hdev)) {
  2145. u8 support = 0x01;
  2146. hci_req_add(req, HCI_OP_WRITE_SC_SUPPORT,
  2147. sizeof(support), &support);
  2148. }
  2149. }
  2150. if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
  2151. lmp_bredr_capable(hdev)) {
  2152. struct hci_cp_write_le_host_supported cp;
  2153. cp.le = 0x01;
  2154. cp.simul = 0x00;
  2155. /* Check first if we already have the right
  2156. * host state (host features set)
  2157. */
  2158. if (cp.le != lmp_host_le_capable(hdev) ||
  2159. cp.simul != lmp_host_le_br_capable(hdev))
  2160. hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED,
  2161. sizeof(cp), &cp);
  2162. }
  2163. if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
  2164. /* Make sure the controller has a good default for
  2165. * advertising data. This also applies to the case
  2166. * where BR/EDR was toggled during the AUTO_OFF phase.
  2167. */
  2168. if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
  2169. list_empty(&hdev->adv_instances)) {
  2170. int err;
  2171. if (ext_adv_capable(hdev)) {
  2172. err = __hci_req_setup_ext_adv_instance(req,
  2173. 0x00);
  2174. if (!err)
  2175. __hci_req_update_scan_rsp_data(req,
  2176. 0x00);
  2177. } else {
  2178. err = 0;
  2179. __hci_req_update_adv_data(req, 0x00);
  2180. __hci_req_update_scan_rsp_data(req, 0x00);
  2181. }
  2182. if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
  2183. if (!ext_adv_capable(hdev))
  2184. __hci_req_enable_advertising(req);
  2185. else if (!err)
  2186. __hci_req_enable_ext_advertising(req);
  2187. }
  2188. } else if (!list_empty(&hdev->adv_instances)) {
  2189. struct adv_info *adv_instance;
  2190. adv_instance = list_first_entry(&hdev->adv_instances,
  2191. struct adv_info, list);
  2192. __hci_req_schedule_adv_instance(req,
  2193. adv_instance->instance,
  2194. true);
  2195. }
  2196. }
  2197. link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
  2198. if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
  2199. hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE,
  2200. sizeof(link_sec), &link_sec);
  2201. if (lmp_bredr_capable(hdev)) {
  2202. if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
  2203. __hci_req_write_fast_connectable(req, true);
  2204. else
  2205. __hci_req_write_fast_connectable(req, false);
  2206. __hci_req_update_scan(req);
  2207. __hci_req_update_class(req);
  2208. __hci_req_update_name(req);
  2209. __hci_req_update_eir(req);
  2210. }
  2211. hci_dev_unlock(hdev);
  2212. return 0;
  2213. }
  2214. int __hci_req_hci_power_on(struct hci_dev *hdev)
  2215. {
  2216. /* Register the available SMP channels (BR/EDR and LE) only when
  2217. * successfully powering on the controller. This late
  2218. * registration is required so that LE SMP can clearly decide if
  2219. * the public address or static address is used.
  2220. */
  2221. smp_register(hdev);
  2222. return __hci_req_sync(hdev, powered_update_hci, 0, HCI_CMD_TIMEOUT,
  2223. NULL);
  2224. }
  2225. void hci_request_setup(struct hci_dev *hdev)
  2226. {
  2227. INIT_WORK(&hdev->discov_update, discov_update);
  2228. INIT_WORK(&hdev->bg_scan_update, bg_scan_update);
  2229. INIT_WORK(&hdev->scan_update, scan_update_work);
  2230. INIT_WORK(&hdev->connectable_update, connectable_update_work);
  2231. INIT_WORK(&hdev->discoverable_update, discoverable_update_work);
  2232. INIT_DELAYED_WORK(&hdev->discov_off, discov_off);
  2233. INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work);
  2234. INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart_work);
  2235. INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
  2236. }
  2237. void hci_request_cancel_all(struct hci_dev *hdev)
  2238. {
  2239. hci_req_sync_cancel(hdev, ENODEV);
  2240. cancel_work_sync(&hdev->discov_update);
  2241. cancel_work_sync(&hdev->bg_scan_update);
  2242. cancel_work_sync(&hdev->scan_update);
  2243. cancel_work_sync(&hdev->connectable_update);
  2244. cancel_work_sync(&hdev->discoverable_update);
  2245. cancel_delayed_work_sync(&hdev->discov_off);
  2246. cancel_delayed_work_sync(&hdev->le_scan_disable);
  2247. cancel_delayed_work_sync(&hdev->le_scan_restart);
  2248. if (hdev->adv_instance_timeout) {
  2249. cancel_delayed_work_sync(&hdev->adv_instance_expire);
  2250. hdev->adv_instance_timeout = 0;
  2251. }
  2252. }