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> | |
31 | ||
fcd89c09 VT |
32 | static void hci_le_connect(struct hci_conn *conn) |
33 | { | |
34 | struct hci_dev *hdev = conn->hdev; | |
35 | struct hci_cp_le_create_conn cp; | |
36 | ||
37 | conn->state = BT_CONNECT; | |
a0c808b3 | 38 | conn->out = true; |
b92a6223 | 39 | conn->link_mode |= HCI_LM_MASTER; |
7b5c0d52 | 40 | conn->sec_level = BT_SECURITY_LOW; |
fcd89c09 VT |
41 | |
42 | memset(&cp, 0, sizeof(cp)); | |
82781e63 AE |
43 | cp.scan_interval = __constant_cpu_to_le16(0x0060); |
44 | cp.scan_window = __constant_cpu_to_le16(0x0030); | |
fcd89c09 | 45 | bacpy(&cp.peer_addr, &conn->dst); |
6d3ce0e7 | 46 | cp.peer_addr_type = conn->dst_type; |
82781e63 AE |
47 | cp.conn_interval_min = __constant_cpu_to_le16(0x0028); |
48 | cp.conn_interval_max = __constant_cpu_to_le16(0x0038); | |
49 | cp.supervision_timeout = __constant_cpu_to_le16(0x002a); | |
50 | cp.min_ce_len = __constant_cpu_to_le16(0x0000); | |
51 | cp.max_ce_len = __constant_cpu_to_le16(0x0000); | |
fcd89c09 VT |
52 | |
53 | hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp); | |
54 | } | |
55 | ||
56 | static void hci_le_connect_cancel(struct hci_conn *conn) | |
57 | { | |
58 | hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL); | |
59 | } | |
60 | ||
4c67bc74 | 61 | void hci_acl_connect(struct hci_conn *conn) |
1da177e4 LT |
62 | { |
63 | struct hci_dev *hdev = conn->hdev; | |
64 | struct inquiry_entry *ie; | |
65 | struct hci_cp_create_conn cp; | |
66 | ||
42d2d87c | 67 | BT_DBG("hcon %p", conn); |
1da177e4 LT |
68 | |
69 | conn->state = BT_CONNECT; | |
a0c808b3 | 70 | conn->out = true; |
a8746417 | 71 | |
1da177e4 LT |
72 | conn->link_mode = HCI_LM_MASTER; |
73 | ||
4c67bc74 MH |
74 | conn->attempt++; |
75 | ||
e4e8e37c MH |
76 | conn->link_policy = hdev->link_policy; |
77 | ||
1da177e4 LT |
78 | memset(&cp, 0, sizeof(cp)); |
79 | bacpy(&cp.bdaddr, &conn->dst); | |
80 | cp.pscan_rep_mode = 0x02; | |
81 | ||
70f23020 AE |
82 | ie = hci_inquiry_cache_lookup(hdev, &conn->dst); |
83 | if (ie) { | |
41a96212 MH |
84 | if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) { |
85 | cp.pscan_rep_mode = ie->data.pscan_rep_mode; | |
86 | cp.pscan_mode = ie->data.pscan_mode; | |
87 | cp.clock_offset = ie->data.clock_offset | | |
82781e63 | 88 | __constant_cpu_to_le16(0x8000); |
41a96212 MH |
89 | } |
90 | ||
1da177e4 | 91 | memcpy(conn->dev_class, ie->data.dev_class, 3); |
58a681ef JH |
92 | if (ie->data.ssp_mode > 0) |
93 | set_bit(HCI_CONN_SSP_ENABLED, &conn->flags); | |
1da177e4 LT |
94 | } |
95 | ||
a8746417 | 96 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
1da177e4 | 97 | if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER)) |
b6a0dc82 | 98 | cp.role_switch = 0x01; |
1da177e4 | 99 | else |
b6a0dc82 | 100 | cp.role_switch = 0x00; |
4c67bc74 | 101 | |
a9de9248 | 102 | hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp); |
1da177e4 LT |
103 | } |
104 | ||
6ac59344 MH |
105 | static void hci_acl_connect_cancel(struct hci_conn *conn) |
106 | { | |
107 | struct hci_cp_create_conn_cancel cp; | |
108 | ||
109 | BT_DBG("%p", conn); | |
110 | ||
d095c1eb | 111 | if (conn->hdev->hci_ver < BLUETOOTH_VER_1_2) |
6ac59344 MH |
112 | return; |
113 | ||
114 | bacpy(&cp.bdaddr, &conn->dst); | |
a9de9248 | 115 | hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp); |
6ac59344 MH |
116 | } |
117 | ||
1da177e4 LT |
118 | void hci_acl_disconn(struct hci_conn *conn, __u8 reason) |
119 | { | |
120 | struct hci_cp_disconnect cp; | |
121 | ||
122 | BT_DBG("%p", conn); | |
123 | ||
124 | conn->state = BT_DISCONN; | |
125 | ||
aca3192c | 126 | cp.handle = cpu_to_le16(conn->handle); |
1da177e4 | 127 | cp.reason = reason; |
a9de9248 | 128 | hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp); |
1da177e4 LT |
129 | } |
130 | ||
131 | void hci_add_sco(struct hci_conn *conn, __u16 handle) | |
132 | { | |
133 | struct hci_dev *hdev = conn->hdev; | |
134 | struct hci_cp_add_sco cp; | |
135 | ||
136 | BT_DBG("%p", conn); | |
137 | ||
138 | conn->state = BT_CONNECT; | |
a0c808b3 | 139 | conn->out = true; |
1da177e4 | 140 | |
efc7688b MH |
141 | conn->attempt++; |
142 | ||
aca3192c | 143 | cp.handle = cpu_to_le16(handle); |
a8746417 | 144 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
1da177e4 | 145 | |
a9de9248 | 146 | hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); |
1da177e4 LT |
147 | } |
148 | ||
b6a0dc82 MH |
149 | void hci_setup_sync(struct hci_conn *conn, __u16 handle) |
150 | { | |
151 | struct hci_dev *hdev = conn->hdev; | |
152 | struct hci_cp_setup_sync_conn cp; | |
153 | ||
154 | BT_DBG("%p", conn); | |
155 | ||
156 | conn->state = BT_CONNECT; | |
a0c808b3 | 157 | conn->out = true; |
b6a0dc82 | 158 | |
efc7688b MH |
159 | conn->attempt++; |
160 | ||
b6a0dc82 | 161 | cp.handle = cpu_to_le16(handle); |
a8746417 | 162 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
b6a0dc82 | 163 | |
82781e63 AE |
164 | cp.tx_bandwidth = __constant_cpu_to_le32(0x00001f40); |
165 | cp.rx_bandwidth = __constant_cpu_to_le32(0x00001f40); | |
166 | cp.max_latency = __constant_cpu_to_le16(0xffff); | |
b6a0dc82 MH |
167 | cp.voice_setting = cpu_to_le16(hdev->voice_setting); |
168 | cp.retrans_effort = 0xff; | |
169 | ||
170 | hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp); | |
171 | } | |
172 | ||
2ce603eb | 173 | void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, |
5974e4c4 | 174 | u16 latency, u16 to_multiplier) |
2ce603eb CT |
175 | { |
176 | struct hci_cp_le_conn_update cp; | |
177 | struct hci_dev *hdev = conn->hdev; | |
178 | ||
179 | memset(&cp, 0, sizeof(cp)); | |
180 | ||
181 | cp.handle = cpu_to_le16(conn->handle); | |
182 | cp.conn_interval_min = cpu_to_le16(min); | |
183 | cp.conn_interval_max = cpu_to_le16(max); | |
184 | cp.conn_latency = cpu_to_le16(latency); | |
185 | cp.supervision_timeout = cpu_to_le16(to_multiplier); | |
82781e63 AE |
186 | cp.min_ce_len = __constant_cpu_to_le16(0x0001); |
187 | cp.max_ce_len = __constant_cpu_to_le16(0x0001); | |
2ce603eb CT |
188 | |
189 | hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp); | |
190 | } | |
2ce603eb | 191 | |
a7a595f6 | 192 | void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8], |
5974e4c4 | 193 | __u8 ltk[16]) |
a7a595f6 VCG |
194 | { |
195 | struct hci_dev *hdev = conn->hdev; | |
196 | struct hci_cp_le_start_enc cp; | |
197 | ||
198 | BT_DBG("%p", conn); | |
199 | ||
200 | memset(&cp, 0, sizeof(cp)); | |
201 | ||
202 | cp.handle = cpu_to_le16(conn->handle); | |
203 | memcpy(cp.ltk, ltk, sizeof(cp.ltk)); | |
204 | cp.ediv = ediv; | |
51beabdf | 205 | memcpy(cp.rand, rand, sizeof(cp.rand)); |
a7a595f6 VCG |
206 | |
207 | hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp); | |
208 | } | |
a7a595f6 | 209 | |
e73439d8 MH |
210 | /* Device _must_ be locked */ |
211 | void hci_sco_setup(struct hci_conn *conn, __u8 status) | |
212 | { | |
213 | struct hci_conn *sco = conn->link; | |
214 | ||
215 | BT_DBG("%p", conn); | |
216 | ||
217 | if (!sco) | |
218 | return; | |
219 | ||
220 | if (!status) { | |
221 | if (lmp_esco_capable(conn->hdev)) | |
222 | hci_setup_sync(sco, conn->handle); | |
223 | else | |
224 | hci_add_sco(sco, conn->handle); | |
225 | } else { | |
226 | hci_proto_connect_cfm(sco, status); | |
227 | hci_conn_del(sco); | |
228 | } | |
229 | } | |
230 | ||
19c40e3b | 231 | static void hci_conn_timeout(struct work_struct *work) |
1da177e4 | 232 | { |
19c40e3b | 233 | struct hci_conn *conn = container_of(work, struct hci_conn, |
5974e4c4 | 234 | disc_work.work); |
2950f21a | 235 | __u8 reason; |
1da177e4 | 236 | |
e05dcc32 | 237 | BT_DBG("conn %p state %s", conn, state_to_string(conn->state)); |
1da177e4 LT |
238 | |
239 | if (atomic_read(&conn->refcnt)) | |
240 | return; | |
241 | ||
6ac59344 MH |
242 | switch (conn->state) { |
243 | case BT_CONNECT: | |
769be974 | 244 | case BT_CONNECT2: |
fcd89c09 VT |
245 | if (conn->out) { |
246 | if (conn->type == ACL_LINK) | |
247 | hci_acl_connect_cancel(conn); | |
248 | else if (conn->type == LE_LINK) | |
249 | hci_le_connect_cancel(conn); | |
250 | } | |
6ac59344 | 251 | break; |
769be974 | 252 | case BT_CONFIG: |
8e87d142 | 253 | case BT_CONNECTED: |
2950f21a MH |
254 | reason = hci_proto_disconn_ind(conn); |
255 | hci_acl_disconn(conn, reason); | |
6ac59344 MH |
256 | break; |
257 | default: | |
1da177e4 | 258 | conn->state = BT_CLOSED; |
6ac59344 MH |
259 | break; |
260 | } | |
1da177e4 LT |
261 | } |
262 | ||
416dc94b GP |
263 | /* Enter sniff mode */ |
264 | static void hci_conn_enter_sniff_mode(struct hci_conn *conn) | |
265 | { | |
266 | struct hci_dev *hdev = conn->hdev; | |
267 | ||
268 | BT_DBG("conn %p mode %d", conn, conn->mode); | |
269 | ||
270 | if (test_bit(HCI_RAW, &hdev->flags)) | |
271 | return; | |
272 | ||
273 | if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn)) | |
274 | return; | |
275 | ||
276 | if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF)) | |
277 | return; | |
278 | ||
279 | if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) { | |
280 | struct hci_cp_sniff_subrate cp; | |
281 | cp.handle = cpu_to_le16(conn->handle); | |
82781e63 AE |
282 | cp.max_latency = __constant_cpu_to_le16(0); |
283 | cp.min_remote_timeout = __constant_cpu_to_le16(0); | |
284 | cp.min_local_timeout = __constant_cpu_to_le16(0); | |
416dc94b GP |
285 | hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp); |
286 | } | |
287 | ||
51a8efd7 | 288 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { |
416dc94b GP |
289 | struct hci_cp_sniff_mode cp; |
290 | cp.handle = cpu_to_le16(conn->handle); | |
291 | cp.max_interval = cpu_to_le16(hdev->sniff_max_interval); | |
292 | cp.min_interval = cpu_to_le16(hdev->sniff_min_interval); | |
82781e63 AE |
293 | cp.attempt = __constant_cpu_to_le16(4); |
294 | cp.timeout = __constant_cpu_to_le16(1); | |
416dc94b GP |
295 | hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp); |
296 | } | |
297 | } | |
298 | ||
04837f64 | 299 | static void hci_conn_idle(unsigned long arg) |
1da177e4 | 300 | { |
04837f64 MH |
301 | struct hci_conn *conn = (void *) arg; |
302 | ||
303 | BT_DBG("conn %p mode %d", conn, conn->mode); | |
304 | ||
305 | hci_conn_enter_sniff_mode(conn); | |
1da177e4 LT |
306 | } |
307 | ||
9f61656a JH |
308 | static void hci_conn_auto_accept(unsigned long arg) |
309 | { | |
310 | struct hci_conn *conn = (void *) arg; | |
311 | struct hci_dev *hdev = conn->hdev; | |
312 | ||
9f61656a | 313 | hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst), |
5974e4c4 | 314 | &conn->dst); |
9f61656a JH |
315 | } |
316 | ||
1da177e4 LT |
317 | struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst) |
318 | { | |
319 | struct hci_conn *conn; | |
320 | ||
321 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); | |
322 | ||
cb601d7e | 323 | conn = kzalloc(sizeof(struct hci_conn), GFP_KERNEL); |
04837f64 | 324 | if (!conn) |
1da177e4 | 325 | return NULL; |
1da177e4 LT |
326 | |
327 | bacpy(&conn->dst, dst); | |
a8746417 MH |
328 | conn->hdev = hdev; |
329 | conn->type = type; | |
330 | conn->mode = HCI_CM_ACTIVE; | |
331 | conn->state = BT_OPEN; | |
93f19c9f | 332 | conn->auth_type = HCI_AT_GENERAL_BONDING; |
17fa4b9d | 333 | conn->io_capability = hdev->io_capability; |
a9583556 | 334 | conn->remote_auth = 0xff; |
13d39315 | 335 | conn->key_type = 0xff; |
1da177e4 | 336 | |
58a681ef | 337 | set_bit(HCI_CONN_POWER_SAVE, &conn->flags); |
052b30b0 | 338 | conn->disc_timeout = HCI_DISCONN_TIMEOUT; |
04837f64 | 339 | |
a8746417 MH |
340 | switch (type) { |
341 | case ACL_LINK: | |
342 | conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; | |
343 | break; | |
344 | case SCO_LINK: | |
345 | if (lmp_esco_capable(hdev)) | |
efc7688b MH |
346 | conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) | |
347 | (hdev->esco_type & EDR_ESCO_MASK); | |
a8746417 MH |
348 | else |
349 | conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK; | |
350 | break; | |
351 | case ESCO_LINK: | |
efc7688b | 352 | conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK; |
a8746417 MH |
353 | break; |
354 | } | |
355 | ||
1da177e4 | 356 | skb_queue_head_init(&conn->data_q); |
04837f64 | 357 | |
70c1f20b | 358 | INIT_LIST_HEAD(&conn->chan_list); |
73d80deb | 359 | |
19c40e3b | 360 | INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout); |
b24b8a24 | 361 | setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn); |
9f61656a | 362 | setup_timer(&conn->auto_accept_timer, hci_conn_auto_accept, |
5974e4c4 | 363 | (unsigned long) conn); |
1da177e4 LT |
364 | |
365 | atomic_set(&conn->refcnt, 0); | |
366 | ||
367 | hci_dev_hold(hdev); | |
368 | ||
1da177e4 | 369 | hci_conn_hash_add(hdev, conn); |
3c54711c | 370 | if (hdev->notify) |
1da177e4 LT |
371 | hdev->notify(hdev, HCI_NOTIFY_CONN_ADD); |
372 | ||
9eba32b8 MH |
373 | atomic_set(&conn->devref, 0); |
374 | ||
a67e899c MH |
375 | hci_conn_init_sysfs(conn); |
376 | ||
1da177e4 LT |
377 | return conn; |
378 | } | |
379 | ||
380 | int hci_conn_del(struct hci_conn *conn) | |
381 | { | |
382 | struct hci_dev *hdev = conn->hdev; | |
383 | ||
384 | BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle); | |
385 | ||
04837f64 MH |
386 | del_timer(&conn->idle_timer); |
387 | ||
19c40e3b | 388 | cancel_delayed_work_sync(&conn->disc_work); |
1da177e4 | 389 | |
9f61656a JH |
390 | del_timer(&conn->auto_accept_timer); |
391 | ||
5b7f9909 | 392 | if (conn->type == ACL_LINK) { |
1da177e4 LT |
393 | struct hci_conn *sco = conn->link; |
394 | if (sco) | |
395 | sco->link = NULL; | |
396 | ||
397 | /* Unacked frames */ | |
398 | hdev->acl_cnt += conn->sent; | |
6ed58ec5 VT |
399 | } else if (conn->type == LE_LINK) { |
400 | if (hdev->le_pkts) | |
401 | hdev->le_cnt += conn->sent; | |
402 | else | |
403 | hdev->acl_cnt += conn->sent; | |
5b7f9909 MH |
404 | } else { |
405 | struct hci_conn *acl = conn->link; | |
406 | if (acl) { | |
407 | acl->link = NULL; | |
408 | hci_conn_put(acl); | |
409 | } | |
1da177e4 LT |
410 | } |
411 | ||
2c33c06a | 412 | hci_chan_list_flush(conn); |
73d80deb | 413 | |
1da177e4 | 414 | hci_conn_hash_del(hdev, conn); |
3c54711c | 415 | if (hdev->notify) |
1da177e4 | 416 | hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); |
7d0db0a3 | 417 | |
1da177e4 | 418 | skb_queue_purge(&conn->data_q); |
1da177e4 | 419 | |
9eba32b8 | 420 | hci_conn_put_device(conn); |
2ae9a6be | 421 | |
384943ec MH |
422 | hci_dev_put(hdev); |
423 | ||
163f4dab TT |
424 | if (conn->handle == 0) |
425 | kfree(conn); | |
426 | ||
1da177e4 LT |
427 | return 0; |
428 | } | |
429 | ||
430 | struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src) | |
431 | { | |
432 | int use_src = bacmp(src, BDADDR_ANY); | |
8035ded4 | 433 | struct hci_dev *hdev = NULL, *d; |
1da177e4 LT |
434 | |
435 | BT_DBG("%s -> %s", batostr(src), batostr(dst)); | |
436 | ||
f20d09d5 | 437 | read_lock(&hci_dev_list_lock); |
1da177e4 | 438 | |
8035ded4 | 439 | list_for_each_entry(d, &hci_dev_list, list) { |
8fc9ced3 GP |
440 | if (!test_bit(HCI_UP, &d->flags) || |
441 | test_bit(HCI_RAW, &d->flags)) | |
1da177e4 LT |
442 | continue; |
443 | ||
8e87d142 | 444 | /* Simple routing: |
1da177e4 LT |
445 | * No source address - find interface with bdaddr != dst |
446 | * Source address - find interface with bdaddr == src | |
447 | */ | |
448 | ||
449 | if (use_src) { | |
450 | if (!bacmp(&d->bdaddr, src)) { | |
451 | hdev = d; break; | |
452 | } | |
453 | } else { | |
454 | if (bacmp(&d->bdaddr, dst)) { | |
455 | hdev = d; break; | |
456 | } | |
457 | } | |
458 | } | |
459 | ||
460 | if (hdev) | |
461 | hdev = hci_dev_hold(hdev); | |
462 | ||
f20d09d5 | 463 | read_unlock(&hci_dev_list_lock); |
1da177e4 LT |
464 | return hdev; |
465 | } | |
466 | EXPORT_SYMBOL(hci_get_route); | |
467 | ||
fcd89c09 | 468 | /* Create SCO, ACL or LE connection. |
1da177e4 | 469 | * Device _must_ be locked */ |
b12f62cf AG |
470 | struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, |
471 | __u8 dst_type, __u8 sec_level, __u8 auth_type) | |
1da177e4 LT |
472 | { |
473 | struct hci_conn *acl; | |
5b7f9909 | 474 | struct hci_conn *sco; |
fcd89c09 | 475 | struct hci_conn *le; |
1da177e4 LT |
476 | |
477 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); | |
478 | ||
fcd89c09 VT |
479 | if (type == LE_LINK) { |
480 | le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst); | |
9f0caeb1 | 481 | if (!le) { |
9f0caeb1 VCG |
482 | le = hci_conn_add(hdev, LE_LINK, dst); |
483 | if (!le) | |
484 | return ERR_PTR(-ENOMEM); | |
485 | ||
b12f62cf | 486 | le->dst_type = bdaddr_to_le(dst_type); |
9f0caeb1 VCG |
487 | hci_le_connect(le); |
488 | } | |
eda42b50 | 489 | |
9f0caeb1 VCG |
490 | le->pending_sec_level = sec_level; |
491 | le->auth_type = auth_type; | |
fcd89c09 VT |
492 | |
493 | hci_conn_hold(le); | |
494 | ||
495 | return le; | |
496 | } | |
497 | ||
70f23020 AE |
498 | acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); |
499 | if (!acl) { | |
500 | acl = hci_conn_add(hdev, ACL_LINK, dst); | |
501 | if (!acl) | |
48c7aba9 | 502 | return ERR_PTR(-ENOMEM); |
1da177e4 LT |
503 | } |
504 | ||
505 | hci_conn_hold(acl); | |
506 | ||
09ab6f4c | 507 | if (acl->state == BT_OPEN || acl->state == BT_CLOSED) { |
765c2a96 JH |
508 | acl->sec_level = BT_SECURITY_LOW; |
509 | acl->pending_sec_level = sec_level; | |
09ab6f4c | 510 | acl->auth_type = auth_type; |
1da177e4 | 511 | hci_acl_connect(acl); |
09ab6f4c | 512 | } |
1da177e4 | 513 | |
5b7f9909 MH |
514 | if (type == ACL_LINK) |
515 | return acl; | |
1da177e4 | 516 | |
70f23020 AE |
517 | sco = hci_conn_hash_lookup_ba(hdev, type, dst); |
518 | if (!sco) { | |
519 | sco = hci_conn_add(hdev, type, dst); | |
520 | if (!sco) { | |
5b7f9909 | 521 | hci_conn_put(acl); |
48c7aba9 | 522 | return ERR_PTR(-ENOMEM); |
1da177e4 | 523 | } |
5b7f9909 | 524 | } |
1da177e4 | 525 | |
5b7f9909 MH |
526 | acl->link = sco; |
527 | sco->link = acl; | |
1da177e4 | 528 | |
5b7f9909 | 529 | hci_conn_hold(sco); |
1da177e4 | 530 | |
5b7f9909 | 531 | if (acl->state == BT_CONNECTED && |
5974e4c4 | 532 | (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { |
58a681ef | 533 | set_bit(HCI_CONN_POWER_SAVE, &acl->flags); |
14b12d0b | 534 | hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON); |
c390216b | 535 | |
51a8efd7 | 536 | if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) { |
e73439d8 | 537 | /* defer SCO setup until mode change completed */ |
51a8efd7 | 538 | set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags); |
e73439d8 MH |
539 | return sco; |
540 | } | |
541 | ||
542 | hci_sco_setup(acl, 0x00); | |
b6a0dc82 | 543 | } |
5b7f9909 MH |
544 | |
545 | return sco; | |
1da177e4 | 546 | } |
1da177e4 | 547 | |
e7c29cb1 MH |
548 | /* Check link security requirement */ |
549 | int hci_conn_check_link_mode(struct hci_conn *conn) | |
550 | { | |
551 | BT_DBG("conn %p", conn); | |
552 | ||
aa64a8b5 | 553 | if (hci_conn_ssp_enabled(conn) && !(conn->link_mode & HCI_LM_ENCRYPT)) |
e7c29cb1 MH |
554 | return 0; |
555 | ||
556 | return 1; | |
557 | } | |
e7c29cb1 | 558 | |
1da177e4 | 559 | /* Authenticate remote device */ |
0684e5f9 | 560 | static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) |
1da177e4 LT |
561 | { |
562 | BT_DBG("conn %p", conn); | |
563 | ||
765c2a96 JH |
564 | if (conn->pending_sec_level > sec_level) |
565 | sec_level = conn->pending_sec_level; | |
566 | ||
96a31833 | 567 | if (sec_level > conn->sec_level) |
765c2a96 | 568 | conn->pending_sec_level = sec_level; |
96a31833 | 569 | else if (conn->link_mode & HCI_LM_AUTH) |
1da177e4 LT |
570 | return 1; |
571 | ||
65cf686e JH |
572 | /* Make sure we preserve an existing MITM requirement*/ |
573 | auth_type |= (conn->auth_type & 0x01); | |
574 | ||
96a31833 MH |
575 | conn->auth_type = auth_type; |
576 | ||
51a8efd7 | 577 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) { |
1da177e4 | 578 | struct hci_cp_auth_requested cp; |
b7d05bad PH |
579 | |
580 | /* encrypt must be pending if auth is also pending */ | |
581 | set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags); | |
582 | ||
aca3192c | 583 | cp.handle = cpu_to_le16(conn->handle); |
40be492f | 584 | hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, |
5974e4c4 | 585 | sizeof(cp), &cp); |
19f8def0 | 586 | if (conn->key_type != 0xff) |
51a8efd7 | 587 | set_bit(HCI_CONN_REAUTH_PEND, &conn->flags); |
1da177e4 | 588 | } |
8c1b2355 | 589 | |
1da177e4 LT |
590 | return 0; |
591 | } | |
1da177e4 | 592 | |
13d39315 WR |
593 | /* Encrypt the the link */ |
594 | static void hci_conn_encrypt(struct hci_conn *conn) | |
595 | { | |
596 | BT_DBG("conn %p", conn); | |
597 | ||
51a8efd7 | 598 | if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) { |
13d39315 WR |
599 | struct hci_cp_set_conn_encrypt cp; |
600 | cp.handle = cpu_to_le16(conn->handle); | |
601 | cp.encrypt = 0x01; | |
602 | hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp), | |
5974e4c4 | 603 | &cp); |
13d39315 WR |
604 | } |
605 | } | |
606 | ||
8c1b2355 | 607 | /* Enable security */ |
0684e5f9 | 608 | int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) |
1da177e4 LT |
609 | { |
610 | BT_DBG("conn %p", conn); | |
611 | ||
13d39315 | 612 | /* For sdp we don't need the link key. */ |
8c1b2355 MH |
613 | if (sec_level == BT_SECURITY_SDP) |
614 | return 1; | |
615 | ||
13d39315 WR |
616 | /* For non 2.1 devices and low security level we don't need the link |
617 | key. */ | |
aa64a8b5 | 618 | if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn)) |
3fdca1e1 | 619 | return 1; |
8c1b2355 | 620 | |
13d39315 WR |
621 | /* For other security levels we need the link key. */ |
622 | if (!(conn->link_mode & HCI_LM_AUTH)) | |
623 | goto auth; | |
624 | ||
625 | /* An authenticated combination key has sufficient security for any | |
626 | security level. */ | |
627 | if (conn->key_type == HCI_LK_AUTH_COMBINATION) | |
628 | goto encrypt; | |
629 | ||
630 | /* An unauthenticated combination key has sufficient security for | |
631 | security level 1 and 2. */ | |
632 | if (conn->key_type == HCI_LK_UNAUTH_COMBINATION && | |
5974e4c4 | 633 | (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW)) |
13d39315 WR |
634 | goto encrypt; |
635 | ||
636 | /* A combination key has always sufficient security for the security | |
637 | levels 1 or 2. High security level requires the combination key | |
638 | is generated using maximum PIN code length (16). | |
639 | For pre 2.1 units. */ | |
640 | if (conn->key_type == HCI_LK_COMBINATION && | |
5974e4c4 | 641 | (sec_level != BT_SECURITY_HIGH || conn->pin_length == 16)) |
13d39315 WR |
642 | goto encrypt; |
643 | ||
644 | auth: | |
51a8efd7 | 645 | if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) |
1da177e4 LT |
646 | return 0; |
647 | ||
6fdf658c LAD |
648 | if (!hci_conn_auth(conn, sec_level, auth_type)) |
649 | return 0; | |
13d39315 WR |
650 | |
651 | encrypt: | |
652 | if (conn->link_mode & HCI_LM_ENCRYPT) | |
653 | return 1; | |
8c1b2355 | 654 | |
13d39315 | 655 | hci_conn_encrypt(conn); |
1da177e4 LT |
656 | return 0; |
657 | } | |
8c1b2355 | 658 | EXPORT_SYMBOL(hci_conn_security); |
1da177e4 | 659 | |
b3b1b061 WR |
660 | /* Check secure link requirement */ |
661 | int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level) | |
662 | { | |
663 | BT_DBG("conn %p", conn); | |
664 | ||
665 | if (sec_level != BT_SECURITY_HIGH) | |
666 | return 1; /* Accept if non-secure is required */ | |
667 | ||
ef4177e2 | 668 | if (conn->sec_level == BT_SECURITY_HIGH) |
b3b1b061 WR |
669 | return 1; |
670 | ||
671 | return 0; /* Reject not secure link */ | |
672 | } | |
673 | EXPORT_SYMBOL(hci_conn_check_secure); | |
674 | ||
1da177e4 LT |
675 | /* Change link key */ |
676 | int hci_conn_change_link_key(struct hci_conn *conn) | |
677 | { | |
678 | BT_DBG("conn %p", conn); | |
679 | ||
51a8efd7 | 680 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) { |
1da177e4 | 681 | struct hci_cp_change_conn_link_key cp; |
aca3192c | 682 | cp.handle = cpu_to_le16(conn->handle); |
40be492f | 683 | hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY, |
5974e4c4 | 684 | sizeof(cp), &cp); |
1da177e4 | 685 | } |
8c1b2355 | 686 | |
1da177e4 LT |
687 | return 0; |
688 | } | |
1da177e4 LT |
689 | |
690 | /* Switch role */ | |
8c1b2355 | 691 | int hci_conn_switch_role(struct hci_conn *conn, __u8 role) |
1da177e4 LT |
692 | { |
693 | BT_DBG("conn %p", conn); | |
694 | ||
695 | if (!role && conn->link_mode & HCI_LM_MASTER) | |
696 | return 1; | |
697 | ||
51a8efd7 | 698 | if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) { |
1da177e4 LT |
699 | struct hci_cp_switch_role cp; |
700 | bacpy(&cp.bdaddr, &conn->dst); | |
701 | cp.role = role; | |
a9de9248 | 702 | hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp); |
1da177e4 | 703 | } |
8c1b2355 | 704 | |
1da177e4 LT |
705 | return 0; |
706 | } | |
707 | EXPORT_SYMBOL(hci_conn_switch_role); | |
708 | ||
04837f64 | 709 | /* Enter active mode */ |
14b12d0b | 710 | void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active) |
04837f64 MH |
711 | { |
712 | struct hci_dev *hdev = conn->hdev; | |
713 | ||
714 | BT_DBG("conn %p mode %d", conn, conn->mode); | |
715 | ||
716 | if (test_bit(HCI_RAW, &hdev->flags)) | |
717 | return; | |
718 | ||
14b12d0b JG |
719 | if (conn->mode != HCI_CM_SNIFF) |
720 | goto timer; | |
721 | ||
58a681ef | 722 | if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active) |
04837f64 MH |
723 | goto timer; |
724 | ||
51a8efd7 | 725 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { |
04837f64 | 726 | struct hci_cp_exit_sniff_mode cp; |
aca3192c | 727 | cp.handle = cpu_to_le16(conn->handle); |
a9de9248 | 728 | hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp); |
04837f64 MH |
729 | } |
730 | ||
731 | timer: | |
732 | if (hdev->idle_timeout > 0) | |
733 | mod_timer(&conn->idle_timer, | |
5974e4c4 | 734 | jiffies + msecs_to_jiffies(hdev->idle_timeout)); |
04837f64 MH |
735 | } |
736 | ||
1da177e4 LT |
737 | /* Drop all connection on the device */ |
738 | void hci_conn_hash_flush(struct hci_dev *hdev) | |
739 | { | |
740 | struct hci_conn_hash *h = &hdev->conn_hash; | |
3c4e0df0 | 741 | struct hci_conn *c, *n; |
1da177e4 LT |
742 | |
743 | BT_DBG("hdev %s", hdev->name); | |
744 | ||
3c4e0df0 | 745 | list_for_each_entry_safe(c, n, &h->list, list) { |
1da177e4 LT |
746 | c->state = BT_CLOSED; |
747 | ||
9f5a0d7b | 748 | hci_proto_disconn_cfm(c, HCI_ERROR_LOCAL_HOST_TERM); |
1da177e4 LT |
749 | hci_conn_del(c); |
750 | } | |
751 | } | |
752 | ||
a9de9248 MH |
753 | /* Check pending connect attempts */ |
754 | void hci_conn_check_pending(struct hci_dev *hdev) | |
755 | { | |
756 | struct hci_conn *conn; | |
757 | ||
758 | BT_DBG("hdev %s", hdev->name); | |
759 | ||
760 | hci_dev_lock(hdev); | |
761 | ||
762 | conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2); | |
763 | if (conn) | |
764 | hci_acl_connect(conn); | |
765 | ||
766 | hci_dev_unlock(hdev); | |
767 | } | |
768 | ||
9eba32b8 MH |
769 | void hci_conn_hold_device(struct hci_conn *conn) |
770 | { | |
771 | atomic_inc(&conn->devref); | |
772 | } | |
773 | EXPORT_SYMBOL(hci_conn_hold_device); | |
774 | ||
775 | void hci_conn_put_device(struct hci_conn *conn) | |
776 | { | |
777 | if (atomic_dec_and_test(&conn->devref)) | |
778 | hci_conn_del_sysfs(conn); | |
779 | } | |
780 | EXPORT_SYMBOL(hci_conn_put_device); | |
781 | ||
1da177e4 LT |
782 | int hci_get_conn_list(void __user *arg) |
783 | { | |
fc5fef61 | 784 | struct hci_conn *c; |
1da177e4 LT |
785 | struct hci_conn_list_req req, *cl; |
786 | struct hci_conn_info *ci; | |
787 | struct hci_dev *hdev; | |
1da177e4 LT |
788 | int n = 0, size, err; |
789 | ||
790 | if (copy_from_user(&req, arg, sizeof(req))) | |
791 | return -EFAULT; | |
792 | ||
793 | if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci)) | |
794 | return -EINVAL; | |
795 | ||
796 | size = sizeof(req) + req.conn_num * sizeof(*ci); | |
797 | ||
70f23020 AE |
798 | cl = kmalloc(size, GFP_KERNEL); |
799 | if (!cl) | |
1da177e4 LT |
800 | return -ENOMEM; |
801 | ||
70f23020 AE |
802 | hdev = hci_dev_get(req.dev_id); |
803 | if (!hdev) { | |
1da177e4 LT |
804 | kfree(cl); |
805 | return -ENODEV; | |
806 | } | |
807 | ||
808 | ci = cl->conn_info; | |
809 | ||
09fd0de5 | 810 | hci_dev_lock(hdev); |
8035ded4 | 811 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
1da177e4 LT |
812 | bacpy(&(ci + n)->bdaddr, &c->dst); |
813 | (ci + n)->handle = c->handle; | |
814 | (ci + n)->type = c->type; | |
815 | (ci + n)->out = c->out; | |
816 | (ci + n)->state = c->state; | |
817 | (ci + n)->link_mode = c->link_mode; | |
818 | if (++n >= req.conn_num) | |
819 | break; | |
820 | } | |
09fd0de5 | 821 | hci_dev_unlock(hdev); |
1da177e4 LT |
822 | |
823 | cl->dev_id = hdev->id; | |
824 | cl->conn_num = n; | |
825 | size = sizeof(req) + n * sizeof(*ci); | |
826 | ||
827 | hci_dev_put(hdev); | |
828 | ||
829 | err = copy_to_user(arg, cl, size); | |
830 | kfree(cl); | |
831 | ||
832 | return err ? -EFAULT : 0; | |
833 | } | |
834 | ||
835 | int hci_get_conn_info(struct hci_dev *hdev, void __user *arg) | |
836 | { | |
837 | struct hci_conn_info_req req; | |
838 | struct hci_conn_info ci; | |
839 | struct hci_conn *conn; | |
840 | char __user *ptr = arg + sizeof(req); | |
841 | ||
842 | if (copy_from_user(&req, arg, sizeof(req))) | |
843 | return -EFAULT; | |
844 | ||
09fd0de5 | 845 | hci_dev_lock(hdev); |
1da177e4 LT |
846 | conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr); |
847 | if (conn) { | |
848 | bacpy(&ci.bdaddr, &conn->dst); | |
849 | ci.handle = conn->handle; | |
850 | ci.type = conn->type; | |
851 | ci.out = conn->out; | |
852 | ci.state = conn->state; | |
853 | ci.link_mode = conn->link_mode; | |
854 | } | |
09fd0de5 | 855 | hci_dev_unlock(hdev); |
1da177e4 LT |
856 | |
857 | if (!conn) | |
858 | return -ENOENT; | |
859 | ||
860 | return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0; | |
861 | } | |
40be492f MH |
862 | |
863 | int hci_get_auth_info(struct hci_dev *hdev, void __user *arg) | |
864 | { | |
865 | struct hci_auth_info_req req; | |
866 | struct hci_conn *conn; | |
867 | ||
868 | if (copy_from_user(&req, arg, sizeof(req))) | |
869 | return -EFAULT; | |
870 | ||
09fd0de5 | 871 | hci_dev_lock(hdev); |
40be492f MH |
872 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); |
873 | if (conn) | |
874 | req.type = conn->auth_type; | |
09fd0de5 | 875 | hci_dev_unlock(hdev); |
40be492f MH |
876 | |
877 | if (!conn) | |
878 | return -ENOENT; | |
879 | ||
880 | return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; | |
881 | } | |
73d80deb LAD |
882 | |
883 | struct hci_chan *hci_chan_create(struct hci_conn *conn) | |
884 | { | |
885 | struct hci_dev *hdev = conn->hdev; | |
886 | struct hci_chan *chan; | |
887 | ||
888 | BT_DBG("%s conn %p", hdev->name, conn); | |
889 | ||
75d7735c | 890 | chan = kzalloc(sizeof(struct hci_chan), GFP_KERNEL); |
73d80deb LAD |
891 | if (!chan) |
892 | return NULL; | |
893 | ||
894 | chan->conn = conn; | |
895 | skb_queue_head_init(&chan->data_q); | |
896 | ||
8192edef | 897 | list_add_rcu(&chan->list, &conn->chan_list); |
73d80deb LAD |
898 | |
899 | return chan; | |
900 | } | |
901 | ||
902 | int hci_chan_del(struct hci_chan *chan) | |
903 | { | |
904 | struct hci_conn *conn = chan->conn; | |
905 | struct hci_dev *hdev = conn->hdev; | |
906 | ||
907 | BT_DBG("%s conn %p chan %p", hdev->name, conn, chan); | |
908 | ||
8192edef GP |
909 | list_del_rcu(&chan->list); |
910 | ||
911 | synchronize_rcu(); | |
73d80deb LAD |
912 | |
913 | skb_queue_purge(&chan->data_q); | |
914 | kfree(chan); | |
915 | ||
916 | return 0; | |
917 | } | |
918 | ||
2c33c06a | 919 | void hci_chan_list_flush(struct hci_conn *conn) |
73d80deb | 920 | { |
2a5a5ec6 | 921 | struct hci_chan *chan, *n; |
73d80deb LAD |
922 | |
923 | BT_DBG("conn %p", conn); | |
924 | ||
2a5a5ec6 | 925 | list_for_each_entry_safe(chan, n, &conn->chan_list, list) |
73d80deb LAD |
926 | hci_chan_del(chan); |
927 | } |