hci_sock.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (C) 2000-2001 Qualcomm Incorporated
  4. Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License version 2 as
  7. published by the Free Software Foundation;
  8. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  9. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  10. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  11. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  12. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  13. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  17. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  18. SOFTWARE IS DISCLAIMED.
  19. */
  20. /* Bluetooth HCI sockets. */
  21. #include <linux/export.h>
  22. #include <linux/utsname.h>
  23. #include <linux/sched.h>
  24. #include <asm/unaligned.h>
  25. #include <net/bluetooth/bluetooth.h>
  26. #include <net/bluetooth/hci_core.h>
  27. #include <net/bluetooth/hci_mon.h>
  28. #include <net/bluetooth/mgmt.h>
  29. #include "mgmt_util.h"
  30. static LIST_HEAD(mgmt_chan_list);
  31. static DEFINE_MUTEX(mgmt_chan_list_lock);
  32. static DEFINE_IDA(sock_cookie_ida);
  33. static atomic_t monitor_promisc = ATOMIC_INIT(0);
  34. /* ----- HCI socket interface ----- */
  35. /* Socket info */
  36. #define hci_pi(sk) ((struct hci_pinfo *) sk)
  37. struct hci_pinfo {
  38. struct bt_sock bt;
  39. struct hci_dev *hdev;
  40. struct hci_filter filter;
  41. __u32 cmsg_mask;
  42. unsigned short channel;
  43. unsigned long flags;
  44. __u32 cookie;
  45. char comm[TASK_COMM_LEN];
  46. };
  47. void hci_sock_set_flag(struct sock *sk, int nr)
  48. {
  49. set_bit(nr, &hci_pi(sk)->flags);
  50. }
  51. void hci_sock_clear_flag(struct sock *sk, int nr)
  52. {
  53. clear_bit(nr, &hci_pi(sk)->flags);
  54. }
  55. int hci_sock_test_flag(struct sock *sk, int nr)
  56. {
  57. return test_bit(nr, &hci_pi(sk)->flags);
  58. }
  59. unsigned short hci_sock_get_channel(struct sock *sk)
  60. {
  61. return hci_pi(sk)->channel;
  62. }
  63. u32 hci_sock_get_cookie(struct sock *sk)
  64. {
  65. return hci_pi(sk)->cookie;
  66. }
  67. static bool hci_sock_gen_cookie(struct sock *sk)
  68. {
  69. int id = hci_pi(sk)->cookie;
  70. if (!id) {
  71. id = ida_simple_get(&sock_cookie_ida, 1, 0, GFP_KERNEL);
  72. if (id < 0)
  73. id = 0xffffffff;
  74. hci_pi(sk)->cookie = id;
  75. get_task_comm(hci_pi(sk)->comm, current);
  76. return true;
  77. }
  78. return false;
  79. }
  80. static void hci_sock_free_cookie(struct sock *sk)
  81. {
  82. int id = hci_pi(sk)->cookie;
  83. if (id) {
  84. hci_pi(sk)->cookie = 0xffffffff;
  85. ida_simple_remove(&sock_cookie_ida, id);
  86. }
  87. }
  88. static inline int hci_test_bit(int nr, const void *addr)
  89. {
  90. return *((const __u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
  91. }
  92. /* Security filter */
  93. #define HCI_SFLT_MAX_OGF 5
  94. struct hci_sec_filter {
  95. __u32 type_mask;
  96. __u32 event_mask[2];
  97. __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
  98. };
  99. static const struct hci_sec_filter hci_sec_filter = {
  100. /* Packet types */
  101. 0x10,
  102. /* Events */
  103. { 0x1000d9fe, 0x0000b00c },
  104. /* Commands */
  105. {
  106. { 0x0 },
  107. /* OGF_LINK_CTL */
  108. { 0xbe000006, 0x00000001, 0x00000000, 0x00 },
  109. /* OGF_LINK_POLICY */
  110. { 0x00005200, 0x00000000, 0x00000000, 0x00 },
  111. /* OGF_HOST_CTL */
  112. { 0xaab00200, 0x2b402aaa, 0x05220154, 0x00 },
  113. /* OGF_INFO_PARAM */
  114. { 0x000002be, 0x00000000, 0x00000000, 0x00 },
  115. /* OGF_STATUS_PARAM */
  116. { 0x000000ea, 0x00000000, 0x00000000, 0x00 }
  117. }
  118. };
  119. static struct bt_sock_list hci_sk_list = {
  120. .lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock)
  121. };
  122. static bool is_filtered_packet(struct sock *sk, struct sk_buff *skb)
  123. {
  124. struct hci_filter *flt;
  125. int flt_type, flt_event;
  126. /* Apply filter */
  127. flt = &hci_pi(sk)->filter;
  128. flt_type = hci_skb_pkt_type(skb) & HCI_FLT_TYPE_BITS;
  129. if (!test_bit(flt_type, &flt->type_mask))
  130. return true;
  131. /* Extra filter for event packets only */
  132. if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT)
  133. return false;
  134. flt_event = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS);
  135. if (!hci_test_bit(flt_event, &flt->event_mask))
  136. return true;
  137. /* Check filter only when opcode is set */
  138. if (!flt->opcode)
  139. return false;
  140. if (flt_event == HCI_EV_CMD_COMPLETE &&
  141. flt->opcode != get_unaligned((__le16 *)(skb->data + 3)))
  142. return true;
  143. if (flt_event == HCI_EV_CMD_STATUS &&
  144. flt->opcode != get_unaligned((__le16 *)(skb->data + 4)))
  145. return true;
  146. return false;
  147. }
  148. /* Send frame to RAW socket */
  149. void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
  150. {
  151. struct sock *sk;
  152. struct sk_buff *skb_copy = NULL;
  153. BT_DBG("hdev %p len %d", hdev, skb->len);
  154. read_lock(&hci_sk_list.lock);
  155. sk_for_each(sk, &hci_sk_list.head) {
  156. struct sk_buff *nskb;
  157. if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev)
  158. continue;
  159. /* Don't send frame to the socket it came from */
  160. if (skb->sk == sk)
  161. continue;
  162. if (hci_pi(sk)->channel == HCI_CHANNEL_RAW) {
  163. if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT &&
  164. hci_skb_pkt_type(skb) != HCI_EVENT_PKT &&
  165. hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
  166. hci_skb_pkt_type(skb) != HCI_SCODATA_PKT)
  167. continue;
  168. if (is_filtered_packet(sk, skb))
  169. continue;
  170. } else if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
  171. if (!bt_cb(skb)->incoming)
  172. continue;
  173. if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT &&
  174. hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
  175. hci_skb_pkt_type(skb) != HCI_SCODATA_PKT)
  176. continue;
  177. } else {
  178. /* Don't send frame to other channel types */
  179. continue;
  180. }
  181. if (!skb_copy) {
  182. /* Create a private copy with headroom */
  183. skb_copy = __pskb_copy_fclone(skb, 1, GFP_ATOMIC, true);
  184. if (!skb_copy)
  185. continue;
  186. /* Put type byte before the data */
  187. memcpy(skb_push(skb_copy, 1), &hci_skb_pkt_type(skb), 1);
  188. }
  189. nskb = skb_clone(skb_copy, GFP_ATOMIC);
  190. if (!nskb)
  191. continue;
  192. if (sock_queue_rcv_skb(sk, nskb))
  193. kfree_skb(nskb);
  194. }
  195. read_unlock(&hci_sk_list.lock);
  196. kfree_skb(skb_copy);
  197. }
  198. /* Send frame to sockets with specific channel */
  199. void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
  200. int flag, struct sock *skip_sk)
  201. {
  202. struct sock *sk;
  203. BT_DBG("channel %u len %d", channel, skb->len);
  204. read_lock(&hci_sk_list.lock);
  205. sk_for_each(sk, &hci_sk_list.head) {
  206. struct sk_buff *nskb;
  207. /* Ignore socket without the flag set */
  208. if (!hci_sock_test_flag(sk, flag))
  209. continue;
  210. /* Skip the original socket */
  211. if (sk == skip_sk)
  212. continue;
  213. if (sk->sk_state != BT_BOUND)
  214. continue;
  215. if (hci_pi(sk)->channel != channel)
  216. continue;
  217. nskb = skb_clone(skb, GFP_ATOMIC);
  218. if (!nskb)
  219. continue;
  220. if (sock_queue_rcv_skb(sk, nskb))
  221. kfree_skb(nskb);
  222. }
  223. read_unlock(&hci_sk_list.lock);
  224. }
  225. /* Send frame to monitor socket */
  226. void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb)
  227. {
  228. struct sk_buff *skb_copy = NULL;
  229. struct hci_mon_hdr *hdr;
  230. __le16 opcode;
  231. if (!atomic_read(&monitor_promisc))
  232. return;
  233. BT_DBG("hdev %p len %d", hdev, skb->len);
  234. switch (hci_skb_pkt_type(skb)) {
  235. case HCI_COMMAND_PKT:
  236. opcode = cpu_to_le16(HCI_MON_COMMAND_PKT);
  237. break;
  238. case HCI_EVENT_PKT:
  239. opcode = cpu_to_le16(HCI_MON_EVENT_PKT);
  240. break;
  241. case HCI_ACLDATA_PKT:
  242. if (bt_cb(skb)->incoming)
  243. opcode = cpu_to_le16(HCI_MON_ACL_RX_PKT);
  244. else
  245. opcode = cpu_to_le16(HCI_MON_ACL_TX_PKT);
  246. break;
  247. case HCI_SCODATA_PKT:
  248. if (bt_cb(skb)->incoming)
  249. opcode = cpu_to_le16(HCI_MON_SCO_RX_PKT);
  250. else
  251. opcode = cpu_to_le16(HCI_MON_SCO_TX_PKT);
  252. break;
  253. case HCI_DIAG_PKT:
  254. opcode = cpu_to_le16(HCI_MON_VENDOR_DIAG);
  255. break;
  256. default:
  257. return;
  258. }
  259. /* Create a private copy with headroom */
  260. skb_copy = __pskb_copy_fclone(skb, HCI_MON_HDR_SIZE, GFP_ATOMIC, true);
  261. if (!skb_copy)
  262. return;
  263. /* Put header before the data */
  264. hdr = (void *)skb_push(skb_copy, HCI_MON_HDR_SIZE);
  265. hdr->opcode = opcode;
  266. hdr->index = cpu_to_le16(hdev->id);
  267. hdr->len = cpu_to_le16(skb->len);
  268. hci_send_to_channel(HCI_CHANNEL_MONITOR, skb_copy,
  269. HCI_SOCK_TRUSTED, NULL);
  270. kfree_skb(skb_copy);
  271. }
  272. void hci_send_monitor_ctrl_event(struct hci_dev *hdev, u16 event,
  273. void *data, u16 data_len, ktime_t tstamp,
  274. int flag, struct sock *skip_sk)
  275. {
  276. struct sock *sk;
  277. __le16 index;
  278. if (hdev)
  279. index = cpu_to_le16(hdev->id);
  280. else
  281. index = cpu_to_le16(MGMT_INDEX_NONE);
  282. read_lock(&hci_sk_list.lock);
  283. sk_for_each(sk, &hci_sk_list.head) {
  284. struct hci_mon_hdr *hdr;
  285. struct sk_buff *skb;
  286. if (hci_pi(sk)->channel != HCI_CHANNEL_CONTROL)
  287. continue;
  288. /* Ignore socket without the flag set */
  289. if (!hci_sock_test_flag(sk, flag))
  290. continue;
  291. /* Skip the original socket */
  292. if (sk == skip_sk)
  293. continue;
  294. skb = bt_skb_alloc(6 + data_len, GFP_ATOMIC);
  295. if (!skb)
  296. continue;
  297. put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
  298. put_unaligned_le16(event, skb_put(skb, 2));
  299. if (data)
  300. memcpy(skb_put(skb, data_len), data, data_len);
  301. skb->tstamp = tstamp;
  302. hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
  303. hdr->opcode = cpu_to_le16(HCI_MON_CTRL_EVENT);
  304. hdr->index = index;
  305. hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
  306. hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
  307. HCI_SOCK_TRUSTED, NULL);
  308. kfree_skb(skb);
  309. }
  310. read_unlock(&hci_sk_list.lock);
  311. }
  312. static struct sk_buff *create_monitor_event(struct hci_dev *hdev, int event)
  313. {
  314. struct hci_mon_hdr *hdr;
  315. struct hci_mon_new_index *ni;
  316. struct hci_mon_index_info *ii;
  317. struct sk_buff *skb;
  318. __le16 opcode;
  319. switch (event) {
  320. case HCI_DEV_REG:
  321. skb = bt_skb_alloc(HCI_MON_NEW_INDEX_SIZE, GFP_ATOMIC);
  322. if (!skb)
  323. return NULL;
  324. ni = (void *)skb_put(skb, HCI_MON_NEW_INDEX_SIZE);
  325. ni->type = hdev->dev_type;
  326. ni->bus = hdev->bus;
  327. bacpy(&ni->bdaddr, &hdev->bdaddr);
  328. memcpy(ni->name, hdev->name, 8);
  329. opcode = cpu_to_le16(HCI_MON_NEW_INDEX);
  330. break;
  331. case HCI_DEV_UNREG:
  332. skb = bt_skb_alloc(0, GFP_ATOMIC);
  333. if (!skb)
  334. return NULL;
  335. opcode = cpu_to_le16(HCI_MON_DEL_INDEX);
  336. break;
  337. case HCI_DEV_SETUP:
  338. if (hdev->manufacturer == 0xffff)
  339. return NULL;
  340. /* fall through */
  341. case HCI_DEV_UP:
  342. skb = bt_skb_alloc(HCI_MON_INDEX_INFO_SIZE, GFP_ATOMIC);
  343. if (!skb)
  344. return NULL;
  345. ii = (void *)skb_put(skb, HCI_MON_INDEX_INFO_SIZE);
  346. bacpy(&ii->bdaddr, &hdev->bdaddr);
  347. ii->manufacturer = cpu_to_le16(hdev->manufacturer);
  348. opcode = cpu_to_le16(HCI_MON_INDEX_INFO);
  349. break;
  350. case HCI_DEV_OPEN:
  351. skb = bt_skb_alloc(0, GFP_ATOMIC);
  352. if (!skb)
  353. return NULL;
  354. opcode = cpu_to_le16(HCI_MON_OPEN_INDEX);
  355. break;
  356. case HCI_DEV_CLOSE:
  357. skb = bt_skb_alloc(0, GFP_ATOMIC);
  358. if (!skb)
  359. return NULL;
  360. opcode = cpu_to_le16(HCI_MON_CLOSE_INDEX);
  361. break;
  362. default:
  363. return NULL;
  364. }
  365. __net_timestamp(skb);
  366. hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
  367. hdr->opcode = opcode;
  368. hdr->index = cpu_to_le16(hdev->id);
  369. hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
  370. return skb;
  371. }
  372. static struct sk_buff *create_monitor_ctrl_open(struct sock *sk)
  373. {
  374. struct hci_mon_hdr *hdr;
  375. struct sk_buff *skb;
  376. u16 format;
  377. u8 ver[3];
  378. u32 flags;
  379. /* No message needed when cookie is not present */
  380. if (!hci_pi(sk)->cookie)
  381. return NULL;
  382. switch (hci_pi(sk)->channel) {
  383. case HCI_CHANNEL_RAW:
  384. format = 0x0000;
  385. ver[0] = BT_SUBSYS_VERSION;
  386. put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1);
  387. break;
  388. case HCI_CHANNEL_USER:
  389. format = 0x0001;
  390. ver[0] = BT_SUBSYS_VERSION;
  391. put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1);
  392. break;
  393. case HCI_CHANNEL_CONTROL:
  394. format = 0x0002;
  395. mgmt_fill_version_info(ver);
  396. break;
  397. default:
  398. /* No message for unsupported format */
  399. return NULL;
  400. }
  401. skb = bt_skb_alloc(14 + TASK_COMM_LEN , GFP_ATOMIC);
  402. if (!skb)
  403. return NULL;
  404. flags = hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) ? 0x1 : 0x0;
  405. put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
  406. put_unaligned_le16(format, skb_put(skb, 2));
  407. memcpy(skb_put(skb, sizeof(ver)), ver, sizeof(ver));
  408. put_unaligned_le32(flags, skb_put(skb, 4));
  409. *skb_put(skb, 1) = TASK_COMM_LEN;
  410. memcpy(skb_put(skb, TASK_COMM_LEN), hci_pi(sk)->comm, TASK_COMM_LEN);
  411. __net_timestamp(skb);
  412. hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
  413. hdr->opcode = cpu_to_le16(HCI_MON_CTRL_OPEN);
  414. if (hci_pi(sk)->hdev)
  415. hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id);
  416. else
  417. hdr->index = cpu_to_le16(HCI_DEV_NONE);
  418. hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
  419. return skb;
  420. }
  421. static struct sk_buff *create_monitor_ctrl_close(struct sock *sk)
  422. {
  423. struct hci_mon_hdr *hdr;
  424. struct sk_buff *skb;
  425. /* No message needed when cookie is not present */
  426. if (!hci_pi(sk)->cookie)
  427. return NULL;
  428. switch (hci_pi(sk)->channel) {
  429. case HCI_CHANNEL_RAW:
  430. case HCI_CHANNEL_USER:
  431. case HCI_CHANNEL_CONTROL:
  432. break;
  433. default:
  434. /* No message for unsupported format */
  435. return NULL;
  436. }
  437. skb = bt_skb_alloc(4, GFP_ATOMIC);
  438. if (!skb)
  439. return NULL;
  440. put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
  441. __net_timestamp(skb);
  442. hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
  443. hdr->opcode = cpu_to_le16(HCI_MON_CTRL_CLOSE);
  444. if (hci_pi(sk)->hdev)
  445. hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id);
  446. else
  447. hdr->index = cpu_to_le16(HCI_DEV_NONE);
  448. hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
  449. return skb;
  450. }
  451. static struct sk_buff *create_monitor_ctrl_command(struct sock *sk, u16 index,
  452. u16 opcode, u16 len,
  453. const void *buf)
  454. {
  455. struct hci_mon_hdr *hdr;
  456. struct sk_buff *skb;
  457. skb = bt_skb_alloc(6 + len, GFP_ATOMIC);
  458. if (!skb)
  459. return NULL;
  460. put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
  461. put_unaligned_le16(opcode, skb_put(skb, 2));
  462. if (buf)
  463. memcpy(skb_put(skb, len), buf, len);
  464. __net_timestamp(skb);
  465. hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
  466. hdr->opcode = cpu_to_le16(HCI_MON_CTRL_COMMAND);
  467. hdr->index = cpu_to_le16(index);
  468. hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
  469. return skb;
  470. }
  471. static void __printf(2, 3)
  472. send_monitor_note(struct sock *sk, const char *fmt, ...)
  473. {
  474. size_t len;
  475. struct hci_mon_hdr *hdr;
  476. struct sk_buff *skb;
  477. va_list args;
  478. va_start(args, fmt);
  479. len = vsnprintf(NULL, 0, fmt, args);
  480. va_end(args);
  481. skb = bt_skb_alloc(len + 1, GFP_ATOMIC);
  482. if (!skb)
  483. return;
  484. va_start(args, fmt);
  485. vsprintf(skb_put(skb, len), fmt, args);
  486. *skb_put(skb, 1) = 0;
  487. va_end(args);
  488. __net_timestamp(skb);
  489. hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
  490. hdr->opcode = cpu_to_le16(HCI_MON_SYSTEM_NOTE);
  491. hdr->index = cpu_to_le16(HCI_DEV_NONE);
  492. hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
  493. if (sock_queue_rcv_skb(sk, skb))
  494. kfree_skb(skb);
  495. }
  496. static void send_monitor_replay(struct sock *sk)
  497. {
  498. struct hci_dev *hdev;
  499. read_lock(&hci_dev_list_lock);
  500. list_for_each_entry(hdev, &hci_dev_list, list) {
  501. struct sk_buff *skb;
  502. skb = create_monitor_event(hdev, HCI_DEV_REG);
  503. if (!skb)
  504. continue;
  505. if (sock_queue_rcv_skb(sk, skb))
  506. kfree_skb(skb);
  507. if (!test_bit(HCI_RUNNING, &hdev->flags))
  508. continue;
  509. skb = create_monitor_event(hdev, HCI_DEV_OPEN);
  510. if (!skb)
  511. continue;
  512. if (sock_queue_rcv_skb(sk, skb))
  513. kfree_skb(skb);
  514. if (test_bit(HCI_UP, &hdev->flags))
  515. skb = create_monitor_event(hdev, HCI_DEV_UP);
  516. else if (hci_dev_test_flag(hdev, HCI_SETUP))
  517. skb = create_monitor_event(hdev, HCI_DEV_SETUP);
  518. else
  519. skb = NULL;
  520. if (skb) {
  521. if (sock_queue_rcv_skb(sk, skb))
  522. kfree_skb(skb);
  523. }
  524. }
  525. read_unlock(&hci_dev_list_lock);
  526. }
  527. static void send_monitor_control_replay(struct sock *mon_sk)
  528. {
  529. struct sock *sk;
  530. read_lock(&hci_sk_list.lock);
  531. sk_for_each(sk, &hci_sk_list.head) {
  532. struct sk_buff *skb;
  533. skb = create_monitor_ctrl_open(sk);
  534. if (!skb)
  535. continue;
  536. if (sock_queue_rcv_skb(mon_sk, skb))
  537. kfree_skb(skb);
  538. }
  539. read_unlock(&hci_sk_list.lock);
  540. }
  541. /* Generate internal stack event */
  542. static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
  543. {
  544. struct hci_event_hdr *hdr;
  545. struct hci_ev_stack_internal *ev;
  546. struct sk_buff *skb;
  547. skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
  548. if (!skb)
  549. return;
  550. hdr = (void *)skb_put(skb, HCI_EVENT_HDR_SIZE);
  551. hdr->evt = HCI_EV_STACK_INTERNAL;
  552. hdr->plen = sizeof(*ev) + dlen;
  553. ev = (void *)skb_put(skb, sizeof(*ev) + dlen);
  554. ev->type = type;
  555. memcpy(ev->data, data, dlen);
  556. bt_cb(skb)->incoming = 1;
  557. __net_timestamp(skb);
  558. hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
  559. hci_send_to_sock(hdev, skb);
  560. kfree_skb(skb);
  561. }
  562. void hci_sock_dev_event(struct hci_dev *hdev, int event)
  563. {
  564. BT_DBG("hdev %s event %d", hdev->name, event);
  565. if (atomic_read(&monitor_promisc)) {
  566. struct sk_buff *skb;
  567. /* Send event to monitor */
  568. skb = create_monitor_event(hdev, event);
  569. if (skb) {
  570. hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
  571. HCI_SOCK_TRUSTED, NULL);
  572. kfree_skb(skb);
  573. }
  574. }
  575. if (event <= HCI_DEV_DOWN) {
  576. struct hci_ev_si_device ev;
  577. /* Send event to sockets */
  578. ev.event = event;
  579. ev.dev_id = hdev->id;
  580. hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev);
  581. }
  582. if (event == HCI_DEV_UNREG) {
  583. struct sock *sk;
  584. /* Detach sockets from device */
  585. read_lock(&hci_sk_list.lock);
  586. sk_for_each(sk, &hci_sk_list.head) {
  587. bh_lock_sock_nested(sk);
  588. if (hci_pi(sk)->hdev == hdev) {
  589. hci_pi(sk)->hdev = NULL;
  590. sk->sk_err = EPIPE;
  591. sk->sk_state = BT_OPEN;
  592. sk->sk_state_change(sk);
  593. hci_dev_put(hdev);
  594. }
  595. bh_unlock_sock(sk);
  596. }
  597. read_unlock(&hci_sk_list.lock);
  598. }
  599. }
  600. static struct hci_mgmt_chan *__hci_mgmt_chan_find(unsigned short channel)
  601. {
  602. struct hci_mgmt_chan *c;
  603. list_for_each_entry(c, &mgmt_chan_list, list) {
  604. if (c->channel == channel)
  605. return c;
  606. }
  607. return NULL;
  608. }
  609. static struct hci_mgmt_chan *hci_mgmt_chan_find(unsigned short channel)
  610. {
  611. struct hci_mgmt_chan *c;
  612. mutex_lock(&mgmt_chan_list_lock);
  613. c = __hci_mgmt_chan_find(channel);
  614. mutex_unlock(&mgmt_chan_list_lock);
  615. return c;
  616. }
  617. int hci_mgmt_chan_register(struct hci_mgmt_chan *c)
  618. {
  619. if (c->channel < HCI_CHANNEL_CONTROL)
  620. return -EINVAL;
  621. mutex_lock(&mgmt_chan_list_lock);
  622. if (__hci_mgmt_chan_find(c->channel)) {
  623. mutex_unlock(&mgmt_chan_list_lock);
  624. return -EALREADY;
  625. }
  626. list_add_tail(&c->list, &mgmt_chan_list);
  627. mutex_unlock(&mgmt_chan_list_lock);
  628. return 0;
  629. }
  630. EXPORT_SYMBOL(hci_mgmt_chan_register);
  631. void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c)
  632. {
  633. mutex_lock(&mgmt_chan_list_lock);
  634. list_del(&c->list);
  635. mutex_unlock(&mgmt_chan_list_lock);
  636. }
  637. EXPORT_SYMBOL(hci_mgmt_chan_unregister);
  638. static int hci_sock_release(struct socket *sock)
  639. {
  640. struct sock *sk = sock->sk;
  641. struct hci_dev *hdev;
  642. struct sk_buff *skb;
  643. BT_DBG("sock %p sk %p", sock, sk);
  644. if (!sk)
  645. return 0;
  646. hdev = hci_pi(sk)->hdev;
  647. switch (hci_pi(sk)->channel) {
  648. case HCI_CHANNEL_MONITOR:
  649. atomic_dec(&monitor_promisc);
  650. break;
  651. case HCI_CHANNEL_RAW:
  652. case HCI_CHANNEL_USER:
  653. case HCI_CHANNEL_CONTROL:
  654. /* Send event to monitor */
  655. skb = create_monitor_ctrl_close(sk);
  656. if (skb) {
  657. hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
  658. HCI_SOCK_TRUSTED, NULL);
  659. kfree_skb(skb);
  660. }
  661. hci_sock_free_cookie(sk);
  662. break;
  663. }
  664. bt_sock_unlink(&hci_sk_list, sk);
  665. if (hdev) {
  666. if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
  667. /* When releasing an user channel exclusive access,
  668. * call hci_dev_do_close directly instead of calling
  669. * hci_dev_close to ensure the exclusive access will
  670. * be released and the controller brought back down.
  671. *
  672. * The checking of HCI_AUTO_OFF is not needed in this
  673. * case since it will have been cleared already when
  674. * opening the user channel.
  675. */
  676. hci_dev_do_close(hdev);
  677. hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
  678. mgmt_index_added(hdev);
  679. }
  680. atomic_dec(&hdev->promisc);
  681. hci_dev_put(hdev);
  682. }
  683. sock_orphan(sk);
  684. skb_queue_purge(&sk->sk_receive_queue);
  685. skb_queue_purge(&sk->sk_write_queue);
  686. sock_put(sk);
  687. return 0;
  688. }
  689. static int hci_sock_blacklist_add(struct hci_dev *hdev, void __user *arg)
  690. {
  691. bdaddr_t bdaddr;
  692. int err;
  693. if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
  694. return -EFAULT;
  695. hci_dev_lock(hdev);
  696. err = hci_bdaddr_list_add(&hdev->blacklist, &bdaddr, BDADDR_BREDR);
  697. hci_dev_unlock(hdev);
  698. return err;
  699. }
  700. static int hci_sock_blacklist_del(struct hci_dev *hdev, void __user *arg)
  701. {
  702. bdaddr_t bdaddr;
  703. int err;
  704. if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
  705. return -EFAULT;
  706. hci_dev_lock(hdev);
  707. err = hci_bdaddr_list_del(&hdev->blacklist, &bdaddr, BDADDR_BREDR);
  708. hci_dev_unlock(hdev);
  709. return err;
  710. }
  711. /* Ioctls that require bound socket */
  712. static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd,
  713. unsigned long arg)
  714. {
  715. struct hci_dev *hdev = hci_pi(sk)->hdev;
  716. if (!hdev)
  717. return -EBADFD;
  718. if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
  719. return -EBUSY;
  720. if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
  721. return -EOPNOTSUPP;
  722. if (hdev->dev_type != HCI_PRIMARY)
  723. return -EOPNOTSUPP;
  724. switch (cmd) {
  725. case HCISETRAW:
  726. if (!capable(CAP_NET_ADMIN))
  727. return -EPERM;
  728. return -EOPNOTSUPP;
  729. case HCIGETCONNINFO:
  730. return hci_get_conn_info(hdev, (void __user *)arg);
  731. case HCIGETAUTHINFO:
  732. return hci_get_auth_info(hdev, (void __user *)arg);
  733. case HCIBLOCKADDR:
  734. if (!capable(CAP_NET_ADMIN))
  735. return -EPERM;
  736. return hci_sock_blacklist_add(hdev, (void __user *)arg);
  737. case HCIUNBLOCKADDR:
  738. if (!capable(CAP_NET_ADMIN))
  739. return -EPERM;
  740. return hci_sock_blacklist_del(hdev, (void __user *)arg);
  741. }
  742. return -ENOIOCTLCMD;
  743. }
  744. static int hci_sock_ioctl(struct socket *sock, unsigned int cmd,
  745. unsigned long arg)
  746. {
  747. void __user *argp = (void __user *)arg;
  748. struct sock *sk = sock->sk;
  749. int err;
  750. BT_DBG("cmd %x arg %lx", cmd, arg);
  751. lock_sock(sk);
  752. if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
  753. err = -EBADFD;
  754. goto done;
  755. }
  756. /* When calling an ioctl on an unbound raw socket, then ensure
  757. * that the monitor gets informed. Ensure that the resulting event
  758. * is only send once by checking if the cookie exists or not. The
  759. * socket cookie will be only ever generated once for the lifetime
  760. * of a given socket.
  761. */
  762. if (hci_sock_gen_cookie(sk)) {
  763. struct sk_buff *skb;
  764. if (capable(CAP_NET_ADMIN))
  765. hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
  766. /* Send event to monitor */
  767. skb = create_monitor_ctrl_open(sk);
  768. if (skb) {
  769. hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
  770. HCI_SOCK_TRUSTED, NULL);
  771. kfree_skb(skb);
  772. }
  773. }
  774. release_sock(sk);
  775. switch (cmd) {
  776. case HCIGETDEVLIST:
  777. return hci_get_dev_list(argp);
  778. case HCIGETDEVINFO:
  779. return hci_get_dev_info(argp);
  780. case HCIGETCONNLIST:
  781. return hci_get_conn_list(argp);
  782. case HCIDEVUP:
  783. if (!capable(CAP_NET_ADMIN))
  784. return -EPERM;
  785. return hci_dev_open(arg);
  786. case HCIDEVDOWN:
  787. if (!capable(CAP_NET_ADMIN))
  788. return -EPERM;
  789. return hci_dev_close(arg);
  790. case HCIDEVRESET:
  791. if (!capable(CAP_NET_ADMIN))
  792. return -EPERM;
  793. return hci_dev_reset(arg);
  794. case HCIDEVRESTAT:
  795. if (!capable(CAP_NET_ADMIN))
  796. return -EPERM;
  797. return hci_dev_reset_stat(arg);
  798. case HCISETSCAN:
  799. case HCISETAUTH:
  800. case HCISETENCRYPT:
  801. case HCISETPTYPE:
  802. case HCISETLINKPOL:
  803. case HCISETLINKMODE:
  804. case HCISETACLMTU:
  805. case HCISETSCOMTU:
  806. if (!capable(CAP_NET_ADMIN))
  807. return -EPERM;
  808. return hci_dev_cmd(cmd, argp);
  809. case HCIINQUIRY:
  810. return hci_inquiry(argp);
  811. }
  812. lock_sock(sk);
  813. err = hci_sock_bound_ioctl(sk, cmd, arg);
  814. done:
  815. release_sock(sk);
  816. return err;
  817. }
  818. static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
  819. int addr_len)
  820. {
  821. struct sockaddr_hci haddr;
  822. struct sock *sk = sock->sk;
  823. struct hci_dev *hdev = NULL;
  824. struct sk_buff *skb;
  825. int len, err = 0;
  826. BT_DBG("sock %p sk %p", sock, sk);
  827. if (!addr)
  828. return -EINVAL;
  829. memset(&haddr, 0, sizeof(haddr));
  830. len = min_t(unsigned int, sizeof(haddr), addr_len);
  831. memcpy(&haddr, addr, len);
  832. if (haddr.hci_family != AF_BLUETOOTH)
  833. return -EINVAL;
  834. lock_sock(sk);
  835. if (sk->sk_state == BT_BOUND) {
  836. err = -EALREADY;
  837. goto done;
  838. }
  839. switch (haddr.hci_channel) {
  840. case HCI_CHANNEL_RAW:
  841. if (hci_pi(sk)->hdev) {
  842. err = -EALREADY;
  843. goto done;
  844. }
  845. if (haddr.hci_dev != HCI_DEV_NONE) {
  846. hdev = hci_dev_get(haddr.hci_dev);
  847. if (!hdev) {
  848. err = -ENODEV;
  849. goto done;
  850. }
  851. atomic_inc(&hdev->promisc);
  852. }
  853. hci_pi(sk)->channel = haddr.hci_channel;
  854. if (!hci_sock_gen_cookie(sk)) {
  855. /* In the case when a cookie has already been assigned,
  856. * then there has been already an ioctl issued against
  857. * an unbound socket and with that triggerd an open
  858. * notification. Send a close notification first to
  859. * allow the state transition to bounded.
  860. */
  861. skb = create_monitor_ctrl_close(sk);
  862. if (skb) {
  863. hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
  864. HCI_SOCK_TRUSTED, NULL);
  865. kfree_skb(skb);
  866. }
  867. }
  868. if (capable(CAP_NET_ADMIN))
  869. hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
  870. hci_pi(sk)->hdev = hdev;
  871. /* Send event to monitor */
  872. skb = create_monitor_ctrl_open(sk);
  873. if (skb) {
  874. hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
  875. HCI_SOCK_TRUSTED, NULL);
  876. kfree_skb(skb);
  877. }
  878. break;
  879. case HCI_CHANNEL_USER:
  880. if (hci_pi(sk)->hdev) {
  881. err = -EALREADY;
  882. goto done;
  883. }
  884. if (haddr.hci_dev == HCI_DEV_NONE) {
  885. err = -EINVAL;
  886. goto done;
  887. }
  888. if (!capable(CAP_NET_ADMIN)) {
  889. err = -EPERM;
  890. goto done;
  891. }
  892. hdev = hci_dev_get(haddr.hci_dev);
  893. if (!hdev) {
  894. err = -ENODEV;
  895. goto done;
  896. }
  897. if (test_bit(HCI_INIT, &hdev->flags) ||
  898. hci_dev_test_flag(hdev, HCI_SETUP) ||
  899. hci_dev_test_flag(hdev, HCI_CONFIG) ||
  900. (!hci_dev_test_flag(hdev, HCI_AUTO_OFF) &&
  901. test_bit(HCI_UP, &hdev->flags))) {
  902. err = -EBUSY;
  903. hci_dev_put(hdev);
  904. goto done;
  905. }
  906. if (hci_dev_test_and_set_flag(hdev, HCI_USER_CHANNEL)) {
  907. err = -EUSERS;
  908. hci_dev_put(hdev);
  909. goto done;
  910. }
  911. mgmt_index_removed(hdev);
  912. err = hci_dev_open(hdev->id);
  913. if (err) {
  914. if (err == -EALREADY) {
  915. /* In case the transport is already up and
  916. * running, clear the error here.
  917. *
  918. * This can happen when opening an user
  919. * channel and HCI_AUTO_OFF grace period
  920. * is still active.
  921. */
  922. err = 0;
  923. } else {
  924. hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
  925. mgmt_index_added(hdev);
  926. hci_dev_put(hdev);
  927. goto done;
  928. }
  929. }
  930. hci_pi(sk)->channel = haddr.hci_channel;
  931. if (!hci_sock_gen_cookie(sk)) {
  932. /* In the case when a cookie has already been assigned,
  933. * this socket will transition from a raw socket into
  934. * an user channel socket. For a clean transition, send
  935. * the close notification first.
  936. */
  937. skb = create_monitor_ctrl_close(sk);
  938. if (skb) {
  939. hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
  940. HCI_SOCK_TRUSTED, NULL);
  941. kfree_skb(skb);
  942. }
  943. }
  944. /* The user channel is restricted to CAP_NET_ADMIN
  945. * capabilities and with that implicitly trusted.
  946. */
  947. hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
  948. hci_pi(sk)->hdev = hdev;
  949. /* Send event to monitor */
  950. skb = create_monitor_ctrl_open(sk);
  951. if (skb) {
  952. hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
  953. HCI_SOCK_TRUSTED, NULL);
  954. kfree_skb(skb);
  955. }
  956. atomic_inc(&hdev->promisc);
  957. break;
  958. case HCI_CHANNEL_MONITOR:
  959. if (haddr.hci_dev != HCI_DEV_NONE) {
  960. err = -EINVAL;
  961. goto done;
  962. }
  963. if (!capable(CAP_NET_RAW)) {
  964. err = -EPERM;
  965. goto done;
  966. }
  967. hci_pi(sk)->channel = haddr.hci_channel;
  968. /* The monitor interface is restricted to CAP_NET_RAW
  969. * capabilities and with that implicitly trusted.
  970. */
  971. hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
  972. send_monitor_note(sk, "Linux version %s (%s)",
  973. init_utsname()->release,
  974. init_utsname()->machine);
  975. send_monitor_note(sk, "Bluetooth subsystem version %u.%u",
  976. BT_SUBSYS_VERSION, BT_SUBSYS_REVISION);
  977. send_monitor_replay(sk);
  978. send_monitor_control_replay(sk);
  979. atomic_inc(&monitor_promisc);
  980. break;
  981. case HCI_CHANNEL_LOGGING:
  982. if (haddr.hci_dev != HCI_DEV_NONE) {
  983. err = -EINVAL;
  984. goto done;
  985. }
  986. if (!capable(CAP_NET_ADMIN)) {
  987. err = -EPERM;
  988. goto done;
  989. }
  990. hci_pi(sk)->channel = haddr.hci_channel;
  991. break;
  992. default:
  993. if (!hci_mgmt_chan_find(haddr.hci_channel)) {
  994. err = -EINVAL;
  995. goto done;
  996. }
  997. if (haddr.hci_dev != HCI_DEV_NONE) {
  998. err = -EINVAL;
  999. goto done;
  1000. }
  1001. /* Users with CAP_NET_ADMIN capabilities are allowed
  1002. * access to all management commands and events. For
  1003. * untrusted users the interface is restricted and
  1004. * also only untrusted events are sent.
  1005. */
  1006. if (capable(CAP_NET_ADMIN))
  1007. hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
  1008. hci_pi(sk)->channel = haddr.hci_channel;
  1009. /* At the moment the index and unconfigured index events
  1010. * are enabled unconditionally. Setting them on each
  1011. * socket when binding keeps this functionality. They
  1012. * however might be cleared later and then sending of these
  1013. * events will be disabled, but that is then intentional.
  1014. *
  1015. * This also enables generic events that are safe to be
  1016. * received by untrusted users. Example for such events
  1017. * are changes to settings, class of device, name etc.
  1018. */
  1019. if (hci_pi(sk)->channel == HCI_CHANNEL_CONTROL) {
  1020. if (!hci_sock_gen_cookie(sk)) {
  1021. /* In the case when a cookie has already been
  1022. * assigned, this socket will transtion from
  1023. * a raw socket into a control socket. To
  1024. * allow for a clean transtion, send the
  1025. * close notification first.
  1026. */
  1027. skb = create_monitor_ctrl_close(sk);
  1028. if (skb) {
  1029. hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
  1030. HCI_SOCK_TRUSTED, NULL);
  1031. kfree_skb(skb);
  1032. }
  1033. }
  1034. /* Send event to monitor */
  1035. skb = create_monitor_ctrl_open(sk);
  1036. if (skb) {
  1037. hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
  1038. HCI_SOCK_TRUSTED, NULL);
  1039. kfree_skb(skb);
  1040. }
  1041. hci_sock_set_flag(sk, HCI_MGMT_INDEX_EVENTS);
  1042. hci_sock_set_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
  1043. hci_sock_set_flag(sk, HCI_MGMT_OPTION_EVENTS);
  1044. hci_sock_set_flag(sk, HCI_MGMT_SETTING_EVENTS);
  1045. hci_sock_set_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS);
  1046. hci_sock_set_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS);
  1047. }
  1048. break;
  1049. }
  1050. sk->sk_state = BT_BOUND;
  1051. done:
  1052. release_sock(sk);
  1053. return err;
  1054. }
  1055. static int hci_sock_getname(struct socket *sock, struct sockaddr *addr,
  1056. int *addr_len, int peer)
  1057. {
  1058. struct sockaddr_hci *haddr = (struct sockaddr_hci *)addr;
  1059. struct sock *sk = sock->sk;
  1060. struct hci_dev *hdev;
  1061. int err = 0;
  1062. BT_DBG("sock %p sk %p", sock, sk);
  1063. if (peer)
  1064. return -EOPNOTSUPP;
  1065. lock_sock(sk);
  1066. hdev = hci_pi(sk)->hdev;
  1067. if (!hdev) {
  1068. err = -EBADFD;
  1069. goto done;
  1070. }
  1071. *addr_len = sizeof(*haddr);
  1072. haddr->hci_family = AF_BLUETOOTH;
  1073. haddr->hci_dev = hdev->id;
  1074. haddr->hci_channel= hci_pi(sk)->channel;
  1075. done:
  1076. release_sock(sk);
  1077. return err;
  1078. }
  1079. static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg,
  1080. struct sk_buff *skb)
  1081. {
  1082. __u32 mask = hci_pi(sk)->cmsg_mask;
  1083. if (mask & HCI_CMSG_DIR) {
  1084. int incoming = bt_cb(skb)->incoming;
  1085. put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming),
  1086. &incoming);
  1087. }
  1088. if (mask & HCI_CMSG_TSTAMP) {
  1089. #ifdef CONFIG_COMPAT
  1090. struct compat_timeval ctv;
  1091. #endif
  1092. struct timeval tv;
  1093. void *data;
  1094. int len;
  1095. skb_get_timestamp(skb, &tv);
  1096. data = &tv;
  1097. len = sizeof(tv);
  1098. #ifdef CONFIG_COMPAT
  1099. if (!COMPAT_USE_64BIT_TIME &&
  1100. (msg->msg_flags & MSG_CMSG_COMPAT)) {
  1101. ctv.tv_sec = tv.tv_sec;
  1102. ctv.tv_usec = tv.tv_usec;
  1103. data = &ctv;
  1104. len = sizeof(ctv);
  1105. }
  1106. #endif
  1107. put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data);
  1108. }
  1109. }
  1110. static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg,
  1111. size_t len, int flags)
  1112. {
  1113. int noblock = flags & MSG_DONTWAIT;
  1114. struct sock *sk = sock->sk;
  1115. struct sk_buff *skb;
  1116. int copied, err;
  1117. unsigned int skblen;
  1118. BT_DBG("sock %p, sk %p", sock, sk);
  1119. if (flags & MSG_OOB)
  1120. return -EOPNOTSUPP;
  1121. if (hci_pi(sk)->channel == HCI_CHANNEL_LOGGING)
  1122. return -EOPNOTSUPP;
  1123. if (sk->sk_state == BT_CLOSED)
  1124. return 0;
  1125. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1126. if (!skb)
  1127. return err;
  1128. skblen = skb->len;
  1129. copied = skb->len;
  1130. if (len < copied) {
  1131. msg->msg_flags |= MSG_TRUNC;
  1132. copied = len;
  1133. }
  1134. skb_reset_transport_header(skb);
  1135. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  1136. switch (hci_pi(sk)->channel) {
  1137. case HCI_CHANNEL_RAW:
  1138. hci_sock_cmsg(sk, msg, skb);
  1139. break;
  1140. case HCI_CHANNEL_USER:
  1141. case HCI_CHANNEL_MONITOR:
  1142. sock_recv_timestamp(msg, sk, skb);
  1143. break;
  1144. default:
  1145. if (hci_mgmt_chan_find(hci_pi(sk)->channel))
  1146. sock_recv_timestamp(msg, sk, skb);
  1147. break;
  1148. }
  1149. skb_free_datagram(sk, skb);
  1150. if (flags & MSG_TRUNC)
  1151. copied = skblen;
  1152. return err ? : copied;
  1153. }
  1154. static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk,
  1155. struct msghdr *msg, size_t msglen)
  1156. {
  1157. void *buf;
  1158. u8 *cp;
  1159. struct mgmt_hdr *hdr;
  1160. u16 opcode, index, len;
  1161. struct hci_dev *hdev = NULL;
  1162. const struct hci_mgmt_handler *handler;
  1163. bool var_len, no_hdev;
  1164. int err;
  1165. BT_DBG("got %zu bytes", msglen);
  1166. if (msglen < sizeof(*hdr))
  1167. return -EINVAL;
  1168. buf = kmalloc(msglen, GFP_KERNEL);
  1169. if (!buf)
  1170. return -ENOMEM;
  1171. if (memcpy_from_msg(buf, msg, msglen)) {
  1172. err = -EFAULT;
  1173. goto done;
  1174. }
  1175. hdr = buf;
  1176. opcode = __le16_to_cpu(hdr->opcode);
  1177. index = __le16_to_cpu(hdr->index);
  1178. len = __le16_to_cpu(hdr->len);
  1179. if (len != msglen - sizeof(*hdr)) {
  1180. err = -EINVAL;
  1181. goto done;
  1182. }
  1183. if (chan->channel == HCI_CHANNEL_CONTROL) {
  1184. struct sk_buff *skb;
  1185. /* Send event to monitor */
  1186. skb = create_monitor_ctrl_command(sk, index, opcode, len,
  1187. buf + sizeof(*hdr));
  1188. if (skb) {
  1189. hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
  1190. HCI_SOCK_TRUSTED, NULL);
  1191. kfree_skb(skb);
  1192. }
  1193. }
  1194. if (opcode >= chan->handler_count ||
  1195. chan->handlers[opcode].func == NULL) {
  1196. BT_DBG("Unknown op %u", opcode);
  1197. err = mgmt_cmd_status(sk, index, opcode,
  1198. MGMT_STATUS_UNKNOWN_COMMAND);
  1199. goto done;
  1200. }
  1201. handler = &chan->handlers[opcode];
  1202. if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) &&
  1203. !(handler->flags & HCI_MGMT_UNTRUSTED)) {
  1204. err = mgmt_cmd_status(sk, index, opcode,
  1205. MGMT_STATUS_PERMISSION_DENIED);
  1206. goto done;
  1207. }
  1208. if (index != MGMT_INDEX_NONE) {
  1209. hdev = hci_dev_get(index);
  1210. if (!hdev) {
  1211. err = mgmt_cmd_status(sk, index, opcode,
  1212. MGMT_STATUS_INVALID_INDEX);
  1213. goto done;
  1214. }
  1215. if (hci_dev_test_flag(hdev, HCI_SETUP) ||
  1216. hci_dev_test_flag(hdev, HCI_CONFIG) ||
  1217. hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
  1218. err = mgmt_cmd_status(sk, index, opcode,
  1219. MGMT_STATUS_INVALID_INDEX);
  1220. goto done;
  1221. }
  1222. if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
  1223. !(handler->flags & HCI_MGMT_UNCONFIGURED)) {
  1224. err = mgmt_cmd_status(sk, index, opcode,
  1225. MGMT_STATUS_INVALID_INDEX);
  1226. goto done;
  1227. }
  1228. }
  1229. no_hdev = (handler->flags & HCI_MGMT_NO_HDEV);
  1230. if (no_hdev != !hdev) {
  1231. err = mgmt_cmd_status(sk, index, opcode,
  1232. MGMT_STATUS_INVALID_INDEX);
  1233. goto done;
  1234. }
  1235. var_len = (handler->flags & HCI_MGMT_VAR_LEN);
  1236. if ((var_len && len < handler->data_len) ||
  1237. (!var_len && len != handler->data_len)) {
  1238. err = mgmt_cmd_status(sk, index, opcode,
  1239. MGMT_STATUS_INVALID_PARAMS);
  1240. goto done;
  1241. }
  1242. if (hdev && chan->hdev_init)
  1243. chan->hdev_init(sk, hdev);
  1244. cp = buf + sizeof(*hdr);
  1245. err = handler->func(sk, hdev, cp, len);
  1246. if (err < 0)
  1247. goto done;
  1248. err = msglen;
  1249. done:
  1250. if (hdev)
  1251. hci_dev_put(hdev);
  1252. kfree(buf);
  1253. return err;
  1254. }
  1255. static int hci_logging_frame(struct sock *sk, struct msghdr *msg, int len)
  1256. {
  1257. struct hci_mon_hdr *hdr;
  1258. struct sk_buff *skb;
  1259. struct hci_dev *hdev;
  1260. u16 index;
  1261. int err;
  1262. /* The logging frame consists at minimum of the standard header,
  1263. * the priority byte, the ident length byte and at least one string
  1264. * terminator NUL byte. Anything shorter are invalid packets.
  1265. */
  1266. if (len < sizeof(*hdr) + 3)
  1267. return -EINVAL;
  1268. skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
  1269. if (!skb)
  1270. return err;
  1271. if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
  1272. err = -EFAULT;
  1273. goto drop;
  1274. }
  1275. hdr = (void *)skb->data;
  1276. if (__le16_to_cpu(hdr->len) != len - sizeof(*hdr)) {
  1277. err = -EINVAL;
  1278. goto drop;
  1279. }
  1280. if (__le16_to_cpu(hdr->opcode) == 0x0000) {
  1281. __u8 priority = skb->data[sizeof(*hdr)];
  1282. __u8 ident_len = skb->data[sizeof(*hdr) + 1];
  1283. /* Only the priorities 0-7 are valid and with that any other
  1284. * value results in an invalid packet.
  1285. *
  1286. * The priority byte is followed by an ident length byte and
  1287. * the NUL terminated ident string. Check that the ident
  1288. * length is not overflowing the packet and also that the
  1289. * ident string itself is NUL terminated. In case the ident
  1290. * length is zero, the length value actually doubles as NUL
  1291. * terminator identifier.
  1292. *
  1293. * The message follows the ident string (if present) and
  1294. * must be NUL terminated. Otherwise it is not a valid packet.
  1295. */
  1296. if (priority > 7 || skb->data[len - 1] != 0x00 ||
  1297. ident_len > len - sizeof(*hdr) - 3 ||
  1298. skb->data[sizeof(*hdr) + ident_len + 1] != 0x00) {
  1299. err = -EINVAL;
  1300. goto drop;
  1301. }
  1302. } else {
  1303. err = -EINVAL;
  1304. goto drop;
  1305. }
  1306. index = __le16_to_cpu(hdr->index);
  1307. if (index != MGMT_INDEX_NONE) {
  1308. hdev = hci_dev_get(index);
  1309. if (!hdev) {
  1310. err = -ENODEV;
  1311. goto drop;
  1312. }
  1313. } else {
  1314. hdev = NULL;
  1315. }
  1316. hdr->opcode = cpu_to_le16(HCI_MON_USER_LOGGING);
  1317. hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, HCI_SOCK_TRUSTED, NULL);
  1318. err = len;
  1319. if (hdev)
  1320. hci_dev_put(hdev);
  1321. drop:
  1322. kfree_skb(skb);
  1323. return err;
  1324. }
  1325. static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,
  1326. size_t len)
  1327. {
  1328. struct sock *sk = sock->sk;
  1329. struct hci_mgmt_chan *chan;
  1330. struct hci_dev *hdev;
  1331. struct sk_buff *skb;
  1332. int err;
  1333. BT_DBG("sock %p sk %p", sock, sk);
  1334. if (msg->msg_flags & MSG_OOB)
  1335. return -EOPNOTSUPP;
  1336. if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE|
  1337. MSG_CMSG_COMPAT))
  1338. return -EINVAL;
  1339. if (len < 4 || len > HCI_MAX_FRAME_SIZE)
  1340. return -EINVAL;
  1341. lock_sock(sk);
  1342. switch (hci_pi(sk)->channel) {
  1343. case HCI_CHANNEL_RAW:
  1344. case HCI_CHANNEL_USER:
  1345. break;
  1346. case HCI_CHANNEL_MONITOR:
  1347. err = -EOPNOTSUPP;
  1348. goto done;
  1349. case HCI_CHANNEL_LOGGING:
  1350. err = hci_logging_frame(sk, msg, len);
  1351. goto done;
  1352. default:
  1353. mutex_lock(&mgmt_chan_list_lock);
  1354. chan = __hci_mgmt_chan_find(hci_pi(sk)->channel);
  1355. if (chan)
  1356. err = hci_mgmt_cmd(chan, sk, msg, len);
  1357. else
  1358. err = -EINVAL;
  1359. mutex_unlock(&mgmt_chan_list_lock);
  1360. goto done;
  1361. }
  1362. hdev = hci_pi(sk)->hdev;
  1363. if (!hdev) {
  1364. err = -EBADFD;
  1365. goto done;
  1366. }
  1367. if (!test_bit(HCI_UP, &hdev->flags)) {
  1368. err = -ENETDOWN;
  1369. goto done;
  1370. }
  1371. skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
  1372. if (!skb)
  1373. goto done;
  1374. if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
  1375. err = -EFAULT;
  1376. goto drop;
  1377. }
  1378. hci_skb_pkt_type(skb) = skb->data[0];
  1379. skb_pull(skb, 1);
  1380. if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
  1381. /* No permission check is needed for user channel
  1382. * since that gets enforced when binding the socket.
  1383. *
  1384. * However check that the packet type is valid.
  1385. */
  1386. if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT &&
  1387. hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
  1388. hci_skb_pkt_type(skb) != HCI_SCODATA_PKT) {
  1389. err = -EINVAL;
  1390. goto drop;
  1391. }
  1392. skb_queue_tail(&hdev->raw_q, skb);
  1393. queue_work(hdev->workqueue, &hdev->tx_work);
  1394. } else if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) {
  1395. u16 opcode = get_unaligned_le16(skb->data);
  1396. u16 ogf = hci_opcode_ogf(opcode);
  1397. u16 ocf = hci_opcode_ocf(opcode);
  1398. if (((ogf > HCI_SFLT_MAX_OGF) ||
  1399. !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
  1400. &hci_sec_filter.ocf_mask[ogf])) &&
  1401. !capable(CAP_NET_RAW)) {
  1402. err = -EPERM;
  1403. goto drop;
  1404. }
  1405. /* Since the opcode has already been extracted here, store
  1406. * a copy of the value for later use by the drivers.
  1407. */
  1408. hci_skb_opcode(skb) = opcode;
  1409. if (ogf == 0x3f) {
  1410. skb_queue_tail(&hdev->raw_q, skb);
  1411. queue_work(hdev->workqueue, &hdev->tx_work);
  1412. } else {
  1413. /* Stand-alone HCI commands must be flagged as
  1414. * single-command requests.
  1415. */
  1416. bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
  1417. skb_queue_tail(&hdev->cmd_q, skb);
  1418. queue_work(hdev->workqueue, &hdev->cmd_work);
  1419. }
  1420. } else {
  1421. if (!capable(CAP_NET_RAW)) {
  1422. err = -EPERM;
  1423. goto drop;
  1424. }
  1425. if (hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
  1426. hci_skb_pkt_type(skb) != HCI_SCODATA_PKT) {
  1427. err = -EINVAL;
  1428. goto drop;
  1429. }
  1430. skb_queue_tail(&hdev->raw_q, skb);
  1431. queue_work(hdev->workqueue, &hdev->tx_work);
  1432. }
  1433. err = len;
  1434. done:
  1435. release_sock(sk);
  1436. return err;
  1437. drop:
  1438. kfree_skb(skb);
  1439. goto done;
  1440. }
  1441. static int hci_sock_setsockopt(struct socket *sock, int level, int optname,
  1442. char __user *optval, unsigned int len)
  1443. {
  1444. struct hci_ufilter uf = { .opcode = 0 };
  1445. struct sock *sk = sock->sk;
  1446. int err = 0, opt = 0;
  1447. BT_DBG("sk %p, opt %d", sk, optname);
  1448. if (level != SOL_HCI)
  1449. return -ENOPROTOOPT;
  1450. lock_sock(sk);
  1451. if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
  1452. err = -EBADFD;
  1453. goto done;
  1454. }
  1455. switch (optname) {
  1456. case HCI_DATA_DIR:
  1457. if (get_user(opt, (int __user *)optval)) {
  1458. err = -EFAULT;
  1459. break;
  1460. }
  1461. if (opt)
  1462. hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
  1463. else
  1464. hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
  1465. break;
  1466. case HCI_TIME_STAMP:
  1467. if (get_user(opt, (int __user *)optval)) {
  1468. err = -EFAULT;
  1469. break;
  1470. }
  1471. if (opt)
  1472. hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
  1473. else
  1474. hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
  1475. break;
  1476. case HCI_FILTER:
  1477. {
  1478. struct hci_filter *f = &hci_pi(sk)->filter;
  1479. uf.type_mask = f->type_mask;
  1480. uf.opcode = f->opcode;
  1481. uf.event_mask[0] = *((u32 *) f->event_mask + 0);
  1482. uf.event_mask[1] = *((u32 *) f->event_mask + 1);
  1483. }
  1484. len = min_t(unsigned int, len, sizeof(uf));
  1485. if (copy_from_user(&uf, optval, len)) {
  1486. err = -EFAULT;
  1487. break;
  1488. }
  1489. if (!capable(CAP_NET_RAW)) {
  1490. uf.type_mask &= hci_sec_filter.type_mask;
  1491. uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0);
  1492. uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1);
  1493. }
  1494. {
  1495. struct hci_filter *f = &hci_pi(sk)->filter;
  1496. f->type_mask = uf.type_mask;
  1497. f->opcode = uf.opcode;
  1498. *((u32 *) f->event_mask + 0) = uf.event_mask[0];
  1499. *((u32 *) f->event_mask + 1) = uf.event_mask[1];
  1500. }
  1501. break;
  1502. default:
  1503. err = -ENOPROTOOPT;
  1504. break;
  1505. }
  1506. done:
  1507. release_sock(sk);
  1508. return err;
  1509. }
  1510. static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
  1511. char __user *optval, int __user *optlen)
  1512. {
  1513. struct hci_ufilter uf;
  1514. struct sock *sk = sock->sk;
  1515. int len, opt, err = 0;
  1516. BT_DBG("sk %p, opt %d", sk, optname);
  1517. if (level != SOL_HCI)
  1518. return -ENOPROTOOPT;
  1519. if (get_user(len, optlen))
  1520. return -EFAULT;
  1521. lock_sock(sk);
  1522. if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
  1523. err = -EBADFD;
  1524. goto done;
  1525. }
  1526. switch (optname) {
  1527. case HCI_DATA_DIR:
  1528. if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
  1529. opt = 1;
  1530. else
  1531. opt = 0;
  1532. if (put_user(opt, optval))
  1533. err = -EFAULT;
  1534. break;
  1535. case HCI_TIME_STAMP:
  1536. if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
  1537. opt = 1;
  1538. else
  1539. opt = 0;
  1540. if (put_user(opt, optval))
  1541. err = -EFAULT;
  1542. break;
  1543. case HCI_FILTER:
  1544. {
  1545. struct hci_filter *f = &hci_pi(sk)->filter;
  1546. memset(&uf, 0, sizeof(uf));
  1547. uf.type_mask = f->type_mask;
  1548. uf.opcode = f->opcode;
  1549. uf.event_mask[0] = *((u32 *) f->event_mask + 0);
  1550. uf.event_mask[1] = *((u32 *) f->event_mask + 1);
  1551. }
  1552. len = min_t(unsigned int, len, sizeof(uf));
  1553. if (copy_to_user(optval, &uf, len))
  1554. err = -EFAULT;
  1555. break;
  1556. default:
  1557. err = -ENOPROTOOPT;
  1558. break;
  1559. }
  1560. done:
  1561. release_sock(sk);
  1562. return err;
  1563. }
  1564. static const struct proto_ops hci_sock_ops = {
  1565. .family = PF_BLUETOOTH,
  1566. .owner = THIS_MODULE,
  1567. .release = hci_sock_release,
  1568. .bind = hci_sock_bind,
  1569. .getname = hci_sock_getname,
  1570. .sendmsg = hci_sock_sendmsg,
  1571. .recvmsg = hci_sock_recvmsg,
  1572. .ioctl = hci_sock_ioctl,
  1573. .poll = datagram_poll,
  1574. .listen = sock_no_listen,
  1575. .shutdown = sock_no_shutdown,
  1576. .setsockopt = hci_sock_setsockopt,
  1577. .getsockopt = hci_sock_getsockopt,
  1578. .connect = sock_no_connect,
  1579. .socketpair = sock_no_socketpair,
  1580. .accept = sock_no_accept,
  1581. .mmap = sock_no_mmap
  1582. };
  1583. static struct proto hci_sk_proto = {
  1584. .name = "HCI",
  1585. .owner = THIS_MODULE,
  1586. .obj_size = sizeof(struct hci_pinfo)
  1587. };
  1588. static int hci_sock_create(struct net *net, struct socket *sock, int protocol,
  1589. int kern)
  1590. {
  1591. struct sock *sk;
  1592. BT_DBG("sock %p", sock);
  1593. if (sock->type != SOCK_RAW)
  1594. return -ESOCKTNOSUPPORT;
  1595. sock->ops = &hci_sock_ops;
  1596. sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto, kern);
  1597. if (!sk)
  1598. return -ENOMEM;
  1599. sock_init_data(sock, sk);
  1600. sock_reset_flag(sk, SOCK_ZAPPED);
  1601. sk->sk_protocol = protocol;
  1602. sock->state = SS_UNCONNECTED;
  1603. sk->sk_state = BT_OPEN;
  1604. bt_sock_link(&hci_sk_list, sk);
  1605. return 0;
  1606. }
  1607. static const struct net_proto_family hci_sock_family_ops = {
  1608. .family = PF_BLUETOOTH,
  1609. .owner = THIS_MODULE,
  1610. .create = hci_sock_create,
  1611. };
  1612. int __init hci_sock_init(void)
  1613. {
  1614. int err;
  1615. BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr));
  1616. err = proto_register(&hci_sk_proto, 0);
  1617. if (err < 0)
  1618. return err;
  1619. err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops);
  1620. if (err < 0) {
  1621. BT_ERR("HCI socket registration failed");
  1622. goto error;
  1623. }
  1624. err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL);
  1625. if (err < 0) {
  1626. BT_ERR("Failed to create HCI proc file");
  1627. bt_sock_unregister(BTPROTO_HCI);
  1628. goto error;
  1629. }
  1630. BT_INFO("HCI socket layer initialized");
  1631. return 0;
  1632. error:
  1633. proto_unregister(&hci_sk_proto);
  1634. return err;
  1635. }
  1636. void hci_sock_cleanup(void)
  1637. {
  1638. bt_procfs_cleanup(&init_net, "hci");
  1639. bt_sock_unregister(BTPROTO_HCI);
  1640. proto_unregister(&hci_sk_proto);
  1641. }