xref: /nrf52832-nimble/packages/NimBLE-latest/nimble/host/src/ble_sm_cmd.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include <string.h>
21 #include <errno.h>
22 #include "nimble/ble.h"
23 #include "nimble/nimble_opt.h"
24 #include "host/ble_sm.h"
25 #include "ble_hs_priv.h"
26 
27 void *
ble_sm_cmd_get(uint8_t opcode,size_t len,struct os_mbuf ** txom)28 ble_sm_cmd_get(uint8_t opcode, size_t len, struct os_mbuf **txom)
29 {
30     struct ble_sm_hdr *hdr;
31 
32     *txom = ble_hs_mbuf_l2cap_pkt();
33     if (*txom == NULL) {
34         return NULL;
35     }
36 
37     if (os_mbuf_extend(*txom, sizeof(*hdr) + len) == NULL) {
38         os_mbuf_free_chain(*txom);
39         return NULL;
40     }
41 
42     hdr = (struct ble_sm_hdr *)(*txom)->om_data;
43 
44     hdr->opcode = opcode;
45 
46     return hdr->data;
47 }
48 
49 /* this function consumes tx os_mbuf */
50 int
ble_sm_tx(uint16_t conn_handle,struct os_mbuf * txom)51 ble_sm_tx(uint16_t conn_handle, struct os_mbuf *txom)
52 {
53     struct ble_l2cap_chan *chan;
54     struct ble_hs_conn *conn;
55 
56     BLE_HS_DBG_ASSERT(ble_hs_locked_by_cur_task());
57 
58     STATS_INC(ble_l2cap_stats, sm_tx);
59 
60     ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_SM,
61                                     &conn, &chan);
62     return ble_l2cap_tx(conn, chan, txom);
63 }
64 
65 #if NIMBLE_BLE_SM
66 void
ble_sm_pair_cmd_log(struct ble_sm_pair_cmd * cmd)67 ble_sm_pair_cmd_log(struct ble_sm_pair_cmd *cmd)
68 {
69     BLE_HS_LOG(DEBUG, "io_cap=%d oob_data_flag=%d authreq=0x%02x "
70                       "mac_enc_key_size=%d init_key_dist=%d "
71                       "resp_key_dist=%d",
72                cmd->io_cap, cmd->oob_data_flag, cmd->authreq,
73                cmd->max_enc_key_size, cmd->init_key_dist,
74                cmd->resp_key_dist);
75 }
76 
77 void
ble_sm_pair_confirm_log(struct ble_sm_pair_confirm * cmd)78 ble_sm_pair_confirm_log(struct ble_sm_pair_confirm *cmd)
79 {
80     BLE_HS_LOG(DEBUG, "value=");
81     ble_hs_log_flat_buf(cmd->value, sizeof cmd->value);
82 }
83 
84 void
ble_sm_pair_random_log(struct ble_sm_pair_random * cmd)85 ble_sm_pair_random_log(struct ble_sm_pair_random *cmd)
86 {
87     BLE_HS_LOG(DEBUG, "value=");
88     ble_hs_log_flat_buf(cmd->value, sizeof cmd->value);
89 }
90 
91 void
ble_sm_pair_fail_log(struct ble_sm_pair_fail * cmd)92 ble_sm_pair_fail_log(struct ble_sm_pair_fail *cmd)
93 {
94     BLE_HS_LOG(DEBUG, "reason=%d", cmd->reason);
95 }
96 
97 void
ble_sm_enc_info_log(struct ble_sm_enc_info * cmd)98 ble_sm_enc_info_log(struct ble_sm_enc_info *cmd)
99 {
100     BLE_HS_LOG(DEBUG, "ltk=");
101     ble_hs_log_flat_buf(cmd->ltk, sizeof cmd->ltk);
102 }
103 
104 void
ble_sm_master_id_log(struct ble_sm_master_id * cmd)105 ble_sm_master_id_log(struct ble_sm_master_id *cmd)
106 {
107     /* These get logged separately to accommodate a bug in the va_args
108      * implementation related to 64-bit integers.
109      */
110     BLE_HS_LOG(DEBUG, "ediv=0x%04x ", cmd->ediv);
111     BLE_HS_LOG(DEBUG, "rand=0x%016llx", cmd->rand_val);
112 }
113 
114 void
ble_sm_id_info_log(struct ble_sm_id_info * cmd)115 ble_sm_id_info_log(struct ble_sm_id_info *cmd)
116 {
117     BLE_HS_LOG(DEBUG, "irk=");
118     ble_hs_log_flat_buf(cmd->irk, sizeof cmd->irk);
119 }
120 
121 void
ble_sm_id_addr_info_log(struct ble_sm_id_addr_info * cmd)122 ble_sm_id_addr_info_log(struct ble_sm_id_addr_info *cmd)
123 {
124     BLE_HS_LOG(DEBUG, "addr_type=%d addr=", cmd->addr_type);
125     BLE_HS_LOG_ADDR(DEBUG, cmd->bd_addr);
126 }
127 
128 void
ble_sm_sign_info_log(struct ble_sm_sign_info * cmd)129 ble_sm_sign_info_log(struct ble_sm_sign_info *cmd)
130 {
131     BLE_HS_LOG(DEBUG, "sig_key=");
132     ble_hs_log_flat_buf(cmd->sig_key, sizeof cmd->sig_key);
133 }
134 
135 void
ble_sm_sec_req_log(struct ble_sm_sec_req * cmd)136 ble_sm_sec_req_log(struct ble_sm_sec_req *cmd)
137 {
138     BLE_HS_LOG(DEBUG, "authreq=0x%02x", cmd->authreq);
139 }
140 
141 void
ble_sm_public_key_log(struct ble_sm_public_key * cmd)142 ble_sm_public_key_log(struct ble_sm_public_key *cmd)
143 {
144     BLE_HS_LOG(DEBUG, "x=");
145     ble_hs_log_flat_buf(cmd->x, sizeof cmd->x);
146     BLE_HS_LOG(DEBUG, "y=");
147     ble_hs_log_flat_buf(cmd->y, sizeof cmd->y);
148 }
149 
150 void
ble_sm_dhkey_check_log(struct ble_sm_dhkey_check * cmd)151 ble_sm_dhkey_check_log(struct ble_sm_dhkey_check *cmd)
152 {
153     BLE_HS_LOG(DEBUG, "value=");
154     ble_hs_log_flat_buf(cmd->value, sizeof cmd->value);
155 }
156 
157 #endif
158