Merge tag 'renesas-cpufreq2-for-v3.17' of git://git.kernel.org/pub/scm/linux/kernel...
[cascardo/linux.git] / net / bluetooth / smp.c
index dfb4e11..f2829a7 100644 (file)
 
 #define AUTH_REQ_MASK   0x07
 
+#define SMP_FLAG_TK_VALID      1
+#define SMP_FLAG_CFM_PENDING   2
+#define SMP_FLAG_MITM_AUTH     3
+#define SMP_FLAG_COMPLETE      4
+#define SMP_FLAG_INITIATOR     5
+
+struct smp_chan {
+       struct l2cap_conn *conn;
+       u8              preq[7]; /* SMP Pairing Request */
+       u8              prsp[7]; /* SMP Pairing Response */
+       u8              prnd[16]; /* SMP Pairing Random (local) */
+       u8              rrnd[16]; /* SMP Pairing Random (remote) */
+       u8              pcnf[16]; /* SMP Pairing Confirm */
+       u8              tk[16]; /* SMP Temporary Key */
+       u8              enc_key_size;
+       u8              remote_key_dist;
+       bdaddr_t        id_addr;
+       u8              id_addr_type;
+       u8              irk[16];
+       struct smp_csrk *csrk;
+       struct smp_csrk *slave_csrk;
+       struct smp_ltk  *ltk;
+       struct smp_ltk  *slave_ltk;
+       struct smp_irk  *remote_irk;
+       unsigned long   flags;
+};
+
 static inline void swap128(const u8 src[16], u8 dst[16])
 {
        int i;
@@ -369,7 +396,7 @@ static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
 
        /* Initialize key for JUST WORKS */
        memset(smp->tk, 0, sizeof(smp->tk));
-       clear_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);
+       clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
 
        BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
 
@@ -388,19 +415,18 @@ static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
                method = JUST_WORKS;
 
        /* Don't confirm locally initiated pairing attempts */
-       if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
-                                          &smp->smp_flags))
+       if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
                method = JUST_WORKS;
 
        /* If Just Works, Continue with Zero TK */
        if (method == JUST_WORKS) {
-               set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);
+               set_bit(SMP_FLAG_TK_VALID, &smp->flags);
                return 0;
        }
 
        /* Not Just Works/Confirm results in MITM Authentication */
        if (method != JUST_CFM)
-               set_bit(SMP_FLAG_MITM_AUTH, &smp->smp_flags);
+               set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
 
        /* If both devices have Keyoard-Display I/O, the master
         * Confirms and the slave Enters the passkey.
@@ -419,7 +445,7 @@ static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
                passkey %= 1000000;
                put_unaligned_le32(passkey, smp->tk);
                BT_DBG("PassKey: %d", passkey);
-               set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);
+               set_bit(SMP_FLAG_TK_VALID, &smp->flags);
        }
 
        hci_dev_lock(hcon->hdev);
@@ -441,15 +467,13 @@ static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
        return ret;
 }
 
-static void confirm_work(struct work_struct *work)
+static u8 smp_confirm(struct smp_chan *smp)
 {
-       struct smp_chan *smp = container_of(work, struct smp_chan, confirm);
        struct l2cap_conn *conn = smp->conn;
        struct hci_dev *hdev = conn->hcon->hdev;
        struct crypto_blkcipher *tfm = hdev->tfm_aes;
        struct smp_cmd_pairing_confirm cp;
        int ret;
-       u8 reason;
 
        BT_DBG("conn %p", conn);
 
@@ -463,35 +487,27 @@ static void confirm_work(struct work_struct *work)
 
        hci_dev_unlock(hdev);
 
-       if (ret) {
-               reason = SMP_UNSPECIFIED;
-               goto error;
-       }
+       if (ret)
+               return SMP_UNSPECIFIED;
 
-       clear_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags);
+       clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
 
        smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
 
-       return;
-
-error:
-       smp_failure(conn, reason);
+       return 0;
 }
 
-static void random_work(struct work_struct *work)
+static u8 smp_random(struct smp_chan *smp)
 {
-       struct smp_chan *smp = container_of(work, struct smp_chan, random);
        struct l2cap_conn *conn = smp->conn;
        struct hci_conn *hcon = conn->hcon;
        struct hci_dev *hdev = hcon->hdev;
        struct crypto_blkcipher *tfm = hdev->tfm_aes;
-       u8 reason, confirm[16];
+       u8 confirm[16];
        int ret;
 
-       if (IS_ERR_OR_NULL(tfm)) {
-               reason = SMP_UNSPECIFIED;
-               goto error;
-       }
+       if (IS_ERR_OR_NULL(tfm))
+               return SMP_UNSPECIFIED;
 
        BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
 
@@ -504,15 +520,12 @@ static void random_work(struct work_struct *work)
 
        hci_dev_unlock(hdev);
 
-       if (ret) {
-               reason = SMP_UNSPECIFIED;
-               goto error;
-       }
+       if (ret)
+               return SMP_UNSPECIFIED;
 
        if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
                BT_ERR("Pairing failed (confirmation values mismatch)");
-               reason = SMP_CONFIRM_FAILED;
-               goto error;
+               return SMP_CONFIRM_FAILED;
        }
 
        if (hcon->out) {
@@ -525,15 +538,13 @@ static void random_work(struct work_struct *work)
                memset(stk + smp->enc_key_size, 0,
                       SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
 
-               if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) {
-                       reason = SMP_UNSPECIFIED;
-                       goto error;
-               }
+               if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
+                       return SMP_UNSPECIFIED;
 
                hci_le_start_enc(hcon, ediv, rand, stk);
                hcon->enc_key_size = smp->enc_key_size;
        } else {
-               u8 stk[16];
+               u8 stk[16], auth;
                __le64 rand = 0;
                __le16 ediv = 0;
 
@@ -545,15 +556,17 @@ static void random_work(struct work_struct *work)
                memset(stk + smp->enc_key_size, 0,
                       SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
 
+               if (hcon->pending_sec_level == BT_SECURITY_HIGH)
+                       auth = 1;
+               else
+                       auth = 0;
+
                hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
-                           HCI_SMP_STK_SLAVE, 0, stk, smp->enc_key_size,
+                           HCI_SMP_STK_SLAVE, auth, stk, smp->enc_key_size,
                            ediv, rand);
        }
 
-       return;
-
-error:
-       smp_failure(conn, reason);
+       return 0;
 }
 
 static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
@@ -564,9 +577,6 @@ static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
        if (!smp)
                return NULL;
 
-       INIT_WORK(&smp->confirm, confirm_work);
-       INIT_WORK(&smp->random, random_work);
-
        smp->conn = conn;
        conn->smp_chan = smp;
        conn->hcon->smp_conn = conn;
@@ -583,7 +593,7 @@ void smp_chan_destroy(struct l2cap_conn *conn)
 
        BUG_ON(!smp);
 
-       complete = test_bit(SMP_FLAG_COMPLETE, &smp->smp_flags);
+       complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
        mgmt_smp_complete(conn->hcon, complete);
 
        kfree(smp->csrk);
@@ -634,7 +644,7 @@ int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
                put_unaligned_le32(value, smp->tk);
                /* Fall Through */
        case MGMT_OP_USER_CONFIRM_REPLY:
-               set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);
+               set_bit(SMP_FLAG_TK_VALID, &smp->flags);
                break;
        case MGMT_OP_USER_PASSKEY_NEG_REPLY:
        case MGMT_OP_USER_CONFIRM_NEG_REPLY:
@@ -646,8 +656,11 @@ int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
        }
 
        /* If it is our turn to send Pairing Confirm, do so now */
-       if (test_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags))
-               queue_work(hcon->hdev->workqueue, &smp->confirm);
+       if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
+               u8 rsp = smp_confirm(smp);
+               if (rsp)
+                       smp_failure(conn, rsp);
+       }
 
        return 0;
 }
@@ -656,14 +669,13 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
 {
        struct smp_cmd_pairing rsp, *req = (void *) skb->data;
        struct smp_chan *smp;
-       u8 key_size;
-       u8 auth = SMP_AUTH_NONE;
+       u8 key_size, auth;
        int ret;
 
        BT_DBG("conn %p", conn);
 
        if (skb->len < sizeof(*req))
-               return SMP_UNSPECIFIED;
+               return SMP_INVALID_PARAMS;
 
        if (conn->hcon->link_mode & HCI_LM_MASTER)
                return SMP_CMD_NOTSUPP;
@@ -681,8 +693,7 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
        skb_pull(skb, sizeof(*req));
 
        /* We didn't start the pairing, so match remote */
-       if (req->auth_req & SMP_AUTH_BONDING)
-               auth = req->auth_req;
+       auth = req->auth_req;
 
        conn->hcon->pending_sec_level = authreq_to_seclevel(auth);
 
@@ -704,7 +715,7 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
        if (ret)
                return SMP_UNSPECIFIED;
 
-       clear_bit(SMP_FLAG_INITIATOR, &smp->smp_flags);
+       clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
 
        return 0;
 }
@@ -713,14 +724,13 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
 {
        struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
        struct smp_chan *smp = conn->smp_chan;
-       struct hci_dev *hdev = conn->hcon->hdev;
        u8 key_size, auth = SMP_AUTH_NONE;
        int ret;
 
        BT_DBG("conn %p", conn);
 
        if (skb->len < sizeof(*rsp))
-               return SMP_UNSPECIFIED;
+               return SMP_INVALID_PARAMS;
 
        if (!(conn->hcon->link_mode & HCI_LM_MASTER))
                return SMP_CMD_NOTSUPP;
@@ -753,11 +763,11 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
        if (ret)
                return SMP_UNSPECIFIED;
 
-       set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags);
+       set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
 
        /* Can't compose response until we have been confirmed */
-       if (test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags))
-               queue_work(hdev->workqueue, &smp->confirm);
+       if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
+               return smp_confirm(smp);
 
        return 0;
 }
@@ -765,12 +775,11 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
 static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
 {
        struct smp_chan *smp = conn->smp_chan;
-       struct hci_dev *hdev = conn->hcon->hdev;
 
        BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
 
        if (skb->len < sizeof(smp->pcnf))
-               return SMP_UNSPECIFIED;
+               return SMP_INVALID_PARAMS;
 
        memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
        skb_pull(skb, sizeof(smp->pcnf));
@@ -778,10 +787,10 @@ static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
        if (conn->hcon->out)
                smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
                             smp->prnd);
-       else if (test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags))
-               queue_work(hdev->workqueue, &smp->confirm);
+       else if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
+               return smp_confirm(smp);
        else
-               set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags);
+               set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
 
        return 0;
 }
@@ -789,19 +798,16 @@ static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
 static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
 {
        struct smp_chan *smp = conn->smp_chan;
-       struct hci_dev *hdev = conn->hcon->hdev;
 
        BT_DBG("conn %p", conn);
 
        if (skb->len < sizeof(smp->rrnd))
-               return SMP_UNSPECIFIED;
+               return SMP_INVALID_PARAMS;
 
        memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
        skb_pull(skb, sizeof(smp->rrnd));
 
-       queue_work(hdev->workqueue, &smp->random);
-
-       return 0;
+       return smp_random(smp);
 }
 
 static u8 smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
@@ -836,7 +842,7 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
        BT_DBG("conn %p", conn);
 
        if (skb->len < sizeof(*rp))
-               return SMP_UNSPECIFIED;
+               return SMP_INVALID_PARAMS;
 
        if (!(conn->hcon->link_mode & HCI_LM_MASTER))
                return SMP_CMD_NOTSUPP;
@@ -861,7 +867,7 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
 
        smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
 
-       clear_bit(SMP_FLAG_INITIATOR, &smp->smp_flags);
+       clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
 
        return 0;
 }
@@ -908,10 +914,11 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
 
        authreq = seclevel_to_authreq(sec_level);
 
-       /* hcon->auth_type is set by pair_device in mgmt.c. If the MITM
-        * flag is set we should also set it for the SMP request.
+       /* Require MITM if IO Capability allows or the security level
+        * requires it.
         */
-       if ((hcon->auth_type & 0x01))
+       if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
+           sec_level > BT_SECURITY_MEDIUM)
                authreq |= SMP_AUTH_MITM;
 
        if (hcon->link_mode & HCI_LM_MASTER) {
@@ -928,7 +935,7 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
                smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
        }
 
-       set_bit(SMP_FLAG_INITIATOR, &smp->smp_flags);
+       set_bit(SMP_FLAG_INITIATOR, &smp->flags);
 
 done:
        hcon->pending_sec_level = sec_level;
@@ -944,7 +951,7 @@ static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
        BT_DBG("conn %p", conn);
 
        if (skb->len < sizeof(*rp))
-               return SMP_UNSPECIFIED;
+               return SMP_INVALID_PARAMS;
 
        /* Ignore this PDU if it wasn't requested */
        if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY))
@@ -969,7 +976,7 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
        BT_DBG("conn %p", conn);
 
        if (skb->len < sizeof(*rp))
-               return SMP_UNSPECIFIED;
+               return SMP_INVALID_PARAMS;
 
        /* Ignore this PDU if it wasn't requested */
        if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY))
@@ -1001,7 +1008,7 @@ static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
        BT_DBG("");
 
        if (skb->len < sizeof(*info))
-               return SMP_UNSPECIFIED;
+               return SMP_INVALID_PARAMS;
 
        /* Ignore this PDU if it wasn't requested */
        if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
@@ -1025,7 +1032,7 @@ static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
        BT_DBG("");
 
        if (skb->len < sizeof(*info))
-               return SMP_UNSPECIFIED;
+               return SMP_INVALID_PARAMS;
 
        /* Ignore this PDU if it wasn't requested */
        if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
@@ -1075,7 +1082,7 @@ static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
        BT_DBG("conn %p", conn);
 
        if (skb->len < sizeof(*rp))
-               return SMP_UNSPECIFIED;
+               return SMP_INVALID_PARAMS;
 
        /* Ignore this PDU if it wasn't requested */
        if (!(smp->remote_key_dist & SMP_DIST_SIGN))
@@ -1358,7 +1365,7 @@ int smp_distribute_keys(struct l2cap_conn *conn)
 
        clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags);
        cancel_delayed_work_sync(&conn->security_timer);
-       set_bit(SMP_FLAG_COMPLETE, &smp->smp_flags);
+       set_bit(SMP_FLAG_COMPLETE, &smp->flags);
        smp_notify_keys(conn);
 
        smp_chan_destroy(conn);