lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// SPDX-License-Identifier: GPL-2.0
 
/******************************************************************************
 *
 * Copyright (C) 2020 SeekWave Technology Co.,Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 ******************************************************************************/
 
#include <linux/kernel.h>
#include <linux/ieee80211.h>
#include <net/cfg80211.h>
 
#include "skw_core.h"
#include "skw_log.h"
#include "skw_mbssid.h"
#include "skw_cfg80211.h"
#include "skw_compat.h"
 
#define SKW_GENMASK_ULL(h, l)   (((~0ULL) - (1ULL << (l)) + 1) & \
               (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
 
static __always_inline u16 skw_get_unaligned_le16(const void *p)
{
   return le16_to_cpup((__le16 *)p);
}
 
static struct skw_element *skw_find_elem(u8 eid, const u8 *ies,
                   int len, const u8 *match,
                   unsigned int match_len,
                   unsigned int match_offset)
{
   struct skw_element *elem;
 
   skw_foreach_element_id(elem, eid, ies, len) {
       if (elem->datalen >= match_offset - 2 + match_len &&
           !memcmp(elem->data + match_offset - 2, match, match_len))
           return (void *)elem;
   }
 
   return NULL;
}
 
static const struct skw_element *
skw_get_profile_continuation(const u8 *ie, size_t ie_len,
           const struct skw_element *mbssid_elem,
           const struct skw_element *sub_elem)
{
   const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
   const struct skw_element *next_mbssid;
   const struct skw_element *sub;
 
   next_mbssid = skw_find_elem(WLAN_EID_MULTIPLE_BSSID,
           mbssid_end, ie_len - (mbssid_end - ie),
           NULL, 0, 0);
 
   if (!next_mbssid ||
       (sub_elem->data + sub_elem->datalen < mbssid_end - 1))
       return NULL;
 
   if (next_mbssid->datalen < 4)
       return NULL;
 
   sub = (void *)&next_mbssid->data[1];
 
   if (next_mbssid->data + next_mbssid->datalen < sub->data + sub->datalen)
       return NULL;
 
   if (sub->id != 0 || sub->datalen < 2)
       return NULL;
 
   return sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?  NULL : next_mbssid;
}
 
static size_t skw_merge_profile(const u8 *ie, size_t ie_len,
               const struct skw_element *mbssid_elem,
               const struct skw_element *sub_elem,
               u8 *merged_ie, size_t max_copy_len)
{
   size_t copied_len = sub_elem->datalen;
   const struct skw_element *next_mbssid;
 
   if (sub_elem->datalen > max_copy_len)
       return 0;
 
   memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
 
   while ((next_mbssid = skw_get_profile_continuation(ie, ie_len,
                   mbssid_elem, sub_elem))) {
       const struct skw_element *next = (void *)&next_mbssid->data[1];
 
       if (copied_len + next->datalen > max_copy_len)
           break;
 
       memcpy(merged_ie + copied_len, next->data, next->datalen);
 
       copied_len += next->datalen;
   }
 
   return copied_len;
}
 
static inline void skw_gen_new_bssid(const u8 *bssid, u8 max_bssid,
               u8 mbssid_index, u8 *new_bssid)
{
   u64 bssid_u64 = skw_mac_to_u64(bssid);
   u64 mask = SKW_GENMASK_ULL(max_bssid - 1, 0);
   u64 new_bssid_u64;
 
   new_bssid_u64 = bssid_u64 & ~mask;
 
   new_bssid_u64 |= ((bssid_u64 & mask) + mbssid_index) & mask;
 
   skw_u64_to_mac(new_bssid_u64, new_bssid);
}
 
static bool is_skw_element_inherited(const struct skw_element *elem,
       const struct skw_element *non_inherit_elem)
{
   u8 id_len, ext_id_len, i, loop_len, id;
   const u8 *list;
 
   if (elem->id == WLAN_EID_MULTIPLE_BSSID)
       return false;
 
   if (!non_inherit_elem || non_inherit_elem->datalen < 2)
       return true;
 
   id_len = non_inherit_elem->data[1];
   if (non_inherit_elem->datalen < 3 + id_len)
       return true;
 
   ext_id_len = non_inherit_elem->data[2 + id_len];
   if (non_inherit_elem->datalen < 3 + id_len + ext_id_len)
       return true;
 
   if (elem->id == SKW_WLAN_EID_EXTENSION) {
       if (!ext_id_len)
           return true;
 
       loop_len = ext_id_len;
       list = &non_inherit_elem->data[3 + id_len];
       id = elem->data[0];
   } else {
       if (!id_len)
           return true;
 
       loop_len = id_len;
       list = &non_inherit_elem->data[2];
       id = elem->id;
   }
 
   for (i = 0; i < loop_len; i++) {
       if (list[i] == id)
           return false;
   }
 
   return true;
}
 
static size_t skw_gen_new_ie(const u8 *ie, size_t ielen,
       const u8 *subelement, size_t subie_len,
       u8 *new_ie, gfp_t gfp)
{
   u8 eid;
   u8 *pos, *tmp;
   const u8 *tmp_old, *tmp_new;
   const struct skw_element *non_inherit;
   u8 *sub_copy;
 
   sub_copy = SKW_KMEMDUP(subelement, subie_len, gfp);
   if (!sub_copy)
       return 0;
 
   pos = &new_ie[0];
 
   /* set new ssid */
   tmp_new = cfg80211_find_ie(WLAN_EID_SSID, sub_copy, subie_len);
   if (tmp_new) {
       memcpy(pos, tmp_new, tmp_new[1] + 2);
       pos += (tmp_new[1] + 2);
   }
 
   /* get non inheritance list if exists */
   eid = SKW_EID_EXT_NON_INHERITANCE;
   non_inherit = skw_find_elem(SKW_WLAN_EID_EXTENSION, sub_copy,
                   subie_len, &eid, 1, 0);
 
   tmp_old = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
   tmp_old = (tmp_old) ? tmp_old + tmp_old[1] + 2 : ie;
 
   while (tmp_old + tmp_old[1] + 2 - ie <= ielen) {
       if (tmp_old[0] == 0) {
           tmp_old++;
           continue;
       }
 
       if (tmp_old[0] == SKW_WLAN_EID_EXTENSION) {
           tmp = (u8 *)skw_find_elem(SKW_WLAN_EID_EXTENSION,
                   sub_copy, subie_len, &tmp_old[2], 1, 2);
       } else {
           tmp = (u8 *)cfg80211_find_ie(tmp_old[0], sub_copy,
                   subie_len);
       }
 
       if (!tmp) {
           const struct skw_element *old_elem = (void *)tmp_old;
 
           if (is_skw_element_inherited(old_elem, non_inherit)) {
               memcpy(pos, tmp_old, tmp_old[1] + 2);
               pos += tmp_old[1] + 2;
           }
       } else {
           if (tmp_old[0] == WLAN_EID_VENDOR_SPECIFIC) {
               if (!memcmp(tmp_old + 2, tmp + 2, 5)) {
                   memcpy(pos, tmp, tmp[1] + 2);
                   pos += tmp[1] + 2;
                   tmp[0] = WLAN_EID_SSID;
               } else {
                   memcpy(pos, tmp_old, tmp_old[1] + 2);
                   pos += tmp_old[1] + 2;
               }
           } else {
               memcpy(pos, tmp, tmp[1] + 2);
               pos += tmp[1] + 2;
               tmp[0] = WLAN_EID_SSID;
           }
       }
 
       if (tmp_old + tmp_old[1] + 2 - ie == ielen)
           break;
 
       tmp_old += tmp_old[1] + 2;
   }
 
   tmp_new = sub_copy;
   while (tmp_new + tmp_new[1] + 2 - sub_copy <= subie_len) {
       if (!(tmp_new[0] == WLAN_EID_NON_TX_BSSID_CAP ||
           tmp_new[0] == WLAN_EID_SSID)) {
           memcpy(pos, tmp_new, tmp_new[1] + 2);
           pos += tmp_new[1] + 2;
       }
 
       if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len)
           break;
 
       tmp_new += tmp_new[1] + 2;
   }
 
   SKW_KFREE(sub_copy);
 
   return pos - new_ie;
}
 
static void skw_parse_mbssid_data(struct wiphy *wiphy,
               struct ieee80211_channel *rx_channel,
               s32 signal,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
               enum cfg80211_bss_frame_type ftype,
#endif
               const u8 *bssid, u64 tsf,
               u16 beacon_interval, const u8 *ie,
               size_t ie_len, gfp_t gfp)
{
   const u8 *idx_ie;
   const struct skw_element *elem, *sub;
   size_t new_ie_len;
   u8 bssid_index;
   u8 max_indicator;
   u8 new_bssid[ETH_ALEN];
   u8 *new_ie, *profile;
   u64 seen_indices = 0;
   u16 capability;
   struct cfg80211_bss *bss;
 
   new_ie = SKW_ZALLOC(IEEE80211_MAX_DATA_LEN, gfp);
   if (!new_ie)
       return;
 
   profile = SKW_ZALLOC(ie_len, gfp);
   if (!profile)
       goto out;
 
   skw_foreach_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ie_len) {
       if (elem->datalen < 4)
           continue;
 
       skw_foreach_element(sub, elem->data + 1, elem->datalen - 1) {
           u8 profile_len;
 
           if (sub->id != 0 || sub->datalen < 4)
               continue;
 
           if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
               sub->data[1] != 2) {
               continue;
           }
 
           memset(profile, 0, ie_len);
 
           profile_len = skw_merge_profile(ie, ie_len,
                   elem, sub, profile, ie_len);
           idx_ie = cfg80211_find_ie(SKW_WLAN_EID_MULTI_BSSID_IDX,
                         profile, profile_len);
 
           if (!idx_ie || idx_ie[1] < 1 ||
               idx_ie[2] == 0 || idx_ie[2] > 46) {
               /* No valid Multiple BSSID-Index element */
               continue;
           }
 
           if (seen_indices & (1ULL << (idx_ie[2])))
               net_dbg_ratelimited("Partial info for BSSID index %d\n",
                       idx_ie[2]);
 
           seen_indices |= (1ULL << (idx_ie[2]));
 
           bssid_index = idx_ie[2];
           max_indicator = elem->data[0];
 
           skw_gen_new_bssid(bssid, max_indicator,
                   bssid_index, new_bssid);
 
           memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
           new_ie_len = skw_gen_new_ie(ie, ie_len, profile,
                       profile_len, new_ie,
                       GFP_KERNEL);
           if (!new_ie_len)
               continue;
 
           capability = skw_get_unaligned_le16(profile + 2);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
           bss = cfg80211_inform_bss(wiphy, rx_channel, ftype,
                       new_bssid, tsf, capability,
                       beacon_interval, new_ie,
                       new_ie_len, signal, gfp);
#else
           bss = cfg80211_inform_bss(wiphy, rx_channel,
                       new_bssid, tsf, capability,
                       beacon_interval, new_ie,
                       new_ie_len, signal, gfp);
#endif
 
           if (!bss)
               break;
 
           skw_bss_priv(bss)->bssid_index = bssid_index;
           skw_bss_priv(bss)->max_bssid_indicator = max_indicator;
 
           cfg80211_put_bss(wiphy, bss);
       }
   }
 
   SKW_KFREE(profile);
out:
   SKW_KFREE(new_ie);
}
 
void skw_mbssid_data_parser(struct wiphy *wiphy, bool beacon,
       struct ieee80211_channel *chan, s32 signal,
       struct ieee80211_mgmt *mgmt, int mgmt_len)
{
   const u8 *ie = mgmt->u.probe_resp.variable;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
   enum cfg80211_bss_frame_type ftype = CFG80211_BSS_FTYPE_PRESP;
#endif
   size_t len = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
 
   if (!cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, mgmt_len - len))
       return;
 
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
   if (beacon)
       ftype = CFG80211_BSS_FTYPE_BEACON;
#endif
 
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
   skw_parse_mbssid_data(wiphy, chan, signal,  ftype, mgmt->bssid,
           le64_to_cpu(mgmt->u.probe_resp.timestamp),
           le16_to_cpu(mgmt->u.probe_resp.beacon_int),
           ie, mgmt_len - len, GFP_KERNEL);
#else
   skw_parse_mbssid_data(wiphy, chan, signal, mgmt->bssid,
           le64_to_cpu(mgmt->u.probe_resp.timestamp),
           le16_to_cpu(mgmt->u.probe_resp.beacon_int),
           ie, mgmt_len - len, GFP_KERNEL);
#endif
}