• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  * Description: hook function for wifi sniffer.
15  */
16 
17 #ifndef __HMAC_SNIFFER_H
18 #define __HMAC_SNIFFER_H
19 
20 #if defined(_PRE_OS_VERSION_LINUX) && defined(_PRE_OS_VERSION) \
21     && (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
22 #include "oal_schedule.h"
23 #include "mac_frame.h"
24 
25 #ifdef __cplusplus
26 #if __cplusplus
27 extern "C" {
28 #endif
29 #endif
30 
31 /*****************************************************************************
32   1 其他头文件包含
33 *****************************************************************************/
34 #if defined(_PRE_WLAN_FEATURE_SNIFFER)
35 
36 #define PCAP_FILE_NUM 5
37 #define PCAP_FILE_NUM_MAX 5
38 #define PCAP_FILE_NUM_MIN 1
39 #define PCAP_FILE_LEN (30 * 1024 * 1024)
40 #define PCAP_FILE_LEN_MIN (1 * 1024 * 1024)
41 #define PCAP_FILE_LEN_MAX (50 * 1024 * 1024)
42 
43 #define PCAP_PKT_MIN_LEN 24
44 #define PCAP_PKT_MAX_LEN 5000
45 
46 #define PCAP_PKT_HDR_MAGIC 0xa1b2c3d4
47 #define PCAP_PKT_HDR_VER_MAJ 0x2
48 #define PCAP_PKT_HDR_VER_MIN 0x4
49 #define PCAP_PKT_HDR_THISZONE 0x0
50 #define PCAP_PKT_HDR_SIGFIGS 0x0
51 #define PCAP_PKT_HDR_SNAPLEN 0xffff
52 #define PCAP_PKT_HDR_LINKTYPE_802_11 0x69
53 #define PCAP_PKT_HDR_LINKTYPE_802_11_RADIOTAP 127
54 
55 #define IEEE80211_MAC_HDR_LEN 26
56 #define TRIM_DATA_PAYLOAD_LEN 2000
57 #define IEEE80211_FRAME_TYPE_DATA 0x02
58 #define IEEE80211_FRAME_TYPE_MGMT 0x00
59 #define IEEE80211_FRAME_SUBTYPE_QOS 0x08
60 #define IEEE80211_FRAME_SUBTYPE_DISASSOC 0x0a
61 
62 #define MAC_ADDR_LEN 6
63 
64 #define DIRECTION_TX 0
65 #define DIRECTION_RX 1
66 
67 #define PCAP_QOS_DATA_PKT_HDR_LEN 26
68 
69 /* frame control structure */
70 typedef struct {
71     osal_u16  bit_protocol_version    : 2,    /* protocol version */
72                 bit_type                : 2,    /* frame type */
73                 bit_sub_type            : 4,    /* subtype */
74                 bit_to_ds               : 1,    /* to DS */
75                 bit_from_ds             : 1,    /* from DS */
76                 bit_more_frag           : 1,    /* more fragment */
77                 bit_retry               : 1,    /* retransmit frame */
78                 bit_power_mgmt          : 1,    /* power management */
79                 bit_more_data           : 1,    /* more data flag */
80                 bit_protected_frame     : 1,    /* encrypt flag */
81                 bit_order               : 1;    /* order bit */
82 } mac_header_control_stru;
83 
84 /* qos frame 2+2+ 6*3 + 2 +1 +1 = 26 */
85 typedef struct __attribute__ ((__packed__)) {
86     mac_header_control_stru st_frame_control;
87     osal_u16              bit_duration_value      : 15, /* duration/id */
88                             bit_duration_flag       : 1;
89     osal_u8               auc_address1[MAC_ADDR_LEN];
90     osal_u8               auc_address2[MAC_ADDR_LEN];
91     osal_u8               auc_address3[MAC_ADDR_LEN];
92     osal_u16              bit_frag_num            : 4, /* sequence control */
93                             bit_seq_num             : 12;
94     osal_u8               bit_qc_tid              : 4,
95                             bit_qc_eosp             : 1,
96                             bit_qc_ack_polocy       : 2,
97                             bit_qc_amsdu            : 1;
98     union {
99         osal_u8           bit_qc_txop_limit;
100         osal_u8           bit_qc_ps_buf_state_resv        : 1,
101                             bit_qc_ps_buf_state_inducated   : 1,
102                             bit_qc_high_priority_buf_ac     : 2,
103                             bit_qc_qosap_buf_load           : 4;
104     } qos_control;
105 } mac_ieee80211_qos_stru;
106 
107 typedef struct {
108     osal_s32  sniffer_switch;
109     osal_s32  cur_file;
110     osal_s32  data_packet_max_len;
111     osal_s32  max_file_num;
112     osal_s32  max_file_len;
113     osal_s32  new_file_num;
114     osal_s32  new_file_len;
115     osal_s32  file_full[PCAP_FILE_NUM];
116     osal_s8  *buffer[PCAP_FILE_NUM];
117     osal_s32  curpos[PCAP_FILE_NUM];
118     osal_u32 gmttime[PCAP_FILE_NUM];        /* ms, between now and 1970-01-01 00:00:00 */
119     osal_u32 microtime[PCAP_FILE_NUM];      /* us, between now and 1970-01-01 00:00:00 */
120     osal_u32 packetcount[PCAP_FILE_NUM];
121     // osal_u32 promis_count[3][15];           /* 3 帧的类型,15 子帧的类型 */
122     osal_semaphore main_sem;
123 } proc_file_config_struct;
124 
125 extern proc_file_config_struct g_sniffer_st;
126 
127 #pragma pack(1)
128 typedef struct {
129     osal_u32 magic;
130     osal_u16 version_major;
131     osal_u16 version_minor;
132     osal_u32 thiszone;
133     osal_u32 sigfigs;
134     osal_u32 snaplen;
135     osal_u32 linktype;
136 } wal_pcap_filehdr_stru;
137 #pragma pack()
138 
139 /* 16 Bytes PCAP frame header's timestamp */
140 typedef struct {
141     osal_u32 gmt_time; /* ms, between now and 1970-01-01 00:00:00 */
142     osal_u32 micro_time; /* us, between now and 1970-01-01 00:00:00 */
143 } wal_pcap_time_stru;
144 
145 /* PCAP frame header (16 Bytes) */
146 #pragma pack(1)
147 typedef struct {
148     wal_pcap_time_stru time;    /* PCAP Frame Header's timestamp */
149     osal_u32         caplen;  /* length of (Frame Header + Frame Body) */
150     osal_u32         len;     /* packet's actual length, len == caplen in common */
151 } wal_pcap_pkthdr_stru;
152 #pragma pack()
153 
154 /* 消息头 */
155 typedef struct {
156     osal_u8 frame_start;
157     osal_u8 func_type;
158     osal_u8 prime_id;
159     osal_u8 reserver[1];
160     osal_u16 frame_len;
161     osal_u16 sn;
162 } wal_msg_header_stru;
163 
164 /* WIFI OTA头部信息 */
165 typedef struct {
166     osal_u32                              tick;            /* 时间戳,精度ms */
167     osal_u8                               en_ota_type;        /* OTA类型 */
168     osal_u8                               frame_hdr_len;   /* 如果是帧,代表帧头长度;如果是其它类型,此字段没有用 */
169     osal_u16                              us_ota_data_len;    /* OTA数据长度,如果是帧,代表帧头和帧体的总长度 */
170     osal_u8                               user_macaddr[MAC_ADDR_LEN];
171     oam_ota_frame_direction_type_enum_uint8 en_frame_direction; /* 表明帧是属于发送流程还是接收流程,只有ota_type是帧的时候这个字段才有意义 */
172     osal_u8                               resv[1];        /* 区分产品 */
173 } wal_ota_hdr_stru;
174 
175 typedef struct {
176     wal_msg_header_stru oam_header;
177     wal_ota_hdr_stru st_ota_hdr;
178 } wal_ota_stru;
179 
180 
sniffer_enough_to_save(osal_u32 packet_len)181 static INLINE__ osal_bool sniffer_enough_to_save(osal_u32 packet_len)
182 {
183     return (g_sniffer_st.curpos[g_sniffer_st.cur_file] + (packet_len)) <= g_sniffer_st.max_file_len;
184 }
185 
sniffer_file_pos(osal_s32 i)186 static INLINE__ osal_s32 sniffer_file_pos(osal_s32 i)
187 {
188     return g_sniffer_st.curpos[i];
189 }
190 
sniffer_cur_file_pos(osal_void)191 static INLINE__ osal_s32 sniffer_cur_file_pos(osal_void)
192 {
193     return sniffer_file_pos(g_sniffer_st.cur_file);
194 }
195 
sniffer_is_cur_file_empty(osal_void)196 static INLINE__ osal_bool sniffer_is_cur_file_empty(osal_void)
197 {
198     return sniffer_cur_file_pos() == 0;
199 }
200 
sniffer_file_buffer(osal_s32 i)201 static INLINE__ osal_s8 *sniffer_file_buffer(osal_s32 i)
202 {
203     return g_sniffer_st.buffer[i];
204 }
205 
sniffer_cur_file_buffer(osal_void)206 static INLINE__ osal_s8 *sniffer_cur_file_buffer(osal_void)
207 {
208     return sniffer_file_buffer(g_sniffer_st.cur_file);
209 }
210 
211 osal_s32 proc_handle_command_sniffer_enable(osal_s32 sniffer_mode);
212 osal_s32 hmac_sniffer_get_switch(void);
213 void proc_handle_command_save_file(void);
214 osal_s32 proc_sniffer_reset_file_num_len(osal_s32 new_file_num, osal_s32 new_file_len);
215 osal_void hmac_sniffer_80211_frame(const osal_u8 *hdr_addr, osal_u8 hdr_len,
216     const osal_u8 *frame_addr, osal_u16 frame_len, osal_u32 msg_id);
217 
218 #ifdef __cplusplus
219 #if __cplusplus
220     }
221 #endif
222 #endif
223 
224 #endif
225 #endif
226 
227 #endif
228