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"
21 #include "common/ieee802_11_common.h"
22 #include "common/wpa_ctrl.h"
24 #include "ieee802_11.h"
26 #include "accounting.h"
27 #include "tkip_countermeasures.h"
29 #include "ieee802_1x.h"
32 #include "wps_hostapd.h"
33 #include "ap_config.h"
36 int hostapd_notif_assoc(struct hostapd_data
*hapd
, const u8
*addr
,
37 const u8
*ie
, size_t ielen
)
41 struct ieee802_11_elems elems
;
45 * This could potentially happen with unexpected event from the
46 * driver wrapper. This was seen at least in one case where the
47 * driver ended up being set to station mode while hostapd was
48 * running, so better make sure we stop processing such an
51 wpa_printf(MSG_DEBUG
, "hostapd_notif_assoc: Skip event with "
56 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_IEEE80211
,
57 HOSTAPD_LEVEL_INFO
, "associated");
59 ieee802_11_parse_elems(ie
, ielen
, &elems
, 0);
61 ie
= elems
.wps_ie
- 2;
62 ielen
= elems
.wps_ie_len
+ 2;
63 wpa_printf(MSG_DEBUG
, "STA included WPS IE in (Re)AssocReq");
64 } else if (elems
.rsn_ie
) {
65 ie
= elems
.rsn_ie
- 2;
66 ielen
= elems
.rsn_ie_len
+ 2;
67 wpa_printf(MSG_DEBUG
, "STA included RSN IE in (Re)AssocReq");
68 } else if (elems
.wpa_ie
) {
69 ie
= elems
.wpa_ie
- 2;
70 ielen
= elems
.wpa_ie_len
+ 2;
71 wpa_printf(MSG_DEBUG
, "STA included WPA IE in (Re)AssocReq");
75 wpa_printf(MSG_DEBUG
, "STA did not include WPS/RSN/WPA IE in "
79 sta
= ap_get_sta(hapd
, addr
);
81 accounting_sta_stop(hapd
, sta
);
83 sta
= ap_sta_add(hapd
, addr
);
87 sta
->flags
&= ~(WLAN_STA_WPS
| WLAN_STA_MAYBE_WPS
);
89 if (hapd
->conf
->wpa
) {
90 if (ie
== NULL
|| ielen
== 0) {
91 if (hapd
->conf
->wps_state
) {
92 wpa_printf(MSG_DEBUG
, "STA did not include "
93 "WPA/RSN IE in (Re)Association "
94 "Request - possible WPS use");
95 sta
->flags
|= WLAN_STA_MAYBE_WPS
;
99 wpa_printf(MSG_DEBUG
, "No WPA/RSN IE from STA");
102 if (hapd
->conf
->wps_state
&& ie
[0] == 0xdd && ie
[1] >= 4 &&
103 os_memcmp(ie
+ 2, "\x00\x50\xf2\x04", 4) == 0) {
104 sta
->flags
|= WLAN_STA_WPS
;
108 if (sta
->wpa_sm
== NULL
)
109 sta
->wpa_sm
= wpa_auth_sta_init(hapd
->wpa_auth
,
111 if (sta
->wpa_sm
== NULL
) {
112 wpa_printf(MSG_ERROR
, "Failed to initialize WPA state "
116 res
= wpa_validate_wpa_ie(hapd
->wpa_auth
, sta
->wpa_sm
,
118 if (res
!= WPA_IE_OK
) {
120 wpa_printf(MSG_DEBUG
, "WPA/RSN information element "
121 "rejected? (res %u)", res
);
122 wpa_hexdump(MSG_DEBUG
, "IE", ie
, ielen
);
123 if (res
== WPA_INVALID_GROUP
)
124 resp
= WLAN_REASON_GROUP_CIPHER_NOT_VALID
;
125 else if (res
== WPA_INVALID_PAIRWISE
)
126 resp
= WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID
;
127 else if (res
== WPA_INVALID_AKMP
)
128 resp
= WLAN_REASON_AKMP_NOT_VALID
;
129 #ifdef CONFIG_IEEE80211W
130 else if (res
== WPA_MGMT_FRAME_PROTECTION_VIOLATION
)
131 resp
= WLAN_REASON_INVALID_IE
;
132 else if (res
== WPA_INVALID_MGMT_GROUP_CIPHER
)
133 resp
= WLAN_REASON_GROUP_CIPHER_NOT_VALID
;
134 #endif /* CONFIG_IEEE80211W */
136 resp
= WLAN_REASON_INVALID_IE
;
137 hapd
->drv
.sta_disassoc(hapd
, sta
->addr
, resp
);
138 ap_free_sta(hapd
, sta
);
141 } else if (hapd
->conf
->wps_state
) {
142 if (ie
&& ielen
> 4 && ie
[0] == 0xdd && ie
[1] >= 4 &&
143 os_memcmp(ie
+ 2, "\x00\x50\xf2\x04", 4) == 0) {
144 sta
->flags
|= WLAN_STA_WPS
;
146 sta
->flags
|= WLAN_STA_MAYBE_WPS
;
150 new_assoc
= (sta
->flags
& WLAN_STA_ASSOC
) == 0;
151 sta
->flags
|= WLAN_STA_AUTH
| WLAN_STA_ASSOC
;
152 wpa_auth_sm_event(sta
->wpa_sm
, WPA_ASSOC
);
154 hostapd_new_assoc_sta(hapd
, sta
, !new_assoc
);
156 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, 1);
162 void hostapd_notif_disassoc(struct hostapd_data
*hapd
, const u8
*addr
)
164 struct sta_info
*sta
;
166 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_IEEE80211
,
167 HOSTAPD_LEVEL_INFO
, "disassociated");
169 sta
= ap_get_sta(hapd
, addr
);
171 wpa_printf(MSG_DEBUG
, "Disassociation notification for "
172 "unknown STA " MACSTR
, MAC2STR(addr
));
176 sta
->flags
&= ~(WLAN_STA_AUTH
| WLAN_STA_ASSOC
);
177 wpa_msg(hapd
->msg_ctx
, MSG_INFO
, AP_STA_DISCONNECTED MACSTR
,
179 wpa_auth_sm_event(sta
->wpa_sm
, WPA_DISASSOC
);
180 sta
->acct_terminate_cause
= RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST
;
181 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, 0);
182 ap_free_sta(hapd
, sta
);
190 static const u8
* get_hdr_bssid(const struct ieee80211_hdr
*hdr
, size_t len
)
195 * PS-Poll frames are 16 bytes. All other frames are
196 * 24 bytes or longer.
201 fc
= le_to_host16(hdr
->frame_control
);
202 type
= WLAN_FC_GET_TYPE(fc
);
203 stype
= WLAN_FC_GET_STYPE(fc
);
206 case WLAN_FC_TYPE_DATA
:
209 switch (fc
& (WLAN_FC_FROMDS
| WLAN_FC_TODS
)) {
210 case WLAN_FC_FROMDS
| WLAN_FC_TODS
:
218 case WLAN_FC_TYPE_CTRL
:
219 if (stype
!= WLAN_FC_STYPE_PSPOLL
)
222 case WLAN_FC_TYPE_MGMT
:
230 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
232 static struct hostapd_data
* get_hapd_bssid(struct hostapd_iface
*iface
,
239 if (bssid
[0] == 0xff && bssid
[1] == 0xff && bssid
[2] == 0xff &&
240 bssid
[3] == 0xff && bssid
[4] == 0xff && bssid
[5] == 0xff)
241 return HAPD_BROADCAST
;
243 for (i
= 0; i
< iface
->num_bss
; i
++) {
244 if (os_memcmp(bssid
, iface
->bss
[i
]->own_addr
, ETH_ALEN
) == 0)
245 return iface
->bss
[i
];
252 static void hostapd_rx_from_unknown_sta(struct hostapd_data
*hapd
,
253 const u8
*frame
, size_t len
)
255 const struct ieee80211_hdr
*hdr
= (const struct ieee80211_hdr
*) frame
;
256 u16 fc
= le_to_host16(hdr
->frame_control
);
257 hapd
= get_hapd_bssid(hapd
->iface
, get_hdr_bssid(hdr
, len
));
258 if (hapd
== NULL
|| hapd
== HAPD_BROADCAST
)
261 ieee802_11_rx_from_unknown(hapd
, hdr
->addr2
,
262 (fc
& (WLAN_FC_TODS
| WLAN_FC_FROMDS
)) ==
263 (WLAN_FC_TODS
| WLAN_FC_FROMDS
));
267 static void hostapd_mgmt_rx(struct hostapd_data
*hapd
, struct rx_mgmt
*rx_mgmt
)
269 struct hostapd_iface
*iface
= hapd
->iface
;
270 const struct ieee80211_hdr
*hdr
;
272 struct hostapd_frame_info fi
;
274 hdr
= (const struct ieee80211_hdr
*) rx_mgmt
->frame
;
275 bssid
= get_hdr_bssid(hdr
, rx_mgmt
->frame_len
);
279 hapd
= get_hapd_bssid(iface
, bssid
);
282 fc
= le_to_host16(hdr
->frame_control
);
285 * Drop frames to unknown BSSIDs except for Beacon frames which
286 * could be used to update neighbor information.
288 if (WLAN_FC_GET_TYPE(fc
) == WLAN_FC_TYPE_MGMT
&&
289 WLAN_FC_GET_STYPE(fc
) == WLAN_FC_STYPE_BEACON
)
290 hapd
= iface
->bss
[0];
295 os_memset(&fi
, 0, sizeof(fi
));
296 fi
.datarate
= rx_mgmt
->datarate
;
297 fi
.ssi_signal
= rx_mgmt
->ssi_signal
;
299 if (hapd
== HAPD_BROADCAST
) {
301 for (i
= 0; i
< iface
->num_bss
; i
++)
302 ieee802_11_mgmt(iface
->bss
[i
], rx_mgmt
->frame
,
303 rx_mgmt
->frame_len
, &fi
);
305 ieee802_11_mgmt(hapd
, rx_mgmt
->frame
, rx_mgmt
->frame_len
, &fi
);
309 static void hostapd_mgmt_tx_cb(struct hostapd_data
*hapd
, const u8
*buf
,
310 size_t len
, u16 stype
, int ok
)
312 struct ieee80211_hdr
*hdr
;
313 hdr
= (struct ieee80211_hdr
*) buf
;
314 hapd
= get_hapd_bssid(hapd
->iface
, get_hdr_bssid(hdr
, len
));
315 if (hapd
== NULL
|| hapd
== HAPD_BROADCAST
)
317 ieee802_11_mgmt_cb(hapd
, buf
, len
, stype
, ok
);
320 #endif /* NEED_AP_MLME */
323 static int hostapd_probe_req_rx(struct hostapd_data
*hapd
, const u8
*sa
,
324 const u8
*ie
, size_t ie_len
)
329 for (i
= 0; hapd
->probereq_cb
&& i
< hapd
->num_probereq_cb
; i
++) {
330 if (hapd
->probereq_cb
[i
].cb(hapd
->probereq_cb
[i
].ctx
,
331 sa
, ie
, ie_len
) > 0) {
340 static int hostapd_event_new_sta(struct hostapd_data
*hapd
, const u8
*addr
)
342 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
346 wpa_printf(MSG_DEBUG
, "Data frame from unknown STA " MACSTR
347 " - adding a new STA", MAC2STR(addr
));
348 sta
= ap_sta_add(hapd
, addr
);
350 hostapd_new_assoc_sta(hapd
, sta
, 0);
352 wpa_printf(MSG_DEBUG
, "Failed to add STA entry for " MACSTR
,
361 static void hostapd_event_eapol_rx(struct hostapd_data
*hapd
, const u8
*src
,
362 const u8
*data
, size_t data_len
)
364 struct hostapd_iface
*iface
= hapd
->iface
;
367 for (j
= 0; j
< iface
->num_bss
; j
++) {
368 if (ap_get_sta(iface
->bss
[j
], src
)) {
369 hapd
= iface
->bss
[j
];
374 ieee802_1x_receive(hapd
, src
, data
, data_len
);
378 void wpa_supplicant_event(void *ctx
, enum wpa_event_type event
,
379 union wpa_event_data
*data
)
381 struct hostapd_data
*hapd
= ctx
;
384 case EVENT_MICHAEL_MIC_FAILURE
:
385 michael_mic_failure(hapd
, data
->michael_mic_failure
.src
, 1);
387 case EVENT_SCAN_RESULTS
:
388 if (hapd
->iface
->scan_cb
)
389 hapd
->iface
->scan_cb(hapd
->iface
);
391 #ifdef CONFIG_IEEE80211R
392 case EVENT_FT_RRB_RX
:
393 wpa_ft_rrb_rx(hapd
->wpa_auth
, data
->ft_rrb_rx
.src
,
394 data
->ft_rrb_rx
.data
, data
->ft_rrb_rx
.data_len
);
396 #endif /* CONFIG_IEEE80211R */
397 case EVENT_WPS_BUTTON_PUSHED
:
398 hostapd_wps_button_pushed(hapd
);
401 case EVENT_TX_STATUS
:
402 switch (data
->tx_status
.type
) {
403 case WLAN_FC_TYPE_MGMT
:
404 hostapd_mgmt_tx_cb(hapd
, data
->tx_status
.data
,
405 data
->tx_status
.data_len
,
406 data
->tx_status
.stype
,
407 data
->tx_status
.ack
);
409 case WLAN_FC_TYPE_DATA
:
410 hostapd_tx_status(hapd
, data
->tx_status
.dst
,
411 data
->tx_status
.data
,
412 data
->tx_status
.data_len
,
413 data
->tx_status
.ack
);
417 case EVENT_RX_FROM_UNKNOWN
:
418 hostapd_rx_from_unknown_sta(hapd
, data
->rx_from_unknown
.frame
,
419 data
->rx_from_unknown
.len
);
422 hostapd_mgmt_rx(hapd
, &data
->rx_mgmt
);
424 #endif /* NEED_AP_MLME */
425 case EVENT_RX_PROBE_REQ
:
426 hostapd_probe_req_rx(hapd
, data
->rx_probe_req
.sa
,
427 data
->rx_probe_req
.ie
,
428 data
->rx_probe_req
.ie_len
);
431 hostapd_event_new_sta(hapd
, data
->new_sta
.addr
);
434 hostapd_event_eapol_rx(hapd
, data
->eapol_rx
.src
,
436 data
->eapol_rx
.data_len
);
439 hostapd_notif_assoc(hapd
, data
->assoc_info
.addr
,
440 data
->assoc_info
.req_ies
,
441 data
->assoc_info
.req_ies_len
);
445 hostapd_notif_disassoc(hapd
, data
->disassoc_info
.addr
);
449 hostapd_notif_disassoc(hapd
, data
->deauth_info
.addr
);
452 wpa_printf(MSG_DEBUG
, "Unknown event %d", event
);