11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds BlueZ - Bluetooth protocol stack for Linux 31da177e4SLinus Torvalds Copyright (C) 2000-2001 Qualcomm Incorporated 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds This program is free software; you can redistribute it and/or modify 81da177e4SLinus Torvalds it under the terms of the GNU General Public License version 2 as 91da177e4SLinus Torvalds published by the Free Software Foundation; 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 121da177e4SLinus Torvalds OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 131da177e4SLinus Torvalds FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 141da177e4SLinus Torvalds IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 151da177e4SLinus Torvalds CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 161da177e4SLinus Torvalds WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 171da177e4SLinus Torvalds ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 181da177e4SLinus Torvalds OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 211da177e4SLinus Torvalds COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 221da177e4SLinus Torvalds SOFTWARE IS DISCLAIMED. 231da177e4SLinus Torvalds */ 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds /* Bluetooth HCI sockets. */ 261da177e4SLinus Torvalds 278c520a59SGustavo Padovan #include <linux/export.h> 281da177e4SLinus Torvalds #include <asm/unaligned.h> 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds #include <net/bluetooth/bluetooth.h> 311da177e4SLinus Torvalds #include <net/bluetooth/hci_core.h> 32cd82e61cSMarcel Holtmann #include <net/bluetooth/hci_mon.h> 33fa4335d7SJohan Hedberg #include <net/bluetooth/mgmt.h> 34fa4335d7SJohan Hedberg 35fa4335d7SJohan Hedberg #include "mgmt_util.h" 361da177e4SLinus Torvalds 37801c1e8dSJohan Hedberg static LIST_HEAD(mgmt_chan_list); 38801c1e8dSJohan Hedberg static DEFINE_MUTEX(mgmt_chan_list_lock); 39801c1e8dSJohan Hedberg 40cd82e61cSMarcel Holtmann static atomic_t monitor_promisc = ATOMIC_INIT(0); 41cd82e61cSMarcel Holtmann 421da177e4SLinus Torvalds /* ----- HCI socket interface ----- */ 431da177e4SLinus Torvalds 44863def58SMarcel Holtmann /* Socket info */ 45863def58SMarcel Holtmann #define hci_pi(sk) ((struct hci_pinfo *) sk) 46863def58SMarcel Holtmann 47863def58SMarcel Holtmann struct hci_pinfo { 48863def58SMarcel Holtmann struct bt_sock bt; 49863def58SMarcel Holtmann struct hci_dev *hdev; 50863def58SMarcel Holtmann struct hci_filter filter; 51863def58SMarcel Holtmann __u32 cmsg_mask; 52863def58SMarcel Holtmann unsigned short channel; 536befc644SMarcel Holtmann unsigned long flags; 54863def58SMarcel Holtmann }; 55863def58SMarcel Holtmann 566befc644SMarcel Holtmann void hci_sock_set_flag(struct sock *sk, int nr) 576befc644SMarcel Holtmann { 586befc644SMarcel Holtmann set_bit(nr, &hci_pi(sk)->flags); 596befc644SMarcel Holtmann } 606befc644SMarcel Holtmann 616befc644SMarcel Holtmann void hci_sock_clear_flag(struct sock *sk, int nr) 626befc644SMarcel Holtmann { 636befc644SMarcel Holtmann clear_bit(nr, &hci_pi(sk)->flags); 646befc644SMarcel Holtmann } 656befc644SMarcel Holtmann 66c85be545SMarcel Holtmann int hci_sock_test_flag(struct sock *sk, int nr) 67c85be545SMarcel Holtmann { 68c85be545SMarcel Holtmann return test_bit(nr, &hci_pi(sk)->flags); 69c85be545SMarcel Holtmann } 70c85be545SMarcel Holtmann 71d0f172b1SJohan Hedberg unsigned short hci_sock_get_channel(struct sock *sk) 72d0f172b1SJohan Hedberg { 73d0f172b1SJohan Hedberg return hci_pi(sk)->channel; 74d0f172b1SJohan Hedberg } 75d0f172b1SJohan Hedberg 769391976aSJiri Slaby static inline int hci_test_bit(int nr, const void *addr) 771da177e4SLinus Torvalds { 789391976aSJiri Slaby return *((const __u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31)); 791da177e4SLinus Torvalds } 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /* Security filter */ 823ad254f7SMarcel Holtmann #define HCI_SFLT_MAX_OGF 5 833ad254f7SMarcel Holtmann 843ad254f7SMarcel Holtmann struct hci_sec_filter { 853ad254f7SMarcel Holtmann __u32 type_mask; 863ad254f7SMarcel Holtmann __u32 event_mask[2]; 873ad254f7SMarcel Holtmann __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4]; 883ad254f7SMarcel Holtmann }; 893ad254f7SMarcel Holtmann 907e67c112SMarcel Holtmann static const struct hci_sec_filter hci_sec_filter = { 911da177e4SLinus Torvalds /* Packet types */ 921da177e4SLinus Torvalds 0x10, 931da177e4SLinus Torvalds /* Events */ 94dd7f5527SMarcel Holtmann { 0x1000d9fe, 0x0000b00c }, 951da177e4SLinus Torvalds /* Commands */ 961da177e4SLinus Torvalds { 971da177e4SLinus Torvalds { 0x0 }, 981da177e4SLinus Torvalds /* OGF_LINK_CTL */ 997c631a67SMarcel Holtmann { 0xbe000006, 0x00000001, 0x00000000, 0x00 }, 1001da177e4SLinus Torvalds /* OGF_LINK_POLICY */ 1017c631a67SMarcel Holtmann { 0x00005200, 0x00000000, 0x00000000, 0x00 }, 1021da177e4SLinus Torvalds /* OGF_HOST_CTL */ 1037c631a67SMarcel Holtmann { 0xaab00200, 0x2b402aaa, 0x05220154, 0x00 }, 1041da177e4SLinus Torvalds /* OGF_INFO_PARAM */ 1057c631a67SMarcel Holtmann { 0x000002be, 0x00000000, 0x00000000, 0x00 }, 1061da177e4SLinus Torvalds /* OGF_STATUS_PARAM */ 1077c631a67SMarcel Holtmann { 0x000000ea, 0x00000000, 0x00000000, 0x00 } 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds }; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds static struct bt_sock_list hci_sk_list = { 112d5fb2962SRobert P. J. Day .lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock) 1131da177e4SLinus Torvalds }; 1141da177e4SLinus Torvalds 115f81fe64fSMarcel Holtmann static bool is_filtered_packet(struct sock *sk, struct sk_buff *skb) 116f81fe64fSMarcel Holtmann { 117f81fe64fSMarcel Holtmann struct hci_filter *flt; 118f81fe64fSMarcel Holtmann int flt_type, flt_event; 119f81fe64fSMarcel Holtmann 120f81fe64fSMarcel Holtmann /* Apply filter */ 121f81fe64fSMarcel Holtmann flt = &hci_pi(sk)->filter; 122f81fe64fSMarcel Holtmann 123f81fe64fSMarcel Holtmann if (bt_cb(skb)->pkt_type == HCI_VENDOR_PKT) 124f81fe64fSMarcel Holtmann flt_type = 0; 125f81fe64fSMarcel Holtmann else 126f81fe64fSMarcel Holtmann flt_type = bt_cb(skb)->pkt_type & HCI_FLT_TYPE_BITS; 127f81fe64fSMarcel Holtmann 128f81fe64fSMarcel Holtmann if (!test_bit(flt_type, &flt->type_mask)) 129f81fe64fSMarcel Holtmann return true; 130f81fe64fSMarcel Holtmann 131f81fe64fSMarcel Holtmann /* Extra filter for event packets only */ 132f81fe64fSMarcel Holtmann if (bt_cb(skb)->pkt_type != HCI_EVENT_PKT) 133f81fe64fSMarcel Holtmann return false; 134f81fe64fSMarcel Holtmann 135f81fe64fSMarcel Holtmann flt_event = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS); 136f81fe64fSMarcel Holtmann 137f81fe64fSMarcel Holtmann if (!hci_test_bit(flt_event, &flt->event_mask)) 138f81fe64fSMarcel Holtmann return true; 139f81fe64fSMarcel Holtmann 140f81fe64fSMarcel Holtmann /* Check filter only when opcode is set */ 141f81fe64fSMarcel Holtmann if (!flt->opcode) 142f81fe64fSMarcel Holtmann return false; 143f81fe64fSMarcel Holtmann 144f81fe64fSMarcel Holtmann if (flt_event == HCI_EV_CMD_COMPLETE && 145f81fe64fSMarcel Holtmann flt->opcode != get_unaligned((__le16 *)(skb->data + 3))) 146f81fe64fSMarcel Holtmann return true; 147f81fe64fSMarcel Holtmann 148f81fe64fSMarcel Holtmann if (flt_event == HCI_EV_CMD_STATUS && 149f81fe64fSMarcel Holtmann flt->opcode != get_unaligned((__le16 *)(skb->data + 4))) 150f81fe64fSMarcel Holtmann return true; 151f81fe64fSMarcel Holtmann 152f81fe64fSMarcel Holtmann return false; 153f81fe64fSMarcel Holtmann } 154f81fe64fSMarcel Holtmann 1551da177e4SLinus Torvalds /* Send frame to RAW socket */ 156470fe1b5SMarcel Holtmann void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds struct sock *sk; 159e0edf373SMarcel Holtmann struct sk_buff *skb_copy = NULL; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds BT_DBG("hdev %p len %d", hdev, skb->len); 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds read_lock(&hci_sk_list.lock); 164470fe1b5SMarcel Holtmann 165b67bfe0dSSasha Levin sk_for_each(sk, &hci_sk_list.head) { 1661da177e4SLinus Torvalds struct sk_buff *nskb; 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev) 1691da177e4SLinus Torvalds continue; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds /* Don't send frame to the socket it came from */ 1721da177e4SLinus Torvalds if (skb->sk == sk) 1731da177e4SLinus Torvalds continue; 1741da177e4SLinus Torvalds 17523500189SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_RAW) { 176f81fe64fSMarcel Holtmann if (is_filtered_packet(sk, skb)) 1771da177e4SLinus Torvalds continue; 17823500189SMarcel Holtmann } else if (hci_pi(sk)->channel == HCI_CHANNEL_USER) { 17923500189SMarcel Holtmann if (!bt_cb(skb)->incoming) 18023500189SMarcel Holtmann continue; 18123500189SMarcel Holtmann if (bt_cb(skb)->pkt_type != HCI_EVENT_PKT && 18223500189SMarcel Holtmann bt_cb(skb)->pkt_type != HCI_ACLDATA_PKT && 18323500189SMarcel Holtmann bt_cb(skb)->pkt_type != HCI_SCODATA_PKT) 18423500189SMarcel Holtmann continue; 18523500189SMarcel Holtmann } else { 18623500189SMarcel Holtmann /* Don't send frame to other channel types */ 18723500189SMarcel Holtmann continue; 18823500189SMarcel Holtmann } 1891da177e4SLinus Torvalds 190e0edf373SMarcel Holtmann if (!skb_copy) { 191e0edf373SMarcel Holtmann /* Create a private copy with headroom */ 192bad93e9dSOctavian Purdila skb_copy = __pskb_copy_fclone(skb, 1, GFP_ATOMIC, true); 193e0edf373SMarcel Holtmann if (!skb_copy) 1941da177e4SLinus Torvalds continue; 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds /* Put type byte before the data */ 197e0edf373SMarcel Holtmann memcpy(skb_push(skb_copy, 1), &bt_cb(skb)->pkt_type, 1); 198e0edf373SMarcel Holtmann } 199e0edf373SMarcel Holtmann 200e0edf373SMarcel Holtmann nskb = skb_clone(skb_copy, GFP_ATOMIC); 201e0edf373SMarcel Holtmann if (!nskb) 202e0edf373SMarcel Holtmann continue; 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds if (sock_queue_rcv_skb(sk, nskb)) 2051da177e4SLinus Torvalds kfree_skb(nskb); 2061da177e4SLinus Torvalds } 207470fe1b5SMarcel Holtmann 208470fe1b5SMarcel Holtmann read_unlock(&hci_sk_list.lock); 209e0edf373SMarcel Holtmann 210e0edf373SMarcel Holtmann kfree_skb(skb_copy); 211470fe1b5SMarcel Holtmann } 212470fe1b5SMarcel Holtmann 2137129069eSJohan Hedberg /* Send frame to sockets with specific channel */ 2147129069eSJohan Hedberg void hci_send_to_channel(unsigned short channel, struct sk_buff *skb, 215c08b1a1dSMarcel Holtmann int flag, struct sock *skip_sk) 216470fe1b5SMarcel Holtmann { 217470fe1b5SMarcel Holtmann struct sock *sk; 218470fe1b5SMarcel Holtmann 2197129069eSJohan Hedberg BT_DBG("channel %u len %d", channel, skb->len); 220470fe1b5SMarcel Holtmann 221470fe1b5SMarcel Holtmann read_lock(&hci_sk_list.lock); 222470fe1b5SMarcel Holtmann 223b67bfe0dSSasha Levin sk_for_each(sk, &hci_sk_list.head) { 224470fe1b5SMarcel Holtmann struct sk_buff *nskb; 225470fe1b5SMarcel Holtmann 226c08b1a1dSMarcel Holtmann /* Ignore socket without the flag set */ 227c85be545SMarcel Holtmann if (!hci_sock_test_flag(sk, flag)) 228c08b1a1dSMarcel Holtmann continue; 229c08b1a1dSMarcel Holtmann 230470fe1b5SMarcel Holtmann /* Skip the original socket */ 231470fe1b5SMarcel Holtmann if (sk == skip_sk) 232470fe1b5SMarcel Holtmann continue; 233470fe1b5SMarcel Holtmann 234470fe1b5SMarcel Holtmann if (sk->sk_state != BT_BOUND) 235470fe1b5SMarcel Holtmann continue; 236470fe1b5SMarcel Holtmann 2377129069eSJohan Hedberg if (hci_pi(sk)->channel != channel) 238d7f72f61SMarcel Holtmann continue; 239d7f72f61SMarcel Holtmann 240d7f72f61SMarcel Holtmann nskb = skb_clone(skb, GFP_ATOMIC); 241d7f72f61SMarcel Holtmann if (!nskb) 242d7f72f61SMarcel Holtmann continue; 243d7f72f61SMarcel Holtmann 244d7f72f61SMarcel Holtmann if (sock_queue_rcv_skb(sk, nskb)) 245d7f72f61SMarcel Holtmann kfree_skb(nskb); 246d7f72f61SMarcel Holtmann } 247d7f72f61SMarcel Holtmann 248d7f72f61SMarcel Holtmann read_unlock(&hci_sk_list.lock); 249d7f72f61SMarcel Holtmann } 250d7f72f61SMarcel Holtmann 251cd82e61cSMarcel Holtmann /* Send frame to monitor socket */ 252cd82e61cSMarcel Holtmann void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb) 253cd82e61cSMarcel Holtmann { 254cd82e61cSMarcel Holtmann struct sk_buff *skb_copy = NULL; 2552b531294SMarcel Holtmann struct hci_mon_hdr *hdr; 256cd82e61cSMarcel Holtmann __le16 opcode; 257cd82e61cSMarcel Holtmann 258cd82e61cSMarcel Holtmann if (!atomic_read(&monitor_promisc)) 259cd82e61cSMarcel Holtmann return; 260cd82e61cSMarcel Holtmann 261cd82e61cSMarcel Holtmann BT_DBG("hdev %p len %d", hdev, skb->len); 262cd82e61cSMarcel Holtmann 263cd82e61cSMarcel Holtmann switch (bt_cb(skb)->pkt_type) { 264cd82e61cSMarcel Holtmann case HCI_COMMAND_PKT: 265dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_COMMAND_PKT); 266cd82e61cSMarcel Holtmann break; 267cd82e61cSMarcel Holtmann case HCI_EVENT_PKT: 268dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_EVENT_PKT); 269cd82e61cSMarcel Holtmann break; 270cd82e61cSMarcel Holtmann case HCI_ACLDATA_PKT: 271cd82e61cSMarcel Holtmann if (bt_cb(skb)->incoming) 272dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_ACL_RX_PKT); 273cd82e61cSMarcel Holtmann else 274dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_ACL_TX_PKT); 275cd82e61cSMarcel Holtmann break; 276cd82e61cSMarcel Holtmann case HCI_SCODATA_PKT: 277cd82e61cSMarcel Holtmann if (bt_cb(skb)->incoming) 278dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_SCO_RX_PKT); 279cd82e61cSMarcel Holtmann else 280dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_SCO_TX_PKT); 281cd82e61cSMarcel Holtmann break; 282cd82e61cSMarcel Holtmann default: 283cd82e61cSMarcel Holtmann return; 284cd82e61cSMarcel Holtmann } 285cd82e61cSMarcel Holtmann 2862b531294SMarcel Holtmann /* Create a private copy with headroom */ 2872b531294SMarcel Holtmann skb_copy = __pskb_copy_fclone(skb, HCI_MON_HDR_SIZE, GFP_ATOMIC, true); 2882b531294SMarcel Holtmann if (!skb_copy) 2892b531294SMarcel Holtmann return; 2902b531294SMarcel Holtmann 2912b531294SMarcel Holtmann /* Put header before the data */ 2922b531294SMarcel Holtmann hdr = (void *) skb_push(skb_copy, HCI_MON_HDR_SIZE); 2932b531294SMarcel Holtmann hdr->opcode = opcode; 2942b531294SMarcel Holtmann hdr->index = cpu_to_le16(hdev->id); 2952b531294SMarcel Holtmann hdr->len = cpu_to_le16(skb->len); 2962b531294SMarcel Holtmann 297c08b1a1dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb_copy, 298c08b1a1dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 299cd82e61cSMarcel Holtmann kfree_skb(skb_copy); 300cd82e61cSMarcel Holtmann } 301cd82e61cSMarcel Holtmann 302cd82e61cSMarcel Holtmann static struct sk_buff *create_monitor_event(struct hci_dev *hdev, int event) 303cd82e61cSMarcel Holtmann { 304cd82e61cSMarcel Holtmann struct hci_mon_hdr *hdr; 305cd82e61cSMarcel Holtmann struct hci_mon_new_index *ni; 306cd82e61cSMarcel Holtmann struct sk_buff *skb; 307cd82e61cSMarcel Holtmann __le16 opcode; 308cd82e61cSMarcel Holtmann 309cd82e61cSMarcel Holtmann switch (event) { 310cd82e61cSMarcel Holtmann case HCI_DEV_REG: 311cd82e61cSMarcel Holtmann skb = bt_skb_alloc(HCI_MON_NEW_INDEX_SIZE, GFP_ATOMIC); 312cd82e61cSMarcel Holtmann if (!skb) 313cd82e61cSMarcel Holtmann return NULL; 314cd82e61cSMarcel Holtmann 315cd82e61cSMarcel Holtmann ni = (void *) skb_put(skb, HCI_MON_NEW_INDEX_SIZE); 316cd82e61cSMarcel Holtmann ni->type = hdev->dev_type; 317cd82e61cSMarcel Holtmann ni->bus = hdev->bus; 318cd82e61cSMarcel Holtmann bacpy(&ni->bdaddr, &hdev->bdaddr); 319cd82e61cSMarcel Holtmann memcpy(ni->name, hdev->name, 8); 320cd82e61cSMarcel Holtmann 321dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_NEW_INDEX); 322cd82e61cSMarcel Holtmann break; 323cd82e61cSMarcel Holtmann 324cd82e61cSMarcel Holtmann case HCI_DEV_UNREG: 325cd82e61cSMarcel Holtmann skb = bt_skb_alloc(0, GFP_ATOMIC); 326cd82e61cSMarcel Holtmann if (!skb) 327cd82e61cSMarcel Holtmann return NULL; 328cd82e61cSMarcel Holtmann 329dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_DEL_INDEX); 330cd82e61cSMarcel Holtmann break; 331cd82e61cSMarcel Holtmann 332cd82e61cSMarcel Holtmann default: 333cd82e61cSMarcel Holtmann return NULL; 334cd82e61cSMarcel Holtmann } 335cd82e61cSMarcel Holtmann 336cd82e61cSMarcel Holtmann __net_timestamp(skb); 337cd82e61cSMarcel Holtmann 338cd82e61cSMarcel Holtmann hdr = (void *) skb_push(skb, HCI_MON_HDR_SIZE); 339cd82e61cSMarcel Holtmann hdr->opcode = opcode; 340cd82e61cSMarcel Holtmann hdr->index = cpu_to_le16(hdev->id); 341cd82e61cSMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE); 342cd82e61cSMarcel Holtmann 343cd82e61cSMarcel Holtmann return skb; 344cd82e61cSMarcel Holtmann } 345cd82e61cSMarcel Holtmann 346cd82e61cSMarcel Holtmann static void send_monitor_replay(struct sock *sk) 347cd82e61cSMarcel Holtmann { 348cd82e61cSMarcel Holtmann struct hci_dev *hdev; 349cd82e61cSMarcel Holtmann 350cd82e61cSMarcel Holtmann read_lock(&hci_dev_list_lock); 351cd82e61cSMarcel Holtmann 352cd82e61cSMarcel Holtmann list_for_each_entry(hdev, &hci_dev_list, list) { 353cd82e61cSMarcel Holtmann struct sk_buff *skb; 354cd82e61cSMarcel Holtmann 355cd82e61cSMarcel Holtmann skb = create_monitor_event(hdev, HCI_DEV_REG); 356cd82e61cSMarcel Holtmann if (!skb) 357cd82e61cSMarcel Holtmann continue; 358cd82e61cSMarcel Holtmann 359cd82e61cSMarcel Holtmann if (sock_queue_rcv_skb(sk, skb)) 360cd82e61cSMarcel Holtmann kfree_skb(skb); 361cd82e61cSMarcel Holtmann } 362cd82e61cSMarcel Holtmann 363cd82e61cSMarcel Holtmann read_unlock(&hci_dev_list_lock); 364cd82e61cSMarcel Holtmann } 365cd82e61cSMarcel Holtmann 366040030efSMarcel Holtmann /* Generate internal stack event */ 367040030efSMarcel Holtmann static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data) 368040030efSMarcel Holtmann { 369040030efSMarcel Holtmann struct hci_event_hdr *hdr; 370040030efSMarcel Holtmann struct hci_ev_stack_internal *ev; 371040030efSMarcel Holtmann struct sk_buff *skb; 372040030efSMarcel Holtmann 373040030efSMarcel Holtmann skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC); 374040030efSMarcel Holtmann if (!skb) 375040030efSMarcel Holtmann return; 376040030efSMarcel Holtmann 377040030efSMarcel Holtmann hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE); 378040030efSMarcel Holtmann hdr->evt = HCI_EV_STACK_INTERNAL; 379040030efSMarcel Holtmann hdr->plen = sizeof(*ev) + dlen; 380040030efSMarcel Holtmann 381040030efSMarcel Holtmann ev = (void *) skb_put(skb, sizeof(*ev) + dlen); 382040030efSMarcel Holtmann ev->type = type; 383040030efSMarcel Holtmann memcpy(ev->data, data, dlen); 384040030efSMarcel Holtmann 385040030efSMarcel Holtmann bt_cb(skb)->incoming = 1; 386040030efSMarcel Holtmann __net_timestamp(skb); 387040030efSMarcel Holtmann 388040030efSMarcel Holtmann bt_cb(skb)->pkt_type = HCI_EVENT_PKT; 389040030efSMarcel Holtmann hci_send_to_sock(hdev, skb); 390040030efSMarcel Holtmann kfree_skb(skb); 391040030efSMarcel Holtmann } 392040030efSMarcel Holtmann 393040030efSMarcel Holtmann void hci_sock_dev_event(struct hci_dev *hdev, int event) 394040030efSMarcel Holtmann { 395040030efSMarcel Holtmann struct hci_ev_si_device ev; 396040030efSMarcel Holtmann 397040030efSMarcel Holtmann BT_DBG("hdev %s event %d", hdev->name, event); 398040030efSMarcel Holtmann 399cd82e61cSMarcel Holtmann /* Send event to monitor */ 400cd82e61cSMarcel Holtmann if (atomic_read(&monitor_promisc)) { 401cd82e61cSMarcel Holtmann struct sk_buff *skb; 402cd82e61cSMarcel Holtmann 403cd82e61cSMarcel Holtmann skb = create_monitor_event(hdev, event); 404cd82e61cSMarcel Holtmann if (skb) { 405c08b1a1dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 406c08b1a1dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 407cd82e61cSMarcel Holtmann kfree_skb(skb); 408cd82e61cSMarcel Holtmann } 409cd82e61cSMarcel Holtmann } 410cd82e61cSMarcel Holtmann 411040030efSMarcel Holtmann /* Send event to sockets */ 412040030efSMarcel Holtmann ev.event = event; 413040030efSMarcel Holtmann ev.dev_id = hdev->id; 414040030efSMarcel Holtmann hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev); 415040030efSMarcel Holtmann 416040030efSMarcel Holtmann if (event == HCI_DEV_UNREG) { 417040030efSMarcel Holtmann struct sock *sk; 418040030efSMarcel Holtmann 419040030efSMarcel Holtmann /* Detach sockets from device */ 420040030efSMarcel Holtmann read_lock(&hci_sk_list.lock); 421b67bfe0dSSasha Levin sk_for_each(sk, &hci_sk_list.head) { 422040030efSMarcel Holtmann bh_lock_sock_nested(sk); 423040030efSMarcel Holtmann if (hci_pi(sk)->hdev == hdev) { 424040030efSMarcel Holtmann hci_pi(sk)->hdev = NULL; 425040030efSMarcel Holtmann sk->sk_err = EPIPE; 426040030efSMarcel Holtmann sk->sk_state = BT_OPEN; 427040030efSMarcel Holtmann sk->sk_state_change(sk); 428040030efSMarcel Holtmann 429040030efSMarcel Holtmann hci_dev_put(hdev); 430040030efSMarcel Holtmann } 431040030efSMarcel Holtmann bh_unlock_sock(sk); 432040030efSMarcel Holtmann } 433040030efSMarcel Holtmann read_unlock(&hci_sk_list.lock); 434040030efSMarcel Holtmann } 435040030efSMarcel Holtmann } 436040030efSMarcel Holtmann 437801c1e8dSJohan Hedberg static struct hci_mgmt_chan *__hci_mgmt_chan_find(unsigned short channel) 438801c1e8dSJohan Hedberg { 439801c1e8dSJohan Hedberg struct hci_mgmt_chan *c; 440801c1e8dSJohan Hedberg 441801c1e8dSJohan Hedberg list_for_each_entry(c, &mgmt_chan_list, list) { 442801c1e8dSJohan Hedberg if (c->channel == channel) 443801c1e8dSJohan Hedberg return c; 444801c1e8dSJohan Hedberg } 445801c1e8dSJohan Hedberg 446801c1e8dSJohan Hedberg return NULL; 447801c1e8dSJohan Hedberg } 448801c1e8dSJohan Hedberg 449801c1e8dSJohan Hedberg static struct hci_mgmt_chan *hci_mgmt_chan_find(unsigned short channel) 450801c1e8dSJohan Hedberg { 451801c1e8dSJohan Hedberg struct hci_mgmt_chan *c; 452801c1e8dSJohan Hedberg 453801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock); 454801c1e8dSJohan Hedberg c = __hci_mgmt_chan_find(channel); 455801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 456801c1e8dSJohan Hedberg 457801c1e8dSJohan Hedberg return c; 458801c1e8dSJohan Hedberg } 459801c1e8dSJohan Hedberg 460801c1e8dSJohan Hedberg int hci_mgmt_chan_register(struct hci_mgmt_chan *c) 461801c1e8dSJohan Hedberg { 462801c1e8dSJohan Hedberg if (c->channel < HCI_CHANNEL_CONTROL) 463801c1e8dSJohan Hedberg return -EINVAL; 464801c1e8dSJohan Hedberg 465801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock); 466801c1e8dSJohan Hedberg if (__hci_mgmt_chan_find(c->channel)) { 467801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 468801c1e8dSJohan Hedberg return -EALREADY; 469801c1e8dSJohan Hedberg } 470801c1e8dSJohan Hedberg 471801c1e8dSJohan Hedberg list_add_tail(&c->list, &mgmt_chan_list); 472801c1e8dSJohan Hedberg 473801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 474801c1e8dSJohan Hedberg 475801c1e8dSJohan Hedberg return 0; 476801c1e8dSJohan Hedberg } 477801c1e8dSJohan Hedberg EXPORT_SYMBOL(hci_mgmt_chan_register); 478801c1e8dSJohan Hedberg 479801c1e8dSJohan Hedberg void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c) 480801c1e8dSJohan Hedberg { 481801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock); 482801c1e8dSJohan Hedberg list_del(&c->list); 483801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 484801c1e8dSJohan Hedberg } 485801c1e8dSJohan Hedberg EXPORT_SYMBOL(hci_mgmt_chan_unregister); 486801c1e8dSJohan Hedberg 4871da177e4SLinus Torvalds static int hci_sock_release(struct socket *sock) 4881da177e4SLinus Torvalds { 4891da177e4SLinus Torvalds struct sock *sk = sock->sk; 4907b005bd3SMarcel Holtmann struct hci_dev *hdev; 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds if (!sk) 4951da177e4SLinus Torvalds return 0; 4961da177e4SLinus Torvalds 4977b005bd3SMarcel Holtmann hdev = hci_pi(sk)->hdev; 4987b005bd3SMarcel Holtmann 499cd82e61cSMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_MONITOR) 500cd82e61cSMarcel Holtmann atomic_dec(&monitor_promisc); 501cd82e61cSMarcel Holtmann 5021da177e4SLinus Torvalds bt_sock_unlink(&hci_sk_list, sk); 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds if (hdev) { 50523500189SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_USER) { 50623500189SMarcel Holtmann mgmt_index_added(hdev); 507a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_USER_CHANNEL); 50823500189SMarcel Holtmann hci_dev_close(hdev->id); 50923500189SMarcel Holtmann } 51023500189SMarcel Holtmann 5111da177e4SLinus Torvalds atomic_dec(&hdev->promisc); 5121da177e4SLinus Torvalds hci_dev_put(hdev); 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds sock_orphan(sk); 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds skb_queue_purge(&sk->sk_receive_queue); 5181da177e4SLinus Torvalds skb_queue_purge(&sk->sk_write_queue); 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds sock_put(sk); 5211da177e4SLinus Torvalds return 0; 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds 524b2a66aadSAntti Julku static int hci_sock_blacklist_add(struct hci_dev *hdev, void __user *arg) 525f0358568SJohan Hedberg { 526f0358568SJohan Hedberg bdaddr_t bdaddr; 5275e762444SAntti Julku int err; 528f0358568SJohan Hedberg 529f0358568SJohan Hedberg if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) 530f0358568SJohan Hedberg return -EFAULT; 531f0358568SJohan Hedberg 53209fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 5335e762444SAntti Julku 534dcc36c16SJohan Hedberg err = hci_bdaddr_list_add(&hdev->blacklist, &bdaddr, BDADDR_BREDR); 5355e762444SAntti Julku 53609fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 5375e762444SAntti Julku 5385e762444SAntti Julku return err; 539f0358568SJohan Hedberg } 540f0358568SJohan Hedberg 541b2a66aadSAntti Julku static int hci_sock_blacklist_del(struct hci_dev *hdev, void __user *arg) 542f0358568SJohan Hedberg { 543f0358568SJohan Hedberg bdaddr_t bdaddr; 5445e762444SAntti Julku int err; 545f0358568SJohan Hedberg 546f0358568SJohan Hedberg if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) 547f0358568SJohan Hedberg return -EFAULT; 548f0358568SJohan Hedberg 54909fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 5505e762444SAntti Julku 551dcc36c16SJohan Hedberg err = hci_bdaddr_list_del(&hdev->blacklist, &bdaddr, BDADDR_BREDR); 5525e762444SAntti Julku 55309fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 5545e762444SAntti Julku 5555e762444SAntti Julku return err; 556f0358568SJohan Hedberg } 557f0358568SJohan Hedberg 5581da177e4SLinus Torvalds /* Ioctls that require bound socket */ 5596039aa73SGustavo Padovan static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd, 5606039aa73SGustavo Padovan unsigned long arg) 5611da177e4SLinus Torvalds { 5621da177e4SLinus Torvalds struct hci_dev *hdev = hci_pi(sk)->hdev; 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds if (!hdev) 5651da177e4SLinus Torvalds return -EBADFD; 5661da177e4SLinus Torvalds 567d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) 5680736cfa8SMarcel Holtmann return -EBUSY; 5690736cfa8SMarcel Holtmann 570d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 571fee746b0SMarcel Holtmann return -EOPNOTSUPP; 572fee746b0SMarcel Holtmann 5735b69bef5SMarcel Holtmann if (hdev->dev_type != HCI_BREDR) 5745b69bef5SMarcel Holtmann return -EOPNOTSUPP; 5755b69bef5SMarcel Holtmann 5761da177e4SLinus Torvalds switch (cmd) { 5771da177e4SLinus Torvalds case HCISETRAW: 5781da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 579bf5b30b8SZhao Hongjiang return -EPERM; 580db596681SMarcel Holtmann return -EOPNOTSUPP; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds case HCIGETCONNINFO: 5831da177e4SLinus Torvalds return hci_get_conn_info(hdev, (void __user *) arg); 5841da177e4SLinus Torvalds 58540be492fSMarcel Holtmann case HCIGETAUTHINFO: 58640be492fSMarcel Holtmann return hci_get_auth_info(hdev, (void __user *) arg); 58740be492fSMarcel Holtmann 588f0358568SJohan Hedberg case HCIBLOCKADDR: 589f0358568SJohan Hedberg if (!capable(CAP_NET_ADMIN)) 590bf5b30b8SZhao Hongjiang return -EPERM; 591b2a66aadSAntti Julku return hci_sock_blacklist_add(hdev, (void __user *) arg); 592f0358568SJohan Hedberg 593f0358568SJohan Hedberg case HCIUNBLOCKADDR: 594f0358568SJohan Hedberg if (!capable(CAP_NET_ADMIN)) 595bf5b30b8SZhao Hongjiang return -EPERM; 596b2a66aadSAntti Julku return hci_sock_blacklist_del(hdev, (void __user *) arg); 5970736cfa8SMarcel Holtmann } 598f0358568SJohan Hedberg 599324d36edSMarcel Holtmann return -ENOIOCTLCMD; 6001da177e4SLinus Torvalds } 6011da177e4SLinus Torvalds 6028fc9ced3SGustavo Padovan static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, 6038fc9ced3SGustavo Padovan unsigned long arg) 6041da177e4SLinus Torvalds { 6051da177e4SLinus Torvalds void __user *argp = (void __user *) arg; 6060736cfa8SMarcel Holtmann struct sock *sk = sock->sk; 6071da177e4SLinus Torvalds int err; 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds BT_DBG("cmd %x arg %lx", cmd, arg); 6101da177e4SLinus Torvalds 611c1c4f956SMarcel Holtmann lock_sock(sk); 612c1c4f956SMarcel Holtmann 613c1c4f956SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 614c1c4f956SMarcel Holtmann err = -EBADFD; 615c1c4f956SMarcel Holtmann goto done; 616c1c4f956SMarcel Holtmann } 617c1c4f956SMarcel Holtmann 618c1c4f956SMarcel Holtmann release_sock(sk); 619c1c4f956SMarcel Holtmann 6201da177e4SLinus Torvalds switch (cmd) { 6211da177e4SLinus Torvalds case HCIGETDEVLIST: 6221da177e4SLinus Torvalds return hci_get_dev_list(argp); 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds case HCIGETDEVINFO: 6251da177e4SLinus Torvalds return hci_get_dev_info(argp); 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds case HCIGETCONNLIST: 6281da177e4SLinus Torvalds return hci_get_conn_list(argp); 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds case HCIDEVUP: 6311da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 632bf5b30b8SZhao Hongjiang return -EPERM; 6331da177e4SLinus Torvalds return hci_dev_open(arg); 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds case HCIDEVDOWN: 6361da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 637bf5b30b8SZhao Hongjiang return -EPERM; 6381da177e4SLinus Torvalds return hci_dev_close(arg); 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds case HCIDEVRESET: 6411da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 642bf5b30b8SZhao Hongjiang return -EPERM; 6431da177e4SLinus Torvalds return hci_dev_reset(arg); 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds case HCIDEVRESTAT: 6461da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 647bf5b30b8SZhao Hongjiang return -EPERM; 6481da177e4SLinus Torvalds return hci_dev_reset_stat(arg); 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds case HCISETSCAN: 6511da177e4SLinus Torvalds case HCISETAUTH: 6521da177e4SLinus Torvalds case HCISETENCRYPT: 6531da177e4SLinus Torvalds case HCISETPTYPE: 6541da177e4SLinus Torvalds case HCISETLINKPOL: 6551da177e4SLinus Torvalds case HCISETLINKMODE: 6561da177e4SLinus Torvalds case HCISETACLMTU: 6571da177e4SLinus Torvalds case HCISETSCOMTU: 6581da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 659bf5b30b8SZhao Hongjiang return -EPERM; 6601da177e4SLinus Torvalds return hci_dev_cmd(cmd, argp); 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds case HCIINQUIRY: 6631da177e4SLinus Torvalds return hci_inquiry(argp); 664c1c4f956SMarcel Holtmann } 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds lock_sock(sk); 667c1c4f956SMarcel Holtmann 6681da177e4SLinus Torvalds err = hci_sock_bound_ioctl(sk, cmd, arg); 669c1c4f956SMarcel Holtmann 670c1c4f956SMarcel Holtmann done: 6711da177e4SLinus Torvalds release_sock(sk); 6721da177e4SLinus Torvalds return err; 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 6758fc9ced3SGustavo Padovan static int hci_sock_bind(struct socket *sock, struct sockaddr *addr, 6768fc9ced3SGustavo Padovan int addr_len) 6771da177e4SLinus Torvalds { 6780381101fSJohan Hedberg struct sockaddr_hci haddr; 6791da177e4SLinus Torvalds struct sock *sk = sock->sk; 6801da177e4SLinus Torvalds struct hci_dev *hdev = NULL; 6810381101fSJohan Hedberg int len, err = 0; 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 6841da177e4SLinus Torvalds 6850381101fSJohan Hedberg if (!addr) 6860381101fSJohan Hedberg return -EINVAL; 6870381101fSJohan Hedberg 6880381101fSJohan Hedberg memset(&haddr, 0, sizeof(haddr)); 6890381101fSJohan Hedberg len = min_t(unsigned int, sizeof(haddr), addr_len); 6900381101fSJohan Hedberg memcpy(&haddr, addr, len); 6910381101fSJohan Hedberg 6920381101fSJohan Hedberg if (haddr.hci_family != AF_BLUETOOTH) 6930381101fSJohan Hedberg return -EINVAL; 6940381101fSJohan Hedberg 6951da177e4SLinus Torvalds lock_sock(sk); 6961da177e4SLinus Torvalds 6977cc2ade2SMarcel Holtmann if (sk->sk_state == BT_BOUND) { 6987cc2ade2SMarcel Holtmann err = -EALREADY; 6997cc2ade2SMarcel Holtmann goto done; 7007cc2ade2SMarcel Holtmann } 7017cc2ade2SMarcel Holtmann 7027cc2ade2SMarcel Holtmann switch (haddr.hci_channel) { 7037cc2ade2SMarcel Holtmann case HCI_CHANNEL_RAW: 7047cc2ade2SMarcel Holtmann if (hci_pi(sk)->hdev) { 7051da177e4SLinus Torvalds err = -EALREADY; 7061da177e4SLinus Torvalds goto done; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 7090381101fSJohan Hedberg if (haddr.hci_dev != HCI_DEV_NONE) { 7100381101fSJohan Hedberg hdev = hci_dev_get(haddr.hci_dev); 71170f23020SAndrei Emeltchenko if (!hdev) { 7121da177e4SLinus Torvalds err = -ENODEV; 7131da177e4SLinus Torvalds goto done; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds atomic_inc(&hdev->promisc); 7171da177e4SLinus Torvalds } 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds hci_pi(sk)->hdev = hdev; 7207cc2ade2SMarcel Holtmann break; 7217cc2ade2SMarcel Holtmann 72223500189SMarcel Holtmann case HCI_CHANNEL_USER: 72323500189SMarcel Holtmann if (hci_pi(sk)->hdev) { 72423500189SMarcel Holtmann err = -EALREADY; 72523500189SMarcel Holtmann goto done; 72623500189SMarcel Holtmann } 72723500189SMarcel Holtmann 72823500189SMarcel Holtmann if (haddr.hci_dev == HCI_DEV_NONE) { 72923500189SMarcel Holtmann err = -EINVAL; 73023500189SMarcel Holtmann goto done; 73123500189SMarcel Holtmann } 73223500189SMarcel Holtmann 73310a8b86fSMarcel Holtmann if (!capable(CAP_NET_ADMIN)) { 73423500189SMarcel Holtmann err = -EPERM; 73523500189SMarcel Holtmann goto done; 73623500189SMarcel Holtmann } 73723500189SMarcel Holtmann 73823500189SMarcel Holtmann hdev = hci_dev_get(haddr.hci_dev); 73923500189SMarcel Holtmann if (!hdev) { 74023500189SMarcel Holtmann err = -ENODEV; 74123500189SMarcel Holtmann goto done; 74223500189SMarcel Holtmann } 74323500189SMarcel Holtmann 74423500189SMarcel Holtmann if (test_bit(HCI_UP, &hdev->flags) || 74523500189SMarcel Holtmann test_bit(HCI_INIT, &hdev->flags) || 746d7a5a11dSMarcel Holtmann hci_dev_test_flag(hdev, HCI_SETUP) || 747d7a5a11dSMarcel Holtmann hci_dev_test_flag(hdev, HCI_CONFIG)) { 74823500189SMarcel Holtmann err = -EBUSY; 74923500189SMarcel Holtmann hci_dev_put(hdev); 75023500189SMarcel Holtmann goto done; 75123500189SMarcel Holtmann } 75223500189SMarcel Holtmann 753238be788SMarcel Holtmann if (hci_dev_test_and_set_flag(hdev, HCI_USER_CHANNEL)) { 75423500189SMarcel Holtmann err = -EUSERS; 75523500189SMarcel Holtmann hci_dev_put(hdev); 75623500189SMarcel Holtmann goto done; 75723500189SMarcel Holtmann } 75823500189SMarcel Holtmann 75923500189SMarcel Holtmann mgmt_index_removed(hdev); 76023500189SMarcel Holtmann 76123500189SMarcel Holtmann err = hci_dev_open(hdev->id); 76223500189SMarcel Holtmann if (err) { 763a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_USER_CHANNEL); 764c6521401SMarcel Holtmann mgmt_index_added(hdev); 76523500189SMarcel Holtmann hci_dev_put(hdev); 76623500189SMarcel Holtmann goto done; 76723500189SMarcel Holtmann } 76823500189SMarcel Holtmann 76923500189SMarcel Holtmann atomic_inc(&hdev->promisc); 77023500189SMarcel Holtmann 77123500189SMarcel Holtmann hci_pi(sk)->hdev = hdev; 77223500189SMarcel Holtmann break; 77323500189SMarcel Holtmann 774cd82e61cSMarcel Holtmann case HCI_CHANNEL_MONITOR: 775cd82e61cSMarcel Holtmann if (haddr.hci_dev != HCI_DEV_NONE) { 776cd82e61cSMarcel Holtmann err = -EINVAL; 777cd82e61cSMarcel Holtmann goto done; 778cd82e61cSMarcel Holtmann } 779cd82e61cSMarcel Holtmann 780cd82e61cSMarcel Holtmann if (!capable(CAP_NET_RAW)) { 781cd82e61cSMarcel Holtmann err = -EPERM; 782cd82e61cSMarcel Holtmann goto done; 783cd82e61cSMarcel Holtmann } 784cd82e61cSMarcel Holtmann 78550ebc055SMarcel Holtmann /* The monitor interface is restricted to CAP_NET_RAW 78650ebc055SMarcel Holtmann * capabilities and with that implicitly trusted. 78750ebc055SMarcel Holtmann */ 78850ebc055SMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 78950ebc055SMarcel Holtmann 790cd82e61cSMarcel Holtmann send_monitor_replay(sk); 791cd82e61cSMarcel Holtmann 792cd82e61cSMarcel Holtmann atomic_inc(&monitor_promisc); 793cd82e61cSMarcel Holtmann break; 794cd82e61cSMarcel Holtmann 7957cc2ade2SMarcel Holtmann default: 796801c1e8dSJohan Hedberg if (!hci_mgmt_chan_find(haddr.hci_channel)) { 7977cc2ade2SMarcel Holtmann err = -EINVAL; 7987cc2ade2SMarcel Holtmann goto done; 7997cc2ade2SMarcel Holtmann } 8007cc2ade2SMarcel Holtmann 801801c1e8dSJohan Hedberg if (haddr.hci_dev != HCI_DEV_NONE) { 802801c1e8dSJohan Hedberg err = -EINVAL; 803801c1e8dSJohan Hedberg goto done; 804801c1e8dSJohan Hedberg } 805801c1e8dSJohan Hedberg 8061195fbb8SMarcel Holtmann /* Users with CAP_NET_ADMIN capabilities are allowed 8071195fbb8SMarcel Holtmann * access to all management commands and events. For 8081195fbb8SMarcel Holtmann * untrusted users the interface is restricted and 8091195fbb8SMarcel Holtmann * also only untrusted events are sent. 81050ebc055SMarcel Holtmann */ 8111195fbb8SMarcel Holtmann if (capable(CAP_NET_ADMIN)) 81250ebc055SMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 81350ebc055SMarcel Holtmann 814f9207338SMarcel Holtmann /* At the moment the index and unconfigured index events 815f9207338SMarcel Holtmann * are enabled unconditionally. Setting them on each 816f9207338SMarcel Holtmann * socket when binding keeps this functionality. They 817f9207338SMarcel Holtmann * however might be cleared later and then sending of these 818f9207338SMarcel Holtmann * events will be disabled, but that is then intentional. 819f6b7712eSMarcel Holtmann * 820f6b7712eSMarcel Holtmann * This also enables generic events that are safe to be 821f6b7712eSMarcel Holtmann * received by untrusted users. Example for such events 822f6b7712eSMarcel Holtmann * are changes to settings, class of device, name etc. 823f9207338SMarcel Holtmann */ 824f9207338SMarcel Holtmann if (haddr.hci_channel == HCI_CHANNEL_CONTROL) { 825f9207338SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_INDEX_EVENTS); 826f9207338SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); 827f6b7712eSMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_GENERIC_EVENTS); 828f9207338SMarcel Holtmann } 829801c1e8dSJohan Hedberg break; 830801c1e8dSJohan Hedberg } 831801c1e8dSJohan Hedberg 8327cc2ade2SMarcel Holtmann 8337cc2ade2SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 8341da177e4SLinus Torvalds sk->sk_state = BT_BOUND; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds done: 8371da177e4SLinus Torvalds release_sock(sk); 8381da177e4SLinus Torvalds return err; 8391da177e4SLinus Torvalds } 8401da177e4SLinus Torvalds 8418fc9ced3SGustavo Padovan static int hci_sock_getname(struct socket *sock, struct sockaddr *addr, 8428fc9ced3SGustavo Padovan int *addr_len, int peer) 8431da177e4SLinus Torvalds { 8441da177e4SLinus Torvalds struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr; 8451da177e4SLinus Torvalds struct sock *sk = sock->sk; 8469d4b68b2SMarcel Holtmann struct hci_dev *hdev; 8479d4b68b2SMarcel Holtmann int err = 0; 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 8501da177e4SLinus Torvalds 85106f43cbcSMarcel Holtmann if (peer) 85206f43cbcSMarcel Holtmann return -EOPNOTSUPP; 85306f43cbcSMarcel Holtmann 8541da177e4SLinus Torvalds lock_sock(sk); 8551da177e4SLinus Torvalds 8569d4b68b2SMarcel Holtmann hdev = hci_pi(sk)->hdev; 8579d4b68b2SMarcel Holtmann if (!hdev) { 8589d4b68b2SMarcel Holtmann err = -EBADFD; 8599d4b68b2SMarcel Holtmann goto done; 8609d4b68b2SMarcel Holtmann } 8619d4b68b2SMarcel Holtmann 8621da177e4SLinus Torvalds *addr_len = sizeof(*haddr); 8631da177e4SLinus Torvalds haddr->hci_family = AF_BLUETOOTH; 8647b005bd3SMarcel Holtmann haddr->hci_dev = hdev->id; 8659d4b68b2SMarcel Holtmann haddr->hci_channel= hci_pi(sk)->channel; 8661da177e4SLinus Torvalds 8679d4b68b2SMarcel Holtmann done: 8681da177e4SLinus Torvalds release_sock(sk); 8699d4b68b2SMarcel Holtmann return err; 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds 8726039aa73SGustavo Padovan static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg, 8736039aa73SGustavo Padovan struct sk_buff *skb) 8741da177e4SLinus Torvalds { 8751da177e4SLinus Torvalds __u32 mask = hci_pi(sk)->cmsg_mask; 8761da177e4SLinus Torvalds 8770d48d939SMarcel Holtmann if (mask & HCI_CMSG_DIR) { 8780d48d939SMarcel Holtmann int incoming = bt_cb(skb)->incoming; 8798fc9ced3SGustavo Padovan put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming), 8808fc9ced3SGustavo Padovan &incoming); 8810d48d939SMarcel Holtmann } 8821da177e4SLinus Torvalds 883a61bbcf2SPatrick McHardy if (mask & HCI_CMSG_TSTAMP) { 884f6e623a6SJohann Felix Soden #ifdef CONFIG_COMPAT 885f6e623a6SJohann Felix Soden struct compat_timeval ctv; 886f6e623a6SJohann Felix Soden #endif 887a61bbcf2SPatrick McHardy struct timeval tv; 888767c5eb5SMarcel Holtmann void *data; 889767c5eb5SMarcel Holtmann int len; 890a61bbcf2SPatrick McHardy 891a61bbcf2SPatrick McHardy skb_get_timestamp(skb, &tv); 892767c5eb5SMarcel Holtmann 8931da97f83SDavid S. Miller data = &tv; 8941da97f83SDavid S. Miller len = sizeof(tv); 8951da97f83SDavid S. Miller #ifdef CONFIG_COMPAT 896da88cea1SH. J. Lu if (!COMPAT_USE_64BIT_TIME && 897da88cea1SH. J. Lu (msg->msg_flags & MSG_CMSG_COMPAT)) { 898767c5eb5SMarcel Holtmann ctv.tv_sec = tv.tv_sec; 899767c5eb5SMarcel Holtmann ctv.tv_usec = tv.tv_usec; 900767c5eb5SMarcel Holtmann data = &ctv; 901767c5eb5SMarcel Holtmann len = sizeof(ctv); 902767c5eb5SMarcel Holtmann } 9031da97f83SDavid S. Miller #endif 904767c5eb5SMarcel Holtmann 905767c5eb5SMarcel Holtmann put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data); 906a61bbcf2SPatrick McHardy } 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds 9091b784140SYing Xue static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 9101b784140SYing Xue int flags) 9111da177e4SLinus Torvalds { 9121da177e4SLinus Torvalds int noblock = flags & MSG_DONTWAIT; 9131da177e4SLinus Torvalds struct sock *sk = sock->sk; 9141da177e4SLinus Torvalds struct sk_buff *skb; 9151da177e4SLinus Torvalds int copied, err; 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds BT_DBG("sock %p, sk %p", sock, sk); 9181da177e4SLinus Torvalds 9191da177e4SLinus Torvalds if (flags & (MSG_OOB)) 9201da177e4SLinus Torvalds return -EOPNOTSUPP; 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds if (sk->sk_state == BT_CLOSED) 9231da177e4SLinus Torvalds return 0; 9241da177e4SLinus Torvalds 92570f23020SAndrei Emeltchenko skb = skb_recv_datagram(sk, flags, noblock, &err); 92670f23020SAndrei Emeltchenko if (!skb) 9271da177e4SLinus Torvalds return err; 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds copied = skb->len; 9301da177e4SLinus Torvalds if (len < copied) { 9311da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC; 9321da177e4SLinus Torvalds copied = len; 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 935badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 93651f3d02bSDavid S. Miller err = skb_copy_datagram_msg(skb, 0, msg, copied); 9371da177e4SLinus Torvalds 9383a208627SMarcel Holtmann switch (hci_pi(sk)->channel) { 9393a208627SMarcel Holtmann case HCI_CHANNEL_RAW: 9401da177e4SLinus Torvalds hci_sock_cmsg(sk, msg, skb); 9413a208627SMarcel Holtmann break; 94223500189SMarcel Holtmann case HCI_CHANNEL_USER: 943cd82e61cSMarcel Holtmann case HCI_CHANNEL_MONITOR: 944cd82e61cSMarcel Holtmann sock_recv_timestamp(msg, sk, skb); 945cd82e61cSMarcel Holtmann break; 946801c1e8dSJohan Hedberg default: 947801c1e8dSJohan Hedberg if (hci_mgmt_chan_find(hci_pi(sk)->channel)) 948801c1e8dSJohan Hedberg sock_recv_timestamp(msg, sk, skb); 949801c1e8dSJohan Hedberg break; 9503a208627SMarcel Holtmann } 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds skb_free_datagram(sk, skb); 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds return err ? : copied; 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds 957fa4335d7SJohan Hedberg static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk, 958fa4335d7SJohan Hedberg struct msghdr *msg, size_t msglen) 959fa4335d7SJohan Hedberg { 960fa4335d7SJohan Hedberg void *buf; 961fa4335d7SJohan Hedberg u8 *cp; 962fa4335d7SJohan Hedberg struct mgmt_hdr *hdr; 963fa4335d7SJohan Hedberg u16 opcode, index, len; 964fa4335d7SJohan Hedberg struct hci_dev *hdev = NULL; 965fa4335d7SJohan Hedberg const struct hci_mgmt_handler *handler; 966fa4335d7SJohan Hedberg bool var_len, no_hdev; 967fa4335d7SJohan Hedberg int err; 968fa4335d7SJohan Hedberg 969fa4335d7SJohan Hedberg BT_DBG("got %zu bytes", msglen); 970fa4335d7SJohan Hedberg 971fa4335d7SJohan Hedberg if (msglen < sizeof(*hdr)) 972fa4335d7SJohan Hedberg return -EINVAL; 973fa4335d7SJohan Hedberg 974fa4335d7SJohan Hedberg buf = kmalloc(msglen, GFP_KERNEL); 975fa4335d7SJohan Hedberg if (!buf) 976fa4335d7SJohan Hedberg return -ENOMEM; 977fa4335d7SJohan Hedberg 978fa4335d7SJohan Hedberg if (memcpy_from_msg(buf, msg, msglen)) { 979fa4335d7SJohan Hedberg err = -EFAULT; 980fa4335d7SJohan Hedberg goto done; 981fa4335d7SJohan Hedberg } 982fa4335d7SJohan Hedberg 983fa4335d7SJohan Hedberg hdr = buf; 984fa4335d7SJohan Hedberg opcode = __le16_to_cpu(hdr->opcode); 985fa4335d7SJohan Hedberg index = __le16_to_cpu(hdr->index); 986fa4335d7SJohan Hedberg len = __le16_to_cpu(hdr->len); 987fa4335d7SJohan Hedberg 988fa4335d7SJohan Hedberg if (len != msglen - sizeof(*hdr)) { 989fa4335d7SJohan Hedberg err = -EINVAL; 990fa4335d7SJohan Hedberg goto done; 991fa4335d7SJohan Hedberg } 992fa4335d7SJohan Hedberg 993fa4335d7SJohan Hedberg if (opcode >= chan->handler_count || 994fa4335d7SJohan Hedberg chan->handlers[opcode].func == NULL) { 995fa4335d7SJohan Hedberg BT_DBG("Unknown op %u", opcode); 996fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 997fa4335d7SJohan Hedberg MGMT_STATUS_UNKNOWN_COMMAND); 998fa4335d7SJohan Hedberg goto done; 999fa4335d7SJohan Hedberg } 1000fa4335d7SJohan Hedberg 1001fa4335d7SJohan Hedberg handler = &chan->handlers[opcode]; 1002fa4335d7SJohan Hedberg 1003fa4335d7SJohan Hedberg if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) && 1004fa4335d7SJohan Hedberg !(handler->flags & HCI_MGMT_UNTRUSTED)) { 1005fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1006fa4335d7SJohan Hedberg MGMT_STATUS_PERMISSION_DENIED); 1007fa4335d7SJohan Hedberg goto done; 1008fa4335d7SJohan Hedberg } 1009fa4335d7SJohan Hedberg 1010fa4335d7SJohan Hedberg if (index != MGMT_INDEX_NONE) { 1011fa4335d7SJohan Hedberg hdev = hci_dev_get(index); 1012fa4335d7SJohan Hedberg if (!hdev) { 1013fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1014fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1015fa4335d7SJohan Hedberg goto done; 1016fa4335d7SJohan Hedberg } 1017fa4335d7SJohan Hedberg 1018fa4335d7SJohan Hedberg if (hci_dev_test_flag(hdev, HCI_SETUP) || 1019fa4335d7SJohan Hedberg hci_dev_test_flag(hdev, HCI_CONFIG) || 1020fa4335d7SJohan Hedberg hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 1021fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1022fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1023fa4335d7SJohan Hedberg goto done; 1024fa4335d7SJohan Hedberg } 1025fa4335d7SJohan Hedberg 1026fa4335d7SJohan Hedberg if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && 1027fa4335d7SJohan Hedberg !(handler->flags & HCI_MGMT_UNCONFIGURED)) { 1028fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1029fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1030fa4335d7SJohan Hedberg goto done; 1031fa4335d7SJohan Hedberg } 1032fa4335d7SJohan Hedberg } 1033fa4335d7SJohan Hedberg 1034fa4335d7SJohan Hedberg no_hdev = (handler->flags & HCI_MGMT_NO_HDEV); 1035fa4335d7SJohan Hedberg if (no_hdev != !hdev) { 1036fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1037fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1038fa4335d7SJohan Hedberg goto done; 1039fa4335d7SJohan Hedberg } 1040fa4335d7SJohan Hedberg 1041fa4335d7SJohan Hedberg var_len = (handler->flags & HCI_MGMT_VAR_LEN); 1042fa4335d7SJohan Hedberg if ((var_len && len < handler->data_len) || 1043fa4335d7SJohan Hedberg (!var_len && len != handler->data_len)) { 1044fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1045fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_PARAMS); 1046fa4335d7SJohan Hedberg goto done; 1047fa4335d7SJohan Hedberg } 1048fa4335d7SJohan Hedberg 1049fa4335d7SJohan Hedberg if (hdev && chan->hdev_init) 1050fa4335d7SJohan Hedberg chan->hdev_init(sk, hdev); 1051fa4335d7SJohan Hedberg 1052fa4335d7SJohan Hedberg cp = buf + sizeof(*hdr); 1053fa4335d7SJohan Hedberg 1054fa4335d7SJohan Hedberg err = handler->func(sk, hdev, cp, len); 1055fa4335d7SJohan Hedberg if (err < 0) 1056fa4335d7SJohan Hedberg goto done; 1057fa4335d7SJohan Hedberg 1058fa4335d7SJohan Hedberg err = msglen; 1059fa4335d7SJohan Hedberg 1060fa4335d7SJohan Hedberg done: 1061fa4335d7SJohan Hedberg if (hdev) 1062fa4335d7SJohan Hedberg hci_dev_put(hdev); 1063fa4335d7SJohan Hedberg 1064fa4335d7SJohan Hedberg kfree(buf); 1065fa4335d7SJohan Hedberg return err; 1066fa4335d7SJohan Hedberg } 1067fa4335d7SJohan Hedberg 10681b784140SYing Xue static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg, 10691b784140SYing Xue size_t len) 10701da177e4SLinus Torvalds { 10711da177e4SLinus Torvalds struct sock *sk = sock->sk; 1072801c1e8dSJohan Hedberg struct hci_mgmt_chan *chan; 10731da177e4SLinus Torvalds struct hci_dev *hdev; 10741da177e4SLinus Torvalds struct sk_buff *skb; 10751da177e4SLinus Torvalds int err; 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 10781da177e4SLinus Torvalds 10791da177e4SLinus Torvalds if (msg->msg_flags & MSG_OOB) 10801da177e4SLinus Torvalds return -EOPNOTSUPP; 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvalds if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE)) 10831da177e4SLinus Torvalds return -EINVAL; 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds if (len < 4 || len > HCI_MAX_FRAME_SIZE) 10861da177e4SLinus Torvalds return -EINVAL; 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds lock_sock(sk); 10891da177e4SLinus Torvalds 10900381101fSJohan Hedberg switch (hci_pi(sk)->channel) { 10910381101fSJohan Hedberg case HCI_CHANNEL_RAW: 109223500189SMarcel Holtmann case HCI_CHANNEL_USER: 10930381101fSJohan Hedberg break; 1094cd82e61cSMarcel Holtmann case HCI_CHANNEL_MONITOR: 1095cd82e61cSMarcel Holtmann err = -EOPNOTSUPP; 1096cd82e61cSMarcel Holtmann goto done; 10970381101fSJohan Hedberg default: 1098801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock); 1099801c1e8dSJohan Hedberg chan = __hci_mgmt_chan_find(hci_pi(sk)->channel); 1100801c1e8dSJohan Hedberg if (chan) 1101fa4335d7SJohan Hedberg err = hci_mgmt_cmd(chan, sk, msg, len); 1102801c1e8dSJohan Hedberg else 11030381101fSJohan Hedberg err = -EINVAL; 1104801c1e8dSJohan Hedberg 1105801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 11060381101fSJohan Hedberg goto done; 11070381101fSJohan Hedberg } 11080381101fSJohan Hedberg 110970f23020SAndrei Emeltchenko hdev = hci_pi(sk)->hdev; 111070f23020SAndrei Emeltchenko if (!hdev) { 11111da177e4SLinus Torvalds err = -EBADFD; 11121da177e4SLinus Torvalds goto done; 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds 11157e21addcSMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags)) { 11167e21addcSMarcel Holtmann err = -ENETDOWN; 11177e21addcSMarcel Holtmann goto done; 11187e21addcSMarcel Holtmann } 11197e21addcSMarcel Holtmann 112070f23020SAndrei Emeltchenko skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err); 112170f23020SAndrei Emeltchenko if (!skb) 11221da177e4SLinus Torvalds goto done; 11231da177e4SLinus Torvalds 11246ce8e9ceSAl Viro if (memcpy_from_msg(skb_put(skb, len), msg, len)) { 11251da177e4SLinus Torvalds err = -EFAULT; 11261da177e4SLinus Torvalds goto drop; 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds 11290d48d939SMarcel Holtmann bt_cb(skb)->pkt_type = *((unsigned char *) skb->data); 11301da177e4SLinus Torvalds skb_pull(skb, 1); 11311da177e4SLinus Torvalds 11321bc5ad16SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_USER) { 11331bc5ad16SMarcel Holtmann /* No permission check is needed for user channel 11341bc5ad16SMarcel Holtmann * since that gets enforced when binding the socket. 11351bc5ad16SMarcel Holtmann * 11361bc5ad16SMarcel Holtmann * However check that the packet type is valid. 11371bc5ad16SMarcel Holtmann */ 11381bc5ad16SMarcel Holtmann if (bt_cb(skb)->pkt_type != HCI_COMMAND_PKT && 11391bc5ad16SMarcel Holtmann bt_cb(skb)->pkt_type != HCI_ACLDATA_PKT && 11401bc5ad16SMarcel Holtmann bt_cb(skb)->pkt_type != HCI_SCODATA_PKT) { 11411bc5ad16SMarcel Holtmann err = -EINVAL; 11421bc5ad16SMarcel Holtmann goto drop; 11431bc5ad16SMarcel Holtmann } 11441bc5ad16SMarcel Holtmann 11451bc5ad16SMarcel Holtmann skb_queue_tail(&hdev->raw_q, skb); 11461bc5ad16SMarcel Holtmann queue_work(hdev->workqueue, &hdev->tx_work); 11471bc5ad16SMarcel Holtmann } else if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) { 114883985319SHarvey Harrison u16 opcode = get_unaligned_le16(skb->data); 11491da177e4SLinus Torvalds u16 ogf = hci_opcode_ogf(opcode); 11501da177e4SLinus Torvalds u16 ocf = hci_opcode_ocf(opcode); 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds if (((ogf > HCI_SFLT_MAX_OGF) || 11533bb3c755SGustavo Padovan !hci_test_bit(ocf & HCI_FLT_OCF_BITS, 11543bb3c755SGustavo Padovan &hci_sec_filter.ocf_mask[ogf])) && 11551da177e4SLinus Torvalds !capable(CAP_NET_RAW)) { 11561da177e4SLinus Torvalds err = -EPERM; 11571da177e4SLinus Torvalds goto drop; 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds 1160fee746b0SMarcel Holtmann if (ogf == 0x3f) { 11611da177e4SLinus Torvalds skb_queue_tail(&hdev->raw_q, skb); 11623eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work); 11631da177e4SLinus Torvalds } else { 116449c922bbSStephen Hemminger /* Stand-alone HCI commands must be flagged as 116511714b3dSJohan Hedberg * single-command requests. 116611714b3dSJohan Hedberg */ 1167db6e3e8dSJohan Hedberg bt_cb(skb)->req.start = true; 116811714b3dSJohan Hedberg 11691da177e4SLinus Torvalds skb_queue_tail(&hdev->cmd_q, skb); 1170c347b765SGustavo F. Padovan queue_work(hdev->workqueue, &hdev->cmd_work); 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds } else { 11731da177e4SLinus Torvalds if (!capable(CAP_NET_RAW)) { 11741da177e4SLinus Torvalds err = -EPERM; 11751da177e4SLinus Torvalds goto drop; 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds skb_queue_tail(&hdev->raw_q, skb); 11793eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work); 11801da177e4SLinus Torvalds } 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds err = len; 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds done: 11851da177e4SLinus Torvalds release_sock(sk); 11861da177e4SLinus Torvalds return err; 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds drop: 11891da177e4SLinus Torvalds kfree_skb(skb); 11901da177e4SLinus Torvalds goto done; 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds 11938fc9ced3SGustavo Padovan static int hci_sock_setsockopt(struct socket *sock, int level, int optname, 11948fc9ced3SGustavo Padovan char __user *optval, unsigned int len) 11951da177e4SLinus Torvalds { 11961da177e4SLinus Torvalds struct hci_ufilter uf = { .opcode = 0 }; 11971da177e4SLinus Torvalds struct sock *sk = sock->sk; 11981da177e4SLinus Torvalds int err = 0, opt = 0; 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds BT_DBG("sk %p, opt %d", sk, optname); 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds lock_sock(sk); 12031da177e4SLinus Torvalds 12042f39cdb7SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 1205c2371e80SMarcel Holtmann err = -EBADFD; 12062f39cdb7SMarcel Holtmann goto done; 12072f39cdb7SMarcel Holtmann } 12082f39cdb7SMarcel Holtmann 12091da177e4SLinus Torvalds switch (optname) { 12101da177e4SLinus Torvalds case HCI_DATA_DIR: 12111da177e4SLinus Torvalds if (get_user(opt, (int __user *)optval)) { 12121da177e4SLinus Torvalds err = -EFAULT; 12131da177e4SLinus Torvalds break; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds if (opt) 12171da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR; 12181da177e4SLinus Torvalds else 12191da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR; 12201da177e4SLinus Torvalds break; 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds case HCI_TIME_STAMP: 12231da177e4SLinus Torvalds if (get_user(opt, (int __user *)optval)) { 12241da177e4SLinus Torvalds err = -EFAULT; 12251da177e4SLinus Torvalds break; 12261da177e4SLinus Torvalds } 12271da177e4SLinus Torvalds 12281da177e4SLinus Torvalds if (opt) 12291da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP; 12301da177e4SLinus Torvalds else 12311da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP; 12321da177e4SLinus Torvalds break; 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds case HCI_FILTER: 12350878b666SMarcel Holtmann { 12360878b666SMarcel Holtmann struct hci_filter *f = &hci_pi(sk)->filter; 12370878b666SMarcel Holtmann 12380878b666SMarcel Holtmann uf.type_mask = f->type_mask; 12390878b666SMarcel Holtmann uf.opcode = f->opcode; 12400878b666SMarcel Holtmann uf.event_mask[0] = *((u32 *) f->event_mask + 0); 12410878b666SMarcel Holtmann uf.event_mask[1] = *((u32 *) f->event_mask + 1); 12420878b666SMarcel Holtmann } 12430878b666SMarcel Holtmann 12441da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(uf)); 12451da177e4SLinus Torvalds if (copy_from_user(&uf, optval, len)) { 12461da177e4SLinus Torvalds err = -EFAULT; 12471da177e4SLinus Torvalds break; 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds if (!capable(CAP_NET_RAW)) { 12511da177e4SLinus Torvalds uf.type_mask &= hci_sec_filter.type_mask; 12521da177e4SLinus Torvalds uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0); 12531da177e4SLinus Torvalds uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1); 12541da177e4SLinus Torvalds } 12551da177e4SLinus Torvalds 12561da177e4SLinus Torvalds { 12571da177e4SLinus Torvalds struct hci_filter *f = &hci_pi(sk)->filter; 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds f->type_mask = uf.type_mask; 12601da177e4SLinus Torvalds f->opcode = uf.opcode; 12611da177e4SLinus Torvalds *((u32 *) f->event_mask + 0) = uf.event_mask[0]; 12621da177e4SLinus Torvalds *((u32 *) f->event_mask + 1) = uf.event_mask[1]; 12631da177e4SLinus Torvalds } 12641da177e4SLinus Torvalds break; 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds default: 12671da177e4SLinus Torvalds err = -ENOPROTOOPT; 12681da177e4SLinus Torvalds break; 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds 12712f39cdb7SMarcel Holtmann done: 12721da177e4SLinus Torvalds release_sock(sk); 12731da177e4SLinus Torvalds return err; 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds 12768fc9ced3SGustavo Padovan static int hci_sock_getsockopt(struct socket *sock, int level, int optname, 12778fc9ced3SGustavo Padovan char __user *optval, int __user *optlen) 12781da177e4SLinus Torvalds { 12791da177e4SLinus Torvalds struct hci_ufilter uf; 12801da177e4SLinus Torvalds struct sock *sk = sock->sk; 1281cedc5469SMarcel Holtmann int len, opt, err = 0; 1282cedc5469SMarcel Holtmann 1283cedc5469SMarcel Holtmann BT_DBG("sk %p, opt %d", sk, optname); 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds if (get_user(len, optlen)) 12861da177e4SLinus Torvalds return -EFAULT; 12871da177e4SLinus Torvalds 1288cedc5469SMarcel Holtmann lock_sock(sk); 1289cedc5469SMarcel Holtmann 1290cedc5469SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 1291c2371e80SMarcel Holtmann err = -EBADFD; 1292cedc5469SMarcel Holtmann goto done; 1293cedc5469SMarcel Holtmann } 1294cedc5469SMarcel Holtmann 12951da177e4SLinus Torvalds switch (optname) { 12961da177e4SLinus Torvalds case HCI_DATA_DIR: 12971da177e4SLinus Torvalds if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR) 12981da177e4SLinus Torvalds opt = 1; 12991da177e4SLinus Torvalds else 13001da177e4SLinus Torvalds opt = 0; 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds if (put_user(opt, optval)) 1303cedc5469SMarcel Holtmann err = -EFAULT; 13041da177e4SLinus Torvalds break; 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds case HCI_TIME_STAMP: 13071da177e4SLinus Torvalds if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP) 13081da177e4SLinus Torvalds opt = 1; 13091da177e4SLinus Torvalds else 13101da177e4SLinus Torvalds opt = 0; 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds if (put_user(opt, optval)) 1313cedc5469SMarcel Holtmann err = -EFAULT; 13141da177e4SLinus Torvalds break; 13151da177e4SLinus Torvalds 13161da177e4SLinus Torvalds case HCI_FILTER: 13171da177e4SLinus Torvalds { 13181da177e4SLinus Torvalds struct hci_filter *f = &hci_pi(sk)->filter; 13191da177e4SLinus Torvalds 1320e15ca9a0SMathias Krause memset(&uf, 0, sizeof(uf)); 13211da177e4SLinus Torvalds uf.type_mask = f->type_mask; 13221da177e4SLinus Torvalds uf.opcode = f->opcode; 13231da177e4SLinus Torvalds uf.event_mask[0] = *((u32 *) f->event_mask + 0); 13241da177e4SLinus Torvalds uf.event_mask[1] = *((u32 *) f->event_mask + 1); 13251da177e4SLinus Torvalds } 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(uf)); 13281da177e4SLinus Torvalds if (copy_to_user(optval, &uf, len)) 1329cedc5469SMarcel Holtmann err = -EFAULT; 13301da177e4SLinus Torvalds break; 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds default: 1333cedc5469SMarcel Holtmann err = -ENOPROTOOPT; 13341da177e4SLinus Torvalds break; 13351da177e4SLinus Torvalds } 13361da177e4SLinus Torvalds 1337cedc5469SMarcel Holtmann done: 1338cedc5469SMarcel Holtmann release_sock(sk); 1339cedc5469SMarcel Holtmann return err; 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds 134290ddc4f0SEric Dumazet static const struct proto_ops hci_sock_ops = { 13431da177e4SLinus Torvalds .family = PF_BLUETOOTH, 13441da177e4SLinus Torvalds .owner = THIS_MODULE, 13451da177e4SLinus Torvalds .release = hci_sock_release, 13461da177e4SLinus Torvalds .bind = hci_sock_bind, 13471da177e4SLinus Torvalds .getname = hci_sock_getname, 13481da177e4SLinus Torvalds .sendmsg = hci_sock_sendmsg, 13491da177e4SLinus Torvalds .recvmsg = hci_sock_recvmsg, 13501da177e4SLinus Torvalds .ioctl = hci_sock_ioctl, 13511da177e4SLinus Torvalds .poll = datagram_poll, 13521da177e4SLinus Torvalds .listen = sock_no_listen, 13531da177e4SLinus Torvalds .shutdown = sock_no_shutdown, 13541da177e4SLinus Torvalds .setsockopt = hci_sock_setsockopt, 13551da177e4SLinus Torvalds .getsockopt = hci_sock_getsockopt, 13561da177e4SLinus Torvalds .connect = sock_no_connect, 13571da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 13581da177e4SLinus Torvalds .accept = sock_no_accept, 13591da177e4SLinus Torvalds .mmap = sock_no_mmap 13601da177e4SLinus Torvalds }; 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds static struct proto hci_sk_proto = { 13631da177e4SLinus Torvalds .name = "HCI", 13641da177e4SLinus Torvalds .owner = THIS_MODULE, 13651da177e4SLinus Torvalds .obj_size = sizeof(struct hci_pinfo) 13661da177e4SLinus Torvalds }; 13671da177e4SLinus Torvalds 13683f378b68SEric Paris static int hci_sock_create(struct net *net, struct socket *sock, int protocol, 13693f378b68SEric Paris int kern) 13701da177e4SLinus Torvalds { 13711da177e4SLinus Torvalds struct sock *sk; 13721da177e4SLinus Torvalds 13731da177e4SLinus Torvalds BT_DBG("sock %p", sock); 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds if (sock->type != SOCK_RAW) 13761da177e4SLinus Torvalds return -ESOCKTNOSUPPORT; 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds sock->ops = &hci_sock_ops; 13791da177e4SLinus Torvalds 1380*11aa9c28SEric W. Biederman sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto, kern); 13811da177e4SLinus Torvalds if (!sk) 13821da177e4SLinus Torvalds return -ENOMEM; 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds sock_init_data(sock, sk); 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds sock_reset_flag(sk, SOCK_ZAPPED); 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds sk->sk_protocol = protocol; 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds sock->state = SS_UNCONNECTED; 13911da177e4SLinus Torvalds sk->sk_state = BT_OPEN; 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds bt_sock_link(&hci_sk_list, sk); 13941da177e4SLinus Torvalds return 0; 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds 1397ec1b4cf7SStephen Hemminger static const struct net_proto_family hci_sock_family_ops = { 13981da177e4SLinus Torvalds .family = PF_BLUETOOTH, 13991da177e4SLinus Torvalds .owner = THIS_MODULE, 14001da177e4SLinus Torvalds .create = hci_sock_create, 14011da177e4SLinus Torvalds }; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds int __init hci_sock_init(void) 14041da177e4SLinus Torvalds { 14051da177e4SLinus Torvalds int err; 14061da177e4SLinus Torvalds 1407b0a8e282SMarcel Holtmann BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr)); 1408b0a8e282SMarcel Holtmann 14091da177e4SLinus Torvalds err = proto_register(&hci_sk_proto, 0); 14101da177e4SLinus Torvalds if (err < 0) 14111da177e4SLinus Torvalds return err; 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops); 1414f7c86637SMasatake YAMATO if (err < 0) { 1415f7c86637SMasatake YAMATO BT_ERR("HCI socket registration failed"); 14161da177e4SLinus Torvalds goto error; 1417f7c86637SMasatake YAMATO } 1418f7c86637SMasatake YAMATO 1419b0316615SAl Viro err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL); 1420f7c86637SMasatake YAMATO if (err < 0) { 1421f7c86637SMasatake YAMATO BT_ERR("Failed to create HCI proc file"); 1422f7c86637SMasatake YAMATO bt_sock_unregister(BTPROTO_HCI); 1423f7c86637SMasatake YAMATO goto error; 1424f7c86637SMasatake YAMATO } 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds BT_INFO("HCI socket layer initialized"); 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds return 0; 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds error: 14311da177e4SLinus Torvalds proto_unregister(&hci_sk_proto); 14321da177e4SLinus Torvalds return err; 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds 1435b7440a14SAnand Gadiyar void hci_sock_cleanup(void) 14361da177e4SLinus Torvalds { 1437f7c86637SMasatake YAMATO bt_procfs_cleanup(&init_net, "hci"); 14385e9d7f86SDavid Herrmann bt_sock_unregister(BTPROTO_HCI); 14391da177e4SLinus Torvalds proto_unregister(&hci_sk_proto); 14401da177e4SLinus Torvalds } 1441