hci_sock.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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 <asm/unaligned.h>
  23. #include <net/bluetooth/bluetooth.h>
  24. #include <net/bluetooth/hci_core.h>
  25. #include <net/bluetooth/hci_mon.h>
  26. #include <net/bluetooth/mgmt.h>
  27. #include "mgmt_util.h"
  28. static LIST_HEAD(mgmt_chan_list);
  29. static DEFINE_MUTEX(mgmt_chan_list_lock);
  30. static atomic_t monitor_promisc = ATOMIC_INIT(0);
  31. /* ----- HCI socket interface ----- */
  32. /* Socket info */
  33. #define hci_pi(sk) ((struct hci_pinfo *) sk)
  34. struct hci_pinfo {
  35. struct bt_sock bt;
  36. struct hci_dev *hdev;
  37. struct hci_filter filter;
  38. __u32 cmsg_mask;
  39. unsigned short channel;
  40. unsigned long flags;
  41. };
  42. void hci_sock_set_flag(struct sock *sk, int nr)
  43. {
  44. set_bit(nr, &hci_pi(sk)->flags);
  45. }
  46. void hci_sock_clear_flag(struct sock *sk, int nr)
  47. {
  48. clear_bit(nr, &hci_pi(sk)->flags);
  49. }
  50. int hci_sock_test_flag(struct sock *sk, int nr)
  51. {
  52. return test_bit(nr, &hci_pi(sk)->flags);
  53. }
  54. unsigned short hci_sock_get_channel(struct sock *sk)
  55. {
  56. return hci_pi(sk)->channel;
  57. }
  58. static inline int hci_test_bit(int nr, const void *addr)
  59. {
  60. return *((const __u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
  61. }
  62. /* Security filter */
  63. #define HCI_SFLT_MAX_OGF 5
  64. struct hci_sec_filter {
  65. __u32 type_mask;
  66. __u32 event_mask[2];
  67. __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
  68. };
  69. static const struct hci_sec_filter hci_sec_filter = {
  70. /* Packet types */
  71. 0x10,
  72. /* Events */
  73. { 0x1000d9fe, 0x0000b00c },
  74. /* Commands */
  75. {
  76. { 0x0 },
  77. /* OGF_LINK_CTL */
  78. { 0xbe000006, 0x00000001, 0x00000000, 0x00 },
  79. /* OGF_LINK_POLICY */
  80. { 0x00005200, 0x00000000, 0x00000000, 0x00 },
  81. /* OGF_HOST_CTL */
  82. { 0xaab00200, 0x2b402aaa, 0x05220154, 0x00 },
  83. /* OGF_INFO_PARAM */
  84. { 0x000002be, 0x00000000, 0x00000000, 0x00 },
  85. /* OGF_STATUS_PARAM */
  86. { 0x000000ea, 0x00000000, 0x00000000, 0x00 }
  87. }
  88. };
  89. static struct bt_sock_list hci_sk_list = {
  90. .lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock)
  91. };
  92. static bool is_filtered_packet(struct sock *sk, struct sk_buff *skb)
  93. {
  94. struct hci_filter *flt;
  95. int flt_type, flt_event;
  96. /* Apply filter */
  97. flt = &hci_pi(sk)->filter;
  98. if (bt_cb(skb)->pkt_type == HCI_VENDOR_PKT)
  99. flt_type = 0;
  100. else
  101. flt_type = bt_cb(skb)->pkt_type & HCI_FLT_TYPE_BITS;
  102. if (!test_bit(flt_type, &flt->type_mask))
  103. return true;
  104. /* Extra filter for event packets only */
  105. if (bt_cb(skb)->pkt_type != HCI_EVENT_PKT)
  106. return false;
  107. flt_event = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS);
  108. if (!hci_test_bit(flt_event, &flt->event_mask))
  109. return true;
  110. /* Check filter only when opcode is set */
  111. if (!flt->opcode)
  112. return false;
  113. if (flt_event == HCI_EV_CMD_COMPLETE &&
  114. flt->opcode != get_unaligned((__le16 *)(skb->data + 3)))
  115. return true;
  116. if (flt_event == HCI_EV_CMD_STATUS &&
  117. flt->opcode != get_unaligned((__le16 *)(skb->data + 4)))
  118. return true;
  119. return false;
  120. }
  121. /* Send frame to RAW socket */
  122. void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
  123. {
  124. struct sock *sk;
  125. struct sk_buff *skb_copy = NULL;
  126. BT_DBG("hdev %p len %d", hdev, skb->len);
  127. read_lock(&hci_sk_list.lock);
  128. sk_for_each(sk, &hci_sk_list.head) {
  129. struct sk_buff *nskb;
  130. if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev)
  131. continue;
  132. /* Don't send frame to the socket it came from */
  133. if (skb->sk == sk)
  134. continue;
  135. if (hci_pi(sk)->channel == HCI_CHANNEL_RAW) {
  136. if (is_filtered_packet(sk, skb))
  137. continue;
  138. } else if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
  139. if (!bt_cb(skb)->incoming)
  140. continue;
  141. if (bt_cb(skb)->pkt_type != HCI_EVENT_PKT &&
  142. bt_cb(skb)->pkt_type != HCI_ACLDATA_PKT &&
  143. bt_cb(skb)->pkt_type != HCI_SCODATA_PKT)
  144. continue;
  145. } else {
  146. /* Don't send frame to other channel types */
  147. continue;
  148. }
  149. if (!skb_copy) {
  150. /* Create a private copy with headroom */
  151. skb_copy = __pskb_copy_fclone(skb, 1, GFP_ATOMIC, true);
  152. if (!skb_copy)
  153. continue;
  154. /* Put type byte before the data */
  155. memcpy(skb_push(skb_copy, 1), &bt_cb(skb)->pkt_type, 1);
  156. }
  157. nskb = skb_clone(skb_copy, GFP_ATOMIC);
  158. if (!nskb)
  159. continue;
  160. if (sock_queue_rcv_skb(sk, nskb))
  161. kfree_skb(nskb);
  162. }
  163. read_unlock(&hci_sk_list.lock);
  164. kfree_skb(skb_copy);
  165. }
  166. /* Send frame to sockets with specific channel */
  167. void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
  168. int flag, struct sock *skip_sk)
  169. {
  170. struct sock *sk;
  171. BT_DBG("channel %u len %d", channel, skb->len);
  172. read_lock(&hci_sk_list.lock);
  173. sk_for_each(sk, &hci_sk_list.head) {
  174. struct sk_buff *nskb;
  175. /* Ignore socket without the flag set */
  176. if (!hci_sock_test_flag(sk, flag))
  177. continue;
  178. /* Skip the original socket */
  179. if (sk == skip_sk)
  180. continue;
  181. if (sk->sk_state != BT_BOUND)
  182. continue;
  183. if (hci_pi(sk)->channel != channel)
  184. continue;
  185. nskb = skb_clone(skb, GFP_ATOMIC);
  186. if (!nskb)
  187. continue;
  188. if (sock_queue_rcv_skb(sk, nskb))
  189. kfree_skb(nskb);
  190. }
  191. read_unlock(&hci_sk_list.lock);
  192. }
  193. /* Send frame to monitor socket */
  194. void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb)
  195. {
  196. struct sk_buff *skb_copy = NULL;
  197. struct hci_mon_hdr *hdr;
  198. __le16 opcode;
  199. if (!atomic_read(&monitor_promisc))
  200. return;
  201. BT_DBG("hdev %p len %d", hdev, skb->len);
  202. switch (bt_cb(skb)->pkt_type) {
  203. case HCI_COMMAND_PKT:
  204. opcode = cpu_to_le16(HCI_MON_COMMAND_PKT);
  205. break;
  206. case HCI_EVENT_PKT:
  207. opcode = cpu_to_le16(HCI_MON_EVENT_PKT);
  208. break;
  209. case HCI_ACLDATA_PKT:
  210. if (bt_cb(skb)->incoming)
  211. opcode = cpu_to_le16(HCI_MON_ACL_RX_PKT);
  212. else
  213. opcode = cpu_to_le16(HCI_MON_ACL_TX_PKT);
  214. break;
  215. case HCI_SCODATA_PKT:
  216. if (bt_cb(skb)->incoming)
  217. opcode = cpu_to_le16(HCI_MON_SCO_RX_PKT);
  218. else
  219. opcode = cpu_to_le16(HCI_MON_SCO_TX_PKT);
  220. break;
  221. default:
  222. return;
  223. }
  224. /* Create a private copy with headroom */
  225. skb_copy = __pskb_copy_fclone(skb, HCI_MON_HDR_SIZE, GFP_ATOMIC, true);
  226. if (!skb_copy)
  227. return;
  228. /* Put header before the data */
  229. hdr = (void *) skb_push(skb_copy, HCI_MON_HDR_SIZE);
  230. hdr->opcode = opcode;
  231. hdr->index = cpu_to_le16(hdev->id);
  232. hdr->len = cpu_to_le16(skb->len);
  233. hci_send_to_channel(HCI_CHANNEL_MONITOR, skb_copy,
  234. HCI_SOCK_TRUSTED, NULL);
  235. kfree_skb(skb_copy);
  236. }
  237. static struct sk_buff *create_monitor_event(struct hci_dev *hdev, int event)
  238. {
  239. struct hci_mon_hdr *hdr;
  240. struct hci_mon_new_index *ni;
  241. struct sk_buff *skb;
  242. __le16 opcode;
  243. switch (event) {
  244. case HCI_DEV_REG:
  245. skb = bt_skb_alloc(HCI_MON_NEW_INDEX_SIZE, GFP_ATOMIC);
  246. if (!skb)
  247. return NULL;
  248. ni = (void *) skb_put(skb, HCI_MON_NEW_INDEX_SIZE);
  249. ni->type = hdev->dev_type;
  250. ni->bus = hdev->bus;
  251. bacpy(&ni->bdaddr, &hdev->bdaddr);
  252. memcpy(ni->name, hdev->name, 8);
  253. opcode = cpu_to_le16(HCI_MON_NEW_INDEX);
  254. break;
  255. case HCI_DEV_UNREG:
  256. skb = bt_skb_alloc(0, GFP_ATOMIC);
  257. if (!skb)
  258. return NULL;
  259. opcode = cpu_to_le16(HCI_MON_DEL_INDEX);
  260. break;
  261. default:
  262. return NULL;
  263. }
  264. __net_timestamp(skb);
  265. hdr = (void *) skb_push(skb, HCI_MON_HDR_SIZE);
  266. hdr->opcode = opcode;
  267. hdr->index = cpu_to_le16(hdev->id);
  268. hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
  269. return skb;
  270. }
  271. static void send_monitor_replay(struct sock *sk)
  272. {
  273. struct hci_dev *hdev;
  274. read_lock(&hci_dev_list_lock);
  275. list_for_each_entry(hdev, &hci_dev_list, list) {
  276. struct sk_buff *skb;
  277. skb = create_monitor_event(hdev, HCI_DEV_REG);
  278. if (!skb)
  279. continue;
  280. if (sock_queue_rcv_skb(sk, skb))
  281. kfree_skb(skb);
  282. }
  283. read_unlock(&hci_dev_list_lock);
  284. }
  285. /* Generate internal stack event */
  286. static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
  287. {
  288. struct hci_event_hdr *hdr;
  289. struct hci_ev_stack_internal *ev;
  290. struct sk_buff *skb;
  291. skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
  292. if (!skb)
  293. return;
  294. hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
  295. hdr->evt = HCI_EV_STACK_INTERNAL;
  296. hdr->plen = sizeof(*ev) + dlen;
  297. ev = (void *) skb_put(skb, sizeof(*ev) + dlen);
  298. ev->type = type;
  299. memcpy(ev->data, data, dlen);
  300. bt_cb(skb)->incoming = 1;
  301. __net_timestamp(skb);
  302. bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
  303. hci_send_to_sock(hdev, skb);
  304. kfree_skb(skb);
  305. }
  306. void hci_sock_dev_event(struct hci_dev *hdev, int event)
  307. {
  308. struct hci_ev_si_device ev;
  309. BT_DBG("hdev %s event %d", hdev->name, event);
  310. /* Send event to monitor */
  311. if (atomic_read(&monitor_promisc)) {
  312. struct sk_buff *skb;
  313. skb = create_monitor_event(hdev, event);
  314. if (skb) {
  315. hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
  316. HCI_SOCK_TRUSTED, NULL);
  317. kfree_skb(skb);
  318. }
  319. }
  320. /* Send event to sockets */
  321. ev.event = event;
  322. ev.dev_id = hdev->id;
  323. hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev);
  324. if (event == HCI_DEV_UNREG) {
  325. struct sock *sk;
  326. /* Detach sockets from device */
  327. read_lock(&hci_sk_list.lock);
  328. sk_for_each(sk, &hci_sk_list.head) {
  329. bh_lock_sock_nested(sk);
  330. if (hci_pi(sk)->hdev == hdev) {
  331. hci_pi(sk)->hdev = NULL;
  332. sk->sk_err = EPIPE;
  333. sk->sk_state = BT_OPEN;
  334. sk->sk_state_change(sk);
  335. hci_dev_put(hdev);
  336. }
  337. bh_unlock_sock(sk);
  338. }
  339. read_unlock(&hci_sk_list.lock);
  340. }
  341. }
  342. static struct hci_mgmt_chan *__hci_mgmt_chan_find(unsigned short channel)
  343. {
  344. struct hci_mgmt_chan *c;
  345. list_for_each_entry(c, &mgmt_chan_list, list) {
  346. if (c->channel == channel)
  347. return c;
  348. }
  349. return NULL;
  350. }
  351. static struct hci_mgmt_chan *hci_mgmt_chan_find(unsigned short channel)
  352. {
  353. struct hci_mgmt_chan *c;
  354. mutex_lock(&mgmt_chan_list_lock);
  355. c = __hci_mgmt_chan_find(channel);
  356. mutex_unlock(&mgmt_chan_list_lock);
  357. return c;
  358. }
  359. int hci_mgmt_chan_register(struct hci_mgmt_chan *c)
  360. {
  361. if (c->channel < HCI_CHANNEL_CONTROL)
  362. return -EINVAL;
  363. mutex_lock(&mgmt_chan_list_lock);
  364. if (__hci_mgmt_chan_find(c->channel)) {
  365. mutex_unlock(&mgmt_chan_list_lock);
  366. return -EALREADY;
  367. }
  368. list_add_tail(&c->list, &mgmt_chan_list);
  369. mutex_unlock(&mgmt_chan_list_lock);
  370. return 0;
  371. }
  372. EXPORT_SYMBOL(hci_mgmt_chan_register);
  373. void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c)
  374. {
  375. mutex_lock(&mgmt_chan_list_lock);
  376. list_del(&c->list);
  377. mutex_unlock(&mgmt_chan_list_lock);
  378. }
  379. EXPORT_SYMBOL(hci_mgmt_chan_unregister);
  380. static int hci_sock_release(struct socket *sock)
  381. {
  382. struct sock *sk = sock->sk;
  383. struct hci_dev *hdev;
  384. BT_DBG("sock %p sk %p", sock, sk);
  385. if (!sk)
  386. return 0;
  387. hdev = hci_pi(sk)->hdev;
  388. if (hci_pi(sk)->channel == HCI_CHANNEL_MONITOR)
  389. atomic_dec(&monitor_promisc);
  390. bt_sock_unlink(&hci_sk_list, sk);
  391. if (hdev) {
  392. if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
  393. hci_dev_close(hdev->id);
  394. hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
  395. mgmt_index_added(hdev);
  396. }
  397. atomic_dec(&hdev->promisc);
  398. hci_dev_put(hdev);
  399. }
  400. sock_orphan(sk);
  401. skb_queue_purge(&sk->sk_receive_queue);
  402. skb_queue_purge(&sk->sk_write_queue);
  403. sock_put(sk);
  404. return 0;
  405. }
  406. static int hci_sock_blacklist_add(struct hci_dev *hdev, void __user *arg)
  407. {
  408. bdaddr_t bdaddr;
  409. int err;
  410. if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
  411. return -EFAULT;
  412. hci_dev_lock(hdev);
  413. err = hci_bdaddr_list_add(&hdev->blacklist, &bdaddr, BDADDR_BREDR);
  414. hci_dev_unlock(hdev);
  415. return err;
  416. }
  417. static int hci_sock_blacklist_del(struct hci_dev *hdev, void __user *arg)
  418. {
  419. bdaddr_t bdaddr;
  420. int err;
  421. if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
  422. return -EFAULT;
  423. hci_dev_lock(hdev);
  424. err = hci_bdaddr_list_del(&hdev->blacklist, &bdaddr, BDADDR_BREDR);
  425. hci_dev_unlock(hdev);
  426. return err;
  427. }
  428. /* Ioctls that require bound socket */
  429. static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd,
  430. unsigned long arg)
  431. {
  432. struct hci_dev *hdev = hci_pi(sk)->hdev;
  433. if (!hdev)
  434. return -EBADFD;
  435. if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
  436. return -EBUSY;
  437. if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
  438. return -EOPNOTSUPP;
  439. if (hdev->dev_type != HCI_BREDR)
  440. return -EOPNOTSUPP;
  441. switch (cmd) {
  442. case HCISETRAW:
  443. if (!capable(CAP_NET_ADMIN))
  444. return -EPERM;
  445. return -EOPNOTSUPP;
  446. case HCIGETCONNINFO:
  447. return hci_get_conn_info(hdev, (void __user *) arg);
  448. case HCIGETAUTHINFO:
  449. return hci_get_auth_info(hdev, (void __user *) arg);
  450. case HCIBLOCKADDR:
  451. if (!capable(CAP_NET_ADMIN))
  452. return -EPERM;
  453. return hci_sock_blacklist_add(hdev, (void __user *) arg);
  454. case HCIUNBLOCKADDR:
  455. if (!capable(CAP_NET_ADMIN))
  456. return -EPERM;
  457. return hci_sock_blacklist_del(hdev, (void __user *) arg);
  458. }
  459. return -ENOIOCTLCMD;
  460. }
  461. static int hci_sock_ioctl(struct socket *sock, unsigned int cmd,
  462. unsigned long arg)
  463. {
  464. void __user *argp = (void __user *) arg;
  465. struct sock *sk = sock->sk;
  466. int err;
  467. BT_DBG("cmd %x arg %lx", cmd, arg);
  468. lock_sock(sk);
  469. if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
  470. err = -EBADFD;
  471. goto done;
  472. }
  473. release_sock(sk);
  474. switch (cmd) {
  475. case HCIGETDEVLIST:
  476. return hci_get_dev_list(argp);
  477. case HCIGETDEVINFO:
  478. return hci_get_dev_info(argp);
  479. case HCIGETCONNLIST:
  480. return hci_get_conn_list(argp);
  481. case HCIDEVUP:
  482. if (!capable(CAP_NET_ADMIN))
  483. return -EPERM;
  484. return hci_dev_open(arg);
  485. case HCIDEVDOWN:
  486. if (!capable(CAP_NET_ADMIN))
  487. return -EPERM;
  488. return hci_dev_close(arg);
  489. case HCIDEVRESET:
  490. if (!capable(CAP_NET_ADMIN))
  491. return -EPERM;
  492. return hci_dev_reset(arg);
  493. case HCIDEVRESTAT:
  494. if (!capable(CAP_NET_ADMIN))
  495. return -EPERM;
  496. return hci_dev_reset_stat(arg);
  497. case HCISETSCAN:
  498. case HCISETAUTH:
  499. case HCISETENCRYPT:
  500. case HCISETPTYPE:
  501. case HCISETLINKPOL:
  502. case HCISETLINKMODE:
  503. case HCISETACLMTU:
  504. case HCISETSCOMTU:
  505. if (!capable(CAP_NET_ADMIN))
  506. return -EPERM;
  507. return hci_dev_cmd(cmd, argp);
  508. case HCIINQUIRY:
  509. return hci_inquiry(argp);
  510. }
  511. lock_sock(sk);
  512. err = hci_sock_bound_ioctl(sk, cmd, arg);
  513. done:
  514. release_sock(sk);
  515. return err;
  516. }
  517. static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
  518. int addr_len)
  519. {
  520. struct sockaddr_hci haddr;
  521. struct sock *sk = sock->sk;
  522. struct hci_dev *hdev = NULL;
  523. int len, err = 0;
  524. BT_DBG("sock %p sk %p", sock, sk);
  525. if (!addr)
  526. return -EINVAL;
  527. memset(&haddr, 0, sizeof(haddr));
  528. len = min_t(unsigned int, sizeof(haddr), addr_len);
  529. memcpy(&haddr, addr, len);
  530. if (haddr.hci_family != AF_BLUETOOTH)
  531. return -EINVAL;
  532. lock_sock(sk);
  533. if (sk->sk_state == BT_BOUND) {
  534. err = -EALREADY;
  535. goto done;
  536. }
  537. switch (haddr.hci_channel) {
  538. case HCI_CHANNEL_RAW:
  539. if (hci_pi(sk)->hdev) {
  540. err = -EALREADY;
  541. goto done;
  542. }
  543. if (haddr.hci_dev != HCI_DEV_NONE) {
  544. hdev = hci_dev_get(haddr.hci_dev);
  545. if (!hdev) {
  546. err = -ENODEV;
  547. goto done;
  548. }
  549. atomic_inc(&hdev->promisc);
  550. }
  551. hci_pi(sk)->hdev = hdev;
  552. break;
  553. case HCI_CHANNEL_USER:
  554. if (hci_pi(sk)->hdev) {
  555. err = -EALREADY;
  556. goto done;
  557. }
  558. if (haddr.hci_dev == HCI_DEV_NONE) {
  559. err = -EINVAL;
  560. goto done;
  561. }
  562. if (!capable(CAP_NET_ADMIN)) {
  563. err = -EPERM;
  564. goto done;
  565. }
  566. hdev = hci_dev_get(haddr.hci_dev);
  567. if (!hdev) {
  568. err = -ENODEV;
  569. goto done;
  570. }
  571. if (test_bit(HCI_INIT, &hdev->flags) ||
  572. hci_dev_test_flag(hdev, HCI_SETUP) ||
  573. hci_dev_test_flag(hdev, HCI_CONFIG) ||
  574. (!hci_dev_test_flag(hdev, HCI_AUTO_OFF) &&
  575. test_bit(HCI_UP, &hdev->flags))) {
  576. err = -EBUSY;
  577. hci_dev_put(hdev);
  578. goto done;
  579. }
  580. if (hci_dev_test_and_set_flag(hdev, HCI_USER_CHANNEL)) {
  581. err = -EUSERS;
  582. hci_dev_put(hdev);
  583. goto done;
  584. }
  585. mgmt_index_removed(hdev);
  586. err = hci_dev_open(hdev->id);
  587. if (err) {
  588. if (err == -EALREADY) {
  589. /* In case the transport is already up and
  590. * running, clear the error here.
  591. *
  592. * This can happen when opening an user
  593. * channel and HCI_AUTO_OFF grace period
  594. * is still active.
  595. */
  596. err = 0;
  597. } else {
  598. hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
  599. mgmt_index_added(hdev);
  600. hci_dev_put(hdev);
  601. goto done;
  602. }
  603. }
  604. atomic_inc(&hdev->promisc);
  605. hci_pi(sk)->hdev = hdev;
  606. break;
  607. case HCI_CHANNEL_MONITOR:
  608. if (haddr.hci_dev != HCI_DEV_NONE) {
  609. err = -EINVAL;
  610. goto done;
  611. }
  612. if (!capable(CAP_NET_RAW)) {
  613. err = -EPERM;
  614. goto done;
  615. }
  616. /* The monitor interface is restricted to CAP_NET_RAW
  617. * capabilities and with that implicitly trusted.
  618. */
  619. hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
  620. send_monitor_replay(sk);
  621. atomic_inc(&monitor_promisc);
  622. break;
  623. default:
  624. if (!hci_mgmt_chan_find(haddr.hci_channel)) {
  625. err = -EINVAL;
  626. goto done;
  627. }
  628. if (haddr.hci_dev != HCI_DEV_NONE) {
  629. err = -EINVAL;
  630. goto done;
  631. }
  632. /* Users with CAP_NET_ADMIN capabilities are allowed
  633. * access to all management commands and events. For
  634. * untrusted users the interface is restricted and
  635. * also only untrusted events are sent.
  636. */
  637. if (capable(CAP_NET_ADMIN))
  638. hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
  639. /* At the moment the index and unconfigured index events
  640. * are enabled unconditionally. Setting them on each
  641. * socket when binding keeps this functionality. They
  642. * however might be cleared later and then sending of these
  643. * events will be disabled, but that is then intentional.
  644. *
  645. * This also enables generic events that are safe to be
  646. * received by untrusted users. Example for such events
  647. * are changes to settings, class of device, name etc.
  648. */
  649. if (haddr.hci_channel == HCI_CHANNEL_CONTROL) {
  650. hci_sock_set_flag(sk, HCI_MGMT_INDEX_EVENTS);
  651. hci_sock_set_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
  652. hci_sock_set_flag(sk, HCI_MGMT_GENERIC_EVENTS);
  653. }
  654. break;
  655. }
  656. hci_pi(sk)->channel = haddr.hci_channel;
  657. sk->sk_state = BT_BOUND;
  658. done:
  659. release_sock(sk);
  660. return err;
  661. }
  662. static int hci_sock_getname(struct socket *sock, struct sockaddr *addr,
  663. int *addr_len, int peer)
  664. {
  665. struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr;
  666. struct sock *sk = sock->sk;
  667. struct hci_dev *hdev;
  668. int err = 0;
  669. BT_DBG("sock %p sk %p", sock, sk);
  670. if (peer)
  671. return -EOPNOTSUPP;
  672. lock_sock(sk);
  673. hdev = hci_pi(sk)->hdev;
  674. if (!hdev) {
  675. err = -EBADFD;
  676. goto done;
  677. }
  678. *addr_len = sizeof(*haddr);
  679. haddr->hci_family = AF_BLUETOOTH;
  680. haddr->hci_dev = hdev->id;
  681. haddr->hci_channel= hci_pi(sk)->channel;
  682. done:
  683. release_sock(sk);
  684. return err;
  685. }
  686. static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg,
  687. struct sk_buff *skb)
  688. {
  689. __u32 mask = hci_pi(sk)->cmsg_mask;
  690. if (mask & HCI_CMSG_DIR) {
  691. int incoming = bt_cb(skb)->incoming;
  692. put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming),
  693. &incoming);
  694. }
  695. if (mask & HCI_CMSG_TSTAMP) {
  696. #ifdef CONFIG_COMPAT
  697. struct compat_timeval ctv;
  698. #endif
  699. struct timeval tv;
  700. void *data;
  701. int len;
  702. skb_get_timestamp(skb, &tv);
  703. data = &tv;
  704. len = sizeof(tv);
  705. #ifdef CONFIG_COMPAT
  706. if (!COMPAT_USE_64BIT_TIME &&
  707. (msg->msg_flags & MSG_CMSG_COMPAT)) {
  708. ctv.tv_sec = tv.tv_sec;
  709. ctv.tv_usec = tv.tv_usec;
  710. data = &ctv;
  711. len = sizeof(ctv);
  712. }
  713. #endif
  714. put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data);
  715. }
  716. }
  717. static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  718. int flags)
  719. {
  720. int noblock = flags & MSG_DONTWAIT;
  721. struct sock *sk = sock->sk;
  722. struct sk_buff *skb;
  723. int copied, err;
  724. BT_DBG("sock %p, sk %p", sock, sk);
  725. if (flags & (MSG_OOB))
  726. return -EOPNOTSUPP;
  727. if (sk->sk_state == BT_CLOSED)
  728. return 0;
  729. skb = skb_recv_datagram(sk, flags, noblock, &err);
  730. if (!skb)
  731. return err;
  732. copied = skb->len;
  733. if (len < copied) {
  734. msg->msg_flags |= MSG_TRUNC;
  735. copied = len;
  736. }
  737. skb_reset_transport_header(skb);
  738. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  739. switch (hci_pi(sk)->channel) {
  740. case HCI_CHANNEL_RAW:
  741. hci_sock_cmsg(sk, msg, skb);
  742. break;
  743. case HCI_CHANNEL_USER:
  744. case HCI_CHANNEL_MONITOR:
  745. sock_recv_timestamp(msg, sk, skb);
  746. break;
  747. default:
  748. if (hci_mgmt_chan_find(hci_pi(sk)->channel))
  749. sock_recv_timestamp(msg, sk, skb);
  750. break;
  751. }
  752. skb_free_datagram(sk, skb);
  753. return err ? : copied;
  754. }
  755. static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk,
  756. struct msghdr *msg, size_t msglen)
  757. {
  758. void *buf;
  759. u8 *cp;
  760. struct mgmt_hdr *hdr;
  761. u16 opcode, index, len;
  762. struct hci_dev *hdev = NULL;
  763. const struct hci_mgmt_handler *handler;
  764. bool var_len, no_hdev;
  765. int err;
  766. BT_DBG("got %zu bytes", msglen);
  767. if (msglen < sizeof(*hdr))
  768. return -EINVAL;
  769. buf = kmalloc(msglen, GFP_KERNEL);
  770. if (!buf)
  771. return -ENOMEM;
  772. if (memcpy_from_msg(buf, msg, msglen)) {
  773. err = -EFAULT;
  774. goto done;
  775. }
  776. hdr = buf;
  777. opcode = __le16_to_cpu(hdr->opcode);
  778. index = __le16_to_cpu(hdr->index);
  779. len = __le16_to_cpu(hdr->len);
  780. if (len != msglen - sizeof(*hdr)) {
  781. err = -EINVAL;
  782. goto done;
  783. }
  784. if (opcode >= chan->handler_count ||
  785. chan->handlers[opcode].func == NULL) {
  786. BT_DBG("Unknown op %u", opcode);
  787. err = mgmt_cmd_status(sk, index, opcode,
  788. MGMT_STATUS_UNKNOWN_COMMAND);
  789. goto done;
  790. }
  791. handler = &chan->handlers[opcode];
  792. if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) &&
  793. !(handler->flags & HCI_MGMT_UNTRUSTED)) {
  794. err = mgmt_cmd_status(sk, index, opcode,
  795. MGMT_STATUS_PERMISSION_DENIED);
  796. goto done;
  797. }
  798. if (index != MGMT_INDEX_NONE) {
  799. hdev = hci_dev_get(index);
  800. if (!hdev) {
  801. err = mgmt_cmd_status(sk, index, opcode,
  802. MGMT_STATUS_INVALID_INDEX);
  803. goto done;
  804. }
  805. if (hci_dev_test_flag(hdev, HCI_SETUP) ||
  806. hci_dev_test_flag(hdev, HCI_CONFIG) ||
  807. hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
  808. err = mgmt_cmd_status(sk, index, opcode,
  809. MGMT_STATUS_INVALID_INDEX);
  810. goto done;
  811. }
  812. if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
  813. !(handler->flags & HCI_MGMT_UNCONFIGURED)) {
  814. err = mgmt_cmd_status(sk, index, opcode,
  815. MGMT_STATUS_INVALID_INDEX);
  816. goto done;
  817. }
  818. }
  819. no_hdev = (handler->flags & HCI_MGMT_NO_HDEV);
  820. if (no_hdev != !hdev) {
  821. err = mgmt_cmd_status(sk, index, opcode,
  822. MGMT_STATUS_INVALID_INDEX);
  823. goto done;
  824. }
  825. var_len = (handler->flags & HCI_MGMT_VAR_LEN);
  826. if ((var_len && len < handler->data_len) ||
  827. (!var_len && len != handler->data_len)) {
  828. err = mgmt_cmd_status(sk, index, opcode,
  829. MGMT_STATUS_INVALID_PARAMS);
  830. goto done;
  831. }
  832. if (hdev && chan->hdev_init)
  833. chan->hdev_init(sk, hdev);
  834. cp = buf + sizeof(*hdr);
  835. err = handler->func(sk, hdev, cp, len);
  836. if (err < 0)
  837. goto done;
  838. err = msglen;
  839. done:
  840. if (hdev)
  841. hci_dev_put(hdev);
  842. kfree(buf);
  843. return err;
  844. }
  845. static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,
  846. size_t len)
  847. {
  848. struct sock *sk = sock->sk;
  849. struct hci_mgmt_chan *chan;
  850. struct hci_dev *hdev;
  851. struct sk_buff *skb;
  852. int err;
  853. BT_DBG("sock %p sk %p", sock, sk);
  854. if (msg->msg_flags & MSG_OOB)
  855. return -EOPNOTSUPP;
  856. if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
  857. return -EINVAL;
  858. if (len < 4 || len > HCI_MAX_FRAME_SIZE)
  859. return -EINVAL;
  860. lock_sock(sk);
  861. switch (hci_pi(sk)->channel) {
  862. case HCI_CHANNEL_RAW:
  863. case HCI_CHANNEL_USER:
  864. break;
  865. case HCI_CHANNEL_MONITOR:
  866. err = -EOPNOTSUPP;
  867. goto done;
  868. default:
  869. mutex_lock(&mgmt_chan_list_lock);
  870. chan = __hci_mgmt_chan_find(hci_pi(sk)->channel);
  871. if (chan)
  872. err = hci_mgmt_cmd(chan, sk, msg, len);
  873. else
  874. err = -EINVAL;
  875. mutex_unlock(&mgmt_chan_list_lock);
  876. goto done;
  877. }
  878. hdev = hci_pi(sk)->hdev;
  879. if (!hdev) {
  880. err = -EBADFD;
  881. goto done;
  882. }
  883. if (!test_bit(HCI_UP, &hdev->flags)) {
  884. err = -ENETDOWN;
  885. goto done;
  886. }
  887. skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
  888. if (!skb)
  889. goto done;
  890. if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
  891. err = -EFAULT;
  892. goto drop;
  893. }
  894. bt_cb(skb)->pkt_type = *((unsigned char *) skb->data);
  895. skb_pull(skb, 1);
  896. if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
  897. /* No permission check is needed for user channel
  898. * since that gets enforced when binding the socket.
  899. *
  900. * However check that the packet type is valid.
  901. */
  902. if (bt_cb(skb)->pkt_type != HCI_COMMAND_PKT &&
  903. bt_cb(skb)->pkt_type != HCI_ACLDATA_PKT &&
  904. bt_cb(skb)->pkt_type != HCI_SCODATA_PKT) {
  905. err = -EINVAL;
  906. goto drop;
  907. }
  908. skb_queue_tail(&hdev->raw_q, skb);
  909. queue_work(hdev->workqueue, &hdev->tx_work);
  910. } else if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) {
  911. u16 opcode = get_unaligned_le16(skb->data);
  912. u16 ogf = hci_opcode_ogf(opcode);
  913. u16 ocf = hci_opcode_ocf(opcode);
  914. if (((ogf > HCI_SFLT_MAX_OGF) ||
  915. !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
  916. &hci_sec_filter.ocf_mask[ogf])) &&
  917. !capable(CAP_NET_RAW)) {
  918. err = -EPERM;
  919. goto drop;
  920. }
  921. if (ogf == 0x3f) {
  922. skb_queue_tail(&hdev->raw_q, skb);
  923. queue_work(hdev->workqueue, &hdev->tx_work);
  924. } else {
  925. /* Stand-alone HCI commands must be flagged as
  926. * single-command requests.
  927. */
  928. bt_cb(skb)->req.start = true;
  929. skb_queue_tail(&hdev->cmd_q, skb);
  930. queue_work(hdev->workqueue, &hdev->cmd_work);
  931. }
  932. } else {
  933. if (!capable(CAP_NET_RAW)) {
  934. err = -EPERM;
  935. goto drop;
  936. }
  937. skb_queue_tail(&hdev->raw_q, skb);
  938. queue_work(hdev->workqueue, &hdev->tx_work);
  939. }
  940. err = len;
  941. done:
  942. release_sock(sk);
  943. return err;
  944. drop:
  945. kfree_skb(skb);
  946. goto done;
  947. }
  948. static int hci_sock_setsockopt(struct socket *sock, int level, int optname,
  949. char __user *optval, unsigned int len)
  950. {
  951. struct hci_ufilter uf = { .opcode = 0 };
  952. struct sock *sk = sock->sk;
  953. int err = 0, opt = 0;
  954. BT_DBG("sk %p, opt %d", sk, optname);
  955. lock_sock(sk);
  956. if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
  957. err = -EBADFD;
  958. goto done;
  959. }
  960. switch (optname) {
  961. case HCI_DATA_DIR:
  962. if (get_user(opt, (int __user *)optval)) {
  963. err = -EFAULT;
  964. break;
  965. }
  966. if (opt)
  967. hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
  968. else
  969. hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
  970. break;
  971. case HCI_TIME_STAMP:
  972. if (get_user(opt, (int __user *)optval)) {
  973. err = -EFAULT;
  974. break;
  975. }
  976. if (opt)
  977. hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
  978. else
  979. hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
  980. break;
  981. case HCI_FILTER:
  982. {
  983. struct hci_filter *f = &hci_pi(sk)->filter;
  984. uf.type_mask = f->type_mask;
  985. uf.opcode = f->opcode;
  986. uf.event_mask[0] = *((u32 *) f->event_mask + 0);
  987. uf.event_mask[1] = *((u32 *) f->event_mask + 1);
  988. }
  989. len = min_t(unsigned int, len, sizeof(uf));
  990. if (copy_from_user(&uf, optval, len)) {
  991. err = -EFAULT;
  992. break;
  993. }
  994. if (!capable(CAP_NET_RAW)) {
  995. uf.type_mask &= hci_sec_filter.type_mask;
  996. uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0);
  997. uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1);
  998. }
  999. {
  1000. struct hci_filter *f = &hci_pi(sk)->filter;
  1001. f->type_mask = uf.type_mask;
  1002. f->opcode = uf.opcode;
  1003. *((u32 *) f->event_mask + 0) = uf.event_mask[0];
  1004. *((u32 *) f->event_mask + 1) = uf.event_mask[1];
  1005. }
  1006. break;
  1007. default:
  1008. err = -ENOPROTOOPT;
  1009. break;
  1010. }
  1011. done:
  1012. release_sock(sk);
  1013. return err;
  1014. }
  1015. static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
  1016. char __user *optval, int __user *optlen)
  1017. {
  1018. struct hci_ufilter uf;
  1019. struct sock *sk = sock->sk;
  1020. int len, opt, err = 0;
  1021. BT_DBG("sk %p, opt %d", sk, optname);
  1022. if (get_user(len, optlen))
  1023. return -EFAULT;
  1024. lock_sock(sk);
  1025. if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
  1026. err = -EBADFD;
  1027. goto done;
  1028. }
  1029. switch (optname) {
  1030. case HCI_DATA_DIR:
  1031. if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
  1032. opt = 1;
  1033. else
  1034. opt = 0;
  1035. if (put_user(opt, optval))
  1036. err = -EFAULT;
  1037. break;
  1038. case HCI_TIME_STAMP:
  1039. if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
  1040. opt = 1;
  1041. else
  1042. opt = 0;
  1043. if (put_user(opt, optval))
  1044. err = -EFAULT;
  1045. break;
  1046. case HCI_FILTER:
  1047. {
  1048. struct hci_filter *f = &hci_pi(sk)->filter;
  1049. memset(&uf, 0, sizeof(uf));
  1050. uf.type_mask = f->type_mask;
  1051. uf.opcode = f->opcode;
  1052. uf.event_mask[0] = *((u32 *) f->event_mask + 0);
  1053. uf.event_mask[1] = *((u32 *) f->event_mask + 1);
  1054. }
  1055. len = min_t(unsigned int, len, sizeof(uf));
  1056. if (copy_to_user(optval, &uf, len))
  1057. err = -EFAULT;
  1058. break;
  1059. default:
  1060. err = -ENOPROTOOPT;
  1061. break;
  1062. }
  1063. done:
  1064. release_sock(sk);
  1065. return err;
  1066. }
  1067. static const struct proto_ops hci_sock_ops = {
  1068. .family = PF_BLUETOOTH,
  1069. .owner = THIS_MODULE,
  1070. .release = hci_sock_release,
  1071. .bind = hci_sock_bind,
  1072. .getname = hci_sock_getname,
  1073. .sendmsg = hci_sock_sendmsg,
  1074. .recvmsg = hci_sock_recvmsg,
  1075. .ioctl = hci_sock_ioctl,
  1076. .poll = datagram_poll,
  1077. .listen = sock_no_listen,
  1078. .shutdown = sock_no_shutdown,
  1079. .setsockopt = hci_sock_setsockopt,
  1080. .getsockopt = hci_sock_getsockopt,
  1081. .connect = sock_no_connect,
  1082. .socketpair = sock_no_socketpair,
  1083. .accept = sock_no_accept,
  1084. .mmap = sock_no_mmap
  1085. };
  1086. static struct proto hci_sk_proto = {
  1087. .name = "HCI",
  1088. .owner = THIS_MODULE,
  1089. .obj_size = sizeof(struct hci_pinfo)
  1090. };
  1091. static int hci_sock_create(struct net *net, struct socket *sock, int protocol,
  1092. int kern)
  1093. {
  1094. struct sock *sk;
  1095. BT_DBG("sock %p", sock);
  1096. if (sock->type != SOCK_RAW)
  1097. return -ESOCKTNOSUPPORT;
  1098. sock->ops = &hci_sock_ops;
  1099. sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto, kern);
  1100. if (!sk)
  1101. return -ENOMEM;
  1102. sock_init_data(sock, sk);
  1103. sock_reset_flag(sk, SOCK_ZAPPED);
  1104. sk->sk_protocol = protocol;
  1105. sock->state = SS_UNCONNECTED;
  1106. sk->sk_state = BT_OPEN;
  1107. bt_sock_link(&hci_sk_list, sk);
  1108. return 0;
  1109. }
  1110. static const struct net_proto_family hci_sock_family_ops = {
  1111. .family = PF_BLUETOOTH,
  1112. .owner = THIS_MODULE,
  1113. .create = hci_sock_create,
  1114. };
  1115. int __init hci_sock_init(void)
  1116. {
  1117. int err;
  1118. BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr));
  1119. err = proto_register(&hci_sk_proto, 0);
  1120. if (err < 0)
  1121. return err;
  1122. err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops);
  1123. if (err < 0) {
  1124. BT_ERR("HCI socket registration failed");
  1125. goto error;
  1126. }
  1127. err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL);
  1128. if (err < 0) {
  1129. BT_ERR("Failed to create HCI proc file");
  1130. bt_sock_unregister(BTPROTO_HCI);
  1131. goto error;
  1132. }
  1133. BT_INFO("HCI socket layer initialized");
  1134. return 0;
  1135. error:
  1136. proto_unregister(&hci_sk_proto);
  1137. return err;
  1138. }
  1139. void hci_sock_cleanup(void)
  1140. {
  1141. bt_procfs_cleanup(&init_net, "hci");
  1142. bt_sock_unregister(BTPROTO_HCI);
  1143. proto_unregister(&hci_sk_proto);
  1144. }