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 |
Ron Shaffer | 2d0a034 | 2010-05-28 11:53:46 -0400 | [diff] [blame] | 3 | Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 5 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License version 2 as |
| 9 | published by the Free Software Foundation; |
| 10 | |
| 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth HCI connection handling. */ |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/module.h> |
| 28 | |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/errno.h> |
| 31 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/slab.h> |
| 33 | #include <linux/poll.h> |
| 34 | #include <linux/fcntl.h> |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/skbuff.h> |
| 37 | #include <linux/interrupt.h> |
| 38 | #include <linux/notifier.h> |
| 39 | #include <net/sock.h> |
| 40 | |
| 41 | #include <asm/system.h> |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 42 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/unaligned.h> |
| 44 | |
| 45 | #include <net/bluetooth/bluetooth.h> |
| 46 | #include <net/bluetooth/hci_core.h> |
| 47 | |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 48 | static void hci_le_connect(struct hci_conn *conn) |
| 49 | { |
| 50 | struct hci_dev *hdev = conn->hdev; |
| 51 | struct hci_cp_le_create_conn cp; |
| 52 | |
| 53 | conn->state = BT_CONNECT; |
| 54 | conn->out = 1; |
Vinicius Costa Gomes | b92a622 | 2011-02-10 22:38:52 -0300 | [diff] [blame] | 55 | conn->link_mode |= HCI_LM_MASTER; |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 56 | |
| 57 | memset(&cp, 0, sizeof(cp)); |
| 58 | cp.scan_interval = cpu_to_le16(0x0004); |
| 59 | cp.scan_window = cpu_to_le16(0x0004); |
| 60 | bacpy(&cp.peer_addr, &conn->dst); |
Andre Guedes | c7f0d99 | 2011-05-31 14:20:57 -0300 | [diff] [blame] | 61 | cp.peer_addr_type = conn->dst_type; |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 62 | cp.conn_interval_min = cpu_to_le16(0x0008); |
| 63 | cp.conn_interval_max = cpu_to_le16(0x0100); |
| 64 | cp.supervision_timeout = cpu_to_le16(0x0064); |
| 65 | cp.min_ce_len = cpu_to_le16(0x0001); |
| 66 | cp.max_ce_len = cpu_to_le16(0x0001); |
| 67 | |
| 68 | hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp); |
| 69 | } |
| 70 | |
| 71 | static void hci_le_connect_cancel(struct hci_conn *conn) |
| 72 | { |
| 73 | hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL); |
| 74 | } |
| 75 | |
Marcel Holtmann | 4c67bc7 | 2006-10-15 17:30:56 +0200 | [diff] [blame] | 76 | void hci_acl_connect(struct hci_conn *conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
| 78 | struct hci_dev *hdev = conn->hdev; |
| 79 | struct inquiry_entry *ie; |
| 80 | struct hci_cp_create_conn cp; |
| 81 | |
| 82 | BT_DBG("%p", conn); |
| 83 | |
| 84 | conn->state = BT_CONNECT; |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 85 | conn->out = 1; |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | conn->link_mode = HCI_LM_MASTER; |
| 88 | |
Marcel Holtmann | 4c67bc7 | 2006-10-15 17:30:56 +0200 | [diff] [blame] | 89 | conn->attempt++; |
| 90 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 91 | conn->link_policy = hdev->link_policy; |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | memset(&cp, 0, sizeof(cp)); |
| 94 | bacpy(&cp.bdaddr, &conn->dst); |
| 95 | cp.pscan_rep_mode = 0x02; |
| 96 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 97 | ie = hci_inquiry_cache_lookup(hdev, &conn->dst); |
| 98 | if (ie) { |
Marcel Holtmann | 41a9621 | 2008-07-14 20:13:48 +0200 | [diff] [blame] | 99 | if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) { |
| 100 | cp.pscan_rep_mode = ie->data.pscan_rep_mode; |
| 101 | cp.pscan_mode = ie->data.pscan_mode; |
| 102 | cp.clock_offset = ie->data.clock_offset | |
| 103 | cpu_to_le16(0x8000); |
| 104 | } |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | memcpy(conn->dev_class, ie->data.dev_class, 3); |
Marcel Holtmann | 41a9621 | 2008-07-14 20:13:48 +0200 | [diff] [blame] | 107 | conn->ssp_mode = ie->data.ssp_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 110 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER)) |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 112 | cp.role_switch = 0x01; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | else |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 114 | cp.role_switch = 0x00; |
Marcel Holtmann | 4c67bc7 | 2006-10-15 17:30:56 +0200 | [diff] [blame] | 115 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 116 | hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 119 | static void hci_acl_connect_cancel(struct hci_conn *conn) |
| 120 | { |
| 121 | struct hci_cp_create_conn_cancel cp; |
| 122 | |
| 123 | BT_DBG("%p", conn); |
| 124 | |
| 125 | if (conn->hdev->hci_ver < 2) |
| 126 | return; |
| 127 | |
| 128 | bacpy(&cp.bdaddr, &conn->dst); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 129 | 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] | 130 | } |
| 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | void hci_acl_disconn(struct hci_conn *conn, __u8 reason) |
| 133 | { |
| 134 | struct hci_cp_disconnect cp; |
| 135 | |
| 136 | BT_DBG("%p", conn); |
| 137 | |
| 138 | conn->state = BT_DISCONN; |
| 139 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 140 | cp.handle = cpu_to_le16(conn->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | cp.reason = reason; |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 142 | hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void hci_add_sco(struct hci_conn *conn, __u16 handle) |
| 146 | { |
| 147 | struct hci_dev *hdev = conn->hdev; |
| 148 | struct hci_cp_add_sco cp; |
| 149 | |
| 150 | BT_DBG("%p", conn); |
| 151 | |
| 152 | conn->state = BT_CONNECT; |
| 153 | conn->out = 1; |
| 154 | |
Marcel Holtmann | efc7688 | 2009-02-06 09:13:37 +0100 | [diff] [blame] | 155 | conn->attempt++; |
| 156 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 157 | cp.handle = cpu_to_le16(handle); |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 158 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 160 | hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 163 | void hci_setup_sync(struct hci_conn *conn, __u16 handle) |
| 164 | { |
| 165 | struct hci_dev *hdev = conn->hdev; |
| 166 | struct hci_cp_setup_sync_conn cp; |
| 167 | |
| 168 | BT_DBG("%p", conn); |
| 169 | |
| 170 | conn->state = BT_CONNECT; |
| 171 | conn->out = 1; |
| 172 | |
Marcel Holtmann | efc7688 | 2009-02-06 09:13:37 +0100 | [diff] [blame] | 173 | conn->attempt++; |
| 174 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 175 | cp.handle = cpu_to_le16(handle); |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 176 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 177 | |
| 178 | cp.tx_bandwidth = cpu_to_le32(0x00001f40); |
| 179 | cp.rx_bandwidth = cpu_to_le32(0x00001f40); |
| 180 | cp.max_latency = cpu_to_le16(0xffff); |
| 181 | cp.voice_setting = cpu_to_le16(hdev->voice_setting); |
| 182 | cp.retrans_effort = 0xff; |
| 183 | |
| 184 | hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp); |
| 185 | } |
| 186 | |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 187 | void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, |
| 188 | u16 latency, u16 to_multiplier) |
| 189 | { |
| 190 | struct hci_cp_le_conn_update cp; |
| 191 | struct hci_dev *hdev = conn->hdev; |
| 192 | |
| 193 | memset(&cp, 0, sizeof(cp)); |
| 194 | |
| 195 | cp.handle = cpu_to_le16(conn->handle); |
| 196 | cp.conn_interval_min = cpu_to_le16(min); |
| 197 | cp.conn_interval_max = cpu_to_le16(max); |
| 198 | cp.conn_latency = cpu_to_le16(latency); |
| 199 | cp.supervision_timeout = cpu_to_le16(to_multiplier); |
| 200 | cp.min_ce_len = cpu_to_le16(0x0001); |
| 201 | cp.max_ce_len = cpu_to_le16(0x0001); |
| 202 | |
| 203 | hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp); |
| 204 | } |
| 205 | EXPORT_SYMBOL(hci_le_conn_update); |
| 206 | |
Marcel Holtmann | e73439d | 2010-07-26 10:06:00 -0400 | [diff] [blame] | 207 | /* Device _must_ be locked */ |
| 208 | void hci_sco_setup(struct hci_conn *conn, __u8 status) |
| 209 | { |
| 210 | struct hci_conn *sco = conn->link; |
| 211 | |
| 212 | BT_DBG("%p", conn); |
| 213 | |
| 214 | if (!sco) |
| 215 | return; |
| 216 | |
| 217 | if (!status) { |
| 218 | if (lmp_esco_capable(conn->hdev)) |
| 219 | hci_setup_sync(sco, conn->handle); |
| 220 | else |
| 221 | hci_add_sco(sco, conn->handle); |
| 222 | } else { |
| 223 | hci_proto_connect_cfm(sco, status); |
| 224 | hci_conn_del(sco); |
| 225 | } |
| 226 | } |
| 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | static void hci_conn_timeout(unsigned long arg) |
| 229 | { |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 230 | struct hci_conn *conn = (void *) arg; |
| 231 | struct hci_dev *hdev = conn->hdev; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 232 | __u8 reason; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
| 234 | BT_DBG("conn %p state %d", conn, conn->state); |
| 235 | |
| 236 | if (atomic_read(&conn->refcnt)) |
| 237 | return; |
| 238 | |
| 239 | hci_dev_lock(hdev); |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 240 | |
| 241 | switch (conn->state) { |
| 242 | case BT_CONNECT: |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 243 | case BT_CONNECT2: |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 244 | if (conn->out) { |
| 245 | if (conn->type == ACL_LINK) |
| 246 | hci_acl_connect_cancel(conn); |
| 247 | else if (conn->type == LE_LINK) |
| 248 | hci_le_connect_cancel(conn); |
| 249 | } |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 250 | break; |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 251 | case BT_CONFIG: |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 252 | case BT_CONNECTED: |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 253 | reason = hci_proto_disconn_ind(conn); |
| 254 | hci_acl_disconn(conn, reason); |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 255 | break; |
| 256 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | conn->state = BT_CLOSED; |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 258 | break; |
| 259 | } |
| 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 264 | static void hci_conn_idle(unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 266 | struct hci_conn *conn = (void *) arg; |
| 267 | |
| 268 | BT_DBG("conn %p mode %d", conn, conn->mode); |
| 269 | |
| 270 | hci_conn_enter_sniff_mode(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Johan Hedberg | 9f61656 | 2011-04-28 11:28:54 -0700 | [diff] [blame] | 273 | static void hci_conn_auto_accept(unsigned long arg) |
| 274 | { |
| 275 | struct hci_conn *conn = (void *) arg; |
| 276 | struct hci_dev *hdev = conn->hdev; |
| 277 | |
| 278 | hci_dev_lock(hdev); |
| 279 | |
| 280 | hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst), |
| 281 | &conn->dst); |
| 282 | |
| 283 | hci_dev_unlock(hdev); |
| 284 | } |
| 285 | |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 286 | struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, |
| 287 | __u16 pkt_type, bdaddr_t *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
| 289 | struct hci_conn *conn; |
| 290 | |
| 291 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); |
| 292 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 293 | conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC); |
| 294 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
| 297 | bacpy(&conn->dst, dst); |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 298 | conn->hdev = hdev; |
| 299 | conn->type = type; |
| 300 | conn->mode = HCI_CM_ACTIVE; |
| 301 | conn->state = BT_OPEN; |
Andrei Emeltchenko | 93f19c9 | 2009-09-03 12:34:19 +0300 | [diff] [blame] | 302 | conn->auth_type = HCI_AT_GENERAL_BONDING; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 303 | conn->io_capability = hdev->io_capability; |
Johan Hedberg | a958355 | 2011-02-19 12:06:01 -0300 | [diff] [blame] | 304 | conn->remote_auth = 0xff; |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 305 | conn->key_type = 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 307 | conn->power_save = 1; |
Marcel Holtmann | 052b30b | 2009-04-26 20:01:22 +0200 | [diff] [blame] | 308 | conn->disc_timeout = HCI_DISCONN_TIMEOUT; |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 309 | |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 310 | switch (type) { |
| 311 | case ACL_LINK: |
| 312 | conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; |
| 313 | break; |
| 314 | case SCO_LINK: |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 315 | if (!pkt_type) |
| 316 | pkt_type = SCO_ESCO_MASK; |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 317 | case ESCO_LINK: |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 318 | if (!pkt_type) |
| 319 | pkt_type = ALL_ESCO_MASK; |
| 320 | if (lmp_esco_capable(hdev)) { |
| 321 | /* HCI Setup Synchronous Connection Command uses |
| 322 | reverse logic on the EDR_ESCO_MASK bits */ |
| 323 | conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) & |
| 324 | hdev->esco_type; |
| 325 | } else { |
| 326 | /* Legacy HCI Add Sco Connection Command uses a |
| 327 | shifted bitmask */ |
| 328 | conn->pkt_type = (pkt_type << 5) & hdev->pkt_type & |
| 329 | SCO_PTYPE_MASK; |
| 330 | } |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 331 | break; |
| 332 | } |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | skb_queue_head_init(&conn->data_q); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 335 | |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 336 | setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn); |
| 337 | setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn); |
Johan Hedberg | 9f61656 | 2011-04-28 11:28:54 -0700 | [diff] [blame] | 338 | setup_timer(&conn->auto_accept_timer, hci_conn_auto_accept, |
| 339 | (unsigned long) conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
| 341 | atomic_set(&conn->refcnt, 0); |
| 342 | |
| 343 | hci_dev_hold(hdev); |
| 344 | |
| 345 | tasklet_disable(&hdev->tx_task); |
| 346 | |
| 347 | hci_conn_hash_add(hdev, conn); |
| 348 | if (hdev->notify) |
| 349 | hdev->notify(hdev, HCI_NOTIFY_CONN_ADD); |
| 350 | |
Marcel Holtmann | 9eba32b | 2009-08-22 14:19:26 -0700 | [diff] [blame] | 351 | atomic_set(&conn->devref, 0); |
| 352 | |
Marcel Holtmann | a67e899 | 2009-05-02 18:24:06 -0700 | [diff] [blame] | 353 | hci_conn_init_sysfs(conn); |
| 354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | tasklet_enable(&hdev->tx_task); |
| 356 | |
| 357 | return conn; |
| 358 | } |
| 359 | |
| 360 | int hci_conn_del(struct hci_conn *conn) |
| 361 | { |
| 362 | struct hci_dev *hdev = conn->hdev; |
| 363 | |
| 364 | BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle); |
| 365 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 366 | del_timer(&conn->idle_timer); |
| 367 | |
| 368 | del_timer(&conn->disc_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
Johan Hedberg | 9f61656 | 2011-04-28 11:28:54 -0700 | [diff] [blame] | 370 | del_timer(&conn->auto_accept_timer); |
| 371 | |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 372 | if (conn->type == ACL_LINK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | struct hci_conn *sco = conn->link; |
| 374 | if (sco) |
| 375 | sco->link = NULL; |
| 376 | |
| 377 | /* Unacked frames */ |
| 378 | hdev->acl_cnt += conn->sent; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 379 | } else if (conn->type == LE_LINK) { |
| 380 | if (hdev->le_pkts) |
| 381 | hdev->le_cnt += conn->sent; |
| 382 | else |
| 383 | hdev->acl_cnt += conn->sent; |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 384 | } else { |
| 385 | struct hci_conn *acl = conn->link; |
| 386 | if (acl) { |
| 387 | acl->link = NULL; |
| 388 | hci_conn_put(acl); |
| 389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | tasklet_disable(&hdev->tx_task); |
Marcel Holtmann | 7d0db0a | 2008-07-14 20:13:51 +0200 | [diff] [blame] | 393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | hci_conn_hash_del(hdev, conn); |
| 395 | if (hdev->notify) |
| 396 | hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); |
Marcel Holtmann | 7d0db0a | 2008-07-14 20:13:51 +0200 | [diff] [blame] | 397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | tasklet_enable(&hdev->tx_task); |
Marcel Holtmann | 7d0db0a | 2008-07-14 20:13:51 +0200 | [diff] [blame] | 399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | skb_queue_purge(&conn->data_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
Marcel Holtmann | 9eba32b | 2009-08-22 14:19:26 -0700 | [diff] [blame] | 402 | hci_conn_put_device(conn); |
Dave Young | 2ae9a6b | 2009-02-21 16:13:34 +0800 | [diff] [blame] | 403 | |
Marcel Holtmann | 384943e | 2009-05-08 18:20:43 -0700 | [diff] [blame] | 404 | hci_dev_put(hdev); |
| 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src) |
| 410 | { |
| 411 | int use_src = bacmp(src, BDADDR_ANY); |
| 412 | struct hci_dev *hdev = NULL; |
| 413 | struct list_head *p; |
| 414 | |
| 415 | BT_DBG("%s -> %s", batostr(src), batostr(dst)); |
| 416 | |
| 417 | read_lock_bh(&hci_dev_list_lock); |
| 418 | |
| 419 | list_for_each(p, &hci_dev_list) { |
| 420 | struct hci_dev *d = list_entry(p, struct hci_dev, list); |
| 421 | |
| 422 | if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags)) |
| 423 | continue; |
| 424 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 425 | /* Simple routing: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | * No source address - find interface with bdaddr != dst |
| 427 | * Source address - find interface with bdaddr == src |
| 428 | */ |
| 429 | |
| 430 | if (use_src) { |
| 431 | if (!bacmp(&d->bdaddr, src)) { |
| 432 | hdev = d; break; |
| 433 | } |
| 434 | } else { |
| 435 | if (bacmp(&d->bdaddr, dst)) { |
| 436 | hdev = d; break; |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | if (hdev) |
| 442 | hdev = hci_dev_hold(hdev); |
| 443 | |
| 444 | read_unlock_bh(&hci_dev_list_lock); |
| 445 | return hdev; |
| 446 | } |
| 447 | EXPORT_SYMBOL(hci_get_route); |
| 448 | |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 449 | /* Create SCO, ACL or LE connection. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | * Device _must_ be locked */ |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 451 | struct hci_conn *hci_connect(struct hci_dev *hdev, int type, |
| 452 | __u16 pkt_type, bdaddr_t *dst, |
| 453 | __u8 sec_level, __u8 auth_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | { |
| 455 | struct hci_conn *acl; |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 456 | struct hci_conn *sco; |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 457 | struct hci_conn *le; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
| 459 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); |
| 460 | |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 461 | if (type == LE_LINK) { |
Andre Guedes | 5e89ece | 2011-05-31 14:20:56 -0300 | [diff] [blame] | 462 | struct adv_entry *entry; |
| 463 | |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 464 | le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst); |
Anderson Briglia | 15c4794 | 2011-02-21 15:09:23 -0300 | [diff] [blame] | 465 | if (le) |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 466 | return ERR_PTR(-EBUSY); |
Andre Guedes | 5e89ece | 2011-05-31 14:20:56 -0300 | [diff] [blame] | 467 | |
| 468 | entry = hci_find_adv_entry(hdev, dst); |
| 469 | if (!entry) |
| 470 | return ERR_PTR(-EHOSTUNREACH); |
| 471 | |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 472 | le = hci_conn_add(hdev, LE_LINK, 0, dst); |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 473 | if (!le) |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 474 | return ERR_PTR(-ENOMEM); |
Andre Guedes | 92398c8 | 2011-05-31 14:20:55 -0300 | [diff] [blame] | 475 | |
Andre Guedes | 5e89ece | 2011-05-31 14:20:56 -0300 | [diff] [blame] | 476 | le->dst_type = entry->bdaddr_type; |
| 477 | |
Andre Guedes | 92398c8 | 2011-05-31 14:20:55 -0300 | [diff] [blame] | 478 | hci_le_connect(le); |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 479 | |
| 480 | hci_conn_hold(le); |
| 481 | |
| 482 | return le; |
| 483 | } |
| 484 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 485 | acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); |
| 486 | if (!acl) { |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 487 | acl = hci_conn_add(hdev, ACL_LINK, 0, dst); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 488 | if (!acl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | return NULL; |
| 490 | } |
| 491 | |
| 492 | hci_conn_hold(acl); |
| 493 | |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 494 | if (acl->state == BT_OPEN || acl->state == BT_CLOSED) { |
Johan Hedberg | 765c2a9 | 2011-01-19 12:06:52 +0530 | [diff] [blame] | 495 | acl->sec_level = BT_SECURITY_LOW; |
| 496 | acl->pending_sec_level = sec_level; |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 497 | acl->auth_type = auth_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | hci_acl_connect(acl); |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 499 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 501 | if (type == ACL_LINK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | return acl; |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 503 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 504 | sco = hci_conn_hash_lookup_ba(hdev, type, dst); |
| 505 | if (!sco) { |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 506 | sco = hci_conn_add(hdev, type, pkt_type, dst); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 507 | if (!sco) { |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 508 | hci_conn_put(acl); |
| 509 | return NULL; |
| 510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | } |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 512 | |
| 513 | acl->link = sco; |
| 514 | sco->link = acl; |
| 515 | |
| 516 | hci_conn_hold(sco); |
| 517 | |
| 518 | if (acl->state == BT_CONNECTED && |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 519 | (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { |
Nick Pelly | c390216 | 2009-11-13 14:16:32 -0800 | [diff] [blame] | 520 | acl->power_save = 1; |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame^] | 521 | hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON); |
Nick Pelly | c390216 | 2009-11-13 14:16:32 -0800 | [diff] [blame] | 522 | |
Marcel Holtmann | e73439d | 2010-07-26 10:06:00 -0400 | [diff] [blame] | 523 | if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) { |
| 524 | /* defer SCO setup until mode change completed */ |
| 525 | set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend); |
| 526 | return sco; |
| 527 | } |
| 528 | |
| 529 | hci_sco_setup(acl, 0x00); |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 530 | } |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 531 | |
| 532 | return sco; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | } |
| 534 | EXPORT_SYMBOL(hci_connect); |
| 535 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 536 | /* Check link security requirement */ |
| 537 | int hci_conn_check_link_mode(struct hci_conn *conn) |
| 538 | { |
| 539 | BT_DBG("conn %p", conn); |
| 540 | |
| 541 | if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 && |
| 542 | !(conn->link_mode & HCI_LM_ENCRYPT)) |
| 543 | return 0; |
| 544 | |
| 545 | return 1; |
| 546 | } |
| 547 | EXPORT_SYMBOL(hci_conn_check_link_mode); |
| 548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | /* Authenticate remote device */ |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 550 | 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] | 551 | { |
| 552 | BT_DBG("conn %p", conn); |
| 553 | |
Johan Hedberg | 765c2a9 | 2011-01-19 12:06:52 +0530 | [diff] [blame] | 554 | if (conn->pending_sec_level > sec_level) |
| 555 | sec_level = conn->pending_sec_level; |
| 556 | |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 557 | if (sec_level > conn->sec_level) |
Johan Hedberg | 765c2a9 | 2011-01-19 12:06:52 +0530 | [diff] [blame] | 558 | conn->pending_sec_level = sec_level; |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 559 | else if (conn->link_mode & HCI_LM_AUTH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | return 1; |
| 561 | |
Johan Hedberg | 65cf686 | 2011-01-19 12:06:49 +0530 | [diff] [blame] | 562 | /* Make sure we preserve an existing MITM requirement*/ |
| 563 | auth_type |= (conn->auth_type & 0x01); |
| 564 | |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 565 | conn->auth_type = auth_type; |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) { |
| 568 | struct hci_cp_auth_requested cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 569 | cp.handle = cpu_to_le16(conn->handle); |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 570 | hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, |
| 571 | sizeof(cp), &cp); |
Waldemar Rymarkiewicz | 62c5f52 | 2011-05-31 15:49:25 +0200 | [diff] [blame] | 572 | if (conn->key_type != 0xff) |
| 573 | set_bit(HCI_CONN_REAUTH_PEND, &conn->pend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | return 0; |
| 577 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 579 | /* Encrypt the the link */ |
| 580 | static void hci_conn_encrypt(struct hci_conn *conn) |
| 581 | { |
| 582 | BT_DBG("conn %p", conn); |
| 583 | |
| 584 | if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) { |
| 585 | struct hci_cp_set_conn_encrypt cp; |
| 586 | cp.handle = cpu_to_le16(conn->handle); |
| 587 | cp.encrypt = 0x01; |
| 588 | hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp), |
| 589 | &cp); |
| 590 | } |
| 591 | } |
| 592 | |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 593 | /* Enable security */ |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 594 | 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] | 595 | { |
| 596 | BT_DBG("conn %p", conn); |
| 597 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 598 | /* For sdp we don't need the link key. */ |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 599 | if (sec_level == BT_SECURITY_SDP) |
| 600 | return 1; |
| 601 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 602 | /* For non 2.1 devices and low security level we don't need the link |
| 603 | key. */ |
Marcel Holtmann | 3fdca1e | 2009-04-28 09:04:55 -0700 | [diff] [blame] | 604 | if (sec_level == BT_SECURITY_LOW && |
| 605 | (!conn->ssp_mode || !conn->hdev->ssp_mode)) |
| 606 | return 1; |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 607 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 608 | /* For other security levels we need the link key. */ |
| 609 | if (!(conn->link_mode & HCI_LM_AUTH)) |
| 610 | goto auth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 612 | /* An authenticated combination key has sufficient security for any |
| 613 | security level. */ |
| 614 | if (conn->key_type == HCI_LK_AUTH_COMBINATION) |
| 615 | goto encrypt; |
| 616 | |
| 617 | /* An unauthenticated combination key has sufficient security for |
| 618 | security level 1 and 2. */ |
| 619 | if (conn->key_type == HCI_LK_UNAUTH_COMBINATION && |
| 620 | (sec_level == BT_SECURITY_MEDIUM || |
| 621 | sec_level == BT_SECURITY_LOW)) |
| 622 | goto encrypt; |
| 623 | |
| 624 | /* A combination key has always sufficient security for the security |
| 625 | levels 1 or 2. High security level requires the combination key |
| 626 | is generated using maximum PIN code length (16). |
| 627 | For pre 2.1 units. */ |
| 628 | if (conn->key_type == HCI_LK_COMBINATION && |
| 629 | (sec_level != BT_SECURITY_HIGH || |
| 630 | conn->pin_length == 16)) |
| 631 | goto encrypt; |
| 632 | |
| 633 | auth: |
Ilia Kolomisnky | 3306054 | 2011-06-15 06:52:26 +0300 | [diff] [blame] | 634 | if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | return 0; |
| 636 | |
Luiz Augusto von Dentz | 6fdf658 | 2011-06-13 15:37:35 +0300 | [diff] [blame] | 637 | if (!hci_conn_auth(conn, sec_level, auth_type)) |
| 638 | return 0; |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 639 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 640 | encrypt: |
| 641 | if (conn->link_mode & HCI_LM_ENCRYPT) |
| 642 | return 1; |
| 643 | |
| 644 | hci_conn_encrypt(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | return 0; |
| 646 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 647 | EXPORT_SYMBOL(hci_conn_security); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
Waldemar Rymarkiewicz | b3b1b06 | 2011-05-06 09:42:31 +0200 | [diff] [blame] | 649 | /* Check secure link requirement */ |
| 650 | int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level) |
| 651 | { |
| 652 | BT_DBG("conn %p", conn); |
| 653 | |
| 654 | if (sec_level != BT_SECURITY_HIGH) |
| 655 | return 1; /* Accept if non-secure is required */ |
| 656 | |
| 657 | if (conn->key_type == HCI_LK_AUTH_COMBINATION || |
| 658 | (conn->key_type == HCI_LK_COMBINATION && |
| 659 | conn->pin_length == 16)) |
| 660 | return 1; |
| 661 | |
| 662 | return 0; /* Reject not secure link */ |
| 663 | } |
| 664 | EXPORT_SYMBOL(hci_conn_check_secure); |
| 665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | /* Change link key */ |
| 667 | int hci_conn_change_link_key(struct hci_conn *conn) |
| 668 | { |
| 669 | BT_DBG("conn %p", conn); |
| 670 | |
| 671 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) { |
| 672 | struct hci_cp_change_conn_link_key cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 673 | cp.handle = cpu_to_le16(conn->handle); |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 674 | hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY, |
| 675 | sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | return 0; |
| 679 | } |
| 680 | EXPORT_SYMBOL(hci_conn_change_link_key); |
| 681 | |
| 682 | /* Switch role */ |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 683 | int hci_conn_switch_role(struct hci_conn *conn, __u8 role) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | { |
| 685 | BT_DBG("conn %p", conn); |
| 686 | |
| 687 | if (!role && conn->link_mode & HCI_LM_MASTER) |
| 688 | return 1; |
| 689 | |
| 690 | if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) { |
| 691 | struct hci_cp_switch_role cp; |
| 692 | bacpy(&cp.bdaddr, &conn->dst); |
| 693 | cp.role = role; |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 694 | hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | return 0; |
| 698 | } |
| 699 | EXPORT_SYMBOL(hci_conn_switch_role); |
| 700 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 701 | /* Enter active mode */ |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame^] | 702 | 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] | 703 | { |
| 704 | struct hci_dev *hdev = conn->hdev; |
| 705 | |
| 706 | BT_DBG("conn %p mode %d", conn, conn->mode); |
| 707 | |
| 708 | if (test_bit(HCI_RAW, &hdev->flags)) |
| 709 | return; |
| 710 | |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame^] | 711 | if (conn->mode != HCI_CM_SNIFF) |
| 712 | goto timer; |
| 713 | |
| 714 | if (!conn->power_save && !force_active) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 715 | goto timer; |
| 716 | |
| 717 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) { |
| 718 | struct hci_cp_exit_sniff_mode cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 719 | cp.handle = cpu_to_le16(conn->handle); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 720 | hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | timer: |
| 724 | if (hdev->idle_timeout > 0) |
| 725 | mod_timer(&conn->idle_timer, |
| 726 | jiffies + msecs_to_jiffies(hdev->idle_timeout)); |
| 727 | } |
| 728 | |
| 729 | /* Enter sniff mode */ |
| 730 | void hci_conn_enter_sniff_mode(struct hci_conn *conn) |
| 731 | { |
| 732 | struct hci_dev *hdev = conn->hdev; |
| 733 | |
| 734 | BT_DBG("conn %p mode %d", conn, conn->mode); |
| 735 | |
| 736 | if (test_bit(HCI_RAW, &hdev->flags)) |
| 737 | return; |
| 738 | |
| 739 | if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn)) |
| 740 | return; |
| 741 | |
| 742 | if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF)) |
| 743 | return; |
| 744 | |
| 745 | if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) { |
| 746 | struct hci_cp_sniff_subrate cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 747 | cp.handle = cpu_to_le16(conn->handle); |
| 748 | cp.max_latency = cpu_to_le16(0); |
| 749 | cp.min_remote_timeout = cpu_to_le16(0); |
| 750 | cp.min_local_timeout = cpu_to_le16(0); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 751 | hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) { |
| 755 | struct hci_cp_sniff_mode cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 756 | cp.handle = cpu_to_le16(conn->handle); |
| 757 | cp.max_interval = cpu_to_le16(hdev->sniff_max_interval); |
| 758 | cp.min_interval = cpu_to_le16(hdev->sniff_min_interval); |
| 759 | cp.attempt = cpu_to_le16(4); |
| 760 | cp.timeout = cpu_to_le16(1); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 761 | hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | /* Drop all connection on the device */ |
| 766 | void hci_conn_hash_flush(struct hci_dev *hdev) |
| 767 | { |
| 768 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 769 | struct list_head *p; |
| 770 | |
| 771 | BT_DBG("hdev %s", hdev->name); |
| 772 | |
| 773 | p = h->list.next; |
| 774 | while (p != &h->list) { |
| 775 | struct hci_conn *c; |
| 776 | |
| 777 | c = list_entry(p, struct hci_conn, list); |
| 778 | p = p->next; |
| 779 | |
| 780 | c->state = BT_CLOSED; |
| 781 | |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 782 | hci_proto_disconn_cfm(c, 0x16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | hci_conn_del(c); |
| 784 | } |
| 785 | } |
| 786 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 787 | /* Check pending connect attempts */ |
| 788 | void hci_conn_check_pending(struct hci_dev *hdev) |
| 789 | { |
| 790 | struct hci_conn *conn; |
| 791 | |
| 792 | BT_DBG("hdev %s", hdev->name); |
| 793 | |
| 794 | hci_dev_lock(hdev); |
| 795 | |
| 796 | conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2); |
| 797 | if (conn) |
| 798 | hci_acl_connect(conn); |
| 799 | |
| 800 | hci_dev_unlock(hdev); |
| 801 | } |
| 802 | |
Marcel Holtmann | 9eba32b | 2009-08-22 14:19:26 -0700 | [diff] [blame] | 803 | void hci_conn_hold_device(struct hci_conn *conn) |
| 804 | { |
| 805 | atomic_inc(&conn->devref); |
| 806 | } |
| 807 | EXPORT_SYMBOL(hci_conn_hold_device); |
| 808 | |
| 809 | void hci_conn_put_device(struct hci_conn *conn) |
| 810 | { |
| 811 | if (atomic_dec_and_test(&conn->devref)) |
| 812 | hci_conn_del_sysfs(conn); |
| 813 | } |
| 814 | EXPORT_SYMBOL(hci_conn_put_device); |
| 815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | int hci_get_conn_list(void __user *arg) |
| 817 | { |
| 818 | struct hci_conn_list_req req, *cl; |
| 819 | struct hci_conn_info *ci; |
| 820 | struct hci_dev *hdev; |
| 821 | struct list_head *p; |
| 822 | int n = 0, size, err; |
| 823 | |
| 824 | if (copy_from_user(&req, arg, sizeof(req))) |
| 825 | return -EFAULT; |
| 826 | |
| 827 | if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci)) |
| 828 | return -EINVAL; |
| 829 | |
| 830 | size = sizeof(req) + req.conn_num * sizeof(*ci); |
| 831 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 832 | cl = kmalloc(size, GFP_KERNEL); |
| 833 | if (!cl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | return -ENOMEM; |
| 835 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 836 | hdev = hci_dev_get(req.dev_id); |
| 837 | if (!hdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | kfree(cl); |
| 839 | return -ENODEV; |
| 840 | } |
| 841 | |
| 842 | ci = cl->conn_info; |
| 843 | |
| 844 | hci_dev_lock_bh(hdev); |
| 845 | list_for_each(p, &hdev->conn_hash.list) { |
| 846 | register struct hci_conn *c; |
| 847 | c = list_entry(p, struct hci_conn, list); |
| 848 | |
| 849 | bacpy(&(ci + n)->bdaddr, &c->dst); |
| 850 | (ci + n)->handle = c->handle; |
| 851 | (ci + n)->type = c->type; |
| 852 | (ci + n)->out = c->out; |
| 853 | (ci + n)->state = c->state; |
| 854 | (ci + n)->link_mode = c->link_mode; |
Nick Pelly | c172849 | 2009-12-09 00:15:41 -0800 | [diff] [blame] | 855 | if (c->type == SCO_LINK) { |
| 856 | (ci + n)->mtu = hdev->sco_mtu; |
| 857 | (ci + n)->cnt = hdev->sco_cnt; |
| 858 | (ci + n)->pkts = hdev->sco_pkts; |
| 859 | } else { |
| 860 | (ci + n)->mtu = hdev->acl_mtu; |
| 861 | (ci + n)->cnt = hdev->acl_cnt; |
| 862 | (ci + n)->pkts = hdev->acl_pkts; |
| 863 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | if (++n >= req.conn_num) |
| 865 | break; |
| 866 | } |
| 867 | hci_dev_unlock_bh(hdev); |
| 868 | |
| 869 | cl->dev_id = hdev->id; |
| 870 | cl->conn_num = n; |
| 871 | size = sizeof(req) + n * sizeof(*ci); |
| 872 | |
| 873 | hci_dev_put(hdev); |
| 874 | |
| 875 | err = copy_to_user(arg, cl, size); |
| 876 | kfree(cl); |
| 877 | |
| 878 | return err ? -EFAULT : 0; |
| 879 | } |
| 880 | |
| 881 | int hci_get_conn_info(struct hci_dev *hdev, void __user *arg) |
| 882 | { |
| 883 | struct hci_conn_info_req req; |
| 884 | struct hci_conn_info ci; |
| 885 | struct hci_conn *conn; |
| 886 | char __user *ptr = arg + sizeof(req); |
| 887 | |
| 888 | if (copy_from_user(&req, arg, sizeof(req))) |
| 889 | return -EFAULT; |
| 890 | |
| 891 | hci_dev_lock_bh(hdev); |
| 892 | conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr); |
| 893 | if (conn) { |
| 894 | bacpy(&ci.bdaddr, &conn->dst); |
| 895 | ci.handle = conn->handle; |
| 896 | ci.type = conn->type; |
| 897 | ci.out = conn->out; |
| 898 | ci.state = conn->state; |
| 899 | ci.link_mode = conn->link_mode; |
Nick Pelly | c172849 | 2009-12-09 00:15:41 -0800 | [diff] [blame] | 900 | if (req.type == SCO_LINK) { |
| 901 | ci.mtu = hdev->sco_mtu; |
| 902 | ci.cnt = hdev->sco_cnt; |
| 903 | ci.pkts = hdev->sco_pkts; |
| 904 | } else { |
| 905 | ci.mtu = hdev->acl_mtu; |
| 906 | ci.cnt = hdev->acl_cnt; |
| 907 | ci.pkts = hdev->acl_pkts; |
| 908 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | } |
| 910 | hci_dev_unlock_bh(hdev); |
| 911 | |
| 912 | if (!conn) |
| 913 | return -ENOENT; |
| 914 | |
| 915 | return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0; |
| 916 | } |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 917 | |
| 918 | int hci_get_auth_info(struct hci_dev *hdev, void __user *arg) |
| 919 | { |
| 920 | struct hci_auth_info_req req; |
| 921 | struct hci_conn *conn; |
| 922 | |
| 923 | if (copy_from_user(&req, arg, sizeof(req))) |
| 924 | return -EFAULT; |
| 925 | |
| 926 | hci_dev_lock_bh(hdev); |
| 927 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); |
| 928 | if (conn) |
| 929 | req.type = conn->auth_type; |
| 930 | hci_dev_unlock_bh(hdev); |
| 931 | |
| 932 | if (!conn) |
| 933 | return -ENOENT; |
| 934 | |
| 935 | return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; |
| 936 | } |