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); 8909380f9eaSLoic Poulain mgmt_index_added(hdev); 89123500189SMarcel Holtmann } 89223500189SMarcel Holtmann 8931da177e4SLinus Torvalds atomic_dec(&hdev->promisc); 8941da177e4SLinus Torvalds hci_dev_put(hdev); 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds sock_orphan(sk); 89811eb85ecSDan Carpenter release_sock(sk); 8991da177e4SLinus Torvalds sock_put(sk); 9001da177e4SLinus Torvalds return 0; 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds 9033d4f9c00SArchie Pusaka static int hci_sock_reject_list_add(struct hci_dev *hdev, void __user *arg) 904f0358568SJohan Hedberg { 905f0358568SJohan Hedberg bdaddr_t bdaddr; 9065e762444SAntti Julku int err; 907f0358568SJohan Hedberg 908f0358568SJohan Hedberg if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) 909f0358568SJohan Hedberg return -EFAULT; 910f0358568SJohan Hedberg 91109fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 9125e762444SAntti Julku 9133d4f9c00SArchie Pusaka err = hci_bdaddr_list_add(&hdev->reject_list, &bdaddr, BDADDR_BREDR); 9145e762444SAntti Julku 91509fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 9165e762444SAntti Julku 9175e762444SAntti Julku return err; 918f0358568SJohan Hedberg } 919f0358568SJohan Hedberg 9203d4f9c00SArchie Pusaka static int hci_sock_reject_list_del(struct hci_dev *hdev, void __user *arg) 921f0358568SJohan Hedberg { 922f0358568SJohan Hedberg bdaddr_t bdaddr; 9235e762444SAntti Julku int err; 924f0358568SJohan Hedberg 925f0358568SJohan Hedberg if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) 926f0358568SJohan Hedberg return -EFAULT; 927f0358568SJohan Hedberg 92809fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 9295e762444SAntti Julku 9303d4f9c00SArchie Pusaka err = hci_bdaddr_list_del(&hdev->reject_list, &bdaddr, BDADDR_BREDR); 9315e762444SAntti Julku 93209fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 9335e762444SAntti Julku 9345e762444SAntti Julku return err; 935f0358568SJohan Hedberg } 936f0358568SJohan Hedberg 9371da177e4SLinus Torvalds /* Ioctls that require bound socket */ 9386039aa73SGustavo Padovan static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd, 9396039aa73SGustavo Padovan unsigned long arg) 9401da177e4SLinus Torvalds { 941e0448092STetsuo Handa struct hci_dev *hdev = hci_hdev_from_sock(sk); 9421da177e4SLinus Torvalds 943e0448092STetsuo Handa if (IS_ERR(hdev)) 944e0448092STetsuo Handa return PTR_ERR(hdev); 9451da177e4SLinus Torvalds 946d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) 9470736cfa8SMarcel Holtmann return -EBUSY; 9480736cfa8SMarcel Holtmann 949d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 950fee746b0SMarcel Holtmann return -EOPNOTSUPP; 951fee746b0SMarcel Holtmann 952ca8bee5dSMarcel Holtmann if (hdev->dev_type != HCI_PRIMARY) 9535b69bef5SMarcel Holtmann return -EOPNOTSUPP; 9545b69bef5SMarcel Holtmann 9551da177e4SLinus Torvalds switch (cmd) { 9561da177e4SLinus Torvalds case HCISETRAW: 9571da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 958bf5b30b8SZhao Hongjiang return -EPERM; 959db596681SMarcel Holtmann return -EOPNOTSUPP; 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds case HCIGETCONNINFO: 9621da177e4SLinus Torvalds return hci_get_conn_info(hdev, (void __user *)arg); 9631da177e4SLinus Torvalds 96440be492fSMarcel Holtmann case HCIGETAUTHINFO: 96540be492fSMarcel Holtmann return hci_get_auth_info(hdev, (void __user *)arg); 96640be492fSMarcel Holtmann 967f0358568SJohan Hedberg case HCIBLOCKADDR: 968f0358568SJohan Hedberg if (!capable(CAP_NET_ADMIN)) 969bf5b30b8SZhao Hongjiang return -EPERM; 9703d4f9c00SArchie Pusaka return hci_sock_reject_list_add(hdev, (void __user *)arg); 971f0358568SJohan Hedberg 972f0358568SJohan Hedberg case HCIUNBLOCKADDR: 973f0358568SJohan Hedberg if (!capable(CAP_NET_ADMIN)) 974bf5b30b8SZhao Hongjiang return -EPERM; 9753d4f9c00SArchie Pusaka return hci_sock_reject_list_del(hdev, (void __user *)arg); 9760736cfa8SMarcel Holtmann } 977f0358568SJohan Hedberg 978324d36edSMarcel Holtmann return -ENOIOCTLCMD; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 9818fc9ced3SGustavo Padovan static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, 9828fc9ced3SGustavo Padovan unsigned long arg) 9831da177e4SLinus Torvalds { 9841da177e4SLinus Torvalds void __user *argp = (void __user *)arg; 9850736cfa8SMarcel Holtmann struct sock *sk = sock->sk; 9861da177e4SLinus Torvalds int err; 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds BT_DBG("cmd %x arg %lx", cmd, arg); 9891da177e4SLinus Torvalds 990000c2fa2SRuihan Li /* Make sure the cmd is valid before doing anything */ 991000c2fa2SRuihan Li switch (cmd) { 992000c2fa2SRuihan Li case HCIGETDEVLIST: 993000c2fa2SRuihan Li case HCIGETDEVINFO: 994000c2fa2SRuihan Li case HCIGETCONNLIST: 995000c2fa2SRuihan Li case HCIDEVUP: 996000c2fa2SRuihan Li case HCIDEVDOWN: 997000c2fa2SRuihan Li case HCIDEVRESET: 998000c2fa2SRuihan Li case HCIDEVRESTAT: 999000c2fa2SRuihan Li case HCISETSCAN: 1000000c2fa2SRuihan Li case HCISETAUTH: 1001000c2fa2SRuihan Li case HCISETENCRYPT: 1002000c2fa2SRuihan Li case HCISETPTYPE: 1003000c2fa2SRuihan Li case HCISETLINKPOL: 1004000c2fa2SRuihan Li case HCISETLINKMODE: 1005000c2fa2SRuihan Li case HCISETACLMTU: 1006000c2fa2SRuihan Li case HCISETSCOMTU: 1007000c2fa2SRuihan Li case HCIINQUIRY: 1008000c2fa2SRuihan Li case HCISETRAW: 1009000c2fa2SRuihan Li case HCIGETCONNINFO: 1010000c2fa2SRuihan Li case HCIGETAUTHINFO: 1011000c2fa2SRuihan Li case HCIBLOCKADDR: 1012000c2fa2SRuihan Li case HCIUNBLOCKADDR: 1013000c2fa2SRuihan Li break; 1014000c2fa2SRuihan Li default: 1015000c2fa2SRuihan Li return -ENOIOCTLCMD; 1016000c2fa2SRuihan Li } 1017000c2fa2SRuihan Li 1018c1c4f956SMarcel Holtmann lock_sock(sk); 1019c1c4f956SMarcel Holtmann 1020c1c4f956SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 1021c1c4f956SMarcel Holtmann err = -EBADFD; 1022c1c4f956SMarcel Holtmann goto done; 1023c1c4f956SMarcel Holtmann } 1024c1c4f956SMarcel Holtmann 1025f81f5b2dSMarcel Holtmann /* When calling an ioctl on an unbound raw socket, then ensure 1026f81f5b2dSMarcel Holtmann * that the monitor gets informed. Ensure that the resulting event 1027f81f5b2dSMarcel Holtmann * is only send once by checking if the cookie exists or not. The 1028f81f5b2dSMarcel Holtmann * socket cookie will be only ever generated once for the lifetime 1029f81f5b2dSMarcel Holtmann * of a given socket. 1030f81f5b2dSMarcel Holtmann */ 1031f81f5b2dSMarcel Holtmann if (hci_sock_gen_cookie(sk)) { 1032f81f5b2dSMarcel Holtmann struct sk_buff *skb; 1033f81f5b2dSMarcel Holtmann 1034*25c150acSRuihan Li /* Perform careful checks before setting the HCI_SOCK_TRUSTED 1035*25c150acSRuihan Li * flag. Make sure that not only the current task but also 1036*25c150acSRuihan Li * the socket opener has the required capability, since 1037*25c150acSRuihan Li * privileged programs can be tricked into making ioctl calls 1038*25c150acSRuihan Li * on HCI sockets, and the socket should not be marked as 1039*25c150acSRuihan Li * trusted simply because the ioctl caller is privileged. 1040*25c150acSRuihan Li */ 1041*25c150acSRuihan Li if (sk_capable(sk, CAP_NET_ADMIN)) 1042f81f5b2dSMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 1043f81f5b2dSMarcel Holtmann 1044f81f5b2dSMarcel Holtmann /* Send event to monitor */ 1045f81f5b2dSMarcel Holtmann skb = create_monitor_ctrl_open(sk); 1046f81f5b2dSMarcel Holtmann if (skb) { 1047f81f5b2dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1048f81f5b2dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1049f81f5b2dSMarcel Holtmann kfree_skb(skb); 1050f81f5b2dSMarcel Holtmann } 1051f81f5b2dSMarcel Holtmann } 1052f81f5b2dSMarcel Holtmann 1053c1c4f956SMarcel Holtmann release_sock(sk); 1054c1c4f956SMarcel Holtmann 10551da177e4SLinus Torvalds switch (cmd) { 10561da177e4SLinus Torvalds case HCIGETDEVLIST: 10571da177e4SLinus Torvalds return hci_get_dev_list(argp); 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds case HCIGETDEVINFO: 10601da177e4SLinus Torvalds return hci_get_dev_info(argp); 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds case HCIGETCONNLIST: 10631da177e4SLinus Torvalds return hci_get_conn_list(argp); 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds case HCIDEVUP: 10661da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1067bf5b30b8SZhao Hongjiang return -EPERM; 10681da177e4SLinus Torvalds return hci_dev_open(arg); 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds case HCIDEVDOWN: 10711da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1072bf5b30b8SZhao Hongjiang return -EPERM; 10731da177e4SLinus Torvalds return hci_dev_close(arg); 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds case HCIDEVRESET: 10761da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1077bf5b30b8SZhao Hongjiang return -EPERM; 10781da177e4SLinus Torvalds return hci_dev_reset(arg); 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds case HCIDEVRESTAT: 10811da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1082bf5b30b8SZhao Hongjiang return -EPERM; 10831da177e4SLinus Torvalds return hci_dev_reset_stat(arg); 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds case HCISETSCAN: 10861da177e4SLinus Torvalds case HCISETAUTH: 10871da177e4SLinus Torvalds case HCISETENCRYPT: 10881da177e4SLinus Torvalds case HCISETPTYPE: 10891da177e4SLinus Torvalds case HCISETLINKPOL: 10901da177e4SLinus Torvalds case HCISETLINKMODE: 10911da177e4SLinus Torvalds case HCISETACLMTU: 10921da177e4SLinus Torvalds case HCISETSCOMTU: 10931da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1094bf5b30b8SZhao Hongjiang return -EPERM; 10951da177e4SLinus Torvalds return hci_dev_cmd(cmd, argp); 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds case HCIINQUIRY: 10981da177e4SLinus Torvalds return hci_inquiry(argp); 1099c1c4f956SMarcel Holtmann } 11001da177e4SLinus Torvalds 11011da177e4SLinus Torvalds lock_sock(sk); 1102c1c4f956SMarcel Holtmann 11031da177e4SLinus Torvalds err = hci_sock_bound_ioctl(sk, cmd, arg); 1104c1c4f956SMarcel Holtmann 1105c1c4f956SMarcel Holtmann done: 11061da177e4SLinus Torvalds release_sock(sk); 11071da177e4SLinus Torvalds return err; 11081da177e4SLinus Torvalds } 11091da177e4SLinus Torvalds 11107a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT 11117a6038b3SArnd Bergmann static int hci_sock_compat_ioctl(struct socket *sock, unsigned int cmd, 11127a6038b3SArnd Bergmann unsigned long arg) 11137a6038b3SArnd Bergmann { 11147a6038b3SArnd Bergmann switch (cmd) { 11157a6038b3SArnd Bergmann case HCIDEVUP: 11167a6038b3SArnd Bergmann case HCIDEVDOWN: 11177a6038b3SArnd Bergmann case HCIDEVRESET: 11187a6038b3SArnd Bergmann case HCIDEVRESTAT: 11197a6038b3SArnd Bergmann return hci_sock_ioctl(sock, cmd, arg); 11207a6038b3SArnd Bergmann } 11217a6038b3SArnd Bergmann 11227a6038b3SArnd Bergmann return hci_sock_ioctl(sock, cmd, (unsigned long)compat_ptr(arg)); 11237a6038b3SArnd Bergmann } 11247a6038b3SArnd Bergmann #endif 11257a6038b3SArnd Bergmann 11268fc9ced3SGustavo Padovan static int hci_sock_bind(struct socket *sock, struct sockaddr *addr, 11278fc9ced3SGustavo Padovan int addr_len) 11281da177e4SLinus Torvalds { 11290381101fSJohan Hedberg struct sockaddr_hci haddr; 11301da177e4SLinus Torvalds struct sock *sk = sock->sk; 11311da177e4SLinus Torvalds struct hci_dev *hdev = NULL; 1132f4cdbb3fSMarcel Holtmann struct sk_buff *skb; 11330381101fSJohan Hedberg int len, err = 0; 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 11361da177e4SLinus Torvalds 11370381101fSJohan Hedberg if (!addr) 11380381101fSJohan Hedberg return -EINVAL; 11390381101fSJohan Hedberg 11400381101fSJohan Hedberg memset(&haddr, 0, sizeof(haddr)); 11410381101fSJohan Hedberg len = min_t(unsigned int, sizeof(haddr), addr_len); 11420381101fSJohan Hedberg memcpy(&haddr, addr, len); 11430381101fSJohan Hedberg 11440381101fSJohan Hedberg if (haddr.hci_family != AF_BLUETOOTH) 11450381101fSJohan Hedberg return -EINVAL; 11460381101fSJohan Hedberg 11471da177e4SLinus Torvalds lock_sock(sk); 11481da177e4SLinus Torvalds 1149e0448092STetsuo Handa /* Allow detaching from dead device and attaching to alive device, if 1150e0448092STetsuo Handa * the caller wants to re-bind (instead of close) this socket in 1151e0448092STetsuo Handa * response to hci_sock_dev_event(HCI_DEV_UNREG) notification. 1152e0448092STetsuo Handa */ 1153e0448092STetsuo Handa hdev = hci_pi(sk)->hdev; 1154e0448092STetsuo Handa if (hdev && hci_dev_test_flag(hdev, HCI_UNREGISTER)) { 1155e0448092STetsuo Handa hci_pi(sk)->hdev = NULL; 1156e0448092STetsuo Handa sk->sk_state = BT_OPEN; 1157e0448092STetsuo Handa hci_dev_put(hdev); 1158e0448092STetsuo Handa } 1159e0448092STetsuo Handa hdev = NULL; 1160e0448092STetsuo Handa 11617cc2ade2SMarcel Holtmann if (sk->sk_state == BT_BOUND) { 11627cc2ade2SMarcel Holtmann err = -EALREADY; 11637cc2ade2SMarcel Holtmann goto done; 11647cc2ade2SMarcel Holtmann } 11657cc2ade2SMarcel Holtmann 11667cc2ade2SMarcel Holtmann switch (haddr.hci_channel) { 11677cc2ade2SMarcel Holtmann case HCI_CHANNEL_RAW: 11687cc2ade2SMarcel Holtmann if (hci_pi(sk)->hdev) { 11691da177e4SLinus Torvalds err = -EALREADY; 11701da177e4SLinus Torvalds goto done; 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds 11730381101fSJohan Hedberg if (haddr.hci_dev != HCI_DEV_NONE) { 11740381101fSJohan Hedberg hdev = hci_dev_get(haddr.hci_dev); 117570f23020SAndrei Emeltchenko if (!hdev) { 11761da177e4SLinus Torvalds err = -ENODEV; 11771da177e4SLinus Torvalds goto done; 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds atomic_inc(&hdev->promisc); 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds 11835a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 1184f81f5b2dSMarcel Holtmann 1185f4cdbb3fSMarcel Holtmann if (!hci_sock_gen_cookie(sk)) { 1186f4cdbb3fSMarcel Holtmann /* In the case when a cookie has already been assigned, 1187f4cdbb3fSMarcel Holtmann * then there has been already an ioctl issued against 118891641b79SZheng Yongjun * an unbound socket and with that triggered an open 1189f4cdbb3fSMarcel Holtmann * notification. Send a close notification first to 1190f4cdbb3fSMarcel Holtmann * allow the state transition to bounded. 1191f81f5b2dSMarcel Holtmann */ 1192f4cdbb3fSMarcel Holtmann skb = create_monitor_ctrl_close(sk); 1193f4cdbb3fSMarcel Holtmann if (skb) { 1194f4cdbb3fSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1195f4cdbb3fSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1196f4cdbb3fSMarcel Holtmann kfree_skb(skb); 1197f4cdbb3fSMarcel Holtmann } 1198f4cdbb3fSMarcel Holtmann } 1199f81f5b2dSMarcel Holtmann 1200f81f5b2dSMarcel Holtmann if (capable(CAP_NET_ADMIN)) 1201f81f5b2dSMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 1202f81f5b2dSMarcel Holtmann 1203f4cdbb3fSMarcel Holtmann hci_pi(sk)->hdev = hdev; 1204f4cdbb3fSMarcel Holtmann 1205f81f5b2dSMarcel Holtmann /* Send event to monitor */ 1206f81f5b2dSMarcel Holtmann skb = create_monitor_ctrl_open(sk); 1207f81f5b2dSMarcel Holtmann if (skb) { 1208f81f5b2dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1209f81f5b2dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1210f81f5b2dSMarcel Holtmann kfree_skb(skb); 1211f81f5b2dSMarcel Holtmann } 12127cc2ade2SMarcel Holtmann break; 12137cc2ade2SMarcel Holtmann 121423500189SMarcel Holtmann case HCI_CHANNEL_USER: 121523500189SMarcel Holtmann if (hci_pi(sk)->hdev) { 121623500189SMarcel Holtmann err = -EALREADY; 121723500189SMarcel Holtmann goto done; 121823500189SMarcel Holtmann } 121923500189SMarcel Holtmann 122023500189SMarcel Holtmann if (haddr.hci_dev == HCI_DEV_NONE) { 122123500189SMarcel Holtmann err = -EINVAL; 122223500189SMarcel Holtmann goto done; 122323500189SMarcel Holtmann } 122423500189SMarcel Holtmann 122510a8b86fSMarcel Holtmann if (!capable(CAP_NET_ADMIN)) { 122623500189SMarcel Holtmann err = -EPERM; 122723500189SMarcel Holtmann goto done; 122823500189SMarcel Holtmann } 122923500189SMarcel Holtmann 123023500189SMarcel Holtmann hdev = hci_dev_get(haddr.hci_dev); 123123500189SMarcel Holtmann if (!hdev) { 123223500189SMarcel Holtmann err = -ENODEV; 123323500189SMarcel Holtmann goto done; 123423500189SMarcel Holtmann } 123523500189SMarcel Holtmann 1236781f899fSMarcel Holtmann if (test_bit(HCI_INIT, &hdev->flags) || 1237d7a5a11dSMarcel Holtmann hci_dev_test_flag(hdev, HCI_SETUP) || 1238781f899fSMarcel Holtmann hci_dev_test_flag(hdev, HCI_CONFIG) || 1239781f899fSMarcel Holtmann (!hci_dev_test_flag(hdev, HCI_AUTO_OFF) && 1240781f899fSMarcel Holtmann test_bit(HCI_UP, &hdev->flags))) { 124123500189SMarcel Holtmann err = -EBUSY; 124223500189SMarcel Holtmann hci_dev_put(hdev); 124323500189SMarcel Holtmann goto done; 124423500189SMarcel Holtmann } 124523500189SMarcel Holtmann 1246238be788SMarcel Holtmann if (hci_dev_test_and_set_flag(hdev, HCI_USER_CHANNEL)) { 124723500189SMarcel Holtmann err = -EUSERS; 124823500189SMarcel Holtmann hci_dev_put(hdev); 124923500189SMarcel Holtmann goto done; 125023500189SMarcel Holtmann } 125123500189SMarcel Holtmann 125223500189SMarcel Holtmann mgmt_index_removed(hdev); 125323500189SMarcel Holtmann 125423500189SMarcel Holtmann err = hci_dev_open(hdev->id); 125523500189SMarcel Holtmann if (err) { 1256781f899fSMarcel Holtmann if (err == -EALREADY) { 1257781f899fSMarcel Holtmann /* In case the transport is already up and 1258781f899fSMarcel Holtmann * running, clear the error here. 1259781f899fSMarcel Holtmann * 12609332ef9dSMasahiro Yamada * This can happen when opening a user 1261781f899fSMarcel Holtmann * channel and HCI_AUTO_OFF grace period 1262781f899fSMarcel Holtmann * is still active. 1263781f899fSMarcel Holtmann */ 1264781f899fSMarcel Holtmann err = 0; 1265781f899fSMarcel Holtmann } else { 1266a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_USER_CHANNEL); 1267c6521401SMarcel Holtmann mgmt_index_added(hdev); 126823500189SMarcel Holtmann hci_dev_put(hdev); 126923500189SMarcel Holtmann goto done; 127023500189SMarcel Holtmann } 1271781f899fSMarcel Holtmann } 127223500189SMarcel Holtmann 12735a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 1274aa1638ddSMarcel Holtmann 1275aa1638ddSMarcel Holtmann if (!hci_sock_gen_cookie(sk)) { 1276aa1638ddSMarcel Holtmann /* In the case when a cookie has already been assigned, 1277aa1638ddSMarcel Holtmann * this socket will transition from a raw socket into 12789332ef9dSMasahiro Yamada * a user channel socket. For a clean transition, send 1279aa1638ddSMarcel Holtmann * the close notification first. 1280aa1638ddSMarcel Holtmann */ 1281aa1638ddSMarcel Holtmann skb = create_monitor_ctrl_close(sk); 1282aa1638ddSMarcel Holtmann if (skb) { 1283aa1638ddSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1284aa1638ddSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1285aa1638ddSMarcel Holtmann kfree_skb(skb); 1286aa1638ddSMarcel Holtmann } 1287aa1638ddSMarcel Holtmann } 1288aa1638ddSMarcel Holtmann 1289aa1638ddSMarcel Holtmann /* The user channel is restricted to CAP_NET_ADMIN 1290aa1638ddSMarcel Holtmann * capabilities and with that implicitly trusted. 1291aa1638ddSMarcel Holtmann */ 1292aa1638ddSMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 1293aa1638ddSMarcel Holtmann 129423500189SMarcel Holtmann hci_pi(sk)->hdev = hdev; 12955a6d2cf5SMarcel Holtmann 1296aa1638ddSMarcel Holtmann /* Send event to monitor */ 1297aa1638ddSMarcel Holtmann skb = create_monitor_ctrl_open(sk); 1298aa1638ddSMarcel Holtmann if (skb) { 1299aa1638ddSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1300aa1638ddSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1301aa1638ddSMarcel Holtmann kfree_skb(skb); 1302aa1638ddSMarcel Holtmann } 1303aa1638ddSMarcel Holtmann 13045a6d2cf5SMarcel Holtmann atomic_inc(&hdev->promisc); 130523500189SMarcel Holtmann break; 130623500189SMarcel Holtmann 1307cd82e61cSMarcel Holtmann case HCI_CHANNEL_MONITOR: 1308cd82e61cSMarcel Holtmann if (haddr.hci_dev != HCI_DEV_NONE) { 1309cd82e61cSMarcel Holtmann err = -EINVAL; 1310cd82e61cSMarcel Holtmann goto done; 1311cd82e61cSMarcel Holtmann } 1312cd82e61cSMarcel Holtmann 1313cd82e61cSMarcel Holtmann if (!capable(CAP_NET_RAW)) { 1314cd82e61cSMarcel Holtmann err = -EPERM; 1315cd82e61cSMarcel Holtmann goto done; 1316cd82e61cSMarcel Holtmann } 1317cd82e61cSMarcel Holtmann 13185a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 13195a6d2cf5SMarcel Holtmann 132050ebc055SMarcel Holtmann /* The monitor interface is restricted to CAP_NET_RAW 132150ebc055SMarcel Holtmann * capabilities and with that implicitly trusted. 132250ebc055SMarcel Holtmann */ 132350ebc055SMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 132450ebc055SMarcel Holtmann 1325787b306cSJohannes Berg send_monitor_note(sk, "Linux version %s (%s)", 1326787b306cSJohannes Berg init_utsname()->release, 1327787b306cSJohannes Berg init_utsname()->machine); 13289e8305b3SMarcel Holtmann send_monitor_note(sk, "Bluetooth subsystem version %u.%u", 13299e8305b3SMarcel Holtmann BT_SUBSYS_VERSION, BT_SUBSYS_REVISION); 1330cd82e61cSMarcel Holtmann send_monitor_replay(sk); 1331249fa169SMarcel Holtmann send_monitor_control_replay(sk); 1332cd82e61cSMarcel Holtmann 1333cd82e61cSMarcel Holtmann atomic_inc(&monitor_promisc); 1334cd82e61cSMarcel Holtmann break; 1335cd82e61cSMarcel Holtmann 1336ac714949SMarcel Holtmann case HCI_CHANNEL_LOGGING: 1337ac714949SMarcel Holtmann if (haddr.hci_dev != HCI_DEV_NONE) { 1338ac714949SMarcel Holtmann err = -EINVAL; 1339ac714949SMarcel Holtmann goto done; 1340ac714949SMarcel Holtmann } 1341ac714949SMarcel Holtmann 1342ac714949SMarcel Holtmann if (!capable(CAP_NET_ADMIN)) { 1343ac714949SMarcel Holtmann err = -EPERM; 1344ac714949SMarcel Holtmann goto done; 1345ac714949SMarcel Holtmann } 13465a6d2cf5SMarcel Holtmann 13475a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 1348ac714949SMarcel Holtmann break; 1349ac714949SMarcel Holtmann 13507cc2ade2SMarcel Holtmann default: 1351801c1e8dSJohan Hedberg if (!hci_mgmt_chan_find(haddr.hci_channel)) { 13527cc2ade2SMarcel Holtmann err = -EINVAL; 13537cc2ade2SMarcel Holtmann goto done; 13547cc2ade2SMarcel Holtmann } 13557cc2ade2SMarcel Holtmann 1356801c1e8dSJohan Hedberg if (haddr.hci_dev != HCI_DEV_NONE) { 1357801c1e8dSJohan Hedberg err = -EINVAL; 1358801c1e8dSJohan Hedberg goto done; 1359801c1e8dSJohan Hedberg } 1360801c1e8dSJohan Hedberg 13611195fbb8SMarcel Holtmann /* Users with CAP_NET_ADMIN capabilities are allowed 13621195fbb8SMarcel Holtmann * access to all management commands and events. For 13631195fbb8SMarcel Holtmann * untrusted users the interface is restricted and 13641195fbb8SMarcel Holtmann * also only untrusted events are sent. 136550ebc055SMarcel Holtmann */ 13661195fbb8SMarcel Holtmann if (capable(CAP_NET_ADMIN)) 136750ebc055SMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 136850ebc055SMarcel Holtmann 13695a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 13705a6d2cf5SMarcel Holtmann 1371f9207338SMarcel Holtmann /* At the moment the index and unconfigured index events 1372f9207338SMarcel Holtmann * are enabled unconditionally. Setting them on each 1373f9207338SMarcel Holtmann * socket when binding keeps this functionality. They 1374f9207338SMarcel Holtmann * however might be cleared later and then sending of these 1375f9207338SMarcel Holtmann * events will be disabled, but that is then intentional. 1376f6b7712eSMarcel Holtmann * 1377f6b7712eSMarcel Holtmann * This also enables generic events that are safe to be 1378f6b7712eSMarcel Holtmann * received by untrusted users. Example for such events 1379f6b7712eSMarcel Holtmann * are changes to settings, class of device, name etc. 1380f9207338SMarcel Holtmann */ 13815a6d2cf5SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_CONTROL) { 1382f4cdbb3fSMarcel Holtmann if (!hci_sock_gen_cookie(sk)) { 1383f4cdbb3fSMarcel Holtmann /* In the case when a cookie has already been 138491641b79SZheng Yongjun * assigned, this socket will transition from 1385f4cdbb3fSMarcel Holtmann * a raw socket into a control socket. To 138691641b79SZheng Yongjun * allow for a clean transition, send the 1387f4cdbb3fSMarcel Holtmann * close notification first. 1388f4cdbb3fSMarcel Holtmann */ 1389f4cdbb3fSMarcel Holtmann skb = create_monitor_ctrl_close(sk); 1390f4cdbb3fSMarcel Holtmann if (skb) { 1391f4cdbb3fSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1392f4cdbb3fSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1393f4cdbb3fSMarcel Holtmann kfree_skb(skb); 1394f4cdbb3fSMarcel Holtmann } 1395f4cdbb3fSMarcel Holtmann } 139670ecce91SMarcel Holtmann 1397249fa169SMarcel Holtmann /* Send event to monitor */ 1398249fa169SMarcel Holtmann skb = create_monitor_ctrl_open(sk); 1399249fa169SMarcel Holtmann if (skb) { 1400249fa169SMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1401249fa169SMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1402249fa169SMarcel Holtmann kfree_skb(skb); 1403249fa169SMarcel Holtmann } 1404249fa169SMarcel Holtmann 1405f9207338SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_INDEX_EVENTS); 1406f9207338SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); 14075504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_OPTION_EVENTS); 14085504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_SETTING_EVENTS); 14095504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS); 14105504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS); 1411f9207338SMarcel Holtmann } 1412801c1e8dSJohan Hedberg break; 1413801c1e8dSJohan Hedberg } 1414801c1e8dSJohan Hedberg 141509572fcaSLuiz Augusto von Dentz /* Default MTU to HCI_MAX_FRAME_SIZE if not set */ 141609572fcaSLuiz Augusto von Dentz if (!hci_pi(sk)->mtu) 141709572fcaSLuiz Augusto von Dentz hci_pi(sk)->mtu = HCI_MAX_FRAME_SIZE; 141809572fcaSLuiz Augusto von Dentz 14191da177e4SLinus Torvalds sk->sk_state = BT_BOUND; 14201da177e4SLinus Torvalds 14211da177e4SLinus Torvalds done: 14221da177e4SLinus Torvalds release_sock(sk); 14231da177e4SLinus Torvalds return err; 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds 14268fc9ced3SGustavo Padovan static int hci_sock_getname(struct socket *sock, struct sockaddr *addr, 14279b2c45d4SDenys Vlasenko int peer) 14281da177e4SLinus Torvalds { 14291da177e4SLinus Torvalds struct sockaddr_hci *haddr = (struct sockaddr_hci *)addr; 14301da177e4SLinus Torvalds struct sock *sk = sock->sk; 14319d4b68b2SMarcel Holtmann struct hci_dev *hdev; 14329d4b68b2SMarcel Holtmann int err = 0; 14331da177e4SLinus Torvalds 14341da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 14351da177e4SLinus Torvalds 143606f43cbcSMarcel Holtmann if (peer) 143706f43cbcSMarcel Holtmann return -EOPNOTSUPP; 143806f43cbcSMarcel Holtmann 14391da177e4SLinus Torvalds lock_sock(sk); 14401da177e4SLinus Torvalds 1441e0448092STetsuo Handa hdev = hci_hdev_from_sock(sk); 1442e0448092STetsuo Handa if (IS_ERR(hdev)) { 1443e0448092STetsuo Handa err = PTR_ERR(hdev); 14449d4b68b2SMarcel Holtmann goto done; 14459d4b68b2SMarcel Holtmann } 14469d4b68b2SMarcel Holtmann 14471da177e4SLinus Torvalds haddr->hci_family = AF_BLUETOOTH; 14487b005bd3SMarcel Holtmann haddr->hci_dev = hdev->id; 14499d4b68b2SMarcel Holtmann haddr->hci_channel= hci_pi(sk)->channel; 14509b2c45d4SDenys Vlasenko err = sizeof(*haddr); 14511da177e4SLinus Torvalds 14529d4b68b2SMarcel Holtmann done: 14531da177e4SLinus Torvalds release_sock(sk); 14549d4b68b2SMarcel Holtmann return err; 14551da177e4SLinus Torvalds } 14561da177e4SLinus Torvalds 14576039aa73SGustavo Padovan static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg, 14586039aa73SGustavo Padovan struct sk_buff *skb) 14591da177e4SLinus Torvalds { 146032929e1fSAlain Michaud __u8 mask = hci_pi(sk)->cmsg_mask; 14611da177e4SLinus Torvalds 14620d48d939SMarcel Holtmann if (mask & HCI_CMSG_DIR) { 14630d48d939SMarcel Holtmann int incoming = bt_cb(skb)->incoming; 14648fc9ced3SGustavo Padovan put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming), 14658fc9ced3SGustavo Padovan &incoming); 14660d48d939SMarcel Holtmann } 14671da177e4SLinus Torvalds 1468a61bbcf2SPatrick McHardy if (mask & HCI_CMSG_TSTAMP) { 1469f6e623a6SJohann Felix Soden #ifdef CONFIG_COMPAT 147013c6ee2aSDeepa Dinamani struct old_timeval32 ctv; 1471f6e623a6SJohann Felix Soden #endif 147213c6ee2aSDeepa Dinamani struct __kernel_old_timeval tv; 1473767c5eb5SMarcel Holtmann void *data; 1474767c5eb5SMarcel Holtmann int len; 1475a61bbcf2SPatrick McHardy 1476a61bbcf2SPatrick McHardy skb_get_timestamp(skb, &tv); 1477767c5eb5SMarcel Holtmann 14781da97f83SDavid S. Miller data = &tv; 14791da97f83SDavid S. Miller len = sizeof(tv); 14801da97f83SDavid S. Miller #ifdef CONFIG_COMPAT 1481da88cea1SH. J. Lu if (!COMPAT_USE_64BIT_TIME && 1482da88cea1SH. J. Lu (msg->msg_flags & MSG_CMSG_COMPAT)) { 1483767c5eb5SMarcel Holtmann ctv.tv_sec = tv.tv_sec; 1484767c5eb5SMarcel Holtmann ctv.tv_usec = tv.tv_usec; 1485767c5eb5SMarcel Holtmann data = &ctv; 1486767c5eb5SMarcel Holtmann len = sizeof(ctv); 1487767c5eb5SMarcel Holtmann } 14881da97f83SDavid S. Miller #endif 1489767c5eb5SMarcel Holtmann 1490767c5eb5SMarcel Holtmann put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data); 1491a61bbcf2SPatrick McHardy } 14921da177e4SLinus Torvalds } 14931da177e4SLinus Torvalds 14948528d3f7SMarcel Holtmann static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg, 14958528d3f7SMarcel Holtmann size_t len, int flags) 14961da177e4SLinus Torvalds { 14971da177e4SLinus Torvalds struct sock *sk = sock->sk; 14981da177e4SLinus Torvalds struct sk_buff *skb; 14991da177e4SLinus Torvalds int copied, err; 150083871f8cSDenis Kenzior unsigned int skblen; 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds BT_DBG("sock %p, sk %p", sock, sk); 15031da177e4SLinus Torvalds 1504d94a6104SMarcel Holtmann if (flags & MSG_OOB) 15051da177e4SLinus Torvalds return -EOPNOTSUPP; 15061da177e4SLinus Torvalds 1507ac714949SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_LOGGING) 1508ac714949SMarcel Holtmann return -EOPNOTSUPP; 1509ac714949SMarcel Holtmann 15101da177e4SLinus Torvalds if (sk->sk_state == BT_CLOSED) 15111da177e4SLinus Torvalds return 0; 15121da177e4SLinus Torvalds 1513f4b41f06SOliver Hartkopp skb = skb_recv_datagram(sk, flags, &err); 151470f23020SAndrei Emeltchenko if (!skb) 15151da177e4SLinus Torvalds return err; 15161da177e4SLinus Torvalds 151783871f8cSDenis Kenzior skblen = skb->len; 15181da177e4SLinus Torvalds copied = skb->len; 15191da177e4SLinus Torvalds if (len < copied) { 15201da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC; 15211da177e4SLinus Torvalds copied = len; 15221da177e4SLinus Torvalds } 15231da177e4SLinus Torvalds 1524badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 152551f3d02bSDavid S. Miller err = skb_copy_datagram_msg(skb, 0, msg, copied); 15261da177e4SLinus Torvalds 15273a208627SMarcel Holtmann switch (hci_pi(sk)->channel) { 15283a208627SMarcel Holtmann case HCI_CHANNEL_RAW: 15291da177e4SLinus Torvalds hci_sock_cmsg(sk, msg, skb); 15303a208627SMarcel Holtmann break; 153123500189SMarcel Holtmann case HCI_CHANNEL_USER: 1532cd82e61cSMarcel Holtmann case HCI_CHANNEL_MONITOR: 1533cd82e61cSMarcel Holtmann sock_recv_timestamp(msg, sk, skb); 1534cd82e61cSMarcel Holtmann break; 1535801c1e8dSJohan Hedberg default: 1536801c1e8dSJohan Hedberg if (hci_mgmt_chan_find(hci_pi(sk)->channel)) 1537801c1e8dSJohan Hedberg sock_recv_timestamp(msg, sk, skb); 1538801c1e8dSJohan Hedberg break; 15393a208627SMarcel Holtmann } 15401da177e4SLinus Torvalds 15411da177e4SLinus Torvalds skb_free_datagram(sk, skb); 15421da177e4SLinus Torvalds 15434f34228bSLuiz Augusto von Dentz if (flags & MSG_TRUNC) 154483871f8cSDenis Kenzior copied = skblen; 154583871f8cSDenis Kenzior 15461da177e4SLinus Torvalds return err ? : copied; 15471da177e4SLinus Torvalds } 15481da177e4SLinus Torvalds 154964ba2eb3SLuiz Augusto von Dentz static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk, 155064ba2eb3SLuiz Augusto von Dentz struct sk_buff *skb) 1551fa4335d7SJohan Hedberg { 1552fa4335d7SJohan Hedberg u8 *cp; 1553fa4335d7SJohan Hedberg struct mgmt_hdr *hdr; 1554fa4335d7SJohan Hedberg u16 opcode, index, len; 1555fa4335d7SJohan Hedberg struct hci_dev *hdev = NULL; 1556fa4335d7SJohan Hedberg const struct hci_mgmt_handler *handler; 1557fa4335d7SJohan Hedberg bool var_len, no_hdev; 1558fa4335d7SJohan Hedberg int err; 1559fa4335d7SJohan Hedberg 156064ba2eb3SLuiz Augusto von Dentz BT_DBG("got %d bytes", skb->len); 1561fa4335d7SJohan Hedberg 156264ba2eb3SLuiz Augusto von Dentz if (skb->len < sizeof(*hdr)) 1563fa4335d7SJohan Hedberg return -EINVAL; 1564fa4335d7SJohan Hedberg 156564ba2eb3SLuiz Augusto von Dentz hdr = (void *)skb->data; 1566fa4335d7SJohan Hedberg opcode = __le16_to_cpu(hdr->opcode); 1567fa4335d7SJohan Hedberg index = __le16_to_cpu(hdr->index); 1568fa4335d7SJohan Hedberg len = __le16_to_cpu(hdr->len); 1569fa4335d7SJohan Hedberg 157064ba2eb3SLuiz Augusto von Dentz if (len != skb->len - sizeof(*hdr)) { 1571fa4335d7SJohan Hedberg err = -EINVAL; 1572fa4335d7SJohan Hedberg goto done; 1573fa4335d7SJohan Hedberg } 1574fa4335d7SJohan Hedberg 157538ceaa00SMarcel Holtmann if (chan->channel == HCI_CHANNEL_CONTROL) { 157664ba2eb3SLuiz Augusto von Dentz struct sk_buff *cmd; 157738ceaa00SMarcel Holtmann 157838ceaa00SMarcel Holtmann /* Send event to monitor */ 157964ba2eb3SLuiz Augusto von Dentz cmd = create_monitor_ctrl_command(sk, index, opcode, len, 158064ba2eb3SLuiz Augusto von Dentz skb->data + sizeof(*hdr)); 158164ba2eb3SLuiz Augusto von Dentz if (cmd) { 158264ba2eb3SLuiz Augusto von Dentz hci_send_to_channel(HCI_CHANNEL_MONITOR, cmd, 158338ceaa00SMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 158464ba2eb3SLuiz Augusto von Dentz kfree_skb(cmd); 158538ceaa00SMarcel Holtmann } 158638ceaa00SMarcel Holtmann } 158738ceaa00SMarcel Holtmann 1588fa4335d7SJohan Hedberg if (opcode >= chan->handler_count || 1589fa4335d7SJohan Hedberg chan->handlers[opcode].func == NULL) { 1590fa4335d7SJohan Hedberg BT_DBG("Unknown op %u", opcode); 1591fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1592fa4335d7SJohan Hedberg MGMT_STATUS_UNKNOWN_COMMAND); 1593fa4335d7SJohan Hedberg goto done; 1594fa4335d7SJohan Hedberg } 1595fa4335d7SJohan Hedberg 1596fa4335d7SJohan Hedberg handler = &chan->handlers[opcode]; 1597fa4335d7SJohan Hedberg 1598fa4335d7SJohan Hedberg if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) && 1599fa4335d7SJohan Hedberg !(handler->flags & HCI_MGMT_UNTRUSTED)) { 1600fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1601fa4335d7SJohan Hedberg MGMT_STATUS_PERMISSION_DENIED); 1602fa4335d7SJohan Hedberg goto done; 1603fa4335d7SJohan Hedberg } 1604fa4335d7SJohan Hedberg 1605fa4335d7SJohan Hedberg if (index != MGMT_INDEX_NONE) { 1606fa4335d7SJohan Hedberg hdev = hci_dev_get(index); 1607fa4335d7SJohan Hedberg if (!hdev) { 1608fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1609fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1610fa4335d7SJohan Hedberg goto done; 1611fa4335d7SJohan Hedberg } 1612fa4335d7SJohan Hedberg 1613fa4335d7SJohan Hedberg if (hci_dev_test_flag(hdev, HCI_SETUP) || 1614fa4335d7SJohan Hedberg hci_dev_test_flag(hdev, HCI_CONFIG) || 1615fa4335d7SJohan Hedberg hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 1616fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1617fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1618fa4335d7SJohan Hedberg goto done; 1619fa4335d7SJohan Hedberg } 1620fa4335d7SJohan Hedberg 1621fa4335d7SJohan Hedberg if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && 1622fa4335d7SJohan Hedberg !(handler->flags & HCI_MGMT_UNCONFIGURED)) { 1623fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1624fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1625fa4335d7SJohan Hedberg goto done; 1626fa4335d7SJohan Hedberg } 1627fa4335d7SJohan Hedberg } 1628fa4335d7SJohan Hedberg 1629d5cc6626SMarcel Holtmann if (!(handler->flags & HCI_MGMT_HDEV_OPTIONAL)) { 1630fa4335d7SJohan Hedberg no_hdev = (handler->flags & HCI_MGMT_NO_HDEV); 1631fa4335d7SJohan Hedberg if (no_hdev != !hdev) { 1632fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1633fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1634fa4335d7SJohan Hedberg goto done; 1635fa4335d7SJohan Hedberg } 1636d5cc6626SMarcel Holtmann } 1637fa4335d7SJohan Hedberg 1638fa4335d7SJohan Hedberg var_len = (handler->flags & HCI_MGMT_VAR_LEN); 1639fa4335d7SJohan Hedberg if ((var_len && len < handler->data_len) || 1640fa4335d7SJohan Hedberg (!var_len && len != handler->data_len)) { 1641fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1642fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_PARAMS); 1643fa4335d7SJohan Hedberg goto done; 1644fa4335d7SJohan Hedberg } 1645fa4335d7SJohan Hedberg 1646fa4335d7SJohan Hedberg if (hdev && chan->hdev_init) 1647fa4335d7SJohan Hedberg chan->hdev_init(sk, hdev); 1648fa4335d7SJohan Hedberg 164964ba2eb3SLuiz Augusto von Dentz cp = skb->data + sizeof(*hdr); 1650fa4335d7SJohan Hedberg 1651fa4335d7SJohan Hedberg err = handler->func(sk, hdev, cp, len); 1652fa4335d7SJohan Hedberg if (err < 0) 1653fa4335d7SJohan Hedberg goto done; 1654fa4335d7SJohan Hedberg 165564ba2eb3SLuiz Augusto von Dentz err = skb->len; 1656fa4335d7SJohan Hedberg 1657fa4335d7SJohan Hedberg done: 1658fa4335d7SJohan Hedberg if (hdev) 1659fa4335d7SJohan Hedberg hci_dev_put(hdev); 1660fa4335d7SJohan Hedberg 1661fa4335d7SJohan Hedberg return err; 1662fa4335d7SJohan Hedberg } 1663fa4335d7SJohan Hedberg 166464ba2eb3SLuiz Augusto von Dentz static int hci_logging_frame(struct sock *sk, struct sk_buff *skb, 166564ba2eb3SLuiz Augusto von Dentz unsigned int flags) 1666ac714949SMarcel Holtmann { 1667ac714949SMarcel Holtmann struct hci_mon_hdr *hdr; 1668ac714949SMarcel Holtmann struct hci_dev *hdev; 1669ac714949SMarcel Holtmann u16 index; 1670ac714949SMarcel Holtmann int err; 1671ac714949SMarcel Holtmann 1672ac714949SMarcel Holtmann /* The logging frame consists at minimum of the standard header, 1673ac714949SMarcel Holtmann * the priority byte, the ident length byte and at least one string 1674ac714949SMarcel Holtmann * terminator NUL byte. Anything shorter are invalid packets. 1675ac714949SMarcel Holtmann */ 167664ba2eb3SLuiz Augusto von Dentz if (skb->len < sizeof(*hdr) + 3) 1677ac714949SMarcel Holtmann return -EINVAL; 1678ac714949SMarcel Holtmann 1679ac714949SMarcel Holtmann hdr = (void *)skb->data; 1680ac714949SMarcel Holtmann 168164ba2eb3SLuiz Augusto von Dentz if (__le16_to_cpu(hdr->len) != skb->len - sizeof(*hdr)) 168264ba2eb3SLuiz Augusto von Dentz return -EINVAL; 1683ac714949SMarcel Holtmann 1684ac714949SMarcel Holtmann if (__le16_to_cpu(hdr->opcode) == 0x0000) { 1685ac714949SMarcel Holtmann __u8 priority = skb->data[sizeof(*hdr)]; 1686ac714949SMarcel Holtmann __u8 ident_len = skb->data[sizeof(*hdr) + 1]; 1687ac714949SMarcel Holtmann 1688ac714949SMarcel Holtmann /* Only the priorities 0-7 are valid and with that any other 1689ac714949SMarcel Holtmann * value results in an invalid packet. 1690ac714949SMarcel Holtmann * 1691ac714949SMarcel Holtmann * The priority byte is followed by an ident length byte and 1692ac714949SMarcel Holtmann * the NUL terminated ident string. Check that the ident 1693ac714949SMarcel Holtmann * length is not overflowing the packet and also that the 1694ac714949SMarcel Holtmann * ident string itself is NUL terminated. In case the ident 1695ac714949SMarcel Holtmann * length is zero, the length value actually doubles as NUL 1696ac714949SMarcel Holtmann * terminator identifier. 1697ac714949SMarcel Holtmann * 1698ac714949SMarcel Holtmann * The message follows the ident string (if present) and 1699ac714949SMarcel Holtmann * must be NUL terminated. Otherwise it is not a valid packet. 1700ac714949SMarcel Holtmann */ 170164ba2eb3SLuiz Augusto von Dentz if (priority > 7 || skb->data[skb->len - 1] != 0x00 || 170264ba2eb3SLuiz Augusto von Dentz ident_len > skb->len - sizeof(*hdr) - 3 || 170364ba2eb3SLuiz Augusto von Dentz skb->data[sizeof(*hdr) + ident_len + 1] != 0x00) 170464ba2eb3SLuiz Augusto von Dentz return -EINVAL; 1705ac714949SMarcel Holtmann } else { 170664ba2eb3SLuiz Augusto von Dentz return -EINVAL; 1707ac714949SMarcel Holtmann } 1708ac714949SMarcel Holtmann 1709ac714949SMarcel Holtmann index = __le16_to_cpu(hdr->index); 1710ac714949SMarcel Holtmann 1711ac714949SMarcel Holtmann if (index != MGMT_INDEX_NONE) { 1712ac714949SMarcel Holtmann hdev = hci_dev_get(index); 171364ba2eb3SLuiz Augusto von Dentz if (!hdev) 171464ba2eb3SLuiz Augusto von Dentz return -ENODEV; 1715ac714949SMarcel Holtmann } else { 1716ac714949SMarcel Holtmann hdev = NULL; 1717ac714949SMarcel Holtmann } 1718ac714949SMarcel Holtmann 1719ac714949SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_USER_LOGGING); 1720ac714949SMarcel Holtmann 1721ac714949SMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, HCI_SOCK_TRUSTED, NULL); 172264ba2eb3SLuiz Augusto von Dentz err = skb->len; 1723ac714949SMarcel Holtmann 1724ac714949SMarcel Holtmann if (hdev) 1725ac714949SMarcel Holtmann hci_dev_put(hdev); 1726ac714949SMarcel Holtmann 1727ac714949SMarcel Holtmann return err; 1728ac714949SMarcel Holtmann } 1729ac714949SMarcel Holtmann 17301b784140SYing Xue static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg, 17311b784140SYing Xue size_t len) 17321da177e4SLinus Torvalds { 17331da177e4SLinus Torvalds struct sock *sk = sock->sk; 1734801c1e8dSJohan Hedberg struct hci_mgmt_chan *chan; 17351da177e4SLinus Torvalds struct hci_dev *hdev; 17361da177e4SLinus Torvalds struct sk_buff *skb; 17371da177e4SLinus Torvalds int err; 17380b59e272STetsuo Handa const unsigned int flags = msg->msg_flags; 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 17411da177e4SLinus Torvalds 17420b59e272STetsuo Handa if (flags & MSG_OOB) 17431da177e4SLinus Torvalds return -EOPNOTSUPP; 17441da177e4SLinus Torvalds 17450b59e272STetsuo Handa if (flags & ~(MSG_DONTWAIT | MSG_NOSIGNAL | MSG_ERRQUEUE | MSG_CMSG_COMPAT)) 17461da177e4SLinus Torvalds return -EINVAL; 17471da177e4SLinus Torvalds 174809572fcaSLuiz Augusto von Dentz if (len < 4 || len > hci_pi(sk)->mtu) 17491da177e4SLinus Torvalds return -EINVAL; 17501da177e4SLinus Torvalds 175164ba2eb3SLuiz Augusto von Dentz skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0); 175264ba2eb3SLuiz Augusto von Dentz if (IS_ERR(skb)) 175364ba2eb3SLuiz Augusto von Dentz return PTR_ERR(skb); 17540b59e272STetsuo Handa 17551da177e4SLinus Torvalds lock_sock(sk); 17561da177e4SLinus Torvalds 17570381101fSJohan Hedberg switch (hci_pi(sk)->channel) { 17580381101fSJohan Hedberg case HCI_CHANNEL_RAW: 175923500189SMarcel Holtmann case HCI_CHANNEL_USER: 17600381101fSJohan Hedberg break; 1761cd82e61cSMarcel Holtmann case HCI_CHANNEL_MONITOR: 1762cd82e61cSMarcel Holtmann err = -EOPNOTSUPP; 176364ba2eb3SLuiz Augusto von Dentz goto drop; 1764ac714949SMarcel Holtmann case HCI_CHANNEL_LOGGING: 176564ba2eb3SLuiz Augusto von Dentz err = hci_logging_frame(sk, skb, flags); 176664ba2eb3SLuiz Augusto von Dentz goto drop; 17670381101fSJohan Hedberg default: 1768801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock); 1769801c1e8dSJohan Hedberg chan = __hci_mgmt_chan_find(hci_pi(sk)->channel); 1770801c1e8dSJohan Hedberg if (chan) 177164ba2eb3SLuiz Augusto von Dentz err = hci_mgmt_cmd(chan, sk, skb); 1772801c1e8dSJohan Hedberg else 17730381101fSJohan Hedberg err = -EINVAL; 1774801c1e8dSJohan Hedberg 1775801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 177664ba2eb3SLuiz Augusto von Dentz goto drop; 17770381101fSJohan Hedberg } 17780381101fSJohan Hedberg 1779e0448092STetsuo Handa hdev = hci_hdev_from_sock(sk); 1780e0448092STetsuo Handa if (IS_ERR(hdev)) { 1781e0448092STetsuo Handa err = PTR_ERR(hdev); 178264ba2eb3SLuiz Augusto von Dentz goto drop; 17831da177e4SLinus Torvalds } 17841da177e4SLinus Torvalds 17857e21addcSMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags)) { 17867e21addcSMarcel Holtmann err = -ENETDOWN; 178764ba2eb3SLuiz Augusto von Dentz goto drop; 17887e21addcSMarcel Holtmann } 17897e21addcSMarcel Holtmann 17908528d3f7SMarcel Holtmann hci_skb_pkt_type(skb) = skb->data[0]; 17911da177e4SLinus Torvalds skb_pull(skb, 1); 17921da177e4SLinus Torvalds 17931bc5ad16SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_USER) { 17941bc5ad16SMarcel Holtmann /* No permission check is needed for user channel 17951bc5ad16SMarcel Holtmann * since that gets enforced when binding the socket. 17961bc5ad16SMarcel Holtmann * 17971bc5ad16SMarcel Holtmann * However check that the packet type is valid. 17981bc5ad16SMarcel Holtmann */ 1799d79f34e3SMarcel Holtmann if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT && 1800d79f34e3SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT && 1801cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_SCODATA_PKT && 1802cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) { 18031bc5ad16SMarcel Holtmann err = -EINVAL; 18041bc5ad16SMarcel Holtmann goto drop; 18051bc5ad16SMarcel Holtmann } 18061bc5ad16SMarcel Holtmann 18071bc5ad16SMarcel Holtmann skb_queue_tail(&hdev->raw_q, skb); 18081bc5ad16SMarcel Holtmann queue_work(hdev->workqueue, &hdev->tx_work); 1809d79f34e3SMarcel Holtmann } else if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) { 181083985319SHarvey Harrison u16 opcode = get_unaligned_le16(skb->data); 18111da177e4SLinus Torvalds u16 ogf = hci_opcode_ogf(opcode); 18121da177e4SLinus Torvalds u16 ocf = hci_opcode_ocf(opcode); 18131da177e4SLinus Torvalds 18141da177e4SLinus Torvalds if (((ogf > HCI_SFLT_MAX_OGF) || 18153bb3c755SGustavo Padovan !hci_test_bit(ocf & HCI_FLT_OCF_BITS, 18163bb3c755SGustavo Padovan &hci_sec_filter.ocf_mask[ogf])) && 18171da177e4SLinus Torvalds !capable(CAP_NET_RAW)) { 18181da177e4SLinus Torvalds err = -EPERM; 18191da177e4SLinus Torvalds goto drop; 18201da177e4SLinus Torvalds } 18211da177e4SLinus Torvalds 18221982162bSMarcel Holtmann /* Since the opcode has already been extracted here, store 18231982162bSMarcel Holtmann * a copy of the value for later use by the drivers. 18241982162bSMarcel Holtmann */ 18251982162bSMarcel Holtmann hci_skb_opcode(skb) = opcode; 18261982162bSMarcel Holtmann 1827fee746b0SMarcel Holtmann if (ogf == 0x3f) { 18281da177e4SLinus Torvalds skb_queue_tail(&hdev->raw_q, skb); 18293eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work); 18301da177e4SLinus Torvalds } else { 183149c922bbSStephen Hemminger /* Stand-alone HCI commands must be flagged as 183211714b3dSJohan Hedberg * single-command requests. 183311714b3dSJohan Hedberg */ 183444d27137SJohan Hedberg bt_cb(skb)->hci.req_flags |= HCI_REQ_START; 183511714b3dSJohan Hedberg 18361da177e4SLinus Torvalds skb_queue_tail(&hdev->cmd_q, skb); 1837c347b765SGustavo F. Padovan queue_work(hdev->workqueue, &hdev->cmd_work); 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds } else { 18401da177e4SLinus Torvalds if (!capable(CAP_NET_RAW)) { 18411da177e4SLinus Torvalds err = -EPERM; 18421da177e4SLinus Torvalds goto drop; 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds 1845d79f34e3SMarcel Holtmann if (hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT && 1846cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_SCODATA_PKT && 1847cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) { 1848bb77543eSMarcel Holtmann err = -EINVAL; 1849bb77543eSMarcel Holtmann goto drop; 1850bb77543eSMarcel Holtmann } 1851bb77543eSMarcel Holtmann 18521da177e4SLinus Torvalds skb_queue_tail(&hdev->raw_q, skb); 18533eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work); 18541da177e4SLinus Torvalds } 18551da177e4SLinus Torvalds 18561da177e4SLinus Torvalds err = len; 18571da177e4SLinus Torvalds 18581da177e4SLinus Torvalds done: 18591da177e4SLinus Torvalds release_sock(sk); 18601da177e4SLinus Torvalds return err; 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds drop: 18631da177e4SLinus Torvalds kfree_skb(skb); 18641da177e4SLinus Torvalds goto done; 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds 186709572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt_old(struct socket *sock, int level, int optname, 1868a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int len) 18691da177e4SLinus Torvalds { 18701da177e4SLinus Torvalds struct hci_ufilter uf = { .opcode = 0 }; 18711da177e4SLinus Torvalds struct sock *sk = sock->sk; 18721da177e4SLinus Torvalds int err = 0, opt = 0; 18731da177e4SLinus Torvalds 18741da177e4SLinus Torvalds BT_DBG("sk %p, opt %d", sk, optname); 18751da177e4SLinus Torvalds 18761da177e4SLinus Torvalds lock_sock(sk); 18771da177e4SLinus Torvalds 18782f39cdb7SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 1879c2371e80SMarcel Holtmann err = -EBADFD; 18802f39cdb7SMarcel Holtmann goto done; 18812f39cdb7SMarcel Holtmann } 18822f39cdb7SMarcel Holtmann 18831da177e4SLinus Torvalds switch (optname) { 18841da177e4SLinus Torvalds case HCI_DATA_DIR: 1885a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&opt, optval, sizeof(opt))) { 18861da177e4SLinus Torvalds err = -EFAULT; 18871da177e4SLinus Torvalds break; 18881da177e4SLinus Torvalds } 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds if (opt) 18911da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR; 18921da177e4SLinus Torvalds else 18931da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR; 18941da177e4SLinus Torvalds break; 18951da177e4SLinus Torvalds 18961da177e4SLinus Torvalds case HCI_TIME_STAMP: 1897a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&opt, optval, sizeof(opt))) { 18981da177e4SLinus Torvalds err = -EFAULT; 18991da177e4SLinus Torvalds break; 19001da177e4SLinus Torvalds } 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds if (opt) 19031da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP; 19041da177e4SLinus Torvalds else 19051da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP; 19061da177e4SLinus Torvalds break; 19071da177e4SLinus Torvalds 19081da177e4SLinus Torvalds case HCI_FILTER: 19090878b666SMarcel Holtmann { 19100878b666SMarcel Holtmann struct hci_filter *f = &hci_pi(sk)->filter; 19110878b666SMarcel Holtmann 19120878b666SMarcel Holtmann uf.type_mask = f->type_mask; 19130878b666SMarcel Holtmann uf.opcode = f->opcode; 19140878b666SMarcel Holtmann uf.event_mask[0] = *((u32 *) f->event_mask + 0); 19150878b666SMarcel Holtmann uf.event_mask[1] = *((u32 *) f->event_mask + 1); 19160878b666SMarcel Holtmann } 19170878b666SMarcel Holtmann 19181da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(uf)); 1919a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&uf, optval, len)) { 19201da177e4SLinus Torvalds err = -EFAULT; 19211da177e4SLinus Torvalds break; 19221da177e4SLinus Torvalds } 19231da177e4SLinus Torvalds 19241da177e4SLinus Torvalds if (!capable(CAP_NET_RAW)) { 19251da177e4SLinus Torvalds uf.type_mask &= hci_sec_filter.type_mask; 19261da177e4SLinus Torvalds uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0); 19271da177e4SLinus Torvalds uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1); 19281da177e4SLinus Torvalds } 19291da177e4SLinus Torvalds 19301da177e4SLinus Torvalds { 19311da177e4SLinus Torvalds struct hci_filter *f = &hci_pi(sk)->filter; 19321da177e4SLinus Torvalds 19331da177e4SLinus Torvalds f->type_mask = uf.type_mask; 19341da177e4SLinus Torvalds f->opcode = uf.opcode; 19351da177e4SLinus Torvalds *((u32 *) f->event_mask + 0) = uf.event_mask[0]; 19361da177e4SLinus Torvalds *((u32 *) f->event_mask + 1) = uf.event_mask[1]; 19371da177e4SLinus Torvalds } 19381da177e4SLinus Torvalds break; 19391da177e4SLinus Torvalds 19401da177e4SLinus Torvalds default: 19411da177e4SLinus Torvalds err = -ENOPROTOOPT; 19421da177e4SLinus Torvalds break; 19431da177e4SLinus Torvalds } 19441da177e4SLinus Torvalds 19452f39cdb7SMarcel Holtmann done: 19461da177e4SLinus Torvalds release_sock(sk); 19471da177e4SLinus Torvalds return err; 19481da177e4SLinus Torvalds } 19491da177e4SLinus Torvalds 195009572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt(struct socket *sock, int level, int optname, 195109572fcaSLuiz Augusto von Dentz sockptr_t optval, unsigned int len) 195209572fcaSLuiz Augusto von Dentz { 195309572fcaSLuiz Augusto von Dentz struct sock *sk = sock->sk; 1954b9f9dbadSDan Carpenter int err = 0; 1955b9f9dbadSDan Carpenter u16 opt; 195609572fcaSLuiz Augusto von Dentz 195709572fcaSLuiz Augusto von Dentz BT_DBG("sk %p, opt %d", sk, optname); 195809572fcaSLuiz Augusto von Dentz 195909572fcaSLuiz Augusto von Dentz if (level == SOL_HCI) 196009572fcaSLuiz Augusto von Dentz return hci_sock_setsockopt_old(sock, level, optname, optval, 196109572fcaSLuiz Augusto von Dentz len); 196209572fcaSLuiz Augusto von Dentz 196309572fcaSLuiz Augusto von Dentz if (level != SOL_BLUETOOTH) 196409572fcaSLuiz Augusto von Dentz return -ENOPROTOOPT; 196509572fcaSLuiz Augusto von Dentz 196609572fcaSLuiz Augusto von Dentz lock_sock(sk); 196709572fcaSLuiz Augusto von Dentz 196809572fcaSLuiz Augusto von Dentz switch (optname) { 196909572fcaSLuiz Augusto von Dentz case BT_SNDMTU: 197009572fcaSLuiz Augusto von Dentz case BT_RCVMTU: 197109572fcaSLuiz Augusto von Dentz switch (hci_pi(sk)->channel) { 197209572fcaSLuiz Augusto von Dentz /* Don't allow changing MTU for channels that are meant for HCI 197309572fcaSLuiz Augusto von Dentz * traffic only. 197409572fcaSLuiz Augusto von Dentz */ 197509572fcaSLuiz Augusto von Dentz case HCI_CHANNEL_RAW: 197609572fcaSLuiz Augusto von Dentz case HCI_CHANNEL_USER: 197709572fcaSLuiz Augusto von Dentz err = -ENOPROTOOPT; 197809572fcaSLuiz Augusto von Dentz goto done; 197909572fcaSLuiz Augusto von Dentz } 198009572fcaSLuiz Augusto von Dentz 1981b9f9dbadSDan Carpenter if (copy_from_sockptr(&opt, optval, sizeof(opt))) { 198209572fcaSLuiz Augusto von Dentz err = -EFAULT; 198309572fcaSLuiz Augusto von Dentz break; 198409572fcaSLuiz Augusto von Dentz } 198509572fcaSLuiz Augusto von Dentz 198609572fcaSLuiz Augusto von Dentz hci_pi(sk)->mtu = opt; 198709572fcaSLuiz Augusto von Dentz break; 198809572fcaSLuiz Augusto von Dentz 198909572fcaSLuiz Augusto von Dentz default: 199009572fcaSLuiz Augusto von Dentz err = -ENOPROTOOPT; 199109572fcaSLuiz Augusto von Dentz break; 199209572fcaSLuiz Augusto von Dentz } 199309572fcaSLuiz Augusto von Dentz 199409572fcaSLuiz Augusto von Dentz done: 199509572fcaSLuiz Augusto von Dentz release_sock(sk); 199609572fcaSLuiz Augusto von Dentz return err; 199709572fcaSLuiz Augusto von Dentz } 199809572fcaSLuiz Augusto von Dentz 199909572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt_old(struct socket *sock, int level, int optname, 20008fc9ced3SGustavo Padovan char __user *optval, int __user *optlen) 20011da177e4SLinus Torvalds { 20021da177e4SLinus Torvalds struct hci_ufilter uf; 20031da177e4SLinus Torvalds struct sock *sk = sock->sk; 2004cedc5469SMarcel Holtmann int len, opt, err = 0; 2005cedc5469SMarcel Holtmann 2006cedc5469SMarcel Holtmann BT_DBG("sk %p, opt %d", sk, optname); 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds if (get_user(len, optlen)) 20091da177e4SLinus Torvalds return -EFAULT; 20101da177e4SLinus Torvalds 2011cedc5469SMarcel Holtmann lock_sock(sk); 2012cedc5469SMarcel Holtmann 2013cedc5469SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 2014c2371e80SMarcel Holtmann err = -EBADFD; 2015cedc5469SMarcel Holtmann goto done; 2016cedc5469SMarcel Holtmann } 2017cedc5469SMarcel Holtmann 20181da177e4SLinus Torvalds switch (optname) { 20191da177e4SLinus Torvalds case HCI_DATA_DIR: 20201da177e4SLinus Torvalds if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR) 20211da177e4SLinus Torvalds opt = 1; 20221da177e4SLinus Torvalds else 20231da177e4SLinus Torvalds opt = 0; 20241da177e4SLinus Torvalds 20251da177e4SLinus Torvalds if (put_user(opt, optval)) 2026cedc5469SMarcel Holtmann err = -EFAULT; 20271da177e4SLinus Torvalds break; 20281da177e4SLinus Torvalds 20291da177e4SLinus Torvalds case HCI_TIME_STAMP: 20301da177e4SLinus Torvalds if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP) 20311da177e4SLinus Torvalds opt = 1; 20321da177e4SLinus Torvalds else 20331da177e4SLinus Torvalds opt = 0; 20341da177e4SLinus Torvalds 20351da177e4SLinus Torvalds if (put_user(opt, optval)) 2036cedc5469SMarcel Holtmann err = -EFAULT; 20371da177e4SLinus Torvalds break; 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds case HCI_FILTER: 20401da177e4SLinus Torvalds { 20411da177e4SLinus Torvalds struct hci_filter *f = &hci_pi(sk)->filter; 20421da177e4SLinus Torvalds 2043e15ca9a0SMathias Krause memset(&uf, 0, sizeof(uf)); 20441da177e4SLinus Torvalds uf.type_mask = f->type_mask; 20451da177e4SLinus Torvalds uf.opcode = f->opcode; 20461da177e4SLinus Torvalds uf.event_mask[0] = *((u32 *) f->event_mask + 0); 20471da177e4SLinus Torvalds uf.event_mask[1] = *((u32 *) f->event_mask + 1); 20481da177e4SLinus Torvalds } 20491da177e4SLinus Torvalds 20501da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(uf)); 20511da177e4SLinus Torvalds if (copy_to_user(optval, &uf, len)) 2052cedc5469SMarcel Holtmann err = -EFAULT; 20531da177e4SLinus Torvalds break; 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds default: 2056cedc5469SMarcel Holtmann err = -ENOPROTOOPT; 20571da177e4SLinus Torvalds break; 20581da177e4SLinus Torvalds } 20591da177e4SLinus Torvalds 2060cedc5469SMarcel Holtmann done: 2061cedc5469SMarcel Holtmann release_sock(sk); 2062cedc5469SMarcel Holtmann return err; 20631da177e4SLinus Torvalds } 20641da177e4SLinus Torvalds 206509572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt(struct socket *sock, int level, int optname, 206609572fcaSLuiz Augusto von Dentz char __user *optval, int __user *optlen) 206709572fcaSLuiz Augusto von Dentz { 206809572fcaSLuiz Augusto von Dentz struct sock *sk = sock->sk; 206909572fcaSLuiz Augusto von Dentz int err = 0; 207009572fcaSLuiz Augusto von Dentz 207109572fcaSLuiz Augusto von Dentz BT_DBG("sk %p, opt %d", sk, optname); 207209572fcaSLuiz Augusto von Dentz 207309572fcaSLuiz Augusto von Dentz if (level == SOL_HCI) 207409572fcaSLuiz Augusto von Dentz return hci_sock_getsockopt_old(sock, level, optname, optval, 207509572fcaSLuiz Augusto von Dentz optlen); 207609572fcaSLuiz Augusto von Dentz 207709572fcaSLuiz Augusto von Dentz if (level != SOL_BLUETOOTH) 207809572fcaSLuiz Augusto von Dentz return -ENOPROTOOPT; 207909572fcaSLuiz Augusto von Dentz 208009572fcaSLuiz Augusto von Dentz lock_sock(sk); 208109572fcaSLuiz Augusto von Dentz 208209572fcaSLuiz Augusto von Dentz switch (optname) { 208309572fcaSLuiz Augusto von Dentz case BT_SNDMTU: 208409572fcaSLuiz Augusto von Dentz case BT_RCVMTU: 208509572fcaSLuiz Augusto von Dentz if (put_user(hci_pi(sk)->mtu, (u16 __user *)optval)) 208609572fcaSLuiz Augusto von Dentz err = -EFAULT; 208709572fcaSLuiz Augusto von Dentz break; 208809572fcaSLuiz Augusto von Dentz 208909572fcaSLuiz Augusto von Dentz default: 209009572fcaSLuiz Augusto von Dentz err = -ENOPROTOOPT; 209109572fcaSLuiz Augusto von Dentz break; 209209572fcaSLuiz Augusto von Dentz } 209309572fcaSLuiz Augusto von Dentz 209409572fcaSLuiz Augusto von Dentz release_sock(sk); 209509572fcaSLuiz Augusto von Dentz return err; 209609572fcaSLuiz Augusto von Dentz } 209709572fcaSLuiz Augusto von Dentz 2098709fca50SNguyen Dinh Phi static void hci_sock_destruct(struct sock *sk) 2099709fca50SNguyen Dinh Phi { 2100b338d917SBrian Gix mgmt_cleanup(sk); 2101709fca50SNguyen Dinh Phi skb_queue_purge(&sk->sk_receive_queue); 2102709fca50SNguyen Dinh Phi skb_queue_purge(&sk->sk_write_queue); 2103709fca50SNguyen Dinh Phi } 2104709fca50SNguyen Dinh Phi 210590ddc4f0SEric Dumazet static const struct proto_ops hci_sock_ops = { 21061da177e4SLinus Torvalds .family = PF_BLUETOOTH, 21071da177e4SLinus Torvalds .owner = THIS_MODULE, 21081da177e4SLinus Torvalds .release = hci_sock_release, 21091da177e4SLinus Torvalds .bind = hci_sock_bind, 21101da177e4SLinus Torvalds .getname = hci_sock_getname, 21111da177e4SLinus Torvalds .sendmsg = hci_sock_sendmsg, 21121da177e4SLinus Torvalds .recvmsg = hci_sock_recvmsg, 21131da177e4SLinus Torvalds .ioctl = hci_sock_ioctl, 21147a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT 21157a6038b3SArnd Bergmann .compat_ioctl = hci_sock_compat_ioctl, 21167a6038b3SArnd Bergmann #endif 2117a11e1d43SLinus Torvalds .poll = datagram_poll, 21181da177e4SLinus Torvalds .listen = sock_no_listen, 21191da177e4SLinus Torvalds .shutdown = sock_no_shutdown, 21201da177e4SLinus Torvalds .setsockopt = hci_sock_setsockopt, 21211da177e4SLinus Torvalds .getsockopt = hci_sock_getsockopt, 21221da177e4SLinus Torvalds .connect = sock_no_connect, 21231da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 21241da177e4SLinus Torvalds .accept = sock_no_accept, 21251da177e4SLinus Torvalds .mmap = sock_no_mmap 21261da177e4SLinus Torvalds }; 21271da177e4SLinus Torvalds 21281da177e4SLinus Torvalds static struct proto hci_sk_proto = { 21291da177e4SLinus Torvalds .name = "HCI", 21301da177e4SLinus Torvalds .owner = THIS_MODULE, 21311da177e4SLinus Torvalds .obj_size = sizeof(struct hci_pinfo) 21321da177e4SLinus Torvalds }; 21331da177e4SLinus Torvalds 21343f378b68SEric Paris static int hci_sock_create(struct net *net, struct socket *sock, int protocol, 21353f378b68SEric Paris int kern) 21361da177e4SLinus Torvalds { 21371da177e4SLinus Torvalds struct sock *sk; 21381da177e4SLinus Torvalds 21391da177e4SLinus Torvalds BT_DBG("sock %p", sock); 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds if (sock->type != SOCK_RAW) 21421da177e4SLinus Torvalds return -ESOCKTNOSUPPORT; 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds sock->ops = &hci_sock_ops; 21451da177e4SLinus Torvalds 214611aa9c28SEric W. Biederman sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto, kern); 21471da177e4SLinus Torvalds if (!sk) 21481da177e4SLinus Torvalds return -ENOMEM; 21491da177e4SLinus Torvalds 21501da177e4SLinus Torvalds sock_init_data(sock, sk); 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds sock_reset_flag(sk, SOCK_ZAPPED); 21531da177e4SLinus Torvalds 21541da177e4SLinus Torvalds sk->sk_protocol = protocol; 21551da177e4SLinus Torvalds 21561da177e4SLinus Torvalds sock->state = SS_UNCONNECTED; 21571da177e4SLinus Torvalds sk->sk_state = BT_OPEN; 2158709fca50SNguyen Dinh Phi sk->sk_destruct = hci_sock_destruct; 21591da177e4SLinus Torvalds 21601da177e4SLinus Torvalds bt_sock_link(&hci_sk_list, sk); 21611da177e4SLinus Torvalds return 0; 21621da177e4SLinus Torvalds } 21631da177e4SLinus Torvalds 2164ec1b4cf7SStephen Hemminger static const struct net_proto_family hci_sock_family_ops = { 21651da177e4SLinus Torvalds .family = PF_BLUETOOTH, 21661da177e4SLinus Torvalds .owner = THIS_MODULE, 21671da177e4SLinus Torvalds .create = hci_sock_create, 21681da177e4SLinus Torvalds }; 21691da177e4SLinus Torvalds 21701da177e4SLinus Torvalds int __init hci_sock_init(void) 21711da177e4SLinus Torvalds { 21721da177e4SLinus Torvalds int err; 21731da177e4SLinus Torvalds 2174b0a8e282SMarcel Holtmann BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr)); 2175b0a8e282SMarcel Holtmann 21761da177e4SLinus Torvalds err = proto_register(&hci_sk_proto, 0); 21771da177e4SLinus Torvalds if (err < 0) 21781da177e4SLinus Torvalds return err; 21791da177e4SLinus Torvalds 21801da177e4SLinus Torvalds err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops); 2181f7c86637SMasatake YAMATO if (err < 0) { 2182f7c86637SMasatake YAMATO BT_ERR("HCI socket registration failed"); 21831da177e4SLinus Torvalds goto error; 2184f7c86637SMasatake YAMATO } 2185f7c86637SMasatake YAMATO 2186b0316615SAl Viro err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL); 2187f7c86637SMasatake YAMATO if (err < 0) { 2188f7c86637SMasatake YAMATO BT_ERR("Failed to create HCI proc file"); 2189f7c86637SMasatake YAMATO bt_sock_unregister(BTPROTO_HCI); 2190f7c86637SMasatake YAMATO goto error; 2191f7c86637SMasatake YAMATO } 21921da177e4SLinus Torvalds 21931da177e4SLinus Torvalds BT_INFO("HCI socket layer initialized"); 21941da177e4SLinus Torvalds 21951da177e4SLinus Torvalds return 0; 21961da177e4SLinus Torvalds 21971da177e4SLinus Torvalds error: 21981da177e4SLinus Torvalds proto_unregister(&hci_sk_proto); 21991da177e4SLinus Torvalds return err; 22001da177e4SLinus Torvalds } 22011da177e4SLinus Torvalds 2202b7440a14SAnand Gadiyar void hci_sock_cleanup(void) 22031da177e4SLinus Torvalds { 2204f7c86637SMasatake YAMATO bt_procfs_cleanup(&init_net, "hci"); 22055e9d7f86SDavid Herrmann bt_sock_unregister(BTPROTO_HCI); 22061da177e4SLinus Torvalds proto_unregister(&hci_sk_proto); 22071da177e4SLinus Torvalds } 2208