aboutsummaryrefslogtreecommitdiffstats
path: root/net/bluetooth/hci_event.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/bluetooth/hci_event.c')
-rw-r--r--net/bluetooth/hci_event.c622
1 files changed, 582 insertions, 40 deletions
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index a290854fdaa6..98b5764e4315 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -274,15 +274,24 @@ static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
274 274
275 if (!status) { 275 if (!status) {
276 __u8 param = *((__u8 *) sent); 276 __u8 param = *((__u8 *) sent);
277 int old_pscan, old_iscan;
277 278
278 clear_bit(HCI_PSCAN, &hdev->flags); 279 old_pscan = test_and_clear_bit(HCI_PSCAN, &hdev->flags);
279 clear_bit(HCI_ISCAN, &hdev->flags); 280 old_iscan = test_and_clear_bit(HCI_ISCAN, &hdev->flags);
280 281
281 if (param & SCAN_INQUIRY) 282 if (param & SCAN_INQUIRY) {
282 set_bit(HCI_ISCAN, &hdev->flags); 283 set_bit(HCI_ISCAN, &hdev->flags);
284 if (!old_iscan)
285 mgmt_discoverable(hdev->id, 1);
286 } else if (old_iscan)
287 mgmt_discoverable(hdev->id, 0);
283 288
284 if (param & SCAN_PAGE) 289 if (param & SCAN_PAGE) {
285 set_bit(HCI_PSCAN, &hdev->flags); 290 set_bit(HCI_PSCAN, &hdev->flags);
291 if (!old_pscan)
292 mgmt_connectable(hdev->id, 1);
293 } else if (old_pscan)
294 mgmt_connectable(hdev->id, 0);
286 } 295 }
287 296
288 hci_req_complete(hdev, HCI_OP_WRITE_SCAN_ENABLE, status); 297 hci_req_complete(hdev, HCI_OP_WRITE_SCAN_ENABLE, status);
@@ -415,6 +424,115 @@ static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
415 hdev->ssp_mode = *((__u8 *) sent); 424 hdev->ssp_mode = *((__u8 *) sent);
416} 425}
417 426
427static u8 hci_get_inquiry_mode(struct hci_dev *hdev)
428{
429 if (hdev->features[6] & LMP_EXT_INQ)
430 return 2;
431
432 if (hdev->features[3] & LMP_RSSI_INQ)
433 return 1;
434
435 if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 &&
436 hdev->lmp_subver == 0x0757)
437 return 1;
438
439 if (hdev->manufacturer == 15) {
440 if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963)
441 return 1;
442 if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963)
443 return 1;
444 if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965)
445 return 1;
446 }
447
448 if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 &&
449 hdev->lmp_subver == 0x1805)
450 return 1;
451
452 return 0;
453}
454
455static void hci_setup_inquiry_mode(struct hci_dev *hdev)
456{
457 u8 mode;
458
459 mode = hci_get_inquiry_mode(hdev);
460
461 hci_send_cmd(hdev, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode);
462}
463
464static void hci_setup_event_mask(struct hci_dev *hdev)
465{
466 /* The second byte is 0xff instead of 0x9f (two reserved bits
467 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
468 * command otherwise */
469 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
470
471 /* Events for 1.2 and newer controllers */
472 if (hdev->lmp_ver > 1) {
473 events[4] |= 0x01; /* Flow Specification Complete */
474 events[4] |= 0x02; /* Inquiry Result with RSSI */
475 events[4] |= 0x04; /* Read Remote Extended Features Complete */
476 events[5] |= 0x08; /* Synchronous Connection Complete */
477 events[5] |= 0x10; /* Synchronous Connection Changed */
478 }
479
480 if (hdev->features[3] & LMP_RSSI_INQ)
481 events[4] |= 0x04; /* Inquiry Result with RSSI */
482
483 if (hdev->features[5] & LMP_SNIFF_SUBR)
484 events[5] |= 0x20; /* Sniff Subrating */
485
486 if (hdev->features[5] & LMP_PAUSE_ENC)
487 events[5] |= 0x80; /* Encryption Key Refresh Complete */
488
489 if (hdev->features[6] & LMP_EXT_INQ)
490 events[5] |= 0x40; /* Extended Inquiry Result */
491
492 if (hdev->features[6] & LMP_NO_FLUSH)
493 events[7] |= 0x01; /* Enhanced Flush Complete */
494
495 if (hdev->features[7] & LMP_LSTO)
496 events[6] |= 0x80; /* Link Supervision Timeout Changed */
497
498 if (hdev->features[6] & LMP_SIMPLE_PAIR) {
499 events[6] |= 0x01; /* IO Capability Request */
500 events[6] |= 0x02; /* IO Capability Response */
501 events[6] |= 0x04; /* User Confirmation Request */
502 events[6] |= 0x08; /* User Passkey Request */
503 events[6] |= 0x10; /* Remote OOB Data Request */
504 events[6] |= 0x20; /* Simple Pairing Complete */
505 events[7] |= 0x04; /* User Passkey Notification */
506 events[7] |= 0x08; /* Keypress Notification */
507 events[7] |= 0x10; /* Remote Host Supported
508 * Features Notification */
509 }
510
511 if (hdev->features[4] & LMP_LE)
512 events[7] |= 0x20; /* LE Meta-Event */
513
514 hci_send_cmd(hdev, HCI_OP_SET_EVENT_MASK, sizeof(events), events);
515}
516
517static void hci_setup(struct hci_dev *hdev)
518{
519 hci_setup_event_mask(hdev);
520
521 if (hdev->lmp_ver > 1)
522 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
523
524 if (hdev->features[6] & LMP_SIMPLE_PAIR) {
525 u8 mode = 0x01;
526 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
527 }
528
529 if (hdev->features[3] & LMP_RSSI_INQ)
530 hci_setup_inquiry_mode(hdev);
531
532 if (hdev->features[7] & LMP_INQ_TX_PWR)
533 hci_send_cmd(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL);
534}
535
418static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb) 536static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
419{ 537{
420 struct hci_rp_read_local_version *rp = (void *) skb->data; 538 struct hci_rp_read_local_version *rp = (void *) skb->data;
@@ -426,11 +544,34 @@ static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
426 544
427 hdev->hci_ver = rp->hci_ver; 545 hdev->hci_ver = rp->hci_ver;
428 hdev->hci_rev = __le16_to_cpu(rp->hci_rev); 546 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
547 hdev->lmp_ver = rp->lmp_ver;
429 hdev->manufacturer = __le16_to_cpu(rp->manufacturer); 548 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
549 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
430 550
431 BT_DBG("%s manufacturer %d hci ver %d:%d", hdev->name, 551 BT_DBG("%s manufacturer %d hci ver %d:%d", hdev->name,
432 hdev->manufacturer, 552 hdev->manufacturer,
433 hdev->hci_ver, hdev->hci_rev); 553 hdev->hci_ver, hdev->hci_rev);
554
555 if (test_bit(HCI_INIT, &hdev->flags))
556 hci_setup(hdev);
557}
558
559static void hci_setup_link_policy(struct hci_dev *hdev)
560{
561 u16 link_policy = 0;
562
563 if (hdev->features[0] & LMP_RSWITCH)
564 link_policy |= HCI_LP_RSWITCH;
565 if (hdev->features[0] & LMP_HOLD)
566 link_policy |= HCI_LP_HOLD;
567 if (hdev->features[0] & LMP_SNIFF)
568 link_policy |= HCI_LP_SNIFF;
569 if (hdev->features[1] & LMP_PARK)
570 link_policy |= HCI_LP_PARK;
571
572 link_policy = cpu_to_le16(link_policy);
573 hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
574 sizeof(link_policy), &link_policy);
434} 575}
435 576
436static void hci_cc_read_local_commands(struct hci_dev *hdev, struct sk_buff *skb) 577static void hci_cc_read_local_commands(struct hci_dev *hdev, struct sk_buff *skb)
@@ -440,9 +581,15 @@ static void hci_cc_read_local_commands(struct hci_dev *hdev, struct sk_buff *skb
440 BT_DBG("%s status 0x%x", hdev->name, rp->status); 581 BT_DBG("%s status 0x%x", hdev->name, rp->status);
441 582
442 if (rp->status) 583 if (rp->status)
443 return; 584 goto done;
444 585
445 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands)); 586 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
587
588 if (test_bit(HCI_INIT, &hdev->flags) && (hdev->commands[5] & 0x10))
589 hci_setup_link_policy(hdev);
590
591done:
592 hci_req_complete(hdev, HCI_OP_READ_LOCAL_COMMANDS, rp->status);
446} 593}
447 594
448static void hci_cc_read_local_features(struct hci_dev *hdev, struct sk_buff *skb) 595static void hci_cc_read_local_features(struct hci_dev *hdev, struct sk_buff *skb)
@@ -548,6 +695,107 @@ static void hci_cc_write_ca_timeout(struct hci_dev *hdev, struct sk_buff *skb)
548 hci_req_complete(hdev, HCI_OP_WRITE_CA_TIMEOUT, status); 695 hci_req_complete(hdev, HCI_OP_WRITE_CA_TIMEOUT, status);
549} 696}
550 697
698static void hci_cc_delete_stored_link_key(struct hci_dev *hdev,
699 struct sk_buff *skb)
700{
701 __u8 status = *((__u8 *) skb->data);
702
703 BT_DBG("%s status 0x%x", hdev->name, status);
704
705 hci_req_complete(hdev, HCI_OP_DELETE_STORED_LINK_KEY, status);
706}
707
708static void hci_cc_set_event_mask(struct hci_dev *hdev, struct sk_buff *skb)
709{
710 __u8 status = *((__u8 *) skb->data);
711
712 BT_DBG("%s status 0x%x", hdev->name, status);
713
714 hci_req_complete(hdev, HCI_OP_SET_EVENT_MASK, status);
715}
716
717static void hci_cc_write_inquiry_mode(struct hci_dev *hdev,
718 struct sk_buff *skb)
719{
720 __u8 status = *((__u8 *) skb->data);
721
722 BT_DBG("%s status 0x%x", hdev->name, status);
723
724 hci_req_complete(hdev, HCI_OP_WRITE_INQUIRY_MODE, status);
725}
726
727static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
728 struct sk_buff *skb)
729{
730 __u8 status = *((__u8 *) skb->data);
731
732 BT_DBG("%s status 0x%x", hdev->name, status);
733
734 hci_req_complete(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, status);
735}
736
737static void hci_cc_set_event_flt(struct hci_dev *hdev, struct sk_buff *skb)
738{
739 __u8 status = *((__u8 *) skb->data);
740
741 BT_DBG("%s status 0x%x", hdev->name, status);
742
743 hci_req_complete(hdev, HCI_OP_SET_EVENT_FLT, status);
744}
745
746static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
747{
748 struct hci_rp_pin_code_reply *rp = (void *) skb->data;
749 struct hci_cp_pin_code_reply *cp;
750 struct hci_conn *conn;
751
752 BT_DBG("%s status 0x%x", hdev->name, rp->status);
753
754 if (test_bit(HCI_MGMT, &hdev->flags))
755 mgmt_pin_code_reply_complete(hdev->id, &rp->bdaddr, rp->status);
756
757 if (rp->status != 0)
758 return;
759
760 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
761 if (!cp)
762 return;
763
764 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
765 if (conn)
766 conn->pin_length = cp->pin_len;
767}
768
769static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
770{
771 struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
772
773 BT_DBG("%s status 0x%x", hdev->name, rp->status);
774
775 if (test_bit(HCI_MGMT, &hdev->flags))
776 mgmt_pin_code_neg_reply_complete(hdev->id, &rp->bdaddr,
777 rp->status);
778}
779static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
780 struct sk_buff *skb)
781{
782 struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
783
784 BT_DBG("%s status 0x%x", hdev->name, rp->status);
785
786 if (rp->status)
787 return;
788
789 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
790 hdev->le_pkts = rp->le_max_pkt;
791
792 hdev->le_cnt = hdev->le_pkts;
793
794 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
795
796 hci_req_complete(hdev, HCI_OP_LE_READ_BUFFER_SIZE, rp->status);
797}
798
551static inline void hci_cs_inquiry(struct hci_dev *hdev, __u8 status) 799static inline void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
552{ 800{
553 BT_DBG("%s status 0x%x", hdev->name, status); 801 BT_DBG("%s status 0x%x", hdev->name, status);
@@ -622,11 +870,14 @@ static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
622 hci_dev_lock(hdev); 870 hci_dev_lock(hdev);
623 871
624 acl = hci_conn_hash_lookup_handle(hdev, handle); 872 acl = hci_conn_hash_lookup_handle(hdev, handle);
625 if (acl && (sco = acl->link)) { 873 if (acl) {
626 sco->state = BT_CLOSED; 874 sco = acl->link;
875 if (sco) {
876 sco->state = BT_CLOSED;
627 877
628 hci_proto_connect_cfm(sco, status); 878 hci_proto_connect_cfm(sco, status);
629 hci_conn_del(sco); 879 hci_conn_del(sco);
880 }
630 } 881 }
631 882
632 hci_dev_unlock(hdev); 883 hci_dev_unlock(hdev);
@@ -687,7 +938,7 @@ static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
687} 938}
688 939
689static int hci_outgoing_auth_needed(struct hci_dev *hdev, 940static int hci_outgoing_auth_needed(struct hci_dev *hdev,
690 struct hci_conn *conn) 941 struct hci_conn *conn)
691{ 942{
692 if (conn->state != BT_CONFIG || !conn->out) 943 if (conn->state != BT_CONFIG || !conn->out)
693 return 0; 944 return 0;
@@ -808,11 +1059,14 @@ static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
808 hci_dev_lock(hdev); 1059 hci_dev_lock(hdev);
809 1060
810 acl = hci_conn_hash_lookup_handle(hdev, handle); 1061 acl = hci_conn_hash_lookup_handle(hdev, handle);
811 if (acl && (sco = acl->link)) { 1062 if (acl) {
812 sco->state = BT_CLOSED; 1063 sco = acl->link;
1064 if (sco) {
1065 sco->state = BT_CLOSED;
813 1066
814 hci_proto_connect_cfm(sco, status); 1067 hci_proto_connect_cfm(sco, status);
815 hci_conn_del(sco); 1068 hci_conn_del(sco);
1069 }
816 } 1070 }
817 1071
818 hci_dev_unlock(hdev); 1072 hci_dev_unlock(hdev);
@@ -872,6 +1126,43 @@ static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
872 hci_dev_unlock(hdev); 1126 hci_dev_unlock(hdev);
873} 1127}
874 1128
1129static void hci_cs_le_create_conn(struct hci_dev *hdev, __u8 status)
1130{
1131 struct hci_cp_le_create_conn *cp;
1132 struct hci_conn *conn;
1133
1134 BT_DBG("%s status 0x%x", hdev->name, status);
1135
1136 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
1137 if (!cp)
1138 return;
1139
1140 hci_dev_lock(hdev);
1141
1142 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->peer_addr);
1143
1144 BT_DBG("%s bdaddr %s conn %p", hdev->name, batostr(&cp->peer_addr),
1145 conn);
1146
1147 if (status) {
1148 if (conn && conn->state == BT_CONNECT) {
1149 conn->state = BT_CLOSED;
1150 hci_proto_connect_cfm(conn, status);
1151 hci_conn_del(conn);
1152 }
1153 } else {
1154 if (!conn) {
1155 conn = hci_conn_add(hdev, LE_LINK, &cp->peer_addr);
1156 if (conn)
1157 conn->out = 1;
1158 else
1159 BT_ERR("No memory for new connection");
1160 }
1161 }
1162
1163 hci_dev_unlock(hdev);
1164}
1165
875static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) 1166static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
876{ 1167{
877 __u8 status = *((__u8 *) skb->data); 1168 __u8 status = *((__u8 *) skb->data);
@@ -942,6 +1233,7 @@ static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *s
942 conn->state = BT_CONFIG; 1233 conn->state = BT_CONFIG;
943 hci_conn_hold(conn); 1234 hci_conn_hold(conn);
944 conn->disc_timeout = HCI_DISCONN_TIMEOUT; 1235 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
1236 mgmt_connected(hdev->id, &ev->bdaddr);
945 } else 1237 } else
946 conn->state = BT_CONNECTED; 1238 conn->state = BT_CONNECTED;
947 1239
@@ -970,8 +1262,11 @@ static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *s
970 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, 1262 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE,
971 sizeof(cp), &cp); 1263 sizeof(cp), &cp);
972 } 1264 }
973 } else 1265 } else {
974 conn->state = BT_CLOSED; 1266 conn->state = BT_CLOSED;
1267 if (conn->type == ACL_LINK)
1268 mgmt_connect_failed(hdev->id, &ev->bdaddr, ev->status);
1269 }
975 1270
976 if (conn->type == ACL_LINK) 1271 if (conn->type == ACL_LINK)
977 hci_sco_setup(conn, ev->status); 1272 hci_sco_setup(conn, ev->status);
@@ -998,7 +1293,8 @@ static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *sk
998 1293
999 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type); 1294 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
1000 1295
1001 if ((mask & HCI_LM_ACCEPT) && !hci_blacklist_lookup(hdev, &ev->bdaddr)) { 1296 if ((mask & HCI_LM_ACCEPT) &&
1297 !hci_blacklist_lookup(hdev, &ev->bdaddr)) {
1002 /* Connection accepted */ 1298 /* Connection accepted */
1003 struct inquiry_entry *ie; 1299 struct inquiry_entry *ie;
1004 struct hci_conn *conn; 1300 struct hci_conn *conn;
@@ -1068,19 +1364,26 @@ static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff
1068 1364
1069 BT_DBG("%s status %d", hdev->name, ev->status); 1365 BT_DBG("%s status %d", hdev->name, ev->status);
1070 1366
1071 if (ev->status) 1367 if (ev->status) {
1368 mgmt_disconnect_failed(hdev->id);
1072 return; 1369 return;
1370 }
1073 1371
1074 hci_dev_lock(hdev); 1372 hci_dev_lock(hdev);
1075 1373
1076 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle)); 1374 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1077 if (conn) { 1375 if (!conn)
1078 conn->state = BT_CLOSED; 1376 goto unlock;
1079 1377
1080 hci_proto_disconn_cfm(conn, ev->reason); 1378 conn->state = BT_CLOSED;
1081 hci_conn_del(conn); 1379
1082 } 1380 if (conn->type == ACL_LINK)
1381 mgmt_disconnected(hdev->id, &conn->dst);
1083 1382
1383 hci_proto_disconn_cfm(conn, ev->reason);
1384 hci_conn_del(conn);
1385
1386unlock:
1084 hci_dev_unlock(hdev); 1387 hci_dev_unlock(hdev);
1085} 1388}
1086 1389
@@ -1393,11 +1696,46 @@ static inline void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *sk
1393 hci_cc_write_ca_timeout(hdev, skb); 1696 hci_cc_write_ca_timeout(hdev, skb);
1394 break; 1697 break;
1395 1698
1699 case HCI_OP_DELETE_STORED_LINK_KEY:
1700 hci_cc_delete_stored_link_key(hdev, skb);
1701 break;
1702
1703 case HCI_OP_SET_EVENT_MASK:
1704 hci_cc_set_event_mask(hdev, skb);
1705 break;
1706
1707 case HCI_OP_WRITE_INQUIRY_MODE:
1708 hci_cc_write_inquiry_mode(hdev, skb);
1709 break;
1710
1711 case HCI_OP_READ_INQ_RSP_TX_POWER:
1712 hci_cc_read_inq_rsp_tx_power(hdev, skb);
1713 break;
1714
1715 case HCI_OP_SET_EVENT_FLT:
1716 hci_cc_set_event_flt(hdev, skb);
1717 break;
1718
1719 case HCI_OP_PIN_CODE_REPLY:
1720 hci_cc_pin_code_reply(hdev, skb);
1721 break;
1722
1723 case HCI_OP_PIN_CODE_NEG_REPLY:
1724 hci_cc_pin_code_neg_reply(hdev, skb);
1725 break;
1726
1727 case HCI_OP_LE_READ_BUFFER_SIZE:
1728 hci_cc_le_read_buffer_size(hdev, skb);
1729 break;
1730
1396 default: 1731 default:
1397 BT_DBG("%s opcode 0x%x", hdev->name, opcode); 1732 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
1398 break; 1733 break;
1399 } 1734 }
1400 1735
1736 if (ev->opcode != HCI_OP_NOP)
1737 del_timer(&hdev->cmd_timer);
1738
1401 if (ev->ncmd) { 1739 if (ev->ncmd) {
1402 atomic_set(&hdev->cmd_cnt, 1); 1740 atomic_set(&hdev->cmd_cnt, 1);
1403 if (!skb_queue_empty(&hdev->cmd_q)) 1741 if (!skb_queue_empty(&hdev->cmd_q))
@@ -1459,11 +1797,23 @@ static inline void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
1459 hci_cs_exit_sniff_mode(hdev, ev->status); 1797 hci_cs_exit_sniff_mode(hdev, ev->status);
1460 break; 1798 break;
1461 1799
1800 case HCI_OP_DISCONNECT:
1801 if (ev->status != 0)
1802 mgmt_disconnect_failed(hdev->id);
1803 break;
1804
1805 case HCI_OP_LE_CREATE_CONN:
1806 hci_cs_le_create_conn(hdev, ev->status);
1807 break;
1808
1462 default: 1809 default:
1463 BT_DBG("%s opcode 0x%x", hdev->name, opcode); 1810 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
1464 break; 1811 break;
1465 } 1812 }
1466 1813
1814 if (ev->opcode != HCI_OP_NOP)
1815 del_timer(&hdev->cmd_timer);
1816
1467 if (ev->ncmd) { 1817 if (ev->ncmd) {
1468 atomic_set(&hdev->cmd_cnt, 1); 1818 atomic_set(&hdev->cmd_cnt, 1);
1469 if (!skb_queue_empty(&hdev->cmd_q)) 1819 if (!skb_queue_empty(&hdev->cmd_q))
@@ -1529,6 +1879,16 @@ static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *s
1529 hdev->acl_cnt += count; 1879 hdev->acl_cnt += count;
1530 if (hdev->acl_cnt > hdev->acl_pkts) 1880 if (hdev->acl_cnt > hdev->acl_pkts)
1531 hdev->acl_cnt = hdev->acl_pkts; 1881 hdev->acl_cnt = hdev->acl_pkts;
1882 } else if (conn->type == LE_LINK) {
1883 if (hdev->le_pkts) {
1884 hdev->le_cnt += count;
1885 if (hdev->le_cnt > hdev->le_pkts)
1886 hdev->le_cnt = hdev->le_pkts;
1887 } else {
1888 hdev->acl_cnt += count;
1889 if (hdev->acl_cnt > hdev->acl_pkts)
1890 hdev->acl_cnt = hdev->acl_pkts;
1891 }
1532 } else { 1892 } else {
1533 hdev->sco_cnt += count; 1893 hdev->sco_cnt += count;
1534 if (hdev->sco_cnt > hdev->sco_pkts) 1894 if (hdev->sco_cnt > hdev->sco_pkts)
@@ -1586,18 +1946,72 @@ static inline void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff
1586 hci_conn_put(conn); 1946 hci_conn_put(conn);
1587 } 1947 }
1588 1948
1949 if (!test_bit(HCI_PAIRABLE, &hdev->flags))
1950 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
1951 sizeof(ev->bdaddr), &ev->bdaddr);
1952
1953 if (test_bit(HCI_MGMT, &hdev->flags))
1954 mgmt_pin_code_request(hdev->id, &ev->bdaddr);
1955
1589 hci_dev_unlock(hdev); 1956 hci_dev_unlock(hdev);
1590} 1957}
1591 1958
1592static inline void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb) 1959static inline void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1593{ 1960{
1961 struct hci_ev_link_key_req *ev = (void *) skb->data;
1962 struct hci_cp_link_key_reply cp;
1963 struct hci_conn *conn;
1964 struct link_key *key;
1965
1594 BT_DBG("%s", hdev->name); 1966 BT_DBG("%s", hdev->name);
1967
1968 if (!test_bit(HCI_LINK_KEYS, &hdev->flags))
1969 return;
1970
1971 hci_dev_lock(hdev);
1972
1973 key = hci_find_link_key(hdev, &ev->bdaddr);
1974 if (!key) {
1975 BT_DBG("%s link key not found for %s", hdev->name,
1976 batostr(&ev->bdaddr));
1977 goto not_found;
1978 }
1979
1980 BT_DBG("%s found key type %u for %s", hdev->name, key->type,
1981 batostr(&ev->bdaddr));
1982
1983 if (!test_bit(HCI_DEBUG_KEYS, &hdev->flags) && key->type == 0x03) {
1984 BT_DBG("%s ignoring debug key", hdev->name);
1985 goto not_found;
1986 }
1987
1988 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
1989
1990 if (key->type == 0x04 && conn && conn->auth_type != 0xff &&
1991 (conn->auth_type & 0x01)) {
1992 BT_DBG("%s ignoring unauthenticated key", hdev->name);
1993 goto not_found;
1994 }
1995
1996 bacpy(&cp.bdaddr, &ev->bdaddr);
1997 memcpy(cp.link_key, key->val, 16);
1998
1999 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
2000
2001 hci_dev_unlock(hdev);
2002
2003 return;
2004
2005not_found:
2006 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
2007 hci_dev_unlock(hdev);
1595} 2008}
1596 2009
1597static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb) 2010static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
1598{ 2011{
1599 struct hci_ev_link_key_notify *ev = (void *) skb->data; 2012 struct hci_ev_link_key_notify *ev = (void *) skb->data;
1600 struct hci_conn *conn; 2013 struct hci_conn *conn;
2014 u8 pin_len = 0;
1601 2015
1602 BT_DBG("%s", hdev->name); 2016 BT_DBG("%s", hdev->name);
1603 2017
@@ -1607,9 +2021,14 @@ static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff
1607 if (conn) { 2021 if (conn) {
1608 hci_conn_hold(conn); 2022 hci_conn_hold(conn);
1609 conn->disc_timeout = HCI_DISCONN_TIMEOUT; 2023 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
2024 pin_len = conn->pin_length;
1610 hci_conn_put(conn); 2025 hci_conn_put(conn);
1611 } 2026 }
1612 2027
2028 if (test_bit(HCI_LINK_KEYS, &hdev->flags))
2029 hci_add_link_key(hdev, 1, &ev->bdaddr, ev->link_key,
2030 ev->key_type, pin_len);
2031
1613 hci_dev_unlock(hdev); 2032 hci_dev_unlock(hdev);
1614} 2033}
1615 2034
@@ -1683,7 +2102,8 @@ static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct
1683 hci_dev_lock(hdev); 2102 hci_dev_lock(hdev);
1684 2103
1685 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) { 2104 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
1686 struct inquiry_info_with_rssi_and_pscan_mode *info = (void *) (skb->data + 1); 2105 struct inquiry_info_with_rssi_and_pscan_mode *info;
2106 info = (void *) (skb->data + 1);
1687 2107
1688 for (; num_rsp; num_rsp--) { 2108 for (; num_rsp; num_rsp--) {
1689 bacpy(&data.bdaddr, &info->bdaddr); 2109 bacpy(&data.bdaddr, &info->bdaddr);
@@ -1824,17 +2244,8 @@ static inline void hci_sync_conn_changed_evt(struct hci_dev *hdev, struct sk_buf
1824static inline void hci_sniff_subrate_evt(struct hci_dev *hdev, struct sk_buff *skb) 2244static inline void hci_sniff_subrate_evt(struct hci_dev *hdev, struct sk_buff *skb)
1825{ 2245{
1826 struct hci_ev_sniff_subrate *ev = (void *) skb->data; 2246 struct hci_ev_sniff_subrate *ev = (void *) skb->data;
1827 struct hci_conn *conn;
1828 2247
1829 BT_DBG("%s status %d", hdev->name, ev->status); 2248 BT_DBG("%s status %d", hdev->name, ev->status);
1830
1831 hci_dev_lock(hdev);
1832
1833 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1834 if (conn) {
1835 }
1836
1837 hci_dev_unlock(hdev);
1838} 2249}
1839 2250
1840static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb) 2251static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
@@ -1852,12 +2263,12 @@ static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct
1852 2263
1853 for (; num_rsp; num_rsp--) { 2264 for (; num_rsp; num_rsp--) {
1854 bacpy(&data.bdaddr, &info->bdaddr); 2265 bacpy(&data.bdaddr, &info->bdaddr);
1855 data.pscan_rep_mode = info->pscan_rep_mode; 2266 data.pscan_rep_mode = info->pscan_rep_mode;
1856 data.pscan_period_mode = info->pscan_period_mode; 2267 data.pscan_period_mode = info->pscan_period_mode;
1857 data.pscan_mode = 0x00; 2268 data.pscan_mode = 0x00;
1858 memcpy(data.dev_class, info->dev_class, 3); 2269 memcpy(data.dev_class, info->dev_class, 3);
1859 data.clock_offset = info->clock_offset; 2270 data.clock_offset = info->clock_offset;
1860 data.rssi = info->rssi; 2271 data.rssi = info->rssi;
1861 data.ssp_mode = 0x01; 2272 data.ssp_mode = 0x01;
1862 info++; 2273 info++;
1863 hci_inquiry_cache_update(hdev, &data); 2274 hci_inquiry_cache_update(hdev, &data);
@@ -1866,6 +2277,25 @@ static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct
1866 hci_dev_unlock(hdev); 2277 hci_dev_unlock(hdev);
1867} 2278}
1868 2279
2280static inline u8 hci_get_auth_req(struct hci_conn *conn)
2281{
2282 /* If remote requests dedicated bonding follow that lead */
2283 if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) {
2284 /* If both remote and local IO capabilities allow MITM
2285 * protection then require it, otherwise don't */
2286 if (conn->remote_cap == 0x03 || conn->io_capability == 0x03)
2287 return 0x02;
2288 else
2289 return 0x03;
2290 }
2291
2292 /* If remote requests no-bonding follow that lead */
2293 if (conn->remote_auth == 0x00 || conn->remote_auth == 0x01)
2294 return 0x00;
2295
2296 return conn->auth_type;
2297}
2298
1869static inline void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb) 2299static inline void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1870{ 2300{
1871 struct hci_ev_io_capa_request *ev = (void *) skb->data; 2301 struct hci_ev_io_capa_request *ev = (void *) skb->data;
@@ -1876,9 +2306,59 @@ static inline void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff
1876 hci_dev_lock(hdev); 2306 hci_dev_lock(hdev);
1877 2307
1878 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr); 2308 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
1879 if (conn) 2309 if (!conn)
1880 hci_conn_hold(conn); 2310 goto unlock;
2311
2312 hci_conn_hold(conn);
2313
2314 if (!test_bit(HCI_MGMT, &hdev->flags))
2315 goto unlock;
2316
2317 if (test_bit(HCI_PAIRABLE, &hdev->flags) ||
2318 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
2319 struct hci_cp_io_capability_reply cp;
2320
2321 bacpy(&cp.bdaddr, &ev->bdaddr);
2322 cp.capability = conn->io_capability;
2323 cp.oob_data = 0;
2324 cp.authentication = hci_get_auth_req(conn);
2325
2326 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
2327 sizeof(cp), &cp);
2328 } else {
2329 struct hci_cp_io_capability_neg_reply cp;
2330
2331 bacpy(&cp.bdaddr, &ev->bdaddr);
2332 cp.reason = 0x16; /* Pairing not allowed */
2333
2334 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
2335 sizeof(cp), &cp);
2336 }
2337
2338unlock:
2339 hci_dev_unlock(hdev);
2340}
2341
2342static inline void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
2343{
2344 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
2345 struct hci_conn *conn;
2346
2347 BT_DBG("%s", hdev->name);
2348
2349 hci_dev_lock(hdev);
1881 2350
2351 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2352 if (!conn)
2353 goto unlock;
2354
2355 hci_conn_hold(conn);
2356
2357 conn->remote_cap = ev->capability;
2358 conn->remote_oob = ev->oob_data;
2359 conn->remote_auth = ev->authentication;
2360
2361unlock:
1882 hci_dev_unlock(hdev); 2362 hci_dev_unlock(hdev);
1883} 2363}
1884 2364
@@ -1914,6 +2394,60 @@ static inline void hci_remote_host_features_evt(struct hci_dev *hdev, struct sk_
1914 hci_dev_unlock(hdev); 2394 hci_dev_unlock(hdev);
1915} 2395}
1916 2396
2397static inline void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2398{
2399 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
2400 struct hci_conn *conn;
2401
2402 BT_DBG("%s status %d", hdev->name, ev->status);
2403
2404 hci_dev_lock(hdev);
2405
2406 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &ev->bdaddr);
2407 if (!conn) {
2408 conn = hci_conn_add(hdev, LE_LINK, &ev->bdaddr);
2409 if (!conn) {
2410 BT_ERR("No memory for new connection");
2411 hci_dev_unlock(hdev);
2412 return;
2413 }
2414 }
2415
2416 if (ev->status) {
2417 hci_proto_connect_cfm(conn, ev->status);
2418 conn->state = BT_CLOSED;
2419 hci_conn_del(conn);
2420 goto unlock;
2421 }
2422
2423 conn->handle = __le16_to_cpu(ev->handle);
2424 conn->state = BT_CONNECTED;
2425
2426 hci_conn_hold_device(conn);
2427 hci_conn_add_sysfs(conn);
2428
2429 hci_proto_connect_cfm(conn, ev->status);
2430
2431unlock:
2432 hci_dev_unlock(hdev);
2433}
2434
2435static inline void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
2436{
2437 struct hci_ev_le_meta *le_ev = (void *) skb->data;
2438
2439 skb_pull(skb, sizeof(*le_ev));
2440
2441 switch (le_ev->subevent) {
2442 case HCI_EV_LE_CONN_COMPLETE:
2443 hci_le_conn_complete_evt(hdev, skb);
2444 break;
2445
2446 default:
2447 break;
2448 }
2449}
2450
1917void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb) 2451void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
1918{ 2452{
1919 struct hci_event_hdr *hdr = (void *) skb->data; 2453 struct hci_event_hdr *hdr = (void *) skb->data;
@@ -2042,6 +2576,10 @@ void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
2042 hci_io_capa_request_evt(hdev, skb); 2576 hci_io_capa_request_evt(hdev, skb);
2043 break; 2577 break;
2044 2578
2579 case HCI_EV_IO_CAPA_REPLY:
2580 hci_io_capa_reply_evt(hdev, skb);
2581 break;
2582
2045 case HCI_EV_SIMPLE_PAIR_COMPLETE: 2583 case HCI_EV_SIMPLE_PAIR_COMPLETE:
2046 hci_simple_pair_complete_evt(hdev, skb); 2584 hci_simple_pair_complete_evt(hdev, skb);
2047 break; 2585 break;
@@ -2050,6 +2588,10 @@ void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
2050 hci_remote_host_features_evt(hdev, skb); 2588 hci_remote_host_features_evt(hdev, skb);
2051 break; 2589 break;
2052 2590
2591 case HCI_EV_LE_META:
2592 hci_le_meta_evt(hdev, skb);
2593 break;
2594
2053 default: 2595 default:
2054 BT_DBG("%s event 0x%x", hdev->name, event); 2596 BT_DBG("%s event 0x%x", hdev->name, event);
2055 break; 2597 break;
@@ -2083,6 +2625,6 @@ void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
2083 2625
2084 bt_cb(skb)->pkt_type = HCI_EVENT_PKT; 2626 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
2085 skb->dev = (void *) hdev; 2627 skb->dev = (void *) hdev;
2086 hci_send_to_sock(hdev, skb); 2628 hci_send_to_sock(hdev, skb, NULL);
2087 kfree_skb(skb); 2629 kfree_skb(skb);
2088} 2630}