fca4ae511d525e24bce141a01a7a6885bff14bdb
[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_IEEE80211W
186                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
187                     sta->sa_query_count > 0)
188                         ap_check_sa_query_timeout(hapd, sta);
189                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
190                     (sta->auth_alg != WLAN_AUTH_FT)) {
191                         /*
192                          * STA has already been associated with MFP and SA
193                          * Query timeout has not been reached. Reject the
194                          * association attempt temporarily and start SA Query,
195                          * if one is not pending.
196                          */
197
198                         if (sta->sa_query_count == 0)
199                                 ap_sta_start_sa_query(hapd, sta);
200
201 #ifdef CONFIG_IEEE80211R
202                         status = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
203
204                         p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
205
206                         hostapd_sta_assoc(hapd, addr, reassoc, status, buf,
207                                           p - buf);
208 #endif /* CONFIG_IEEE80211R */
209                         return 0;
210                 }
211
212                 if (wpa_auth_uses_mfp(sta->wpa_sm))
213                         sta->flags |= WLAN_STA_MFP;
214                 else
215                         sta->flags &= ~WLAN_STA_MFP;
216 #endif /* CONFIG_IEEE80211W */
217
218 #ifdef CONFIG_IEEE80211R
219                 if (sta->auth_alg == WLAN_AUTH_FT) {
220                         status = wpa_ft_validate_reassoc(sta->wpa_sm, req_ies,
221                                                          req_ies_len);
222                         if (status != WLAN_STATUS_SUCCESS) {
223                                 if (status == WLAN_STATUS_INVALID_PMKID)
224                                         reason = WLAN_REASON_INVALID_IE;
225                                 if (status == WLAN_STATUS_INVALID_MDIE)
226                                         reason = WLAN_REASON_INVALID_IE;
227                                 if (status == WLAN_STATUS_INVALID_FTIE)
228                                         reason = WLAN_REASON_INVALID_IE;
229                                 goto fail;
230                         }
231                 }
232 #endif /* CONFIG_IEEE80211R */
233         } else if (hapd->conf->wps_state) {
234 #ifdef CONFIG_WPS
235                 struct wpabuf *wps;
236                 if (req_ies)
237                         wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
238                                                           WPS_IE_VENDOR_TYPE);
239                 else
240                         wps = NULL;
241 #ifdef CONFIG_WPS_STRICT
242                 if (wps && wps_validate_assoc_req(wps) < 0) {
243                         reason = WLAN_REASON_INVALID_IE;
244                         status = WLAN_STATUS_INVALID_IE;
245                         wpabuf_free(wps);
246                         goto fail;
247                 }
248 #endif /* CONFIG_WPS_STRICT */
249                 if (wps) {
250                         sta->flags |= WLAN_STA_WPS;
251                         if (wps_is_20(wps)) {
252                                 wpa_printf(MSG_DEBUG, "WPS: STA supports "
253                                            "WPS 2.0");
254                                 sta->flags |= WLAN_STA_WPS2;
255                         }
256                 } else
257                         sta->flags |= WLAN_STA_MAYBE_WPS;
258                 wpabuf_free(wps);
259 #endif /* CONFIG_WPS */
260         }
261 #ifdef CONFIG_WPS
262 skip_wpa_check:
263 #endif /* CONFIG_WPS */
264
265 #ifdef CONFIG_IEEE80211R
266         p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
267                                         sta->auth_alg, req_ies, req_ies_len);
268
269         hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
270 #else /* CONFIG_IEEE80211R */
271         /* Keep compiler silent about unused variables */
272         if (status) {
273         }
274 #endif /* CONFIG_IEEE80211R */
275
276         new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
277         sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
278
279         if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
280                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
281         else
282                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
283
284         hostapd_new_assoc_sta(hapd, sta, !new_assoc);
285
286         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
287
288 #ifdef CONFIG_P2P
289         if (req_ies) {
290                 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
291                                       req_ies, req_ies_len);
292         }
293 #endif /* CONFIG_P2P */
294
295         return 0;
296
297 fail:
298 #ifdef CONFIG_IEEE80211R
299         hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
300 #endif /* CONFIG_IEEE80211R */
301         hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
302         ap_free_sta(hapd, sta);
303         return -1;
304 }
305
306
307 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
308 {
309         struct sta_info *sta;
310
311         if (addr == NULL) {
312                 /*
313                  * This could potentially happen with unexpected event from the
314                  * driver wrapper. This was seen at least in one case where the
315                  * driver ended up reporting a station mode event while hostapd
316                  * was running, so better make sure we stop processing such an
317                  * event here.
318                  */
319                 wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event "
320                            "with no address");
321                 return;
322         }
323
324         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
325                        HOSTAPD_LEVEL_INFO, "disassociated");
326
327         sta = ap_get_sta(hapd, addr);
328         if (sta == NULL) {
329                 wpa_printf(MSG_DEBUG, "Disassociation notification for "
330                            "unknown STA " MACSTR, MAC2STR(addr));
331                 return;
332         }
333
334         ap_sta_set_authorized(hapd, sta, 0);
335         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
336         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
337         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
338         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
339         ap_free_sta(hapd, sta);
340 }
341
342
343 void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
344 {
345         struct sta_info *sta = ap_get_sta(hapd, addr);
346
347         if (!sta || !hapd->conf->disassoc_low_ack)
348                 return;
349
350         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
351                        HOSTAPD_LEVEL_INFO, "disconnected due to excessive "
352                        "missing ACKs");
353         hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
354         if (sta)
355                 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
356 }
357
358
359 void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
360                              int offset)
361 {
362 #ifdef NEED_AP_MLME
363         int channel;
364
365         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
366                        HOSTAPD_LEVEL_INFO, "driver had channel switch: "
367                        "freq=%d, ht=%d, offset=%d", freq, ht, offset);
368
369         hapd->iface->freq = freq;
370
371         channel = hostapd_hw_get_channel(hapd, freq);
372         if (!channel) {
373                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
374                                HOSTAPD_LEVEL_WARNING, "driver switched to "
375                                "bad channel!");
376                 return;
377         }
378
379         hapd->iconf->channel = channel;
380         hapd->iconf->ieee80211n = ht;
381         hapd->iconf->secondary_channel = offset;
382 #endif /* NEED_AP_MLME */
383 }
384
385
386 int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
387                          const u8 *bssid, const u8 *ie, size_t ie_len,
388                          int ssi_signal)
389 {
390         size_t i;
391         int ret = 0;
392
393         if (sa == NULL || ie == NULL)
394                 return -1;
395
396         random_add_randomness(sa, ETH_ALEN);
397         for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
398                 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
399                                             sa, da, bssid, ie, ie_len,
400                                             ssi_signal) > 0) {
401                         ret = 1;
402                         break;
403                 }
404         }
405         return ret;
406 }
407
408
409 #ifdef HOSTAPD
410
411 #ifdef CONFIG_IEEE80211R
412 static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
413                                           const u8 *bssid,
414                                           u16 auth_transaction, u16 status,
415                                           const u8 *ies, size_t ies_len)
416 {
417         struct hostapd_data *hapd = ctx;
418         struct sta_info *sta;
419
420         sta = ap_get_sta(hapd, dst);
421         if (sta == NULL)
422                 return;
423
424         hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
425                        HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
426         sta->flags |= WLAN_STA_AUTH;
427
428         hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
429 }
430 #endif /* CONFIG_IEEE80211R */
431
432
433 static void hostapd_notif_auth(struct hostapd_data *hapd,
434                                struct auth_info *rx_auth)
435 {
436         struct sta_info *sta;
437         u16 status = WLAN_STATUS_SUCCESS;
438         u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
439         size_t resp_ies_len = 0;
440
441         sta = ap_get_sta(hapd, rx_auth->peer);
442         if (!sta) {
443                 sta = ap_sta_add(hapd, rx_auth->peer);
444                 if (sta == NULL) {
445                         status = WLAN_STATUS_UNSPECIFIED_FAILURE;
446                         goto fail;
447                 }
448         }
449         sta->flags &= ~WLAN_STA_PREAUTH;
450         ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
451 #ifdef CONFIG_IEEE80211R
452         if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
453                 sta->auth_alg = WLAN_AUTH_FT;
454                 if (sta->wpa_sm == NULL)
455                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
456                                                         sta->addr);
457                 if (sta->wpa_sm == NULL) {
458                         wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
459                                    "state machine");
460                         status = WLAN_STATUS_UNSPECIFIED_FAILURE;
461                         goto fail;
462                 }
463                 wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
464                                     rx_auth->auth_transaction, rx_auth->ies,
465                                     rx_auth->ies_len,
466                                     hostapd_notify_auth_ft_finish, hapd);
467                 return;
468         }
469 #endif /* CONFIG_IEEE80211R */
470 fail:
471         hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
472                          status, resp_ies, resp_ies_len);
473 }
474
475
476 static void hostapd_action_rx(struct hostapd_data *hapd,
477                               struct rx_action *action)
478 {
479         struct sta_info *sta;
480
481         wpa_printf(MSG_DEBUG, "RX_ACTION cat %d action plen %d",
482                    action->category, (int) action->len);
483
484         sta = ap_get_sta(hapd, action->sa);
485         if (sta == NULL) {
486                 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
487                 return;
488         }
489 #ifdef CONFIG_IEEE80211R
490         if (action->category == WLAN_ACTION_FT) {
491                 wpa_printf(MSG_DEBUG, "%s: FT_ACTION length %d",
492                            __func__, (int) action->len);
493                 wpa_ft_action_rx(sta->wpa_sm, action->data, action->len);
494         }
495 #endif /* CONFIG_IEEE80211R */
496 #ifdef CONFIG_IEEE80211W
497         if (action->category == WLAN_ACTION_SA_QUERY && action->len >= 4) {
498                 wpa_printf(MSG_DEBUG, "%s: SA_QUERY_ACTION length %d",
499                            __func__, (int) action->len);
500                 ieee802_11_sa_query_action(hapd, action->sa,
501                                            *(action->data + 1),
502                                            action->data + 2);
503         }
504 #endif /* CONFIG_IEEE80211W */
505 }
506
507
508 #ifdef NEED_AP_MLME
509
510 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
511
512 static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
513                                             const u8 *bssid)
514 {
515         size_t i;
516
517         if (bssid == NULL)
518                 return NULL;
519         if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
520             bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
521                 return HAPD_BROADCAST;
522
523         for (i = 0; i < iface->num_bss; i++) {
524                 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
525                         return iface->bss[i];
526         }
527
528         return NULL;
529 }
530
531
532 static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
533                                         const u8 *bssid, const u8 *addr,
534                                         int wds)
535 {
536         hapd = get_hapd_bssid(hapd->iface, bssid);
537         if (hapd == NULL || hapd == HAPD_BROADCAST)
538                 return;
539
540         ieee802_11_rx_from_unknown(hapd, addr, wds);
541 }
542
543
544 static void hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
545 {
546         struct hostapd_iface *iface = hapd->iface;
547         const struct ieee80211_hdr *hdr;
548         const u8 *bssid;
549         struct hostapd_frame_info fi;
550
551         hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
552         bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
553         if (bssid == NULL)
554                 return;
555
556         hapd = get_hapd_bssid(iface, bssid);
557         if (hapd == NULL) {
558                 u16 fc;
559                 fc = le_to_host16(hdr->frame_control);
560
561                 /*
562                  * Drop frames to unknown BSSIDs except for Beacon frames which
563                  * could be used to update neighbor information.
564                  */
565                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
566                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
567                         hapd = iface->bss[0];
568                 else
569                         return;
570         }
571
572         os_memset(&fi, 0, sizeof(fi));
573         fi.datarate = rx_mgmt->datarate;
574         fi.ssi_signal = rx_mgmt->ssi_signal;
575
576         if (hapd == HAPD_BROADCAST) {
577                 size_t i;
578                 for (i = 0; i < iface->num_bss; i++)
579                         ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
580                                         rx_mgmt->frame_len, &fi);
581         } else
582                 ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len, &fi);
583
584         random_add_randomness(&fi, sizeof(fi));
585 }
586
587
588 static void hostapd_rx_action(struct hostapd_data *hapd,
589                               struct rx_action *rx_action)
590 {
591         struct rx_mgmt rx_mgmt;
592         u8 *buf;
593         struct ieee80211_hdr *hdr;
594
595         wpa_printf(MSG_DEBUG, "EVENT_RX_ACTION DA=" MACSTR " SA=" MACSTR
596                    " BSSID=" MACSTR " category=%u",
597                    MAC2STR(rx_action->da), MAC2STR(rx_action->sa),
598                    MAC2STR(rx_action->bssid), rx_action->category);
599         wpa_hexdump(MSG_MSGDUMP, "Received action frame contents",
600                     rx_action->data, rx_action->len);
601
602         buf = os_zalloc(24 + 1 + rx_action->len);
603         if (buf == NULL)
604                 return;
605         hdr = (struct ieee80211_hdr *) buf;
606         hdr->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
607                                           WLAN_FC_STYPE_ACTION);
608         if (rx_action->category == WLAN_ACTION_SA_QUERY) {
609                 /*
610                  * Assume frame was protected; it would have been dropped if
611                  * not.
612                  */
613                 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
614         }
615         os_memcpy(hdr->addr1, rx_action->da, ETH_ALEN);
616         os_memcpy(hdr->addr2, rx_action->sa, ETH_ALEN);
617         os_memcpy(hdr->addr3, rx_action->bssid, ETH_ALEN);
618         buf[24] = rx_action->category;
619         os_memcpy(buf + 24 + 1, rx_action->data, rx_action->len);
620         os_memset(&rx_mgmt, 0, sizeof(rx_mgmt));
621         rx_mgmt.frame = buf;
622         rx_mgmt.frame_len = 24 + 1 + rx_action->len;
623         hostapd_mgmt_rx(hapd, &rx_mgmt);
624         os_free(buf);
625 }
626
627
628 static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
629                                size_t len, u16 stype, int ok)
630 {
631         struct ieee80211_hdr *hdr;
632         hdr = (struct ieee80211_hdr *) buf;
633         hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
634         if (hapd == NULL || hapd == HAPD_BROADCAST)
635                 return;
636         ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
637 }
638
639 #endif /* NEED_AP_MLME */
640
641
642 static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
643 {
644         struct sta_info *sta = ap_get_sta(hapd, addr);
645         if (sta)
646                 return 0;
647
648         wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
649                    " - adding a new STA", MAC2STR(addr));
650         sta = ap_sta_add(hapd, addr);
651         if (sta) {
652                 hostapd_new_assoc_sta(hapd, sta, 0);
653         } else {
654                 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
655                            MAC2STR(addr));
656                 return -1;
657         }
658
659         return 0;
660 }
661
662
663 static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
664                                    const u8 *data, size_t data_len)
665 {
666         struct hostapd_iface *iface = hapd->iface;
667         size_t j;
668
669         for (j = 0; j < iface->num_bss; j++) {
670                 if (ap_get_sta(iface->bss[j], src)) {
671                         hapd = iface->bss[j];
672                         break;
673                 }
674         }
675
676         ieee802_1x_receive(hapd, src, data, data_len);
677 }
678
679
680 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
681                           union wpa_event_data *data)
682 {
683         struct hostapd_data *hapd = ctx;
684 #ifndef CONFIG_NO_STDOUT_DEBUG
685         int level = MSG_DEBUG;
686
687         if (event == EVENT_RX_MGMT && data && data->rx_mgmt.frame &&
688             data->rx_mgmt.frame_len >= 24) {
689                 const struct ieee80211_hdr *hdr;
690                 u16 fc;
691                 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
692                 fc = le_to_host16(hdr->frame_control);
693                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
694                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
695                         level = MSG_EXCESSIVE;
696         }
697
698         wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
699                 event_to_string(event), event);
700 #endif /* CONFIG_NO_STDOUT_DEBUG */
701
702         switch (event) {
703         case EVENT_MICHAEL_MIC_FAILURE:
704                 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
705                 break;
706         case EVENT_SCAN_RESULTS:
707                 if (hapd->iface->scan_cb)
708                         hapd->iface->scan_cb(hapd->iface);
709                 break;
710 #ifdef CONFIG_IEEE80211R
711         case EVENT_FT_RRB_RX:
712                 wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
713                               data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
714                 break;
715 #endif /* CONFIG_IEEE80211R */
716         case EVENT_WPS_BUTTON_PUSHED:
717                 hostapd_wps_button_pushed(hapd, NULL);
718                 break;
719 #ifdef NEED_AP_MLME
720         case EVENT_TX_STATUS:
721                 switch (data->tx_status.type) {
722                 case WLAN_FC_TYPE_MGMT:
723                         hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
724                                            data->tx_status.data_len,
725                                            data->tx_status.stype,
726                                            data->tx_status.ack);
727                         break;
728                 case WLAN_FC_TYPE_DATA:
729                         hostapd_tx_status(hapd, data->tx_status.dst,
730                                           data->tx_status.data,
731                                           data->tx_status.data_len,
732                                           data->tx_status.ack);
733                         break;
734                 }
735                 break;
736         case EVENT_EAPOL_TX_STATUS:
737                 hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
738                                         data->eapol_tx_status.data,
739                                         data->eapol_tx_status.data_len,
740                                         data->eapol_tx_status.ack);
741                 break;
742         case EVENT_DRIVER_CLIENT_POLL_OK:
743                 hostapd_client_poll_ok(hapd, data->client_poll.addr);
744                 break;
745         case EVENT_RX_FROM_UNKNOWN:
746                 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
747                                             data->rx_from_unknown.addr,
748                                             data->rx_from_unknown.wds);
749                 break;
750         case EVENT_RX_MGMT:
751                 hostapd_mgmt_rx(hapd, &data->rx_mgmt);
752                 break;
753 #endif /* NEED_AP_MLME */
754         case EVENT_RX_PROBE_REQ:
755                 if (data->rx_probe_req.sa == NULL ||
756                     data->rx_probe_req.ie == NULL)
757                         break;
758                 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
759                                      data->rx_probe_req.da,
760                                      data->rx_probe_req.bssid,
761                                      data->rx_probe_req.ie,
762                                      data->rx_probe_req.ie_len,
763                                      data->rx_probe_req.ssi_signal);
764                 break;
765         case EVENT_NEW_STA:
766                 hostapd_event_new_sta(hapd, data->new_sta.addr);
767                 break;
768         case EVENT_EAPOL_RX:
769                 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
770                                        data->eapol_rx.data,
771                                        data->eapol_rx.data_len);
772                 break;
773         case EVENT_ASSOC:
774                 hostapd_notif_assoc(hapd, data->assoc_info.addr,
775                                     data->assoc_info.req_ies,
776                                     data->assoc_info.req_ies_len,
777                                     data->assoc_info.reassoc);
778                 break;
779         case EVENT_DISASSOC:
780                 if (data)
781                         hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
782                 break;
783         case EVENT_DEAUTH:
784                 if (data)
785                         hostapd_notif_disassoc(hapd, data->deauth_info.addr);
786                 break;
787         case EVENT_STATION_LOW_ACK:
788                 if (!data)
789                         break;
790                 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
791                 break;
792         case EVENT_RX_ACTION:
793                 if (data->rx_action.da == NULL || data->rx_action.sa == NULL ||
794                     data->rx_action.bssid == NULL)
795                         break;
796 #ifdef NEED_AP_MLME
797                 hostapd_rx_action(hapd, &data->rx_action);
798 #endif /* NEED_AP_MLME */
799                 hostapd_action_rx(hapd, &data->rx_action);
800                 break;
801         case EVENT_AUTH:
802                 hostapd_notif_auth(hapd, &data->auth);
803                 break;
804         case EVENT_CH_SWITCH:
805                 if (!data)
806                         break;
807                 hostapd_event_ch_switch(hapd, data->ch_switch.freq,
808                                         data->ch_switch.ht_enabled,
809                                         data->ch_switch.ch_offset);
810                 break;
811         default:
812                 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
813                 break;
814         }
815 }
816
817 #endif /* HOSTAPD */