Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2010 Nokia Corporation |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License version 2 as |
| 7 | published by the Free Software Foundation; |
| 8 | |
| 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 10 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 11 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 12 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 13 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 14 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | |
| 18 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 19 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 20 | SOFTWARE IS DISCLAIMED. |
| 21 | */ |
| 22 | |
| 23 | /* Bluetooth HCI Management interface */ |
| 24 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 25 | #include <linux/kernel.h> |
Szymon Janc | 7235975 | 2011-02-17 14:16:32 +0100 | [diff] [blame] | 26 | #include <linux/uaccess.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
| 29 | |
| 30 | #include <net/bluetooth/bluetooth.h> |
| 31 | #include <net/bluetooth/hci_core.h> |
| 32 | #include <net/bluetooth/mgmt.h> |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 33 | #include <net/bluetooth/smp.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 34 | |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 35 | #define MGMT_VERSION 0 |
| 36 | #define MGMT_REVISION 1 |
| 37 | |
Andre Guedes | 2519a1f | 2011-11-07 11:45:24 -0300 | [diff] [blame] | 38 | #define INQUIRY_LEN_BREDR 0x08 /* TGAP(100) */ |
| 39 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 40 | #define SERVICE_CACHE_TIMEOUT (5 * 1000) |
| 41 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 42 | struct pending_cmd { |
| 43 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 44 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 45 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 46 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 47 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 48 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 51 | /* HCI to MGMT error code conversion table */ |
| 52 | static u8 mgmt_status_table[] = { |
| 53 | MGMT_STATUS_SUCCESS, |
| 54 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 55 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 56 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 57 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 58 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
| 59 | MGMT_STATUS_NOT_PAIRED, /* PIN or Key Missing */ |
| 60 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 61 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 62 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 63 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 64 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 65 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 66 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 67 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 68 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 69 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 70 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 71 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 72 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 73 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 74 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 75 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 76 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 77 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 78 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 79 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 80 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 81 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 82 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 83 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 84 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 85 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 86 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 87 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 88 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 89 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 90 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 91 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 92 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 93 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 94 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 95 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 96 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 97 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 98 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 99 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 100 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 101 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 102 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 103 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 104 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 105 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 106 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 107 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 108 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 109 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 110 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 111 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 112 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 113 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 114 | }; |
| 115 | |
| 116 | static u8 mgmt_status(u8 hci_status) |
| 117 | { |
| 118 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 119 | return mgmt_status_table[hci_status]; |
| 120 | |
| 121 | return MGMT_STATUS_FAILED; |
| 122 | } |
| 123 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 124 | static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 125 | { |
| 126 | struct sk_buff *skb; |
| 127 | struct mgmt_hdr *hdr; |
| 128 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 129 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 130 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 131 | BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 132 | |
| 133 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_ATOMIC); |
| 134 | if (!skb) |
| 135 | return -ENOMEM; |
| 136 | |
| 137 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 138 | |
| 139 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 140 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 141 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 142 | |
| 143 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 144 | ev->status = status; |
| 145 | put_unaligned_le16(cmd, &ev->opcode); |
| 146 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 147 | err = sock_queue_rcv_skb(sk, skb); |
| 148 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 149 | kfree_skb(skb); |
| 150 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 151 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 154 | static int cmd_complete(struct sock *sk, u16 index, u16 cmd, void *rp, |
| 155 | size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 156 | { |
| 157 | struct sk_buff *skb; |
| 158 | struct mgmt_hdr *hdr; |
| 159 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 160 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 161 | |
| 162 | BT_DBG("sock %p", sk); |
| 163 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 164 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_ATOMIC); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 165 | if (!skb) |
| 166 | return -ENOMEM; |
| 167 | |
| 168 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 169 | |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 170 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 171 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 172 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 173 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 174 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
| 175 | put_unaligned_le16(cmd, &ev->opcode); |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 176 | |
| 177 | if (rp) |
| 178 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 179 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 180 | err = sock_queue_rcv_skb(sk, skb); |
| 181 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 182 | kfree_skb(skb); |
| 183 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 184 | return err;; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 187 | static int read_version(struct sock *sk) |
| 188 | { |
| 189 | struct mgmt_rp_read_version rp; |
| 190 | |
| 191 | BT_DBG("sock %p", sk); |
| 192 | |
| 193 | rp.version = MGMT_VERSION; |
| 194 | put_unaligned_le16(MGMT_REVISION, &rp.revision); |
| 195 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 196 | return cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, &rp, |
| 197 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 200 | static int read_index_list(struct sock *sk) |
| 201 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 202 | struct mgmt_rp_read_index_list *rp; |
| 203 | struct list_head *p; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 204 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 205 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 206 | u16 count; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 207 | int i, err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 208 | |
| 209 | BT_DBG("sock %p", sk); |
| 210 | |
| 211 | read_lock(&hci_dev_list_lock); |
| 212 | |
| 213 | count = 0; |
| 214 | list_for_each(p, &hci_dev_list) { |
| 215 | count++; |
| 216 | } |
| 217 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 218 | rp_len = sizeof(*rp) + (2 * count); |
| 219 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 220 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 221 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 222 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 223 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 224 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 225 | put_unaligned_le16(count, &rp->num_controllers); |
| 226 | |
| 227 | i = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 228 | list_for_each_entry(d, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 229 | if (test_and_clear_bit(HCI_AUTO_OFF, &d->dev_flags)) |
Johan Hedberg | e0f9309 | 2011-11-09 01:44:22 +0200 | [diff] [blame] | 230 | cancel_delayed_work(&d->power_off); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 231 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 232 | if (test_bit(HCI_SETUP, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 233 | continue; |
| 234 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 235 | put_unaligned_le16(d->id, &rp->index[i++]); |
| 236 | BT_DBG("Added hci%u", d->id); |
| 237 | } |
| 238 | |
| 239 | read_unlock(&hci_dev_list_lock); |
| 240 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 241 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, rp, |
| 242 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 243 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 244 | kfree(rp); |
| 245 | |
| 246 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 249 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 250 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 251 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 252 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 253 | settings |= MGMT_SETTING_POWERED; |
| 254 | settings |= MGMT_SETTING_CONNECTABLE; |
| 255 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 256 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 257 | settings |= MGMT_SETTING_PAIRABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 258 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 259 | if (hdev->features[6] & LMP_SIMPLE_PAIR) |
| 260 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 261 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 262 | if (!(hdev->features[4] & LMP_NO_BREDR)) { |
| 263 | settings |= MGMT_SETTING_BREDR; |
| 264 | settings |= MGMT_SETTING_LINK_SECURITY; |
| 265 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 266 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 267 | if (hdev->features[4] & LMP_LE) |
| 268 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 269 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 270 | return settings; |
| 271 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 272 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 273 | static u32 get_current_settings(struct hci_dev *hdev) |
| 274 | { |
| 275 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 276 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 277 | if (test_bit(HCI_UP, &hdev->flags)) |
| 278 | settings |= MGMT_SETTING_POWERED; |
| 279 | else |
| 280 | return settings; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 281 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 282 | if (test_bit(HCI_PSCAN, &hdev->flags)) |
| 283 | settings |= MGMT_SETTING_CONNECTABLE; |
| 284 | |
| 285 | if (test_bit(HCI_ISCAN, &hdev->flags)) |
| 286 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 287 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 288 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 289 | settings |= MGMT_SETTING_PAIRABLE; |
| 290 | |
| 291 | if (!(hdev->features[4] & LMP_NO_BREDR)) |
| 292 | settings |= MGMT_SETTING_BREDR; |
| 293 | |
Andre Guedes | 59e2940 | 2011-12-30 10:34:03 -0300 | [diff] [blame] | 294 | if (hdev->host_features[0] & LMP_HOST_LE) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 295 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 296 | |
| 297 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 298 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 299 | |
Johan Hedberg | 84bde9d6c | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 300 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 301 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 302 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 303 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 306 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 307 | |
| 308 | static u8 bluetooth_base_uuid[] = { |
| 309 | 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, |
| 310 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 311 | }; |
| 312 | |
| 313 | static u16 get_uuid16(u8 *uuid128) |
| 314 | { |
| 315 | u32 val; |
| 316 | int i; |
| 317 | |
| 318 | for (i = 0; i < 12; i++) { |
| 319 | if (bluetooth_base_uuid[i] != uuid128[i]) |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | memcpy(&val, &uuid128[12], 4); |
| 324 | |
| 325 | val = le32_to_cpu(val); |
| 326 | if (val > 0xffff) |
| 327 | return 0; |
| 328 | |
| 329 | return (u16) val; |
| 330 | } |
| 331 | |
| 332 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 333 | { |
| 334 | u8 *ptr = data; |
| 335 | u16 eir_len = 0; |
| 336 | u16 uuid16_list[HCI_MAX_EIR_LENGTH / sizeof(u16)]; |
| 337 | int i, truncated = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 338 | struct bt_uuid *uuid; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 339 | size_t name_len; |
| 340 | |
| 341 | name_len = strlen(hdev->dev_name); |
| 342 | |
| 343 | if (name_len > 0) { |
| 344 | /* EIR Data type */ |
| 345 | if (name_len > 48) { |
| 346 | name_len = 48; |
| 347 | ptr[1] = EIR_NAME_SHORT; |
| 348 | } else |
| 349 | ptr[1] = EIR_NAME_COMPLETE; |
| 350 | |
| 351 | /* EIR Data length */ |
| 352 | ptr[0] = name_len + 1; |
| 353 | |
| 354 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 355 | |
| 356 | eir_len += (name_len + 2); |
| 357 | ptr += (name_len + 2); |
| 358 | } |
| 359 | |
| 360 | memset(uuid16_list, 0, sizeof(uuid16_list)); |
| 361 | |
| 362 | /* Group all UUID16 types */ |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 363 | list_for_each_entry(uuid, &hdev->uuids, list) { |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 364 | u16 uuid16; |
| 365 | |
| 366 | uuid16 = get_uuid16(uuid->uuid); |
| 367 | if (uuid16 == 0) |
| 368 | return; |
| 369 | |
| 370 | if (uuid16 < 0x1100) |
| 371 | continue; |
| 372 | |
| 373 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 374 | continue; |
| 375 | |
| 376 | /* Stop if not enough space to put next UUID */ |
| 377 | if (eir_len + 2 + sizeof(u16) > HCI_MAX_EIR_LENGTH) { |
| 378 | truncated = 1; |
| 379 | break; |
| 380 | } |
| 381 | |
| 382 | /* Check for duplicates */ |
| 383 | for (i = 0; uuid16_list[i] != 0; i++) |
| 384 | if (uuid16_list[i] == uuid16) |
| 385 | break; |
| 386 | |
| 387 | if (uuid16_list[i] == 0) { |
| 388 | uuid16_list[i] = uuid16; |
| 389 | eir_len += sizeof(u16); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | if (uuid16_list[0] != 0) { |
| 394 | u8 *length = ptr; |
| 395 | |
| 396 | /* EIR Data type */ |
| 397 | ptr[1] = truncated ? EIR_UUID16_SOME : EIR_UUID16_ALL; |
| 398 | |
| 399 | ptr += 2; |
| 400 | eir_len += 2; |
| 401 | |
| 402 | for (i = 0; uuid16_list[i] != 0; i++) { |
| 403 | *ptr++ = (uuid16_list[i] & 0x00ff); |
| 404 | *ptr++ = (uuid16_list[i] & 0xff00) >> 8; |
| 405 | } |
| 406 | |
| 407 | /* EIR Data length */ |
| 408 | *length = (i * sizeof(u16)) + 1; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | static int update_eir(struct hci_dev *hdev) |
| 413 | { |
| 414 | struct hci_cp_write_eir cp; |
| 415 | |
| 416 | if (!(hdev->features[6] & LMP_EXT_INQ)) |
| 417 | return 0; |
| 418 | |
Johan Hedberg | 84bde9d6c | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 419 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 420 | return 0; |
| 421 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 422 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 423 | return 0; |
| 424 | |
| 425 | memset(&cp, 0, sizeof(cp)); |
| 426 | |
| 427 | create_eir(hdev, cp.data); |
| 428 | |
| 429 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
| 430 | return 0; |
| 431 | |
| 432 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 433 | |
| 434 | return hci_send_cmd(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
| 435 | } |
| 436 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 437 | static u8 get_service_classes(struct hci_dev *hdev) |
| 438 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 439 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 440 | u8 val = 0; |
| 441 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 442 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 443 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 444 | |
| 445 | return val; |
| 446 | } |
| 447 | |
| 448 | static int update_class(struct hci_dev *hdev) |
| 449 | { |
| 450 | u8 cod[3]; |
| 451 | |
| 452 | BT_DBG("%s", hdev->name); |
| 453 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 454 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 455 | return 0; |
| 456 | |
| 457 | cod[0] = hdev->minor_class; |
| 458 | cod[1] = hdev->major_class; |
| 459 | cod[2] = get_service_classes(hdev); |
| 460 | |
| 461 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
| 462 | return 0; |
| 463 | |
| 464 | return hci_send_cmd(hdev, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod); |
| 465 | } |
| 466 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 467 | static void service_cache_off(struct work_struct *work) |
| 468 | { |
| 469 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 470 | service_cache.work); |
| 471 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 472 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 473 | return; |
| 474 | |
| 475 | hci_dev_lock(hdev); |
| 476 | |
| 477 | update_eir(hdev); |
| 478 | update_class(hdev); |
| 479 | |
| 480 | hci_dev_unlock(hdev); |
| 481 | } |
| 482 | |
| 483 | static void mgmt_init_hdev(struct hci_dev *hdev) |
| 484 | { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 485 | if (!test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 486 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
| 487 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 488 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 489 | schedule_delayed_work(&hdev->service_cache, |
| 490 | msecs_to_jiffies(SERVICE_CACHE_TIMEOUT)); |
| 491 | } |
| 492 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 493 | static int read_controller_info(struct sock *sk, u16 index) |
| 494 | { |
| 495 | struct mgmt_rp_read_info rp; |
| 496 | struct hci_dev *hdev; |
| 497 | |
| 498 | BT_DBG("sock %p hci%u", sk, index); |
| 499 | |
| 500 | hdev = hci_dev_get(index); |
| 501 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 502 | return cmd_status(sk, index, MGMT_OP_READ_INFO, |
| 503 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 504 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 505 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 506 | cancel_delayed_work_sync(&hdev->power_off); |
| 507 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 508 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 509 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 510 | if (test_and_clear_bit(HCI_PI_MGMT_INIT, &hci_pi(sk)->flags)) |
| 511 | mgmt_init_hdev(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 512 | |
| 513 | memset(&rp, 0, sizeof(rp)); |
| 514 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 515 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 516 | |
| 517 | rp.version = hdev->hci_ver; |
| 518 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 519 | put_unaligned_le16(hdev->manufacturer, &rp.manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 520 | |
| 521 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 522 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 523 | |
| 524 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 525 | |
| 526 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
| 527 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 528 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 529 | hci_dev_put(hdev); |
| 530 | |
| 531 | return cmd_complete(sk, index, MGMT_OP_READ_INFO, &rp, sizeof(rp)); |
| 532 | } |
| 533 | |
| 534 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 535 | { |
| 536 | sock_put(cmd->sk); |
| 537 | kfree(cmd->param); |
| 538 | kfree(cmd); |
| 539 | } |
| 540 | |
| 541 | static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode, |
| 542 | struct hci_dev *hdev, |
| 543 | void *data, u16 len) |
| 544 | { |
| 545 | struct pending_cmd *cmd; |
| 546 | |
| 547 | cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC); |
| 548 | if (!cmd) |
| 549 | return NULL; |
| 550 | |
| 551 | cmd->opcode = opcode; |
| 552 | cmd->index = hdev->id; |
| 553 | |
| 554 | cmd->param = kmalloc(len, GFP_ATOMIC); |
| 555 | if (!cmd->param) { |
| 556 | kfree(cmd); |
| 557 | return NULL; |
| 558 | } |
| 559 | |
| 560 | if (data) |
| 561 | memcpy(cmd->param, data, len); |
| 562 | |
| 563 | cmd->sk = sk; |
| 564 | sock_hold(sk); |
| 565 | |
| 566 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 567 | |
| 568 | return cmd; |
| 569 | } |
| 570 | |
| 571 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
| 572 | void (*cb)(struct pending_cmd *cmd, void *data), |
| 573 | void *data) |
| 574 | { |
| 575 | struct list_head *p, *n; |
| 576 | |
| 577 | list_for_each_safe(p, n, &hdev->mgmt_pending) { |
| 578 | struct pending_cmd *cmd; |
| 579 | |
| 580 | cmd = list_entry(p, struct pending_cmd, list); |
| 581 | |
| 582 | if (opcode > 0 && cmd->opcode != opcode) |
| 583 | continue; |
| 584 | |
| 585 | cb(cmd, data); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 590 | { |
| 591 | struct pending_cmd *cmd; |
| 592 | |
| 593 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 594 | if (cmd->opcode == opcode) |
| 595 | return cmd; |
| 596 | } |
| 597 | |
| 598 | return NULL; |
| 599 | } |
| 600 | |
| 601 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 602 | { |
| 603 | list_del(&cmd->list); |
| 604 | mgmt_pending_free(cmd); |
| 605 | } |
| 606 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 607 | static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 608 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 609 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 610 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 611 | return cmd_complete(sk, hdev->id, opcode, &settings, sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 614 | static int set_powered(struct sock *sk, u16 index, unsigned char *data, u16 len) |
| 615 | { |
| 616 | struct mgmt_mode *cp; |
| 617 | struct hci_dev *hdev; |
| 618 | struct pending_cmd *cmd; |
| 619 | int err, up; |
| 620 | |
| 621 | cp = (void *) data; |
| 622 | |
| 623 | BT_DBG("request for hci%u", index); |
| 624 | |
| 625 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 626 | return cmd_status(sk, index, MGMT_OP_SET_POWERED, |
| 627 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 628 | |
| 629 | hdev = hci_dev_get(index); |
| 630 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 631 | return cmd_status(sk, index, MGMT_OP_SET_POWERED, |
| 632 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 633 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 634 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 635 | |
| 636 | up = test_bit(HCI_UP, &hdev->flags); |
| 637 | if ((cp->val && up) || (!cp->val && !up)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 638 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 639 | goto failed; |
| 640 | } |
| 641 | |
| 642 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 643 | err = cmd_status(sk, index, MGMT_OP_SET_POWERED, |
| 644 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 645 | goto failed; |
| 646 | } |
| 647 | |
| 648 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 649 | if (!cmd) { |
| 650 | err = -ENOMEM; |
| 651 | goto failed; |
| 652 | } |
| 653 | |
| 654 | if (cp->val) |
Gustavo F. Padovan | 7f97104 | 2011-12-18 12:40:32 -0200 | [diff] [blame] | 655 | schedule_work(&hdev->power_on); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 656 | else |
Gustavo F. Padovan | 80b7ab3 | 2011-12-17 14:52:27 -0200 | [diff] [blame] | 657 | schedule_work(&hdev->power_off.work); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 658 | |
| 659 | err = 0; |
| 660 | |
| 661 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 662 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 663 | hci_dev_put(hdev); |
| 664 | return err; |
| 665 | } |
| 666 | |
| 667 | static int set_discoverable(struct sock *sk, u16 index, unsigned char *data, |
| 668 | u16 len) |
| 669 | { |
| 670 | struct mgmt_cp_set_discoverable *cp; |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 671 | struct hci_dev *hdev; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 672 | struct pending_cmd *cmd; |
| 673 | u8 scan; |
| 674 | int err; |
| 675 | |
| 676 | cp = (void *) data; |
| 677 | |
| 678 | BT_DBG("request for hci%u", index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 679 | |
| 680 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 681 | return cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, |
| 682 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 683 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 684 | hdev = hci_dev_get(index); |
| 685 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 686 | return cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, |
| 687 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 688 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 689 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 690 | |
| 691 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 692 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, |
| 693 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 694 | goto failed; |
| 695 | } |
| 696 | |
| 697 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 698 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 699 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, |
| 700 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 701 | goto failed; |
| 702 | } |
| 703 | |
| 704 | if (cp->val == test_bit(HCI_ISCAN, &hdev->flags) && |
| 705 | test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 706 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 707 | goto failed; |
| 708 | } |
| 709 | |
| 710 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 711 | if (!cmd) { |
| 712 | err = -ENOMEM; |
| 713 | goto failed; |
| 714 | } |
| 715 | |
| 716 | scan = SCAN_PAGE; |
| 717 | |
| 718 | if (cp->val) |
| 719 | scan |= SCAN_INQUIRY; |
| 720 | else |
| 721 | cancel_delayed_work(&hdev->discov_off); |
| 722 | |
| 723 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 724 | if (err < 0) |
| 725 | mgmt_pending_remove(cmd); |
| 726 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 727 | if (cp->val) |
| 728 | hdev->discov_timeout = get_unaligned_le16(&cp->timeout); |
| 729 | |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 730 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 731 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 732 | hci_dev_put(hdev); |
| 733 | |
| 734 | return err; |
| 735 | } |
| 736 | |
| 737 | static int set_connectable(struct sock *sk, u16 index, unsigned char *data, |
| 738 | u16 len) |
| 739 | { |
| 740 | struct mgmt_mode *cp; |
| 741 | struct hci_dev *hdev; |
| 742 | struct pending_cmd *cmd; |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 743 | u8 scan; |
| 744 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 745 | |
| 746 | cp = (void *) data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 747 | |
| 748 | BT_DBG("request for hci%u", index); |
| 749 | |
| 750 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 751 | return cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, |
| 752 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 753 | |
| 754 | hdev = hci_dev_get(index); |
| 755 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 756 | return cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, |
| 757 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 758 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 759 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 760 | |
| 761 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 762 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, |
| 763 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 764 | goto failed; |
| 765 | } |
| 766 | |
| 767 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 768 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 769 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, |
| 770 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 771 | goto failed; |
| 772 | } |
| 773 | |
| 774 | if (cp->val == test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 775 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 776 | goto failed; |
| 777 | } |
| 778 | |
| 779 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 780 | if (!cmd) { |
| 781 | err = -ENOMEM; |
| 782 | goto failed; |
| 783 | } |
| 784 | |
| 785 | if (cp->val) |
| 786 | scan = SCAN_PAGE; |
| 787 | else |
| 788 | scan = 0; |
| 789 | |
| 790 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 791 | if (err < 0) |
| 792 | mgmt_pending_remove(cmd); |
| 793 | |
| 794 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 795 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 796 | hci_dev_put(hdev); |
| 797 | |
| 798 | return err; |
| 799 | } |
| 800 | |
| 801 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, |
| 802 | u16 data_len, struct sock *skip_sk) |
| 803 | { |
| 804 | struct sk_buff *skb; |
| 805 | struct mgmt_hdr *hdr; |
| 806 | |
| 807 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_ATOMIC); |
| 808 | if (!skb) |
| 809 | return -ENOMEM; |
| 810 | |
| 811 | bt_cb(skb)->channel = HCI_CHANNEL_CONTROL; |
| 812 | |
| 813 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 814 | hdr->opcode = cpu_to_le16(event); |
| 815 | if (hdev) |
| 816 | hdr->index = cpu_to_le16(hdev->id); |
| 817 | else |
| 818 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
| 819 | hdr->len = cpu_to_le16(data_len); |
| 820 | |
| 821 | if (data) |
| 822 | memcpy(skb_put(skb, data_len), data, data_len); |
| 823 | |
| 824 | hci_send_to_sock(NULL, skb, skip_sk); |
| 825 | kfree_skb(skb); |
| 826 | |
| 827 | return 0; |
| 828 | } |
| 829 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 830 | static int set_pairable(struct sock *sk, u16 index, unsigned char *data, |
| 831 | u16 len) |
| 832 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 833 | struct mgmt_mode *cp; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 834 | struct hci_dev *hdev; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 835 | __le32 ev; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 836 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 837 | |
| 838 | cp = (void *) data; |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 839 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 840 | BT_DBG("request for hci%u", index); |
| 841 | |
| 842 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 843 | return cmd_status(sk, index, MGMT_OP_SET_PAIRABLE, |
| 844 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 845 | |
| 846 | hdev = hci_dev_get(index); |
| 847 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 848 | return cmd_status(sk, index, MGMT_OP_SET_PAIRABLE, |
| 849 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 850 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 851 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 852 | |
| 853 | if (cp->val) |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 854 | set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 855 | else |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 856 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 857 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 858 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 859 | if (err < 0) |
| 860 | goto failed; |
| 861 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 862 | ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 863 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 864 | err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 865 | |
| 866 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 867 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 868 | hci_dev_put(hdev); |
| 869 | |
| 870 | return err; |
| 871 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 872 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 873 | static int add_uuid(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 874 | { |
| 875 | struct mgmt_cp_add_uuid *cp; |
| 876 | struct hci_dev *hdev; |
| 877 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 878 | int err; |
| 879 | |
| 880 | cp = (void *) data; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 881 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 882 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 883 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 884 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 885 | return cmd_status(sk, index, MGMT_OP_ADD_UUID, |
| 886 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 887 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 888 | hdev = hci_dev_get(index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 889 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 890 | return cmd_status(sk, index, MGMT_OP_ADD_UUID, |
| 891 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 892 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 893 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 894 | |
| 895 | uuid = kmalloc(sizeof(*uuid), GFP_ATOMIC); |
| 896 | if (!uuid) { |
| 897 | err = -ENOMEM; |
| 898 | goto failed; |
| 899 | } |
| 900 | |
| 901 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 902 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 903 | |
| 904 | list_add(&uuid->list, &hdev->uuids); |
| 905 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 906 | err = update_class(hdev); |
| 907 | if (err < 0) |
| 908 | goto failed; |
| 909 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 910 | err = update_eir(hdev); |
| 911 | if (err < 0) |
| 912 | goto failed; |
| 913 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 914 | err = cmd_complete(sk, index, MGMT_OP_ADD_UUID, NULL, 0); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 915 | |
| 916 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 917 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 918 | hci_dev_put(hdev); |
| 919 | |
| 920 | return err; |
| 921 | } |
| 922 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 923 | static int remove_uuid(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 924 | { |
| 925 | struct list_head *p, *n; |
Szymon Janc | 779cb85 | 2011-02-25 19:05:47 +0100 | [diff] [blame] | 926 | struct mgmt_cp_remove_uuid *cp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 927 | struct hci_dev *hdev; |
| 928 | u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 929 | int err, found; |
| 930 | |
| 931 | cp = (void *) data; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 932 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 933 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 934 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 935 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 936 | return cmd_status(sk, index, MGMT_OP_REMOVE_UUID, |
| 937 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 938 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 939 | hdev = hci_dev_get(index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 940 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 941 | return cmd_status(sk, index, MGMT_OP_REMOVE_UUID, |
| 942 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 943 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 944 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 945 | |
| 946 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
| 947 | err = hci_uuids_clear(hdev); |
| 948 | goto unlock; |
| 949 | } |
| 950 | |
| 951 | found = 0; |
| 952 | |
| 953 | list_for_each_safe(p, n, &hdev->uuids) { |
| 954 | struct bt_uuid *match = list_entry(p, struct bt_uuid, list); |
| 955 | |
| 956 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 957 | continue; |
| 958 | |
| 959 | list_del(&match->list); |
| 960 | found++; |
| 961 | } |
| 962 | |
| 963 | if (found == 0) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 964 | err = cmd_status(sk, index, MGMT_OP_REMOVE_UUID, |
| 965 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 966 | goto unlock; |
| 967 | } |
| 968 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 969 | err = update_class(hdev); |
| 970 | if (err < 0) |
| 971 | goto unlock; |
| 972 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 973 | err = update_eir(hdev); |
| 974 | if (err < 0) |
| 975 | goto unlock; |
| 976 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 977 | err = cmd_complete(sk, index, MGMT_OP_REMOVE_UUID, NULL, 0); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 978 | |
| 979 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 980 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 981 | hci_dev_put(hdev); |
| 982 | |
| 983 | return err; |
| 984 | } |
| 985 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 986 | static int set_dev_class(struct sock *sk, u16 index, unsigned char *data, |
| 987 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 988 | { |
| 989 | struct hci_dev *hdev; |
| 990 | struct mgmt_cp_set_dev_class *cp; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 991 | int err; |
| 992 | |
| 993 | cp = (void *) data; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 994 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 995 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 996 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 997 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 998 | return cmd_status(sk, index, MGMT_OP_SET_DEV_CLASS, |
| 999 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1000 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1001 | hdev = hci_dev_get(index); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1002 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1003 | return cmd_status(sk, index, MGMT_OP_SET_DEV_CLASS, |
| 1004 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1005 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1006 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1007 | |
| 1008 | hdev->major_class = cp->major; |
| 1009 | hdev->minor_class = cp->minor; |
| 1010 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1011 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1012 | hci_dev_unlock(hdev); |
| 1013 | cancel_delayed_work_sync(&hdev->service_cache); |
| 1014 | hci_dev_lock(hdev); |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 1015 | update_eir(hdev); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1016 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 1017 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1018 | err = update_class(hdev); |
| 1019 | |
| 1020 | if (err == 0) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1021 | err = cmd_complete(sk, index, MGMT_OP_SET_DEV_CLASS, NULL, 0); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1022 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1023 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1024 | hci_dev_put(hdev); |
| 1025 | |
| 1026 | return err; |
| 1027 | } |
| 1028 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 1029 | static int load_link_keys(struct sock *sk, u16 index, unsigned char *data, |
| 1030 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1031 | { |
| 1032 | struct hci_dev *hdev; |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 1033 | struct mgmt_cp_load_link_keys *cp; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1034 | u16 key_count, expected_len; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 1035 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1036 | |
| 1037 | cp = (void *) data; |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1038 | |
| 1039 | if (len < sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1040 | return cmd_status(sk, index, MGMT_OP_LOAD_LINK_KEYS, |
| 1041 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1042 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1043 | key_count = get_unaligned_le16(&cp->key_count); |
| 1044 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 1045 | expected_len = sizeof(*cp) + key_count * |
| 1046 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 1047 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 1048 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 1049 | len, expected_len); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1050 | return cmd_status(sk, index, MGMT_OP_LOAD_LINK_KEYS, |
| 1051 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1052 | } |
| 1053 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1054 | hdev = hci_dev_get(index); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1055 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1056 | return cmd_status(sk, index, MGMT_OP_LOAD_LINK_KEYS, |
| 1057 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1058 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1059 | BT_DBG("hci%u debug_keys %u key_count %u", index, cp->debug_keys, |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1060 | key_count); |
| 1061 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1062 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1063 | |
| 1064 | hci_link_keys_clear(hdev); |
| 1065 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1066 | set_bit(HCI_LINK_KEYS, &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1067 | |
| 1068 | if (cp->debug_keys) |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1069 | set_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1070 | else |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1071 | clear_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1072 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 1073 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 1074 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1075 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1076 | hci_add_link_key(hdev, NULL, 0, &key->bdaddr, key->val, key->type, |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1077 | key->pin_len); |
| 1078 | } |
| 1079 | |
Johan Hedberg | 0e5f875 | 2011-11-11 16:18:54 +0200 | [diff] [blame] | 1080 | cmd_complete(sk, index, MGMT_OP_LOAD_LINK_KEYS, NULL, 0); |
| 1081 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1082 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1083 | hci_dev_put(hdev); |
| 1084 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 1085 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1086 | } |
| 1087 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 1088 | static int remove_keys(struct sock *sk, u16 index, unsigned char *data, |
| 1089 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1090 | { |
| 1091 | struct hci_dev *hdev; |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 1092 | struct mgmt_cp_remove_keys *cp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1093 | struct mgmt_rp_remove_keys rp; |
| 1094 | struct hci_cp_disconnect dc; |
| 1095 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1096 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1097 | int err; |
| 1098 | |
| 1099 | cp = (void *) data; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1100 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1101 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1102 | return cmd_status(sk, index, MGMT_OP_REMOVE_KEYS, |
| 1103 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1104 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1105 | hdev = hci_dev_get(index); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1106 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1107 | return cmd_status(sk, index, MGMT_OP_REMOVE_KEYS, |
| 1108 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1109 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1110 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1111 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1112 | memset(&rp, 0, sizeof(rp)); |
| 1113 | bacpy(&rp.bdaddr, &cp->bdaddr); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1114 | rp.status = MGMT_STATUS_FAILED; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1115 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1116 | err = hci_remove_link_key(hdev, &cp->bdaddr); |
| 1117 | if (err < 0) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1118 | rp.status = MGMT_STATUS_NOT_PAIRED; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1119 | goto unlock; |
| 1120 | } |
| 1121 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1122 | if (!test_bit(HCI_UP, &hdev->flags) || !cp->disconnect) { |
| 1123 | err = cmd_complete(sk, index, MGMT_OP_REMOVE_KEYS, &rp, |
| 1124 | sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1125 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1126 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1127 | |
| 1128 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1129 | if (!conn) { |
| 1130 | err = cmd_complete(sk, index, MGMT_OP_REMOVE_KEYS, &rp, |
| 1131 | sizeof(rp)); |
| 1132 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1135 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_KEYS, hdev, cp, sizeof(*cp)); |
| 1136 | if (!cmd) { |
| 1137 | err = -ENOMEM; |
| 1138 | goto unlock; |
| 1139 | } |
| 1140 | |
| 1141 | put_unaligned_le16(conn->handle, &dc.handle); |
| 1142 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 1143 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1144 | if (err < 0) |
| 1145 | mgmt_pending_remove(cmd); |
| 1146 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1147 | unlock: |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1148 | if (err < 0) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1149 | err = cmd_complete(sk, index, MGMT_OP_REMOVE_KEYS, &rp, |
| 1150 | sizeof(rp)); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1151 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1152 | hci_dev_put(hdev); |
| 1153 | |
| 1154 | return err; |
| 1155 | } |
| 1156 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1157 | static int disconnect(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1158 | { |
| 1159 | struct hci_dev *hdev; |
| 1160 | struct mgmt_cp_disconnect *cp; |
| 1161 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1162 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1163 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1164 | int err; |
| 1165 | |
| 1166 | BT_DBG(""); |
| 1167 | |
| 1168 | cp = (void *) data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1169 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1170 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1171 | return cmd_status(sk, index, MGMT_OP_DISCONNECT, |
| 1172 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1173 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1174 | hdev = hci_dev_get(index); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1175 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1176 | return cmd_status(sk, index, MGMT_OP_DISCONNECT, |
| 1177 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1178 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1179 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1180 | |
| 1181 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1182 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, |
| 1183 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1184 | goto failed; |
| 1185 | } |
| 1186 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1187 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1188 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, |
| 1189 | MGMT_STATUS_BUSY); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1190 | goto failed; |
| 1191 | } |
| 1192 | |
| 1193 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 1194 | if (!conn) |
| 1195 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->bdaddr); |
| 1196 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1197 | if (!conn) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1198 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, |
| 1199 | MGMT_STATUS_NOT_CONNECTED); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1200 | goto failed; |
| 1201 | } |
| 1202 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1203 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1204 | if (!cmd) { |
| 1205 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1206 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1207 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1208 | |
| 1209 | put_unaligned_le16(conn->handle, &dc.handle); |
| 1210 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 1211 | |
| 1212 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1213 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1214 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1215 | |
| 1216 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1217 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1218 | hci_dev_put(hdev); |
| 1219 | |
| 1220 | return err; |
| 1221 | } |
| 1222 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 1223 | static u8 link_to_mgmt(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1224 | { |
| 1225 | switch (link_type) { |
| 1226 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 1227 | switch (addr_type) { |
| 1228 | case ADDR_LE_DEV_PUBLIC: |
| 1229 | return MGMT_ADDR_LE_PUBLIC; |
| 1230 | case ADDR_LE_DEV_RANDOM: |
| 1231 | return MGMT_ADDR_LE_RANDOM; |
| 1232 | default: |
| 1233 | return MGMT_ADDR_INVALID; |
| 1234 | } |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1235 | case ACL_LINK: |
| 1236 | return MGMT_ADDR_BREDR; |
| 1237 | default: |
| 1238 | return MGMT_ADDR_INVALID; |
| 1239 | } |
| 1240 | } |
| 1241 | |
Szymon Janc | 8ce6284 | 2011-03-01 16:55:32 +0100 | [diff] [blame] | 1242 | static int get_connections(struct sock *sk, u16 index) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1243 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1244 | struct mgmt_rp_get_connections *rp; |
| 1245 | struct hci_dev *hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1246 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1247 | size_t rp_len; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1248 | u16 count; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1249 | int i, err; |
| 1250 | |
| 1251 | BT_DBG(""); |
| 1252 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1253 | hdev = hci_dev_get(index); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1254 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1255 | return cmd_status(sk, index, MGMT_OP_GET_CONNECTIONS, |
| 1256 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1257 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1258 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1259 | |
| 1260 | count = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 1261 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 1262 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 1263 | count++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1264 | } |
| 1265 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1266 | rp_len = sizeof(*rp) + (count * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1267 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 1268 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1269 | err = -ENOMEM; |
| 1270 | goto unlock; |
| 1271 | } |
| 1272 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1273 | put_unaligned_le16(count, &rp->conn_count); |
| 1274 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1275 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1276 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 1277 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 1278 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1279 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 1280 | rp->addr[i].type = link_to_mgmt(c->type, c->dst_type); |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1281 | if (rp->addr[i].type == MGMT_ADDR_INVALID) |
| 1282 | continue; |
| 1283 | i++; |
| 1284 | } |
| 1285 | |
| 1286 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 1287 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1288 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1289 | err = cmd_complete(sk, index, MGMT_OP_GET_CONNECTIONS, rp, rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1290 | |
| 1291 | unlock: |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1292 | kfree(rp); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1293 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1294 | hci_dev_put(hdev); |
| 1295 | return err; |
| 1296 | } |
| 1297 | |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1298 | static int send_pin_code_neg_reply(struct sock *sk, u16 index, |
| 1299 | struct hci_dev *hdev, struct mgmt_cp_pin_code_neg_reply *cp) |
| 1300 | { |
| 1301 | struct pending_cmd *cmd; |
| 1302 | int err; |
| 1303 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1304 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp, |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1305 | sizeof(*cp)); |
| 1306 | if (!cmd) |
| 1307 | return -ENOMEM; |
| 1308 | |
| 1309 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, sizeof(cp->bdaddr), |
| 1310 | &cp->bdaddr); |
| 1311 | if (err < 0) |
| 1312 | mgmt_pending_remove(cmd); |
| 1313 | |
| 1314 | return err; |
| 1315 | } |
| 1316 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1317 | static int pin_code_reply(struct sock *sk, u16 index, unsigned char *data, |
| 1318 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1319 | { |
| 1320 | struct hci_dev *hdev; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1321 | struct hci_conn *conn; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1322 | struct mgmt_cp_pin_code_reply *cp; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1323 | struct mgmt_cp_pin_code_neg_reply ncp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1324 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1325 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1326 | int err; |
| 1327 | |
| 1328 | BT_DBG(""); |
| 1329 | |
| 1330 | cp = (void *) data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1331 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1332 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1333 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
| 1334 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1335 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1336 | hdev = hci_dev_get(index); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1337 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1338 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
| 1339 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1340 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1341 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1342 | |
| 1343 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1344 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
| 1345 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1346 | goto failed; |
| 1347 | } |
| 1348 | |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1349 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); |
| 1350 | if (!conn) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1351 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
| 1352 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1353 | goto failed; |
| 1354 | } |
| 1355 | |
| 1356 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
| 1357 | bacpy(&ncp.bdaddr, &cp->bdaddr); |
| 1358 | |
| 1359 | BT_ERR("PIN code is not 16 bytes long"); |
| 1360 | |
| 1361 | err = send_pin_code_neg_reply(sk, index, hdev, &ncp); |
| 1362 | if (err >= 0) |
| 1363 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1364 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1365 | |
| 1366 | goto failed; |
| 1367 | } |
| 1368 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1369 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1370 | if (!cmd) { |
| 1371 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1372 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1373 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1374 | |
| 1375 | bacpy(&reply.bdaddr, &cp->bdaddr); |
| 1376 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 1377 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1378 | |
| 1379 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 1380 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1381 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1382 | |
| 1383 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1384 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1385 | hci_dev_put(hdev); |
| 1386 | |
| 1387 | return err; |
| 1388 | } |
| 1389 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1390 | static int pin_code_neg_reply(struct sock *sk, u16 index, unsigned char *data, |
| 1391 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1392 | { |
| 1393 | struct hci_dev *hdev; |
| 1394 | struct mgmt_cp_pin_code_neg_reply *cp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1395 | int err; |
| 1396 | |
| 1397 | BT_DBG(""); |
| 1398 | |
| 1399 | cp = (void *) data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1400 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1401 | if (len != sizeof(*cp)) |
| 1402 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1403 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1404 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1405 | hdev = hci_dev_get(index); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1406 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1407 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1408 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1409 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1410 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1411 | |
| 1412 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1413 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1414 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1415 | goto failed; |
| 1416 | } |
| 1417 | |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1418 | err = send_pin_code_neg_reply(sk, index, hdev, cp); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1419 | |
| 1420 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1421 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1422 | hci_dev_put(hdev); |
| 1423 | |
| 1424 | return err; |
| 1425 | } |
| 1426 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1427 | static int set_io_capability(struct sock *sk, u16 index, unsigned char *data, |
| 1428 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1429 | { |
| 1430 | struct hci_dev *hdev; |
| 1431 | struct mgmt_cp_set_io_capability *cp; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1432 | |
| 1433 | BT_DBG(""); |
| 1434 | |
| 1435 | cp = (void *) data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1436 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1437 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1438 | return cmd_status(sk, index, MGMT_OP_SET_IO_CAPABILITY, |
| 1439 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1440 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1441 | hdev = hci_dev_get(index); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1442 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1443 | return cmd_status(sk, index, MGMT_OP_SET_IO_CAPABILITY, |
| 1444 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1445 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1446 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1447 | |
| 1448 | hdev->io_capability = cp->io_capability; |
| 1449 | |
| 1450 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Szymon Janc | b8534e0 | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 1451 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1452 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1453 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1454 | hci_dev_put(hdev); |
| 1455 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1456 | return cmd_complete(sk, index, MGMT_OP_SET_IO_CAPABILITY, NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1457 | } |
| 1458 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1459 | static inline struct pending_cmd *find_pairing(struct hci_conn *conn) |
| 1460 | { |
| 1461 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1462 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1463 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1464 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1465 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 1466 | continue; |
| 1467 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1468 | if (cmd->user_data != conn) |
| 1469 | continue; |
| 1470 | |
| 1471 | return cmd; |
| 1472 | } |
| 1473 | |
| 1474 | return NULL; |
| 1475 | } |
| 1476 | |
| 1477 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 1478 | { |
| 1479 | struct mgmt_rp_pair_device rp; |
| 1480 | struct hci_conn *conn = cmd->user_data; |
| 1481 | |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 1482 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 1483 | rp.addr.type = link_to_mgmt(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1484 | rp.status = status; |
| 1485 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1486 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1487 | |
| 1488 | /* So we don't get further callbacks for this connection */ |
| 1489 | conn->connect_cfm_cb = NULL; |
| 1490 | conn->security_cfm_cb = NULL; |
| 1491 | conn->disconn_cfm_cb = NULL; |
| 1492 | |
| 1493 | hci_conn_put(conn); |
| 1494 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1495 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 1499 | { |
| 1500 | struct pending_cmd *cmd; |
| 1501 | |
| 1502 | BT_DBG("status %u", status); |
| 1503 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 1504 | cmd = find_pairing(conn); |
| 1505 | if (!cmd) |
| 1506 | BT_DBG("Unable to find a pending command"); |
| 1507 | else |
| 1508 | pairing_complete(cmd, status); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1509 | } |
| 1510 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1511 | static int pair_device(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1512 | { |
| 1513 | struct hci_dev *hdev; |
| 1514 | struct mgmt_cp_pair_device *cp; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 1515 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1516 | struct pending_cmd *cmd; |
| 1517 | u8 sec_level, auth_type; |
| 1518 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1519 | int err; |
| 1520 | |
| 1521 | BT_DBG(""); |
| 1522 | |
| 1523 | cp = (void *) data; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1524 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1525 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1526 | return cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, |
| 1527 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1528 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1529 | hdev = hci_dev_get(index); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1530 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1531 | return cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, |
| 1532 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1533 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1534 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1535 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 1536 | sec_level = BT_SECURITY_MEDIUM; |
| 1537 | if (cp->io_cap == 0x03) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1538 | auth_type = HCI_AT_DEDICATED_BONDING; |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 1539 | else |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1540 | auth_type = HCI_AT_DEDICATED_BONDING_MITM; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1541 | |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 1542 | if (cp->addr.type == MGMT_ADDR_BREDR) |
| 1543 | conn = hci_connect(hdev, ACL_LINK, &cp->addr.bdaddr, sec_level, |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1544 | auth_type); |
| 1545 | else |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 1546 | conn = hci_connect(hdev, LE_LINK, &cp->addr.bdaddr, sec_level, |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1547 | auth_type); |
| 1548 | |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 1549 | memset(&rp, 0, sizeof(rp)); |
| 1550 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 1551 | rp.addr.type = cp->addr.type; |
| 1552 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1553 | if (IS_ERR(conn)) { |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 1554 | rp.status = -PTR_ERR(conn); |
| 1555 | err = cmd_complete(sk, index, MGMT_OP_PAIR_DEVICE, |
| 1556 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1557 | goto unlock; |
| 1558 | } |
| 1559 | |
| 1560 | if (conn->connect_cfm_cb) { |
| 1561 | hci_conn_put(conn); |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 1562 | rp.status = EBUSY; |
| 1563 | err = cmd_complete(sk, index, MGMT_OP_PAIR_DEVICE, |
| 1564 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1565 | goto unlock; |
| 1566 | } |
| 1567 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1568 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1569 | if (!cmd) { |
| 1570 | err = -ENOMEM; |
| 1571 | hci_conn_put(conn); |
| 1572 | goto unlock; |
| 1573 | } |
| 1574 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1575 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 1576 | if (cp->addr.type == MGMT_ADDR_BREDR) |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1577 | conn->connect_cfm_cb = pairing_complete_cb; |
| 1578 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1579 | conn->security_cfm_cb = pairing_complete_cb; |
| 1580 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 1581 | conn->io_capability = cp->io_cap; |
| 1582 | cmd->user_data = conn; |
| 1583 | |
| 1584 | if (conn->state == BT_CONNECTED && |
| 1585 | hci_conn_security(conn, sec_level, auth_type)) |
| 1586 | pairing_complete(cmd, 0); |
| 1587 | |
| 1588 | err = 0; |
| 1589 | |
| 1590 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1591 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1592 | hci_dev_put(hdev); |
| 1593 | |
| 1594 | return err; |
| 1595 | } |
| 1596 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame^] | 1597 | static int cancel_pair_device(struct sock *sk, u16 index, |
| 1598 | unsigned char *data, u16 len) |
| 1599 | { |
| 1600 | struct mgmt_addr_info *addr = (void *) data; |
| 1601 | struct hci_dev *hdev; |
| 1602 | struct pending_cmd *cmd; |
| 1603 | struct hci_conn *conn; |
| 1604 | int err; |
| 1605 | |
| 1606 | BT_DBG(""); |
| 1607 | |
| 1608 | if (len != sizeof(*addr)) |
| 1609 | return cmd_status(sk, index, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 1610 | MGMT_STATUS_INVALID_PARAMS); |
| 1611 | |
| 1612 | hdev = hci_dev_get(index); |
| 1613 | if (!hdev) |
| 1614 | return cmd_status(sk, index, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 1615 | MGMT_STATUS_INVALID_PARAMS); |
| 1616 | |
| 1617 | hci_dev_lock(hdev); |
| 1618 | |
| 1619 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 1620 | if (!cmd) { |
| 1621 | err = cmd_status(sk, index, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 1622 | MGMT_STATUS_INVALID_PARAMS); |
| 1623 | goto unlock; |
| 1624 | } |
| 1625 | |
| 1626 | conn = cmd->user_data; |
| 1627 | |
| 1628 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
| 1629 | err = cmd_status(sk, index, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 1630 | MGMT_STATUS_INVALID_PARAMS); |
| 1631 | goto unlock; |
| 1632 | } |
| 1633 | |
| 1634 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 1635 | |
| 1636 | err = cmd_complete(sk, index, MGMT_OP_CANCEL_PAIR_DEVICE, addr, |
| 1637 | sizeof(*addr)); |
| 1638 | unlock: |
| 1639 | hci_dev_unlock(hdev); |
| 1640 | hci_dev_put(hdev); |
| 1641 | |
| 1642 | return err; |
| 1643 | } |
| 1644 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1645 | static int user_pairing_resp(struct sock *sk, u16 index, bdaddr_t *bdaddr, |
| 1646 | u16 mgmt_op, u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1647 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1648 | struct pending_cmd *cmd; |
| 1649 | struct hci_dev *hdev; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1650 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1651 | int err; |
| 1652 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1653 | hdev = hci_dev_get(index); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1654 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1655 | return cmd_status(sk, index, mgmt_op, |
| 1656 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1657 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1658 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 1659 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1660 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1661 | err = cmd_status(sk, index, mgmt_op, MGMT_STATUS_NOT_POWERED); |
| 1662 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1663 | } |
| 1664 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 1665 | /* |
| 1666 | * Check for an existing ACL link, if present pair via |
| 1667 | * HCI commands. |
| 1668 | * |
| 1669 | * If no ACL link is present, check for an LE link and if |
| 1670 | * present, pair via the SMP engine. |
| 1671 | * |
| 1672 | * If neither ACL nor LE links are present, fail with error. |
| 1673 | */ |
| 1674 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, bdaddr); |
| 1675 | if (!conn) { |
| 1676 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, bdaddr); |
| 1677 | if (!conn) { |
| 1678 | err = cmd_status(sk, index, mgmt_op, |
| 1679 | MGMT_STATUS_NOT_CONNECTED); |
| 1680 | goto done; |
| 1681 | } |
| 1682 | |
| 1683 | /* Continue with pairing via SMP */ |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 1684 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 1685 | |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 1686 | if (!err) |
| 1687 | err = cmd_status(sk, index, mgmt_op, |
| 1688 | MGMT_STATUS_SUCCESS); |
| 1689 | else |
| 1690 | err = cmd_status(sk, index, mgmt_op, |
| 1691 | MGMT_STATUS_FAILED); |
| 1692 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 1693 | goto done; |
| 1694 | } |
| 1695 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1696 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, bdaddr, sizeof(*bdaddr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1697 | if (!cmd) { |
| 1698 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1699 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1700 | } |
| 1701 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1702 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 1703 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 1704 | struct hci_cp_user_passkey_reply cp; |
| 1705 | |
| 1706 | bacpy(&cp.bdaddr, bdaddr); |
| 1707 | cp.passkey = passkey; |
| 1708 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 1709 | } else |
| 1710 | err = hci_send_cmd(hdev, hci_op, sizeof(*bdaddr), bdaddr); |
| 1711 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1712 | if (err < 0) |
| 1713 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1714 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1715 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1716 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1717 | hci_dev_put(hdev); |
| 1718 | |
| 1719 | return err; |
| 1720 | } |
| 1721 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1722 | static int user_confirm_reply(struct sock *sk, u16 index, void *data, u16 len) |
| 1723 | { |
| 1724 | struct mgmt_cp_user_confirm_reply *cp = (void *) data; |
| 1725 | |
| 1726 | BT_DBG(""); |
| 1727 | |
| 1728 | if (len != sizeof(*cp)) |
| 1729 | return cmd_status(sk, index, MGMT_OP_USER_CONFIRM_REPLY, |
| 1730 | MGMT_STATUS_INVALID_PARAMS); |
| 1731 | |
| 1732 | return user_pairing_resp(sk, index, &cp->bdaddr, |
| 1733 | MGMT_OP_USER_CONFIRM_REPLY, |
| 1734 | HCI_OP_USER_CONFIRM_REPLY, 0); |
| 1735 | } |
| 1736 | |
| 1737 | static int user_confirm_neg_reply(struct sock *sk, u16 index, void *data, |
| 1738 | u16 len) |
| 1739 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 1740 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1741 | |
| 1742 | BT_DBG(""); |
| 1743 | |
| 1744 | if (len != sizeof(*cp)) |
| 1745 | return cmd_status(sk, index, MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 1746 | MGMT_STATUS_INVALID_PARAMS); |
| 1747 | |
| 1748 | return user_pairing_resp(sk, index, &cp->bdaddr, |
| 1749 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 1750 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
| 1751 | } |
| 1752 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 1753 | static int user_passkey_reply(struct sock *sk, u16 index, void *data, u16 len) |
| 1754 | { |
| 1755 | struct mgmt_cp_user_passkey_reply *cp = (void *) data; |
| 1756 | |
| 1757 | BT_DBG(""); |
| 1758 | |
| 1759 | if (len != sizeof(*cp)) |
| 1760 | return cmd_status(sk, index, MGMT_OP_USER_PASSKEY_REPLY, |
| 1761 | EINVAL); |
| 1762 | |
| 1763 | return user_pairing_resp(sk, index, &cp->bdaddr, |
| 1764 | MGMT_OP_USER_PASSKEY_REPLY, |
| 1765 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
| 1766 | } |
| 1767 | |
| 1768 | static int user_passkey_neg_reply(struct sock *sk, u16 index, void *data, |
| 1769 | u16 len) |
| 1770 | { |
| 1771 | struct mgmt_cp_user_passkey_neg_reply *cp = (void *) data; |
| 1772 | |
| 1773 | BT_DBG(""); |
| 1774 | |
| 1775 | if (len != sizeof(*cp)) |
| 1776 | return cmd_status(sk, index, MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 1777 | EINVAL); |
| 1778 | |
| 1779 | return user_pairing_resp(sk, index, &cp->bdaddr, |
| 1780 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 1781 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
| 1782 | } |
| 1783 | |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1784 | static int set_local_name(struct sock *sk, u16 index, unsigned char *data, |
| 1785 | u16 len) |
| 1786 | { |
| 1787 | struct mgmt_cp_set_local_name *mgmt_cp = (void *) data; |
| 1788 | struct hci_cp_write_local_name hci_cp; |
| 1789 | struct hci_dev *hdev; |
| 1790 | struct pending_cmd *cmd; |
| 1791 | int err; |
| 1792 | |
| 1793 | BT_DBG(""); |
| 1794 | |
| 1795 | if (len != sizeof(*mgmt_cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1796 | return cmd_status(sk, index, MGMT_OP_SET_LOCAL_NAME, |
| 1797 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1798 | |
| 1799 | hdev = hci_dev_get(index); |
| 1800 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1801 | return cmd_status(sk, index, MGMT_OP_SET_LOCAL_NAME, |
| 1802 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1803 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1804 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1805 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1806 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1807 | if (!cmd) { |
| 1808 | err = -ENOMEM; |
| 1809 | goto failed; |
| 1810 | } |
| 1811 | |
| 1812 | memcpy(hci_cp.name, mgmt_cp->name, sizeof(hci_cp.name)); |
| 1813 | err = hci_send_cmd(hdev, HCI_OP_WRITE_LOCAL_NAME, sizeof(hci_cp), |
| 1814 | &hci_cp); |
| 1815 | if (err < 0) |
| 1816 | mgmt_pending_remove(cmd); |
| 1817 | |
| 1818 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1819 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1820 | hci_dev_put(hdev); |
| 1821 | |
| 1822 | return err; |
| 1823 | } |
| 1824 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1825 | static int read_local_oob_data(struct sock *sk, u16 index) |
| 1826 | { |
| 1827 | struct hci_dev *hdev; |
| 1828 | struct pending_cmd *cmd; |
| 1829 | int err; |
| 1830 | |
| 1831 | BT_DBG("hci%u", index); |
| 1832 | |
| 1833 | hdev = hci_dev_get(index); |
| 1834 | if (!hdev) |
| 1835 | return cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1836 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1837 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1838 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1839 | |
| 1840 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 1841 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1842 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1843 | goto unlock; |
| 1844 | } |
| 1845 | |
| 1846 | if (!(hdev->features[6] & LMP_SIMPLE_PAIR)) { |
| 1847 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1848 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1849 | goto unlock; |
| 1850 | } |
| 1851 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1852 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1853 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 1854 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1855 | goto unlock; |
| 1856 | } |
| 1857 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1858 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1859 | if (!cmd) { |
| 1860 | err = -ENOMEM; |
| 1861 | goto unlock; |
| 1862 | } |
| 1863 | |
| 1864 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 1865 | if (err < 0) |
| 1866 | mgmt_pending_remove(cmd); |
| 1867 | |
| 1868 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1869 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1870 | hci_dev_put(hdev); |
| 1871 | |
| 1872 | return err; |
| 1873 | } |
| 1874 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1875 | static int add_remote_oob_data(struct sock *sk, u16 index, unsigned char *data, |
| 1876 | u16 len) |
| 1877 | { |
| 1878 | struct hci_dev *hdev; |
| 1879 | struct mgmt_cp_add_remote_oob_data *cp = (void *) data; |
| 1880 | int err; |
| 1881 | |
| 1882 | BT_DBG("hci%u ", index); |
| 1883 | |
| 1884 | if (len != sizeof(*cp)) |
| 1885 | return cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1886 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1887 | |
| 1888 | hdev = hci_dev_get(index); |
| 1889 | if (!hdev) |
| 1890 | return cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1891 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1892 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1893 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1894 | |
| 1895 | err = hci_add_remote_oob_data(hdev, &cp->bdaddr, cp->hash, |
| 1896 | cp->randomizer); |
| 1897 | if (err < 0) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1898 | err = cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 1899 | MGMT_STATUS_FAILED); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1900 | else |
| 1901 | err = cmd_complete(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, NULL, |
| 1902 | 0); |
| 1903 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1904 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1905 | hci_dev_put(hdev); |
| 1906 | |
| 1907 | return err; |
| 1908 | } |
| 1909 | |
| 1910 | static int remove_remote_oob_data(struct sock *sk, u16 index, |
| 1911 | unsigned char *data, u16 len) |
| 1912 | { |
| 1913 | struct hci_dev *hdev; |
| 1914 | struct mgmt_cp_remove_remote_oob_data *cp = (void *) data; |
| 1915 | int err; |
| 1916 | |
| 1917 | BT_DBG("hci%u ", index); |
| 1918 | |
| 1919 | if (len != sizeof(*cp)) |
| 1920 | return cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1921 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1922 | |
| 1923 | hdev = hci_dev_get(index); |
| 1924 | if (!hdev) |
| 1925 | return cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1926 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1927 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1928 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1929 | |
| 1930 | err = hci_remove_remote_oob_data(hdev, &cp->bdaddr); |
| 1931 | if (err < 0) |
| 1932 | err = cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1933 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1934 | else |
| 1935 | err = cmd_complete(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 1936 | NULL, 0); |
| 1937 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1938 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1939 | hci_dev_put(hdev); |
| 1940 | |
| 1941 | return err; |
| 1942 | } |
| 1943 | |
Johan Hedberg | 450dfda | 2011-11-12 11:58:22 +0200 | [diff] [blame] | 1944 | static int start_discovery(struct sock *sk, u16 index, |
| 1945 | unsigned char *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1946 | { |
Johan Hedberg | 450dfda | 2011-11-12 11:58:22 +0200 | [diff] [blame] | 1947 | struct mgmt_cp_start_discovery *cp = (void *) data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1948 | struct pending_cmd *cmd; |
| 1949 | struct hci_dev *hdev; |
| 1950 | int err; |
| 1951 | |
| 1952 | BT_DBG("hci%u", index); |
| 1953 | |
Johan Hedberg | 450dfda | 2011-11-12 11:58:22 +0200 | [diff] [blame] | 1954 | if (len != sizeof(*cp)) |
| 1955 | return cmd_status(sk, index, MGMT_OP_START_DISCOVERY, |
| 1956 | MGMT_STATUS_INVALID_PARAMS); |
| 1957 | |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1958 | hdev = hci_dev_get(index); |
| 1959 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1960 | return cmd_status(sk, index, MGMT_OP_START_DISCOVERY, |
| 1961 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1962 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1963 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1964 | |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 1965 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1966 | err = cmd_status(sk, index, MGMT_OP_START_DISCOVERY, |
| 1967 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 1968 | goto failed; |
| 1969 | } |
| 1970 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 1971 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
| 1972 | err = cmd_status(sk, index, MGMT_OP_START_DISCOVERY, |
| 1973 | MGMT_STATUS_BUSY); |
| 1974 | goto failed; |
| 1975 | } |
| 1976 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1977 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1978 | if (!cmd) { |
| 1979 | err = -ENOMEM; |
| 1980 | goto failed; |
| 1981 | } |
| 1982 | |
Andre Guedes | 2519a1f | 2011-11-07 11:45:24 -0300 | [diff] [blame] | 1983 | err = hci_do_inquiry(hdev, INQUIRY_LEN_BREDR); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1984 | if (err < 0) |
| 1985 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 1986 | else |
| 1987 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1988 | |
| 1989 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1990 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1991 | hci_dev_put(hdev); |
| 1992 | |
| 1993 | return err; |
| 1994 | } |
| 1995 | |
| 1996 | static int stop_discovery(struct sock *sk, u16 index) |
| 1997 | { |
| 1998 | struct hci_dev *hdev; |
| 1999 | struct pending_cmd *cmd; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2000 | struct hci_cp_remote_name_req_cancel cp; |
| 2001 | struct inquiry_entry *e; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2002 | int err; |
| 2003 | |
| 2004 | BT_DBG("hci%u", index); |
| 2005 | |
| 2006 | hdev = hci_dev_get(index); |
| 2007 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2008 | return cmd_status(sk, index, MGMT_OP_STOP_DISCOVERY, |
| 2009 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2010 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2011 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2012 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2013 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2014 | err = cmd_status(sk, index, MGMT_OP_STOP_DISCOVERY, |
| 2015 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2016 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2017 | } |
| 2018 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2019 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2020 | if (!cmd) { |
| 2021 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2022 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2023 | } |
| 2024 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2025 | if (hdev->discovery.state == DISCOVERY_INQUIRY) { |
| 2026 | err = hci_cancel_inquiry(hdev); |
| 2027 | if (err < 0) |
| 2028 | mgmt_pending_remove(cmd); |
| 2029 | else |
| 2030 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 2031 | goto unlock; |
| 2032 | } |
| 2033 | |
| 2034 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_PENDING); |
| 2035 | if (!e) { |
| 2036 | mgmt_pending_remove(cmd); |
| 2037 | err = cmd_complete(sk, index, MGMT_OP_STOP_DISCOVERY, NULL, 0); |
| 2038 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 2039 | goto unlock; |
| 2040 | } |
| 2041 | |
| 2042 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 2043 | err = hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL, |
| 2044 | sizeof(cp), &cp); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2045 | if (err < 0) |
| 2046 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2047 | else |
| 2048 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2049 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2050 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2051 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2052 | hci_dev_put(hdev); |
| 2053 | |
| 2054 | return err; |
| 2055 | } |
| 2056 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2057 | static int confirm_name(struct sock *sk, u16 index, unsigned char *data, |
| 2058 | u16 len) |
| 2059 | { |
| 2060 | struct mgmt_cp_confirm_name *cp = (void *) data; |
| 2061 | struct inquiry_entry *e; |
| 2062 | struct hci_dev *hdev; |
| 2063 | int err; |
| 2064 | |
| 2065 | BT_DBG("hci%u", index); |
| 2066 | |
| 2067 | if (len != sizeof(*cp)) |
| 2068 | return cmd_status(sk, index, MGMT_OP_CONFIRM_NAME, |
| 2069 | MGMT_STATUS_INVALID_PARAMS); |
| 2070 | |
| 2071 | hdev = hci_dev_get(index); |
| 2072 | if (!hdev) |
| 2073 | return cmd_status(sk, index, MGMT_OP_CONFIRM_NAME, |
| 2074 | MGMT_STATUS_INVALID_PARAMS); |
| 2075 | |
| 2076 | hci_dev_lock(hdev); |
| 2077 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2078 | if (!hci_discovery_active(hdev)) { |
| 2079 | err = cmd_status(sk, index, MGMT_OP_CONFIRM_NAME, |
| 2080 | MGMT_STATUS_FAILED); |
| 2081 | goto failed; |
| 2082 | } |
| 2083 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2084 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->bdaddr); |
| 2085 | if (!e) { |
| 2086 | err = cmd_status (sk, index, MGMT_OP_CONFIRM_NAME, |
| 2087 | MGMT_STATUS_INVALID_PARAMS); |
| 2088 | goto failed; |
| 2089 | } |
| 2090 | |
| 2091 | if (cp->name_known) { |
| 2092 | e->name_state = NAME_KNOWN; |
| 2093 | list_del(&e->list); |
| 2094 | } else { |
| 2095 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 2096 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | err = 0; |
| 2100 | |
| 2101 | failed: |
| 2102 | hci_dev_unlock(hdev); |
| 2103 | |
| 2104 | return err; |
| 2105 | } |
| 2106 | |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2107 | static int block_device(struct sock *sk, u16 index, unsigned char *data, |
| 2108 | u16 len) |
| 2109 | { |
| 2110 | struct hci_dev *hdev; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2111 | struct mgmt_cp_block_device *cp = (void *) data; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2112 | int err; |
| 2113 | |
| 2114 | BT_DBG("hci%u", index); |
| 2115 | |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2116 | if (len != sizeof(*cp)) |
| 2117 | return cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2118 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2119 | |
| 2120 | hdev = hci_dev_get(index); |
| 2121 | if (!hdev) |
| 2122 | return cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2123 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2124 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2125 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2126 | |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2127 | err = hci_blacklist_add(hdev, &cp->bdaddr); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2128 | if (err < 0) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2129 | err = cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE, |
| 2130 | MGMT_STATUS_FAILED); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2131 | else |
| 2132 | err = cmd_complete(sk, index, MGMT_OP_BLOCK_DEVICE, |
| 2133 | NULL, 0); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2134 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2135 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2136 | hci_dev_put(hdev); |
| 2137 | |
| 2138 | return err; |
| 2139 | } |
| 2140 | |
| 2141 | static int unblock_device(struct sock *sk, u16 index, unsigned char *data, |
| 2142 | u16 len) |
| 2143 | { |
| 2144 | struct hci_dev *hdev; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2145 | struct mgmt_cp_unblock_device *cp = (void *) data; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2146 | int err; |
| 2147 | |
| 2148 | BT_DBG("hci%u", index); |
| 2149 | |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2150 | if (len != sizeof(*cp)) |
| 2151 | return cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2152 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2153 | |
| 2154 | hdev = hci_dev_get(index); |
| 2155 | if (!hdev) |
| 2156 | return cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2157 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2158 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2159 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2160 | |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2161 | err = hci_blacklist_del(hdev, &cp->bdaddr); |
| 2162 | |
| 2163 | if (err < 0) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2164 | err = cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
| 2165 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2166 | else |
| 2167 | err = cmd_complete(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
| 2168 | NULL, 0); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2169 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2170 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2171 | hci_dev_put(hdev); |
| 2172 | |
| 2173 | return err; |
| 2174 | } |
| 2175 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2176 | static int set_fast_connectable(struct sock *sk, u16 index, |
| 2177 | unsigned char *data, u16 len) |
| 2178 | { |
| 2179 | struct hci_dev *hdev; |
Johan Hedberg | f7c6869ce | 2011-12-15 00:47:36 +0200 | [diff] [blame] | 2180 | struct mgmt_mode *cp = (void *) data; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2181 | struct hci_cp_write_page_scan_activity acp; |
| 2182 | u8 type; |
| 2183 | int err; |
| 2184 | |
| 2185 | BT_DBG("hci%u", index); |
| 2186 | |
| 2187 | if (len != sizeof(*cp)) |
| 2188 | return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2189 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2190 | |
| 2191 | hdev = hci_dev_get(index); |
| 2192 | if (!hdev) |
| 2193 | return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2194 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2195 | |
| 2196 | hci_dev_lock(hdev); |
| 2197 | |
Johan Hedberg | f7c6869ce | 2011-12-15 00:47:36 +0200 | [diff] [blame] | 2198 | if (cp->val) { |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2199 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 2200 | acp.interval = 0x0024; /* 22.5 msec page scan interval */ |
| 2201 | } else { |
| 2202 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 2203 | acp.interval = 0x0800; /* default 1.28 sec page scan */ |
| 2204 | } |
| 2205 | |
| 2206 | acp.window = 0x0012; /* default 11.25 msec page scan window */ |
| 2207 | |
| 2208 | err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 2209 | sizeof(acp), &acp); |
| 2210 | if (err < 0) { |
| 2211 | err = cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2212 | MGMT_STATUS_FAILED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2213 | goto done; |
| 2214 | } |
| 2215 | |
| 2216 | err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
| 2217 | if (err < 0) { |
| 2218 | err = cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2219 | MGMT_STATUS_FAILED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2220 | goto done; |
| 2221 | } |
| 2222 | |
| 2223 | err = cmd_complete(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
| 2224 | NULL, 0); |
| 2225 | done: |
| 2226 | hci_dev_unlock(hdev); |
| 2227 | hci_dev_put(hdev); |
| 2228 | |
| 2229 | return err; |
| 2230 | } |
| 2231 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2232 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 2233 | { |
| 2234 | unsigned char *buf; |
| 2235 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2236 | u16 opcode, index, len; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2237 | int err; |
| 2238 | |
| 2239 | BT_DBG("got %zu bytes", msglen); |
| 2240 | |
| 2241 | if (msglen < sizeof(*hdr)) |
| 2242 | return -EINVAL; |
| 2243 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 2244 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2245 | if (!buf) |
| 2246 | return -ENOMEM; |
| 2247 | |
| 2248 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 2249 | err = -EFAULT; |
| 2250 | goto done; |
| 2251 | } |
| 2252 | |
| 2253 | hdr = (struct mgmt_hdr *) buf; |
| 2254 | opcode = get_unaligned_le16(&hdr->opcode); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2255 | index = get_unaligned_le16(&hdr->index); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2256 | len = get_unaligned_le16(&hdr->len); |
| 2257 | |
| 2258 | if (len != msglen - sizeof(*hdr)) { |
| 2259 | err = -EINVAL; |
| 2260 | goto done; |
| 2261 | } |
| 2262 | |
| 2263 | switch (opcode) { |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 2264 | case MGMT_OP_READ_VERSION: |
| 2265 | err = read_version(sk); |
| 2266 | break; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 2267 | case MGMT_OP_READ_INDEX_LIST: |
| 2268 | err = read_index_list(sk); |
| 2269 | break; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 2270 | case MGMT_OP_READ_INFO: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2271 | err = read_controller_info(sk, index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 2272 | break; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2273 | case MGMT_OP_SET_POWERED: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2274 | err = set_powered(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2275 | break; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2276 | case MGMT_OP_SET_DISCOVERABLE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2277 | err = set_discoverable(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2278 | break; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2279 | case MGMT_OP_SET_CONNECTABLE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2280 | err = set_connectable(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2281 | break; |
Johan Hedberg | f7c6869ce | 2011-12-15 00:47:36 +0200 | [diff] [blame] | 2282 | case MGMT_OP_SET_FAST_CONNECTABLE: |
| 2283 | err = set_fast_connectable(sk, index, buf + sizeof(*hdr), |
| 2284 | len); |
| 2285 | break; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 2286 | case MGMT_OP_SET_PAIRABLE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2287 | err = set_pairable(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 2288 | break; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2289 | case MGMT_OP_ADD_UUID: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2290 | err = add_uuid(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2291 | break; |
| 2292 | case MGMT_OP_REMOVE_UUID: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2293 | err = remove_uuid(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2294 | break; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2295 | case MGMT_OP_SET_DEV_CLASS: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2296 | err = set_dev_class(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2297 | break; |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2298 | case MGMT_OP_LOAD_LINK_KEYS: |
| 2299 | err = load_link_keys(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2300 | break; |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2301 | case MGMT_OP_REMOVE_KEYS: |
| 2302 | err = remove_keys(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2303 | break; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2304 | case MGMT_OP_DISCONNECT: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2305 | err = disconnect(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2306 | break; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2307 | case MGMT_OP_GET_CONNECTIONS: |
Szymon Janc | 8ce6284 | 2011-03-01 16:55:32 +0100 | [diff] [blame] | 2308 | err = get_connections(sk, index); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2309 | break; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2310 | case MGMT_OP_PIN_CODE_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2311 | err = pin_code_reply(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2312 | break; |
| 2313 | case MGMT_OP_PIN_CODE_NEG_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2314 | err = pin_code_neg_reply(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2315 | break; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2316 | case MGMT_OP_SET_IO_CAPABILITY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2317 | err = set_io_capability(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2318 | break; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2319 | case MGMT_OP_PAIR_DEVICE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2320 | err = pair_device(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2321 | break; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame^] | 2322 | case MGMT_OP_CANCEL_PAIR_DEVICE: |
| 2323 | err = cancel_pair_device(sk, index, buf + sizeof(*hdr), len); |
| 2324 | break; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2325 | case MGMT_OP_USER_CONFIRM_REPLY: |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2326 | err = user_confirm_reply(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2327 | break; |
| 2328 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2329 | err = user_confirm_neg_reply(sk, index, buf + sizeof(*hdr), |
| 2330 | len); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2331 | break; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2332 | case MGMT_OP_USER_PASSKEY_REPLY: |
| 2333 | err = user_passkey_reply(sk, index, buf + sizeof(*hdr), len); |
| 2334 | break; |
| 2335 | case MGMT_OP_USER_PASSKEY_NEG_REPLY: |
| 2336 | err = user_passkey_neg_reply(sk, index, buf + sizeof(*hdr), |
| 2337 | len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2338 | break; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2339 | case MGMT_OP_SET_LOCAL_NAME: |
| 2340 | err = set_local_name(sk, index, buf + sizeof(*hdr), len); |
| 2341 | break; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2342 | case MGMT_OP_READ_LOCAL_OOB_DATA: |
| 2343 | err = read_local_oob_data(sk, index); |
| 2344 | break; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2345 | case MGMT_OP_ADD_REMOTE_OOB_DATA: |
| 2346 | err = add_remote_oob_data(sk, index, buf + sizeof(*hdr), len); |
| 2347 | break; |
| 2348 | case MGMT_OP_REMOVE_REMOTE_OOB_DATA: |
| 2349 | err = remove_remote_oob_data(sk, index, buf + sizeof(*hdr), |
| 2350 | len); |
| 2351 | break; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2352 | case MGMT_OP_START_DISCOVERY: |
Johan Hedberg | 450dfda | 2011-11-12 11:58:22 +0200 | [diff] [blame] | 2353 | err = start_discovery(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2354 | break; |
| 2355 | case MGMT_OP_STOP_DISCOVERY: |
| 2356 | err = stop_discovery(sk, index); |
| 2357 | break; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2358 | case MGMT_OP_CONFIRM_NAME: |
| 2359 | err = confirm_name(sk, index, buf + sizeof(*hdr), len); |
| 2360 | break; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2361 | case MGMT_OP_BLOCK_DEVICE: |
| 2362 | err = block_device(sk, index, buf + sizeof(*hdr), len); |
| 2363 | break; |
| 2364 | case MGMT_OP_UNBLOCK_DEVICE: |
| 2365 | err = unblock_device(sk, index, buf + sizeof(*hdr), len); |
| 2366 | break; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2367 | default: |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2368 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2369 | err = cmd_status(sk, index, opcode, |
| 2370 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2371 | break; |
| 2372 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2373 | |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2374 | if (err < 0) |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2375 | goto done; |
| 2376 | |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2377 | err = msglen; |
| 2378 | |
| 2379 | done: |
| 2380 | kfree(buf); |
| 2381 | return err; |
| 2382 | } |
| 2383 | |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2384 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 2385 | { |
| 2386 | u8 *status = data; |
| 2387 | |
| 2388 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 2389 | mgmt_pending_remove(cmd); |
| 2390 | } |
| 2391 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2392 | int mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2393 | { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2394 | return mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2395 | } |
| 2396 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2397 | int mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2398 | { |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2399 | u8 status = ENODEV; |
| 2400 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2401 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2402 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2403 | return mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | struct cmd_lookup { |
| 2407 | u8 val; |
| 2408 | struct sock *sk; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2409 | struct hci_dev *hdev; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2410 | }; |
| 2411 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2412 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2413 | { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2414 | struct cmd_lookup *match = data; |
| 2415 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2416 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2417 | |
| 2418 | list_del(&cmd->list); |
| 2419 | |
| 2420 | if (match->sk == NULL) { |
| 2421 | match->sk = cmd->sk; |
| 2422 | sock_hold(match->sk); |
| 2423 | } |
| 2424 | |
| 2425 | mgmt_pending_free(cmd); |
| 2426 | } |
| 2427 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2428 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2429 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2430 | struct cmd_lookup match = { powered, NULL, hdev }; |
| 2431 | __le32 ev; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2432 | int ret; |
| 2433 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2434 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2435 | |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2436 | if (!powered) { |
| 2437 | u8 status = ENETDOWN; |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2438 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2439 | } |
| 2440 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2441 | ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2442 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2443 | ret = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), |
| 2444 | match.sk); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2445 | |
| 2446 | if (match.sk) |
| 2447 | sock_put(match.sk); |
| 2448 | |
| 2449 | return ret; |
| 2450 | } |
| 2451 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2452 | int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2453 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2454 | struct cmd_lookup match = { discoverable, NULL, hdev }; |
| 2455 | __le32 ev; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2456 | int ret; |
| 2457 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2458 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, settings_rsp, &match); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2459 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2460 | ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2461 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2462 | ret = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2463 | match.sk); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2464 | if (match.sk) |
| 2465 | sock_put(match.sk); |
| 2466 | |
| 2467 | return ret; |
| 2468 | } |
| 2469 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2470 | int mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2471 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2472 | __le32 ev; |
| 2473 | struct cmd_lookup match = { connectable, NULL, hdev }; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2474 | int ret; |
| 2475 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2476 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, settings_rsp, |
| 2477 | &match); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2478 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2479 | ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2480 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2481 | ret = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), match.sk); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2482 | |
| 2483 | if (match.sk) |
| 2484 | sock_put(match.sk); |
| 2485 | |
| 2486 | return ret; |
| 2487 | } |
| 2488 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2489 | int mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status) |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2490 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2491 | u8 mgmt_err = mgmt_status(status); |
| 2492 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2493 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2494 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2495 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2496 | |
| 2497 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2498 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2499 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2500 | |
| 2501 | return 0; |
| 2502 | } |
| 2503 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2504 | int mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 2505 | u8 persistent) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2506 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2507 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2508 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2509 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2510 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2511 | ev.store_hint = persistent; |
| 2512 | bacpy(&ev.key.bdaddr, &key->bdaddr); |
| 2513 | ev.key.type = key->type; |
| 2514 | memcpy(ev.key.val, key->val, 16); |
| 2515 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2516 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2517 | return mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2518 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2519 | |
Johan Hedberg | afc747a | 2012-01-15 18:11:07 +0200 | [diff] [blame] | 2520 | int mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2521 | u8 addr_type, u8 *name, u8 name_len, |
| 2522 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2523 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2524 | char buf[512]; |
| 2525 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 2526 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2527 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2528 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 2529 | ev->addr.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2530 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2531 | if (name_len > 0) |
| 2532 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 2533 | name, name_len); |
| 2534 | |
| 2535 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
| 2536 | eir_len = eir_append_data(&ev->eir[eir_len], eir_len, |
| 2537 | EIR_CLASS_OF_DEV, dev_class, 3); |
| 2538 | |
| 2539 | put_unaligned_le16(eir_len, &ev->eir_len); |
| 2540 | |
| 2541 | return mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 2542 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2543 | } |
| 2544 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2545 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 2546 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 2547 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2548 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2549 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2550 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2551 | bacpy(&rp.bdaddr, &cp->bdaddr); |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 2552 | rp.status = 0; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2553 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2554 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2555 | |
| 2556 | *sk = cmd->sk; |
| 2557 | sock_hold(*sk); |
| 2558 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2559 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2560 | } |
| 2561 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2562 | static void remove_keys_rsp(struct pending_cmd *cmd, void *data) |
| 2563 | { |
| 2564 | u8 *status = data; |
| 2565 | struct mgmt_cp_remove_keys *cp = cmd->param; |
| 2566 | struct mgmt_rp_remove_keys rp; |
| 2567 | |
| 2568 | memset(&rp, 0, sizeof(rp)); |
| 2569 | bacpy(&rp.bdaddr, &cp->bdaddr); |
| 2570 | if (status != NULL) |
| 2571 | rp.status = *status; |
| 2572 | |
| 2573 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_REMOVE_KEYS, &rp, |
| 2574 | sizeof(rp)); |
| 2575 | |
| 2576 | mgmt_pending_remove(cmd); |
| 2577 | } |
| 2578 | |
Johan Hedberg | afc747a | 2012-01-15 18:11:07 +0200 | [diff] [blame] | 2579 | int mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2580 | u8 link_type, u8 addr_type) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2581 | { |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2582 | struct mgmt_addr_info ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2583 | struct sock *sk = NULL; |
| 2584 | int err; |
| 2585 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2586 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2587 | |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2588 | bacpy(&ev.bdaddr, bdaddr); |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2589 | ev.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2590 | |
Johan Hedberg | afc747a | 2012-01-15 18:11:07 +0200 | [diff] [blame] | 2591 | err = mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), |
| 2592 | sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2593 | |
| 2594 | if (sk) |
| 2595 | sock_put(sk); |
| 2596 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2597 | mgmt_pending_foreach(MGMT_OP_REMOVE_KEYS, hdev, remove_keys_rsp, NULL); |
| 2598 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2599 | return err; |
| 2600 | } |
| 2601 | |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 2602 | int mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2603 | { |
| 2604 | struct pending_cmd *cmd; |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2605 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2606 | int err; |
| 2607 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2608 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2609 | if (!cmd) |
| 2610 | return -ENOENT; |
| 2611 | |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 2612 | if (bdaddr) { |
| 2613 | struct mgmt_rp_disconnect rp; |
| 2614 | |
| 2615 | bacpy(&rp.bdaddr, bdaddr); |
| 2616 | rp.status = status; |
| 2617 | |
| 2618 | err = cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 2619 | &rp, sizeof(rp)); |
| 2620 | } else |
| 2621 | err = cmd_status(cmd->sk, hdev->id, MGMT_OP_DISCONNECT, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2622 | mgmt_err); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2623 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2624 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2625 | |
| 2626 | return err; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2627 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2628 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2629 | int mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 2630 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2631 | { |
| 2632 | struct mgmt_ev_connect_failed ev; |
| 2633 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2634 | bacpy(&ev.addr.bdaddr, bdaddr); |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2635 | ev.addr.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2636 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2637 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2638 | return mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2639 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2640 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2641 | int mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2642 | { |
| 2643 | struct mgmt_ev_pin_code_request ev; |
| 2644 | |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2645 | bacpy(&ev.bdaddr, bdaddr); |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 2646 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2647 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2648 | return mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2649 | NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2650 | } |
| 2651 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2652 | int mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2653 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2654 | { |
| 2655 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2656 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2657 | int err; |
| 2658 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2659 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2660 | if (!cmd) |
| 2661 | return -ENOENT; |
| 2662 | |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2663 | bacpy(&rp.bdaddr, bdaddr); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2664 | rp.status = mgmt_status(status); |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2665 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2666 | err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, &rp, |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2667 | sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2668 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2669 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2670 | |
| 2671 | return err; |
| 2672 | } |
| 2673 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2674 | int mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2675 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2676 | { |
| 2677 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2678 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2679 | int err; |
| 2680 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2681 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2682 | if (!cmd) |
| 2683 | return -ENOENT; |
| 2684 | |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2685 | bacpy(&rp.bdaddr, bdaddr); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2686 | rp.status = mgmt_status(status); |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2687 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2688 | err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, &rp, |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2689 | sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2690 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2691 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2692 | |
| 2693 | return err; |
| 2694 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2695 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2696 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2697 | __le32 value, u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2698 | { |
| 2699 | struct mgmt_ev_user_confirm_request ev; |
| 2700 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2701 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2702 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2703 | bacpy(&ev.bdaddr, bdaddr); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 2704 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2705 | put_unaligned_le32(value, &ev.value); |
| 2706 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2707 | return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev), |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2708 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2709 | } |
| 2710 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2711 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 2712 | { |
| 2713 | struct mgmt_ev_user_passkey_request ev; |
| 2714 | |
| 2715 | BT_DBG("%s", hdev->name); |
| 2716 | |
| 2717 | bacpy(&ev.bdaddr, bdaddr); |
| 2718 | |
| 2719 | return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev), |
| 2720 | NULL); |
| 2721 | } |
| 2722 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2723 | static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2724 | u8 status, u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2725 | { |
| 2726 | struct pending_cmd *cmd; |
| 2727 | struct mgmt_rp_user_confirm_reply rp; |
| 2728 | int err; |
| 2729 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2730 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2731 | if (!cmd) |
| 2732 | return -ENOENT; |
| 2733 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2734 | bacpy(&rp.bdaddr, bdaddr); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2735 | rp.status = mgmt_status(status); |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2736 | err = cmd_complete(cmd->sk, hdev->id, opcode, &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2737 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2738 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2739 | |
| 2740 | return err; |
| 2741 | } |
| 2742 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2743 | int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2744 | u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2745 | { |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2746 | return user_pairing_resp_complete(hdev, bdaddr, status, |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2747 | MGMT_OP_USER_CONFIRM_REPLY); |
| 2748 | } |
| 2749 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2750 | int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, |
| 2751 | bdaddr_t *bdaddr, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2752 | { |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2753 | return user_pairing_resp_complete(hdev, bdaddr, status, |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2754 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
| 2755 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 2756 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2757 | int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2758 | u8 status) |
| 2759 | { |
| 2760 | return user_pairing_resp_complete(hdev, bdaddr, status, |
| 2761 | MGMT_OP_USER_PASSKEY_REPLY); |
| 2762 | } |
| 2763 | |
| 2764 | int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, |
| 2765 | bdaddr_t *bdaddr, u8 status) |
| 2766 | { |
| 2767 | return user_pairing_resp_complete(hdev, bdaddr, status, |
| 2768 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
| 2769 | } |
| 2770 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2771 | int mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 2772 | { |
| 2773 | struct mgmt_ev_auth_failed ev; |
| 2774 | |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 2775 | bacpy(&ev.bdaddr, bdaddr); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2776 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 2777 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2778 | return mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 2779 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2780 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2781 | int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2782 | { |
| 2783 | struct pending_cmd *cmd; |
| 2784 | struct mgmt_cp_set_local_name ev; |
| 2785 | int err; |
| 2786 | |
| 2787 | memset(&ev, 0, sizeof(ev)); |
| 2788 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
| 2789 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2790 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2791 | if (!cmd) |
| 2792 | goto send_event; |
| 2793 | |
| 2794 | if (status) { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2795 | err = cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2796 | mgmt_status(status)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2797 | goto failed; |
| 2798 | } |
| 2799 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2800 | update_eir(hdev); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2801 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2802 | err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, &ev, |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2803 | sizeof(ev)); |
| 2804 | if (err < 0) |
| 2805 | goto failed; |
| 2806 | |
| 2807 | send_event: |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2808 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2809 | cmd ? cmd->sk : NULL); |
| 2810 | |
| 2811 | failed: |
| 2812 | if (cmd) |
| 2813 | mgmt_pending_remove(cmd); |
| 2814 | return err; |
| 2815 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2816 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2817 | int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash, |
| 2818 | u8 *randomizer, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2819 | { |
| 2820 | struct pending_cmd *cmd; |
| 2821 | int err; |
| 2822 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2823 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2824 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2825 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2826 | if (!cmd) |
| 2827 | return -ENOENT; |
| 2828 | |
| 2829 | if (status) { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2830 | err = cmd_status(cmd->sk, hdev->id, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2831 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 2832 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2833 | } else { |
| 2834 | struct mgmt_rp_read_local_oob_data rp; |
| 2835 | |
| 2836 | memcpy(rp.hash, hash, sizeof(rp.hash)); |
| 2837 | memcpy(rp.randomizer, randomizer, sizeof(rp.randomizer)); |
| 2838 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2839 | err = cmd_complete(cmd->sk, hdev->id, |
| 2840 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 2841 | &rp, sizeof(rp)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2842 | } |
| 2843 | |
| 2844 | mgmt_pending_remove(cmd); |
| 2845 | |
| 2846 | return err; |
| 2847 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2848 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2849 | int mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2850 | u8 addr_type, u8 *dev_class, s8 rssi, |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 2851 | u8 cfm_name, u8 *eir, u16 eir_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2852 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 2853 | char buf[512]; |
| 2854 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 2855 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2856 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 2857 | /* Leave 5 bytes for a potential CoD field */ |
| 2858 | if (sizeof(*ev) + eir_len + 5 > sizeof(buf)) |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 2859 | return -EINVAL; |
| 2860 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 2861 | memset(buf, 0, sizeof(buf)); |
| 2862 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 2863 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 2864 | ev->addr.type = link_to_mgmt(link_type, addr_type); |
| 2865 | ev->rssi = rssi; |
| 2866 | ev->confirm_name = cfm_name; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2867 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 2868 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 2869 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2870 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 2871 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 2872 | eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
| 2873 | dev_class, 3); |
| 2874 | |
| 2875 | put_unaligned_le16(eir_len, &ev->eir_len); |
| 2876 | |
| 2877 | ev_size = sizeof(*ev) + eir_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 2878 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 2879 | return mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2880 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 2881 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2882 | int mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 2883 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 2884 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2885 | struct mgmt_ev_device_found *ev; |
| 2886 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 2887 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 2888 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2889 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 2890 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2891 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 2892 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2893 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 2894 | ev->addr.type = link_to_mgmt(link_type, addr_type); |
| 2895 | ev->rssi = rssi; |
| 2896 | |
| 2897 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name, |
| 2898 | name_len); |
| 2899 | |
| 2900 | put_unaligned_le16(eir_len, &ev->eir_len); |
| 2901 | |
| 2902 | return mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 2903 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 2904 | |
Andre Guedes | 7a13510 | 2011-11-09 17:14:25 -0300 | [diff] [blame] | 2905 | int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2906 | { |
| 2907 | struct pending_cmd *cmd; |
| 2908 | int err; |
| 2909 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2910 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2911 | if (!cmd) |
| 2912 | return -ENOENT; |
| 2913 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2914 | err = cmd_status(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2915 | mgmt_pending_remove(cmd); |
| 2916 | |
| 2917 | return err; |
| 2918 | } |
| 2919 | |
Andre Guedes | e6d465c | 2011-11-09 17:14:26 -0300 | [diff] [blame] | 2920 | int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 2921 | { |
| 2922 | struct pending_cmd *cmd; |
| 2923 | int err; |
| 2924 | |
| 2925 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 2926 | if (!cmd) |
| 2927 | return -ENOENT; |
| 2928 | |
Andre Guedes | e75a8b0c | 2012-01-02 16:50:53 -0300 | [diff] [blame] | 2929 | err = cmd_status(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2930 | mgmt_pending_remove(cmd); |
| 2931 | |
| 2932 | return err; |
| 2933 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 2934 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2935 | int mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 2936 | { |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2937 | struct pending_cmd *cmd; |
| 2938 | |
| 2939 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2940 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2941 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2942 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2943 | |
| 2944 | if (cmd != NULL) { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2945 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, NULL, 0); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2946 | mgmt_pending_remove(cmd); |
| 2947 | } |
| 2948 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2949 | return mgmt_event(MGMT_EV_DISCOVERING, hdev, &discovering, |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 2950 | sizeof(discovering), NULL); |
| 2951 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2952 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2953 | int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2954 | { |
| 2955 | struct pending_cmd *cmd; |
| 2956 | struct mgmt_ev_device_blocked ev; |
| 2957 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2958 | cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2959 | |
| 2960 | bacpy(&ev.bdaddr, bdaddr); |
| 2961 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2962 | return mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &ev, sizeof(ev), |
| 2963 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2964 | } |
| 2965 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2966 | int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2967 | { |
| 2968 | struct pending_cmd *cmd; |
| 2969 | struct mgmt_ev_device_unblocked ev; |
| 2970 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2971 | cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2972 | |
| 2973 | bacpy(&ev.bdaddr, bdaddr); |
| 2974 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2975 | return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &ev, sizeof(ev), |
| 2976 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2977 | } |