]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/wpa_auth_glue.c
Allow RSNE in EAPOL-Key msg 3/4 to be replaced for testing purposes
[thirdparty/hostap.git] / src / ap / wpa_auth_glue.c
CommitLineData
c442055e
JM
1/*
2 * hostapd / WPA authenticator glue code
47b55a3e 3 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
c442055e 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
c442055e
JM
7 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
c5fee160
MB
12#include "utils/eloop.h"
13#include "utils/list.h"
c442055e 14#include "common/ieee802_11_defs.h"
47b55a3e 15#include "common/sae.h"
2c502460 16#include "common/wpa_ctrl.h"
f8e09bc5 17#include "crypto/sha1.h"
c442055e
JM
18#include "eapol_auth/eapol_auth_sm.h"
19#include "eapol_auth/eapol_auth_sm_i.h"
20#include "eap_server/eap.h"
21#include "l2_packet/l2_packet.h"
50bd8e0a 22#include "eth_p_oui.h"
6226e38d
JM
23#include "hostapd.h"
24#include "ieee802_1x.h"
25#include "preauth_auth.h"
26#include "sta_info.h"
27#include "tkip_countermeasures.h"
8b06c1ed
JM
28#include "ap_drv_ops.h"
29#include "ap_config.h"
1034f67b 30#include "ieee802_11.h"
d90f10fa 31#include "pmksa_cache_auth.h"
8b06c1ed 32#include "wpa_auth.h"
373f6c72 33#include "wpa_auth_glue.h"
c442055e
JM
34
35
36static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
7af092a0 37 struct hostapd_config *iconf,
c442055e
JM
38 struct wpa_auth_config *wconf)
39{
e36a5894
JM
40 int sae_pw_id;
41
d28b43f6 42 os_memset(wconf, 0, sizeof(*wconf));
c442055e
JM
43 wconf->wpa = conf->wpa;
44 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
45 wconf->wpa_pairwise = conf->wpa_pairwise;
46 wconf->wpa_group = conf->wpa_group;
47 wconf->wpa_group_rekey = conf->wpa_group_rekey;
48 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
49 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
50 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
41f140d3 51 wconf->wpa_group_update_count = conf->wpa_group_update_count;
6f234c1e
JM
52 wconf->wpa_disable_eapol_key_retries =
53 conf->wpa_disable_eapol_key_retries;
41f140d3 54 wconf->wpa_pairwise_update_count = conf->wpa_pairwise_update_count;
c442055e
JM
55 wconf->rsn_pairwise = conf->rsn_pairwise;
56 wconf->rsn_preauth = conf->rsn_preauth;
57 wconf->eapol_version = conf->eapol_version;
524dc5bf
JM
58#ifdef CONFIG_MACSEC
59 if (wconf->eapol_version > 2)
60 wconf->eapol_version = 2;
61#endif /* CONFIG_MACSEC */
c442055e 62 wconf->wmm_enabled = conf->wmm_enabled;
721abef9 63 wconf->wmm_uapsd = conf->wmm_uapsd;
cb465555 64 wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
875ab60d
MV
65#ifdef CONFIG_OCV
66 wconf->ocv = conf->ocv;
67#endif /* CONFIG_OCV */
c442055e 68 wconf->okc = conf->okc;
c442055e 69 wconf->ieee80211w = conf->ieee80211w;
92d407db 70 wconf->beacon_prot = conf->beacon_prot;
8dd9f9cd 71 wconf->group_mgmt_cipher = conf->group_mgmt_cipher;
ba3d435f 72 wconf->sae_require_mfp = conf->sae_require_mfp;
4ec1fd8e 73#ifdef CONFIG_IEEE80211R_AP
c442055e 74 wconf->ssid_len = conf->ssid.ssid_len;
d6c3067d
JM
75 if (wconf->ssid_len > SSID_MAX_LEN)
76 wconf->ssid_len = SSID_MAX_LEN;
c442055e
JM
77 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
78 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
79 MOBILITY_DOMAIN_ID_LEN);
80 if (conf->nas_identifier &&
81 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
82 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
83 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
84 wconf->r0_key_holder_len);
85 }
86 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
87 wconf->r0_key_lifetime = conf->r0_key_lifetime;
3a3e2832 88 wconf->r1_max_key_lifetime = conf->r1_max_key_lifetime;
c442055e 89 wconf->reassociation_deadline = conf->reassociation_deadline;
3a46cf93
MB
90 wconf->rkh_pos_timeout = conf->rkh_pos_timeout;
91 wconf->rkh_neg_timeout = conf->rkh_neg_timeout;
92 wconf->rkh_pull_timeout = conf->rkh_pull_timeout;
93 wconf->rkh_pull_retries = conf->rkh_pull_retries;
94 wconf->r0kh_list = &conf->r0kh_list;
95 wconf->r1kh_list = &conf->r1kh_list;
c442055e 96 wconf->pmk_r1_push = conf->pmk_r1_push;
d7956add 97 wconf->ft_over_ds = conf->ft_over_ds;
96590564 98 wconf->ft_psk_generate_local = conf->ft_psk_generate_local;
4ec1fd8e 99#endif /* CONFIG_IEEE80211R_AP */
83421850
JM
100#ifdef CONFIG_HS20
101 wconf->disable_gtk = conf->disable_dgaf;
a14896e8
JM
102 if (conf->osen) {
103 wconf->disable_gtk = 1;
104 wconf->wpa = WPA_PROTO_OSEN;
105 wconf->wpa_key_mgmt = WPA_KEY_MGMT_OSEN;
106 wconf->wpa_pairwise = 0;
107 wconf->wpa_group = WPA_CIPHER_CCMP;
108 wconf->rsn_pairwise = WPA_CIPHER_CCMP;
109 wconf->rsn_preauth = 0;
110 wconf->disable_pmksa_caching = 1;
a14896e8 111 wconf->ieee80211w = 1;
a14896e8 112 }
83421850 113#endif /* CONFIG_HS20 */
7af092a0
JB
114#ifdef CONFIG_TESTING_OPTIONS
115 wconf->corrupt_gtk_rekey_mic_probability =
116 iconf->corrupt_gtk_rekey_mic_probability;
bc02843e
JM
117 if (conf->own_ie_override &&
118 wpabuf_len(conf->own_ie_override) <= MAX_OWN_IE_OVERRIDE) {
119 wconf->own_ie_override_len = wpabuf_len(conf->own_ie_override);
120 os_memcpy(wconf->own_ie_override,
121 wpabuf_head(conf->own_ie_override),
122 wconf->own_ie_override_len);
123 }
4d64fd37
JM
124 if (conf->rsne_override_eapol &&
125 wpabuf_len(conf->rsne_override_eapol) <= MAX_OWN_IE_OVERRIDE) {
126 wconf->rsne_override_eapol_set = 1;
127 wconf->rsne_override_eapol_len =
128 wpabuf_len(conf->rsne_override_eapol);
129 os_memcpy(wconf->rsne_override_eapol,
130 wpabuf_head(conf->rsne_override_eapol),
131 wconf->rsne_override_eapol_len);
132 }
f2c4b44b
JM
133 if (conf->rsnxe_override_eapol &&
134 wpabuf_len(conf->rsnxe_override_eapol) <= MAX_OWN_IE_OVERRIDE) {
9128b672 135 wconf->rsnxe_override_eapol_set = 1;
f2c4b44b
JM
136 wconf->rsnxe_override_eapol_len =
137 wpabuf_len(conf->rsnxe_override_eapol);
138 os_memcpy(wconf->rsnxe_override_eapol,
139 wpabuf_head(conf->rsnxe_override_eapol),
140 wconf->rsnxe_override_eapol_len);
141 }
8d84c75f
JM
142 if (conf->gtk_rsc_override &&
143 wpabuf_len(conf->gtk_rsc_override) > 0 &&
144 wpabuf_len(conf->gtk_rsc_override) <= WPA_KEY_RSC_LEN) {
145 os_memcpy(wconf->gtk_rsc_override,
146 wpabuf_head(conf->gtk_rsc_override),
147 wpabuf_len(conf->gtk_rsc_override));
148 wconf->gtk_rsc_override_set = 1;
149 }
150 if (conf->igtk_rsc_override &&
151 wpabuf_len(conf->igtk_rsc_override) > 0 &&
152 wpabuf_len(conf->igtk_rsc_override) <= WPA_KEY_RSC_LEN) {
153 os_memcpy(wconf->igtk_rsc_override,
154 wpabuf_head(conf->igtk_rsc_override),
155 wpabuf_len(conf->igtk_rsc_override));
156 wconf->igtk_rsc_override_set = 1;
157 }
7af092a0 158#endif /* CONFIG_TESTING_OPTIONS */
25ef8529
JM
159#ifdef CONFIG_P2P
160 os_memcpy(wconf->ip_addr_go, conf->ip_addr_go, 4);
161 os_memcpy(wconf->ip_addr_mask, conf->ip_addr_mask, 4);
162 os_memcpy(wconf->ip_addr_start, conf->ip_addr_start, 4);
163 os_memcpy(wconf->ip_addr_end, conf->ip_addr_end, 4);
164#endif /* CONFIG_P2P */
7eace378
JM
165#ifdef CONFIG_FILS
166 wconf->fils_cache_id_set = conf->fils_cache_id_set;
167 os_memcpy(wconf->fils_cache_id, conf->fils_cache_id,
168 FILS_CACHE_ID_LEN);
169#endif /* CONFIG_FILS */
a36e13a7 170 wconf->sae_pwe = conf->sae_pwe;
e36a5894 171 sae_pw_id = hostapd_sae_pw_id_in_use(conf);
641d79f1 172 if (sae_pw_id == 2 && wconf->sae_pwe != 3)
e36a5894
JM
173 wconf->sae_pwe = 1;
174 else if (sae_pw_id == 1 && wconf->sae_pwe == 0)
175 wconf->sae_pwe = 2;
65a44e84
JM
176#ifdef CONFIG_OWE
177 wconf->owe_ptk_workaround = conf->owe_ptk_workaround;
178#endif /* CONFIG_OWE */
c442055e
JM
179}
180
181
182static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
183 logger_level level, const char *txt)
184{
185#ifndef CONFIG_NO_HOSTAPD_LOGGER
186 struct hostapd_data *hapd = ctx;
187 int hlevel;
188
189 switch (level) {
190 case LOGGER_WARNING:
191 hlevel = HOSTAPD_LEVEL_WARNING;
192 break;
193 case LOGGER_INFO:
194 hlevel = HOSTAPD_LEVEL_INFO;
195 break;
196 case LOGGER_DEBUG:
197 default:
198 hlevel = HOSTAPD_LEVEL_DEBUG;
199 break;
200 }
201
202 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
203#endif /* CONFIG_NO_HOSTAPD_LOGGER */
204}
205
206
207static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
208 u16 reason)
209{
210 struct hostapd_data *hapd = ctx;
211 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
212 "STA " MACSTR " reason %d",
213 __func__, MAC2STR(addr), reason);
214 ap_sta_disconnect(hapd, NULL, addr, reason);
215}
216
217
c772d054 218static int hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
c442055e
JM
219{
220 struct hostapd_data *hapd = ctx;
c772d054 221 return michael_mic_failure(hapd, addr, 0);
c442055e
JM
222}
223
224
2c502460
JM
225static void hostapd_wpa_auth_psk_failure_report(void *ctx, const u8 *addr)
226{
227 struct hostapd_data *hapd = ctx;
228 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POSSIBLE_PSK_MISMATCH MACSTR,
229 MAC2STR(addr));
230}
231
232
c442055e
JM
233static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
234 wpa_eapol_variable var, int value)
235{
236 struct hostapd_data *hapd = ctx;
237 struct sta_info *sta = ap_get_sta(hapd, addr);
238 if (sta == NULL)
239 return;
240 switch (var) {
241 case WPA_EAPOL_portEnabled:
242 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
243 break;
244 case WPA_EAPOL_portValid:
245 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
246 break;
247 case WPA_EAPOL_authorized:
248 ieee802_1x_set_sta_authorized(hapd, sta, value);
249 break;
250 case WPA_EAPOL_portControl_Auto:
251 if (sta->eapol_sm)
252 sta->eapol_sm->portControl = Auto;
253 break;
254 case WPA_EAPOL_keyRun:
255 if (sta->eapol_sm)
256 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
257 break;
258 case WPA_EAPOL_keyAvailable:
259 if (sta->eapol_sm)
260 sta->eapol_sm->eap_if->eapKeyAvailable =
261 value ? TRUE : FALSE;
262 break;
263 case WPA_EAPOL_keyDone:
264 if (sta->eapol_sm)
265 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
266 break;
267 case WPA_EAPOL_inc_EapolFramesTx:
268 if (sta->eapol_sm)
269 sta->eapol_sm->dot1xAuthEapolFramesTx++;
270 break;
271 }
272}
273
274
275static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
276 wpa_eapol_variable var)
277{
278 struct hostapd_data *hapd = ctx;
279 struct sta_info *sta = ap_get_sta(hapd, addr);
280 if (sta == NULL || sta->eapol_sm == NULL)
281 return -1;
282 switch (var) {
283 case WPA_EAPOL_keyRun:
284 return sta->eapol_sm->keyRun;
285 case WPA_EAPOL_keyAvailable:
286 return sta->eapol_sm->eap_if->eapKeyAvailable;
287 default:
288 return -1;
289 }
290}
291
292
293static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
759fd76b 294 const u8 *p2p_dev_addr,
dbfa691d
JM
295 const u8 *prev_psk, size_t *psk_len,
296 int *vlan_id)
c442055e
JM
297{
298 struct hostapd_data *hapd = ctx;
05ab9712 299 struct sta_info *sta = ap_get_sta(hapd, addr);
47b55a3e
JM
300 const u8 *psk;
301
dbfa691d
JM
302 if (vlan_id)
303 *vlan_id = 0;
7a12edd1
JM
304 if (psk_len)
305 *psk_len = PMK_LEN;
306
47b55a3e
JM
307#ifdef CONFIG_SAE
308 if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
309 if (!sta->sae || prev_psk)
310 return NULL;
311 return sta->sae->pmk;
312 }
e61fea6b
JM
313 if (sta && wpa_auth_uses_sae(sta->wpa_sm)) {
314 wpa_printf(MSG_DEBUG,
315 "No PSK for STA trying to use SAE with PMKSA caching");
316 return NULL;
317 }
47b55a3e
JM
318#endif /* CONFIG_SAE */
319
09368515
JM
320#ifdef CONFIG_OWE
321 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
7a12edd1
JM
322 sta && sta->owe_pmk) {
323 if (psk_len)
324 *psk_len = sta->owe_pmk_len;
09368515 325 return sta->owe_pmk;
7a12edd1 326 }
d90f10fa
JM
327 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) && sta) {
328 struct rsn_pmksa_cache_entry *sa;
329
330 sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
331 if (sa && sa->akmp == WPA_KEY_MGMT_OWE) {
332 if (psk_len)
333 *psk_len = sa->pmk_len;
334 return sa->pmk;
335 }
336 }
09368515
JM
337#endif /* CONFIG_OWE */
338
dbfa691d
JM
339 psk = hostapd_get_psk(hapd->conf, addr, p2p_dev_addr, prev_psk,
340 vlan_id);
59dff51d
MB
341 /*
342 * This is about to iterate over all psks, prev_psk gives the last
343 * returned psk which should not be returned again.
5ee56c4e 344 * logic list (all hostapd_get_psk; all sta->psk)
59dff51d 345 */
5ee56c4e
MB
346 if (sta && sta->psk && !psk) {
347 struct hostapd_sta_wpa_psk_short *pos;
dbfa691d
JM
348
349 if (vlan_id)
350 *vlan_id = 0;
5ee56c4e
MB
351 psk = sta->psk->psk;
352 for (pos = sta->psk; pos; pos = pos->next) {
f8e09bc5
MB
353 if (pos->is_passphrase) {
354 pbkdf2_sha1(pos->passphrase,
355 hapd->conf->ssid.ssid,
356 hapd->conf->ssid.ssid_len, 4096,
357 pos->psk, PMK_LEN);
358 pos->is_passphrase = 0;
359 }
5ee56c4e
MB
360 if (pos->psk == prev_psk) {
361 psk = pos->next ? pos->next->psk : NULL;
362 break;
363 }
364 }
365 }
59dff51d 366 return psk;
c442055e
JM
367}
368
369
370static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
371 size_t *len)
372{
373 struct hostapd_data *hapd = ctx;
374 const u8 *key;
375 size_t keylen;
376 struct sta_info *sta;
377
378 sta = ap_get_sta(hapd, addr);
400de9b1
BG
379 if (sta == NULL) {
380 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Cannot find STA");
c442055e 381 return -1;
400de9b1 382 }
c442055e
JM
383
384 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
400de9b1
BG
385 if (key == NULL) {
386 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Key is null, eapol_sm: %p",
387 sta->eapol_sm);
c442055e 388 return -1;
400de9b1 389 }
c442055e
JM
390
391 if (keylen > *len)
392 keylen = *len;
393 os_memcpy(msk, key, keylen);
394 *len = keylen;
395
396 return 0;
397}
398
399
71934751 400static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
c442055e 401 const u8 *addr, int idx, u8 *key,
a919a260 402 size_t key_len, enum key_flag key_flag)
c442055e
JM
403{
404 struct hostapd_data *hapd = ctx;
405 const char *ifname = hapd->conf->iface;
406
407 if (vlan_id > 0) {
408 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
99d8c4dc
GG
409 if (!ifname) {
410 if (!(hapd->iface->drv_flags &
411 WPA_DRIVER_FLAGS_VLAN_OFFLOAD))
412 return -1;
413 ifname = hapd->conf->iface;
414 }
c442055e
JM
415 }
416
16579769
JM
417#ifdef CONFIG_TESTING_OPTIONS
418 if (addr && !is_broadcast_ether_addr(addr)) {
419 struct sta_info *sta;
420
421 sta = ap_get_sta(hapd, addr);
422 if (sta) {
423 sta->last_tk_alg = alg;
424 sta->last_tk_key_idx = idx;
425 if (key)
426 os_memcpy(sta->last_tk, key, key_len);
427 sta->last_tk_len = key_len;
428 }
f8bfc9cb
PK
429 } else if (alg == WPA_ALG_IGTK ||
430 alg == WPA_ALG_BIP_GMAC_128 ||
431 alg == WPA_ALG_BIP_GMAC_256 ||
432 alg == WPA_ALG_BIP_CMAC_256) {
16579769
JM
433 hapd->last_igtk_alg = alg;
434 hapd->last_igtk_key_idx = idx;
435 if (key)
436 os_memcpy(hapd->last_igtk, key, key_len);
437 hapd->last_igtk_len = key_len;
16579769
JM
438 } else {
439 hapd->last_gtk_alg = alg;
440 hapd->last_gtk_key_idx = idx;
441 if (key)
442 os_memcpy(hapd->last_gtk, key, key_len);
443 hapd->last_gtk_len = key_len;
444 }
445#endif /* CONFIG_TESTING_OPTIONS */
4d3ae54f 446 return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, vlan_id, 1,
a919a260 447 NULL, 0, key, key_len, key_flag);
c442055e
JM
448}
449
450
451static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
452 u8 *seq)
453{
454 struct hostapd_data *hapd = ctx;
455 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
456}
457
458
459static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
460 const u8 *data, size_t data_len,
461 int encrypt)
462{
463 struct hostapd_data *hapd = ctx;
4378fc14
FF
464 struct sta_info *sta;
465 u32 flags = 0;
466
9d4ff04a
JM
467#ifdef CONFIG_TESTING_OPTIONS
468 if (hapd->ext_eapol_frame_io) {
469 size_t hex_len = 2 * data_len + 1;
470 char *hex = os_malloc(hex_len);
471
472 if (hex == NULL)
473 return -1;
474 wpa_snprintf_hex(hex, hex_len, data, data_len);
475 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
476 MAC2STR(addr), hex);
477 os_free(hex);
478 return 0;
479 }
480#endif /* CONFIG_TESTING_OPTIONS */
481
4378fc14
FF
482 sta = ap_get_sta(hapd, addr);
483 if (sta)
484 flags = hostapd_sta_flags_to_drv(sta->flags);
485
3acdf771 486 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
4378fc14 487 encrypt, flags);
c442055e
JM
488}
489
490
491static int hostapd_wpa_auth_for_each_sta(
492 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
493 void *cb_ctx)
494{
495 struct hostapd_data *hapd = ctx;
496 struct sta_info *sta;
497
498 for (sta = hapd->sta_list; sta; sta = sta->next) {
499 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
500 return 1;
501 }
502 return 0;
503}
504
505
506struct wpa_auth_iface_iter_data {
507 int (*cb)(struct wpa_authenticator *sm, void *ctx);
508 void *cb_ctx;
509};
510
511static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
512{
513 struct wpa_auth_iface_iter_data *data = ctx;
514 size_t i;
515 for (i = 0; i < iface->num_bss; i++) {
03bcb0af
JM
516 if (iface->bss[i]->wpa_auth &&
517 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
c442055e
JM
518 return 1;
519 }
520 return 0;
521}
522
523
524static int hostapd_wpa_auth_for_each_auth(
525 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
526 void *cb_ctx)
527{
528 struct hostapd_data *hapd = ctx;
529 struct wpa_auth_iface_iter_data data;
3776ac73
JM
530 if (hapd->iface->interfaces == NULL ||
531 hapd->iface->interfaces->for_each_interface == NULL)
1b56c26c 532 return -1;
c442055e
JM
533 data.cb = cb;
534 data.cb_ctx = cb_ctx;
3776ac73
JM
535 return hapd->iface->interfaces->for_each_interface(
536 hapd->iface->interfaces, wpa_auth_iface_iter, &data);
c442055e
JM
537}
538
539
4ec1fd8e 540#ifdef CONFIG_IEEE80211R_AP
67ccef7e 541
c5fee160
MB
542struct wpa_ft_rrb_rx_later_data {
543 struct dl_list list;
544 u8 addr[ETH_ALEN];
545 size_t data_len;
546 /* followed by data_len octets of data */
547};
548
549static void hostapd_wpa_ft_rrb_rx_later(void *eloop_ctx, void *timeout_ctx)
550{
551 struct hostapd_data *hapd = eloop_ctx;
552 struct wpa_ft_rrb_rx_later_data *data, *n;
553
554 dl_list_for_each_safe(data, n, &hapd->l2_queue,
555 struct wpa_ft_rrb_rx_later_data, list) {
556 if (hapd->wpa_auth) {
557 wpa_ft_rrb_rx(hapd->wpa_auth, data->addr,
558 (const u8 *) (data + 1),
559 data->data_len);
560 }
561 dl_list_del(&data->list);
562 os_free(data);
563 }
564}
565
566
67ccef7e
JM
567struct wpa_auth_ft_iface_iter_data {
568 struct hostapd_data *src_hapd;
569 const u8 *dst;
570 const u8 *data;
571 size_t data_len;
572};
573
574
575static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
576{
577 struct wpa_auth_ft_iface_iter_data *idata = ctx;
c5fee160 578 struct wpa_ft_rrb_rx_later_data *data;
67ccef7e
JM
579 struct hostapd_data *hapd;
580 size_t j;
581
582 for (j = 0; j < iface->num_bss; j++) {
583 hapd = iface->bss[j];
c5fee160
MB
584 if (hapd == idata->src_hapd ||
585 !hapd->wpa_auth ||
586 os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) != 0)
71456dbd 587 continue;
c5fee160
MB
588
589 wpa_printf(MSG_DEBUG,
590 "FT: Send RRB data directly to locally managed BSS "
591 MACSTR "@%s -> " MACSTR "@%s",
592 MAC2STR(idata->src_hapd->own_addr),
593 idata->src_hapd->conf->iface,
594 MAC2STR(hapd->own_addr), hapd->conf->iface);
595
596 /* Defer wpa_ft_rrb_rx() until next eloop step as this is
597 * when it would be triggered when reading from a socket.
598 * This avoids
599 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
600 * that is calling hapd0:recv handler from within
601 * hapd0:send directly.
602 */
603 data = os_zalloc(sizeof(*data) + idata->data_len);
604 if (!data)
67ccef7e 605 return 1;
c5fee160
MB
606
607 os_memcpy(data->addr, idata->src_hapd->own_addr, ETH_ALEN);
608 os_memcpy(data + 1, idata->data, idata->data_len);
609 data->data_len = idata->data_len;
610
611 dl_list_add(&hapd->l2_queue, &data->list);
612
613 if (!eloop_is_timeout_registered(hostapd_wpa_ft_rrb_rx_later,
614 hapd, NULL))
615 eloop_register_timeout(0, 0,
616 hostapd_wpa_ft_rrb_rx_later,
617 hapd, NULL);
618
619 return 1;
67ccef7e
JM
620 }
621
622 return 0;
623}
624
4ec1fd8e 625#endif /* CONFIG_IEEE80211R_AP */
67ccef7e
JM
626
627
c442055e
JM
628static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
629 const u8 *data, size_t data_len)
630{
631 struct hostapd_data *hapd = ctx;
7e031850
JM
632 struct l2_ethhdr *buf;
633 int ret;
c442055e 634
9d4ff04a
JM
635#ifdef CONFIG_TESTING_OPTIONS
636 if (hapd->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
637 size_t hex_len = 2 * data_len + 1;
638 char *hex = os_malloc(hex_len);
639
640 if (hex == NULL)
641 return -1;
642 wpa_snprintf_hex(hex, hex_len, data, data_len);
643 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
644 MAC2STR(dst), hex);
645 os_free(hex);
646 return 0;
647 }
648#endif /* CONFIG_TESTING_OPTIONS */
649
4ec1fd8e 650#ifdef CONFIG_IEEE80211R_AP
3776ac73
JM
651 if (proto == ETH_P_RRB && hapd->iface->interfaces &&
652 hapd->iface->interfaces->for_each_interface) {
67ccef7e
JM
653 int res;
654 struct wpa_auth_ft_iface_iter_data idata;
655 idata.src_hapd = hapd;
656 idata.dst = dst;
657 idata.data = data;
658 idata.data_len = data_len;
3776ac73
JM
659 res = hapd->iface->interfaces->for_each_interface(
660 hapd->iface->interfaces, hostapd_wpa_auth_ft_iter,
661 &idata);
67ccef7e
JM
662 if (res == 1)
663 return data_len;
664 }
4ec1fd8e 665#endif /* CONFIG_IEEE80211R_AP */
67ccef7e 666
c442055e
JM
667 if (hapd->l2 == NULL)
668 return -1;
7e031850
JM
669
670 buf = os_malloc(sizeof(*buf) + data_len);
671 if (buf == NULL)
672 return -1;
673 os_memcpy(buf->h_dest, dst, ETH_ALEN);
674 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
675 buf->h_proto = host_to_be16(proto);
676 os_memcpy(buf + 1, data, data_len);
677 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
678 sizeof(*buf) + data_len);
679 os_free(buf);
2683690d 680 return ret;
c442055e
JM
681}
682
683
50bd8e0a
MB
684#ifdef CONFIG_ETH_P_OUI
685static struct eth_p_oui_ctx * hostapd_wpa_get_oui(struct hostapd_data *hapd,
686 u8 oui_suffix)
687{
688 switch (oui_suffix) {
689#ifdef CONFIG_IEEE80211R_AP
690 case FT_PACKET_R0KH_R1KH_PULL:
691 return hapd->oui_pull;
692 case FT_PACKET_R0KH_R1KH_RESP:
693 return hapd->oui_resp;
694 case FT_PACKET_R0KH_R1KH_PUSH:
695 return hapd->oui_push;
eefe8630
MB
696 case FT_PACKET_R0KH_R1KH_SEQ_REQ:
697 return hapd->oui_sreq;
698 case FT_PACKET_R0KH_R1KH_SEQ_RESP:
699 return hapd->oui_sresp;
50bd8e0a
MB
700#endif /* CONFIG_IEEE80211R_AP */
701 default:
702 return NULL;
703 }
704}
705#endif /* CONFIG_ETH_P_OUI */
706
707
708#ifdef CONFIG_IEEE80211R_AP
709
710struct oui_deliver_later_data {
711 struct dl_list list;
712 u8 src_addr[ETH_ALEN];
713 u8 dst_addr[ETH_ALEN];
714 size_t data_len;
715 u8 oui_suffix;
716 /* followed by data_len octets of data */
717};
718
719static void hostapd_oui_deliver_later(void *eloop_ctx, void *timeout_ctx)
720{
721 struct hostapd_data *hapd = eloop_ctx;
722 struct oui_deliver_later_data *data, *n;
723 struct eth_p_oui_ctx *oui_ctx;
724
725 dl_list_for_each_safe(data, n, &hapd->l2_oui_queue,
726 struct oui_deliver_later_data, list) {
727 oui_ctx = hostapd_wpa_get_oui(hapd, data->oui_suffix);
a422d9b4
JM
728 wpa_printf(MSG_DEBUG, "RRB(%s): %s src=" MACSTR " dst=" MACSTR
729 " oui_suffix=%u data_len=%u data=%p",
730 hapd->conf->iface, __func__,
731 MAC2STR(data->src_addr), MAC2STR(data->dst_addr),
732 data->oui_suffix, (unsigned int) data->data_len,
733 data);
50bd8e0a
MB
734 if (hapd->wpa_auth && oui_ctx) {
735 eth_p_oui_deliver(oui_ctx, data->src_addr,
736 data->dst_addr,
737 (const u8 *) (data + 1),
738 data->data_len);
739 }
740 dl_list_del(&data->list);
741 os_free(data);
742 }
743}
744
745
746struct wpa_auth_oui_iface_iter_data {
747 struct hostapd_data *src_hapd;
748 const u8 *dst_addr;
749 const u8 *data;
750 size_t data_len;
751 u8 oui_suffix;
752};
753
754static int hostapd_wpa_auth_oui_iter(struct hostapd_iface *iface, void *ctx)
755{
756 struct wpa_auth_oui_iface_iter_data *idata = ctx;
757 struct oui_deliver_later_data *data;
3cd4db23 758 struct hostapd_data *hapd, *src_hapd = idata->src_hapd;
50bd8e0a
MB
759 size_t j;
760
761 for (j = 0; j < iface->num_bss; j++) {
762 hapd = iface->bss[j];
3cd4db23
JM
763 if (hapd == src_hapd)
764 continue; /* don't deliver back to same interface */
765 if (!wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) ||
766 hapd->conf->ssid.ssid_len !=
767 src_hapd->conf->ssid.ssid_len ||
768 os_memcmp(hapd->conf->ssid.ssid,
769 src_hapd->conf->ssid.ssid,
770 hapd->conf->ssid.ssid_len) != 0 ||
771 os_memcmp(hapd->conf->mobility_domain,
772 src_hapd->conf->mobility_domain,
c133c785 773 MOBILITY_DOMAIN_ID_LEN) != 0)
3cd4db23 774 continue; /* no matching FT SSID/mobility domain */
50bd8e0a
MB
775 if (!is_multicast_ether_addr(idata->dst_addr) &&
776 os_memcmp(hapd->own_addr, idata->dst_addr, ETH_ALEN) != 0)
3cd4db23 777 continue; /* destination address does not match */
50bd8e0a
MB
778
779 /* defer eth_p_oui_deliver until next eloop step as this is
780 * when it would be triggerd from reading from sock
781 * This avoids
782 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
783 * that is calling hapd0:recv handler from within
784 * hapd0:send directly.
785 */
786 data = os_zalloc(sizeof(*data) + idata->data_len);
787 if (!data)
788 return 1;
a422d9b4
JM
789 wpa_printf(MSG_DEBUG,
790 "RRB(%s): local delivery to %s dst=" MACSTR
791 " oui_suffix=%u data_len=%u data=%p",
3cd4db23 792 src_hapd->conf->iface, hapd->conf->iface,
a422d9b4
JM
793 MAC2STR(idata->dst_addr), idata->oui_suffix,
794 (unsigned int) idata->data_len, data);
50bd8e0a 795
3cd4db23 796 os_memcpy(data->src_addr, src_hapd->own_addr, ETH_ALEN);
50bd8e0a
MB
797 os_memcpy(data->dst_addr, idata->dst_addr, ETH_ALEN);
798 os_memcpy(data + 1, idata->data, idata->data_len);
799 data->data_len = idata->data_len;
800 data->oui_suffix = idata->oui_suffix;
801
7b1105af 802 dl_list_add_tail(&hapd->l2_oui_queue, &data->list);
50bd8e0a
MB
803
804 if (!eloop_is_timeout_registered(hostapd_oui_deliver_later,
805 hapd, NULL))
806 eloop_register_timeout(0, 0,
807 hostapd_oui_deliver_later,
808 hapd, NULL);
809
4834c686
JW
810 /* If dst_addr is a multicast address, do not return any
811 * non-zero value here. Otherwise, the iteration of
812 * for_each_interface() will be stopped. */
813 if (!is_multicast_ether_addr(idata->dst_addr))
814 return 1;
50bd8e0a
MB
815 }
816
817 return 0;
818}
819
820#endif /* CONFIG_IEEE80211R_AP */
821
822
823static int hostapd_wpa_auth_send_oui(void *ctx, const u8 *dst, u8 oui_suffix,
824 const u8 *data, size_t data_len)
825{
826#ifdef CONFIG_ETH_P_OUI
827 struct hostapd_data *hapd = ctx;
828 struct eth_p_oui_ctx *oui_ctx;
829
a422d9b4
JM
830 wpa_printf(MSG_DEBUG, "RRB(%s): send to dst=" MACSTR
831 " oui_suffix=%u data_len=%u",
832 hapd->conf->iface, MAC2STR(dst), oui_suffix,
833 (unsigned int) data_len);
50bd8e0a
MB
834#ifdef CONFIG_IEEE80211R_AP
835 if (hapd->iface->interfaces &&
836 hapd->iface->interfaces->for_each_interface) {
837 struct wpa_auth_oui_iface_iter_data idata;
838 int res;
839
840 idata.src_hapd = hapd;
841 idata.dst_addr = dst;
842 idata.data = data;
843 idata.data_len = data_len;
844 idata.oui_suffix = oui_suffix;
845 res = hapd->iface->interfaces->for_each_interface(
846 hapd->iface->interfaces, hostapd_wpa_auth_oui_iter,
847 &idata);
848 if (res == 1)
849 return data_len;
850 }
851#endif /* CONFIG_IEEE80211R_AP */
852
853 oui_ctx = hostapd_wpa_get_oui(hapd, oui_suffix);
854 if (!oui_ctx)
855 return -1;
856
857 return eth_p_oui_send(oui_ctx, hapd->own_addr, dst, data, data_len);
858#else /* CONFIG_ETH_P_OUI */
859 return -1;
860#endif /* CONFIG_ETH_P_OUI */
861}
862
863
bef4d07a
MV
864static int hostapd_channel_info(void *ctx, struct wpa_channel_info *ci)
865{
866 struct hostapd_data *hapd = ctx;
867
868 return hostapd_drv_channel_info(hapd, ci);
869}
870
1034f67b 871
dbfa691d
JM
872static int hostapd_wpa_auth_update_vlan(void *ctx, const u8 *addr, int vlan_id)
873{
874#ifndef CONFIG_NO_VLAN
875 struct hostapd_data *hapd = ctx;
876 struct sta_info *sta;
dbfa691d
JM
877
878 sta = ap_get_sta(hapd, addr);
879 if (!sta)
880 return -1;
881
99d8c4dc
GG
882 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
883 struct vlan_description vlan_desc;
dbfa691d 884
99d8c4dc
GG
885 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
886 vlan_desc.notempty = 1;
887 vlan_desc.untagged = vlan_id;
888 if (!hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
889 wpa_printf(MSG_INFO,
890 "Invalid VLAN ID %d in wpa_psk_file",
891 vlan_id);
892 return -1;
893 }
894
895 if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0) {
896 wpa_printf(MSG_INFO,
897 "Failed to assign VLAN ID %d from wpa_psk_file to "
898 MACSTR, vlan_id, MAC2STR(sta->addr));
899 return -1;
900 }
901 } else {
902 sta->vlan_id = vlan_id;
dbfa691d
JM
903 }
904
905 wpa_printf(MSG_INFO,
906 "Assigned VLAN ID %d from wpa_psk_file to " MACSTR,
907 vlan_id, MAC2STR(sta->addr));
908 if ((sta->flags & WLAN_STA_ASSOC) &&
909 ap_sta_bind_vlan(hapd, sta) < 0)
910 return -1;
911#endif /* CONFIG_NO_VLAN */
912
913 return 0;
914}
915
916
1034f67b
MV
917#ifdef CONFIG_OCV
918static int hostapd_get_sta_tx_params(void *ctx, const u8 *addr,
919 int ap_max_chanwidth, int ap_seg1_idx,
920 int *bandwidth, int *seg1_idx)
921{
922 struct hostapd_data *hapd = ctx;
923 struct sta_info *sta;
924
925 sta = ap_get_sta(hapd, addr);
926 if (!sta) {
927 hostapd_wpa_auth_logger(hapd, addr, LOGGER_INFO,
928 "Failed to get STA info to validate received OCI");
929 return -1;
930 }
931
932 return get_tx_parameters(sta, ap_max_chanwidth, ap_seg1_idx, bandwidth,
933 seg1_idx);
934}
935#endif /* CONFIG_OCV */
936
937
4ec1fd8e 938#ifdef CONFIG_IEEE80211R_AP
c442055e
JM
939
940static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
941 const u8 *data, size_t data_len)
942{
943 struct hostapd_data *hapd = ctx;
944 int res;
945 struct ieee80211_mgmt *m;
946 size_t mlen;
947 struct sta_info *sta;
948
949 sta = ap_get_sta(hapd, dst);
950 if (sta == NULL || sta->wpa_sm == NULL)
951 return -1;
952
953 m = os_zalloc(sizeof(*m) + data_len);
954 if (m == NULL)
955 return -1;
956 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
957 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
958 WLAN_FC_STYPE_ACTION);
959 os_memcpy(m->da, dst, ETH_ALEN);
960 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
961 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
962 os_memcpy(&m->u, data, data_len);
963
37100274 964 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0, NULL, 0, 0);
c442055e
JM
965 os_free(m);
966 return res;
967}
968
969
970static struct wpa_state_machine *
971hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
972{
973 struct hostapd_data *hapd = ctx;
974 struct sta_info *sta;
df3b2e22 975 int ret;
c442055e 976
4cf5efec
JM
977 wpa_printf(MSG_DEBUG, "Add station entry for " MACSTR
978 " based on WPA authenticator callback",
979 MAC2STR(sta_addr));
df3b2e22
SSG
980 ret = hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT);
981
982 /*
983 * The expected return values from hostapd_add_sta_node() are
984 * 0: successfully added STA entry
985 * -EOPNOTSUPP: driver or driver wrapper does not support/need this
986 * operations
987 * any other negative value: error in adding the STA entry */
988 if (ret < 0 && ret != -EOPNOTSUPP)
88b32a99
SP
989 return NULL;
990
c442055e
JM
991 sta = ap_sta_add(hapd, sta_addr);
992 if (sta == NULL)
993 return NULL;
df3b2e22 994 if (ret == 0)
4cf5efec 995 sta->added_unassoc = 1;
df3b2e22 996
4cf5efec 997 sta->ft_over_ds = 1;
21c9b690
JM
998 if (sta->wpa_sm) {
999 sta->auth_alg = WLAN_AUTH_FT;
c442055e 1000 return sta->wpa_sm;
21c9b690 1001 }
c442055e 1002
94ddef3e 1003 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr, NULL);
c442055e
JM
1004 if (sta->wpa_sm == NULL) {
1005 ap_free_sta(hapd, sta);
1006 return NULL;
1007 }
1008 sta->auth_alg = WLAN_AUTH_FT;
1009
1010 return sta->wpa_sm;
1011}
1012
1013
47a039b0
MB
1014static int hostapd_wpa_auth_set_vlan(void *ctx, const u8 *sta_addr,
1015 struct vlan_description *vlan)
1016{
1017 struct hostapd_data *hapd = ctx;
1018 struct sta_info *sta;
1019
1020 sta = ap_get_sta(hapd, sta_addr);
1021 if (!sta || !sta->wpa_sm)
1022 return -1;
1023
1024 if (vlan->notempty &&
1025 !hostapd_vlan_valid(hapd->conf->vlan, vlan)) {
1026 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1027 HOSTAPD_LEVEL_INFO,
1028 "Invalid VLAN %d%s received from FT",
1029 vlan->untagged, vlan->tagged[0] ? "+" : "");
1030 return -1;
1031 }
1032
1033 if (ap_sta_set_vlan(hapd, sta, vlan) < 0)
1034 return -1;
1035 /* Configure wpa_group for GTK but ignore error due to driver not
1036 * knowing this STA. */
1037 ap_sta_bind_vlan(hapd, sta);
1038
1039 if (sta->vlan_id)
1040 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1041 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
1042
1043 return 0;
1044}
1045
1046
1047static int hostapd_wpa_auth_get_vlan(void *ctx, const u8 *sta_addr,
1048 struct vlan_description *vlan)
1049{
1050 struct hostapd_data *hapd = ctx;
1051 struct sta_info *sta;
1052
1053 sta = ap_get_sta(hapd, sta_addr);
1054 if (!sta)
1055 return -1;
1056
1057 if (sta->vlan_desc)
1058 *vlan = *sta->vlan_desc;
1059 else
1060 os_memset(vlan, 0, sizeof(*vlan));
1061
1062 return 0;
1063}
1064
1065
8c345234
MB
1066static int
1067hostapd_wpa_auth_set_identity(void *ctx, const u8 *sta_addr,
1068 const u8 *identity, size_t identity_len)
1069{
1070 struct hostapd_data *hapd = ctx;
1071 struct sta_info *sta;
1072
1073 sta = ap_get_sta(hapd, sta_addr);
1074 if (!sta)
1075 return -1;
1076
1077 os_free(sta->identity);
1078 sta->identity = NULL;
1079
1080 if (sta->eapol_sm) {
1081 os_free(sta->eapol_sm->identity);
1082 sta->eapol_sm->identity = NULL;
1083 sta->eapol_sm->identity_len = 0;
1084 }
1085
1086 if (!identity_len)
1087 return 0;
1088
1089 /* sta->identity is NULL terminated */
1090 sta->identity = os_zalloc(identity_len + 1);
1091 if (!sta->identity)
1092 return -1;
1093 os_memcpy(sta->identity, identity, identity_len);
1094
1095 if (sta->eapol_sm) {
1096 sta->eapol_sm->identity = os_zalloc(identity_len);
1097 if (!sta->eapol_sm->identity)
1098 return -1;
1099 os_memcpy(sta->eapol_sm->identity, identity, identity_len);
1100 sta->eapol_sm->identity_len = identity_len;
1101 }
1102
1103 return 0;
1104}
1105
1106
1107static size_t
1108hostapd_wpa_auth_get_identity(void *ctx, const u8 *sta_addr, const u8 **buf)
1109{
1110 struct hostapd_data *hapd = ctx;
1111 struct sta_info *sta;
1112 size_t len;
1113 char *identity;
1114
1115 sta = ap_get_sta(hapd, sta_addr);
1116 if (!sta)
1117 return 0;
1118
1119 *buf = ieee802_1x_get_identity(sta->eapol_sm, &len);
1120 if (*buf && len)
1121 return len;
1122
1123 if (!sta->identity) {
1124 *buf = NULL;
1125 return 0;
1126 }
1127
1128 identity = sta->identity;
1129 len = os_strlen(identity);
1130 *buf = (u8 *) identity;
1131
1132 return len;
1133}
1134
1135
1136static int
1137hostapd_wpa_auth_set_radius_cui(void *ctx, const u8 *sta_addr,
1138 const u8 *radius_cui, size_t radius_cui_len)
1139{
1140 struct hostapd_data *hapd = ctx;
1141 struct sta_info *sta;
1142
1143 sta = ap_get_sta(hapd, sta_addr);
1144 if (!sta)
1145 return -1;
1146
1147 os_free(sta->radius_cui);
1148 sta->radius_cui = NULL;
1149
1150 if (sta->eapol_sm) {
1151 wpabuf_free(sta->eapol_sm->radius_cui);
1152 sta->eapol_sm->radius_cui = NULL;
1153 }
1154
1155 if (!radius_cui)
1156 return 0;
1157
1158 /* sta->radius_cui is NULL terminated */
1159 sta->radius_cui = os_zalloc(radius_cui_len + 1);
1160 if (!sta->radius_cui)
1161 return -1;
1162 os_memcpy(sta->radius_cui, radius_cui, radius_cui_len);
1163
1164 if (sta->eapol_sm) {
1165 sta->eapol_sm->radius_cui = wpabuf_alloc_copy(radius_cui,
1166 radius_cui_len);
1167 if (!sta->eapol_sm->radius_cui)
1168 return -1;
1169 }
1170
1171 return 0;
1172}
1173
1174
1175static size_t
1176hostapd_wpa_auth_get_radius_cui(void *ctx, const u8 *sta_addr, const u8 **buf)
1177{
1178 struct hostapd_data *hapd = ctx;
1179 struct sta_info *sta;
1180 struct wpabuf *b;
1181 size_t len;
1182 char *radius_cui;
1183
1184 sta = ap_get_sta(hapd, sta_addr);
1185 if (!sta)
1186 return 0;
1187
1188 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
1189 if (b) {
1190 len = wpabuf_len(b);
1191 *buf = wpabuf_head(b);
1192 return len;
1193 }
1194
1195 if (!sta->radius_cui) {
1196 *buf = NULL;
1197 return 0;
1198 }
1199
1200 radius_cui = sta->radius_cui;
1201 len = os_strlen(radius_cui);
1202 *buf = (u8 *) radius_cui;
1203
1204 return len;
1205}
1206
1207
3cb286ca
MB
1208static void hostapd_wpa_auth_set_session_timeout(void *ctx, const u8 *sta_addr,
1209 int session_timeout)
1210{
1211 struct hostapd_data *hapd = ctx;
1212 struct sta_info *sta;
1213
1214 sta = ap_get_sta(hapd, sta_addr);
1215 if (!sta)
1216 return;
1217
1218 if (session_timeout) {
1219 os_get_reltime(&sta->session_timeout);
1220 sta->session_timeout.sec += session_timeout;
1221 sta->session_timeout_set = 1;
1222 ap_sta_session_timeout(hapd, sta, session_timeout);
1223 } else {
1224 sta->session_timeout_set = 0;
1225 ap_sta_no_session_timeout(hapd, sta);
1226 }
1227}
1228
1229
1230static int hostapd_wpa_auth_get_session_timeout(void *ctx, const u8 *sta_addr)
1231{
1232 struct hostapd_data *hapd = ctx;
1233 struct sta_info *sta;
1234 struct os_reltime now, remaining;
1235
1236 sta = ap_get_sta(hapd, sta_addr);
1237 if (!sta || !sta->session_timeout_set)
1238 return 0;
1239
1240 os_get_reltime(&now);
1241 if (os_reltime_before(&sta->session_timeout, &now)) {
1242 /* already expired, return >0 as timeout was set */
1243 return 1;
1244 }
1245
1246 os_reltime_sub(&sta->session_timeout, &now, &remaining);
1247
1248 return (remaining.sec > 0) ? remaining.sec : 1;
1249}
1250
1251
c442055e
JM
1252static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
1253 size_t len)
1254{
1255 struct hostapd_data *hapd = ctx;
7e031850
JM
1256 struct l2_ethhdr *ethhdr;
1257 if (len < sizeof(*ethhdr))
1258 return;
1259 ethhdr = (struct l2_ethhdr *) buf;
1260 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
1261 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
170c545a
MB
1262 if (!is_multicast_ether_addr(ethhdr->h_dest) &&
1263 os_memcmp(hapd->own_addr, ethhdr->h_dest, ETH_ALEN) != 0)
1264 return;
7e031850
JM
1265 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
1266 len - sizeof(*ethhdr));
c442055e
JM
1267}
1268
88b32a99 1269
50bd8e0a
MB
1270static void hostapd_rrb_oui_receive(void *ctx, const u8 *src_addr,
1271 const u8 *dst_addr, u8 oui_suffix,
1272 const u8 *buf, size_t len)
1273{
1274 struct hostapd_data *hapd = ctx;
1275
1276 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
1277 MACSTR, MAC2STR(src_addr), MAC2STR(dst_addr));
1278 if (!is_multicast_ether_addr(dst_addr) &&
1279 os_memcmp(hapd->own_addr, dst_addr, ETH_ALEN) != 0)
1280 return;
1281 wpa_ft_rrb_oui_rx(hapd->wpa_auth, src_addr, dst_addr, oui_suffix, buf,
1282 len);
1283}
1284
1285
88b32a99
SP
1286static int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr,
1287 u8 *tspec_ie, size_t tspec_ielen)
1288{
1289 struct hostapd_data *hapd = ctx;
1290 return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen);
1291}
1292
50bd8e0a
MB
1293
1294
1295static int hostapd_wpa_register_ft_oui(struct hostapd_data *hapd,
1296 const char *ft_iface)
1297{
1298 hapd->oui_pull = eth_p_oui_register(hapd, ft_iface,
1299 FT_PACKET_R0KH_R1KH_PULL,
1300 hostapd_rrb_oui_receive, hapd);
1301 if (!hapd->oui_pull)
1302 return -1;
1303
1304 hapd->oui_resp = eth_p_oui_register(hapd, ft_iface,
1305 FT_PACKET_R0KH_R1KH_RESP,
1306 hostapd_rrb_oui_receive, hapd);
1307 if (!hapd->oui_resp)
1308 return -1;
1309
1310 hapd->oui_push = eth_p_oui_register(hapd, ft_iface,
1311 FT_PACKET_R0KH_R1KH_PUSH,
1312 hostapd_rrb_oui_receive, hapd);
1313 if (!hapd->oui_push)
1314 return -1;
1315
eefe8630
MB
1316 hapd->oui_sreq = eth_p_oui_register(hapd, ft_iface,
1317 FT_PACKET_R0KH_R1KH_SEQ_REQ,
1318 hostapd_rrb_oui_receive, hapd);
1319 if (!hapd->oui_sreq)
1320 return -1;
1321
1322 hapd->oui_sresp = eth_p_oui_register(hapd, ft_iface,
1323 FT_PACKET_R0KH_R1KH_SEQ_RESP,
1324 hostapd_rrb_oui_receive, hapd);
1325 if (!hapd->oui_sresp)
1326 return -1;
1327
50bd8e0a
MB
1328 return 0;
1329}
1330
1331
1332static void hostapd_wpa_unregister_ft_oui(struct hostapd_data *hapd)
1333{
1334 eth_p_oui_unregister(hapd->oui_pull);
1335 hapd->oui_pull = NULL;
1336 eth_p_oui_unregister(hapd->oui_resp);
1337 hapd->oui_resp = NULL;
1338 eth_p_oui_unregister(hapd->oui_push);
1339 hapd->oui_push = NULL;
eefe8630
MB
1340 eth_p_oui_unregister(hapd->oui_sreq);
1341 hapd->oui_sreq = NULL;
1342 eth_p_oui_unregister(hapd->oui_sresp);
1343 hapd->oui_sresp = NULL;
50bd8e0a 1344}
4ec1fd8e 1345#endif /* CONFIG_IEEE80211R_AP */
c442055e
JM
1346
1347
1348int hostapd_setup_wpa(struct hostapd_data *hapd)
1349{
1350 struct wpa_auth_config _conf;
cef8fac0
JB
1351 static const struct wpa_auth_callbacks cb = {
1352 .logger = hostapd_wpa_auth_logger,
1353 .disconnect = hostapd_wpa_auth_disconnect,
1354 .mic_failure_report = hostapd_wpa_auth_mic_failure_report,
1355 .psk_failure_report = hostapd_wpa_auth_psk_failure_report,
1356 .set_eapol = hostapd_wpa_auth_set_eapol,
1357 .get_eapol = hostapd_wpa_auth_get_eapol,
1358 .get_psk = hostapd_wpa_auth_get_psk,
1359 .get_msk = hostapd_wpa_auth_get_msk,
1360 .set_key = hostapd_wpa_auth_set_key,
1361 .get_seqnum = hostapd_wpa_auth_get_seqnum,
1362 .send_eapol = hostapd_wpa_auth_send_eapol,
1363 .for_each_sta = hostapd_wpa_auth_for_each_sta,
1364 .for_each_auth = hostapd_wpa_auth_for_each_auth,
1365 .send_ether = hostapd_wpa_auth_send_ether,
50bd8e0a 1366 .send_oui = hostapd_wpa_auth_send_oui,
bef4d07a 1367 .channel_info = hostapd_channel_info,
dbfa691d 1368 .update_vlan = hostapd_wpa_auth_update_vlan,
1034f67b
MV
1369#ifdef CONFIG_OCV
1370 .get_sta_tx_params = hostapd_get_sta_tx_params,
1371#endif /* CONFIG_OCV */
cef8fac0
JB
1372#ifdef CONFIG_IEEE80211R_AP
1373 .send_ft_action = hostapd_wpa_auth_send_ft_action,
1374 .add_sta = hostapd_wpa_auth_add_sta,
1375 .add_tspec = hostapd_wpa_auth_add_tspec,
47a039b0
MB
1376 .set_vlan = hostapd_wpa_auth_set_vlan,
1377 .get_vlan = hostapd_wpa_auth_get_vlan,
8c345234
MB
1378 .set_identity = hostapd_wpa_auth_set_identity,
1379 .get_identity = hostapd_wpa_auth_get_identity,
1380 .set_radius_cui = hostapd_wpa_auth_set_radius_cui,
1381 .get_radius_cui = hostapd_wpa_auth_get_radius_cui,
3cb286ca
MB
1382 .set_session_timeout = hostapd_wpa_auth_set_session_timeout,
1383 .get_session_timeout = hostapd_wpa_auth_get_session_timeout,
cef8fac0
JB
1384#endif /* CONFIG_IEEE80211R_AP */
1385 };
c442055e
JM
1386 const u8 *wpa_ie;
1387 size_t wpa_ie_len;
1388
7af092a0 1389 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &_conf);
e4bf4db9
JM
1390 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
1391 _conf.tx_status = 1;
88b32a99
SP
1392 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME)
1393 _conf.ap_mlme = 1;
1a7963e3
AW
1394
1395 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) &&
1396 (hapd->conf->wpa_deny_ptk0_rekey == PTK0_REKEY_ALLOW_NEVER ||
1397 (hapd->conf->wpa_deny_ptk0_rekey == PTK0_REKEY_ALLOW_LOCAL_OK &&
1398 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_SAFE_PTK0_REKEYS)))) {
1399 wpa_msg(hapd->msg_ctx, MSG_INFO,
1400 "Disable PTK0 rekey support - replaced with disconnect");
1401 _conf.wpa_deny_ptk0_rekey = 1;
1402 }
1403
cef8fac0 1404 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb, hapd);
c442055e
JM
1405 if (hapd->wpa_auth == NULL) {
1406 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1407 return -1;
1408 }
1409
1410 if (hostapd_set_privacy(hapd, 1)) {
1411 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1412 "for interface %s", hapd->conf->iface);
1413 return -1;
1414 }
1415
1416 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1417 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1418 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1419 "the kernel driver.");
1420 return -1;
1421 }
1422
1423 if (rsn_preauth_iface_init(hapd)) {
1424 wpa_printf(MSG_ERROR, "Initialization of RSN "
1425 "pre-authentication failed.");
1426 return -1;
1427 }
1428
4ec1fd8e 1429#ifdef CONFIG_IEEE80211R_AP
b92d2a57 1430 if (!hostapd_drv_none(hapd) &&
66d46406 1431 wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt)) {
50bd8e0a
MB
1432 const char *ft_iface;
1433
1434 ft_iface = hapd->conf->bridge[0] ? hapd->conf->bridge :
1435 hapd->conf->iface;
1436 hapd->l2 = l2_packet_init(ft_iface, NULL, ETH_P_RRB,
7e031850 1437 hostapd_rrb_receive, hapd, 1);
ccaabeaa 1438 if (!hapd->l2) {
c442055e
JM
1439 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1440 "interface");
1441 return -1;
1442 }
50bd8e0a
MB
1443
1444 if (hostapd_wpa_register_ft_oui(hapd, ft_iface)) {
1445 wpa_printf(MSG_ERROR,
1446 "Failed to open ETH_P_OUI interface");
1447 return -1;
1448 }
c442055e 1449 }
4ec1fd8e 1450#endif /* CONFIG_IEEE80211R_AP */
c442055e
JM
1451
1452 return 0;
1453
1454}
1455
1456
1457void hostapd_reconfig_wpa(struct hostapd_data *hapd)
1458{
1459 struct wpa_auth_config wpa_auth_conf;
7af092a0 1460 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &wpa_auth_conf);
c442055e
JM
1461 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
1462}
1463
1464
1465void hostapd_deinit_wpa(struct hostapd_data *hapd)
1466{
01a17491 1467 ieee80211_tkip_countermeasures_deinit(hapd);
c442055e
JM
1468 rsn_preauth_iface_deinit(hapd);
1469 if (hapd->wpa_auth) {
1470 wpa_deinit(hapd->wpa_auth);
1471 hapd->wpa_auth = NULL;
1472
1b822f52 1473 if (hapd->drv_priv && hostapd_set_privacy(hapd, 0)) {
c442055e
JM
1474 wpa_printf(MSG_DEBUG, "Could not disable "
1475 "PrivacyInvoked for interface %s",
1476 hapd->conf->iface);
1477 }
1478
1b822f52
JM
1479 if (hapd->drv_priv &&
1480 hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
c442055e
JM
1481 wpa_printf(MSG_DEBUG, "Could not remove generic "
1482 "information element from interface %s",
1483 hapd->conf->iface);
1484 }
1485 }
1486 ieee802_1x_deinit(hapd);
1487
4ec1fd8e 1488#ifdef CONFIG_IEEE80211R_AP
c5fee160
MB
1489 eloop_cancel_timeout(hostapd_wpa_ft_rrb_rx_later, hapd, ELOOP_ALL_CTX);
1490 hostapd_wpa_ft_rrb_rx_later(hapd, NULL); /* flush without delivering */
50bd8e0a
MB
1491 eloop_cancel_timeout(hostapd_oui_deliver_later, hapd, ELOOP_ALL_CTX);
1492 hostapd_oui_deliver_later(hapd, NULL); /* flush without delivering */
c442055e 1493 l2_packet_deinit(hapd->l2);
66f4dd15 1494 hapd->l2 = NULL;
50bd8e0a 1495 hostapd_wpa_unregister_ft_oui(hapd);
4ec1fd8e 1496#endif /* CONFIG_IEEE80211R_AP */
c442055e 1497}