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. */ 267a6038b3SArnd Bergmann #include <linux/compat.h> 278c520a59SGustavo Padovan #include <linux/export.h> 28787b306cSJohannes Berg #include <linux/utsname.h> 2970ecce91SMarcel Holtmann #include <linux/sched.h> 301da177e4SLinus Torvalds #include <asm/unaligned.h> 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds #include <net/bluetooth/bluetooth.h> 331da177e4SLinus Torvalds #include <net/bluetooth/hci_core.h> 34cd82e61cSMarcel Holtmann #include <net/bluetooth/hci_mon.h> 35fa4335d7SJohan Hedberg #include <net/bluetooth/mgmt.h> 36fa4335d7SJohan Hedberg 37fa4335d7SJohan Hedberg #include "mgmt_util.h" 381da177e4SLinus Torvalds 39801c1e8dSJohan Hedberg static LIST_HEAD(mgmt_chan_list); 40801c1e8dSJohan Hedberg static DEFINE_MUTEX(mgmt_chan_list_lock); 41801c1e8dSJohan Hedberg 4270ecce91SMarcel Holtmann static DEFINE_IDA(sock_cookie_ida); 4370ecce91SMarcel Holtmann 44cd82e61cSMarcel Holtmann static atomic_t monitor_promisc = ATOMIC_INIT(0); 45cd82e61cSMarcel Holtmann 461da177e4SLinus Torvalds /* ----- HCI socket interface ----- */ 471da177e4SLinus Torvalds 48863def58SMarcel Holtmann /* Socket info */ 49863def58SMarcel Holtmann #define hci_pi(sk) ((struct hci_pinfo *) sk) 50863def58SMarcel Holtmann 51863def58SMarcel Holtmann struct hci_pinfo { 52863def58SMarcel Holtmann struct bt_sock bt; 53863def58SMarcel Holtmann struct hci_dev *hdev; 54863def58SMarcel Holtmann struct hci_filter filter; 5532929e1fSAlain Michaud __u8 cmsg_mask; 56863def58SMarcel Holtmann unsigned short channel; 576befc644SMarcel Holtmann unsigned long flags; 5870ecce91SMarcel Holtmann __u32 cookie; 5970ecce91SMarcel Holtmann char comm[TASK_COMM_LEN]; 6009572fcaSLuiz Augusto von Dentz __u16 mtu; 61863def58SMarcel Holtmann }; 62863def58SMarcel Holtmann 63e0448092STetsuo Handa static struct hci_dev *hci_hdev_from_sock(struct sock *sk) 64e0448092STetsuo Handa { 65e0448092STetsuo Handa struct hci_dev *hdev = hci_pi(sk)->hdev; 66e0448092STetsuo Handa 67e0448092STetsuo Handa if (!hdev) 68e0448092STetsuo Handa return ERR_PTR(-EBADFD); 69e0448092STetsuo Handa if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) 70e0448092STetsuo Handa return ERR_PTR(-EPIPE); 71e0448092STetsuo Handa return hdev; 72e0448092STetsuo Handa } 73e0448092STetsuo Handa 746befc644SMarcel Holtmann void hci_sock_set_flag(struct sock *sk, int nr) 756befc644SMarcel Holtmann { 766befc644SMarcel Holtmann set_bit(nr, &hci_pi(sk)->flags); 776befc644SMarcel Holtmann } 786befc644SMarcel Holtmann 796befc644SMarcel Holtmann void hci_sock_clear_flag(struct sock *sk, int nr) 806befc644SMarcel Holtmann { 816befc644SMarcel Holtmann clear_bit(nr, &hci_pi(sk)->flags); 826befc644SMarcel Holtmann } 836befc644SMarcel Holtmann 84c85be545SMarcel Holtmann int hci_sock_test_flag(struct sock *sk, int nr) 85c85be545SMarcel Holtmann { 86c85be545SMarcel Holtmann return test_bit(nr, &hci_pi(sk)->flags); 87c85be545SMarcel Holtmann } 88c85be545SMarcel Holtmann 89d0f172b1SJohan Hedberg unsigned short hci_sock_get_channel(struct sock *sk) 90d0f172b1SJohan Hedberg { 91d0f172b1SJohan Hedberg return hci_pi(sk)->channel; 92d0f172b1SJohan Hedberg } 93d0f172b1SJohan Hedberg 9470ecce91SMarcel Holtmann u32 hci_sock_get_cookie(struct sock *sk) 9570ecce91SMarcel Holtmann { 9670ecce91SMarcel Holtmann return hci_pi(sk)->cookie; 9770ecce91SMarcel Holtmann } 9870ecce91SMarcel Holtmann 99df1cb87aSMarcel Holtmann static bool hci_sock_gen_cookie(struct sock *sk) 100df1cb87aSMarcel Holtmann { 101df1cb87aSMarcel Holtmann int id = hci_pi(sk)->cookie; 102df1cb87aSMarcel Holtmann 103df1cb87aSMarcel Holtmann if (!id) { 104df1cb87aSMarcel Holtmann id = ida_simple_get(&sock_cookie_ida, 1, 0, GFP_KERNEL); 105df1cb87aSMarcel Holtmann if (id < 0) 106df1cb87aSMarcel Holtmann id = 0xffffffff; 107df1cb87aSMarcel Holtmann 108df1cb87aSMarcel Holtmann hci_pi(sk)->cookie = id; 109df1cb87aSMarcel Holtmann get_task_comm(hci_pi(sk)->comm, current); 110df1cb87aSMarcel Holtmann return true; 111df1cb87aSMarcel Holtmann } 112df1cb87aSMarcel Holtmann 113df1cb87aSMarcel Holtmann return false; 114df1cb87aSMarcel Holtmann } 115df1cb87aSMarcel Holtmann 116df1cb87aSMarcel Holtmann static void hci_sock_free_cookie(struct sock *sk) 117df1cb87aSMarcel Holtmann { 118df1cb87aSMarcel Holtmann int id = hci_pi(sk)->cookie; 119df1cb87aSMarcel Holtmann 120df1cb87aSMarcel Holtmann if (id) { 121df1cb87aSMarcel Holtmann hci_pi(sk)->cookie = 0xffffffff; 122df1cb87aSMarcel Holtmann ida_simple_remove(&sock_cookie_ida, id); 123df1cb87aSMarcel Holtmann } 124df1cb87aSMarcel Holtmann } 125df1cb87aSMarcel Holtmann 1269391976aSJiri Slaby static inline int hci_test_bit(int nr, const void *addr) 1271da177e4SLinus Torvalds { 1289391976aSJiri Slaby return *((const __u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31)); 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /* Security filter */ 1323ad254f7SMarcel Holtmann #define HCI_SFLT_MAX_OGF 5 1333ad254f7SMarcel Holtmann 1343ad254f7SMarcel Holtmann struct hci_sec_filter { 1353ad254f7SMarcel Holtmann __u32 type_mask; 1363ad254f7SMarcel Holtmann __u32 event_mask[2]; 1373ad254f7SMarcel Holtmann __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4]; 1383ad254f7SMarcel Holtmann }; 1393ad254f7SMarcel Holtmann 1407e67c112SMarcel Holtmann static const struct hci_sec_filter hci_sec_filter = { 1411da177e4SLinus Torvalds /* Packet types */ 1421da177e4SLinus Torvalds 0x10, 1431da177e4SLinus Torvalds /* Events */ 144dd7f5527SMarcel Holtmann { 0x1000d9fe, 0x0000b00c }, 1451da177e4SLinus Torvalds /* Commands */ 1461da177e4SLinus Torvalds { 1471da177e4SLinus Torvalds { 0x0 }, 1481da177e4SLinus Torvalds /* OGF_LINK_CTL */ 1497c631a67SMarcel Holtmann { 0xbe000006, 0x00000001, 0x00000000, 0x00 }, 1501da177e4SLinus Torvalds /* OGF_LINK_POLICY */ 1517c631a67SMarcel Holtmann { 0x00005200, 0x00000000, 0x00000000, 0x00 }, 1521da177e4SLinus Torvalds /* OGF_HOST_CTL */ 1537c631a67SMarcel Holtmann { 0xaab00200, 0x2b402aaa, 0x05220154, 0x00 }, 1541da177e4SLinus Torvalds /* OGF_INFO_PARAM */ 1557c631a67SMarcel Holtmann { 0x000002be, 0x00000000, 0x00000000, 0x00 }, 1561da177e4SLinus Torvalds /* OGF_STATUS_PARAM */ 1577c631a67SMarcel Holtmann { 0x000000ea, 0x00000000, 0x00000000, 0x00 } 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds }; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds static struct bt_sock_list hci_sk_list = { 162d5fb2962SRobert P. J. Day .lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock) 1631da177e4SLinus Torvalds }; 1641da177e4SLinus Torvalds 165f81fe64fSMarcel Holtmann static bool is_filtered_packet(struct sock *sk, struct sk_buff *skb) 166f81fe64fSMarcel Holtmann { 167f81fe64fSMarcel Holtmann struct hci_filter *flt; 168f81fe64fSMarcel Holtmann int flt_type, flt_event; 169f81fe64fSMarcel Holtmann 170f81fe64fSMarcel Holtmann /* Apply filter */ 171f81fe64fSMarcel Holtmann flt = &hci_pi(sk)->filter; 172f81fe64fSMarcel Holtmann 173d79f34e3SMarcel Holtmann flt_type = hci_skb_pkt_type(skb) & HCI_FLT_TYPE_BITS; 174f81fe64fSMarcel Holtmann 175f81fe64fSMarcel Holtmann if (!test_bit(flt_type, &flt->type_mask)) 176f81fe64fSMarcel Holtmann return true; 177f81fe64fSMarcel Holtmann 178f81fe64fSMarcel Holtmann /* Extra filter for event packets only */ 179d79f34e3SMarcel Holtmann if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT) 180f81fe64fSMarcel Holtmann return false; 181f81fe64fSMarcel Holtmann 182f81fe64fSMarcel Holtmann flt_event = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS); 183f81fe64fSMarcel Holtmann 184f81fe64fSMarcel Holtmann if (!hci_test_bit(flt_event, &flt->event_mask)) 185f81fe64fSMarcel Holtmann return true; 186f81fe64fSMarcel Holtmann 187f81fe64fSMarcel Holtmann /* Check filter only when opcode is set */ 188f81fe64fSMarcel Holtmann if (!flt->opcode) 189f81fe64fSMarcel Holtmann return false; 190f81fe64fSMarcel Holtmann 191f81fe64fSMarcel Holtmann if (flt_event == HCI_EV_CMD_COMPLETE && 192f81fe64fSMarcel Holtmann flt->opcode != get_unaligned((__le16 *)(skb->data + 3))) 193f81fe64fSMarcel Holtmann return true; 194f81fe64fSMarcel Holtmann 195f81fe64fSMarcel Holtmann if (flt_event == HCI_EV_CMD_STATUS && 196f81fe64fSMarcel Holtmann flt->opcode != get_unaligned((__le16 *)(skb->data + 4))) 197f81fe64fSMarcel Holtmann return true; 198f81fe64fSMarcel Holtmann 199f81fe64fSMarcel Holtmann return false; 200f81fe64fSMarcel Holtmann } 201f81fe64fSMarcel Holtmann 2021da177e4SLinus Torvalds /* Send frame to RAW socket */ 203470fe1b5SMarcel Holtmann void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb) 2041da177e4SLinus Torvalds { 2051da177e4SLinus Torvalds struct sock *sk; 206e0edf373SMarcel Holtmann struct sk_buff *skb_copy = NULL; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds BT_DBG("hdev %p len %d", hdev, skb->len); 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds read_lock(&hci_sk_list.lock); 211470fe1b5SMarcel Holtmann 212b67bfe0dSSasha Levin sk_for_each(sk, &hci_sk_list.head) { 2131da177e4SLinus Torvalds struct sk_buff *nskb; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev) 2161da177e4SLinus Torvalds continue; 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds /* Don't send frame to the socket it came from */ 2191da177e4SLinus Torvalds if (skb->sk == sk) 2201da177e4SLinus Torvalds continue; 2211da177e4SLinus Torvalds 22223500189SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_RAW) { 223d79f34e3SMarcel Holtmann if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT && 224d79f34e3SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_EVENT_PKT && 225d79f34e3SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT && 226cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_SCODATA_PKT && 227cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) 228bb77543eSMarcel Holtmann continue; 229f81fe64fSMarcel Holtmann if (is_filtered_packet(sk, skb)) 2301da177e4SLinus Torvalds continue; 23123500189SMarcel Holtmann } else if (hci_pi(sk)->channel == HCI_CHANNEL_USER) { 23223500189SMarcel Holtmann if (!bt_cb(skb)->incoming) 23323500189SMarcel Holtmann continue; 234d79f34e3SMarcel Holtmann if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT && 235d79f34e3SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT && 236cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_SCODATA_PKT && 237cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) 23823500189SMarcel Holtmann continue; 23923500189SMarcel Holtmann } else { 24023500189SMarcel Holtmann /* Don't send frame to other channel types */ 24123500189SMarcel Holtmann continue; 24223500189SMarcel Holtmann } 2431da177e4SLinus Torvalds 244e0edf373SMarcel Holtmann if (!skb_copy) { 245e0edf373SMarcel Holtmann /* Create a private copy with headroom */ 246bad93e9dSOctavian Purdila skb_copy = __pskb_copy_fclone(skb, 1, GFP_ATOMIC, true); 247e0edf373SMarcel Holtmann if (!skb_copy) 2481da177e4SLinus Torvalds continue; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds /* Put type byte before the data */ 251d79f34e3SMarcel Holtmann memcpy(skb_push(skb_copy, 1), &hci_skb_pkt_type(skb), 1); 252e0edf373SMarcel Holtmann } 253e0edf373SMarcel Holtmann 254e0edf373SMarcel Holtmann nskb = skb_clone(skb_copy, GFP_ATOMIC); 255e0edf373SMarcel Holtmann if (!nskb) 256e0edf373SMarcel Holtmann continue; 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds if (sock_queue_rcv_skb(sk, nskb)) 2591da177e4SLinus Torvalds kfree_skb(nskb); 2601da177e4SLinus Torvalds } 261470fe1b5SMarcel Holtmann 262470fe1b5SMarcel Holtmann read_unlock(&hci_sk_list.lock); 263e0edf373SMarcel Holtmann 264e0edf373SMarcel Holtmann kfree_skb(skb_copy); 265470fe1b5SMarcel Holtmann } 266470fe1b5SMarcel Holtmann 2677129069eSJohan Hedberg /* Send frame to sockets with specific channel */ 268a9ee77afSSebastian Andrzej Siewior static void __hci_send_to_channel(unsigned short channel, struct sk_buff *skb, 269c08b1a1dSMarcel Holtmann int flag, struct sock *skip_sk) 270470fe1b5SMarcel Holtmann { 271470fe1b5SMarcel Holtmann struct sock *sk; 272470fe1b5SMarcel Holtmann 2737129069eSJohan Hedberg BT_DBG("channel %u len %d", channel, skb->len); 274470fe1b5SMarcel Holtmann 275b67bfe0dSSasha Levin sk_for_each(sk, &hci_sk_list.head) { 276470fe1b5SMarcel Holtmann struct sk_buff *nskb; 277470fe1b5SMarcel Holtmann 278c08b1a1dSMarcel Holtmann /* Ignore socket without the flag set */ 279c85be545SMarcel Holtmann if (!hci_sock_test_flag(sk, flag)) 280c08b1a1dSMarcel Holtmann continue; 281c08b1a1dSMarcel Holtmann 282470fe1b5SMarcel Holtmann /* Skip the original socket */ 283470fe1b5SMarcel Holtmann if (sk == skip_sk) 284470fe1b5SMarcel Holtmann continue; 285470fe1b5SMarcel Holtmann 286470fe1b5SMarcel Holtmann if (sk->sk_state != BT_BOUND) 287470fe1b5SMarcel Holtmann continue; 288470fe1b5SMarcel Holtmann 2897129069eSJohan Hedberg if (hci_pi(sk)->channel != channel) 290d7f72f61SMarcel Holtmann continue; 291d7f72f61SMarcel Holtmann 292d7f72f61SMarcel Holtmann nskb = skb_clone(skb, GFP_ATOMIC); 293d7f72f61SMarcel Holtmann if (!nskb) 294d7f72f61SMarcel Holtmann continue; 295d7f72f61SMarcel Holtmann 296d7f72f61SMarcel Holtmann if (sock_queue_rcv_skb(sk, nskb)) 297d7f72f61SMarcel Holtmann kfree_skb(nskb); 298d7f72f61SMarcel Holtmann } 299d7f72f61SMarcel Holtmann 300a9ee77afSSebastian Andrzej Siewior } 301a9ee77afSSebastian Andrzej Siewior 302a9ee77afSSebastian Andrzej Siewior void hci_send_to_channel(unsigned short channel, struct sk_buff *skb, 303a9ee77afSSebastian Andrzej Siewior int flag, struct sock *skip_sk) 304a9ee77afSSebastian Andrzej Siewior { 305a9ee77afSSebastian Andrzej Siewior read_lock(&hci_sk_list.lock); 306a9ee77afSSebastian Andrzej Siewior __hci_send_to_channel(channel, skb, flag, skip_sk); 307d7f72f61SMarcel Holtmann read_unlock(&hci_sk_list.lock); 308d7f72f61SMarcel Holtmann } 309d7f72f61SMarcel Holtmann 310cd82e61cSMarcel Holtmann /* Send frame to monitor socket */ 311cd82e61cSMarcel Holtmann void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb) 312cd82e61cSMarcel Holtmann { 313cd82e61cSMarcel Holtmann struct sk_buff *skb_copy = NULL; 3142b531294SMarcel Holtmann struct hci_mon_hdr *hdr; 315cd82e61cSMarcel Holtmann __le16 opcode; 316cd82e61cSMarcel Holtmann 317cd82e61cSMarcel Holtmann if (!atomic_read(&monitor_promisc)) 318cd82e61cSMarcel Holtmann return; 319cd82e61cSMarcel Holtmann 320cd82e61cSMarcel Holtmann BT_DBG("hdev %p len %d", hdev, skb->len); 321cd82e61cSMarcel Holtmann 322d79f34e3SMarcel Holtmann switch (hci_skb_pkt_type(skb)) { 323cd82e61cSMarcel Holtmann case HCI_COMMAND_PKT: 324dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_COMMAND_PKT); 325cd82e61cSMarcel Holtmann break; 326cd82e61cSMarcel Holtmann case HCI_EVENT_PKT: 327dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_EVENT_PKT); 328cd82e61cSMarcel Holtmann break; 329cd82e61cSMarcel Holtmann case HCI_ACLDATA_PKT: 330cd82e61cSMarcel Holtmann if (bt_cb(skb)->incoming) 331dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_ACL_RX_PKT); 332cd82e61cSMarcel Holtmann else 333dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_ACL_TX_PKT); 334cd82e61cSMarcel Holtmann break; 335cd82e61cSMarcel Holtmann case HCI_SCODATA_PKT: 336cd82e61cSMarcel Holtmann if (bt_cb(skb)->incoming) 337dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_SCO_RX_PKT); 338cd82e61cSMarcel Holtmann else 339dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_SCO_TX_PKT); 340cd82e61cSMarcel Holtmann break; 341f9a619dbSLuiz Augusto von Dentz case HCI_ISODATA_PKT: 342f9a619dbSLuiz Augusto von Dentz if (bt_cb(skb)->incoming) 343f9a619dbSLuiz Augusto von Dentz opcode = cpu_to_le16(HCI_MON_ISO_RX_PKT); 344f9a619dbSLuiz Augusto von Dentz else 345f9a619dbSLuiz Augusto von Dentz opcode = cpu_to_le16(HCI_MON_ISO_TX_PKT); 346f9a619dbSLuiz Augusto von Dentz break; 347e875ff84SMarcel Holtmann case HCI_DIAG_PKT: 348e875ff84SMarcel Holtmann opcode = cpu_to_le16(HCI_MON_VENDOR_DIAG); 349e875ff84SMarcel Holtmann break; 350cd82e61cSMarcel Holtmann default: 351cd82e61cSMarcel Holtmann return; 352cd82e61cSMarcel Holtmann } 353cd82e61cSMarcel Holtmann 3542b531294SMarcel Holtmann /* Create a private copy with headroom */ 3552b531294SMarcel Holtmann skb_copy = __pskb_copy_fclone(skb, HCI_MON_HDR_SIZE, GFP_ATOMIC, true); 3562b531294SMarcel Holtmann if (!skb_copy) 3572b531294SMarcel Holtmann return; 3582b531294SMarcel Holtmann 3592b531294SMarcel Holtmann /* Put header before the data */ 360d58ff351SJohannes Berg hdr = skb_push(skb_copy, HCI_MON_HDR_SIZE); 3612b531294SMarcel Holtmann hdr->opcode = opcode; 3622b531294SMarcel Holtmann hdr->index = cpu_to_le16(hdev->id); 3632b531294SMarcel Holtmann hdr->len = cpu_to_le16(skb->len); 3642b531294SMarcel Holtmann 365c08b1a1dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb_copy, 366c08b1a1dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 367cd82e61cSMarcel Holtmann kfree_skb(skb_copy); 368cd82e61cSMarcel Holtmann } 369cd82e61cSMarcel Holtmann 37038ceaa00SMarcel Holtmann void hci_send_monitor_ctrl_event(struct hci_dev *hdev, u16 event, 37138ceaa00SMarcel Holtmann void *data, u16 data_len, ktime_t tstamp, 37238ceaa00SMarcel Holtmann int flag, struct sock *skip_sk) 37338ceaa00SMarcel Holtmann { 37438ceaa00SMarcel Holtmann struct sock *sk; 37538ceaa00SMarcel Holtmann __le16 index; 37638ceaa00SMarcel Holtmann 37738ceaa00SMarcel Holtmann if (hdev) 37838ceaa00SMarcel Holtmann index = cpu_to_le16(hdev->id); 37938ceaa00SMarcel Holtmann else 38038ceaa00SMarcel Holtmann index = cpu_to_le16(MGMT_INDEX_NONE); 38138ceaa00SMarcel Holtmann 38238ceaa00SMarcel Holtmann read_lock(&hci_sk_list.lock); 38338ceaa00SMarcel Holtmann 38438ceaa00SMarcel Holtmann sk_for_each(sk, &hci_sk_list.head) { 38538ceaa00SMarcel Holtmann struct hci_mon_hdr *hdr; 38638ceaa00SMarcel Holtmann struct sk_buff *skb; 38738ceaa00SMarcel Holtmann 38838ceaa00SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_CONTROL) 38938ceaa00SMarcel Holtmann continue; 39038ceaa00SMarcel Holtmann 39138ceaa00SMarcel Holtmann /* Ignore socket without the flag set */ 39238ceaa00SMarcel Holtmann if (!hci_sock_test_flag(sk, flag)) 39338ceaa00SMarcel Holtmann continue; 39438ceaa00SMarcel Holtmann 39538ceaa00SMarcel Holtmann /* Skip the original socket */ 39638ceaa00SMarcel Holtmann if (sk == skip_sk) 39738ceaa00SMarcel Holtmann continue; 39838ceaa00SMarcel Holtmann 39938ceaa00SMarcel Holtmann skb = bt_skb_alloc(6 + data_len, GFP_ATOMIC); 40038ceaa00SMarcel Holtmann if (!skb) 40138ceaa00SMarcel Holtmann continue; 40238ceaa00SMarcel Holtmann 40338ceaa00SMarcel Holtmann put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4)); 40438ceaa00SMarcel Holtmann put_unaligned_le16(event, skb_put(skb, 2)); 40538ceaa00SMarcel Holtmann 40638ceaa00SMarcel Holtmann if (data) 40759ae1d12SJohannes Berg skb_put_data(skb, data, data_len); 40838ceaa00SMarcel Holtmann 40938ceaa00SMarcel Holtmann skb->tstamp = tstamp; 41038ceaa00SMarcel Holtmann 411d58ff351SJohannes Berg hdr = skb_push(skb, HCI_MON_HDR_SIZE); 41238ceaa00SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_CTRL_EVENT); 41338ceaa00SMarcel Holtmann hdr->index = index; 41438ceaa00SMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE); 41538ceaa00SMarcel Holtmann 416a9ee77afSSebastian Andrzej Siewior __hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 41738ceaa00SMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 41838ceaa00SMarcel Holtmann kfree_skb(skb); 41938ceaa00SMarcel Holtmann } 42038ceaa00SMarcel Holtmann 42138ceaa00SMarcel Holtmann read_unlock(&hci_sk_list.lock); 42238ceaa00SMarcel Holtmann } 42338ceaa00SMarcel Holtmann 424cd82e61cSMarcel Holtmann static struct sk_buff *create_monitor_event(struct hci_dev *hdev, int event) 425cd82e61cSMarcel Holtmann { 426cd82e61cSMarcel Holtmann struct hci_mon_hdr *hdr; 427cd82e61cSMarcel Holtmann struct hci_mon_new_index *ni; 4286c566dd5SMarcel Holtmann struct hci_mon_index_info *ii; 429cd82e61cSMarcel Holtmann struct sk_buff *skb; 430cd82e61cSMarcel Holtmann __le16 opcode; 431cd82e61cSMarcel Holtmann 432cd82e61cSMarcel Holtmann switch (event) { 433cd82e61cSMarcel Holtmann case HCI_DEV_REG: 434cd82e61cSMarcel Holtmann skb = bt_skb_alloc(HCI_MON_NEW_INDEX_SIZE, GFP_ATOMIC); 435cd82e61cSMarcel Holtmann if (!skb) 436cd82e61cSMarcel Holtmann return NULL; 437cd82e61cSMarcel Holtmann 4384df864c1SJohannes Berg ni = skb_put(skb, HCI_MON_NEW_INDEX_SIZE); 439cd82e61cSMarcel Holtmann ni->type = hdev->dev_type; 440cd82e61cSMarcel Holtmann ni->bus = hdev->bus; 441cd82e61cSMarcel Holtmann bacpy(&ni->bdaddr, &hdev->bdaddr); 442cd82e61cSMarcel Holtmann memcpy(ni->name, hdev->name, 8); 443cd82e61cSMarcel Holtmann 444dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_NEW_INDEX); 445cd82e61cSMarcel Holtmann break; 446cd82e61cSMarcel Holtmann 447cd82e61cSMarcel Holtmann case HCI_DEV_UNREG: 448cd82e61cSMarcel Holtmann skb = bt_skb_alloc(0, GFP_ATOMIC); 449cd82e61cSMarcel Holtmann if (!skb) 450cd82e61cSMarcel Holtmann return NULL; 451cd82e61cSMarcel Holtmann 452dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_DEL_INDEX); 453cd82e61cSMarcel Holtmann break; 454cd82e61cSMarcel Holtmann 455e131d74aSMarcel Holtmann case HCI_DEV_SETUP: 456e131d74aSMarcel Holtmann if (hdev->manufacturer == 0xffff) 457e131d74aSMarcel Holtmann return NULL; 45819186c7bSGustavo A. R. Silva fallthrough; 459e131d74aSMarcel Holtmann 4606c566dd5SMarcel Holtmann case HCI_DEV_UP: 4616c566dd5SMarcel Holtmann skb = bt_skb_alloc(HCI_MON_INDEX_INFO_SIZE, GFP_ATOMIC); 4626c566dd5SMarcel Holtmann if (!skb) 4636c566dd5SMarcel Holtmann return NULL; 4646c566dd5SMarcel Holtmann 4654df864c1SJohannes Berg ii = skb_put(skb, HCI_MON_INDEX_INFO_SIZE); 4666c566dd5SMarcel Holtmann bacpy(&ii->bdaddr, &hdev->bdaddr); 4676c566dd5SMarcel Holtmann ii->manufacturer = cpu_to_le16(hdev->manufacturer); 4686c566dd5SMarcel Holtmann 4696c566dd5SMarcel Holtmann opcode = cpu_to_le16(HCI_MON_INDEX_INFO); 4706c566dd5SMarcel Holtmann break; 4716c566dd5SMarcel Holtmann 47222db3cbcSMarcel Holtmann case HCI_DEV_OPEN: 47322db3cbcSMarcel Holtmann skb = bt_skb_alloc(0, GFP_ATOMIC); 47422db3cbcSMarcel Holtmann if (!skb) 47522db3cbcSMarcel Holtmann return NULL; 47622db3cbcSMarcel Holtmann 47722db3cbcSMarcel Holtmann opcode = cpu_to_le16(HCI_MON_OPEN_INDEX); 47822db3cbcSMarcel Holtmann break; 47922db3cbcSMarcel Holtmann 48022db3cbcSMarcel Holtmann case HCI_DEV_CLOSE: 48122db3cbcSMarcel Holtmann skb = bt_skb_alloc(0, GFP_ATOMIC); 48222db3cbcSMarcel Holtmann if (!skb) 48322db3cbcSMarcel Holtmann return NULL; 48422db3cbcSMarcel Holtmann 48522db3cbcSMarcel Holtmann opcode = cpu_to_le16(HCI_MON_CLOSE_INDEX); 48622db3cbcSMarcel Holtmann break; 48722db3cbcSMarcel Holtmann 488cd82e61cSMarcel Holtmann default: 489cd82e61cSMarcel Holtmann return NULL; 490cd82e61cSMarcel Holtmann } 491cd82e61cSMarcel Holtmann 492cd82e61cSMarcel Holtmann __net_timestamp(skb); 493cd82e61cSMarcel Holtmann 494d58ff351SJohannes Berg hdr = skb_push(skb, HCI_MON_HDR_SIZE); 495cd82e61cSMarcel Holtmann hdr->opcode = opcode; 496cd82e61cSMarcel Holtmann hdr->index = cpu_to_le16(hdev->id); 497cd82e61cSMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE); 498cd82e61cSMarcel Holtmann 499cd82e61cSMarcel Holtmann return skb; 500cd82e61cSMarcel Holtmann } 501cd82e61cSMarcel Holtmann 502249fa169SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_open(struct sock *sk) 503249fa169SMarcel Holtmann { 504249fa169SMarcel Holtmann struct hci_mon_hdr *hdr; 505249fa169SMarcel Holtmann struct sk_buff *skb; 506d0bef1d2SMarcel Holtmann u16 format; 507249fa169SMarcel Holtmann u8 ver[3]; 508249fa169SMarcel Holtmann u32 flags; 509249fa169SMarcel Holtmann 5100ef2c42fSMarcel Holtmann /* No message needed when cookie is not present */ 5110ef2c42fSMarcel Holtmann if (!hci_pi(sk)->cookie) 5120ef2c42fSMarcel Holtmann return NULL; 5130ef2c42fSMarcel Holtmann 514d0bef1d2SMarcel Holtmann switch (hci_pi(sk)->channel) { 515f81f5b2dSMarcel Holtmann case HCI_CHANNEL_RAW: 516f81f5b2dSMarcel Holtmann format = 0x0000; 517f81f5b2dSMarcel Holtmann ver[0] = BT_SUBSYS_VERSION; 518f81f5b2dSMarcel Holtmann put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1); 519f81f5b2dSMarcel Holtmann break; 520aa1638ddSMarcel Holtmann case HCI_CHANNEL_USER: 521aa1638ddSMarcel Holtmann format = 0x0001; 522aa1638ddSMarcel Holtmann ver[0] = BT_SUBSYS_VERSION; 523aa1638ddSMarcel Holtmann put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1); 524aa1638ddSMarcel Holtmann break; 525d0bef1d2SMarcel Holtmann case HCI_CHANNEL_CONTROL: 526d0bef1d2SMarcel Holtmann format = 0x0002; 527d0bef1d2SMarcel Holtmann mgmt_fill_version_info(ver); 528d0bef1d2SMarcel Holtmann break; 529d0bef1d2SMarcel Holtmann default: 530d0bef1d2SMarcel Holtmann /* No message for unsupported format */ 531d0bef1d2SMarcel Holtmann return NULL; 532d0bef1d2SMarcel Holtmann } 533d0bef1d2SMarcel Holtmann 534249fa169SMarcel Holtmann skb = bt_skb_alloc(14 + TASK_COMM_LEN , GFP_ATOMIC); 535249fa169SMarcel Holtmann if (!skb) 536249fa169SMarcel Holtmann return NULL; 537249fa169SMarcel Holtmann 538249fa169SMarcel Holtmann flags = hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) ? 0x1 : 0x0; 539249fa169SMarcel Holtmann 540249fa169SMarcel Holtmann put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4)); 541249fa169SMarcel Holtmann put_unaligned_le16(format, skb_put(skb, 2)); 54259ae1d12SJohannes Berg skb_put_data(skb, ver, sizeof(ver)); 543249fa169SMarcel Holtmann put_unaligned_le32(flags, skb_put(skb, 4)); 544634fef61SJohannes Berg skb_put_u8(skb, TASK_COMM_LEN); 54559ae1d12SJohannes Berg skb_put_data(skb, hci_pi(sk)->comm, TASK_COMM_LEN); 546249fa169SMarcel Holtmann 547249fa169SMarcel Holtmann __net_timestamp(skb); 548249fa169SMarcel Holtmann 549d58ff351SJohannes Berg hdr = skb_push(skb, HCI_MON_HDR_SIZE); 550249fa169SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_CTRL_OPEN); 5510ef2c42fSMarcel Holtmann if (hci_pi(sk)->hdev) 5520ef2c42fSMarcel Holtmann hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id); 5530ef2c42fSMarcel Holtmann else 554249fa169SMarcel Holtmann hdr->index = cpu_to_le16(HCI_DEV_NONE); 555249fa169SMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE); 556249fa169SMarcel Holtmann 557249fa169SMarcel Holtmann return skb; 558249fa169SMarcel Holtmann } 559249fa169SMarcel Holtmann 560249fa169SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_close(struct sock *sk) 561249fa169SMarcel Holtmann { 562249fa169SMarcel Holtmann struct hci_mon_hdr *hdr; 563249fa169SMarcel Holtmann struct sk_buff *skb; 564249fa169SMarcel Holtmann 5650ef2c42fSMarcel Holtmann /* No message needed when cookie is not present */ 5660ef2c42fSMarcel Holtmann if (!hci_pi(sk)->cookie) 5670ef2c42fSMarcel Holtmann return NULL; 5680ef2c42fSMarcel Holtmann 569d0bef1d2SMarcel Holtmann switch (hci_pi(sk)->channel) { 570f81f5b2dSMarcel Holtmann case HCI_CHANNEL_RAW: 571aa1638ddSMarcel Holtmann case HCI_CHANNEL_USER: 572d0bef1d2SMarcel Holtmann case HCI_CHANNEL_CONTROL: 573d0bef1d2SMarcel Holtmann break; 574d0bef1d2SMarcel Holtmann default: 575d0bef1d2SMarcel Holtmann /* No message for unsupported format */ 576d0bef1d2SMarcel Holtmann return NULL; 577d0bef1d2SMarcel Holtmann } 578d0bef1d2SMarcel Holtmann 579249fa169SMarcel Holtmann skb = bt_skb_alloc(4, GFP_ATOMIC); 580249fa169SMarcel Holtmann if (!skb) 581249fa169SMarcel Holtmann return NULL; 582249fa169SMarcel Holtmann 583249fa169SMarcel Holtmann put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4)); 584249fa169SMarcel Holtmann 585249fa169SMarcel Holtmann __net_timestamp(skb); 586249fa169SMarcel Holtmann 587d58ff351SJohannes Berg hdr = skb_push(skb, HCI_MON_HDR_SIZE); 588249fa169SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_CTRL_CLOSE); 5890ef2c42fSMarcel Holtmann if (hci_pi(sk)->hdev) 5900ef2c42fSMarcel Holtmann hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id); 5910ef2c42fSMarcel Holtmann else 592249fa169SMarcel Holtmann hdr->index = cpu_to_le16(HCI_DEV_NONE); 593249fa169SMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE); 594249fa169SMarcel Holtmann 595249fa169SMarcel Holtmann return skb; 596249fa169SMarcel Holtmann } 597249fa169SMarcel Holtmann 59838ceaa00SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_command(struct sock *sk, u16 index, 59938ceaa00SMarcel Holtmann u16 opcode, u16 len, 60038ceaa00SMarcel Holtmann const void *buf) 60138ceaa00SMarcel Holtmann { 60238ceaa00SMarcel Holtmann struct hci_mon_hdr *hdr; 60338ceaa00SMarcel Holtmann struct sk_buff *skb; 60438ceaa00SMarcel Holtmann 60538ceaa00SMarcel Holtmann skb = bt_skb_alloc(6 + len, GFP_ATOMIC); 60638ceaa00SMarcel Holtmann if (!skb) 60738ceaa00SMarcel Holtmann return NULL; 60838ceaa00SMarcel Holtmann 60938ceaa00SMarcel Holtmann put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4)); 61038ceaa00SMarcel Holtmann put_unaligned_le16(opcode, skb_put(skb, 2)); 61138ceaa00SMarcel Holtmann 61238ceaa00SMarcel Holtmann if (buf) 61359ae1d12SJohannes Berg skb_put_data(skb, buf, len); 61438ceaa00SMarcel Holtmann 61538ceaa00SMarcel Holtmann __net_timestamp(skb); 61638ceaa00SMarcel Holtmann 617d58ff351SJohannes Berg hdr = skb_push(skb, HCI_MON_HDR_SIZE); 61838ceaa00SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_CTRL_COMMAND); 61938ceaa00SMarcel Holtmann hdr->index = cpu_to_le16(index); 62038ceaa00SMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE); 62138ceaa00SMarcel Holtmann 62238ceaa00SMarcel Holtmann return skb; 62338ceaa00SMarcel Holtmann } 62438ceaa00SMarcel Holtmann 625787b306cSJohannes Berg static void __printf(2, 3) 626787b306cSJohannes Berg send_monitor_note(struct sock *sk, const char *fmt, ...) 627dd31506dSMarcel Holtmann { 628787b306cSJohannes Berg size_t len; 629dd31506dSMarcel Holtmann struct hci_mon_hdr *hdr; 630dd31506dSMarcel Holtmann struct sk_buff *skb; 631787b306cSJohannes Berg va_list args; 632787b306cSJohannes Berg 633787b306cSJohannes Berg va_start(args, fmt); 634787b306cSJohannes Berg len = vsnprintf(NULL, 0, fmt, args); 635787b306cSJohannes Berg va_end(args); 636dd31506dSMarcel Holtmann 637dd31506dSMarcel Holtmann skb = bt_skb_alloc(len + 1, GFP_ATOMIC); 638dd31506dSMarcel Holtmann if (!skb) 639dd31506dSMarcel Holtmann return; 640dd31506dSMarcel Holtmann 641787b306cSJohannes Berg va_start(args, fmt); 642787b306cSJohannes Berg vsprintf(skb_put(skb, len), fmt, args); 6434df864c1SJohannes Berg *(u8 *)skb_put(skb, 1) = 0; 644787b306cSJohannes Berg va_end(args); 645dd31506dSMarcel Holtmann 646dd31506dSMarcel Holtmann __net_timestamp(skb); 647dd31506dSMarcel Holtmann 648dd31506dSMarcel Holtmann hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE); 649dd31506dSMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_SYSTEM_NOTE); 650dd31506dSMarcel Holtmann hdr->index = cpu_to_le16(HCI_DEV_NONE); 651dd31506dSMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE); 652dd31506dSMarcel Holtmann 653dd31506dSMarcel Holtmann if (sock_queue_rcv_skb(sk, skb)) 654dd31506dSMarcel Holtmann kfree_skb(skb); 655dd31506dSMarcel Holtmann } 656dd31506dSMarcel Holtmann 657cd82e61cSMarcel Holtmann static void send_monitor_replay(struct sock *sk) 658cd82e61cSMarcel Holtmann { 659cd82e61cSMarcel Holtmann struct hci_dev *hdev; 660cd82e61cSMarcel Holtmann 661cd82e61cSMarcel Holtmann read_lock(&hci_dev_list_lock); 662cd82e61cSMarcel Holtmann 663cd82e61cSMarcel Holtmann list_for_each_entry(hdev, &hci_dev_list, list) { 664cd82e61cSMarcel Holtmann struct sk_buff *skb; 665cd82e61cSMarcel Holtmann 666cd82e61cSMarcel Holtmann skb = create_monitor_event(hdev, HCI_DEV_REG); 667cd82e61cSMarcel Holtmann if (!skb) 668cd82e61cSMarcel Holtmann continue; 669cd82e61cSMarcel Holtmann 670cd82e61cSMarcel Holtmann if (sock_queue_rcv_skb(sk, skb)) 671cd82e61cSMarcel Holtmann kfree_skb(skb); 67222db3cbcSMarcel Holtmann 67322db3cbcSMarcel Holtmann if (!test_bit(HCI_RUNNING, &hdev->flags)) 67422db3cbcSMarcel Holtmann continue; 67522db3cbcSMarcel Holtmann 67622db3cbcSMarcel Holtmann skb = create_monitor_event(hdev, HCI_DEV_OPEN); 67722db3cbcSMarcel Holtmann if (!skb) 67822db3cbcSMarcel Holtmann continue; 67922db3cbcSMarcel Holtmann 68022db3cbcSMarcel Holtmann if (sock_queue_rcv_skb(sk, skb)) 68122db3cbcSMarcel Holtmann kfree_skb(skb); 6826c566dd5SMarcel Holtmann 683e131d74aSMarcel Holtmann if (test_bit(HCI_UP, &hdev->flags)) 6846c566dd5SMarcel Holtmann skb = create_monitor_event(hdev, HCI_DEV_UP); 685e131d74aSMarcel Holtmann else if (hci_dev_test_flag(hdev, HCI_SETUP)) 686e131d74aSMarcel Holtmann skb = create_monitor_event(hdev, HCI_DEV_SETUP); 687e131d74aSMarcel Holtmann else 688e131d74aSMarcel Holtmann skb = NULL; 6896c566dd5SMarcel Holtmann 690e131d74aSMarcel Holtmann if (skb) { 6916c566dd5SMarcel Holtmann if (sock_queue_rcv_skb(sk, skb)) 6926c566dd5SMarcel Holtmann kfree_skb(skb); 693cd82e61cSMarcel Holtmann } 694e131d74aSMarcel Holtmann } 695cd82e61cSMarcel Holtmann 696cd82e61cSMarcel Holtmann read_unlock(&hci_dev_list_lock); 697cd82e61cSMarcel Holtmann } 698cd82e61cSMarcel Holtmann 699249fa169SMarcel Holtmann static void send_monitor_control_replay(struct sock *mon_sk) 700249fa169SMarcel Holtmann { 701249fa169SMarcel Holtmann struct sock *sk; 702249fa169SMarcel Holtmann 703249fa169SMarcel Holtmann read_lock(&hci_sk_list.lock); 704249fa169SMarcel Holtmann 705249fa169SMarcel Holtmann sk_for_each(sk, &hci_sk_list.head) { 706249fa169SMarcel Holtmann struct sk_buff *skb; 707249fa169SMarcel Holtmann 708249fa169SMarcel Holtmann skb = create_monitor_ctrl_open(sk); 709249fa169SMarcel Holtmann if (!skb) 710249fa169SMarcel Holtmann continue; 711249fa169SMarcel Holtmann 712249fa169SMarcel Holtmann if (sock_queue_rcv_skb(mon_sk, skb)) 713249fa169SMarcel Holtmann kfree_skb(skb); 714249fa169SMarcel Holtmann } 715249fa169SMarcel Holtmann 716249fa169SMarcel Holtmann read_unlock(&hci_sk_list.lock); 717249fa169SMarcel Holtmann } 718249fa169SMarcel Holtmann 719040030efSMarcel Holtmann /* Generate internal stack event */ 720040030efSMarcel Holtmann static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data) 721040030efSMarcel Holtmann { 722040030efSMarcel Holtmann struct hci_event_hdr *hdr; 723040030efSMarcel Holtmann struct hci_ev_stack_internal *ev; 724040030efSMarcel Holtmann struct sk_buff *skb; 725040030efSMarcel Holtmann 726040030efSMarcel Holtmann skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC); 727040030efSMarcel Holtmann if (!skb) 728040030efSMarcel Holtmann return; 729040030efSMarcel Holtmann 7304df864c1SJohannes Berg hdr = skb_put(skb, HCI_EVENT_HDR_SIZE); 731040030efSMarcel Holtmann hdr->evt = HCI_EV_STACK_INTERNAL; 732040030efSMarcel Holtmann hdr->plen = sizeof(*ev) + dlen; 733040030efSMarcel Holtmann 7344df864c1SJohannes Berg ev = skb_put(skb, sizeof(*ev) + dlen); 735040030efSMarcel Holtmann ev->type = type; 736040030efSMarcel Holtmann memcpy(ev->data, data, dlen); 737040030efSMarcel Holtmann 738040030efSMarcel Holtmann bt_cb(skb)->incoming = 1; 739040030efSMarcel Holtmann __net_timestamp(skb); 740040030efSMarcel Holtmann 741d79f34e3SMarcel Holtmann hci_skb_pkt_type(skb) = HCI_EVENT_PKT; 742040030efSMarcel Holtmann hci_send_to_sock(hdev, skb); 743040030efSMarcel Holtmann kfree_skb(skb); 744040030efSMarcel Holtmann } 745040030efSMarcel Holtmann 746040030efSMarcel Holtmann void hci_sock_dev_event(struct hci_dev *hdev, int event) 747040030efSMarcel Holtmann { 748040030efSMarcel Holtmann BT_DBG("hdev %s event %d", hdev->name, event); 749040030efSMarcel Holtmann 750cd82e61cSMarcel Holtmann if (atomic_read(&monitor_promisc)) { 751cd82e61cSMarcel Holtmann struct sk_buff *skb; 752cd82e61cSMarcel Holtmann 753ed1b28a4SMarcel Holtmann /* Send event to monitor */ 754cd82e61cSMarcel Holtmann skb = create_monitor_event(hdev, event); 755cd82e61cSMarcel Holtmann if (skb) { 756c08b1a1dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 757c08b1a1dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 758cd82e61cSMarcel Holtmann kfree_skb(skb); 759cd82e61cSMarcel Holtmann } 760cd82e61cSMarcel Holtmann } 761cd82e61cSMarcel Holtmann 762ed1b28a4SMarcel Holtmann if (event <= HCI_DEV_DOWN) { 763ed1b28a4SMarcel Holtmann struct hci_ev_si_device ev; 764ed1b28a4SMarcel Holtmann 765040030efSMarcel Holtmann /* Send event to sockets */ 766040030efSMarcel Holtmann ev.event = event; 767040030efSMarcel Holtmann ev.dev_id = hdev->id; 768040030efSMarcel Holtmann hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev); 769ed1b28a4SMarcel Holtmann } 770040030efSMarcel Holtmann 771040030efSMarcel Holtmann if (event == HCI_DEV_UNREG) { 772040030efSMarcel Holtmann struct sock *sk; 773040030efSMarcel Holtmann 774e0448092STetsuo Handa /* Wake up sockets using this dead device */ 775040030efSMarcel Holtmann read_lock(&hci_sk_list.lock); 776b67bfe0dSSasha Levin sk_for_each(sk, &hci_sk_list.head) { 777040030efSMarcel Holtmann if (hci_pi(sk)->hdev == hdev) { 778040030efSMarcel Holtmann sk->sk_err = EPIPE; 779040030efSMarcel Holtmann sk->sk_state_change(sk); 780040030efSMarcel Holtmann } 781040030efSMarcel Holtmann } 782040030efSMarcel Holtmann read_unlock(&hci_sk_list.lock); 783040030efSMarcel Holtmann } 784040030efSMarcel Holtmann } 785040030efSMarcel Holtmann 786801c1e8dSJohan Hedberg static struct hci_mgmt_chan *__hci_mgmt_chan_find(unsigned short channel) 787801c1e8dSJohan Hedberg { 788801c1e8dSJohan Hedberg struct hci_mgmt_chan *c; 789801c1e8dSJohan Hedberg 790801c1e8dSJohan Hedberg list_for_each_entry(c, &mgmt_chan_list, list) { 791801c1e8dSJohan Hedberg if (c->channel == channel) 792801c1e8dSJohan Hedberg return c; 793801c1e8dSJohan Hedberg } 794801c1e8dSJohan Hedberg 795801c1e8dSJohan Hedberg return NULL; 796801c1e8dSJohan Hedberg } 797801c1e8dSJohan Hedberg 798801c1e8dSJohan Hedberg static struct hci_mgmt_chan *hci_mgmt_chan_find(unsigned short channel) 799801c1e8dSJohan Hedberg { 800801c1e8dSJohan Hedberg struct hci_mgmt_chan *c; 801801c1e8dSJohan Hedberg 802801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock); 803801c1e8dSJohan Hedberg c = __hci_mgmt_chan_find(channel); 804801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 805801c1e8dSJohan Hedberg 806801c1e8dSJohan Hedberg return c; 807801c1e8dSJohan Hedberg } 808801c1e8dSJohan Hedberg 809801c1e8dSJohan Hedberg int hci_mgmt_chan_register(struct hci_mgmt_chan *c) 810801c1e8dSJohan Hedberg { 811801c1e8dSJohan Hedberg if (c->channel < HCI_CHANNEL_CONTROL) 812801c1e8dSJohan Hedberg return -EINVAL; 813801c1e8dSJohan Hedberg 814801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock); 815801c1e8dSJohan Hedberg if (__hci_mgmt_chan_find(c->channel)) { 816801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 817801c1e8dSJohan Hedberg return -EALREADY; 818801c1e8dSJohan Hedberg } 819801c1e8dSJohan Hedberg 820801c1e8dSJohan Hedberg list_add_tail(&c->list, &mgmt_chan_list); 821801c1e8dSJohan Hedberg 822801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 823801c1e8dSJohan Hedberg 824801c1e8dSJohan Hedberg return 0; 825801c1e8dSJohan Hedberg } 826801c1e8dSJohan Hedberg EXPORT_SYMBOL(hci_mgmt_chan_register); 827801c1e8dSJohan Hedberg 828801c1e8dSJohan Hedberg void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c) 829801c1e8dSJohan Hedberg { 830801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock); 831801c1e8dSJohan Hedberg list_del(&c->list); 832801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 833801c1e8dSJohan Hedberg } 834801c1e8dSJohan Hedberg EXPORT_SYMBOL(hci_mgmt_chan_unregister); 835801c1e8dSJohan Hedberg 8361da177e4SLinus Torvalds static int hci_sock_release(struct socket *sock) 8371da177e4SLinus Torvalds { 8381da177e4SLinus Torvalds struct sock *sk = sock->sk; 8397b005bd3SMarcel Holtmann struct hci_dev *hdev; 840249fa169SMarcel Holtmann struct sk_buff *skb; 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds if (!sk) 8451da177e4SLinus Torvalds return 0; 8461da177e4SLinus Torvalds 84711eb85ecSDan Carpenter lock_sock(sk); 84811eb85ecSDan Carpenter 84970ecce91SMarcel Holtmann switch (hci_pi(sk)->channel) { 85070ecce91SMarcel Holtmann case HCI_CHANNEL_MONITOR: 851cd82e61cSMarcel Holtmann atomic_dec(&monitor_promisc); 85270ecce91SMarcel Holtmann break; 853f81f5b2dSMarcel Holtmann case HCI_CHANNEL_RAW: 854aa1638ddSMarcel Holtmann case HCI_CHANNEL_USER: 85570ecce91SMarcel Holtmann case HCI_CHANNEL_CONTROL: 856249fa169SMarcel Holtmann /* Send event to monitor */ 857249fa169SMarcel Holtmann skb = create_monitor_ctrl_close(sk); 858249fa169SMarcel Holtmann if (skb) { 859249fa169SMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 860249fa169SMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 861249fa169SMarcel Holtmann kfree_skb(skb); 862249fa169SMarcel Holtmann } 863249fa169SMarcel Holtmann 864df1cb87aSMarcel Holtmann hci_sock_free_cookie(sk); 86570ecce91SMarcel Holtmann break; 86670ecce91SMarcel Holtmann } 867cd82e61cSMarcel Holtmann 8681da177e4SLinus Torvalds bt_sock_unlink(&hci_sk_list, sk); 8691da177e4SLinus Torvalds 870e20a2e9cSMyungho Jung hdev = hci_pi(sk)->hdev; 8711da177e4SLinus Torvalds if (hdev) { 8720acef50bSAbhishek Pandit-Subedi if (hci_pi(sk)->channel == HCI_CHANNEL_USER && 8730acef50bSAbhishek Pandit-Subedi !hci_dev_test_flag(hdev, HCI_UNREGISTER)) { 8749332ef9dSMasahiro Yamada /* When releasing a user channel exclusive access, 8756b3cc1dbSSimon Fels * call hci_dev_do_close directly instead of calling 8766b3cc1dbSSimon Fels * hci_dev_close to ensure the exclusive access will 8776b3cc1dbSSimon Fels * be released and the controller brought back down. 8786b3cc1dbSSimon Fels * 8796b3cc1dbSSimon Fels * The checking of HCI_AUTO_OFF is not needed in this 8806b3cc1dbSSimon Fels * case since it will have been cleared already when 8816b3cc1dbSSimon Fels * opening the user channel. 8820acef50bSAbhishek Pandit-Subedi * 8830acef50bSAbhishek Pandit-Subedi * Make sure to also check that we haven't already 8840acef50bSAbhishek Pandit-Subedi * unregistered since all the cleanup will have already 8850acef50bSAbhishek Pandit-Subedi * been complete and hdev will get released when we put 8860acef50bSAbhishek Pandit-Subedi * below. 8876b3cc1dbSSimon Fels */ 8886b3cc1dbSSimon Fels hci_dev_do_close(hdev); 8899380f9eaSLoic Poulain hci_dev_clear_flag(hdev, HCI_USER_CHANNEL); 890359ee4f8SAbhishek Pandit-Subedi hci_register_suspend_notifier(hdev); 8919380f9eaSLoic Poulain mgmt_index_added(hdev); 89223500189SMarcel Holtmann } 89323500189SMarcel Holtmann 8941da177e4SLinus Torvalds atomic_dec(&hdev->promisc); 8951da177e4SLinus Torvalds hci_dev_put(hdev); 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds sock_orphan(sk); 89911eb85ecSDan Carpenter release_sock(sk); 9001da177e4SLinus Torvalds sock_put(sk); 9011da177e4SLinus Torvalds return 0; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds 9043d4f9c00SArchie Pusaka static int hci_sock_reject_list_add(struct hci_dev *hdev, void __user *arg) 905f0358568SJohan Hedberg { 906f0358568SJohan Hedberg bdaddr_t bdaddr; 9075e762444SAntti Julku int err; 908f0358568SJohan Hedberg 909f0358568SJohan Hedberg if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) 910f0358568SJohan Hedberg return -EFAULT; 911f0358568SJohan Hedberg 91209fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 9135e762444SAntti Julku 9143d4f9c00SArchie Pusaka err = hci_bdaddr_list_add(&hdev->reject_list, &bdaddr, BDADDR_BREDR); 9155e762444SAntti Julku 91609fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 9175e762444SAntti Julku 9185e762444SAntti Julku return err; 919f0358568SJohan Hedberg } 920f0358568SJohan Hedberg 9213d4f9c00SArchie Pusaka static int hci_sock_reject_list_del(struct hci_dev *hdev, void __user *arg) 922f0358568SJohan Hedberg { 923f0358568SJohan Hedberg bdaddr_t bdaddr; 9245e762444SAntti Julku int err; 925f0358568SJohan Hedberg 926f0358568SJohan Hedberg if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) 927f0358568SJohan Hedberg return -EFAULT; 928f0358568SJohan Hedberg 92909fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 9305e762444SAntti Julku 9313d4f9c00SArchie Pusaka err = hci_bdaddr_list_del(&hdev->reject_list, &bdaddr, BDADDR_BREDR); 9325e762444SAntti Julku 93309fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 9345e762444SAntti Julku 9355e762444SAntti Julku return err; 936f0358568SJohan Hedberg } 937f0358568SJohan Hedberg 9381da177e4SLinus Torvalds /* Ioctls that require bound socket */ 9396039aa73SGustavo Padovan static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd, 9406039aa73SGustavo Padovan unsigned long arg) 9411da177e4SLinus Torvalds { 942e0448092STetsuo Handa struct hci_dev *hdev = hci_hdev_from_sock(sk); 9431da177e4SLinus Torvalds 944e0448092STetsuo Handa if (IS_ERR(hdev)) 945e0448092STetsuo Handa return PTR_ERR(hdev); 9461da177e4SLinus Torvalds 947d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) 9480736cfa8SMarcel Holtmann return -EBUSY; 9490736cfa8SMarcel Holtmann 950d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 951fee746b0SMarcel Holtmann return -EOPNOTSUPP; 952fee746b0SMarcel Holtmann 953ca8bee5dSMarcel Holtmann if (hdev->dev_type != HCI_PRIMARY) 9545b69bef5SMarcel Holtmann return -EOPNOTSUPP; 9555b69bef5SMarcel Holtmann 9561da177e4SLinus Torvalds switch (cmd) { 9571da177e4SLinus Torvalds case HCISETRAW: 9581da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 959bf5b30b8SZhao Hongjiang return -EPERM; 960db596681SMarcel Holtmann return -EOPNOTSUPP; 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds case HCIGETCONNINFO: 9631da177e4SLinus Torvalds return hci_get_conn_info(hdev, (void __user *)arg); 9641da177e4SLinus Torvalds 96540be492fSMarcel Holtmann case HCIGETAUTHINFO: 96640be492fSMarcel Holtmann return hci_get_auth_info(hdev, (void __user *)arg); 96740be492fSMarcel Holtmann 968f0358568SJohan Hedberg case HCIBLOCKADDR: 969f0358568SJohan Hedberg if (!capable(CAP_NET_ADMIN)) 970bf5b30b8SZhao Hongjiang return -EPERM; 9713d4f9c00SArchie Pusaka return hci_sock_reject_list_add(hdev, (void __user *)arg); 972f0358568SJohan Hedberg 973f0358568SJohan Hedberg case HCIUNBLOCKADDR: 974f0358568SJohan Hedberg if (!capable(CAP_NET_ADMIN)) 975bf5b30b8SZhao Hongjiang return -EPERM; 9763d4f9c00SArchie Pusaka return hci_sock_reject_list_del(hdev, (void __user *)arg); 9770736cfa8SMarcel Holtmann } 978f0358568SJohan Hedberg 979324d36edSMarcel Holtmann return -ENOIOCTLCMD; 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds 9828fc9ced3SGustavo Padovan static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, 9838fc9ced3SGustavo Padovan unsigned long arg) 9841da177e4SLinus Torvalds { 9851da177e4SLinus Torvalds void __user *argp = (void __user *)arg; 9860736cfa8SMarcel Holtmann struct sock *sk = sock->sk; 9871da177e4SLinus Torvalds int err; 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds BT_DBG("cmd %x arg %lx", cmd, arg); 9901da177e4SLinus Torvalds 991c1c4f956SMarcel Holtmann lock_sock(sk); 992c1c4f956SMarcel Holtmann 993c1c4f956SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 994c1c4f956SMarcel Holtmann err = -EBADFD; 995c1c4f956SMarcel Holtmann goto done; 996c1c4f956SMarcel Holtmann } 997c1c4f956SMarcel Holtmann 998f81f5b2dSMarcel Holtmann /* When calling an ioctl on an unbound raw socket, then ensure 999f81f5b2dSMarcel Holtmann * that the monitor gets informed. Ensure that the resulting event 1000f81f5b2dSMarcel Holtmann * is only send once by checking if the cookie exists or not. The 1001f81f5b2dSMarcel Holtmann * socket cookie will be only ever generated once for the lifetime 1002f81f5b2dSMarcel Holtmann * of a given socket. 1003f81f5b2dSMarcel Holtmann */ 1004f81f5b2dSMarcel Holtmann if (hci_sock_gen_cookie(sk)) { 1005f81f5b2dSMarcel Holtmann struct sk_buff *skb; 1006f81f5b2dSMarcel Holtmann 1007f81f5b2dSMarcel Holtmann if (capable(CAP_NET_ADMIN)) 1008f81f5b2dSMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 1009f81f5b2dSMarcel Holtmann 1010f81f5b2dSMarcel Holtmann /* Send event to monitor */ 1011f81f5b2dSMarcel Holtmann skb = create_monitor_ctrl_open(sk); 1012f81f5b2dSMarcel Holtmann if (skb) { 1013f81f5b2dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1014f81f5b2dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1015f81f5b2dSMarcel Holtmann kfree_skb(skb); 1016f81f5b2dSMarcel Holtmann } 1017f81f5b2dSMarcel Holtmann } 1018f81f5b2dSMarcel Holtmann 1019c1c4f956SMarcel Holtmann release_sock(sk); 1020c1c4f956SMarcel Holtmann 10211da177e4SLinus Torvalds switch (cmd) { 10221da177e4SLinus Torvalds case HCIGETDEVLIST: 10231da177e4SLinus Torvalds return hci_get_dev_list(argp); 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds case HCIGETDEVINFO: 10261da177e4SLinus Torvalds return hci_get_dev_info(argp); 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds case HCIGETCONNLIST: 10291da177e4SLinus Torvalds return hci_get_conn_list(argp); 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds case HCIDEVUP: 10321da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1033bf5b30b8SZhao Hongjiang return -EPERM; 10341da177e4SLinus Torvalds return hci_dev_open(arg); 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds case HCIDEVDOWN: 10371da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1038bf5b30b8SZhao Hongjiang return -EPERM; 10391da177e4SLinus Torvalds return hci_dev_close(arg); 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds case HCIDEVRESET: 10421da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1043bf5b30b8SZhao Hongjiang return -EPERM; 10441da177e4SLinus Torvalds return hci_dev_reset(arg); 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds case HCIDEVRESTAT: 10471da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1048bf5b30b8SZhao Hongjiang return -EPERM; 10491da177e4SLinus Torvalds return hci_dev_reset_stat(arg); 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds case HCISETSCAN: 10521da177e4SLinus Torvalds case HCISETAUTH: 10531da177e4SLinus Torvalds case HCISETENCRYPT: 10541da177e4SLinus Torvalds case HCISETPTYPE: 10551da177e4SLinus Torvalds case HCISETLINKPOL: 10561da177e4SLinus Torvalds case HCISETLINKMODE: 10571da177e4SLinus Torvalds case HCISETACLMTU: 10581da177e4SLinus Torvalds case HCISETSCOMTU: 10591da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1060bf5b30b8SZhao Hongjiang return -EPERM; 10611da177e4SLinus Torvalds return hci_dev_cmd(cmd, argp); 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds case HCIINQUIRY: 10641da177e4SLinus Torvalds return hci_inquiry(argp); 1065c1c4f956SMarcel Holtmann } 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds lock_sock(sk); 1068c1c4f956SMarcel Holtmann 10691da177e4SLinus Torvalds err = hci_sock_bound_ioctl(sk, cmd, arg); 1070c1c4f956SMarcel Holtmann 1071c1c4f956SMarcel Holtmann done: 10721da177e4SLinus Torvalds release_sock(sk); 10731da177e4SLinus Torvalds return err; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds 10767a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT 10777a6038b3SArnd Bergmann static int hci_sock_compat_ioctl(struct socket *sock, unsigned int cmd, 10787a6038b3SArnd Bergmann unsigned long arg) 10797a6038b3SArnd Bergmann { 10807a6038b3SArnd Bergmann switch (cmd) { 10817a6038b3SArnd Bergmann case HCIDEVUP: 10827a6038b3SArnd Bergmann case HCIDEVDOWN: 10837a6038b3SArnd Bergmann case HCIDEVRESET: 10847a6038b3SArnd Bergmann case HCIDEVRESTAT: 10857a6038b3SArnd Bergmann return hci_sock_ioctl(sock, cmd, arg); 10867a6038b3SArnd Bergmann } 10877a6038b3SArnd Bergmann 10887a6038b3SArnd Bergmann return hci_sock_ioctl(sock, cmd, (unsigned long)compat_ptr(arg)); 10897a6038b3SArnd Bergmann } 10907a6038b3SArnd Bergmann #endif 10917a6038b3SArnd Bergmann 10928fc9ced3SGustavo Padovan static int hci_sock_bind(struct socket *sock, struct sockaddr *addr, 10938fc9ced3SGustavo Padovan int addr_len) 10941da177e4SLinus Torvalds { 10950381101fSJohan Hedberg struct sockaddr_hci haddr; 10961da177e4SLinus Torvalds struct sock *sk = sock->sk; 10971da177e4SLinus Torvalds struct hci_dev *hdev = NULL; 1098f4cdbb3fSMarcel Holtmann struct sk_buff *skb; 10990381101fSJohan Hedberg int len, err = 0; 11001da177e4SLinus Torvalds 11011da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 11021da177e4SLinus Torvalds 11030381101fSJohan Hedberg if (!addr) 11040381101fSJohan Hedberg return -EINVAL; 11050381101fSJohan Hedberg 11060381101fSJohan Hedberg memset(&haddr, 0, sizeof(haddr)); 11070381101fSJohan Hedberg len = min_t(unsigned int, sizeof(haddr), addr_len); 11080381101fSJohan Hedberg memcpy(&haddr, addr, len); 11090381101fSJohan Hedberg 11100381101fSJohan Hedberg if (haddr.hci_family != AF_BLUETOOTH) 11110381101fSJohan Hedberg return -EINVAL; 11120381101fSJohan Hedberg 11131da177e4SLinus Torvalds lock_sock(sk); 11141da177e4SLinus Torvalds 1115e0448092STetsuo Handa /* Allow detaching from dead device and attaching to alive device, if 1116e0448092STetsuo Handa * the caller wants to re-bind (instead of close) this socket in 1117e0448092STetsuo Handa * response to hci_sock_dev_event(HCI_DEV_UNREG) notification. 1118e0448092STetsuo Handa */ 1119e0448092STetsuo Handa hdev = hci_pi(sk)->hdev; 1120e0448092STetsuo Handa if (hdev && hci_dev_test_flag(hdev, HCI_UNREGISTER)) { 1121e0448092STetsuo Handa hci_pi(sk)->hdev = NULL; 1122e0448092STetsuo Handa sk->sk_state = BT_OPEN; 1123e0448092STetsuo Handa hci_dev_put(hdev); 1124e0448092STetsuo Handa } 1125e0448092STetsuo Handa hdev = NULL; 1126e0448092STetsuo Handa 11277cc2ade2SMarcel Holtmann if (sk->sk_state == BT_BOUND) { 11287cc2ade2SMarcel Holtmann err = -EALREADY; 11297cc2ade2SMarcel Holtmann goto done; 11307cc2ade2SMarcel Holtmann } 11317cc2ade2SMarcel Holtmann 11327cc2ade2SMarcel Holtmann switch (haddr.hci_channel) { 11337cc2ade2SMarcel Holtmann case HCI_CHANNEL_RAW: 11347cc2ade2SMarcel Holtmann if (hci_pi(sk)->hdev) { 11351da177e4SLinus Torvalds err = -EALREADY; 11361da177e4SLinus Torvalds goto done; 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds 11390381101fSJohan Hedberg if (haddr.hci_dev != HCI_DEV_NONE) { 11400381101fSJohan Hedberg hdev = hci_dev_get(haddr.hci_dev); 114170f23020SAndrei Emeltchenko if (!hdev) { 11421da177e4SLinus Torvalds err = -ENODEV; 11431da177e4SLinus Torvalds goto done; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds atomic_inc(&hdev->promisc); 11471da177e4SLinus Torvalds } 11481da177e4SLinus Torvalds 11495a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 1150f81f5b2dSMarcel Holtmann 1151f4cdbb3fSMarcel Holtmann if (!hci_sock_gen_cookie(sk)) { 1152f4cdbb3fSMarcel Holtmann /* In the case when a cookie has already been assigned, 1153f4cdbb3fSMarcel Holtmann * then there has been already an ioctl issued against 115491641b79SZheng Yongjun * an unbound socket and with that triggered an open 1155f4cdbb3fSMarcel Holtmann * notification. Send a close notification first to 1156f4cdbb3fSMarcel Holtmann * allow the state transition to bounded. 1157f81f5b2dSMarcel Holtmann */ 1158f4cdbb3fSMarcel Holtmann skb = create_monitor_ctrl_close(sk); 1159f4cdbb3fSMarcel Holtmann if (skb) { 1160f4cdbb3fSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1161f4cdbb3fSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1162f4cdbb3fSMarcel Holtmann kfree_skb(skb); 1163f4cdbb3fSMarcel Holtmann } 1164f4cdbb3fSMarcel Holtmann } 1165f81f5b2dSMarcel Holtmann 1166f81f5b2dSMarcel Holtmann if (capable(CAP_NET_ADMIN)) 1167f81f5b2dSMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 1168f81f5b2dSMarcel Holtmann 1169f4cdbb3fSMarcel Holtmann hci_pi(sk)->hdev = hdev; 1170f4cdbb3fSMarcel Holtmann 1171f81f5b2dSMarcel Holtmann /* Send event to monitor */ 1172f81f5b2dSMarcel Holtmann skb = create_monitor_ctrl_open(sk); 1173f81f5b2dSMarcel Holtmann if (skb) { 1174f81f5b2dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1175f81f5b2dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1176f81f5b2dSMarcel Holtmann kfree_skb(skb); 1177f81f5b2dSMarcel Holtmann } 11787cc2ade2SMarcel Holtmann break; 11797cc2ade2SMarcel Holtmann 118023500189SMarcel Holtmann case HCI_CHANNEL_USER: 118123500189SMarcel Holtmann if (hci_pi(sk)->hdev) { 118223500189SMarcel Holtmann err = -EALREADY; 118323500189SMarcel Holtmann goto done; 118423500189SMarcel Holtmann } 118523500189SMarcel Holtmann 118623500189SMarcel Holtmann if (haddr.hci_dev == HCI_DEV_NONE) { 118723500189SMarcel Holtmann err = -EINVAL; 118823500189SMarcel Holtmann goto done; 118923500189SMarcel Holtmann } 119023500189SMarcel Holtmann 119110a8b86fSMarcel Holtmann if (!capable(CAP_NET_ADMIN)) { 119223500189SMarcel Holtmann err = -EPERM; 119323500189SMarcel Holtmann goto done; 119423500189SMarcel Holtmann } 119523500189SMarcel Holtmann 119623500189SMarcel Holtmann hdev = hci_dev_get(haddr.hci_dev); 119723500189SMarcel Holtmann if (!hdev) { 119823500189SMarcel Holtmann err = -ENODEV; 119923500189SMarcel Holtmann goto done; 120023500189SMarcel Holtmann } 120123500189SMarcel Holtmann 1202781f899fSMarcel Holtmann if (test_bit(HCI_INIT, &hdev->flags) || 1203d7a5a11dSMarcel Holtmann hci_dev_test_flag(hdev, HCI_SETUP) || 1204781f899fSMarcel Holtmann hci_dev_test_flag(hdev, HCI_CONFIG) || 1205781f899fSMarcel Holtmann (!hci_dev_test_flag(hdev, HCI_AUTO_OFF) && 1206781f899fSMarcel Holtmann test_bit(HCI_UP, &hdev->flags))) { 120723500189SMarcel Holtmann err = -EBUSY; 120823500189SMarcel Holtmann hci_dev_put(hdev); 120923500189SMarcel Holtmann goto done; 121023500189SMarcel Holtmann } 121123500189SMarcel Holtmann 1212238be788SMarcel Holtmann if (hci_dev_test_and_set_flag(hdev, HCI_USER_CHANNEL)) { 121323500189SMarcel Holtmann err = -EUSERS; 121423500189SMarcel Holtmann hci_dev_put(hdev); 121523500189SMarcel Holtmann goto done; 121623500189SMarcel Holtmann } 121723500189SMarcel Holtmann 121823500189SMarcel Holtmann mgmt_index_removed(hdev); 1219359ee4f8SAbhishek Pandit-Subedi hci_unregister_suspend_notifier(hdev); 122023500189SMarcel Holtmann 122123500189SMarcel Holtmann err = hci_dev_open(hdev->id); 122223500189SMarcel Holtmann if (err) { 1223781f899fSMarcel Holtmann if (err == -EALREADY) { 1224781f899fSMarcel Holtmann /* In case the transport is already up and 1225781f899fSMarcel Holtmann * running, clear the error here. 1226781f899fSMarcel Holtmann * 12279332ef9dSMasahiro Yamada * This can happen when opening a user 1228781f899fSMarcel Holtmann * channel and HCI_AUTO_OFF grace period 1229781f899fSMarcel Holtmann * is still active. 1230781f899fSMarcel Holtmann */ 1231781f899fSMarcel Holtmann err = 0; 1232781f899fSMarcel Holtmann } else { 1233a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_USER_CHANNEL); 1234359ee4f8SAbhishek Pandit-Subedi hci_register_suspend_notifier(hdev); 1235c6521401SMarcel Holtmann mgmt_index_added(hdev); 123623500189SMarcel Holtmann hci_dev_put(hdev); 123723500189SMarcel Holtmann goto done; 123823500189SMarcel Holtmann } 1239781f899fSMarcel Holtmann } 124023500189SMarcel Holtmann 12415a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 1242aa1638ddSMarcel Holtmann 1243aa1638ddSMarcel Holtmann if (!hci_sock_gen_cookie(sk)) { 1244aa1638ddSMarcel Holtmann /* In the case when a cookie has already been assigned, 1245aa1638ddSMarcel Holtmann * this socket will transition from a raw socket into 12469332ef9dSMasahiro Yamada * a user channel socket. For a clean transition, send 1247aa1638ddSMarcel Holtmann * the close notification first. 1248aa1638ddSMarcel Holtmann */ 1249aa1638ddSMarcel Holtmann skb = create_monitor_ctrl_close(sk); 1250aa1638ddSMarcel Holtmann if (skb) { 1251aa1638ddSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1252aa1638ddSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1253aa1638ddSMarcel Holtmann kfree_skb(skb); 1254aa1638ddSMarcel Holtmann } 1255aa1638ddSMarcel Holtmann } 1256aa1638ddSMarcel Holtmann 1257aa1638ddSMarcel Holtmann /* The user channel is restricted to CAP_NET_ADMIN 1258aa1638ddSMarcel Holtmann * capabilities and with that implicitly trusted. 1259aa1638ddSMarcel Holtmann */ 1260aa1638ddSMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 1261aa1638ddSMarcel Holtmann 126223500189SMarcel Holtmann hci_pi(sk)->hdev = hdev; 12635a6d2cf5SMarcel Holtmann 1264aa1638ddSMarcel Holtmann /* Send event to monitor */ 1265aa1638ddSMarcel Holtmann skb = create_monitor_ctrl_open(sk); 1266aa1638ddSMarcel Holtmann if (skb) { 1267aa1638ddSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1268aa1638ddSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1269aa1638ddSMarcel Holtmann kfree_skb(skb); 1270aa1638ddSMarcel Holtmann } 1271aa1638ddSMarcel Holtmann 12725a6d2cf5SMarcel Holtmann atomic_inc(&hdev->promisc); 127323500189SMarcel Holtmann break; 127423500189SMarcel Holtmann 1275cd82e61cSMarcel Holtmann case HCI_CHANNEL_MONITOR: 1276cd82e61cSMarcel Holtmann if (haddr.hci_dev != HCI_DEV_NONE) { 1277cd82e61cSMarcel Holtmann err = -EINVAL; 1278cd82e61cSMarcel Holtmann goto done; 1279cd82e61cSMarcel Holtmann } 1280cd82e61cSMarcel Holtmann 1281cd82e61cSMarcel Holtmann if (!capable(CAP_NET_RAW)) { 1282cd82e61cSMarcel Holtmann err = -EPERM; 1283cd82e61cSMarcel Holtmann goto done; 1284cd82e61cSMarcel Holtmann } 1285cd82e61cSMarcel Holtmann 12865a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 12875a6d2cf5SMarcel Holtmann 128850ebc055SMarcel Holtmann /* The monitor interface is restricted to CAP_NET_RAW 128950ebc055SMarcel Holtmann * capabilities and with that implicitly trusted. 129050ebc055SMarcel Holtmann */ 129150ebc055SMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 129250ebc055SMarcel Holtmann 1293787b306cSJohannes Berg send_monitor_note(sk, "Linux version %s (%s)", 1294787b306cSJohannes Berg init_utsname()->release, 1295787b306cSJohannes Berg init_utsname()->machine); 12969e8305b3SMarcel Holtmann send_monitor_note(sk, "Bluetooth subsystem version %u.%u", 12979e8305b3SMarcel Holtmann BT_SUBSYS_VERSION, BT_SUBSYS_REVISION); 1298cd82e61cSMarcel Holtmann send_monitor_replay(sk); 1299249fa169SMarcel Holtmann send_monitor_control_replay(sk); 1300cd82e61cSMarcel Holtmann 1301cd82e61cSMarcel Holtmann atomic_inc(&monitor_promisc); 1302cd82e61cSMarcel Holtmann break; 1303cd82e61cSMarcel Holtmann 1304ac714949SMarcel Holtmann case HCI_CHANNEL_LOGGING: 1305ac714949SMarcel Holtmann if (haddr.hci_dev != HCI_DEV_NONE) { 1306ac714949SMarcel Holtmann err = -EINVAL; 1307ac714949SMarcel Holtmann goto done; 1308ac714949SMarcel Holtmann } 1309ac714949SMarcel Holtmann 1310ac714949SMarcel Holtmann if (!capable(CAP_NET_ADMIN)) { 1311ac714949SMarcel Holtmann err = -EPERM; 1312ac714949SMarcel Holtmann goto done; 1313ac714949SMarcel Holtmann } 13145a6d2cf5SMarcel Holtmann 13155a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 1316ac714949SMarcel Holtmann break; 1317ac714949SMarcel Holtmann 13187cc2ade2SMarcel Holtmann default: 1319801c1e8dSJohan Hedberg if (!hci_mgmt_chan_find(haddr.hci_channel)) { 13207cc2ade2SMarcel Holtmann err = -EINVAL; 13217cc2ade2SMarcel Holtmann goto done; 13227cc2ade2SMarcel Holtmann } 13237cc2ade2SMarcel Holtmann 1324801c1e8dSJohan Hedberg if (haddr.hci_dev != HCI_DEV_NONE) { 1325801c1e8dSJohan Hedberg err = -EINVAL; 1326801c1e8dSJohan Hedberg goto done; 1327801c1e8dSJohan Hedberg } 1328801c1e8dSJohan Hedberg 13291195fbb8SMarcel Holtmann /* Users with CAP_NET_ADMIN capabilities are allowed 13301195fbb8SMarcel Holtmann * access to all management commands and events. For 13311195fbb8SMarcel Holtmann * untrusted users the interface is restricted and 13321195fbb8SMarcel Holtmann * also only untrusted events are sent. 133350ebc055SMarcel Holtmann */ 13341195fbb8SMarcel Holtmann if (capable(CAP_NET_ADMIN)) 133550ebc055SMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 133650ebc055SMarcel Holtmann 13375a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 13385a6d2cf5SMarcel Holtmann 1339f9207338SMarcel Holtmann /* At the moment the index and unconfigured index events 1340f9207338SMarcel Holtmann * are enabled unconditionally. Setting them on each 1341f9207338SMarcel Holtmann * socket when binding keeps this functionality. They 1342f9207338SMarcel Holtmann * however might be cleared later and then sending of these 1343f9207338SMarcel Holtmann * events will be disabled, but that is then intentional. 1344f6b7712eSMarcel Holtmann * 1345f6b7712eSMarcel Holtmann * This also enables generic events that are safe to be 1346f6b7712eSMarcel Holtmann * received by untrusted users. Example for such events 1347f6b7712eSMarcel Holtmann * are changes to settings, class of device, name etc. 1348f9207338SMarcel Holtmann */ 13495a6d2cf5SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_CONTROL) { 1350f4cdbb3fSMarcel Holtmann if (!hci_sock_gen_cookie(sk)) { 1351f4cdbb3fSMarcel Holtmann /* In the case when a cookie has already been 135291641b79SZheng Yongjun * assigned, this socket will transition from 1353f4cdbb3fSMarcel Holtmann * a raw socket into a control socket. To 135491641b79SZheng Yongjun * allow for a clean transition, send the 1355f4cdbb3fSMarcel Holtmann * close notification first. 1356f4cdbb3fSMarcel Holtmann */ 1357f4cdbb3fSMarcel Holtmann skb = create_monitor_ctrl_close(sk); 1358f4cdbb3fSMarcel Holtmann if (skb) { 1359f4cdbb3fSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1360f4cdbb3fSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1361f4cdbb3fSMarcel Holtmann kfree_skb(skb); 1362f4cdbb3fSMarcel Holtmann } 1363f4cdbb3fSMarcel Holtmann } 136470ecce91SMarcel Holtmann 1365249fa169SMarcel Holtmann /* Send event to monitor */ 1366249fa169SMarcel Holtmann skb = create_monitor_ctrl_open(sk); 1367249fa169SMarcel Holtmann if (skb) { 1368249fa169SMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1369249fa169SMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1370249fa169SMarcel Holtmann kfree_skb(skb); 1371249fa169SMarcel Holtmann } 1372249fa169SMarcel Holtmann 1373f9207338SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_INDEX_EVENTS); 1374f9207338SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); 13755504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_OPTION_EVENTS); 13765504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_SETTING_EVENTS); 13775504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS); 13785504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS); 1379f9207338SMarcel Holtmann } 1380801c1e8dSJohan Hedberg break; 1381801c1e8dSJohan Hedberg } 1382801c1e8dSJohan Hedberg 138309572fcaSLuiz Augusto von Dentz /* Default MTU to HCI_MAX_FRAME_SIZE if not set */ 138409572fcaSLuiz Augusto von Dentz if (!hci_pi(sk)->mtu) 138509572fcaSLuiz Augusto von Dentz hci_pi(sk)->mtu = HCI_MAX_FRAME_SIZE; 138609572fcaSLuiz Augusto von Dentz 13871da177e4SLinus Torvalds sk->sk_state = BT_BOUND; 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds done: 13901da177e4SLinus Torvalds release_sock(sk); 13911da177e4SLinus Torvalds return err; 13921da177e4SLinus Torvalds } 13931da177e4SLinus Torvalds 13948fc9ced3SGustavo Padovan static int hci_sock_getname(struct socket *sock, struct sockaddr *addr, 13959b2c45d4SDenys Vlasenko int peer) 13961da177e4SLinus Torvalds { 13971da177e4SLinus Torvalds struct sockaddr_hci *haddr = (struct sockaddr_hci *)addr; 13981da177e4SLinus Torvalds struct sock *sk = sock->sk; 13999d4b68b2SMarcel Holtmann struct hci_dev *hdev; 14009d4b68b2SMarcel Holtmann int err = 0; 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 14031da177e4SLinus Torvalds 140406f43cbcSMarcel Holtmann if (peer) 140506f43cbcSMarcel Holtmann return -EOPNOTSUPP; 140606f43cbcSMarcel Holtmann 14071da177e4SLinus Torvalds lock_sock(sk); 14081da177e4SLinus Torvalds 1409e0448092STetsuo Handa hdev = hci_hdev_from_sock(sk); 1410e0448092STetsuo Handa if (IS_ERR(hdev)) { 1411e0448092STetsuo Handa err = PTR_ERR(hdev); 14129d4b68b2SMarcel Holtmann goto done; 14139d4b68b2SMarcel Holtmann } 14149d4b68b2SMarcel Holtmann 14151da177e4SLinus Torvalds haddr->hci_family = AF_BLUETOOTH; 14167b005bd3SMarcel Holtmann haddr->hci_dev = hdev->id; 14179d4b68b2SMarcel Holtmann haddr->hci_channel= hci_pi(sk)->channel; 14189b2c45d4SDenys Vlasenko err = sizeof(*haddr); 14191da177e4SLinus Torvalds 14209d4b68b2SMarcel Holtmann done: 14211da177e4SLinus Torvalds release_sock(sk); 14229d4b68b2SMarcel Holtmann return err; 14231da177e4SLinus Torvalds } 14241da177e4SLinus Torvalds 14256039aa73SGustavo Padovan static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg, 14266039aa73SGustavo Padovan struct sk_buff *skb) 14271da177e4SLinus Torvalds { 142832929e1fSAlain Michaud __u8 mask = hci_pi(sk)->cmsg_mask; 14291da177e4SLinus Torvalds 14300d48d939SMarcel Holtmann if (mask & HCI_CMSG_DIR) { 14310d48d939SMarcel Holtmann int incoming = bt_cb(skb)->incoming; 14328fc9ced3SGustavo Padovan put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming), 14338fc9ced3SGustavo Padovan &incoming); 14340d48d939SMarcel Holtmann } 14351da177e4SLinus Torvalds 1436a61bbcf2SPatrick McHardy if (mask & HCI_CMSG_TSTAMP) { 1437f6e623a6SJohann Felix Soden #ifdef CONFIG_COMPAT 143813c6ee2aSDeepa Dinamani struct old_timeval32 ctv; 1439f6e623a6SJohann Felix Soden #endif 144013c6ee2aSDeepa Dinamani struct __kernel_old_timeval tv; 1441767c5eb5SMarcel Holtmann void *data; 1442767c5eb5SMarcel Holtmann int len; 1443a61bbcf2SPatrick McHardy 1444a61bbcf2SPatrick McHardy skb_get_timestamp(skb, &tv); 1445767c5eb5SMarcel Holtmann 14461da97f83SDavid S. Miller data = &tv; 14471da97f83SDavid S. Miller len = sizeof(tv); 14481da97f83SDavid S. Miller #ifdef CONFIG_COMPAT 1449da88cea1SH. J. Lu if (!COMPAT_USE_64BIT_TIME && 1450da88cea1SH. J. Lu (msg->msg_flags & MSG_CMSG_COMPAT)) { 1451767c5eb5SMarcel Holtmann ctv.tv_sec = tv.tv_sec; 1452767c5eb5SMarcel Holtmann ctv.tv_usec = tv.tv_usec; 1453767c5eb5SMarcel Holtmann data = &ctv; 1454767c5eb5SMarcel Holtmann len = sizeof(ctv); 1455767c5eb5SMarcel Holtmann } 14561da97f83SDavid S. Miller #endif 1457767c5eb5SMarcel Holtmann 1458767c5eb5SMarcel Holtmann put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data); 1459a61bbcf2SPatrick McHardy } 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 14628528d3f7SMarcel Holtmann static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg, 14638528d3f7SMarcel Holtmann size_t len, int flags) 14641da177e4SLinus Torvalds { 14651da177e4SLinus Torvalds struct sock *sk = sock->sk; 14661da177e4SLinus Torvalds struct sk_buff *skb; 14671da177e4SLinus Torvalds int copied, err; 146883871f8cSDenis Kenzior unsigned int skblen; 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds BT_DBG("sock %p, sk %p", sock, sk); 14711da177e4SLinus Torvalds 1472d94a6104SMarcel Holtmann if (flags & MSG_OOB) 14731da177e4SLinus Torvalds return -EOPNOTSUPP; 14741da177e4SLinus Torvalds 1475ac714949SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_LOGGING) 1476ac714949SMarcel Holtmann return -EOPNOTSUPP; 1477ac714949SMarcel Holtmann 14781da177e4SLinus Torvalds if (sk->sk_state == BT_CLOSED) 14791da177e4SLinus Torvalds return 0; 14801da177e4SLinus Torvalds 1481f4b41f06SOliver Hartkopp skb = skb_recv_datagram(sk, flags, &err); 148270f23020SAndrei Emeltchenko if (!skb) 14831da177e4SLinus Torvalds return err; 14841da177e4SLinus Torvalds 148583871f8cSDenis Kenzior skblen = skb->len; 14861da177e4SLinus Torvalds copied = skb->len; 14871da177e4SLinus Torvalds if (len < copied) { 14881da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC; 14891da177e4SLinus Torvalds copied = len; 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds 1492badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 149351f3d02bSDavid S. Miller err = skb_copy_datagram_msg(skb, 0, msg, copied); 14941da177e4SLinus Torvalds 14953a208627SMarcel Holtmann switch (hci_pi(sk)->channel) { 14963a208627SMarcel Holtmann case HCI_CHANNEL_RAW: 14971da177e4SLinus Torvalds hci_sock_cmsg(sk, msg, skb); 14983a208627SMarcel Holtmann break; 149923500189SMarcel Holtmann case HCI_CHANNEL_USER: 1500cd82e61cSMarcel Holtmann case HCI_CHANNEL_MONITOR: 1501cd82e61cSMarcel Holtmann sock_recv_timestamp(msg, sk, skb); 1502cd82e61cSMarcel Holtmann break; 1503801c1e8dSJohan Hedberg default: 1504801c1e8dSJohan Hedberg if (hci_mgmt_chan_find(hci_pi(sk)->channel)) 1505801c1e8dSJohan Hedberg sock_recv_timestamp(msg, sk, skb); 1506801c1e8dSJohan Hedberg break; 15073a208627SMarcel Holtmann } 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds skb_free_datagram(sk, skb); 15101da177e4SLinus Torvalds 15114f34228bSLuiz Augusto von Dentz if (flags & MSG_TRUNC) 151283871f8cSDenis Kenzior copied = skblen; 151383871f8cSDenis Kenzior 15141da177e4SLinus Torvalds return err ? : copied; 15151da177e4SLinus Torvalds } 15161da177e4SLinus Torvalds 151764ba2eb3SLuiz Augusto von Dentz static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk, 151864ba2eb3SLuiz Augusto von Dentz struct sk_buff *skb) 1519fa4335d7SJohan Hedberg { 1520fa4335d7SJohan Hedberg u8 *cp; 1521fa4335d7SJohan Hedberg struct mgmt_hdr *hdr; 1522fa4335d7SJohan Hedberg u16 opcode, index, len; 1523fa4335d7SJohan Hedberg struct hci_dev *hdev = NULL; 1524fa4335d7SJohan Hedberg const struct hci_mgmt_handler *handler; 1525fa4335d7SJohan Hedberg bool var_len, no_hdev; 1526fa4335d7SJohan Hedberg int err; 1527fa4335d7SJohan Hedberg 152864ba2eb3SLuiz Augusto von Dentz BT_DBG("got %d bytes", skb->len); 1529fa4335d7SJohan Hedberg 153064ba2eb3SLuiz Augusto von Dentz if (skb->len < sizeof(*hdr)) 1531fa4335d7SJohan Hedberg return -EINVAL; 1532fa4335d7SJohan Hedberg 153364ba2eb3SLuiz Augusto von Dentz hdr = (void *)skb->data; 1534fa4335d7SJohan Hedberg opcode = __le16_to_cpu(hdr->opcode); 1535fa4335d7SJohan Hedberg index = __le16_to_cpu(hdr->index); 1536fa4335d7SJohan Hedberg len = __le16_to_cpu(hdr->len); 1537fa4335d7SJohan Hedberg 153864ba2eb3SLuiz Augusto von Dentz if (len != skb->len - sizeof(*hdr)) { 1539fa4335d7SJohan Hedberg err = -EINVAL; 1540fa4335d7SJohan Hedberg goto done; 1541fa4335d7SJohan Hedberg } 1542fa4335d7SJohan Hedberg 154338ceaa00SMarcel Holtmann if (chan->channel == HCI_CHANNEL_CONTROL) { 154464ba2eb3SLuiz Augusto von Dentz struct sk_buff *cmd; 154538ceaa00SMarcel Holtmann 154638ceaa00SMarcel Holtmann /* Send event to monitor */ 154764ba2eb3SLuiz Augusto von Dentz cmd = create_monitor_ctrl_command(sk, index, opcode, len, 154864ba2eb3SLuiz Augusto von Dentz skb->data + sizeof(*hdr)); 154964ba2eb3SLuiz Augusto von Dentz if (cmd) { 155064ba2eb3SLuiz Augusto von Dentz hci_send_to_channel(HCI_CHANNEL_MONITOR, cmd, 155138ceaa00SMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 155264ba2eb3SLuiz Augusto von Dentz kfree_skb(cmd); 155338ceaa00SMarcel Holtmann } 155438ceaa00SMarcel Holtmann } 155538ceaa00SMarcel Holtmann 1556fa4335d7SJohan Hedberg if (opcode >= chan->handler_count || 1557fa4335d7SJohan Hedberg chan->handlers[opcode].func == NULL) { 1558fa4335d7SJohan Hedberg BT_DBG("Unknown op %u", opcode); 1559fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1560fa4335d7SJohan Hedberg MGMT_STATUS_UNKNOWN_COMMAND); 1561fa4335d7SJohan Hedberg goto done; 1562fa4335d7SJohan Hedberg } 1563fa4335d7SJohan Hedberg 1564fa4335d7SJohan Hedberg handler = &chan->handlers[opcode]; 1565fa4335d7SJohan Hedberg 1566fa4335d7SJohan Hedberg if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) && 1567fa4335d7SJohan Hedberg !(handler->flags & HCI_MGMT_UNTRUSTED)) { 1568fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1569fa4335d7SJohan Hedberg MGMT_STATUS_PERMISSION_DENIED); 1570fa4335d7SJohan Hedberg goto done; 1571fa4335d7SJohan Hedberg } 1572fa4335d7SJohan Hedberg 1573fa4335d7SJohan Hedberg if (index != MGMT_INDEX_NONE) { 1574fa4335d7SJohan Hedberg hdev = hci_dev_get(index); 1575fa4335d7SJohan Hedberg if (!hdev) { 1576fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1577fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1578fa4335d7SJohan Hedberg goto done; 1579fa4335d7SJohan Hedberg } 1580fa4335d7SJohan Hedberg 1581fa4335d7SJohan Hedberg if (hci_dev_test_flag(hdev, HCI_SETUP) || 1582fa4335d7SJohan Hedberg hci_dev_test_flag(hdev, HCI_CONFIG) || 1583fa4335d7SJohan Hedberg hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 1584fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1585fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1586fa4335d7SJohan Hedberg goto done; 1587fa4335d7SJohan Hedberg } 1588fa4335d7SJohan Hedberg 1589fa4335d7SJohan Hedberg if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && 1590fa4335d7SJohan Hedberg !(handler->flags & HCI_MGMT_UNCONFIGURED)) { 1591fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1592fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1593fa4335d7SJohan Hedberg goto done; 1594fa4335d7SJohan Hedberg } 1595fa4335d7SJohan Hedberg } 1596fa4335d7SJohan Hedberg 1597d5cc6626SMarcel Holtmann if (!(handler->flags & HCI_MGMT_HDEV_OPTIONAL)) { 1598fa4335d7SJohan Hedberg no_hdev = (handler->flags & HCI_MGMT_NO_HDEV); 1599fa4335d7SJohan Hedberg if (no_hdev != !hdev) { 1600fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1601fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1602fa4335d7SJohan Hedberg goto done; 1603fa4335d7SJohan Hedberg } 1604d5cc6626SMarcel Holtmann } 1605fa4335d7SJohan Hedberg 1606fa4335d7SJohan Hedberg var_len = (handler->flags & HCI_MGMT_VAR_LEN); 1607fa4335d7SJohan Hedberg if ((var_len && len < handler->data_len) || 1608fa4335d7SJohan Hedberg (!var_len && len != handler->data_len)) { 1609fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1610fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_PARAMS); 1611fa4335d7SJohan Hedberg goto done; 1612fa4335d7SJohan Hedberg } 1613fa4335d7SJohan Hedberg 1614fa4335d7SJohan Hedberg if (hdev && chan->hdev_init) 1615fa4335d7SJohan Hedberg chan->hdev_init(sk, hdev); 1616fa4335d7SJohan Hedberg 161764ba2eb3SLuiz Augusto von Dentz cp = skb->data + sizeof(*hdr); 1618fa4335d7SJohan Hedberg 1619fa4335d7SJohan Hedberg err = handler->func(sk, hdev, cp, len); 1620fa4335d7SJohan Hedberg if (err < 0) 1621fa4335d7SJohan Hedberg goto done; 1622fa4335d7SJohan Hedberg 162364ba2eb3SLuiz Augusto von Dentz err = skb->len; 1624fa4335d7SJohan Hedberg 1625fa4335d7SJohan Hedberg done: 1626fa4335d7SJohan Hedberg if (hdev) 1627fa4335d7SJohan Hedberg hci_dev_put(hdev); 1628fa4335d7SJohan Hedberg 1629fa4335d7SJohan Hedberg return err; 1630fa4335d7SJohan Hedberg } 1631fa4335d7SJohan Hedberg 163264ba2eb3SLuiz Augusto von Dentz static int hci_logging_frame(struct sock *sk, struct sk_buff *skb, 163364ba2eb3SLuiz Augusto von Dentz unsigned int flags) 1634ac714949SMarcel Holtmann { 1635ac714949SMarcel Holtmann struct hci_mon_hdr *hdr; 1636ac714949SMarcel Holtmann struct hci_dev *hdev; 1637ac714949SMarcel Holtmann u16 index; 1638ac714949SMarcel Holtmann int err; 1639ac714949SMarcel Holtmann 1640ac714949SMarcel Holtmann /* The logging frame consists at minimum of the standard header, 1641ac714949SMarcel Holtmann * the priority byte, the ident length byte and at least one string 1642ac714949SMarcel Holtmann * terminator NUL byte. Anything shorter are invalid packets. 1643ac714949SMarcel Holtmann */ 164464ba2eb3SLuiz Augusto von Dentz if (skb->len < sizeof(*hdr) + 3) 1645ac714949SMarcel Holtmann return -EINVAL; 1646ac714949SMarcel Holtmann 1647ac714949SMarcel Holtmann hdr = (void *)skb->data; 1648ac714949SMarcel Holtmann 164964ba2eb3SLuiz Augusto von Dentz if (__le16_to_cpu(hdr->len) != skb->len - sizeof(*hdr)) 165064ba2eb3SLuiz Augusto von Dentz return -EINVAL; 1651ac714949SMarcel Holtmann 1652ac714949SMarcel Holtmann if (__le16_to_cpu(hdr->opcode) == 0x0000) { 1653ac714949SMarcel Holtmann __u8 priority = skb->data[sizeof(*hdr)]; 1654ac714949SMarcel Holtmann __u8 ident_len = skb->data[sizeof(*hdr) + 1]; 1655ac714949SMarcel Holtmann 1656ac714949SMarcel Holtmann /* Only the priorities 0-7 are valid and with that any other 1657ac714949SMarcel Holtmann * value results in an invalid packet. 1658ac714949SMarcel Holtmann * 1659ac714949SMarcel Holtmann * The priority byte is followed by an ident length byte and 1660ac714949SMarcel Holtmann * the NUL terminated ident string. Check that the ident 1661ac714949SMarcel Holtmann * length is not overflowing the packet and also that the 1662ac714949SMarcel Holtmann * ident string itself is NUL terminated. In case the ident 1663ac714949SMarcel Holtmann * length is zero, the length value actually doubles as NUL 1664ac714949SMarcel Holtmann * terminator identifier. 1665ac714949SMarcel Holtmann * 1666ac714949SMarcel Holtmann * The message follows the ident string (if present) and 1667ac714949SMarcel Holtmann * must be NUL terminated. Otherwise it is not a valid packet. 1668ac714949SMarcel Holtmann */ 166964ba2eb3SLuiz Augusto von Dentz if (priority > 7 || skb->data[skb->len - 1] != 0x00 || 167064ba2eb3SLuiz Augusto von Dentz ident_len > skb->len - sizeof(*hdr) - 3 || 167164ba2eb3SLuiz Augusto von Dentz skb->data[sizeof(*hdr) + ident_len + 1] != 0x00) 167264ba2eb3SLuiz Augusto von Dentz return -EINVAL; 1673ac714949SMarcel Holtmann } else { 167464ba2eb3SLuiz Augusto von Dentz return -EINVAL; 1675ac714949SMarcel Holtmann } 1676ac714949SMarcel Holtmann 1677ac714949SMarcel Holtmann index = __le16_to_cpu(hdr->index); 1678ac714949SMarcel Holtmann 1679ac714949SMarcel Holtmann if (index != MGMT_INDEX_NONE) { 1680ac714949SMarcel Holtmann hdev = hci_dev_get(index); 168164ba2eb3SLuiz Augusto von Dentz if (!hdev) 168264ba2eb3SLuiz Augusto von Dentz return -ENODEV; 1683ac714949SMarcel Holtmann } else { 1684ac714949SMarcel Holtmann hdev = NULL; 1685ac714949SMarcel Holtmann } 1686ac714949SMarcel Holtmann 1687ac714949SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_USER_LOGGING); 1688ac714949SMarcel Holtmann 1689ac714949SMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, HCI_SOCK_TRUSTED, NULL); 169064ba2eb3SLuiz Augusto von Dentz err = skb->len; 1691ac714949SMarcel Holtmann 1692ac714949SMarcel Holtmann if (hdev) 1693ac714949SMarcel Holtmann hci_dev_put(hdev); 1694ac714949SMarcel Holtmann 1695ac714949SMarcel Holtmann return err; 1696ac714949SMarcel Holtmann } 1697ac714949SMarcel Holtmann 16981b784140SYing Xue static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg, 16991b784140SYing Xue size_t len) 17001da177e4SLinus Torvalds { 17011da177e4SLinus Torvalds struct sock *sk = sock->sk; 1702801c1e8dSJohan Hedberg struct hci_mgmt_chan *chan; 17031da177e4SLinus Torvalds struct hci_dev *hdev; 17041da177e4SLinus Torvalds struct sk_buff *skb; 17051da177e4SLinus Torvalds int err; 17060b59e272STetsuo Handa const unsigned int flags = msg->msg_flags; 17071da177e4SLinus Torvalds 17081da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 17091da177e4SLinus Torvalds 17100b59e272STetsuo Handa if (flags & MSG_OOB) 17111da177e4SLinus Torvalds return -EOPNOTSUPP; 17121da177e4SLinus Torvalds 17130b59e272STetsuo Handa if (flags & ~(MSG_DONTWAIT | MSG_NOSIGNAL | MSG_ERRQUEUE | MSG_CMSG_COMPAT)) 17141da177e4SLinus Torvalds return -EINVAL; 17151da177e4SLinus Torvalds 171609572fcaSLuiz Augusto von Dentz if (len < 4 || len > hci_pi(sk)->mtu) 17171da177e4SLinus Torvalds return -EINVAL; 17181da177e4SLinus Torvalds 171964ba2eb3SLuiz Augusto von Dentz skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0); 172064ba2eb3SLuiz Augusto von Dentz if (IS_ERR(skb)) 172164ba2eb3SLuiz Augusto von Dentz return PTR_ERR(skb); 17220b59e272STetsuo Handa 17231da177e4SLinus Torvalds lock_sock(sk); 17241da177e4SLinus Torvalds 17250381101fSJohan Hedberg switch (hci_pi(sk)->channel) { 17260381101fSJohan Hedberg case HCI_CHANNEL_RAW: 172723500189SMarcel Holtmann case HCI_CHANNEL_USER: 17280381101fSJohan Hedberg break; 1729cd82e61cSMarcel Holtmann case HCI_CHANNEL_MONITOR: 1730cd82e61cSMarcel Holtmann err = -EOPNOTSUPP; 173164ba2eb3SLuiz Augusto von Dentz goto drop; 1732ac714949SMarcel Holtmann case HCI_CHANNEL_LOGGING: 173364ba2eb3SLuiz Augusto von Dentz err = hci_logging_frame(sk, skb, flags); 173464ba2eb3SLuiz Augusto von Dentz goto drop; 17350381101fSJohan Hedberg default: 1736801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock); 1737801c1e8dSJohan Hedberg chan = __hci_mgmt_chan_find(hci_pi(sk)->channel); 1738801c1e8dSJohan Hedberg if (chan) 173964ba2eb3SLuiz Augusto von Dentz err = hci_mgmt_cmd(chan, sk, skb); 1740801c1e8dSJohan Hedberg else 17410381101fSJohan Hedberg err = -EINVAL; 1742801c1e8dSJohan Hedberg 1743801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 174464ba2eb3SLuiz Augusto von Dentz goto drop; 17450381101fSJohan Hedberg } 17460381101fSJohan Hedberg 1747e0448092STetsuo Handa hdev = hci_hdev_from_sock(sk); 1748e0448092STetsuo Handa if (IS_ERR(hdev)) { 1749e0448092STetsuo Handa err = PTR_ERR(hdev); 175064ba2eb3SLuiz Augusto von Dentz goto drop; 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds 17537e21addcSMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags)) { 17547e21addcSMarcel Holtmann err = -ENETDOWN; 175564ba2eb3SLuiz Augusto von Dentz goto drop; 17567e21addcSMarcel Holtmann } 17577e21addcSMarcel Holtmann 17588528d3f7SMarcel Holtmann hci_skb_pkt_type(skb) = skb->data[0]; 17591da177e4SLinus Torvalds skb_pull(skb, 1); 17601da177e4SLinus Torvalds 17611bc5ad16SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_USER) { 17621bc5ad16SMarcel Holtmann /* No permission check is needed for user channel 17631bc5ad16SMarcel Holtmann * since that gets enforced when binding the socket. 17641bc5ad16SMarcel Holtmann * 17651bc5ad16SMarcel Holtmann * However check that the packet type is valid. 17661bc5ad16SMarcel Holtmann */ 1767d79f34e3SMarcel Holtmann if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT && 1768d79f34e3SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT && 1769cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_SCODATA_PKT && 1770cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) { 17711bc5ad16SMarcel Holtmann err = -EINVAL; 17721bc5ad16SMarcel Holtmann goto drop; 17731bc5ad16SMarcel Holtmann } 17741bc5ad16SMarcel Holtmann 17751bc5ad16SMarcel Holtmann skb_queue_tail(&hdev->raw_q, skb); 17761bc5ad16SMarcel Holtmann queue_work(hdev->workqueue, &hdev->tx_work); 1777d79f34e3SMarcel Holtmann } else if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) { 177883985319SHarvey Harrison u16 opcode = get_unaligned_le16(skb->data); 17791da177e4SLinus Torvalds u16 ogf = hci_opcode_ogf(opcode); 17801da177e4SLinus Torvalds u16 ocf = hci_opcode_ocf(opcode); 17811da177e4SLinus Torvalds 17821da177e4SLinus Torvalds if (((ogf > HCI_SFLT_MAX_OGF) || 17833bb3c755SGustavo Padovan !hci_test_bit(ocf & HCI_FLT_OCF_BITS, 17843bb3c755SGustavo Padovan &hci_sec_filter.ocf_mask[ogf])) && 17851da177e4SLinus Torvalds !capable(CAP_NET_RAW)) { 17861da177e4SLinus Torvalds err = -EPERM; 17871da177e4SLinus Torvalds goto drop; 17881da177e4SLinus Torvalds } 17891da177e4SLinus Torvalds 17901982162bSMarcel Holtmann /* Since the opcode has already been extracted here, store 17911982162bSMarcel Holtmann * a copy of the value for later use by the drivers. 17921982162bSMarcel Holtmann */ 17931982162bSMarcel Holtmann hci_skb_opcode(skb) = opcode; 17941982162bSMarcel Holtmann 1795fee746b0SMarcel Holtmann if (ogf == 0x3f) { 17961da177e4SLinus Torvalds skb_queue_tail(&hdev->raw_q, skb); 17973eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work); 17981da177e4SLinus Torvalds } else { 179949c922bbSStephen Hemminger /* Stand-alone HCI commands must be flagged as 180011714b3dSJohan Hedberg * single-command requests. 180111714b3dSJohan Hedberg */ 180244d27137SJohan Hedberg bt_cb(skb)->hci.req_flags |= HCI_REQ_START; 180311714b3dSJohan Hedberg 18041da177e4SLinus Torvalds skb_queue_tail(&hdev->cmd_q, skb); 1805c347b765SGustavo F. Padovan queue_work(hdev->workqueue, &hdev->cmd_work); 18061da177e4SLinus Torvalds } 18071da177e4SLinus Torvalds } else { 18081da177e4SLinus Torvalds if (!capable(CAP_NET_RAW)) { 18091da177e4SLinus Torvalds err = -EPERM; 18101da177e4SLinus Torvalds goto drop; 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds 1813d79f34e3SMarcel Holtmann if (hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT && 1814cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_SCODATA_PKT && 1815cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) { 1816bb77543eSMarcel Holtmann err = -EINVAL; 1817bb77543eSMarcel Holtmann goto drop; 1818bb77543eSMarcel Holtmann } 1819bb77543eSMarcel Holtmann 18201da177e4SLinus Torvalds skb_queue_tail(&hdev->raw_q, skb); 18213eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work); 18221da177e4SLinus Torvalds } 18231da177e4SLinus Torvalds 18241da177e4SLinus Torvalds err = len; 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds done: 18271da177e4SLinus Torvalds release_sock(sk); 18281da177e4SLinus Torvalds return err; 18291da177e4SLinus Torvalds 18301da177e4SLinus Torvalds drop: 18311da177e4SLinus Torvalds kfree_skb(skb); 18321da177e4SLinus Torvalds goto done; 18331da177e4SLinus Torvalds } 18341da177e4SLinus Torvalds 183509572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt_old(struct socket *sock, int level, int optname, 1836a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int len) 18371da177e4SLinus Torvalds { 18381da177e4SLinus Torvalds struct hci_ufilter uf = { .opcode = 0 }; 18391da177e4SLinus Torvalds struct sock *sk = sock->sk; 18401da177e4SLinus Torvalds int err = 0, opt = 0; 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds BT_DBG("sk %p, opt %d", sk, optname); 18431da177e4SLinus Torvalds 18441da177e4SLinus Torvalds lock_sock(sk); 18451da177e4SLinus Torvalds 18462f39cdb7SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 1847c2371e80SMarcel Holtmann err = -EBADFD; 18482f39cdb7SMarcel Holtmann goto done; 18492f39cdb7SMarcel Holtmann } 18502f39cdb7SMarcel Holtmann 18511da177e4SLinus Torvalds switch (optname) { 18521da177e4SLinus Torvalds case HCI_DATA_DIR: 1853a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&opt, optval, sizeof(opt))) { 18541da177e4SLinus Torvalds err = -EFAULT; 18551da177e4SLinus Torvalds break; 18561da177e4SLinus Torvalds } 18571da177e4SLinus Torvalds 18581da177e4SLinus Torvalds if (opt) 18591da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR; 18601da177e4SLinus Torvalds else 18611da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR; 18621da177e4SLinus Torvalds break; 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds case HCI_TIME_STAMP: 1865a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&opt, optval, sizeof(opt))) { 18661da177e4SLinus Torvalds err = -EFAULT; 18671da177e4SLinus Torvalds break; 18681da177e4SLinus Torvalds } 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds if (opt) 18711da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP; 18721da177e4SLinus Torvalds else 18731da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP; 18741da177e4SLinus Torvalds break; 18751da177e4SLinus Torvalds 18761da177e4SLinus Torvalds case HCI_FILTER: 18770878b666SMarcel Holtmann { 18780878b666SMarcel Holtmann struct hci_filter *f = &hci_pi(sk)->filter; 18790878b666SMarcel Holtmann 18800878b666SMarcel Holtmann uf.type_mask = f->type_mask; 18810878b666SMarcel Holtmann uf.opcode = f->opcode; 18820878b666SMarcel Holtmann uf.event_mask[0] = *((u32 *) f->event_mask + 0); 18830878b666SMarcel Holtmann uf.event_mask[1] = *((u32 *) f->event_mask + 1); 18840878b666SMarcel Holtmann } 18850878b666SMarcel Holtmann 18861da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(uf)); 1887a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&uf, optval, len)) { 18881da177e4SLinus Torvalds err = -EFAULT; 18891da177e4SLinus Torvalds break; 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds if (!capable(CAP_NET_RAW)) { 18931da177e4SLinus Torvalds uf.type_mask &= hci_sec_filter.type_mask; 18941da177e4SLinus Torvalds uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0); 18951da177e4SLinus Torvalds uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1); 18961da177e4SLinus Torvalds } 18971da177e4SLinus Torvalds 18981da177e4SLinus Torvalds { 18991da177e4SLinus Torvalds struct hci_filter *f = &hci_pi(sk)->filter; 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds f->type_mask = uf.type_mask; 19021da177e4SLinus Torvalds f->opcode = uf.opcode; 19031da177e4SLinus Torvalds *((u32 *) f->event_mask + 0) = uf.event_mask[0]; 19041da177e4SLinus Torvalds *((u32 *) f->event_mask + 1) = uf.event_mask[1]; 19051da177e4SLinus Torvalds } 19061da177e4SLinus Torvalds break; 19071da177e4SLinus Torvalds 19081da177e4SLinus Torvalds default: 19091da177e4SLinus Torvalds err = -ENOPROTOOPT; 19101da177e4SLinus Torvalds break; 19111da177e4SLinus Torvalds } 19121da177e4SLinus Torvalds 19132f39cdb7SMarcel Holtmann done: 19141da177e4SLinus Torvalds release_sock(sk); 19151da177e4SLinus Torvalds return err; 19161da177e4SLinus Torvalds } 19171da177e4SLinus Torvalds 191809572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt(struct socket *sock, int level, int optname, 191909572fcaSLuiz Augusto von Dentz sockptr_t optval, unsigned int len) 192009572fcaSLuiz Augusto von Dentz { 192109572fcaSLuiz Augusto von Dentz struct sock *sk = sock->sk; 1922b9f9dbadSDan Carpenter int err = 0; 1923b9f9dbadSDan Carpenter u16 opt; 192409572fcaSLuiz Augusto von Dentz 192509572fcaSLuiz Augusto von Dentz BT_DBG("sk %p, opt %d", sk, optname); 192609572fcaSLuiz Augusto von Dentz 192709572fcaSLuiz Augusto von Dentz if (level == SOL_HCI) 192809572fcaSLuiz Augusto von Dentz return hci_sock_setsockopt_old(sock, level, optname, optval, 192909572fcaSLuiz Augusto von Dentz len); 193009572fcaSLuiz Augusto von Dentz 193109572fcaSLuiz Augusto von Dentz if (level != SOL_BLUETOOTH) 193209572fcaSLuiz Augusto von Dentz return -ENOPROTOOPT; 193309572fcaSLuiz Augusto von Dentz 193409572fcaSLuiz Augusto von Dentz lock_sock(sk); 193509572fcaSLuiz Augusto von Dentz 193609572fcaSLuiz Augusto von Dentz switch (optname) { 193709572fcaSLuiz Augusto von Dentz case BT_SNDMTU: 193809572fcaSLuiz Augusto von Dentz case BT_RCVMTU: 193909572fcaSLuiz Augusto von Dentz switch (hci_pi(sk)->channel) { 194009572fcaSLuiz Augusto von Dentz /* Don't allow changing MTU for channels that are meant for HCI 194109572fcaSLuiz Augusto von Dentz * traffic only. 194209572fcaSLuiz Augusto von Dentz */ 194309572fcaSLuiz Augusto von Dentz case HCI_CHANNEL_RAW: 194409572fcaSLuiz Augusto von Dentz case HCI_CHANNEL_USER: 194509572fcaSLuiz Augusto von Dentz err = -ENOPROTOOPT; 194609572fcaSLuiz Augusto von Dentz goto done; 194709572fcaSLuiz Augusto von Dentz } 194809572fcaSLuiz Augusto von Dentz 1949b9f9dbadSDan Carpenter if (copy_from_sockptr(&opt, optval, sizeof(opt))) { 195009572fcaSLuiz Augusto von Dentz err = -EFAULT; 195109572fcaSLuiz Augusto von Dentz break; 195209572fcaSLuiz Augusto von Dentz } 195309572fcaSLuiz Augusto von Dentz 195409572fcaSLuiz Augusto von Dentz hci_pi(sk)->mtu = opt; 195509572fcaSLuiz Augusto von Dentz break; 195609572fcaSLuiz Augusto von Dentz 195709572fcaSLuiz Augusto von Dentz default: 195809572fcaSLuiz Augusto von Dentz err = -ENOPROTOOPT; 195909572fcaSLuiz Augusto von Dentz break; 196009572fcaSLuiz Augusto von Dentz } 196109572fcaSLuiz Augusto von Dentz 196209572fcaSLuiz Augusto von Dentz done: 196309572fcaSLuiz Augusto von Dentz release_sock(sk); 196409572fcaSLuiz Augusto von Dentz return err; 196509572fcaSLuiz Augusto von Dentz } 196609572fcaSLuiz Augusto von Dentz 196709572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt_old(struct socket *sock, int level, int optname, 19688fc9ced3SGustavo Padovan char __user *optval, int __user *optlen) 19691da177e4SLinus Torvalds { 19701da177e4SLinus Torvalds struct hci_ufilter uf; 19711da177e4SLinus Torvalds struct sock *sk = sock->sk; 1972cedc5469SMarcel Holtmann int len, opt, err = 0; 1973cedc5469SMarcel Holtmann 1974cedc5469SMarcel Holtmann BT_DBG("sk %p, opt %d", sk, optname); 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds if (get_user(len, optlen)) 19771da177e4SLinus Torvalds return -EFAULT; 19781da177e4SLinus Torvalds 1979cedc5469SMarcel Holtmann lock_sock(sk); 1980cedc5469SMarcel Holtmann 1981cedc5469SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 1982c2371e80SMarcel Holtmann err = -EBADFD; 1983cedc5469SMarcel Holtmann goto done; 1984cedc5469SMarcel Holtmann } 1985cedc5469SMarcel Holtmann 19861da177e4SLinus Torvalds switch (optname) { 19871da177e4SLinus Torvalds case HCI_DATA_DIR: 19881da177e4SLinus Torvalds if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR) 19891da177e4SLinus Torvalds opt = 1; 19901da177e4SLinus Torvalds else 19911da177e4SLinus Torvalds opt = 0; 19921da177e4SLinus Torvalds 19931da177e4SLinus Torvalds if (put_user(opt, optval)) 1994cedc5469SMarcel Holtmann err = -EFAULT; 19951da177e4SLinus Torvalds break; 19961da177e4SLinus Torvalds 19971da177e4SLinus Torvalds case HCI_TIME_STAMP: 19981da177e4SLinus Torvalds if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP) 19991da177e4SLinus Torvalds opt = 1; 20001da177e4SLinus Torvalds else 20011da177e4SLinus Torvalds opt = 0; 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds if (put_user(opt, optval)) 2004cedc5469SMarcel Holtmann err = -EFAULT; 20051da177e4SLinus Torvalds break; 20061da177e4SLinus Torvalds 20071da177e4SLinus Torvalds case HCI_FILTER: 20081da177e4SLinus Torvalds { 20091da177e4SLinus Torvalds struct hci_filter *f = &hci_pi(sk)->filter; 20101da177e4SLinus Torvalds 2011e15ca9a0SMathias Krause memset(&uf, 0, sizeof(uf)); 20121da177e4SLinus Torvalds uf.type_mask = f->type_mask; 20131da177e4SLinus Torvalds uf.opcode = f->opcode; 20141da177e4SLinus Torvalds uf.event_mask[0] = *((u32 *) f->event_mask + 0); 20151da177e4SLinus Torvalds uf.event_mask[1] = *((u32 *) f->event_mask + 1); 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(uf)); 20191da177e4SLinus Torvalds if (copy_to_user(optval, &uf, len)) 2020cedc5469SMarcel Holtmann err = -EFAULT; 20211da177e4SLinus Torvalds break; 20221da177e4SLinus Torvalds 20231da177e4SLinus Torvalds default: 2024cedc5469SMarcel Holtmann err = -ENOPROTOOPT; 20251da177e4SLinus Torvalds break; 20261da177e4SLinus Torvalds } 20271da177e4SLinus Torvalds 2028cedc5469SMarcel Holtmann done: 2029cedc5469SMarcel Holtmann release_sock(sk); 2030cedc5469SMarcel Holtmann return err; 20311da177e4SLinus Torvalds } 20321da177e4SLinus Torvalds 203309572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt(struct socket *sock, int level, int optname, 203409572fcaSLuiz Augusto von Dentz char __user *optval, int __user *optlen) 203509572fcaSLuiz Augusto von Dentz { 203609572fcaSLuiz Augusto von Dentz struct sock *sk = sock->sk; 203709572fcaSLuiz Augusto von Dentz int err = 0; 203809572fcaSLuiz Augusto von Dentz 203909572fcaSLuiz Augusto von Dentz BT_DBG("sk %p, opt %d", sk, optname); 204009572fcaSLuiz Augusto von Dentz 204109572fcaSLuiz Augusto von Dentz if (level == SOL_HCI) 204209572fcaSLuiz Augusto von Dentz return hci_sock_getsockopt_old(sock, level, optname, optval, 204309572fcaSLuiz Augusto von Dentz optlen); 204409572fcaSLuiz Augusto von Dentz 204509572fcaSLuiz Augusto von Dentz if (level != SOL_BLUETOOTH) 204609572fcaSLuiz Augusto von Dentz return -ENOPROTOOPT; 204709572fcaSLuiz Augusto von Dentz 204809572fcaSLuiz Augusto von Dentz lock_sock(sk); 204909572fcaSLuiz Augusto von Dentz 205009572fcaSLuiz Augusto von Dentz switch (optname) { 205109572fcaSLuiz Augusto von Dentz case BT_SNDMTU: 205209572fcaSLuiz Augusto von Dentz case BT_RCVMTU: 205309572fcaSLuiz Augusto von Dentz if (put_user(hci_pi(sk)->mtu, (u16 __user *)optval)) 205409572fcaSLuiz Augusto von Dentz err = -EFAULT; 205509572fcaSLuiz Augusto von Dentz break; 205609572fcaSLuiz Augusto von Dentz 205709572fcaSLuiz Augusto von Dentz default: 205809572fcaSLuiz Augusto von Dentz err = -ENOPROTOOPT; 205909572fcaSLuiz Augusto von Dentz break; 206009572fcaSLuiz Augusto von Dentz } 206109572fcaSLuiz Augusto von Dentz 206209572fcaSLuiz Augusto von Dentz release_sock(sk); 206309572fcaSLuiz Augusto von Dentz return err; 206409572fcaSLuiz Augusto von Dentz } 206509572fcaSLuiz Augusto von Dentz 2066709fca50SNguyen Dinh Phi static void hci_sock_destruct(struct sock *sk) 2067709fca50SNguyen Dinh Phi { 2068*b338d917SBrian Gix mgmt_cleanup(sk); 2069709fca50SNguyen Dinh Phi skb_queue_purge(&sk->sk_receive_queue); 2070709fca50SNguyen Dinh Phi skb_queue_purge(&sk->sk_write_queue); 2071709fca50SNguyen Dinh Phi } 2072709fca50SNguyen Dinh Phi 207390ddc4f0SEric Dumazet static const struct proto_ops hci_sock_ops = { 20741da177e4SLinus Torvalds .family = PF_BLUETOOTH, 20751da177e4SLinus Torvalds .owner = THIS_MODULE, 20761da177e4SLinus Torvalds .release = hci_sock_release, 20771da177e4SLinus Torvalds .bind = hci_sock_bind, 20781da177e4SLinus Torvalds .getname = hci_sock_getname, 20791da177e4SLinus Torvalds .sendmsg = hci_sock_sendmsg, 20801da177e4SLinus Torvalds .recvmsg = hci_sock_recvmsg, 20811da177e4SLinus Torvalds .ioctl = hci_sock_ioctl, 20827a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT 20837a6038b3SArnd Bergmann .compat_ioctl = hci_sock_compat_ioctl, 20847a6038b3SArnd Bergmann #endif 2085a11e1d43SLinus Torvalds .poll = datagram_poll, 20861da177e4SLinus Torvalds .listen = sock_no_listen, 20871da177e4SLinus Torvalds .shutdown = sock_no_shutdown, 20881da177e4SLinus Torvalds .setsockopt = hci_sock_setsockopt, 20891da177e4SLinus Torvalds .getsockopt = hci_sock_getsockopt, 20901da177e4SLinus Torvalds .connect = sock_no_connect, 20911da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 20921da177e4SLinus Torvalds .accept = sock_no_accept, 20931da177e4SLinus Torvalds .mmap = sock_no_mmap 20941da177e4SLinus Torvalds }; 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds static struct proto hci_sk_proto = { 20971da177e4SLinus Torvalds .name = "HCI", 20981da177e4SLinus Torvalds .owner = THIS_MODULE, 20991da177e4SLinus Torvalds .obj_size = sizeof(struct hci_pinfo) 21001da177e4SLinus Torvalds }; 21011da177e4SLinus Torvalds 21023f378b68SEric Paris static int hci_sock_create(struct net *net, struct socket *sock, int protocol, 21033f378b68SEric Paris int kern) 21041da177e4SLinus Torvalds { 21051da177e4SLinus Torvalds struct sock *sk; 21061da177e4SLinus Torvalds 21071da177e4SLinus Torvalds BT_DBG("sock %p", sock); 21081da177e4SLinus Torvalds 21091da177e4SLinus Torvalds if (sock->type != SOCK_RAW) 21101da177e4SLinus Torvalds return -ESOCKTNOSUPPORT; 21111da177e4SLinus Torvalds 21121da177e4SLinus Torvalds sock->ops = &hci_sock_ops; 21131da177e4SLinus Torvalds 211411aa9c28SEric W. Biederman sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto, kern); 21151da177e4SLinus Torvalds if (!sk) 21161da177e4SLinus Torvalds return -ENOMEM; 21171da177e4SLinus Torvalds 21181da177e4SLinus Torvalds sock_init_data(sock, sk); 21191da177e4SLinus Torvalds 21201da177e4SLinus Torvalds sock_reset_flag(sk, SOCK_ZAPPED); 21211da177e4SLinus Torvalds 21221da177e4SLinus Torvalds sk->sk_protocol = protocol; 21231da177e4SLinus Torvalds 21241da177e4SLinus Torvalds sock->state = SS_UNCONNECTED; 21251da177e4SLinus Torvalds sk->sk_state = BT_OPEN; 2126709fca50SNguyen Dinh Phi sk->sk_destruct = hci_sock_destruct; 21271da177e4SLinus Torvalds 21281da177e4SLinus Torvalds bt_sock_link(&hci_sk_list, sk); 21291da177e4SLinus Torvalds return 0; 21301da177e4SLinus Torvalds } 21311da177e4SLinus Torvalds 2132ec1b4cf7SStephen Hemminger static const struct net_proto_family hci_sock_family_ops = { 21331da177e4SLinus Torvalds .family = PF_BLUETOOTH, 21341da177e4SLinus Torvalds .owner = THIS_MODULE, 21351da177e4SLinus Torvalds .create = hci_sock_create, 21361da177e4SLinus Torvalds }; 21371da177e4SLinus Torvalds 21381da177e4SLinus Torvalds int __init hci_sock_init(void) 21391da177e4SLinus Torvalds { 21401da177e4SLinus Torvalds int err; 21411da177e4SLinus Torvalds 2142b0a8e282SMarcel Holtmann BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr)); 2143b0a8e282SMarcel Holtmann 21441da177e4SLinus Torvalds err = proto_register(&hci_sk_proto, 0); 21451da177e4SLinus Torvalds if (err < 0) 21461da177e4SLinus Torvalds return err; 21471da177e4SLinus Torvalds 21481da177e4SLinus Torvalds err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops); 2149f7c86637SMasatake YAMATO if (err < 0) { 2150f7c86637SMasatake YAMATO BT_ERR("HCI socket registration failed"); 21511da177e4SLinus Torvalds goto error; 2152f7c86637SMasatake YAMATO } 2153f7c86637SMasatake YAMATO 2154b0316615SAl Viro err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL); 2155f7c86637SMasatake YAMATO if (err < 0) { 2156f7c86637SMasatake YAMATO BT_ERR("Failed to create HCI proc file"); 2157f7c86637SMasatake YAMATO bt_sock_unregister(BTPROTO_HCI); 2158f7c86637SMasatake YAMATO goto error; 2159f7c86637SMasatake YAMATO } 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds BT_INFO("HCI socket layer initialized"); 21621da177e4SLinus Torvalds 21631da177e4SLinus Torvalds return 0; 21641da177e4SLinus Torvalds 21651da177e4SLinus Torvalds error: 21661da177e4SLinus Torvalds proto_unregister(&hci_sk_proto); 21671da177e4SLinus Torvalds return err; 21681da177e4SLinus Torvalds } 21691da177e4SLinus Torvalds 2170b7440a14SAnand Gadiyar void hci_sock_cleanup(void) 21711da177e4SLinus Torvalds { 2172f7c86637SMasatake YAMATO bt_procfs_cleanup(&init_net, "hci"); 21735e9d7f86SDavid Herrmann bt_sock_unregister(BTPROTO_HCI); 21741da177e4SLinus Torvalds proto_unregister(&hci_sk_proto); 21751da177e4SLinus Torvalds } 2176