FT: Add FT AP support for drivers that manage MLME internally
[mech_eap.git] / src / ap / drv_callbacks.c
1 /*
2  * hostapd / Callback functions for driver wrappers
3  * Copyright (c) 2002-2009, 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 "radius/radius.h"
13 #include "drivers/driver.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/ieee802_11_common.h"
16 #include "crypto/random.h"
17 #include "p2p/p2p.h"
18 #include "wps/wps.h"
19 #include "hostapd.h"
20 #include "ieee802_11.h"
21 #include "sta_info.h"
22 #include "accounting.h"
23 #include "tkip_countermeasures.h"
24 #include "ieee802_1x.h"
25 #include "wpa_auth.h"
26 #include "wps_hostapd.h"
27 #include "ap_drv_ops.h"
28 #include "ap_config.h"
29 #include "hw_features.h"
30
31
32 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
33                         const u8 *req_ies, size_t req_ies_len, int reassoc)
34 {
35         struct sta_info *sta;
36         int new_assoc, res;
37         struct ieee802_11_elems elems;
38         const u8 *ie;
39         size_t ielen;
40 #ifdef CONFIG_IEEE80211R
41         u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
42         u8 *p = buf;
43 #endif /* CONFIG_IEEE80211R */
44         u16 reason = WLAN_REASON_UNSPECIFIED;
45         u16 status = WLAN_STATUS_SUCCESS;
46
47         if (addr == NULL) {
48                 /*
49                  * This could potentially happen with unexpected event from the
50                  * driver wrapper. This was seen at least in one case where the
51                  * driver ended up being set to station mode while hostapd was
52                  * running, so better make sure we stop processing such an
53                  * event here.
54                  */
55                 wpa_printf(MSG_DEBUG, "hostapd_notif_assoc: Skip event with "
56                            "no address");
57                 return -1;
58         }
59         random_add_randomness(addr, ETH_ALEN);
60
61         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
62                        HOSTAPD_LEVEL_INFO, "associated");
63
64         ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0);
65         if (elems.wps_ie) {
66                 ie = elems.wps_ie - 2;
67                 ielen = elems.wps_ie_len + 2;
68                 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
69         } else if (elems.rsn_ie) {
70                 ie = elems.rsn_ie - 2;
71                 ielen = elems.rsn_ie_len + 2;
72                 wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
73         } else if (elems.wpa_ie) {
74                 ie = elems.wpa_ie - 2;
75                 ielen = elems.wpa_ie_len + 2;
76                 wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
77         } else {
78                 ie = NULL;
79                 ielen = 0;
80                 wpa_printf(MSG_DEBUG, "STA did not include WPS/RSN/WPA IE in "
81                            "(Re)AssocReq");
82         }
83
84         sta = ap_get_sta(hapd, addr);
85         if (sta) {
86                 accounting_sta_stop(hapd, sta);
87
88                 /*
89                  * Make sure that the previously registered inactivity timer
90                  * will not remove the STA immediately.
91                  */
92                 sta->timeout_next = STA_NULLFUNC;
93         } else {
94                 sta = ap_sta_add(hapd, addr);
95                 if (sta == NULL) {
96                         hostapd_drv_sta_disassoc(hapd, addr,
97                                                  WLAN_REASON_DISASSOC_AP_BUSY);
98                         return -1;
99                 }
100         }
101         sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
102
103 #ifdef CONFIG_P2P
104         if (elems.p2p) {
105                 wpabuf_free(sta->p2p_ie);
106                 sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
107                                                           P2P_IE_VENDOR_TYPE);
108         }
109 #endif /* CONFIG_P2P */
110
111         if (hapd->conf->wpa) {
112                 if (ie == NULL || ielen == 0) {
113 #ifdef CONFIG_WPS
114                         if (hapd->conf->wps_state) {
115                                 wpa_printf(MSG_DEBUG, "STA did not include "
116                                            "WPA/RSN IE in (Re)Association "
117                                            "Request - possible WPS use");
118                                 sta->flags |= WLAN_STA_MAYBE_WPS;
119                                 goto skip_wpa_check;
120                         }
121 #endif /* CONFIG_WPS */
122
123                         wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
124                         return -1;
125                 }
126 #ifdef CONFIG_WPS
127                 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
128                     os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
129                         struct wpabuf *wps;
130                         sta->flags |= WLAN_STA_WPS;
131                         wps = ieee802_11_vendor_ie_concat(ie, ielen,
132                                                           WPS_IE_VENDOR_TYPE);
133                         if (wps) {
134                                 if (wps_is_20(wps)) {
135                                         wpa_printf(MSG_DEBUG, "WPS: STA "
136                                                    "supports WPS 2.0");
137                                         sta->flags |= WLAN_STA_WPS2;
138                                 }
139                                 wpabuf_free(wps);
140                         }
141                         goto skip_wpa_check;
142                 }
143 #endif /* CONFIG_WPS */
144
145                 if (sta->wpa_sm == NULL)
146                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
147                                                         sta->addr);
148                 if (sta->wpa_sm == NULL) {
149                         wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
150                                    "machine");
151                         return -1;
152                 }
153                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
154                                           ie, ielen,
155                                           elems.mdie, elems.mdie_len);
156                 if (res != WPA_IE_OK) {
157                         wpa_printf(MSG_DEBUG, "WPA/RSN information element "
158                                    "rejected? (res %u)", res);
159                         wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
160                         if (res == WPA_INVALID_GROUP) {
161                                 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
162                                 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
163                         } else if (res == WPA_INVALID_PAIRWISE) {
164                                 reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
165                                 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
166                         } else if (res == WPA_INVALID_AKMP) {
167                                 reason = WLAN_REASON_AKMP_NOT_VALID;
168                                 status = WLAN_STATUS_AKMP_NOT_VALID;
169                         }
170 #ifdef CONFIG_IEEE80211W
171                         else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) {
172                                 reason = WLAN_REASON_INVALID_IE;
173                                 status = WLAN_STATUS_INVALID_IE;
174                         } else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) {
175                                 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
176                                 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
177                         }
178 #endif /* CONFIG_IEEE80211W */
179                         else {
180                                 reason = WLAN_REASON_INVALID_IE;
181                                 status = WLAN_STATUS_INVALID_IE;
182                         }
183                         goto fail;
184                 }
185 #ifdef CONFIG_IEEE80211R
186                 if (sta->auth_alg == WLAN_AUTH_FT) {
187                         status = wpa_ft_validate_reassoc(sta->wpa_sm, req_ies,
188                                                          req_ies_len);
189                         if (status != WLAN_STATUS_SUCCESS) {
190                                 if (status == WLAN_STATUS_INVALID_PMKID)
191                                         reason = WLAN_REASON_INVALID_IE;
192                                 if (status == WLAN_STATUS_INVALID_MDIE)
193                                         reason = WLAN_REASON_INVALID_IE;
194                                 if (status == WLAN_STATUS_INVALID_FTIE)
195                                         reason = WLAN_REASON_INVALID_IE;
196                                 goto fail;
197                         }
198                 }
199 #endif /* CONFIG_IEEE80211R */
200         } else if (hapd->conf->wps_state) {
201 #ifdef CONFIG_WPS
202                 struct wpabuf *wps;
203                 if (req_ies)
204                         wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
205                                                           WPS_IE_VENDOR_TYPE);
206                 else
207                         wps = NULL;
208 #ifdef CONFIG_WPS_STRICT
209                 if (wps && wps_validate_assoc_req(wps) < 0) {
210                         reason = WLAN_REASON_INVALID_IE;
211                         status = WLAN_STATUS_INVALID_IE;
212                         wpabuf_free(wps);
213                         goto fail;
214                 }
215 #endif /* CONFIG_WPS_STRICT */
216                 if (wps) {
217                         sta->flags |= WLAN_STA_WPS;
218                         if (wps_is_20(wps)) {
219                                 wpa_printf(MSG_DEBUG, "WPS: STA supports "
220                                            "WPS 2.0");
221                                 sta->flags |= WLAN_STA_WPS2;
222                         }
223                 } else
224                         sta->flags |= WLAN_STA_MAYBE_WPS;
225                 wpabuf_free(wps);
226 #endif /* CONFIG_WPS */
227         }
228 #ifdef CONFIG_WPS
229 skip_wpa_check:
230 #endif /* CONFIG_WPS */
231
232 #ifdef CONFIG_IEEE80211R
233         p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
234                                         sta->auth_alg, req_ies, req_ies_len);
235
236         hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
237 #else /* CONFIG_IEEE80211R */
238         /* Keep compiler silent about unused variables */
239         if (status) {
240         }
241 #endif /* CONFIG_IEEE80211R */
242
243         new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
244         sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
245
246         if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
247                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
248         else
249                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
250
251         hostapd_new_assoc_sta(hapd, sta, !new_assoc);
252
253         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
254
255 #ifdef CONFIG_P2P
256         if (req_ies) {
257                 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
258                                       req_ies, req_ies_len);
259         }
260 #endif /* CONFIG_P2P */
261
262         return 0;
263
264 fail:
265 #ifdef CONFIG_IEEE80211R
266         hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
267 #endif /* CONFIG_IEEE80211R */
268         hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
269         ap_free_sta(hapd, sta);
270         return -1;
271 }
272
273
274 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
275 {
276         struct sta_info *sta;
277
278         if (addr == NULL) {
279                 /*
280                  * This could potentially happen with unexpected event from the
281                  * driver wrapper. This was seen at least in one case where the
282                  * driver ended up reporting a station mode event while hostapd
283                  * was running, so better make sure we stop processing such an
284                  * event here.
285                  */
286                 wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event "
287                            "with no address");
288                 return;
289         }
290
291         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
292                        HOSTAPD_LEVEL_INFO, "disassociated");
293
294         sta = ap_get_sta(hapd, addr);
295         if (sta == NULL) {
296                 wpa_printf(MSG_DEBUG, "Disassociation notification for "
297                            "unknown STA " MACSTR, MAC2STR(addr));
298                 return;
299         }
300
301         ap_sta_set_authorized(hapd, sta, 0);
302         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
303         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
304         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
305         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
306         ap_free_sta(hapd, sta);
307 }
308
309
310 void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
311 {
312         struct sta_info *sta = ap_get_sta(hapd, addr);
313
314         if (!sta || !hapd->conf->disassoc_low_ack)
315                 return;
316
317         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
318                        HOSTAPD_LEVEL_INFO, "disconnected due to excessive "
319                        "missing ACKs");
320         hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
321         if (sta)
322                 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
323 }
324
325
326 void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
327                              int offset)
328 {
329 #ifdef NEED_AP_MLME
330         int channel;
331
332         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
333                        HOSTAPD_LEVEL_INFO, "driver had channel switch: "
334                        "freq=%d, ht=%d, offset=%d", freq, ht, offset);
335
336         hapd->iface->freq = freq;
337
338         channel = hostapd_hw_get_channel(hapd, freq);
339         if (!channel) {
340                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
341                                HOSTAPD_LEVEL_WARNING, "driver switched to "
342                                "bad channel!");
343                 return;
344         }
345
346         hapd->iconf->channel = channel;
347         hapd->iconf->ieee80211n = ht;
348         hapd->iconf->secondary_channel = offset;
349 #endif /* NEED_AP_MLME */
350 }
351
352
353 int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
354                          const u8 *bssid, const u8 *ie, size_t ie_len,
355                          int ssi_signal)
356 {
357         size_t i;
358         int ret = 0;
359
360         if (sa == NULL || ie == NULL)
361                 return -1;
362
363         random_add_randomness(sa, ETH_ALEN);
364         for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
365                 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
366                                             sa, da, bssid, ie, ie_len,
367                                             ssi_signal) > 0) {
368                         ret = 1;
369                         break;
370                 }
371         }
372         return ret;
373 }
374
375
376 #ifdef CONFIG_IEEE80211R
377 static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
378                                           const u8 *bssid,
379                                           u16 auth_transaction, u16 status,
380                                           const u8 *ies, size_t ies_len)
381 {
382         struct hostapd_data *hapd = ctx;
383         struct sta_info *sta;
384
385         sta = ap_get_sta(hapd, dst);
386         if (sta == NULL)
387                 return;
388
389         hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
390                        HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
391         sta->flags |= WLAN_STA_AUTH;
392
393         hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
394 }
395 #endif /* CONFIG_IEEE80211R */
396
397
398 #ifdef HOSTAPD
399
400 static void hostapd_notif_auth(struct hostapd_data *hapd,
401                                struct auth_info *rx_auth)
402 {
403         struct sta_info *sta;
404         u16 status = WLAN_STATUS_SUCCESS;
405         u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
406         size_t resp_ies_len = 0;
407
408         sta = ap_get_sta(hapd, rx_auth->peer);
409         if (!sta) {
410                 sta = ap_sta_add(hapd, rx_auth->peer);
411                 if (sta == NULL) {
412                         status = WLAN_STATUS_UNSPECIFIED_FAILURE;
413                         goto fail;
414                 }
415         }
416         sta->flags &= ~WLAN_STA_PREAUTH;
417         ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
418 #ifdef CONFIG_IEEE80211R
419         if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
420                 sta->auth_alg = WLAN_AUTH_FT;
421                 if (sta->wpa_sm == NULL)
422                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
423                                                         sta->addr);
424                 if (sta->wpa_sm == NULL) {
425                         wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
426                                    "state machine");
427                         status = WLAN_STATUS_UNSPECIFIED_FAILURE;
428                         goto fail;
429                 }
430                 wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
431                                     rx_auth->auth_transaction, rx_auth->ies,
432                                     rx_auth->ies_len,
433                                     hostapd_notify_auth_ft_finish, hapd);
434                 return;
435         }
436 #endif /* CONFIG_IEEE80211R */
437 fail:
438         hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
439                          status, resp_ies, resp_ies_len);
440 }
441
442
443 static void hostapd_action_rx(struct hostapd_data *hapd,
444                               struct rx_action *action)
445 {
446         struct sta_info *sta;
447
448         sta = ap_get_sta(hapd, action->sa);
449         if (sta == NULL) {
450                 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
451                 return;
452         }
453 #ifdef CONFIG_IEEE80211R
454         if (action->category == WLAN_ACTION_FT) {
455                 wpa_printf(MSG_DEBUG, "%s: FT_ACTION length %d",
456                            __func__, (int) action->len);
457                 wpa_ft_action_rx(sta->wpa_sm, action->data, action->len);
458         }
459 #endif /* CONFIG_IEEE80211R */
460 }
461
462
463 #ifdef NEED_AP_MLME
464
465 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
466
467 static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
468                                             const u8 *bssid)
469 {
470         size_t i;
471
472         if (bssid == NULL)
473                 return NULL;
474         if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
475             bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
476                 return HAPD_BROADCAST;
477
478         for (i = 0; i < iface->num_bss; i++) {
479                 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
480                         return iface->bss[i];
481         }
482
483         return NULL;
484 }
485
486
487 static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
488                                         const u8 *bssid, const u8 *addr,
489                                         int wds)
490 {
491         hapd = get_hapd_bssid(hapd->iface, bssid);
492         if (hapd == NULL || hapd == HAPD_BROADCAST)
493                 return;
494
495         ieee802_11_rx_from_unknown(hapd, addr, wds);
496 }
497
498
499 static void hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
500 {
501         struct hostapd_iface *iface = hapd->iface;
502         const struct ieee80211_hdr *hdr;
503         const u8 *bssid;
504         struct hostapd_frame_info fi;
505
506         hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
507         bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
508         if (bssid == NULL)
509                 return;
510
511         hapd = get_hapd_bssid(iface, bssid);
512         if (hapd == NULL) {
513                 u16 fc;
514                 fc = le_to_host16(hdr->frame_control);
515
516                 /*
517                  * Drop frames to unknown BSSIDs except for Beacon frames which
518                  * could be used to update neighbor information.
519                  */
520                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
521                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
522                         hapd = iface->bss[0];
523                 else
524                         return;
525         }
526
527         os_memset(&fi, 0, sizeof(fi));
528         fi.datarate = rx_mgmt->datarate;
529         fi.ssi_signal = rx_mgmt->ssi_signal;
530
531         if (hapd == HAPD_BROADCAST) {
532                 size_t i;
533                 for (i = 0; i < iface->num_bss; i++)
534                         ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
535                                         rx_mgmt->frame_len, &fi);
536         } else
537                 ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len, &fi);
538
539         random_add_randomness(&fi, sizeof(fi));
540 }
541
542
543 static void hostapd_rx_action(struct hostapd_data *hapd,
544                               struct rx_action *rx_action)
545 {
546         struct rx_mgmt rx_mgmt;
547         u8 *buf;
548         struct ieee80211_hdr *hdr;
549
550         wpa_printf(MSG_DEBUG, "EVENT_RX_ACTION DA=" MACSTR " SA=" MACSTR
551                    " BSSID=" MACSTR " category=%u",
552                    MAC2STR(rx_action->da), MAC2STR(rx_action->sa),
553                    MAC2STR(rx_action->bssid), rx_action->category);
554         wpa_hexdump(MSG_MSGDUMP, "Received action frame contents",
555                     rx_action->data, rx_action->len);
556
557         buf = os_zalloc(24 + 1 + rx_action->len);
558         if (buf == NULL)
559                 return;
560         hdr = (struct ieee80211_hdr *) buf;
561         hdr->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
562                                           WLAN_FC_STYPE_ACTION);
563         if (rx_action->category == WLAN_ACTION_SA_QUERY) {
564                 /*
565                  * Assume frame was protected; it would have been dropped if
566                  * not.
567                  */
568                 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
569         }
570         os_memcpy(hdr->addr1, rx_action->da, ETH_ALEN);
571         os_memcpy(hdr->addr2, rx_action->sa, ETH_ALEN);
572         os_memcpy(hdr->addr3, rx_action->bssid, ETH_ALEN);
573         buf[24] = rx_action->category;
574         os_memcpy(buf + 24 + 1, rx_action->data, rx_action->len);
575         os_memset(&rx_mgmt, 0, sizeof(rx_mgmt));
576         rx_mgmt.frame = buf;
577         rx_mgmt.frame_len = 24 + 1 + rx_action->len;
578         hostapd_mgmt_rx(hapd, &rx_mgmt);
579         os_free(buf);
580 }
581
582
583 static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
584                                size_t len, u16 stype, int ok)
585 {
586         struct ieee80211_hdr *hdr;
587         hdr = (struct ieee80211_hdr *) buf;
588         hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
589         if (hapd == NULL || hapd == HAPD_BROADCAST)
590                 return;
591         ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
592 }
593
594 #endif /* NEED_AP_MLME */
595
596
597 static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
598 {
599         struct sta_info *sta = ap_get_sta(hapd, addr);
600         if (sta)
601                 return 0;
602
603         wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
604                    " - adding a new STA", MAC2STR(addr));
605         sta = ap_sta_add(hapd, addr);
606         if (sta) {
607                 hostapd_new_assoc_sta(hapd, sta, 0);
608         } else {
609                 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
610                            MAC2STR(addr));
611                 return -1;
612         }
613
614         return 0;
615 }
616
617
618 static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
619                                    const u8 *data, size_t data_len)
620 {
621         struct hostapd_iface *iface = hapd->iface;
622         size_t j;
623
624         for (j = 0; j < iface->num_bss; j++) {
625                 if (ap_get_sta(iface->bss[j], src)) {
626                         hapd = iface->bss[j];
627                         break;
628                 }
629         }
630
631         ieee802_1x_receive(hapd, src, data, data_len);
632 }
633
634
635 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
636                           union wpa_event_data *data)
637 {
638         struct hostapd_data *hapd = ctx;
639 #ifndef CONFIG_NO_STDOUT_DEBUG
640         int level = MSG_DEBUG;
641
642         if (event == EVENT_RX_MGMT && data && data->rx_mgmt.frame &&
643             data->rx_mgmt.frame_len >= 24) {
644                 const struct ieee80211_hdr *hdr;
645                 u16 fc;
646                 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
647                 fc = le_to_host16(hdr->frame_control);
648                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
649                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
650                         level = MSG_EXCESSIVE;
651         }
652
653         wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
654                 event_to_string(event), event);
655 #endif /* CONFIG_NO_STDOUT_DEBUG */
656
657         switch (event) {
658         case EVENT_MICHAEL_MIC_FAILURE:
659                 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
660                 break;
661         case EVENT_SCAN_RESULTS:
662                 if (hapd->iface->scan_cb)
663                         hapd->iface->scan_cb(hapd->iface);
664                 break;
665 #ifdef CONFIG_IEEE80211R
666         case EVENT_FT_RRB_RX:
667                 wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
668                               data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
669                 break;
670 #endif /* CONFIG_IEEE80211R */
671         case EVENT_WPS_BUTTON_PUSHED:
672                 hostapd_wps_button_pushed(hapd, NULL);
673                 break;
674 #ifdef NEED_AP_MLME
675         case EVENT_TX_STATUS:
676                 switch (data->tx_status.type) {
677                 case WLAN_FC_TYPE_MGMT:
678                         hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
679                                            data->tx_status.data_len,
680                                            data->tx_status.stype,
681                                            data->tx_status.ack);
682                         break;
683                 case WLAN_FC_TYPE_DATA:
684                         hostapd_tx_status(hapd, data->tx_status.dst,
685                                           data->tx_status.data,
686                                           data->tx_status.data_len,
687                                           data->tx_status.ack);
688                         break;
689                 }
690                 break;
691         case EVENT_EAPOL_TX_STATUS:
692                 hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
693                                         data->eapol_tx_status.data,
694                                         data->eapol_tx_status.data_len,
695                                         data->eapol_tx_status.ack);
696                 break;
697         case EVENT_DRIVER_CLIENT_POLL_OK:
698                 hostapd_client_poll_ok(hapd, data->client_poll.addr);
699                 break;
700         case EVENT_RX_FROM_UNKNOWN:
701                 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
702                                             data->rx_from_unknown.addr,
703                                             data->rx_from_unknown.wds);
704                 break;
705         case EVENT_RX_MGMT:
706                 hostapd_mgmt_rx(hapd, &data->rx_mgmt);
707                 break;
708 #endif /* NEED_AP_MLME */
709         case EVENT_RX_PROBE_REQ:
710                 if (data->rx_probe_req.sa == NULL ||
711                     data->rx_probe_req.ie == NULL)
712                         break;
713                 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
714                                      data->rx_probe_req.da,
715                                      data->rx_probe_req.bssid,
716                                      data->rx_probe_req.ie,
717                                      data->rx_probe_req.ie_len,
718                                      data->rx_probe_req.ssi_signal);
719                 break;
720         case EVENT_NEW_STA:
721                 hostapd_event_new_sta(hapd, data->new_sta.addr);
722                 break;
723         case EVENT_EAPOL_RX:
724                 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
725                                        data->eapol_rx.data,
726                                        data->eapol_rx.data_len);
727                 break;
728         case EVENT_ASSOC:
729                 hostapd_notif_assoc(hapd, data->assoc_info.addr,
730                                     data->assoc_info.req_ies,
731                                     data->assoc_info.req_ies_len,
732                                     data->assoc_info.reassoc);
733                 break;
734         case EVENT_DISASSOC:
735                 if (data)
736                         hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
737                 break;
738         case EVENT_DEAUTH:
739                 if (data)
740                         hostapd_notif_disassoc(hapd, data->deauth_info.addr);
741                 break;
742         case EVENT_STATION_LOW_ACK:
743                 if (!data)
744                         break;
745                 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
746                 break;
747         case EVENT_RX_ACTION:
748                 if (data->rx_action.da == NULL || data->rx_action.sa == NULL ||
749                     data->rx_action.bssid == NULL)
750                         break;
751 #ifdef NEED_AP_MLME
752                 hostapd_rx_action(hapd, &data->rx_action);
753 #endif /* NEED_AP_MLME */
754                 hostapd_action_rx(hapd, &data->rx_action);
755                 break;
756         case EVENT_AUTH:
757                 hostapd_notif_auth(hapd, &data->auth);
758                 break;
759         case EVENT_CH_SWITCH:
760                 if (!data)
761                         break;
762                 hostapd_event_ch_switch(hapd, data->ch_switch.freq,
763                                         data->ch_switch.ht_enabled,
764                                         data->ch_switch.ch_offset);
765                 break;
766         default:
767                 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
768                 break;
769         }
770 }
771
772 #endif /* HOSTAPD */