Commit | Line | Data |
---|---|---|
8e87d142 | 1 | /* |
1da177e4 | 2 | BlueZ - Bluetooth protocol stack for Linux |
2d0a0346 | 3 | Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved. |
1da177e4 LT |
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 | |
8e87d142 YH |
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 | |
1da177e4 LT |
18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
19 | ||
8e87d142 YH |
20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS | |
1da177e4 LT |
22 | SOFTWARE IS DISCLAIMED. |
23 | */ | |
24 | ||
25 | /* Bluetooth HCI connection handling. */ | |
26 | ||
8c520a59 | 27 | #include <linux/export.h> |
1da177e4 LT |
28 | |
29 | #include <net/bluetooth/bluetooth.h> | |
30 | #include <net/bluetooth/hci_core.h> | |
4bc58f51 | 31 | #include <net/bluetooth/l2cap.h> |
1da177e4 | 32 | |
ac4b7236 | 33 | #include "smp.h" |
7024728e MH |
34 | #include "a2mp.h" |
35 | ||
2dea632f FD |
36 | struct sco_param { |
37 | u16 pkt_type; | |
38 | u16 max_latency; | |
39 | }; | |
40 | ||
41 | static const struct sco_param sco_param_cvsd[] = { | |
42 | { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a }, /* S3 */ | |
43 | { EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007 }, /* S2 */ | |
44 | { EDR_ESCO_MASK | ESCO_EV3, 0x0007 }, /* S1 */ | |
45 | { EDR_ESCO_MASK | ESCO_HV3, 0xffff }, /* D1 */ | |
46 | { EDR_ESCO_MASK | ESCO_HV1, 0xffff }, /* D0 */ | |
47 | }; | |
48 | ||
49 | static const struct sco_param sco_param_wideband[] = { | |
50 | { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d }, /* T2 */ | |
51 | { EDR_ESCO_MASK | ESCO_EV3, 0x0008 }, /* T1 */ | |
52 | }; | |
53 | ||
1aef8669 | 54 | static void hci_le_create_connection_cancel(struct hci_conn *conn) |
fcd89c09 VT |
55 | { |
56 | hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL); | |
57 | } | |
58 | ||
1aef8669 | 59 | static void hci_acl_create_connection(struct hci_conn *conn) |
1da177e4 LT |
60 | { |
61 | struct hci_dev *hdev = conn->hdev; | |
62 | struct inquiry_entry *ie; | |
63 | struct hci_cp_create_conn cp; | |
64 | ||
42d2d87c | 65 | BT_DBG("hcon %p", conn); |
1da177e4 LT |
66 | |
67 | conn->state = BT_CONNECT; | |
a0c808b3 | 68 | conn->out = true; |
40bef302 | 69 | conn->role = HCI_ROLE_MASTER; |
1da177e4 | 70 | |
4c67bc74 MH |
71 | conn->attempt++; |
72 | ||
e4e8e37c MH |
73 | conn->link_policy = hdev->link_policy; |
74 | ||
1da177e4 LT |
75 | memset(&cp, 0, sizeof(cp)); |
76 | bacpy(&cp.bdaddr, &conn->dst); | |
77 | cp.pscan_rep_mode = 0x02; | |
78 | ||
70f23020 AE |
79 | ie = hci_inquiry_cache_lookup(hdev, &conn->dst); |
80 | if (ie) { | |
41a96212 MH |
81 | if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) { |
82 | cp.pscan_rep_mode = ie->data.pscan_rep_mode; | |
83 | cp.pscan_mode = ie->data.pscan_mode; | |
84 | cp.clock_offset = ie->data.clock_offset | | |
dcf4adbf | 85 | cpu_to_le16(0x8000); |
41a96212 MH |
86 | } |
87 | ||
1da177e4 | 88 | memcpy(conn->dev_class, ie->data.dev_class, 3); |
58a681ef JH |
89 | if (ie->data.ssp_mode > 0) |
90 | set_bit(HCI_CONN_SSP_ENABLED, &conn->flags); | |
1da177e4 LT |
91 | } |
92 | ||
a8746417 | 93 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
1da177e4 | 94 | if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER)) |
b6a0dc82 | 95 | cp.role_switch = 0x01; |
1da177e4 | 96 | else |
b6a0dc82 | 97 | cp.role_switch = 0x00; |
4c67bc74 | 98 | |
a9de9248 | 99 | hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp); |
1da177e4 LT |
100 | } |
101 | ||
1aef8669 | 102 | static void hci_acl_create_connection_cancel(struct hci_conn *conn) |
6ac59344 MH |
103 | { |
104 | struct hci_cp_create_conn_cancel cp; | |
105 | ||
38b3fef1 | 106 | BT_DBG("hcon %p", conn); |
6ac59344 | 107 | |
d095c1eb | 108 | if (conn->hdev->hci_ver < BLUETOOTH_VER_1_2) |
6ac59344 MH |
109 | return; |
110 | ||
111 | bacpy(&cp.bdaddr, &conn->dst); | |
a9de9248 | 112 | hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp); |
6ac59344 MH |
113 | } |
114 | ||
93796fa6 CT |
115 | static void hci_reject_sco(struct hci_conn *conn) |
116 | { | |
117 | struct hci_cp_reject_sync_conn_req cp; | |
118 | ||
119 | cp.reason = HCI_ERROR_REMOTE_USER_TERM; | |
120 | bacpy(&cp.bdaddr, &conn->dst); | |
121 | ||
122 | hci_send_cmd(conn->hdev, HCI_OP_REJECT_SYNC_CONN_REQ, sizeof(cp), &cp); | |
123 | } | |
124 | ||
bed71748 | 125 | void hci_disconnect(struct hci_conn *conn, __u8 reason) |
1da177e4 LT |
126 | { |
127 | struct hci_cp_disconnect cp; | |
128 | ||
38b3fef1 | 129 | BT_DBG("hcon %p", conn); |
1da177e4 LT |
130 | |
131 | conn->state = BT_DISCONN; | |
132 | ||
aca3192c | 133 | cp.handle = cpu_to_le16(conn->handle); |
1da177e4 | 134 | cp.reason = reason; |
a9de9248 | 135 | hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp); |
1da177e4 LT |
136 | } |
137 | ||
a2b1976b | 138 | static void hci_amp_disconn(struct hci_conn *conn) |
53502d69 AE |
139 | { |
140 | struct hci_cp_disconn_phy_link cp; | |
141 | ||
142 | BT_DBG("hcon %p", conn); | |
143 | ||
144 | conn->state = BT_DISCONN; | |
145 | ||
146 | cp.phy_handle = HCI_PHY_HANDLE(conn->handle); | |
a2b1976b | 147 | cp.reason = hci_proto_disconn_ind(conn); |
53502d69 AE |
148 | hci_send_cmd(conn->hdev, HCI_OP_DISCONN_PHY_LINK, |
149 | sizeof(cp), &cp); | |
150 | } | |
151 | ||
57f5d0d1 | 152 | static void hci_add_sco(struct hci_conn *conn, __u16 handle) |
1da177e4 LT |
153 | { |
154 | struct hci_dev *hdev = conn->hdev; | |
155 | struct hci_cp_add_sco cp; | |
156 | ||
38b3fef1 | 157 | BT_DBG("hcon %p", conn); |
1da177e4 LT |
158 | |
159 | conn->state = BT_CONNECT; | |
a0c808b3 | 160 | conn->out = true; |
1da177e4 | 161 | |
efc7688b MH |
162 | conn->attempt++; |
163 | ||
aca3192c | 164 | cp.handle = cpu_to_le16(handle); |
a8746417 | 165 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
1da177e4 | 166 | |
a9de9248 | 167 | hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); |
1da177e4 LT |
168 | } |
169 | ||
2dea632f | 170 | bool hci_setup_sync(struct hci_conn *conn, __u16 handle) |
b6a0dc82 MH |
171 | { |
172 | struct hci_dev *hdev = conn->hdev; | |
173 | struct hci_cp_setup_sync_conn cp; | |
2dea632f | 174 | const struct sco_param *param; |
b6a0dc82 | 175 | |
38b3fef1 | 176 | BT_DBG("hcon %p", conn); |
b6a0dc82 MH |
177 | |
178 | conn->state = BT_CONNECT; | |
a0c808b3 | 179 | conn->out = true; |
b6a0dc82 | 180 | |
efc7688b MH |
181 | conn->attempt++; |
182 | ||
b6a0dc82 | 183 | cp.handle = cpu_to_le16(handle); |
b6a0dc82 | 184 | |
dcf4adbf JP |
185 | cp.tx_bandwidth = cpu_to_le32(0x00001f40); |
186 | cp.rx_bandwidth = cpu_to_le32(0x00001f40); | |
10c62ddc FD |
187 | cp.voice_setting = cpu_to_le16(conn->setting); |
188 | ||
189 | switch (conn->setting & SCO_AIRMODE_MASK) { | |
190 | case SCO_AIRMODE_TRANSP: | |
2dea632f FD |
191 | if (conn->attempt > ARRAY_SIZE(sco_param_wideband)) |
192 | return false; | |
10c62ddc | 193 | cp.retrans_effort = 0x02; |
2dea632f | 194 | param = &sco_param_wideband[conn->attempt - 1]; |
10c62ddc FD |
195 | break; |
196 | case SCO_AIRMODE_CVSD: | |
2dea632f FD |
197 | if (conn->attempt > ARRAY_SIZE(sco_param_cvsd)) |
198 | return false; | |
199 | cp.retrans_effort = 0x01; | |
200 | param = &sco_param_cvsd[conn->attempt - 1]; | |
10c62ddc | 201 | break; |
2dea632f FD |
202 | default: |
203 | return false; | |
10c62ddc | 204 | } |
b6a0dc82 | 205 | |
2dea632f FD |
206 | cp.pkt_type = __cpu_to_le16(param->pkt_type); |
207 | cp.max_latency = __cpu_to_le16(param->max_latency); | |
208 | ||
209 | if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0) | |
210 | return false; | |
211 | ||
212 | return true; | |
b6a0dc82 MH |
213 | } |
214 | ||
7d6ca693 JH |
215 | u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency, |
216 | u16 to_multiplier) | |
2ce603eb | 217 | { |
2ce603eb | 218 | struct hci_dev *hdev = conn->hdev; |
f044eb05 MH |
219 | struct hci_conn_params *params; |
220 | struct hci_cp_le_conn_update cp; | |
2ce603eb | 221 | |
f044eb05 | 222 | hci_dev_lock(hdev); |
2ce603eb | 223 | |
f044eb05 MH |
224 | params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); |
225 | if (params) { | |
226 | params->conn_min_interval = min; | |
227 | params->conn_max_interval = max; | |
228 | params->conn_latency = latency; | |
229 | params->supervision_timeout = to_multiplier; | |
230 | } | |
231 | ||
232 | hci_dev_unlock(hdev); | |
233 | ||
234 | memset(&cp, 0, sizeof(cp)); | |
2ce603eb CT |
235 | cp.handle = cpu_to_le16(conn->handle); |
236 | cp.conn_interval_min = cpu_to_le16(min); | |
237 | cp.conn_interval_max = cpu_to_le16(max); | |
238 | cp.conn_latency = cpu_to_le16(latency); | |
239 | cp.supervision_timeout = cpu_to_le16(to_multiplier); | |
dcf4adbf JP |
240 | cp.min_ce_len = cpu_to_le16(0x0000); |
241 | cp.max_ce_len = cpu_to_le16(0x0000); | |
2ce603eb CT |
242 | |
243 | hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp); | |
7d6ca693 JH |
244 | |
245 | if (params) | |
246 | return 0x01; | |
247 | ||
248 | return 0x00; | |
2ce603eb | 249 | } |
2ce603eb | 250 | |
fe39c7b2 | 251 | void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand, |
5974e4c4 | 252 | __u8 ltk[16]) |
a7a595f6 VCG |
253 | { |
254 | struct hci_dev *hdev = conn->hdev; | |
255 | struct hci_cp_le_start_enc cp; | |
256 | ||
38b3fef1 | 257 | BT_DBG("hcon %p", conn); |
a7a595f6 VCG |
258 | |
259 | memset(&cp, 0, sizeof(cp)); | |
260 | ||
261 | cp.handle = cpu_to_le16(conn->handle); | |
fe39c7b2 | 262 | cp.rand = rand; |
a7a595f6 | 263 | cp.ediv = ediv; |
fe39c7b2 | 264 | memcpy(cp.ltk, ltk, sizeof(cp.ltk)); |
a7a595f6 VCG |
265 | |
266 | hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp); | |
267 | } | |
a7a595f6 | 268 | |
e73439d8 MH |
269 | /* Device _must_ be locked */ |
270 | void hci_sco_setup(struct hci_conn *conn, __u8 status) | |
271 | { | |
272 | struct hci_conn *sco = conn->link; | |
273 | ||
e73439d8 MH |
274 | if (!sco) |
275 | return; | |
276 | ||
38b3fef1 AE |
277 | BT_DBG("hcon %p", conn); |
278 | ||
e73439d8 MH |
279 | if (!status) { |
280 | if (lmp_esco_capable(conn->hdev)) | |
281 | hci_setup_sync(sco, conn->handle); | |
282 | else | |
283 | hci_add_sco(sco, conn->handle); | |
284 | } else { | |
285 | hci_proto_connect_cfm(sco, status); | |
286 | hci_conn_del(sco); | |
287 | } | |
288 | } | |
289 | ||
19c40e3b | 290 | static void hci_conn_timeout(struct work_struct *work) |
1da177e4 | 291 | { |
19c40e3b | 292 | struct hci_conn *conn = container_of(work, struct hci_conn, |
5974e4c4 | 293 | disc_work.work); |
1d56dc4f | 294 | int refcnt = atomic_read(&conn->refcnt); |
1da177e4 | 295 | |
38b3fef1 | 296 | BT_DBG("hcon %p state %s", conn, state_to_string(conn->state)); |
1da177e4 | 297 | |
1d56dc4f LR |
298 | WARN_ON(refcnt < 0); |
299 | ||
300 | /* FIXME: It was observed that in pairing failed scenario, refcnt | |
301 | * drops below 0. Probably this is because l2cap_conn_del calls | |
302 | * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is | |
303 | * dropped. After that loop hci_chan_del is called which also drops | |
304 | * conn. For now make sure that ACL is alive if refcnt is higher then 0, | |
305 | * otherwise drop it. | |
306 | */ | |
307 | if (refcnt > 0) | |
1da177e4 LT |
308 | return; |
309 | ||
6ac59344 MH |
310 | switch (conn->state) { |
311 | case BT_CONNECT: | |
769be974 | 312 | case BT_CONNECT2: |
fcd89c09 VT |
313 | if (conn->out) { |
314 | if (conn->type == ACL_LINK) | |
1aef8669 | 315 | hci_acl_create_connection_cancel(conn); |
fcd89c09 | 316 | else if (conn->type == LE_LINK) |
1aef8669 | 317 | hci_le_create_connection_cancel(conn); |
93796fa6 CT |
318 | } else if (conn->type == SCO_LINK || conn->type == ESCO_LINK) { |
319 | hci_reject_sco(conn); | |
fcd89c09 | 320 | } |
6ac59344 | 321 | break; |
769be974 | 322 | case BT_CONFIG: |
8e87d142 | 323 | case BT_CONNECTED: |
40051e46 MH |
324 | if (conn->type == AMP_LINK) { |
325 | hci_amp_disconn(conn); | |
326 | } else { | |
327 | __u8 reason = hci_proto_disconn_ind(conn); | |
df935429 MH |
328 | |
329 | /* When we are master of an established connection | |
330 | * and it enters the disconnect timeout, then go | |
331 | * ahead and try to read the current clock offset. | |
332 | * | |
333 | * Processing of the result is done within the | |
334 | * event handling and hci_clock_offset_evt function. | |
335 | */ | |
336 | if (conn->type == ACL_LINK && | |
40bef302 | 337 | conn->role == HCI_ROLE_MASTER) { |
df935429 MH |
338 | struct hci_dev *hdev = conn->hdev; |
339 | struct hci_cp_read_clock_offset cp; | |
340 | ||
341 | cp.handle = cpu_to_le16(conn->handle); | |
342 | ||
343 | hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, | |
344 | sizeof(cp), &cp); | |
345 | } | |
346 | ||
40051e46 MH |
347 | hci_disconnect(conn, reason); |
348 | } | |
6ac59344 MH |
349 | break; |
350 | default: | |
1da177e4 | 351 | conn->state = BT_CLOSED; |
6ac59344 MH |
352 | break; |
353 | } | |
1da177e4 LT |
354 | } |
355 | ||
416dc94b | 356 | /* Enter sniff mode */ |
a74a84f6 | 357 | static void hci_conn_idle(struct work_struct *work) |
416dc94b | 358 | { |
a74a84f6 JH |
359 | struct hci_conn *conn = container_of(work, struct hci_conn, |
360 | idle_work.work); | |
416dc94b GP |
361 | struct hci_dev *hdev = conn->hdev; |
362 | ||
38b3fef1 | 363 | BT_DBG("hcon %p mode %d", conn, conn->mode); |
416dc94b | 364 | |
416dc94b GP |
365 | if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn)) |
366 | return; | |
367 | ||
368 | if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF)) | |
369 | return; | |
370 | ||
371 | if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) { | |
372 | struct hci_cp_sniff_subrate cp; | |
373 | cp.handle = cpu_to_le16(conn->handle); | |
dcf4adbf JP |
374 | cp.max_latency = cpu_to_le16(0); |
375 | cp.min_remote_timeout = cpu_to_le16(0); | |
376 | cp.min_local_timeout = cpu_to_le16(0); | |
416dc94b GP |
377 | hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp); |
378 | } | |
379 | ||
51a8efd7 | 380 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { |
416dc94b GP |
381 | struct hci_cp_sniff_mode cp; |
382 | cp.handle = cpu_to_le16(conn->handle); | |
383 | cp.max_interval = cpu_to_le16(hdev->sniff_max_interval); | |
384 | cp.min_interval = cpu_to_le16(hdev->sniff_min_interval); | |
dcf4adbf JP |
385 | cp.attempt = cpu_to_le16(4); |
386 | cp.timeout = cpu_to_le16(1); | |
416dc94b GP |
387 | hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp); |
388 | } | |
389 | } | |
390 | ||
7bc18d9d | 391 | static void hci_conn_auto_accept(struct work_struct *work) |
9f61656a | 392 | { |
7bc18d9d JH |
393 | struct hci_conn *conn = container_of(work, struct hci_conn, |
394 | auto_accept_work.work); | |
9f61656a | 395 | |
7bc18d9d | 396 | hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst), |
5974e4c4 | 397 | &conn->dst); |
9f61656a JH |
398 | } |
399 | ||
9489eca4 JH |
400 | static void le_conn_timeout(struct work_struct *work) |
401 | { | |
402 | struct hci_conn *conn = container_of(work, struct hci_conn, | |
403 | le_conn_timeout.work); | |
3c857757 | 404 | struct hci_dev *hdev = conn->hdev; |
9489eca4 JH |
405 | |
406 | BT_DBG(""); | |
407 | ||
3c857757 JH |
408 | /* We could end up here due to having done directed advertising, |
409 | * so clean up the state if necessary. This should however only | |
410 | * happen with broken hardware or if low duty cycle was used | |
411 | * (which doesn't have a timeout of its own). | |
412 | */ | |
413 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { | |
414 | u8 enable = 0x00; | |
415 | hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), | |
416 | &enable); | |
417 | hci_le_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT); | |
418 | return; | |
419 | } | |
420 | ||
9489eca4 JH |
421 | hci_le_create_connection_cancel(conn); |
422 | } | |
423 | ||
a5c4e309 JH |
424 | struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst, |
425 | u8 role) | |
1da177e4 LT |
426 | { |
427 | struct hci_conn *conn; | |
428 | ||
6ed93dc6 | 429 | BT_DBG("%s dst %pMR", hdev->name, dst); |
1da177e4 | 430 | |
cb601d7e | 431 | conn = kzalloc(sizeof(struct hci_conn), GFP_KERNEL); |
04837f64 | 432 | if (!conn) |
1da177e4 | 433 | return NULL; |
1da177e4 LT |
434 | |
435 | bacpy(&conn->dst, dst); | |
662e8820 | 436 | bacpy(&conn->src, &hdev->bdaddr); |
a8746417 MH |
437 | conn->hdev = hdev; |
438 | conn->type = type; | |
a5c4e309 | 439 | conn->role = role; |
a8746417 MH |
440 | conn->mode = HCI_CM_ACTIVE; |
441 | conn->state = BT_OPEN; | |
93f19c9f | 442 | conn->auth_type = HCI_AT_GENERAL_BONDING; |
17fa4b9d | 443 | conn->io_capability = hdev->io_capability; |
a9583556 | 444 | conn->remote_auth = 0xff; |
13d39315 | 445 | conn->key_type = 0xff; |
5a134fae | 446 | conn->tx_power = HCI_TX_POWER_INVALID; |
d0455ed9 | 447 | conn->max_tx_power = HCI_TX_POWER_INVALID; |
1da177e4 | 448 | |
58a681ef | 449 | set_bit(HCI_CONN_POWER_SAVE, &conn->flags); |
052b30b0 | 450 | conn->disc_timeout = HCI_DISCONN_TIMEOUT; |
04837f64 | 451 | |
a5c4e309 JH |
452 | if (conn->role == HCI_ROLE_MASTER) |
453 | conn->out = true; | |
454 | ||
a8746417 MH |
455 | switch (type) { |
456 | case ACL_LINK: | |
457 | conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; | |
458 | break; | |
9c84d1da JH |
459 | case LE_LINK: |
460 | /* conn->src should reflect the local identity address */ | |
461 | hci_copy_identity_address(hdev, &conn->src, &conn->src_type); | |
462 | break; | |
a8746417 MH |
463 | case SCO_LINK: |
464 | if (lmp_esco_capable(hdev)) | |
efc7688b MH |
465 | conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) | |
466 | (hdev->esco_type & EDR_ESCO_MASK); | |
a8746417 MH |
467 | else |
468 | conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK; | |
469 | break; | |
470 | case ESCO_LINK: | |
efc7688b | 471 | conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK; |
a8746417 MH |
472 | break; |
473 | } | |
474 | ||
1da177e4 | 475 | skb_queue_head_init(&conn->data_q); |
04837f64 | 476 | |
70c1f20b | 477 | INIT_LIST_HEAD(&conn->chan_list); |
73d80deb | 478 | |
19c40e3b | 479 | INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout); |
7bc18d9d | 480 | INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept); |
a74a84f6 | 481 | INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle); |
9489eca4 | 482 | INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout); |
1da177e4 LT |
483 | |
484 | atomic_set(&conn->refcnt, 0); | |
485 | ||
486 | hci_dev_hold(hdev); | |
487 | ||
1da177e4 | 488 | hci_conn_hash_add(hdev, conn); |
3c54711c | 489 | if (hdev->notify) |
1da177e4 LT |
490 | hdev->notify(hdev, HCI_NOTIFY_CONN_ADD); |
491 | ||
a67e899c MH |
492 | hci_conn_init_sysfs(conn); |
493 | ||
1da177e4 LT |
494 | return conn; |
495 | } | |
496 | ||
497 | int hci_conn_del(struct hci_conn *conn) | |
498 | { | |
499 | struct hci_dev *hdev = conn->hdev; | |
500 | ||
38b3fef1 | 501 | BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle); |
1da177e4 | 502 | |
19c40e3b | 503 | cancel_delayed_work_sync(&conn->disc_work); |
7bc18d9d | 504 | cancel_delayed_work_sync(&conn->auto_accept_work); |
a74a84f6 | 505 | cancel_delayed_work_sync(&conn->idle_work); |
9f61656a | 506 | |
5b7f9909 | 507 | if (conn->type == ACL_LINK) { |
1da177e4 LT |
508 | struct hci_conn *sco = conn->link; |
509 | if (sco) | |
510 | sco->link = NULL; | |
511 | ||
512 | /* Unacked frames */ | |
513 | hdev->acl_cnt += conn->sent; | |
6ed58ec5 | 514 | } else if (conn->type == LE_LINK) { |
9489eca4 JH |
515 | cancel_delayed_work_sync(&conn->le_conn_timeout); |
516 | ||
6ed58ec5 VT |
517 | if (hdev->le_pkts) |
518 | hdev->le_cnt += conn->sent; | |
519 | else | |
520 | hdev->acl_cnt += conn->sent; | |
5b7f9909 MH |
521 | } else { |
522 | struct hci_conn *acl = conn->link; | |
523 | if (acl) { | |
524 | acl->link = NULL; | |
76a68ba0 | 525 | hci_conn_drop(acl); |
5b7f9909 | 526 | } |
1da177e4 LT |
527 | } |
528 | ||
2c33c06a | 529 | hci_chan_list_flush(conn); |
73d80deb | 530 | |
9740e49d AE |
531 | if (conn->amp_mgr) |
532 | amp_mgr_put(conn->amp_mgr); | |
533 | ||
1da177e4 | 534 | hci_conn_hash_del(hdev, conn); |
3c54711c | 535 | if (hdev->notify) |
1da177e4 | 536 | hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); |
7d0db0a3 | 537 | |
1da177e4 | 538 | skb_queue_purge(&conn->data_q); |
1da177e4 | 539 | |
fc225c3f | 540 | hci_conn_del_sysfs(conn); |
2ae9a6be | 541 | |
384943ec MH |
542 | hci_dev_put(hdev); |
543 | ||
8d12356f | 544 | hci_conn_put(conn); |
163f4dab | 545 | |
1da177e4 LT |
546 | return 0; |
547 | } | |
548 | ||
549 | struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src) | |
550 | { | |
551 | int use_src = bacmp(src, BDADDR_ANY); | |
8035ded4 | 552 | struct hci_dev *hdev = NULL, *d; |
1da177e4 | 553 | |
6ed93dc6 | 554 | BT_DBG("%pMR -> %pMR", src, dst); |
1da177e4 | 555 | |
f20d09d5 | 556 | read_lock(&hci_dev_list_lock); |
1da177e4 | 557 | |
8035ded4 | 558 | list_for_each_entry(d, &hci_dev_list, list) { |
8fc9ced3 | 559 | if (!test_bit(HCI_UP, &d->flags) || |
af750e94 | 560 | test_bit(HCI_USER_CHANNEL, &d->dev_flags) || |
d300fa9b | 561 | d->dev_type != HCI_BREDR) |
1da177e4 LT |
562 | continue; |
563 | ||
8e87d142 | 564 | /* Simple routing: |
1da177e4 LT |
565 | * No source address - find interface with bdaddr != dst |
566 | * Source address - find interface with bdaddr == src | |
567 | */ | |
568 | ||
569 | if (use_src) { | |
570 | if (!bacmp(&d->bdaddr, src)) { | |
571 | hdev = d; break; | |
572 | } | |
573 | } else { | |
574 | if (bacmp(&d->bdaddr, dst)) { | |
575 | hdev = d; break; | |
576 | } | |
577 | } | |
578 | } | |
579 | ||
580 | if (hdev) | |
581 | hdev = hci_dev_hold(hdev); | |
582 | ||
f20d09d5 | 583 | read_unlock(&hci_dev_list_lock); |
1da177e4 LT |
584 | return hdev; |
585 | } | |
586 | EXPORT_SYMBOL(hci_get_route); | |
587 | ||
9bb3c01f | 588 | /* This function requires the caller holds hdev->lock */ |
06c053fb | 589 | void hci_le_conn_failed(struct hci_conn *conn, u8 status) |
9bb3c01f AG |
590 | { |
591 | struct hci_dev *hdev = conn->hdev; | |
592 | ||
593 | conn->state = BT_CLOSED; | |
594 | ||
595 | mgmt_connect_failed(hdev, &conn->dst, conn->type, conn->dst_type, | |
596 | status); | |
597 | ||
598 | hci_proto_connect_cfm(conn, status); | |
599 | ||
600 | hci_conn_del(conn); | |
a4790dbd AG |
601 | |
602 | /* Since we may have temporarily stopped the background scanning in | |
603 | * favor of connection establishment, we should restart it. | |
604 | */ | |
605 | hci_update_background_scan(hdev); | |
3c857757 JH |
606 | |
607 | /* Re-enable advertising in case this was a failed connection | |
608 | * attempt as a peripheral. | |
609 | */ | |
610 | mgmt_reenable_advertising(hdev); | |
9bb3c01f AG |
611 | } |
612 | ||
1d399ae5 AG |
613 | static void create_le_conn_complete(struct hci_dev *hdev, u8 status) |
614 | { | |
615 | struct hci_conn *conn; | |
616 | ||
617 | if (status == 0) | |
618 | return; | |
619 | ||
620 | BT_ERR("HCI request failed to create LE connection: status 0x%2.2x", | |
621 | status); | |
622 | ||
623 | hci_dev_lock(hdev); | |
624 | ||
625 | conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT); | |
626 | if (!conn) | |
627 | goto done; | |
628 | ||
06c053fb | 629 | hci_le_conn_failed(conn, status); |
1d399ae5 AG |
630 | |
631 | done: | |
632 | hci_dev_unlock(hdev); | |
633 | } | |
634 | ||
2acf3d90 AG |
635 | static void hci_req_add_le_create_conn(struct hci_request *req, |
636 | struct hci_conn *conn) | |
637 | { | |
638 | struct hci_cp_le_create_conn cp; | |
639 | struct hci_dev *hdev = conn->hdev; | |
640 | u8 own_addr_type; | |
641 | ||
642 | memset(&cp, 0, sizeof(cp)); | |
643 | ||
644 | /* Update random address, but set require_privacy to false so | |
645 | * that we never connect with an unresolvable address. | |
646 | */ | |
647 | if (hci_update_random_address(req, false, &own_addr_type)) | |
648 | return; | |
649 | ||
2acf3d90 AG |
650 | cp.scan_interval = cpu_to_le16(hdev->le_scan_interval); |
651 | cp.scan_window = cpu_to_le16(hdev->le_scan_window); | |
652 | bacpy(&cp.peer_addr, &conn->dst); | |
653 | cp.peer_addr_type = conn->dst_type; | |
654 | cp.own_address_type = own_addr_type; | |
655 | cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval); | |
656 | cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval); | |
037fc415 MH |
657 | cp.conn_latency = cpu_to_le16(conn->le_conn_latency); |
658 | cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout); | |
dcf4adbf JP |
659 | cp.min_ce_len = cpu_to_le16(0x0000); |
660 | cp.max_ce_len = cpu_to_le16(0x0000); | |
2acf3d90 AG |
661 | |
662 | hci_req_add(req, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp); | |
b46e0030 JH |
663 | |
664 | conn->state = BT_CONNECT; | |
2acf3d90 AG |
665 | } |
666 | ||
3c857757 JH |
667 | static void hci_req_directed_advertising(struct hci_request *req, |
668 | struct hci_conn *conn) | |
669 | { | |
670 | struct hci_dev *hdev = req->hdev; | |
671 | struct hci_cp_le_set_adv_param cp; | |
672 | u8 own_addr_type; | |
673 | u8 enable; | |
674 | ||
5ce194c4 | 675 | /* Clear the HCI_LE_ADV bit temporarily so that the |
3c857757 JH |
676 | * hci_update_random_address knows that it's safe to go ahead |
677 | * and write a new random address. The flag will be set back on | |
678 | * as soon as the SET_ADV_ENABLE HCI command completes. | |
679 | */ | |
5ce194c4 | 680 | clear_bit(HCI_LE_ADV, &hdev->dev_flags); |
3c857757 JH |
681 | |
682 | /* Set require_privacy to false so that the remote device has a | |
683 | * chance of identifying us. | |
684 | */ | |
685 | if (hci_update_random_address(req, false, &own_addr_type) < 0) | |
686 | return; | |
687 | ||
688 | memset(&cp, 0, sizeof(cp)); | |
689 | cp.type = LE_ADV_DIRECT_IND; | |
690 | cp.own_address_type = own_addr_type; | |
691 | cp.direct_addr_type = conn->dst_type; | |
692 | bacpy(&cp.direct_addr, &conn->dst); | |
693 | cp.channel_map = hdev->le_adv_channel_map; | |
694 | ||
695 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); | |
696 | ||
697 | enable = 0x01; | |
698 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); | |
699 | ||
700 | conn->state = BT_CONNECT; | |
701 | } | |
702 | ||
04a6c589 | 703 | struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, |
cdd6275e | 704 | u8 dst_type, u8 sec_level, u16 conn_timeout, |
e804d25d | 705 | u8 role) |
1da177e4 | 706 | { |
4292f1f3 | 707 | struct hci_conn_params *params; |
f1e5d547 | 708 | struct hci_conn *conn; |
1ebfcc1f | 709 | struct smp_irk *irk; |
2acf3d90 | 710 | struct hci_request req; |
1d399ae5 | 711 | int err; |
1da177e4 | 712 | |
620ad521 AG |
713 | /* Some devices send ATT messages as soon as the physical link is |
714 | * established. To be able to handle these ATT messages, the user- | |
715 | * space first establishes the connection and then starts the pairing | |
716 | * process. | |
717 | * | |
718 | * So if a hci_conn object already exists for the following connection | |
719 | * attempt, we simply update pending_sec_level and auth_type fields | |
720 | * and return the object found. | |
721 | */ | |
f1e5d547 | 722 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst); |
620ad521 AG |
723 | if (conn) { |
724 | conn->pending_sec_level = sec_level; | |
620ad521 AG |
725 | goto done; |
726 | } | |
dfc94dbd | 727 | |
620ad521 AG |
728 | /* Since the controller supports only one LE connection attempt at a |
729 | * time, we return -EBUSY if there is any connection attempt running. | |
730 | */ | |
731 | conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT); | |
732 | if (conn) | |
733 | return ERR_PTR(-EBUSY); | |
46a190cb | 734 | |
edb4b466 MH |
735 | /* When given an identity address with existing identity |
736 | * resolving key, the connection needs to be established | |
737 | * to a resolvable random address. | |
738 | * | |
739 | * This uses the cached random resolvable address from | |
740 | * a previous scan. When no cached address is available, | |
741 | * try connecting to the identity address instead. | |
742 | * | |
743 | * Storing the resolvable random address is required here | |
744 | * to handle connection failures. The address will later | |
745 | * be resolved back into the original identity address | |
746 | * from the connect request. | |
747 | */ | |
1ebfcc1f JH |
748 | irk = hci_find_irk_by_addr(hdev, dst, dst_type); |
749 | if (irk && bacmp(&irk->rpa, BDADDR_ANY)) { | |
750 | dst = &irk->rpa; | |
751 | dst_type = ADDR_LE_DEV_RANDOM; | |
752 | } | |
753 | ||
a5c4e309 | 754 | conn = hci_conn_add(hdev, LE_LINK, dst, role); |
620ad521 AG |
755 | if (!conn) |
756 | return ERR_PTR(-ENOMEM); | |
9f0caeb1 | 757 | |
1ebfcc1f | 758 | conn->dst_type = dst_type; |
620ad521 | 759 | conn->sec_level = BT_SECURITY_LOW; |
f1e5d547 | 760 | conn->pending_sec_level = sec_level; |
09ae260b | 761 | conn->conn_timeout = conn_timeout; |
4292f1f3 | 762 | |
3c857757 JH |
763 | hci_req_init(&req, hdev); |
764 | ||
376f54c1 JH |
765 | /* Disable advertising if we're active. For master role |
766 | * connections most controllers will refuse to connect if | |
767 | * advertising is enabled, and for slave role connections we | |
768 | * anyway have to disable it in order to start directed | |
769 | * advertising. | |
770 | */ | |
771 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) { | |
772 | u8 enable = 0x00; | |
773 | hci_req_add(&req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), | |
774 | &enable); | |
775 | } | |
776 | ||
cdd6275e | 777 | /* If requested to connect as slave use directed advertising */ |
e804d25d | 778 | if (conn->role == HCI_ROLE_SLAVE) { |
e8bb6b97 JH |
779 | /* If we're active scanning most controllers are unable |
780 | * to initiate advertising. Simply reject the attempt. | |
781 | */ | |
782 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && | |
783 | hdev->le_scan_type == LE_SCAN_ACTIVE) { | |
784 | skb_queue_purge(&req.cmd_q); | |
785 | hci_conn_del(conn); | |
786 | return ERR_PTR(-EBUSY); | |
787 | } | |
788 | ||
3c857757 JH |
789 | hci_req_directed_advertising(&req, conn); |
790 | goto create_conn; | |
791 | } | |
792 | ||
4292f1f3 AG |
793 | params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); |
794 | if (params) { | |
795 | conn->le_conn_min_interval = params->conn_min_interval; | |
796 | conn->le_conn_max_interval = params->conn_max_interval; | |
037fc415 MH |
797 | conn->le_conn_latency = params->conn_latency; |
798 | conn->le_supv_timeout = params->supervision_timeout; | |
4292f1f3 AG |
799 | } else { |
800 | conn->le_conn_min_interval = hdev->le_conn_min_interval; | |
801 | conn->le_conn_max_interval = hdev->le_conn_max_interval; | |
04fb7d90 MH |
802 | conn->le_conn_latency = hdev->le_conn_latency; |
803 | conn->le_supv_timeout = hdev->le_supv_timeout; | |
4292f1f3 | 804 | } |
eda42b50 | 805 | |
2acf3d90 | 806 | /* If controller is scanning, we stop it since some controllers are |
81ad6fd9 JH |
807 | * not able to scan and connect at the same time. Also set the |
808 | * HCI_LE_SCAN_INTERRUPTED flag so that the command complete | |
809 | * handler for scan disabling knows to set the correct discovery | |
810 | * state. | |
2acf3d90 AG |
811 | */ |
812 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) { | |
813 | hci_req_add_le_scan_disable(&req); | |
81ad6fd9 | 814 | set_bit(HCI_LE_SCAN_INTERRUPTED, &hdev->dev_flags); |
2acf3d90 AG |
815 | } |
816 | ||
81ad6fd9 JH |
817 | hci_req_add_le_create_conn(&req, conn); |
818 | ||
3c857757 | 819 | create_conn: |
81ad6fd9 | 820 | err = hci_req_run(&req, create_le_conn_complete); |
2acf3d90 AG |
821 | if (err) { |
822 | hci_conn_del(conn); | |
620ad521 | 823 | return ERR_PTR(err); |
2acf3d90 | 824 | } |
fcd89c09 | 825 | |
620ad521 AG |
826 | done: |
827 | hci_conn_hold(conn); | |
f1e5d547 | 828 | return conn; |
d04aef4c | 829 | } |
fcd89c09 | 830 | |
04a6c589 AG |
831 | struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst, |
832 | u8 sec_level, u8 auth_type) | |
1da177e4 LT |
833 | { |
834 | struct hci_conn *acl; | |
fcd89c09 | 835 | |
56f87901 JH |
836 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
837 | return ERR_PTR(-ENOTSUPP); | |
838 | ||
70f23020 AE |
839 | acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); |
840 | if (!acl) { | |
a5c4e309 | 841 | acl = hci_conn_add(hdev, ACL_LINK, dst, HCI_ROLE_MASTER); |
70f23020 | 842 | if (!acl) |
48c7aba9 | 843 | return ERR_PTR(-ENOMEM); |
1da177e4 LT |
844 | } |
845 | ||
846 | hci_conn_hold(acl); | |
847 | ||
09ab6f4c | 848 | if (acl->state == BT_OPEN || acl->state == BT_CLOSED) { |
765c2a96 JH |
849 | acl->sec_level = BT_SECURITY_LOW; |
850 | acl->pending_sec_level = sec_level; | |
09ab6f4c | 851 | acl->auth_type = auth_type; |
1aef8669 | 852 | hci_acl_create_connection(acl); |
09ab6f4c | 853 | } |
1da177e4 | 854 | |
db474275 VCG |
855 | return acl; |
856 | } | |
857 | ||
10c62ddc FD |
858 | struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst, |
859 | __u16 setting) | |
db474275 VCG |
860 | { |
861 | struct hci_conn *acl; | |
862 | struct hci_conn *sco; | |
863 | ||
e660ed6c | 864 | acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING); |
db474275 | 865 | if (IS_ERR(acl)) |
5b7f9909 | 866 | return acl; |
1da177e4 | 867 | |
70f23020 AE |
868 | sco = hci_conn_hash_lookup_ba(hdev, type, dst); |
869 | if (!sco) { | |
a5c4e309 | 870 | sco = hci_conn_add(hdev, type, dst, HCI_ROLE_MASTER); |
70f23020 | 871 | if (!sco) { |
76a68ba0 | 872 | hci_conn_drop(acl); |
48c7aba9 | 873 | return ERR_PTR(-ENOMEM); |
1da177e4 | 874 | } |
5b7f9909 | 875 | } |
1da177e4 | 876 | |
5b7f9909 MH |
877 | acl->link = sco; |
878 | sco->link = acl; | |
1da177e4 | 879 | |
5b7f9909 | 880 | hci_conn_hold(sco); |
1da177e4 | 881 | |
10c62ddc FD |
882 | sco->setting = setting; |
883 | ||
5b7f9909 | 884 | if (acl->state == BT_CONNECTED && |
5974e4c4 | 885 | (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { |
58a681ef | 886 | set_bit(HCI_CONN_POWER_SAVE, &acl->flags); |
14b12d0b | 887 | hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON); |
c390216b | 888 | |
51a8efd7 | 889 | if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) { |
e73439d8 | 890 | /* defer SCO setup until mode change completed */ |
51a8efd7 | 891 | set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags); |
e73439d8 MH |
892 | return sco; |
893 | } | |
894 | ||
895 | hci_sco_setup(acl, 0x00); | |
b6a0dc82 | 896 | } |
5b7f9909 MH |
897 | |
898 | return sco; | |
1da177e4 | 899 | } |
1da177e4 | 900 | |
e7c29cb1 MH |
901 | /* Check link security requirement */ |
902 | int hci_conn_check_link_mode(struct hci_conn *conn) | |
903 | { | |
38b3fef1 | 904 | BT_DBG("hcon %p", conn); |
e7c29cb1 | 905 | |
40b552aa MH |
906 | /* In Secure Connections Only mode, it is required that Secure |
907 | * Connections is used and the link is encrypted with AES-CCM | |
908 | * using a P-256 authenticated combination key. | |
909 | */ | |
910 | if (test_bit(HCI_SC_ONLY, &conn->hdev->flags)) { | |
911 | if (!hci_conn_sc_enabled(conn) || | |
912 | !test_bit(HCI_CONN_AES_CCM, &conn->flags) || | |
913 | conn->key_type != HCI_LK_AUTH_COMBINATION_P256) | |
914 | return 0; | |
915 | } | |
916 | ||
4dae2798 JH |
917 | if (hci_conn_ssp_enabled(conn) && |
918 | !test_bit(HCI_CONN_ENCRYPT, &conn->flags)) | |
e7c29cb1 MH |
919 | return 0; |
920 | ||
921 | return 1; | |
922 | } | |
e7c29cb1 | 923 | |
1da177e4 | 924 | /* Authenticate remote device */ |
0684e5f9 | 925 | static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) |
1da177e4 | 926 | { |
38b3fef1 | 927 | BT_DBG("hcon %p", conn); |
1da177e4 | 928 | |
765c2a96 JH |
929 | if (conn->pending_sec_level > sec_level) |
930 | sec_level = conn->pending_sec_level; | |
931 | ||
96a31833 | 932 | if (sec_level > conn->sec_level) |
765c2a96 | 933 | conn->pending_sec_level = sec_level; |
4dae2798 | 934 | else if (test_bit(HCI_CONN_AUTH, &conn->flags)) |
1da177e4 LT |
935 | return 1; |
936 | ||
65cf686e JH |
937 | /* Make sure we preserve an existing MITM requirement*/ |
938 | auth_type |= (conn->auth_type & 0x01); | |
939 | ||
96a31833 MH |
940 | conn->auth_type = auth_type; |
941 | ||
51a8efd7 | 942 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) { |
1da177e4 | 943 | struct hci_cp_auth_requested cp; |
b7d05bad | 944 | |
aca3192c | 945 | cp.handle = cpu_to_le16(conn->handle); |
40be492f | 946 | hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, |
5974e4c4 | 947 | sizeof(cp), &cp); |
09da1f34 JH |
948 | |
949 | /* If we're already encrypted set the REAUTH_PEND flag, | |
950 | * otherwise set the ENCRYPT_PEND. | |
951 | */ | |
4dae2798 | 952 | if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) |
51a8efd7 | 953 | set_bit(HCI_CONN_REAUTH_PEND, &conn->flags); |
09da1f34 JH |
954 | else |
955 | set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags); | |
1da177e4 | 956 | } |
8c1b2355 | 957 | |
1da177e4 LT |
958 | return 0; |
959 | } | |
1da177e4 | 960 | |
13d39315 WR |
961 | /* Encrypt the the link */ |
962 | static void hci_conn_encrypt(struct hci_conn *conn) | |
963 | { | |
38b3fef1 | 964 | BT_DBG("hcon %p", conn); |
13d39315 | 965 | |
51a8efd7 | 966 | if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) { |
13d39315 WR |
967 | struct hci_cp_set_conn_encrypt cp; |
968 | cp.handle = cpu_to_le16(conn->handle); | |
969 | cp.encrypt = 0x01; | |
970 | hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp), | |
5974e4c4 | 971 | &cp); |
13d39315 WR |
972 | } |
973 | } | |
974 | ||
8c1b2355 | 975 | /* Enable security */ |
e7cafc45 JH |
976 | int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type, |
977 | bool initiator) | |
1da177e4 | 978 | { |
38b3fef1 | 979 | BT_DBG("hcon %p", conn); |
1da177e4 | 980 | |
d8343f12 VCG |
981 | if (conn->type == LE_LINK) |
982 | return smp_conn_security(conn, sec_level); | |
983 | ||
13d39315 | 984 | /* For sdp we don't need the link key. */ |
8c1b2355 MH |
985 | if (sec_level == BT_SECURITY_SDP) |
986 | return 1; | |
987 | ||
13d39315 WR |
988 | /* For non 2.1 devices and low security level we don't need the link |
989 | key. */ | |
aa64a8b5 | 990 | if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn)) |
3fdca1e1 | 991 | return 1; |
8c1b2355 | 992 | |
13d39315 | 993 | /* For other security levels we need the link key. */ |
4dae2798 | 994 | if (!test_bit(HCI_CONN_AUTH, &conn->flags)) |
13d39315 WR |
995 | goto auth; |
996 | ||
7b5a9241 MH |
997 | /* An authenticated FIPS approved combination key has sufficient |
998 | * security for security level 4. */ | |
999 | if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256 && | |
1000 | sec_level == BT_SECURITY_FIPS) | |
1001 | goto encrypt; | |
1002 | ||
1003 | /* An authenticated combination key has sufficient security for | |
1004 | security level 3. */ | |
1005 | if ((conn->key_type == HCI_LK_AUTH_COMBINATION_P192 || | |
1006 | conn->key_type == HCI_LK_AUTH_COMBINATION_P256) && | |
1007 | sec_level == BT_SECURITY_HIGH) | |
13d39315 WR |
1008 | goto encrypt; |
1009 | ||
1010 | /* An unauthenticated combination key has sufficient security for | |
1011 | security level 1 and 2. */ | |
66138ce8 MH |
1012 | if ((conn->key_type == HCI_LK_UNAUTH_COMBINATION_P192 || |
1013 | conn->key_type == HCI_LK_UNAUTH_COMBINATION_P256) && | |
5974e4c4 | 1014 | (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW)) |
13d39315 WR |
1015 | goto encrypt; |
1016 | ||
1017 | /* A combination key has always sufficient security for the security | |
1018 | levels 1 or 2. High security level requires the combination key | |
1019 | is generated using maximum PIN code length (16). | |
1020 | For pre 2.1 units. */ | |
1021 | if (conn->key_type == HCI_LK_COMBINATION && | |
7b5a9241 MH |
1022 | (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW || |
1023 | conn->pin_length == 16)) | |
13d39315 WR |
1024 | goto encrypt; |
1025 | ||
1026 | auth: | |
51a8efd7 | 1027 | if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) |
1da177e4 LT |
1028 | return 0; |
1029 | ||
6fdf658c LAD |
1030 | if (!hci_conn_auth(conn, sec_level, auth_type)) |
1031 | return 0; | |
13d39315 WR |
1032 | |
1033 | encrypt: | |
4dae2798 | 1034 | if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) |
13d39315 | 1035 | return 1; |
8c1b2355 | 1036 | |
13d39315 | 1037 | hci_conn_encrypt(conn); |
1da177e4 LT |
1038 | return 0; |
1039 | } | |
8c1b2355 | 1040 | EXPORT_SYMBOL(hci_conn_security); |
1da177e4 | 1041 | |
b3b1b061 WR |
1042 | /* Check secure link requirement */ |
1043 | int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level) | |
1044 | { | |
38b3fef1 | 1045 | BT_DBG("hcon %p", conn); |
b3b1b061 | 1046 | |
9cb2e030 MH |
1047 | /* Accept if non-secure or higher security level is required */ |
1048 | if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS) | |
1049 | return 1; | |
b3b1b061 | 1050 | |
9cb2e030 MH |
1051 | /* Accept if secure or higher security level is already present */ |
1052 | if (conn->sec_level == BT_SECURITY_HIGH || | |
1053 | conn->sec_level == BT_SECURITY_FIPS) | |
b3b1b061 WR |
1054 | return 1; |
1055 | ||
9cb2e030 MH |
1056 | /* Reject not secure link */ |
1057 | return 0; | |
b3b1b061 WR |
1058 | } |
1059 | EXPORT_SYMBOL(hci_conn_check_secure); | |
1060 | ||
1da177e4 LT |
1061 | /* Change link key */ |
1062 | int hci_conn_change_link_key(struct hci_conn *conn) | |
1063 | { | |
38b3fef1 | 1064 | BT_DBG("hcon %p", conn); |
1da177e4 | 1065 | |
51a8efd7 | 1066 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) { |
1da177e4 | 1067 | struct hci_cp_change_conn_link_key cp; |
aca3192c | 1068 | cp.handle = cpu_to_le16(conn->handle); |
40be492f | 1069 | hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY, |
5974e4c4 | 1070 | sizeof(cp), &cp); |
1da177e4 | 1071 | } |
8c1b2355 | 1072 | |
1da177e4 LT |
1073 | return 0; |
1074 | } | |
1da177e4 LT |
1075 | |
1076 | /* Switch role */ | |
8c1b2355 | 1077 | int hci_conn_switch_role(struct hci_conn *conn, __u8 role) |
1da177e4 | 1078 | { |
38b3fef1 | 1079 | BT_DBG("hcon %p", conn); |
1da177e4 | 1080 | |
40bef302 | 1081 | if (role == conn->role) |
1da177e4 LT |
1082 | return 1; |
1083 | ||
51a8efd7 | 1084 | if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) { |
1da177e4 LT |
1085 | struct hci_cp_switch_role cp; |
1086 | bacpy(&cp.bdaddr, &conn->dst); | |
1087 | cp.role = role; | |
a9de9248 | 1088 | hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp); |
1da177e4 | 1089 | } |
8c1b2355 | 1090 | |
1da177e4 LT |
1091 | return 0; |
1092 | } | |
1093 | EXPORT_SYMBOL(hci_conn_switch_role); | |
1094 | ||
04837f64 | 1095 | /* Enter active mode */ |
14b12d0b | 1096 | void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active) |
04837f64 MH |
1097 | { |
1098 | struct hci_dev *hdev = conn->hdev; | |
1099 | ||
38b3fef1 | 1100 | BT_DBG("hcon %p mode %d", conn, conn->mode); |
04837f64 | 1101 | |
14b12d0b JG |
1102 | if (conn->mode != HCI_CM_SNIFF) |
1103 | goto timer; | |
1104 | ||
58a681ef | 1105 | if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active) |
04837f64 MH |
1106 | goto timer; |
1107 | ||
51a8efd7 | 1108 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { |
04837f64 | 1109 | struct hci_cp_exit_sniff_mode cp; |
aca3192c | 1110 | cp.handle = cpu_to_le16(conn->handle); |
a9de9248 | 1111 | hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp); |
04837f64 MH |
1112 | } |
1113 | ||
1114 | timer: | |
1115 | if (hdev->idle_timeout > 0) | |
a74a84f6 JH |
1116 | queue_delayed_work(hdev->workqueue, &conn->idle_work, |
1117 | msecs_to_jiffies(hdev->idle_timeout)); | |
04837f64 MH |
1118 | } |
1119 | ||
1da177e4 LT |
1120 | /* Drop all connection on the device */ |
1121 | void hci_conn_hash_flush(struct hci_dev *hdev) | |
1122 | { | |
1123 | struct hci_conn_hash *h = &hdev->conn_hash; | |
3c4e0df0 | 1124 | struct hci_conn *c, *n; |
1da177e4 LT |
1125 | |
1126 | BT_DBG("hdev %s", hdev->name); | |
1127 | ||
3c4e0df0 | 1128 | list_for_each_entry_safe(c, n, &h->list, list) { |
1da177e4 LT |
1129 | c->state = BT_CLOSED; |
1130 | ||
9f5a0d7b | 1131 | hci_proto_disconn_cfm(c, HCI_ERROR_LOCAL_HOST_TERM); |
1da177e4 LT |
1132 | hci_conn_del(c); |
1133 | } | |
1134 | } | |
1135 | ||
a9de9248 MH |
1136 | /* Check pending connect attempts */ |
1137 | void hci_conn_check_pending(struct hci_dev *hdev) | |
1138 | { | |
1139 | struct hci_conn *conn; | |
1140 | ||
1141 | BT_DBG("hdev %s", hdev->name); | |
1142 | ||
1143 | hci_dev_lock(hdev); | |
1144 | ||
1145 | conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2); | |
1146 | if (conn) | |
1aef8669 | 1147 | hci_acl_create_connection(conn); |
a9de9248 MH |
1148 | |
1149 | hci_dev_unlock(hdev); | |
1150 | } | |
1151 | ||
4dae2798 JH |
1152 | static u32 get_link_mode(struct hci_conn *conn) |
1153 | { | |
1154 | u32 link_mode = 0; | |
1155 | ||
40bef302 | 1156 | if (conn->role == HCI_ROLE_MASTER) |
4dae2798 JH |
1157 | link_mode |= HCI_LM_MASTER; |
1158 | ||
1159 | if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) | |
1160 | link_mode |= HCI_LM_ENCRYPT; | |
1161 | ||
1162 | if (test_bit(HCI_CONN_AUTH, &conn->flags)) | |
1163 | link_mode |= HCI_LM_AUTH; | |
1164 | ||
1165 | if (test_bit(HCI_CONN_SECURE, &conn->flags)) | |
1166 | link_mode |= HCI_LM_SECURE; | |
1167 | ||
1168 | if (test_bit(HCI_CONN_FIPS, &conn->flags)) | |
1169 | link_mode |= HCI_LM_FIPS; | |
1170 | ||
1171 | return link_mode; | |
1172 | } | |
1173 | ||
1da177e4 LT |
1174 | int hci_get_conn_list(void __user *arg) |
1175 | { | |
fc5fef61 | 1176 | struct hci_conn *c; |
1da177e4 LT |
1177 | struct hci_conn_list_req req, *cl; |
1178 | struct hci_conn_info *ci; | |
1179 | struct hci_dev *hdev; | |
1da177e4 LT |
1180 | int n = 0, size, err; |
1181 | ||
1182 | if (copy_from_user(&req, arg, sizeof(req))) | |
1183 | return -EFAULT; | |
1184 | ||
1185 | if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci)) | |
1186 | return -EINVAL; | |
1187 | ||
1188 | size = sizeof(req) + req.conn_num * sizeof(*ci); | |
1189 | ||
70f23020 AE |
1190 | cl = kmalloc(size, GFP_KERNEL); |
1191 | if (!cl) | |
1da177e4 LT |
1192 | return -ENOMEM; |
1193 | ||
70f23020 AE |
1194 | hdev = hci_dev_get(req.dev_id); |
1195 | if (!hdev) { | |
1da177e4 LT |
1196 | kfree(cl); |
1197 | return -ENODEV; | |
1198 | } | |
1199 | ||
1200 | ci = cl->conn_info; | |
1201 | ||
09fd0de5 | 1202 | hci_dev_lock(hdev); |
8035ded4 | 1203 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
1da177e4 LT |
1204 | bacpy(&(ci + n)->bdaddr, &c->dst); |
1205 | (ci + n)->handle = c->handle; | |
1206 | (ci + n)->type = c->type; | |
1207 | (ci + n)->out = c->out; | |
1208 | (ci + n)->state = c->state; | |
4dae2798 | 1209 | (ci + n)->link_mode = get_link_mode(c); |
1da177e4 LT |
1210 | if (++n >= req.conn_num) |
1211 | break; | |
1212 | } | |
09fd0de5 | 1213 | hci_dev_unlock(hdev); |
1da177e4 LT |
1214 | |
1215 | cl->dev_id = hdev->id; | |
1216 | cl->conn_num = n; | |
1217 | size = sizeof(req) + n * sizeof(*ci); | |
1218 | ||
1219 | hci_dev_put(hdev); | |
1220 | ||
1221 | err = copy_to_user(arg, cl, size); | |
1222 | kfree(cl); | |
1223 | ||
1224 | return err ? -EFAULT : 0; | |
1225 | } | |
1226 | ||
1227 | int hci_get_conn_info(struct hci_dev *hdev, void __user *arg) | |
1228 | { | |
1229 | struct hci_conn_info_req req; | |
1230 | struct hci_conn_info ci; | |
1231 | struct hci_conn *conn; | |
1232 | char __user *ptr = arg + sizeof(req); | |
1233 | ||
1234 | if (copy_from_user(&req, arg, sizeof(req))) | |
1235 | return -EFAULT; | |
1236 | ||
09fd0de5 | 1237 | hci_dev_lock(hdev); |
1da177e4 LT |
1238 | conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr); |
1239 | if (conn) { | |
1240 | bacpy(&ci.bdaddr, &conn->dst); | |
1241 | ci.handle = conn->handle; | |
1242 | ci.type = conn->type; | |
1243 | ci.out = conn->out; | |
1244 | ci.state = conn->state; | |
4dae2798 | 1245 | ci.link_mode = get_link_mode(conn); |
1da177e4 | 1246 | } |
09fd0de5 | 1247 | hci_dev_unlock(hdev); |
1da177e4 LT |
1248 | |
1249 | if (!conn) | |
1250 | return -ENOENT; | |
1251 | ||
1252 | return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0; | |
1253 | } | |
40be492f MH |
1254 | |
1255 | int hci_get_auth_info(struct hci_dev *hdev, void __user *arg) | |
1256 | { | |
1257 | struct hci_auth_info_req req; | |
1258 | struct hci_conn *conn; | |
1259 | ||
1260 | if (copy_from_user(&req, arg, sizeof(req))) | |
1261 | return -EFAULT; | |
1262 | ||
09fd0de5 | 1263 | hci_dev_lock(hdev); |
40be492f MH |
1264 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); |
1265 | if (conn) | |
1266 | req.type = conn->auth_type; | |
09fd0de5 | 1267 | hci_dev_unlock(hdev); |
40be492f MH |
1268 | |
1269 | if (!conn) | |
1270 | return -ENOENT; | |
1271 | ||
1272 | return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; | |
1273 | } | |
73d80deb LAD |
1274 | |
1275 | struct hci_chan *hci_chan_create(struct hci_conn *conn) | |
1276 | { | |
1277 | struct hci_dev *hdev = conn->hdev; | |
1278 | struct hci_chan *chan; | |
1279 | ||
38b3fef1 | 1280 | BT_DBG("%s hcon %p", hdev->name, conn); |
73d80deb | 1281 | |
75d7735c | 1282 | chan = kzalloc(sizeof(struct hci_chan), GFP_KERNEL); |
73d80deb LAD |
1283 | if (!chan) |
1284 | return NULL; | |
1285 | ||
1286 | chan->conn = conn; | |
1287 | skb_queue_head_init(&chan->data_q); | |
168df8e5 | 1288 | chan->state = BT_CONNECTED; |
73d80deb | 1289 | |
8192edef | 1290 | list_add_rcu(&chan->list, &conn->chan_list); |
73d80deb LAD |
1291 | |
1292 | return chan; | |
1293 | } | |
1294 | ||
9472007c | 1295 | void hci_chan_del(struct hci_chan *chan) |
73d80deb LAD |
1296 | { |
1297 | struct hci_conn *conn = chan->conn; | |
1298 | struct hci_dev *hdev = conn->hdev; | |
1299 | ||
38b3fef1 | 1300 | BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan); |
73d80deb | 1301 | |
8192edef GP |
1302 | list_del_rcu(&chan->list); |
1303 | ||
1304 | synchronize_rcu(); | |
73d80deb | 1305 | |
76a68ba0 | 1306 | hci_conn_drop(conn); |
e9b02748 | 1307 | |
73d80deb LAD |
1308 | skb_queue_purge(&chan->data_q); |
1309 | kfree(chan); | |
73d80deb LAD |
1310 | } |
1311 | ||
2c33c06a | 1312 | void hci_chan_list_flush(struct hci_conn *conn) |
73d80deb | 1313 | { |
2a5a5ec6 | 1314 | struct hci_chan *chan, *n; |
73d80deb | 1315 | |
38b3fef1 | 1316 | BT_DBG("hcon %p", conn); |
73d80deb | 1317 | |
2a5a5ec6 | 1318 | list_for_each_entry_safe(chan, n, &conn->chan_list, list) |
73d80deb LAD |
1319 | hci_chan_del(chan); |
1320 | } | |
42c4e53e AE |
1321 | |
1322 | static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon, | |
1323 | __u16 handle) | |
1324 | { | |
1325 | struct hci_chan *hchan; | |
1326 | ||
1327 | list_for_each_entry(hchan, &hcon->chan_list, list) { | |
1328 | if (hchan->handle == handle) | |
1329 | return hchan; | |
1330 | } | |
1331 | ||
1332 | return NULL; | |
1333 | } | |
1334 | ||
1335 | struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle) | |
1336 | { | |
1337 | struct hci_conn_hash *h = &hdev->conn_hash; | |
1338 | struct hci_conn *hcon; | |
1339 | struct hci_chan *hchan = NULL; | |
1340 | ||
1341 | rcu_read_lock(); | |
1342 | ||
1343 | list_for_each_entry_rcu(hcon, &h->list, list) { | |
1344 | hchan = __hci_chan_lookup_handle(hcon, handle); | |
1345 | if (hchan) | |
1346 | break; | |
1347 | } | |
1348 | ||
1349 | rcu_read_unlock(); | |
1350 | ||
1351 | return hchan; | |
1352 | } |