2 * hostapd / Callback functions for driver wrappers
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "radius/radius.h"
19 #include "drivers/driver.h"
20 #include "common/ieee802_11_defs.h"
22 #include "ieee802_11.h"
24 #include "accounting.h"
25 #include "tkip_countermeasures.h"
27 #include "ieee802_1x.h"
30 #include "wps_hostapd.h"
31 #include "ap_config.h"
34 int hostapd_notif_assoc(struct hostapd_data
*hapd
, const u8
*addr
,
35 const u8
*ie
, size_t ielen
)
40 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_IEEE80211
,
41 HOSTAPD_LEVEL_INFO
, "associated");
43 sta
= ap_get_sta(hapd
, addr
);
45 accounting_sta_stop(hapd
, sta
);
47 sta
= ap_sta_add(hapd
, addr
);
51 sta
->flags
&= ~(WLAN_STA_WPS
| WLAN_STA_MAYBE_WPS
);
53 if (hapd
->conf
->wpa
) {
54 if (ie
== NULL
|| ielen
== 0) {
55 if (hapd
->conf
->wps_state
) {
56 wpa_printf(MSG_DEBUG
, "STA did not include "
57 "WPA/RSN IE in (Re)Association "
58 "Request - possible WPS use");
59 sta
->flags
|= WLAN_STA_MAYBE_WPS
;
63 wpa_printf(MSG_DEBUG
, "No WPA/RSN IE from STA");
66 if (hapd
->conf
->wps_state
&& ie
[0] == 0xdd && ie
[1] >= 4 &&
67 os_memcmp(ie
+ 2, "\x00\x50\xf2\x04", 4) == 0) {
68 sta
->flags
|= WLAN_STA_WPS
;
72 if (sta
->wpa_sm
== NULL
)
73 sta
->wpa_sm
= wpa_auth_sta_init(hapd
->wpa_auth
,
75 if (sta
->wpa_sm
== NULL
) {
76 wpa_printf(MSG_ERROR
, "Failed to initialize WPA state "
80 res
= wpa_validate_wpa_ie(hapd
->wpa_auth
, sta
->wpa_sm
,
82 if (res
!= WPA_IE_OK
) {
84 wpa_printf(MSG_DEBUG
, "WPA/RSN information element "
85 "rejected? (res %u)", res
);
86 wpa_hexdump(MSG_DEBUG
, "IE", ie
, ielen
);
87 if (res
== WPA_INVALID_GROUP
)
88 resp
= WLAN_REASON_GROUP_CIPHER_NOT_VALID
;
89 else if (res
== WPA_INVALID_PAIRWISE
)
90 resp
= WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID
;
91 else if (res
== WPA_INVALID_AKMP
)
92 resp
= WLAN_REASON_AKMP_NOT_VALID
;
93 #ifdef CONFIG_IEEE80211W
94 else if (res
== WPA_MGMT_FRAME_PROTECTION_VIOLATION
)
95 resp
= WLAN_REASON_INVALID_IE
;
96 else if (res
== WPA_INVALID_MGMT_GROUP_CIPHER
)
97 resp
= WLAN_REASON_GROUP_CIPHER_NOT_VALID
;
98 #endif /* CONFIG_IEEE80211W */
100 resp
= WLAN_REASON_INVALID_IE
;
101 hapd
->drv
.sta_disassoc(hapd
, sta
->addr
, resp
);
102 ap_free_sta(hapd
, sta
);
105 } else if (hapd
->conf
->wps_state
) {
106 if (ie
&& ielen
> 4 && ie
[0] == 0xdd && ie
[1] >= 4 &&
107 os_memcmp(ie
+ 2, "\x00\x50\xf2\x04", 4) == 0) {
108 sta
->flags
|= WLAN_STA_WPS
;
110 sta
->flags
|= WLAN_STA_MAYBE_WPS
;
114 new_assoc
= (sta
->flags
& WLAN_STA_ASSOC
) == 0;
115 sta
->flags
|= WLAN_STA_AUTH
| WLAN_STA_ASSOC
;
116 wpa_auth_sm_event(sta
->wpa_sm
, WPA_ASSOC
);
118 hostapd_new_assoc_sta(hapd
, sta
, !new_assoc
);
120 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, 1);
126 void hostapd_notif_disassoc(struct hostapd_data
*hapd
, const u8
*addr
)
128 struct sta_info
*sta
;
130 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_IEEE80211
,
131 HOSTAPD_LEVEL_INFO
, "disassociated");
133 sta
= ap_get_sta(hapd
, addr
);
135 wpa_printf(MSG_DEBUG
, "Disassociation notification for "
136 "unknown STA " MACSTR
, MAC2STR(addr
));
140 sta
->flags
&= ~(WLAN_STA_AUTH
| WLAN_STA_ASSOC
);
141 wpa_auth_sm_event(sta
->wpa_sm
, WPA_DISASSOC
);
142 sta
->acct_terminate_cause
= RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST
;
143 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, 0);
144 ap_free_sta(hapd
, sta
);
152 static const u8
* get_hdr_bssid(const struct ieee80211_hdr
*hdr
, size_t len
)
157 * PS-Poll frames are 16 bytes. All other frames are
158 * 24 bytes or longer.
163 fc
= le_to_host16(hdr
->frame_control
);
164 type
= WLAN_FC_GET_TYPE(fc
);
165 stype
= WLAN_FC_GET_STYPE(fc
);
168 case WLAN_FC_TYPE_DATA
:
171 switch (fc
& (WLAN_FC_FROMDS
| WLAN_FC_TODS
)) {
172 case WLAN_FC_FROMDS
| WLAN_FC_TODS
:
180 case WLAN_FC_TYPE_CTRL
:
181 if (stype
!= WLAN_FC_STYPE_PSPOLL
)
184 case WLAN_FC_TYPE_MGMT
:
192 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
194 static struct hostapd_data
* get_hapd_bssid(struct hostapd_iface
*iface
,
201 if (bssid
[0] == 0xff && bssid
[1] == 0xff && bssid
[2] == 0xff &&
202 bssid
[3] == 0xff && bssid
[4] == 0xff && bssid
[5] == 0xff)
203 return HAPD_BROADCAST
;
205 for (i
= 0; i
< iface
->num_bss
; i
++) {
206 if (os_memcmp(bssid
, iface
->bss
[i
]->own_addr
, ETH_ALEN
) == 0)
207 return iface
->bss
[i
];
214 static void hostapd_rx_from_unknown_sta(struct hostapd_data
*hapd
,
215 const u8
*frame
, size_t len
)
217 const struct ieee80211_hdr
*hdr
= (const struct ieee80211_hdr
*) frame
;
218 u16 fc
= le_to_host16(hdr
->frame_control
);
219 hapd
= get_hapd_bssid(hapd
->iface
, get_hdr_bssid(hdr
, len
));
220 if (hapd
== NULL
|| hapd
== HAPD_BROADCAST
)
223 ieee802_11_rx_from_unknown(hapd
, hdr
->addr2
,
224 (fc
& (WLAN_FC_TODS
| WLAN_FC_FROMDS
)) ==
225 (WLAN_FC_TODS
| WLAN_FC_FROMDS
));
229 static void hostapd_mgmt_rx(struct hostapd_data
*hapd
, struct rx_mgmt
*rx_mgmt
)
231 struct hostapd_iface
*iface
= hapd
->iface
;
232 const struct ieee80211_hdr
*hdr
;
234 struct hostapd_frame_info fi
;
236 hdr
= (const struct ieee80211_hdr
*) rx_mgmt
->frame
;
237 bssid
= get_hdr_bssid(hdr
, rx_mgmt
->frame_len
);
241 hapd
= get_hapd_bssid(iface
, bssid
);
244 fc
= le_to_host16(hdr
->frame_control
);
247 * Drop frames to unknown BSSIDs except for Beacon frames which
248 * could be used to update neighbor information.
250 if (WLAN_FC_GET_TYPE(fc
) == WLAN_FC_TYPE_MGMT
&&
251 WLAN_FC_GET_STYPE(fc
) == WLAN_FC_STYPE_BEACON
)
252 hapd
= iface
->bss
[0];
257 os_memset(&fi
, 0, sizeof(fi
));
258 fi
.datarate
= rx_mgmt
->datarate
;
259 fi
.ssi_signal
= rx_mgmt
->ssi_signal
;
261 if (hapd
== HAPD_BROADCAST
) {
263 for (i
= 0; i
< iface
->num_bss
; i
++)
264 ieee802_11_mgmt(iface
->bss
[i
], rx_mgmt
->frame
,
265 rx_mgmt
->frame_len
, &fi
);
267 ieee802_11_mgmt(hapd
, rx_mgmt
->frame
, rx_mgmt
->frame_len
, &fi
);
271 static void hostapd_mgmt_tx_cb(struct hostapd_data
*hapd
, const u8
*buf
,
272 size_t len
, u16 stype
, int ok
)
274 struct ieee80211_hdr
*hdr
;
275 hdr
= (struct ieee80211_hdr
*) buf
;
276 hapd
= get_hapd_bssid(hapd
->iface
, get_hdr_bssid(hdr
, len
));
277 if (hapd
== NULL
|| hapd
== HAPD_BROADCAST
)
279 ieee802_11_mgmt_cb(hapd
, buf
, len
, stype
, ok
);
282 #endif /* NEED_AP_MLME */
285 static int hostapd_probe_req_rx(struct hostapd_data
*hapd
, const u8
*sa
,
286 const u8
*ie
, size_t ie_len
)
291 for (i
= 0; hapd
->probereq_cb
&& i
< hapd
->num_probereq_cb
; i
++) {
292 if (hapd
->probereq_cb
[i
].cb(hapd
->probereq_cb
[i
].ctx
,
293 sa
, ie
, ie_len
) > 0) {
302 static int hostapd_event_new_sta(struct hostapd_data
*hapd
, const u8
*addr
)
304 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
308 wpa_printf(MSG_DEBUG
, "Data frame from unknown STA " MACSTR
309 " - adding a new STA", MAC2STR(addr
));
310 sta
= ap_sta_add(hapd
, addr
);
312 hostapd_new_assoc_sta(hapd
, sta
, 0);
314 wpa_printf(MSG_DEBUG
, "Failed to add STA entry for " MACSTR
,
323 static void hostapd_event_eapol_rx(struct hostapd_data
*hapd
, const u8
*src
,
324 const u8
*data
, size_t data_len
)
326 struct hostapd_iface
*iface
= hapd
->iface
;
329 for (j
= 0; j
< iface
->num_bss
; j
++) {
330 if (ap_get_sta(iface
->bss
[j
], src
)) {
331 hapd
= iface
->bss
[j
];
336 ieee802_1x_receive(hapd
, src
, data
, data_len
);
340 void wpa_supplicant_event(void *ctx
, enum wpa_event_type event
,
341 union wpa_event_data
*data
)
343 struct hostapd_data
*hapd
= ctx
;
346 case EVENT_MICHAEL_MIC_FAILURE
:
347 michael_mic_failure(hapd
, data
->michael_mic_failure
.src
, 1);
349 case EVENT_SCAN_RESULTS
:
350 if (hapd
->iface
->scan_cb
)
351 hapd
->iface
->scan_cb(hapd
->iface
);
353 #ifdef CONFIG_IEEE80211R
354 case EVENT_FT_RRB_RX
:
355 wpa_ft_rrb_rx(hapd
->wpa_auth
, data
->ft_rrb_rx
.src
,
356 data
->ft_rrb_rx
.data
, data
->ft_rrb_rx
.data_len
);
358 #endif /* CONFIG_IEEE80211R */
359 case EVENT_WPS_BUTTON_PUSHED
:
360 hostapd_wps_button_pushed(hapd
);
363 case EVENT_TX_STATUS
:
364 switch (data
->tx_status
.type
) {
365 case WLAN_FC_TYPE_MGMT
:
366 hostapd_mgmt_tx_cb(hapd
, data
->tx_status
.data
,
367 data
->tx_status
.data_len
,
368 data
->tx_status
.stype
,
369 data
->tx_status
.ack
);
371 case WLAN_FC_TYPE_DATA
:
372 hostapd_tx_status(hapd
, data
->tx_status
.dst
,
373 data
->tx_status
.data
,
374 data
->tx_status
.data_len
,
375 data
->tx_status
.ack
);
379 case EVENT_RX_FROM_UNKNOWN
:
380 hostapd_rx_from_unknown_sta(hapd
, data
->rx_from_unknown
.frame
,
381 data
->rx_from_unknown
.len
);
384 hostapd_mgmt_rx(hapd
, &data
->rx_mgmt
);
386 #endif /* NEED_AP_MLME */
387 case EVENT_RX_PROBE_REQ
:
388 hostapd_probe_req_rx(hapd
, data
->rx_probe_req
.sa
,
389 data
->rx_probe_req
.ie
,
390 data
->rx_probe_req
.ie_len
);
393 hostapd_event_new_sta(hapd
, data
->new_sta
.addr
);
396 hostapd_event_eapol_rx(hapd
, data
->eapol_rx
.src
,
398 data
->eapol_rx
.data_len
);
401 hostapd_notif_assoc(hapd
, data
->assoc_info
.addr
,
402 data
->assoc_info
.req_ies
,
403 data
->assoc_info
.req_ies_len
);
407 hostapd_notif_disassoc(hapd
, data
->disassoc_info
.addr
);
411 hostapd_notif_disassoc(hapd
, data
->deauth_info
.addr
);
414 wpa_printf(MSG_DEBUG
, "Unknown event %d", event
);