GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / staging / wilc1000 / coreconfigurator.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include "coreconfigurator.h"
3 #include "wilc_wlan_if.h"
4 #include "wilc_wlan.h"
5 #include <linux/errno.h>
6 #include <linux/slab.h>
7 #define TAG_PARAM_OFFSET        (MAC_HDR_LEN + TIME_STAMP_LEN + \
8                                  BEACON_INTERVAL_LEN + CAP_INFO_LEN)
9
10 enum basic_frame_type {
11         FRAME_TYPE_CONTROL     = 0x04,
12         FRAME_TYPE_DATA        = 0x08,
13         FRAME_TYPE_MANAGEMENT  = 0x00,
14         FRAME_TYPE_RESERVED    = 0x0C,
15         FRAME_TYPE_FORCE_32BIT = 0xFFFFFFFF
16 };
17
18 enum sub_frame_type {
19         ASSOC_REQ             = 0x00,
20         ASSOC_RSP             = 0x10,
21         REASSOC_REQ           = 0x20,
22         REASSOC_RSP           = 0x30,
23         PROBE_REQ             = 0x40,
24         PROBE_RSP             = 0x50,
25         BEACON                = 0x80,
26         ATIM                  = 0x90,
27         DISASOC               = 0xA0,
28         AUTH                  = 0xB0,
29         DEAUTH                = 0xC0,
30         ACTION                = 0xD0,
31         PS_POLL               = 0xA4,
32         RTS                   = 0xB4,
33         CTS                   = 0xC4,
34         ACK                   = 0xD4,
35         CFEND                 = 0xE4,
36         CFEND_ACK             = 0xF4,
37         DATA                  = 0x08,
38         DATA_ACK              = 0x18,
39         DATA_POLL             = 0x28,
40         DATA_POLL_ACK         = 0x38,
41         NULL_FRAME            = 0x48,
42         CFACK                 = 0x58,
43         CFPOLL                = 0x68,
44         CFPOLL_ACK            = 0x78,
45         QOS_DATA              = 0x88,
46         QOS_DATA_ACK          = 0x98,
47         QOS_DATA_POLL         = 0xA8,
48         QOS_DATA_POLL_ACK     = 0xB8,
49         QOS_NULL_FRAME        = 0xC8,
50         QOS_CFPOLL            = 0xE8,
51         QOS_CFPOLL_ACK        = 0xF8,
52         BLOCKACK_REQ          = 0x84,
53         BLOCKACK              = 0x94,
54         FRAME_SUBTYPE_FORCE_32BIT  = 0xFFFFFFFF
55 };
56
57 enum info_element_id {
58         ISSID               = 0,   /* Service Set Identifier         */
59         ISUPRATES           = 1,   /* Supported Rates                */
60         IFHPARMS            = 2,   /* FH parameter set               */
61         IDSPARMS            = 3,   /* DS parameter set               */
62         ICFPARMS            = 4,   /* CF parameter set               */
63         ITIM                = 5,   /* Traffic Information Map        */
64         IIBPARMS            = 6,   /* IBSS parameter set             */
65         ICOUNTRY            = 7,   /* Country element                */
66         IEDCAPARAMS         = 12,  /* EDCA parameter set             */
67         ITSPEC              = 13,  /* Traffic Specification          */
68         ITCLAS              = 14,  /* Traffic Classification         */
69         ISCHED              = 15,  /* Schedule                       */
70         ICTEXT              = 16,  /* Challenge Text                 */
71         IPOWERCONSTRAINT    = 32,  /* Power Constraint               */
72         IPOWERCAPABILITY    = 33,  /* Power Capability               */
73         ITPCREQUEST         = 34,  /* TPC Request                    */
74         ITPCREPORT          = 35,  /* TPC Report                     */
75         ISUPCHANNEL         = 36,  /* Supported channel list         */
76         ICHSWANNOUNC        = 37,  /* Channel Switch Announcement    */
77         IMEASUREMENTREQUEST = 38,  /* Measurement request            */
78         IMEASUREMENTREPORT  = 39,  /* Measurement report             */
79         IQUIET              = 40,  /* Quiet element Info             */
80         IIBSSDFS            = 41,  /* IBSS DFS                       */
81         IERPINFO            = 42,  /* ERP Information                */
82         ITSDELAY            = 43,  /* TS Delay                       */
83         ITCLASPROCESS       = 44,  /* TCLAS Processing               */
84         IHTCAP              = 45,  /* HT Capabilities                */
85         IQOSCAP             = 46,  /* QoS Capability                 */
86         IRSNELEMENT         = 48,  /* RSN Information Element        */
87         IEXSUPRATES         = 50,  /* Extended Supported Rates       */
88         IEXCHSWANNOUNC      = 60,  /* Extended Ch Switch Announcement*/
89         IHTOPERATION        = 61,  /* HT Information                 */
90         ISECCHOFF           = 62,  /* Secondary Channel Offeset      */
91         I2040COEX           = 72,  /* 20/40 Coexistence IE           */
92         I2040INTOLCHREPORT  = 73,  /* 20/40 Intolerant channel report*/
93         IOBSSSCAN           = 74,  /* OBSS Scan parameters           */
94         IEXTCAP             = 127, /* Extended capability            */
95         IWMM                = 221, /* WMM parameters                 */
96         IWPAELEMENT         = 221, /* WPA Information Element        */
97         INFOELEM_ID_FORCE_32BIT  = 0xFFFFFFFF
98 };
99
100 static inline u16 get_beacon_period(u8 *data)
101 {
102         u16 bcn_per;
103
104         bcn_per  = data[0];
105         bcn_per |= (data[1] << 8);
106
107         return bcn_per;
108 }
109
110 static inline u32 get_beacon_timestamp_lo(u8 *data)
111 {
112         u32 time_stamp = 0;
113         u32 index    = MAC_HDR_LEN;
114
115         time_stamp |= data[index++];
116         time_stamp |= (data[index++] << 8);
117         time_stamp |= (data[index++] << 16);
118         time_stamp |= (data[index]   << 24);
119
120         return time_stamp;
121 }
122
123 static inline u32 get_beacon_timestamp_hi(u8 *data)
124 {
125         u32 time_stamp = 0;
126         u32 index    = (MAC_HDR_LEN + 4);
127
128         time_stamp |= data[index++];
129         time_stamp |= (data[index++] << 8);
130         time_stamp |= (data[index++] << 16);
131         time_stamp |= (data[index]   << 24);
132
133         return time_stamp;
134 }
135
136 static inline enum sub_frame_type get_sub_type(u8 *header)
137 {
138         return ((enum sub_frame_type)(header[0] & 0xFC));
139 }
140
141 static inline u8 get_to_ds(u8 *header)
142 {
143         return (header[1] & 0x01);
144 }
145
146 static inline u8 get_from_ds(u8 *header)
147 {
148         return ((header[1] & 0x02) >> 1);
149 }
150
151 static inline void get_address1(u8 *pu8msa, u8 *addr)
152 {
153         memcpy(addr, pu8msa + 4, 6);
154 }
155
156 static inline void get_address2(u8 *pu8msa, u8 *addr)
157 {
158         memcpy(addr, pu8msa + 10, 6);
159 }
160
161 static inline void get_address3(u8 *pu8msa, u8 *addr)
162 {
163         memcpy(addr, pu8msa + 16, 6);
164 }
165
166 static inline void get_BSSID(u8 *data, u8 *bssid)
167 {
168         if (get_from_ds(data) == 1)
169                 get_address2(data, bssid);
170         else if (get_to_ds(data) == 1)
171                 get_address1(data, bssid);
172         else
173                 get_address3(data, bssid);
174 }
175
176 static inline void get_ssid(u8 *data, u8 *ssid, u8 *p_ssid_len)
177 {
178         u8 len = 0;
179         u8 i   = 0;
180         u8 j   = 0;
181
182         len = data[TAG_PARAM_OFFSET + 1];
183         j   = TAG_PARAM_OFFSET + 2;
184
185         if (len >= MAX_SSID_LEN)
186                 len = 0;
187
188         for (i = 0; i < len; i++, j++)
189                 ssid[i] = data[j];
190
191         ssid[len] = '\0';
192
193         *p_ssid_len = len;
194 }
195
196 static inline u16 get_cap_info(u8 *data)
197 {
198         u16 cap_info = 0;
199         u16 index    = MAC_HDR_LEN;
200         enum sub_frame_type st;
201
202         st = get_sub_type(data);
203
204         if ((st == BEACON) || (st == PROBE_RSP))
205                 index += TIME_STAMP_LEN + BEACON_INTERVAL_LEN;
206
207         cap_info  = data[index];
208         cap_info |= (data[index + 1] << 8);
209
210         return cap_info;
211 }
212
213 static inline u16 get_assoc_resp_cap_info(u8 *data)
214 {
215         u16 cap_info;
216
217         cap_info  = data[0];
218         cap_info |= (data[1] << 8);
219
220         return cap_info;
221 }
222
223 static inline u16 get_asoc_status(u8 *data)
224 {
225         u16 asoc_status;
226
227         asoc_status = data[3];
228         return (asoc_status << 8) | data[2];
229 }
230
231 static inline u16 get_asoc_id(u8 *data)
232 {
233         u16 asoc_id;
234
235         asoc_id  = data[4];
236         asoc_id |= (data[5] << 8);
237
238         return asoc_id;
239 }
240
241 static u8 *get_tim_elm(u8 *pu8msa, u16 rx_len, u16 tag_param_offset)
242 {
243         u16 index;
244
245         index = tag_param_offset;
246
247         while (index < (rx_len - FCS_LEN)) {
248                 if (pu8msa[index] == ITIM)
249                         return &pu8msa[index];
250                 index += (IE_HDR_LEN + pu8msa[index + 1]);
251         }
252
253         return NULL;
254 }
255
256 static u8 get_current_channel_802_11n(u8 *pu8msa, u16 rx_len)
257 {
258         u16 index;
259
260         index = TAG_PARAM_OFFSET;
261         while (index < (rx_len - FCS_LEN)) {
262                 if (pu8msa[index] == IDSPARMS)
263                         return pu8msa[index + 2];
264                 index += pu8msa[index + 1] + IE_HDR_LEN;
265         }
266
267         return 0;
268 }
269
270 s32 wilc_parse_network_info(u8 *msg_buffer,
271                             struct network_info **ret_network_info)
272 {
273         struct network_info *network_info = NULL;
274         u8 msg_type = 0;
275         u8 msg_id = 0;
276         u16 msg_len = 0;
277
278         u16 wid_id = (u16)WID_NIL;
279         u16 wid_len  = 0;
280         u8 *wid_val = NULL;
281
282         msg_type = msg_buffer[0];
283
284         if ('N' != msg_type)
285                 return -EFAULT;
286
287         msg_id = msg_buffer[1];
288         msg_len = MAKE_WORD16(msg_buffer[2], msg_buffer[3]);
289         wid_id = MAKE_WORD16(msg_buffer[4], msg_buffer[5]);
290         wid_len = MAKE_WORD16(msg_buffer[6], msg_buffer[7]);
291         wid_val = &msg_buffer[8];
292
293         {
294                 u8 *msa = NULL;
295                 u16 rx_len = 0;
296                 u8 *tim_elm = NULL;
297                 u8 *ies = NULL;
298                 u16 ies_len = 0;
299                 u8 index = 0;
300                 u32 tsf_lo;
301                 u32 tsf_hi;
302
303                 network_info = kzalloc(sizeof(*network_info), GFP_KERNEL);
304                 if (!network_info)
305                         return -ENOMEM;
306
307                 network_info->rssi = wid_val[0];
308
309                 msa = &wid_val[1];
310
311                 rx_len = wid_len - 1;
312                 network_info->cap_info = get_cap_info(msa);
313                 network_info->tsf_lo = get_beacon_timestamp_lo(msa);
314
315                 tsf_lo = get_beacon_timestamp_lo(msa);
316                 tsf_hi = get_beacon_timestamp_hi(msa);
317
318                 network_info->tsf_hi = tsf_lo | ((u64)tsf_hi << 32);
319
320                 get_ssid(msa, network_info->ssid, &network_info->ssid_len);
321                 get_BSSID(msa, network_info->bssid);
322
323                 network_info->ch = get_current_channel_802_11n(msa,
324                                                         rx_len + FCS_LEN);
325
326                 index = MAC_HDR_LEN + TIME_STAMP_LEN;
327
328                 network_info->beacon_period = get_beacon_period(msa + index);
329
330                 index += BEACON_INTERVAL_LEN + CAP_INFO_LEN;
331
332                 tim_elm = get_tim_elm(msa, rx_len + FCS_LEN, index);
333                 if (tim_elm)
334                         network_info->dtim_period = tim_elm[3];
335                 ies = &msa[TAG_PARAM_OFFSET];
336                 ies_len = rx_len - TAG_PARAM_OFFSET;
337
338                 if (ies_len > 0) {
339                         network_info->ies = kmemdup(ies, ies_len, GFP_KERNEL);
340                         if (!network_info->ies) {
341                                 kfree(network_info);
342                                 return -ENOMEM;
343                         }
344                 }
345                 network_info->ies_len = ies_len;
346         }
347
348         *ret_network_info = network_info;
349
350         return 0;
351 }
352
353 s32 wilc_parse_assoc_resp_info(u8 *buffer, u32 buffer_len,
354                                struct connect_resp_info **ret_connect_resp_info)
355 {
356         struct connect_resp_info *connect_resp_info = NULL;
357         u16 assoc_resp_len = 0;
358         u8 *ies = NULL;
359         u16 ies_len = 0;
360
361         connect_resp_info = kzalloc(sizeof(*connect_resp_info), GFP_KERNEL);
362         if (!connect_resp_info)
363                 return -ENOMEM;
364
365         assoc_resp_len = (u16)buffer_len;
366
367         connect_resp_info->status = get_asoc_status(buffer);
368         if (connect_resp_info->status == SUCCESSFUL_STATUSCODE) {
369                 connect_resp_info->capability = get_assoc_resp_cap_info(buffer);
370                 connect_resp_info->assoc_id = get_asoc_id(buffer);
371
372                 ies = &buffer[CAP_INFO_LEN + STATUS_CODE_LEN + AID_LEN];
373                 ies_len = assoc_resp_len - (CAP_INFO_LEN + STATUS_CODE_LEN +
374                                             AID_LEN);
375
376                 connect_resp_info->ies = kmemdup(ies, ies_len, GFP_KERNEL);
377                 if (!connect_resp_info->ies) {
378                         kfree(connect_resp_info);
379                         return -ENOMEM;
380                 }
381
382                 connect_resp_info->ies_len = ies_len;
383         }
384
385         *ret_connect_resp_info = connect_resp_info;
386
387         return 0;
388 }