blob: 2d4b438f3cbe834b7cf5c346716004eff6027232 [file] [log] [blame]
developer617abbd2024-04-23 14:50:01 +08001From 63eb73fe7fe0cb9e088b95cffe4b123885bf9ede Mon Sep 17 00:00:00 2001
2From: "howard.hsu" <howard-yh.hsu@mediatek.com>
3Date: Wed, 19 Jan 2022 19:18:07 +0800
4Subject: [PATCH 029/104] mtk: hostapd: Add neighbor report and BSS Termination
5 for MBO certification
6
71. Add hostapd_neighbor_count() and hostapd_neighbor_insert_buffer ()
8The first function can count the number of neighbor report in neighbore report
9database. The second can iterate neighbor report database to build up neighbor
10report data.
11
122. Support including neighbor report elements in ANQP response
133. Support including neignbor report elements in BTM response
144. Support configuring BSS Termination TSF by using hostapd_cli command
155. Disable interface if BSS Termination TSF is set
166. Support including neighbor report elements in BTM request
177. Add hostapd_neighbor_set_own_report_pref()
188. Add hostapd_neighbor_set_pref_by_non_pref_chan()
19
20Revert set_send_disassoc_frame_timer
21---
22 hostapd/ctrl_iface.c | 5 ++
23 src/ap/ap_config.c | 1 +
24 src/ap/ap_config.h | 1 +
25 src/ap/ctrl_iface_ap.c | 18 ++++++-
26 src/ap/gas_serv.c | 29 ++++++++++
27 src/ap/gas_serv.h | 2 +
28 src/ap/neighbor_db.c | 118 +++++++++++++++++++++++++++++++++++++++++
29 src/ap/neighbor_db.h | 9 ++++
30 src/ap/wnm_ap.c | 43 +++++++++++++--
31 9 files changed, 221 insertions(+), 5 deletions(-)
32
33diff --git a/hostapd/ctrl_iface.c b/hostapd/ctrl_iface.c
34index f76226cf4..4240319b7 100644
35--- a/hostapd/ctrl_iface.c
36+++ b/hostapd/ctrl_iface.c
37@@ -1300,6 +1300,11 @@ static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
38 #endif /* CONFIG_DPP */
39 } else if (os_strcasecmp(cmd, "setband") == 0) {
40 ret = hostapd_ctrl_iface_set_band(hapd, value);
41+ } else if (os_strcasecmp(cmd, "bss_termination_tsf") == 0) {
42+ int termination_sec = atoi(value);
43+ hapd->conf->bss_termination_tsf = termination_sec;
44+ wpa_printf(MSG_DEBUG, "BSS Termination TSF: value = %d",
45+ termination_sec);
46 } else {
47 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
48 if (ret)
49diff --git a/src/ap/ap_config.c b/src/ap/ap_config.c
50index ca67aeb41..6c8b10291 100644
51--- a/src/ap/ap_config.c
52+++ b/src/ap/ap_config.c
53@@ -175,6 +175,7 @@ void hostapd_config_defaults_bss(struct hostapd_bss_config *bss)
54 bss->pasn_comeback_after = 10;
55 bss->pasn_noauth = 1;
56 #endif /* CONFIG_PASN */
57+ bss->bss_termination_tsf = 0;
58 }
59
60
61diff --git a/src/ap/ap_config.h b/src/ap/ap_config.h
62index d10b00be9..379dc22cf 100644
63--- a/src/ap/ap_config.h
64+++ b/src/ap/ap_config.h
65@@ -560,6 +560,7 @@ struct hostapd_bss_config {
66 int wnm_sleep_mode;
67 int wnm_sleep_mode_no_keys;
68 int bss_transition;
69+ unsigned int bss_termination_tsf;
70
71 /* IEEE 802.11u - Interworking */
72 int interworking;
73diff --git a/src/ap/ctrl_iface_ap.c b/src/ap/ctrl_iface_ap.c
74index cd7db4fc6..a2f89260c 100644
75--- a/src/ap/ctrl_iface_ap.c
76+++ b/src/ap/ctrl_iface_ap.c
77@@ -1377,6 +1377,10 @@ int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
78 wpa_printf(MSG_DEBUG, "Invalid bss_term data");
79 return -1;
80 }
81+ if (hapd->conf->bss_termination_tsf) {
82+ WPA_PUT_LE64(&bss_term_dur[2], hapd->conf->bss_termination_tsf);
83+ }
84+
85 end++;
86 WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
87 }
88@@ -1403,16 +1407,26 @@ int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
89 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
90 }
91
92- if (os_strstr(cmd, " pref=1"))
93+ if (os_strstr(cmd, " pref=1")) {
94 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
95+ if (nei_len == 0) {
96+ // Add neigibor report from neighbor report db to nei_rep buffer
97+ nei_len = hostapd_neighbor_insert_buffer (hapd, nei_rep, 1000);
98+ }
99+ }
100 if (os_strstr(cmd, " abridged=1"))
101 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
102- if (os_strstr(cmd, " disassoc_imminent=1"))
103+ if (os_strstr(cmd, " disassoc_imminent=1")) {
104 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
105+ /* Set own BSS neighbor report preference value as 0 */
106+ hostapd_neighbor_set_own_report_pref(hapd, nei_rep, nei_len, 0);
107+ }
108 if (os_strstr(cmd, " link_removal_imminent=1"))
109 req_mode |= WNM_BSS_TM_REQ_LINK_REMOVAL_IMMINENT;
110
111 #ifdef CONFIG_MBO
112+ hostapd_neighbor_set_pref_by_non_pref_chan(hapd, sta, nei_rep, nei_len);
113+
114 pos = os_strstr(cmd, "mbo=");
115 if (pos) {
116 unsigned int mbo_reason, cell_pref, reassoc_delay;
117diff --git a/src/ap/gas_serv.c b/src/ap/gas_serv.c
118index 4642e4927..cce6df41c 100644
119--- a/src/ap/gas_serv.c
120+++ b/src/ap/gas_serv.c
121@@ -19,6 +19,7 @@
122 #include "dpp_hostapd.h"
123 #include "sta_info.h"
124 #include "gas_serv.h"
125+#include "neighbor_db.h"
126
127
128 #ifdef CONFIG_DPP
129@@ -369,6 +370,24 @@ static void anqp_add_network_auth_type(struct hostapd_data *hapd,
130 }
131 }
132
133+static void anqp_add_neighbor_report(struct hostapd_data *hapd,
134+ struct wpabuf *buf)
135+{
136+ struct hostapd_neighbor_entry *nr;
137+ u8 *len_pos = gas_anqp_add_element(buf, ANQP_NEIGHBOR_REPORT);
138+ if (dl_list_empty(&hapd->nr_db)) {
139+ wpabuf_put_le16(buf, 0);
140+ }
141+ else {
142+ dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list ) {
143+ wpabuf_put_u8(buf, WLAN_EID_NEIGHBOR_REPORT);
144+ wpabuf_put_u8(buf, wpabuf_len(nr->nr));
145+ wpabuf_put_buf(buf, nr->nr);
146+ }
147+ }
148+ gas_anqp_set_element_len(buf, len_pos);
149+}
150+
151
152 static void anqp_add_roaming_consortium(struct hostapd_data *hapd,
153 struct wpabuf *buf)
154@@ -986,6 +1005,9 @@ gas_serv_build_gas_resp_payload(struct hostapd_data *hapd,
155 len += 1000;
156 if (request & ANQP_REQ_ICON_REQUEST)
157 len += 65536;
158+ if (request & ANQP_REQ_NEIGHBOR_REPORT) {
159+ len += (40 * hostapd_neighbor_count(hapd));
160+ }
161 #ifdef CONFIG_FILS
162 if (request & ANQP_FILS_REALM_INFO)
163 len += 2 * dl_list_len(&hapd->conf->fils_realms);
164@@ -1028,6 +1050,8 @@ gas_serv_build_gas_resp_payload(struct hostapd_data *hapd,
165 anqp_add_elem(hapd, buf, ANQP_TDLS_CAPABILITY);
166 if (request & ANQP_REQ_EMERGENCY_NAI)
167 anqp_add_elem(hapd, buf, ANQP_EMERGENCY_NAI);
168+ if (request & ANQP_REQ_NEIGHBOR_REPORT)
169+ anqp_add_neighbor_report(hapd, buf);
170
171 for (i = 0; i < num_extra_req; i++) {
172 #ifdef CONFIG_FILS
173@@ -1172,6 +1196,11 @@ static void rx_anqp_query_list_id(struct hostapd_data *hapd, u16 info_id,
174 "Emergency NAI",
175 get_anqp_elem(hapd, info_id) != NULL, qi);
176 break;
177+ case ANQP_NEIGHBOR_REPORT:
178+ set_anqp_req(ANQP_REQ_NEIGHBOR_REPORT,
179+ "Neighbor Report",
180+ get_anqp_elem(hapd, info_id) != NULL, qi);
181+ break;
182 default:
183 #ifdef CONFIG_FILS
184 if (info_id == ANQP_FILS_REALM_INFO &&
185diff --git a/src/ap/gas_serv.h b/src/ap/gas_serv.h
186index 7646a98a4..ce492b53f 100644
187--- a/src/ap/gas_serv.h
188+++ b/src/ap/gas_serv.h
189@@ -40,6 +40,8 @@
190 (1 << (ANQP_TDLS_CAPABILITY - ANQP_QUERY_LIST))
191 #define ANQP_REQ_EMERGENCY_NAI \
192 (1 << (ANQP_EMERGENCY_NAI - ANQP_QUERY_LIST))
193+#define ANQP_REQ_NEIGHBOR_REPORT \
194+ (1 << (ANQP_NEIGHBOR_REPORT - ANQP_QUERY_LIST))
195 /*
196 * First 15 Hotspot 2.0 vendor specific ANQP-elements can be included in the
197 * optimized bitmap.
198diff --git a/src/ap/neighbor_db.c b/src/ap/neighbor_db.c
199index f7a7d83d4..d9216a5ae 100644
200--- a/src/ap/neighbor_db.c
201+++ b/src/ap/neighbor_db.c
202@@ -89,6 +89,38 @@ int hostapd_neighbor_show(struct hostapd_data *hapd, char *buf, size_t buflen)
203 }
204
205
206+int hostapd_neighbor_count(struct hostapd_data *hapd)
207+{
208+ struct hostapd_neighbor_entry *nr;
209+ int count = 0;
210+
211+ dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry,
212+ list) {
213+ count++;
214+ }
215+ return count;
216+}
217+
218+
219+int hostapd_neighbor_insert_buffer(struct hostapd_data *hapd, char *buf,
220+ size_t buflen)
221+{
222+ struct hostapd_neighbor_entry *nr;
223+ char *pos = buf;
224+
225+ dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry,
226+ list) {
227+ /* For neighbor report IE, we only need bssid and nr*/
228+ *pos++ = WLAN_EID_NEIGHBOR_REPORT;
229+ *pos++ = wpabuf_len(nr->nr);
230+ os_memcpy(pos, wpabuf_head(nr->nr), wpabuf_len(nr->nr));
231+ pos += wpabuf_len(nr->nr);
232+ }
233+
234+ return pos - buf;
235+}
236+
237+
238 static void hostapd_neighbor_clear_entry(struct hostapd_neighbor_entry *nr)
239 {
240 wpabuf_free(nr->nr);
241@@ -364,3 +396,89 @@ int hostapd_neighbor_sync_own_report(struct hostapd_data *hapd)
242
243 return 0;
244 }
245+
246+void hostapd_neighbor_set_own_report_pref(struct hostapd_data *hapd, char *nei_buf,
247+ size_t buflen, const int pref)
248+{
249+ struct hostapd_neighbor_entry *nr;
250+ char *pos, *next_nr;
251+
252+ pos = nei_buf;
253+ next_nr = nei_buf;
254+
255+ dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry,
256+ list) {
257+ pos = next_nr;
258+ next_nr = pos + 2 + wpabuf_len(nr->nr);
259+ /* Shift 2 bytes for Element ID and Neighbor report length */
260+ pos = pos + 2;
261+ if(os_memcmp(pos, hapd->own_addr, ETH_ALEN) == 0) {
262+ /* Shift for BSSID + BSSID info + Op_class + channel num + PHY type */
263+ pos = pos + 6 + 4 + 1 + 1 + 1;
264+
265+ /* Iterate Subelement */
266+ while (next_nr - pos > 0) {
267+ if (*pos == 3) {
268+ pos = pos + 2;
269+ *pos = pref;
270+ return;
271+ } else {
272+ pos++;
273+ int shift_len = *pos++;
274+ pos = pos + shift_len;
275+ }
276+ }
277+ }
278+ }
279+}
280+
281+#ifdef CONFIG_MBO
282+void hostapd_neighbor_set_pref_by_non_pref_chan(struct hostapd_data *hapd,
283+ struct sta_info* sta, char *nei_buf, size_t buflen)
284+{
285+ struct hostapd_neighbor_entry *nr;
286+ struct mbo_non_pref_chan_info *info;
287+ u8 i;
288+
289+ for(info = sta->non_pref_chan; info; info = info->next) {
290+ /* Check OP_Class and Channel num */
291+ for(i = 0; i < info->num_channels; i++) {
292+ char *pos, *next_nr;
293+
294+ pos = nei_buf;
295+ next_nr = nei_buf;
296+
297+ /* Iterate Neighbor report database */
298+ dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry,
299+ list) {
300+ pos = next_nr;
301+ next_nr = pos + 2 + wpabuf_len(nr->nr);
302+ /**
303+ * Shift 12 bytes for Element ID, Neighbor report length,
304+ * BSSID and BSSID info.
305+ */
306+ pos = pos + 12;
307+ int nr_op_class = *pos++;
308+ int nr_channel = *pos;
309+ if(info->op_class == nr_op_class && info->channels[i] == nr_channel) {
310+ /* Shift for Channel Num + PHY type */
311+ pos = pos + 1 + 1;
312+
313+ // Iterate Subelement
314+ while(next_nr - pos > 0) {
315+ if(*pos == 3) {
316+ pos = pos + 2;
317+ *pos = info->pref;
318+ break;
319+ }else {
320+ pos++;
321+ int shift_len = *pos++;
322+ pos = pos + shift_len;
323+ }
324+ }
325+ }
326+ }
327+ }
328+ }
329+}
330+#endif
331diff --git a/src/ap/neighbor_db.h b/src/ap/neighbor_db.h
332index 53f714203..cf1400256 100644
333--- a/src/ap/neighbor_db.h
334+++ b/src/ap/neighbor_db.h
335@@ -25,4 +25,13 @@ int hostapd_neighbor_remove(struct hostapd_data *hapd, const u8 *bssid,
336 const struct wpa_ssid_value *ssid);
337 void hostapd_free_neighbor_db(struct hostapd_data *hapd);
338
339+int hostapd_neighbor_count(struct hostapd_data *hapd);
340+int hostapd_neighbor_insert_buffer(struct hostapd_data *hapd, char *buf,
341+ size_t buflen);
342+void hostapd_neighbor_set_own_report_pref(struct hostapd_data *hapd, char *nei_buf,
343+ size_t buflen, const int pref);
344+#ifdef CONFIG_MBO
345+void hostapd_neighbor_set_pref_by_non_pref_chan(struct hostapd_data *hapd,
346+ struct sta_info* sta, char *nei_buf, size_t buflen);
347+#endif
348 #endif /* NEIGHBOR_DB_H */
349diff --git a/src/ap/wnm_ap.c b/src/ap/wnm_ap.c
350index d259200c9..4ac96b1be 100644
351--- a/src/ap/wnm_ap.c
352+++ b/src/ap/wnm_ap.c
353@@ -20,6 +20,7 @@
354 #include "ap/wpa_auth.h"
355 #include "mbo_ap.h"
356 #include "wnm_ap.h"
357+#include "ap/neighbor_db.h"
358
359 #define MAX_TFS_IE_LEN 1024
360
361@@ -390,13 +391,24 @@ static int ieee802_11_send_bss_trans_mgmt_request(struct hostapd_data *hapd,
362 u8 *pos;
363 int res;
364
365- mgmt = os_zalloc(sizeof(*mgmt));
366- if (mgmt == NULL)
367+ int nr_num = hostapd_neighbor_count(hapd);
368+ int nr_size = ETH_ALEN + 4 + 1 + 1 + 1 + 5;
369+ int total_nr_size = nr_num * nr_size;
370+ u8 *nr_data = os_malloc(total_nr_size);
371+ int nr_data_len = 0;
372+ if(nr_data == NULL) {
373+ wpa_printf (MSG_ERROR, "Failed to allocate memory");
374+ } else {
375+ nr_data_len = hostapd_neighbor_insert_buffer(hapd, nr_data, total_nr_size);
376+ }
377+ mgmt = os_zalloc(sizeof(*mgmt) + nr_data_len);
378+ if (mgmt == NULL) {
379+ wpa_printf (MSG_ERROR, "Failed to allocate memory for mgmt frame");
380 return -1;
381+ }
382
383 sta = ap_get_sta(hapd, addr);
384 own_addr = wnm_ap_get_own_addr(hapd, sta);
385-
386 os_memcpy(mgmt->da, addr, ETH_ALEN);
387 os_memcpy(mgmt->sa, own_addr, ETH_ALEN);
388 os_memcpy(mgmt->bssid, own_addr, ETH_ALEN);
389@@ -406,10 +418,18 @@ static int ieee802_11_send_bss_trans_mgmt_request(struct hostapd_data *hapd,
390 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
391 mgmt->u.action.u.bss_tm_req.dialog_token = dialog_token;
392 mgmt->u.action.u.bss_tm_req.req_mode = 0;
393+ if(nr_num) {
394+ mgmt->u.action.u.bss_tm_req.req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
395+ }
396 mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0);
397 mgmt->u.action.u.bss_tm_req.validity_interval = 1;
398 pos = mgmt->u.action.u.bss_tm_req.variable;
399
400+ if(nr_num) {
401+ os_memcpy(pos, nr_data, nr_data_len);
402+ pos += nr_data_len;
403+ }
404+
405 hapd->openwrt_stats.wnm.bss_transition_request_tx++;
406 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request to "
407 MACSTR " dialog_token=%u req_mode=0x%x disassoc_timer=%u "
408@@ -915,6 +935,22 @@ static void set_disassoc_timer(struct hostapd_data *hapd, struct sta_info *sta,
409 }
410
411
412+void bss_termination_disable_iface(void *eloop_ctx, void *timeout_ctx)
413+{
414+ struct hostapd_data *hapd = eloop_ctx;
415+ hostapd_disable_iface(hapd->iface);
416+}
417+
418+
419+static void set_disable_iface_timer(struct hostapd_data *hapd, struct sta_info *sta,
420+ int disable_iface_timer)
421+{
422+ wpa_printf(MSG_DEBUG, "Disable interface timer set to %d secs", disable_iface_timer);
423+ eloop_register_timeout(disable_iface_timer, 0,
424+ bss_termination_disable_iface, hapd, NULL);
425+}
426+
427+
428 int wnm_send_ess_disassoc_imminent(struct hostapd_data *hapd,
429 struct sta_info *sta, const char *url,
430 int disassoc_timer)
431@@ -1006,6 +1042,7 @@ int wnm_send_bss_tm_req(struct hostapd_data *hapd, struct sta_info *sta,
432 bss_term_dur) {
433 os_memcpy(pos, bss_term_dur, 12);
434 pos += 12;
435+ set_disable_iface_timer(hapd, sta, hapd->conf->bss_termination_tsf);
436 }
437
438 if (url) {
439--
4402.39.2
441