nimble/sm: Add support for rejecting pairing with disabled SM

If pairing is not supported host shall response to Pairing Request
or Security Request with Pairing Failed PDU and set reason to
"Pairing not supported".

< ACL Data TX: Handle 76 flags 0x00 dlen 11
      SMP: Pairing Request (0x01) len 6
        IO capability: DisplayYesNo (0x01)
        OOB data: Authentication data not present (0x00)
        Authentication requirement: Bonding, MITM, SC, No Keypresses (0x0d)
        Max encryption key size: 16
        Initiator key distribution: EncKey Sign LinkKey (0x0d)
        Responder key distribution: EncKey IdKey Sign LinkKey (0x0f)
> ACL Data RX: Handle 76 flags 0x02 dlen 6
      SMP: Pairing Failed (0x05) len 1
        Reason: Pairing not supported (0x05)


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/f50818b2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/f50818b2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/f50818b2

Branch: refs/heads/develop
Commit: f50818b2ad5fa8af2d9fd5c5f5a19e1e54ee550a
Parents: 257381d
Author: Szymon Janc <[email protected]>
Authored: Tue Jan 3 17:37:21 2017 +0100
Committer: Szymon Janc <[email protected]>
Committed: Wed Jan 4 15:27:52 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_hs_conn.c |  6 ++--
 net/nimble/host/src/ble_sm.c      | 66 ++++++++++++++++++++++++----------
 net/nimble/host/src/ble_sm_priv.h |  5 ++-
 3 files changed, 52 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f50818b2/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 e92f9e5..ebf65aa 100644
--- a/net/nimble/host/src/ble_hs_conn.c
+++ b/net/nimble/host/src/ble_hs_conn.c
@@ -138,10 +138,9 @@ ble_hs_conn_alloc(void)
         goto err;
     }
 
-    /* XXX: We should create the SM channel even if not configured.  We need it
-     * to reject SM messages.
+    /* Create the SM channel even if not configured. We need it to reject SM
+     * messages.
      */
-#if NIMBLE_BLE_SM
     chan = ble_sm_create_chan();
     if (chan == NULL) {
         goto err;
@@ -150,7 +149,6 @@ ble_hs_conn_alloc(void)
     if (rc != 0) {
         goto err;
     }
-#endif
 
     rc = ble_gatts_conn_init(&conn->bhc_gatt_svr);
     if (rc != 0) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f50818b2/net/nimble/host/src/ble_sm.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm.c b/net/nimble/host/src/ble_sm.c
index a49c3d2..fa16433 100644
--- a/net/nimble/host/src/ble_sm.c
+++ b/net/nimble/host/src/ble_sm.c
@@ -2236,24 +2236,6 @@ ble_sm_rx(uint16_t conn_handle, struct os_mbuf **om)
     return rc;
 }
 
-struct ble_l2cap_chan *
-ble_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_SM_MTU;
-    chan->blc_default_mtu = BLE_SM_MTU;
-    chan->blc_rx_fn = ble_sm_rx;
-
-    return chan;
-}
-
 int
 ble_sm_inject_io(uint16_t conn_handle, struct ble_sm_io *pkey)
 {
@@ -2378,5 +2360,53 @@ ble_sm_init(void)
 
     return 0;
 }
+#else
+/* if pairing is not supported it is only needed to reply with Pairing
+ * Failed with 'Pairing not Supported' reason so this function can be very
+ * simple
+ */
+static int
+ble_sm_rx(uint16_t handle, struct os_mbuf **om)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    struct os_mbuf *txom;
+    uint8_t *cmd;
 
+    txom = ble_hs_mbuf_l2cap_pkt();
+    if (txom == NULL) {
+        return BLE_HS_ENOMEM;
+    }
+
+    cmd = os_mbuf_extend(txom, BLE_SM_HDR_SZ + BLE_SM_PAIR_FAIL_SZ);
+    if (cmd == NULL) {
+        os_mbuf_free_chain(txom);
+        return BLE_HS_ENOMEM;
+    }
+
+    cmd[0] = BLE_SM_OP_PAIR_FAIL;
+    cmd[1] = BLE_SM_ERR_PAIR_NOT_SUPP;
+
+    ble_hs_misc_conn_chan_find_reqd(handle, BLE_L2CAP_CID_SM, &conn, &chan);
+
+    return ble_l2cap_tx(conn, chan, txom);
+}
 #endif
+
+struct ble_l2cap_chan *
+ble_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_SM_MTU;
+    chan->blc_default_mtu = BLE_SM_MTU;
+    chan->blc_rx_fn = ble_sm_rx;
+
+    return chan;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f50818b2/net/nimble/host/src/ble_sm_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_priv.h 
b/net/nimble/host/src/ble_sm_priv.h
index d75d475..e802d8d 100644
--- a/net/nimble/host/src/ble_sm_priv.h
+++ b/net/nimble/host/src/ble_sm_priv.h
@@ -302,8 +302,6 @@ int ble_sm_dbg_num_procs(void);
 
 uint8_t ble_sm_build_authreq(void);
 
-struct ble_l2cap_chan *ble_sm_create_chan(void);
-
 void ble_sm_pair_cmd_parse(void *payload, int len,
                            struct ble_sm_pair_cmd *cmd);
 int ble_sm_pair_cmd_is_valid(struct ble_sm_pair_cmd *cmd);
@@ -478,7 +476,6 @@ int ble_sm_init(void);
 #else
 
 #define ble_sm_dbg_num_procs() 0
-#define ble_sm_create_chan() NULL
 #define ble_sm_enc_change_rx(evt) ((void)(evt))
 #define ble_sm_ltk_req_rx(evt) ((void)(evt))
 #define ble_sm_enc_key_refresh_rx(evt) ((void)(evt))
@@ -494,6 +491,8 @@ int ble_sm_init(void);
 
 #endif
 
+struct ble_l2cap_chan *ble_sm_create_chan(void);
+
 #ifdef __cplusplus
 }
 #endif

Reply via email to