• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 
19 /* ****************************************************************************
20   1 头文件包含
21 **************************************************************************** */
22 #include "oal_ext_if.h"
23 #include "oal_err_wifi.h"
24 #include "oal_mem.h"
25 #include "mac_frame.h"
26 #include "hmac_wapi.h"
27 #include "mac_data.h"
28 #include "hmac_tx_data.h"
29 
30 #ifdef __cplusplus
31 #if __cplusplus
32 extern "C" {
33 #endif
34 #endif
35 
36 /* ****************************************************************************
37   2 全局变量定义
38 **************************************************************************** */
39 const hi_u8 g_auc_wapi_pn_init[WAPI_PN_LEN] = {0x36, 0x5c, 0x36, 0x5c, 0x36, 0x5c, 0x36,
40                                                0x5c, 0x36, 0x5c, 0x36, 0x5c, 0x36, 0x5c, 0x36, 0x5c};
41 
42 const hi_u8 g_auc_wapi_oui[WAPI_IE_OUI_SIZE] = { 0x00, 0x14, 0x72 };
43 
44 /* ****************************************************************************
45   3 函数实现
46 **************************************************************************** */
47 /* ****************************************************************************
48  功能描述  : 将PN倒转一下
49  输入参数  : UWORD8 *pucPn - PN值数组存储的地方
50              UWORD8 ucLen - PN值的长度
51 **************************************************************************** */
hmac_wpi_swap_pn(hi_u8 * puc_pn,hi_u8 len)52 static inline hi_void hmac_wpi_swap_pn(hi_u8 *puc_pn, hi_u8 len)
53 {
54     hi_u8 index;
55     hi_u8 temp;
56 
57     for (index = 0; index < len / 2; index++) { /* 2 用于计算 */
58         temp = puc_pn[index];
59         puc_pn[index] = puc_pn[len - 1 - index];
60         puc_pn[len - 1 - index] = temp;
61     }
62 }
63 
64 /* ****************************************************************************
65  功能描述  : 判断是否为qos帧
66  修改历史      :
67   1.日    期   : 2015年5月20日
68     作    者   : HiSilicon
69     修改内容   : 新生成函数
70 **************************************************************************** */
hmac_wapi_is_qos(const mac_ieee80211_frame_stru * mac_hdr)71 static inline hi_u8 hmac_wapi_is_qos(const mac_ieee80211_frame_stru *mac_hdr)
72 {
73     if ((mac_hdr->frame_control.type == WLAN_DATA_BASICTYPE) && (WLAN_QOS_DATA & mac_hdr->frame_control.sub_type)) {
74         return HI_TRUE;
75     }
76 
77     return HI_FALSE;
78 }
79 
80 /* ****************************************************************************
81  功能描述  : 计算mic数据,作为计算mic之用
82  输出参数  : mic数据缓冲区的长度
83  修改历史      :
84   1.日    期   : 2015年5月21日
85     作    者   : HiSilicon
86     修改内容   : 创建
87     其    他   :hmac_rx_get_mac_hdr_len已经过滤4地址的情况,此处不再考虑
88                 mic数据具体构成见<wapi实施指南>5.2.2.3
89 **************************************************************************** */
hmac_wapi_calc_mic_data(const mac_ieee80211_frame_stru * mac_hdr,hi_u8 keyidx,const hi_u8 * puc_payload,hi_u16 us_pdu_len,const mic_date_stru * mac_date)90 hi_u32 hmac_wapi_calc_mic_data(const mac_ieee80211_frame_stru *mac_hdr, hi_u8 keyidx, const hi_u8 *puc_payload,
91     hi_u16 us_pdu_len, const mic_date_stru *mac_date)
92 {
93     hi_u8 us_is_qos;
94     hi_u8 *puc_mic_oringin = HI_NULL;
95     hi_u8 *puc_mic = mac_date->puc_mic;
96     hi_u16 us_mic_len = mac_date->us_mic_len;
97 
98     if (memset_s(puc_mic, us_mic_len, 0, us_mic_len) != EOK) {
99         return HI_FAIL;
100     }
101 
102     puc_mic_oringin = puc_mic;
103 
104     /* frame control */
105     if (memcpy_s(puc_mic, sizeof(mac_hdr->frame_control), (hi_u8 *)&(mac_hdr->frame_control),
106         sizeof(mac_hdr->frame_control)) != EOK) {
107         oam_error_log0(0, OAM_SF_CFG, "hmac_wapi_calc_mic_data:: st_frame_control memcpy_s fail.");
108         return HI_FAIL;
109     }
110     puc_mic[0] &= ~(BIT4 | BIT5 | BIT6); /* sub type */
111     puc_mic[1] &= ~(BIT3 | BIT4 | BIT5); /* retry, pwr Mgmt, more data */
112     puc_mic[1] |= BIT6;
113 
114     puc_mic += sizeof(mac_hdr->frame_control);
115 
116     /* addr1 */
117     mac_get_address1((hi_u8 *)mac_hdr, WLAN_MAC_ADDR_LEN, puc_mic, WLAN_MAC_ADDR_LEN);
118     puc_mic += OAL_MAC_ADDR_LEN;
119 
120     /* addr2 */
121     mac_get_address2((hi_u8 *)mac_hdr, WLAN_MAC_ADDR_LEN, puc_mic, WLAN_MAC_ADDR_LEN);
122     puc_mic += OAL_MAC_ADDR_LEN;
123 
124     /* 序列控制 */
125     if (memset_s(puc_mic, WAPI_MIC_SEQ_CONTROL_LEN, 0, WAPI_MIC_SEQ_CONTROL_LEN) != EOK) {
126         return HI_FAIL;
127     }
128     puc_mic[0] = (hi_u8)(mac_hdr->frag_num);
129     puc_mic += 2; /* 数组位置增2 */
130 
131     /* addr3 */
132     mac_get_address3((hi_u8 *)mac_hdr, WLAN_MAC_ADDR_LEN, puc_mic, WLAN_MAC_ADDR_LEN);
133     puc_mic += OAL_MAC_ADDR_LEN;
134 
135     /* 跳过addr4 */
136     puc_mic += OAL_MAC_ADDR_LEN;
137 
138     /* qos ctrl */
139     us_is_qos = hmac_wapi_is_qos(mac_hdr);
140     if (us_is_qos == HI_TRUE) {
141         mac_get_qos_ctrl((hi_u8 *)mac_hdr, puc_mic);
142         puc_mic += MAC_QOS_CTL_LEN;
143     }
144 
145     /* keyidx + reserve总共2个字节 */
146     *puc_mic = keyidx;
147     puc_mic += 2; /* 数组位置增2 */
148 
149     /* 填充pdulen 协议写明大端字节序 */
150     *puc_mic = (hi_u8)((us_pdu_len & 0xff00) >> 8); /* 右移8位 */
151     *(puc_mic + 1) = (hi_u8)(us_pdu_len & 0x00ff);
152 
153     /* ***********填充第2部分****************** */
154     puc_mic_oringin += (HI_TRUE == hmac_wapi_is_qos(mac_hdr)) ? SMS4_MIC_PART1_QOS_LEN : SMS4_MIC_PART1_NO_QOS_LEN;
155     if (memcpy_s(puc_mic_oringin, us_pdu_len, puc_payload, us_pdu_len) != EOK) {
156         oam_error_log0(0, OAM_SF_CFG, "hmac_wapi_calc_mic_data:: puc_payload memcpy_s fail.");
157         return HI_FAIL;
158     }
159 
160     return HI_SUCCESS;
161 }
162 
163 /* ****************************************************************************
164  功能描述  : 申请空间,用于存放mic
165  返 回 值  : 申请到的空间指针
166  修改历史      :
167   1.日    期   : 2015年5月21日
168     作    者   : HiSilicon
169     修改内容   : 创建
170 **************************************************************************** */
hmac_wapi_mic_alloc(hi_u8 is_qos,hi_u16 us_pdu_len,hi_u16 * pus_mic_len)171 static inline hi_u8 *hmac_wapi_mic_alloc(hi_u8 is_qos, hi_u16 us_pdu_len, hi_u16 *pus_mic_len)
172 {
173     hi_u16 us_mic_part1_len;
174     hi_u16 us_mic_part2_len;
175     hi_u16 us_mic_len;
176 
177     us_mic_part1_len = (HI_TRUE == is_qos) ? SMS4_MIC_PART1_QOS_LEN : SMS4_MIC_PART1_NO_QOS_LEN;
178     /* 按照协议,补齐不足位,16字节对齐 */
179     us_mic_part2_len = padding(us_pdu_len, SMS4_PADDING_LEN);
180     us_mic_len = us_mic_part1_len + us_mic_part2_len;
181     *pus_mic_len = us_mic_len;
182 
183     return oal_mem_alloc(OAL_MEM_POOL_ID_LOCAL, us_mic_len);
184 }
185 
186 /* ****************************************************************************
187  功能描述  : 释放申请的mic空间
188  修改历史      :
189   1.日    期   : 2015年5月21日
190     作    者   : HiSilicon
191     修改内容   : 创建
192     其    他
193 **************************************************************************** */
hmac_wapi_mic_free(const hi_u8 * puc_mic)194 static inline hi_u32 hmac_wapi_mic_free(const hi_u8 *puc_mic)
195 {
196     if (puc_mic != HI_NULL) {
197         oal_mem_free(puc_mic);
198         return HI_SUCCESS;
199     }
200     return HI_FAIL;
201 }
202 
203 /* ****************************************************************************
204  功能描述  : 判断keyidx是否合法
205  修改历史      :
206   1.日    期   : 2012年5月2日
207     作    者   : HiSilicon
208     修改内容   : 新生成函数
209 **************************************************************************** */
hmac_wapi_is_keyidx_valid(hmac_wapi_stru * wapi,hi_u8 keyidx_rx)210 hi_u8 hmac_wapi_is_keyidx_valid(hmac_wapi_stru *wapi, hi_u8 keyidx_rx)
211 {
212     if (wapi->keyidx != keyidx_rx && wapi->keyupdate_flg != HI_TRUE) {
213         oam_warning_log3(0, OAM_SF_ANY, "{hmac_wapi_is_keyidx_valid::keyidx==%u, uc_keyidx_rx==%u, update==%u.}",
214             wapi->keyidx, keyidx_rx, wapi->keyupdate_flg);
215         wapi_rx_idx_update_err(wapi);
216         return HI_FALSE;
217     }
218 
219     wapi->keyupdate_flg = HI_FALSE; /* 更新完成取消标志 */
220 
221     /* key没有启用 */
222     if (wapi->ast_wapi_key[keyidx_rx].key_en != HI_TRUE) {
223         oam_warning_log1(0, OAM_SF_ANY, "{hmac_wapi_is_keyidx_valid::keyen==%u.}",
224             wapi->ast_wapi_key[keyidx_rx].key_en);
225         wapi_rx_idx_update_err(wapi);
226         return HI_FALSE;
227     }
228 
229     return HI_TRUE;
230 }
231 
232 /* ****************************************************************************
233  功能描述  : 单播帧判断数据奇偶正确性
234  返 回 值  : 如果为偶数则返回false
235  修改历史      :
236   1.日    期   : 2015年5月21日
237     作    者   : HiSilicon
238     修改内容   : 创建
239     其    他
240 **************************************************************************** */
hmac_wapi_is_pn_odd_ucast(const hi_u8 * puc_pn)241 static inline hi_u8 hmac_wapi_is_pn_odd_ucast(const hi_u8 *puc_pn)
242 {
243     return (hi_u8)(((*puc_pn & BIT0) == 0) ? HI_FALSE : HI_TRUE);
244 }
245 
246 /* ****************************************************************************
247  功能描述  : 判断接收到的pn是否大于记录的pn,最后一个数值进位后会小于AP侧记录的值,所以不用检查
248  修改历史      :
249 
250   1.日    期   : 2015年5月21日
251     作    者   : HiSilicon
252     修改内容   : 创建
253 **************************************************************************** */
hmac_wapi_is_pn_bigger(const hi_u8 * puc_pn,const hi_u8 * puc_pn_rx)254 hi_u8 hmac_wapi_is_pn_bigger(const hi_u8 *puc_pn, const hi_u8 *puc_pn_rx)
255 {
256     hi_u8 pnidx;
257 
258     for (pnidx = SMS4_PN_LEN - 1; pnidx > 0; pnidx--) {
259         if ((puc_pn[pnidx] != puc_pn_rx[pnidx])) {
260             if (puc_pn[pnidx] > puc_pn_rx[pnidx]) {
261                 oam_warning_log2(0, OAM_SF_ANY, "{hmac_wapi_is_pn_bigger::err! puc_pn==%u, puc_pn_rx==%u.}",
262                     puc_pn[pnidx], puc_pn_rx[pnidx]);
263                 return HI_FALSE;
264             }
265 
266             return HI_TRUE;
267         }
268     }
269 
270     return HI_TRUE;
271 }
272 
273 /* ****************************************************************************
274  功能描述  : 组播帧判断数据奇偶性
275  修改历史      :
276 
277   1.日    期   : 2015年5月21日
278     作    者   : HiSilicon
279     修改内容   : 创建
280 **************************************************************************** */
hmac_wapi_is_pn_odd_bcast(const hi_u8 * puc_pn)281 static inline hi_u8 hmac_wapi_is_pn_odd_bcast(const hi_u8 *puc_pn)
282 {
283     hi_unref_param(puc_pn);
284     return HI_TRUE;
285 }
286 
287 /* ****************************************************************************
288  功能描述  : 每收到一个帧,更新pn
289  修改历史      :
290   1.日    期   : 2015年5月21日
291     作    者   : HiSilicon
292     修改内容   : 创建
293 **************************************************************************** */
hmac_wapi_pn_update(hi_u8 * puc_pn,hi_u8 inc)294 hi_void hmac_wapi_pn_update(hi_u8 *puc_pn, hi_u8 inc)
295 {
296     hi_u32 loop;
297     hi_u32 loop_num;
298     hi_u32 overlow; /* 进位 */
299     hi_u32 *pul_pn = HI_NULL;
300 
301     pul_pn = (hi_u32 *)puc_pn;
302     loop_num = WAPI_PN_LEN / sizeof(hi_u32);
303     overlow = inc;
304 
305     for (loop = 0; loop < loop_num; loop++) {
306         if (*pul_pn > (*pul_pn + overlow)) {
307             *pul_pn += overlow;
308             overlow = 1; /* 溢出高位加1 */
309         } else {
310             *pul_pn += overlow;
311             break;
312         }
313         pul_pn++;
314     }
315 }
316 
317 /* ****************************************************************************
318  功能描述  : 处理已经分片或者不需分片的netbuff链
319              1)如果已经分片,则处理这个链,将链上所有netbuff上的数据进行加密处理
320              2)如果没有分片,则处理单个netbuff,将这个netbuff上的数据进行加密处理
321  修改历史      :
322 
323   1.日    期   : 2015年5月21日
324     作    者   : HiSilicon
325     修改内容   : 创建
326 **************************************************************************** */
hmac_wapi_netbuff_tx_handle(hmac_wapi_stru * wapi,oal_netbuf_stru * netbuf)327 oal_netbuf_stru *hmac_wapi_netbuff_tx_handle(hmac_wapi_stru *wapi, oal_netbuf_stru *netbuf)
328 {
329     hi_u32 ret;
330     oal_netbuf_stru *netbuf_tmp = HI_NULL;  /* 指向需要释放的netbuff */
331     oal_netbuf_stru *netbuf_prev = HI_NULL; /* 指向已经加密的netbuff */
332     oal_netbuf_stru *buf_first = HI_NULL;   /* 指向还未加密的netbuff */
333 
334     /* 不加密wai帧 */
335     if (MAC_DATA_WAPI == mac_get_data_type_from_80211(netbuf, MAC_80211_QOS_HTC_4ADDR_FRAME_LEN)) {
336         oam_warning_log0(0, OAM_SF_ANY, "{hmac_wapi_netbuff_tx_handle::wai, dont encrypt!.}");
337         return netbuf;
338     }
339 
340     ret = wapi->wapi_encrypt(wapi, netbuf);
341     if (ret != HI_SUCCESS) {
342         hmac_free_netbuf_list(netbuf);
343         return HI_NULL;
344     }
345 
346     netbuf_tmp = netbuf;
347 
348     /* 使netbuff指针指向下一个需要加密的分片帧 */
349     netbuf_prev = oal_netbuf_next(netbuf);
350     if (netbuf_prev == HI_NULL) {
351         return HI_NULL;
352     }
353     buf_first = netbuf_prev;
354     netbuf = oal_netbuf_next(netbuf_prev);
355 
356     oal_netbuf_free(netbuf_tmp);
357 
358     while (netbuf != HI_NULL) {
359         ret = wapi->wapi_encrypt(wapi, netbuf);
360         if (ret != HI_SUCCESS) {
361             hmac_free_netbuf_list(buf_first);
362             return HI_NULL;
363         }
364         set_oal_netbuf_next(netbuf_prev, oal_netbuf_next(netbuf));
365         netbuf_tmp = netbuf;
366         netbuf_prev = oal_netbuf_next(netbuf);
367         if (netbuf_prev == HI_NULL) {
368             return HI_NULL;
369         }
370         netbuf = oal_netbuf_next(netbuf_prev);
371 
372         oal_netbuf_free(netbuf_tmp);
373     }
374     return buf_first;
375 }
376 
377 /* ****************************************************************************
378  功能描述  : 接收处理比发送要简单,因为每次只会有一个netbuff需要处理
379              假设加密的netbuff为1,解密后的为0,那么输入为1,解密完成后变为
380              1->0 本函数将1删掉,然后将已经解密的0往上送
381  修改历史      :
382   1.日    期   : 2015年5月21日
383     作    者   : HiSilicon
384     修改内容   : 创建
385 **************************************************************************** */
hmac_wapi_netbuff_rx_handle(hmac_wapi_stru * wapi,oal_netbuf_stru * netbuf)386 oal_netbuf_stru *hmac_wapi_netbuff_rx_handle(hmac_wapi_stru *wapi, oal_netbuf_stru *netbuf)
387 {
388     hi_u32 ret;
389     oal_netbuf_stru *netbuf_tmp = HI_NULL; /* 指向需要释放的netbuff */
390 
391     /* 非加密帧,不进行解密 */
392     if (!((oal_netbuf_data(netbuf))[1] & 0x40)) {
393         return netbuf;
394     }
395 
396     ret = wapi->wapi_decrypt(wapi, netbuf);
397     if (ret != HI_SUCCESS) {
398         return HI_NULL;
399     }
400 
401     netbuf_tmp = netbuf;
402     netbuf = oal_netbuf_next(netbuf);
403     oal_netbuf_free(netbuf_tmp);
404 
405     return netbuf;
406 }
407 
408 /* ****************************************************************************
409  功能描述  : 增加/更新 key
410  修改历史      :
411   1.日    期   : 2015年5月21日
412     作    者   : HiSilicon
413     修改内容   : 创建
414 **************************************************************************** */
hmac_wapi_add_key(hmac_wapi_stru * wapi,hi_u8 key_index,const hi_u8 * puc_key)415 hi_u32 hmac_wapi_add_key(hmac_wapi_stru *wapi, hi_u8 key_index, const hi_u8 *puc_key)
416 {
417     hmac_wapi_key_stru *key = HI_NULL;
418 
419     wapi_is_port_valid(wapi) = HI_TRUE;
420     wapi->keyidx = key_index;
421     wapi->keyupdate_flg = HI_TRUE;
422     key = &(wapi->ast_wapi_key[key_index]);
423 
424     if (memcpy_s(key->auc_wpi_ek, WAPI_KEY_LEN, puc_key, WAPI_KEY_LEN) != EOK) {
425         oam_error_log0(0, OAM_SF_CFG, "hmac_wapi_add_key:: puc_key memcpy_s fail.");
426         return HI_FAIL;
427     }
428     if (memcpy_s(key->auc_wpi_ck, WAPI_KEY_LEN, puc_key + WAPI_KEY_LEN, WAPI_KEY_LEN) != EOK) {
429         oam_error_log0(0, OAM_SF_CFG, "hmac_wapi_add_key:: puc_key memcpy_s fail.");
430         return HI_FAIL;
431     }
432     key->key_en = HI_TRUE;
433 
434     /* 重置PN */
435     if (memcpy_s(key->auc_pn_rx, WAPI_PN_LEN, g_auc_wapi_pn_init, WAPI_PN_LEN) != EOK) {
436         oam_error_log0(0, OAM_SF_CFG, "hmac_wapi_add_key:: g_auc_wapi_pn_init memcpy_s fail.");
437         return HI_FAIL;
438     }
439     if (memcpy_s(key->auc_pn_tx, WAPI_PN_LEN, g_auc_wapi_pn_init, WAPI_PN_LEN) != EOK) {
440         oam_error_log0(0, OAM_SF_CFG, "hmac_wapi_add_key:: g_auc_wapi_pn_init memcpy_s fail.");
441         return HI_FAIL;
442     }
443 
444     return HI_SUCCESS;
445 }
446 
447 /* ****************************************************************************
448  功能描述  : 检验wpi头的合法性
449  修改历史      :
450   1.日    期   : 2015年5月21日
451     作    者   : HiSilicon
452     修改内容   : 创建
453 **************************************************************************** */
hmac_wapi_is_wpihdr_valid(hmac_wapi_stru * wapi,const hi_u8 * puc_wapi_hdr)454 hi_u8 hmac_wapi_is_wpihdr_valid(hmac_wapi_stru *wapi, const hi_u8 *puc_wapi_hdr)
455 {
456     hi_u8 keyidx_rx;
457     const hi_u8 *puc_pn_rx = HI_NULL;
458 
459     keyidx_rx = *puc_wapi_hdr;
460 
461     if (hmac_wapi_is_keyidx_valid(wapi, keyidx_rx) != HI_TRUE) {
462         oam_warning_log0(0, OAM_SF_ANY, "{hmac_wapi_is_wpihdr_valid::hmac_wapi_is_keyidx_valid==false.}");
463         return HI_FALSE;
464     }
465 
466     puc_pn_rx = puc_wapi_hdr + SMS4_KEY_IDX + SMS4_WAPI_HDR_RESERVE;
467     if (wapi->wapi_is_pn_odd(puc_pn_rx) != HI_TRUE) {
468         oam_warning_log0(0, OAM_SF_ANY, "{hmac_wapi_is_wpihdr_valid::wapi_is_pn_odd==false.}");
469         return HI_FALSE;
470     }
471     return HI_TRUE;
472 }
473 
474 /* ****************************************************************************
475  功能描述  : 对数据进行解密
476  修改历史      :
477   1.日    期   : 2015年5月21日
478     作    者   : HiSilicon
479     修改内容   : 创建
480     其    他   : 此函数如果返回错误,需要将netbuff释放掉,不需要再送到后面进行处理
481 **************************************************************************** */
482 /* 规则5.1 避免函数过长,函数不超过50行(非空非注释),申请例外:对数据进行解密的算法函数,功能内聚,建议屏蔽 */
hmac_wapi_decrypt(hmac_wapi_stru * wapi,oal_netbuf_stru * netbuf)483 hi_u32 hmac_wapi_decrypt(hmac_wapi_stru *wapi, oal_netbuf_stru *netbuf)
484 {
485     hi_u8 auc_calc_mic[SMS4_MIC_LEN];
486     hi_u16 us_mic_len;
487     hmac_wapi_crypt_stru wpi_key_ek = { 0 };
488     hmac_wapi_crypt_stru wpi_key_ck = { 0 };
489     mic_date_stru mac_date;
490 
491     wapi_rx_port_valid(wapi);
492 
493     /* *********** 1. 解密前的数据准备,获取各头指针和内容长度 *********** */
494     hi_u16 netbuff_len = (hi_u16)oal_netbuf_len(netbuf);
495 
496     /* 获取mac头 */
497     mac_ieee80211_frame_stru *mac_hdr = (mac_ieee80211_frame_stru *)oal_netbuf_data(netbuf); /* for ut,del temprarily */
498 
499     /* wapi的数据帧一般为QOS帧  */
500     hmac_rx_ctl_stru *rx_ctl_in = (hmac_rx_ctl_stru *)oal_netbuf_cb(netbuf);
501     hi_u8 mac_hdr_len           = rx_ctl_in->mac_header_len;
502     hi_u8 *puc_wapi_hdr         = (hi_u8 *)mac_hdr + mac_hdr_len;
503     hi_u8 *puc_pn = puc_wapi_hdr + SMS4_KEY_IDX + SMS4_WAPI_HDR_RESERVE;
504 
505     oam_warning_log1(0, OAM_SF_ANY, "{hmac_wpi_decrypt::uc_mac_hdr_len %u!.}", mac_hdr_len);
506 
507     if (netbuff_len < (hi_u16)(mac_hdr_len + HMAC_WAPI_HDR_LEN + SMS4_MIC_LEN)) {
508         oam_error_log2(0, OAM_SF_ANY, "{hmac_wpi_decrypt::netbuff_len %u,machdr len %u err}", netbuff_len, mac_hdr_len);
509         oal_netbuf_free(netbuf);
510         wapi_rx_netbuf_len_err(wapi);
511         return HI_FAIL;
512     }
513 
514     hi_u16 us_pdu_len = netbuff_len - mac_hdr_len - HMAC_WAPI_HDR_LEN - SMS4_MIC_LEN;
515     hi_u8 key_index   = *puc_wapi_hdr;
516 
517     if (key_index >= HMAC_WAPI_MAX_KEYID) {
518         wapi_rx_idx_err(wapi);
519         oam_error_log1(0, OAM_SF_ANY, "{hmac_wpi_decrypt::uc_key_index %u err!.}", key_index);
520         oal_netbuf_free(netbuf);
521         return HI_FAIL;
522     }
523 
524     if (hmac_wapi_is_wpihdr_valid(wapi, puc_wapi_hdr) != HI_TRUE) {
525         oal_netbuf_free(netbuf);
526         return HI_FAIL;
527     }
528 
529     /* *********** 2. 准备新的netbuff,用来存放解密后的数据, 填写cb字段 *********** */
530     oal_netbuf_stru *netbuff_des = oal_netbuf_alloc(WLAN_MGMT_NETBUF_SIZE, 0, 4); /* align 4 */
531     if (netbuff_des == HI_NULL) {
532         wapi_rx_memalloc_err(wapi);
533         oal_netbuf_free(netbuf);
534         return HI_ERR_CODE_ALLOC_MEM_FAIL;
535     }
536 
537     /* 先拷贝mac头 */
538     oal_netbuf_init(netbuff_des, mac_hdr_len);
539 
540     hi_u32 wapi_result = oal_netbuf_copydata(netbuf, 0, oal_netbuf_data(netbuff_des),
541         oal_netbuf_len(netbuff_des), mac_hdr_len);
542     if (wapi_result != HI_SUCCESS) {
543         oam_error_log0(0, OAM_SF_CFG, "hmac_wapi_decrypt:: ul_wapi_result fail.");
544         oal_netbuf_free(netbuf);
545         oal_netbuf_free(netbuff_des);
546         return wapi_result;
547     }
548 
549     /* 拷贝cb */
550     hmac_rx_ctl_stru *rx_ctl = (hmac_rx_ctl_stru *)oal_netbuf_cb(netbuff_des);
551     if (memcpy_s(rx_ctl, sizeof(hmac_rx_ctl_stru), oal_netbuf_cb(netbuf), sizeof(hmac_rx_ctl_stru)) != EOK) {
552         oal_netbuf_free(netbuff_des);
553         oal_netbuf_free(netbuf);
554         oam_error_log0(0, OAM_SF_CFG, "hmac_wapi_decrypt:: pst_netbuf memcpy_s fail.");
555         return HI_FAIL;
556     }
557 
558     /* *********** 3. 解密前的密钥准备和PN准备 *********** */
559     hmac_wpi_swap_pn(puc_pn, SMS4_PN_LEN);
560 
561     /******************** 4. 解密************************* */
562     wpi_key_ek.puc_iv  = puc_pn;
563     wpi_key_ek.iv_len  = SMS4_PN_LEN; /* iv key len 16 */
564     wpi_key_ek.puc_key = wapi->ast_wapi_key[key_index].auc_wpi_ek;
565     wpi_key_ek.key_len = 16; /* ck key len 16 */
566     wapi_result = hmac_wpi_decrypt(wpi_key_ek, puc_pn + SMS4_PN_LEN, (us_pdu_len + SMS4_MIC_LEN), /* 需解密的长度 */
567                                    (oal_netbuf_data(netbuff_des) + mac_hdr_len));
568     if (wapi_result != HI_SUCCESS) {
569         oal_netbuf_free(netbuff_des);
570         /* 返回之前注意入参netbuff是否在外面被释放 */
571         oal_netbuf_free(netbuf);
572         return HI_ERR_CODE_WAPI_DECRYPT_FAIL;
573     }
574 
575     /* mic作为校验数,不需要put */
576     oal_netbuf_put(netbuff_des, us_pdu_len);
577 
578     /* *********** 5. 计算mic,并进行校验 *********** */
579     hi_u8 *puc_mic_data = hmac_wapi_mic_alloc(hmac_wapi_is_qos(mac_hdr), us_pdu_len, &us_mic_len);
580     if (puc_mic_data == HI_NULL) {
581         wapi_rx_memalloc_err(wapi);
582         oal_netbuf_free(netbuff_des);
583         /* 注意netbuff后续是否有释放处理 */
584         oal_netbuf_free(netbuf);
585         return HI_ERR_CODE_ALLOC_MEM_FAIL;
586     }
587 
588     /* 计算mic预备数据 */
589     mac_date.puc_mic = puc_mic_data;
590     mac_date.us_mic_len = us_mic_len;
591     if (hmac_wapi_calc_mic_data(mac_hdr, key_index, oal_netbuf_data(netbuff_des) + mac_hdr_len, us_pdu_len,
592         &mac_date) != HI_SUCCESS) {
593         oam_warning_log0(0, OAM_SF_ANY, "hmac_wapi_calc_mic_data return NON SUCCESS. ");
594     }
595 
596     wpi_key_ck.puc_iv  = puc_pn;
597     wpi_key_ck.iv_len  = 16; /* iv key len 16 */
598     wpi_key_ck.puc_key = wapi->ast_wapi_key[key_index].auc_wpi_ck;
599     wpi_key_ck.key_len = 16; /* ck key len 16 */
600     wapi_result = hmac_wpi_pmac(wpi_key_ck, puc_mic_data, (us_mic_len >> 4), auc_calc_mic, SMS4_MIC_LEN); /* 右移4位 */
601 
602     /* 计算完mic后,释放mic data */
603     if (hmac_wapi_mic_free(puc_mic_data) != HI_SUCCESS) {
604         oam_warning_log0(0, OAM_SF_ANY, "hmac_wapi_mic_free return NON SUCCESS. ");
605     }
606 
607     if (wapi_result != HI_SUCCESS) {
608         oal_netbuf_free(netbuff_des);
609         oal_netbuf_free(netbuf);
610         return HI_ERR_CODE_WAPI_MIC_CALC_FAIL;
611     }
612 
613     hi_u8 *puc_mic = oal_netbuf_data(netbuff_des) + mac_hdr_len + us_pdu_len;
614     if (memcmp(puc_mic, auc_calc_mic, SMS4_MIC_LEN) != 0) { /* 比较MIC */
615         oam_warning_log0(0, OAM_SF_ANY, "{hmac_wpi_decrypt::mic check fail!.}");
616         wapi_rx_mic_err(wapi);
617         oal_netbuf_free(netbuff_des);
618         oal_netbuf_free(netbuf);
619         return HI_ERR_CODE_WAPI_MIC_CMP_FAIL;
620     }
621 
622     /* 返回前清protected */
623     (oal_netbuf_data(netbuff_des))[1] &= ~0x40;
624 
625     /* 填写cb */
626     rx_ctl->pul_mac_hdr_start_addr = (hi_u32 *)oal_netbuf_header(netbuff_des);
627     rx_ctl->mac_header_len         = mac_hdr_len;
628     rx_ctl->us_frame_len           = (hi_u16)oal_netbuf_len(netbuff_des);
629 
630     set_oal_netbuf_next(netbuff_des, oal_netbuf_next(netbuf));
631     set_oal_netbuf_next(netbuf, netbuff_des);
632 
633     hmac_wapi_pn_update(wapi->ast_wapi_key[wapi->keyidx].auc_pn_rx, wapi->pn_inc);
634 
635     oam_warning_log0(0, OAM_SF_ANY, "{hmac_wpi_decrypt::OK!.}");
636     wapi_rx_decrypt_ok(wapi);
637 
638     return HI_SUCCESS;
639 }
640 
hmac_wapi_encrypt_mic(hmac_wapi_stru * wapi,hmac_wapi_encrypt_stru * hmac_wapi,const mac_ieee80211_frame_stru * mac_hdr,const hi_u8 * puc_payload)641 static hi_u32 hmac_wapi_encrypt_mic(hmac_wapi_stru *wapi, hmac_wapi_encrypt_stru *hmac_wapi,
642     const mac_ieee80211_frame_stru *mac_hdr, const hi_u8 *puc_payload)
643 {
644     mic_date_stru mac_date;
645     /* *********** 2. 计算mic,wapi的数据帧一般为QOS帧  *********** */
646     hi_u8 *puc_mic_data = hmac_wapi_mic_alloc(hmac_wapi_is_qos(mac_hdr), hmac_wapi->pdu_len, &hmac_wapi->us_mic_len);
647     if (puc_mic_data == HI_NULL) {
648         wapi_tx_memalloc_err(wapi);
649         oam_error_log0(0, OAM_SF_ANY, "{hmac_wapi_encrypt::hmac_wapi_mic_alloc err!");
650         return HI_ERR_CODE_ALLOC_MEM_FAIL;
651     }
652 
653     /* 计算mic预备数据 */
654     mac_date.puc_mic = puc_mic_data;
655     mac_date.us_mic_len = hmac_wapi->us_mic_len;
656     if (hmac_wapi_calc_mic_data(mac_hdr, hmac_wapi->key_index, puc_payload, hmac_wapi->pdu_len, puc_mic_data,
657         hmac_wapi->us_mic_len) != HI_SUCCESS) {
658         oam_warning_log0(0, OAM_SF_CFG, "hmac_wapi_calc_mic_data return NON SUCCESS. ");
659     }
660 
661     if (memcpy_s(hmac_wapi->auc_pn_swap, SMS4_PN_LEN, wapi->ast_wapi_key[hmac_wapi->key_index].auc_pn_tx,
662         SMS4_PN_LEN) != EOK) {
663         if (hmac_wapi_mic_free(puc_mic_data) != HI_SUCCESS) {
664             oam_warning_log0(0, OAM_SF_CFG, "hmac_wapi_mic_free return NON SUCCESS. ");
665         }
666 
667         oam_error_log0(0, OAM_SF_CFG, "hmac_wapi_encrypt:: auc_pn_tx memcpy_s fail.");
668         return HI_FAIL;
669     }
670     hmac_wpi_swap_pn(hmac_wapi->auc_pn_swap, SMS4_PN_LEN);
671     /* 计算mic */
672     hmac_wapi->wpi_key_ck.puc_iv = hmac_wapi->auc_pn_swap;
673     hmac_wapi->wpi_key_ck.iv_len = SMS4_PN_LEN; /* iv key len 16 */
674     hmac_wapi->wpi_key_ck.puc_key = wapi->ast_wapi_key[hmac_wapi->key_index].auc_wpi_ck;
675     hmac_wapi->wpi_key_ck.key_len = 16; /* ck key len 16 */
676     hi_u32 ret = hmac_wpi_pmac(hmac_wapi->wpi_key_ck, puc_mic_data, (hmac_wapi->us_mic_len >> 4),
677         hmac_wapi->auc_calc_mic, SMS4_MIC_LEN); /* 右移4位 */
678 
679     if (hmac_wapi_mic_free(puc_mic_data) != HI_SUCCESS) {
680         oam_warning_log0(0, OAM_SF_CFG, "hmac_wapi_mic_free return NON SUCCESS. ");
681     }
682     if (ret == HI_FAIL) {
683         wapi_tx_mic_err(wapi);
684         oam_error_log0(0, OAM_SF_ANY, "{hmac_wapi_encrypt::hmac_wpi_pmac mic calc err!");
685         return HI_ERR_CODE_WAPI_MIC_CALC_FAIL;
686     }
687 
688     return HI_SUCCESS;
689 }
690 
hmac_wapi_encrypt_action(hmac_wapi_stru * wapi,oal_netbuf_stru * netbuf,hmac_wapi_encrypt_stru * hmac_wapi,hi_u8 * puc_datain,oal_netbuf_stru * netbuf_des)691 static hi_u32 hmac_wapi_encrypt_action(hmac_wapi_stru *wapi, oal_netbuf_stru *netbuf, hmac_wapi_encrypt_stru *hmac_wapi,
692     hi_u8 *puc_datain, oal_netbuf_stru *netbuf_des)
693 {
694     /* *********************** 4. 加密 *********************** */
695     hmac_wapi->wpi_key_ek.puc_iv = hmac_wapi->auc_pn_swap;
696     hmac_wapi->wpi_key_ek.iv_len = SMS4_PN_LEN;
697     hmac_wapi->wpi_key_ek.puc_key = wapi->ast_wapi_key[hmac_wapi->key_index].auc_wpi_ek;
698     hmac_wapi->wpi_key_ek.key_len = WAPI_KEY_LEN;
699     hi_u32 ret = hmac_wpi_encrypt(hmac_wapi->wpi_key_ek, puc_datain, hmac_wapi->pdu_len + SMS4_MIC_LEN,
700         oal_netbuf_data(netbuf_des) + HMAC_WAPI_HDR_LEN + MAC_80211_QOS_HTC_4ADDR_FRAME_LEN);
701 
702     oal_free(puc_datain);
703     if (ret != HI_SUCCESS) {
704         oal_netbuf_free(netbuf_des);
705         oam_error_log1(0, OAM_SF_ANY, "{hmac_wapi_encrypt::hmac_wpi_encrypt err==%u!", ret);
706         return HI_ERR_CODE_WAPI_ENRYPT_FAIL;
707     }
708     /* 此处put完之后,netbuff的len为mac头+pdulen+sms4+wapi的长度 */
709     oal_netbuf_put(netbuf_des, hmac_wapi->pdu_len + SMS4_MIC_LEN + HMAC_WAPI_HDR_LEN);
710 
711     /* **************** 5. 填写wapi头 **************** */
712     hi_u8 *puc_wapi_hdr = oal_netbuf_data(netbuf_des) + MAC_80211_QOS_HTC_4ADDR_FRAME_LEN;
713 
714     /* 填写WPI头 -- keyIndex */
715     *(puc_wapi_hdr) = hmac_wapi->key_index;
716     /* 保留位清零 */
717     *(puc_wapi_hdr + SMS4_KEY_IDX) = 0;
718     /* 填写PN */
719     if (memcpy_s((puc_wapi_hdr + SMS4_KEY_IDX + SMS4_WAPI_HDR_RESERVE), SMS4_PN_LEN,
720         wapi->ast_wapi_key[hmac_wapi->key_index].auc_pn_tx, SMS4_PN_LEN) != EOK) {
721         oal_netbuf_free(netbuf_des);
722         oam_error_log0(0, OAM_SF_CFG, "hmac_wapi_encrypt:: auc_pn_tx memcpy_s fail.");
723         return HI_FAIL;
724     }
725 
726     /* 再次填写cb */
727     ((hmac_tx_ctl_stru *)oal_netbuf_cb(netbuf_des))->frame_header =
728         (mac_ieee80211_frame_stru *)oal_netbuf_data(netbuf_des);
729 
730     /* 不包括mac hdr */
731     ((hmac_tx_ctl_stru *)oal_netbuf_cb(netbuf_des))->us_mpdu_len =
732         (hi_u16)(HMAC_WAPI_HDR_LEN + hmac_wapi->pdu_len + SMS4_MIC_LEN);
733 
734     set_oal_netbuf_next(netbuf_des, oal_netbuf_next(netbuf));
735     set_oal_netbuf_next(netbuf, netbuf_des);
736     /* 更新pn */
737     hmac_wapi_pn_update(wapi->ast_wapi_key[wapi->keyidx].auc_pn_tx, wapi->pn_inc);
738     oam_warning_log0(0, OAM_SF_ANY, "{hmac_wapi_encrypt::hmac_wpi_encrypt OK!");
739 
740     wapi_tx_encrypt_ok(wapi);
741 
742     return HI_SUCCESS;
743 }
744 
745 /* ****************************************************************************
746  功能描述  : 对数据进行加密,处理完成之后,无论是否分片,
747             第一个netbuff为处理前,没有加密的netbuff,
748             后面挂的netbuff为加密过的netbuff,请注意!
749  修改历史      :
750   1.日    期   : 2015年5月21日
751     作    者   : HiSilicon
752     修改内容   : 创建
753 **************************************************************************** */
hmac_wapi_encrypt(hmac_wapi_stru * wapi,oal_netbuf_stru * netbuf)754 hi_u32 hmac_wapi_encrypt(hmac_wapi_stru *wapi, oal_netbuf_stru *netbuf)
755 {
756     hmac_wapi_encrypt_stru hmac_wapi = { 0 };
757     hmac_wapi.key_index = wapi->keyidx;
758 
759     /* *********** 1. 加密前的数据准备,获取各头指针和内容长度 *********** */
760     /* 获取mac头 */
761     mac_ieee80211_frame_stru *mac_hdr = ((hmac_tx_ctl_stru *)oal_netbuf_cb(netbuf))->frame_header;
762     hmac_wapi.mac_hdr_len = ((hmac_tx_ctl_stru *)oal_netbuf_cb(netbuf))->frame_header_length;
763 
764     /* 设置加密位 注意,mac头涉及加密,所以需要在最开始设置 */
765     mac_set_protectedframe((hi_u8 *)mac_hdr);
766 
767     oam_warning_log2(0, OAM_SF_ANY, "{hmac_wapi_encrypt:len %u!fra %u}", hmac_wapi.mac_hdr_len,
768         mac_hdr->frame_control.more_frag);
769     hmac_wapi.pdu_len = (hi_u16)(((hmac_tx_ctl_stru *)oal_netbuf_cb(netbuf))->us_mpdu_len);
770     hi_u8 *puc_payload = oal_netbuf_data(netbuf) + MAC_80211_QOS_HTC_4ADDR_FRAME_LEN;
771 
772     /* *********** 2. 计算mic,wapi的数据帧一般为QOS帧  *********** */
773     hi_u32 ret = hmac_wapi_encrypt_mic(wapi, &hmac_wapi, mac_hdr, puc_payload);
774     if (ret != HI_SUCCESS) {
775         return ret;
776     }
777 
778     /* *********** 3. 准备新的netbuff,用来存放加密后的数据, 填写cb,并准备加密前的数据 *********** */
779     oal_netbuf_stru *netbuf_des = oal_netbuf_alloc(WLAN_LARGE_NETBUF_SIZE, 0, 4); /* align 4 */
780     if (netbuf_des == HI_NULL) {
781         wapi_tx_memalloc_err(wapi);
782         oam_error_log0(0, OAM_SF_ANY, "{hmac_wapi_encrypt::pst_netbuff_des alloc err!");
783         return HI_ERR_CODE_ALLOC_MEM_FAIL;
784     }
785 
786     /* 填写cb */
787     if (memcpy_s(oal_netbuf_cb(netbuf_des), oal_netbuf_cb_size(), oal_netbuf_cb(netbuf), oal_netbuf_cb_size()) != EOK) {
788         oal_netbuf_free(netbuf_des);
789         oam_error_log0(0, OAM_SF_CFG, "hmac_wapi_encrypt:: pst_netbuf memcpy_s fail.");
790         return HI_FAIL;
791     }
792 
793     /* 先拷贝mac头,为了后续hcc处理,此处填写最大的空间 */
794     oal_netbuf_init(netbuf_des, MAC_80211_QOS_HTC_4ADDR_FRAME_LEN);
795     if (memcpy_s(oal_netbuf_data(netbuf_des), hmac_wapi.mac_hdr_len, mac_hdr, hmac_wapi.mac_hdr_len) != EOK) {
796         oal_netbuf_free(netbuf_des);
797         oam_error_log0(0, OAM_SF_CFG, "hmac_wapi_encrypt:: pst_mac_hdr memcpy_s fail.");
798         return HI_FAIL;
799     }
800 
801     hi_u8 *puc_datain = (hi_u8 *)oal_memalloc(hmac_wapi.pdu_len + SMS4_MIC_LEN);
802     if (puc_datain == HI_NULL) {
803         oal_netbuf_free(netbuf_des);
804         oam_error_log0(0, OAM_SF_ANY, "{hmac_wapi_encrypt::puc_datain alloc err!");
805         wapi_tx_memalloc_err(wapi);
806         return HI_ERR_CODE_ALLOC_MEM_FAIL;
807     }
808     if ((memcpy_s(puc_datain, hmac_wapi.pdu_len, puc_payload, hmac_wapi.pdu_len) != EOK) ||
809         (memcpy_s(puc_datain + hmac_wapi.pdu_len, SMS4_MIC_LEN, hmac_wapi.auc_calc_mic, SMS4_MIC_LEN) != EOK)) {
810         oal_free(puc_datain);
811         oal_netbuf_free(netbuf_des);
812         oam_error_log0(0, OAM_SF_CFG, "hmac_wapi_encrypt:: puc_payload memcpy_s fail.");
813         return HI_FAIL;
814     }
815 
816     /* *********************** 4. 加密 *********************** */
817     /* ********************* 5. 填写wapi头 ******************* */
818     return hmac_wapi_encrypt_action(wapi, netbuf, &hmac_wapi, puc_datain, netbuf_des);
819 }
820 
821 /* ****************************************************************************
822  功能描述  : 去初始化wapi对象
823  修改历史      :
824   1.日    期   : 2015年5月26日
825     作    者   : HiSilicon
826     修改内容   : 新生成函数
827 **************************************************************************** */
hmac_wapi_deinit(hmac_wapi_stru * wapi)828 hi_u32 hmac_wapi_deinit(hmac_wapi_stru *wapi)
829 {
830     if (memset_s(wapi, sizeof(hmac_wapi_stru), 0, sizeof(hmac_wapi_stru)) != EOK) {
831         return HI_FAIL;
832     }
833 
834     return HI_SUCCESS;
835 }
836 
837 /* ****************************************************************************
838  功能描述  : 初始化wapi对象
839  修改历史      :
840   1.日    期   : 2015年5月26日
841     作    者   : HiSilicon
842     修改内容   : 新生成函数
843 **************************************************************************** */
hmac_wapi_init(hmac_wapi_stru * wapi,hi_u8 pairwise)844 hi_u32 hmac_wapi_init(hmac_wapi_stru *wapi, hi_u8 pairwise)
845 {
846     hi_u32 loop, ret;
847 
848     ret = hmac_wapi_deinit(wapi);
849     if (ret != HI_SUCCESS) {
850         oam_warning_log0(0, OAM_SF_UM, "hmac_wapi_deinit return NON SUCCESS. ");
851     }
852 
853     if (pairwise == HI_TRUE) {
854         wapi->pn_inc = WAPI_UCAST_INC;
855         wapi->wapi_is_pn_odd = hmac_wapi_is_pn_odd_ucast;
856     } else {
857         wapi->pn_inc = WAPI_BCAST_INC;
858         wapi->wapi_is_pn_odd = hmac_wapi_is_pn_odd_bcast;
859     }
860 
861     for (loop = 0; loop < HMAC_WAPI_MAX_KEYID; loop++) {
862         wapi->ast_wapi_key[loop].key_en = HI_FALSE;
863     }
864 
865     wapi->port_valid = HI_FALSE;
866     wapi->wapi_decrypt = hmac_wapi_decrypt;
867     wapi->wapi_encrypt = hmac_wapi_encrypt;
868     wapi->wapi_netbuff_txhandle = hmac_wapi_netbuff_tx_handle;
869     wapi->wapi_netbuff_rxhandle = hmac_wapi_netbuff_rx_handle;
870     return HI_SUCCESS;
871 }
872 
873 #ifdef _PRE_WAPI_DEBUG
874 /* ****************************************************************************
875  功能描述  : 打印帧内容
876  修改历史      :
877   1.日    期   : 2015年5月21日
878     作    者   : HiSilicon
879     修改内容   : 创建
880 **************************************************************************** */
hmac_wapi_dump_frame(hi_u8 * puc_info,hi_u8 * puc_data,hi_u32 len)881 hi_void hmac_wapi_dump_frame(hi_u8 *puc_info, hi_u8 *puc_data, hi_u32 len)
882 {
883     hi_u32 loop;
884     for (loop = 0; loop < len; loop += 4) { /* 循环步长为4 */
885         oal_io_print("%2x ", loop / 4);     /* 循环步长为4 */
886         oal_io_print("%2x %2x %2x %2x \r\n", puc_data[loop], puc_data[loop + 1], puc_data[loop + 2],
887             puc_data[loop + 3]); /* 2 3 数组位置增量 */
888     }
889 }
890 
891 /* ****************************************************************************
892  功能描述  : 打印用户wapi内容
893  修改历史      :
894   1.日    期   : 2015年5月21日
895     作    者   : HiSilicon
896     修改内容   : 创建
897 **************************************************************************** */
hmac_wapi_display_usr_info(hmac_user_stru * hmac_user)898 hi_u32 hmac_wapi_display_usr_info(hmac_user_stru *hmac_user)
899 {
900     hi_u32 loop = 0;
901     hmac_wapi_stru *wapi = HI_NULL;
902     hmac_wapi_key_stru *key = HI_NULL;
903     hmac_wapi_debug *debug = HI_NULL;
904 
905     oam_warning_log1(0, OAM_SF_ANY, "wapi port is %u!", wapi_is_port_valid(&hmac_user->wapi));
906     if (wapi_is_port_valid(&hmac_user->wapi != HI_TRUE)) {
907         oal_io_print("Err! wapi port is not valid!\n");
908 
909         return HI_FAILURE;
910     }
911 
912     wapi = &(hmac_user->wapi);
913     oam_warning_log0(0, OAM_SF_ANY, "keyidx\tupdate\t\tpn_inc\t\n");
914     oam_warning_log3(0, OAM_SF_ANY, "%u\t%08x%04x\t\n", wapi->keyidx, wapi->keyupdate_flg, wapi->pn_inc);
915 
916     for (loop = 0; loop < HMAC_WAPI_MAX_KEYID; loop++) {
917         key = &wapi->ast_wapi_key[loop];
918         hmac_wapi_dump_frame("ek :", key->auc_wpi_ek, WAPI_KEY_LEN);
919         hmac_wapi_dump_frame("ck :", key->auc_wpi_ck, WAPI_KEY_LEN);
920         hmac_wapi_dump_frame("pn_local_rx :", key->auc_pn_rx, WAPI_PN_LEN);
921         hmac_wapi_dump_frame("pn_local_tx :", key->auc_pn_tx, WAPI_PN_LEN);
922         oam_warning_log1(0, OAM_SF_ANY, "key_en: %u\n", key->key_en);
923     }
924 
925     debug = &wapi->debug;
926     oam_warning_log0(0, OAM_SF_ANY, "TX DEBUG INFO:");
927     hmac_wapi_dump_frame("pn_rx :", debug->aucrx_pn, WAPI_PN_LEN);
928     oam_warning_log4(0, OAM_SF_ANY, "tx_drop==%u, tx_wai==%u, tx_port_valid==%u, tx_memalloc_fail==%u",
929         debug->ultx_ucast_drop, debug->ultx_wai, debug->ultx_port_valid, debug->ultx_memalloc_err);
930     oam_warning_log3(0, OAM_SF_ANY, "tx_mic_calc_fail==%u, tx_encrypt_ok==%u, tx_memalloc_err==%u",
931         debug->ultx_mic_calc_fail, debug->ultx_encrypt_ok, debug->ultx_memalloc_err);
932 
933     oam_warning_log0(0, OAM_SF_ANY, "RX DEBUG INFO:");
934     oam_warning_log4(0, OAM_SF_ANY, "rx_port_valid==%u, rx_idx_err==%u, rx_netbuff_len_err==%u, rx_idx_update_err==%u",
935         debug->ulrx_port_valid, debug->ulrx_idx_err, debug->ulrx_netbuff_len_err, debug->ulrx_idx_update_err);
936 
937     oam_warning_log4(0, OAM_SF_ANY, "rx_key_en_err==%u, rx_pn_odd_err==%u, rx_pn_replay_err==%u, rx_decrypt_ok==%u",
938         debug->ulrx_key_en_err, debug->ulrx_pn_odd_err, debug->ulrx_pn_replay_err, debug->ulrx_decrypt_ok);
939     return HI_SUCCESS;
940 }
941 
942 /* ****************************************************************************
943  功能描述  : 打印wapi内容
944  修改历史      :
945   1.日    期   : 2015年5月21日
946     作    者   : HiSilicon
947     修改内容   : 创建
948 **************************************************************************** */
hmac_wapi_display_info(mac_vap_stru * mac_vap,hi_u16 us_usr_idx)949 hi_u32 hmac_wapi_display_info(mac_vap_stru *mac_vap, hi_u16 us_usr_idx)
950 {
951     hmac_user_stru *hmac_user = HI_NULL;
952     hmac_user_stru *hmac_multi_user = HI_NULL;
953     hi_u32 ret;
954 
955     hmac_multi_user = (hmac_user_stru *)hmac_user_get_user_stru(mac_vap->multi_user_idx);
956     if (hmac_multi_user == HI_NULL) {
957         oam_warning_log1(mac_vap->vap_id, OAM_SF_ANY, "Err! multi usr %u does not exist!", mac_vap->multi_user_idx);
958         return HI_ERR_CODE_PTR_NULL;
959     }
960 
961     oam_warning_log0(mac_vap->vap_id, OAM_SF_ANY, "*****************multi usr info start****************");
962     ret = hmac_wapi_display_usr_info(hmac_multi_user);
963     if (ret != HI_SUCCESS) {
964         oam_warning_log0(mac_vap->vap_id, OAM_SF_ANY, "hmac_wapi_display_usr_info return NON SUCCESS. ");
965     }
966 
967     oam_warning_log0(mac_vap->vap_id, OAM_SF_ANY, "*****************multi usr info end****************");
968 
969     hmac_user = (hmac_user_stru *)hmac_user_get_user_stru(us_usr_idx);
970     if (hmac_user == HI_NULL) {
971         oam_warning_log1(mac_vap->vap_id, OAM_SF_ANY, "Err! ucast usr %u does not exist!", us_usr_idx);
972         return HI_ERR_CODE_PTR_NULL;
973     }
974 
975     oam_warning_log0(mac_vap->vap_id, OAM_SF_ANY, "*****************ucast usr info start****************");
976     ret = hmac_wapi_display_usr_info(hmac_user);
977     if (ret != HI_SUCCESS) {
978         oam_warning_log0(mac_vap->vap_id, OAM_SF_ANY, "hmac_wapi_display_usr_info return NON SUCCESS. ");
979     }
980     oam_warning_log0(mac_vap->vap_id, OAM_SF_ANY, "*****************ucast usr info end****************");
981 
982     return HI_SUCCESS;
983 }
984 #endif
985 
986 #ifdef __cplusplus
987 #if __cplusplus
988 }
989 #endif
990 #endif
991