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 | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 123 | flt_type = hci_skb_pkt_type(skb) & 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 */ |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 129 | if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT) |
Marcel Holtmann | f81fe64 | 2013-08-25 23:25:15 -0700 | [diff] [blame] | 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 | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 173 | if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT && |
| 174 | hci_skb_pkt_type(skb) != HCI_EVENT_PKT && |
| 175 | hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT && |
| 176 | hci_skb_pkt_type(skb) != HCI_SCODATA_PKT) |
Marcel Holtmann | bb77543 | 2015-10-09 16:13:50 +0200 | [diff] [blame] | 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; |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 183 | if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT && |
| 184 | hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT && |
| 185 | hci_skb_pkt_type(skb) != HCI_SCODATA_PKT) |
Marcel Holtmann | 2350018 | 2013-08-26 21:40:52 -0700 | [diff] [blame] | 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 */ |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 199 | memcpy(skb_push(skb_copy, 1), &hci_skb_pkt_type(skb), 1); |
Marcel Holtmann | e0edf37 | 2012-02-20 14:50:36 +0100 | [diff] [blame] | 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 | |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 265 | switch (hci_skb_pkt_type(skb)) { |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 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 | |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 450 | hci_skb_pkt_type(skb) = 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 | 6039aa7 | 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 | 6039aa7 | 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 | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 1214 | hci_skb_pkt_type(skb) = *((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 | */ |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 1223 | if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT && |
| 1224 | hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT && |
| 1225 | hci_skb_pkt_type(skb) != HCI_SCODATA_PKT) { |
Marcel Holtmann | 1bc5ad1 | 2013-12-17 03:21:25 -0800 | [diff] [blame] | 1226 | err = -EINVAL; |
| 1227 | goto drop; |
| 1228 | } |
| 1229 | |
| 1230 | skb_queue_tail(&hdev->raw_q, skb); |
| 1231 | queue_work(hdev->workqueue, &hdev->tx_work); |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 1232 | } else if (hci_skb_pkt_type(skb) == 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 | 1982162 | 2015-11-06 07:42:20 +0100 | [diff] [blame^] | 1245 | /* Since the opcode has already been extracted here, store |
| 1246 | * a copy of the value for later use by the drivers. |
| 1247 | */ |
| 1248 | hci_skb_opcode(skb) = opcode; |
| 1249 | |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 1250 | if (ogf == 0x3f) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | skb_queue_tail(&hdev->raw_q, skb); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 1252 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | } else { |
Stephen Hemminger | 49c922b | 2014-10-27 21:12:20 -0700 | [diff] [blame] | 1254 | /* Stand-alone HCI commands must be flagged as |
Johan Hedberg | 11714b3 | 2013-03-05 20:37:47 +0200 | [diff] [blame] | 1255 | * single-command requests. |
| 1256 | */ |
Johan Hedberg | 44d2713 | 2015-11-05 09:31:40 +0200 | [diff] [blame] | 1257 | bt_cb(skb)->hci.req_flags |= HCI_REQ_START; |
Johan Hedberg | 11714b3 | 2013-03-05 20:37:47 +0200 | [diff] [blame] | 1258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | skb_queue_tail(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 1260 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | } |
| 1262 | } else { |
| 1263 | if (!capable(CAP_NET_RAW)) { |
| 1264 | err = -EPERM; |
| 1265 | goto drop; |
| 1266 | } |
| 1267 | |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 1268 | if (hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT && |
| 1269 | hci_skb_pkt_type(skb) != HCI_SCODATA_PKT) { |
Marcel Holtmann | bb77543 | 2015-10-09 16:13:50 +0200 | [diff] [blame] | 1270 | err = -EINVAL; |
| 1271 | goto drop; |
| 1272 | } |
| 1273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | skb_queue_tail(&hdev->raw_q, skb); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 1275 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | err = len; |
| 1279 | |
| 1280 | done: |
| 1281 | release_sock(sk); |
| 1282 | return err; |
| 1283 | |
| 1284 | drop: |
| 1285 | kfree_skb(skb); |
| 1286 | goto done; |
| 1287 | } |
| 1288 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1289 | static int hci_sock_setsockopt(struct socket *sock, int level, int optname, |
| 1290 | char __user *optval, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | { |
| 1292 | struct hci_ufilter uf = { .opcode = 0 }; |
| 1293 | struct sock *sk = sock->sk; |
| 1294 | int err = 0, opt = 0; |
| 1295 | |
| 1296 | BT_DBG("sk %p, opt %d", sk, optname); |
| 1297 | |
| 1298 | lock_sock(sk); |
| 1299 | |
Marcel Holtmann | 2f39cdb | 2012-02-20 14:50:32 +0100 | [diff] [blame] | 1300 | if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { |
Marcel Holtmann | c2371e8 | 2013-08-26 09:29:39 -0700 | [diff] [blame] | 1301 | err = -EBADFD; |
Marcel Holtmann | 2f39cdb | 2012-02-20 14:50:32 +0100 | [diff] [blame] | 1302 | goto done; |
| 1303 | } |
| 1304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | switch (optname) { |
| 1306 | case HCI_DATA_DIR: |
| 1307 | if (get_user(opt, (int __user *)optval)) { |
| 1308 | err = -EFAULT; |
| 1309 | break; |
| 1310 | } |
| 1311 | |
| 1312 | if (opt) |
| 1313 | hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR; |
| 1314 | else |
| 1315 | hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR; |
| 1316 | break; |
| 1317 | |
| 1318 | case HCI_TIME_STAMP: |
| 1319 | if (get_user(opt, (int __user *)optval)) { |
| 1320 | err = -EFAULT; |
| 1321 | break; |
| 1322 | } |
| 1323 | |
| 1324 | if (opt) |
| 1325 | hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP; |
| 1326 | else |
| 1327 | hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP; |
| 1328 | break; |
| 1329 | |
| 1330 | case HCI_FILTER: |
Marcel Holtmann | 0878b66 | 2007-05-05 00:35:59 +0200 | [diff] [blame] | 1331 | { |
| 1332 | struct hci_filter *f = &hci_pi(sk)->filter; |
| 1333 | |
| 1334 | uf.type_mask = f->type_mask; |
| 1335 | uf.opcode = f->opcode; |
| 1336 | uf.event_mask[0] = *((u32 *) f->event_mask + 0); |
| 1337 | uf.event_mask[1] = *((u32 *) f->event_mask + 1); |
| 1338 | } |
| 1339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | len = min_t(unsigned int, len, sizeof(uf)); |
| 1341 | if (copy_from_user(&uf, optval, len)) { |
| 1342 | err = -EFAULT; |
| 1343 | break; |
| 1344 | } |
| 1345 | |
| 1346 | if (!capable(CAP_NET_RAW)) { |
| 1347 | uf.type_mask &= hci_sec_filter.type_mask; |
| 1348 | uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0); |
| 1349 | uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1); |
| 1350 | } |
| 1351 | |
| 1352 | { |
| 1353 | struct hci_filter *f = &hci_pi(sk)->filter; |
| 1354 | |
| 1355 | f->type_mask = uf.type_mask; |
| 1356 | f->opcode = uf.opcode; |
| 1357 | *((u32 *) f->event_mask + 0) = uf.event_mask[0]; |
| 1358 | *((u32 *) f->event_mask + 1) = uf.event_mask[1]; |
| 1359 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1360 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | |
| 1362 | default: |
| 1363 | err = -ENOPROTOOPT; |
| 1364 | break; |
| 1365 | } |
| 1366 | |
Marcel Holtmann | 2f39cdb | 2012-02-20 14:50:32 +0100 | [diff] [blame] | 1367 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | release_sock(sk); |
| 1369 | return err; |
| 1370 | } |
| 1371 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1372 | static int hci_sock_getsockopt(struct socket *sock, int level, int optname, |
| 1373 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | { |
| 1375 | struct hci_ufilter uf; |
| 1376 | struct sock *sk = sock->sk; |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1377 | int len, opt, err = 0; |
| 1378 | |
| 1379 | BT_DBG("sk %p, opt %d", sk, optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | |
| 1381 | if (get_user(len, optlen)) |
| 1382 | return -EFAULT; |
| 1383 | |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1384 | lock_sock(sk); |
| 1385 | |
| 1386 | if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { |
Marcel Holtmann | c2371e8 | 2013-08-26 09:29:39 -0700 | [diff] [blame] | 1387 | err = -EBADFD; |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1388 | goto done; |
| 1389 | } |
| 1390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | switch (optname) { |
| 1392 | case HCI_DATA_DIR: |
| 1393 | if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR) |
| 1394 | opt = 1; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1395 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | opt = 0; |
| 1397 | |
| 1398 | if (put_user(opt, optval)) |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1399 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | break; |
| 1401 | |
| 1402 | case HCI_TIME_STAMP: |
| 1403 | if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP) |
| 1404 | opt = 1; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1405 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | opt = 0; |
| 1407 | |
| 1408 | if (put_user(opt, optval)) |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1409 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | break; |
| 1411 | |
| 1412 | case HCI_FILTER: |
| 1413 | { |
| 1414 | struct hci_filter *f = &hci_pi(sk)->filter; |
| 1415 | |
Mathias Krause | e15ca9a | 2012-08-15 11:31:46 +0000 | [diff] [blame] | 1416 | memset(&uf, 0, sizeof(uf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | uf.type_mask = f->type_mask; |
| 1418 | uf.opcode = f->opcode; |
| 1419 | uf.event_mask[0] = *((u32 *) f->event_mask + 0); |
| 1420 | uf.event_mask[1] = *((u32 *) f->event_mask + 1); |
| 1421 | } |
| 1422 | |
| 1423 | len = min_t(unsigned int, len, sizeof(uf)); |
| 1424 | if (copy_to_user(optval, &uf, len)) |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1425 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | break; |
| 1427 | |
| 1428 | default: |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1429 | err = -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | break; |
| 1431 | } |
| 1432 | |
Marcel Holtmann | cedc546 | 2012-02-20 14:50:33 +0100 | [diff] [blame] | 1433 | done: |
| 1434 | release_sock(sk); |
| 1435 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | } |
| 1437 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1438 | static const struct proto_ops hci_sock_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | .family = PF_BLUETOOTH, |
| 1440 | .owner = THIS_MODULE, |
| 1441 | .release = hci_sock_release, |
| 1442 | .bind = hci_sock_bind, |
| 1443 | .getname = hci_sock_getname, |
| 1444 | .sendmsg = hci_sock_sendmsg, |
| 1445 | .recvmsg = hci_sock_recvmsg, |
| 1446 | .ioctl = hci_sock_ioctl, |
| 1447 | .poll = datagram_poll, |
| 1448 | .listen = sock_no_listen, |
| 1449 | .shutdown = sock_no_shutdown, |
| 1450 | .setsockopt = hci_sock_setsockopt, |
| 1451 | .getsockopt = hci_sock_getsockopt, |
| 1452 | .connect = sock_no_connect, |
| 1453 | .socketpair = sock_no_socketpair, |
| 1454 | .accept = sock_no_accept, |
| 1455 | .mmap = sock_no_mmap |
| 1456 | }; |
| 1457 | |
| 1458 | static struct proto hci_sk_proto = { |
| 1459 | .name = "HCI", |
| 1460 | .owner = THIS_MODULE, |
| 1461 | .obj_size = sizeof(struct hci_pinfo) |
| 1462 | }; |
| 1463 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 1464 | static int hci_sock_create(struct net *net, struct socket *sock, int protocol, |
| 1465 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | { |
| 1467 | struct sock *sk; |
| 1468 | |
| 1469 | BT_DBG("sock %p", sock); |
| 1470 | |
| 1471 | if (sock->type != SOCK_RAW) |
| 1472 | return -ESOCKTNOSUPPORT; |
| 1473 | |
| 1474 | sock->ops = &hci_sock_ops; |
| 1475 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 1476 | sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto, kern); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | if (!sk) |
| 1478 | return -ENOMEM; |
| 1479 | |
| 1480 | sock_init_data(sock, sk); |
| 1481 | |
| 1482 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 1483 | |
| 1484 | sk->sk_protocol = protocol; |
| 1485 | |
| 1486 | sock->state = SS_UNCONNECTED; |
| 1487 | sk->sk_state = BT_OPEN; |
| 1488 | |
| 1489 | bt_sock_link(&hci_sk_list, sk); |
| 1490 | return 0; |
| 1491 | } |
| 1492 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 1493 | static const struct net_proto_family hci_sock_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | .family = PF_BLUETOOTH, |
| 1495 | .owner = THIS_MODULE, |
| 1496 | .create = hci_sock_create, |
| 1497 | }; |
| 1498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | int __init hci_sock_init(void) |
| 1500 | { |
| 1501 | int err; |
| 1502 | |
Marcel Holtmann | b0a8e28 | 2015-01-11 15:18:17 -0800 | [diff] [blame] | 1503 | BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr)); |
| 1504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | err = proto_register(&hci_sk_proto, 0); |
| 1506 | if (err < 0) |
| 1507 | return err; |
| 1508 | |
| 1509 | err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops); |
Masatake YAMATO | f7c8663 | 2012-07-26 01:28:36 +0900 | [diff] [blame] | 1510 | if (err < 0) { |
| 1511 | BT_ERR("HCI socket registration failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | goto error; |
Masatake YAMATO | f7c8663 | 2012-07-26 01:28:36 +0900 | [diff] [blame] | 1513 | } |
| 1514 | |
Al Viro | b031661 | 2013-04-04 19:14:33 -0400 | [diff] [blame] | 1515 | err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL); |
Masatake YAMATO | f7c8663 | 2012-07-26 01:28:36 +0900 | [diff] [blame] | 1516 | if (err < 0) { |
| 1517 | BT_ERR("Failed to create HCI proc file"); |
| 1518 | bt_sock_unregister(BTPROTO_HCI); |
| 1519 | goto error; |
| 1520 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | BT_INFO("HCI socket layer initialized"); |
| 1523 | |
| 1524 | return 0; |
| 1525 | |
| 1526 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | proto_unregister(&hci_sk_proto); |
| 1528 | return err; |
| 1529 | } |
| 1530 | |
Anand Gadiyar | b7440a14 | 2011-02-22 12:43:09 +0530 | [diff] [blame] | 1531 | void hci_sock_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | { |
Masatake YAMATO | f7c8663 | 2012-07-26 01:28:36 +0900 | [diff] [blame] | 1533 | bt_procfs_cleanup(&init_net, "hci"); |
David Herrmann | 5e9d7f8 | 2013-02-24 19:36:51 +0100 | [diff] [blame] | 1534 | bt_sock_unregister(BTPROTO_HCI); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | proto_unregister(&hci_sk_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | } |