Bluetooth: Track keys in the connection context

To avoid spending unnecessary time looking up the same keys multiple
times simply store a pointer to them from the connection context.

Change-Id: I94b550870c863237348a2cab7ba056be2a7f4bc5
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
diff --git a/net/bluetooth/conn.h b/net/bluetooth/conn.h
index 94f6165..089f726 100644
--- a/net/bluetooth/conn.h
+++ b/net/bluetooth/conn.h
@@ -55,6 +55,8 @@
 	/* Queue for outgoing ACL data */
 	struct nano_fifo	tx_queue;
 
+	struct bt_keys		*keys;
+
 	/* Fixed channel contexts */
 	struct bt_conn_l2cap	l2cap;
 	void			*att;
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index d03b6a8..1b27b3e 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -558,7 +558,6 @@
 {
 	struct bt_hci_evt_le_ltk_request *evt = (void *)buf->data;
 	struct bt_conn *conn;
-	struct bt_keys *keys;
 	uint16_t handle;
 
 	handle = sys_le16_to_cpu(evt->handle);
@@ -571,9 +570,12 @@
 		return;
 	}
 
-	keys = bt_keys_find(BT_KEYS_SLAVE_LTK, &conn->dst);
-	if (keys && keys->slave_ltk.rand == evt->rand &&
-	    keys->slave_ltk.ediv == evt->ediv) {
+	if (!conn->keys)
+		conn->keys = bt_keys_find(BT_KEYS_SLAVE_LTK, &conn->dst);
+
+	if (conn->keys && (conn->keys->keys & BT_KEYS_SLAVE_LTK) &&
+	    conn->keys->slave_ltk.rand == evt->rand &&
+	    conn->keys->slave_ltk.ediv == evt->ediv) {
 		struct bt_hci_cp_le_ltk_req_reply *cp;
 
 		buf = bt_hci_cmd_create(BT_HCI_OP_LE_LTK_REQ_REPLY,
@@ -585,7 +587,7 @@
 
 		cp = bt_buf_add(buf, sizeof(*cp));
 		cp->handle = evt->handle;
-		memcpy(cp->ltk, keys->slave_ltk.val, 16);
+		memcpy(cp->ltk, conn->keys->slave_ltk.val, 16);
 
 		bt_hci_cmd_send(BT_HCI_OP_LE_LTK_REQ_REPLY, buf);
 	} else {