FST: Send FST Action frames to AP mode processing
[mech_eap.git] / src / ap / drv_callbacks.c
1 /*
2  * hostapd / Callback functions for driver wrappers
3  * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "radius/radius.h"
14 #include "drivers/driver.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ieee802_11_common.h"
17 #include "common/wpa_ctrl.h"
18 #include "crypto/random.h"
19 #include "p2p/p2p.h"
20 #include "wps/wps.h"
21 #include "fst/fst.h"
22 #include "wnm_ap.h"
23 #include "hostapd.h"
24 #include "ieee802_11.h"
25 #include "sta_info.h"
26 #include "accounting.h"
27 #include "tkip_countermeasures.h"
28 #include "ieee802_1x.h"
29 #include "wpa_auth.h"
30 #include "wps_hostapd.h"
31 #include "ap_drv_ops.h"
32 #include "ap_config.h"
33 #include "hw_features.h"
34 #include "dfs.h"
35 #include "beacon.h"
36
37
38 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
39                         const u8 *req_ies, size_t req_ies_len, int reassoc)
40 {
41         struct sta_info *sta;
42         int new_assoc, res;
43         struct ieee802_11_elems elems;
44         const u8 *ie;
45         size_t ielen;
46 #ifdef CONFIG_IEEE80211R
47         u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
48         u8 *p = buf;
49 #endif /* CONFIG_IEEE80211R */
50         u16 reason = WLAN_REASON_UNSPECIFIED;
51         u16 status = WLAN_STATUS_SUCCESS;
52         const u8 *p2p_dev_addr = NULL;
53
54         if (addr == NULL) {
55                 /*
56                  * This could potentially happen with unexpected event from the
57                  * driver wrapper. This was seen at least in one case where the
58                  * driver ended up being set to station mode while hostapd was
59                  * running, so better make sure we stop processing such an
60                  * event here.
61                  */
62                 wpa_printf(MSG_DEBUG, "hostapd_notif_assoc: Skip event with "
63                            "no address");
64                 return -1;
65         }
66         random_add_randomness(addr, ETH_ALEN);
67
68         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
69                        HOSTAPD_LEVEL_INFO, "associated");
70
71         ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0);
72         if (elems.wps_ie) {
73                 ie = elems.wps_ie - 2;
74                 ielen = elems.wps_ie_len + 2;
75                 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
76         } else if (elems.rsn_ie) {
77                 ie = elems.rsn_ie - 2;
78                 ielen = elems.rsn_ie_len + 2;
79                 wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
80         } else if (elems.wpa_ie) {
81                 ie = elems.wpa_ie - 2;
82                 ielen = elems.wpa_ie_len + 2;
83                 wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
84 #ifdef CONFIG_HS20
85         } else if (elems.osen) {
86                 ie = elems.osen - 2;
87                 ielen = elems.osen_len + 2;
88                 wpa_printf(MSG_DEBUG, "STA included OSEN IE in (Re)AssocReq");
89 #endif /* CONFIG_HS20 */
90         } else {
91                 ie = NULL;
92                 ielen = 0;
93                 wpa_printf(MSG_DEBUG, "STA did not include WPS/RSN/WPA IE in "
94                            "(Re)AssocReq");
95         }
96
97         sta = ap_get_sta(hapd, addr);
98         if (sta) {
99                 ap_sta_no_session_timeout(hapd, sta);
100                 accounting_sta_stop(hapd, sta);
101
102                 /*
103                  * Make sure that the previously registered inactivity timer
104                  * will not remove the STA immediately.
105                  */
106                 sta->timeout_next = STA_NULLFUNC;
107         } else {
108                 sta = ap_sta_add(hapd, addr);
109                 if (sta == NULL) {
110                         hostapd_drv_sta_disassoc(hapd, addr,
111                                                  WLAN_REASON_DISASSOC_AP_BUSY);
112                         return -1;
113                 }
114         }
115         sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
116
117 #ifdef CONFIG_P2P
118         if (elems.p2p) {
119                 wpabuf_free(sta->p2p_ie);
120                 sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
121                                                           P2P_IE_VENDOR_TYPE);
122                 if (sta->p2p_ie)
123                         p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
124         }
125 #endif /* CONFIG_P2P */
126
127 #ifdef CONFIG_IEEE80211N
128 #ifdef NEED_AP_MLME
129         if (elems.ht_capabilities &&
130             (hapd->iface->conf->ht_capab &
131              HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
132                 struct ieee80211_ht_capabilities *ht_cap =
133                         (struct ieee80211_ht_capabilities *)
134                         elems.ht_capabilities;
135
136                 if (le_to_host16(ht_cap->ht_capabilities_info) &
137                     HT_CAP_INFO_40MHZ_INTOLERANT)
138                         ht40_intolerant_add(hapd->iface, sta);
139         }
140 #endif /* NEED_AP_MLME */
141 #endif /* CONFIG_IEEE80211N */
142
143 #ifdef CONFIG_INTERWORKING
144         if (elems.ext_capab && elems.ext_capab_len > 4) {
145                 if (elems.ext_capab[4] & 0x01)
146                         sta->qos_map_enabled = 1;
147         }
148 #endif /* CONFIG_INTERWORKING */
149
150 #ifdef CONFIG_HS20
151         wpabuf_free(sta->hs20_ie);
152         if (elems.hs20 && elems.hs20_len > 4) {
153                 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
154                                                  elems.hs20_len - 4);
155         } else
156                 sta->hs20_ie = NULL;
157 #endif /* CONFIG_HS20 */
158
159 #ifdef CONFIG_FST
160         wpabuf_free(sta->mb_ies);
161         if (hapd->iface->fst)
162                 sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
163         else
164                 sta->mb_ies = NULL;
165 #endif /* CONFIG_FST */
166
167         if (hapd->conf->wpa) {
168                 if (ie == NULL || ielen == 0) {
169 #ifdef CONFIG_WPS
170                         if (hapd->conf->wps_state) {
171                                 wpa_printf(MSG_DEBUG, "STA did not include "
172                                            "WPA/RSN IE in (Re)Association "
173                                            "Request - possible WPS use");
174                                 sta->flags |= WLAN_STA_MAYBE_WPS;
175                                 goto skip_wpa_check;
176                         }
177 #endif /* CONFIG_WPS */
178
179                         wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
180                         return -1;
181                 }
182 #ifdef CONFIG_WPS
183                 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
184                     os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
185                         struct wpabuf *wps;
186                         sta->flags |= WLAN_STA_WPS;
187                         wps = ieee802_11_vendor_ie_concat(ie, ielen,
188                                                           WPS_IE_VENDOR_TYPE);
189                         if (wps) {
190                                 if (wps_is_20(wps)) {
191                                         wpa_printf(MSG_DEBUG, "WPS: STA "
192                                                    "supports WPS 2.0");
193                                         sta->flags |= WLAN_STA_WPS2;
194                                 }
195                                 wpabuf_free(wps);
196                         }
197                         goto skip_wpa_check;
198                 }
199 #endif /* CONFIG_WPS */
200
201                 if (sta->wpa_sm == NULL)
202                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
203                                                         sta->addr,
204                                                         p2p_dev_addr);
205                 if (sta->wpa_sm == NULL) {
206                         wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
207                                    "machine");
208                         return -1;
209                 }
210                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
211                                           ie, ielen,
212                                           elems.mdie, elems.mdie_len);
213                 if (res != WPA_IE_OK) {
214                         wpa_printf(MSG_DEBUG, "WPA/RSN information element "
215                                    "rejected? (res %u)", res);
216                         wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
217                         if (res == WPA_INVALID_GROUP) {
218                                 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
219                                 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
220                         } else if (res == WPA_INVALID_PAIRWISE) {
221                                 reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
222                                 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
223                         } else if (res == WPA_INVALID_AKMP) {
224                                 reason = WLAN_REASON_AKMP_NOT_VALID;
225                                 status = WLAN_STATUS_AKMP_NOT_VALID;
226                         }
227 #ifdef CONFIG_IEEE80211W
228                         else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) {
229                                 reason = WLAN_REASON_INVALID_IE;
230                                 status = WLAN_STATUS_INVALID_IE;
231                         } else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) {
232                                 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
233                                 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
234                         }
235 #endif /* CONFIG_IEEE80211W */
236                         else {
237                                 reason = WLAN_REASON_INVALID_IE;
238                                 status = WLAN_STATUS_INVALID_IE;
239                         }
240                         goto fail;
241                 }
242 #ifdef CONFIG_IEEE80211W
243                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
244                     sta->sa_query_count > 0)
245                         ap_check_sa_query_timeout(hapd, sta);
246                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
247                     (sta->auth_alg != WLAN_AUTH_FT)) {
248                         /*
249                          * STA has already been associated with MFP and SA
250                          * Query timeout has not been reached. Reject the
251                          * association attempt temporarily and start SA Query,
252                          * if one is not pending.
253                          */
254
255                         if (sta->sa_query_count == 0)
256                                 ap_sta_start_sa_query(hapd, sta);
257
258 #ifdef CONFIG_IEEE80211R
259                         status = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
260
261                         p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
262
263                         hostapd_sta_assoc(hapd, addr, reassoc, status, buf,
264                                           p - buf);
265 #endif /* CONFIG_IEEE80211R */
266                         return 0;
267                 }
268
269                 if (wpa_auth_uses_mfp(sta->wpa_sm))
270                         sta->flags |= WLAN_STA_MFP;
271                 else
272                         sta->flags &= ~WLAN_STA_MFP;
273 #endif /* CONFIG_IEEE80211W */
274
275 #ifdef CONFIG_IEEE80211R
276                 if (sta->auth_alg == WLAN_AUTH_FT) {
277                         status = wpa_ft_validate_reassoc(sta->wpa_sm, req_ies,
278                                                          req_ies_len);
279                         if (status != WLAN_STATUS_SUCCESS) {
280                                 if (status == WLAN_STATUS_INVALID_PMKID)
281                                         reason = WLAN_REASON_INVALID_IE;
282                                 if (status == WLAN_STATUS_INVALID_MDIE)
283                                         reason = WLAN_REASON_INVALID_IE;
284                                 if (status == WLAN_STATUS_INVALID_FTIE)
285                                         reason = WLAN_REASON_INVALID_IE;
286                                 goto fail;
287                         }
288                 }
289 #endif /* CONFIG_IEEE80211R */
290         } else if (hapd->conf->wps_state) {
291 #ifdef CONFIG_WPS
292                 struct wpabuf *wps;
293                 if (req_ies)
294                         wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
295                                                           WPS_IE_VENDOR_TYPE);
296                 else
297                         wps = NULL;
298 #ifdef CONFIG_WPS_STRICT
299                 if (wps && wps_validate_assoc_req(wps) < 0) {
300                         reason = WLAN_REASON_INVALID_IE;
301                         status = WLAN_STATUS_INVALID_IE;
302                         wpabuf_free(wps);
303                         goto fail;
304                 }
305 #endif /* CONFIG_WPS_STRICT */
306                 if (wps) {
307                         sta->flags |= WLAN_STA_WPS;
308                         if (wps_is_20(wps)) {
309                                 wpa_printf(MSG_DEBUG, "WPS: STA supports "
310                                            "WPS 2.0");
311                                 sta->flags |= WLAN_STA_WPS2;
312                         }
313                 } else
314                         sta->flags |= WLAN_STA_MAYBE_WPS;
315                 wpabuf_free(wps);
316 #endif /* CONFIG_WPS */
317 #ifdef CONFIG_HS20
318         } else if (hapd->conf->osen) {
319                 if (elems.osen == NULL) {
320                         hostapd_logger(
321                                 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
322                                 HOSTAPD_LEVEL_INFO,
323                                 "No HS 2.0 OSEN element in association request");
324                         return WLAN_STATUS_INVALID_IE;
325                 }
326
327                 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
328                 if (sta->wpa_sm == NULL)
329                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
330                                                         sta->addr, NULL);
331                 if (sta->wpa_sm == NULL) {
332                         wpa_printf(MSG_WARNING, "Failed to initialize WPA "
333                                    "state machine");
334                         return WLAN_STATUS_UNSPECIFIED_FAILURE;
335                 }
336                 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
337                                       elems.osen - 2, elems.osen_len + 2) < 0)
338                         return WLAN_STATUS_INVALID_IE;
339 #endif /* CONFIG_HS20 */
340         }
341 #ifdef CONFIG_WPS
342 skip_wpa_check:
343 #endif /* CONFIG_WPS */
344
345 #ifdef CONFIG_IEEE80211R
346         p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
347                                         sta->auth_alg, req_ies, req_ies_len);
348
349         hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
350
351         if (sta->auth_alg == WLAN_AUTH_FT)
352                 ap_sta_set_authorized(hapd, sta, 1);
353 #else /* CONFIG_IEEE80211R */
354         /* Keep compiler silent about unused variables */
355         if (status) {
356         }
357 #endif /* CONFIG_IEEE80211R */
358
359         new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
360         sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
361         sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
362
363         hostapd_set_sta_flags(hapd, sta);
364
365         if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
366                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
367         else
368                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
369
370         hostapd_new_assoc_sta(hapd, sta, !new_assoc);
371
372         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
373
374 #ifdef CONFIG_P2P
375         if (req_ies) {
376                 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
377                                       req_ies, req_ies_len);
378         }
379 #endif /* CONFIG_P2P */
380
381         return 0;
382
383 fail:
384 #ifdef CONFIG_IEEE80211R
385         hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
386 #endif /* CONFIG_IEEE80211R */
387         hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
388         ap_free_sta(hapd, sta);
389         return -1;
390 }
391
392
393 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
394 {
395         struct sta_info *sta;
396
397         if (addr == NULL) {
398                 /*
399                  * This could potentially happen with unexpected event from the
400                  * driver wrapper. This was seen at least in one case where the
401                  * driver ended up reporting a station mode event while hostapd
402                  * was running, so better make sure we stop processing such an
403                  * event here.
404                  */
405                 wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event "
406                            "with no address");
407                 return;
408         }
409
410         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
411                        HOSTAPD_LEVEL_INFO, "disassociated");
412
413         sta = ap_get_sta(hapd, addr);
414         if (sta == NULL) {
415                 wpa_printf(MSG_DEBUG, "Disassociation notification for "
416                            "unknown STA " MACSTR, MAC2STR(addr));
417                 return;
418         }
419
420         ap_sta_set_authorized(hapd, sta, 0);
421         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
422         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
423         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
424         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
425         ap_free_sta(hapd, sta);
426 }
427
428
429 void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
430 {
431         struct sta_info *sta = ap_get_sta(hapd, addr);
432
433         if (!sta || !hapd->conf->disassoc_low_ack)
434                 return;
435
436         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
437                        HOSTAPD_LEVEL_INFO, "disconnected due to excessive "
438                        "missing ACKs");
439         hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
440         if (sta)
441                 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
442 }
443
444
445 void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
446                              int offset, int width, int cf1, int cf2)
447 {
448 #ifdef NEED_AP_MLME
449         int channel, chwidth, seg0_idx = 0, seg1_idx = 0, is_dfs;
450
451         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
452                        HOSTAPD_LEVEL_INFO,
453                        "driver had channel switch: freq=%d, ht=%d, offset=%d, width=%d (%s), cf1=%d, cf2=%d",
454                        freq, ht, offset, width, channel_width_to_string(width),
455                        cf1, cf2);
456
457         hapd->iface->freq = freq;
458
459         channel = hostapd_hw_get_channel(hapd, freq);
460         if (!channel) {
461                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
462                                HOSTAPD_LEVEL_WARNING, "driver switched to "
463                                "bad channel!");
464                 return;
465         }
466
467         switch (width) {
468         case CHAN_WIDTH_80:
469                 chwidth = VHT_CHANWIDTH_80MHZ;
470                 break;
471         case CHAN_WIDTH_80P80:
472                 chwidth = VHT_CHANWIDTH_80P80MHZ;
473                 break;
474         case CHAN_WIDTH_160:
475                 chwidth = VHT_CHANWIDTH_160MHZ;
476                 break;
477         case CHAN_WIDTH_20_NOHT:
478         case CHAN_WIDTH_20:
479         case CHAN_WIDTH_40:
480         default:
481                 chwidth = VHT_CHANWIDTH_USE_HT;
482                 break;
483         }
484
485         switch (hapd->iface->current_mode->mode) {
486         case HOSTAPD_MODE_IEEE80211A:
487                 if (cf1 > 5000)
488                         seg0_idx = (cf1 - 5000) / 5;
489                 if (cf2 > 5000)
490                         seg1_idx = (cf2 - 5000) / 5;
491                 break;
492         default:
493                 seg0_idx = hostapd_hw_get_channel(hapd, cf1);
494                 seg1_idx = hostapd_hw_get_channel(hapd, cf2);
495                 break;
496         }
497
498         hapd->iconf->channel = channel;
499         hapd->iconf->ieee80211n = ht;
500         if (!ht)
501                 hapd->iconf->ieee80211ac = 0;
502         hapd->iconf->secondary_channel = offset;
503         hapd->iconf->vht_oper_chwidth = chwidth;
504         hapd->iconf->vht_oper_centr_freq_seg0_idx = seg0_idx;
505         hapd->iconf->vht_oper_centr_freq_seg1_idx = seg1_idx;
506
507         is_dfs = ieee80211_is_dfs(freq);
508
509         if (hapd->csa_in_progress &&
510             freq == hapd->cs_freq_params.freq) {
511                 hostapd_cleanup_cs_params(hapd);
512                 ieee802_11_set_beacon(hapd);
513
514                 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
515                         "freq=%d dfs=%d", freq, is_dfs);
516         } else if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
517                 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
518                         "freq=%d dfs=%d", freq, is_dfs);
519         }
520 #endif /* NEED_AP_MLME */
521 }
522
523
524 void hostapd_event_connect_failed_reason(struct hostapd_data *hapd,
525                                          const u8 *addr, int reason_code)
526 {
527         switch (reason_code) {
528         case MAX_CLIENT_REACHED:
529                 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_MAX_STA MACSTR,
530                         MAC2STR(addr));
531                 break;
532         case BLOCKED_CLIENT:
533                 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_BLOCKED_STA MACSTR,
534                         MAC2STR(addr));
535                 break;
536         }
537 }
538
539
540 #ifdef CONFIG_ACS
541 static void hostapd_acs_channel_selected(struct hostapd_data *hapd,
542                                          struct acs_selected_channels *acs_res)
543 {
544         int ret, i;
545
546         if (hapd->iconf->channel) {
547                 wpa_printf(MSG_INFO, "ACS: Channel was already set to %d",
548                            hapd->iconf->channel);
549                 return;
550         }
551
552         if (!hapd->iface->current_mode) {
553                 for (i = 0; i < hapd->iface->num_hw_features; i++) {
554                         struct hostapd_hw_modes *mode =
555                                 &hapd->iface->hw_features[i];
556
557                         if (mode->mode == acs_res->hw_mode) {
558                                 hapd->iface->current_mode = mode;
559                                 break;
560                         }
561                 }
562                 if (!hapd->iface->current_mode) {
563                         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
564                                        HOSTAPD_LEVEL_WARNING,
565                                        "driver selected to bad hw_mode");
566                         return;
567                 }
568         }
569
570         hapd->iface->freq = hostapd_hw_get_freq(hapd, acs_res->pri_channel);
571
572         if (!acs_res->pri_channel) {
573                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
574                                HOSTAPD_LEVEL_WARNING,
575                                "driver switched to bad channel");
576                 return;
577         }
578
579         hapd->iconf->channel = acs_res->pri_channel;
580         hapd->iconf->acs = 1;
581
582         if (acs_res->sec_channel == 0)
583                 hapd->iconf->secondary_channel = 0;
584         else if (acs_res->sec_channel < acs_res->pri_channel)
585                 hapd->iconf->secondary_channel = -1;
586         else if (acs_res->sec_channel > acs_res->pri_channel)
587                 hapd->iconf->secondary_channel = 1;
588         else {
589                 wpa_printf(MSG_ERROR, "Invalid secondary channel!");
590                 return;
591         }
592
593         if (hapd->iface->conf->ieee80211ac) {
594                 /* set defaults for backwards compatibility */
595                 hapd->iconf->vht_oper_centr_freq_seg1_idx = 0;
596                 hapd->iconf->vht_oper_centr_freq_seg0_idx = 0;
597                 hapd->iconf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
598                 if (acs_res->ch_width == 80) {
599                         hapd->iconf->vht_oper_centr_freq_seg0_idx =
600                                 acs_res->vht_seg0_center_ch;
601                         hapd->iconf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
602                 } else if (acs_res->ch_width == 160) {
603                         if (acs_res->vht_seg1_center_ch == 0) {
604                                 hapd->iconf->vht_oper_centr_freq_seg0_idx =
605                                         acs_res->vht_seg0_center_ch;
606                                 hapd->iconf->vht_oper_chwidth =
607                                         VHT_CHANWIDTH_160MHZ;
608                         } else {
609                                 hapd->iconf->vht_oper_centr_freq_seg0_idx =
610                                         acs_res->vht_seg0_center_ch;
611                                 hapd->iconf->vht_oper_centr_freq_seg1_idx =
612                                         acs_res->vht_seg1_center_ch;
613                                 hapd->iconf->vht_oper_chwidth =
614                                         VHT_CHANWIDTH_80P80MHZ;
615                         }
616                 }
617         }
618
619         ret = hostapd_acs_completed(hapd->iface, 0);
620         if (ret) {
621                 wpa_printf(MSG_ERROR,
622                            "ACS: Possibly channel configuration is invalid");
623         }
624 }
625 #endif /* CONFIG_ACS */
626
627
628 int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
629                          const u8 *bssid, const u8 *ie, size_t ie_len,
630                          int ssi_signal)
631 {
632         size_t i;
633         int ret = 0;
634
635         if (sa == NULL || ie == NULL)
636                 return -1;
637
638         random_add_randomness(sa, ETH_ALEN);
639         for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
640                 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
641                                             sa, da, bssid, ie, ie_len,
642                                             ssi_signal) > 0) {
643                         ret = 1;
644                         break;
645                 }
646         }
647         return ret;
648 }
649
650
651 #ifdef HOSTAPD
652
653 #ifdef CONFIG_IEEE80211R
654 static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
655                                           const u8 *bssid,
656                                           u16 auth_transaction, u16 status,
657                                           const u8 *ies, size_t ies_len)
658 {
659         struct hostapd_data *hapd = ctx;
660         struct sta_info *sta;
661
662         sta = ap_get_sta(hapd, dst);
663         if (sta == NULL)
664                 return;
665
666         hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
667                        HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
668         sta->flags |= WLAN_STA_AUTH;
669
670         hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
671 }
672 #endif /* CONFIG_IEEE80211R */
673
674
675 static void hostapd_notif_auth(struct hostapd_data *hapd,
676                                struct auth_info *rx_auth)
677 {
678         struct sta_info *sta;
679         u16 status = WLAN_STATUS_SUCCESS;
680         u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
681         size_t resp_ies_len = 0;
682
683         sta = ap_get_sta(hapd, rx_auth->peer);
684         if (!sta) {
685                 sta = ap_sta_add(hapd, rx_auth->peer);
686                 if (sta == NULL) {
687                         status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
688                         goto fail;
689                 }
690         }
691         sta->flags &= ~WLAN_STA_PREAUTH;
692         ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
693 #ifdef CONFIG_IEEE80211R
694         if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
695                 sta->auth_alg = WLAN_AUTH_FT;
696                 if (sta->wpa_sm == NULL)
697                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
698                                                         sta->addr, NULL);
699                 if (sta->wpa_sm == NULL) {
700                         wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
701                                    "state machine");
702                         status = WLAN_STATUS_UNSPECIFIED_FAILURE;
703                         goto fail;
704                 }
705                 wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
706                                     rx_auth->auth_transaction, rx_auth->ies,
707                                     rx_auth->ies_len,
708                                     hostapd_notify_auth_ft_finish, hapd);
709                 return;
710         }
711 #endif /* CONFIG_IEEE80211R */
712 fail:
713         hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
714                          status, resp_ies, resp_ies_len);
715 }
716
717
718 static void hostapd_action_rx(struct hostapd_data *hapd,
719                               struct rx_mgmt *drv_mgmt)
720 {
721         struct ieee80211_mgmt *mgmt;
722         struct sta_info *sta;
723         size_t plen __maybe_unused;
724         u16 fc;
725
726         if (drv_mgmt->frame_len < 24 + 1)
727                 return;
728
729         plen = drv_mgmt->frame_len - 24 - 1;
730
731         mgmt = (struct ieee80211_mgmt *) drv_mgmt->frame;
732         fc = le_to_host16(mgmt->frame_control);
733         if (WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION)
734                 return; /* handled by the driver */
735
736         wpa_printf(MSG_DEBUG, "RX_ACTION cat %d action plen %d",
737                    mgmt->u.action.category, (int) plen);
738
739         sta = ap_get_sta(hapd, mgmt->sa);
740         if (sta == NULL) {
741                 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
742                 return;
743         }
744 #ifdef CONFIG_IEEE80211R
745         if (mgmt->u.action.category == WLAN_ACTION_FT) {
746                 const u8 *payload = drv_mgmt->frame + 24 + 1;
747                 wpa_ft_action_rx(sta->wpa_sm, payload, plen);
748         }
749 #endif /* CONFIG_IEEE80211R */
750 #ifdef CONFIG_IEEE80211W
751         if (mgmt->u.action.category == WLAN_ACTION_SA_QUERY && plen >= 4) {
752                 ieee802_11_sa_query_action(
753                         hapd, mgmt->sa,
754                         mgmt->u.action.u.sa_query_resp.action,
755                         mgmt->u.action.u.sa_query_resp.trans_id);
756         }
757 #endif /* CONFIG_IEEE80211W */
758 #ifdef CONFIG_WNM
759         if (mgmt->u.action.category == WLAN_ACTION_WNM) {
760                 ieee802_11_rx_wnm_action_ap(hapd, mgmt, drv_mgmt->frame_len);
761         }
762 #endif /* CONFIG_WNM */
763 #ifdef CONFIG_FST
764         if (mgmt->u.action.category == WLAN_ACTION_FST && hapd->iface->fst) {
765                 fst_rx_action(hapd->iface->fst, mgmt, drv_mgmt->frame_len);
766                 return;
767         }
768 #endif /* CONFIG_FST */
769
770 }
771
772
773 #ifdef NEED_AP_MLME
774
775 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
776
777 static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
778                                             const u8 *bssid)
779 {
780         size_t i;
781
782         if (bssid == NULL)
783                 return NULL;
784         if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
785             bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
786                 return HAPD_BROADCAST;
787
788         for (i = 0; i < iface->num_bss; i++) {
789                 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
790                         return iface->bss[i];
791         }
792
793         return NULL;
794 }
795
796
797 static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
798                                         const u8 *bssid, const u8 *addr,
799                                         int wds)
800 {
801         hapd = get_hapd_bssid(hapd->iface, bssid);
802         if (hapd == NULL || hapd == HAPD_BROADCAST)
803                 return;
804
805         ieee802_11_rx_from_unknown(hapd, addr, wds);
806 }
807
808
809 static int hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
810 {
811         struct hostapd_iface *iface = hapd->iface;
812         const struct ieee80211_hdr *hdr;
813         const u8 *bssid;
814         struct hostapd_frame_info fi;
815         int ret;
816
817 #ifdef CONFIG_TESTING_OPTIONS
818         if (hapd->ext_mgmt_frame_handling) {
819                 size_t hex_len = 2 * rx_mgmt->frame_len + 1;
820                 char *hex = os_malloc(hex_len);
821                 if (hex) {
822                         wpa_snprintf_hex(hex, hex_len, rx_mgmt->frame,
823                                          rx_mgmt->frame_len);
824                         wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-RX %s", hex);
825                         os_free(hex);
826                 }
827                 return 1;
828         }
829 #endif /* CONFIG_TESTING_OPTIONS */
830
831         hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
832         bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
833         if (bssid == NULL)
834                 return 0;
835
836         hapd = get_hapd_bssid(iface, bssid);
837         if (hapd == NULL) {
838                 u16 fc;
839                 fc = le_to_host16(hdr->frame_control);
840
841                 /*
842                  * Drop frames to unknown BSSIDs except for Beacon frames which
843                  * could be used to update neighbor information.
844                  */
845                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
846                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
847                         hapd = iface->bss[0];
848                 else
849                         return 0;
850         }
851
852         os_memset(&fi, 0, sizeof(fi));
853         fi.datarate = rx_mgmt->datarate;
854         fi.ssi_signal = rx_mgmt->ssi_signal;
855
856         if (hapd == HAPD_BROADCAST) {
857                 size_t i;
858                 ret = 0;
859                 for (i = 0; i < iface->num_bss; i++) {
860                         /* if bss is set, driver will call this function for
861                          * each bss individually. */
862                         if (rx_mgmt->drv_priv &&
863                             (iface->bss[i]->drv_priv != rx_mgmt->drv_priv))
864                                 continue;
865
866                         if (ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
867                                             rx_mgmt->frame_len, &fi) > 0)
868                                 ret = 1;
869                 }
870         } else
871                 ret = ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len,
872                                       &fi);
873
874         random_add_randomness(&fi, sizeof(fi));
875
876         return ret;
877 }
878
879
880 static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
881                                size_t len, u16 stype, int ok)
882 {
883         struct ieee80211_hdr *hdr;
884         hdr = (struct ieee80211_hdr *) buf;
885         hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
886         if (hapd == NULL || hapd == HAPD_BROADCAST)
887                 return;
888         ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
889 }
890
891 #endif /* NEED_AP_MLME */
892
893
894 static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
895 {
896         struct sta_info *sta = ap_get_sta(hapd, addr);
897         if (sta)
898                 return 0;
899
900         wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
901                    " - adding a new STA", MAC2STR(addr));
902         sta = ap_sta_add(hapd, addr);
903         if (sta) {
904                 hostapd_new_assoc_sta(hapd, sta, 0);
905         } else {
906                 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
907                            MAC2STR(addr));
908                 return -1;
909         }
910
911         return 0;
912 }
913
914
915 static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
916                                    const u8 *data, size_t data_len)
917 {
918         struct hostapd_iface *iface = hapd->iface;
919         struct sta_info *sta;
920         size_t j;
921
922         for (j = 0; j < iface->num_bss; j++) {
923                 if ((sta = ap_get_sta(iface->bss[j], src))) {
924                         if (sta->flags & WLAN_STA_ASSOC) {
925                                 hapd = iface->bss[j];
926                                 break;
927                         }
928                 }
929         }
930
931         ieee802_1x_receive(hapd, src, data, data_len);
932 }
933
934
935 static struct hostapd_channel_data * hostapd_get_mode_channel(
936         struct hostapd_iface *iface, unsigned int freq)
937 {
938         int i;
939         struct hostapd_channel_data *chan;
940
941         for (i = 0; i < iface->current_mode->num_channels; i++) {
942                 chan = &iface->current_mode->channels[i];
943                 if (!chan)
944                         return NULL;
945                 if ((unsigned int) chan->freq == freq)
946                         return chan;
947         }
948
949         return NULL;
950 }
951
952
953 static void hostapd_update_nf(struct hostapd_iface *iface,
954                               struct hostapd_channel_data *chan,
955                               struct freq_survey *survey)
956 {
957         if (!iface->chans_surveyed) {
958                 chan->min_nf = survey->nf;
959                 iface->lowest_nf = survey->nf;
960         } else {
961                 if (dl_list_empty(&chan->survey_list))
962                         chan->min_nf = survey->nf;
963                 else if (survey->nf < chan->min_nf)
964                         chan->min_nf = survey->nf;
965                 if (survey->nf < iface->lowest_nf)
966                         iface->lowest_nf = survey->nf;
967         }
968 }
969
970
971 static void hostapd_single_channel_get_survey(struct hostapd_iface *iface,
972                                               struct survey_results *survey_res)
973 {
974         struct hostapd_channel_data *chan;
975         struct freq_survey *survey;
976         u64 divisor, dividend;
977
978         survey = dl_list_first(&survey_res->survey_list, struct freq_survey,
979                                list);
980         if (!survey || !survey->freq)
981                 return;
982
983         chan = hostapd_get_mode_channel(iface, survey->freq);
984         if (!chan || chan->flag & HOSTAPD_CHAN_DISABLED)
985                 return;
986
987         wpa_printf(MSG_DEBUG, "Single Channel Survey: (freq=%d channel_time=%ld channel_time_busy=%ld)",
988                    survey->freq,
989                    (unsigned long int) survey->channel_time,
990                    (unsigned long int) survey->channel_time_busy);
991
992         if (survey->channel_time > iface->last_channel_time &&
993             survey->channel_time > survey->channel_time_busy) {
994                 dividend = survey->channel_time_busy -
995                         iface->last_channel_time_busy;
996                 divisor = survey->channel_time - iface->last_channel_time;
997
998                 iface->channel_utilization = dividend * 255 / divisor;
999                 wpa_printf(MSG_DEBUG, "Channel Utilization: %d",
1000                            iface->channel_utilization);
1001         }
1002         iface->last_channel_time = survey->channel_time;
1003         iface->last_channel_time_busy = survey->channel_time_busy;
1004 }
1005
1006
1007 static void hostapd_event_get_survey(struct hostapd_data *hapd,
1008                                      struct survey_results *survey_results)
1009 {
1010         struct hostapd_iface *iface = hapd->iface;
1011         struct freq_survey *survey, *tmp;
1012         struct hostapd_channel_data *chan;
1013
1014         if (dl_list_empty(&survey_results->survey_list)) {
1015                 wpa_printf(MSG_DEBUG, "No survey data received");
1016                 return;
1017         }
1018
1019         if (survey_results->freq_filter) {
1020                 hostapd_single_channel_get_survey(iface, survey_results);
1021                 return;
1022         }
1023
1024         dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
1025                               struct freq_survey, list) {
1026                 chan = hostapd_get_mode_channel(iface, survey->freq);
1027                 if (!chan)
1028                         continue;
1029                 if (chan->flag & HOSTAPD_CHAN_DISABLED)
1030                         continue;
1031
1032                 dl_list_del(&survey->list);
1033                 dl_list_add_tail(&chan->survey_list, &survey->list);
1034
1035                 hostapd_update_nf(iface, chan, survey);
1036
1037                 iface->chans_surveyed++;
1038         }
1039 }
1040
1041
1042 #ifdef NEED_AP_MLME
1043
1044 static void hostapd_event_iface_unavailable(struct hostapd_data *hapd)
1045 {
1046         wpa_printf(MSG_DEBUG, "Interface %s is unavailable -- stopped",
1047                    hapd->conf->iface);
1048
1049         if (hapd->csa_in_progress) {
1050                 wpa_printf(MSG_INFO, "CSA failed (%s was stopped)",
1051                            hapd->conf->iface);
1052                 hostapd_switch_channel_fallback(hapd->iface,
1053                                                 &hapd->cs_freq_params);
1054         }
1055 }
1056
1057
1058 static void hostapd_event_dfs_radar_detected(struct hostapd_data *hapd,
1059                                              struct dfs_event *radar)
1060 {
1061         wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
1062         hostapd_dfs_radar_detected(hapd->iface, radar->freq, radar->ht_enabled,
1063                                    radar->chan_offset, radar->chan_width,
1064                                    radar->cf1, radar->cf2);
1065 }
1066
1067
1068 static void hostapd_event_dfs_cac_finished(struct hostapd_data *hapd,
1069                                            struct dfs_event *radar)
1070 {
1071         wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
1072         hostapd_dfs_complete_cac(hapd->iface, 1, radar->freq, radar->ht_enabled,
1073                                  radar->chan_offset, radar->chan_width,
1074                                  radar->cf1, radar->cf2);
1075 }
1076
1077
1078 static void hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd,
1079                                           struct dfs_event *radar)
1080 {
1081         wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
1082         hostapd_dfs_complete_cac(hapd->iface, 0, radar->freq, radar->ht_enabled,
1083                                  radar->chan_offset, radar->chan_width,
1084                                  radar->cf1, radar->cf2);
1085 }
1086
1087
1088 static void hostapd_event_dfs_nop_finished(struct hostapd_data *hapd,
1089                                            struct dfs_event *radar)
1090 {
1091         wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
1092         hostapd_dfs_nop_finished(hapd->iface, radar->freq, radar->ht_enabled,
1093                                  radar->chan_offset, radar->chan_width,
1094                                  radar->cf1, radar->cf2);
1095 }
1096
1097
1098 static void hostapd_event_dfs_cac_started(struct hostapd_data *hapd,
1099                                           struct dfs_event *radar)
1100 {
1101         wpa_printf(MSG_DEBUG, "DFS offload CAC started on %d MHz", radar->freq);
1102         hostapd_dfs_start_cac(hapd->iface, radar->freq, radar->ht_enabled,
1103                               radar->chan_offset, radar->chan_width,
1104                               radar->cf1, radar->cf2);
1105 }
1106
1107 #endif /* NEED_AP_MLME */
1108
1109
1110 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1111                           union wpa_event_data *data)
1112 {
1113         struct hostapd_data *hapd = ctx;
1114 #ifndef CONFIG_NO_STDOUT_DEBUG
1115         int level = MSG_DEBUG;
1116
1117         if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
1118             data->rx_mgmt.frame_len >= 24) {
1119                 const struct ieee80211_hdr *hdr;
1120                 u16 fc;
1121                 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
1122                 fc = le_to_host16(hdr->frame_control);
1123                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1124                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
1125                         level = MSG_EXCESSIVE;
1126                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1127                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
1128                         level = MSG_EXCESSIVE;
1129         }
1130
1131         wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
1132                 event_to_string(event), event);
1133 #endif /* CONFIG_NO_STDOUT_DEBUG */
1134
1135         switch (event) {
1136         case EVENT_MICHAEL_MIC_FAILURE:
1137                 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
1138                 break;
1139         case EVENT_SCAN_RESULTS:
1140                 if (hapd->iface->scan_cb)
1141                         hapd->iface->scan_cb(hapd->iface);
1142                 break;
1143         case EVENT_WPS_BUTTON_PUSHED:
1144                 hostapd_wps_button_pushed(hapd, NULL);
1145                 break;
1146 #ifdef NEED_AP_MLME
1147         case EVENT_TX_STATUS:
1148                 switch (data->tx_status.type) {
1149                 case WLAN_FC_TYPE_MGMT:
1150                         hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
1151                                            data->tx_status.data_len,
1152                                            data->tx_status.stype,
1153                                            data->tx_status.ack);
1154                         break;
1155                 case WLAN_FC_TYPE_DATA:
1156                         hostapd_tx_status(hapd, data->tx_status.dst,
1157                                           data->tx_status.data,
1158                                           data->tx_status.data_len,
1159                                           data->tx_status.ack);
1160                         break;
1161                 }
1162                 break;
1163         case EVENT_EAPOL_TX_STATUS:
1164                 hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
1165                                         data->eapol_tx_status.data,
1166                                         data->eapol_tx_status.data_len,
1167                                         data->eapol_tx_status.ack);
1168                 break;
1169         case EVENT_DRIVER_CLIENT_POLL_OK:
1170                 hostapd_client_poll_ok(hapd, data->client_poll.addr);
1171                 break;
1172         case EVENT_RX_FROM_UNKNOWN:
1173                 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
1174                                             data->rx_from_unknown.addr,
1175                                             data->rx_from_unknown.wds);
1176                 break;
1177 #endif /* NEED_AP_MLME */
1178         case EVENT_RX_MGMT:
1179                 if (!data->rx_mgmt.frame)
1180                         break;
1181 #ifdef NEED_AP_MLME
1182                 if (hostapd_mgmt_rx(hapd, &data->rx_mgmt) > 0)
1183                         break;
1184 #endif /* NEED_AP_MLME */
1185                 hostapd_action_rx(hapd, &data->rx_mgmt);
1186                 break;
1187         case EVENT_RX_PROBE_REQ:
1188                 if (data->rx_probe_req.sa == NULL ||
1189                     data->rx_probe_req.ie == NULL)
1190                         break;
1191                 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
1192                                      data->rx_probe_req.da,
1193                                      data->rx_probe_req.bssid,
1194                                      data->rx_probe_req.ie,
1195                                      data->rx_probe_req.ie_len,
1196                                      data->rx_probe_req.ssi_signal);
1197                 break;
1198         case EVENT_NEW_STA:
1199                 hostapd_event_new_sta(hapd, data->new_sta.addr);
1200                 break;
1201         case EVENT_EAPOL_RX:
1202                 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
1203                                        data->eapol_rx.data,
1204                                        data->eapol_rx.data_len);
1205                 break;
1206         case EVENT_ASSOC:
1207                 if (!data)
1208                         return;
1209                 hostapd_notif_assoc(hapd, data->assoc_info.addr,
1210                                     data->assoc_info.req_ies,
1211                                     data->assoc_info.req_ies_len,
1212                                     data->assoc_info.reassoc);
1213                 break;
1214         case EVENT_DISASSOC:
1215                 if (data)
1216                         hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
1217                 break;
1218         case EVENT_DEAUTH:
1219                 if (data)
1220                         hostapd_notif_disassoc(hapd, data->deauth_info.addr);
1221                 break;
1222         case EVENT_STATION_LOW_ACK:
1223                 if (!data)
1224                         break;
1225                 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
1226                 break;
1227         case EVENT_AUTH:
1228                 hostapd_notif_auth(hapd, &data->auth);
1229                 break;
1230         case EVENT_CH_SWITCH:
1231                 if (!data)
1232                         break;
1233                 hostapd_event_ch_switch(hapd, data->ch_switch.freq,
1234                                         data->ch_switch.ht_enabled,
1235                                         data->ch_switch.ch_offset,
1236                                         data->ch_switch.ch_width,
1237                                         data->ch_switch.cf1,
1238                                         data->ch_switch.cf2);
1239                 break;
1240         case EVENT_CONNECT_FAILED_REASON:
1241                 if (!data)
1242                         break;
1243                 hostapd_event_connect_failed_reason(
1244                         hapd, data->connect_failed_reason.addr,
1245                         data->connect_failed_reason.code);
1246                 break;
1247         case EVENT_SURVEY:
1248                 hostapd_event_get_survey(hapd, &data->survey_results);
1249                 break;
1250 #ifdef NEED_AP_MLME
1251         case EVENT_INTERFACE_UNAVAILABLE:
1252                 hostapd_event_iface_unavailable(hapd);
1253                 break;
1254         case EVENT_DFS_RADAR_DETECTED:
1255                 if (!data)
1256                         break;
1257                 hostapd_event_dfs_radar_detected(hapd, &data->dfs_event);
1258                 break;
1259         case EVENT_DFS_CAC_FINISHED:
1260                 if (!data)
1261                         break;
1262                 hostapd_event_dfs_cac_finished(hapd, &data->dfs_event);
1263                 break;
1264         case EVENT_DFS_CAC_ABORTED:
1265                 if (!data)
1266                         break;
1267                 hostapd_event_dfs_cac_aborted(hapd, &data->dfs_event);
1268                 break;
1269         case EVENT_DFS_NOP_FINISHED:
1270                 if (!data)
1271                         break;
1272                 hostapd_event_dfs_nop_finished(hapd, &data->dfs_event);
1273                 break;
1274         case EVENT_CHANNEL_LIST_CHANGED:
1275                 /* channel list changed (regulatory?), update channel list */
1276                 /* TODO: check this. hostapd_get_hw_features() initializes
1277                  * too much stuff. */
1278                 /* hostapd_get_hw_features(hapd->iface); */
1279                 hostapd_channel_list_updated(
1280                         hapd->iface, data->channel_list_changed.initiator);
1281                 break;
1282         case EVENT_DFS_CAC_STARTED:
1283                 if (!data)
1284                         break;
1285                 hostapd_event_dfs_cac_started(hapd, &data->dfs_event);
1286                 break;
1287 #endif /* NEED_AP_MLME */
1288         case EVENT_INTERFACE_ENABLED:
1289                 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_ENABLED);
1290                 if (hapd->disabled && hapd->started) {
1291                         hapd->disabled = 0;
1292                         /*
1293                          * Try to re-enable interface if the driver stopped it
1294                          * when the interface got disabled.
1295                          */
1296                         wpa_auth_reconfig_group_keys(hapd->wpa_auth);
1297                         hapd->reenable_beacon = 1;
1298                         ieee802_11_set_beacon(hapd);
1299                 }
1300                 break;
1301         case EVENT_INTERFACE_DISABLED:
1302                 hostapd_free_stas(hapd);
1303                 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_DISABLED);
1304                 hapd->disabled = 1;
1305                 break;
1306 #ifdef CONFIG_ACS
1307         case EVENT_ACS_CHANNEL_SELECTED:
1308                 hostapd_acs_channel_selected(hapd,
1309                                              &data->acs_selected_channels);
1310                 break;
1311 #endif /* CONFIG_ACS */
1312         default:
1313                 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
1314                 break;
1315         }
1316 }
1317
1318 #endif /* HOSTAPD */