YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | BlueZ - Bluetooth protocol stack for Linux |
Hemant Gupta | 6bca5cd | 2013-06-08 19:07:57 +0530 | [diff] [blame] | 3 | Copyright (c) 2000-2001, The Linux Foundation. All rights reserved. |
| 4 | Copyright (c) 2010-2013, The Linux Foundation. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | |
| 6 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License version 2 as |
| 10 | published by the Free Software Foundation; |
| 11 | |
| 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 13 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 15 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 16 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 17 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 18 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 20 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 21 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 22 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | SOFTWARE IS DISCLAIMED. |
| 24 | */ |
| 25 | |
| 26 | /* Bluetooth HCI connection handling. */ |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | |
| 30 | #include <linux/types.h> |
| 31 | #include <linux/errno.h> |
| 32 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/slab.h> |
| 34 | #include <linux/poll.h> |
| 35 | #include <linux/fcntl.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/skbuff.h> |
| 38 | #include <linux/interrupt.h> |
| 39 | #include <linux/notifier.h> |
| 40 | #include <net/sock.h> |
| 41 | |
| 42 | #include <asm/system.h> |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 43 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <asm/unaligned.h> |
| 45 | |
| 46 | #include <net/bluetooth/bluetooth.h> |
| 47 | #include <net/bluetooth/hci_core.h> |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 48 | #include <net/bluetooth/l2cap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 50 | struct hci_conn *hci_le_connect(struct hci_dev *hdev, __u16 pkt_type, |
| 51 | bdaddr_t *dst, __u8 sec_level, __u8 auth_type, |
| 52 | struct bt_le_params *le_params) |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 53 | { |
Sunny Kapdi | 93bef89 | 2012-07-30 14:52:56 -0700 | [diff] [blame] | 54 | struct hci_conn *le, *le_wlist_conn; |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 55 | struct hci_cp_le_create_conn cp; |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 56 | struct adv_entry *entry; |
| 57 | struct link_key *key; |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 58 | |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 59 | BT_DBG("%p", hdev); |
Brian Gix | 114f3a6 | 2011-09-27 14:02:20 -0700 | [diff] [blame] | 60 | |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 61 | le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst); |
| 62 | if (le) { |
Sunny Kapdi | 93bef89 | 2012-07-30 14:52:56 -0700 | [diff] [blame] | 63 | le_wlist_conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
| 64 | BDADDR_ANY); |
| 65 | if (!le_wlist_conn) { |
| 66 | hci_conn_hold(le); |
| 67 | return le; |
| 68 | } else { |
| 69 | BT_DBG("remove wlist conn"); |
| 70 | le->out = 1; |
| 71 | le->link_mode |= HCI_LM_MASTER; |
| 72 | le->sec_level = BT_SECURITY_LOW; |
| 73 | le->type = LE_LINK; |
| 74 | hci_proto_connect_cfm(le, 0); |
| 75 | hci_conn_del(le_wlist_conn); |
| 76 | return le; |
| 77 | } |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | key = hci_find_link_key_type(hdev, dst, KEY_TYPE_LTK); |
| 81 | if (!key) { |
| 82 | entry = hci_find_adv_entry(hdev, dst); |
| 83 | if (entry) |
| 84 | le = hci_le_conn_add(hdev, dst, |
| 85 | entry->bdaddr_type); |
| 86 | else |
| 87 | le = hci_le_conn_add(hdev, dst, 0); |
| 88 | } else { |
| 89 | le = hci_le_conn_add(hdev, dst, key->addr_type); |
| 90 | } |
| 91 | |
| 92 | if (!le) |
| 93 | return ERR_PTR(-ENOMEM); |
| 94 | |
| 95 | hci_conn_hold(le); |
| 96 | |
| 97 | le->state = BT_CONNECT; |
| 98 | le->out = 1; |
| 99 | le->link_mode |= HCI_LM_MASTER; |
| 100 | le->sec_level = BT_SECURITY_LOW; |
| 101 | le->type = LE_LINK; |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 102 | |
| 103 | memset(&cp, 0, sizeof(cp)); |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 104 | if (l2cap_sock_le_params_valid(le_params)) { |
| 105 | cp.supervision_timeout = |
| 106 | cpu_to_le16(le_params->supervision_timeout); |
| 107 | cp.scan_interval = cpu_to_le16(le_params->scan_interval); |
| 108 | cp.scan_window = cpu_to_le16(le_params->scan_window); |
| 109 | cp.conn_interval_min = cpu_to_le16(le_params->interval_min); |
| 110 | cp.conn_interval_max = cpu_to_le16(le_params->interval_max); |
| 111 | cp.conn_latency = cpu_to_le16(le_params->latency); |
| 112 | cp.min_ce_len = cpu_to_le16(le_params->min_ce_len); |
| 113 | cp.max_ce_len = cpu_to_le16(le_params->max_ce_len); |
| 114 | le->conn_timeout = le_params->conn_timeout; |
| 115 | } else { |
| 116 | cp.supervision_timeout = cpu_to_le16(BT_LE_SUP_TO_DEFAULT); |
| 117 | cp.scan_interval = cpu_to_le16(BT_LE_SCAN_INTERVAL_DEF); |
| 118 | cp.scan_window = cpu_to_le16(BT_LE_SCAN_WINDOW_DEF); |
| 119 | cp.conn_interval_min = cpu_to_le16(BT_LE_CONN_INTERVAL_MIN_DEF); |
| 120 | cp.conn_interval_max = cpu_to_le16(BT_LE_CONN_INTERVAL_MAX_DEF); |
| 121 | cp.conn_latency = cpu_to_le16(BT_LE_LATENCY_DEF); |
| 122 | le->conn_timeout = 5; |
| 123 | } |
Sunny Kapdi | 93bef89 | 2012-07-30 14:52:56 -0700 | [diff] [blame] | 124 | if (!bacmp(&le->dst, BDADDR_ANY)) { |
| 125 | cp.filter_policy = 0x01; |
| 126 | le->conn_timeout = 0; |
| 127 | } else { |
| 128 | bacpy(&cp.peer_addr, &le->dst); |
| 129 | cp.peer_addr_type = le->dst_type; |
| 130 | } |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 131 | |
| 132 | hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp); |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 133 | |
| 134 | return le; |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 135 | } |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 136 | EXPORT_SYMBOL(hci_le_connect); |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 137 | |
| 138 | static void hci_le_connect_cancel(struct hci_conn *conn) |
| 139 | { |
| 140 | hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL); |
| 141 | } |
| 142 | |
Sunny Kapdi | 93bef89 | 2012-07-30 14:52:56 -0700 | [diff] [blame] | 143 | void hci_le_cancel_create_connect(struct hci_dev *hdev, bdaddr_t *dst) |
| 144 | { |
| 145 | struct hci_conn *le; |
| 146 | |
| 147 | BT_DBG("%p", hdev); |
| 148 | |
| 149 | le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst); |
| 150 | if (le) { |
| 151 | BT_DBG("send hci connect cancel"); |
| 152 | hci_le_connect_cancel(le); |
| 153 | hci_conn_del(le); |
| 154 | } |
| 155 | } |
| 156 | EXPORT_SYMBOL(hci_le_cancel_create_connect); |
| 157 | |
| 158 | void hci_le_add_dev_white_list(struct hci_dev *hdev, bdaddr_t *dst) |
| 159 | { |
| 160 | struct hci_cp_le_add_dev_white_list cp; |
| 161 | struct adv_entry *entry; |
| 162 | struct link_key *key; |
| 163 | |
| 164 | BT_DBG("%p", hdev); |
| 165 | |
| 166 | memset(&cp, 0, sizeof(cp)); |
| 167 | bacpy(&cp.addr, dst); |
| 168 | |
| 169 | key = hci_find_link_key_type(hdev, dst, KEY_TYPE_LTK); |
| 170 | if (!key) { |
| 171 | entry = hci_find_adv_entry(hdev, dst); |
| 172 | if (entry) |
| 173 | cp.addr_type = entry->bdaddr_type; |
| 174 | else |
| 175 | cp.addr_type = 0x00; |
| 176 | } else { |
| 177 | cp.addr_type = key->addr_type; |
| 178 | } |
| 179 | |
| 180 | hci_send_cmd(hdev, HCI_OP_LE_ADD_DEV_WHITE_LIST, sizeof(cp), &cp); |
| 181 | } |
| 182 | EXPORT_SYMBOL(hci_le_add_dev_white_list); |
| 183 | |
| 184 | void hci_le_remove_dev_white_list(struct hci_dev *hdev, bdaddr_t *dst) |
| 185 | { |
| 186 | struct hci_cp_le_remove_dev_white_list cp; |
| 187 | struct adv_entry *entry; |
| 188 | struct link_key *key; |
| 189 | |
| 190 | BT_DBG("%p", hdev); |
| 191 | |
| 192 | memset(&cp, 0, sizeof(cp)); |
| 193 | bacpy(&cp.addr, dst); |
| 194 | |
| 195 | key = hci_find_link_key_type(hdev, dst, KEY_TYPE_LTK); |
| 196 | if (!key) { |
| 197 | entry = hci_find_adv_entry(hdev, dst); |
| 198 | if (entry) |
| 199 | cp.addr_type = entry->bdaddr_type; |
| 200 | else |
| 201 | cp.addr_type = 0x00; |
| 202 | } else { |
| 203 | cp.addr_type = key->addr_type; |
| 204 | } |
| 205 | |
| 206 | hci_send_cmd(hdev, HCI_OP_LE_REMOVE_DEV_WHITE_LIST, sizeof(cp), &cp); |
| 207 | } |
| 208 | EXPORT_SYMBOL(hci_le_remove_dev_white_list); |
| 209 | |
Mallikarjuna GB | 0478d65 | 2012-08-22 14:18:26 +0530 | [diff] [blame] | 210 | static inline bool is_role_switch_possible(struct hci_dev *hdev) |
| 211 | { |
| 212 | if (hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECTED)) |
| 213 | return false; |
| 214 | return true; |
| 215 | } |
| 216 | |
Marcel Holtmann | 4c67bc7 | 2006-10-15 17:30:56 +0200 | [diff] [blame] | 217 | void hci_acl_connect(struct hci_conn *conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
| 219 | struct hci_dev *hdev = conn->hdev; |
| 220 | struct inquiry_entry *ie; |
| 221 | struct hci_cp_create_conn cp; |
| 222 | |
| 223 | BT_DBG("%p", conn); |
| 224 | |
| 225 | conn->state = BT_CONNECT; |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 226 | conn->out = 1; |
| 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | conn->link_mode = HCI_LM_MASTER; |
| 229 | |
Marcel Holtmann | 4c67bc7 | 2006-10-15 17:30:56 +0200 | [diff] [blame] | 230 | conn->attempt++; |
| 231 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 232 | conn->link_policy = hdev->link_policy; |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | memset(&cp, 0, sizeof(cp)); |
| 235 | bacpy(&cp.bdaddr, &conn->dst); |
| 236 | cp.pscan_rep_mode = 0x02; |
| 237 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 238 | ie = hci_inquiry_cache_lookup(hdev, &conn->dst); |
| 239 | if (ie) { |
Marcel Holtmann | 41a9621 | 2008-07-14 20:13:48 +0200 | [diff] [blame] | 240 | if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) { |
| 241 | cp.pscan_rep_mode = ie->data.pscan_rep_mode; |
| 242 | cp.pscan_mode = ie->data.pscan_mode; |
| 243 | cp.clock_offset = ie->data.clock_offset | |
| 244 | cpu_to_le16(0x8000); |
| 245 | } |
| 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | memcpy(conn->dev_class, ie->data.dev_class, 3); |
Marcel Holtmann | 41a9621 | 2008-07-14 20:13:48 +0200 | [diff] [blame] | 248 | conn->ssp_mode = ie->data.ssp_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 251 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
Mallikarjuna GB | 0478d65 | 2012-08-22 14:18:26 +0530 | [diff] [blame] | 252 | if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER) |
| 253 | && is_role_switch_possible(hdev)) |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 254 | cp.role_switch = 0x01; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | else |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 256 | cp.role_switch = 0x00; |
Marcel Holtmann | 4c67bc7 | 2006-10-15 17:30:56 +0200 | [diff] [blame] | 257 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 258 | hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 261 | static void hci_acl_connect_cancel(struct hci_conn *conn) |
| 262 | { |
| 263 | struct hci_cp_create_conn_cancel cp; |
| 264 | |
| 265 | BT_DBG("%p", conn); |
| 266 | |
| 267 | if (conn->hdev->hci_ver < 2) |
| 268 | return; |
| 269 | |
| 270 | bacpy(&cp.bdaddr, &conn->dst); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 271 | hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp); |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | void hci_acl_disconn(struct hci_conn *conn, __u8 reason) |
| 275 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | BT_DBG("%p", conn); |
| 277 | |
| 278 | conn->state = BT_DISCONN; |
| 279 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 280 | if (conn->hdev->dev_type == HCI_BREDR) { |
| 281 | struct hci_cp_disconnect cp; |
| 282 | cp.handle = cpu_to_le16(conn->handle); |
| 283 | cp.reason = reason; |
| 284 | hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp); |
| 285 | } else { |
| 286 | struct hci_cp_disconn_phys_link cp; |
| 287 | cp.phy_handle = (u8) conn->handle; |
| 288 | cp.reason = reason; |
| 289 | hci_send_cmd(conn->hdev, HCI_OP_DISCONN_PHYS_LINK, |
| 290 | sizeof(cp), &cp); |
| 291 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void hci_add_sco(struct hci_conn *conn, __u16 handle) |
| 295 | { |
| 296 | struct hci_dev *hdev = conn->hdev; |
| 297 | struct hci_cp_add_sco cp; |
| 298 | |
| 299 | BT_DBG("%p", conn); |
| 300 | |
| 301 | conn->state = BT_CONNECT; |
| 302 | conn->out = 1; |
| 303 | |
Marcel Holtmann | efc7688 | 2009-02-06 09:13:37 +0100 | [diff] [blame] | 304 | conn->attempt++; |
| 305 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 306 | cp.handle = cpu_to_le16(handle); |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 307 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 309 | hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 312 | void hci_setup_sync(struct hci_conn *conn, __u16 handle) |
| 313 | { |
| 314 | struct hci_dev *hdev = conn->hdev; |
| 315 | struct hci_cp_setup_sync_conn cp; |
| 316 | |
| 317 | BT_DBG("%p", conn); |
| 318 | |
| 319 | conn->state = BT_CONNECT; |
| 320 | conn->out = 1; |
| 321 | |
Marcel Holtmann | efc7688 | 2009-02-06 09:13:37 +0100 | [diff] [blame] | 322 | conn->attempt++; |
| 323 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 324 | cp.handle = cpu_to_le16(handle); |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 325 | |
| 326 | cp.tx_bandwidth = cpu_to_le32(0x00001f40); |
| 327 | cp.rx_bandwidth = cpu_to_le32(0x00001f40); |
Kun Han Kim | 15b911f | 2011-07-08 09:30:28 -0700 | [diff] [blame] | 328 | if (conn->hdev->is_wbs) { |
| 329 | /* Transparent Data */ |
| 330 | uint16_t voice_setting = hdev->voice_setting | ACF_TRANS; |
| 331 | cp.max_latency = cpu_to_le16(0x000D); |
| 332 | cp.pkt_type = cpu_to_le16(ESCO_WBS); |
| 333 | cp.voice_setting = cpu_to_le16(voice_setting); |
| 334 | /* Retransmission Effort */ |
| 335 | cp.retrans_effort = RE_LINK_QUALITY; |
| 336 | } else { |
| 337 | cp.max_latency = cpu_to_le16(0x000A); |
| 338 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
| 339 | cp.voice_setting = cpu_to_le16(hdev->voice_setting); |
| 340 | cp.retrans_effort = RE_POWER_CONSUMP; |
| 341 | } |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 342 | |
| 343 | hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp); |
| 344 | } |
| 345 | |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 346 | void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, |
| 347 | u16 latency, u16 to_multiplier) |
| 348 | { |
| 349 | struct hci_cp_le_conn_update cp; |
| 350 | struct hci_dev *hdev = conn->hdev; |
| 351 | |
| 352 | memset(&cp, 0, sizeof(cp)); |
| 353 | |
| 354 | cp.handle = cpu_to_le16(conn->handle); |
| 355 | cp.conn_interval_min = cpu_to_le16(min); |
| 356 | cp.conn_interval_max = cpu_to_le16(max); |
| 357 | cp.conn_latency = cpu_to_le16(latency); |
| 358 | cp.supervision_timeout = cpu_to_le16(to_multiplier); |
| 359 | cp.min_ce_len = cpu_to_le16(0x0001); |
| 360 | cp.max_ce_len = cpu_to_le16(0x0001); |
| 361 | |
| 362 | hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp); |
| 363 | } |
| 364 | EXPORT_SYMBOL(hci_le_conn_update); |
| 365 | |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 366 | void hci_read_rssi(struct hci_conn *conn) |
| 367 | { |
| 368 | struct hci_cp_read_rssi cp; |
| 369 | struct hci_dev *hdev = conn->hdev; |
| 370 | |
| 371 | memset(&cp, 0, sizeof(cp)); |
| 372 | cp.handle = cpu_to_le16(conn->handle); |
| 373 | |
| 374 | hci_send_cmd(hdev, HCI_OP_READ_RSSI, sizeof(cp), &cp); |
| 375 | } |
| 376 | EXPORT_SYMBOL(hci_read_rssi); |
| 377 | |
Vinicius Costa Gomes | 735038c | 2011-06-09 18:50:47 -0300 | [diff] [blame] | 378 | void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8], |
| 379 | __u8 ltk[16]) |
| 380 | { |
| 381 | struct hci_dev *hdev = conn->hdev; |
| 382 | struct hci_cp_le_start_enc cp; |
| 383 | |
| 384 | BT_DBG("%p", conn); |
| 385 | |
| 386 | memset(&cp, 0, sizeof(cp)); |
| 387 | |
| 388 | cp.handle = cpu_to_le16(conn->handle); |
| 389 | memcpy(cp.ltk, ltk, sizeof(cp.ltk)); |
| 390 | cp.ediv = ediv; |
Brian Gix | 842bc5e | 2011-09-07 08:13:41 -0700 | [diff] [blame] | 391 | memcpy(cp.rand, rand, sizeof(cp.rand)); |
Vinicius Costa Gomes | 735038c | 2011-06-09 18:50:47 -0300 | [diff] [blame] | 392 | |
| 393 | hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp); |
| 394 | } |
| 395 | EXPORT_SYMBOL(hci_le_start_enc); |
| 396 | |
| 397 | void hci_le_ltk_reply(struct hci_conn *conn, u8 ltk[16]) |
| 398 | { |
| 399 | struct hci_dev *hdev = conn->hdev; |
| 400 | struct hci_cp_le_ltk_reply cp; |
| 401 | |
| 402 | BT_DBG("%p", conn); |
| 403 | |
| 404 | memset(&cp, 0, sizeof(cp)); |
| 405 | |
| 406 | cp.handle = cpu_to_le16(conn->handle); |
| 407 | memcpy(cp.ltk, ltk, sizeof(ltk)); |
| 408 | |
| 409 | hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp); |
| 410 | } |
| 411 | EXPORT_SYMBOL(hci_le_ltk_reply); |
| 412 | |
| 413 | void hci_le_ltk_neg_reply(struct hci_conn *conn) |
| 414 | { |
| 415 | struct hci_dev *hdev = conn->hdev; |
| 416 | struct hci_cp_le_ltk_neg_reply cp; |
| 417 | |
| 418 | BT_DBG("%p", conn); |
| 419 | |
| 420 | memset(&cp, 0, sizeof(cp)); |
| 421 | |
| 422 | cp.handle = cpu_to_le16(conn->handle); |
| 423 | |
| 424 | hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(cp), &cp); |
| 425 | } |
| 426 | |
Marcel Holtmann | e73439d | 2010-07-26 10:06:00 -0400 | [diff] [blame] | 427 | /* Device _must_ be locked */ |
| 428 | void hci_sco_setup(struct hci_conn *conn, __u8 status) |
| 429 | { |
| 430 | struct hci_conn *sco = conn->link; |
| 431 | |
| 432 | BT_DBG("%p", conn); |
| 433 | |
| 434 | if (!sco) |
| 435 | return; |
| 436 | |
| 437 | if (!status) { |
| 438 | if (lmp_esco_capable(conn->hdev)) |
| 439 | hci_setup_sync(sco, conn->handle); |
| 440 | else |
| 441 | hci_add_sco(sco, conn->handle); |
| 442 | } else { |
| 443 | hci_proto_connect_cfm(sco, status); |
| 444 | hci_conn_del(sco); |
| 445 | } |
| 446 | } |
| 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | static void hci_conn_timeout(unsigned long arg) |
| 449 | { |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 450 | struct hci_conn *conn = (void *) arg; |
| 451 | struct hci_dev *hdev = conn->hdev; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 452 | __u8 reason; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
| 454 | BT_DBG("conn %p state %d", conn, conn->state); |
| 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | hci_dev_lock(hdev); |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 457 | |
| 458 | switch (conn->state) { |
| 459 | case BT_CONNECT: |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 460 | case BT_CONNECT2: |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 461 | if (conn->out) { |
| 462 | if (conn->type == ACL_LINK) |
| 463 | hci_acl_connect_cancel(conn); |
| 464 | else if (conn->type == LE_LINK) |
| 465 | hci_le_connect_cancel(conn); |
| 466 | } |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 467 | break; |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 468 | case BT_CONFIG: |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 469 | case BT_CONNECTED: |
Brian Gix | 114f3a6 | 2011-09-27 14:02:20 -0700 | [diff] [blame] | 470 | if (!atomic_read(&conn->refcnt)) { |
| 471 | reason = hci_proto_disconn_ind(conn); |
| 472 | hci_acl_disconn(conn, reason); |
| 473 | } |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 474 | break; |
| 475 | default: |
Brian Gix | 114f3a6 | 2011-09-27 14:02:20 -0700 | [diff] [blame] | 476 | if (!atomic_read(&conn->refcnt)) |
| 477 | conn->state = BT_CLOSED; |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 478 | break; |
| 479 | } |
| 480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 484 | static void hci_conn_idle(unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 486 | struct hci_conn *conn = (void *) arg; |
| 487 | |
| 488 | BT_DBG("conn %p mode %d", conn, conn->mode); |
| 489 | |
| 490 | hci_conn_enter_sniff_mode(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 493 | static void hci_conn_rssi_update(struct work_struct *work) |
| 494 | { |
| 495 | struct delayed_work *delayed = |
| 496 | container_of(work, struct delayed_work, work); |
| 497 | struct hci_conn *conn = |
| 498 | container_of(delayed, struct hci_conn, rssi_update_work); |
| 499 | |
| 500 | BT_DBG("conn %p mode %d", conn, conn->mode); |
| 501 | |
| 502 | hci_read_rssi(conn); |
| 503 | } |
| 504 | |
Prabhakaran Mc | afface8 | 2012-04-10 11:38:35 +0530 | [diff] [blame] | 505 | static void encryption_disabled_timeout(unsigned long userdata) |
| 506 | { |
| 507 | struct hci_conn *conn = (struct hci_conn *)userdata; |
| 508 | BT_INFO("conn %p Grace Prd Exp ", conn); |
| 509 | |
| 510 | hci_encrypt_cfm(conn, 0, 0); |
| 511 | |
| 512 | if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) { |
| 513 | struct hci_cp_set_conn_encrypt cp; |
| 514 | BT_INFO("HCI_CONN_ENCRYPT_PEND is set"); |
| 515 | cp.handle = cpu_to_le16(conn->handle); |
| 516 | cp.encrypt = 1; |
| 517 | hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, |
| 518 | sizeof(cp), &cp); |
| 519 | } |
| 520 | |
| 521 | } |
| 522 | |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 523 | struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, |
| 524 | __u16 pkt_type, bdaddr_t *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | { |
| 526 | struct hci_conn *conn; |
| 527 | |
| 528 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); |
| 529 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 530 | conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC); |
| 531 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | |
| 534 | bacpy(&conn->dst, dst); |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 535 | conn->hdev = hdev; |
| 536 | conn->type = type; |
| 537 | conn->mode = HCI_CM_ACTIVE; |
| 538 | conn->state = BT_OPEN; |
Andrei Emeltchenko | 93f19c9 | 2009-09-03 12:34:19 +0300 | [diff] [blame] | 539 | conn->auth_type = HCI_AT_GENERAL_BONDING; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 540 | conn->io_capability = hdev->io_capability; |
Johan Hedberg | a958355 | 2011-02-19 12:06:01 -0300 | [diff] [blame] | 541 | conn->remote_auth = 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 543 | conn->power_save = 1; |
Marcel Holtmann | 052b30b | 2009-04-26 20:01:22 +0200 | [diff] [blame] | 544 | conn->disc_timeout = HCI_DISCONN_TIMEOUT; |
Srinivas Krovvidi | 6aadc41 | 2012-09-03 18:47:50 +0530 | [diff] [blame] | 545 | conn->conn_valid = true; |
| 546 | spin_lock_init(&conn->lock); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 547 | |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 548 | switch (type) { |
| 549 | case ACL_LINK: |
| 550 | conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; |
Rahul Kashyap | d5553ba | 2012-01-02 19:27:19 +0530 | [diff] [blame] | 551 | conn->link_policy = hdev->link_policy; |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 552 | break; |
| 553 | case SCO_LINK: |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 554 | if (!pkt_type) |
| 555 | pkt_type = SCO_ESCO_MASK; |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 556 | case ESCO_LINK: |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 557 | if (!pkt_type) |
| 558 | pkt_type = ALL_ESCO_MASK; |
| 559 | if (lmp_esco_capable(hdev)) { |
| 560 | /* HCI Setup Synchronous Connection Command uses |
| 561 | reverse logic on the EDR_ESCO_MASK bits */ |
| 562 | conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) & |
| 563 | hdev->esco_type; |
| 564 | } else { |
| 565 | /* Legacy HCI Add Sco Connection Command uses a |
| 566 | shifted bitmask */ |
| 567 | conn->pkt_type = (pkt_type << 5) & hdev->pkt_type & |
| 568 | SCO_PTYPE_MASK; |
| 569 | } |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 570 | break; |
| 571 | } |
| 572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | skb_queue_head_init(&conn->data_q); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 574 | |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 575 | setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn); |
| 576 | setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn); |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 577 | INIT_DELAYED_WORK(&conn->rssi_update_work, hci_conn_rssi_update); |
Prabhakaran Mc | afface8 | 2012-04-10 11:38:35 +0530 | [diff] [blame] | 578 | setup_timer(&conn->encrypt_pause_timer, encryption_disabled_timeout, |
| 579 | (unsigned long)conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
| 581 | atomic_set(&conn->refcnt, 0); |
| 582 | |
| 583 | hci_dev_hold(hdev); |
| 584 | |
| 585 | tasklet_disable(&hdev->tx_task); |
| 586 | |
| 587 | hci_conn_hash_add(hdev, conn); |
| 588 | if (hdev->notify) |
| 589 | hdev->notify(hdev, HCI_NOTIFY_CONN_ADD); |
| 590 | |
Marcel Holtmann | 9eba32b | 2009-08-22 14:19:26 -0700 | [diff] [blame] | 591 | atomic_set(&conn->devref, 0); |
| 592 | |
Marcel Holtmann | a67e899 | 2009-05-02 18:24:06 -0700 | [diff] [blame] | 593 | hci_conn_init_sysfs(conn); |
| 594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | tasklet_enable(&hdev->tx_task); |
| 596 | |
| 597 | return conn; |
| 598 | } |
| 599 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 600 | struct hci_conn *hci_le_conn_add(struct hci_dev *hdev, bdaddr_t *dst, |
| 601 | __u8 addr_type) |
| 602 | { |
| 603 | struct hci_conn *conn = hci_conn_add(hdev, LE_LINK, 0, dst); |
| 604 | if (!conn) |
| 605 | return NULL; |
| 606 | |
| 607 | conn->dst_type = addr_type; |
| 608 | |
| 609 | return conn; |
| 610 | } |
| 611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | int hci_conn_del(struct hci_conn *conn) |
| 613 | { |
| 614 | struct hci_dev *hdev = conn->hdev; |
| 615 | |
| 616 | BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle); |
| 617 | |
Srinivas Krovvidi | 6aadc41 | 2012-09-03 18:47:50 +0530 | [diff] [blame] | 618 | spin_lock_bh(&conn->lock); |
| 619 | conn->conn_valid = false; /* conn data is being released */ |
| 620 | spin_unlock_bh(&conn->lock); |
| 621 | |
Brian Gix | 3cd6204 | 2012-01-11 15:18:17 -0800 | [diff] [blame] | 622 | /* Make sure no timers are running */ |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 623 | del_timer(&conn->idle_timer); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 624 | del_timer(&conn->disc_timer); |
Brian Gix | 3cd6204 | 2012-01-11 15:18:17 -0800 | [diff] [blame] | 625 | del_timer(&conn->smp_timer); |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 626 | __cancel_delayed_work(&conn->rssi_update_work); |
Prabhakaran Mc | afface8 | 2012-04-10 11:38:35 +0530 | [diff] [blame] | 627 | del_timer(&conn->encrypt_pause_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 629 | if (conn->type == ACL_LINK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | struct hci_conn *sco = conn->link; |
| 631 | if (sco) |
| 632 | sco->link = NULL; |
| 633 | |
| 634 | /* Unacked frames */ |
| 635 | hdev->acl_cnt += conn->sent; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 636 | } else if (conn->type == LE_LINK) { |
| 637 | if (hdev->le_pkts) |
| 638 | hdev->le_cnt += conn->sent; |
| 639 | else |
| 640 | hdev->acl_cnt += conn->sent; |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 641 | } else { |
| 642 | struct hci_conn *acl = conn->link; |
| 643 | if (acl) { |
| 644 | acl->link = NULL; |
| 645 | hci_conn_put(acl); |
| 646 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | tasklet_disable(&hdev->tx_task); |
Marcel Holtmann | 7d0db0a | 2008-07-14 20:13:51 +0200 | [diff] [blame] | 650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | hci_conn_hash_del(hdev, conn); |
| 652 | if (hdev->notify) |
| 653 | hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); |
Marcel Holtmann | 7d0db0a | 2008-07-14 20:13:51 +0200 | [diff] [blame] | 654 | |
AnubhavGupta | 01d9e36 | 2011-12-17 17:14:51 +0530 | [diff] [blame] | 655 | tasklet_schedule(&hdev->tx_task); |
| 656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | tasklet_enable(&hdev->tx_task); |
Marcel Holtmann | 7d0db0a | 2008-07-14 20:13:51 +0200 | [diff] [blame] | 658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | skb_queue_purge(&conn->data_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
Marcel Holtmann | 9eba32b | 2009-08-22 14:19:26 -0700 | [diff] [blame] | 661 | hci_conn_put_device(conn); |
Dave Young | 2ae9a6b | 2009-02-21 16:13:34 +0800 | [diff] [blame] | 662 | |
Hemant Gupta | 6bca5cd | 2013-06-08 19:07:57 +0530 | [diff] [blame] | 663 | if (conn->hidp_session_valid) |
| 664 | hci_conn_put_device(conn); |
| 665 | |
Marcel Holtmann | 384943e | 2009-05-08 18:20:43 -0700 | [diff] [blame] | 666 | hci_dev_put(hdev); |
| 667 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | struct hci_chan *hci_chan_add(struct hci_dev *hdev) |
| 672 | { |
| 673 | struct hci_chan *chan; |
| 674 | |
| 675 | BT_DBG("%s", hdev->name); |
| 676 | |
| 677 | chan = kzalloc(sizeof(struct hci_chan), GFP_ATOMIC); |
| 678 | if (!chan) |
| 679 | return NULL; |
| 680 | |
| 681 | atomic_set(&chan->refcnt, 0); |
| 682 | |
| 683 | hci_dev_hold(hdev); |
| 684 | |
| 685 | chan->hdev = hdev; |
| 686 | |
| 687 | list_add(&chan->list, &hdev->chan_list.list); |
| 688 | |
| 689 | return chan; |
| 690 | } |
Peter Krystad | a8417e6 | 2012-03-21 16:58:17 -0700 | [diff] [blame] | 691 | EXPORT_SYMBOL(hci_chan_add); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 692 | |
| 693 | int hci_chan_del(struct hci_chan *chan) |
| 694 | { |
| 695 | BT_DBG("%s chan %p", chan->hdev->name, chan); |
| 696 | |
| 697 | list_del(&chan->list); |
| 698 | |
| 699 | hci_conn_put(chan->conn); |
| 700 | hci_dev_put(chan->hdev); |
| 701 | |
| 702 | kfree(chan); |
Tomas Targownik | 1be668d | 2011-06-30 16:30:44 -0300 | [diff] [blame] | 703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | return 0; |
| 705 | } |
| 706 | |
Peter Krystad | d6a9ceb | 2011-12-01 15:44:54 -0800 | [diff] [blame] | 707 | int hci_chan_put(struct hci_chan *chan) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 708 | { |
| 709 | struct hci_cp_disconn_logical_link cp; |
Mat Martineau | 9f8d467 | 2011-12-14 12:10:46 -0800 | [diff] [blame] | 710 | struct hci_conn *hcon; |
| 711 | u16 ll_handle; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 712 | |
| 713 | BT_DBG("chan %p refcnt %d", chan, atomic_read(&chan->refcnt)); |
| 714 | if (!atomic_dec_and_test(&chan->refcnt)) |
Peter Krystad | d6a9ceb | 2011-12-01 15:44:54 -0800 | [diff] [blame] | 715 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 716 | |
Mat Martineau | 9f8d467 | 2011-12-14 12:10:46 -0800 | [diff] [blame] | 717 | hcon = chan->conn; |
| 718 | ll_handle = chan->ll_handle; |
| 719 | |
| 720 | hci_chan_del(chan); |
| 721 | |
| 722 | BT_DBG("chan->conn->state %d", hcon->state); |
| 723 | if (hcon->state == BT_CONNECTED) { |
| 724 | cp.log_handle = cpu_to_le16(ll_handle); |
| 725 | hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 726 | sizeof(cp), &cp); |
Mat Martineau | 9f8d467 | 2011-12-14 12:10:46 -0800 | [diff] [blame] | 727 | } |
Peter Krystad | d6a9ceb | 2011-12-01 15:44:54 -0800 | [diff] [blame] | 728 | |
| 729 | return 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 730 | } |
| 731 | EXPORT_SYMBOL(hci_chan_put); |
| 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src) |
| 734 | { |
| 735 | int use_src = bacmp(src, BDADDR_ANY); |
| 736 | struct hci_dev *hdev = NULL; |
| 737 | struct list_head *p; |
| 738 | |
| 739 | BT_DBG("%s -> %s", batostr(src), batostr(dst)); |
| 740 | |
| 741 | read_lock_bh(&hci_dev_list_lock); |
| 742 | |
| 743 | list_for_each(p, &hci_dev_list) { |
| 744 | struct hci_dev *d = list_entry(p, struct hci_dev, list); |
| 745 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 746 | if (d->dev_type != HCI_BREDR) |
| 747 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags)) |
| 749 | continue; |
| 750 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 751 | /* Simple routing: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | * No source address - find interface with bdaddr != dst |
| 753 | * Source address - find interface with bdaddr == src |
| 754 | */ |
| 755 | |
| 756 | if (use_src) { |
| 757 | if (!bacmp(&d->bdaddr, src)) { |
| 758 | hdev = d; break; |
| 759 | } |
| 760 | } else { |
| 761 | if (bacmp(&d->bdaddr, dst)) { |
| 762 | hdev = d; break; |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | if (hdev) |
| 768 | hdev = hci_dev_hold(hdev); |
| 769 | |
| 770 | read_unlock_bh(&hci_dev_list_lock); |
| 771 | return hdev; |
| 772 | } |
| 773 | EXPORT_SYMBOL(hci_get_route); |
| 774 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 775 | struct hci_dev *hci_dev_get_type(u8 amp_type) |
| 776 | { |
| 777 | struct hci_dev *hdev = NULL; |
| 778 | struct hci_dev *d; |
| 779 | |
| 780 | BT_DBG("amp_type %d", amp_type); |
| 781 | |
| 782 | read_lock_bh(&hci_dev_list_lock); |
| 783 | |
| 784 | list_for_each_entry(d, &hci_dev_list, list) { |
| 785 | if ((d->amp_type == amp_type) && test_bit(HCI_UP, &d->flags)) { |
| 786 | hdev = d; |
| 787 | break; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | if (hdev) |
| 792 | hdev = hci_dev_hold(hdev); |
| 793 | |
| 794 | read_unlock_bh(&hci_dev_list_lock); |
| 795 | return hdev; |
| 796 | } |
| 797 | EXPORT_SYMBOL(hci_dev_get_type); |
| 798 | |
| 799 | struct hci_dev *hci_dev_get_amp(bdaddr_t *dst) |
| 800 | { |
| 801 | struct hci_dev *d; |
| 802 | struct hci_dev *hdev = NULL; |
| 803 | |
| 804 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); |
| 805 | |
| 806 | read_lock_bh(&hci_dev_list_lock); |
| 807 | |
| 808 | list_for_each_entry(d, &hci_dev_list, list) { |
| 809 | struct hci_conn *conn; |
| 810 | if (d->dev_type == HCI_BREDR) |
| 811 | continue; |
| 812 | conn = hci_conn_hash_lookup_ba(d, ACL_LINK, dst); |
| 813 | if (conn) { |
| 814 | hdev = d; |
| 815 | break; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | if (hdev) |
| 820 | hdev = hci_dev_hold(hdev); |
| 821 | |
| 822 | read_unlock_bh(&hci_dev_list_lock); |
| 823 | return hdev; |
| 824 | } |
| 825 | EXPORT_SYMBOL(hci_dev_get_amp); |
| 826 | |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 827 | /* Create SCO, ACL or LE connection. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | * Device _must_ be locked */ |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 829 | struct hci_conn *hci_connect(struct hci_dev *hdev, int type, |
| 830 | __u16 pkt_type, bdaddr_t *dst, |
| 831 | __u8 sec_level, __u8 auth_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | { |
| 833 | struct hci_conn *acl; |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 834 | struct hci_conn *sco; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
| 836 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); |
| 837 | |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 838 | if (type == LE_LINK) |
| 839 | return hci_le_connect(hdev, pkt_type, dst, sec_level, |
| 840 | auth_type, NULL); |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 841 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 842 | acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); |
| 843 | if (!acl) { |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 844 | acl = hci_conn_add(hdev, ACL_LINK, 0, dst); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 845 | if (!acl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | return NULL; |
| 847 | } |
| 848 | |
| 849 | hci_conn_hold(acl); |
| 850 | |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 851 | if (acl->state == BT_OPEN || acl->state == BT_CLOSED) { |
Johan Hedberg | 765c2a9 | 2011-01-19 12:06:52 +0530 | [diff] [blame] | 852 | acl->sec_level = BT_SECURITY_LOW; |
| 853 | acl->pending_sec_level = sec_level; |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 854 | acl->auth_type = auth_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | hci_acl_connect(acl); |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 856 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 858 | if (type == ACL_LINK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | return acl; |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 860 | |
Srinivas Krovvidi | 645f4d4 | 2012-08-02 13:23:53 +0530 | [diff] [blame] | 861 | /* type of connection already existing can be ESCO or SCO |
| 862 | * so check for both types before creating new */ |
| 863 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 864 | sco = hci_conn_hash_lookup_ba(hdev, type, dst); |
Srinivas Krovvidi | 645f4d4 | 2012-08-02 13:23:53 +0530 | [diff] [blame] | 865 | |
| 866 | if (!sco && type == ESCO_LINK) { |
| 867 | sco = hci_conn_hash_lookup_ba(hdev, SCO_LINK, dst); |
| 868 | } else if (!sco && type == SCO_LINK) { |
| 869 | /* this case can be practically not possible */ |
| 870 | sco = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, dst); |
| 871 | } |
| 872 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 873 | if (!sco) { |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 874 | sco = hci_conn_add(hdev, type, pkt_type, dst); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 875 | if (!sco) { |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 876 | hci_conn_put(acl); |
| 877 | return NULL; |
| 878 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | } |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 880 | |
| 881 | acl->link = sco; |
| 882 | sco->link = acl; |
| 883 | |
| 884 | hci_conn_hold(sco); |
| 885 | |
| 886 | if (acl->state == BT_CONNECTED && |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 887 | (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { |
Nick Pelly | c390216 | 2009-11-13 14:16:32 -0800 | [diff] [blame] | 888 | acl->power_save = 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 889 | hci_conn_enter_active_mode(acl, 1); |
Nick Pelly | c390216 | 2009-11-13 14:16:32 -0800 | [diff] [blame] | 890 | |
Marcel Holtmann | e73439d | 2010-07-26 10:06:00 -0400 | [diff] [blame] | 891 | if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) { |
| 892 | /* defer SCO setup until mode change completed */ |
| 893 | set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend); |
| 894 | return sco; |
| 895 | } |
| 896 | |
| 897 | hci_sco_setup(acl, 0x00); |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 898 | } |
Marcel Holtmann | 5b7f9909 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 899 | |
| 900 | return sco; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
| 902 | EXPORT_SYMBOL(hci_connect); |
| 903 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 904 | void hci_disconnect(struct hci_conn *conn, __u8 reason) |
| 905 | { |
| 906 | BT_DBG("conn %p", conn); |
| 907 | |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 908 | hci_proto_disconn_cfm(conn, reason, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 909 | } |
| 910 | EXPORT_SYMBOL(hci_disconnect); |
| 911 | |
| 912 | void hci_disconnect_amp(struct hci_conn *conn, __u8 reason) |
| 913 | { |
| 914 | struct hci_dev *hdev = NULL; |
| 915 | |
| 916 | BT_DBG("conn %p", conn); |
| 917 | |
| 918 | read_lock_bh(&hci_dev_list_lock); |
| 919 | |
| 920 | list_for_each_entry(hdev, &hci_dev_list, list) { |
| 921 | struct hci_conn *c; |
| 922 | if (hdev == conn->hdev) |
| 923 | continue; |
| 924 | if (hdev->amp_type == HCI_BREDR) |
| 925 | continue; |
| 926 | c = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &conn->dst); |
| 927 | if (c) |
| 928 | hci_disconnect(c, reason); |
| 929 | } |
| 930 | |
| 931 | read_unlock_bh(&hci_dev_list_lock); |
| 932 | } |
| 933 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 934 | /* Check link security requirement */ |
| 935 | int hci_conn_check_link_mode(struct hci_conn *conn) |
| 936 | { |
| 937 | BT_DBG("conn %p", conn); |
| 938 | |
| 939 | if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 && |
| 940 | !(conn->link_mode & HCI_LM_ENCRYPT)) |
| 941 | return 0; |
| 942 | |
| 943 | return 1; |
| 944 | } |
| 945 | EXPORT_SYMBOL(hci_conn_check_link_mode); |
| 946 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | /* Authenticate remote device */ |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 948 | static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | { |
| 950 | BT_DBG("conn %p", conn); |
| 951 | |
Johan Hedberg | 765c2a9 | 2011-01-19 12:06:52 +0530 | [diff] [blame] | 952 | if (conn->pending_sec_level > sec_level) |
| 953 | sec_level = conn->pending_sec_level; |
| 954 | |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 955 | if (sec_level > conn->sec_level) |
Johan Hedberg | 765c2a9 | 2011-01-19 12:06:52 +0530 | [diff] [blame] | 956 | conn->pending_sec_level = sec_level; |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 957 | else if (conn->link_mode & HCI_LM_AUTH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | return 1; |
| 959 | |
Johan Hedberg | 65cf686 | 2011-01-19 12:06:49 +0530 | [diff] [blame] | 960 | /* Make sure we preserve an existing MITM requirement*/ |
| 961 | auth_type |= (conn->auth_type & 0x01); |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 962 | conn->auth_type = auth_type; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 963 | conn->auth_initiator = 1; |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 964 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) { |
| 966 | struct hci_cp_auth_requested cp; |
Peter Hurley | a5e5b08 | 2012-01-13 15:11:30 +0100 | [diff] [blame] | 967 | |
| 968 | /* encrypt must be pending if auth is also pending */ |
| 969 | set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend); |
| 970 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 971 | cp.handle = cpu_to_le16(conn->handle); |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 972 | hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, |
| 973 | sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | return 0; |
| 977 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 979 | /* Enable security */ |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 980 | int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 982 | BT_DBG("conn %p %d %d", conn, sec_level, auth_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 984 | if (sec_level == BT_SECURITY_SDP) |
| 985 | return 1; |
| 986 | |
Marcel Holtmann | 3fdca1e | 2009-04-28 09:04:55 -0700 | [diff] [blame] | 987 | if (sec_level == BT_SECURITY_LOW && |
| 988 | (!conn->ssp_mode || !conn->hdev->ssp_mode)) |
| 989 | return 1; |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 990 | |
Brian Gix | 2e2f50d | 2011-09-13 12:36:04 -0700 | [diff] [blame] | 991 | if (conn->type == LE_LINK) { |
| 992 | if (conn->pending_sec_level > sec_level) |
| 993 | sec_level = conn->pending_sec_level; |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 994 | |
Brian Gix | 2e2f50d | 2011-09-13 12:36:04 -0700 | [diff] [blame] | 995 | if (sec_level > conn->sec_level) |
| 996 | conn->pending_sec_level = sec_level; |
| 997 | hci_proto_connect_cfm(conn, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 998 | return 0; |
Brian Gix | 2e2f50d | 2011-09-13 12:36:04 -0700 | [diff] [blame] | 999 | } else if (conn->link_mode & HCI_LM_ENCRYPT) { |
| 1000 | return hci_conn_auth(conn, sec_level, auth_type); |
Ilia Kolomisnky | edc44dd | 2011-06-15 06:52:26 +0300 | [diff] [blame] | 1001 | } else if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) { |
Brian Gix | 2e2f50d | 2011-09-13 12:36:04 -0700 | [diff] [blame] | 1002 | return 0; |
| 1003 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1004 | |
| 1005 | if (hci_conn_auth(conn, sec_level, auth_type)) { |
| 1006 | struct hci_cp_set_conn_encrypt cp; |
Prabhakaran Mc | afface8 | 2012-04-10 11:38:35 +0530 | [diff] [blame] | 1007 | if (timer_pending(&conn->encrypt_pause_timer)) { |
| 1008 | BT_INFO("encrypt_pause_timer is pending"); |
| 1009 | return 0; |
| 1010 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1011 | cp.handle = cpu_to_le16(conn->handle); |
| 1012 | cp.encrypt = 1; |
| 1013 | hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, |
| 1014 | sizeof(cp), &cp); |
| 1015 | } |
| 1016 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | return 0; |
| 1018 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 1019 | EXPORT_SYMBOL(hci_conn_security); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
| 1021 | /* Change link key */ |
| 1022 | int hci_conn_change_link_key(struct hci_conn *conn) |
| 1023 | { |
| 1024 | BT_DBG("conn %p", conn); |
| 1025 | |
| 1026 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) { |
| 1027 | struct hci_cp_change_conn_link_key cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1028 | cp.handle = cpu_to_le16(conn->handle); |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 1029 | hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY, |
| 1030 | sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 1032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | return 0; |
| 1034 | } |
| 1035 | EXPORT_SYMBOL(hci_conn_change_link_key); |
| 1036 | |
| 1037 | /* Switch role */ |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 1038 | int hci_conn_switch_role(struct hci_conn *conn, __u8 role) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | { |
| 1040 | BT_DBG("conn %p", conn); |
| 1041 | |
| 1042 | if (!role && conn->link_mode & HCI_LM_MASTER) |
| 1043 | return 1; |
| 1044 | |
| 1045 | if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) { |
| 1046 | struct hci_cp_switch_role cp; |
| 1047 | bacpy(&cp.bdaddr, &conn->dst); |
| 1048 | cp.role = role; |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1049 | hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 1051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | return 0; |
| 1053 | } |
| 1054 | EXPORT_SYMBOL(hci_conn_switch_role); |
| 1055 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1056 | /* Enter active mode */ |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 1057 | void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1058 | { |
| 1059 | struct hci_dev *hdev = conn->hdev; |
| 1060 | |
| 1061 | BT_DBG("conn %p mode %d", conn, conn->mode); |
| 1062 | |
| 1063 | if (test_bit(HCI_RAW, &hdev->flags)) |
| 1064 | return; |
| 1065 | |
Sunny Kapdi | 39eaba3 | 2012-07-22 21:29:38 -0700 | [diff] [blame] | 1066 | if (conn->type == LE_LINK) |
| 1067 | return; |
| 1068 | |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 1069 | if (conn->mode != HCI_CM_SNIFF) |
| 1070 | goto timer; |
| 1071 | |
| 1072 | if (!conn->power_save && !force_active) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1073 | goto timer; |
| 1074 | |
| 1075 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) { |
| 1076 | struct hci_cp_exit_sniff_mode cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1077 | cp.handle = cpu_to_le16(conn->handle); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1078 | hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | timer: |
Rahul Kashyap | b44f9e2 | 2012-05-03 16:45:17 +0530 | [diff] [blame] | 1082 | if (hdev->idle_timeout > 0) { |
Srinivas Krovvidi | 6aadc41 | 2012-09-03 18:47:50 +0530 | [diff] [blame] | 1083 | spin_lock_bh(&conn->lock); |
| 1084 | if (conn->conn_valid) { |
| 1085 | mod_timer(&conn->idle_timer, |
| 1086 | jiffies + msecs_to_jiffies(hdev->idle_timeout)); |
Srinivas Krovvidi | 6aadc41 | 2012-09-03 18:47:50 +0530 | [diff] [blame] | 1087 | } |
| 1088 | spin_unlock_bh(&conn->lock); |
Rahul Kashyap | b44f9e2 | 2012-05-03 16:45:17 +0530 | [diff] [blame] | 1089 | } |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1090 | } |
| 1091 | |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 1092 | static inline void hci_conn_stop_rssi_timer(struct hci_conn *conn) |
| 1093 | { |
| 1094 | BT_DBG("conn %p", conn); |
| 1095 | cancel_delayed_work(&conn->rssi_update_work); |
| 1096 | } |
| 1097 | |
| 1098 | static inline void hci_conn_start_rssi_timer(struct hci_conn *conn, |
| 1099 | u16 interval) |
| 1100 | { |
| 1101 | struct hci_dev *hdev = conn->hdev; |
| 1102 | BT_DBG("conn %p, pending %d", conn, |
| 1103 | delayed_work_pending(&conn->rssi_update_work)); |
| 1104 | if (!delayed_work_pending(&conn->rssi_update_work)) { |
| 1105 | queue_delayed_work(hdev->workqueue, &conn->rssi_update_work, |
| 1106 | msecs_to_jiffies(interval)); |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | void hci_conn_set_rssi_reporter(struct hci_conn *conn, |
| 1111 | s8 rssi_threshold, u16 interval, u8 updateOnThreshExceed) |
| 1112 | { |
| 1113 | if (conn) { |
| 1114 | conn->rssi_threshold = rssi_threshold; |
| 1115 | conn->rssi_update_interval = interval; |
| 1116 | conn->rssi_update_thresh_exceed = updateOnThreshExceed; |
| 1117 | hci_conn_start_rssi_timer(conn, interval); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | void hci_conn_unset_rssi_reporter(struct hci_conn *conn) |
| 1122 | { |
| 1123 | if (conn) { |
| 1124 | BT_DBG("Deleting the rssi_update_timer"); |
| 1125 | hci_conn_stop_rssi_timer(conn); |
| 1126 | } |
| 1127 | } |
| 1128 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1129 | /* Enter sniff mode */ |
| 1130 | void hci_conn_enter_sniff_mode(struct hci_conn *conn) |
| 1131 | { |
| 1132 | struct hci_dev *hdev = conn->hdev; |
| 1133 | |
| 1134 | BT_DBG("conn %p mode %d", conn, conn->mode); |
| 1135 | |
| 1136 | if (test_bit(HCI_RAW, &hdev->flags)) |
| 1137 | return; |
| 1138 | |
Sunny Kapdi | 39eaba3 | 2012-07-22 21:29:38 -0700 | [diff] [blame] | 1139 | if (conn->type == LE_LINK) |
| 1140 | return; |
| 1141 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1142 | if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn)) |
| 1143 | return; |
| 1144 | |
Srinivas Krovvidi | 32ba935 | 2012-01-26 10:48:08 +0530 | [diff] [blame] | 1145 | if (conn->mode != HCI_CM_ACTIVE || |
| 1146 | !(conn->link_policy & HCI_LP_SNIFF) || |
| 1147 | (hci_find_link_key(hdev, &conn->dst) == NULL)) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1148 | return; |
| 1149 | |
| 1150 | if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) { |
| 1151 | struct hci_cp_sniff_subrate cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1152 | cp.handle = cpu_to_le16(conn->handle); |
| 1153 | cp.max_latency = cpu_to_le16(0); |
| 1154 | cp.min_remote_timeout = cpu_to_le16(0); |
| 1155 | cp.min_local_timeout = cpu_to_le16(0); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1156 | hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) { |
| 1160 | struct hci_cp_sniff_mode cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1161 | cp.handle = cpu_to_le16(conn->handle); |
| 1162 | cp.max_interval = cpu_to_le16(hdev->sniff_max_interval); |
| 1163 | cp.min_interval = cpu_to_le16(hdev->sniff_min_interval); |
| 1164 | cp.attempt = cpu_to_le16(4); |
| 1165 | cp.timeout = cpu_to_le16(1); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1166 | hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | |
Peter Krystad | a8417e6 | 2012-03-21 16:58:17 -0700 | [diff] [blame] | 1170 | struct hci_chan *hci_chan_create(struct hci_chan *chan, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1171 | struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs) |
| 1172 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1173 | struct hci_cp_create_logical_link cp; |
| 1174 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1175 | chan->state = BT_CONNECT; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1176 | chan->tx_fs = *tx_fs; |
| 1177 | chan->rx_fs = *rx_fs; |
| 1178 | cp.phy_handle = chan->conn->handle; |
| 1179 | cp.tx_fs.id = chan->tx_fs.id; |
| 1180 | cp.tx_fs.type = chan->tx_fs.type; |
| 1181 | cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu); |
| 1182 | cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time); |
| 1183 | cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency); |
| 1184 | cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to); |
| 1185 | cp.rx_fs.id = chan->rx_fs.id; |
| 1186 | cp.rx_fs.type = chan->rx_fs.type; |
| 1187 | cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu); |
| 1188 | cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time); |
| 1189 | cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency); |
| 1190 | cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to); |
| 1191 | hci_conn_hold(chan->conn); |
Peter Krystad | a8417e6 | 2012-03-21 16:58:17 -0700 | [diff] [blame] | 1192 | if (chan->conn->out) |
| 1193 | hci_send_cmd(chan->conn->hdev, HCI_OP_CREATE_LOGICAL_LINK, |
| 1194 | sizeof(cp), &cp); |
| 1195 | else |
| 1196 | hci_send_cmd(chan->conn->hdev, HCI_OP_ACCEPT_LOGICAL_LINK, |
| 1197 | sizeof(cp), &cp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1198 | return chan; |
| 1199 | } |
| 1200 | EXPORT_SYMBOL(hci_chan_create); |
| 1201 | |
| 1202 | void hci_chan_modify(struct hci_chan *chan, |
| 1203 | struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs) |
| 1204 | { |
| 1205 | struct hci_cp_flow_spec_modify cp; |
| 1206 | |
| 1207 | chan->tx_fs = *tx_fs; |
| 1208 | chan->rx_fs = *rx_fs; |
| 1209 | cp.log_handle = cpu_to_le16(chan->ll_handle); |
| 1210 | cp.tx_fs.id = tx_fs->id; |
| 1211 | cp.tx_fs.type = tx_fs->type; |
| 1212 | cp.tx_fs.max_sdu = cpu_to_le16(tx_fs->max_sdu); |
| 1213 | cp.tx_fs.sdu_arr_time = cpu_to_le32(tx_fs->sdu_arr_time); |
| 1214 | cp.tx_fs.acc_latency = cpu_to_le32(tx_fs->acc_latency); |
| 1215 | cp.tx_fs.flush_to = cpu_to_le32(tx_fs->flush_to); |
| 1216 | cp.rx_fs.id = rx_fs->id; |
| 1217 | cp.rx_fs.type = rx_fs->type; |
| 1218 | cp.rx_fs.max_sdu = cpu_to_le16(rx_fs->max_sdu); |
| 1219 | cp.rx_fs.sdu_arr_time = cpu_to_le32(rx_fs->sdu_arr_time); |
| 1220 | cp.rx_fs.acc_latency = cpu_to_le32(rx_fs->acc_latency); |
| 1221 | cp.rx_fs.flush_to = cpu_to_le32(rx_fs->flush_to); |
| 1222 | hci_conn_hold(chan->conn); |
| 1223 | hci_send_cmd(chan->conn->hdev, HCI_OP_FLOW_SPEC_MODIFY, sizeof(cp), |
| 1224 | &cp); |
| 1225 | } |
| 1226 | EXPORT_SYMBOL(hci_chan_modify); |
| 1227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | /* Drop all connection on the device */ |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 1229 | void hci_conn_hash_flush(struct hci_dev *hdev, u8 is_process) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | { |
| 1231 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 1232 | struct list_head *p; |
| 1233 | |
| 1234 | BT_DBG("hdev %s", hdev->name); |
| 1235 | |
| 1236 | p = h->list.next; |
| 1237 | while (p != &h->list) { |
| 1238 | struct hci_conn *c; |
| 1239 | |
| 1240 | c = list_entry(p, struct hci_conn, list); |
| 1241 | p = p->next; |
| 1242 | |
| 1243 | c->state = BT_CLOSED; |
| 1244 | |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 1245 | hci_proto_disconn_cfm(c, 0x16, is_process); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | hci_conn_del(c); |
| 1247 | } |
| 1248 | } |
| 1249 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1250 | /* Check pending connect attempts */ |
| 1251 | void hci_conn_check_pending(struct hci_dev *hdev) |
| 1252 | { |
| 1253 | struct hci_conn *conn; |
| 1254 | |
| 1255 | BT_DBG("hdev %s", hdev->name); |
| 1256 | |
| 1257 | hci_dev_lock(hdev); |
| 1258 | |
| 1259 | conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2); |
| 1260 | if (conn) |
| 1261 | hci_acl_connect(conn); |
| 1262 | |
| 1263 | hci_dev_unlock(hdev); |
| 1264 | } |
| 1265 | |
Marcel Holtmann | 9eba32b | 2009-08-22 14:19:26 -0700 | [diff] [blame] | 1266 | void hci_conn_hold_device(struct hci_conn *conn) |
| 1267 | { |
| 1268 | atomic_inc(&conn->devref); |
| 1269 | } |
| 1270 | EXPORT_SYMBOL(hci_conn_hold_device); |
| 1271 | |
| 1272 | void hci_conn_put_device(struct hci_conn *conn) |
| 1273 | { |
Hemant Gupta | 6bca5cd | 2013-06-08 19:07:57 +0530 | [diff] [blame] | 1274 | if (atomic_dec_and_test(&conn->devref)) { |
| 1275 | conn->hidp_session_valid = false; |
Marcel Holtmann | 9eba32b | 2009-08-22 14:19:26 -0700 | [diff] [blame] | 1276 | hci_conn_del_sysfs(conn); |
Hemant Gupta | 6bca5cd | 2013-06-08 19:07:57 +0530 | [diff] [blame] | 1277 | } |
Marcel Holtmann | 9eba32b | 2009-08-22 14:19:26 -0700 | [diff] [blame] | 1278 | } |
| 1279 | EXPORT_SYMBOL(hci_conn_put_device); |
| 1280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | int hci_get_conn_list(void __user *arg) |
| 1282 | { |
| 1283 | struct hci_conn_list_req req, *cl; |
| 1284 | struct hci_conn_info *ci; |
| 1285 | struct hci_dev *hdev; |
| 1286 | struct list_head *p; |
| 1287 | int n = 0, size, err; |
| 1288 | |
| 1289 | if (copy_from_user(&req, arg, sizeof(req))) |
| 1290 | return -EFAULT; |
| 1291 | |
| 1292 | if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci)) |
| 1293 | return -EINVAL; |
| 1294 | |
| 1295 | size = sizeof(req) + req.conn_num * sizeof(*ci); |
| 1296 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1297 | cl = kmalloc(size, GFP_KERNEL); |
| 1298 | if (!cl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | return -ENOMEM; |
| 1300 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1301 | hdev = hci_dev_get(req.dev_id); |
| 1302 | if (!hdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | kfree(cl); |
| 1304 | return -ENODEV; |
| 1305 | } |
| 1306 | |
| 1307 | ci = cl->conn_info; |
| 1308 | |
| 1309 | hci_dev_lock_bh(hdev); |
| 1310 | list_for_each(p, &hdev->conn_hash.list) { |
| 1311 | register struct hci_conn *c; |
| 1312 | c = list_entry(p, struct hci_conn, list); |
| 1313 | |
| 1314 | bacpy(&(ci + n)->bdaddr, &c->dst); |
| 1315 | (ci + n)->handle = c->handle; |
| 1316 | (ci + n)->type = c->type; |
| 1317 | (ci + n)->out = c->out; |
| 1318 | (ci + n)->state = c->state; |
| 1319 | (ci + n)->link_mode = c->link_mode; |
Nick Pelly | c172849 | 2009-12-09 00:15:41 -0800 | [diff] [blame] | 1320 | if (c->type == SCO_LINK) { |
| 1321 | (ci + n)->mtu = hdev->sco_mtu; |
| 1322 | (ci + n)->cnt = hdev->sco_cnt; |
| 1323 | (ci + n)->pkts = hdev->sco_pkts; |
| 1324 | } else { |
| 1325 | (ci + n)->mtu = hdev->acl_mtu; |
| 1326 | (ci + n)->cnt = hdev->acl_cnt; |
| 1327 | (ci + n)->pkts = hdev->acl_pkts; |
| 1328 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | if (++n >= req.conn_num) |
| 1330 | break; |
| 1331 | } |
| 1332 | hci_dev_unlock_bh(hdev); |
| 1333 | |
| 1334 | cl->dev_id = hdev->id; |
| 1335 | cl->conn_num = n; |
| 1336 | size = sizeof(req) + n * sizeof(*ci); |
| 1337 | |
| 1338 | hci_dev_put(hdev); |
| 1339 | |
| 1340 | err = copy_to_user(arg, cl, size); |
| 1341 | kfree(cl); |
| 1342 | |
| 1343 | return err ? -EFAULT : 0; |
| 1344 | } |
| 1345 | |
| 1346 | int hci_get_conn_info(struct hci_dev *hdev, void __user *arg) |
| 1347 | { |
| 1348 | struct hci_conn_info_req req; |
| 1349 | struct hci_conn_info ci; |
| 1350 | struct hci_conn *conn; |
| 1351 | char __user *ptr = arg + sizeof(req); |
| 1352 | |
| 1353 | if (copy_from_user(&req, arg, sizeof(req))) |
| 1354 | return -EFAULT; |
| 1355 | |
| 1356 | hci_dev_lock_bh(hdev); |
| 1357 | conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr); |
| 1358 | if (conn) { |
| 1359 | bacpy(&ci.bdaddr, &conn->dst); |
| 1360 | ci.handle = conn->handle; |
| 1361 | ci.type = conn->type; |
| 1362 | ci.out = conn->out; |
| 1363 | ci.state = conn->state; |
| 1364 | ci.link_mode = conn->link_mode; |
Nick Pelly | c172849 | 2009-12-09 00:15:41 -0800 | [diff] [blame] | 1365 | if (req.type == SCO_LINK) { |
| 1366 | ci.mtu = hdev->sco_mtu; |
| 1367 | ci.cnt = hdev->sco_cnt; |
| 1368 | ci.pkts = hdev->sco_pkts; |
| 1369 | } else { |
| 1370 | ci.mtu = hdev->acl_mtu; |
| 1371 | ci.cnt = hdev->acl_cnt; |
| 1372 | ci.pkts = hdev->acl_pkts; |
| 1373 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1374 | ci.pending_sec_level = conn->pending_sec_level; |
| 1375 | ci.ssp_mode = conn->ssp_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | } |
| 1377 | hci_dev_unlock_bh(hdev); |
| 1378 | |
| 1379 | if (!conn) |
| 1380 | return -ENOENT; |
| 1381 | |
| 1382 | return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0; |
| 1383 | } |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 1384 | |
| 1385 | int hci_get_auth_info(struct hci_dev *hdev, void __user *arg) |
| 1386 | { |
| 1387 | struct hci_auth_info_req req; |
| 1388 | struct hci_conn *conn; |
| 1389 | |
| 1390 | if (copy_from_user(&req, arg, sizeof(req))) |
| 1391 | return -EFAULT; |
| 1392 | |
| 1393 | hci_dev_lock_bh(hdev); |
| 1394 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); |
| 1395 | if (conn) |
| 1396 | req.type = conn->auth_type; |
| 1397 | hci_dev_unlock_bh(hdev); |
| 1398 | |
| 1399 | if (!conn) |
| 1400 | return -ENOENT; |
| 1401 | |
| 1402 | return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; |
| 1403 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1404 | |
| 1405 | int hci_set_auth_info(struct hci_dev *hdev, void __user *arg) |
| 1406 | { |
| 1407 | struct hci_auth_info_req req; |
| 1408 | struct hci_conn *conn; |
| 1409 | |
| 1410 | if (copy_from_user(&req, arg, sizeof(req))) |
| 1411 | return -EFAULT; |
| 1412 | |
| 1413 | hci_dev_lock_bh(hdev); |
| 1414 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); |
Srinivas Krovvidi | 52b05ba | 2012-08-27 18:32:45 +0530 | [diff] [blame] | 1415 | if (conn) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1416 | conn->auth_type = req.type; |
Srinivas Krovvidi | 52b05ba | 2012-08-27 18:32:45 +0530 | [diff] [blame] | 1417 | switch (conn->auth_type) { |
| 1418 | case HCI_AT_NO_BONDING: |
| 1419 | conn->pending_sec_level = BT_SECURITY_LOW; |
| 1420 | break; |
| 1421 | case HCI_AT_DEDICATED_BONDING: |
| 1422 | case HCI_AT_GENERAL_BONDING: |
| 1423 | conn->pending_sec_level = BT_SECURITY_MEDIUM; |
| 1424 | break; |
| 1425 | case HCI_AT_DEDICATED_BONDING_MITM: |
| 1426 | case HCI_AT_GENERAL_BONDING_MITM: |
| 1427 | conn->pending_sec_level = BT_SECURITY_HIGH; |
| 1428 | break; |
| 1429 | default: |
| 1430 | break; |
| 1431 | } |
| 1432 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1433 | hci_dev_unlock_bh(hdev); |
| 1434 | |
| 1435 | if (!conn) |
| 1436 | return -ENOENT; |
| 1437 | |
| 1438 | return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; |
| 1439 | } |