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