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 | 6d3ce0e7 | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst) |
| 287 | { |
| 288 | struct hci_conn *conn; |
| 289 | |
| 290 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); |
| 291 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 292 | conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC); |
| 293 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
| 296 | bacpy(&conn->dst, dst); |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 297 | conn->hdev = hdev; |
| 298 | conn->type = type; |
| 299 | conn->mode = HCI_CM_ACTIVE; |
| 300 | conn->state = BT_OPEN; |
Andrei Emeltchenko | 93f19c9 | 2009-09-03 12:34:19 +0300 | [diff] [blame] | 301 | conn->auth_type = HCI_AT_GENERAL_BONDING; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 302 | conn->io_capability = hdev->io_capability; |
Johan Hedberg | a958355 | 2011-02-19 12:06:01 -0300 | [diff] [blame] | 303 | conn->remote_auth = 0xff; |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 304 | conn->key_type = 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 306 | conn->power_save = 1; |
Marcel Holtmann | 052b30b | 2009-04-26 20:01:22 +0200 | [diff] [blame] | 307 | conn->disc_timeout = HCI_DISCONN_TIMEOUT; |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 308 | |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 309 | switch (type) { |
| 310 | case ACL_LINK: |
| 311 | conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; |
| 312 | break; |
| 313 | case SCO_LINK: |
| 314 | if (lmp_esco_capable(hdev)) |
Marcel Holtmann | efc7688 | 2009-02-06 09:13:37 +0100 | [diff] [blame] | 315 | conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) | |
| 316 | (hdev->esco_type & EDR_ESCO_MASK); |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 317 | else |
| 318 | conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK; |
| 319 | break; |
| 320 | case ESCO_LINK: |
Marcel Holtmann | efc7688 | 2009-02-06 09:13:37 +0100 | [diff] [blame] | 321 | conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK; |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | skb_queue_head_init(&conn->data_q); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 326 | |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 327 | setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn); |
| 328 | setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn); |
Johan Hedberg | 9f61656 | 2011-04-28 11:28:54 -0700 | [diff] [blame] | 329 | setup_timer(&conn->auto_accept_timer, hci_conn_auto_accept, |
| 330 | (unsigned long) conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
| 332 | atomic_set(&conn->refcnt, 0); |
| 333 | |
| 334 | hci_dev_hold(hdev); |
| 335 | |
| 336 | tasklet_disable(&hdev->tx_task); |
| 337 | |
| 338 | hci_conn_hash_add(hdev, conn); |
| 339 | if (hdev->notify) |
| 340 | hdev->notify(hdev, HCI_NOTIFY_CONN_ADD); |
| 341 | |
Marcel Holtmann | 9eba32b | 2009-08-22 14:19:26 -0700 | [diff] [blame] | 342 | atomic_set(&conn->devref, 0); |
| 343 | |
Marcel Holtmann | a67e899 | 2009-05-02 18:24:06 -0700 | [diff] [blame] | 344 | hci_conn_init_sysfs(conn); |
| 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | tasklet_enable(&hdev->tx_task); |
| 347 | |
| 348 | return conn; |
| 349 | } |
| 350 | |
| 351 | int hci_conn_del(struct hci_conn *conn) |
| 352 | { |
| 353 | struct hci_dev *hdev = conn->hdev; |
| 354 | |
| 355 | BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle); |
| 356 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 357 | del_timer(&conn->idle_timer); |
| 358 | |
| 359 | del_timer(&conn->disc_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
Johan Hedberg | 9f61656 | 2011-04-28 11:28:54 -0700 | [diff] [blame] | 361 | del_timer(&conn->auto_accept_timer); |
| 362 | |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 363 | if (conn->type == ACL_LINK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | struct hci_conn *sco = conn->link; |
| 365 | if (sco) |
| 366 | sco->link = NULL; |
| 367 | |
| 368 | /* Unacked frames */ |
| 369 | hdev->acl_cnt += conn->sent; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 370 | } else if (conn->type == LE_LINK) { |
| 371 | if (hdev->le_pkts) |
| 372 | hdev->le_cnt += conn->sent; |
| 373 | else |
| 374 | hdev->acl_cnt += conn->sent; |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 375 | } else { |
| 376 | struct hci_conn *acl = conn->link; |
| 377 | if (acl) { |
| 378 | acl->link = NULL; |
| 379 | hci_conn_put(acl); |
| 380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | tasklet_disable(&hdev->tx_task); |
Marcel Holtmann | 7d0db0a | 2008-07-14 20:13:51 +0200 | [diff] [blame] | 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | hci_conn_hash_del(hdev, conn); |
| 386 | if (hdev->notify) |
| 387 | hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); |
Marcel Holtmann | 7d0db0a | 2008-07-14 20:13:51 +0200 | [diff] [blame] | 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | tasklet_enable(&hdev->tx_task); |
Marcel Holtmann | 7d0db0a | 2008-07-14 20:13:51 +0200 | [diff] [blame] | 390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | skb_queue_purge(&conn->data_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Marcel Holtmann | 9eba32b | 2009-08-22 14:19:26 -0700 | [diff] [blame] | 393 | hci_conn_put_device(conn); |
Dave Young | 2ae9a6b | 2009-02-21 16:13:34 +0800 | [diff] [blame] | 394 | |
Marcel Holtmann | 384943e | 2009-05-08 18:20:43 -0700 | [diff] [blame] | 395 | hci_dev_put(hdev); |
| 396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src) |
| 401 | { |
| 402 | int use_src = bacmp(src, BDADDR_ANY); |
| 403 | struct hci_dev *hdev = NULL; |
| 404 | struct list_head *p; |
| 405 | |
| 406 | BT_DBG("%s -> %s", batostr(src), batostr(dst)); |
| 407 | |
| 408 | read_lock_bh(&hci_dev_list_lock); |
| 409 | |
| 410 | list_for_each(p, &hci_dev_list) { |
| 411 | struct hci_dev *d = list_entry(p, struct hci_dev, list); |
| 412 | |
| 413 | if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags)) |
| 414 | continue; |
| 415 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 416 | /* Simple routing: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | * No source address - find interface with bdaddr != dst |
| 418 | * Source address - find interface with bdaddr == src |
| 419 | */ |
| 420 | |
| 421 | if (use_src) { |
| 422 | if (!bacmp(&d->bdaddr, src)) { |
| 423 | hdev = d; break; |
| 424 | } |
| 425 | } else { |
| 426 | if (bacmp(&d->bdaddr, dst)) { |
| 427 | hdev = d; break; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (hdev) |
| 433 | hdev = hci_dev_hold(hdev); |
| 434 | |
| 435 | read_unlock_bh(&hci_dev_list_lock); |
| 436 | return hdev; |
| 437 | } |
| 438 | EXPORT_SYMBOL(hci_get_route); |
| 439 | |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 440 | /* Create SCO, ACL or LE connection. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | * Device _must_ be locked */ |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 442 | struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, __u8 sec_level, __u8 auth_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
| 444 | struct hci_conn *acl; |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 445 | struct hci_conn *sco; |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 446 | struct hci_conn *le; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
| 448 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); |
| 449 | |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 450 | if (type == LE_LINK) { |
Andre Guedes | eda42b5 | 2011-05-31 14:20:56 -0300 | [diff] [blame] | 451 | struct adv_entry *entry; |
| 452 | |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 453 | le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst); |
Anderson Briglia | 15c4794 | 2011-02-21 15:09:23 -0300 | [diff] [blame] | 454 | if (le) |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 455 | return ERR_PTR(-EBUSY); |
Andre Guedes | eda42b5 | 2011-05-31 14:20:56 -0300 | [diff] [blame] | 456 | |
| 457 | entry = hci_find_adv_entry(hdev, dst); |
| 458 | if (!entry) |
| 459 | return ERR_PTR(-EHOSTUNREACH); |
| 460 | |
Anderson Briglia | 15c4794 | 2011-02-21 15:09:23 -0300 | [diff] [blame] | 461 | le = hci_conn_add(hdev, LE_LINK, dst); |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 462 | if (!le) |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 463 | return ERR_PTR(-ENOMEM); |
Andre Guedes | 893d675 | 2011-05-31 14:20:55 -0300 | [diff] [blame] | 464 | |
Andre Guedes | eda42b5 | 2011-05-31 14:20:56 -0300 | [diff] [blame] | 465 | le->dst_type = entry->bdaddr_type; |
| 466 | |
Andre Guedes | 893d675 | 2011-05-31 14:20:55 -0300 | [diff] [blame] | 467 | hci_le_connect(le); |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 468 | |
| 469 | hci_conn_hold(le); |
| 470 | |
| 471 | return le; |
| 472 | } |
| 473 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 474 | acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); |
| 475 | if (!acl) { |
| 476 | acl = hci_conn_add(hdev, ACL_LINK, dst); |
| 477 | if (!acl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | return NULL; |
| 479 | } |
| 480 | |
| 481 | hci_conn_hold(acl); |
| 482 | |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 483 | if (acl->state == BT_OPEN || acl->state == BT_CLOSED) { |
Johan Hedberg | 765c2a9 | 2011-01-19 12:06:52 +0530 | [diff] [blame] | 484 | acl->sec_level = BT_SECURITY_LOW; |
| 485 | acl->pending_sec_level = sec_level; |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 486 | acl->auth_type = auth_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | hci_acl_connect(acl); |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 488 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 490 | if (type == ACL_LINK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | return acl; |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 492 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 493 | sco = hci_conn_hash_lookup_ba(hdev, type, dst); |
| 494 | if (!sco) { |
| 495 | sco = hci_conn_add(hdev, type, dst); |
| 496 | if (!sco) { |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 497 | hci_conn_put(acl); |
| 498 | return NULL; |
| 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 | |
| 502 | acl->link = sco; |
| 503 | sco->link = acl; |
| 504 | |
| 505 | hci_conn_hold(sco); |
| 506 | |
| 507 | if (acl->state == BT_CONNECTED && |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 508 | (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { |
Nick Pelly | c390216 | 2009-11-13 14:16:32 -0800 | [diff] [blame] | 509 | acl->power_save = 1; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 510 | hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON); |
Nick Pelly | c390216 | 2009-11-13 14:16:32 -0800 | [diff] [blame] | 511 | |
Marcel Holtmann | e73439d | 2010-07-26 10:06:00 -0400 | [diff] [blame] | 512 | if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) { |
| 513 | /* defer SCO setup until mode change completed */ |
| 514 | set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend); |
| 515 | return sco; |
| 516 | } |
| 517 | |
| 518 | hci_sco_setup(acl, 0x00); |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 519 | } |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 520 | |
| 521 | return sco; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } |
| 523 | EXPORT_SYMBOL(hci_connect); |
| 524 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 525 | /* Check link security requirement */ |
| 526 | int hci_conn_check_link_mode(struct hci_conn *conn) |
| 527 | { |
| 528 | BT_DBG("conn %p", conn); |
| 529 | |
| 530 | if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 && |
| 531 | !(conn->link_mode & HCI_LM_ENCRYPT)) |
| 532 | return 0; |
| 533 | |
| 534 | return 1; |
| 535 | } |
| 536 | EXPORT_SYMBOL(hci_conn_check_link_mode); |
| 537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | /* Authenticate remote device */ |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 539 | 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] | 540 | { |
| 541 | BT_DBG("conn %p", conn); |
| 542 | |
Johan Hedberg | 765c2a9 | 2011-01-19 12:06:52 +0530 | [diff] [blame] | 543 | if (conn->pending_sec_level > sec_level) |
| 544 | sec_level = conn->pending_sec_level; |
| 545 | |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 546 | if (sec_level > conn->sec_level) |
Johan Hedberg | 765c2a9 | 2011-01-19 12:06:52 +0530 | [diff] [blame] | 547 | conn->pending_sec_level = sec_level; |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 548 | else if (conn->link_mode & HCI_LM_AUTH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | return 1; |
| 550 | |
Johan Hedberg | 65cf686 | 2011-01-19 12:06:49 +0530 | [diff] [blame] | 551 | /* Make sure we preserve an existing MITM requirement*/ |
| 552 | auth_type |= (conn->auth_type & 0x01); |
| 553 | |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 554 | conn->auth_type = auth_type; |
| 555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) { |
| 557 | struct hci_cp_auth_requested cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 558 | cp.handle = cpu_to_le16(conn->handle); |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 559 | hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, |
| 560 | sizeof(cp), &cp); |
Waldemar Rymarkiewicz | 19f8def | 2011-05-31 15:49:25 +0200 | [diff] [blame] | 561 | if (conn->key_type != 0xff) |
| 562 | set_bit(HCI_CONN_REAUTH_PEND, &conn->pend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | return 0; |
| 566 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 568 | /* Encrypt the the link */ |
| 569 | static void hci_conn_encrypt(struct hci_conn *conn) |
| 570 | { |
| 571 | BT_DBG("conn %p", conn); |
| 572 | |
| 573 | if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) { |
| 574 | struct hci_cp_set_conn_encrypt cp; |
| 575 | cp.handle = cpu_to_le16(conn->handle); |
| 576 | cp.encrypt = 0x01; |
| 577 | hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp), |
| 578 | &cp); |
| 579 | } |
| 580 | } |
| 581 | |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 582 | /* Enable security */ |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 583 | 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] | 584 | { |
| 585 | BT_DBG("conn %p", conn); |
| 586 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 587 | /* For sdp we don't need the link key. */ |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 588 | if (sec_level == BT_SECURITY_SDP) |
| 589 | return 1; |
| 590 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 591 | /* For non 2.1 devices and low security level we don't need the link |
| 592 | key. */ |
Marcel Holtmann | 3fdca1e | 2009-04-28 09:04:55 -0700 | [diff] [blame] | 593 | if (sec_level == BT_SECURITY_LOW && |
| 594 | (!conn->ssp_mode || !conn->hdev->ssp_mode)) |
| 595 | return 1; |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 596 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 597 | /* For other security levels we need the link key. */ |
| 598 | if (!(conn->link_mode & HCI_LM_AUTH)) |
| 599 | goto auth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 601 | /* An authenticated combination key has sufficient security for any |
| 602 | security level. */ |
| 603 | if (conn->key_type == HCI_LK_AUTH_COMBINATION) |
| 604 | goto encrypt; |
| 605 | |
| 606 | /* An unauthenticated combination key has sufficient security for |
| 607 | security level 1 and 2. */ |
| 608 | if (conn->key_type == HCI_LK_UNAUTH_COMBINATION && |
| 609 | (sec_level == BT_SECURITY_MEDIUM || |
| 610 | sec_level == BT_SECURITY_LOW)) |
| 611 | goto encrypt; |
| 612 | |
| 613 | /* A combination key has always sufficient security for the security |
| 614 | levels 1 or 2. High security level requires the combination key |
| 615 | is generated using maximum PIN code length (16). |
| 616 | For pre 2.1 units. */ |
| 617 | if (conn->key_type == HCI_LK_COMBINATION && |
| 618 | (sec_level != BT_SECURITY_HIGH || |
| 619 | conn->pin_length == 16)) |
| 620 | goto encrypt; |
| 621 | |
| 622 | auth: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) |
| 624 | return 0; |
| 625 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 626 | hci_conn_auth(conn, sec_level, auth_type); |
| 627 | return 0; |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 628 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 629 | encrypt: |
| 630 | if (conn->link_mode & HCI_LM_ENCRYPT) |
| 631 | return 1; |
| 632 | |
| 633 | hci_conn_encrypt(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | return 0; |
| 635 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 636 | EXPORT_SYMBOL(hci_conn_security); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Waldemar Rymarkiewicz | b3b1b06 | 2011-05-06 09:42:31 +0200 | [diff] [blame] | 638 | /* Check secure link requirement */ |
| 639 | int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level) |
| 640 | { |
| 641 | BT_DBG("conn %p", conn); |
| 642 | |
| 643 | if (sec_level != BT_SECURITY_HIGH) |
| 644 | return 1; /* Accept if non-secure is required */ |
| 645 | |
Waldemar Rymarkiewicz | ef4177e | 2011-06-02 14:24:52 +0200 | [diff] [blame] | 646 | if (conn->sec_level == BT_SECURITY_HIGH) |
Waldemar Rymarkiewicz | b3b1b06 | 2011-05-06 09:42:31 +0200 | [diff] [blame] | 647 | return 1; |
| 648 | |
| 649 | return 0; /* Reject not secure link */ |
| 650 | } |
| 651 | EXPORT_SYMBOL(hci_conn_check_secure); |
| 652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | /* Change link key */ |
| 654 | int hci_conn_change_link_key(struct hci_conn *conn) |
| 655 | { |
| 656 | BT_DBG("conn %p", conn); |
| 657 | |
| 658 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) { |
| 659 | struct hci_cp_change_conn_link_key cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 660 | cp.handle = cpu_to_le16(conn->handle); |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 661 | hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY, |
| 662 | sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | return 0; |
| 666 | } |
| 667 | EXPORT_SYMBOL(hci_conn_change_link_key); |
| 668 | |
| 669 | /* Switch role */ |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 670 | int hci_conn_switch_role(struct hci_conn *conn, __u8 role) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | { |
| 672 | BT_DBG("conn %p", conn); |
| 673 | |
| 674 | if (!role && conn->link_mode & HCI_LM_MASTER) |
| 675 | return 1; |
| 676 | |
| 677 | if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) { |
| 678 | struct hci_cp_switch_role cp; |
| 679 | bacpy(&cp.bdaddr, &conn->dst); |
| 680 | cp.role = role; |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 681 | hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | return 0; |
| 685 | } |
| 686 | EXPORT_SYMBOL(hci_conn_switch_role); |
| 687 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 688 | /* Enter active mode */ |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 689 | 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] | 690 | { |
| 691 | struct hci_dev *hdev = conn->hdev; |
| 692 | |
| 693 | BT_DBG("conn %p mode %d", conn, conn->mode); |
| 694 | |
| 695 | if (test_bit(HCI_RAW, &hdev->flags)) |
| 696 | return; |
| 697 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 698 | if (conn->mode != HCI_CM_SNIFF) |
| 699 | goto timer; |
| 700 | |
| 701 | if (!conn->power_save && !force_active) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 702 | goto timer; |
| 703 | |
| 704 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) { |
| 705 | struct hci_cp_exit_sniff_mode cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 706 | cp.handle = cpu_to_le16(conn->handle); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 707 | hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | timer: |
| 711 | if (hdev->idle_timeout > 0) |
| 712 | mod_timer(&conn->idle_timer, |
| 713 | jiffies + msecs_to_jiffies(hdev->idle_timeout)); |
| 714 | } |
| 715 | |
| 716 | /* Enter sniff mode */ |
| 717 | void hci_conn_enter_sniff_mode(struct hci_conn *conn) |
| 718 | { |
| 719 | struct hci_dev *hdev = conn->hdev; |
| 720 | |
| 721 | BT_DBG("conn %p mode %d", conn, conn->mode); |
| 722 | |
| 723 | if (test_bit(HCI_RAW, &hdev->flags)) |
| 724 | return; |
| 725 | |
| 726 | if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn)) |
| 727 | return; |
| 728 | |
| 729 | if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF)) |
| 730 | return; |
| 731 | |
| 732 | if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) { |
| 733 | struct hci_cp_sniff_subrate cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 734 | cp.handle = cpu_to_le16(conn->handle); |
| 735 | cp.max_latency = cpu_to_le16(0); |
| 736 | cp.min_remote_timeout = cpu_to_le16(0); |
| 737 | cp.min_local_timeout = cpu_to_le16(0); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 738 | hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) { |
| 742 | struct hci_cp_sniff_mode cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 743 | cp.handle = cpu_to_le16(conn->handle); |
| 744 | cp.max_interval = cpu_to_le16(hdev->sniff_max_interval); |
| 745 | cp.min_interval = cpu_to_le16(hdev->sniff_min_interval); |
| 746 | cp.attempt = cpu_to_le16(4); |
| 747 | cp.timeout = cpu_to_le16(1); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 748 | hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | /* Drop all connection on the device */ |
| 753 | void hci_conn_hash_flush(struct hci_dev *hdev) |
| 754 | { |
| 755 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 756 | struct list_head *p; |
| 757 | |
| 758 | BT_DBG("hdev %s", hdev->name); |
| 759 | |
| 760 | p = h->list.next; |
| 761 | while (p != &h->list) { |
| 762 | struct hci_conn *c; |
| 763 | |
| 764 | c = list_entry(p, struct hci_conn, list); |
| 765 | p = p->next; |
| 766 | |
| 767 | c->state = BT_CLOSED; |
| 768 | |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 769 | hci_proto_disconn_cfm(c, 0x16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | hci_conn_del(c); |
| 771 | } |
| 772 | } |
| 773 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 774 | /* Check pending connect attempts */ |
| 775 | void hci_conn_check_pending(struct hci_dev *hdev) |
| 776 | { |
| 777 | struct hci_conn *conn; |
| 778 | |
| 779 | BT_DBG("hdev %s", hdev->name); |
| 780 | |
| 781 | hci_dev_lock(hdev); |
| 782 | |
| 783 | conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2); |
| 784 | if (conn) |
| 785 | hci_acl_connect(conn); |
| 786 | |
| 787 | hci_dev_unlock(hdev); |
| 788 | } |
| 789 | |
Marcel Holtmann | 9eba32b | 2009-08-22 14:19:26 -0700 | [diff] [blame] | 790 | void hci_conn_hold_device(struct hci_conn *conn) |
| 791 | { |
| 792 | atomic_inc(&conn->devref); |
| 793 | } |
| 794 | EXPORT_SYMBOL(hci_conn_hold_device); |
| 795 | |
| 796 | void hci_conn_put_device(struct hci_conn *conn) |
| 797 | { |
| 798 | if (atomic_dec_and_test(&conn->devref)) |
| 799 | hci_conn_del_sysfs(conn); |
| 800 | } |
| 801 | EXPORT_SYMBOL(hci_conn_put_device); |
| 802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | int hci_get_conn_list(void __user *arg) |
| 804 | { |
| 805 | struct hci_conn_list_req req, *cl; |
| 806 | struct hci_conn_info *ci; |
| 807 | struct hci_dev *hdev; |
| 808 | struct list_head *p; |
| 809 | int n = 0, size, err; |
| 810 | |
| 811 | if (copy_from_user(&req, arg, sizeof(req))) |
| 812 | return -EFAULT; |
| 813 | |
| 814 | if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci)) |
| 815 | return -EINVAL; |
| 816 | |
| 817 | size = sizeof(req) + req.conn_num * sizeof(*ci); |
| 818 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 819 | cl = kmalloc(size, GFP_KERNEL); |
| 820 | if (!cl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | return -ENOMEM; |
| 822 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 823 | hdev = hci_dev_get(req.dev_id); |
| 824 | if (!hdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | kfree(cl); |
| 826 | return -ENODEV; |
| 827 | } |
| 828 | |
| 829 | ci = cl->conn_info; |
| 830 | |
| 831 | hci_dev_lock_bh(hdev); |
| 832 | list_for_each(p, &hdev->conn_hash.list) { |
| 833 | register struct hci_conn *c; |
| 834 | c = list_entry(p, struct hci_conn, list); |
| 835 | |
| 836 | bacpy(&(ci + n)->bdaddr, &c->dst); |
| 837 | (ci + n)->handle = c->handle; |
| 838 | (ci + n)->type = c->type; |
| 839 | (ci + n)->out = c->out; |
| 840 | (ci + n)->state = c->state; |
| 841 | (ci + n)->link_mode = c->link_mode; |
| 842 | if (++n >= req.conn_num) |
| 843 | break; |
| 844 | } |
| 845 | hci_dev_unlock_bh(hdev); |
| 846 | |
| 847 | cl->dev_id = hdev->id; |
| 848 | cl->conn_num = n; |
| 849 | size = sizeof(req) + n * sizeof(*ci); |
| 850 | |
| 851 | hci_dev_put(hdev); |
| 852 | |
| 853 | err = copy_to_user(arg, cl, size); |
| 854 | kfree(cl); |
| 855 | |
| 856 | return err ? -EFAULT : 0; |
| 857 | } |
| 858 | |
| 859 | int hci_get_conn_info(struct hci_dev *hdev, void __user *arg) |
| 860 | { |
| 861 | struct hci_conn_info_req req; |
| 862 | struct hci_conn_info ci; |
| 863 | struct hci_conn *conn; |
| 864 | char __user *ptr = arg + sizeof(req); |
| 865 | |
| 866 | if (copy_from_user(&req, arg, sizeof(req))) |
| 867 | return -EFAULT; |
| 868 | |
| 869 | hci_dev_lock_bh(hdev); |
| 870 | conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr); |
| 871 | if (conn) { |
| 872 | bacpy(&ci.bdaddr, &conn->dst); |
| 873 | ci.handle = conn->handle; |
| 874 | ci.type = conn->type; |
| 875 | ci.out = conn->out; |
| 876 | ci.state = conn->state; |
| 877 | ci.link_mode = conn->link_mode; |
| 878 | } |
| 879 | hci_dev_unlock_bh(hdev); |
| 880 | |
| 881 | if (!conn) |
| 882 | return -ENOENT; |
| 883 | |
| 884 | return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0; |
| 885 | } |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 886 | |
| 887 | int hci_get_auth_info(struct hci_dev *hdev, void __user *arg) |
| 888 | { |
| 889 | struct hci_auth_info_req req; |
| 890 | struct hci_conn *conn; |
| 891 | |
| 892 | if (copy_from_user(&req, arg, sizeof(req))) |
| 893 | return -EFAULT; |
| 894 | |
| 895 | hci_dev_lock_bh(hdev); |
| 896 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); |
| 897 | if (conn) |
| 898 | req.type = conn->auth_type; |
| 899 | hci_dev_unlock_bh(hdev); |
| 900 | |
| 901 | if (!conn) |
| 902 | return -ENOENT; |
| 903 | |
| 904 | return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; |
| 905 | } |