hci_conn.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
  4. Copyright 2023-2024 NXP
  5. Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License version 2 as
  8. published by the Free Software Foundation;
  9. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  10. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  11. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  12. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  13. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  14. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  18. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  19. SOFTWARE IS DISCLAIMED.
  20. */
  21. /* Bluetooth HCI connection handling. */
  22. #include <linux/export.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/errqueue.h>
  25. #include <net/bluetooth/bluetooth.h>
  26. #include <net/bluetooth/hci_core.h>
  27. #include <net/bluetooth/l2cap.h>
  28. #include <net/bluetooth/iso.h>
  29. #include <net/bluetooth/mgmt.h>
  30. #include "smp.h"
  31. #include "eir.h"
  32. struct sco_param {
  33. u16 pkt_type;
  34. u16 max_latency;
  35. u8 retrans_effort;
  36. };
  37. struct conn_handle_t {
  38. struct hci_conn *conn;
  39. __u16 handle;
  40. };
  41. static const struct sco_param esco_param_cvsd[] = {
  42. { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a, 0x01 }, /* S3 */
  43. { EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007, 0x01 }, /* S2 */
  44. { EDR_ESCO_MASK | ESCO_EV3, 0x0007, 0x01 }, /* S1 */
  45. { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0x01 }, /* D1 */
  46. { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0x01 }, /* D0 */
  47. };
  48. static const struct sco_param sco_param_cvsd[] = {
  49. { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0xff }, /* D1 */
  50. { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0xff }, /* D0 */
  51. };
  52. static const struct sco_param esco_param_msbc[] = {
  53. { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d, 0x02 }, /* T2 */
  54. { EDR_ESCO_MASK | ESCO_EV3, 0x0008, 0x02 }, /* T1 */
  55. };
  56. /* This function requires the caller holds hdev->lock */
  57. void hci_connect_le_scan_cleanup(struct hci_conn *conn, u8 status)
  58. {
  59. struct hci_conn_params *params;
  60. struct hci_dev *hdev = conn->hdev;
  61. struct smp_irk *irk;
  62. bdaddr_t *bdaddr;
  63. u8 bdaddr_type;
  64. bdaddr = &conn->dst;
  65. bdaddr_type = conn->dst_type;
  66. /* Check if we need to convert to identity address */
  67. irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
  68. if (irk) {
  69. bdaddr = &irk->bdaddr;
  70. bdaddr_type = irk->addr_type;
  71. }
  72. params = hci_pend_le_action_lookup(&hdev->pend_le_conns, bdaddr,
  73. bdaddr_type);
  74. if (!params)
  75. return;
  76. if (params->conn) {
  77. hci_conn_drop(params->conn);
  78. hci_conn_put(params->conn);
  79. params->conn = NULL;
  80. }
  81. if (!params->explicit_connect)
  82. return;
  83. /* If the status indicates successful cancellation of
  84. * the attempt (i.e. Unknown Connection Id) there's no point of
  85. * notifying failure since we'll go back to keep trying to
  86. * connect. The only exception is explicit connect requests
  87. * where a timeout + cancel does indicate an actual failure.
  88. */
  89. if (status && status != HCI_ERROR_UNKNOWN_CONN_ID)
  90. mgmt_connect_failed(hdev, conn, status);
  91. /* The connection attempt was doing scan for new RPA, and is
  92. * in scan phase. If params are not associated with any other
  93. * autoconnect action, remove them completely. If they are, just unmark
  94. * them as waiting for connection, by clearing explicit_connect field.
  95. */
  96. params->explicit_connect = false;
  97. hci_pend_le_list_del_init(params);
  98. switch (params->auto_connect) {
  99. case HCI_AUTO_CONN_EXPLICIT:
  100. hci_conn_params_del(hdev, bdaddr, bdaddr_type);
  101. /* return instead of break to avoid duplicate scan update */
  102. return;
  103. case HCI_AUTO_CONN_DIRECT:
  104. case HCI_AUTO_CONN_ALWAYS:
  105. hci_pend_le_list_add(params, &hdev->pend_le_conns);
  106. break;
  107. case HCI_AUTO_CONN_REPORT:
  108. hci_pend_le_list_add(params, &hdev->pend_le_reports);
  109. break;
  110. default:
  111. break;
  112. }
  113. hci_update_passive_scan(hdev);
  114. }
  115. static void hci_conn_cleanup(struct hci_conn *conn)
  116. {
  117. struct hci_dev *hdev = conn->hdev;
  118. if (test_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags))
  119. hci_conn_params_del(conn->hdev, &conn->dst, conn->dst_type);
  120. if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
  121. hci_remove_link_key(hdev, &conn->dst);
  122. hci_chan_list_flush(conn);
  123. if (HCI_CONN_HANDLE_UNSET(conn->handle))
  124. ida_free(&hdev->unset_handle_ida, conn->handle);
  125. if (conn->cleanup)
  126. conn->cleanup(conn);
  127. if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
  128. switch (conn->setting & SCO_AIRMODE_MASK) {
  129. case SCO_AIRMODE_CVSD:
  130. case SCO_AIRMODE_TRANSP:
  131. if (hdev->notify)
  132. hdev->notify(hdev, HCI_NOTIFY_DISABLE_SCO);
  133. break;
  134. }
  135. } else {
  136. if (hdev->notify)
  137. hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
  138. }
  139. debugfs_remove_recursive(conn->debugfs);
  140. hci_conn_del_sysfs(conn);
  141. hci_dev_put(hdev);
  142. }
  143. int hci_disconnect(struct hci_conn *conn, __u8 reason)
  144. {
  145. BT_DBG("hcon %p", conn);
  146. /* When we are central of an established connection and it enters
  147. * the disconnect timeout, then go ahead and try to read the
  148. * current clock offset. Processing of the result is done
  149. * within the event handling and hci_clock_offset_evt function.
  150. */
  151. if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER &&
  152. (conn->state == BT_CONNECTED || conn->state == BT_CONFIG)) {
  153. struct hci_dev *hdev = conn->hdev;
  154. struct hci_cp_read_clock_offset clkoff_cp;
  155. clkoff_cp.handle = cpu_to_le16(conn->handle);
  156. hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp),
  157. &clkoff_cp);
  158. }
  159. return hci_abort_conn(conn, reason);
  160. }
  161. static void hci_add_sco(struct hci_conn *conn, __u16 handle)
  162. {
  163. struct hci_dev *hdev = conn->hdev;
  164. struct hci_cp_add_sco cp;
  165. BT_DBG("hcon %p", conn);
  166. conn->state = BT_CONNECT;
  167. conn->out = true;
  168. conn->attempt++;
  169. cp.handle = cpu_to_le16(handle);
  170. cp.pkt_type = cpu_to_le16(conn->pkt_type);
  171. hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
  172. }
  173. static bool find_next_esco_param(struct hci_conn *conn,
  174. const struct sco_param *esco_param, int size)
  175. {
  176. if (!conn->parent)
  177. return false;
  178. for (; conn->attempt <= size; conn->attempt++) {
  179. if (lmp_esco_2m_capable(conn->parent) ||
  180. (esco_param[conn->attempt - 1].pkt_type & ESCO_2EV3))
  181. break;
  182. BT_DBG("hcon %p skipped attempt %d, eSCO 2M not supported",
  183. conn, conn->attempt);
  184. }
  185. return conn->attempt <= size;
  186. }
  187. static int configure_datapath_sync(struct hci_dev *hdev, struct bt_codec *codec)
  188. {
  189. int err;
  190. __u8 vnd_len, *vnd_data = NULL;
  191. struct hci_op_configure_data_path *cmd = NULL;
  192. /* Do not take below 2 checks as error since the 1st means user do not
  193. * want to use HFP offload mode and the 2nd means the vendor controller
  194. * do not need to send below HCI command for offload mode.
  195. */
  196. if (!codec->data_path || !hdev->get_codec_config_data)
  197. return 0;
  198. err = hdev->get_codec_config_data(hdev, ESCO_LINK, codec, &vnd_len,
  199. &vnd_data);
  200. if (err < 0)
  201. goto error;
  202. cmd = kzalloc(sizeof(*cmd) + vnd_len, GFP_KERNEL);
  203. if (!cmd) {
  204. err = -ENOMEM;
  205. goto error;
  206. }
  207. err = hdev->get_data_path_id(hdev, &cmd->data_path_id);
  208. if (err < 0)
  209. goto error;
  210. cmd->vnd_len = vnd_len;
  211. memcpy(cmd->vnd_data, vnd_data, vnd_len);
  212. cmd->direction = 0x00;
  213. __hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH,
  214. sizeof(*cmd) + vnd_len, cmd, HCI_CMD_TIMEOUT);
  215. cmd->direction = 0x01;
  216. err = __hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH,
  217. sizeof(*cmd) + vnd_len, cmd,
  218. HCI_CMD_TIMEOUT);
  219. error:
  220. kfree(cmd);
  221. kfree(vnd_data);
  222. return err;
  223. }
  224. static int hci_enhanced_setup_sync(struct hci_dev *hdev, void *data)
  225. {
  226. struct conn_handle_t *conn_handle = data;
  227. struct hci_conn *conn = conn_handle->conn;
  228. __u16 handle = conn_handle->handle;
  229. struct hci_cp_enhanced_setup_sync_conn cp;
  230. const struct sco_param *param;
  231. kfree(conn_handle);
  232. if (!hci_conn_valid(hdev, conn))
  233. return -ECANCELED;
  234. bt_dev_dbg(hdev, "hcon %p", conn);
  235. configure_datapath_sync(hdev, &conn->codec);
  236. conn->state = BT_CONNECT;
  237. conn->out = true;
  238. conn->attempt++;
  239. memset(&cp, 0x00, sizeof(cp));
  240. cp.handle = cpu_to_le16(handle);
  241. cp.tx_bandwidth = cpu_to_le32(0x00001f40);
  242. cp.rx_bandwidth = cpu_to_le32(0x00001f40);
  243. switch (conn->codec.id) {
  244. case BT_CODEC_MSBC:
  245. if (!find_next_esco_param(conn, esco_param_msbc,
  246. ARRAY_SIZE(esco_param_msbc)))
  247. return -EINVAL;
  248. param = &esco_param_msbc[conn->attempt - 1];
  249. cp.tx_coding_format.id = 0x05;
  250. cp.rx_coding_format.id = 0x05;
  251. cp.tx_codec_frame_size = __cpu_to_le16(60);
  252. cp.rx_codec_frame_size = __cpu_to_le16(60);
  253. cp.in_bandwidth = __cpu_to_le32(32000);
  254. cp.out_bandwidth = __cpu_to_le32(32000);
  255. cp.in_coding_format.id = 0x04;
  256. cp.out_coding_format.id = 0x04;
  257. cp.in_coded_data_size = __cpu_to_le16(16);
  258. cp.out_coded_data_size = __cpu_to_le16(16);
  259. cp.in_pcm_data_format = 2;
  260. cp.out_pcm_data_format = 2;
  261. cp.in_pcm_sample_payload_msb_pos = 0;
  262. cp.out_pcm_sample_payload_msb_pos = 0;
  263. cp.in_data_path = conn->codec.data_path;
  264. cp.out_data_path = conn->codec.data_path;
  265. cp.in_transport_unit_size = 1;
  266. cp.out_transport_unit_size = 1;
  267. break;
  268. case BT_CODEC_TRANSPARENT:
  269. if (!find_next_esco_param(conn, esco_param_msbc,
  270. ARRAY_SIZE(esco_param_msbc)))
  271. return -EINVAL;
  272. param = &esco_param_msbc[conn->attempt - 1];
  273. cp.tx_coding_format.id = 0x03;
  274. cp.rx_coding_format.id = 0x03;
  275. cp.tx_codec_frame_size = __cpu_to_le16(60);
  276. cp.rx_codec_frame_size = __cpu_to_le16(60);
  277. cp.in_bandwidth = __cpu_to_le32(0x1f40);
  278. cp.out_bandwidth = __cpu_to_le32(0x1f40);
  279. cp.in_coding_format.id = 0x03;
  280. cp.out_coding_format.id = 0x03;
  281. cp.in_coded_data_size = __cpu_to_le16(16);
  282. cp.out_coded_data_size = __cpu_to_le16(16);
  283. cp.in_pcm_data_format = 2;
  284. cp.out_pcm_data_format = 2;
  285. cp.in_pcm_sample_payload_msb_pos = 0;
  286. cp.out_pcm_sample_payload_msb_pos = 0;
  287. cp.in_data_path = conn->codec.data_path;
  288. cp.out_data_path = conn->codec.data_path;
  289. cp.in_transport_unit_size = 1;
  290. cp.out_transport_unit_size = 1;
  291. break;
  292. case BT_CODEC_CVSD:
  293. if (conn->parent && lmp_esco_capable(conn->parent)) {
  294. if (!find_next_esco_param(conn, esco_param_cvsd,
  295. ARRAY_SIZE(esco_param_cvsd)))
  296. return -EINVAL;
  297. param = &esco_param_cvsd[conn->attempt - 1];
  298. } else {
  299. if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
  300. return -EINVAL;
  301. param = &sco_param_cvsd[conn->attempt - 1];
  302. }
  303. cp.tx_coding_format.id = 2;
  304. cp.rx_coding_format.id = 2;
  305. cp.tx_codec_frame_size = __cpu_to_le16(60);
  306. cp.rx_codec_frame_size = __cpu_to_le16(60);
  307. cp.in_bandwidth = __cpu_to_le32(16000);
  308. cp.out_bandwidth = __cpu_to_le32(16000);
  309. cp.in_coding_format.id = 4;
  310. cp.out_coding_format.id = 4;
  311. cp.in_coded_data_size = __cpu_to_le16(16);
  312. cp.out_coded_data_size = __cpu_to_le16(16);
  313. cp.in_pcm_data_format = 2;
  314. cp.out_pcm_data_format = 2;
  315. cp.in_pcm_sample_payload_msb_pos = 0;
  316. cp.out_pcm_sample_payload_msb_pos = 0;
  317. cp.in_data_path = conn->codec.data_path;
  318. cp.out_data_path = conn->codec.data_path;
  319. cp.in_transport_unit_size = 16;
  320. cp.out_transport_unit_size = 16;
  321. break;
  322. default:
  323. return -EINVAL;
  324. }
  325. cp.retrans_effort = param->retrans_effort;
  326. cp.pkt_type = __cpu_to_le16(param->pkt_type);
  327. cp.max_latency = __cpu_to_le16(param->max_latency);
  328. if (hci_send_cmd(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
  329. return -EIO;
  330. return 0;
  331. }
  332. static bool hci_setup_sync_conn(struct hci_conn *conn, __u16 handle)
  333. {
  334. struct hci_dev *hdev = conn->hdev;
  335. struct hci_cp_setup_sync_conn cp;
  336. const struct sco_param *param;
  337. bt_dev_dbg(hdev, "hcon %p", conn);
  338. conn->state = BT_CONNECT;
  339. conn->out = true;
  340. conn->attempt++;
  341. cp.handle = cpu_to_le16(handle);
  342. cp.tx_bandwidth = cpu_to_le32(0x00001f40);
  343. cp.rx_bandwidth = cpu_to_le32(0x00001f40);
  344. cp.voice_setting = cpu_to_le16(conn->setting);
  345. switch (conn->setting & SCO_AIRMODE_MASK) {
  346. case SCO_AIRMODE_TRANSP:
  347. if (!find_next_esco_param(conn, esco_param_msbc,
  348. ARRAY_SIZE(esco_param_msbc)))
  349. return false;
  350. param = &esco_param_msbc[conn->attempt - 1];
  351. break;
  352. case SCO_AIRMODE_CVSD:
  353. if (conn->parent && lmp_esco_capable(conn->parent)) {
  354. if (!find_next_esco_param(conn, esco_param_cvsd,
  355. ARRAY_SIZE(esco_param_cvsd)))
  356. return false;
  357. param = &esco_param_cvsd[conn->attempt - 1];
  358. } else {
  359. if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
  360. return false;
  361. param = &sco_param_cvsd[conn->attempt - 1];
  362. }
  363. break;
  364. default:
  365. return false;
  366. }
  367. cp.retrans_effort = param->retrans_effort;
  368. cp.pkt_type = __cpu_to_le16(param->pkt_type);
  369. cp.max_latency = __cpu_to_le16(param->max_latency);
  370. if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
  371. return false;
  372. return true;
  373. }
  374. bool hci_setup_sync(struct hci_conn *conn, __u16 handle)
  375. {
  376. int result;
  377. struct conn_handle_t *conn_handle;
  378. if (enhanced_sync_conn_capable(conn->hdev)) {
  379. conn_handle = kzalloc_obj(*conn_handle);
  380. if (!conn_handle)
  381. return false;
  382. conn_handle->conn = conn;
  383. conn_handle->handle = handle;
  384. result = hci_cmd_sync_queue(conn->hdev, hci_enhanced_setup_sync,
  385. conn_handle, NULL);
  386. if (result < 0)
  387. kfree(conn_handle);
  388. return result == 0;
  389. }
  390. return hci_setup_sync_conn(conn, handle);
  391. }
  392. u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
  393. u16 to_multiplier)
  394. {
  395. struct hci_dev *hdev = conn->hdev;
  396. struct hci_conn_params *params;
  397. struct hci_cp_le_conn_update cp;
  398. hci_dev_lock(hdev);
  399. params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
  400. if (params) {
  401. params->conn_min_interval = min;
  402. params->conn_max_interval = max;
  403. params->conn_latency = latency;
  404. params->supervision_timeout = to_multiplier;
  405. }
  406. hci_dev_unlock(hdev);
  407. memset(&cp, 0, sizeof(cp));
  408. cp.handle = cpu_to_le16(conn->handle);
  409. cp.conn_interval_min = cpu_to_le16(min);
  410. cp.conn_interval_max = cpu_to_le16(max);
  411. cp.conn_latency = cpu_to_le16(latency);
  412. cp.supervision_timeout = cpu_to_le16(to_multiplier);
  413. cp.min_ce_len = cpu_to_le16(0x0000);
  414. cp.max_ce_len = cpu_to_le16(0x0000);
  415. hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
  416. if (params)
  417. return 0x01;
  418. return 0x00;
  419. }
  420. void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
  421. __u8 ltk[16], __u8 key_size)
  422. {
  423. struct hci_dev *hdev = conn->hdev;
  424. struct hci_cp_le_start_enc cp;
  425. BT_DBG("hcon %p", conn);
  426. memset(&cp, 0, sizeof(cp));
  427. cp.handle = cpu_to_le16(conn->handle);
  428. cp.rand = rand;
  429. cp.ediv = ediv;
  430. memcpy(cp.ltk, ltk, key_size);
  431. hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
  432. }
  433. /* Device _must_ be locked */
  434. void hci_sco_setup(struct hci_conn *conn, __u8 status)
  435. {
  436. struct hci_link *link;
  437. link = list_first_entry_or_null(&conn->link_list, struct hci_link, list);
  438. if (!link || !link->conn)
  439. return;
  440. BT_DBG("hcon %p", conn);
  441. if (!status) {
  442. if (lmp_esco_capable(conn->hdev))
  443. hci_setup_sync(link->conn, conn->handle);
  444. else
  445. hci_add_sco(link->conn, conn->handle);
  446. } else {
  447. hci_connect_cfm(link->conn, status);
  448. hci_conn_del(link->conn);
  449. }
  450. }
  451. static void hci_conn_timeout(struct work_struct *work)
  452. {
  453. struct hci_conn *conn = container_of(work, struct hci_conn,
  454. disc_work.work);
  455. int refcnt = atomic_read(&conn->refcnt);
  456. BT_DBG("hcon %p state %s", conn, state_to_string(conn->state));
  457. WARN_ON(refcnt < 0);
  458. /* FIXME: It was observed that in pairing failed scenario, refcnt
  459. * drops below 0. Probably this is because l2cap_conn_del calls
  460. * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is
  461. * dropped. After that loop hci_chan_del is called which also drops
  462. * conn. For now make sure that ACL is alive if refcnt is higher then 0,
  463. * otherwise drop it.
  464. */
  465. if (refcnt > 0)
  466. return;
  467. hci_abort_conn(conn, hci_proto_disconn_ind(conn));
  468. }
  469. /* Enter sniff mode */
  470. static void hci_conn_idle(struct work_struct *work)
  471. {
  472. struct hci_conn *conn = container_of(work, struct hci_conn,
  473. idle_work.work);
  474. struct hci_dev *hdev = conn->hdev;
  475. BT_DBG("hcon %p mode %d", conn, conn->mode);
  476. if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
  477. return;
  478. if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
  479. return;
  480. if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
  481. struct hci_cp_sniff_subrate cp;
  482. cp.handle = cpu_to_le16(conn->handle);
  483. cp.max_latency = cpu_to_le16(0);
  484. cp.min_remote_timeout = cpu_to_le16(0);
  485. cp.min_local_timeout = cpu_to_le16(0);
  486. hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
  487. }
  488. if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
  489. struct hci_cp_sniff_mode cp;
  490. cp.handle = cpu_to_le16(conn->handle);
  491. cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
  492. cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
  493. cp.attempt = cpu_to_le16(4);
  494. cp.timeout = cpu_to_le16(1);
  495. hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
  496. }
  497. }
  498. static void hci_conn_auto_accept(struct work_struct *work)
  499. {
  500. struct hci_conn *conn = container_of(work, struct hci_conn,
  501. auto_accept_work.work);
  502. hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
  503. &conn->dst);
  504. }
  505. static void le_disable_advertising(struct hci_dev *hdev)
  506. {
  507. if (ext_adv_capable(hdev)) {
  508. struct hci_cp_le_set_ext_adv_enable cp;
  509. cp.enable = 0x00;
  510. cp.num_of_sets = 0x00;
  511. hci_send_cmd(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, sizeof(cp),
  512. &cp);
  513. } else {
  514. u8 enable = 0x00;
  515. hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
  516. &enable);
  517. }
  518. }
  519. static void le_conn_timeout(struct work_struct *work)
  520. {
  521. struct hci_conn *conn = container_of(work, struct hci_conn,
  522. le_conn_timeout.work);
  523. struct hci_dev *hdev = conn->hdev;
  524. BT_DBG("");
  525. /* We could end up here due to having done directed advertising,
  526. * so clean up the state if necessary. This should however only
  527. * happen with broken hardware or if low duty cycle was used
  528. * (which doesn't have a timeout of its own).
  529. */
  530. if (conn->role == HCI_ROLE_SLAVE) {
  531. /* Disable LE Advertising */
  532. le_disable_advertising(hdev);
  533. hci_dev_lock(hdev);
  534. hci_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT);
  535. hci_dev_unlock(hdev);
  536. return;
  537. }
  538. hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
  539. }
  540. struct iso_list_data {
  541. union {
  542. u8 cig;
  543. u8 big;
  544. };
  545. union {
  546. u8 cis;
  547. u8 bis;
  548. u16 sync_handle;
  549. };
  550. int count;
  551. bool big_term;
  552. bool pa_sync_term;
  553. bool big_sync_term;
  554. };
  555. static void bis_list(struct hci_conn *conn, void *data)
  556. {
  557. struct iso_list_data *d = data;
  558. /* Skip if not broadcast/ANY address */
  559. if (bacmp(&conn->dst, BDADDR_ANY))
  560. return;
  561. if (d->big != conn->iso_qos.bcast.big || d->bis == BT_ISO_QOS_BIS_UNSET ||
  562. d->bis != conn->iso_qos.bcast.bis)
  563. return;
  564. d->count++;
  565. }
  566. static int terminate_big_sync(struct hci_dev *hdev, void *data)
  567. {
  568. struct iso_list_data *d = data;
  569. bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", d->big, d->bis);
  570. hci_disable_per_advertising_sync(hdev, d->bis);
  571. hci_remove_ext_adv_instance_sync(hdev, d->bis, NULL);
  572. /* Only terminate BIG if it has been created */
  573. if (!d->big_term)
  574. return 0;
  575. return hci_le_terminate_big_sync(hdev, d->big,
  576. HCI_ERROR_LOCAL_HOST_TERM);
  577. }
  578. static void terminate_big_destroy(struct hci_dev *hdev, void *data, int err)
  579. {
  580. kfree(data);
  581. }
  582. static int hci_le_terminate_big(struct hci_dev *hdev, struct hci_conn *conn)
  583. {
  584. struct iso_list_data *d;
  585. int ret;
  586. bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", conn->iso_qos.bcast.big,
  587. conn->iso_qos.bcast.bis);
  588. d = kzalloc_obj(*d);
  589. if (!d)
  590. return -ENOMEM;
  591. d->big = conn->iso_qos.bcast.big;
  592. d->bis = conn->iso_qos.bcast.bis;
  593. d->big_term = test_and_clear_bit(HCI_CONN_BIG_CREATED, &conn->flags);
  594. ret = hci_cmd_sync_queue(hdev, terminate_big_sync, d,
  595. terminate_big_destroy);
  596. if (ret)
  597. kfree(d);
  598. return ret;
  599. }
  600. static int big_terminate_sync(struct hci_dev *hdev, void *data)
  601. {
  602. struct iso_list_data *d = data;
  603. bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", d->big,
  604. d->sync_handle);
  605. if (d->big_sync_term)
  606. hci_le_big_terminate_sync(hdev, d->big);
  607. if (d->pa_sync_term)
  608. return hci_le_pa_terminate_sync(hdev, d->sync_handle);
  609. return 0;
  610. }
  611. static void find_bis(struct hci_conn *conn, void *data)
  612. {
  613. struct iso_list_data *d = data;
  614. /* Ignore if BIG doesn't match */
  615. if (d->big != conn->iso_qos.bcast.big)
  616. return;
  617. d->count++;
  618. }
  619. static int hci_le_big_terminate(struct hci_dev *hdev, struct hci_conn *conn)
  620. {
  621. struct iso_list_data *d;
  622. int ret;
  623. bt_dev_dbg(hdev, "hcon %p big 0x%2.2x sync_handle 0x%4.4x", conn,
  624. conn->iso_qos.bcast.big, conn->sync_handle);
  625. d = kzalloc_obj(*d);
  626. if (!d)
  627. return -ENOMEM;
  628. d->big = conn->iso_qos.bcast.big;
  629. d->sync_handle = conn->sync_handle;
  630. if (conn->type == PA_LINK &&
  631. test_and_clear_bit(HCI_CONN_PA_SYNC, &conn->flags)) {
  632. hci_conn_hash_list_flag(hdev, find_bis, PA_LINK,
  633. HCI_CONN_PA_SYNC, d);
  634. if (!d->count)
  635. d->pa_sync_term = true;
  636. d->count = 0;
  637. }
  638. if (test_and_clear_bit(HCI_CONN_BIG_SYNC, &conn->flags)) {
  639. hci_conn_hash_list_flag(hdev, find_bis, BIS_LINK,
  640. HCI_CONN_BIG_SYNC, d);
  641. if (!d->count)
  642. d->big_sync_term = true;
  643. }
  644. if (!d->pa_sync_term && !d->big_sync_term)
  645. return 0;
  646. ret = hci_cmd_sync_queue(hdev, big_terminate_sync, d,
  647. terminate_big_destroy);
  648. if (ret)
  649. kfree(d);
  650. return ret;
  651. }
  652. /* Cleanup BIS connection
  653. *
  654. * Detects if there any BIS left connected in a BIG
  655. * broadcaster: Remove advertising instance and terminate BIG.
  656. * broadcaster receiver: Terminate BIG sync and terminate PA sync.
  657. */
  658. static void bis_cleanup(struct hci_conn *conn)
  659. {
  660. struct hci_dev *hdev = conn->hdev;
  661. struct hci_conn *bis;
  662. bt_dev_dbg(hdev, "conn %p", conn);
  663. if (conn->role == HCI_ROLE_MASTER) {
  664. if (!test_and_clear_bit(HCI_CONN_PER_ADV, &conn->flags))
  665. return;
  666. /* Check if ISO connection is a BIS and terminate advertising
  667. * set and BIG if there are no other connections using it.
  668. */
  669. bis = hci_conn_hash_lookup_big_state(hdev,
  670. conn->iso_qos.bcast.big,
  671. BT_CONNECTED,
  672. HCI_ROLE_MASTER);
  673. if (bis)
  674. return;
  675. bis = hci_conn_hash_lookup_big_state(hdev,
  676. conn->iso_qos.bcast.big,
  677. BT_CONNECT,
  678. HCI_ROLE_MASTER);
  679. if (bis)
  680. return;
  681. bis = hci_conn_hash_lookup_big_state(hdev,
  682. conn->iso_qos.bcast.big,
  683. BT_OPEN,
  684. HCI_ROLE_MASTER);
  685. if (bis)
  686. return;
  687. hci_le_terminate_big(hdev, conn);
  688. } else {
  689. hci_le_big_terminate(hdev, conn);
  690. }
  691. }
  692. static int remove_cig_sync(struct hci_dev *hdev, void *data)
  693. {
  694. u8 handle = PTR_UINT(data);
  695. return hci_le_remove_cig_sync(hdev, handle);
  696. }
  697. static int hci_le_remove_cig(struct hci_dev *hdev, u8 handle)
  698. {
  699. bt_dev_dbg(hdev, "handle 0x%2.2x", handle);
  700. return hci_cmd_sync_queue(hdev, remove_cig_sync, UINT_PTR(handle),
  701. NULL);
  702. }
  703. static void find_cis(struct hci_conn *conn, void *data)
  704. {
  705. struct iso_list_data *d = data;
  706. /* Ignore broadcast or if CIG don't match */
  707. if (!bacmp(&conn->dst, BDADDR_ANY) || d->cig != conn->iso_qos.ucast.cig)
  708. return;
  709. d->count++;
  710. }
  711. /* Cleanup CIS connection:
  712. *
  713. * Detects if there any CIS left connected in a CIG and remove it.
  714. */
  715. static void cis_cleanup(struct hci_conn *conn)
  716. {
  717. struct hci_dev *hdev = conn->hdev;
  718. struct iso_list_data d;
  719. if (conn->iso_qos.ucast.cig == BT_ISO_QOS_CIG_UNSET)
  720. return;
  721. memset(&d, 0, sizeof(d));
  722. d.cig = conn->iso_qos.ucast.cig;
  723. /* Check if ISO connection is a CIS and remove CIG if there are
  724. * no other connections using it.
  725. */
  726. hci_conn_hash_list_state(hdev, find_cis, CIS_LINK, BT_BOUND, &d);
  727. hci_conn_hash_list_state(hdev, find_cis, CIS_LINK, BT_CONNECT,
  728. &d);
  729. hci_conn_hash_list_state(hdev, find_cis, CIS_LINK, BT_CONNECTED,
  730. &d);
  731. if (d.count)
  732. return;
  733. hci_le_remove_cig(hdev, conn->iso_qos.ucast.cig);
  734. }
  735. static int hci_conn_hash_alloc_unset(struct hci_dev *hdev)
  736. {
  737. return ida_alloc_range(&hdev->unset_handle_ida, HCI_CONN_HANDLE_MAX + 1,
  738. U16_MAX, GFP_ATOMIC);
  739. }
  740. static struct hci_conn *__hci_conn_add(struct hci_dev *hdev, int type,
  741. bdaddr_t *dst, u8 dst_type,
  742. u8 role, u16 handle)
  743. {
  744. struct hci_conn *conn;
  745. struct smp_irk *irk = NULL;
  746. switch (type) {
  747. case ACL_LINK:
  748. if (!hdev->acl_mtu)
  749. return ERR_PTR(-ECONNREFUSED);
  750. break;
  751. case CIS_LINK:
  752. case BIS_LINK:
  753. case PA_LINK:
  754. if (!hdev->iso_mtu)
  755. return ERR_PTR(-ECONNREFUSED);
  756. irk = hci_get_irk(hdev, dst, dst_type);
  757. break;
  758. case LE_LINK:
  759. if (hdev->le_mtu && hdev->le_mtu < HCI_MIN_LE_MTU)
  760. return ERR_PTR(-ECONNREFUSED);
  761. if (!hdev->le_mtu && hdev->acl_mtu < HCI_MIN_LE_MTU)
  762. return ERR_PTR(-ECONNREFUSED);
  763. irk = hci_get_irk(hdev, dst, dst_type);
  764. break;
  765. case SCO_LINK:
  766. case ESCO_LINK:
  767. if (!hdev->sco_pkts)
  768. /* Controller does not support SCO or eSCO over HCI */
  769. return ERR_PTR(-ECONNREFUSED);
  770. break;
  771. default:
  772. return ERR_PTR(-ECONNREFUSED);
  773. }
  774. bt_dev_dbg(hdev, "dst %pMR handle 0x%4.4x", dst, handle);
  775. conn = kzalloc_obj(*conn);
  776. if (!conn)
  777. return ERR_PTR(-ENOMEM);
  778. /* If and IRK exists use its identity address */
  779. if (!irk) {
  780. bacpy(&conn->dst, dst);
  781. conn->dst_type = dst_type;
  782. } else {
  783. bacpy(&conn->dst, &irk->bdaddr);
  784. conn->dst_type = irk->addr_type;
  785. }
  786. bacpy(&conn->src, &hdev->bdaddr);
  787. conn->handle = handle;
  788. conn->hdev = hdev;
  789. conn->type = type;
  790. conn->role = role;
  791. conn->mode = HCI_CM_ACTIVE;
  792. conn->state = BT_OPEN;
  793. conn->auth_type = HCI_AT_GENERAL_BONDING;
  794. conn->io_capability = hdev->io_capability;
  795. conn->remote_auth = 0xff;
  796. conn->key_type = 0xff;
  797. conn->rssi = HCI_RSSI_INVALID;
  798. conn->tx_power = HCI_TX_POWER_INVALID;
  799. conn->max_tx_power = HCI_TX_POWER_INVALID;
  800. conn->sync_handle = HCI_SYNC_HANDLE_INVALID;
  801. conn->sid = HCI_SID_INVALID;
  802. set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
  803. conn->disc_timeout = HCI_DISCONN_TIMEOUT;
  804. /* Set Default Authenticated payload timeout to 30s */
  805. conn->auth_payload_timeout = DEFAULT_AUTH_PAYLOAD_TIMEOUT;
  806. if (conn->role == HCI_ROLE_MASTER)
  807. conn->out = true;
  808. switch (type) {
  809. case ACL_LINK:
  810. conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
  811. conn->link_policy = hdev->link_policy;
  812. conn->mtu = hdev->acl_mtu;
  813. break;
  814. case LE_LINK:
  815. /* conn->src should reflect the local identity address */
  816. hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
  817. conn->mtu = hdev->le_mtu ? hdev->le_mtu : hdev->acl_mtu;
  818. /* Use the controller supported PHYS as default until the
  819. * remote features are resolved.
  820. */
  821. conn->le_tx_def_phys = hdev->le_tx_def_phys;
  822. conn->le_rx_def_phys = hdev->le_tx_def_phys;
  823. break;
  824. case CIS_LINK:
  825. /* conn->src should reflect the local identity address */
  826. hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
  827. if (conn->role == HCI_ROLE_MASTER)
  828. conn->cleanup = cis_cleanup;
  829. conn->mtu = hdev->iso_mtu;
  830. break;
  831. case PA_LINK:
  832. case BIS_LINK:
  833. /* conn->src should reflect the local identity address */
  834. hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
  835. conn->cleanup = bis_cleanup;
  836. conn->mtu = hdev->iso_mtu;
  837. break;
  838. case SCO_LINK:
  839. if (lmp_esco_capable(hdev))
  840. conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
  841. (hdev->esco_type & EDR_ESCO_MASK);
  842. else
  843. conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
  844. conn->mtu = hdev->sco_mtu;
  845. break;
  846. case ESCO_LINK:
  847. conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK;
  848. conn->mtu = hdev->sco_mtu;
  849. break;
  850. }
  851. skb_queue_head_init(&conn->data_q);
  852. skb_queue_head_init(&conn->tx_q.queue);
  853. INIT_LIST_HEAD(&conn->chan_list);
  854. INIT_LIST_HEAD(&conn->link_list);
  855. INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout);
  856. INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept);
  857. INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle);
  858. INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout);
  859. atomic_set(&conn->refcnt, 0);
  860. hci_dev_hold(hdev);
  861. hci_conn_hash_add(hdev, conn);
  862. /* The SCO and eSCO connections will only be notified when their
  863. * setup has been completed. This is different to ACL links which
  864. * can be notified right away.
  865. */
  866. if (conn->type != SCO_LINK && conn->type != ESCO_LINK) {
  867. if (hdev->notify)
  868. hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
  869. }
  870. hci_conn_init_sysfs(conn);
  871. return conn;
  872. }
  873. struct hci_conn *hci_conn_add_unset(struct hci_dev *hdev, int type,
  874. bdaddr_t *dst, u8 dst_type, u8 role)
  875. {
  876. int handle;
  877. bt_dev_dbg(hdev, "dst %pMR", dst);
  878. handle = hci_conn_hash_alloc_unset(hdev);
  879. if (unlikely(handle < 0))
  880. return ERR_PTR(-ECONNREFUSED);
  881. return __hci_conn_add(hdev, type, dst, dst_type, role, handle);
  882. }
  883. struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
  884. u8 dst_type, u8 role, u16 handle)
  885. {
  886. if (handle > HCI_CONN_HANDLE_MAX)
  887. return ERR_PTR(-EINVAL);
  888. return __hci_conn_add(hdev, type, dst, dst_type, role, handle);
  889. }
  890. static void hci_conn_cleanup_child(struct hci_conn *conn, u8 reason)
  891. {
  892. if (!reason)
  893. reason = HCI_ERROR_REMOTE_USER_TERM;
  894. /* Due to race, SCO/ISO conn might be not established yet at this point,
  895. * and nothing else will clean it up. In other cases it is done via HCI
  896. * events.
  897. */
  898. switch (conn->type) {
  899. case SCO_LINK:
  900. case ESCO_LINK:
  901. if (HCI_CONN_HANDLE_UNSET(conn->handle))
  902. hci_conn_failed(conn, reason);
  903. break;
  904. case CIS_LINK:
  905. case BIS_LINK:
  906. case PA_LINK:
  907. if ((conn->state != BT_CONNECTED &&
  908. !test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) ||
  909. test_bit(HCI_CONN_BIG_CREATED, &conn->flags))
  910. hci_conn_failed(conn, reason);
  911. break;
  912. }
  913. }
  914. static void hci_conn_unlink(struct hci_conn *conn)
  915. {
  916. struct hci_dev *hdev = conn->hdev;
  917. bt_dev_dbg(hdev, "hcon %p", conn);
  918. if (!conn->parent) {
  919. struct hci_link *link, *t;
  920. list_for_each_entry_safe(link, t, &conn->link_list, list) {
  921. struct hci_conn *child = link->conn;
  922. hci_conn_unlink(child);
  923. /* If hdev is down it means
  924. * hci_dev_close_sync/hci_conn_hash_flush is in progress
  925. * and links don't need to be cleanup as all connections
  926. * would be cleanup.
  927. */
  928. if (!test_bit(HCI_UP, &hdev->flags))
  929. continue;
  930. hci_conn_cleanup_child(child, conn->abort_reason);
  931. }
  932. return;
  933. }
  934. if (!conn->link)
  935. return;
  936. list_del_rcu(&conn->link->list);
  937. synchronize_rcu();
  938. hci_conn_drop(conn->parent);
  939. hci_conn_put(conn->parent);
  940. conn->parent = NULL;
  941. kfree(conn->link);
  942. conn->link = NULL;
  943. }
  944. void hci_conn_del(struct hci_conn *conn)
  945. {
  946. struct hci_dev *hdev = conn->hdev;
  947. BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle);
  948. hci_conn_unlink(conn);
  949. disable_delayed_work_sync(&conn->disc_work);
  950. disable_delayed_work_sync(&conn->auto_accept_work);
  951. disable_delayed_work_sync(&conn->idle_work);
  952. /* Remove the connection from the list so unacked logic can detect when
  953. * a certain pool is not being utilized.
  954. */
  955. hci_conn_hash_del(hdev, conn);
  956. /* Handle unacked frames:
  957. *
  958. * - In case there are no connection, or if restoring the buffers
  959. * considered in transist would overflow, restore all buffers to the
  960. * pool.
  961. * - Otherwise restore just the buffers considered in transit for the
  962. * hci_conn
  963. */
  964. switch (conn->type) {
  965. case ACL_LINK:
  966. if (!hci_conn_num(hdev, ACL_LINK) ||
  967. hdev->acl_cnt + conn->sent > hdev->acl_pkts)
  968. hdev->acl_cnt = hdev->acl_pkts;
  969. else
  970. hdev->acl_cnt += conn->sent;
  971. break;
  972. case LE_LINK:
  973. cancel_delayed_work(&conn->le_conn_timeout);
  974. if (hdev->le_pkts) {
  975. if (!hci_conn_num(hdev, LE_LINK) ||
  976. hdev->le_cnt + conn->sent > hdev->le_pkts)
  977. hdev->le_cnt = hdev->le_pkts;
  978. else
  979. hdev->le_cnt += conn->sent;
  980. } else {
  981. if ((!hci_conn_num(hdev, LE_LINK) &&
  982. !hci_conn_num(hdev, ACL_LINK)) ||
  983. hdev->acl_cnt + conn->sent > hdev->acl_pkts)
  984. hdev->acl_cnt = hdev->acl_pkts;
  985. else
  986. hdev->acl_cnt += conn->sent;
  987. }
  988. break;
  989. case CIS_LINK:
  990. case BIS_LINK:
  991. case PA_LINK:
  992. if (!hci_iso_count(hdev) ||
  993. hdev->iso_cnt + conn->sent > hdev->iso_pkts)
  994. hdev->iso_cnt = hdev->iso_pkts;
  995. else
  996. hdev->iso_cnt += conn->sent;
  997. break;
  998. }
  999. skb_queue_purge(&conn->data_q);
  1000. skb_queue_purge(&conn->tx_q.queue);
  1001. /* Remove the connection from the list and cleanup its remaining
  1002. * state. This is a separate function since for some cases like
  1003. * BT_CONNECT_SCAN we *only* want the cleanup part without the
  1004. * rest of hci_conn_del.
  1005. */
  1006. hci_conn_cleanup(conn);
  1007. /* Dequeue callbacks using connection pointer as data */
  1008. hci_cmd_sync_dequeue(hdev, NULL, conn, NULL);
  1009. }
  1010. struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, uint8_t src_type)
  1011. {
  1012. int use_src = bacmp(src, BDADDR_ANY);
  1013. struct hci_dev *hdev = NULL, *d;
  1014. BT_DBG("%pMR -> %pMR", src, dst);
  1015. read_lock(&hci_dev_list_lock);
  1016. list_for_each_entry(d, &hci_dev_list, list) {
  1017. if (!test_bit(HCI_UP, &d->flags) ||
  1018. hci_dev_test_flag(d, HCI_USER_CHANNEL))
  1019. continue;
  1020. /* Simple routing:
  1021. * No source address - find interface with bdaddr != dst
  1022. * Source address - find interface with bdaddr == src
  1023. */
  1024. if (use_src) {
  1025. bdaddr_t id_addr;
  1026. u8 id_addr_type;
  1027. if (src_type == BDADDR_BREDR) {
  1028. if (!lmp_bredr_capable(d))
  1029. continue;
  1030. bacpy(&id_addr, &d->bdaddr);
  1031. id_addr_type = BDADDR_BREDR;
  1032. } else {
  1033. if (!lmp_le_capable(d))
  1034. continue;
  1035. hci_copy_identity_address(d, &id_addr,
  1036. &id_addr_type);
  1037. /* Convert from HCI to three-value type */
  1038. if (id_addr_type == ADDR_LE_DEV_PUBLIC)
  1039. id_addr_type = BDADDR_LE_PUBLIC;
  1040. else
  1041. id_addr_type = BDADDR_LE_RANDOM;
  1042. }
  1043. if (!bacmp(&id_addr, src) && id_addr_type == src_type) {
  1044. hdev = d; break;
  1045. }
  1046. } else {
  1047. if (bacmp(&d->bdaddr, dst)) {
  1048. hdev = d; break;
  1049. }
  1050. }
  1051. }
  1052. if (hdev)
  1053. hdev = hci_dev_hold(hdev);
  1054. read_unlock(&hci_dev_list_lock);
  1055. return hdev;
  1056. }
  1057. EXPORT_SYMBOL(hci_get_route);
  1058. /* This function requires the caller holds hdev->lock */
  1059. static void hci_le_conn_failed(struct hci_conn *conn, u8 status)
  1060. {
  1061. struct hci_dev *hdev = conn->hdev;
  1062. hci_connect_le_scan_cleanup(conn, status);
  1063. /* Enable advertising in case this was a failed connection
  1064. * attempt as a peripheral.
  1065. */
  1066. hci_enable_advertising(hdev);
  1067. }
  1068. /* This function requires the caller holds hdev->lock */
  1069. void hci_conn_failed(struct hci_conn *conn, u8 status)
  1070. {
  1071. struct hci_dev *hdev = conn->hdev;
  1072. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  1073. switch (conn->type) {
  1074. case LE_LINK:
  1075. hci_le_conn_failed(conn, status);
  1076. break;
  1077. case ACL_LINK:
  1078. mgmt_connect_failed(hdev, conn, status);
  1079. break;
  1080. }
  1081. /* In case of BIG/PA sync failed, clear conn flags so that
  1082. * the conns will be correctly cleaned up by ISO layer
  1083. */
  1084. test_and_clear_bit(HCI_CONN_BIG_SYNC_FAILED, &conn->flags);
  1085. test_and_clear_bit(HCI_CONN_PA_SYNC_FAILED, &conn->flags);
  1086. conn->state = BT_CLOSED;
  1087. hci_connect_cfm(conn, status);
  1088. hci_conn_del(conn);
  1089. }
  1090. /* This function requires the caller holds hdev->lock */
  1091. u8 hci_conn_set_handle(struct hci_conn *conn, u16 handle)
  1092. {
  1093. struct hci_dev *hdev = conn->hdev;
  1094. bt_dev_dbg(hdev, "hcon %p handle 0x%4.4x", conn, handle);
  1095. if (conn->handle == handle)
  1096. return 0;
  1097. if (handle > HCI_CONN_HANDLE_MAX) {
  1098. bt_dev_err(hdev, "Invalid handle: 0x%4.4x > 0x%4.4x",
  1099. handle, HCI_CONN_HANDLE_MAX);
  1100. return HCI_ERROR_INVALID_PARAMETERS;
  1101. }
  1102. /* If abort_reason has been sent it means the connection is being
  1103. * aborted and the handle shall not be changed.
  1104. */
  1105. if (conn->abort_reason)
  1106. return conn->abort_reason;
  1107. if (HCI_CONN_HANDLE_UNSET(conn->handle))
  1108. ida_free(&hdev->unset_handle_ida, conn->handle);
  1109. conn->handle = handle;
  1110. return 0;
  1111. }
  1112. struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
  1113. u8 dst_type, bool dst_resolved, u8 sec_level,
  1114. u16 conn_timeout, u8 role, u8 phy, u8 sec_phy)
  1115. {
  1116. struct hci_conn *conn;
  1117. struct smp_irk *irk;
  1118. int err;
  1119. /* Let's make sure that le is enabled.*/
  1120. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
  1121. if (lmp_le_capable(hdev))
  1122. return ERR_PTR(-ECONNREFUSED);
  1123. return ERR_PTR(-EOPNOTSUPP);
  1124. }
  1125. /* Since the controller supports only one LE connection attempt at a
  1126. * time, we return -EBUSY if there is any connection attempt running.
  1127. */
  1128. if (hci_lookup_le_connect(hdev))
  1129. return ERR_PTR(-EBUSY);
  1130. /* If there's already a connection object but it's not in
  1131. * scanning state it means it must already be established, in
  1132. * which case we can't do anything else except report a failure
  1133. * to connect.
  1134. */
  1135. conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
  1136. if (conn && !test_bit(HCI_CONN_SCANNING, &conn->flags)) {
  1137. return ERR_PTR(-EBUSY);
  1138. }
  1139. /* Check if the destination address has been resolved by the controller
  1140. * since if it did then the identity address shall be used.
  1141. */
  1142. if (!dst_resolved) {
  1143. /* When given an identity address with existing identity
  1144. * resolving key, the connection needs to be established
  1145. * to a resolvable random address.
  1146. *
  1147. * Storing the resolvable random address is required here
  1148. * to handle connection failures. The address will later
  1149. * be resolved back into the original identity address
  1150. * from the connect request.
  1151. */
  1152. irk = hci_find_irk_by_addr(hdev, dst, dst_type);
  1153. if (irk && bacmp(&irk->rpa, BDADDR_ANY)) {
  1154. dst = &irk->rpa;
  1155. dst_type = ADDR_LE_DEV_RANDOM;
  1156. }
  1157. }
  1158. if (conn) {
  1159. bacpy(&conn->dst, dst);
  1160. } else {
  1161. conn = hci_conn_add_unset(hdev, LE_LINK, dst, dst_type, role);
  1162. if (IS_ERR(conn))
  1163. return conn;
  1164. hci_conn_hold(conn);
  1165. conn->pending_sec_level = sec_level;
  1166. }
  1167. conn->sec_level = BT_SECURITY_LOW;
  1168. conn->conn_timeout = conn_timeout;
  1169. conn->le_adv_phy = phy;
  1170. conn->le_adv_sec_phy = sec_phy;
  1171. err = hci_connect_le_sync(hdev, conn);
  1172. if (err) {
  1173. hci_conn_del(conn);
  1174. return ERR_PTR(err);
  1175. }
  1176. return conn;
  1177. }
  1178. static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
  1179. {
  1180. struct hci_conn *conn;
  1181. conn = hci_conn_hash_lookup_le(hdev, addr, type);
  1182. if (!conn)
  1183. return false;
  1184. if (conn->state != BT_CONNECTED)
  1185. return false;
  1186. return true;
  1187. }
  1188. /* This function requires the caller holds hdev->lock */
  1189. static int hci_explicit_conn_params_set(struct hci_dev *hdev,
  1190. bdaddr_t *addr, u8 addr_type)
  1191. {
  1192. struct hci_conn_params *params;
  1193. if (is_connected(hdev, addr, addr_type))
  1194. return -EISCONN;
  1195. params = hci_conn_params_lookup(hdev, addr, addr_type);
  1196. if (!params) {
  1197. params = hci_conn_params_add(hdev, addr, addr_type);
  1198. if (!params)
  1199. return -ENOMEM;
  1200. /* If we created new params, mark them to be deleted in
  1201. * hci_connect_le_scan_cleanup. It's different case than
  1202. * existing disabled params, those will stay after cleanup.
  1203. */
  1204. params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
  1205. }
  1206. /* We're trying to connect, so make sure params are at pend_le_conns */
  1207. if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
  1208. params->auto_connect == HCI_AUTO_CONN_REPORT ||
  1209. params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
  1210. hci_pend_le_list_del_init(params);
  1211. hci_pend_le_list_add(params, &hdev->pend_le_conns);
  1212. }
  1213. params->explicit_connect = true;
  1214. BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
  1215. params->auto_connect);
  1216. return 0;
  1217. }
  1218. static int qos_set_big(struct hci_dev *hdev, struct bt_iso_qos *qos)
  1219. {
  1220. struct hci_conn *conn;
  1221. u8 big;
  1222. /* Allocate a BIG if not set */
  1223. if (qos->bcast.big == BT_ISO_QOS_BIG_UNSET) {
  1224. for (big = 0x00; big < 0xef; big++) {
  1225. conn = hci_conn_hash_lookup_big(hdev, big);
  1226. if (!conn)
  1227. break;
  1228. }
  1229. if (big == 0xef)
  1230. return -EADDRNOTAVAIL;
  1231. /* Update BIG */
  1232. qos->bcast.big = big;
  1233. }
  1234. return 0;
  1235. }
  1236. static int qos_set_bis(struct hci_dev *hdev, struct bt_iso_qos *qos)
  1237. {
  1238. struct hci_conn *conn;
  1239. u8 bis;
  1240. /* Allocate BIS if not set */
  1241. if (qos->bcast.bis == BT_ISO_QOS_BIS_UNSET) {
  1242. if (qos->bcast.big != BT_ISO_QOS_BIG_UNSET) {
  1243. conn = hci_conn_hash_lookup_big(hdev, qos->bcast.big);
  1244. if (conn) {
  1245. /* If the BIG handle is already matched to an advertising
  1246. * handle, do not allocate a new one.
  1247. */
  1248. qos->bcast.bis = conn->iso_qos.bcast.bis;
  1249. return 0;
  1250. }
  1251. }
  1252. /* Find an unused adv set to advertise BIS, skip instance 0x00
  1253. * since it is reserved as general purpose set.
  1254. */
  1255. for (bis = 0x01; bis < hdev->le_num_of_adv_sets;
  1256. bis++) {
  1257. conn = hci_conn_hash_lookup_bis(hdev, BDADDR_ANY, bis);
  1258. if (!conn)
  1259. break;
  1260. }
  1261. if (bis == hdev->le_num_of_adv_sets)
  1262. return -EADDRNOTAVAIL;
  1263. /* Update BIS */
  1264. qos->bcast.bis = bis;
  1265. }
  1266. return 0;
  1267. }
  1268. /* This function requires the caller holds hdev->lock */
  1269. static struct hci_conn *hci_add_bis(struct hci_dev *hdev, bdaddr_t *dst,
  1270. __u8 sid, struct bt_iso_qos *qos,
  1271. __u8 base_len, __u8 *base, u16 timeout)
  1272. {
  1273. struct hci_conn *conn;
  1274. int err;
  1275. /* Let's make sure that le is enabled.*/
  1276. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
  1277. if (lmp_le_capable(hdev))
  1278. return ERR_PTR(-ECONNREFUSED);
  1279. return ERR_PTR(-EOPNOTSUPP);
  1280. }
  1281. err = qos_set_big(hdev, qos);
  1282. if (err)
  1283. return ERR_PTR(err);
  1284. err = qos_set_bis(hdev, qos);
  1285. if (err)
  1286. return ERR_PTR(err);
  1287. /* Check if the LE Create BIG command has already been sent */
  1288. conn = hci_conn_hash_lookup_per_adv_bis(hdev, dst, qos->bcast.big,
  1289. qos->bcast.big);
  1290. if (conn)
  1291. return ERR_PTR(-EADDRINUSE);
  1292. /* Check BIS settings against other bound BISes, since all
  1293. * BISes in a BIG must have the same value for all parameters
  1294. */
  1295. conn = hci_conn_hash_lookup_big(hdev, qos->bcast.big);
  1296. if (conn && (memcmp(qos, &conn->iso_qos, sizeof(*qos)) ||
  1297. base_len != conn->le_per_adv_data_len ||
  1298. memcmp(conn->le_per_adv_data, base, base_len)))
  1299. return ERR_PTR(-EADDRINUSE);
  1300. conn = hci_conn_add_unset(hdev, BIS_LINK, dst, 0, HCI_ROLE_MASTER);
  1301. if (IS_ERR(conn))
  1302. return conn;
  1303. conn->state = BT_CONNECT;
  1304. conn->sid = sid;
  1305. conn->conn_timeout = timeout;
  1306. hci_conn_hold(conn);
  1307. return conn;
  1308. }
  1309. /* This function requires the caller holds hdev->lock */
  1310. struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
  1311. u8 dst_type, u8 sec_level,
  1312. u16 conn_timeout,
  1313. enum conn_reasons conn_reason)
  1314. {
  1315. struct hci_conn *conn;
  1316. /* Let's make sure that le is enabled.*/
  1317. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
  1318. if (lmp_le_capable(hdev))
  1319. return ERR_PTR(-ECONNREFUSED);
  1320. return ERR_PTR(-EOPNOTSUPP);
  1321. }
  1322. /* Some devices send ATT messages as soon as the physical link is
  1323. * established. To be able to handle these ATT messages, the user-
  1324. * space first establishes the connection and then starts the pairing
  1325. * process.
  1326. *
  1327. * So if a hci_conn object already exists for the following connection
  1328. * attempt, we simply update pending_sec_level and auth_type fields
  1329. * and return the object found.
  1330. */
  1331. conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
  1332. if (conn) {
  1333. if (conn->pending_sec_level < sec_level)
  1334. conn->pending_sec_level = sec_level;
  1335. goto done;
  1336. }
  1337. BT_DBG("requesting refresh of dst_addr");
  1338. conn = hci_conn_add_unset(hdev, LE_LINK, dst, dst_type,
  1339. HCI_ROLE_MASTER);
  1340. if (IS_ERR(conn))
  1341. return conn;
  1342. if (hci_explicit_conn_params_set(hdev, dst, dst_type) < 0) {
  1343. hci_conn_del(conn);
  1344. return ERR_PTR(-EBUSY);
  1345. }
  1346. conn->state = BT_CONNECT;
  1347. set_bit(HCI_CONN_SCANNING, &conn->flags);
  1348. conn->sec_level = BT_SECURITY_LOW;
  1349. conn->pending_sec_level = sec_level;
  1350. conn->conn_timeout = conn_timeout;
  1351. conn->conn_reason = conn_reason;
  1352. hci_update_passive_scan(hdev);
  1353. done:
  1354. hci_conn_hold(conn);
  1355. return conn;
  1356. }
  1357. struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
  1358. u8 sec_level, u8 auth_type,
  1359. enum conn_reasons conn_reason, u16 timeout)
  1360. {
  1361. struct hci_conn *acl;
  1362. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
  1363. if (lmp_bredr_capable(hdev))
  1364. return ERR_PTR(-ECONNREFUSED);
  1365. return ERR_PTR(-EOPNOTSUPP);
  1366. }
  1367. /* Reject outgoing connection to device with same BD ADDR against
  1368. * CVE-2020-26555
  1369. */
  1370. if (!bacmp(&hdev->bdaddr, dst)) {
  1371. bt_dev_dbg(hdev, "Reject connection with same BD_ADDR %pMR\n",
  1372. dst);
  1373. return ERR_PTR(-ECONNREFUSED);
  1374. }
  1375. acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
  1376. if (!acl) {
  1377. acl = hci_conn_add_unset(hdev, ACL_LINK, dst, 0,
  1378. HCI_ROLE_MASTER);
  1379. if (IS_ERR(acl))
  1380. return acl;
  1381. }
  1382. hci_conn_hold(acl);
  1383. acl->conn_reason = conn_reason;
  1384. if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
  1385. int err;
  1386. acl->sec_level = BT_SECURITY_LOW;
  1387. acl->pending_sec_level = sec_level;
  1388. acl->auth_type = auth_type;
  1389. acl->conn_timeout = timeout;
  1390. err = hci_connect_acl_sync(hdev, acl);
  1391. if (err) {
  1392. hci_conn_del(acl);
  1393. return ERR_PTR(err);
  1394. }
  1395. }
  1396. return acl;
  1397. }
  1398. static struct hci_link *hci_conn_link(struct hci_conn *parent,
  1399. struct hci_conn *conn)
  1400. {
  1401. struct hci_dev *hdev = parent->hdev;
  1402. struct hci_link *link;
  1403. bt_dev_dbg(hdev, "parent %p hcon %p", parent, conn);
  1404. if (conn->link)
  1405. return conn->link;
  1406. if (conn->parent)
  1407. return NULL;
  1408. link = kzalloc_obj(*link);
  1409. if (!link)
  1410. return NULL;
  1411. link->conn = hci_conn_hold(conn);
  1412. conn->link = link;
  1413. conn->parent = hci_conn_get(parent);
  1414. /* Use list_add_tail_rcu append to the list */
  1415. list_add_tail_rcu(&link->list, &parent->link_list);
  1416. return link;
  1417. }
  1418. struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
  1419. __u16 setting, struct bt_codec *codec,
  1420. u16 timeout)
  1421. {
  1422. struct hci_conn *acl;
  1423. struct hci_conn *sco;
  1424. struct hci_link *link;
  1425. acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING,
  1426. CONN_REASON_SCO_CONNECT, timeout);
  1427. if (IS_ERR(acl))
  1428. return acl;
  1429. sco = hci_conn_hash_lookup_ba(hdev, type, dst);
  1430. if (!sco) {
  1431. sco = hci_conn_add_unset(hdev, type, dst, 0, HCI_ROLE_MASTER);
  1432. if (IS_ERR(sco)) {
  1433. hci_conn_drop(acl);
  1434. return sco;
  1435. }
  1436. }
  1437. link = hci_conn_link(acl, sco);
  1438. if (!link) {
  1439. hci_conn_drop(acl);
  1440. hci_conn_drop(sco);
  1441. return ERR_PTR(-ENOLINK);
  1442. }
  1443. sco->setting = setting;
  1444. sco->codec = *codec;
  1445. if (acl->state == BT_CONNECTED &&
  1446. (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
  1447. set_bit(HCI_CONN_POWER_SAVE, &acl->flags);
  1448. hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON);
  1449. if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) {
  1450. /* defer SCO setup until mode change completed */
  1451. set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags);
  1452. return sco;
  1453. }
  1454. hci_sco_setup(acl, 0x00);
  1455. }
  1456. return sco;
  1457. }
  1458. static int hci_le_create_big(struct hci_conn *conn, struct bt_iso_qos *qos)
  1459. {
  1460. struct hci_dev *hdev = conn->hdev;
  1461. struct hci_cp_le_create_big cp;
  1462. struct iso_list_data data;
  1463. memset(&cp, 0, sizeof(cp));
  1464. data.big = qos->bcast.big;
  1465. data.bis = qos->bcast.bis;
  1466. data.count = 0;
  1467. /* Create a BIS for each bound connection */
  1468. hci_conn_hash_list_state(hdev, bis_list, BIS_LINK,
  1469. BT_BOUND, &data);
  1470. cp.handle = qos->bcast.big;
  1471. cp.adv_handle = qos->bcast.bis;
  1472. cp.num_bis = data.count;
  1473. hci_cpu_to_le24(qos->bcast.out.interval, cp.bis.sdu_interval);
  1474. cp.bis.sdu = cpu_to_le16(qos->bcast.out.sdu);
  1475. cp.bis.latency = cpu_to_le16(qos->bcast.out.latency);
  1476. cp.bis.rtn = qos->bcast.out.rtn;
  1477. cp.bis.phy = qos->bcast.out.phys;
  1478. cp.bis.packing = qos->bcast.packing;
  1479. cp.bis.framing = qos->bcast.framing;
  1480. cp.bis.encryption = qos->bcast.encryption;
  1481. memcpy(cp.bis.bcode, qos->bcast.bcode, sizeof(cp.bis.bcode));
  1482. return hci_send_cmd(hdev, HCI_OP_LE_CREATE_BIG, sizeof(cp), &cp);
  1483. }
  1484. static int set_cig_params_sync(struct hci_dev *hdev, void *data)
  1485. {
  1486. DEFINE_FLEX(struct hci_cp_le_set_cig_params, pdu, cis, num_cis, 0x1f);
  1487. u8 cig_id = PTR_UINT(data);
  1488. struct hci_conn *conn;
  1489. struct bt_iso_qos *qos;
  1490. u8 aux_num_cis = 0;
  1491. u8 cis_id;
  1492. hci_dev_lock(hdev);
  1493. conn = hci_conn_hash_lookup_cig(hdev, cig_id);
  1494. if (!conn) {
  1495. hci_dev_unlock(hdev);
  1496. return 0;
  1497. }
  1498. qos = &conn->iso_qos;
  1499. pdu->cig_id = cig_id;
  1500. hci_cpu_to_le24(qos->ucast.out.interval, pdu->c_interval);
  1501. hci_cpu_to_le24(qos->ucast.in.interval, pdu->p_interval);
  1502. pdu->sca = qos->ucast.sca;
  1503. pdu->packing = qos->ucast.packing;
  1504. pdu->framing = qos->ucast.framing;
  1505. pdu->c_latency = cpu_to_le16(qos->ucast.out.latency);
  1506. pdu->p_latency = cpu_to_le16(qos->ucast.in.latency);
  1507. /* Reprogram all CIS(s) with the same CIG, valid range are:
  1508. * num_cis: 0x00 to 0x1F
  1509. * cis_id: 0x00 to 0xEF
  1510. */
  1511. for (cis_id = 0x00; cis_id < 0xf0 &&
  1512. aux_num_cis < pdu->num_cis; cis_id++) {
  1513. struct hci_cis_params *cis;
  1514. conn = hci_conn_hash_lookup_cis(hdev, NULL, 0, cig_id, cis_id);
  1515. if (!conn)
  1516. continue;
  1517. qos = &conn->iso_qos;
  1518. cis = &pdu->cis[aux_num_cis++];
  1519. cis->cis_id = cis_id;
  1520. cis->c_sdu = cpu_to_le16(conn->iso_qos.ucast.out.sdu);
  1521. cis->p_sdu = cpu_to_le16(conn->iso_qos.ucast.in.sdu);
  1522. cis->c_phys = qos->ucast.out.phys ? qos->ucast.out.phys :
  1523. qos->ucast.in.phys;
  1524. cis->p_phys = qos->ucast.in.phys ? qos->ucast.in.phys :
  1525. qos->ucast.out.phys;
  1526. cis->c_rtn = qos->ucast.out.rtn;
  1527. cis->p_rtn = qos->ucast.in.rtn;
  1528. }
  1529. pdu->num_cis = aux_num_cis;
  1530. hci_dev_unlock(hdev);
  1531. if (!pdu->num_cis)
  1532. return 0;
  1533. return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_CIG_PARAMS,
  1534. struct_size(pdu, cis, pdu->num_cis),
  1535. pdu, HCI_CMD_TIMEOUT);
  1536. }
  1537. static bool hci_le_set_cig_params(struct hci_conn *conn, struct bt_iso_qos *qos)
  1538. {
  1539. struct hci_dev *hdev = conn->hdev;
  1540. struct iso_list_data data;
  1541. memset(&data, 0, sizeof(data));
  1542. /* Allocate first still reconfigurable CIG if not set */
  1543. if (qos->ucast.cig == BT_ISO_QOS_CIG_UNSET) {
  1544. for (data.cig = 0x00; data.cig < 0xf0; data.cig++) {
  1545. data.count = 0;
  1546. hci_conn_hash_list_state(hdev, find_cis, CIS_LINK,
  1547. BT_CONNECT, &data);
  1548. if (data.count)
  1549. continue;
  1550. hci_conn_hash_list_state(hdev, find_cis, CIS_LINK,
  1551. BT_CONNECTED, &data);
  1552. if (!data.count)
  1553. break;
  1554. }
  1555. if (data.cig == 0xf0)
  1556. return false;
  1557. /* Update CIG */
  1558. qos->ucast.cig = data.cig;
  1559. }
  1560. if (qos->ucast.cis != BT_ISO_QOS_CIS_UNSET) {
  1561. if (hci_conn_hash_lookup_cis(hdev, NULL, 0, qos->ucast.cig,
  1562. qos->ucast.cis))
  1563. return false;
  1564. goto done;
  1565. }
  1566. /* Allocate first available CIS if not set */
  1567. for (data.cig = qos->ucast.cig, data.cis = 0x00; data.cis < 0xf0;
  1568. data.cis++) {
  1569. if (!hci_conn_hash_lookup_cis(hdev, NULL, 0, data.cig,
  1570. data.cis)) {
  1571. /* Update CIS */
  1572. qos->ucast.cis = data.cis;
  1573. break;
  1574. }
  1575. }
  1576. if (qos->ucast.cis == BT_ISO_QOS_CIS_UNSET)
  1577. return false;
  1578. done:
  1579. conn->iso_qos = *qos;
  1580. if (hci_cmd_sync_queue(hdev, set_cig_params_sync,
  1581. UINT_PTR(qos->ucast.cig), NULL) < 0)
  1582. return false;
  1583. return true;
  1584. }
  1585. struct hci_conn *hci_bind_cis(struct hci_dev *hdev, bdaddr_t *dst,
  1586. __u8 dst_type, struct bt_iso_qos *qos,
  1587. u16 timeout)
  1588. {
  1589. struct hci_conn *cis;
  1590. cis = hci_conn_hash_lookup_cis(hdev, dst, dst_type, qos->ucast.cig,
  1591. qos->ucast.cis);
  1592. if (!cis) {
  1593. cis = hci_conn_add_unset(hdev, CIS_LINK, dst, dst_type,
  1594. HCI_ROLE_MASTER);
  1595. if (IS_ERR(cis))
  1596. return cis;
  1597. cis->cleanup = cis_cleanup;
  1598. cis->dst_type = dst_type;
  1599. cis->iso_qos.ucast.cig = BT_ISO_QOS_CIG_UNSET;
  1600. cis->iso_qos.ucast.cis = BT_ISO_QOS_CIS_UNSET;
  1601. cis->conn_timeout = timeout;
  1602. }
  1603. if (cis->state == BT_CONNECTED)
  1604. return cis;
  1605. /* Check if CIS has been set and the settings matches */
  1606. if (cis->state == BT_BOUND &&
  1607. !memcmp(&cis->iso_qos, qos, sizeof(*qos)))
  1608. return cis;
  1609. /* Update LINK PHYs according to QoS preference */
  1610. cis->le_tx_phy = qos->ucast.out.phys;
  1611. cis->le_rx_phy = qos->ucast.in.phys;
  1612. /* If output interval is not set use the input interval as it cannot be
  1613. * 0x000000.
  1614. */
  1615. if (!qos->ucast.out.interval)
  1616. qos->ucast.out.interval = qos->ucast.in.interval;
  1617. /* If input interval is not set use the output interval as it cannot be
  1618. * 0x000000.
  1619. */
  1620. if (!qos->ucast.in.interval)
  1621. qos->ucast.in.interval = qos->ucast.out.interval;
  1622. /* If output latency is not set use the input latency as it cannot be
  1623. * 0x0000.
  1624. */
  1625. if (!qos->ucast.out.latency)
  1626. qos->ucast.out.latency = qos->ucast.in.latency;
  1627. /* If input latency is not set use the output latency as it cannot be
  1628. * 0x0000.
  1629. */
  1630. if (!qos->ucast.in.latency)
  1631. qos->ucast.in.latency = qos->ucast.out.latency;
  1632. if (!hci_le_set_cig_params(cis, qos)) {
  1633. hci_conn_drop(cis);
  1634. return ERR_PTR(-EINVAL);
  1635. }
  1636. hci_conn_hold(cis);
  1637. cis->state = BT_BOUND;
  1638. return cis;
  1639. }
  1640. bool hci_iso_setup_path(struct hci_conn *conn)
  1641. {
  1642. struct hci_dev *hdev = conn->hdev;
  1643. struct hci_cp_le_setup_iso_path cmd;
  1644. memset(&cmd, 0, sizeof(cmd));
  1645. if (conn->iso_qos.ucast.out.sdu) {
  1646. cmd.handle = cpu_to_le16(conn->handle);
  1647. cmd.direction = 0x00; /* Input (Host to Controller) */
  1648. cmd.path = 0x00; /* HCI path if enabled */
  1649. cmd.codec = 0x03; /* Transparent Data */
  1650. if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd),
  1651. &cmd) < 0)
  1652. return false;
  1653. }
  1654. if (conn->iso_qos.ucast.in.sdu) {
  1655. cmd.handle = cpu_to_le16(conn->handle);
  1656. cmd.direction = 0x01; /* Output (Controller to Host) */
  1657. cmd.path = 0x00; /* HCI path if enabled */
  1658. cmd.codec = 0x03; /* Transparent Data */
  1659. if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd),
  1660. &cmd) < 0)
  1661. return false;
  1662. }
  1663. return true;
  1664. }
  1665. int hci_conn_check_create_cis(struct hci_conn *conn)
  1666. {
  1667. if (conn->type != CIS_LINK)
  1668. return -EINVAL;
  1669. if (!conn->parent || conn->parent->state != BT_CONNECTED ||
  1670. conn->state != BT_CONNECT || HCI_CONN_HANDLE_UNSET(conn->handle))
  1671. return 1;
  1672. return 0;
  1673. }
  1674. static int hci_create_cis_sync(struct hci_dev *hdev, void *data)
  1675. {
  1676. return hci_le_create_cis_sync(hdev);
  1677. }
  1678. int hci_le_create_cis_pending(struct hci_dev *hdev)
  1679. {
  1680. struct hci_conn *conn;
  1681. bool pending = false;
  1682. rcu_read_lock();
  1683. list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
  1684. if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) {
  1685. rcu_read_unlock();
  1686. return -EBUSY;
  1687. }
  1688. if (!hci_conn_check_create_cis(conn))
  1689. pending = true;
  1690. }
  1691. rcu_read_unlock();
  1692. if (!pending)
  1693. return 0;
  1694. /* Queue Create CIS */
  1695. return hci_cmd_sync_queue(hdev, hci_create_cis_sync, NULL, NULL);
  1696. }
  1697. static void hci_iso_qos_setup(struct hci_dev *hdev, struct hci_conn *conn,
  1698. struct bt_iso_io_qos *qos, __u8 phys)
  1699. {
  1700. /* Only set MTU if PHY is enabled */
  1701. if (!qos->sdu && qos->phys)
  1702. qos->sdu = conn->mtu;
  1703. /* Use the same PHY as ACL if set to any */
  1704. if (qos->phys == BT_ISO_PHY_ANY)
  1705. qos->phys = phys;
  1706. /* Use LE ACL connection interval if not set */
  1707. if (!qos->interval)
  1708. /* ACL interval unit in 1.25 ms to us */
  1709. qos->interval = conn->le_conn_interval * 1250;
  1710. /* Use LE ACL connection latency if not set */
  1711. if (!qos->latency)
  1712. qos->latency = conn->le_conn_latency;
  1713. }
  1714. static int create_big_sync(struct hci_dev *hdev, void *data)
  1715. {
  1716. struct hci_conn *conn = data;
  1717. struct bt_iso_qos *qos = &conn->iso_qos;
  1718. u16 interval, sync_interval = 0;
  1719. u32 flags = 0;
  1720. int err;
  1721. if (qos->bcast.out.phys == BIT(1))
  1722. flags |= MGMT_ADV_FLAG_SEC_2M;
  1723. /* Align intervals */
  1724. interval = (qos->bcast.out.interval / 1250) * qos->bcast.sync_factor;
  1725. if (qos->bcast.bis)
  1726. sync_interval = interval * 4;
  1727. err = hci_start_per_adv_sync(hdev, qos->bcast.bis, conn->sid,
  1728. conn->le_per_adv_data_len,
  1729. conn->le_per_adv_data, flags, interval,
  1730. interval, sync_interval);
  1731. if (err)
  1732. return err;
  1733. return hci_le_create_big(conn, &conn->iso_qos);
  1734. }
  1735. struct hci_conn *hci_pa_create_sync(struct hci_dev *hdev, bdaddr_t *dst,
  1736. __u8 dst_type, __u8 sid,
  1737. struct bt_iso_qos *qos)
  1738. {
  1739. struct hci_conn *conn;
  1740. bt_dev_dbg(hdev, "dst %pMR type %d sid %d", dst, dst_type, sid);
  1741. conn = hci_conn_add_unset(hdev, PA_LINK, dst, dst_type, HCI_ROLE_SLAVE);
  1742. if (IS_ERR(conn))
  1743. return conn;
  1744. conn->iso_qos = *qos;
  1745. conn->sid = sid;
  1746. conn->state = BT_LISTEN;
  1747. conn->conn_timeout = msecs_to_jiffies(qos->bcast.sync_timeout * 10);
  1748. hci_conn_hold(conn);
  1749. hci_connect_pa_sync(hdev, conn);
  1750. return conn;
  1751. }
  1752. int hci_conn_big_create_sync(struct hci_dev *hdev, struct hci_conn *hcon,
  1753. struct bt_iso_qos *qos, __u16 sync_handle,
  1754. __u8 num_bis, __u8 bis[])
  1755. {
  1756. int err;
  1757. if (num_bis < 0x01 || num_bis > ISO_MAX_NUM_BIS)
  1758. return -EINVAL;
  1759. err = qos_set_big(hdev, qos);
  1760. if (err)
  1761. return err;
  1762. if (hcon) {
  1763. /* Update hcon QoS */
  1764. hcon->iso_qos = *qos;
  1765. hcon->num_bis = num_bis;
  1766. memcpy(hcon->bis, bis, num_bis);
  1767. hcon->conn_timeout = msecs_to_jiffies(qos->bcast.timeout * 10);
  1768. }
  1769. return hci_connect_big_sync(hdev, hcon);
  1770. }
  1771. static void create_big_complete(struct hci_dev *hdev, void *data, int err)
  1772. {
  1773. struct hci_conn *conn = data;
  1774. bt_dev_dbg(hdev, "conn %p", conn);
  1775. if (err) {
  1776. bt_dev_err(hdev, "Unable to create BIG: %d", err);
  1777. hci_connect_cfm(conn, err);
  1778. hci_conn_del(conn);
  1779. }
  1780. }
  1781. struct hci_conn *hci_bind_bis(struct hci_dev *hdev, bdaddr_t *dst, __u8 sid,
  1782. struct bt_iso_qos *qos,
  1783. __u8 base_len, __u8 *base, u16 timeout)
  1784. {
  1785. struct hci_conn *conn;
  1786. struct hci_conn *parent;
  1787. __u8 eir[HCI_MAX_PER_AD_LENGTH];
  1788. struct hci_link *link;
  1789. /* Look for any BIS that is open for rebinding */
  1790. conn = hci_conn_hash_lookup_big_state(hdev, qos->bcast.big, BT_OPEN,
  1791. HCI_ROLE_MASTER);
  1792. if (conn) {
  1793. memcpy(qos, &conn->iso_qos, sizeof(*qos));
  1794. conn->state = BT_CONNECTED;
  1795. return conn;
  1796. }
  1797. if (base_len && base)
  1798. base_len = eir_append_service_data(eir, 0, 0x1851,
  1799. base, base_len);
  1800. /* We need hci_conn object using the BDADDR_ANY as dst */
  1801. conn = hci_add_bis(hdev, dst, sid, qos, base_len, eir, timeout);
  1802. if (IS_ERR(conn))
  1803. return conn;
  1804. /* Update LINK PHYs according to QoS preference */
  1805. conn->le_tx_def_phys = qos->bcast.out.phys;
  1806. /* Add Basic Announcement into Peridic Adv Data if BASE is set */
  1807. if (base_len && base) {
  1808. memcpy(conn->le_per_adv_data, eir, sizeof(eir));
  1809. conn->le_per_adv_data_len = base_len;
  1810. }
  1811. hci_iso_qos_setup(hdev, conn, &qos->bcast.out,
  1812. conn->le_tx_def_phys ? conn->le_tx_def_phys :
  1813. hdev->le_tx_def_phys);
  1814. conn->iso_qos = *qos;
  1815. conn->state = BT_BOUND;
  1816. /* Link BISes together */
  1817. parent = hci_conn_hash_lookup_big(hdev,
  1818. conn->iso_qos.bcast.big);
  1819. if (parent && parent != conn) {
  1820. link = hci_conn_link(parent, conn);
  1821. hci_conn_drop(conn);
  1822. if (!link)
  1823. return ERR_PTR(-ENOLINK);
  1824. }
  1825. return conn;
  1826. }
  1827. int hci_past_bis(struct hci_conn *conn, bdaddr_t *dst, __u8 dst_type)
  1828. {
  1829. struct hci_conn *le;
  1830. /* Lookup existing LE connection to rebind to */
  1831. le = hci_conn_hash_lookup_le(conn->hdev, dst, dst_type);
  1832. if (!le)
  1833. return -EINVAL;
  1834. return hci_past_sync(conn, le);
  1835. }
  1836. static void bis_mark_per_adv(struct hci_conn *conn, void *data)
  1837. {
  1838. struct iso_list_data *d = data;
  1839. /* Skip if not broadcast/ANY address */
  1840. if (bacmp(&conn->dst, BDADDR_ANY))
  1841. return;
  1842. if (d->big != conn->iso_qos.bcast.big ||
  1843. d->bis == BT_ISO_QOS_BIS_UNSET ||
  1844. d->bis != conn->iso_qos.bcast.bis)
  1845. return;
  1846. set_bit(HCI_CONN_PER_ADV, &conn->flags);
  1847. }
  1848. struct hci_conn *hci_connect_bis(struct hci_dev *hdev, bdaddr_t *dst,
  1849. __u8 dst_type, __u8 sid,
  1850. struct bt_iso_qos *qos,
  1851. __u8 base_len, __u8 *base, u16 timeout)
  1852. {
  1853. struct hci_conn *conn;
  1854. int err;
  1855. struct iso_list_data data;
  1856. conn = hci_bind_bis(hdev, dst, sid, qos, base_len, base, timeout);
  1857. if (IS_ERR(conn))
  1858. return conn;
  1859. if (conn->state == BT_CONNECTED)
  1860. return conn;
  1861. /* Check if SID needs to be allocated then search for the first
  1862. * available.
  1863. */
  1864. if (conn->sid == HCI_SID_INVALID) {
  1865. u8 sid;
  1866. for (sid = 0; sid <= 0x0f; sid++) {
  1867. if (!hci_find_adv_sid(hdev, sid)) {
  1868. conn->sid = sid;
  1869. break;
  1870. }
  1871. }
  1872. }
  1873. data.big = qos->bcast.big;
  1874. data.bis = qos->bcast.bis;
  1875. /* Set HCI_CONN_PER_ADV for all bound connections, to mark that
  1876. * the start periodic advertising and create BIG commands have
  1877. * been queued
  1878. */
  1879. hci_conn_hash_list_state(hdev, bis_mark_per_adv, BIS_LINK,
  1880. BT_BOUND, &data);
  1881. /* Queue start periodic advertising and create BIG */
  1882. err = hci_cmd_sync_queue(hdev, create_big_sync, conn,
  1883. create_big_complete);
  1884. if (err < 0) {
  1885. hci_conn_drop(conn);
  1886. return ERR_PTR(err);
  1887. }
  1888. return conn;
  1889. }
  1890. struct hci_conn *hci_connect_cis(struct hci_dev *hdev, bdaddr_t *dst,
  1891. __u8 dst_type, struct bt_iso_qos *qos,
  1892. u16 timeout)
  1893. {
  1894. struct hci_conn *le;
  1895. struct hci_conn *cis;
  1896. struct hci_link *link;
  1897. if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
  1898. le = hci_connect_le(hdev, dst, dst_type, false,
  1899. BT_SECURITY_LOW,
  1900. HCI_LE_CONN_TIMEOUT,
  1901. HCI_ROLE_SLAVE, 0, 0);
  1902. else
  1903. le = hci_connect_le_scan(hdev, dst, dst_type,
  1904. BT_SECURITY_LOW,
  1905. HCI_LE_CONN_TIMEOUT,
  1906. CONN_REASON_ISO_CONNECT);
  1907. if (IS_ERR(le))
  1908. return le;
  1909. hci_iso_qos_setup(hdev, le, &qos->ucast.out,
  1910. le->le_tx_def_phys ? le->le_tx_def_phys :
  1911. hdev->le_tx_def_phys);
  1912. hci_iso_qos_setup(hdev, le, &qos->ucast.in,
  1913. le->le_rx_def_phys ? le->le_rx_def_phys :
  1914. hdev->le_rx_def_phys);
  1915. cis = hci_bind_cis(hdev, dst, dst_type, qos, timeout);
  1916. if (IS_ERR(cis)) {
  1917. hci_conn_drop(le);
  1918. return cis;
  1919. }
  1920. link = hci_conn_link(le, cis);
  1921. hci_conn_drop(cis);
  1922. if (!link) {
  1923. hci_conn_drop(le);
  1924. return ERR_PTR(-ENOLINK);
  1925. }
  1926. cis->state = BT_CONNECT;
  1927. hci_le_create_cis_pending(hdev);
  1928. return cis;
  1929. }
  1930. /* Check link security requirement */
  1931. int hci_conn_check_link_mode(struct hci_conn *conn)
  1932. {
  1933. BT_DBG("hcon %p", conn);
  1934. /* In Secure Connections Only mode, it is required that Secure
  1935. * Connections is used and the link is encrypted with AES-CCM
  1936. * using a P-256 authenticated combination key.
  1937. */
  1938. if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) {
  1939. if (!hci_conn_sc_enabled(conn) ||
  1940. !test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
  1941. conn->key_type != HCI_LK_AUTH_COMBINATION_P256)
  1942. return 0;
  1943. }
  1944. /* AES encryption is required for Level 4:
  1945. *
  1946. * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 3, Part C
  1947. * page 1319:
  1948. *
  1949. * 128-bit equivalent strength for link and encryption keys
  1950. * required using FIPS approved algorithms (E0 not allowed,
  1951. * SAFER+ not allowed, and P-192 not allowed; encryption key
  1952. * not shortened)
  1953. */
  1954. if (conn->sec_level == BT_SECURITY_FIPS &&
  1955. !test_bit(HCI_CONN_AES_CCM, &conn->flags)) {
  1956. bt_dev_err(conn->hdev,
  1957. "Invalid security: Missing AES-CCM usage");
  1958. return 0;
  1959. }
  1960. if (hci_conn_ssp_enabled(conn) &&
  1961. !test_bit(HCI_CONN_ENCRYPT, &conn->flags))
  1962. return 0;
  1963. return 1;
  1964. }
  1965. /* Authenticate remote device */
  1966. static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
  1967. {
  1968. BT_DBG("hcon %p", conn);
  1969. if (conn->pending_sec_level > sec_level)
  1970. sec_level = conn->pending_sec_level;
  1971. if (sec_level > conn->sec_level)
  1972. conn->pending_sec_level = sec_level;
  1973. else if (test_bit(HCI_CONN_AUTH, &conn->flags))
  1974. return 1;
  1975. /* Make sure we preserve an existing MITM requirement*/
  1976. auth_type |= (conn->auth_type & 0x01);
  1977. conn->auth_type = auth_type;
  1978. if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
  1979. struct hci_cp_auth_requested cp;
  1980. cp.handle = cpu_to_le16(conn->handle);
  1981. hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
  1982. sizeof(cp), &cp);
  1983. /* Set the ENCRYPT_PEND to trigger encryption after
  1984. * authentication.
  1985. */
  1986. if (!test_bit(HCI_CONN_ENCRYPT, &conn->flags))
  1987. set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
  1988. }
  1989. return 0;
  1990. }
  1991. /* Encrypt the link */
  1992. static void hci_conn_encrypt(struct hci_conn *conn)
  1993. {
  1994. BT_DBG("hcon %p", conn);
  1995. if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
  1996. struct hci_cp_set_conn_encrypt cp;
  1997. cp.handle = cpu_to_le16(conn->handle);
  1998. cp.encrypt = 0x01;
  1999. hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
  2000. &cp);
  2001. }
  2002. }
  2003. /* Enable security */
  2004. int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
  2005. bool initiator)
  2006. {
  2007. BT_DBG("hcon %p", conn);
  2008. if (conn->type == LE_LINK)
  2009. return smp_conn_security(conn, sec_level);
  2010. /* For sdp we don't need the link key. */
  2011. if (sec_level == BT_SECURITY_SDP)
  2012. return 1;
  2013. /* For non 2.1 devices and low security level we don't need the link
  2014. key. */
  2015. if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn))
  2016. return 1;
  2017. /* For other security levels we need the link key. */
  2018. if (!test_bit(HCI_CONN_AUTH, &conn->flags))
  2019. goto auth;
  2020. switch (conn->key_type) {
  2021. case HCI_LK_AUTH_COMBINATION_P256:
  2022. /* An authenticated FIPS approved combination key has
  2023. * sufficient security for security level 4 or lower.
  2024. */
  2025. if (sec_level <= BT_SECURITY_FIPS)
  2026. goto encrypt;
  2027. break;
  2028. case HCI_LK_AUTH_COMBINATION_P192:
  2029. /* An authenticated combination key has sufficient security for
  2030. * security level 3 or lower.
  2031. */
  2032. if (sec_level <= BT_SECURITY_HIGH)
  2033. goto encrypt;
  2034. break;
  2035. case HCI_LK_UNAUTH_COMBINATION_P192:
  2036. case HCI_LK_UNAUTH_COMBINATION_P256:
  2037. /* An unauthenticated combination key has sufficient security
  2038. * for security level 2 or lower.
  2039. */
  2040. if (sec_level <= BT_SECURITY_MEDIUM)
  2041. goto encrypt;
  2042. break;
  2043. case HCI_LK_COMBINATION:
  2044. /* A combination key has always sufficient security for the
  2045. * security levels 2 or lower. High security level requires the
  2046. * combination key is generated using maximum PIN code length
  2047. * (16). For pre 2.1 units.
  2048. */
  2049. if (sec_level <= BT_SECURITY_MEDIUM || conn->pin_length == 16)
  2050. goto encrypt;
  2051. break;
  2052. default:
  2053. break;
  2054. }
  2055. auth:
  2056. if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
  2057. return 0;
  2058. if (initiator)
  2059. set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
  2060. if (!hci_conn_auth(conn, sec_level, auth_type))
  2061. return 0;
  2062. encrypt:
  2063. if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) {
  2064. /* Ensure that the encryption key size has been read,
  2065. * otherwise stall the upper layer responses.
  2066. */
  2067. if (!conn->enc_key_size)
  2068. return 0;
  2069. /* Nothing else needed, all requirements are met */
  2070. return 1;
  2071. }
  2072. hci_conn_encrypt(conn);
  2073. return 0;
  2074. }
  2075. EXPORT_SYMBOL(hci_conn_security);
  2076. /* Check secure link requirement */
  2077. int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
  2078. {
  2079. BT_DBG("hcon %p", conn);
  2080. /* Accept if non-secure or higher security level is required */
  2081. if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS)
  2082. return 1;
  2083. /* Accept if secure or higher security level is already present */
  2084. if (conn->sec_level == BT_SECURITY_HIGH ||
  2085. conn->sec_level == BT_SECURITY_FIPS)
  2086. return 1;
  2087. /* Reject not secure link */
  2088. return 0;
  2089. }
  2090. EXPORT_SYMBOL(hci_conn_check_secure);
  2091. /* Switch role */
  2092. int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
  2093. {
  2094. BT_DBG("hcon %p", conn);
  2095. if (role == conn->role)
  2096. return 1;
  2097. if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) {
  2098. struct hci_cp_switch_role cp;
  2099. bacpy(&cp.bdaddr, &conn->dst);
  2100. cp.role = role;
  2101. hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
  2102. }
  2103. return 0;
  2104. }
  2105. EXPORT_SYMBOL(hci_conn_switch_role);
  2106. /* Enter active mode */
  2107. void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
  2108. {
  2109. struct hci_dev *hdev = conn->hdev;
  2110. BT_DBG("hcon %p mode %d", conn, conn->mode);
  2111. if (conn->mode != HCI_CM_SNIFF)
  2112. goto timer;
  2113. if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active)
  2114. goto timer;
  2115. if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
  2116. struct hci_cp_exit_sniff_mode cp;
  2117. cp.handle = cpu_to_le16(conn->handle);
  2118. hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
  2119. }
  2120. timer:
  2121. if (hdev->idle_timeout > 0)
  2122. mod_delayed_work(hdev->workqueue, &conn->idle_work,
  2123. msecs_to_jiffies(hdev->idle_timeout));
  2124. }
  2125. /* Drop all connection on the device */
  2126. void hci_conn_hash_flush(struct hci_dev *hdev)
  2127. {
  2128. struct list_head *head = &hdev->conn_hash.list;
  2129. struct hci_conn *conn;
  2130. BT_DBG("hdev %s", hdev->name);
  2131. /* We should not traverse the list here, because hci_conn_del
  2132. * can remove extra links, which may cause the list traversal
  2133. * to hit items that have already been released.
  2134. */
  2135. while ((conn = list_first_entry_or_null(head,
  2136. struct hci_conn,
  2137. list)) != NULL) {
  2138. conn->state = BT_CLOSED;
  2139. hci_disconn_cfm(conn, HCI_ERROR_LOCAL_HOST_TERM);
  2140. hci_conn_del(conn);
  2141. }
  2142. }
  2143. static u32 get_link_mode(struct hci_conn *conn)
  2144. {
  2145. u32 link_mode = 0;
  2146. if (conn->role == HCI_ROLE_MASTER)
  2147. link_mode |= HCI_LM_MASTER;
  2148. if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
  2149. link_mode |= HCI_LM_ENCRYPT;
  2150. if (test_bit(HCI_CONN_AUTH, &conn->flags))
  2151. link_mode |= HCI_LM_AUTH;
  2152. if (test_bit(HCI_CONN_SECURE, &conn->flags))
  2153. link_mode |= HCI_LM_SECURE;
  2154. if (test_bit(HCI_CONN_FIPS, &conn->flags))
  2155. link_mode |= HCI_LM_FIPS;
  2156. return link_mode;
  2157. }
  2158. int hci_get_conn_list(void __user *arg)
  2159. {
  2160. struct hci_conn *c;
  2161. struct hci_conn_list_req req, *cl;
  2162. struct hci_conn_info *ci;
  2163. struct hci_dev *hdev;
  2164. int n = 0, size, err;
  2165. if (copy_from_user(&req, arg, sizeof(req)))
  2166. return -EFAULT;
  2167. if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
  2168. return -EINVAL;
  2169. size = sizeof(req) + req.conn_num * sizeof(*ci);
  2170. cl = kmalloc(size, GFP_KERNEL);
  2171. if (!cl)
  2172. return -ENOMEM;
  2173. hdev = hci_dev_get(req.dev_id);
  2174. if (!hdev) {
  2175. kfree(cl);
  2176. return -ENODEV;
  2177. }
  2178. ci = cl->conn_info;
  2179. hci_dev_lock(hdev);
  2180. list_for_each_entry(c, &hdev->conn_hash.list, list) {
  2181. bacpy(&(ci + n)->bdaddr, &c->dst);
  2182. (ci + n)->handle = c->handle;
  2183. (ci + n)->type = c->type;
  2184. (ci + n)->out = c->out;
  2185. (ci + n)->state = c->state;
  2186. (ci + n)->link_mode = get_link_mode(c);
  2187. if (++n >= req.conn_num)
  2188. break;
  2189. }
  2190. hci_dev_unlock(hdev);
  2191. cl->dev_id = hdev->id;
  2192. cl->conn_num = n;
  2193. size = sizeof(req) + n * sizeof(*ci);
  2194. hci_dev_put(hdev);
  2195. err = copy_to_user(arg, cl, size);
  2196. kfree(cl);
  2197. return err ? -EFAULT : 0;
  2198. }
  2199. int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
  2200. {
  2201. struct hci_conn_info_req req;
  2202. struct hci_conn_info ci;
  2203. struct hci_conn *conn;
  2204. char __user *ptr = arg + sizeof(req);
  2205. if (copy_from_user(&req, arg, sizeof(req)))
  2206. return -EFAULT;
  2207. hci_dev_lock(hdev);
  2208. conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
  2209. if (conn) {
  2210. bacpy(&ci.bdaddr, &conn->dst);
  2211. ci.handle = conn->handle;
  2212. ci.type = conn->type;
  2213. ci.out = conn->out;
  2214. ci.state = conn->state;
  2215. ci.link_mode = get_link_mode(conn);
  2216. }
  2217. hci_dev_unlock(hdev);
  2218. if (!conn)
  2219. return -ENOENT;
  2220. return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
  2221. }
  2222. int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
  2223. {
  2224. struct hci_auth_info_req req;
  2225. struct hci_conn *conn;
  2226. if (copy_from_user(&req, arg, sizeof(req)))
  2227. return -EFAULT;
  2228. hci_dev_lock(hdev);
  2229. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
  2230. if (conn)
  2231. req.type = conn->auth_type;
  2232. hci_dev_unlock(hdev);
  2233. if (!conn)
  2234. return -ENOENT;
  2235. return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
  2236. }
  2237. struct hci_chan *hci_chan_create(struct hci_conn *conn)
  2238. {
  2239. struct hci_dev *hdev = conn->hdev;
  2240. struct hci_chan *chan;
  2241. BT_DBG("%s hcon %p", hdev->name, conn);
  2242. if (test_bit(HCI_CONN_DROP, &conn->flags)) {
  2243. BT_DBG("Refusing to create new hci_chan");
  2244. return NULL;
  2245. }
  2246. chan = kzalloc_obj(*chan);
  2247. if (!chan)
  2248. return NULL;
  2249. chan->conn = hci_conn_get(conn);
  2250. skb_queue_head_init(&chan->data_q);
  2251. chan->state = BT_CONNECTED;
  2252. list_add_rcu(&chan->list, &conn->chan_list);
  2253. return chan;
  2254. }
  2255. void hci_chan_del(struct hci_chan *chan)
  2256. {
  2257. struct hci_conn *conn = chan->conn;
  2258. struct hci_dev *hdev = conn->hdev;
  2259. BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan);
  2260. list_del_rcu(&chan->list);
  2261. synchronize_rcu();
  2262. /* Prevent new hci_chan's to be created for this hci_conn */
  2263. set_bit(HCI_CONN_DROP, &conn->flags);
  2264. hci_conn_put(conn);
  2265. skb_queue_purge(&chan->data_q);
  2266. kfree(chan);
  2267. }
  2268. void hci_chan_list_flush(struct hci_conn *conn)
  2269. {
  2270. struct hci_chan *chan, *n;
  2271. BT_DBG("hcon %p", conn);
  2272. list_for_each_entry_safe(chan, n, &conn->chan_list, list)
  2273. hci_chan_del(chan);
  2274. }
  2275. static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon,
  2276. __u16 handle)
  2277. {
  2278. struct hci_chan *hchan;
  2279. list_for_each_entry(hchan, &hcon->chan_list, list) {
  2280. if (hchan->handle == handle)
  2281. return hchan;
  2282. }
  2283. return NULL;
  2284. }
  2285. struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle)
  2286. {
  2287. struct hci_conn_hash *h = &hdev->conn_hash;
  2288. struct hci_conn *hcon;
  2289. struct hci_chan *hchan = NULL;
  2290. rcu_read_lock();
  2291. list_for_each_entry_rcu(hcon, &h->list, list) {
  2292. hchan = __hci_chan_lookup_handle(hcon, handle);
  2293. if (hchan)
  2294. break;
  2295. }
  2296. rcu_read_unlock();
  2297. return hchan;
  2298. }
  2299. u32 hci_conn_get_phy(struct hci_conn *conn)
  2300. {
  2301. u32 phys = 0;
  2302. /* BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 2, Part B page 471:
  2303. * Table 6.2: Packets defined for synchronous, asynchronous, and
  2304. * CPB logical transport types.
  2305. */
  2306. switch (conn->type) {
  2307. case SCO_LINK:
  2308. /* SCO logical transport (1 Mb/s):
  2309. * HV1, HV2, HV3 and DV.
  2310. */
  2311. phys |= BT_PHY_BR_1M_1SLOT;
  2312. break;
  2313. case ACL_LINK:
  2314. /* ACL logical transport (1 Mb/s) ptt=0:
  2315. * DH1, DM3, DH3, DM5 and DH5.
  2316. */
  2317. phys |= BT_PHY_BR_1M_1SLOT;
  2318. if (conn->pkt_type & (HCI_DM3 | HCI_DH3))
  2319. phys |= BT_PHY_BR_1M_3SLOT;
  2320. if (conn->pkt_type & (HCI_DM5 | HCI_DH5))
  2321. phys |= BT_PHY_BR_1M_5SLOT;
  2322. /* ACL logical transport (2 Mb/s) ptt=1:
  2323. * 2-DH1, 2-DH3 and 2-DH5.
  2324. */
  2325. if (!(conn->pkt_type & HCI_2DH1))
  2326. phys |= BT_PHY_EDR_2M_1SLOT;
  2327. if (!(conn->pkt_type & HCI_2DH3))
  2328. phys |= BT_PHY_EDR_2M_3SLOT;
  2329. if (!(conn->pkt_type & HCI_2DH5))
  2330. phys |= BT_PHY_EDR_2M_5SLOT;
  2331. /* ACL logical transport (3 Mb/s) ptt=1:
  2332. * 3-DH1, 3-DH3 and 3-DH5.
  2333. */
  2334. if (!(conn->pkt_type & HCI_3DH1))
  2335. phys |= BT_PHY_EDR_3M_1SLOT;
  2336. if (!(conn->pkt_type & HCI_3DH3))
  2337. phys |= BT_PHY_EDR_3M_3SLOT;
  2338. if (!(conn->pkt_type & HCI_3DH5))
  2339. phys |= BT_PHY_EDR_3M_5SLOT;
  2340. break;
  2341. case ESCO_LINK:
  2342. /* eSCO logical transport (1 Mb/s): EV3, EV4 and EV5 */
  2343. phys |= BT_PHY_BR_1M_1SLOT;
  2344. if (!(conn->pkt_type & (ESCO_EV4 | ESCO_EV5)))
  2345. phys |= BT_PHY_BR_1M_3SLOT;
  2346. /* eSCO logical transport (2 Mb/s): 2-EV3, 2-EV5 */
  2347. if (!(conn->pkt_type & ESCO_2EV3))
  2348. phys |= BT_PHY_EDR_2M_1SLOT;
  2349. if (!(conn->pkt_type & ESCO_2EV5))
  2350. phys |= BT_PHY_EDR_2M_3SLOT;
  2351. /* eSCO logical transport (3 Mb/s): 3-EV3, 3-EV5 */
  2352. if (!(conn->pkt_type & ESCO_3EV3))
  2353. phys |= BT_PHY_EDR_3M_1SLOT;
  2354. if (!(conn->pkt_type & ESCO_3EV5))
  2355. phys |= BT_PHY_EDR_3M_3SLOT;
  2356. break;
  2357. case LE_LINK:
  2358. if (conn->le_tx_def_phys & HCI_LE_SET_PHY_1M)
  2359. phys |= BT_PHY_LE_1M_TX;
  2360. if (conn->le_rx_def_phys & HCI_LE_SET_PHY_1M)
  2361. phys |= BT_PHY_LE_1M_RX;
  2362. if (conn->le_tx_def_phys & HCI_LE_SET_PHY_2M)
  2363. phys |= BT_PHY_LE_2M_TX;
  2364. if (conn->le_rx_def_phys & HCI_LE_SET_PHY_2M)
  2365. phys |= BT_PHY_LE_2M_RX;
  2366. if (conn->le_tx_def_phys & HCI_LE_SET_PHY_CODED)
  2367. phys |= BT_PHY_LE_CODED_TX;
  2368. if (conn->le_rx_def_phys & HCI_LE_SET_PHY_CODED)
  2369. phys |= BT_PHY_LE_CODED_RX;
  2370. break;
  2371. }
  2372. return phys;
  2373. }
  2374. static u16 bt_phy_pkt_type(struct hci_conn *conn, u32 phys)
  2375. {
  2376. u16 pkt_type = conn->pkt_type;
  2377. if (phys & BT_PHY_BR_1M_3SLOT)
  2378. pkt_type |= HCI_DM3 | HCI_DH3;
  2379. else
  2380. pkt_type &= ~(HCI_DM3 | HCI_DH3);
  2381. if (phys & BT_PHY_BR_1M_5SLOT)
  2382. pkt_type |= HCI_DM5 | HCI_DH5;
  2383. else
  2384. pkt_type &= ~(HCI_DM5 | HCI_DH5);
  2385. if (phys & BT_PHY_EDR_2M_1SLOT)
  2386. pkt_type &= ~HCI_2DH1;
  2387. else
  2388. pkt_type |= HCI_2DH1;
  2389. if (phys & BT_PHY_EDR_2M_3SLOT)
  2390. pkt_type &= ~HCI_2DH3;
  2391. else
  2392. pkt_type |= HCI_2DH3;
  2393. if (phys & BT_PHY_EDR_2M_5SLOT)
  2394. pkt_type &= ~HCI_2DH5;
  2395. else
  2396. pkt_type |= HCI_2DH5;
  2397. if (phys & BT_PHY_EDR_3M_1SLOT)
  2398. pkt_type &= ~HCI_3DH1;
  2399. else
  2400. pkt_type |= HCI_3DH1;
  2401. if (phys & BT_PHY_EDR_3M_3SLOT)
  2402. pkt_type &= ~HCI_3DH3;
  2403. else
  2404. pkt_type |= HCI_3DH3;
  2405. if (phys & BT_PHY_EDR_3M_5SLOT)
  2406. pkt_type &= ~HCI_3DH5;
  2407. else
  2408. pkt_type |= HCI_3DH5;
  2409. return pkt_type;
  2410. }
  2411. static int bt_phy_le_phy(u32 phys, u8 *tx_phys, u8 *rx_phys)
  2412. {
  2413. if (!tx_phys || !rx_phys)
  2414. return -EINVAL;
  2415. *tx_phys = 0;
  2416. *rx_phys = 0;
  2417. if (phys & BT_PHY_LE_1M_TX)
  2418. *tx_phys |= HCI_LE_SET_PHY_1M;
  2419. if (phys & BT_PHY_LE_1M_RX)
  2420. *rx_phys |= HCI_LE_SET_PHY_1M;
  2421. if (phys & BT_PHY_LE_2M_TX)
  2422. *tx_phys |= HCI_LE_SET_PHY_2M;
  2423. if (phys & BT_PHY_LE_2M_RX)
  2424. *rx_phys |= HCI_LE_SET_PHY_2M;
  2425. if (phys & BT_PHY_LE_CODED_TX)
  2426. *tx_phys |= HCI_LE_SET_PHY_CODED;
  2427. if (phys & BT_PHY_LE_CODED_RX)
  2428. *rx_phys |= HCI_LE_SET_PHY_CODED;
  2429. return 0;
  2430. }
  2431. int hci_conn_set_phy(struct hci_conn *conn, u32 phys)
  2432. {
  2433. u8 tx_phys, rx_phys;
  2434. switch (conn->type) {
  2435. case SCO_LINK:
  2436. case ESCO_LINK:
  2437. return -EINVAL;
  2438. case ACL_LINK:
  2439. /* Only allow setting BR/EDR PHYs if link type is ACL */
  2440. if (phys & ~BT_PHY_BREDR_MASK)
  2441. return -EINVAL;
  2442. return hci_acl_change_pkt_type(conn,
  2443. bt_phy_pkt_type(conn, phys));
  2444. case LE_LINK:
  2445. /* Only allow setting LE PHYs if link type is LE */
  2446. if (phys & ~BT_PHY_LE_MASK)
  2447. return -EINVAL;
  2448. if (bt_phy_le_phy(phys, &tx_phys, &rx_phys))
  2449. return -EINVAL;
  2450. return hci_le_set_phy(conn, tx_phys, rx_phys);
  2451. default:
  2452. return -EINVAL;
  2453. }
  2454. }
  2455. static int abort_conn_sync(struct hci_dev *hdev, void *data)
  2456. {
  2457. struct hci_conn *conn = data;
  2458. if (!hci_conn_valid(hdev, conn))
  2459. return -ECANCELED;
  2460. return hci_abort_conn_sync(hdev, conn, conn->abort_reason);
  2461. }
  2462. int hci_abort_conn(struct hci_conn *conn, u8 reason)
  2463. {
  2464. struct hci_dev *hdev = conn->hdev;
  2465. /* If abort_reason has already been set it means the connection is
  2466. * already being aborted so don't attempt to overwrite it.
  2467. */
  2468. if (conn->abort_reason)
  2469. return 0;
  2470. bt_dev_dbg(hdev, "handle 0x%2.2x reason 0x%2.2x", conn->handle, reason);
  2471. conn->abort_reason = reason;
  2472. /* If the connection is pending check the command opcode since that
  2473. * might be blocking on hci_cmd_sync_work while waiting its respective
  2474. * event so we need to hci_cmd_sync_cancel to cancel it.
  2475. *
  2476. * hci_connect_le serializes the connection attempts so only one
  2477. * connection can be in BT_CONNECT at time.
  2478. */
  2479. if (conn->state == BT_CONNECT && READ_ONCE(hdev->req_status) == HCI_REQ_PEND) {
  2480. switch (hci_skb_event(hdev->sent_cmd)) {
  2481. case HCI_EV_CONN_COMPLETE:
  2482. case HCI_EV_LE_CONN_COMPLETE:
  2483. case HCI_EV_LE_ENHANCED_CONN_COMPLETE:
  2484. case HCI_EVT_LE_CIS_ESTABLISHED:
  2485. hci_cmd_sync_cancel(hdev, ECANCELED);
  2486. break;
  2487. }
  2488. /* Cancel connect attempt if still queued/pending */
  2489. } else if (!hci_cancel_connect_sync(hdev, conn)) {
  2490. return 0;
  2491. }
  2492. /* Run immediately if on cmd_sync_work since this may be called
  2493. * as a result to MGMT_OP_DISCONNECT/MGMT_OP_UNPAIR which does
  2494. * already queue its callback on cmd_sync_work.
  2495. */
  2496. return hci_cmd_sync_run_once(hdev, abort_conn_sync, conn, NULL);
  2497. }
  2498. void hci_setup_tx_timestamp(struct sk_buff *skb, size_t key_offset,
  2499. const struct sockcm_cookie *sockc)
  2500. {
  2501. struct sock *sk = skb ? skb->sk : NULL;
  2502. int key;
  2503. /* This shall be called on a single skb of those generated by user
  2504. * sendmsg(), and only when the sendmsg() does not return error to
  2505. * user. This is required for keeping the tskey that increments here in
  2506. * sync with possible sendmsg() counting by user.
  2507. *
  2508. * Stream sockets shall set key_offset to sendmsg() length in bytes
  2509. * and call with the last fragment, others to 1 and first fragment.
  2510. */
  2511. if (!skb || !sockc || !sk || !key_offset)
  2512. return;
  2513. sock_tx_timestamp(sk, sockc, &skb_shinfo(skb)->tx_flags);
  2514. if (sk->sk_type == SOCK_STREAM)
  2515. key = atomic_add_return(key_offset, &sk->sk_tskey);
  2516. if (sockc->tsflags & SOF_TIMESTAMPING_OPT_ID &&
  2517. sockc->tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK) {
  2518. if (sockc->tsflags & SOCKCM_FLAG_TS_OPT_ID) {
  2519. skb_shinfo(skb)->tskey = sockc->ts_opt_id;
  2520. } else {
  2521. if (sk->sk_type != SOCK_STREAM)
  2522. key = atomic_inc_return(&sk->sk_tskey);
  2523. skb_shinfo(skb)->tskey = key - 1;
  2524. }
  2525. }
  2526. }
  2527. void hci_conn_tx_queue(struct hci_conn *conn, struct sk_buff *skb)
  2528. {
  2529. struct tx_queue *comp = &conn->tx_q;
  2530. bool track = false;
  2531. /* Emit SND now, ie. just before sending to driver */
  2532. if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP)
  2533. __skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SND);
  2534. /* COMPLETION tstamp is emitted for tracked skb later in Number of
  2535. * Completed Packets event. Available only for flow controlled cases.
  2536. *
  2537. * TODO: SCO support without flowctl (needs to be done in drivers)
  2538. */
  2539. switch (conn->type) {
  2540. case CIS_LINK:
  2541. case BIS_LINK:
  2542. case PA_LINK:
  2543. case ACL_LINK:
  2544. case LE_LINK:
  2545. break;
  2546. case SCO_LINK:
  2547. case ESCO_LINK:
  2548. if (!hci_dev_test_flag(conn->hdev, HCI_SCO_FLOWCTL))
  2549. return;
  2550. break;
  2551. default:
  2552. return;
  2553. }
  2554. if (skb->sk && (skb_shinfo(skb)->tx_flags & SKBTX_COMPLETION_TSTAMP))
  2555. track = true;
  2556. /* If nothing is tracked, just count extra skbs at the queue head */
  2557. if (!track && !comp->tracked) {
  2558. comp->extra++;
  2559. return;
  2560. }
  2561. if (track) {
  2562. skb = skb_clone_sk(skb);
  2563. if (!skb)
  2564. goto count_only;
  2565. comp->tracked++;
  2566. } else {
  2567. skb = skb_clone(skb, GFP_KERNEL);
  2568. if (!skb)
  2569. goto count_only;
  2570. }
  2571. skb_queue_tail(&comp->queue, skb);
  2572. return;
  2573. count_only:
  2574. /* Stop tracking skbs, and only count. This will not emit timestamps for
  2575. * the packets, but if we get here something is more seriously wrong.
  2576. */
  2577. comp->tracked = 0;
  2578. comp->extra += skb_queue_len(&comp->queue) + 1;
  2579. skb_queue_purge(&comp->queue);
  2580. }
  2581. void hci_conn_tx_dequeue(struct hci_conn *conn)
  2582. {
  2583. struct tx_queue *comp = &conn->tx_q;
  2584. struct sk_buff *skb;
  2585. /* If there are tracked skbs, the counted extra go before dequeuing real
  2586. * skbs, to keep ordering. When nothing is tracked, the ordering doesn't
  2587. * matter so dequeue real skbs first to get rid of them ASAP.
  2588. */
  2589. if (comp->extra && (comp->tracked || skb_queue_empty(&comp->queue))) {
  2590. comp->extra--;
  2591. return;
  2592. }
  2593. skb = skb_dequeue(&comp->queue);
  2594. if (!skb)
  2595. return;
  2596. if (skb->sk) {
  2597. comp->tracked--;
  2598. __skb_tstamp_tx(skb, NULL, NULL, skb->sk,
  2599. SCM_TSTAMP_COMPLETION);
  2600. }
  2601. kfree_skb(skb);
  2602. }
  2603. u8 *hci_conn_key_enc_size(struct hci_conn *conn)
  2604. {
  2605. if (conn->type == ACL_LINK) {
  2606. struct link_key *key;
  2607. key = hci_find_link_key(conn->hdev, &conn->dst);
  2608. if (!key)
  2609. return NULL;
  2610. return &key->pin_len;
  2611. } else if (conn->type == LE_LINK) {
  2612. struct smp_ltk *ltk;
  2613. ltk = hci_find_ltk(conn->hdev, &conn->dst, conn->dst_type,
  2614. conn->role);
  2615. if (!ltk)
  2616. return NULL;
  2617. return &ltk->enc_size;
  2618. }
  2619. return NULL;
  2620. }
  2621. int hci_ethtool_ts_info(unsigned int index, int sk_proto,
  2622. struct kernel_ethtool_ts_info *info)
  2623. {
  2624. struct hci_dev *hdev;
  2625. hdev = hci_dev_get(index);
  2626. if (!hdev)
  2627. return -ENODEV;
  2628. info->so_timestamping =
  2629. SOF_TIMESTAMPING_RX_SOFTWARE |
  2630. SOF_TIMESTAMPING_SOFTWARE;
  2631. info->phc_index = -1;
  2632. info->tx_types = BIT(HWTSTAMP_TX_OFF);
  2633. info->rx_filters = BIT(HWTSTAMP_FILTER_NONE);
  2634. switch (sk_proto) {
  2635. case BTPROTO_ISO:
  2636. case BTPROTO_L2CAP:
  2637. info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE;
  2638. info->so_timestamping |= SOF_TIMESTAMPING_TX_COMPLETION;
  2639. break;
  2640. case BTPROTO_SCO:
  2641. info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE;
  2642. if (hci_dev_test_flag(hdev, HCI_SCO_FLOWCTL))
  2643. info->so_timestamping |= SOF_TIMESTAMPING_TX_COMPLETION;
  2644. break;
  2645. }
  2646. hci_dev_put(hdev);
  2647. return 0;
  2648. }