2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
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;
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
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
18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22 SOFTWARE IS DISCLAIMED.
25 /* Bluetooth HCI connection handling. */
27 #include <linux/export.h>
28 #include <linux/debugfs.h>
30 #include <net/bluetooth/bluetooth.h>
31 #include <net/bluetooth/hci_core.h>
32 #include <net/bluetooth/l2cap.h>
34 #include "hci_request.h"
44 static const struct sco_param esco_param_cvsd[] = {
45 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a, 0x01 }, /* S3 */
46 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007, 0x01 }, /* S2 */
47 { EDR_ESCO_MASK | ESCO_EV3, 0x0007, 0x01 }, /* S1 */
48 { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0x01 }, /* D1 */
49 { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0x01 }, /* D0 */
52 static const struct sco_param sco_param_cvsd[] = {
53 { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0xff }, /* D1 */
54 { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0xff }, /* D0 */
57 static const struct sco_param esco_param_msbc[] = {
58 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d, 0x02 }, /* T2 */
59 { EDR_ESCO_MASK | ESCO_EV3, 0x0008, 0x02 }, /* T1 */
62 /* This function requires the caller holds hdev->lock */
63 static void hci_connect_le_scan_cleanup(struct hci_conn *conn)
65 struct hci_conn_params *params;
66 struct hci_dev *hdev = conn->hdev;
72 bdaddr_type = conn->dst_type;
74 /* Check if we need to convert to identity address */
75 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
77 bdaddr = &irk->bdaddr;
78 bdaddr_type = irk->addr_type;
81 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, bdaddr,
83 if (!params || !params->explicit_connect)
86 /* The connection attempt was doing scan for new RPA, and is
87 * in scan phase. If params are not associated with any other
88 * autoconnect action, remove them completely. If they are, just unmark
89 * them as waiting for connection, by clearing explicit_connect field.
91 params->explicit_connect = false;
93 list_del_init(¶ms->action);
95 switch (params->auto_connect) {
96 case HCI_AUTO_CONN_EXPLICIT:
97 hci_conn_params_del(hdev, bdaddr, bdaddr_type);
98 /* return instead of break to avoid duplicate scan update */
100 case HCI_AUTO_CONN_DIRECT:
101 case HCI_AUTO_CONN_ALWAYS:
102 list_add(¶ms->action, &hdev->pend_le_conns);
104 case HCI_AUTO_CONN_REPORT:
105 list_add(¶ms->action, &hdev->pend_le_reports);
111 hci_update_passive_scan(hdev);
114 static void hci_conn_cleanup(struct hci_conn *conn)
116 struct hci_dev *hdev = conn->hdev;
118 if (test_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags))
119 hci_conn_params_del(conn->hdev, &conn->dst, conn->dst_type);
121 hci_chan_list_flush(conn);
123 hci_conn_hash_del(hdev, conn);
125 if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
126 switch (conn->setting & SCO_AIRMODE_MASK) {
127 case SCO_AIRMODE_CVSD:
128 case SCO_AIRMODE_TRANSP:
130 hdev->notify(hdev, HCI_NOTIFY_DISABLE_SCO);
135 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
138 hci_conn_del_sysfs(conn);
140 debugfs_remove_recursive(conn->debugfs);
147 static void le_scan_cleanup(struct work_struct *work)
149 struct hci_conn *conn = container_of(work, struct hci_conn,
151 struct hci_dev *hdev = conn->hdev;
152 struct hci_conn *c = NULL;
154 BT_DBG("%s hcon %p", hdev->name, conn);
158 /* Check that the hci_conn is still around */
160 list_for_each_entry_rcu(c, &hdev->conn_hash.list, list) {
167 hci_connect_le_scan_cleanup(conn);
168 hci_conn_cleanup(conn);
171 hci_dev_unlock(hdev);
176 static void hci_connect_le_scan_remove(struct hci_conn *conn)
178 BT_DBG("%s hcon %p", conn->hdev->name, conn);
180 /* We can't call hci_conn_del/hci_conn_cleanup here since that
181 * could deadlock with another hci_conn_del() call that's holding
182 * hci_dev_lock and doing cancel_delayed_work_sync(&conn->disc_work).
183 * Instead, grab temporary extra references to the hci_dev and
184 * hci_conn and perform the necessary cleanup in a separate work
188 hci_dev_hold(conn->hdev);
191 /* Even though we hold a reference to the hdev, many other
192 * things might get cleaned up meanwhile, including the hdev's
193 * own workqueue, so we can't use that for scheduling.
195 schedule_work(&conn->le_scan_cleanup);
198 static void hci_acl_create_connection(struct hci_conn *conn)
200 struct hci_dev *hdev = conn->hdev;
201 struct inquiry_entry *ie;
202 struct hci_cp_create_conn cp;
204 BT_DBG("hcon %p", conn);
206 /* Many controllers disallow HCI Create Connection while it is doing
207 * HCI Inquiry. So we cancel the Inquiry first before issuing HCI Create
208 * Connection. This may cause the MGMT discovering state to become false
209 * without user space's request but it is okay since the MGMT Discovery
210 * APIs do not promise that discovery should be done forever. Instead,
211 * the user space monitors the status of MGMT discovering and it may
212 * request for discovery again when this flag becomes false.
214 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
215 /* Put this connection to "pending" state so that it will be
216 * executed after the inquiry cancel command complete event.
218 conn->state = BT_CONNECT2;
219 hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL);
223 conn->state = BT_CONNECT;
225 conn->role = HCI_ROLE_MASTER;
229 conn->link_policy = hdev->link_policy;
231 memset(&cp, 0, sizeof(cp));
232 bacpy(&cp.bdaddr, &conn->dst);
233 cp.pscan_rep_mode = 0x02;
235 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
237 if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
238 cp.pscan_rep_mode = ie->data.pscan_rep_mode;
239 cp.pscan_mode = ie->data.pscan_mode;
240 cp.clock_offset = ie->data.clock_offset |
244 memcpy(conn->dev_class, ie->data.dev_class, 3);
247 cp.pkt_type = cpu_to_le16(conn->pkt_type);
248 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
249 cp.role_switch = 0x01;
251 cp.role_switch = 0x00;
253 hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
256 int hci_disconnect(struct hci_conn *conn, __u8 reason)
258 BT_DBG("hcon %p", conn);
260 /* When we are central of an established connection and it enters
261 * the disconnect timeout, then go ahead and try to read the
262 * current clock offset. Processing of the result is done
263 * within the event handling and hci_clock_offset_evt function.
265 if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER &&
266 (conn->state == BT_CONNECTED || conn->state == BT_CONFIG)) {
267 struct hci_dev *hdev = conn->hdev;
268 struct hci_cp_read_clock_offset clkoff_cp;
270 clkoff_cp.handle = cpu_to_le16(conn->handle);
271 hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp),
275 return hci_abort_conn(conn, reason);
278 static void hci_add_sco(struct hci_conn *conn, __u16 handle)
280 struct hci_dev *hdev = conn->hdev;
281 struct hci_cp_add_sco cp;
283 BT_DBG("hcon %p", conn);
285 conn->state = BT_CONNECT;
290 cp.handle = cpu_to_le16(handle);
291 cp.pkt_type = cpu_to_le16(conn->pkt_type);
293 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
296 static bool find_next_esco_param(struct hci_conn *conn,
297 const struct sco_param *esco_param, int size)
299 for (; conn->attempt <= size; conn->attempt++) {
300 if (lmp_esco_2m_capable(conn->link) ||
301 (esco_param[conn->attempt - 1].pkt_type & ESCO_2EV3))
303 BT_DBG("hcon %p skipped attempt %d, eSCO 2M not supported",
304 conn, conn->attempt);
307 return conn->attempt <= size;
310 static bool hci_enhanced_setup_sync_conn(struct hci_conn *conn, __u16 handle)
312 struct hci_dev *hdev = conn->hdev;
313 struct hci_cp_enhanced_setup_sync_conn cp;
314 const struct sco_param *param;
316 bt_dev_dbg(hdev, "hcon %p", conn);
318 /* for offload use case, codec needs to configured before opening SCO */
319 if (conn->codec.data_path)
320 hci_req_configure_datapath(hdev, &conn->codec);
322 conn->state = BT_CONNECT;
327 memset(&cp, 0x00, sizeof(cp));
329 cp.handle = cpu_to_le16(handle);
331 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
332 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
334 switch (conn->codec.id) {
336 if (!find_next_esco_param(conn, esco_param_msbc,
337 ARRAY_SIZE(esco_param_msbc)))
340 param = &esco_param_msbc[conn->attempt - 1];
341 cp.tx_coding_format.id = 0x05;
342 cp.rx_coding_format.id = 0x05;
343 cp.tx_codec_frame_size = __cpu_to_le16(60);
344 cp.rx_codec_frame_size = __cpu_to_le16(60);
345 cp.in_bandwidth = __cpu_to_le32(32000);
346 cp.out_bandwidth = __cpu_to_le32(32000);
347 cp.in_coding_format.id = 0x04;
348 cp.out_coding_format.id = 0x04;
349 cp.in_coded_data_size = __cpu_to_le16(16);
350 cp.out_coded_data_size = __cpu_to_le16(16);
351 cp.in_pcm_data_format = 2;
352 cp.out_pcm_data_format = 2;
353 cp.in_pcm_sample_payload_msb_pos = 0;
354 cp.out_pcm_sample_payload_msb_pos = 0;
355 cp.in_data_path = conn->codec.data_path;
356 cp.out_data_path = conn->codec.data_path;
357 cp.in_transport_unit_size = 1;
358 cp.out_transport_unit_size = 1;
361 case BT_CODEC_TRANSPARENT:
362 if (!find_next_esco_param(conn, esco_param_msbc,
363 ARRAY_SIZE(esco_param_msbc)))
365 param = &esco_param_msbc[conn->attempt - 1];
366 cp.tx_coding_format.id = 0x03;
367 cp.rx_coding_format.id = 0x03;
368 cp.tx_codec_frame_size = __cpu_to_le16(60);
369 cp.rx_codec_frame_size = __cpu_to_le16(60);
370 cp.in_bandwidth = __cpu_to_le32(0x1f40);
371 cp.out_bandwidth = __cpu_to_le32(0x1f40);
372 cp.in_coding_format.id = 0x03;
373 cp.out_coding_format.id = 0x03;
374 cp.in_coded_data_size = __cpu_to_le16(16);
375 cp.out_coded_data_size = __cpu_to_le16(16);
376 cp.in_pcm_data_format = 2;
377 cp.out_pcm_data_format = 2;
378 cp.in_pcm_sample_payload_msb_pos = 0;
379 cp.out_pcm_sample_payload_msb_pos = 0;
380 cp.in_data_path = conn->codec.data_path;
381 cp.out_data_path = conn->codec.data_path;
382 cp.in_transport_unit_size = 1;
383 cp.out_transport_unit_size = 1;
387 if (lmp_esco_capable(conn->link)) {
388 if (!find_next_esco_param(conn, esco_param_cvsd,
389 ARRAY_SIZE(esco_param_cvsd)))
391 param = &esco_param_cvsd[conn->attempt - 1];
393 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
395 param = &sco_param_cvsd[conn->attempt - 1];
397 cp.tx_coding_format.id = 2;
398 cp.rx_coding_format.id = 2;
399 cp.tx_codec_frame_size = __cpu_to_le16(60);
400 cp.rx_codec_frame_size = __cpu_to_le16(60);
401 cp.in_bandwidth = __cpu_to_le32(16000);
402 cp.out_bandwidth = __cpu_to_le32(16000);
403 cp.in_coding_format.id = 4;
404 cp.out_coding_format.id = 4;
405 cp.in_coded_data_size = __cpu_to_le16(16);
406 cp.out_coded_data_size = __cpu_to_le16(16);
407 cp.in_pcm_data_format = 2;
408 cp.out_pcm_data_format = 2;
409 cp.in_pcm_sample_payload_msb_pos = 0;
410 cp.out_pcm_sample_payload_msb_pos = 0;
411 cp.in_data_path = conn->codec.data_path;
412 cp.out_data_path = conn->codec.data_path;
413 cp.in_transport_unit_size = 16;
414 cp.out_transport_unit_size = 16;
420 cp.retrans_effort = param->retrans_effort;
421 cp.pkt_type = __cpu_to_le16(param->pkt_type);
422 cp.max_latency = __cpu_to_le16(param->max_latency);
424 if (hci_send_cmd(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
430 static bool hci_setup_sync_conn(struct hci_conn *conn, __u16 handle)
432 struct hci_dev *hdev = conn->hdev;
433 struct hci_cp_setup_sync_conn cp;
434 const struct sco_param *param;
436 bt_dev_dbg(hdev, "hcon %p", conn);
438 conn->state = BT_CONNECT;
443 cp.handle = cpu_to_le16(handle);
445 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
446 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
447 cp.voice_setting = cpu_to_le16(conn->setting);
449 switch (conn->setting & SCO_AIRMODE_MASK) {
450 case SCO_AIRMODE_TRANSP:
451 if (!find_next_esco_param(conn, esco_param_msbc,
452 ARRAY_SIZE(esco_param_msbc)))
454 param = &esco_param_msbc[conn->attempt - 1];
456 case SCO_AIRMODE_CVSD:
457 if (lmp_esco_capable(conn->link)) {
458 if (!find_next_esco_param(conn, esco_param_cvsd,
459 ARRAY_SIZE(esco_param_cvsd)))
461 param = &esco_param_cvsd[conn->attempt - 1];
463 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
465 param = &sco_param_cvsd[conn->attempt - 1];
472 cp.retrans_effort = param->retrans_effort;
473 cp.pkt_type = __cpu_to_le16(param->pkt_type);
474 cp.max_latency = __cpu_to_le16(param->max_latency);
476 if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
482 bool hci_setup_sync(struct hci_conn *conn, __u16 handle)
484 if (enhanced_sync_conn_capable(conn->hdev))
485 return hci_enhanced_setup_sync_conn(conn, handle);
487 return hci_setup_sync_conn(conn, handle);
490 u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
493 struct hci_dev *hdev = conn->hdev;
494 struct hci_conn_params *params;
495 struct hci_cp_le_conn_update cp;
499 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
501 params->conn_min_interval = min;
502 params->conn_max_interval = max;
503 params->conn_latency = latency;
504 params->supervision_timeout = to_multiplier;
507 hci_dev_unlock(hdev);
509 memset(&cp, 0, sizeof(cp));
510 cp.handle = cpu_to_le16(conn->handle);
511 cp.conn_interval_min = cpu_to_le16(min);
512 cp.conn_interval_max = cpu_to_le16(max);
513 cp.conn_latency = cpu_to_le16(latency);
514 cp.supervision_timeout = cpu_to_le16(to_multiplier);
515 cp.min_ce_len = cpu_to_le16(0x0000);
516 cp.max_ce_len = cpu_to_le16(0x0000);
518 hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
526 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
527 __u8 ltk[16], __u8 key_size)
529 struct hci_dev *hdev = conn->hdev;
530 struct hci_cp_le_start_enc cp;
532 BT_DBG("hcon %p", conn);
534 memset(&cp, 0, sizeof(cp));
536 cp.handle = cpu_to_le16(conn->handle);
539 memcpy(cp.ltk, ltk, key_size);
541 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
544 /* Device _must_ be locked */
545 void hci_sco_setup(struct hci_conn *conn, __u8 status)
547 struct hci_conn *sco = conn->link;
552 BT_DBG("hcon %p", conn);
555 if (lmp_esco_capable(conn->hdev))
556 hci_setup_sync(sco, conn->handle);
558 hci_add_sco(sco, conn->handle);
560 hci_connect_cfm(sco, status);
565 static void hci_conn_timeout(struct work_struct *work)
567 struct hci_conn *conn = container_of(work, struct hci_conn,
569 int refcnt = atomic_read(&conn->refcnt);
571 BT_DBG("hcon %p state %s", conn, state_to_string(conn->state));
575 /* FIXME: It was observed that in pairing failed scenario, refcnt
576 * drops below 0. Probably this is because l2cap_conn_del calls
577 * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is
578 * dropped. After that loop hci_chan_del is called which also drops
579 * conn. For now make sure that ACL is alive if refcnt is higher then 0,
585 /* LE connections in scanning state need special handling */
586 if (conn->state == BT_CONNECT && conn->type == LE_LINK &&
587 test_bit(HCI_CONN_SCANNING, &conn->flags)) {
588 hci_connect_le_scan_remove(conn);
592 hci_abort_conn(conn, hci_proto_disconn_ind(conn));
595 /* Enter sniff mode */
596 static void hci_conn_idle(struct work_struct *work)
598 struct hci_conn *conn = container_of(work, struct hci_conn,
600 struct hci_dev *hdev = conn->hdev;
602 BT_DBG("hcon %p mode %d", conn, conn->mode);
604 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
607 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
610 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
611 struct hci_cp_sniff_subrate cp;
612 cp.handle = cpu_to_le16(conn->handle);
613 cp.max_latency = cpu_to_le16(0);
614 cp.min_remote_timeout = cpu_to_le16(0);
615 cp.min_local_timeout = cpu_to_le16(0);
616 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
619 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
620 struct hci_cp_sniff_mode cp;
621 cp.handle = cpu_to_le16(conn->handle);
622 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
623 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
624 cp.attempt = cpu_to_le16(4);
625 cp.timeout = cpu_to_le16(1);
626 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
630 static void hci_conn_auto_accept(struct work_struct *work)
632 struct hci_conn *conn = container_of(work, struct hci_conn,
633 auto_accept_work.work);
635 hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
639 static void le_disable_advertising(struct hci_dev *hdev)
641 if (ext_adv_capable(hdev)) {
642 struct hci_cp_le_set_ext_adv_enable cp;
645 cp.num_of_sets = 0x00;
647 hci_send_cmd(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, sizeof(cp),
651 hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
656 static void le_conn_timeout(struct work_struct *work)
658 struct hci_conn *conn = container_of(work, struct hci_conn,
659 le_conn_timeout.work);
660 struct hci_dev *hdev = conn->hdev;
664 /* We could end up here due to having done directed advertising,
665 * so clean up the state if necessary. This should however only
666 * happen with broken hardware or if low duty cycle was used
667 * (which doesn't have a timeout of its own).
669 if (conn->role == HCI_ROLE_SLAVE) {
670 /* Disable LE Advertising */
671 le_disable_advertising(hdev);
673 hci_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT);
674 hci_dev_unlock(hdev);
678 hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
681 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
684 struct hci_conn *conn;
686 BT_DBG("%s dst %pMR", hdev->name, dst);
688 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
692 bacpy(&conn->dst, dst);
693 bacpy(&conn->src, &hdev->bdaddr);
694 conn->handle = HCI_CONN_HANDLE_UNSET;
698 conn->mode = HCI_CM_ACTIVE;
699 conn->state = BT_OPEN;
700 conn->auth_type = HCI_AT_GENERAL_BONDING;
701 conn->io_capability = hdev->io_capability;
702 conn->remote_auth = 0xff;
703 conn->key_type = 0xff;
704 conn->rssi = HCI_RSSI_INVALID;
705 conn->tx_power = HCI_TX_POWER_INVALID;
706 conn->max_tx_power = HCI_TX_POWER_INVALID;
708 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
709 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
711 /* Set Default Authenticated payload timeout to 30s */
712 conn->auth_payload_timeout = DEFAULT_AUTH_PAYLOAD_TIMEOUT;
714 if (conn->role == HCI_ROLE_MASTER)
719 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
722 /* conn->src should reflect the local identity address */
723 hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
726 if (lmp_esco_capable(hdev))
727 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
728 (hdev->esco_type & EDR_ESCO_MASK);
730 conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
733 conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK;
737 skb_queue_head_init(&conn->data_q);
739 INIT_LIST_HEAD(&conn->chan_list);
741 INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout);
742 INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept);
743 INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle);
744 INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout);
745 INIT_WORK(&conn->le_scan_cleanup, le_scan_cleanup);
747 atomic_set(&conn->refcnt, 0);
751 hci_conn_hash_add(hdev, conn);
753 /* The SCO and eSCO connections will only be notified when their
754 * setup has been completed. This is different to ACL links which
755 * can be notified right away.
757 if (conn->type != SCO_LINK && conn->type != ESCO_LINK) {
759 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
762 hci_conn_init_sysfs(conn);
767 int hci_conn_del(struct hci_conn *conn)
769 struct hci_dev *hdev = conn->hdev;
771 BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle);
773 cancel_delayed_work_sync(&conn->disc_work);
774 cancel_delayed_work_sync(&conn->auto_accept_work);
775 cancel_delayed_work_sync(&conn->idle_work);
777 if (conn->type == ACL_LINK) {
778 struct hci_conn *sco = conn->link;
783 hdev->acl_cnt += conn->sent;
784 } else if (conn->type == LE_LINK) {
785 cancel_delayed_work(&conn->le_conn_timeout);
788 hdev->le_cnt += conn->sent;
790 hdev->acl_cnt += conn->sent;
792 struct hci_conn *acl = conn->link;
800 amp_mgr_put(conn->amp_mgr);
802 skb_queue_purge(&conn->data_q);
804 /* Remove the connection from the list and cleanup its remaining
805 * state. This is a separate function since for some cases like
806 * BT_CONNECT_SCAN we *only* want the cleanup part without the
807 * rest of hci_conn_del.
809 hci_conn_cleanup(conn);
814 struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, uint8_t src_type)
816 int use_src = bacmp(src, BDADDR_ANY);
817 struct hci_dev *hdev = NULL, *d;
819 BT_DBG("%pMR -> %pMR", src, dst);
821 read_lock(&hci_dev_list_lock);
823 list_for_each_entry(d, &hci_dev_list, list) {
824 if (!test_bit(HCI_UP, &d->flags) ||
825 hci_dev_test_flag(d, HCI_USER_CHANNEL) ||
826 d->dev_type != HCI_PRIMARY)
830 * No source address - find interface with bdaddr != dst
831 * Source address - find interface with bdaddr == src
838 if (src_type == BDADDR_BREDR) {
839 if (!lmp_bredr_capable(d))
841 bacpy(&id_addr, &d->bdaddr);
842 id_addr_type = BDADDR_BREDR;
844 if (!lmp_le_capable(d))
847 hci_copy_identity_address(d, &id_addr,
850 /* Convert from HCI to three-value type */
851 if (id_addr_type == ADDR_LE_DEV_PUBLIC)
852 id_addr_type = BDADDR_LE_PUBLIC;
854 id_addr_type = BDADDR_LE_RANDOM;
857 if (!bacmp(&id_addr, src) && id_addr_type == src_type) {
861 if (bacmp(&d->bdaddr, dst)) {
868 hdev = hci_dev_hold(hdev);
870 read_unlock(&hci_dev_list_lock);
873 EXPORT_SYMBOL(hci_get_route);
875 /* This function requires the caller holds hdev->lock */
876 static void hci_le_conn_failed(struct hci_conn *conn, u8 status)
878 struct hci_dev *hdev = conn->hdev;
879 struct hci_conn_params *params;
881 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
883 if (params && params->conn) {
884 hci_conn_drop(params->conn);
885 hci_conn_put(params->conn);
889 /* If the status indicates successful cancellation of
890 * the attempt (i.e. Unknown Connection Id) there's no point of
891 * notifying failure since we'll go back to keep trying to
892 * connect. The only exception is explicit connect requests
893 * where a timeout + cancel does indicate an actual failure.
895 if (status != HCI_ERROR_UNKNOWN_CONN_ID ||
896 (params && params->explicit_connect))
897 mgmt_connect_failed(hdev, &conn->dst, conn->type,
898 conn->dst_type, status);
900 /* Since we may have temporarily stopped the background scanning in
901 * favor of connection establishment, we should restart it.
903 hci_update_passive_scan(hdev);
905 /* Enable advertising in case this was a failed connection
906 * attempt as a peripheral.
908 hci_enable_advertising(hdev);
911 /* This function requires the caller holds hdev->lock */
912 void hci_conn_failed(struct hci_conn *conn, u8 status)
914 struct hci_dev *hdev = conn->hdev;
916 bt_dev_dbg(hdev, "status 0x%2.2x", status);
918 switch (conn->type) {
920 hci_le_conn_failed(conn, status);
923 mgmt_connect_failed(hdev, &conn->dst, conn->type,
924 conn->dst_type, status);
928 conn->state = BT_CLOSED;
929 hci_connect_cfm(conn, status);
933 static void create_le_conn_complete(struct hci_dev *hdev, void *data, int err)
935 struct hci_conn *conn = data;
940 hci_connect_le_scan_cleanup(conn);
944 bt_dev_err(hdev, "request failed to create LE connection: err %d", err);
946 /* Check if connection is still pending */
947 if (conn != hci_lookup_le_connect(hdev))
950 hci_conn_failed(conn, err);
953 hci_dev_unlock(hdev);
956 static int hci_connect_le_sync(struct hci_dev *hdev, void *data)
958 struct hci_conn *conn = data;
960 bt_dev_dbg(hdev, "conn %p", conn);
962 return hci_le_create_conn_sync(hdev, conn);
965 struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
966 u8 dst_type, bool dst_resolved, u8 sec_level,
967 u16 conn_timeout, u8 role)
969 struct hci_conn *conn;
973 /* Let's make sure that le is enabled.*/
974 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
975 if (lmp_le_capable(hdev))
976 return ERR_PTR(-ECONNREFUSED);
978 return ERR_PTR(-EOPNOTSUPP);
981 /* Since the controller supports only one LE connection attempt at a
982 * time, we return -EBUSY if there is any connection attempt running.
984 if (hci_lookup_le_connect(hdev))
985 return ERR_PTR(-EBUSY);
987 /* If there's already a connection object but it's not in
988 * scanning state it means it must already be established, in
989 * which case we can't do anything else except report a failure
992 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
993 if (conn && !test_bit(HCI_CONN_SCANNING, &conn->flags)) {
994 return ERR_PTR(-EBUSY);
997 /* Check if the destination address has been resolved by the controller
998 * since if it did then the identity address shall be used.
1000 if (!dst_resolved) {
1001 /* When given an identity address with existing identity
1002 * resolving key, the connection needs to be established
1003 * to a resolvable random address.
1005 * Storing the resolvable random address is required here
1006 * to handle connection failures. The address will later
1007 * be resolved back into the original identity address
1008 * from the connect request.
1010 irk = hci_find_irk_by_addr(hdev, dst, dst_type);
1011 if (irk && bacmp(&irk->rpa, BDADDR_ANY)) {
1013 dst_type = ADDR_LE_DEV_RANDOM;
1018 bacpy(&conn->dst, dst);
1020 conn = hci_conn_add(hdev, LE_LINK, dst, role);
1022 return ERR_PTR(-ENOMEM);
1023 hci_conn_hold(conn);
1024 conn->pending_sec_level = sec_level;
1027 conn->dst_type = dst_type;
1028 conn->sec_level = BT_SECURITY_LOW;
1029 conn->conn_timeout = conn_timeout;
1031 conn->state = BT_CONNECT;
1032 clear_bit(HCI_CONN_SCANNING, &conn->flags);
1034 err = hci_cmd_sync_queue(hdev, hci_connect_le_sync, conn,
1035 create_le_conn_complete);
1038 return ERR_PTR(err);
1044 static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
1046 struct hci_conn *conn;
1048 conn = hci_conn_hash_lookup_le(hdev, addr, type);
1052 if (conn->state != BT_CONNECTED)
1058 /* This function requires the caller holds hdev->lock */
1059 static int hci_explicit_conn_params_set(struct hci_dev *hdev,
1060 bdaddr_t *addr, u8 addr_type)
1062 struct hci_conn_params *params;
1064 if (is_connected(hdev, addr, addr_type))
1067 params = hci_conn_params_lookup(hdev, addr, addr_type);
1069 params = hci_conn_params_add(hdev, addr, addr_type);
1073 /* If we created new params, mark them to be deleted in
1074 * hci_connect_le_scan_cleanup. It's different case than
1075 * existing disabled params, those will stay after cleanup.
1077 params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
1080 /* We're trying to connect, so make sure params are at pend_le_conns */
1081 if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
1082 params->auto_connect == HCI_AUTO_CONN_REPORT ||
1083 params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
1084 list_del_init(¶ms->action);
1085 list_add(¶ms->action, &hdev->pend_le_conns);
1088 params->explicit_connect = true;
1090 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
1091 params->auto_connect);
1096 /* This function requires the caller holds hdev->lock */
1097 struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
1098 u8 dst_type, u8 sec_level,
1100 enum conn_reasons conn_reason)
1102 struct hci_conn *conn;
1104 /* Let's make sure that le is enabled.*/
1105 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1106 if (lmp_le_capable(hdev))
1107 return ERR_PTR(-ECONNREFUSED);
1109 return ERR_PTR(-EOPNOTSUPP);
1112 /* Some devices send ATT messages as soon as the physical link is
1113 * established. To be able to handle these ATT messages, the user-
1114 * space first establishes the connection and then starts the pairing
1117 * So if a hci_conn object already exists for the following connection
1118 * attempt, we simply update pending_sec_level and auth_type fields
1119 * and return the object found.
1121 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
1123 if (conn->pending_sec_level < sec_level)
1124 conn->pending_sec_level = sec_level;
1128 BT_DBG("requesting refresh of dst_addr");
1130 conn = hci_conn_add(hdev, LE_LINK, dst, HCI_ROLE_MASTER);
1132 return ERR_PTR(-ENOMEM);
1134 if (hci_explicit_conn_params_set(hdev, dst, dst_type) < 0) {
1136 return ERR_PTR(-EBUSY);
1139 conn->state = BT_CONNECT;
1140 set_bit(HCI_CONN_SCANNING, &conn->flags);
1141 conn->dst_type = dst_type;
1142 conn->sec_level = BT_SECURITY_LOW;
1143 conn->pending_sec_level = sec_level;
1144 conn->conn_timeout = conn_timeout;
1145 conn->conn_reason = conn_reason;
1147 hci_update_passive_scan(hdev);
1150 hci_conn_hold(conn);
1154 struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
1155 u8 sec_level, u8 auth_type,
1156 enum conn_reasons conn_reason)
1158 struct hci_conn *acl;
1160 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
1161 if (lmp_bredr_capable(hdev))
1162 return ERR_PTR(-ECONNREFUSED);
1164 return ERR_PTR(-EOPNOTSUPP);
1167 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
1169 acl = hci_conn_add(hdev, ACL_LINK, dst, HCI_ROLE_MASTER);
1171 return ERR_PTR(-ENOMEM);
1176 acl->conn_reason = conn_reason;
1177 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
1178 acl->sec_level = BT_SECURITY_LOW;
1179 acl->pending_sec_level = sec_level;
1180 acl->auth_type = auth_type;
1181 hci_acl_create_connection(acl);
1187 struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
1188 __u16 setting, struct bt_codec *codec)
1190 struct hci_conn *acl;
1191 struct hci_conn *sco;
1193 acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING,
1194 CONN_REASON_SCO_CONNECT);
1198 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
1200 sco = hci_conn_add(hdev, type, dst, HCI_ROLE_MASTER);
1203 return ERR_PTR(-ENOMEM);
1212 sco->setting = setting;
1213 sco->codec = *codec;
1215 if (acl->state == BT_CONNECTED &&
1216 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
1217 set_bit(HCI_CONN_POWER_SAVE, &acl->flags);
1218 hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON);
1220 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) {
1221 /* defer SCO setup until mode change completed */
1222 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags);
1226 hci_sco_setup(acl, 0x00);
1232 /* Check link security requirement */
1233 int hci_conn_check_link_mode(struct hci_conn *conn)
1235 BT_DBG("hcon %p", conn);
1237 /* In Secure Connections Only mode, it is required that Secure
1238 * Connections is used and the link is encrypted with AES-CCM
1239 * using a P-256 authenticated combination key.
1241 if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) {
1242 if (!hci_conn_sc_enabled(conn) ||
1243 !test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
1244 conn->key_type != HCI_LK_AUTH_COMBINATION_P256)
1248 /* AES encryption is required for Level 4:
1250 * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 3, Part C
1253 * 128-bit equivalent strength for link and encryption keys
1254 * required using FIPS approved algorithms (E0 not allowed,
1255 * SAFER+ not allowed, and P-192 not allowed; encryption key
1258 if (conn->sec_level == BT_SECURITY_FIPS &&
1259 !test_bit(HCI_CONN_AES_CCM, &conn->flags)) {
1260 bt_dev_err(conn->hdev,
1261 "Invalid security: Missing AES-CCM usage");
1265 if (hci_conn_ssp_enabled(conn) &&
1266 !test_bit(HCI_CONN_ENCRYPT, &conn->flags))
1272 /* Authenticate remote device */
1273 static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
1275 BT_DBG("hcon %p", conn);
1277 if (conn->pending_sec_level > sec_level)
1278 sec_level = conn->pending_sec_level;
1280 if (sec_level > conn->sec_level)
1281 conn->pending_sec_level = sec_level;
1282 else if (test_bit(HCI_CONN_AUTH, &conn->flags))
1285 /* Make sure we preserve an existing MITM requirement*/
1286 auth_type |= (conn->auth_type & 0x01);
1288 conn->auth_type = auth_type;
1290 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
1291 struct hci_cp_auth_requested cp;
1293 cp.handle = cpu_to_le16(conn->handle);
1294 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
1297 /* If we're already encrypted set the REAUTH_PEND flag,
1298 * otherwise set the ENCRYPT_PEND.
1300 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
1301 set_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
1303 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
1309 /* Encrypt the link */
1310 static void hci_conn_encrypt(struct hci_conn *conn)
1312 BT_DBG("hcon %p", conn);
1314 if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
1315 struct hci_cp_set_conn_encrypt cp;
1316 cp.handle = cpu_to_le16(conn->handle);
1318 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
1323 /* Enable security */
1324 int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
1327 BT_DBG("hcon %p", conn);
1329 if (conn->type == LE_LINK)
1330 return smp_conn_security(conn, sec_level);
1332 /* For sdp we don't need the link key. */
1333 if (sec_level == BT_SECURITY_SDP)
1336 /* For non 2.1 devices and low security level we don't need the link
1338 if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn))
1341 /* For other security levels we need the link key. */
1342 if (!test_bit(HCI_CONN_AUTH, &conn->flags))
1345 /* An authenticated FIPS approved combination key has sufficient
1346 * security for security level 4. */
1347 if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256 &&
1348 sec_level == BT_SECURITY_FIPS)
1351 /* An authenticated combination key has sufficient security for
1352 security level 3. */
1353 if ((conn->key_type == HCI_LK_AUTH_COMBINATION_P192 ||
1354 conn->key_type == HCI_LK_AUTH_COMBINATION_P256) &&
1355 sec_level == BT_SECURITY_HIGH)
1358 /* An unauthenticated combination key has sufficient security for
1359 security level 1 and 2. */
1360 if ((conn->key_type == HCI_LK_UNAUTH_COMBINATION_P192 ||
1361 conn->key_type == HCI_LK_UNAUTH_COMBINATION_P256) &&
1362 (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW))
1365 /* A combination key has always sufficient security for the security
1366 levels 1 or 2. High security level requires the combination key
1367 is generated using maximum PIN code length (16).
1368 For pre 2.1 units. */
1369 if (conn->key_type == HCI_LK_COMBINATION &&
1370 (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW ||
1371 conn->pin_length == 16))
1375 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
1379 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
1381 if (!hci_conn_auth(conn, sec_level, auth_type))
1385 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) {
1386 /* Ensure that the encryption key size has been read,
1387 * otherwise stall the upper layer responses.
1389 if (!conn->enc_key_size)
1392 /* Nothing else needed, all requirements are met */
1396 hci_conn_encrypt(conn);
1399 EXPORT_SYMBOL(hci_conn_security);
1401 /* Check secure link requirement */
1402 int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
1404 BT_DBG("hcon %p", conn);
1406 /* Accept if non-secure or higher security level is required */
1407 if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS)
1410 /* Accept if secure or higher security level is already present */
1411 if (conn->sec_level == BT_SECURITY_HIGH ||
1412 conn->sec_level == BT_SECURITY_FIPS)
1415 /* Reject not secure link */
1418 EXPORT_SYMBOL(hci_conn_check_secure);
1421 int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
1423 BT_DBG("hcon %p", conn);
1425 if (role == conn->role)
1428 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) {
1429 struct hci_cp_switch_role cp;
1430 bacpy(&cp.bdaddr, &conn->dst);
1432 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
1437 EXPORT_SYMBOL(hci_conn_switch_role);
1439 /* Enter active mode */
1440 void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
1442 struct hci_dev *hdev = conn->hdev;
1444 BT_DBG("hcon %p mode %d", conn, conn->mode);
1446 if (conn->mode != HCI_CM_SNIFF)
1449 if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active)
1452 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
1453 struct hci_cp_exit_sniff_mode cp;
1454 cp.handle = cpu_to_le16(conn->handle);
1455 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
1459 if (hdev->idle_timeout > 0)
1460 queue_delayed_work(hdev->workqueue, &conn->idle_work,
1461 msecs_to_jiffies(hdev->idle_timeout));
1464 /* Drop all connection on the device */
1465 void hci_conn_hash_flush(struct hci_dev *hdev)
1467 struct hci_conn_hash *h = &hdev->conn_hash;
1468 struct hci_conn *c, *n;
1470 BT_DBG("hdev %s", hdev->name);
1472 list_for_each_entry_safe(c, n, &h->list, list) {
1473 c->state = BT_CLOSED;
1475 hci_disconn_cfm(c, HCI_ERROR_LOCAL_HOST_TERM);
1480 /* Check pending connect attempts */
1481 void hci_conn_check_pending(struct hci_dev *hdev)
1483 struct hci_conn *conn;
1485 BT_DBG("hdev %s", hdev->name);
1489 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
1491 hci_acl_create_connection(conn);
1493 hci_dev_unlock(hdev);
1496 static u32 get_link_mode(struct hci_conn *conn)
1500 if (conn->role == HCI_ROLE_MASTER)
1501 link_mode |= HCI_LM_MASTER;
1503 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
1504 link_mode |= HCI_LM_ENCRYPT;
1506 if (test_bit(HCI_CONN_AUTH, &conn->flags))
1507 link_mode |= HCI_LM_AUTH;
1509 if (test_bit(HCI_CONN_SECURE, &conn->flags))
1510 link_mode |= HCI_LM_SECURE;
1512 if (test_bit(HCI_CONN_FIPS, &conn->flags))
1513 link_mode |= HCI_LM_FIPS;
1518 int hci_get_conn_list(void __user *arg)
1521 struct hci_conn_list_req req, *cl;
1522 struct hci_conn_info *ci;
1523 struct hci_dev *hdev;
1524 int n = 0, size, err;
1526 if (copy_from_user(&req, arg, sizeof(req)))
1529 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
1532 size = sizeof(req) + req.conn_num * sizeof(*ci);
1534 cl = kmalloc(size, GFP_KERNEL);
1538 hdev = hci_dev_get(req.dev_id);
1547 list_for_each_entry(c, &hdev->conn_hash.list, list) {
1548 bacpy(&(ci + n)->bdaddr, &c->dst);
1549 (ci + n)->handle = c->handle;
1550 (ci + n)->type = c->type;
1551 (ci + n)->out = c->out;
1552 (ci + n)->state = c->state;
1553 (ci + n)->link_mode = get_link_mode(c);
1554 if (++n >= req.conn_num)
1557 hci_dev_unlock(hdev);
1559 cl->dev_id = hdev->id;
1561 size = sizeof(req) + n * sizeof(*ci);
1565 err = copy_to_user(arg, cl, size);
1568 return err ? -EFAULT : 0;
1571 int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
1573 struct hci_conn_info_req req;
1574 struct hci_conn_info ci;
1575 struct hci_conn *conn;
1576 char __user *ptr = arg + sizeof(req);
1578 if (copy_from_user(&req, arg, sizeof(req)))
1582 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
1584 bacpy(&ci.bdaddr, &conn->dst);
1585 ci.handle = conn->handle;
1586 ci.type = conn->type;
1588 ci.state = conn->state;
1589 ci.link_mode = get_link_mode(conn);
1591 hci_dev_unlock(hdev);
1596 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
1599 int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
1601 struct hci_auth_info_req req;
1602 struct hci_conn *conn;
1604 if (copy_from_user(&req, arg, sizeof(req)))
1608 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1610 req.type = conn->auth_type;
1611 hci_dev_unlock(hdev);
1616 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1619 struct hci_chan *hci_chan_create(struct hci_conn *conn)
1621 struct hci_dev *hdev = conn->hdev;
1622 struct hci_chan *chan;
1624 BT_DBG("%s hcon %p", hdev->name, conn);
1626 if (test_bit(HCI_CONN_DROP, &conn->flags)) {
1627 BT_DBG("Refusing to create new hci_chan");
1631 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
1635 chan->conn = hci_conn_get(conn);
1636 skb_queue_head_init(&chan->data_q);
1637 chan->state = BT_CONNECTED;
1639 list_add_rcu(&chan->list, &conn->chan_list);
1644 void hci_chan_del(struct hci_chan *chan)
1646 struct hci_conn *conn = chan->conn;
1647 struct hci_dev *hdev = conn->hdev;
1649 BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan);
1651 list_del_rcu(&chan->list);
1655 /* Prevent new hci_chan's to be created for this hci_conn */
1656 set_bit(HCI_CONN_DROP, &conn->flags);
1660 skb_queue_purge(&chan->data_q);
1664 void hci_chan_list_flush(struct hci_conn *conn)
1666 struct hci_chan *chan, *n;
1668 BT_DBG("hcon %p", conn);
1670 list_for_each_entry_safe(chan, n, &conn->chan_list, list)
1674 static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon,
1677 struct hci_chan *hchan;
1679 list_for_each_entry(hchan, &hcon->chan_list, list) {
1680 if (hchan->handle == handle)
1687 struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle)
1689 struct hci_conn_hash *h = &hdev->conn_hash;
1690 struct hci_conn *hcon;
1691 struct hci_chan *hchan = NULL;
1695 list_for_each_entry_rcu(hcon, &h->list, list) {
1696 hchan = __hci_chan_lookup_handle(hcon, handle);
1706 u32 hci_conn_get_phy(struct hci_conn *conn)
1710 /* BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 2, Part B page 471:
1711 * Table 6.2: Packets defined for synchronous, asynchronous, and
1712 * CPB logical transport types.
1714 switch (conn->type) {
1716 /* SCO logical transport (1 Mb/s):
1717 * HV1, HV2, HV3 and DV.
1719 phys |= BT_PHY_BR_1M_1SLOT;
1724 /* ACL logical transport (1 Mb/s) ptt=0:
1725 * DH1, DM3, DH3, DM5 and DH5.
1727 phys |= BT_PHY_BR_1M_1SLOT;
1729 if (conn->pkt_type & (HCI_DM3 | HCI_DH3))
1730 phys |= BT_PHY_BR_1M_3SLOT;
1732 if (conn->pkt_type & (HCI_DM5 | HCI_DH5))
1733 phys |= BT_PHY_BR_1M_5SLOT;
1735 /* ACL logical transport (2 Mb/s) ptt=1:
1736 * 2-DH1, 2-DH3 and 2-DH5.
1738 if (!(conn->pkt_type & HCI_2DH1))
1739 phys |= BT_PHY_EDR_2M_1SLOT;
1741 if (!(conn->pkt_type & HCI_2DH3))
1742 phys |= BT_PHY_EDR_2M_3SLOT;
1744 if (!(conn->pkt_type & HCI_2DH5))
1745 phys |= BT_PHY_EDR_2M_5SLOT;
1747 /* ACL logical transport (3 Mb/s) ptt=1:
1748 * 3-DH1, 3-DH3 and 3-DH5.
1750 if (!(conn->pkt_type & HCI_3DH1))
1751 phys |= BT_PHY_EDR_3M_1SLOT;
1753 if (!(conn->pkt_type & HCI_3DH3))
1754 phys |= BT_PHY_EDR_3M_3SLOT;
1756 if (!(conn->pkt_type & HCI_3DH5))
1757 phys |= BT_PHY_EDR_3M_5SLOT;
1762 /* eSCO logical transport (1 Mb/s): EV3, EV4 and EV5 */
1763 phys |= BT_PHY_BR_1M_1SLOT;
1765 if (!(conn->pkt_type & (ESCO_EV4 | ESCO_EV5)))
1766 phys |= BT_PHY_BR_1M_3SLOT;
1768 /* eSCO logical transport (2 Mb/s): 2-EV3, 2-EV5 */
1769 if (!(conn->pkt_type & ESCO_2EV3))
1770 phys |= BT_PHY_EDR_2M_1SLOT;
1772 if (!(conn->pkt_type & ESCO_2EV5))
1773 phys |= BT_PHY_EDR_2M_3SLOT;
1775 /* eSCO logical transport (3 Mb/s): 3-EV3, 3-EV5 */
1776 if (!(conn->pkt_type & ESCO_3EV3))
1777 phys |= BT_PHY_EDR_3M_1SLOT;
1779 if (!(conn->pkt_type & ESCO_3EV5))
1780 phys |= BT_PHY_EDR_3M_3SLOT;
1785 if (conn->le_tx_phy & HCI_LE_SET_PHY_1M)
1786 phys |= BT_PHY_LE_1M_TX;
1788 if (conn->le_rx_phy & HCI_LE_SET_PHY_1M)
1789 phys |= BT_PHY_LE_1M_RX;
1791 if (conn->le_tx_phy & HCI_LE_SET_PHY_2M)
1792 phys |= BT_PHY_LE_2M_TX;
1794 if (conn->le_rx_phy & HCI_LE_SET_PHY_2M)
1795 phys |= BT_PHY_LE_2M_RX;
1797 if (conn->le_tx_phy & HCI_LE_SET_PHY_CODED)
1798 phys |= BT_PHY_LE_CODED_TX;
1800 if (conn->le_rx_phy & HCI_LE_SET_PHY_CODED)
1801 phys |= BT_PHY_LE_CODED_RX;