···387387 if (!(auth & SMP_AUTH_BONDING) && method == JUST_CFM)388388 method = JUST_WORKS;389389390390+ /* Don't confirm locally initiated pairing attempts */391391+ if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,392392+ &smp->smp_flags))393393+ method = JUST_WORKS;394394+390395 /* If Just Works, Continue with Zero TK */391396 if (method == JUST_WORKS) {392397 set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);···427422 if (method == REQ_PASSKEY)428423 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,429424 hcon->type, hcon->dst_type);425425+ else if (method == JUST_CFM)426426+ ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,427427+ hcon->type, hcon->dst_type,428428+ passkey, 1);430429 else431430 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,432431 hcon->type, hcon->dst_type,433433- cpu_to_le32(passkey), 0);432432+ passkey, 0);434433435434 hci_dev_unlock(hcon->hdev);436435···556547 smp_failure(conn, reason);557548}558549559559-static void smp_reencrypt(struct work_struct *work)560560-{561561- struct smp_chan *smp = container_of(work, struct smp_chan,562562- reencrypt.work);563563- struct l2cap_conn *conn = smp->conn;564564- struct hci_conn *hcon = conn->hcon;565565- struct smp_ltk *ltk = smp->ltk;566566-567567- BT_DBG("");568568-569569- hci_le_start_enc(hcon, ltk->ediv, ltk->rand, ltk->val);570570- hcon->enc_key_size = ltk->enc_size;571571-}572572-573550static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)574551{575552 struct smp_chan *smp;···566571567572 INIT_WORK(&smp->confirm, confirm_work);568573 INIT_WORK(&smp->random, random_work);569569- INIT_DELAYED_WORK(&smp->reencrypt, smp_reencrypt);570574571575 smp->conn = conn;572576 conn->smp_chan = smp;···582588 bool complete;583589584590 BUG_ON(!smp);585585-586586- cancel_delayed_work_sync(&smp->reencrypt);587591588592 complete = test_bit(SMP_FLAG_COMPLETE, &smp->smp_flags);589593 mgmt_smp_complete(conn->hcon, complete);···703711 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);704712 if (ret)705713 return SMP_UNSPECIFIED;714714+715715+ clear_bit(SMP_FLAG_INITIATOR, &smp->smp_flags);706716707717 return 0;708718}···861867862868 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);863869870870+ clear_bit(SMP_FLAG_INITIATOR, &smp->smp_flags);871871+864872 return 0;865873}866874···880884int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)881885{882886 struct l2cap_conn *conn = hcon->l2cap_data;883883- struct smp_chan *smp = conn->smp_chan;887887+ struct smp_chan *smp;884888 __u8 authreq;885889886890 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);891891+892892+ /* This may be NULL if there's an unexpected disconnection */893893+ if (!conn)894894+ return 1;887895888896 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))889897 return 1;···927927 cp.auth_req = authreq;928928 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);929929 }930930+931931+ set_bit(SMP_FLAG_INITIATOR, &smp->smp_flags);930932931933done:932934 hcon->pending_sec_level = sec_level;···1059105710601058 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,10611059 smp->id_addr_type, smp->irk, &rpa);10621062-10631063- /* Track the connection based on the Identity Address from now on */10641064- bacpy(&hcon->dst, &smp->id_addr);10651065- hcon->dst_type = smp->id_addr_type;10661066-10671067- l2cap_conn_update_id_addr(hcon);1068106010691061 smp_distribute_keys(conn);10701062···12101214 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];12111215 bool persistent;1212121612131213- if (smp->remote_irk)12171217+ if (smp->remote_irk) {12141218 mgmt_new_irk(hdev, smp->remote_irk);12191219+ /* Now that user space can be considered to know the12201220+ * identity address track the connection based on it12211221+ * from now on.12221222+ */12231223+ bacpy(&hcon->dst, &smp->remote_irk->bdaddr);12241224+ hcon->dst_type = smp->remote_irk->addr_type;12251225+ l2cap_conn_update_id_addr(hcon);12261226+ }1215122712161228 /* The LTKs and CSRKs should be persistent only if both sides12171229 * had the bonding bit set in their authentication requests.···12571253 struct smp_chan *smp = conn->smp_chan;12581254 struct hci_conn *hcon = conn->hcon;12591255 struct hci_dev *hdev = hcon->hdev;12601260- bool ltk_encrypt;12611256 __u8 *keydist;1262125712631258 BT_DBG("conn %p", conn);···13561353 if ((smp->remote_key_dist & 0x07))13571354 return 0;1358135513591359- /* Check if we should try to re-encrypt the link with the LTK.13601360- * SMP_FLAG_LTK_ENCRYPT flag is used to track whether we've13611361- * already tried this (in which case we shouldn't try again).13621362- *13631363- * The request will trigger an encryption key refresh event13641364- * which will cause a call to auth_cfm and eventually lead to13651365- * l2cap_core.c calling this smp_distribute_keys function again13661366- * and thereby completing the process.13671367- */13681368- if (smp->ltk)13691369- ltk_encrypt = !test_and_set_bit(SMP_FLAG_LTK_ENCRYPT,13701370- &smp->smp_flags);13711371- else13721372- ltk_encrypt = false;13561356+ clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags);13571357+ cancel_delayed_work_sync(&conn->security_timer);13581358+ set_bit(SMP_FLAG_COMPLETE, &smp->smp_flags);13591359+ smp_notify_keys(conn);1373136013741374- /* Re-encrypt the link with LTK if possible */13751375- if (ltk_encrypt && hcon->out) {13761376- queue_delayed_work(hdev->req_workqueue, &smp->reencrypt,13771377- SMP_REENCRYPT_TIMEOUT);13781378- } else {13791379- clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags);13801380- cancel_delayed_work_sync(&conn->security_timer);13811381- set_bit(SMP_FLAG_COMPLETE, &smp->smp_flags);13821382- smp_notify_keys(conn);13831383- smp_chan_destroy(conn);13841384- }13611361+ smp_chan_destroy(conn);1385136213861363 return 0;13871364}