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) { 104*0a8af30aSChristophe JAILLET id = ida_alloc_min(&sock_cookie_ida, 1, 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; 122*0a8af30aSChristophe JAILLET ida_free(&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 26769ae5065SLuiz Augusto von Dentz static void hci_sock_copy_creds(struct sock *sk, struct sk_buff *skb) 26869ae5065SLuiz Augusto von Dentz { 26969ae5065SLuiz Augusto von Dentz struct scm_creds *creds; 27069ae5065SLuiz Augusto von Dentz 27169ae5065SLuiz Augusto von Dentz if (!sk || WARN_ON(!skb)) 27269ae5065SLuiz Augusto von Dentz return; 27369ae5065SLuiz Augusto von Dentz 27469ae5065SLuiz Augusto von Dentz creds = &bt_cb(skb)->creds; 27569ae5065SLuiz Augusto von Dentz 27669ae5065SLuiz Augusto von Dentz /* Check if peer credentials is set */ 27769ae5065SLuiz Augusto von Dentz if (!sk->sk_peer_pid) { 27869ae5065SLuiz Augusto von Dentz /* Check if parent peer credentials is set */ 27969ae5065SLuiz Augusto von Dentz if (bt_sk(sk)->parent && bt_sk(sk)->parent->sk_peer_pid) 28069ae5065SLuiz Augusto von Dentz sk = bt_sk(sk)->parent; 28169ae5065SLuiz Augusto von Dentz else 28269ae5065SLuiz Augusto von Dentz return; 28369ae5065SLuiz Augusto von Dentz } 28469ae5065SLuiz Augusto von Dentz 28569ae5065SLuiz Augusto von Dentz /* Check if scm_creds already set */ 28669ae5065SLuiz Augusto von Dentz if (creds->pid == pid_vnr(sk->sk_peer_pid)) 28769ae5065SLuiz Augusto von Dentz return; 28869ae5065SLuiz Augusto von Dentz 28969ae5065SLuiz Augusto von Dentz memset(creds, 0, sizeof(*creds)); 29069ae5065SLuiz Augusto von Dentz 29169ae5065SLuiz Augusto von Dentz creds->pid = pid_vnr(sk->sk_peer_pid); 29269ae5065SLuiz Augusto von Dentz if (sk->sk_peer_cred) { 29369ae5065SLuiz Augusto von Dentz creds->uid = sk->sk_peer_cred->uid; 29469ae5065SLuiz Augusto von Dentz creds->gid = sk->sk_peer_cred->gid; 29569ae5065SLuiz Augusto von Dentz } 29669ae5065SLuiz Augusto von Dentz } 29769ae5065SLuiz Augusto von Dentz 29869ae5065SLuiz Augusto von Dentz static struct sk_buff *hci_skb_clone(struct sk_buff *skb) 29969ae5065SLuiz Augusto von Dentz { 30069ae5065SLuiz Augusto von Dentz struct sk_buff *nskb; 30169ae5065SLuiz Augusto von Dentz 30269ae5065SLuiz Augusto von Dentz if (!skb) 30369ae5065SLuiz Augusto von Dentz return NULL; 30469ae5065SLuiz Augusto von Dentz 30569ae5065SLuiz Augusto von Dentz nskb = skb_clone(skb, GFP_ATOMIC); 30669ae5065SLuiz Augusto von Dentz if (!nskb) 30769ae5065SLuiz Augusto von Dentz return NULL; 30869ae5065SLuiz Augusto von Dentz 30969ae5065SLuiz Augusto von Dentz hci_sock_copy_creds(skb->sk, nskb); 31069ae5065SLuiz Augusto von Dentz 31169ae5065SLuiz Augusto von Dentz return nskb; 31269ae5065SLuiz Augusto von Dentz } 31369ae5065SLuiz Augusto von Dentz 3147129069eSJohan Hedberg /* Send frame to sockets with specific channel */ 315a9ee77afSSebastian Andrzej Siewior static void __hci_send_to_channel(unsigned short channel, struct sk_buff *skb, 316c08b1a1dSMarcel Holtmann int flag, struct sock *skip_sk) 317470fe1b5SMarcel Holtmann { 318470fe1b5SMarcel Holtmann struct sock *sk; 319470fe1b5SMarcel Holtmann 3207129069eSJohan Hedberg BT_DBG("channel %u len %d", channel, skb->len); 321470fe1b5SMarcel Holtmann 322b67bfe0dSSasha Levin sk_for_each(sk, &hci_sk_list.head) { 323470fe1b5SMarcel Holtmann struct sk_buff *nskb; 324470fe1b5SMarcel Holtmann 325c08b1a1dSMarcel Holtmann /* Ignore socket without the flag set */ 326c85be545SMarcel Holtmann if (!hci_sock_test_flag(sk, flag)) 327c08b1a1dSMarcel Holtmann continue; 328c08b1a1dSMarcel Holtmann 329470fe1b5SMarcel Holtmann /* Skip the original socket */ 330470fe1b5SMarcel Holtmann if (sk == skip_sk) 331470fe1b5SMarcel Holtmann continue; 332470fe1b5SMarcel Holtmann 333470fe1b5SMarcel Holtmann if (sk->sk_state != BT_BOUND) 334470fe1b5SMarcel Holtmann continue; 335470fe1b5SMarcel Holtmann 3367129069eSJohan Hedberg if (hci_pi(sk)->channel != channel) 337d7f72f61SMarcel Holtmann continue; 338d7f72f61SMarcel Holtmann 33969ae5065SLuiz Augusto von Dentz nskb = hci_skb_clone(skb); 340d7f72f61SMarcel Holtmann if (!nskb) 341d7f72f61SMarcel Holtmann continue; 342d7f72f61SMarcel Holtmann 343d7f72f61SMarcel Holtmann if (sock_queue_rcv_skb(sk, nskb)) 344d7f72f61SMarcel Holtmann kfree_skb(nskb); 345d7f72f61SMarcel Holtmann } 346d7f72f61SMarcel Holtmann 347a9ee77afSSebastian Andrzej Siewior } 348a9ee77afSSebastian Andrzej Siewior 349a9ee77afSSebastian Andrzej Siewior void hci_send_to_channel(unsigned short channel, struct sk_buff *skb, 350a9ee77afSSebastian Andrzej Siewior int flag, struct sock *skip_sk) 351a9ee77afSSebastian Andrzej Siewior { 352a9ee77afSSebastian Andrzej Siewior read_lock(&hci_sk_list.lock); 353a9ee77afSSebastian Andrzej Siewior __hci_send_to_channel(channel, skb, flag, skip_sk); 354d7f72f61SMarcel Holtmann read_unlock(&hci_sk_list.lock); 355d7f72f61SMarcel Holtmann } 356d7f72f61SMarcel Holtmann 357cd82e61cSMarcel Holtmann /* Send frame to monitor socket */ 358cd82e61cSMarcel Holtmann void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb) 359cd82e61cSMarcel Holtmann { 360cd82e61cSMarcel Holtmann struct sk_buff *skb_copy = NULL; 3612b531294SMarcel Holtmann struct hci_mon_hdr *hdr; 362cd82e61cSMarcel Holtmann __le16 opcode; 363cd82e61cSMarcel Holtmann 364cd82e61cSMarcel Holtmann if (!atomic_read(&monitor_promisc)) 365cd82e61cSMarcel Holtmann return; 366cd82e61cSMarcel Holtmann 367cd82e61cSMarcel Holtmann BT_DBG("hdev %p len %d", hdev, skb->len); 368cd82e61cSMarcel Holtmann 369d79f34e3SMarcel Holtmann switch (hci_skb_pkt_type(skb)) { 370cd82e61cSMarcel Holtmann case HCI_COMMAND_PKT: 371dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_COMMAND_PKT); 372cd82e61cSMarcel Holtmann break; 373cd82e61cSMarcel Holtmann case HCI_EVENT_PKT: 374dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_EVENT_PKT); 375cd82e61cSMarcel Holtmann break; 376cd82e61cSMarcel Holtmann case HCI_ACLDATA_PKT: 377cd82e61cSMarcel Holtmann if (bt_cb(skb)->incoming) 378dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_ACL_RX_PKT); 379cd82e61cSMarcel Holtmann else 380dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_ACL_TX_PKT); 381cd82e61cSMarcel Holtmann break; 382cd82e61cSMarcel Holtmann case HCI_SCODATA_PKT: 383cd82e61cSMarcel Holtmann if (bt_cb(skb)->incoming) 384dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_SCO_RX_PKT); 385cd82e61cSMarcel Holtmann else 386dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_SCO_TX_PKT); 387cd82e61cSMarcel Holtmann break; 388f9a619dbSLuiz Augusto von Dentz case HCI_ISODATA_PKT: 389f9a619dbSLuiz Augusto von Dentz if (bt_cb(skb)->incoming) 390f9a619dbSLuiz Augusto von Dentz opcode = cpu_to_le16(HCI_MON_ISO_RX_PKT); 391f9a619dbSLuiz Augusto von Dentz else 392f9a619dbSLuiz Augusto von Dentz opcode = cpu_to_le16(HCI_MON_ISO_TX_PKT); 393f9a619dbSLuiz Augusto von Dentz break; 394e875ff84SMarcel Holtmann case HCI_DIAG_PKT: 395e875ff84SMarcel Holtmann opcode = cpu_to_le16(HCI_MON_VENDOR_DIAG); 396e875ff84SMarcel Holtmann break; 397cd82e61cSMarcel Holtmann default: 398cd82e61cSMarcel Holtmann return; 399cd82e61cSMarcel Holtmann } 400cd82e61cSMarcel Holtmann 4012b531294SMarcel Holtmann /* Create a private copy with headroom */ 4022b531294SMarcel Holtmann skb_copy = __pskb_copy_fclone(skb, HCI_MON_HDR_SIZE, GFP_ATOMIC, true); 4032b531294SMarcel Holtmann if (!skb_copy) 4042b531294SMarcel Holtmann return; 4052b531294SMarcel Holtmann 40669ae5065SLuiz Augusto von Dentz hci_sock_copy_creds(skb->sk, skb_copy); 40769ae5065SLuiz Augusto von Dentz 4082b531294SMarcel Holtmann /* Put header before the data */ 409d58ff351SJohannes Berg hdr = skb_push(skb_copy, HCI_MON_HDR_SIZE); 4102b531294SMarcel Holtmann hdr->opcode = opcode; 4112b531294SMarcel Holtmann hdr->index = cpu_to_le16(hdev->id); 4122b531294SMarcel Holtmann hdr->len = cpu_to_le16(skb->len); 4132b531294SMarcel Holtmann 414c08b1a1dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb_copy, 415c08b1a1dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 416cd82e61cSMarcel Holtmann kfree_skb(skb_copy); 417cd82e61cSMarcel Holtmann } 418cd82e61cSMarcel Holtmann 41938ceaa00SMarcel Holtmann void hci_send_monitor_ctrl_event(struct hci_dev *hdev, u16 event, 42038ceaa00SMarcel Holtmann void *data, u16 data_len, ktime_t tstamp, 42138ceaa00SMarcel Holtmann int flag, struct sock *skip_sk) 42238ceaa00SMarcel Holtmann { 42338ceaa00SMarcel Holtmann struct sock *sk; 42438ceaa00SMarcel Holtmann __le16 index; 42538ceaa00SMarcel Holtmann 42638ceaa00SMarcel Holtmann if (hdev) 42738ceaa00SMarcel Holtmann index = cpu_to_le16(hdev->id); 42838ceaa00SMarcel Holtmann else 42938ceaa00SMarcel Holtmann index = cpu_to_le16(MGMT_INDEX_NONE); 43038ceaa00SMarcel Holtmann 43138ceaa00SMarcel Holtmann read_lock(&hci_sk_list.lock); 43238ceaa00SMarcel Holtmann 43338ceaa00SMarcel Holtmann sk_for_each(sk, &hci_sk_list.head) { 43438ceaa00SMarcel Holtmann struct hci_mon_hdr *hdr; 43538ceaa00SMarcel Holtmann struct sk_buff *skb; 43638ceaa00SMarcel Holtmann 43738ceaa00SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_CONTROL) 43838ceaa00SMarcel Holtmann continue; 43938ceaa00SMarcel Holtmann 44038ceaa00SMarcel Holtmann /* Ignore socket without the flag set */ 44138ceaa00SMarcel Holtmann if (!hci_sock_test_flag(sk, flag)) 44238ceaa00SMarcel Holtmann continue; 44338ceaa00SMarcel Holtmann 44438ceaa00SMarcel Holtmann /* Skip the original socket */ 44538ceaa00SMarcel Holtmann if (sk == skip_sk) 44638ceaa00SMarcel Holtmann continue; 44738ceaa00SMarcel Holtmann 44838ceaa00SMarcel Holtmann skb = bt_skb_alloc(6 + data_len, GFP_ATOMIC); 44938ceaa00SMarcel Holtmann if (!skb) 45038ceaa00SMarcel Holtmann continue; 45138ceaa00SMarcel Holtmann 45238ceaa00SMarcel Holtmann put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4)); 45338ceaa00SMarcel Holtmann put_unaligned_le16(event, skb_put(skb, 2)); 45438ceaa00SMarcel Holtmann 45538ceaa00SMarcel Holtmann if (data) 45659ae1d12SJohannes Berg skb_put_data(skb, data, data_len); 45738ceaa00SMarcel Holtmann 45838ceaa00SMarcel Holtmann skb->tstamp = tstamp; 45938ceaa00SMarcel Holtmann 460d58ff351SJohannes Berg hdr = skb_push(skb, HCI_MON_HDR_SIZE); 46138ceaa00SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_CTRL_EVENT); 46238ceaa00SMarcel Holtmann hdr->index = index; 46338ceaa00SMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE); 46438ceaa00SMarcel Holtmann 465a9ee77afSSebastian Andrzej Siewior __hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 46638ceaa00SMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 46738ceaa00SMarcel Holtmann kfree_skb(skb); 46838ceaa00SMarcel Holtmann } 46938ceaa00SMarcel Holtmann 47038ceaa00SMarcel Holtmann read_unlock(&hci_sk_list.lock); 47138ceaa00SMarcel Holtmann } 47238ceaa00SMarcel Holtmann 473cd82e61cSMarcel Holtmann static struct sk_buff *create_monitor_event(struct hci_dev *hdev, int event) 474cd82e61cSMarcel Holtmann { 475cd82e61cSMarcel Holtmann struct hci_mon_hdr *hdr; 476cd82e61cSMarcel Holtmann struct hci_mon_new_index *ni; 4776c566dd5SMarcel Holtmann struct hci_mon_index_info *ii; 478cd82e61cSMarcel Holtmann struct sk_buff *skb; 479cd82e61cSMarcel Holtmann __le16 opcode; 480cd82e61cSMarcel Holtmann 481cd82e61cSMarcel Holtmann switch (event) { 482cd82e61cSMarcel Holtmann case HCI_DEV_REG: 483cd82e61cSMarcel Holtmann skb = bt_skb_alloc(HCI_MON_NEW_INDEX_SIZE, GFP_ATOMIC); 484cd82e61cSMarcel Holtmann if (!skb) 485cd82e61cSMarcel Holtmann return NULL; 486cd82e61cSMarcel Holtmann 4874df864c1SJohannes Berg ni = skb_put(skb, HCI_MON_NEW_INDEX_SIZE); 488cd82e61cSMarcel Holtmann ni->type = hdev->dev_type; 489cd82e61cSMarcel Holtmann ni->bus = hdev->bus; 490cd82e61cSMarcel Holtmann bacpy(&ni->bdaddr, &hdev->bdaddr); 491cb3871b1SKees Cook memcpy_and_pad(ni->name, sizeof(ni->name), hdev->name, 492cb3871b1SKees Cook strnlen(hdev->name, sizeof(ni->name)), '\0'); 493cd82e61cSMarcel Holtmann 494dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_NEW_INDEX); 495cd82e61cSMarcel Holtmann break; 496cd82e61cSMarcel Holtmann 497cd82e61cSMarcel Holtmann case HCI_DEV_UNREG: 498cd82e61cSMarcel Holtmann skb = bt_skb_alloc(0, GFP_ATOMIC); 499cd82e61cSMarcel Holtmann if (!skb) 500cd82e61cSMarcel Holtmann return NULL; 501cd82e61cSMarcel Holtmann 502dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_DEL_INDEX); 503cd82e61cSMarcel Holtmann break; 504cd82e61cSMarcel Holtmann 505e131d74aSMarcel Holtmann case HCI_DEV_SETUP: 506e131d74aSMarcel Holtmann if (hdev->manufacturer == 0xffff) 507e131d74aSMarcel Holtmann return NULL; 50819186c7bSGustavo A. R. Silva fallthrough; 509e131d74aSMarcel Holtmann 5106c566dd5SMarcel Holtmann case HCI_DEV_UP: 5116c566dd5SMarcel Holtmann skb = bt_skb_alloc(HCI_MON_INDEX_INFO_SIZE, GFP_ATOMIC); 5126c566dd5SMarcel Holtmann if (!skb) 5136c566dd5SMarcel Holtmann return NULL; 5146c566dd5SMarcel Holtmann 5154df864c1SJohannes Berg ii = skb_put(skb, HCI_MON_INDEX_INFO_SIZE); 5166c566dd5SMarcel Holtmann bacpy(&ii->bdaddr, &hdev->bdaddr); 5176c566dd5SMarcel Holtmann ii->manufacturer = cpu_to_le16(hdev->manufacturer); 5186c566dd5SMarcel Holtmann 5196c566dd5SMarcel Holtmann opcode = cpu_to_le16(HCI_MON_INDEX_INFO); 5206c566dd5SMarcel Holtmann break; 5216c566dd5SMarcel Holtmann 52222db3cbcSMarcel Holtmann case HCI_DEV_OPEN: 52322db3cbcSMarcel Holtmann skb = bt_skb_alloc(0, GFP_ATOMIC); 52422db3cbcSMarcel Holtmann if (!skb) 52522db3cbcSMarcel Holtmann return NULL; 52622db3cbcSMarcel Holtmann 52722db3cbcSMarcel Holtmann opcode = cpu_to_le16(HCI_MON_OPEN_INDEX); 52822db3cbcSMarcel Holtmann break; 52922db3cbcSMarcel Holtmann 53022db3cbcSMarcel Holtmann case HCI_DEV_CLOSE: 53122db3cbcSMarcel Holtmann skb = bt_skb_alloc(0, GFP_ATOMIC); 53222db3cbcSMarcel Holtmann if (!skb) 53322db3cbcSMarcel Holtmann return NULL; 53422db3cbcSMarcel Holtmann 53522db3cbcSMarcel Holtmann opcode = cpu_to_le16(HCI_MON_CLOSE_INDEX); 53622db3cbcSMarcel Holtmann break; 53722db3cbcSMarcel Holtmann 538cd82e61cSMarcel Holtmann default: 539cd82e61cSMarcel Holtmann return NULL; 540cd82e61cSMarcel Holtmann } 541cd82e61cSMarcel Holtmann 542cd82e61cSMarcel Holtmann __net_timestamp(skb); 543cd82e61cSMarcel Holtmann 544d58ff351SJohannes Berg hdr = skb_push(skb, HCI_MON_HDR_SIZE); 545cd82e61cSMarcel Holtmann hdr->opcode = opcode; 546cd82e61cSMarcel Holtmann hdr->index = cpu_to_le16(hdev->id); 547cd82e61cSMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE); 548cd82e61cSMarcel Holtmann 549cd82e61cSMarcel Holtmann return skb; 550cd82e61cSMarcel Holtmann } 551cd82e61cSMarcel Holtmann 552249fa169SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_open(struct sock *sk) 553249fa169SMarcel Holtmann { 554249fa169SMarcel Holtmann struct hci_mon_hdr *hdr; 555249fa169SMarcel Holtmann struct sk_buff *skb; 556d0bef1d2SMarcel Holtmann u16 format; 557249fa169SMarcel Holtmann u8 ver[3]; 558249fa169SMarcel Holtmann u32 flags; 559249fa169SMarcel Holtmann 5600ef2c42fSMarcel Holtmann /* No message needed when cookie is not present */ 5610ef2c42fSMarcel Holtmann if (!hci_pi(sk)->cookie) 5620ef2c42fSMarcel Holtmann return NULL; 5630ef2c42fSMarcel Holtmann 564d0bef1d2SMarcel Holtmann switch (hci_pi(sk)->channel) { 565f81f5b2dSMarcel Holtmann case HCI_CHANNEL_RAW: 566f81f5b2dSMarcel Holtmann format = 0x0000; 567f81f5b2dSMarcel Holtmann ver[0] = BT_SUBSYS_VERSION; 568f81f5b2dSMarcel Holtmann put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1); 569f81f5b2dSMarcel Holtmann break; 570aa1638ddSMarcel Holtmann case HCI_CHANNEL_USER: 571aa1638ddSMarcel Holtmann format = 0x0001; 572aa1638ddSMarcel Holtmann ver[0] = BT_SUBSYS_VERSION; 573aa1638ddSMarcel Holtmann put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1); 574aa1638ddSMarcel Holtmann break; 575d0bef1d2SMarcel Holtmann case HCI_CHANNEL_CONTROL: 576d0bef1d2SMarcel Holtmann format = 0x0002; 577d0bef1d2SMarcel Holtmann mgmt_fill_version_info(ver); 578d0bef1d2SMarcel Holtmann break; 579d0bef1d2SMarcel Holtmann default: 580d0bef1d2SMarcel Holtmann /* No message for unsupported format */ 581d0bef1d2SMarcel Holtmann return NULL; 582d0bef1d2SMarcel Holtmann } 583d0bef1d2SMarcel Holtmann 584249fa169SMarcel Holtmann skb = bt_skb_alloc(14 + TASK_COMM_LEN, GFP_ATOMIC); 585249fa169SMarcel Holtmann if (!skb) 586249fa169SMarcel Holtmann return NULL; 587249fa169SMarcel Holtmann 58869ae5065SLuiz Augusto von Dentz hci_sock_copy_creds(sk, skb); 58969ae5065SLuiz Augusto von Dentz 590249fa169SMarcel Holtmann flags = hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) ? 0x1 : 0x0; 591249fa169SMarcel Holtmann 592249fa169SMarcel Holtmann put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4)); 593249fa169SMarcel Holtmann put_unaligned_le16(format, skb_put(skb, 2)); 59459ae1d12SJohannes Berg skb_put_data(skb, ver, sizeof(ver)); 595249fa169SMarcel Holtmann put_unaligned_le32(flags, skb_put(skb, 4)); 596634fef61SJohannes Berg skb_put_u8(skb, TASK_COMM_LEN); 59759ae1d12SJohannes Berg skb_put_data(skb, hci_pi(sk)->comm, TASK_COMM_LEN); 598249fa169SMarcel Holtmann 599249fa169SMarcel Holtmann __net_timestamp(skb); 600249fa169SMarcel Holtmann 601d58ff351SJohannes Berg hdr = skb_push(skb, HCI_MON_HDR_SIZE); 602249fa169SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_CTRL_OPEN); 6030ef2c42fSMarcel Holtmann if (hci_pi(sk)->hdev) 6040ef2c42fSMarcel Holtmann hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id); 6050ef2c42fSMarcel Holtmann else 606249fa169SMarcel Holtmann hdr->index = cpu_to_le16(HCI_DEV_NONE); 607249fa169SMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE); 608249fa169SMarcel Holtmann 609249fa169SMarcel Holtmann return skb; 610249fa169SMarcel Holtmann } 611249fa169SMarcel Holtmann 612249fa169SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_close(struct sock *sk) 613249fa169SMarcel Holtmann { 614249fa169SMarcel Holtmann struct hci_mon_hdr *hdr; 615249fa169SMarcel Holtmann struct sk_buff *skb; 616249fa169SMarcel Holtmann 6170ef2c42fSMarcel Holtmann /* No message needed when cookie is not present */ 6180ef2c42fSMarcel Holtmann if (!hci_pi(sk)->cookie) 6190ef2c42fSMarcel Holtmann return NULL; 6200ef2c42fSMarcel Holtmann 621d0bef1d2SMarcel Holtmann switch (hci_pi(sk)->channel) { 622f81f5b2dSMarcel Holtmann case HCI_CHANNEL_RAW: 623aa1638ddSMarcel Holtmann case HCI_CHANNEL_USER: 624d0bef1d2SMarcel Holtmann case HCI_CHANNEL_CONTROL: 625d0bef1d2SMarcel Holtmann break; 626d0bef1d2SMarcel Holtmann default: 627d0bef1d2SMarcel Holtmann /* No message for unsupported format */ 628d0bef1d2SMarcel Holtmann return NULL; 629d0bef1d2SMarcel Holtmann } 630d0bef1d2SMarcel Holtmann 631249fa169SMarcel Holtmann skb = bt_skb_alloc(4, GFP_ATOMIC); 632249fa169SMarcel Holtmann if (!skb) 633249fa169SMarcel Holtmann return NULL; 634249fa169SMarcel Holtmann 63569ae5065SLuiz Augusto von Dentz hci_sock_copy_creds(sk, skb); 63669ae5065SLuiz Augusto von Dentz 637249fa169SMarcel Holtmann put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4)); 638249fa169SMarcel Holtmann 639249fa169SMarcel Holtmann __net_timestamp(skb); 640249fa169SMarcel Holtmann 641d58ff351SJohannes Berg hdr = skb_push(skb, HCI_MON_HDR_SIZE); 642249fa169SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_CTRL_CLOSE); 6430ef2c42fSMarcel Holtmann if (hci_pi(sk)->hdev) 6440ef2c42fSMarcel Holtmann hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id); 6450ef2c42fSMarcel Holtmann else 646249fa169SMarcel Holtmann hdr->index = cpu_to_le16(HCI_DEV_NONE); 647249fa169SMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE); 648249fa169SMarcel Holtmann 649249fa169SMarcel Holtmann return skb; 650249fa169SMarcel Holtmann } 651249fa169SMarcel Holtmann 65238ceaa00SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_command(struct sock *sk, u16 index, 65338ceaa00SMarcel Holtmann u16 opcode, u16 len, 65438ceaa00SMarcel Holtmann const void *buf) 65538ceaa00SMarcel Holtmann { 65638ceaa00SMarcel Holtmann struct hci_mon_hdr *hdr; 65738ceaa00SMarcel Holtmann struct sk_buff *skb; 65838ceaa00SMarcel Holtmann 65938ceaa00SMarcel Holtmann skb = bt_skb_alloc(6 + len, GFP_ATOMIC); 66038ceaa00SMarcel Holtmann if (!skb) 66138ceaa00SMarcel Holtmann return NULL; 66238ceaa00SMarcel Holtmann 66369ae5065SLuiz Augusto von Dentz hci_sock_copy_creds(sk, skb); 66469ae5065SLuiz Augusto von Dentz 66538ceaa00SMarcel Holtmann put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4)); 66638ceaa00SMarcel Holtmann put_unaligned_le16(opcode, skb_put(skb, 2)); 66738ceaa00SMarcel Holtmann 66838ceaa00SMarcel Holtmann if (buf) 66959ae1d12SJohannes Berg skb_put_data(skb, buf, len); 67038ceaa00SMarcel Holtmann 67138ceaa00SMarcel Holtmann __net_timestamp(skb); 67238ceaa00SMarcel Holtmann 673d58ff351SJohannes Berg hdr = skb_push(skb, HCI_MON_HDR_SIZE); 67438ceaa00SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_CTRL_COMMAND); 67538ceaa00SMarcel Holtmann hdr->index = cpu_to_le16(index); 67638ceaa00SMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE); 67738ceaa00SMarcel Holtmann 67838ceaa00SMarcel Holtmann return skb; 67938ceaa00SMarcel Holtmann } 68038ceaa00SMarcel Holtmann 681787b306cSJohannes Berg static void __printf(2, 3) 682787b306cSJohannes Berg send_monitor_note(struct sock *sk, const char *fmt, ...) 683dd31506dSMarcel Holtmann { 684787b306cSJohannes Berg size_t len; 685dd31506dSMarcel Holtmann struct hci_mon_hdr *hdr; 686dd31506dSMarcel Holtmann struct sk_buff *skb; 687787b306cSJohannes Berg va_list args; 688787b306cSJohannes Berg 689787b306cSJohannes Berg va_start(args, fmt); 690787b306cSJohannes Berg len = vsnprintf(NULL, 0, fmt, args); 691787b306cSJohannes Berg va_end(args); 692dd31506dSMarcel Holtmann 693dd31506dSMarcel Holtmann skb = bt_skb_alloc(len + 1, GFP_ATOMIC); 694dd31506dSMarcel Holtmann if (!skb) 695dd31506dSMarcel Holtmann return; 696dd31506dSMarcel Holtmann 69769ae5065SLuiz Augusto von Dentz hci_sock_copy_creds(sk, skb); 69869ae5065SLuiz Augusto von Dentz 699787b306cSJohannes Berg va_start(args, fmt); 700787b306cSJohannes Berg vsprintf(skb_put(skb, len), fmt, args); 7014df864c1SJohannes Berg *(u8 *)skb_put(skb, 1) = 0; 702787b306cSJohannes Berg va_end(args); 703dd31506dSMarcel Holtmann 704dd31506dSMarcel Holtmann __net_timestamp(skb); 705dd31506dSMarcel Holtmann 706dd31506dSMarcel Holtmann hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE); 707dd31506dSMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_SYSTEM_NOTE); 708dd31506dSMarcel Holtmann hdr->index = cpu_to_le16(HCI_DEV_NONE); 709dd31506dSMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE); 710dd31506dSMarcel Holtmann 711dd31506dSMarcel Holtmann if (sock_queue_rcv_skb(sk, skb)) 712dd31506dSMarcel Holtmann kfree_skb(skb); 713dd31506dSMarcel Holtmann } 714dd31506dSMarcel Holtmann 715cd82e61cSMarcel Holtmann static void send_monitor_replay(struct sock *sk) 716cd82e61cSMarcel Holtmann { 717cd82e61cSMarcel Holtmann struct hci_dev *hdev; 718cd82e61cSMarcel Holtmann 719cd82e61cSMarcel Holtmann read_lock(&hci_dev_list_lock); 720cd82e61cSMarcel Holtmann 721cd82e61cSMarcel Holtmann list_for_each_entry(hdev, &hci_dev_list, list) { 722cd82e61cSMarcel Holtmann struct sk_buff *skb; 723cd82e61cSMarcel Holtmann 724cd82e61cSMarcel Holtmann skb = create_monitor_event(hdev, HCI_DEV_REG); 725cd82e61cSMarcel Holtmann if (!skb) 726cd82e61cSMarcel Holtmann continue; 727cd82e61cSMarcel Holtmann 728cd82e61cSMarcel Holtmann if (sock_queue_rcv_skb(sk, skb)) 729cd82e61cSMarcel Holtmann kfree_skb(skb); 73022db3cbcSMarcel Holtmann 73122db3cbcSMarcel Holtmann if (!test_bit(HCI_RUNNING, &hdev->flags)) 73222db3cbcSMarcel Holtmann continue; 73322db3cbcSMarcel Holtmann 73422db3cbcSMarcel Holtmann skb = create_monitor_event(hdev, HCI_DEV_OPEN); 73522db3cbcSMarcel Holtmann if (!skb) 73622db3cbcSMarcel Holtmann continue; 73722db3cbcSMarcel Holtmann 73822db3cbcSMarcel Holtmann if (sock_queue_rcv_skb(sk, skb)) 73922db3cbcSMarcel Holtmann kfree_skb(skb); 7406c566dd5SMarcel Holtmann 741e131d74aSMarcel Holtmann if (test_bit(HCI_UP, &hdev->flags)) 7426c566dd5SMarcel Holtmann skb = create_monitor_event(hdev, HCI_DEV_UP); 743e131d74aSMarcel Holtmann else if (hci_dev_test_flag(hdev, HCI_SETUP)) 744e131d74aSMarcel Holtmann skb = create_monitor_event(hdev, HCI_DEV_SETUP); 745e131d74aSMarcel Holtmann else 746e131d74aSMarcel Holtmann skb = NULL; 7476c566dd5SMarcel Holtmann 748e131d74aSMarcel Holtmann if (skb) { 7496c566dd5SMarcel Holtmann if (sock_queue_rcv_skb(sk, skb)) 7506c566dd5SMarcel Holtmann kfree_skb(skb); 751cd82e61cSMarcel Holtmann } 752e131d74aSMarcel Holtmann } 753cd82e61cSMarcel Holtmann 754cd82e61cSMarcel Holtmann read_unlock(&hci_dev_list_lock); 755cd82e61cSMarcel Holtmann } 756cd82e61cSMarcel Holtmann 757249fa169SMarcel Holtmann static void send_monitor_control_replay(struct sock *mon_sk) 758249fa169SMarcel Holtmann { 759249fa169SMarcel Holtmann struct sock *sk; 760249fa169SMarcel Holtmann 761249fa169SMarcel Holtmann read_lock(&hci_sk_list.lock); 762249fa169SMarcel Holtmann 763249fa169SMarcel Holtmann sk_for_each(sk, &hci_sk_list.head) { 764249fa169SMarcel Holtmann struct sk_buff *skb; 765249fa169SMarcel Holtmann 766249fa169SMarcel Holtmann skb = create_monitor_ctrl_open(sk); 767249fa169SMarcel Holtmann if (!skb) 768249fa169SMarcel Holtmann continue; 769249fa169SMarcel Holtmann 770249fa169SMarcel Holtmann if (sock_queue_rcv_skb(mon_sk, skb)) 771249fa169SMarcel Holtmann kfree_skb(skb); 772249fa169SMarcel Holtmann } 773249fa169SMarcel Holtmann 774249fa169SMarcel Holtmann read_unlock(&hci_sk_list.lock); 775249fa169SMarcel Holtmann } 776249fa169SMarcel Holtmann 777040030efSMarcel Holtmann /* Generate internal stack event */ 778040030efSMarcel Holtmann static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data) 779040030efSMarcel Holtmann { 780040030efSMarcel Holtmann struct hci_event_hdr *hdr; 781040030efSMarcel Holtmann struct hci_ev_stack_internal *ev; 782040030efSMarcel Holtmann struct sk_buff *skb; 783040030efSMarcel Holtmann 784040030efSMarcel Holtmann skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC); 785040030efSMarcel Holtmann if (!skb) 786040030efSMarcel Holtmann return; 787040030efSMarcel Holtmann 7884df864c1SJohannes Berg hdr = skb_put(skb, HCI_EVENT_HDR_SIZE); 789040030efSMarcel Holtmann hdr->evt = HCI_EV_STACK_INTERNAL; 790040030efSMarcel Holtmann hdr->plen = sizeof(*ev) + dlen; 791040030efSMarcel Holtmann 7924df864c1SJohannes Berg ev = skb_put(skb, sizeof(*ev) + dlen); 793040030efSMarcel Holtmann ev->type = type; 794040030efSMarcel Holtmann memcpy(ev->data, data, dlen); 795040030efSMarcel Holtmann 796040030efSMarcel Holtmann bt_cb(skb)->incoming = 1; 797040030efSMarcel Holtmann __net_timestamp(skb); 798040030efSMarcel Holtmann 799d79f34e3SMarcel Holtmann hci_skb_pkt_type(skb) = HCI_EVENT_PKT; 800040030efSMarcel Holtmann hci_send_to_sock(hdev, skb); 801040030efSMarcel Holtmann kfree_skb(skb); 802040030efSMarcel Holtmann } 803040030efSMarcel Holtmann 804040030efSMarcel Holtmann void hci_sock_dev_event(struct hci_dev *hdev, int event) 805040030efSMarcel Holtmann { 806040030efSMarcel Holtmann BT_DBG("hdev %s event %d", hdev->name, event); 807040030efSMarcel Holtmann 808cd82e61cSMarcel Holtmann if (atomic_read(&monitor_promisc)) { 809cd82e61cSMarcel Holtmann struct sk_buff *skb; 810cd82e61cSMarcel Holtmann 811ed1b28a4SMarcel Holtmann /* Send event to monitor */ 812cd82e61cSMarcel Holtmann skb = create_monitor_event(hdev, event); 813cd82e61cSMarcel Holtmann if (skb) { 814c08b1a1dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 815c08b1a1dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 816cd82e61cSMarcel Holtmann kfree_skb(skb); 817cd82e61cSMarcel Holtmann } 818cd82e61cSMarcel Holtmann } 819cd82e61cSMarcel Holtmann 820ed1b28a4SMarcel Holtmann if (event <= HCI_DEV_DOWN) { 821ed1b28a4SMarcel Holtmann struct hci_ev_si_device ev; 822ed1b28a4SMarcel Holtmann 823040030efSMarcel Holtmann /* Send event to sockets */ 824040030efSMarcel Holtmann ev.event = event; 825040030efSMarcel Holtmann ev.dev_id = hdev->id; 826040030efSMarcel Holtmann hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev); 827ed1b28a4SMarcel Holtmann } 828040030efSMarcel Holtmann 829040030efSMarcel Holtmann if (event == HCI_DEV_UNREG) { 830040030efSMarcel Holtmann struct sock *sk; 831040030efSMarcel Holtmann 832e0448092STetsuo Handa /* Wake up sockets using this dead device */ 833040030efSMarcel Holtmann read_lock(&hci_sk_list.lock); 834b67bfe0dSSasha Levin sk_for_each(sk, &hci_sk_list.head) { 835040030efSMarcel Holtmann if (hci_pi(sk)->hdev == hdev) { 836040030efSMarcel Holtmann sk->sk_err = EPIPE; 837040030efSMarcel Holtmann sk->sk_state_change(sk); 838040030efSMarcel Holtmann } 839040030efSMarcel Holtmann } 840040030efSMarcel Holtmann read_unlock(&hci_sk_list.lock); 841040030efSMarcel Holtmann } 842040030efSMarcel Holtmann } 843040030efSMarcel Holtmann 844801c1e8dSJohan Hedberg static struct hci_mgmt_chan *__hci_mgmt_chan_find(unsigned short channel) 845801c1e8dSJohan Hedberg { 846801c1e8dSJohan Hedberg struct hci_mgmt_chan *c; 847801c1e8dSJohan Hedberg 848801c1e8dSJohan Hedberg list_for_each_entry(c, &mgmt_chan_list, list) { 849801c1e8dSJohan Hedberg if (c->channel == channel) 850801c1e8dSJohan Hedberg return c; 851801c1e8dSJohan Hedberg } 852801c1e8dSJohan Hedberg 853801c1e8dSJohan Hedberg return NULL; 854801c1e8dSJohan Hedberg } 855801c1e8dSJohan Hedberg 856801c1e8dSJohan Hedberg static struct hci_mgmt_chan *hci_mgmt_chan_find(unsigned short channel) 857801c1e8dSJohan Hedberg { 858801c1e8dSJohan Hedberg struct hci_mgmt_chan *c; 859801c1e8dSJohan Hedberg 860801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock); 861801c1e8dSJohan Hedberg c = __hci_mgmt_chan_find(channel); 862801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 863801c1e8dSJohan Hedberg 864801c1e8dSJohan Hedberg return c; 865801c1e8dSJohan Hedberg } 866801c1e8dSJohan Hedberg 867801c1e8dSJohan Hedberg int hci_mgmt_chan_register(struct hci_mgmt_chan *c) 868801c1e8dSJohan Hedberg { 869801c1e8dSJohan Hedberg if (c->channel < HCI_CHANNEL_CONTROL) 870801c1e8dSJohan Hedberg return -EINVAL; 871801c1e8dSJohan Hedberg 872801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock); 873801c1e8dSJohan Hedberg if (__hci_mgmt_chan_find(c->channel)) { 874801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 875801c1e8dSJohan Hedberg return -EALREADY; 876801c1e8dSJohan Hedberg } 877801c1e8dSJohan Hedberg 878801c1e8dSJohan Hedberg list_add_tail(&c->list, &mgmt_chan_list); 879801c1e8dSJohan Hedberg 880801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 881801c1e8dSJohan Hedberg 882801c1e8dSJohan Hedberg return 0; 883801c1e8dSJohan Hedberg } 884801c1e8dSJohan Hedberg EXPORT_SYMBOL(hci_mgmt_chan_register); 885801c1e8dSJohan Hedberg 886801c1e8dSJohan Hedberg void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c) 887801c1e8dSJohan Hedberg { 888801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock); 889801c1e8dSJohan Hedberg list_del(&c->list); 890801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 891801c1e8dSJohan Hedberg } 892801c1e8dSJohan Hedberg EXPORT_SYMBOL(hci_mgmt_chan_unregister); 893801c1e8dSJohan Hedberg 8941da177e4SLinus Torvalds static int hci_sock_release(struct socket *sock) 8951da177e4SLinus Torvalds { 8961da177e4SLinus Torvalds struct sock *sk = sock->sk; 8977b005bd3SMarcel Holtmann struct hci_dev *hdev; 898249fa169SMarcel Holtmann struct sk_buff *skb; 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds if (!sk) 9031da177e4SLinus Torvalds return 0; 9041da177e4SLinus Torvalds 90511eb85ecSDan Carpenter lock_sock(sk); 90611eb85ecSDan Carpenter 90770ecce91SMarcel Holtmann switch (hci_pi(sk)->channel) { 90870ecce91SMarcel Holtmann case HCI_CHANNEL_MONITOR: 909cd82e61cSMarcel Holtmann atomic_dec(&monitor_promisc); 91070ecce91SMarcel Holtmann break; 911f81f5b2dSMarcel Holtmann case HCI_CHANNEL_RAW: 912aa1638ddSMarcel Holtmann case HCI_CHANNEL_USER: 91370ecce91SMarcel Holtmann case HCI_CHANNEL_CONTROL: 914249fa169SMarcel Holtmann /* Send event to monitor */ 915249fa169SMarcel Holtmann skb = create_monitor_ctrl_close(sk); 916249fa169SMarcel Holtmann if (skb) { 917249fa169SMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 918249fa169SMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 919249fa169SMarcel Holtmann kfree_skb(skb); 920249fa169SMarcel Holtmann } 921249fa169SMarcel Holtmann 922df1cb87aSMarcel Holtmann hci_sock_free_cookie(sk); 92370ecce91SMarcel Holtmann break; 92470ecce91SMarcel Holtmann } 925cd82e61cSMarcel Holtmann 9261da177e4SLinus Torvalds bt_sock_unlink(&hci_sk_list, sk); 9271da177e4SLinus Torvalds 928e20a2e9cSMyungho Jung hdev = hci_pi(sk)->hdev; 9291da177e4SLinus Torvalds if (hdev) { 9300acef50bSAbhishek Pandit-Subedi if (hci_pi(sk)->channel == HCI_CHANNEL_USER && 9310acef50bSAbhishek Pandit-Subedi !hci_dev_test_flag(hdev, HCI_UNREGISTER)) { 9329332ef9dSMasahiro Yamada /* When releasing a user channel exclusive access, 9336b3cc1dbSSimon Fels * call hci_dev_do_close directly instead of calling 9346b3cc1dbSSimon Fels * hci_dev_close to ensure the exclusive access will 9356b3cc1dbSSimon Fels * be released and the controller brought back down. 9366b3cc1dbSSimon Fels * 9376b3cc1dbSSimon Fels * The checking of HCI_AUTO_OFF is not needed in this 9386b3cc1dbSSimon Fels * case since it will have been cleared already when 9396b3cc1dbSSimon Fels * opening the user channel. 9400acef50bSAbhishek Pandit-Subedi * 9410acef50bSAbhishek Pandit-Subedi * Make sure to also check that we haven't already 9420acef50bSAbhishek Pandit-Subedi * unregistered since all the cleanup will have already 9430acef50bSAbhishek Pandit-Subedi * been complete and hdev will get released when we put 9440acef50bSAbhishek Pandit-Subedi * below. 9456b3cc1dbSSimon Fels */ 9466b3cc1dbSSimon Fels hci_dev_do_close(hdev); 9479380f9eaSLoic Poulain hci_dev_clear_flag(hdev, HCI_USER_CHANNEL); 9489380f9eaSLoic Poulain mgmt_index_added(hdev); 94923500189SMarcel Holtmann } 95023500189SMarcel Holtmann 9511da177e4SLinus Torvalds atomic_dec(&hdev->promisc); 9521da177e4SLinus Torvalds hci_dev_put(hdev); 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds sock_orphan(sk); 95611eb85ecSDan Carpenter release_sock(sk); 9571da177e4SLinus Torvalds sock_put(sk); 9581da177e4SLinus Torvalds return 0; 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds 9613d4f9c00SArchie Pusaka static int hci_sock_reject_list_add(struct hci_dev *hdev, void __user *arg) 962f0358568SJohan Hedberg { 963f0358568SJohan Hedberg bdaddr_t bdaddr; 9645e762444SAntti Julku int err; 965f0358568SJohan Hedberg 966f0358568SJohan Hedberg if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) 967f0358568SJohan Hedberg return -EFAULT; 968f0358568SJohan Hedberg 96909fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 9705e762444SAntti Julku 9713d4f9c00SArchie Pusaka err = hci_bdaddr_list_add(&hdev->reject_list, &bdaddr, BDADDR_BREDR); 9725e762444SAntti Julku 97309fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 9745e762444SAntti Julku 9755e762444SAntti Julku return err; 976f0358568SJohan Hedberg } 977f0358568SJohan Hedberg 9783d4f9c00SArchie Pusaka static int hci_sock_reject_list_del(struct hci_dev *hdev, void __user *arg) 979f0358568SJohan Hedberg { 980f0358568SJohan Hedberg bdaddr_t bdaddr; 9815e762444SAntti Julku int err; 982f0358568SJohan Hedberg 983f0358568SJohan Hedberg if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) 984f0358568SJohan Hedberg return -EFAULT; 985f0358568SJohan Hedberg 98609fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 9875e762444SAntti Julku 9883d4f9c00SArchie Pusaka err = hci_bdaddr_list_del(&hdev->reject_list, &bdaddr, BDADDR_BREDR); 9895e762444SAntti Julku 99009fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 9915e762444SAntti Julku 9925e762444SAntti Julku return err; 993f0358568SJohan Hedberg } 994f0358568SJohan Hedberg 9951da177e4SLinus Torvalds /* Ioctls that require bound socket */ 9966039aa73SGustavo Padovan static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd, 9976039aa73SGustavo Padovan unsigned long arg) 9981da177e4SLinus Torvalds { 999e0448092STetsuo Handa struct hci_dev *hdev = hci_hdev_from_sock(sk); 10001da177e4SLinus Torvalds 1001e0448092STetsuo Handa if (IS_ERR(hdev)) 1002e0448092STetsuo Handa return PTR_ERR(hdev); 10031da177e4SLinus Torvalds 1004d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) 10050736cfa8SMarcel Holtmann return -EBUSY; 10060736cfa8SMarcel Holtmann 1007d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 1008fee746b0SMarcel Holtmann return -EOPNOTSUPP; 1009fee746b0SMarcel Holtmann 1010ca8bee5dSMarcel Holtmann if (hdev->dev_type != HCI_PRIMARY) 10115b69bef5SMarcel Holtmann return -EOPNOTSUPP; 10125b69bef5SMarcel Holtmann 10131da177e4SLinus Torvalds switch (cmd) { 10141da177e4SLinus Torvalds case HCISETRAW: 10151da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1016bf5b30b8SZhao Hongjiang return -EPERM; 1017db596681SMarcel Holtmann return -EOPNOTSUPP; 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds case HCIGETCONNINFO: 10201da177e4SLinus Torvalds return hci_get_conn_info(hdev, (void __user *)arg); 10211da177e4SLinus Torvalds 102240be492fSMarcel Holtmann case HCIGETAUTHINFO: 102340be492fSMarcel Holtmann return hci_get_auth_info(hdev, (void __user *)arg); 102440be492fSMarcel Holtmann 1025f0358568SJohan Hedberg case HCIBLOCKADDR: 1026f0358568SJohan Hedberg if (!capable(CAP_NET_ADMIN)) 1027bf5b30b8SZhao Hongjiang return -EPERM; 10283d4f9c00SArchie Pusaka return hci_sock_reject_list_add(hdev, (void __user *)arg); 1029f0358568SJohan Hedberg 1030f0358568SJohan Hedberg case HCIUNBLOCKADDR: 1031f0358568SJohan Hedberg if (!capable(CAP_NET_ADMIN)) 1032bf5b30b8SZhao Hongjiang return -EPERM; 10333d4f9c00SArchie Pusaka return hci_sock_reject_list_del(hdev, (void __user *)arg); 10340736cfa8SMarcel Holtmann } 1035f0358568SJohan Hedberg 1036324d36edSMarcel Holtmann return -ENOIOCTLCMD; 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds 10398fc9ced3SGustavo Padovan static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, 10408fc9ced3SGustavo Padovan unsigned long arg) 10411da177e4SLinus Torvalds { 10421da177e4SLinus Torvalds void __user *argp = (void __user *)arg; 10430736cfa8SMarcel Holtmann struct sock *sk = sock->sk; 10441da177e4SLinus Torvalds int err; 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds BT_DBG("cmd %x arg %lx", cmd, arg); 10471da177e4SLinus Torvalds 1048000c2fa2SRuihan Li /* Make sure the cmd is valid before doing anything */ 1049000c2fa2SRuihan Li switch (cmd) { 1050000c2fa2SRuihan Li case HCIGETDEVLIST: 1051000c2fa2SRuihan Li case HCIGETDEVINFO: 1052000c2fa2SRuihan Li case HCIGETCONNLIST: 1053000c2fa2SRuihan Li case HCIDEVUP: 1054000c2fa2SRuihan Li case HCIDEVDOWN: 1055000c2fa2SRuihan Li case HCIDEVRESET: 1056000c2fa2SRuihan Li case HCIDEVRESTAT: 1057000c2fa2SRuihan Li case HCISETSCAN: 1058000c2fa2SRuihan Li case HCISETAUTH: 1059000c2fa2SRuihan Li case HCISETENCRYPT: 1060000c2fa2SRuihan Li case HCISETPTYPE: 1061000c2fa2SRuihan Li case HCISETLINKPOL: 1062000c2fa2SRuihan Li case HCISETLINKMODE: 1063000c2fa2SRuihan Li case HCISETACLMTU: 1064000c2fa2SRuihan Li case HCISETSCOMTU: 1065000c2fa2SRuihan Li case HCIINQUIRY: 1066000c2fa2SRuihan Li case HCISETRAW: 1067000c2fa2SRuihan Li case HCIGETCONNINFO: 1068000c2fa2SRuihan Li case HCIGETAUTHINFO: 1069000c2fa2SRuihan Li case HCIBLOCKADDR: 1070000c2fa2SRuihan Li case HCIUNBLOCKADDR: 1071000c2fa2SRuihan Li break; 1072000c2fa2SRuihan Li default: 1073000c2fa2SRuihan Li return -ENOIOCTLCMD; 1074000c2fa2SRuihan Li } 1075000c2fa2SRuihan Li 1076c1c4f956SMarcel Holtmann lock_sock(sk); 1077c1c4f956SMarcel Holtmann 1078c1c4f956SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 1079c1c4f956SMarcel Holtmann err = -EBADFD; 1080c1c4f956SMarcel Holtmann goto done; 1081c1c4f956SMarcel Holtmann } 1082c1c4f956SMarcel Holtmann 1083f81f5b2dSMarcel Holtmann /* When calling an ioctl on an unbound raw socket, then ensure 1084f81f5b2dSMarcel Holtmann * that the monitor gets informed. Ensure that the resulting event 1085f81f5b2dSMarcel Holtmann * is only send once by checking if the cookie exists or not. The 1086f81f5b2dSMarcel Holtmann * socket cookie will be only ever generated once for the lifetime 1087f81f5b2dSMarcel Holtmann * of a given socket. 1088f81f5b2dSMarcel Holtmann */ 1089f81f5b2dSMarcel Holtmann if (hci_sock_gen_cookie(sk)) { 1090f81f5b2dSMarcel Holtmann struct sk_buff *skb; 1091f81f5b2dSMarcel Holtmann 109225c150acSRuihan Li /* Perform careful checks before setting the HCI_SOCK_TRUSTED 109325c150acSRuihan Li * flag. Make sure that not only the current task but also 109425c150acSRuihan Li * the socket opener has the required capability, since 109525c150acSRuihan Li * privileged programs can be tricked into making ioctl calls 109625c150acSRuihan Li * on HCI sockets, and the socket should not be marked as 109725c150acSRuihan Li * trusted simply because the ioctl caller is privileged. 109825c150acSRuihan Li */ 109925c150acSRuihan Li if (sk_capable(sk, CAP_NET_ADMIN)) 1100f81f5b2dSMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 1101f81f5b2dSMarcel Holtmann 1102f81f5b2dSMarcel Holtmann /* Send event to monitor */ 1103f81f5b2dSMarcel Holtmann skb = create_monitor_ctrl_open(sk); 1104f81f5b2dSMarcel Holtmann if (skb) { 1105f81f5b2dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1106f81f5b2dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1107f81f5b2dSMarcel Holtmann kfree_skb(skb); 1108f81f5b2dSMarcel Holtmann } 1109f81f5b2dSMarcel Holtmann } 1110f81f5b2dSMarcel Holtmann 1111c1c4f956SMarcel Holtmann release_sock(sk); 1112c1c4f956SMarcel Holtmann 11131da177e4SLinus Torvalds switch (cmd) { 11141da177e4SLinus Torvalds case HCIGETDEVLIST: 11151da177e4SLinus Torvalds return hci_get_dev_list(argp); 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds case HCIGETDEVINFO: 11181da177e4SLinus Torvalds return hci_get_dev_info(argp); 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds case HCIGETCONNLIST: 11211da177e4SLinus Torvalds return hci_get_conn_list(argp); 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds case HCIDEVUP: 11241da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1125bf5b30b8SZhao Hongjiang return -EPERM; 11261da177e4SLinus Torvalds return hci_dev_open(arg); 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds case HCIDEVDOWN: 11291da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1130bf5b30b8SZhao Hongjiang return -EPERM; 11311da177e4SLinus Torvalds return hci_dev_close(arg); 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds case HCIDEVRESET: 11341da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1135bf5b30b8SZhao Hongjiang return -EPERM; 11361da177e4SLinus Torvalds return hci_dev_reset(arg); 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds case HCIDEVRESTAT: 11391da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1140bf5b30b8SZhao Hongjiang return -EPERM; 11411da177e4SLinus Torvalds return hci_dev_reset_stat(arg); 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds case HCISETSCAN: 11441da177e4SLinus Torvalds case HCISETAUTH: 11451da177e4SLinus Torvalds case HCISETENCRYPT: 11461da177e4SLinus Torvalds case HCISETPTYPE: 11471da177e4SLinus Torvalds case HCISETLINKPOL: 11481da177e4SLinus Torvalds case HCISETLINKMODE: 11491da177e4SLinus Torvalds case HCISETACLMTU: 11501da177e4SLinus Torvalds case HCISETSCOMTU: 11511da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 1152bf5b30b8SZhao Hongjiang return -EPERM; 11531da177e4SLinus Torvalds return hci_dev_cmd(cmd, argp); 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds case HCIINQUIRY: 11561da177e4SLinus Torvalds return hci_inquiry(argp); 1157c1c4f956SMarcel Holtmann } 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds lock_sock(sk); 1160c1c4f956SMarcel Holtmann 11611da177e4SLinus Torvalds err = hci_sock_bound_ioctl(sk, cmd, arg); 1162c1c4f956SMarcel Holtmann 1163c1c4f956SMarcel Holtmann done: 11641da177e4SLinus Torvalds release_sock(sk); 11651da177e4SLinus Torvalds return err; 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds 11687a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT 11697a6038b3SArnd Bergmann static int hci_sock_compat_ioctl(struct socket *sock, unsigned int cmd, 11707a6038b3SArnd Bergmann unsigned long arg) 11717a6038b3SArnd Bergmann { 11727a6038b3SArnd Bergmann switch (cmd) { 11737a6038b3SArnd Bergmann case HCIDEVUP: 11747a6038b3SArnd Bergmann case HCIDEVDOWN: 11757a6038b3SArnd Bergmann case HCIDEVRESET: 11767a6038b3SArnd Bergmann case HCIDEVRESTAT: 11777a6038b3SArnd Bergmann return hci_sock_ioctl(sock, cmd, arg); 11787a6038b3SArnd Bergmann } 11797a6038b3SArnd Bergmann 11807a6038b3SArnd Bergmann return hci_sock_ioctl(sock, cmd, (unsigned long)compat_ptr(arg)); 11817a6038b3SArnd Bergmann } 11827a6038b3SArnd Bergmann #endif 11837a6038b3SArnd Bergmann 11848fc9ced3SGustavo Padovan static int hci_sock_bind(struct socket *sock, struct sockaddr *addr, 11858fc9ced3SGustavo Padovan int addr_len) 11861da177e4SLinus Torvalds { 11870381101fSJohan Hedberg struct sockaddr_hci haddr; 11881da177e4SLinus Torvalds struct sock *sk = sock->sk; 11891da177e4SLinus Torvalds struct hci_dev *hdev = NULL; 1190f4cdbb3fSMarcel Holtmann struct sk_buff *skb; 11910381101fSJohan Hedberg int len, err = 0; 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 11941da177e4SLinus Torvalds 11950381101fSJohan Hedberg if (!addr) 11960381101fSJohan Hedberg return -EINVAL; 11970381101fSJohan Hedberg 11980381101fSJohan Hedberg memset(&haddr, 0, sizeof(haddr)); 11990381101fSJohan Hedberg len = min_t(unsigned int, sizeof(haddr), addr_len); 12000381101fSJohan Hedberg memcpy(&haddr, addr, len); 12010381101fSJohan Hedberg 12020381101fSJohan Hedberg if (haddr.hci_family != AF_BLUETOOTH) 12030381101fSJohan Hedberg return -EINVAL; 12040381101fSJohan Hedberg 12051da177e4SLinus Torvalds lock_sock(sk); 12061da177e4SLinus Torvalds 1207e0448092STetsuo Handa /* Allow detaching from dead device and attaching to alive device, if 1208e0448092STetsuo Handa * the caller wants to re-bind (instead of close) this socket in 1209e0448092STetsuo Handa * response to hci_sock_dev_event(HCI_DEV_UNREG) notification. 1210e0448092STetsuo Handa */ 1211e0448092STetsuo Handa hdev = hci_pi(sk)->hdev; 1212e0448092STetsuo Handa if (hdev && hci_dev_test_flag(hdev, HCI_UNREGISTER)) { 1213e0448092STetsuo Handa hci_pi(sk)->hdev = NULL; 1214e0448092STetsuo Handa sk->sk_state = BT_OPEN; 1215e0448092STetsuo Handa hci_dev_put(hdev); 1216e0448092STetsuo Handa } 1217e0448092STetsuo Handa hdev = NULL; 1218e0448092STetsuo Handa 12197cc2ade2SMarcel Holtmann if (sk->sk_state == BT_BOUND) { 12207cc2ade2SMarcel Holtmann err = -EALREADY; 12217cc2ade2SMarcel Holtmann goto done; 12227cc2ade2SMarcel Holtmann } 12237cc2ade2SMarcel Holtmann 12247cc2ade2SMarcel Holtmann switch (haddr.hci_channel) { 12257cc2ade2SMarcel Holtmann case HCI_CHANNEL_RAW: 12267cc2ade2SMarcel Holtmann if (hci_pi(sk)->hdev) { 12271da177e4SLinus Torvalds err = -EALREADY; 12281da177e4SLinus Torvalds goto done; 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds 12310381101fSJohan Hedberg if (haddr.hci_dev != HCI_DEV_NONE) { 12320381101fSJohan Hedberg hdev = hci_dev_get(haddr.hci_dev); 123370f23020SAndrei Emeltchenko if (!hdev) { 12341da177e4SLinus Torvalds err = -ENODEV; 12351da177e4SLinus Torvalds goto done; 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds atomic_inc(&hdev->promisc); 12391da177e4SLinus Torvalds } 12401da177e4SLinus Torvalds 12415a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 1242f81f5b2dSMarcel Holtmann 1243f4cdbb3fSMarcel Holtmann if (!hci_sock_gen_cookie(sk)) { 1244f4cdbb3fSMarcel Holtmann /* In the case when a cookie has already been assigned, 1245f4cdbb3fSMarcel Holtmann * then there has been already an ioctl issued against 124691641b79SZheng Yongjun * an unbound socket and with that triggered an open 1247f4cdbb3fSMarcel Holtmann * notification. Send a close notification first to 1248f4cdbb3fSMarcel Holtmann * allow the state transition to bounded. 1249f81f5b2dSMarcel Holtmann */ 1250f4cdbb3fSMarcel Holtmann skb = create_monitor_ctrl_close(sk); 1251f4cdbb3fSMarcel Holtmann if (skb) { 1252f4cdbb3fSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1253f4cdbb3fSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1254f4cdbb3fSMarcel Holtmann kfree_skb(skb); 1255f4cdbb3fSMarcel Holtmann } 1256f4cdbb3fSMarcel Holtmann } 1257f81f5b2dSMarcel Holtmann 1258f81f5b2dSMarcel Holtmann if (capable(CAP_NET_ADMIN)) 1259f81f5b2dSMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 1260f81f5b2dSMarcel Holtmann 1261f4cdbb3fSMarcel Holtmann hci_pi(sk)->hdev = hdev; 1262f4cdbb3fSMarcel Holtmann 1263f81f5b2dSMarcel Holtmann /* Send event to monitor */ 1264f81f5b2dSMarcel Holtmann skb = create_monitor_ctrl_open(sk); 1265f81f5b2dSMarcel Holtmann if (skb) { 1266f81f5b2dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1267f81f5b2dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1268f81f5b2dSMarcel Holtmann kfree_skb(skb); 1269f81f5b2dSMarcel Holtmann } 12707cc2ade2SMarcel Holtmann break; 12717cc2ade2SMarcel Holtmann 127223500189SMarcel Holtmann case HCI_CHANNEL_USER: 127323500189SMarcel Holtmann if (hci_pi(sk)->hdev) { 127423500189SMarcel Holtmann err = -EALREADY; 127523500189SMarcel Holtmann goto done; 127623500189SMarcel Holtmann } 127723500189SMarcel Holtmann 127823500189SMarcel Holtmann if (haddr.hci_dev == HCI_DEV_NONE) { 127923500189SMarcel Holtmann err = -EINVAL; 128023500189SMarcel Holtmann goto done; 128123500189SMarcel Holtmann } 128223500189SMarcel Holtmann 128310a8b86fSMarcel Holtmann if (!capable(CAP_NET_ADMIN)) { 128423500189SMarcel Holtmann err = -EPERM; 128523500189SMarcel Holtmann goto done; 128623500189SMarcel Holtmann } 128723500189SMarcel Holtmann 128823500189SMarcel Holtmann hdev = hci_dev_get(haddr.hci_dev); 128923500189SMarcel Holtmann if (!hdev) { 129023500189SMarcel Holtmann err = -ENODEV; 129123500189SMarcel Holtmann goto done; 129223500189SMarcel Holtmann } 129323500189SMarcel Holtmann 1294781f899fSMarcel Holtmann if (test_bit(HCI_INIT, &hdev->flags) || 1295d7a5a11dSMarcel Holtmann hci_dev_test_flag(hdev, HCI_SETUP) || 1296781f899fSMarcel Holtmann hci_dev_test_flag(hdev, HCI_CONFIG) || 1297781f899fSMarcel Holtmann (!hci_dev_test_flag(hdev, HCI_AUTO_OFF) && 1298781f899fSMarcel Holtmann test_bit(HCI_UP, &hdev->flags))) { 129923500189SMarcel Holtmann err = -EBUSY; 130023500189SMarcel Holtmann hci_dev_put(hdev); 130123500189SMarcel Holtmann goto done; 130223500189SMarcel Holtmann } 130323500189SMarcel Holtmann 1304238be788SMarcel Holtmann if (hci_dev_test_and_set_flag(hdev, HCI_USER_CHANNEL)) { 130523500189SMarcel Holtmann err = -EUSERS; 130623500189SMarcel Holtmann hci_dev_put(hdev); 130723500189SMarcel Holtmann goto done; 130823500189SMarcel Holtmann } 130923500189SMarcel Holtmann 131023500189SMarcel Holtmann mgmt_index_removed(hdev); 131123500189SMarcel Holtmann 131223500189SMarcel Holtmann err = hci_dev_open(hdev->id); 131323500189SMarcel Holtmann if (err) { 1314781f899fSMarcel Holtmann if (err == -EALREADY) { 1315781f899fSMarcel Holtmann /* In case the transport is already up and 1316781f899fSMarcel Holtmann * running, clear the error here. 1317781f899fSMarcel Holtmann * 13189332ef9dSMasahiro Yamada * This can happen when opening a user 1319781f899fSMarcel Holtmann * channel and HCI_AUTO_OFF grace period 1320781f899fSMarcel Holtmann * is still active. 1321781f899fSMarcel Holtmann */ 1322781f899fSMarcel Holtmann err = 0; 1323781f899fSMarcel Holtmann } else { 1324a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_USER_CHANNEL); 1325c6521401SMarcel Holtmann mgmt_index_added(hdev); 132623500189SMarcel Holtmann hci_dev_put(hdev); 132723500189SMarcel Holtmann goto done; 132823500189SMarcel Holtmann } 1329781f899fSMarcel Holtmann } 133023500189SMarcel Holtmann 13315a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 1332aa1638ddSMarcel Holtmann 1333aa1638ddSMarcel Holtmann if (!hci_sock_gen_cookie(sk)) { 1334aa1638ddSMarcel Holtmann /* In the case when a cookie has already been assigned, 1335aa1638ddSMarcel Holtmann * this socket will transition from a raw socket into 13369332ef9dSMasahiro Yamada * a user channel socket. For a clean transition, send 1337aa1638ddSMarcel Holtmann * the close notification first. 1338aa1638ddSMarcel Holtmann */ 1339aa1638ddSMarcel Holtmann skb = create_monitor_ctrl_close(sk); 1340aa1638ddSMarcel Holtmann if (skb) { 1341aa1638ddSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1342aa1638ddSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1343aa1638ddSMarcel Holtmann kfree_skb(skb); 1344aa1638ddSMarcel Holtmann } 1345aa1638ddSMarcel Holtmann } 1346aa1638ddSMarcel Holtmann 1347aa1638ddSMarcel Holtmann /* The user channel is restricted to CAP_NET_ADMIN 1348aa1638ddSMarcel Holtmann * capabilities and with that implicitly trusted. 1349aa1638ddSMarcel Holtmann */ 1350aa1638ddSMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 1351aa1638ddSMarcel Holtmann 135223500189SMarcel Holtmann hci_pi(sk)->hdev = hdev; 13535a6d2cf5SMarcel Holtmann 1354aa1638ddSMarcel Holtmann /* Send event to monitor */ 1355aa1638ddSMarcel Holtmann skb = create_monitor_ctrl_open(sk); 1356aa1638ddSMarcel Holtmann if (skb) { 1357aa1638ddSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1358aa1638ddSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1359aa1638ddSMarcel Holtmann kfree_skb(skb); 1360aa1638ddSMarcel Holtmann } 1361aa1638ddSMarcel Holtmann 13625a6d2cf5SMarcel Holtmann atomic_inc(&hdev->promisc); 136323500189SMarcel Holtmann break; 136423500189SMarcel Holtmann 1365cd82e61cSMarcel Holtmann case HCI_CHANNEL_MONITOR: 1366cd82e61cSMarcel Holtmann if (haddr.hci_dev != HCI_DEV_NONE) { 1367cd82e61cSMarcel Holtmann err = -EINVAL; 1368cd82e61cSMarcel Holtmann goto done; 1369cd82e61cSMarcel Holtmann } 1370cd82e61cSMarcel Holtmann 1371cd82e61cSMarcel Holtmann if (!capable(CAP_NET_RAW)) { 1372cd82e61cSMarcel Holtmann err = -EPERM; 1373cd82e61cSMarcel Holtmann goto done; 1374cd82e61cSMarcel Holtmann } 1375cd82e61cSMarcel Holtmann 13765a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 13775a6d2cf5SMarcel Holtmann 137850ebc055SMarcel Holtmann /* The monitor interface is restricted to CAP_NET_RAW 137950ebc055SMarcel Holtmann * capabilities and with that implicitly trusted. 138050ebc055SMarcel Holtmann */ 138150ebc055SMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 138250ebc055SMarcel Holtmann 1383787b306cSJohannes Berg send_monitor_note(sk, "Linux version %s (%s)", 1384787b306cSJohannes Berg init_utsname()->release, 1385787b306cSJohannes Berg init_utsname()->machine); 13869e8305b3SMarcel Holtmann send_monitor_note(sk, "Bluetooth subsystem version %u.%u", 13879e8305b3SMarcel Holtmann BT_SUBSYS_VERSION, BT_SUBSYS_REVISION); 1388cd82e61cSMarcel Holtmann send_monitor_replay(sk); 1389249fa169SMarcel Holtmann send_monitor_control_replay(sk); 1390cd82e61cSMarcel Holtmann 1391cd82e61cSMarcel Holtmann atomic_inc(&monitor_promisc); 1392cd82e61cSMarcel Holtmann break; 1393cd82e61cSMarcel Holtmann 1394ac714949SMarcel Holtmann case HCI_CHANNEL_LOGGING: 1395ac714949SMarcel Holtmann if (haddr.hci_dev != HCI_DEV_NONE) { 1396ac714949SMarcel Holtmann err = -EINVAL; 1397ac714949SMarcel Holtmann goto done; 1398ac714949SMarcel Holtmann } 1399ac714949SMarcel Holtmann 1400ac714949SMarcel Holtmann if (!capable(CAP_NET_ADMIN)) { 1401ac714949SMarcel Holtmann err = -EPERM; 1402ac714949SMarcel Holtmann goto done; 1403ac714949SMarcel Holtmann } 14045a6d2cf5SMarcel Holtmann 14055a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 1406ac714949SMarcel Holtmann break; 1407ac714949SMarcel Holtmann 14087cc2ade2SMarcel Holtmann default: 1409801c1e8dSJohan Hedberg if (!hci_mgmt_chan_find(haddr.hci_channel)) { 14107cc2ade2SMarcel Holtmann err = -EINVAL; 14117cc2ade2SMarcel Holtmann goto done; 14127cc2ade2SMarcel Holtmann } 14137cc2ade2SMarcel Holtmann 1414801c1e8dSJohan Hedberg if (haddr.hci_dev != HCI_DEV_NONE) { 1415801c1e8dSJohan Hedberg err = -EINVAL; 1416801c1e8dSJohan Hedberg goto done; 1417801c1e8dSJohan Hedberg } 1418801c1e8dSJohan Hedberg 14191195fbb8SMarcel Holtmann /* Users with CAP_NET_ADMIN capabilities are allowed 14201195fbb8SMarcel Holtmann * access to all management commands and events. For 14211195fbb8SMarcel Holtmann * untrusted users the interface is restricted and 14221195fbb8SMarcel Holtmann * also only untrusted events are sent. 142350ebc055SMarcel Holtmann */ 14241195fbb8SMarcel Holtmann if (capable(CAP_NET_ADMIN)) 142550ebc055SMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); 142650ebc055SMarcel Holtmann 14275a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel; 14285a6d2cf5SMarcel Holtmann 1429f9207338SMarcel Holtmann /* At the moment the index and unconfigured index events 1430f9207338SMarcel Holtmann * are enabled unconditionally. Setting them on each 1431f9207338SMarcel Holtmann * socket when binding keeps this functionality. They 1432f9207338SMarcel Holtmann * however might be cleared later and then sending of these 1433f9207338SMarcel Holtmann * events will be disabled, but that is then intentional. 1434f6b7712eSMarcel Holtmann * 1435f6b7712eSMarcel Holtmann * This also enables generic events that are safe to be 1436f6b7712eSMarcel Holtmann * received by untrusted users. Example for such events 1437f6b7712eSMarcel Holtmann * are changes to settings, class of device, name etc. 1438f9207338SMarcel Holtmann */ 14395a6d2cf5SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_CONTROL) { 1440f4cdbb3fSMarcel Holtmann if (!hci_sock_gen_cookie(sk)) { 1441f4cdbb3fSMarcel Holtmann /* In the case when a cookie has already been 144291641b79SZheng Yongjun * assigned, this socket will transition from 1443f4cdbb3fSMarcel Holtmann * a raw socket into a control socket. To 144491641b79SZheng Yongjun * allow for a clean transition, send the 1445f4cdbb3fSMarcel Holtmann * close notification first. 1446f4cdbb3fSMarcel Holtmann */ 1447f4cdbb3fSMarcel Holtmann skb = create_monitor_ctrl_close(sk); 1448f4cdbb3fSMarcel Holtmann if (skb) { 1449f4cdbb3fSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1450f4cdbb3fSMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1451f4cdbb3fSMarcel Holtmann kfree_skb(skb); 1452f4cdbb3fSMarcel Holtmann } 1453f4cdbb3fSMarcel Holtmann } 145470ecce91SMarcel Holtmann 1455249fa169SMarcel Holtmann /* Send event to monitor */ 1456249fa169SMarcel Holtmann skb = create_monitor_ctrl_open(sk); 1457249fa169SMarcel Holtmann if (skb) { 1458249fa169SMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, 1459249fa169SMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 1460249fa169SMarcel Holtmann kfree_skb(skb); 1461249fa169SMarcel Holtmann } 1462249fa169SMarcel Holtmann 1463f9207338SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_INDEX_EVENTS); 1464f9207338SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); 14655504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_OPTION_EVENTS); 14665504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_SETTING_EVENTS); 14675504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS); 14685504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS); 1469f9207338SMarcel Holtmann } 1470801c1e8dSJohan Hedberg break; 1471801c1e8dSJohan Hedberg } 1472801c1e8dSJohan Hedberg 147309572fcaSLuiz Augusto von Dentz /* Default MTU to HCI_MAX_FRAME_SIZE if not set */ 147409572fcaSLuiz Augusto von Dentz if (!hci_pi(sk)->mtu) 147509572fcaSLuiz Augusto von Dentz hci_pi(sk)->mtu = HCI_MAX_FRAME_SIZE; 147609572fcaSLuiz Augusto von Dentz 14771da177e4SLinus Torvalds sk->sk_state = BT_BOUND; 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds done: 14801da177e4SLinus Torvalds release_sock(sk); 14811da177e4SLinus Torvalds return err; 14821da177e4SLinus Torvalds } 14831da177e4SLinus Torvalds 14848fc9ced3SGustavo Padovan static int hci_sock_getname(struct socket *sock, struct sockaddr *addr, 14859b2c45d4SDenys Vlasenko int peer) 14861da177e4SLinus Torvalds { 14871da177e4SLinus Torvalds struct sockaddr_hci *haddr = (struct sockaddr_hci *)addr; 14881da177e4SLinus Torvalds struct sock *sk = sock->sk; 14899d4b68b2SMarcel Holtmann struct hci_dev *hdev; 14909d4b68b2SMarcel Holtmann int err = 0; 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 14931da177e4SLinus Torvalds 149406f43cbcSMarcel Holtmann if (peer) 149506f43cbcSMarcel Holtmann return -EOPNOTSUPP; 149606f43cbcSMarcel Holtmann 14971da177e4SLinus Torvalds lock_sock(sk); 14981da177e4SLinus Torvalds 1499e0448092STetsuo Handa hdev = hci_hdev_from_sock(sk); 1500e0448092STetsuo Handa if (IS_ERR(hdev)) { 1501e0448092STetsuo Handa err = PTR_ERR(hdev); 15029d4b68b2SMarcel Holtmann goto done; 15039d4b68b2SMarcel Holtmann } 15049d4b68b2SMarcel Holtmann 15051da177e4SLinus Torvalds haddr->hci_family = AF_BLUETOOTH; 15067b005bd3SMarcel Holtmann haddr->hci_dev = hdev->id; 15079d4b68b2SMarcel Holtmann haddr->hci_channel= hci_pi(sk)->channel; 15089b2c45d4SDenys Vlasenko err = sizeof(*haddr); 15091da177e4SLinus Torvalds 15109d4b68b2SMarcel Holtmann done: 15111da177e4SLinus Torvalds release_sock(sk); 15129d4b68b2SMarcel Holtmann return err; 15131da177e4SLinus Torvalds } 15141da177e4SLinus Torvalds 15156039aa73SGustavo Padovan static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg, 15166039aa73SGustavo Padovan struct sk_buff *skb) 15171da177e4SLinus Torvalds { 151832929e1fSAlain Michaud __u8 mask = hci_pi(sk)->cmsg_mask; 15191da177e4SLinus Torvalds 15200d48d939SMarcel Holtmann if (mask & HCI_CMSG_DIR) { 15210d48d939SMarcel Holtmann int incoming = bt_cb(skb)->incoming; 15228fc9ced3SGustavo Padovan put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming), 15238fc9ced3SGustavo Padovan &incoming); 15240d48d939SMarcel Holtmann } 15251da177e4SLinus Torvalds 1526a61bbcf2SPatrick McHardy if (mask & HCI_CMSG_TSTAMP) { 1527f6e623a6SJohann Felix Soden #ifdef CONFIG_COMPAT 152813c6ee2aSDeepa Dinamani struct old_timeval32 ctv; 1529f6e623a6SJohann Felix Soden #endif 153013c6ee2aSDeepa Dinamani struct __kernel_old_timeval tv; 1531767c5eb5SMarcel Holtmann void *data; 1532767c5eb5SMarcel Holtmann int len; 1533a61bbcf2SPatrick McHardy 1534a61bbcf2SPatrick McHardy skb_get_timestamp(skb, &tv); 1535767c5eb5SMarcel Holtmann 15361da97f83SDavid S. Miller data = &tv; 15371da97f83SDavid S. Miller len = sizeof(tv); 15381da97f83SDavid S. Miller #ifdef CONFIG_COMPAT 1539da88cea1SH. J. Lu if (!COMPAT_USE_64BIT_TIME && 1540da88cea1SH. J. Lu (msg->msg_flags & MSG_CMSG_COMPAT)) { 1541767c5eb5SMarcel Holtmann ctv.tv_sec = tv.tv_sec; 1542767c5eb5SMarcel Holtmann ctv.tv_usec = tv.tv_usec; 1543767c5eb5SMarcel Holtmann data = &ctv; 1544767c5eb5SMarcel Holtmann len = sizeof(ctv); 1545767c5eb5SMarcel Holtmann } 15461da97f83SDavid S. Miller #endif 1547767c5eb5SMarcel Holtmann 1548767c5eb5SMarcel Holtmann put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data); 1549a61bbcf2SPatrick McHardy } 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds 15528528d3f7SMarcel Holtmann static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg, 15538528d3f7SMarcel Holtmann size_t len, int flags) 15541da177e4SLinus Torvalds { 155569ae5065SLuiz Augusto von Dentz struct scm_cookie scm; 15561da177e4SLinus Torvalds struct sock *sk = sock->sk; 15571da177e4SLinus Torvalds struct sk_buff *skb; 15581da177e4SLinus Torvalds int copied, err; 155983871f8cSDenis Kenzior unsigned int skblen; 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds BT_DBG("sock %p, sk %p", sock, sk); 15621da177e4SLinus Torvalds 1563d94a6104SMarcel Holtmann if (flags & MSG_OOB) 15641da177e4SLinus Torvalds return -EOPNOTSUPP; 15651da177e4SLinus Torvalds 1566ac714949SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_LOGGING) 1567ac714949SMarcel Holtmann return -EOPNOTSUPP; 1568ac714949SMarcel Holtmann 15691da177e4SLinus Torvalds if (sk->sk_state == BT_CLOSED) 15701da177e4SLinus Torvalds return 0; 15711da177e4SLinus Torvalds 1572f4b41f06SOliver Hartkopp skb = skb_recv_datagram(sk, flags, &err); 157370f23020SAndrei Emeltchenko if (!skb) 15741da177e4SLinus Torvalds return err; 15751da177e4SLinus Torvalds 157683871f8cSDenis Kenzior skblen = skb->len; 15771da177e4SLinus Torvalds copied = skb->len; 15781da177e4SLinus Torvalds if (len < copied) { 15791da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC; 15801da177e4SLinus Torvalds copied = len; 15811da177e4SLinus Torvalds } 15821da177e4SLinus Torvalds 1583badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 158451f3d02bSDavid S. Miller err = skb_copy_datagram_msg(skb, 0, msg, copied); 15851da177e4SLinus Torvalds 15863a208627SMarcel Holtmann switch (hci_pi(sk)->channel) { 15873a208627SMarcel Holtmann case HCI_CHANNEL_RAW: 15881da177e4SLinus Torvalds hci_sock_cmsg(sk, msg, skb); 15893a208627SMarcel Holtmann break; 159023500189SMarcel Holtmann case HCI_CHANNEL_USER: 1591cd82e61cSMarcel Holtmann case HCI_CHANNEL_MONITOR: 1592cd82e61cSMarcel Holtmann sock_recv_timestamp(msg, sk, skb); 1593cd82e61cSMarcel Holtmann break; 1594801c1e8dSJohan Hedberg default: 1595801c1e8dSJohan Hedberg if (hci_mgmt_chan_find(hci_pi(sk)->channel)) 1596801c1e8dSJohan Hedberg sock_recv_timestamp(msg, sk, skb); 1597801c1e8dSJohan Hedberg break; 15983a208627SMarcel Holtmann } 15991da177e4SLinus Torvalds 160069ae5065SLuiz Augusto von Dentz memset(&scm, 0, sizeof(scm)); 160169ae5065SLuiz Augusto von Dentz scm.creds = bt_cb(skb)->creds; 160269ae5065SLuiz Augusto von Dentz 16031da177e4SLinus Torvalds skb_free_datagram(sk, skb); 16041da177e4SLinus Torvalds 16054f34228bSLuiz Augusto von Dentz if (flags & MSG_TRUNC) 160683871f8cSDenis Kenzior copied = skblen; 160783871f8cSDenis Kenzior 160869ae5065SLuiz Augusto von Dentz scm_recv(sock, msg, &scm, flags); 160969ae5065SLuiz Augusto von Dentz 16101da177e4SLinus Torvalds return err ? : copied; 16111da177e4SLinus Torvalds } 16121da177e4SLinus Torvalds 161364ba2eb3SLuiz Augusto von Dentz static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk, 161464ba2eb3SLuiz Augusto von Dentz struct sk_buff *skb) 1615fa4335d7SJohan Hedberg { 1616fa4335d7SJohan Hedberg u8 *cp; 1617fa4335d7SJohan Hedberg struct mgmt_hdr *hdr; 1618fa4335d7SJohan Hedberg u16 opcode, index, len; 1619fa4335d7SJohan Hedberg struct hci_dev *hdev = NULL; 1620fa4335d7SJohan Hedberg const struct hci_mgmt_handler *handler; 1621fa4335d7SJohan Hedberg bool var_len, no_hdev; 1622fa4335d7SJohan Hedberg int err; 1623fa4335d7SJohan Hedberg 162464ba2eb3SLuiz Augusto von Dentz BT_DBG("got %d bytes", skb->len); 1625fa4335d7SJohan Hedberg 162664ba2eb3SLuiz Augusto von Dentz if (skb->len < sizeof(*hdr)) 1627fa4335d7SJohan Hedberg return -EINVAL; 1628fa4335d7SJohan Hedberg 162964ba2eb3SLuiz Augusto von Dentz hdr = (void *)skb->data; 1630fa4335d7SJohan Hedberg opcode = __le16_to_cpu(hdr->opcode); 1631fa4335d7SJohan Hedberg index = __le16_to_cpu(hdr->index); 1632fa4335d7SJohan Hedberg len = __le16_to_cpu(hdr->len); 1633fa4335d7SJohan Hedberg 163464ba2eb3SLuiz Augusto von Dentz if (len != skb->len - sizeof(*hdr)) { 1635fa4335d7SJohan Hedberg err = -EINVAL; 1636fa4335d7SJohan Hedberg goto done; 1637fa4335d7SJohan Hedberg } 1638fa4335d7SJohan Hedberg 163938ceaa00SMarcel Holtmann if (chan->channel == HCI_CHANNEL_CONTROL) { 164064ba2eb3SLuiz Augusto von Dentz struct sk_buff *cmd; 164138ceaa00SMarcel Holtmann 164238ceaa00SMarcel Holtmann /* Send event to monitor */ 164364ba2eb3SLuiz Augusto von Dentz cmd = create_monitor_ctrl_command(sk, index, opcode, len, 164464ba2eb3SLuiz Augusto von Dentz skb->data + sizeof(*hdr)); 164564ba2eb3SLuiz Augusto von Dentz if (cmd) { 164664ba2eb3SLuiz Augusto von Dentz hci_send_to_channel(HCI_CHANNEL_MONITOR, cmd, 164738ceaa00SMarcel Holtmann HCI_SOCK_TRUSTED, NULL); 164864ba2eb3SLuiz Augusto von Dentz kfree_skb(cmd); 164938ceaa00SMarcel Holtmann } 165038ceaa00SMarcel Holtmann } 165138ceaa00SMarcel Holtmann 1652fa4335d7SJohan Hedberg if (opcode >= chan->handler_count || 1653fa4335d7SJohan Hedberg chan->handlers[opcode].func == NULL) { 1654fa4335d7SJohan Hedberg BT_DBG("Unknown op %u", opcode); 1655fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1656fa4335d7SJohan Hedberg MGMT_STATUS_UNKNOWN_COMMAND); 1657fa4335d7SJohan Hedberg goto done; 1658fa4335d7SJohan Hedberg } 1659fa4335d7SJohan Hedberg 1660fa4335d7SJohan Hedberg handler = &chan->handlers[opcode]; 1661fa4335d7SJohan Hedberg 1662fa4335d7SJohan Hedberg if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) && 1663fa4335d7SJohan Hedberg !(handler->flags & HCI_MGMT_UNTRUSTED)) { 1664fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1665fa4335d7SJohan Hedberg MGMT_STATUS_PERMISSION_DENIED); 1666fa4335d7SJohan Hedberg goto done; 1667fa4335d7SJohan Hedberg } 1668fa4335d7SJohan Hedberg 1669fa4335d7SJohan Hedberg if (index != MGMT_INDEX_NONE) { 1670fa4335d7SJohan Hedberg hdev = hci_dev_get(index); 1671fa4335d7SJohan Hedberg if (!hdev) { 1672fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1673fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1674fa4335d7SJohan Hedberg goto done; 1675fa4335d7SJohan Hedberg } 1676fa4335d7SJohan Hedberg 1677fa4335d7SJohan Hedberg if (hci_dev_test_flag(hdev, HCI_SETUP) || 1678fa4335d7SJohan Hedberg hci_dev_test_flag(hdev, HCI_CONFIG) || 1679fa4335d7SJohan Hedberg hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 1680fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1681fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1682fa4335d7SJohan Hedberg goto done; 1683fa4335d7SJohan Hedberg } 1684fa4335d7SJohan Hedberg 1685fa4335d7SJohan Hedberg if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && 1686fa4335d7SJohan Hedberg !(handler->flags & HCI_MGMT_UNCONFIGURED)) { 1687fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1688fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1689fa4335d7SJohan Hedberg goto done; 1690fa4335d7SJohan Hedberg } 1691fa4335d7SJohan Hedberg } 1692fa4335d7SJohan Hedberg 1693d5cc6626SMarcel Holtmann if (!(handler->flags & HCI_MGMT_HDEV_OPTIONAL)) { 1694fa4335d7SJohan Hedberg no_hdev = (handler->flags & HCI_MGMT_NO_HDEV); 1695fa4335d7SJohan Hedberg if (no_hdev != !hdev) { 1696fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1697fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX); 1698fa4335d7SJohan Hedberg goto done; 1699fa4335d7SJohan Hedberg } 1700d5cc6626SMarcel Holtmann } 1701fa4335d7SJohan Hedberg 1702fa4335d7SJohan Hedberg var_len = (handler->flags & HCI_MGMT_VAR_LEN); 1703fa4335d7SJohan Hedberg if ((var_len && len < handler->data_len) || 1704fa4335d7SJohan Hedberg (!var_len && len != handler->data_len)) { 1705fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode, 1706fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_PARAMS); 1707fa4335d7SJohan Hedberg goto done; 1708fa4335d7SJohan Hedberg } 1709fa4335d7SJohan Hedberg 1710fa4335d7SJohan Hedberg if (hdev && chan->hdev_init) 1711fa4335d7SJohan Hedberg chan->hdev_init(sk, hdev); 1712fa4335d7SJohan Hedberg 171364ba2eb3SLuiz Augusto von Dentz cp = skb->data + sizeof(*hdr); 1714fa4335d7SJohan Hedberg 1715fa4335d7SJohan Hedberg err = handler->func(sk, hdev, cp, len); 1716fa4335d7SJohan Hedberg if (err < 0) 1717fa4335d7SJohan Hedberg goto done; 1718fa4335d7SJohan Hedberg 171964ba2eb3SLuiz Augusto von Dentz err = skb->len; 1720fa4335d7SJohan Hedberg 1721fa4335d7SJohan Hedberg done: 1722fa4335d7SJohan Hedberg if (hdev) 1723fa4335d7SJohan Hedberg hci_dev_put(hdev); 1724fa4335d7SJohan Hedberg 1725fa4335d7SJohan Hedberg return err; 1726fa4335d7SJohan Hedberg } 1727fa4335d7SJohan Hedberg 172864ba2eb3SLuiz Augusto von Dentz static int hci_logging_frame(struct sock *sk, struct sk_buff *skb, 172964ba2eb3SLuiz Augusto von Dentz unsigned int flags) 1730ac714949SMarcel Holtmann { 1731ac714949SMarcel Holtmann struct hci_mon_hdr *hdr; 1732ac714949SMarcel Holtmann struct hci_dev *hdev; 1733ac714949SMarcel Holtmann u16 index; 1734ac714949SMarcel Holtmann int err; 1735ac714949SMarcel Holtmann 1736ac714949SMarcel Holtmann /* The logging frame consists at minimum of the standard header, 1737ac714949SMarcel Holtmann * the priority byte, the ident length byte and at least one string 1738ac714949SMarcel Holtmann * terminator NUL byte. Anything shorter are invalid packets. 1739ac714949SMarcel Holtmann */ 174064ba2eb3SLuiz Augusto von Dentz if (skb->len < sizeof(*hdr) + 3) 1741ac714949SMarcel Holtmann return -EINVAL; 1742ac714949SMarcel Holtmann 1743ac714949SMarcel Holtmann hdr = (void *)skb->data; 1744ac714949SMarcel Holtmann 174564ba2eb3SLuiz Augusto von Dentz if (__le16_to_cpu(hdr->len) != skb->len - sizeof(*hdr)) 174664ba2eb3SLuiz Augusto von Dentz return -EINVAL; 1747ac714949SMarcel Holtmann 1748ac714949SMarcel Holtmann if (__le16_to_cpu(hdr->opcode) == 0x0000) { 1749ac714949SMarcel Holtmann __u8 priority = skb->data[sizeof(*hdr)]; 1750ac714949SMarcel Holtmann __u8 ident_len = skb->data[sizeof(*hdr) + 1]; 1751ac714949SMarcel Holtmann 1752ac714949SMarcel Holtmann /* Only the priorities 0-7 are valid and with that any other 1753ac714949SMarcel Holtmann * value results in an invalid packet. 1754ac714949SMarcel Holtmann * 1755ac714949SMarcel Holtmann * The priority byte is followed by an ident length byte and 1756ac714949SMarcel Holtmann * the NUL terminated ident string. Check that the ident 1757ac714949SMarcel Holtmann * length is not overflowing the packet and also that the 1758ac714949SMarcel Holtmann * ident string itself is NUL terminated. In case the ident 1759ac714949SMarcel Holtmann * length is zero, the length value actually doubles as NUL 1760ac714949SMarcel Holtmann * terminator identifier. 1761ac714949SMarcel Holtmann * 1762ac714949SMarcel Holtmann * The message follows the ident string (if present) and 1763ac714949SMarcel Holtmann * must be NUL terminated. Otherwise it is not a valid packet. 1764ac714949SMarcel Holtmann */ 176564ba2eb3SLuiz Augusto von Dentz if (priority > 7 || skb->data[skb->len - 1] != 0x00 || 176664ba2eb3SLuiz Augusto von Dentz ident_len > skb->len - sizeof(*hdr) - 3 || 176764ba2eb3SLuiz Augusto von Dentz skb->data[sizeof(*hdr) + ident_len + 1] != 0x00) 176864ba2eb3SLuiz Augusto von Dentz return -EINVAL; 1769ac714949SMarcel Holtmann } else { 177064ba2eb3SLuiz Augusto von Dentz return -EINVAL; 1771ac714949SMarcel Holtmann } 1772ac714949SMarcel Holtmann 1773ac714949SMarcel Holtmann index = __le16_to_cpu(hdr->index); 1774ac714949SMarcel Holtmann 1775ac714949SMarcel Holtmann if (index != MGMT_INDEX_NONE) { 1776ac714949SMarcel Holtmann hdev = hci_dev_get(index); 177764ba2eb3SLuiz Augusto von Dentz if (!hdev) 177864ba2eb3SLuiz Augusto von Dentz return -ENODEV; 1779ac714949SMarcel Holtmann } else { 1780ac714949SMarcel Holtmann hdev = NULL; 1781ac714949SMarcel Holtmann } 1782ac714949SMarcel Holtmann 1783ac714949SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_USER_LOGGING); 1784ac714949SMarcel Holtmann 1785ac714949SMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, HCI_SOCK_TRUSTED, NULL); 178664ba2eb3SLuiz Augusto von Dentz err = skb->len; 1787ac714949SMarcel Holtmann 1788ac714949SMarcel Holtmann if (hdev) 1789ac714949SMarcel Holtmann hci_dev_put(hdev); 1790ac714949SMarcel Holtmann 1791ac714949SMarcel Holtmann return err; 1792ac714949SMarcel Holtmann } 1793ac714949SMarcel Holtmann 17941b784140SYing Xue static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg, 17951b784140SYing Xue size_t len) 17961da177e4SLinus Torvalds { 17971da177e4SLinus Torvalds struct sock *sk = sock->sk; 1798801c1e8dSJohan Hedberg struct hci_mgmt_chan *chan; 17991da177e4SLinus Torvalds struct hci_dev *hdev; 18001da177e4SLinus Torvalds struct sk_buff *skb; 18011da177e4SLinus Torvalds int err; 18020b59e272STetsuo Handa const unsigned int flags = msg->msg_flags; 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk); 18051da177e4SLinus Torvalds 18060b59e272STetsuo Handa if (flags & MSG_OOB) 18071da177e4SLinus Torvalds return -EOPNOTSUPP; 18081da177e4SLinus Torvalds 18090b59e272STetsuo Handa if (flags & ~(MSG_DONTWAIT | MSG_NOSIGNAL | MSG_ERRQUEUE | MSG_CMSG_COMPAT)) 18101da177e4SLinus Torvalds return -EINVAL; 18111da177e4SLinus Torvalds 181209572fcaSLuiz Augusto von Dentz if (len < 4 || len > hci_pi(sk)->mtu) 18131da177e4SLinus Torvalds return -EINVAL; 18141da177e4SLinus Torvalds 181564ba2eb3SLuiz Augusto von Dentz skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0); 181664ba2eb3SLuiz Augusto von Dentz if (IS_ERR(skb)) 181764ba2eb3SLuiz Augusto von Dentz return PTR_ERR(skb); 18180b59e272STetsuo Handa 18191da177e4SLinus Torvalds lock_sock(sk); 18201da177e4SLinus Torvalds 18210381101fSJohan Hedberg switch (hci_pi(sk)->channel) { 18220381101fSJohan Hedberg case HCI_CHANNEL_RAW: 182323500189SMarcel Holtmann case HCI_CHANNEL_USER: 18240381101fSJohan Hedberg break; 1825cd82e61cSMarcel Holtmann case HCI_CHANNEL_MONITOR: 1826cd82e61cSMarcel Holtmann err = -EOPNOTSUPP; 182764ba2eb3SLuiz Augusto von Dentz goto drop; 1828ac714949SMarcel Holtmann case HCI_CHANNEL_LOGGING: 182964ba2eb3SLuiz Augusto von Dentz err = hci_logging_frame(sk, skb, flags); 183064ba2eb3SLuiz Augusto von Dentz goto drop; 18310381101fSJohan Hedberg default: 1832801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock); 1833801c1e8dSJohan Hedberg chan = __hci_mgmt_chan_find(hci_pi(sk)->channel); 1834801c1e8dSJohan Hedberg if (chan) 183564ba2eb3SLuiz Augusto von Dentz err = hci_mgmt_cmd(chan, sk, skb); 1836801c1e8dSJohan Hedberg else 18370381101fSJohan Hedberg err = -EINVAL; 1838801c1e8dSJohan Hedberg 1839801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock); 184064ba2eb3SLuiz Augusto von Dentz goto drop; 18410381101fSJohan Hedberg } 18420381101fSJohan Hedberg 1843e0448092STetsuo Handa hdev = hci_hdev_from_sock(sk); 1844e0448092STetsuo Handa if (IS_ERR(hdev)) { 1845e0448092STetsuo Handa err = PTR_ERR(hdev); 184664ba2eb3SLuiz Augusto von Dentz goto drop; 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds 18497e21addcSMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags)) { 18507e21addcSMarcel Holtmann err = -ENETDOWN; 185164ba2eb3SLuiz Augusto von Dentz goto drop; 18527e21addcSMarcel Holtmann } 18537e21addcSMarcel Holtmann 18548528d3f7SMarcel Holtmann hci_skb_pkt_type(skb) = skb->data[0]; 18551da177e4SLinus Torvalds skb_pull(skb, 1); 18561da177e4SLinus Torvalds 18571bc5ad16SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_USER) { 18581bc5ad16SMarcel Holtmann /* No permission check is needed for user channel 18591bc5ad16SMarcel Holtmann * since that gets enforced when binding the socket. 18601bc5ad16SMarcel Holtmann * 18611bc5ad16SMarcel Holtmann * However check that the packet type is valid. 18621bc5ad16SMarcel Holtmann */ 1863d79f34e3SMarcel Holtmann if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT && 1864d79f34e3SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT && 1865cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_SCODATA_PKT && 1866cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) { 18671bc5ad16SMarcel Holtmann err = -EINVAL; 18681bc5ad16SMarcel Holtmann goto drop; 18691bc5ad16SMarcel Holtmann } 18701bc5ad16SMarcel Holtmann 18711bc5ad16SMarcel Holtmann skb_queue_tail(&hdev->raw_q, skb); 18721bc5ad16SMarcel Holtmann queue_work(hdev->workqueue, &hdev->tx_work); 1873d79f34e3SMarcel Holtmann } else if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) { 187483985319SHarvey Harrison u16 opcode = get_unaligned_le16(skb->data); 18751da177e4SLinus Torvalds u16 ogf = hci_opcode_ogf(opcode); 18761da177e4SLinus Torvalds u16 ocf = hci_opcode_ocf(opcode); 18771da177e4SLinus Torvalds 18781da177e4SLinus Torvalds if (((ogf > HCI_SFLT_MAX_OGF) || 18793bb3c755SGustavo Padovan !hci_test_bit(ocf & HCI_FLT_OCF_BITS, 18803bb3c755SGustavo Padovan &hci_sec_filter.ocf_mask[ogf])) && 18811da177e4SLinus Torvalds !capable(CAP_NET_RAW)) { 18821da177e4SLinus Torvalds err = -EPERM; 18831da177e4SLinus Torvalds goto drop; 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds 18861982162bSMarcel Holtmann /* Since the opcode has already been extracted here, store 18871982162bSMarcel Holtmann * a copy of the value for later use by the drivers. 18881982162bSMarcel Holtmann */ 18891982162bSMarcel Holtmann hci_skb_opcode(skb) = opcode; 18901982162bSMarcel Holtmann 1891fee746b0SMarcel Holtmann if (ogf == 0x3f) { 18921da177e4SLinus Torvalds skb_queue_tail(&hdev->raw_q, skb); 18933eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work); 18941da177e4SLinus Torvalds } else { 189549c922bbSStephen Hemminger /* Stand-alone HCI commands must be flagged as 189611714b3dSJohan Hedberg * single-command requests. 189711714b3dSJohan Hedberg */ 189844d27137SJohan Hedberg bt_cb(skb)->hci.req_flags |= HCI_REQ_START; 189911714b3dSJohan Hedberg 19001da177e4SLinus Torvalds skb_queue_tail(&hdev->cmd_q, skb); 1901c347b765SGustavo F. Padovan queue_work(hdev->workqueue, &hdev->cmd_work); 19021da177e4SLinus Torvalds } 19031da177e4SLinus Torvalds } else { 19041da177e4SLinus Torvalds if (!capable(CAP_NET_RAW)) { 19051da177e4SLinus Torvalds err = -EPERM; 19061da177e4SLinus Torvalds goto drop; 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds 1909d79f34e3SMarcel Holtmann if (hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT && 1910cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_SCODATA_PKT && 1911cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) { 1912bb77543eSMarcel Holtmann err = -EINVAL; 1913bb77543eSMarcel Holtmann goto drop; 1914bb77543eSMarcel Holtmann } 1915bb77543eSMarcel Holtmann 19161da177e4SLinus Torvalds skb_queue_tail(&hdev->raw_q, skb); 19173eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work); 19181da177e4SLinus Torvalds } 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds err = len; 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds done: 19231da177e4SLinus Torvalds release_sock(sk); 19241da177e4SLinus Torvalds return err; 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds drop: 19271da177e4SLinus Torvalds kfree_skb(skb); 19281da177e4SLinus Torvalds goto done; 19291da177e4SLinus Torvalds } 19301da177e4SLinus Torvalds 193109572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt_old(struct socket *sock, int level, int optname, 1932a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int len) 19331da177e4SLinus Torvalds { 19341da177e4SLinus Torvalds struct hci_ufilter uf = { .opcode = 0 }; 19351da177e4SLinus Torvalds struct sock *sk = sock->sk; 19361da177e4SLinus Torvalds int err = 0, opt = 0; 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds BT_DBG("sk %p, opt %d", sk, optname); 19391da177e4SLinus Torvalds 19401da177e4SLinus Torvalds lock_sock(sk); 19411da177e4SLinus Torvalds 19422f39cdb7SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 1943c2371e80SMarcel Holtmann err = -EBADFD; 19442f39cdb7SMarcel Holtmann goto done; 19452f39cdb7SMarcel Holtmann } 19462f39cdb7SMarcel Holtmann 19471da177e4SLinus Torvalds switch (optname) { 19481da177e4SLinus Torvalds case HCI_DATA_DIR: 1949a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&opt, optval, sizeof(opt))) { 19501da177e4SLinus Torvalds err = -EFAULT; 19511da177e4SLinus Torvalds break; 19521da177e4SLinus Torvalds } 19531da177e4SLinus Torvalds 19541da177e4SLinus Torvalds if (opt) 19551da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR; 19561da177e4SLinus Torvalds else 19571da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR; 19581da177e4SLinus Torvalds break; 19591da177e4SLinus Torvalds 19601da177e4SLinus Torvalds case HCI_TIME_STAMP: 1961a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&opt, optval, sizeof(opt))) { 19621da177e4SLinus Torvalds err = -EFAULT; 19631da177e4SLinus Torvalds break; 19641da177e4SLinus Torvalds } 19651da177e4SLinus Torvalds 19661da177e4SLinus Torvalds if (opt) 19671da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP; 19681da177e4SLinus Torvalds else 19691da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP; 19701da177e4SLinus Torvalds break; 19711da177e4SLinus Torvalds 19721da177e4SLinus Torvalds case HCI_FILTER: 19730878b666SMarcel Holtmann { 19740878b666SMarcel Holtmann struct hci_filter *f = &hci_pi(sk)->filter; 19750878b666SMarcel Holtmann 19760878b666SMarcel Holtmann uf.type_mask = f->type_mask; 19770878b666SMarcel Holtmann uf.opcode = f->opcode; 19780878b666SMarcel Holtmann uf.event_mask[0] = *((u32 *) f->event_mask + 0); 19790878b666SMarcel Holtmann uf.event_mask[1] = *((u32 *) f->event_mask + 1); 19800878b666SMarcel Holtmann } 19810878b666SMarcel Holtmann 19821da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(uf)); 1983a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&uf, optval, len)) { 19841da177e4SLinus Torvalds err = -EFAULT; 19851da177e4SLinus Torvalds break; 19861da177e4SLinus Torvalds } 19871da177e4SLinus Torvalds 19881da177e4SLinus Torvalds if (!capable(CAP_NET_RAW)) { 19891da177e4SLinus Torvalds uf.type_mask &= hci_sec_filter.type_mask; 19901da177e4SLinus Torvalds uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0); 19911da177e4SLinus Torvalds uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1); 19921da177e4SLinus Torvalds } 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds { 19951da177e4SLinus Torvalds struct hci_filter *f = &hci_pi(sk)->filter; 19961da177e4SLinus Torvalds 19971da177e4SLinus Torvalds f->type_mask = uf.type_mask; 19981da177e4SLinus Torvalds f->opcode = uf.opcode; 19991da177e4SLinus Torvalds *((u32 *) f->event_mask + 0) = uf.event_mask[0]; 20001da177e4SLinus Torvalds *((u32 *) f->event_mask + 1) = uf.event_mask[1]; 20011da177e4SLinus Torvalds } 20021da177e4SLinus Torvalds break; 20031da177e4SLinus Torvalds 20041da177e4SLinus Torvalds default: 20051da177e4SLinus Torvalds err = -ENOPROTOOPT; 20061da177e4SLinus Torvalds break; 20071da177e4SLinus Torvalds } 20081da177e4SLinus Torvalds 20092f39cdb7SMarcel Holtmann done: 20101da177e4SLinus Torvalds release_sock(sk); 20111da177e4SLinus Torvalds return err; 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds 201409572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt(struct socket *sock, int level, int optname, 201509572fcaSLuiz Augusto von Dentz sockptr_t optval, unsigned int len) 201609572fcaSLuiz Augusto von Dentz { 201709572fcaSLuiz Augusto von Dentz struct sock *sk = sock->sk; 2018b9f9dbadSDan Carpenter int err = 0; 2019b9f9dbadSDan Carpenter u16 opt; 202009572fcaSLuiz Augusto von Dentz 202109572fcaSLuiz Augusto von Dentz BT_DBG("sk %p, opt %d", sk, optname); 202209572fcaSLuiz Augusto von Dentz 202309572fcaSLuiz Augusto von Dentz if (level == SOL_HCI) 202409572fcaSLuiz Augusto von Dentz return hci_sock_setsockopt_old(sock, level, optname, optval, 202509572fcaSLuiz Augusto von Dentz len); 202609572fcaSLuiz Augusto von Dentz 202709572fcaSLuiz Augusto von Dentz if (level != SOL_BLUETOOTH) 202809572fcaSLuiz Augusto von Dentz return -ENOPROTOOPT; 202909572fcaSLuiz Augusto von Dentz 203009572fcaSLuiz Augusto von Dentz lock_sock(sk); 203109572fcaSLuiz Augusto von Dentz 203209572fcaSLuiz Augusto von Dentz switch (optname) { 203309572fcaSLuiz Augusto von Dentz case BT_SNDMTU: 203409572fcaSLuiz Augusto von Dentz case BT_RCVMTU: 203509572fcaSLuiz Augusto von Dentz switch (hci_pi(sk)->channel) { 203609572fcaSLuiz Augusto von Dentz /* Don't allow changing MTU for channels that are meant for HCI 203709572fcaSLuiz Augusto von Dentz * traffic only. 203809572fcaSLuiz Augusto von Dentz */ 203909572fcaSLuiz Augusto von Dentz case HCI_CHANNEL_RAW: 204009572fcaSLuiz Augusto von Dentz case HCI_CHANNEL_USER: 204109572fcaSLuiz Augusto von Dentz err = -ENOPROTOOPT; 204209572fcaSLuiz Augusto von Dentz goto done; 204309572fcaSLuiz Augusto von Dentz } 204409572fcaSLuiz Augusto von Dentz 2045b9f9dbadSDan Carpenter if (copy_from_sockptr(&opt, optval, sizeof(opt))) { 204609572fcaSLuiz Augusto von Dentz err = -EFAULT; 204709572fcaSLuiz Augusto von Dentz break; 204809572fcaSLuiz Augusto von Dentz } 204909572fcaSLuiz Augusto von Dentz 205009572fcaSLuiz Augusto von Dentz hci_pi(sk)->mtu = opt; 205109572fcaSLuiz Augusto von Dentz break; 205209572fcaSLuiz Augusto von Dentz 205309572fcaSLuiz Augusto von Dentz default: 205409572fcaSLuiz Augusto von Dentz err = -ENOPROTOOPT; 205509572fcaSLuiz Augusto von Dentz break; 205609572fcaSLuiz Augusto von Dentz } 205709572fcaSLuiz Augusto von Dentz 205809572fcaSLuiz Augusto von Dentz done: 205909572fcaSLuiz Augusto von Dentz release_sock(sk); 206009572fcaSLuiz Augusto von Dentz return err; 206109572fcaSLuiz Augusto von Dentz } 206209572fcaSLuiz Augusto von Dentz 206309572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt_old(struct socket *sock, int level, int optname, 20648fc9ced3SGustavo Padovan char __user *optval, int __user *optlen) 20651da177e4SLinus Torvalds { 20661da177e4SLinus Torvalds struct hci_ufilter uf; 20671da177e4SLinus Torvalds struct sock *sk = sock->sk; 2068cedc5469SMarcel Holtmann int len, opt, err = 0; 2069cedc5469SMarcel Holtmann 2070cedc5469SMarcel Holtmann BT_DBG("sk %p, opt %d", sk, optname); 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds if (get_user(len, optlen)) 20731da177e4SLinus Torvalds return -EFAULT; 20741da177e4SLinus Torvalds 2075cedc5469SMarcel Holtmann lock_sock(sk); 2076cedc5469SMarcel Holtmann 2077cedc5469SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 2078c2371e80SMarcel Holtmann err = -EBADFD; 2079cedc5469SMarcel Holtmann goto done; 2080cedc5469SMarcel Holtmann } 2081cedc5469SMarcel Holtmann 20821da177e4SLinus Torvalds switch (optname) { 20831da177e4SLinus Torvalds case HCI_DATA_DIR: 20841da177e4SLinus Torvalds if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR) 20851da177e4SLinus Torvalds opt = 1; 20861da177e4SLinus Torvalds else 20871da177e4SLinus Torvalds opt = 0; 20881da177e4SLinus Torvalds 20891da177e4SLinus Torvalds if (put_user(opt, optval)) 2090cedc5469SMarcel Holtmann err = -EFAULT; 20911da177e4SLinus Torvalds break; 20921da177e4SLinus Torvalds 20931da177e4SLinus Torvalds case HCI_TIME_STAMP: 20941da177e4SLinus Torvalds if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP) 20951da177e4SLinus Torvalds opt = 1; 20961da177e4SLinus Torvalds else 20971da177e4SLinus Torvalds opt = 0; 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds if (put_user(opt, optval)) 2100cedc5469SMarcel Holtmann err = -EFAULT; 21011da177e4SLinus Torvalds break; 21021da177e4SLinus Torvalds 21031da177e4SLinus Torvalds case HCI_FILTER: 21041da177e4SLinus Torvalds { 21051da177e4SLinus Torvalds struct hci_filter *f = &hci_pi(sk)->filter; 21061da177e4SLinus Torvalds 2107e15ca9a0SMathias Krause memset(&uf, 0, sizeof(uf)); 21081da177e4SLinus Torvalds uf.type_mask = f->type_mask; 21091da177e4SLinus Torvalds uf.opcode = f->opcode; 21101da177e4SLinus Torvalds uf.event_mask[0] = *((u32 *) f->event_mask + 0); 21111da177e4SLinus Torvalds uf.event_mask[1] = *((u32 *) f->event_mask + 1); 21121da177e4SLinus Torvalds } 21131da177e4SLinus Torvalds 21141da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(uf)); 21151da177e4SLinus Torvalds if (copy_to_user(optval, &uf, len)) 2116cedc5469SMarcel Holtmann err = -EFAULT; 21171da177e4SLinus Torvalds break; 21181da177e4SLinus Torvalds 21191da177e4SLinus Torvalds default: 2120cedc5469SMarcel Holtmann err = -ENOPROTOOPT; 21211da177e4SLinus Torvalds break; 21221da177e4SLinus Torvalds } 21231da177e4SLinus Torvalds 2124cedc5469SMarcel Holtmann done: 2125cedc5469SMarcel Holtmann release_sock(sk); 2126cedc5469SMarcel Holtmann return err; 21271da177e4SLinus Torvalds } 21281da177e4SLinus Torvalds 212909572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt(struct socket *sock, int level, int optname, 213009572fcaSLuiz Augusto von Dentz char __user *optval, int __user *optlen) 213109572fcaSLuiz Augusto von Dentz { 213209572fcaSLuiz Augusto von Dentz struct sock *sk = sock->sk; 213309572fcaSLuiz Augusto von Dentz int err = 0; 213409572fcaSLuiz Augusto von Dentz 213509572fcaSLuiz Augusto von Dentz BT_DBG("sk %p, opt %d", sk, optname); 213609572fcaSLuiz Augusto von Dentz 213709572fcaSLuiz Augusto von Dentz if (level == SOL_HCI) 213809572fcaSLuiz Augusto von Dentz return hci_sock_getsockopt_old(sock, level, optname, optval, 213909572fcaSLuiz Augusto von Dentz optlen); 214009572fcaSLuiz Augusto von Dentz 214109572fcaSLuiz Augusto von Dentz if (level != SOL_BLUETOOTH) 214209572fcaSLuiz Augusto von Dentz return -ENOPROTOOPT; 214309572fcaSLuiz Augusto von Dentz 214409572fcaSLuiz Augusto von Dentz lock_sock(sk); 214509572fcaSLuiz Augusto von Dentz 214609572fcaSLuiz Augusto von Dentz switch (optname) { 214709572fcaSLuiz Augusto von Dentz case BT_SNDMTU: 214809572fcaSLuiz Augusto von Dentz case BT_RCVMTU: 214909572fcaSLuiz Augusto von Dentz if (put_user(hci_pi(sk)->mtu, (u16 __user *)optval)) 215009572fcaSLuiz Augusto von Dentz err = -EFAULT; 215109572fcaSLuiz Augusto von Dentz break; 215209572fcaSLuiz Augusto von Dentz 215309572fcaSLuiz Augusto von Dentz default: 215409572fcaSLuiz Augusto von Dentz err = -ENOPROTOOPT; 215509572fcaSLuiz Augusto von Dentz break; 215609572fcaSLuiz Augusto von Dentz } 215709572fcaSLuiz Augusto von Dentz 215809572fcaSLuiz Augusto von Dentz release_sock(sk); 215909572fcaSLuiz Augusto von Dentz return err; 216009572fcaSLuiz Augusto von Dentz } 216109572fcaSLuiz Augusto von Dentz 2162709fca50SNguyen Dinh Phi static void hci_sock_destruct(struct sock *sk) 2163709fca50SNguyen Dinh Phi { 2164b338d917SBrian Gix mgmt_cleanup(sk); 2165709fca50SNguyen Dinh Phi skb_queue_purge(&sk->sk_receive_queue); 2166709fca50SNguyen Dinh Phi skb_queue_purge(&sk->sk_write_queue); 2167709fca50SNguyen Dinh Phi } 2168709fca50SNguyen Dinh Phi 216990ddc4f0SEric Dumazet static const struct proto_ops hci_sock_ops = { 21701da177e4SLinus Torvalds .family = PF_BLUETOOTH, 21711da177e4SLinus Torvalds .owner = THIS_MODULE, 21721da177e4SLinus Torvalds .release = hci_sock_release, 21731da177e4SLinus Torvalds .bind = hci_sock_bind, 21741da177e4SLinus Torvalds .getname = hci_sock_getname, 21751da177e4SLinus Torvalds .sendmsg = hci_sock_sendmsg, 21761da177e4SLinus Torvalds .recvmsg = hci_sock_recvmsg, 21771da177e4SLinus Torvalds .ioctl = hci_sock_ioctl, 21787a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT 21797a6038b3SArnd Bergmann .compat_ioctl = hci_sock_compat_ioctl, 21807a6038b3SArnd Bergmann #endif 2181a11e1d43SLinus Torvalds .poll = datagram_poll, 21821da177e4SLinus Torvalds .listen = sock_no_listen, 21831da177e4SLinus Torvalds .shutdown = sock_no_shutdown, 21841da177e4SLinus Torvalds .setsockopt = hci_sock_setsockopt, 21851da177e4SLinus Torvalds .getsockopt = hci_sock_getsockopt, 21861da177e4SLinus Torvalds .connect = sock_no_connect, 21871da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 21881da177e4SLinus Torvalds .accept = sock_no_accept, 21891da177e4SLinus Torvalds .mmap = sock_no_mmap 21901da177e4SLinus Torvalds }; 21911da177e4SLinus Torvalds 21921da177e4SLinus Torvalds static struct proto hci_sk_proto = { 21931da177e4SLinus Torvalds .name = "HCI", 21941da177e4SLinus Torvalds .owner = THIS_MODULE, 21951da177e4SLinus Torvalds .obj_size = sizeof(struct hci_pinfo) 21961da177e4SLinus Torvalds }; 21971da177e4SLinus Torvalds 21983f378b68SEric Paris static int hci_sock_create(struct net *net, struct socket *sock, int protocol, 21993f378b68SEric Paris int kern) 22001da177e4SLinus Torvalds { 22011da177e4SLinus Torvalds struct sock *sk; 22021da177e4SLinus Torvalds 22031da177e4SLinus Torvalds BT_DBG("sock %p", sock); 22041da177e4SLinus Torvalds 22051da177e4SLinus Torvalds if (sock->type != SOCK_RAW) 22061da177e4SLinus Torvalds return -ESOCKTNOSUPPORT; 22071da177e4SLinus Torvalds 22081da177e4SLinus Torvalds sock->ops = &hci_sock_ops; 22091da177e4SLinus Torvalds 22106bfa273eSLuiz Augusto von Dentz sk = bt_sock_alloc(net, sock, &hci_sk_proto, protocol, GFP_ATOMIC, 22116bfa273eSLuiz Augusto von Dentz kern); 22121da177e4SLinus Torvalds if (!sk) 22131da177e4SLinus Torvalds return -ENOMEM; 22141da177e4SLinus Torvalds 22151da177e4SLinus Torvalds sock->state = SS_UNCONNECTED; 2216709fca50SNguyen Dinh Phi sk->sk_destruct = hci_sock_destruct; 22171da177e4SLinus Torvalds 22181da177e4SLinus Torvalds bt_sock_link(&hci_sk_list, sk); 22191da177e4SLinus Torvalds return 0; 22201da177e4SLinus Torvalds } 22211da177e4SLinus Torvalds 2222ec1b4cf7SStephen Hemminger static const struct net_proto_family hci_sock_family_ops = { 22231da177e4SLinus Torvalds .family = PF_BLUETOOTH, 22241da177e4SLinus Torvalds .owner = THIS_MODULE, 22251da177e4SLinus Torvalds .create = hci_sock_create, 22261da177e4SLinus Torvalds }; 22271da177e4SLinus Torvalds 22281da177e4SLinus Torvalds int __init hci_sock_init(void) 22291da177e4SLinus Torvalds { 22301da177e4SLinus Torvalds int err; 22311da177e4SLinus Torvalds 2232b0a8e282SMarcel Holtmann BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr)); 2233b0a8e282SMarcel Holtmann 22341da177e4SLinus Torvalds err = proto_register(&hci_sk_proto, 0); 22351da177e4SLinus Torvalds if (err < 0) 22361da177e4SLinus Torvalds return err; 22371da177e4SLinus Torvalds 22381da177e4SLinus Torvalds err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops); 2239f7c86637SMasatake YAMATO if (err < 0) { 2240f7c86637SMasatake YAMATO BT_ERR("HCI socket registration failed"); 22411da177e4SLinus Torvalds goto error; 2242f7c86637SMasatake YAMATO } 2243f7c86637SMasatake YAMATO 2244b0316615SAl Viro err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL); 2245f7c86637SMasatake YAMATO if (err < 0) { 2246f7c86637SMasatake YAMATO BT_ERR("Failed to create HCI proc file"); 2247f7c86637SMasatake YAMATO bt_sock_unregister(BTPROTO_HCI); 2248f7c86637SMasatake YAMATO goto error; 2249f7c86637SMasatake YAMATO } 22501da177e4SLinus Torvalds 22511da177e4SLinus Torvalds BT_INFO("HCI socket layer initialized"); 22521da177e4SLinus Torvalds 22531da177e4SLinus Torvalds return 0; 22541da177e4SLinus Torvalds 22551da177e4SLinus Torvalds error: 22561da177e4SLinus Torvalds proto_unregister(&hci_sk_proto); 22571da177e4SLinus Torvalds return err; 22581da177e4SLinus Torvalds } 22591da177e4SLinus Torvalds 2260b7440a14SAnand Gadiyar void hci_sock_cleanup(void) 22611da177e4SLinus Torvalds { 2262f7c86637SMasatake YAMATO bt_procfs_cleanup(&init_net, "hci"); 22635e9d7f86SDavid Herrmann bt_sock_unregister(BTPROTO_HCI); 22641da177e4SLinus Torvalds proto_unregister(&hci_sk_proto); 22651da177e4SLinus Torvalds } 2266