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> |
Johan Hedberg | fa4335d | 2015-03-17 13:48:50 +0200 | [diff] [blame] | 33 | #include <net/bluetooth/mgmt.h> |
| 34 | |
| 35 | #include "mgmt_util.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Johan Hedberg | 801c1e8 | 2015-03-06 21:08:50 +0200 | [diff] [blame] | 37 | static LIST_HEAD(mgmt_chan_list); |
| 38 | static DEFINE_MUTEX(mgmt_chan_list_lock); |
| 39 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 40 | static atomic_t monitor_promisc = ATOMIC_INIT(0); |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | /* ----- HCI socket interface ----- */ |
| 43 | |
Marcel Holtmann | 863def5 | 2014-07-11 05:41:00 +0200 | [diff] [blame] | 44 | /* Socket info */ |
| 45 | #define hci_pi(sk) ((struct hci_pinfo *) sk) |
| 46 | |
| 47 | struct hci_pinfo { |
| 48 | struct bt_sock bt; |
| 49 | struct hci_dev *hdev; |
| 50 | struct hci_filter filter; |
| 51 | __u32 cmsg_mask; |
| 52 | unsigned short channel; |
Marcel Holtmann | 6befc64 | 2015-03-14 19:27:53 -0700 | [diff] [blame] | 53 | unsigned long flags; |
Marcel Holtmann | 863def5 | 2014-07-11 05:41:00 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
Marcel Holtmann | 6befc64 | 2015-03-14 19:27:53 -0700 | [diff] [blame] | 56 | void hci_sock_set_flag(struct sock *sk, int nr) |
| 57 | { |
| 58 | set_bit(nr, &hci_pi(sk)->flags); |
| 59 | } |
| 60 | |
| 61 | void hci_sock_clear_flag(struct sock *sk, int nr) |
| 62 | { |
| 63 | clear_bit(nr, &hci_pi(sk)->flags); |
| 64 | } |
| 65 | |
Marcel Holtmann | c85be54 | 2015-03-14 19:28:00 -0700 | [diff] [blame] | 66 | int hci_sock_test_flag(struct sock *sk, int nr) |
| 67 | { |
| 68 | return test_bit(nr, &hci_pi(sk)->flags); |
| 69 | } |
| 70 | |
Johan Hedberg | d0f172b | 2015-03-17 13:48:46 +0200 | [diff] [blame] | 71 | unsigned short hci_sock_get_channel(struct sock *sk) |
| 72 | { |
| 73 | return hci_pi(sk)->channel; |
| 74 | } |
| 75 | |
Jiri Slaby | 9391976 | 2015-02-19 15:20:43 +0100 | [diff] [blame] | 76 | static inline int hci_test_bit(int nr, const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Jiri Slaby | 9391976 | 2015-02-19 15:20:43 +0100 | [diff] [blame] | 78 | return *((const __u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* Security filter */ |
Marcel Holtmann | 3ad254f | 2014-07-11 05:36:39 +0200 | [diff] [blame] | 82 | #define HCI_SFLT_MAX_OGF 5 |
| 83 | |
| 84 | struct hci_sec_filter { |
| 85 | __u32 type_mask; |
| 86 | __u32 event_mask[2]; |
| 87 | __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4]; |
| 88 | }; |
| 89 | |
Marcel Holtmann | 7e67c11 | 2014-07-11 05:36:40 +0200 | [diff] [blame] | 90 | static const struct hci_sec_filter hci_sec_filter = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | /* Packet types */ |
| 92 | 0x10, |
| 93 | /* Events */ |
Marcel Holtmann | dd7f552 | 2005-10-28 19:20:53 +0200 | [diff] [blame] | 94 | { 0x1000d9fe, 0x0000b00c }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | /* Commands */ |
| 96 | { |
| 97 | { 0x0 }, |
| 98 | /* OGF_LINK_CTL */ |
Marcel Holtmann | 7c631a6 | 2007-09-09 08:39:43 +0200 | [diff] [blame] | 99 | { 0xbe000006, 0x00000001, 0x00000000, 0x00 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | /* OGF_LINK_POLICY */ |
Marcel Holtmann | 7c631a6 | 2007-09-09 08:39:43 +0200 | [diff] [blame] | 101 | { 0x00005200, 0x00000000, 0x00000000, 0x00 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | /* OGF_HOST_CTL */ |
Marcel Holtmann | 7c631a6 | 2007-09-09 08:39:43 +0200 | [diff] [blame] | 103 | { 0xaab00200, 0x2b402aaa, 0x05220154, 0x00 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | /* OGF_INFO_PARAM */ |
Marcel Holtmann | 7c631a6 | 2007-09-09 08:39:43 +0200 | [diff] [blame] | 105 | { 0x000002be, 0x00000000, 0x00000000, 0x00 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | /* OGF_STATUS_PARAM */ |
Marcel Holtmann | 7c631a6 | 2007-09-09 08:39:43 +0200 | [diff] [blame] | 107 | { 0x000000ea, 0x00000000, 0x00000000, 0x00 } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | }; |
| 110 | |
| 111 | static struct bt_sock_list hci_sk_list = { |
Robert P. J. Day | d5fb296 | 2008-03-28 16:17:38 -0700 | [diff] [blame] | 112 | .lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
Marcel Holtmann | f81fe64 | 2013-08-25 23:25:15 -0700 | [diff] [blame] | 115 | static bool is_filtered_packet(struct sock *sk, struct sk_buff *skb) |
| 116 | { |
| 117 | struct hci_filter *flt; |
| 118 | int flt_type, flt_event; |
| 119 | |
| 120 | /* Apply filter */ |
| 121 | flt = &hci_pi(sk)->filter; |
| 122 | |
Marcel Holtmann | 8cd4f58 | 2015-10-09 16:13:49 +0200 | [diff] [blame] | 123 | flt_type = bt_cb(skb)->pkt_type & HCI_FLT_TYPE_BITS; |
Marcel Holtmann | f81fe64 | 2013-08-25 23:25:15 -0700 | [diff] [blame] | 124 | |
| 125 | if (!test_bit(flt_type, &flt->type_mask)) |
| 126 | return true; |
| 127 | |
| 128 | /* Extra filter for event packets only */ |
| 129 | if (bt_cb(skb)->pkt_type != HCI_EVENT_PKT) |
| 130 | return false; |
| 131 | |
| 132 | flt_event = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS); |
| 133 | |
| 134 | if (!hci_test_bit(flt_event, &flt->event_mask)) |
| 135 | return true; |
| 136 | |
| 137 | /* Check filter only when opcode is set */ |
| 138 | if (!flt->opcode) |
| 139 | return false; |
| 140 | |
| 141 | if (flt_event == HCI_EV_CMD_COMPLETE && |
| 142 | flt->opcode != get_unaligned((__le16 *)(skb->data + 3))) |
| 143 | return true; |
| 144 | |
| 145 | if (flt_event == HCI_EV_CMD_STATUS && |
| 146 | flt->opcode != get_unaligned((__le16 *)(skb->data + 4))) |
| 147 | return true; |
| 148 | |
| 149 | return false; |
| 150 | } |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* Send frame to RAW socket */ |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 153 | 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] | 154 | { |
| 155 | struct sock *sk; |
Marcel Holtmann | e0edf37 | 2012-02-20 14:50:36 +0100 | [diff] [blame] | 156 | struct sk_buff *skb_copy = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | BT_DBG("hdev %p len %d", hdev, skb->len); |
| 159 | |
| 160 | read_lock(&hci_sk_list.lock); |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 161 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 162 | sk_for_each(sk, &hci_sk_list.head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | struct sk_buff *nskb; |
| 164 | |
| 165 | if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev) |
| 166 | continue; |
| 167 | |
| 168 | /* Don't send frame to the socket it came from */ |
| 169 | if (skb->sk == sk) |
| 170 | continue; |
| 171 | |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 172 | if (hci_pi(sk)->channel == HCI_CHANNEL_RAW) { |
Marcel Holtmann | bb77543 | 2015-10-09 16:13:50 +0200 | [diff] [blame] | 173 | if (bt_cb(skb)->pkt_type != HCI_COMMAND_PKT && |
| 174 | bt_cb(skb)->pkt_type != HCI_EVENT_PKT && |
| 175 | bt_cb(skb)->pkt_type != HCI_ACLDATA_PKT && |
| 176 | bt_cb(skb)->pkt_type != HCI_SCODATA_PKT) |
| 177 | continue; |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 178 | if (is_filtered_packet(sk, skb)) |
| 179 | continue; |
| 180 | } else if (hci_pi(sk)->channel == HCI_CHANNEL_USER) { |
| 181 | if (!bt_cb(skb)->incoming) |
| 182 | continue; |
| 183 | if (bt_cb(skb)->pkt_type != HCI_EVENT_PKT && |
| 184 | bt_cb(skb)->pkt_type != HCI_ACLDATA_PKT && |
| 185 | bt_cb(skb)->pkt_type != HCI_SCODATA_PKT) |
| 186 | continue; |
| 187 | } else { |
| 188 | /* Don't send frame to other channel types */ |
Johan Hedberg | a40c406 | 2010-12-08 00:21:07 +0200 | [diff] [blame] | 189 | continue; |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Marcel Holtmann | e0edf37 | 2012-02-20 14:50:36 +0100 | [diff] [blame] | 192 | if (!skb_copy) { |
| 193 | /* Create a private copy with headroom */ |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 194 | skb_copy = __pskb_copy_fclone(skb, 1, GFP_ATOMIC, true); |
Marcel Holtmann | e0edf37 | 2012-02-20 14:50:36 +0100 | [diff] [blame] | 195 | if (!skb_copy) |
| 196 | continue; |
| 197 | |
| 198 | /* Put type byte before the data */ |
| 199 | memcpy(skb_push(skb_copy, 1), &bt_cb(skb)->pkt_type, 1); |
| 200 | } |
| 201 | |
| 202 | nskb = skb_clone(skb_copy, GFP_ATOMIC); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 203 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | continue; |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | if (sock_queue_rcv_skb(sk, nskb)) |
| 207 | kfree_skb(nskb); |
| 208 | } |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 209 | |
| 210 | read_unlock(&hci_sk_list.lock); |
Marcel Holtmann | e0edf37 | 2012-02-20 14:50:36 +0100 | [diff] [blame] | 211 | |
| 212 | kfree_skb(skb_copy); |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Johan Hedberg | 7129069 | 2015-02-20 13:26:23 +0200 | [diff] [blame] | 215 | /* Send frame to sockets with specific channel */ |
| 216 | void hci_send_to_channel(unsigned short channel, struct sk_buff *skb, |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 217 | int flag, struct sock *skip_sk) |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 218 | { |
| 219 | struct sock *sk; |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 220 | |
Johan Hedberg | 7129069 | 2015-02-20 13:26:23 +0200 | [diff] [blame] | 221 | BT_DBG("channel %u len %d", channel, skb->len); |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 222 | |
| 223 | read_lock(&hci_sk_list.lock); |
| 224 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 225 | sk_for_each(sk, &hci_sk_list.head) { |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 226 | struct sk_buff *nskb; |
| 227 | |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 228 | /* Ignore socket without the flag set */ |
Marcel Holtmann | c85be54 | 2015-03-14 19:28:00 -0700 | [diff] [blame] | 229 | if (!hci_sock_test_flag(sk, flag)) |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 230 | continue; |
| 231 | |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 232 | /* Skip the original socket */ |
| 233 | if (sk == skip_sk) |
| 234 | continue; |
| 235 | |
| 236 | if (sk->sk_state != BT_BOUND) |
| 237 | continue; |
| 238 | |
Johan Hedberg | 7129069 | 2015-02-20 13:26:23 +0200 | [diff] [blame] | 239 | if (hci_pi(sk)->channel != channel) |
Marcel Holtmann | d7f72f6 | 2015-01-11 19:33:32 -0800 | [diff] [blame] | 240 | continue; |
| 241 | |
| 242 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 243 | if (!nskb) |
| 244 | continue; |
| 245 | |
| 246 | if (sock_queue_rcv_skb(sk, nskb)) |
| 247 | kfree_skb(nskb); |
| 248 | } |
| 249 | |
| 250 | read_unlock(&hci_sk_list.lock); |
| 251 | } |
| 252 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 253 | /* Send frame to monitor socket */ |
| 254 | void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb) |
| 255 | { |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 256 | struct sk_buff *skb_copy = NULL; |
Marcel Holtmann | 2b53129 | 2015-01-11 19:33:31 -0800 | [diff] [blame] | 257 | struct hci_mon_hdr *hdr; |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 258 | __le16 opcode; |
| 259 | |
| 260 | if (!atomic_read(&monitor_promisc)) |
| 261 | return; |
| 262 | |
| 263 | BT_DBG("hdev %p len %d", hdev, skb->len); |
| 264 | |
| 265 | switch (bt_cb(skb)->pkt_type) { |
| 266 | case HCI_COMMAND_PKT: |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 267 | opcode = cpu_to_le16(HCI_MON_COMMAND_PKT); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 268 | break; |
| 269 | case HCI_EVENT_PKT: |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 270 | opcode = cpu_to_le16(HCI_MON_EVENT_PKT); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 271 | break; |
| 272 | case HCI_ACLDATA_PKT: |
| 273 | if (bt_cb(skb)->incoming) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 274 | opcode = cpu_to_le16(HCI_MON_ACL_RX_PKT); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 275 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 276 | opcode = cpu_to_le16(HCI_MON_ACL_TX_PKT); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 277 | break; |
| 278 | case HCI_SCODATA_PKT: |
| 279 | if (bt_cb(skb)->incoming) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 280 | opcode = cpu_to_le16(HCI_MON_SCO_RX_PKT); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 281 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 282 | opcode = cpu_to_le16(HCI_MON_SCO_TX_PKT); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 283 | break; |
Marcel Holtmann | e875ff8 | 2015-10-07 16:38:35 +0200 | [diff] [blame] | 284 | case HCI_DIAG_PKT: |
| 285 | opcode = cpu_to_le16(HCI_MON_VENDOR_DIAG); |
| 286 | break; |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 287 | default: |
| 288 | return; |
| 289 | } |
| 290 | |
Marcel Holtmann | 2b53129 | 2015-01-11 19:33:31 -0800 | [diff] [blame] | 291 | /* Create a private copy with headroom */ |
| 292 | skb_copy = __pskb_copy_fclone(skb, HCI_MON_HDR_SIZE, GFP_ATOMIC, true); |
| 293 | if (!skb_copy) |
| 294 | return; |
| 295 | |
| 296 | /* Put header before the data */ |
| 297 | hdr = (void *) skb_push(skb_copy, HCI_MON_HDR_SIZE); |
| 298 | hdr->opcode = opcode; |
| 299 | hdr->index = cpu_to_le16(hdev->id); |
| 300 | hdr->len = cpu_to_le16(skb->len); |
| 301 | |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 302 | hci_send_to_channel(HCI_CHANNEL_MONITOR, skb_copy, |
| 303 | HCI_SOCK_TRUSTED, NULL); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 304 | kfree_skb(skb_copy); |
| 305 | } |
| 306 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 307 | static struct sk_buff *create_monitor_event(struct hci_dev *hdev, int event) |
| 308 | { |
| 309 | struct hci_mon_hdr *hdr; |
| 310 | struct hci_mon_new_index *ni; |
Marcel Holtmann | 6c566dd | 2015-10-07 15:32:13 +0200 | [diff] [blame] | 311 | struct hci_mon_index_info *ii; |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 312 | struct sk_buff *skb; |
| 313 | __le16 opcode; |
| 314 | |
| 315 | switch (event) { |
| 316 | case HCI_DEV_REG: |
| 317 | skb = bt_skb_alloc(HCI_MON_NEW_INDEX_SIZE, GFP_ATOMIC); |
| 318 | if (!skb) |
| 319 | return NULL; |
| 320 | |
Marcel Holtmann | 6c566dd | 2015-10-07 15:32:13 +0200 | [diff] [blame] | 321 | ni = (void *)skb_put(skb, HCI_MON_NEW_INDEX_SIZE); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 322 | ni->type = hdev->dev_type; |
| 323 | ni->bus = hdev->bus; |
| 324 | bacpy(&ni->bdaddr, &hdev->bdaddr); |
| 325 | memcpy(ni->name, hdev->name, 8); |
| 326 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 327 | opcode = cpu_to_le16(HCI_MON_NEW_INDEX); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 328 | break; |
| 329 | |
| 330 | case HCI_DEV_UNREG: |
| 331 | skb = bt_skb_alloc(0, GFP_ATOMIC); |
| 332 | if (!skb) |
| 333 | return NULL; |
| 334 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 335 | opcode = cpu_to_le16(HCI_MON_DEL_INDEX); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 336 | break; |
| 337 | |
Marcel Holtmann | e131d74 | 2015-10-20 02:30:47 +0200 | [diff] [blame] | 338 | case HCI_DEV_SETUP: |
| 339 | if (hdev->manufacturer == 0xffff) |
| 340 | return NULL; |
| 341 | |
| 342 | /* fall through */ |
| 343 | |
Marcel Holtmann | 6c566dd | 2015-10-07 15:32:13 +0200 | [diff] [blame] | 344 | case HCI_DEV_UP: |
| 345 | skb = bt_skb_alloc(HCI_MON_INDEX_INFO_SIZE, GFP_ATOMIC); |
| 346 | if (!skb) |
| 347 | return NULL; |
| 348 | |
| 349 | ii = (void *)skb_put(skb, HCI_MON_INDEX_INFO_SIZE); |
| 350 | bacpy(&ii->bdaddr, &hdev->bdaddr); |
| 351 | ii->manufacturer = cpu_to_le16(hdev->manufacturer); |
| 352 | |
| 353 | opcode = cpu_to_le16(HCI_MON_INDEX_INFO); |
| 354 | break; |
| 355 | |
Marcel Holtmann | 22db3cbc | 2015-10-04 23:34:03 +0200 | [diff] [blame] | 356 | case HCI_DEV_OPEN: |
| 357 | skb = bt_skb_alloc(0, GFP_ATOMIC); |
| 358 | if (!skb) |
| 359 | return NULL; |
| 360 | |
| 361 | opcode = cpu_to_le16(HCI_MON_OPEN_INDEX); |
| 362 | break; |
| 363 | |
| 364 | case HCI_DEV_CLOSE: |
| 365 | skb = bt_skb_alloc(0, GFP_ATOMIC); |
| 366 | if (!skb) |
| 367 | return NULL; |
| 368 | |
| 369 | opcode = cpu_to_le16(HCI_MON_CLOSE_INDEX); |
| 370 | break; |
| 371 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 372 | default: |
| 373 | return NULL; |
| 374 | } |
| 375 | |
| 376 | __net_timestamp(skb); |
| 377 | |
| 378 | hdr = (void *) skb_push(skb, HCI_MON_HDR_SIZE); |
| 379 | hdr->opcode = opcode; |
| 380 | hdr->index = cpu_to_le16(hdev->id); |
| 381 | hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE); |
| 382 | |
| 383 | return skb; |
| 384 | } |
| 385 | |
| 386 | static void send_monitor_replay(struct sock *sk) |
| 387 | { |
| 388 | struct hci_dev *hdev; |
| 389 | |
| 390 | read_lock(&hci_dev_list_lock); |
| 391 | |
| 392 | list_for_each_entry(hdev, &hci_dev_list, list) { |
| 393 | struct sk_buff *skb; |
| 394 | |
| 395 | skb = create_monitor_event(hdev, HCI_DEV_REG); |
| 396 | if (!skb) |
| 397 | continue; |
| 398 | |
| 399 | if (sock_queue_rcv_skb(sk, skb)) |
| 400 | kfree_skb(skb); |
Marcel Holtmann | 22db3cbc | 2015-10-04 23:34:03 +0200 | [diff] [blame] | 401 | |
| 402 | if (!test_bit(HCI_RUNNING, &hdev->flags)) |
| 403 | continue; |
| 404 | |
| 405 | skb = create_monitor_event(hdev, HCI_DEV_OPEN); |
| 406 | if (!skb) |
| 407 | continue; |
| 408 | |
| 409 | if (sock_queue_rcv_skb(sk, skb)) |
| 410 | kfree_skb(skb); |
Marcel Holtmann | 6c566dd | 2015-10-07 15:32:13 +0200 | [diff] [blame] | 411 | |
Marcel Holtmann | e131d74 | 2015-10-20 02:30:47 +0200 | [diff] [blame] | 412 | if (test_bit(HCI_UP, &hdev->flags)) |
| 413 | skb = create_monitor_event(hdev, HCI_DEV_UP); |
| 414 | else if (hci_dev_test_flag(hdev, HCI_SETUP)) |
| 415 | skb = create_monitor_event(hdev, HCI_DEV_SETUP); |
| 416 | else |
| 417 | skb = NULL; |
Marcel Holtmann | 6c566dd | 2015-10-07 15:32:13 +0200 | [diff] [blame] | 418 | |
Marcel Holtmann | e131d74 | 2015-10-20 02:30:47 +0200 | [diff] [blame] | 419 | if (skb) { |
| 420 | if (sock_queue_rcv_skb(sk, skb)) |
| 421 | kfree_skb(skb); |
| 422 | } |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | read_unlock(&hci_dev_list_lock); |
| 426 | } |
| 427 | |
Marcel Holtmann | 040030e | 2012-02-20 14:50:37 +0100 | [diff] [blame] | 428 | /* Generate internal stack event */ |
| 429 | static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data) |
| 430 | { |
| 431 | struct hci_event_hdr *hdr; |
| 432 | struct hci_ev_stack_internal *ev; |
| 433 | struct sk_buff *skb; |
| 434 | |
| 435 | skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC); |
| 436 | if (!skb) |
| 437 | return; |
| 438 | |
| 439 | hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE); |
| 440 | hdr->evt = HCI_EV_STACK_INTERNAL; |
| 441 | hdr->plen = sizeof(*ev) + dlen; |
| 442 | |
| 443 | ev = (void *) skb_put(skb, sizeof(*ev) + dlen); |
| 444 | ev->type = type; |
| 445 | memcpy(ev->data, data, dlen); |
| 446 | |
| 447 | bt_cb(skb)->incoming = 1; |
| 448 | __net_timestamp(skb); |
| 449 | |
| 450 | bt_cb(skb)->pkt_type = HCI_EVENT_PKT; |
Marcel Holtmann | 040030e | 2012-02-20 14:50:37 +0100 | [diff] [blame] | 451 | hci_send_to_sock(hdev, skb); |
| 452 | kfree_skb(skb); |
| 453 | } |
| 454 | |
| 455 | void hci_sock_dev_event(struct hci_dev *hdev, int event) |
| 456 | { |
Marcel Holtmann | 040030e | 2012-02-20 14:50:37 +0100 | [diff] [blame] | 457 | BT_DBG("hdev %s event %d", hdev->name, event); |
| 458 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 459 | if (atomic_read(&monitor_promisc)) { |
| 460 | struct sk_buff *skb; |
| 461 | |
Marcel Holtmann | ed1b28a | 2015-10-04 23:33:59 +0200 | [diff] [blame] | 462 | /* Send event to monitor */ |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 463 | skb = create_monitor_event(hdev, event); |
| 464 | if (skb) { |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 465 | hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, |
| 466 | HCI_SOCK_TRUSTED, NULL); |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 467 | kfree_skb(skb); |
| 468 | } |
| 469 | } |
| 470 | |
Marcel Holtmann | ed1b28a | 2015-10-04 23:33:59 +0200 | [diff] [blame] | 471 | if (event <= HCI_DEV_DOWN) { |
| 472 | struct hci_ev_si_device ev; |
| 473 | |
| 474 | /* Send event to sockets */ |
| 475 | ev.event = event; |
| 476 | ev.dev_id = hdev->id; |
| 477 | hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev); |
| 478 | } |
Marcel Holtmann | 040030e | 2012-02-20 14:50:37 +0100 | [diff] [blame] | 479 | |
| 480 | if (event == HCI_DEV_UNREG) { |
| 481 | struct sock *sk; |
Marcel Holtmann | 040030e | 2012-02-20 14:50:37 +0100 | [diff] [blame] | 482 | |
| 483 | /* Detach sockets from device */ |
| 484 | read_lock(&hci_sk_list.lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 485 | sk_for_each(sk, &hci_sk_list.head) { |
Marcel Holtmann | 040030e | 2012-02-20 14:50:37 +0100 | [diff] [blame] | 486 | bh_lock_sock_nested(sk); |
| 487 | if (hci_pi(sk)->hdev == hdev) { |
| 488 | hci_pi(sk)->hdev = NULL; |
| 489 | sk->sk_err = EPIPE; |
| 490 | sk->sk_state = BT_OPEN; |
| 491 | sk->sk_state_change(sk); |
| 492 | |
| 493 | hci_dev_put(hdev); |
| 494 | } |
| 495 | bh_unlock_sock(sk); |
| 496 | } |
| 497 | read_unlock(&hci_sk_list.lock); |
| 498 | } |
| 499 | } |
| 500 | |
Johan Hedberg | 801c1e8 | 2015-03-06 21:08:50 +0200 | [diff] [blame] | 501 | static struct hci_mgmt_chan *__hci_mgmt_chan_find(unsigned short channel) |
| 502 | { |
| 503 | struct hci_mgmt_chan *c; |
| 504 | |
| 505 | list_for_each_entry(c, &mgmt_chan_list, list) { |
| 506 | if (c->channel == channel) |
| 507 | return c; |
| 508 | } |
| 509 | |
| 510 | return NULL; |
| 511 | } |
| 512 | |
| 513 | static struct hci_mgmt_chan *hci_mgmt_chan_find(unsigned short channel) |
| 514 | { |
| 515 | struct hci_mgmt_chan *c; |
| 516 | |
| 517 | mutex_lock(&mgmt_chan_list_lock); |
| 518 | c = __hci_mgmt_chan_find(channel); |
| 519 | mutex_unlock(&mgmt_chan_list_lock); |
| 520 | |
| 521 | return c; |
| 522 | } |
| 523 | |
| 524 | int hci_mgmt_chan_register(struct hci_mgmt_chan *c) |
| 525 | { |
| 526 | if (c->channel < HCI_CHANNEL_CONTROL) |
| 527 | return -EINVAL; |
| 528 | |
| 529 | mutex_lock(&mgmt_chan_list_lock); |
| 530 | if (__hci_mgmt_chan_find(c->channel)) { |
| 531 | mutex_unlock(&mgmt_chan_list_lock); |
| 532 | return -EALREADY; |
| 533 | } |
| 534 | |
| 535 | list_add_tail(&c->list, &mgmt_chan_list); |
| 536 | |
| 537 | mutex_unlock(&mgmt_chan_list_lock); |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | EXPORT_SYMBOL(hci_mgmt_chan_register); |
| 542 | |
| 543 | void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c) |
| 544 | { |
| 545 | mutex_lock(&mgmt_chan_list_lock); |
| 546 | list_del(&c->list); |
| 547 | mutex_unlock(&mgmt_chan_list_lock); |
| 548 | } |
| 549 | EXPORT_SYMBOL(hci_mgmt_chan_unregister); |
| 550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | static int hci_sock_release(struct socket *sock) |
| 552 | { |
| 553 | struct sock *sk = sock->sk; |
Marcel Holtmann | 7b005bd | 2006-02-13 11:40:03 +0100 | [diff] [blame] | 554 | struct hci_dev *hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
| 556 | BT_DBG("sock %p sk %p", sock, sk); |
| 557 | |
| 558 | if (!sk) |
| 559 | return 0; |
| 560 | |
Marcel Holtmann | 7b005bd | 2006-02-13 11:40:03 +0100 | [diff] [blame] | 561 | hdev = hci_pi(sk)->hdev; |
| 562 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 563 | if (hci_pi(sk)->channel == HCI_CHANNEL_MONITOR) |
| 564 | atomic_dec(&monitor_promisc); |
| 565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | bt_sock_unlink(&hci_sk_list, sk); |
| 567 | |
| 568 | if (hdev) { |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 569 | if (hci_pi(sk)->channel == HCI_CHANNEL_USER) { |
Simon Fels | 6b3cc1d | 2015-09-02 12:10:12 +0200 | [diff] [blame] | 570 | /* When releasing an user channel exclusive access, |
| 571 | * call hci_dev_do_close directly instead of calling |
| 572 | * hci_dev_close to ensure the exclusive access will |
| 573 | * be released and the controller brought back down. |
| 574 | * |
| 575 | * The checking of HCI_AUTO_OFF is not needed in this |
| 576 | * case since it will have been cleared already when |
| 577 | * opening the user channel. |
| 578 | */ |
| 579 | hci_dev_do_close(hdev); |
Loic Poulain | 9380f9e | 2015-05-21 16:46:41 +0200 | [diff] [blame] | 580 | hci_dev_clear_flag(hdev, HCI_USER_CHANNEL); |
| 581 | mgmt_index_added(hdev); |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | atomic_dec(&hdev->promisc); |
| 585 | hci_dev_put(hdev); |
| 586 | } |
| 587 | |
| 588 | sock_orphan(sk); |
| 589 | |
| 590 | skb_queue_purge(&sk->sk_receive_queue); |
| 591 | skb_queue_purge(&sk->sk_write_queue); |
| 592 | |
| 593 | sock_put(sk); |
| 594 | return 0; |
| 595 | } |
| 596 | |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 597 | 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] | 598 | { |
| 599 | bdaddr_t bdaddr; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 600 | int err; |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 601 | |
| 602 | if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) |
| 603 | return -EFAULT; |
| 604 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 605 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 606 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 607 | err = hci_bdaddr_list_add(&hdev->blacklist, &bdaddr, BDADDR_BREDR); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 608 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 609 | hci_dev_unlock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 610 | |
| 611 | return err; |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 614 | 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] | 615 | { |
| 616 | bdaddr_t bdaddr; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 617 | int err; |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 618 | |
| 619 | if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) |
| 620 | return -EFAULT; |
| 621 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 622 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 623 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 624 | err = hci_bdaddr_list_del(&hdev->blacklist, &bdaddr, BDADDR_BREDR); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 625 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 626 | hci_dev_unlock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 627 | |
| 628 | return err; |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 629 | } |
| 630 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 631 | /* Ioctls that require bound socket */ |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 632 | static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd, |
| 633 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | { |
| 635 | struct hci_dev *hdev = hci_pi(sk)->hdev; |
| 636 | |
| 637 | if (!hdev) |
| 638 | return -EBADFD; |
| 639 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 640 | if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 641 | return -EBUSY; |
| 642 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 643 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 644 | return -EOPNOTSUPP; |
| 645 | |
Marcel Holtmann | 5b69bef5 | 2013-10-10 10:02:08 -0700 | [diff] [blame] | 646 | if (hdev->dev_type != HCI_BREDR) |
| 647 | return -EOPNOTSUPP; |
| 648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | switch (cmd) { |
| 650 | case HCISETRAW: |
| 651 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 652 | return -EPERM; |
Marcel Holtmann | db59668 | 2014-04-16 20:04:38 -0700 | [diff] [blame] | 653 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | case HCIGETCONNINFO: |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 656 | return hci_get_conn_info(hdev, (void __user *) arg); |
| 657 | |
| 658 | case HCIGETAUTHINFO: |
| 659 | return hci_get_auth_info(hdev, (void __user *) arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 661 | case HCIBLOCKADDR: |
| 662 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 663 | return -EPERM; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 664 | return hci_sock_blacklist_add(hdev, (void __user *) arg); |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 665 | |
| 666 | case HCIUNBLOCKADDR: |
| 667 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 668 | return -EPERM; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 669 | return hci_sock_blacklist_del(hdev, (void __user *) arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 671 | |
Marcel Holtmann | 324d36e | 2013-10-10 10:50:06 -0700 | [diff] [blame] | 672 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 675 | static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, |
| 676 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | { |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 678 | void __user *argp = (void __user *) arg; |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 679 | struct sock *sk = sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | int err; |
| 681 | |
| 682 | BT_DBG("cmd %x arg %lx", cmd, arg); |
| 683 | |
Marcel Holtmann | c1c4f95 | 2013-08-26 09:39:55 -0700 | [diff] [blame] | 684 | lock_sock(sk); |
| 685 | |
| 686 | if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { |
| 687 | err = -EBADFD; |
| 688 | goto done; |
| 689 | } |
| 690 | |
| 691 | release_sock(sk); |
| 692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | switch (cmd) { |
| 694 | case HCIGETDEVLIST: |
| 695 | return hci_get_dev_list(argp); |
| 696 | |
| 697 | case HCIGETDEVINFO: |
| 698 | return hci_get_dev_info(argp); |
| 699 | |
| 700 | case HCIGETCONNLIST: |
| 701 | return hci_get_conn_list(argp); |
| 702 | |
| 703 | case HCIDEVUP: |
| 704 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 705 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | return hci_dev_open(arg); |
| 707 | |
| 708 | case HCIDEVDOWN: |
| 709 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 710 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | return hci_dev_close(arg); |
| 712 | |
| 713 | case HCIDEVRESET: |
| 714 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 715 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | return hci_dev_reset(arg); |
| 717 | |
| 718 | case HCIDEVRESTAT: |
| 719 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 720 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | return hci_dev_reset_stat(arg); |
| 722 | |
| 723 | case HCISETSCAN: |
| 724 | case HCISETAUTH: |
| 725 | case HCISETENCRYPT: |
| 726 | case HCISETPTYPE: |
| 727 | case HCISETLINKPOL: |
| 728 | case HCISETLINKMODE: |
| 729 | case HCISETACLMTU: |
| 730 | case HCISETSCOMTU: |
| 731 | if (!capable(CAP_NET_ADMIN)) |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 732 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | return hci_dev_cmd(cmd, argp); |
| 734 | |
| 735 | case HCIINQUIRY: |
| 736 | return hci_inquiry(argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } |
Marcel Holtmann | c1c4f95 | 2013-08-26 09:39:55 -0700 | [diff] [blame] | 738 | |
| 739 | lock_sock(sk); |
| 740 | |
| 741 | err = hci_sock_bound_ioctl(sk, cmd, arg); |
| 742 | |
| 743 | done: |
| 744 | release_sock(sk); |
| 745 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 748 | static int hci_sock_bind(struct socket *sock, struct sockaddr *addr, |
| 749 | int addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 751 | struct sockaddr_hci haddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | struct sock *sk = sock->sk; |
| 753 | struct hci_dev *hdev = NULL; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 754 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
| 756 | BT_DBG("sock %p sk %p", sock, sk); |
| 757 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 758 | if (!addr) |
| 759 | return -EINVAL; |
| 760 | |
| 761 | memset(&haddr, 0, sizeof(haddr)); |
| 762 | len = min_t(unsigned int, sizeof(haddr), addr_len); |
| 763 | memcpy(&haddr, addr, len); |
| 764 | |
| 765 | if (haddr.hci_family != AF_BLUETOOTH) |
| 766 | return -EINVAL; |
| 767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | lock_sock(sk); |
| 769 | |
Marcel Holtmann | 7cc2ade | 2012-02-20 14:50:35 +0100 | [diff] [blame] | 770 | if (sk->sk_state == BT_BOUND) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | err = -EALREADY; |
| 772 | goto done; |
| 773 | } |
| 774 | |
Marcel Holtmann | 7cc2ade | 2012-02-20 14:50:35 +0100 | [diff] [blame] | 775 | switch (haddr.hci_channel) { |
| 776 | case HCI_CHANNEL_RAW: |
| 777 | if (hci_pi(sk)->hdev) { |
| 778 | err = -EALREADY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | goto done; |
| 780 | } |
| 781 | |
Marcel Holtmann | 7cc2ade | 2012-02-20 14:50:35 +0100 | [diff] [blame] | 782 | if (haddr.hci_dev != HCI_DEV_NONE) { |
| 783 | hdev = hci_dev_get(haddr.hci_dev); |
| 784 | if (!hdev) { |
| 785 | err = -ENODEV; |
| 786 | goto done; |
| 787 | } |
| 788 | |
| 789 | atomic_inc(&hdev->promisc); |
| 790 | } |
| 791 | |
| 792 | hci_pi(sk)->hdev = hdev; |
| 793 | break; |
| 794 | |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 795 | case HCI_CHANNEL_USER: |
| 796 | if (hci_pi(sk)->hdev) { |
| 797 | err = -EALREADY; |
| 798 | goto done; |
| 799 | } |
| 800 | |
| 801 | if (haddr.hci_dev == HCI_DEV_NONE) { |
| 802 | err = -EINVAL; |
| 803 | goto done; |
| 804 | } |
| 805 | |
Marcel Holtmann | 10a8b86 | 2013-10-01 22:59:24 -0700 | [diff] [blame] | 806 | if (!capable(CAP_NET_ADMIN)) { |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 807 | err = -EPERM; |
| 808 | goto done; |
| 809 | } |
| 810 | |
| 811 | hdev = hci_dev_get(haddr.hci_dev); |
| 812 | if (!hdev) { |
| 813 | err = -ENODEV; |
| 814 | goto done; |
| 815 | } |
| 816 | |
Marcel Holtmann | 781f899 | 2015-06-06 06:06:49 +0200 | [diff] [blame] | 817 | if (test_bit(HCI_INIT, &hdev->flags) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 818 | hci_dev_test_flag(hdev, HCI_SETUP) || |
Marcel Holtmann | 781f899 | 2015-06-06 06:06:49 +0200 | [diff] [blame] | 819 | hci_dev_test_flag(hdev, HCI_CONFIG) || |
| 820 | (!hci_dev_test_flag(hdev, HCI_AUTO_OFF) && |
| 821 | test_bit(HCI_UP, &hdev->flags))) { |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 822 | err = -EBUSY; |
| 823 | hci_dev_put(hdev); |
| 824 | goto done; |
| 825 | } |
| 826 | |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 827 | if (hci_dev_test_and_set_flag(hdev, HCI_USER_CHANNEL)) { |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 828 | err = -EUSERS; |
| 829 | hci_dev_put(hdev); |
| 830 | goto done; |
| 831 | } |
| 832 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 833 | mgmt_index_removed(hdev); |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 834 | |
| 835 | err = hci_dev_open(hdev->id); |
| 836 | if (err) { |
Marcel Holtmann | 781f899 | 2015-06-06 06:06:49 +0200 | [diff] [blame] | 837 | if (err == -EALREADY) { |
| 838 | /* In case the transport is already up and |
| 839 | * running, clear the error here. |
| 840 | * |
| 841 | * This can happen when opening an user |
| 842 | * channel and HCI_AUTO_OFF grace period |
| 843 | * is still active. |
| 844 | */ |
| 845 | err = 0; |
| 846 | } else { |
| 847 | hci_dev_clear_flag(hdev, HCI_USER_CHANNEL); |
| 848 | mgmt_index_added(hdev); |
| 849 | hci_dev_put(hdev); |
| 850 | goto done; |
| 851 | } |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | atomic_inc(&hdev->promisc); |
| 855 | |
| 856 | hci_pi(sk)->hdev = hdev; |
| 857 | break; |
| 858 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 859 | case HCI_CHANNEL_MONITOR: |
| 860 | if (haddr.hci_dev != HCI_DEV_NONE) { |
| 861 | err = -EINVAL; |
| 862 | goto done; |
| 863 | } |
| 864 | |
| 865 | if (!capable(CAP_NET_RAW)) { |
| 866 | err = -EPERM; |
| 867 | goto done; |
| 868 | } |
| 869 | |
Marcel Holtmann | 50ebc05 | 2015-03-14 19:27:58 -0700 | [diff] [blame] | 870 | /* The monitor interface is restricted to CAP_NET_RAW |
| 871 | * capabilities and with that implicitly trusted. |
| 872 | */ |
| 873 | hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); |
| 874 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 875 | send_monitor_replay(sk); |
| 876 | |
| 877 | atomic_inc(&monitor_promisc); |
| 878 | break; |
| 879 | |
Marcel Holtmann | 7cc2ade | 2012-02-20 14:50:35 +0100 | [diff] [blame] | 880 | default: |
Johan Hedberg | 801c1e8 | 2015-03-06 21:08:50 +0200 | [diff] [blame] | 881 | if (!hci_mgmt_chan_find(haddr.hci_channel)) { |
| 882 | err = -EINVAL; |
| 883 | goto done; |
| 884 | } |
| 885 | |
| 886 | if (haddr.hci_dev != HCI_DEV_NONE) { |
| 887 | err = -EINVAL; |
| 888 | goto done; |
| 889 | } |
| 890 | |
Marcel Holtmann | 1195fbb | 2015-03-14 19:28:04 -0700 | [diff] [blame] | 891 | /* Users with CAP_NET_ADMIN capabilities are allowed |
| 892 | * access to all management commands and events. For |
| 893 | * untrusted users the interface is restricted and |
| 894 | * also only untrusted events are sent. |
Marcel Holtmann | 50ebc05 | 2015-03-14 19:27:58 -0700 | [diff] [blame] | 895 | */ |
Marcel Holtmann | 1195fbb | 2015-03-14 19:28:04 -0700 | [diff] [blame] | 896 | if (capable(CAP_NET_ADMIN)) |
| 897 | hci_sock_set_flag(sk, HCI_SOCK_TRUSTED); |
Marcel Holtmann | 50ebc05 | 2015-03-14 19:27:58 -0700 | [diff] [blame] | 898 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 899 | /* At the moment the index and unconfigured index events |
| 900 | * are enabled unconditionally. Setting them on each |
| 901 | * socket when binding keeps this functionality. They |
| 902 | * however might be cleared later and then sending of these |
| 903 | * events will be disabled, but that is then intentional. |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 904 | * |
| 905 | * This also enables generic events that are safe to be |
| 906 | * received by untrusted users. Example for such events |
| 907 | * are changes to settings, class of device, name etc. |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 908 | */ |
| 909 | if (haddr.hci_channel == HCI_CHANNEL_CONTROL) { |
| 910 | hci_sock_set_flag(sk, HCI_MGMT_INDEX_EVENTS); |
| 911 | hci_sock_set_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 912 | hci_sock_set_flag(sk, HCI_MGMT_GENERIC_EVENTS); |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 913 | } |
Johan Hedberg | 801c1e8 | 2015-03-06 21:08:50 +0200 | [diff] [blame] | 914 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Marcel Holtmann | 7cc2ade | 2012-02-20 14:50:35 +0100 | [diff] [blame] | 917 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 918 | hci_pi(sk)->channel = haddr.hci_channel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | sk->sk_state = BT_BOUND; |
| 920 | |
| 921 | done: |
| 922 | release_sock(sk); |
| 923 | return err; |
| 924 | } |
| 925 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 926 | static int hci_sock_getname(struct socket *sock, struct sockaddr *addr, |
| 927 | int *addr_len, int peer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | { |
| 929 | struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr; |
| 930 | struct sock *sk = sock->sk; |
Marcel Holtmann | 9d4b68b | 2013-08-26 00:20:37 -0700 | [diff] [blame] | 931 | struct hci_dev *hdev; |
| 932 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | |
| 934 | BT_DBG("sock %p sk %p", sock, sk); |
| 935 | |
Marcel Holtmann | 06f43cb | 2013-08-26 00:06:30 -0700 | [diff] [blame] | 936 | if (peer) |
| 937 | return -EOPNOTSUPP; |
| 938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | lock_sock(sk); |
| 940 | |
Marcel Holtmann | 9d4b68b | 2013-08-26 00:20:37 -0700 | [diff] [blame] | 941 | hdev = hci_pi(sk)->hdev; |
| 942 | if (!hdev) { |
| 943 | err = -EBADFD; |
| 944 | goto done; |
| 945 | } |
| 946 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | *addr_len = sizeof(*haddr); |
| 948 | haddr->hci_family = AF_BLUETOOTH; |
Marcel Holtmann | 7b005bd | 2006-02-13 11:40:03 +0100 | [diff] [blame] | 949 | haddr->hci_dev = hdev->id; |
Marcel Holtmann | 9d4b68b | 2013-08-26 00:20:37 -0700 | [diff] [blame] | 950 | haddr->hci_channel= hci_pi(sk)->channel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
Marcel Holtmann | 9d4b68b | 2013-08-26 00:20:37 -0700 | [diff] [blame] | 952 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | release_sock(sk); |
Marcel Holtmann | 9d4b68b | 2013-08-26 00:20:37 -0700 | [diff] [blame] | 954 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 957 | static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg, |
| 958 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | { |
| 960 | __u32 mask = hci_pi(sk)->cmsg_mask; |
| 961 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 962 | if (mask & HCI_CMSG_DIR) { |
| 963 | int incoming = bt_cb(skb)->incoming; |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 964 | put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming), |
| 965 | &incoming); |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 968 | if (mask & HCI_CMSG_TSTAMP) { |
Johann Felix Soden | f6e623a | 2010-02-15 22:23:48 +0100 | [diff] [blame] | 969 | #ifdef CONFIG_COMPAT |
| 970 | struct compat_timeval ctv; |
| 971 | #endif |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 972 | struct timeval tv; |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 973 | void *data; |
| 974 | int len; |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 975 | |
| 976 | skb_get_timestamp(skb, &tv); |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 977 | |
David S. Miller | 1da97f8 | 2007-09-12 14:10:58 +0200 | [diff] [blame] | 978 | data = &tv; |
| 979 | len = sizeof(tv); |
| 980 | #ifdef CONFIG_COMPAT |
H. J. Lu | da88cea | 2012-02-10 14:12:15 -0800 | [diff] [blame] | 981 | if (!COMPAT_USE_64BIT_TIME && |
| 982 | (msg->msg_flags & MSG_CMSG_COMPAT)) { |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 983 | ctv.tv_sec = tv.tv_sec; |
| 984 | ctv.tv_usec = tv.tv_usec; |
| 985 | data = &ctv; |
| 986 | len = sizeof(ctv); |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 987 | } |
David S. Miller | 1da97f8 | 2007-09-12 14:10:58 +0200 | [diff] [blame] | 988 | #endif |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 989 | |
| 990 | put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data); |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 991 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 993 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 994 | static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, |
| 995 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | { |
| 997 | int noblock = flags & MSG_DONTWAIT; |
| 998 | struct sock *sk = sock->sk; |
| 999 | struct sk_buff *skb; |
| 1000 | int copied, err; |
| 1001 | |
| 1002 | BT_DBG("sock %p, sk %p", sock, sk); |
| 1003 | |
Marcel Holtmann | d94a610 | 2015-10-25 22:45:18 +0100 | [diff] [blame^] | 1004 | if (flags & MSG_OOB) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | return -EOPNOTSUPP; |
| 1006 | |
| 1007 | if (sk->sk_state == BT_CLOSED) |
| 1008 | return 0; |
| 1009 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1010 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 1011 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | return err; |
| 1013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | copied = skb->len; |
| 1015 | if (len < copied) { |
| 1016 | msg->msg_flags |= MSG_TRUNC; |
| 1017 | copied = len; |
| 1018 | } |
| 1019 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 1020 | skb_reset_transport_header(skb); |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 1021 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | |
Marcel Holtmann | 3a20862 | 2012-02-20 14:50:34 +0100 | [diff] [blame] | 1023 | switch (hci_pi(sk)->channel) { |
| 1024 | case HCI_CHANNEL_RAW: |
| 1025 | hci_sock_cmsg(sk, msg, skb); |
| 1026 | break; |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 1027 | case HCI_CHANNEL_USER: |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 1028 | case HCI_CHANNEL_MONITOR: |
| 1029 | sock_recv_timestamp(msg, sk, skb); |
| 1030 | break; |
Johan Hedberg | 801c1e8 | 2015-03-06 21:08:50 +0200 | [diff] [blame] | 1031 | default: |
| 1032 | if (hci_mgmt_chan_find(hci_pi(sk)->channel)) |
| 1033 | sock_recv_timestamp(msg, sk, skb); |
| 1034 | break; |
Marcel Holtmann | 3a20862 | 2012-02-20 14:50:34 +0100 | [diff] [blame] | 1035 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | |
| 1037 | skb_free_datagram(sk, skb); |
| 1038 | |
| 1039 | return err ? : copied; |
| 1040 | } |
| 1041 | |
Johan Hedberg | fa4335d | 2015-03-17 13:48:50 +0200 | [diff] [blame] | 1042 | static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk, |
| 1043 | struct msghdr *msg, size_t msglen) |
| 1044 | { |
| 1045 | void *buf; |
| 1046 | u8 *cp; |
| 1047 | struct mgmt_hdr *hdr; |
| 1048 | u16 opcode, index, len; |
| 1049 | struct hci_dev *hdev = NULL; |
| 1050 | const struct hci_mgmt_handler *handler; |
| 1051 | bool var_len, no_hdev; |
| 1052 | int err; |
| 1053 | |
| 1054 | BT_DBG("got %zu bytes", msglen); |
| 1055 | |
| 1056 | if (msglen < sizeof(*hdr)) |
| 1057 | return -EINVAL; |
| 1058 | |
| 1059 | buf = kmalloc(msglen, GFP_KERNEL); |
| 1060 | if (!buf) |
| 1061 | return -ENOMEM; |
| 1062 | |
| 1063 | if (memcpy_from_msg(buf, msg, msglen)) { |
| 1064 | err = -EFAULT; |
| 1065 | goto done; |
| 1066 | } |
| 1067 | |
| 1068 | hdr = buf; |
| 1069 | opcode = __le16_to_cpu(hdr->opcode); |
| 1070 | index = __le16_to_cpu(hdr->index); |
| 1071 | len = __le16_to_cpu(hdr->len); |
| 1072 | |
| 1073 | if (len != msglen - sizeof(*hdr)) { |
| 1074 | err = -EINVAL; |
| 1075 | goto done; |
| 1076 | } |
| 1077 | |
| 1078 | if (opcode >= chan->handler_count || |
| 1079 | chan->handlers[opcode].func == NULL) { |
| 1080 | BT_DBG("Unknown op %u", opcode); |
| 1081 | err = mgmt_cmd_status(sk, index, opcode, |
| 1082 | MGMT_STATUS_UNKNOWN_COMMAND); |
| 1083 | goto done; |
| 1084 | } |
| 1085 | |
| 1086 | handler = &chan->handlers[opcode]; |
| 1087 | |
| 1088 | if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) && |
| 1089 | !(handler->flags & HCI_MGMT_UNTRUSTED)) { |
| 1090 | err = mgmt_cmd_status(sk, index, opcode, |
| 1091 | MGMT_STATUS_PERMISSION_DENIED); |
| 1092 | goto done; |
| 1093 | } |
| 1094 | |
| 1095 | if (index != MGMT_INDEX_NONE) { |
| 1096 | hdev = hci_dev_get(index); |
| 1097 | if (!hdev) { |
| 1098 | err = mgmt_cmd_status(sk, index, opcode, |
| 1099 | MGMT_STATUS_INVALID_INDEX); |
| 1100 | goto done; |
| 1101 | } |
| 1102 | |
| 1103 | if (hci_dev_test_flag(hdev, HCI_SETUP) || |
| 1104 | hci_dev_test_flag(hdev, HCI_CONFIG) || |
| 1105 | hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
| 1106 | err = mgmt_cmd_status(sk, index, opcode, |
| 1107 | MGMT_STATUS_INVALID_INDEX); |
| 1108 | goto done; |
| 1109 | } |
| 1110 | |
| 1111 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && |
| 1112 | !(handler->flags & HCI_MGMT_UNCONFIGURED)) { |
| 1113 | err = mgmt_cmd_status(sk, index, opcode, |
| 1114 | MGMT_STATUS_INVALID_INDEX); |
| 1115 | goto done; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | no_hdev = (handler->flags & HCI_MGMT_NO_HDEV); |
| 1120 | if (no_hdev != !hdev) { |
| 1121 | err = mgmt_cmd_status(sk, index, opcode, |
| 1122 | MGMT_STATUS_INVALID_INDEX); |
| 1123 | goto done; |
| 1124 | } |
| 1125 | |
| 1126 | var_len = (handler->flags & HCI_MGMT_VAR_LEN); |
| 1127 | if ((var_len && len < handler->data_len) || |
| 1128 | (!var_len && len != handler->data_len)) { |
| 1129 | err = mgmt_cmd_status(sk, index, opcode, |
| 1130 | MGMT_STATUS_INVALID_PARAMS); |
| 1131 | goto done; |
| 1132 | } |
| 1133 | |
| 1134 | if (hdev && chan->hdev_init) |
| 1135 | chan->hdev_init(sk, hdev); |
| 1136 | |
| 1137 | cp = buf + sizeof(*hdr); |
| 1138 | |
| 1139 | err = handler->func(sk, hdev, cp, len); |
| 1140 | if (err < 0) |
| 1141 | goto done; |
| 1142 | |
| 1143 | err = msglen; |
| 1144 | |
| 1145 | done: |
| 1146 | if (hdev) |
| 1147 | hci_dev_put(hdev); |
| 1148 | |
| 1149 | kfree(buf); |
| 1150 | return err; |
| 1151 | } |
| 1152 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1153 | static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg, |
| 1154 | size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | { |
| 1156 | struct sock *sk = sock->sk; |
Johan Hedberg | 801c1e8 | 2015-03-06 21:08:50 +0200 | [diff] [blame] | 1157 | struct hci_mgmt_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | struct hci_dev *hdev; |
| 1159 | struct sk_buff *skb; |
| 1160 | int err; |
| 1161 | |
| 1162 | BT_DBG("sock %p sk %p", sock, sk); |
| 1163 | |
| 1164 | if (msg->msg_flags & MSG_OOB) |
| 1165 | return -EOPNOTSUPP; |
| 1166 | |
| 1167 | if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE)) |
| 1168 | return -EINVAL; |
| 1169 | |
| 1170 | if (len < 4 || len > HCI_MAX_FRAME_SIZE) |
| 1171 | return -EINVAL; |
| 1172 | |
| 1173 | lock_sock(sk); |
| 1174 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1175 | switch (hci_pi(sk)->channel) { |
| 1176 | case HCI_CHANNEL_RAW: |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 1177 | case HCI_CHANNEL_USER: |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1178 | break; |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 1179 | case HCI_CHANNEL_MONITOR: |
| 1180 | err = -EOPNOTSUPP; |
| 1181 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1182 | default: |
Johan Hedberg | 801c1e8 | 2015-03-06 21:08:50 +0200 | [diff] [blame] | 1183 | mutex_lock(&mgmt_chan_list_lock); |
| 1184 | chan = __hci_mgmt_chan_find(hci_pi(sk)->channel); |
| 1185 | if (chan) |
Johan Hedberg | fa4335d | 2015-03-17 13:48:50 +0200 | [diff] [blame] | 1186 | err = hci_mgmt_cmd(chan, sk, msg, len); |
Johan Hedberg | 801c1e8 | 2015-03-06 21:08:50 +0200 | [diff] [blame] | 1187 | else |
| 1188 | err = -EINVAL; |
| 1189 | |
| 1190 | mutex_unlock(&mgmt_chan_list_lock); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1191 | goto done; |
| 1192 | } |
| 1193 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1194 | hdev = hci_pi(sk)->hdev; |
| 1195 | if (!hdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | err = -EBADFD; |
| 1197 | goto done; |
| 1198 | } |
| 1199 | |
Marcel Holtmann | 7e21add | 2009-11-18 01:05:00 +0100 | [diff] [blame] | 1200 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 1201 | err = -ENETDOWN; |
| 1202 | goto done; |
| 1203 | } |
| 1204 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1205 | skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err); |
| 1206 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | goto done; |
| 1208 | |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 1209 | if (memcpy_from_msg(skb_put(skb, len), msg, len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | err = -EFAULT; |
| 1211 | goto drop; |
| 1212 | } |
| 1213 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 1214 | bt_cb(skb)->pkt_type = *((unsigned char *) skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | skb_pull(skb, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | |
Marcel Holtmann | 1bc5ad1 | 2013-12-17 03:21:25 -0800 | [diff] [blame] | 1217 | if (hci_pi(sk)->channel == HCI_CHANNEL_USER) { |
| 1218 | /* No permission check is needed for user channel |
| 1219 | * since that gets enforced when binding the socket. |
| 1220 | * |
| 1221 | * However check that the packet type is valid. |
| 1222 | */ |
| 1223 | if (bt_cb(skb)->pkt_type != HCI_COMMAND_PKT && |
| 1224 | bt_cb(skb)->pkt_type != HCI_ACLDATA_PKT && |
| 1225 | bt_cb(skb)->pkt_type != HCI_SCODATA_PKT) { |
| 1226 | err = -EINVAL; |
| 1227 | goto drop; |
| 1228 | } |
| 1229 | |
| 1230 | skb_queue_tail(&hdev->raw_q, skb); |
| 1231 | queue_work(hdev->workqueue, &hdev->tx_work); |
| 1232 | } else if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) { |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 1233 | u16 opcode = get_unaligned_le16(skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | u16 ogf = hci_opcode_ogf(opcode); |
| 1235 | u16 ocf = hci_opcode_ocf(opcode); |
| 1236 | |
| 1237 | if (((ogf > HCI_SFLT_MAX_OGF) || |
Gustavo Padovan | 3bb3c75 | 2012-05-17 00:36:22 -0300 | [diff] [blame] | 1238 | !hci_test_bit(ocf & HCI_FLT_OCF_BITS, |
| 1239 | &hci_sec_filter.ocf_mask[ogf])) && |
| 1240 | !capable(CAP_NET_RAW)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | err = -EPERM; |
| 1242 | goto drop; |
| 1243 | } |
| 1244 | |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 1245 | if (ogf == 0x3f) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | skb_queue_tail(&hdev->raw_q, skb); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 1247 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | } else { |
Stephen Hemminger | 49c922b | 2014-10-27 21:12:20 -0700 | [diff] [blame] | 1249 | /* Stand-alone HCI commands must be flagged as |
Johan Hedberg | 11714b3 | 2013-03-05 20:37:47 +0200 | [diff] [blame] | 1250 | * single-command requests. |
| 1251 | */ |
Johan Hedberg | db6e3e8 | 2015-03-30 23:21:02 +0300 | [diff] [blame] | 1252 | bt_cb(skb)->req.start = true; |
Johan Hedberg | 11714b3 | 2013-03-05 20:37:47 +0200 | [diff] [blame] | 1253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | skb_queue_tail(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 1255 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | } |
| 1257 | } else { |
| 1258 | if (!capable(CAP_NET_RAW)) { |
| 1259 | err = -EPERM; |
| 1260 | goto drop; |
| 1261 | } |
| 1262 | |
Marcel Holtmann | bb77543 | 2015-10-09 16:13:50 +0200 | [diff] [blame] | 1263 | if (bt_cb(skb)->pkt_type != HCI_ACLDATA_PKT && |
| 1264 | bt_cb(skb)->pkt_type != HCI_SCODATA_PKT) { |
| 1265 | err = -EINVAL; |
| 1266 | goto drop; |
| 1267 | } |
| 1268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | skb_queue_tail(&hdev->raw_q, skb); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 1270 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | err = len; |
| 1274 | |
| 1275 | done: |
| 1276 | release_sock(sk); |
| 1277 | return err; |
| 1278 | |
| 1279 | drop: |
| 1280 | kfree_skb(skb); |
| 1281 | goto done; |
| 1282 | } |
| 1283 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1284 | static int hci_sock_setsockopt(struct socket *sock, int level, int optname, |
| 1285 | char __user *optval, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | { |
| 1287 | struct hci_ufilter uf = { .opcode = 0 }; |
| 1288 | struct sock *sk = sock->sk; |
| 1289 | int err = 0, opt = 0; |
| 1290 | |
| 1291 | BT_DBG("sk %p, opt %d", sk, optname); |
| 1292 | |
| 1293 | lock_sock(sk); |
| 1294 | |
Marcel Holtmann | 2f39cdb | 2012-02-20 14:50:32 +0100 | [diff] [blame] | 1295 | if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { |
Marcel Holtmann | c2371e8 | 2013-08-26 09:29:39 -0700 | [diff] [blame] | 1296 | err = -EBADFD; |
Marcel Holtmann | 2f39cdb | 2012-02-20 14:50:32 +0100 | [diff] [blame] | 1297 | goto done; |
| 1298 | } |
| 1299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | switch (optname) { |
| 1301 | case HCI_DATA_DIR: |
| 1302 | if (get_user(opt, (int __user *)optval)) { |
| 1303 | err = -EFAULT; |
| 1304 | break; |
| 1305 | } |
| 1306 | |
| 1307 | if (opt) |
| 1308 | hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR; |
| 1309 | else |
| 1310 | hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR; |
| 1311 | break; |
| 1312 | |
| 1313 | case HCI_TIME_STAMP: |
| 1314 | if (get_user(opt, (int __user *)optval)) { |
| 1315 | err = -EFAULT; |
| 1316 | break; |
| 1317 | } |
| 1318 | |
| 1319 | if (opt) |
| 1320 | hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP; |
| 1321 | else |
| 1322 | hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP; |
| 1323 | break; |
| 1324 | |
| 1325 | case HCI_FILTER: |
Marcel Holtmann | 0878b66 | 2007-05-05 00:35:59 +0200 | [diff] [blame] | 1326 | { |
| 1327 | struct hci_filter *f = &hci_pi(sk)->filter; |
| 1328 | |
| 1329 | uf.type_mask = f->type_mask; |
| 1330 | uf.opcode = f->opcode; |
| 1331 | uf.event_mask[0] = *((u32 *) f->event_mask + 0); |
| 1332 | uf.event_mask[1] = *((u32 *) f->event_mask + 1); |
| 1333 | } |
| 1334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | len = min_t(unsigned int, len, sizeof(uf)); |
| 1336 | if (copy_from_user(&uf, optval, len)) { |
| 1337 | err = -EFAULT; |
| 1338 | break; |
| 1339 | } |
| 1340 | |
| 1341 | if (!capable(CAP_NET_RAW)) { |
| 1342 | uf.type_mask &= hci_sec_filter.type_mask; |
| 1343 | uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0); |
| 1344 | uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1); |
| 1345 | } |
| 1346 | |
| 1347 | { |
| 1348 | struct hci_filter *f = &hci_pi(sk)->filter; |
| 1349 | |
| 1350 | f->type_mask = uf.type_mask; |
| 1351 | f->opcode = uf.opcode; |
| 1352 | *((u32 *) f->event_mask + 0) = uf.event_mask[0]; |
| 1353 | *((u32 *) f->event_mask + 1) = uf.event_mask[1]; |
| 1354 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1355 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | |
| 1357 | default: |
| 1358 | err = -ENOPROTOOPT; |
| 1359 | break; |
| 1360 | } |
| 1361 | |
Marcel Holtmann | 2f39cdb | 2012-02-20 14:50:32 +0100 | [diff] [blame] | 1362 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | release_sock(sk); |
| 1364 | return err; |
| 1365 | } |
| 1366 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1367 | static int hci_sock_getsockopt(struct socket *sock, int level, int optname, |
| 1368 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | { |
| 1370 | struct hci_ufilter uf; |
| 1371 | struct sock *sk = sock->sk; |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1372 | int len, opt, err = 0; |
| 1373 | |
| 1374 | BT_DBG("sk %p, opt %d", sk, optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | |
| 1376 | if (get_user(len, optlen)) |
| 1377 | return -EFAULT; |
| 1378 | |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1379 | lock_sock(sk); |
| 1380 | |
| 1381 | if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { |
Marcel Holtmann | c2371e8 | 2013-08-26 09:29:39 -0700 | [diff] [blame] | 1382 | err = -EBADFD; |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1383 | goto done; |
| 1384 | } |
| 1385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | switch (optname) { |
| 1387 | case HCI_DATA_DIR: |
| 1388 | if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR) |
| 1389 | opt = 1; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1390 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | opt = 0; |
| 1392 | |
| 1393 | if (put_user(opt, optval)) |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1394 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | break; |
| 1396 | |
| 1397 | case HCI_TIME_STAMP: |
| 1398 | if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP) |
| 1399 | opt = 1; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1400 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | opt = 0; |
| 1402 | |
| 1403 | if (put_user(opt, optval)) |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1404 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | break; |
| 1406 | |
| 1407 | case HCI_FILTER: |
| 1408 | { |
| 1409 | struct hci_filter *f = &hci_pi(sk)->filter; |
| 1410 | |
Mathias Krause | e15ca9a | 2012-08-15 11:31:46 +0000 | [diff] [blame] | 1411 | memset(&uf, 0, sizeof(uf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | uf.type_mask = f->type_mask; |
| 1413 | uf.opcode = f->opcode; |
| 1414 | uf.event_mask[0] = *((u32 *) f->event_mask + 0); |
| 1415 | uf.event_mask[1] = *((u32 *) f->event_mask + 1); |
| 1416 | } |
| 1417 | |
| 1418 | len = min_t(unsigned int, len, sizeof(uf)); |
| 1419 | if (copy_to_user(optval, &uf, len)) |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1420 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | break; |
| 1422 | |
| 1423 | default: |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1424 | err = -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | break; |
| 1426 | } |
| 1427 | |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1428 | done: |
| 1429 | release_sock(sk); |
| 1430 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1433 | static const struct proto_ops hci_sock_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | .family = PF_BLUETOOTH, |
| 1435 | .owner = THIS_MODULE, |
| 1436 | .release = hci_sock_release, |
| 1437 | .bind = hci_sock_bind, |
| 1438 | .getname = hci_sock_getname, |
| 1439 | .sendmsg = hci_sock_sendmsg, |
| 1440 | .recvmsg = hci_sock_recvmsg, |
| 1441 | .ioctl = hci_sock_ioctl, |
| 1442 | .poll = datagram_poll, |
| 1443 | .listen = sock_no_listen, |
| 1444 | .shutdown = sock_no_shutdown, |
| 1445 | .setsockopt = hci_sock_setsockopt, |
| 1446 | .getsockopt = hci_sock_getsockopt, |
| 1447 | .connect = sock_no_connect, |
| 1448 | .socketpair = sock_no_socketpair, |
| 1449 | .accept = sock_no_accept, |
| 1450 | .mmap = sock_no_mmap |
| 1451 | }; |
| 1452 | |
| 1453 | static struct proto hci_sk_proto = { |
| 1454 | .name = "HCI", |
| 1455 | .owner = THIS_MODULE, |
| 1456 | .obj_size = sizeof(struct hci_pinfo) |
| 1457 | }; |
| 1458 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 1459 | static int hci_sock_create(struct net *net, struct socket *sock, int protocol, |
| 1460 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | { |
| 1462 | struct sock *sk; |
| 1463 | |
| 1464 | BT_DBG("sock %p", sock); |
| 1465 | |
| 1466 | if (sock->type != SOCK_RAW) |
| 1467 | return -ESOCKTNOSUPPORT; |
| 1468 | |
| 1469 | sock->ops = &hci_sock_ops; |
| 1470 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 1471 | sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto, kern); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | if (!sk) |
| 1473 | return -ENOMEM; |
| 1474 | |
| 1475 | sock_init_data(sock, sk); |
| 1476 | |
| 1477 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 1478 | |
| 1479 | sk->sk_protocol = protocol; |
| 1480 | |
| 1481 | sock->state = SS_UNCONNECTED; |
| 1482 | sk->sk_state = BT_OPEN; |
| 1483 | |
| 1484 | bt_sock_link(&hci_sk_list, sk); |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 1488 | static const struct net_proto_family hci_sock_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | .family = PF_BLUETOOTH, |
| 1490 | .owner = THIS_MODULE, |
| 1491 | .create = hci_sock_create, |
| 1492 | }; |
| 1493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | int __init hci_sock_init(void) |
| 1495 | { |
| 1496 | int err; |
| 1497 | |
Marcel Holtmann | b0a8e28 | 2015-01-11 15:18:17 -0800 | [diff] [blame] | 1498 | BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr)); |
| 1499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | err = proto_register(&hci_sk_proto, 0); |
| 1501 | if (err < 0) |
| 1502 | return err; |
| 1503 | |
| 1504 | err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops); |
Masatake YAMATO | f7c8663 | 2012-07-26 01:28:36 +0900 | [diff] [blame] | 1505 | if (err < 0) { |
| 1506 | BT_ERR("HCI socket registration failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | goto error; |
Masatake YAMATO | f7c8663 | 2012-07-26 01:28:36 +0900 | [diff] [blame] | 1508 | } |
| 1509 | |
Al Viro | b031661 | 2013-04-04 19:14:33 -0400 | [diff] [blame] | 1510 | err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL); |
Masatake YAMATO | f7c8663 | 2012-07-26 01:28:36 +0900 | [diff] [blame] | 1511 | if (err < 0) { |
| 1512 | BT_ERR("Failed to create HCI proc file"); |
| 1513 | bt_sock_unregister(BTPROTO_HCI); |
| 1514 | goto error; |
| 1515 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | BT_INFO("HCI socket layer initialized"); |
| 1518 | |
| 1519 | return 0; |
| 1520 | |
| 1521 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | proto_unregister(&hci_sk_proto); |
| 1523 | return err; |
| 1524 | } |
| 1525 | |
Anand Gadiyar | b7440a14 | 2011-02-22 12:43:09 +0530 | [diff] [blame] | 1526 | void hci_sock_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | { |
Masatake YAMATO | f7c8663 | 2012-07-26 01:28:36 +0900 | [diff] [blame] | 1528 | bt_procfs_cleanup(&init_net, "hci"); |
David Herrmann | 5e9d7f8 | 2013-02-24 19:36:51 +0100 | [diff] [blame] | 1529 | bt_sock_unregister(BTPROTO_HCI); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | proto_unregister(&hci_sk_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | } |