]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/ieee802_11_shared.c
OCV: Include and verify OCI in SA Query frames
[thirdparty/hostap.git] / src / ap / ieee802_11_shared.c
CommitLineData
d4370eac
MP
1/*
2 * hostapd / IEEE 802.11 Management
b6668734 3 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
d4370eac 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
d4370eac
MP
7 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "common/ieee802_11_defs.h"
f9da7505 13#include "common/ocv.h"
d4370eac
MP
14#include "hostapd.h"
15#include "sta_info.h"
16#include "ap_config.h"
17#include "ap_drv_ops.h"
f9da7505 18#include "wpa_auth.h"
39b97072 19#include "ieee802_11.h"
d4370eac
MP
20
21
22#ifdef CONFIG_IEEE80211W
23
24u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
25 struct sta_info *sta, u8 *eid)
26{
27 u8 *pos = eid;
28 u32 timeout, tu;
10e694a6 29 struct os_reltime now, passed;
d4370eac
MP
30
31 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
32 *pos++ = 5;
33 *pos++ = WLAN_TIMEOUT_ASSOC_COMEBACK;
10e694a6
JB
34 os_get_reltime(&now);
35 os_reltime_sub(&now, &sta->sa_query_start, &passed);
d4370eac
MP
36 tu = (passed.sec * 1000000 + passed.usec) / 1024;
37 if (hapd->conf->assoc_sa_query_max_timeout > tu)
38 timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
39 else
40 timeout = 0;
41 if (timeout < hapd->conf->assoc_sa_query_max_timeout)
42 timeout++; /* add some extra time for local timers */
43 WPA_PUT_LE32(pos, timeout);
44 pos += 4;
45
46 return pos;
47}
48
49
50/* MLME-SAQuery.request */
51void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
52 const u8 *addr, const u8 *trans_id)
53{
f9da7505
MV
54#ifdef CONFIG_OCV
55 struct sta_info *sta;
56#endif /* CONFIG_OCV */
57 struct ieee80211_mgmt *mgmt;
58 u8 *oci_ie = NULL;
59 u8 oci_ie_len = 0;
d4370eac
MP
60 u8 *end;
61
62 wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
63 MACSTR, MAC2STR(addr));
64 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
65 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
66
f9da7505
MV
67#ifdef CONFIG_OCV
68 sta = ap_get_sta(hapd, addr);
69 if (sta && wpa_auth_uses_ocv(sta->wpa_sm)) {
70 struct wpa_channel_info ci;
71
72 if (hostapd_drv_channel_info(hapd, &ci) != 0) {
73 wpa_printf(MSG_WARNING,
74 "Failed to get channel info for OCI element in SA Query Request");
75 return;
76 }
77
78 oci_ie_len = OCV_OCI_EXTENDED_LEN;
79 oci_ie = os_zalloc(oci_ie_len);
80 if (!oci_ie) {
81 wpa_printf(MSG_WARNING,
82 "Failed to allocate buffer for OCI element in SA Query Request");
83 return;
84 }
85
86 if (ocv_insert_extended_oci(&ci, oci_ie) < 0) {
87 os_free(oci_ie);
88 return;
89 }
90 }
91#endif /* CONFIG_OCV */
92
93 mgmt = os_zalloc(sizeof(*mgmt) + oci_ie_len);
94 if (!mgmt) {
95 wpa_printf(MSG_DEBUG,
96 "Failed to allocate buffer for SA Query Response frame");
97 os_free(oci_ie);
98 return;
99 }
100
101 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
102 WLAN_FC_STYPE_ACTION);
103 os_memcpy(mgmt->da, addr, ETH_ALEN);
104 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
105 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
106 mgmt->u.action.category = WLAN_ACTION_SA_QUERY;
107 mgmt->u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
108 os_memcpy(mgmt->u.action.u.sa_query_req.trans_id, trans_id,
d4370eac 109 WLAN_SA_QUERY_TR_ID_LEN);
f9da7505
MV
110 end = mgmt->u.action.u.sa_query_req.variable;
111#ifdef CONFIG_OCV
112 if (oci_ie_len > 0) {
113 os_memcpy(end, oci_ie, oci_ie_len);
114 end += oci_ie_len;
115 }
116#endif /* CONFIG_OCV */
117 if (hostapd_drv_send_mlme(hapd, mgmt, end - (u8 *) mgmt, 0) < 0)
61323e70 118 wpa_printf(MSG_INFO, "ieee802_11_send_sa_query_req: send failed");
f9da7505
MV
119
120 os_free(mgmt);
121 os_free(oci_ie);
d4370eac
MP
122}
123
124
19df9b07
JM
125static void ieee802_11_send_sa_query_resp(struct hostapd_data *hapd,
126 const u8 *sa, const u8 *trans_id)
d4370eac
MP
127{
128 struct sta_info *sta;
f9da7505
MV
129 struct ieee80211_mgmt *resp;
130 u8 *oci_ie = NULL;
131 u8 oci_ie_len = 0;
d4370eac
MP
132 u8 *end;
133
134 wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Request from "
135 MACSTR, MAC2STR(sa));
136 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
137 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
138
139 sta = ap_get_sta(hapd, sa);
140 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
141 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignore SA Query Request "
142 "from unassociated STA " MACSTR, MAC2STR(sa));
143 return;
144 }
145
f9da7505
MV
146#ifdef CONFIG_OCV
147 if (wpa_auth_uses_ocv(sta->wpa_sm)) {
148 struct wpa_channel_info ci;
149
150 if (hostapd_drv_channel_info(hapd, &ci) != 0) {
151 wpa_printf(MSG_WARNING,
152 "Failed to get channel info for OCI element in SA Query Response");
153 return;
154 }
155
156 oci_ie_len = OCV_OCI_EXTENDED_LEN;
157 oci_ie = os_zalloc(oci_ie_len);
158 if (!oci_ie) {
159 wpa_printf(MSG_WARNING,
160 "Failed to allocate buffer for for OCI element in SA Query Response");
161 return;
162 }
163
164 if (ocv_insert_extended_oci(&ci, oci_ie) < 0) {
165 os_free(oci_ie);
166 return;
167 }
168 }
169#endif /* CONFIG_OCV */
170
171 resp = os_zalloc(sizeof(*resp) + oci_ie_len);
172 if (!resp) {
173 wpa_printf(MSG_DEBUG,
174 "Failed to allocate buffer for SA Query Response frame");
175 os_free(oci_ie);
176 return;
177 }
178
d4370eac
MP
179 wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Response to "
180 MACSTR, MAC2STR(sa));
181
f9da7505
MV
182 resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
183 WLAN_FC_STYPE_ACTION);
184 os_memcpy(resp->da, sa, ETH_ALEN);
185 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
186 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
187 resp->u.action.category = WLAN_ACTION_SA_QUERY;
188 resp->u.action.u.sa_query_req.action = WLAN_SA_QUERY_RESPONSE;
189 os_memcpy(resp->u.action.u.sa_query_req.trans_id, trans_id,
d4370eac 190 WLAN_SA_QUERY_TR_ID_LEN);
f9da7505
MV
191 end = resp->u.action.u.sa_query_req.variable;
192#ifdef CONFIG_OCV
193 if (oci_ie_len > 0) {
194 os_memcpy(end, oci_ie, oci_ie_len);
195 end += oci_ie_len;
196 }
197#endif /* CONFIG_OCV */
198 if (hostapd_drv_send_mlme(hapd, resp, end - (u8 *) resp, 0) < 0)
61323e70 199 wpa_printf(MSG_INFO, "ieee80211_mgmt_sa_query_request: send failed");
f9da7505
MV
200
201 os_free(resp);
202 os_free(oci_ie);
d4370eac
MP
203}
204
205
f9da7505
MV
206void ieee802_11_sa_query_action(struct hostapd_data *hapd,
207 const struct ieee80211_mgmt *mgmt,
208 size_t len)
d4370eac
MP
209{
210 struct sta_info *sta;
211 int i;
f9da7505
MV
212 const u8 *sa = mgmt->sa;
213 const u8 action_type = mgmt->u.action.u.sa_query_resp.action;
214 const u8 *trans_id = mgmt->u.action.u.sa_query_resp.trans_id;
215
216 sta = ap_get_sta(hapd, sa);
217
218#ifdef CONFIG_OCV
219 if (sta && wpa_auth_uses_ocv(sta->wpa_sm)) {
220 struct ieee802_11_elems elems;
221 struct wpa_channel_info ci;
222 int tx_chanwidth;
223 int tx_seg1_idx;
224 size_t ies_len;
225 const u8 *ies;
226
227 ies = mgmt->u.action.u.sa_query_resp.variable;
228 ies_len = len - (ies - (u8 *) mgmt);
229 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) ==
230 ParseFailed) {
231 wpa_printf(MSG_DEBUG,
232 "SA Query: Failed to parse elements");
233 return;
234 }
235
236 if (hostapd_drv_channel_info(hapd, &ci) != 0) {
237 wpa_printf(MSG_WARNING,
238 "Failed to get channel info to validate received OCI in SA Query Action frame");
239 return;
240 }
241
242 if (get_sta_tx_parameters(sta->wpa_sm,
243 channel_width_to_int(ci.chanwidth),
244 ci.seg1_idx, &tx_chanwidth,
245 &tx_seg1_idx) < 0)
246 return;
247
248 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
249 tx_chanwidth, tx_seg1_idx) != 0) {
250 wpa_printf(MSG_WARNING, "%s", ocv_errorstr);
251 return;
252 }
253 }
254#endif /* CONFIG_OCV */
d4370eac
MP
255
256 if (action_type == WLAN_SA_QUERY_REQUEST) {
257 ieee802_11_send_sa_query_resp(hapd, sa, trans_id);
258 return;
259 }
260
261 if (action_type != WLAN_SA_QUERY_RESPONSE) {
262 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
263 "Action %d", action_type);
264 return;
265 }
266
267 wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
268 MACSTR, MAC2STR(sa));
269 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
270 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
271
272 /* MLME-SAQuery.confirm */
273
d4370eac
MP
274 if (sta == NULL || sta->sa_query_trans_id == NULL) {
275 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
276 "pending SA Query request found");
277 return;
278 }
279
280 for (i = 0; i < sta->sa_query_count; i++) {
281 if (os_memcmp(sta->sa_query_trans_id +
282 i * WLAN_SA_QUERY_TR_ID_LEN,
283 trans_id, WLAN_SA_QUERY_TR_ID_LEN) == 0)
284 break;
285 }
286
287 if (i >= sta->sa_query_count) {
288 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
289 "transaction identifier found");
290 return;
291 }
292
293 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
294 HOSTAPD_LEVEL_DEBUG,
295 "Reply to pending SA Query received");
296 ap_sta_stop_sa_query(hapd, sta);
297}
298
299#endif /* CONFIG_IEEE80211W */
06c4d247
JM
300
301
8cd6b7bc
JB
302static void hostapd_ext_capab_byte(struct hostapd_data *hapd, u8 *pos, int idx)
303{
304 *pos = 0x00;
305
306 switch (idx) {
307 case 0: /* Bits 0-7 */
db63757d
PS
308 if (hapd->iconf->obss_interval)
309 *pos |= 0x01; /* Bit 0 - Coexistence management */
6315bfdb
AO
310 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)
311 *pos |= 0x04; /* Bit 2 - Extended Channel Switching */
8cd6b7bc
JB
312 break;
313 case 1: /* Bits 8-15 */
7d597d46
KP
314 if (hapd->conf->proxy_arp)
315 *pos |= 0x10; /* Bit 12 - Proxy ARP */
d514b502
JM
316 if (hapd->conf->coloc_intf_reporting) {
317 /* Bit 13 - Collocated Interference Reporting */
318 *pos |= 0x20;
319 }
8cd6b7bc
JB
320 break;
321 case 2: /* Bits 16-23 */
322 if (hapd->conf->wnm_sleep_mode)
323 *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
324 if (hapd->conf->bss_transition)
325 *pos |= 0x08; /* Bit 19 - BSS Transition */
326 break;
327 case 3: /* Bits 24-31 */
b5bf84ba 328#ifdef CONFIG_WNM_AP
8cd6b7bc 329 *pos |= 0x02; /* Bit 25 - SSID List */
b5bf84ba 330#endif /* CONFIG_WNM_AP */
8cd6b7bc
JB
331 if (hapd->conf->time_advertisement == 2)
332 *pos |= 0x08; /* Bit 27 - UTC TSF Offset */
333 if (hapd->conf->interworking)
334 *pos |= 0x80; /* Bit 31 - Interworking */
335 break;
336 case 4: /* Bits 32-39 */
c551700f
KP
337 if (hapd->conf->qos_map_set_len)
338 *pos |= 0x01; /* Bit 32 - QoS Map */
8cd6b7bc
JB
339 if (hapd->conf->tdls & TDLS_PROHIBIT)
340 *pos |= 0x40; /* Bit 38 - TDLS Prohibited */
341 if (hapd->conf->tdls & TDLS_PROHIBIT_CHAN_SWITCH) {
342 /* Bit 39 - TDLS Channel Switching Prohibited */
343 *pos |= 0x80;
344 }
345 break;
346 case 5: /* Bits 40-47 */
3fb17a95
JM
347#ifdef CONFIG_HS20
348 if (hapd->conf->hs20)
349 *pos |= 0x40; /* Bit 46 - WNM-Notification */
350#endif /* CONFIG_HS20 */
e5783434
JM
351#ifdef CONFIG_MBO
352 if (hapd->conf->mbo_enabled)
353 *pos |= 0x40; /* Bit 46 - WNM-Notification */
354#endif /* CONFIG_MBO */
8cd6b7bc
JB
355 break;
356 case 6: /* Bits 48-55 */
357 if (hapd->conf->ssid.utf8_ssid)
358 *pos |= 0x01; /* Bit 48 - UTF-8 SSID */
359 break;
f55acd90
JM
360 case 7: /* Bits 56-63 */
361 break;
faecb392
LD
362 case 8: /* Bits 64-71 */
363 if (hapd->conf->ftm_responder)
364 *pos |= 0x40; /* Bit 70 - FTM responder */
365 if (hapd->conf->ftm_initiator)
366 *pos |= 0x80; /* Bit 71 - FTM initiator */
703470bf 367 break;
f55acd90
JM
368 case 9: /* Bits 72-79 */
369#ifdef CONFIG_FILS
370 if ((hapd->conf->wpa & WPA_PROTO_RSN) &&
371 wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt))
372 *pos |= 0x01;
373#endif /* CONFIG_FILS */
faecb392 374 break;
8cd6b7bc
JB
375 }
376}
377
378
06c4d247
JM
379u8 * hostapd_eid_ext_capab(struct hostapd_data *hapd, u8 *eid)
380{
381 u8 *pos = eid;
8cd6b7bc 382 u8 len = 0, i;
06c4d247
JM
383
384 if (hapd->conf->tdls & (TDLS_PROHIBIT | TDLS_PROHIBIT_CHAN_SWITCH))
385 len = 5;
386 if (len < 4 && hapd->conf->interworking)
387 len = 4;
c79938a5
JM
388 if (len < 3 && hapd->conf->wnm_sleep_mode)
389 len = 3;
db63757d
PS
390 if (len < 1 && hapd->iconf->obss_interval)
391 len = 1;
b93c8509
JM
392 if (len < 7 && hapd->conf->ssid.utf8_ssid)
393 len = 7;
faecb392
LD
394 if (len < 9 &&
395 (hapd->conf->ftm_initiator || hapd->conf->ftm_responder))
396 len = 9;
b5bf84ba 397#ifdef CONFIG_WNM_AP
0a66ce3c
JM
398 if (len < 4)
399 len = 4;
b5bf84ba 400#endif /* CONFIG_WNM_AP */
3fb17a95
JM
401#ifdef CONFIG_HS20
402 if (hapd->conf->hs20 && len < 6)
403 len = 6;
404#endif /* CONFIG_HS20 */
e5783434
JM
405#ifdef CONFIG_MBO
406 if (hapd->conf->mbo_enabled && len < 6)
407 len = 6;
408#endif /* CONFIG_MBO */
f55acd90
JM
409#ifdef CONFIG_FILS
410 if ((!(hapd->conf->wpa & WPA_PROTO_RSN) ||
411 !wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt)) && len < 10)
412 len = 10;
413#endif /* CONFIG_FILS */
8cd6b7bc
JB
414 if (len < hapd->iface->extended_capa_len)
415 len = hapd->iface->extended_capa_len;
06c4d247
JM
416 if (len == 0)
417 return eid;
418
419 *pos++ = WLAN_EID_EXT_CAPAB;
420 *pos++ = len;
8cd6b7bc
JB
421 for (i = 0; i < len; i++, pos++) {
422 hostapd_ext_capab_byte(hapd, pos, i);
06c4d247 423
8cd6b7bc
JB
424 if (i < hapd->iface->extended_capa_len) {
425 *pos &= ~hapd->iface->extended_capa_mask[i];
426 *pos |= hapd->iface->extended_capa[i];
427 }
428 }
b93c8509 429
3db5439a
JM
430 while (len > 0 && eid[1 + len] == 0) {
431 len--;
432 eid[1] = len;
433 }
434 if (len == 0)
435 return eid;
436
437 return eid + 2 + len;
06c4d247
JM
438}
439
440
c551700f
KP
441u8 * hostapd_eid_qos_map_set(struct hostapd_data *hapd, u8 *eid)
442{
443 u8 *pos = eid;
444 u8 len = hapd->conf->qos_map_set_len;
445
446 if (!len)
447 return eid;
448
449 *pos++ = WLAN_EID_QOS_MAP_SET;
450 *pos++ = len;
451 os_memcpy(pos, hapd->conf->qos_map_set, len);
452 pos += len;
453
454 return pos;
455}
456
457
06c4d247
JM
458u8 * hostapd_eid_interworking(struct hostapd_data *hapd, u8 *eid)
459{
460 u8 *pos = eid;
461#ifdef CONFIG_INTERWORKING
462 u8 *len;
463
464 if (!hapd->conf->interworking)
465 return eid;
466
467 *pos++ = WLAN_EID_INTERWORKING;
468 len = pos++;
469
470 *pos = hapd->conf->access_network_type;
471 if (hapd->conf->internet)
472 *pos |= INTERWORKING_ANO_INTERNET;
473 if (hapd->conf->asra)
474 *pos |= INTERWORKING_ANO_ASRA;
475 if (hapd->conf->esr)
476 *pos |= INTERWORKING_ANO_ESR;
477 if (hapd->conf->uesa)
478 *pos |= INTERWORKING_ANO_UESA;
479 pos++;
480
481 if (hapd->conf->venue_info_set) {
482 *pos++ = hapd->conf->venue_group;
483 *pos++ = hapd->conf->venue_type;
484 }
485
486 if (!is_zero_ether_addr(hapd->conf->hessid)) {
487 os_memcpy(pos, hapd->conf->hessid, ETH_ALEN);
488 pos += ETH_ALEN;
489 }
490
491 *len = pos - len - 1;
492#endif /* CONFIG_INTERWORKING */
493
494 return pos;
495}
c7c178e1
JM
496
497
498u8 * hostapd_eid_adv_proto(struct hostapd_data *hapd, u8 *eid)
499{
500 u8 *pos = eid;
501#ifdef CONFIG_INTERWORKING
502
503 /* TODO: Separate configuration for ANQP? */
504 if (!hapd->conf->interworking)
505 return eid;
506
507 *pos++ = WLAN_EID_ADV_PROTO;
508 *pos++ = 2;
1d21e9dd 509 *pos++ = 0x7F; /* Query Response Length Limit | PAME-BI */
c7c178e1
JM
510 *pos++ = ACCESS_NETWORK_QUERY_PROTOCOL;
511#endif /* CONFIG_INTERWORKING */
512
513 return pos;
514}
4b2a77ab
JM
515
516
517u8 * hostapd_eid_roaming_consortium(struct hostapd_data *hapd, u8 *eid)
518{
519 u8 *pos = eid;
520#ifdef CONFIG_INTERWORKING
521 u8 *len;
522 unsigned int i, count;
523
524 if (!hapd->conf->interworking ||
525 hapd->conf->roaming_consortium == NULL ||
526 hapd->conf->roaming_consortium_count == 0)
527 return eid;
528
529 *pos++ = WLAN_EID_ROAMING_CONSORTIUM;
530 len = pos++;
531
532 /* Number of ANQP OIs (in addition to the max 3 listed here) */
533 if (hapd->conf->roaming_consortium_count > 3 + 255)
534 *pos++ = 255;
535 else if (hapd->conf->roaming_consortium_count > 3)
536 *pos++ = hapd->conf->roaming_consortium_count - 3;
537 else
538 *pos++ = 0;
539
540 /* OU #1 and #2 Lengths */
541 *pos = hapd->conf->roaming_consortium[0].len;
542 if (hapd->conf->roaming_consortium_count > 1)
543 *pos |= hapd->conf->roaming_consortium[1].len << 4;
544 pos++;
545
546 if (hapd->conf->roaming_consortium_count > 3)
547 count = 3;
548 else
549 count = hapd->conf->roaming_consortium_count;
550
551 for (i = 0; i < count; i++) {
552 os_memcpy(pos, hapd->conf->roaming_consortium[i].oi,
553 hapd->conf->roaming_consortium[i].len);
554 pos += hapd->conf->roaming_consortium[i].len;
555 }
556
557 *len = pos - len - 1;
558#endif /* CONFIG_INTERWORKING */
559
560 return pos;
561}
39b97072
JM
562
563
564u8 * hostapd_eid_time_adv(struct hostapd_data *hapd, u8 *eid)
565{
566 if (hapd->conf->time_advertisement != 2)
567 return eid;
568
569 if (hapd->time_adv == NULL &&
570 hostapd_update_time_adv(hapd) < 0)
571 return eid;
572
4c8a333b
JM
573 if (hapd->time_adv == NULL)
574 return eid;
575
39b97072
JM
576 os_memcpy(eid, wpabuf_head(hapd->time_adv),
577 wpabuf_len(hapd->time_adv));
578 eid += wpabuf_len(hapd->time_adv);
579
580 return eid;
581}
582
583
584u8 * hostapd_eid_time_zone(struct hostapd_data *hapd, u8 *eid)
585{
586 size_t len;
587
b375b04b 588 if (hapd->conf->time_advertisement != 2 || !hapd->conf->time_zone)
39b97072
JM
589 return eid;
590
591 len = os_strlen(hapd->conf->time_zone);
592
593 *eid++ = WLAN_EID_TIME_ZONE;
594 *eid++ = len;
595 os_memcpy(eid, hapd->conf->time_zone, len);
596 eid += len;
597
598 return eid;
599}
600
601
602int hostapd_update_time_adv(struct hostapd_data *hapd)
603{
604 const int elen = 2 + 1 + 10 + 5 + 1;
605 struct os_time t;
606 struct os_tm tm;
607 u8 *pos;
608
609 if (hapd->conf->time_advertisement != 2)
610 return 0;
611
612 if (os_get_time(&t) < 0 || os_gmtime(t.sec, &tm) < 0)
613 return -1;
614
615 if (!hapd->time_adv) {
616 hapd->time_adv = wpabuf_alloc(elen);
617 if (hapd->time_adv == NULL)
618 return -1;
619 pos = wpabuf_put(hapd->time_adv, elen);
620 } else
621 pos = wpabuf_mhead_u8(hapd->time_adv);
622
623 *pos++ = WLAN_EID_TIME_ADVERTISEMENT;
624 *pos++ = 1 + 10 + 5 + 1;
625
626 *pos++ = 2; /* UTC time at which the TSF timer is 0 */
627
628 /* Time Value at TSF 0 */
629 /* FIX: need to calculate this based on the current TSF value */
630 WPA_PUT_LE16(pos, tm.year); /* Year */
631 pos += 2;
632 *pos++ = tm.month; /* Month */
633 *pos++ = tm.day; /* Day of month */
634 *pos++ = tm.hour; /* Hours */
635 *pos++ = tm.min; /* Minutes */
636 *pos++ = tm.sec; /* Seconds */
637 WPA_PUT_LE16(pos, 0); /* Milliseconds (not used) */
638 pos += 2;
639 *pos++ = 0; /* Reserved */
640
641 /* Time Error */
642 /* TODO: fill in an estimate on the error */
643 *pos++ = 0;
644 *pos++ = 0;
645 *pos++ = 0;
646 *pos++ = 0;
647 *pos++ = 0;
648
649 *pos++ = hapd->time_update_counter++;
650
651 return 0;
652}
b6668734
JM
653
654
655u8 * hostapd_eid_bss_max_idle_period(struct hostapd_data *hapd, u8 *eid)
656{
657 u8 *pos = eid;
658
b5bf84ba 659#ifdef CONFIG_WNM_AP
b6668734
JM
660 if (hapd->conf->ap_max_inactivity > 0) {
661 unsigned int val;
662 *pos++ = WLAN_EID_BSS_MAX_IDLE_PERIOD;
663 *pos++ = 3;
664 val = hapd->conf->ap_max_inactivity;
665 if (val > 68000)
666 val = 68000;
667 val *= 1000;
668 val /= 1024;
669 if (val == 0)
670 val = 1;
671 if (val > 65535)
672 val = 65535;
673 WPA_PUT_LE16(pos, val);
674 pos += 2;
675 *pos++ = 0x00; /* TODO: Protected Keep-Alive Required */
676 }
b5bf84ba 677#endif /* CONFIG_WNM_AP */
b6668734
JM
678
679 return pos;
680}
fb9a1c3e
AS
681
682
683#ifdef CONFIG_MBO
684
685u8 * hostapd_eid_mbo(struct hostapd_data *hapd, u8 *eid, size_t len)
686{
65833d71 687 u8 mbo[9], *mbo_pos = mbo;
fb9a1c3e
AS
688 u8 *pos = eid;
689
0f0aa2a6
AB
690 if (!hapd->conf->mbo_enabled &&
691 !OCE_STA_CFON_ENABLED(hapd) && !OCE_AP_ENABLED(hapd))
fb9a1c3e
AS
692 return eid;
693
65833d71
AP
694 if (hapd->conf->mbo_enabled) {
695 *mbo_pos++ = MBO_ATTR_ID_AP_CAPA_IND;
696 *mbo_pos++ = 1;
697 /* Not Cellular aware */
698 *mbo_pos++ = 0;
699 }
fb9a1c3e 700
65833d71 701 if (hapd->conf->mbo_enabled && hapd->mbo_assoc_disallow) {
fb9a1c3e
AS
702 *mbo_pos++ = MBO_ATTR_ID_ASSOC_DISALLOW;
703 *mbo_pos++ = 1;
704 *mbo_pos++ = hapd->mbo_assoc_disallow;
705 }
706
0f0aa2a6 707 if (OCE_STA_CFON_ENABLED(hapd) || OCE_AP_ENABLED(hapd)) {
65833d71
AP
708 u8 ctrl;
709
710 ctrl = OCE_RELEASE;
0f0aa2a6 711 if (OCE_STA_CFON_ENABLED(hapd) && !OCE_AP_ENABLED(hapd))
65833d71
AP
712 ctrl |= OCE_IS_STA_CFON;
713
714 *mbo_pos++ = OCE_ATTR_ID_CAPA_IND;
715 *mbo_pos++ = 1;
716 *mbo_pos++ = ctrl;
717 }
718
fb9a1c3e
AS
719 pos += mbo_add_ie(pos, len, mbo, mbo_pos - mbo);
720
721 return pos;
722}
723
724
725u8 hostapd_mbo_ie_len(struct hostapd_data *hapd)
726{
65833d71
AP
727 u8 len;
728
0f0aa2a6
AB
729 if (!hapd->conf->mbo_enabled &&
730 !OCE_STA_CFON_ENABLED(hapd) && !OCE_AP_ENABLED(hapd))
fb9a1c3e
AS
731 return 0;
732
733 /*
734 * MBO IE header (6) + Capability Indication attribute (3) +
735 * Association Disallowed attribute (3) = 12
736 */
65833d71
AP
737 len = 6;
738 if (hapd->conf->mbo_enabled)
739 len += 3 + (hapd->mbo_assoc_disallow ? 3 : 0);
740
741 /* OCE capability indication attribute (3) */
0f0aa2a6 742 if (OCE_STA_CFON_ENABLED(hapd) || OCE_AP_ENABLED(hapd))
65833d71
AP
743 len += 3;
744
745 return len;
fb9a1c3e
AS
746}
747
748#endif /* CONFIG_MBO */
adf0478e
JM
749
750
18e3e9c6
AP
751#ifdef CONFIG_OWE
752static int hostapd_eid_owe_trans_enabled(struct hostapd_data *hapd)
753{
754 return hapd->conf->owe_transition_ssid_len > 0 &&
755 !is_zero_ether_addr(hapd->conf->owe_transition_bssid);
756}
757#endif /* CONFIG_OWE */
758
759
760size_t hostapd_eid_owe_trans_len(struct hostapd_data *hapd)
761{
762#ifdef CONFIG_OWE
763 if (!hostapd_eid_owe_trans_enabled(hapd))
764 return 0;
765 return 6 + ETH_ALEN + 1 + hapd->conf->owe_transition_ssid_len;
766#else /* CONFIG_OWE */
767 return 0;
768#endif /* CONFIG_OWE */
769}
770
771
772u8 * hostapd_eid_owe_trans(struct hostapd_data *hapd, u8 *eid,
773 size_t len)
774{
775#ifdef CONFIG_OWE
776 u8 *pos = eid;
777 size_t elen;
778
779 if (hapd->conf->owe_transition_ifname[0] &&
780 !hostapd_eid_owe_trans_enabled(hapd))
781 hostapd_owe_trans_get_info(hapd);
782
783 if (!hostapd_eid_owe_trans_enabled(hapd))
784 return pos;
785
786 elen = hostapd_eid_owe_trans_len(hapd);
787 if (len < elen) {
788 wpa_printf(MSG_DEBUG,
789 "OWE: Not enough room in the buffer for OWE IE");
790 return pos;
791 }
792
793 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
794 *pos++ = elen - 2;
795 WPA_PUT_BE24(pos, OUI_WFA);
796 pos += 3;
797 *pos++ = OWE_OUI_TYPE;
798 os_memcpy(pos, hapd->conf->owe_transition_bssid, ETH_ALEN);
799 pos += ETH_ALEN;
800 *pos++ = hapd->conf->owe_transition_ssid_len;
801 os_memcpy(pos, hapd->conf->owe_transition_ssid,
802 hapd->conf->owe_transition_ssid_len);
803 pos += hapd->conf->owe_transition_ssid_len;
804
805 return pos;
806#else /* CONFIG_OWE */
807 return eid;
808#endif /* CONFIG_OWE */
809}
810
811
adf0478e
JM
812void ap_copy_sta_supp_op_classes(struct sta_info *sta,
813 const u8 *supp_op_classes,
814 size_t supp_op_classes_len)
815{
816 if (!supp_op_classes)
817 return;
818 os_free(sta->supp_op_classes);
819 sta->supp_op_classes = os_malloc(1 + supp_op_classes_len);
820 if (!sta->supp_op_classes)
821 return;
822
823 sta->supp_op_classes[0] = supp_op_classes_len;
824 os_memcpy(sta->supp_op_classes + 1, supp_op_classes,
825 supp_op_classes_len);
826}
198a942c
JM
827
828
829u8 * hostapd_eid_fils_indic(struct hostapd_data *hapd, u8 *eid, int hessid)
830{
831 u8 *pos = eid;
832#ifdef CONFIG_FILS
833 u8 *len;
834 u16 fils_info = 0;
26bf70e3
JM
835 size_t realms;
836 struct fils_realm *realm;
198a942c
JM
837
838 if (!(hapd->conf->wpa & WPA_PROTO_RSN) ||
839 !wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt))
840 return pos;
841
26bf70e3
JM
842 realms = dl_list_len(&hapd->conf->fils_realms);
843 if (realms > 7)
844 realms = 7; /* 3 bit count field limits this to max 7 */
845
198a942c
JM
846 *pos++ = WLAN_EID_FILS_INDICATION;
847 len = pos++;
848 /* TODO: B0..B2: Number of Public Key Identifiers */
94f66e8a 849 if (hapd->conf->erp_domain) {
94f66e8a 850 /* B3..B5: Number of Realm Identifiers */
26bf70e3 851 fils_info |= realms << 3;
94f66e8a 852 }
198a942c
JM
853 /* TODO: B6: FILS IP Address Configuration */
854 if (hapd->conf->fils_cache_id_set)
855 fils_info |= BIT(7);
856 if (hessid && !is_zero_ether_addr(hapd->conf->hessid))
857 fils_info |= BIT(8); /* HESSID Included */
858 /* FILS Shared Key Authentication without PFS Supported */
859 fils_info |= BIT(9);
1764559e
JM
860 if (hapd->conf->fils_dh_group) {
861 /* FILS Shared Key Authentication with PFS Supported */
862 fils_info |= BIT(10);
863 }
198a942c
JM
864 /* TODO: B11: FILS Public Key Authentication Supported */
865 /* B12..B15: Reserved */
866 WPA_PUT_LE16(pos, fils_info);
867 pos += 2;
868 if (hapd->conf->fils_cache_id_set) {
869 os_memcpy(pos, hapd->conf->fils_cache_id, FILS_CACHE_ID_LEN);
870 pos += FILS_CACHE_ID_LEN;
871 }
872 if (hessid && !is_zero_ether_addr(hapd->conf->hessid)) {
873 os_memcpy(pos, hapd->conf->hessid, ETH_ALEN);
874 pos += ETH_ALEN;
875 }
26bf70e3
JM
876
877 dl_list_for_each(realm, &hapd->conf->fils_realms, struct fils_realm,
878 list) {
879 if (realms == 0)
880 break;
881 realms--;
882 os_memcpy(pos, realm->hash, 2);
94f66e8a
JM
883 pos += 2;
884 }
198a942c
JM
885 *len = pos - len - 1;
886#endif /* CONFIG_FILS */
887
888 return pos;
889}
1034f67b
MV
890
891
892#ifdef CONFIG_OCV
893int get_tx_parameters(struct sta_info *sta, int ap_max_chanwidth,
894 int ap_seg1_idx, int *bandwidth, int *seg1_idx)
895{
896 int ht_40mhz = 0;
897 int vht_80p80 = 0;
898 int requested_bw;
899
900 if (sta->ht_capabilities)
901 ht_40mhz = !!(sta->ht_capabilities->ht_capabilities_info &
902 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET);
903
904 if (sta->vht_operation) {
905 struct ieee80211_vht_operation *oper = sta->vht_operation;
906
907 /*
908 * If a VHT Operation element was present, use it to determine
909 * the supported channel bandwidth.
910 */
911 if (oper->vht_op_info_chwidth == 0) {
912 requested_bw = ht_40mhz ? 40 : 20;
913 } else if (oper->vht_op_info_chan_center_freq_seg1_idx == 0) {
914 requested_bw = 80;
915 } else {
916 int diff;
917
918 requested_bw = 160;
919 diff = abs((int)
920 oper->vht_op_info_chan_center_freq_seg0_idx -
921 (int)
922 oper->vht_op_info_chan_center_freq_seg1_idx);
923 vht_80p80 = oper->vht_op_info_chan_center_freq_seg1_idx
924 != 0 && diff > 16;
925 }
926 } else if (sta->vht_capabilities) {
927 struct ieee80211_vht_capabilities *capab;
928 int vht_chanwidth;
929
930 capab = sta->vht_capabilities;
931
932 /*
933 * If only the VHT Capabilities element is present (e.g., for
934 * normal clients), use it to determine the supported channel
935 * bandwidth.
936 */
937 vht_chanwidth = capab->vht_capabilities_info &
938 VHT_CAP_SUPP_CHAN_WIDTH_MASK;
939 vht_80p80 = capab->vht_capabilities_info &
940 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
941
942 /* TODO: Also take into account Extended NSS BW Support field */
943 requested_bw = vht_chanwidth ? 160 : 80;
944 } else {
945 requested_bw = ht_40mhz ? 40 : 20;
946 }
947
948 *bandwidth = requested_bw < ap_max_chanwidth ?
949 requested_bw : ap_max_chanwidth;
950
951 *seg1_idx = 0;
952 if (ap_seg1_idx && vht_80p80)
953 *seg1_idx = ap_seg1_idx;
954
955 return 0;
956}
957#endif /* CONFIG_OCV */