YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2000-2001 Qualcomm Incorporated |
| 4 | |
| 5 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License version 2 as |
| 9 | published by the Free Software Foundation; |
| 10 | |
| 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth HCI sockets. */ |
| 26 | |
Gustavo Padovan | 8c520a5 | 2012-05-23 04:04:22 -0300 | [diff] [blame] | 27 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
| 29 | |
| 30 | #include <net/bluetooth/bluetooth.h> |
| 31 | #include <net/bluetooth/hci_core.h> |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 32 | #include <net/bluetooth/hci_mon.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Johan Hedberg | 801c1e8 | 2015-03-06 21:08:50 +0200 | [diff] [blame] | 34 | static LIST_HEAD(mgmt_chan_list); |
| 35 | static DEFINE_MUTEX(mgmt_chan_list_lock); |
| 36 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 37 | static atomic_t monitor_promisc = ATOMIC_INIT(0); |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | /* ----- HCI socket interface ----- */ |
| 40 | |
Marcel Holtmann | 863def5 | 2014-07-11 05:41:00 +0200 | [diff] [blame] | 41 | /* Socket info */ |
| 42 | #define hci_pi(sk) ((struct hci_pinfo *) sk) |
| 43 | |
| 44 | struct hci_pinfo { |
| 45 | struct bt_sock bt; |
| 46 | struct hci_dev *hdev; |
| 47 | struct hci_filter filter; |
| 48 | __u32 cmsg_mask; |
| 49 | unsigned short channel; |
| 50 | }; |
| 51 | |
Jiri Slaby | 9391976 | 2015-02-19 15:20:43 +0100 | [diff] [blame] | 52 | static inline int hci_test_bit(int nr, const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { |
Jiri Slaby | 9391976 | 2015-02-19 15:20:43 +0100 | [diff] [blame] | 54 | return *((const __u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* Security filter */ |
Marcel Holtmann | 3ad254f | 2014-07-11 05:36:39 +0200 | [diff] [blame] | 58 | #define HCI_SFLT_MAX_OGF 5 |
| 59 | |
| 60 | struct hci_sec_filter { |
| 61 | __u32 type_mask; |
| 62 | __u32 event_mask[2]; |
| 63 | __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4]; |
| 64 | }; |
| 65 | |
Marcel Holtmann | 7e67c11 | 2014-07-11 05:36:40 +0200 | [diff] [blame] | 66 | static const struct hci_sec_filter hci_sec_filter = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* Packet types */ |
| 68 | 0x10, |
| 69 | /* Events */ |
Marcel Holtmann | dd7f552 | 2005-10-28 19:20:53 +0200 | [diff] [blame] | 70 | { 0x1000d9fe, 0x0000b00c }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | /* Commands */ |
| 72 | { |
| 73 | { 0x0 }, |
| 74 | /* OGF_LINK_CTL */ |
Marcel Holtmann | 7c631a6 | 2007-09-09 08:39:43 +0200 | [diff] [blame] | 75 | { 0xbe000006, 0x00000001, 0x00000000, 0x00 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | /* OGF_LINK_POLICY */ |
Marcel Holtmann | 7c631a6 | 2007-09-09 08:39:43 +0200 | [diff] [blame] | 77 | { 0x00005200, 0x00000000, 0x00000000, 0x00 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | /* OGF_HOST_CTL */ |
Marcel Holtmann | 7c631a6 | 2007-09-09 08:39:43 +0200 | [diff] [blame] | 79 | { 0xaab00200, 0x2b402aaa, 0x05220154, 0x00 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /* OGF_INFO_PARAM */ |
Marcel Holtmann | 7c631a6 | 2007-09-09 08:39:43 +0200 | [diff] [blame] | 81 | { 0x000002be, 0x00000000, 0x00000000, 0x00 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | /* OGF_STATUS_PARAM */ |
Marcel Holtmann | 7c631a6 | 2007-09-09 08:39:43 +0200 | [diff] [blame] | 83 | { 0x000000ea, 0x00000000, 0x00000000, 0x00 } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | }; |
| 86 | |
| 87 | static struct bt_sock_list hci_sk_list = { |
Robert P. J. Day | d5fb296 | 2008-03-28 16:17:38 -0700 | [diff] [blame] | 88 | .lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
Marcel Holtmann | f81fe64 | 2013-08-25 23:25:15 -0700 | [diff] [blame] | 91 | static bool is_filtered_packet(struct sock *sk, struct sk_buff *skb) |
| 92 | { |
| 93 | struct hci_filter *flt; |
| 94 | int flt_type, flt_event; |
| 95 | |
| 96 | /* Apply filter */ |
| 97 | flt = &hci_pi(sk)->filter; |
| 98 | |
| 99 | if (bt_cb(skb)->pkt_type == HCI_VENDOR_PKT) |
| 100 | flt_type = 0; |
| 101 | else |
| 102 | flt_type = bt_cb(skb)->pkt_type & HCI_FLT_TYPE_BITS; |
| 103 | |
| 104 | if (!test_bit(flt_type, &flt->type_mask)) |
| 105 | return true; |
| 106 | |
| 107 | /* Extra filter for event packets only */ |
| 108 | if (bt_cb(skb)->pkt_type != HCI_EVENT_PKT) |
| 109 | return false; |
| 110 | |
| 111 | flt_event = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS); |
| 112 | |
| 113 | if (!hci_test_bit(flt_event, &flt->event_mask)) |
| 114 | return true; |
| 115 | |
| 116 | /* Check filter only when opcode is set */ |
| 117 | if (!flt->opcode) |
| 118 | return false; |
| 119 | |
| 120 | if (flt_event == HCI_EV_CMD_COMPLETE && |
| 121 | flt->opcode != get_unaligned((__le16 *)(skb->data + 3))) |
| 122 | return true; |
| 123 | |
| 124 | if (flt_event == HCI_EV_CMD_STATUS && |
| 125 | flt->opcode != get_unaligned((__le16 *)(skb->data + 4))) |
| 126 | return true; |
| 127 | |
| 128 | return false; |
| 129 | } |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | /* Send frame to RAW socket */ |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 132 | void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | struct sock *sk; |
Marcel Holtmann | e0edf37 | 2012-02-20 14:50:36 +0100 | [diff] [blame] | 135 | struct sk_buff *skb_copy = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | BT_DBG("hdev %p len %d", hdev, skb->len); |
| 138 | |
| 139 | read_lock(&hci_sk_list.lock); |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 140 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 141 | sk_for_each(sk, &hci_sk_list.head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | struct sk_buff *nskb; |
| 143 | |
| 144 | if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev) |
| 145 | continue; |
| 146 | |
| 147 | /* Don't send frame to the socket it came from */ |
| 148 | if (skb->sk == sk) |
| 149 | continue; |
| 150 | |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 151 | if (hci_pi(sk)->channel == HCI_CHANNEL_RAW) { |
| 152 | if (is_filtered_packet(sk, skb)) |
| 153 | continue; |
| 154 | } else if (hci_pi(sk)->channel == HCI_CHANNEL_USER) { |
| 155 | if (!bt_cb(skb)->incoming) |
| 156 | continue; |
| 157 | if (bt_cb(skb)->pkt_type != HCI_EVENT_PKT && |
| 158 | bt_cb(skb)->pkt_type != HCI_ACLDATA_PKT && |
| 159 | bt_cb(skb)->pkt_type != HCI_SCODATA_PKT) |
| 160 | continue; |
| 161 | } else { |
| 162 | /* Don't send frame to other channel types */ |
Johan Hedberg | a40c406 | 2010-12-08 00:21:07 +0200 | [diff] [blame] | 163 | continue; |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Marcel Holtmann | e0edf37 | 2012-02-20 14:50:36 +0100 | [diff] [blame] | 166 | if (!skb_copy) { |
| 167 | /* Create a private copy with headroom */ |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 168 | skb_copy = __pskb_copy_fclone(skb, 1, GFP_ATOMIC, true); |
Marcel Holtmann | e0edf37 | 2012-02-20 14:50:36 +0100 | [diff] [blame] | 169 | if (!skb_copy) |
| 170 | continue; |
| 171 | |
| 172 | /* Put type byte before the data */ |
| 173 | memcpy(skb_push(skb_copy, 1), &bt_cb(skb)->pkt_type, 1); |
| 174 | } |
| 175 | |
| 176 | nskb = skb_clone(skb_copy, GFP_ATOMIC); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 177 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | continue; |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | if (sock_queue_rcv_skb(sk, nskb)) |
| 181 | kfree_skb(nskb); |
| 182 | } |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 183 | |
| 184 | read_unlock(&hci_sk_list.lock); |
Marcel Holtmann | e0edf37 | 2012-02-20 14:50:36 +0100 | [diff] [blame] | 185 | |
| 186 | kfree_skb(skb_copy); |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 187 | } |
| 188 | |
Johan Hedberg | 7129069 | 2015-02-20 13:26:23 +0200 | [diff] [blame] | 189 | /* Send frame to sockets with specific channel */ |
| 190 | void hci_send_to_channel(unsigned short channel, struct sk_buff *skb, |
| 191 | struct sock *skip_sk) |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 192 | { |
| 193 | struct sock *sk; |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 194 | |
Johan Hedberg | 7129069 | 2015-02-20 13:26:23 +0200 | [diff] [blame] | 195 | BT_DBG("channel %u len %d", channel, skb->len); |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 196 | |
| 197 | read_lock(&hci_sk_list.lock); |
| 198 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 199 | sk_for_each(sk, &hci_sk_list.head) { |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 200 | struct sk_buff *nskb; |
| 201 | |
| 202 | /* Skip the original socket */ |
| 203 | if (sk == skip_sk) |
| 204 | continue; |
| 205 | |
| 206 | if (sk->sk_state != BT_BOUND) |
| 207 | continue; |
| 208 | |
Johan Hedberg | 7129069 | 2015-02-20 13:26:23 +0200 | [diff] [blame] | 209 | if (hci_pi(sk)->channel != channel) |
Marcel Holtmann | d7f72f6 | 2015-01-11 19:33:32 -0800 | [diff] [blame] | 210 | continue; |
| 211 | |
| 212 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 213 | if (!nskb) |
| 214 | continue; |
| 215 | |
| 216 | if (sock_queue_rcv_skb(sk, nskb)) |
| 217 | kfree_skb(nskb); |
| 218 | } |
| 219 | |
| 220 | read_unlock(&hci_sk_list.lock); |
| 221 | } |
| 222 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 223 | /* Send frame to monitor socket */ |
| 224 | void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb) |
| 225 | { |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 226 | struct sk_buff *skb_copy = NULL; |
Marcel Holtmann | 2b53129 | 2015-01-11 19:33:31 -0800 | [diff] [blame] | 227 | struct hci_mon_hdr *hdr; |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 228 | __le16 opcode; |
| 229 | |
| 230 | if (!atomic_read(&monitor_promisc)) |
| 231 | return; |
| 232 | |
| 233 | BT_DBG("hdev %p len %d", hdev, skb->len); |
| 234 | |
| 235 | switch (bt_cb(skb)->pkt_type) { |
| 236 | case HCI_COMMAND_PKT: |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 237 | opcode = cpu_to_le16(HCI_MON_COMMAND_PKT); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 238 | break; |
| 239 | case HCI_EVENT_PKT: |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 240 | opcode = cpu_to_le16(HCI_MON_EVENT_PKT); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 241 | break; |
| 242 | case HCI_ACLDATA_PKT: |
| 243 | if (bt_cb(skb)->incoming) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 244 | opcode = cpu_to_le16(HCI_MON_ACL_RX_PKT); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 245 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 246 | opcode = cpu_to_le16(HCI_MON_ACL_TX_PKT); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 247 | break; |
| 248 | case HCI_SCODATA_PKT: |
| 249 | if (bt_cb(skb)->incoming) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 250 | opcode = cpu_to_le16(HCI_MON_SCO_RX_PKT); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 251 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 252 | opcode = cpu_to_le16(HCI_MON_SCO_TX_PKT); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 253 | break; |
| 254 | default: |
| 255 | return; |
| 256 | } |
| 257 | |
Marcel Holtmann | 2b53129 | 2015-01-11 19:33:31 -0800 | [diff] [blame] | 258 | /* Create a private copy with headroom */ |
| 259 | skb_copy = __pskb_copy_fclone(skb, HCI_MON_HDR_SIZE, GFP_ATOMIC, true); |
| 260 | if (!skb_copy) |
| 261 | return; |
| 262 | |
| 263 | /* Put header before the data */ |
| 264 | hdr = (void *) skb_push(skb_copy, HCI_MON_HDR_SIZE); |
| 265 | hdr->opcode = opcode; |
| 266 | hdr->index = cpu_to_le16(hdev->id); |
| 267 | hdr->len = cpu_to_le16(skb->len); |
| 268 | |
Johan Hedberg | 03f310e | 2015-02-20 13:26:24 +0200 | [diff] [blame] | 269 | hci_send_to_channel(HCI_CHANNEL_MONITOR, skb_copy, NULL); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 270 | kfree_skb(skb_copy); |
| 271 | } |
| 272 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 273 | static struct sk_buff *create_monitor_event(struct hci_dev *hdev, int event) |
| 274 | { |
| 275 | struct hci_mon_hdr *hdr; |
| 276 | struct hci_mon_new_index *ni; |
| 277 | struct sk_buff *skb; |
| 278 | __le16 opcode; |
| 279 | |
| 280 | switch (event) { |
| 281 | case HCI_DEV_REG: |
| 282 | skb = bt_skb_alloc(HCI_MON_NEW_INDEX_SIZE, GFP_ATOMIC); |
| 283 | if (!skb) |
| 284 | return NULL; |
| 285 | |
| 286 | ni = (void *) skb_put(skb, HCI_MON_NEW_INDEX_SIZE); |
| 287 | ni->type = hdev->dev_type; |
| 288 | ni->bus = hdev->bus; |
| 289 | bacpy(&ni->bdaddr, &hdev->bdaddr); |
| 290 | memcpy(ni->name, hdev->name, 8); |
| 291 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 292 | opcode = cpu_to_le16(HCI_MON_NEW_INDEX); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 293 | break; |
| 294 | |
| 295 | case HCI_DEV_UNREG: |
| 296 | skb = bt_skb_alloc(0, GFP_ATOMIC); |
| 297 | if (!skb) |
| 298 | return NULL; |
| 299 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 300 | opcode = cpu_to_le16(HCI_MON_DEL_INDEX); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 301 | break; |
| 302 | |
| 303 | default: |
| 304 | return NULL; |
| 305 | } |
| 306 | |
| 307 | __net_timestamp(skb); |
| 308 | |
| 309 | hdr = (void *) skb_push(skb, HCI_MON_HDR_SIZE); |
| 310 | hdr->opcode = opcode; |
| 311 | hdr->index = cpu_to_le16(hdev->id); |
| 312 | hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE); |
| 313 | |
| 314 | return skb; |
| 315 | } |
| 316 | |
| 317 | static void send_monitor_replay(struct sock *sk) |
| 318 | { |
| 319 | struct hci_dev *hdev; |
| 320 | |
| 321 | read_lock(&hci_dev_list_lock); |
| 322 | |
| 323 | list_for_each_entry(hdev, &hci_dev_list, list) { |
| 324 | struct sk_buff *skb; |
| 325 | |
| 326 | skb = create_monitor_event(hdev, HCI_DEV_REG); |
| 327 | if (!skb) |
| 328 | continue; |
| 329 | |
| 330 | if (sock_queue_rcv_skb(sk, skb)) |
| 331 | kfree_skb(skb); |
| 332 | } |
| 333 | |
| 334 | read_unlock(&hci_dev_list_lock); |
| 335 | } |
| 336 | |
Marcel Holtmann | 040030e | 2012-02-20 14:50:37 +0100 | [diff] [blame] | 337 | /* Generate internal stack event */ |
| 338 | static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data) |
| 339 | { |
| 340 | struct hci_event_hdr *hdr; |
| 341 | struct hci_ev_stack_internal *ev; |
| 342 | struct sk_buff *skb; |
| 343 | |
| 344 | skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC); |
| 345 | if (!skb) |
| 346 | return; |
| 347 | |
| 348 | hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE); |
| 349 | hdr->evt = HCI_EV_STACK_INTERNAL; |
| 350 | hdr->plen = sizeof(*ev) + dlen; |
| 351 | |
| 352 | ev = (void *) skb_put(skb, sizeof(*ev) + dlen); |
| 353 | ev->type = type; |
| 354 | memcpy(ev->data, data, dlen); |
| 355 | |
| 356 | bt_cb(skb)->incoming = 1; |
| 357 | __net_timestamp(skb); |
| 358 | |
| 359 | bt_cb(skb)->pkt_type = HCI_EVENT_PKT; |
Marcel Holtmann | 040030e | 2012-02-20 14:50:37 +0100 | [diff] [blame] | 360 | hci_send_to_sock(hdev, skb); |
| 361 | kfree_skb(skb); |
| 362 | } |
| 363 | |
| 364 | void hci_sock_dev_event(struct hci_dev *hdev, int event) |
| 365 | { |
| 366 | struct hci_ev_si_device ev; |
| 367 | |
| 368 | BT_DBG("hdev %s event %d", hdev->name, event); |
| 369 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 370 | /* Send event to monitor */ |
| 371 | if (atomic_read(&monitor_promisc)) { |
| 372 | struct sk_buff *skb; |
| 373 | |
| 374 | skb = create_monitor_event(hdev, event); |
| 375 | if (skb) { |
Johan Hedberg | 03f310e | 2015-02-20 13:26:24 +0200 | [diff] [blame] | 376 | hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, NULL); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 377 | kfree_skb(skb); |
| 378 | } |
| 379 | } |
| 380 | |
Marcel Holtmann | 040030e | 2012-02-20 14:50:37 +0100 | [diff] [blame] | 381 | /* Send event to sockets */ |
| 382 | ev.event = event; |
| 383 | ev.dev_id = hdev->id; |
| 384 | hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev); |
| 385 | |
| 386 | if (event == HCI_DEV_UNREG) { |
| 387 | struct sock *sk; |
Marcel Holtmann | 040030e | 2012-02-20 14:50:37 +0100 | [diff] [blame] | 388 | |
| 389 | /* Detach sockets from device */ |
| 390 | read_lock(&hci_sk_list.lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 391 | sk_for_each(sk, &hci_sk_list.head) { |
Marcel Holtmann | 040030e | 2012-02-20 14:50:37 +0100 | [diff] [blame] | 392 | bh_lock_sock_nested(sk); |
| 393 | if (hci_pi(sk)->hdev == hdev) { |
| 394 | hci_pi(sk)->hdev = NULL; |
| 395 | sk->sk_err = EPIPE; |
| 396 | sk->sk_state = BT_OPEN; |
| 397 | sk->sk_state_change(sk); |
| 398 | |
| 399 | hci_dev_put(hdev); |
| 400 | } |
| 401 | bh_unlock_sock(sk); |
| 402 | } |
| 403 | read_unlock(&hci_sk_list.lock); |
| 404 | } |
| 405 | } |
| 406 | |
Johan Hedberg | 801c1e8 | 2015-03-06 21:08:50 +0200 | [diff] [blame] | 407 | static struct hci_mgmt_chan *__hci_mgmt_chan_find(unsigned short channel) |
| 408 | { |
| 409 | struct hci_mgmt_chan *c; |
| 410 | |
| 411 | list_for_each_entry(c, &mgmt_chan_list, list) { |
| 412 | if (c->channel == channel) |
| 413 | return c; |
| 414 | } |
| 415 | |
| 416 | return NULL; |
| 417 | } |
| 418 | |
| 419 | static struct hci_mgmt_chan *hci_mgmt_chan_find(unsigned short channel) |
| 420 | { |
| 421 | struct hci_mgmt_chan *c; |
| 422 | |
| 423 | mutex_lock(&mgmt_chan_list_lock); |
| 424 | c = __hci_mgmt_chan_find(channel); |
| 425 | mutex_unlock(&mgmt_chan_list_lock); |
| 426 | |
| 427 | return c; |
| 428 | } |
| 429 | |
| 430 | int hci_mgmt_chan_register(struct hci_mgmt_chan *c) |
| 431 | { |
| 432 | if (c->channel < HCI_CHANNEL_CONTROL) |
| 433 | return -EINVAL; |
| 434 | |
| 435 | mutex_lock(&mgmt_chan_list_lock); |
| 436 | if (__hci_mgmt_chan_find(c->channel)) { |
| 437 | mutex_unlock(&mgmt_chan_list_lock); |
| 438 | return -EALREADY; |
| 439 | } |
| 440 | |
| 441 | list_add_tail(&c->list, &mgmt_chan_list); |
| 442 | |
| 443 | mutex_unlock(&mgmt_chan_list_lock); |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | EXPORT_SYMBOL(hci_mgmt_chan_register); |
| 448 | |
| 449 | void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c) |
| 450 | { |
| 451 | mutex_lock(&mgmt_chan_list_lock); |
| 452 | list_del(&c->list); |
| 453 | mutex_unlock(&mgmt_chan_list_lock); |
| 454 | } |
| 455 | EXPORT_SYMBOL(hci_mgmt_chan_unregister); |
| 456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | static int hci_sock_release(struct socket *sock) |
| 458 | { |
| 459 | struct sock *sk = sock->sk; |
Marcel Holtmann | 7b005bd | 2006-02-13 11:40:03 +0100 | [diff] [blame] | 460 | struct hci_dev *hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
| 462 | BT_DBG("sock %p sk %p", sock, sk); |
| 463 | |
| 464 | if (!sk) |
| 465 | return 0; |
| 466 | |
Marcel Holtmann | 7b005bd | 2006-02-13 11:40:03 +0100 | [diff] [blame] | 467 | hdev = hci_pi(sk)->hdev; |
| 468 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 469 | if (hci_pi(sk)->channel == HCI_CHANNEL_MONITOR) |
| 470 | atomic_dec(&monitor_promisc); |
| 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | bt_sock_unlink(&hci_sk_list, sk); |
| 473 | |
| 474 | if (hdev) { |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 475 | if (hci_pi(sk)->channel == HCI_CHANNEL_USER) { |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 476 | mgmt_index_added(hdev); |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 477 | clear_bit(HCI_USER_CHANNEL, &hdev->dev_flags); |
| 478 | hci_dev_close(hdev->id); |
| 479 | } |
| 480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | atomic_dec(&hdev->promisc); |
| 482 | hci_dev_put(hdev); |
| 483 | } |
| 484 | |
| 485 | sock_orphan(sk); |
| 486 | |
| 487 | skb_queue_purge(&sk->sk_receive_queue); |
| 488 | skb_queue_purge(&sk->sk_write_queue); |
| 489 | |
| 490 | sock_put(sk); |
| 491 | return 0; |
| 492 | } |
| 493 | |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 494 | static int hci_sock_blacklist_add(struct hci_dev *hdev, void __user *arg) |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 495 | { |
| 496 | bdaddr_t bdaddr; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 497 | int err; |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 498 | |
| 499 | if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) |
| 500 | return -EFAULT; |
| 501 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 502 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 503 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 504 | err = hci_bdaddr_list_add(&hdev->blacklist, &bdaddr, BDADDR_BREDR); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 505 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 506 | hci_dev_unlock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 507 | |
| 508 | return err; |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 509 | } |
| 510 | |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 511 | static int hci_sock_blacklist_del(struct hci_dev *hdev, void __user *arg) |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 512 | { |
| 513 | bdaddr_t bdaddr; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 514 | int err; |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 515 | |
| 516 | if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) |
| 517 | return -EFAULT; |
| 518 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 519 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 520 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 521 | err = hci_bdaddr_list_del(&hdev->blacklist, &bdaddr, BDADDR_BREDR); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 522 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 523 | hci_dev_unlock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 524 | |
| 525 | return err; |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 526 | } |
| 527 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 528 | /* Ioctls that require bound socket */ |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 529 | static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd, |
| 530 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | { |
| 532 | struct hci_dev *hdev = hci_pi(sk)->hdev; |
| 533 | |
| 534 | if (!hdev) |
| 535 | return -EBADFD; |
| 536 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame^] | 537 | if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 538 | return -EBUSY; |
| 539 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame^] | 540 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 541 | return -EOPNOTSUPP; |
| 542 | |
Marcel Holtmann | 5b69bef5 | 2013-10-10 10:02:08 -0700 | [diff] [blame] | 543 | if (hdev->dev_type != HCI_BREDR) |
| 544 | return -EOPNOTSUPP; |
| 545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | switch (cmd) { |
| 547 | case HCISETRAW: |
| 548 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 549 | return -EPERM; |
Marcel Holtmann | db59668 | 2014-04-16 20:04:38 -0700 | [diff] [blame] | 550 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | case HCIGETCONNINFO: |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 553 | return hci_get_conn_info(hdev, (void __user *) arg); |
| 554 | |
| 555 | case HCIGETAUTHINFO: |
| 556 | return hci_get_auth_info(hdev, (void __user *) arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 558 | case HCIBLOCKADDR: |
| 559 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 560 | return -EPERM; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 561 | return hci_sock_blacklist_add(hdev, (void __user *) arg); |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 562 | |
| 563 | case HCIUNBLOCKADDR: |
| 564 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 565 | return -EPERM; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 566 | return hci_sock_blacklist_del(hdev, (void __user *) arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 568 | |
Marcel Holtmann | 324d36e | 2013-10-10 10:50:06 -0700 | [diff] [blame] | 569 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 572 | static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, |
| 573 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | { |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 575 | void __user *argp = (void __user *) arg; |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 576 | struct sock *sk = sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | int err; |
| 578 | |
| 579 | BT_DBG("cmd %x arg %lx", cmd, arg); |
| 580 | |
Marcel Holtmann | c1c4f95 | 2013-08-26 09:39:55 -0700 | [diff] [blame] | 581 | lock_sock(sk); |
| 582 | |
| 583 | if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { |
| 584 | err = -EBADFD; |
| 585 | goto done; |
| 586 | } |
| 587 | |
| 588 | release_sock(sk); |
| 589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | switch (cmd) { |
| 591 | case HCIGETDEVLIST: |
| 592 | return hci_get_dev_list(argp); |
| 593 | |
| 594 | case HCIGETDEVINFO: |
| 595 | return hci_get_dev_info(argp); |
| 596 | |
| 597 | case HCIGETCONNLIST: |
| 598 | return hci_get_conn_list(argp); |
| 599 | |
| 600 | case HCIDEVUP: |
| 601 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 602 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | return hci_dev_open(arg); |
| 604 | |
| 605 | case HCIDEVDOWN: |
| 606 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 607 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | return hci_dev_close(arg); |
| 609 | |
| 610 | case HCIDEVRESET: |
| 611 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 612 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | return hci_dev_reset(arg); |
| 614 | |
| 615 | case HCIDEVRESTAT: |
| 616 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 617 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | return hci_dev_reset_stat(arg); |
| 619 | |
| 620 | case HCISETSCAN: |
| 621 | case HCISETAUTH: |
| 622 | case HCISETENCRYPT: |
| 623 | case HCISETPTYPE: |
| 624 | case HCISETLINKPOL: |
| 625 | case HCISETLINKMODE: |
| 626 | case HCISETACLMTU: |
| 627 | case HCISETSCOMTU: |
| 628 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 629 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | return hci_dev_cmd(cmd, argp); |
| 631 | |
| 632 | case HCIINQUIRY: |
| 633 | return hci_inquiry(argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
Marcel Holtmann | c1c4f95 | 2013-08-26 09:39:55 -0700 | [diff] [blame] | 635 | |
| 636 | lock_sock(sk); |
| 637 | |
| 638 | err = hci_sock_bound_ioctl(sk, cmd, arg); |
| 639 | |
| 640 | done: |
| 641 | release_sock(sk); |
| 642 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 645 | static int hci_sock_bind(struct socket *sock, struct sockaddr *addr, |
| 646 | int addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 648 | struct sockaddr_hci haddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | struct sock *sk = sock->sk; |
| 650 | struct hci_dev *hdev = NULL; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 651 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
| 653 | BT_DBG("sock %p sk %p", sock, sk); |
| 654 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 655 | if (!addr) |
| 656 | return -EINVAL; |
| 657 | |
| 658 | memset(&haddr, 0, sizeof(haddr)); |
| 659 | len = min_t(unsigned int, sizeof(haddr), addr_len); |
| 660 | memcpy(&haddr, addr, len); |
| 661 | |
| 662 | if (haddr.hci_family != AF_BLUETOOTH) |
| 663 | return -EINVAL; |
| 664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | lock_sock(sk); |
| 666 | |
Marcel Holtmann | 7cc2ade | 2012-02-20 14:50:35 +0100 | [diff] [blame] | 667 | if (sk->sk_state == BT_BOUND) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | err = -EALREADY; |
| 669 | goto done; |
| 670 | } |
| 671 | |
Marcel Holtmann | 7cc2ade | 2012-02-20 14:50:35 +0100 | [diff] [blame] | 672 | switch (haddr.hci_channel) { |
| 673 | case HCI_CHANNEL_RAW: |
| 674 | if (hci_pi(sk)->hdev) { |
| 675 | err = -EALREADY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | goto done; |
| 677 | } |
| 678 | |
Marcel Holtmann | 7cc2ade | 2012-02-20 14:50:35 +0100 | [diff] [blame] | 679 | if (haddr.hci_dev != HCI_DEV_NONE) { |
| 680 | hdev = hci_dev_get(haddr.hci_dev); |
| 681 | if (!hdev) { |
| 682 | err = -ENODEV; |
| 683 | goto done; |
| 684 | } |
| 685 | |
| 686 | atomic_inc(&hdev->promisc); |
| 687 | } |
| 688 | |
| 689 | hci_pi(sk)->hdev = hdev; |
| 690 | break; |
| 691 | |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 692 | case HCI_CHANNEL_USER: |
| 693 | if (hci_pi(sk)->hdev) { |
| 694 | err = -EALREADY; |
| 695 | goto done; |
| 696 | } |
| 697 | |
| 698 | if (haddr.hci_dev == HCI_DEV_NONE) { |
| 699 | err = -EINVAL; |
| 700 | goto done; |
| 701 | } |
| 702 | |
Marcel Holtmann | 10a8b86 | 2013-10-01 22:59:24 -0700 | [diff] [blame] | 703 | if (!capable(CAP_NET_ADMIN)) { |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 704 | err = -EPERM; |
| 705 | goto done; |
| 706 | } |
| 707 | |
| 708 | hdev = hci_dev_get(haddr.hci_dev); |
| 709 | if (!hdev) { |
| 710 | err = -ENODEV; |
| 711 | goto done; |
| 712 | } |
| 713 | |
| 714 | if (test_bit(HCI_UP, &hdev->flags) || |
| 715 | test_bit(HCI_INIT, &hdev->flags) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame^] | 716 | hci_dev_test_flag(hdev, HCI_SETUP) || |
| 717 | hci_dev_test_flag(hdev, HCI_CONFIG)) { |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 718 | err = -EBUSY; |
| 719 | hci_dev_put(hdev); |
| 720 | goto done; |
| 721 | } |
| 722 | |
| 723 | if (test_and_set_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 724 | err = -EUSERS; |
| 725 | hci_dev_put(hdev); |
| 726 | goto done; |
| 727 | } |
| 728 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 729 | mgmt_index_removed(hdev); |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 730 | |
| 731 | err = hci_dev_open(hdev->id); |
| 732 | if (err) { |
| 733 | clear_bit(HCI_USER_CHANNEL, &hdev->dev_flags); |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 734 | mgmt_index_added(hdev); |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 735 | hci_dev_put(hdev); |
| 736 | goto done; |
| 737 | } |
| 738 | |
| 739 | atomic_inc(&hdev->promisc); |
| 740 | |
| 741 | hci_pi(sk)->hdev = hdev; |
| 742 | break; |
| 743 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 744 | case HCI_CHANNEL_MONITOR: |
| 745 | if (haddr.hci_dev != HCI_DEV_NONE) { |
| 746 | err = -EINVAL; |
| 747 | goto done; |
| 748 | } |
| 749 | |
| 750 | if (!capable(CAP_NET_RAW)) { |
| 751 | err = -EPERM; |
| 752 | goto done; |
| 753 | } |
| 754 | |
| 755 | send_monitor_replay(sk); |
| 756 | |
| 757 | atomic_inc(&monitor_promisc); |
| 758 | break; |
| 759 | |
Marcel Holtmann | 7cc2ade | 2012-02-20 14:50:35 +0100 | [diff] [blame] | 760 | default: |
Johan Hedberg | 801c1e8 | 2015-03-06 21:08:50 +0200 | [diff] [blame] | 761 | if (!hci_mgmt_chan_find(haddr.hci_channel)) { |
| 762 | err = -EINVAL; |
| 763 | goto done; |
| 764 | } |
| 765 | |
| 766 | if (haddr.hci_dev != HCI_DEV_NONE) { |
| 767 | err = -EINVAL; |
| 768 | goto done; |
| 769 | } |
| 770 | |
| 771 | if (!capable(CAP_NET_ADMIN)) { |
| 772 | err = -EPERM; |
| 773 | goto done; |
| 774 | } |
| 775 | |
| 776 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Marcel Holtmann | 7cc2ade | 2012-02-20 14:50:35 +0100 | [diff] [blame] | 779 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 780 | hci_pi(sk)->channel = haddr.hci_channel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | sk->sk_state = BT_BOUND; |
| 782 | |
| 783 | done: |
| 784 | release_sock(sk); |
| 785 | return err; |
| 786 | } |
| 787 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 788 | static int hci_sock_getname(struct socket *sock, struct sockaddr *addr, |
| 789 | int *addr_len, int peer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | { |
| 791 | struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr; |
| 792 | struct sock *sk = sock->sk; |
Marcel Holtmann | 9d4b68b | 2013-08-26 00:20:37 -0700 | [diff] [blame] | 793 | struct hci_dev *hdev; |
| 794 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
| 796 | BT_DBG("sock %p sk %p", sock, sk); |
| 797 | |
Marcel Holtmann | 06f43cb | 2013-08-26 00:06:30 -0700 | [diff] [blame] | 798 | if (peer) |
| 799 | return -EOPNOTSUPP; |
| 800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | lock_sock(sk); |
| 802 | |
Marcel Holtmann | 9d4b68b | 2013-08-26 00:20:37 -0700 | [diff] [blame] | 803 | hdev = hci_pi(sk)->hdev; |
| 804 | if (!hdev) { |
| 805 | err = -EBADFD; |
| 806 | goto done; |
| 807 | } |
| 808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | *addr_len = sizeof(*haddr); |
| 810 | haddr->hci_family = AF_BLUETOOTH; |
Marcel Holtmann | 7b005bd | 2006-02-13 11:40:03 +0100 | [diff] [blame] | 811 | haddr->hci_dev = hdev->id; |
Marcel Holtmann | 9d4b68b | 2013-08-26 00:20:37 -0700 | [diff] [blame] | 812 | haddr->hci_channel= hci_pi(sk)->channel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
Marcel Holtmann | 9d4b68b | 2013-08-26 00:20:37 -0700 | [diff] [blame] | 814 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | release_sock(sk); |
Marcel Holtmann | 9d4b68b | 2013-08-26 00:20:37 -0700 | [diff] [blame] | 816 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 819 | static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg, |
| 820 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | { |
| 822 | __u32 mask = hci_pi(sk)->cmsg_mask; |
| 823 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 824 | if (mask & HCI_CMSG_DIR) { |
| 825 | int incoming = bt_cb(skb)->incoming; |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 826 | put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming), |
| 827 | &incoming); |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 830 | if (mask & HCI_CMSG_TSTAMP) { |
Johann Felix Soden | f6e623a | 2010-02-15 22:23:48 +0100 | [diff] [blame] | 831 | #ifdef CONFIG_COMPAT |
| 832 | struct compat_timeval ctv; |
| 833 | #endif |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 834 | struct timeval tv; |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 835 | void *data; |
| 836 | int len; |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 837 | |
| 838 | skb_get_timestamp(skb, &tv); |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 839 | |
David S. Miller | 1da97f8 | 2007-09-12 14:10:58 +0200 | [diff] [blame] | 840 | data = &tv; |
| 841 | len = sizeof(tv); |
| 842 | #ifdef CONFIG_COMPAT |
H. J. Lu | da88cea | 2012-02-10 14:12:15 -0800 | [diff] [blame] | 843 | if (!COMPAT_USE_64BIT_TIME && |
| 844 | (msg->msg_flags & MSG_CMSG_COMPAT)) { |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 845 | ctv.tv_sec = tv.tv_sec; |
| 846 | ctv.tv_usec = tv.tv_usec; |
| 847 | data = &ctv; |
| 848 | len = sizeof(ctv); |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 849 | } |
David S. Miller | 1da97f8 | 2007-09-12 14:10:58 +0200 | [diff] [blame] | 850 | #endif |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 851 | |
| 852 | put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data); |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 853 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 855 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 856 | static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, |
| 857 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | { |
| 859 | int noblock = flags & MSG_DONTWAIT; |
| 860 | struct sock *sk = sock->sk; |
| 861 | struct sk_buff *skb; |
| 862 | int copied, err; |
| 863 | |
| 864 | BT_DBG("sock %p, sk %p", sock, sk); |
| 865 | |
| 866 | if (flags & (MSG_OOB)) |
| 867 | return -EOPNOTSUPP; |
| 868 | |
| 869 | if (sk->sk_state == BT_CLOSED) |
| 870 | return 0; |
| 871 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 872 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 873 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | return err; |
| 875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | copied = skb->len; |
| 877 | if (len < copied) { |
| 878 | msg->msg_flags |= MSG_TRUNC; |
| 879 | copied = len; |
| 880 | } |
| 881 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 882 | skb_reset_transport_header(skb); |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 883 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Marcel Holtmann | 3a20862 | 2012-02-20 14:50:34 +0100 | [diff] [blame] | 885 | switch (hci_pi(sk)->channel) { |
| 886 | case HCI_CHANNEL_RAW: |
| 887 | hci_sock_cmsg(sk, msg, skb); |
| 888 | break; |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 889 | case HCI_CHANNEL_USER: |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 890 | case HCI_CHANNEL_MONITOR: |
| 891 | sock_recv_timestamp(msg, sk, skb); |
| 892 | break; |
Johan Hedberg | 801c1e8 | 2015-03-06 21:08:50 +0200 | [diff] [blame] | 893 | default: |
| 894 | if (hci_mgmt_chan_find(hci_pi(sk)->channel)) |
| 895 | sock_recv_timestamp(msg, sk, skb); |
| 896 | break; |
Marcel Holtmann | 3a20862 | 2012-02-20 14:50:34 +0100 | [diff] [blame] | 897 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
| 899 | skb_free_datagram(sk, skb); |
| 900 | |
| 901 | return err ? : copied; |
| 902 | } |
| 903 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 904 | static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg, |
| 905 | size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | { |
| 907 | struct sock *sk = sock->sk; |
Johan Hedberg | 801c1e8 | 2015-03-06 21:08:50 +0200 | [diff] [blame] | 908 | struct hci_mgmt_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | struct hci_dev *hdev; |
| 910 | struct sk_buff *skb; |
| 911 | int err; |
| 912 | |
| 913 | BT_DBG("sock %p sk %p", sock, sk); |
| 914 | |
| 915 | if (msg->msg_flags & MSG_OOB) |
| 916 | return -EOPNOTSUPP; |
| 917 | |
| 918 | if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE)) |
| 919 | return -EINVAL; |
| 920 | |
| 921 | if (len < 4 || len > HCI_MAX_FRAME_SIZE) |
| 922 | return -EINVAL; |
| 923 | |
| 924 | lock_sock(sk); |
| 925 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 926 | switch (hci_pi(sk)->channel) { |
| 927 | case HCI_CHANNEL_RAW: |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 928 | case HCI_CHANNEL_USER: |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 929 | break; |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 930 | case HCI_CHANNEL_MONITOR: |
| 931 | err = -EOPNOTSUPP; |
| 932 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 933 | default: |
Johan Hedberg | 801c1e8 | 2015-03-06 21:08:50 +0200 | [diff] [blame] | 934 | mutex_lock(&mgmt_chan_list_lock); |
| 935 | chan = __hci_mgmt_chan_find(hci_pi(sk)->channel); |
| 936 | if (chan) |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 937 | err = mgmt_control(chan, sk, msg, len); |
Johan Hedberg | 801c1e8 | 2015-03-06 21:08:50 +0200 | [diff] [blame] | 938 | else |
| 939 | err = -EINVAL; |
| 940 | |
| 941 | mutex_unlock(&mgmt_chan_list_lock); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 942 | goto done; |
| 943 | } |
| 944 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 945 | hdev = hci_pi(sk)->hdev; |
| 946 | if (!hdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | err = -EBADFD; |
| 948 | goto done; |
| 949 | } |
| 950 | |
Marcel Holtmann | 7e21add | 2009-11-18 01:05:00 +0100 | [diff] [blame] | 951 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 952 | err = -ENETDOWN; |
| 953 | goto done; |
| 954 | } |
| 955 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 956 | skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err); |
| 957 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | goto done; |
| 959 | |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 960 | if (memcpy_from_msg(skb_put(skb, len), msg, len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | err = -EFAULT; |
| 962 | goto drop; |
| 963 | } |
| 964 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 965 | bt_cb(skb)->pkt_type = *((unsigned char *) skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | skb_pull(skb, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
Marcel Holtmann | 1bc5ad1 | 2013-12-17 03:21:25 -0800 | [diff] [blame] | 968 | if (hci_pi(sk)->channel == HCI_CHANNEL_USER) { |
| 969 | /* No permission check is needed for user channel |
| 970 | * since that gets enforced when binding the socket. |
| 971 | * |
| 972 | * However check that the packet type is valid. |
| 973 | */ |
| 974 | if (bt_cb(skb)->pkt_type != HCI_COMMAND_PKT && |
| 975 | bt_cb(skb)->pkt_type != HCI_ACLDATA_PKT && |
| 976 | bt_cb(skb)->pkt_type != HCI_SCODATA_PKT) { |
| 977 | err = -EINVAL; |
| 978 | goto drop; |
| 979 | } |
| 980 | |
| 981 | skb_queue_tail(&hdev->raw_q, skb); |
| 982 | queue_work(hdev->workqueue, &hdev->tx_work); |
| 983 | } else if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) { |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 984 | u16 opcode = get_unaligned_le16(skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | u16 ogf = hci_opcode_ogf(opcode); |
| 986 | u16 ocf = hci_opcode_ocf(opcode); |
| 987 | |
| 988 | if (((ogf > HCI_SFLT_MAX_OGF) || |
Gustavo Padovan | 3bb3c75 | 2012-05-17 00:36:22 -0300 | [diff] [blame] | 989 | !hci_test_bit(ocf & HCI_FLT_OCF_BITS, |
| 990 | &hci_sec_filter.ocf_mask[ogf])) && |
| 991 | !capable(CAP_NET_RAW)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | err = -EPERM; |
| 993 | goto drop; |
| 994 | } |
| 995 | |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 996 | if (ogf == 0x3f) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | skb_queue_tail(&hdev->raw_q, skb); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 998 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | } else { |
Stephen Hemminger | 49c922b | 2014-10-27 21:12:20 -0700 | [diff] [blame] | 1000 | /* Stand-alone HCI commands must be flagged as |
Johan Hedberg | 11714b3 | 2013-03-05 20:37:47 +0200 | [diff] [blame] | 1001 | * single-command requests. |
| 1002 | */ |
Eyal Birger | 6368c23 | 2015-03-01 14:58:26 +0200 | [diff] [blame] | 1003 | bt_cb(skb)->req_start = 1; |
Johan Hedberg | 11714b3 | 2013-03-05 20:37:47 +0200 | [diff] [blame] | 1004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | skb_queue_tail(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 1006 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | } |
| 1008 | } else { |
| 1009 | if (!capable(CAP_NET_RAW)) { |
| 1010 | err = -EPERM; |
| 1011 | goto drop; |
| 1012 | } |
| 1013 | |
| 1014 | skb_queue_tail(&hdev->raw_q, skb); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 1015 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | err = len; |
| 1019 | |
| 1020 | done: |
| 1021 | release_sock(sk); |
| 1022 | return err; |
| 1023 | |
| 1024 | drop: |
| 1025 | kfree_skb(skb); |
| 1026 | goto done; |
| 1027 | } |
| 1028 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1029 | static int hci_sock_setsockopt(struct socket *sock, int level, int optname, |
| 1030 | char __user *optval, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | { |
| 1032 | struct hci_ufilter uf = { .opcode = 0 }; |
| 1033 | struct sock *sk = sock->sk; |
| 1034 | int err = 0, opt = 0; |
| 1035 | |
| 1036 | BT_DBG("sk %p, opt %d", sk, optname); |
| 1037 | |
| 1038 | lock_sock(sk); |
| 1039 | |
Marcel Holtmann | 2f39cdb | 2012-02-20 14:50:32 +0100 | [diff] [blame] | 1040 | if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { |
Marcel Holtmann | c2371e8 | 2013-08-26 09:29:39 -0700 | [diff] [blame] | 1041 | err = -EBADFD; |
Marcel Holtmann | 2f39cdb | 2012-02-20 14:50:32 +0100 | [diff] [blame] | 1042 | goto done; |
| 1043 | } |
| 1044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | switch (optname) { |
| 1046 | case HCI_DATA_DIR: |
| 1047 | if (get_user(opt, (int __user *)optval)) { |
| 1048 | err = -EFAULT; |
| 1049 | break; |
| 1050 | } |
| 1051 | |
| 1052 | if (opt) |
| 1053 | hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR; |
| 1054 | else |
| 1055 | hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR; |
| 1056 | break; |
| 1057 | |
| 1058 | case HCI_TIME_STAMP: |
| 1059 | if (get_user(opt, (int __user *)optval)) { |
| 1060 | err = -EFAULT; |
| 1061 | break; |
| 1062 | } |
| 1063 | |
| 1064 | if (opt) |
| 1065 | hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP; |
| 1066 | else |
| 1067 | hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP; |
| 1068 | break; |
| 1069 | |
| 1070 | case HCI_FILTER: |
Marcel Holtmann | 0878b66 | 2007-05-05 00:35:59 +0200 | [diff] [blame] | 1071 | { |
| 1072 | struct hci_filter *f = &hci_pi(sk)->filter; |
| 1073 | |
| 1074 | uf.type_mask = f->type_mask; |
| 1075 | uf.opcode = f->opcode; |
| 1076 | uf.event_mask[0] = *((u32 *) f->event_mask + 0); |
| 1077 | uf.event_mask[1] = *((u32 *) f->event_mask + 1); |
| 1078 | } |
| 1079 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | len = min_t(unsigned int, len, sizeof(uf)); |
| 1081 | if (copy_from_user(&uf, optval, len)) { |
| 1082 | err = -EFAULT; |
| 1083 | break; |
| 1084 | } |
| 1085 | |
| 1086 | if (!capable(CAP_NET_RAW)) { |
| 1087 | uf.type_mask &= hci_sec_filter.type_mask; |
| 1088 | uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0); |
| 1089 | uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1); |
| 1090 | } |
| 1091 | |
| 1092 | { |
| 1093 | struct hci_filter *f = &hci_pi(sk)->filter; |
| 1094 | |
| 1095 | f->type_mask = uf.type_mask; |
| 1096 | f->opcode = uf.opcode; |
| 1097 | *((u32 *) f->event_mask + 0) = uf.event_mask[0]; |
| 1098 | *((u32 *) f->event_mask + 1) = uf.event_mask[1]; |
| 1099 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1100 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | |
| 1102 | default: |
| 1103 | err = -ENOPROTOOPT; |
| 1104 | break; |
| 1105 | } |
| 1106 | |
Marcel Holtmann | 2f39cdb | 2012-02-20 14:50:32 +0100 | [diff] [blame] | 1107 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | release_sock(sk); |
| 1109 | return err; |
| 1110 | } |
| 1111 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1112 | static int hci_sock_getsockopt(struct socket *sock, int level, int optname, |
| 1113 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | { |
| 1115 | struct hci_ufilter uf; |
| 1116 | struct sock *sk = sock->sk; |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1117 | int len, opt, err = 0; |
| 1118 | |
| 1119 | BT_DBG("sk %p, opt %d", sk, optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | |
| 1121 | if (get_user(len, optlen)) |
| 1122 | return -EFAULT; |
| 1123 | |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1124 | lock_sock(sk); |
| 1125 | |
| 1126 | if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { |
Marcel Holtmann | c2371e8 | 2013-08-26 09:29:39 -0700 | [diff] [blame] | 1127 | err = -EBADFD; |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1128 | goto done; |
| 1129 | } |
| 1130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | switch (optname) { |
| 1132 | case HCI_DATA_DIR: |
| 1133 | if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR) |
| 1134 | opt = 1; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1135 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | opt = 0; |
| 1137 | |
| 1138 | if (put_user(opt, optval)) |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1139 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | break; |
| 1141 | |
| 1142 | case HCI_TIME_STAMP: |
| 1143 | if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP) |
| 1144 | opt = 1; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1145 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | opt = 0; |
| 1147 | |
| 1148 | if (put_user(opt, optval)) |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1149 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | break; |
| 1151 | |
| 1152 | case HCI_FILTER: |
| 1153 | { |
| 1154 | struct hci_filter *f = &hci_pi(sk)->filter; |
| 1155 | |
Mathias Krause | e15ca9a | 2012-08-15 11:31:46 +0000 | [diff] [blame] | 1156 | memset(&uf, 0, sizeof(uf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | uf.type_mask = f->type_mask; |
| 1158 | uf.opcode = f->opcode; |
| 1159 | uf.event_mask[0] = *((u32 *) f->event_mask + 0); |
| 1160 | uf.event_mask[1] = *((u32 *) f->event_mask + 1); |
| 1161 | } |
| 1162 | |
| 1163 | len = min_t(unsigned int, len, sizeof(uf)); |
| 1164 | if (copy_to_user(optval, &uf, len)) |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1165 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | break; |
| 1167 | |
| 1168 | default: |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1169 | err = -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | break; |
| 1171 | } |
| 1172 | |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1173 | done: |
| 1174 | release_sock(sk); |
| 1175 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1178 | static const struct proto_ops hci_sock_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | .family = PF_BLUETOOTH, |
| 1180 | .owner = THIS_MODULE, |
| 1181 | .release = hci_sock_release, |
| 1182 | .bind = hci_sock_bind, |
| 1183 | .getname = hci_sock_getname, |
| 1184 | .sendmsg = hci_sock_sendmsg, |
| 1185 | .recvmsg = hci_sock_recvmsg, |
| 1186 | .ioctl = hci_sock_ioctl, |
| 1187 | .poll = datagram_poll, |
| 1188 | .listen = sock_no_listen, |
| 1189 | .shutdown = sock_no_shutdown, |
| 1190 | .setsockopt = hci_sock_setsockopt, |
| 1191 | .getsockopt = hci_sock_getsockopt, |
| 1192 | .connect = sock_no_connect, |
| 1193 | .socketpair = sock_no_socketpair, |
| 1194 | .accept = sock_no_accept, |
| 1195 | .mmap = sock_no_mmap |
| 1196 | }; |
| 1197 | |
| 1198 | static struct proto hci_sk_proto = { |
| 1199 | .name = "HCI", |
| 1200 | .owner = THIS_MODULE, |
| 1201 | .obj_size = sizeof(struct hci_pinfo) |
| 1202 | }; |
| 1203 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 1204 | static int hci_sock_create(struct net *net, struct socket *sock, int protocol, |
| 1205 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | { |
| 1207 | struct sock *sk; |
| 1208 | |
| 1209 | BT_DBG("sock %p", sock); |
| 1210 | |
| 1211 | if (sock->type != SOCK_RAW) |
| 1212 | return -ESOCKTNOSUPPORT; |
| 1213 | |
| 1214 | sock->ops = &hci_sock_ops; |
| 1215 | |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 1216 | sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | if (!sk) |
| 1218 | return -ENOMEM; |
| 1219 | |
| 1220 | sock_init_data(sock, sk); |
| 1221 | |
| 1222 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 1223 | |
| 1224 | sk->sk_protocol = protocol; |
| 1225 | |
| 1226 | sock->state = SS_UNCONNECTED; |
| 1227 | sk->sk_state = BT_OPEN; |
| 1228 | |
| 1229 | bt_sock_link(&hci_sk_list, sk); |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 1233 | static const struct net_proto_family hci_sock_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | .family = PF_BLUETOOTH, |
| 1235 | .owner = THIS_MODULE, |
| 1236 | .create = hci_sock_create, |
| 1237 | }; |
| 1238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | int __init hci_sock_init(void) |
| 1240 | { |
| 1241 | int err; |
| 1242 | |
Marcel Holtmann | b0a8e28 | 2015-01-11 15:18:17 -0800 | [diff] [blame] | 1243 | BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr)); |
| 1244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | err = proto_register(&hci_sk_proto, 0); |
| 1246 | if (err < 0) |
| 1247 | return err; |
| 1248 | |
| 1249 | err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops); |
Masatake YAMATO | f7c8663 | 2012-07-26 01:28:36 +0900 | [diff] [blame] | 1250 | if (err < 0) { |
| 1251 | BT_ERR("HCI socket registration failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | goto error; |
Masatake YAMATO | f7c8663 | 2012-07-26 01:28:36 +0900 | [diff] [blame] | 1253 | } |
| 1254 | |
Al Viro | b031661 | 2013-04-04 19:14:33 -0400 | [diff] [blame] | 1255 | err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL); |
Masatake YAMATO | f7c8663 | 2012-07-26 01:28:36 +0900 | [diff] [blame] | 1256 | if (err < 0) { |
| 1257 | BT_ERR("Failed to create HCI proc file"); |
| 1258 | bt_sock_unregister(BTPROTO_HCI); |
| 1259 | goto error; |
| 1260 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | BT_INFO("HCI socket layer initialized"); |
| 1263 | |
| 1264 | return 0; |
| 1265 | |
| 1266 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | proto_unregister(&hci_sk_proto); |
| 1268 | return err; |
| 1269 | } |
| 1270 | |
Anand Gadiyar | b7440a14 | 2011-02-22 12:43:09 +0530 | [diff] [blame] | 1271 | void hci_sock_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | { |
Masatake YAMATO | f7c8663 | 2012-07-26 01:28:36 +0900 | [diff] [blame] | 1273 | bt_procfs_cleanup(&init_net, "hci"); |
David Herrmann | 5e9d7f8 | 2013-02-24 19:36:51 +0100 | [diff] [blame] | 1274 | bt_sock_unregister(BTPROTO_HCI); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | proto_unregister(&hci_sk_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | } |