You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/02/10 04:17:22 UTC
[3/5] incubator-mynewt-larva git commit: Stub security manager
implementation.
Stub security manager implementation.
Right now we just respond to paring requests with a "pairing not
supported" error response.
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/3ad18b7f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/3ad18b7f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/3ad18b7f
Branch: refs/heads/master
Commit: 3ad18b7f633833e8214947d66ea9354663099258
Parents: 3485a6a
Author: Christopher Collins <cc...@gmail.com>
Authored: Tue Feb 9 19:06:25 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Tue Feb 9 19:10:44 2016 -0800
----------------------------------------------------------------------
net/nimble/host/include/host/ble_hs.h | 1 +
net/nimble/host/include/host/ble_l2cap.h | 15 ++
net/nimble/host/src/ble_hs_conn.c | 12 +-
net/nimble/host/src/ble_l2cap.c | 2 +
net/nimble/host/src/ble_l2cap_priv.h | 1 +
net/nimble/host/src/ble_l2cap_sm.c | 202 ++++++++++++++++++++++++++
net/nimble/host/src/ble_l2cap_sm.h | 27 ++++
7 files changed, 258 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3ad18b7f/net/nimble/host/include/host/ble_hs.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_hs.h b/net/nimble/host/include/host/ble_hs.h
index eaa5a3a..45769b9 100644
--- a/net/nimble/host/include/host/ble_hs.h
+++ b/net/nimble/host/include/host/ble_hs.h
@@ -48,6 +48,7 @@
#define BLE_HS_EDONE 15
#define BLE_HS_EBUSY 16
#define BLE_HS_EREJECT 17
+#define BLE_HS_EUNKNOWN 18
#define BLE_HS_ERR_ATT_BASE 0x100 /* 256 */
#define BLE_HS_ATT_ERR(x) ((x) ? BLE_HS_ERR_ATT_BASE + (x) : 0)
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3ad18b7f/net/nimble/host/include/host/ble_l2cap.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_l2cap.h b/net/nimble/host/include/host/ble_l2cap.h
index ac45b2a..a04400b 100644
--- a/net/nimble/host/include/host/ble_l2cap.h
+++ b/net/nimble/host/include/host/ble_l2cap.h
@@ -51,6 +51,21 @@ struct ble_hs_conn;
#define BLE_L2CAP_ERR_MTU_EXCEEDED 0x0001
#define BLE_L2CAP_ERR_INVALID_CID 0x0002
+#define BLE_L2CAP_SM_OP_PAIR_REQ 0x01
+#define BLE_L2CAP_SM_OP_PAIR_RSP 0x02
+#define BLE_L2CAP_SM_OP_PAIR_CONFIRM 0x03
+#define BLE_L2CAP_SM_OP_PAIR_RANDOM 0x04
+#define BLE_L2CAP_SM_OP_PAIR_FAILED 0x05
+#define BLE_L2CAP_SM_OP_ENC_INFO 0x06
+#define BLE_L2CAP_SM_OP_MASTER_ID 0x07
+#define BLE_L2CAP_SM_OP_IDENTITY_INFO 0x08
+#define BLE_L2CAP_SM_OP_IDENTITY_ADDR_INFO 0x09
+#define BLE_L2CAP_SM_OP_SIGN_INFO 0x0a
+#define BLE_L2CAP_SM_OP_SEC_REQ 0x0b
+#define BLE_L2CAP_SM_OP_PAIR_PUBLIC_KEY 0x0c
+#define BLE_L2CAP_SM_OP_PAIR_DHKEY_CHECK 0x0d
+#define BLE_L2CAP_SM_OP_PAIR_KEYPRESS_NOTIFY 0x0e
+
typedef void ble_l2cap_sig_update_fn(int status, void *arg);
struct ble_l2cap_sig_update_params {
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3ad18b7f/net/nimble/host/src/ble_hs_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_conn.c b/net/nimble/host/src/ble_hs_conn.c
index a8ca744..8e54bc2 100644
--- a/net/nimble/host/src/ble_hs_conn.c
+++ b/net/nimble/host/src/ble_hs_conn.c
@@ -25,6 +25,7 @@
#include "ble_hs_priv.h"
#include "ble_l2cap_priv.h"
#include "ble_l2cap_sig.h"
+#include "ble_l2cap_sm.h"
#include "ble_att_priv.h"
#include "ble_gatt_priv.h"
#include "ble_hs_conn.h"
@@ -181,12 +182,19 @@ ble_hs_conn_alloc(void)
goto err;
}
- rc = ble_gatts_conn_init(&conn->bhc_gatt_svr);
+ chan = ble_l2cap_sm_create_chan();
+ if (chan == NULL) {
+ goto err;
+ }
+ rc = ble_hs_conn_chan_insert(conn, chan);
if (rc != 0) {
goto err;
}
- /* XXX: Sort channels by cid. */
+ rc = ble_gatts_conn_init(&conn->bhc_gatt_svr);
+ if (rc != 0) {
+ goto err;
+ }
return conn;
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3ad18b7f/net/nimble/host/src/ble_l2cap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap.c b/net/nimble/host/src/ble_l2cap.c
index 05e6bd0..e4ad665 100644
--- a/net/nimble/host/src/ble_l2cap.c
+++ b/net/nimble/host/src/ble_l2cap.c
@@ -226,6 +226,8 @@ ble_l2cap_rx(struct ble_hs_conn *conn,
chan = ble_hs_conn_chan_find(conn, l2cap_hdr.blh_cid);
if (chan == NULL) {
+ BLE_HS_LOG(DEBUG, "rx on unknown L2CAP channel: %d\n",
+ l2cap_hdr.blh_cid);
rc = BLE_HS_ENOENT;
goto err;
}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3ad18b7f/net/nimble/host/src/ble_l2cap_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_priv.h b/net/nimble/host/src/ble_l2cap_priv.h
index 75c34fb..991e3c5 100644
--- a/net/nimble/host/src/ble_l2cap_priv.h
+++ b/net/nimble/host/src/ble_l2cap_priv.h
@@ -58,6 +58,7 @@ struct ble_l2cap_sig_update_rsp {
#define BLE_L2CAP_CID_ATT 4
#define BLE_L2CAP_CID_SIG 5
+#define BLE_L2CAP_CID_SM 6
#define BLE_L2CAP_HDR_SZ 4
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3ad18b7f/net/nimble/host/src/ble_l2cap_sm.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sm.c b/net/nimble/host/src/ble_l2cap_sm.c
new file mode 100644
index 0000000..df1c74d
--- /dev/null
+++ b/net/nimble/host/src/ble_l2cap_sm.c
@@ -0,0 +1,202 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <string.h>
+#include <errno.h>
+#include <assert.h>
+#include "console/console.h"
+#include "nimble/ble.h"
+#include "ble_hs_priv.h"
+#include "ble_hs_conn.h"
+#include "ble_gap_priv.h"
+#include "ble_l2cap_priv.h"
+#include "ble_att_priv.h"
+#include "ble_l2cap_priv.h"
+#include "ble_l2cap_sm.h"
+
+typedef int ble_l2cap_sm_rx_fn(uint16_t conn_handle, uint8_t op,
+ struct os_mbuf **om);
+
+static int ble_l2cap_sm_rx_noop(uint16_t conn_handle, uint8_t op,
+ struct os_mbuf **om);
+static int ble_l2cap_sm_pair_req_rx(uint16_t conn_handle, uint8_t op,
+ struct os_mbuf **om);
+
+static ble_l2cap_sm_rx_fn * const ble_l2cap_sm_dispatch[] = {
+ [BLE_L2CAP_SM_OP_PAIR_REQ] = ble_l2cap_sm_pair_req_rx,
+ [BLE_L2CAP_SM_OP_PAIR_RSP] = ble_l2cap_sm_rx_noop,
+ [BLE_L2CAP_SM_OP_PAIR_CONFIRM] = ble_l2cap_sm_rx_noop,
+ [BLE_L2CAP_SM_OP_PAIR_RANDOM] = ble_l2cap_sm_rx_noop,
+ [BLE_L2CAP_SM_OP_PAIR_FAILED] = ble_l2cap_sm_rx_noop,
+ [BLE_L2CAP_SM_OP_ENC_INFO] = ble_l2cap_sm_rx_noop,
+ [BLE_L2CAP_SM_OP_MASTER_ID] = ble_l2cap_sm_rx_noop,
+ [BLE_L2CAP_SM_OP_IDENTITY_INFO] = ble_l2cap_sm_rx_noop,
+ [BLE_L2CAP_SM_OP_IDENTITY_ADDR_INFO] = ble_l2cap_sm_rx_noop,
+ [BLE_L2CAP_SM_OP_SIGN_INFO] = ble_l2cap_sm_rx_noop,
+ [BLE_L2CAP_SM_OP_SEC_REQ] = ble_l2cap_sm_rx_noop,
+ [BLE_L2CAP_SM_OP_PAIR_PUBLIC_KEY] = ble_l2cap_sm_rx_noop,
+ [BLE_L2CAP_SM_OP_PAIR_DHKEY_CHECK] = ble_l2cap_sm_rx_noop,
+ [BLE_L2CAP_SM_OP_PAIR_KEYPRESS_NOTIFY] = ble_l2cap_sm_rx_noop,
+};
+
+/**
+ * Lock restrictions: None.
+ */
+static ble_l2cap_sm_rx_fn *
+ble_l2cap_sm_dispatch_get(uint8_t op)
+{
+ if (op > sizeof ble_l2cap_sm_dispatch / sizeof ble_l2cap_sm_dispatch[0]) {
+ return NULL;
+ }
+
+ return ble_l2cap_sm_dispatch[op];
+}
+
+/**
+ * Lock restrictions:
+ * o Caller unlocks ble_hs_conn.
+ */
+static int
+ble_l2cap_sm_conn_chan_find(uint16_t conn_handle,
+ struct ble_hs_conn **out_conn,
+ struct ble_l2cap_chan **out_chan)
+{
+ ble_hs_conn_lock();
+
+ *out_conn = ble_hs_conn_find(conn_handle);
+ if (*out_conn != NULL) {
+ *out_chan = ble_hs_conn_chan_find(*out_conn, BLE_L2CAP_CID_SM);
+ assert(*out_chan != NULL);
+ }
+
+ ble_hs_conn_unlock();
+
+ if (*out_conn == NULL) {
+ return BLE_HS_ENOTCONN;
+ } else {
+ return 0;
+ }
+}
+
+/**
+ * Lock restrictions: None.
+ */
+static int
+ble_l2cap_sm_rx_noop(uint16_t conn_handle, uint8_t op, struct os_mbuf **om)
+{
+ return 0;
+}
+
+static int
+ble_l2cap_sm_pair_req_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om)
+{
+ struct ble_l2cap_chan *chan;
+ struct ble_hs_conn *conn;
+ struct os_mbuf *txom;
+ uint8_t *u;
+ int rc;
+
+ txom = NULL;
+
+ rc = ble_l2cap_sm_conn_chan_find(conn_handle, &conn, &chan);
+ if (rc != 0) {
+ rc = BLE_HS_EUNKNOWN;
+ goto err;
+ }
+
+ txom = ble_hs_misc_pkthdr();
+ if (txom == NULL) {
+ rc = BLE_HS_ENOMEM;
+ goto err;
+ }
+
+ u = os_mbuf_extend(txom, 2);
+ if (u == NULL) {
+ rc = BLE_HS_ENOMEM;
+ goto err;
+ }
+
+ /* Op. */
+ u[0] = BLE_L2CAP_SM_OP_PAIR_FAILED;
+
+ /* Reason: Pairing not supported. */
+ u[1] = 5;
+
+ rc = ble_l2cap_tx(conn, chan, txom);
+ return rc;
+
+err:
+ os_mbuf_free_chain(txom);
+ return rc;
+}
+
+/**
+ * Lock restrictions:
+ * o Caller unlocks ble_hs_conn.
+ */
+static int
+ble_l2cap_sm_rx(uint16_t conn_handle, struct os_mbuf **om)
+{
+ ble_l2cap_sm_rx_fn *rx_cb;
+ uint8_t op;
+ int rc;
+
+ BLE_HS_LOG(DEBUG, "L2CAP - rxed security manager msg: ");
+ ble_hs_misc_log_mbuf(*om);
+ BLE_HS_LOG(DEBUG, "\n");
+
+ *om = os_mbuf_pullup(*om, 1);
+ if (*om == NULL) {
+ return BLE_HS_EBADDATA;
+ }
+ op = *(*om)->om_data;
+
+ /* Strip L2CAP sm header from the front of the mbuf. */
+ os_mbuf_adj(*om, 1);
+
+ rx_cb = ble_l2cap_sm_dispatch_get(op);
+ if (rx_cb != NULL) {
+ rc = rx_cb(conn_handle, op, om);
+ } else {
+ rc = BLE_HS_ENOTSUP;
+ }
+
+ return rc;
+}
+/**
+ * Lock restrictions: None.
+ */
+struct ble_l2cap_chan *
+ble_l2cap_sm_create_chan(void)
+{
+ struct ble_l2cap_chan *chan;
+
+ chan = ble_l2cap_chan_alloc();
+ if (chan == NULL) {
+ return NULL;
+ }
+
+ chan->blc_cid = BLE_L2CAP_CID_SM;
+ chan->blc_my_mtu = BLE_L2CAP_SM_MTU;
+ chan->blc_default_mtu = BLE_L2CAP_SM_MTU;
+ chan->blc_rx_fn = ble_l2cap_sm_rx;
+
+ return chan;
+}
+
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/3ad18b7f/net/nimble/host/src/ble_l2cap_sm.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sm.h b/net/nimble/host/src/ble_l2cap_sm.h
new file mode 100644
index 0000000..e2099f6
--- /dev/null
+++ b/net/nimble/host/src/ble_l2cap_sm.h
@@ -0,0 +1,27 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef H_BLE_L2CAP_SM_
+#define H_BLE_L2CAP_SM_
+
+#define BLE_L2CAP_SM_MTU 65
+
+struct ble_l2cap_chan *ble_l2cap_sm_create_chan(void);
+
+#endif