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"
21 #include "ieee802_11.h"
23 #include "accounting.h"
24 #include "tkip_countermeasures.h"
26 #include "ieee802_1x.h"
29 #include "wps_hostapd.h"
30 #include "ap_config.h"
33 int hostapd_notif_new_sta(struct hostapd_data
*hapd
, const u8
*addr
)
35 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
39 wpa_printf(MSG_DEBUG
, "Data frame from unknown STA " MACSTR
40 " - adding a new STA", MAC2STR(addr
));
41 sta
= ap_sta_add(hapd
, addr
);
43 hostapd_new_assoc_sta(hapd
, sta
, 0);
45 wpa_printf(MSG_DEBUG
, "Failed to add STA entry for " MACSTR
,
54 int hostapd_notif_assoc(struct hostapd_data
*hapd
, const u8
*addr
,
55 const u8
*ie
, size_t ielen
)
60 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_IEEE80211
,
61 HOSTAPD_LEVEL_INFO
, "associated");
63 sta
= ap_get_sta(hapd
, addr
);
65 accounting_sta_stop(hapd
, sta
);
67 sta
= ap_sta_add(hapd
, addr
);
71 sta
->flags
&= ~(WLAN_STA_WPS
| WLAN_STA_MAYBE_WPS
);
73 if (hapd
->conf
->wpa
) {
74 if (ie
== NULL
|| ielen
== 0) {
75 if (hapd
->conf
->wps_state
) {
76 wpa_printf(MSG_DEBUG
, "STA did not include "
77 "WPA/RSN IE in (Re)Association "
78 "Request - possible WPS use");
79 sta
->flags
|= WLAN_STA_MAYBE_WPS
;
83 wpa_printf(MSG_DEBUG
, "No WPA/RSN IE from STA");
86 if (hapd
->conf
->wps_state
&& ie
[0] == 0xdd && ie
[1] >= 4 &&
87 os_memcmp(ie
+ 2, "\x00\x50\xf2\x04", 4) == 0) {
88 sta
->flags
|= WLAN_STA_WPS
;
92 if (sta
->wpa_sm
== NULL
)
93 sta
->wpa_sm
= wpa_auth_sta_init(hapd
->wpa_auth
,
95 if (sta
->wpa_sm
== NULL
) {
96 wpa_printf(MSG_ERROR
, "Failed to initialize WPA state "
100 res
= wpa_validate_wpa_ie(hapd
->wpa_auth
, sta
->wpa_sm
,
102 if (res
!= WPA_IE_OK
) {
104 wpa_printf(MSG_DEBUG
, "WPA/RSN information element "
105 "rejected? (res %u)", res
);
106 wpa_hexdump(MSG_DEBUG
, "IE", ie
, ielen
);
107 if (res
== WPA_INVALID_GROUP
)
108 resp
= WLAN_REASON_GROUP_CIPHER_NOT_VALID
;
109 else if (res
== WPA_INVALID_PAIRWISE
)
110 resp
= WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID
;
111 else if (res
== WPA_INVALID_AKMP
)
112 resp
= WLAN_REASON_AKMP_NOT_VALID
;
113 #ifdef CONFIG_IEEE80211W
114 else if (res
== WPA_MGMT_FRAME_PROTECTION_VIOLATION
)
115 resp
= WLAN_REASON_INVALID_IE
;
116 else if (res
== WPA_INVALID_MGMT_GROUP_CIPHER
)
117 resp
= WLAN_REASON_GROUP_CIPHER_NOT_VALID
;
118 #endif /* CONFIG_IEEE80211W */
120 resp
= WLAN_REASON_INVALID_IE
;
121 hapd
->drv
.sta_disassoc(hapd
, sta
->addr
, resp
);
122 ap_free_sta(hapd
, sta
);
125 } else if (hapd
->conf
->wps_state
) {
126 if (ie
&& ielen
> 4 && ie
[0] == 0xdd && ie
[1] >= 4 &&
127 os_memcmp(ie
+ 2, "\x00\x50\xf2\x04", 4) == 0) {
128 sta
->flags
|= WLAN_STA_WPS
;
130 sta
->flags
|= WLAN_STA_MAYBE_WPS
;
134 new_assoc
= (sta
->flags
& WLAN_STA_ASSOC
) == 0;
135 sta
->flags
|= WLAN_STA_AUTH
| WLAN_STA_ASSOC
;
136 wpa_auth_sm_event(sta
->wpa_sm
, WPA_ASSOC
);
138 hostapd_new_assoc_sta(hapd
, sta
, !new_assoc
);
140 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, 1);
146 void hostapd_notif_disassoc(struct hostapd_data
*hapd
, const u8
*addr
)
148 struct sta_info
*sta
;
150 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_IEEE80211
,
151 HOSTAPD_LEVEL_INFO
, "disassociated");
153 sta
= ap_get_sta(hapd
, addr
);
155 wpa_printf(MSG_DEBUG
, "Disassociation notification for "
156 "unknown STA " MACSTR
, MAC2STR(addr
));
160 sta
->flags
&= ~(WLAN_STA_AUTH
| WLAN_STA_ASSOC
);
161 wpa_auth_sm_event(sta
->wpa_sm
, WPA_DISASSOC
);
162 sta
->acct_terminate_cause
= RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST
;
163 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, 0);
164 ap_free_sta(hapd
, sta
);
168 void hostapd_eapol_receive(struct hostapd_data
*hapd
, const u8
*sa
,
169 const u8
*buf
, size_t len
)
171 ieee802_1x_receive(hapd
, sa
, buf
, len
);
175 struct hostapd_data
* hostapd_sta_get_bss(struct hostapd_data
*hapd
,
178 struct hostapd_iface
*iface
= hapd
->iface
;
181 for (j
= 0; j
< iface
->num_bss
; j
++) {
182 hapd
= iface
->bss
[j
];
183 if (ap_get_sta(hapd
, addr
))
195 static const u8
* get_hdr_bssid(const struct ieee80211_hdr
*hdr
, size_t len
)
200 * PS-Poll frames are 16 bytes. All other frames are
201 * 24 bytes or longer.
206 fc
= le_to_host16(hdr
->frame_control
);
207 type
= WLAN_FC_GET_TYPE(fc
);
208 stype
= WLAN_FC_GET_STYPE(fc
);
211 case WLAN_FC_TYPE_DATA
:
214 switch (fc
& (WLAN_FC_FROMDS
| WLAN_FC_TODS
)) {
215 case WLAN_FC_FROMDS
| WLAN_FC_TODS
:
223 case WLAN_FC_TYPE_CTRL
:
224 if (stype
!= WLAN_FC_STYPE_PSPOLL
)
227 case WLAN_FC_TYPE_MGMT
:
235 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
237 static struct hostapd_data
* get_hapd_bssid(struct hostapd_iface
*iface
,
244 if (bssid
[0] == 0xff && bssid
[1] == 0xff && bssid
[2] == 0xff &&
245 bssid
[3] == 0xff && bssid
[4] == 0xff && bssid
[5] == 0xff)
246 return HAPD_BROADCAST
;
248 for (i
= 0; i
< iface
->num_bss
; i
++) {
249 if (os_memcmp(bssid
, iface
->bss
[i
]->own_addr
, ETH_ALEN
) == 0)
250 return iface
->bss
[i
];
257 static void hostapd_rx_from_unknown_sta(struct hostapd_data
*hapd
,
258 const struct ieee80211_hdr
*hdr
,
261 u16 fc
= le_to_host16(hdr
->frame_control
);
262 hapd
= get_hapd_bssid(hapd
->iface
, get_hdr_bssid(hdr
, len
));
263 if (hapd
== NULL
|| hapd
== HAPD_BROADCAST
)
266 ieee802_11_rx_from_unknown(hapd
, hdr
->addr2
,
267 (fc
& (WLAN_FC_TODS
| WLAN_FC_FROMDS
)) ==
268 (WLAN_FC_TODS
| WLAN_FC_FROMDS
));
272 static void hostapd_mgmt_rx(struct hostapd_data
*hapd
, const u8
*buf
,
273 size_t len
, struct hostapd_frame_info
*fi
)
275 struct hostapd_iface
*iface
= hapd
->iface
;
276 const struct ieee80211_hdr
*hdr
;
279 hdr
= (const struct ieee80211_hdr
*) buf
;
280 bssid
= get_hdr_bssid(hdr
, len
);
284 hapd
= get_hapd_bssid(iface
, bssid
);
287 fc
= le_to_host16(hdr
->frame_control
);
290 * Drop frames to unknown BSSIDs except for Beacon frames which
291 * could be used to update neighbor information.
293 if (WLAN_FC_GET_TYPE(fc
) == WLAN_FC_TYPE_MGMT
&&
294 WLAN_FC_GET_STYPE(fc
) == WLAN_FC_STYPE_BEACON
)
295 hapd
= iface
->bss
[0];
300 if (hapd
== HAPD_BROADCAST
) {
302 for (i
= 0; i
< iface
->num_bss
; i
++)
303 ieee802_11_mgmt(iface
->bss
[i
], buf
, len
, fi
);
305 ieee802_11_mgmt(hapd
, buf
, 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 void wpa_supplicant_event(void *ctx
, wpa_event_type event
,
324 union wpa_event_data
*data
)
326 struct hostapd_data
*hapd
= ctx
;
329 case EVENT_MICHAEL_MIC_FAILURE
:
330 michael_mic_failure(hapd
, data
->michael_mic_failure
.src
, 1);
332 case EVENT_SCAN_RESULTS
:
333 if (hapd
->iface
->scan_cb
)
334 hapd
->iface
->scan_cb(hapd
->iface
);
336 #ifdef CONFIG_IEEE80211R
337 case EVENT_FT_RRB_RX
:
338 wpa_ft_rrb_rx(hapd
->wpa_auth
, data
->ft_rrb_rx
.src
,
339 data
->ft_rrb_rx
.data
, data
->ft_rrb_rx
.data_len
);
341 #endif /* CONFIG_IEEE80211R */
342 case EVENT_WPS_BUTTON_PUSHED
:
343 hostapd_wps_button_pushed(hapd
);
346 case EVENT_TX_STATUS
:
347 switch (data
->tx_status
.type
) {
348 case WLAN_FC_TYPE_MGMT
:
349 hostapd_mgmt_tx_cb(hapd
, data
->tx_status
.data
,
350 data
->tx_status
.data_len
,
351 data
->tx_status
.stype
,
352 data
->tx_status
.ack
);
354 case WLAN_FC_TYPE_DATA
:
355 hostapd_tx_status(hapd
, data
->tx_status
.dst
,
356 data
->tx_status
.data
,
357 data
->tx_status
.data_len
,
358 data
->tx_status
.ack
);
362 case EVENT_RX_FROM_UNKNOWN
:
363 hostapd_rx_from_unknown_sta(hapd
, data
->rx_from_unknown
.hdr
,
364 data
->rx_from_unknown
.len
);
367 hostapd_mgmt_rx(hapd
, data
->rx_mgmt
.frame
,
368 data
->rx_mgmt
.frame_len
, data
->rx_mgmt
.fi
);
370 #endif /* NEED_AP_MLME */
372 wpa_printf(MSG_DEBUG
, "Unknown event %d", event
);
380 void hostapd_probe_req_rx(struct hostapd_data
*hapd
, const u8
*sa
,
381 const u8
*ie
, size_t ie_len
)
385 for (i
= 0; hapd
->probereq_cb
&& i
< hapd
->num_probereq_cb
; i
++)
386 hapd
->probereq_cb
[i
].cb(hapd
->probereq_cb
[i
].ctx
,