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"
20 #include "ieee802_11.h"
22 #include "accounting.h"
23 #include "tkip_countermeasures.h"
25 #include "ieee802_1x.h"
28 #include "wps_hostapd.h"
32 int hostapd_notif_new_sta(struct hostapd_data
*hapd
, const u8
*addr
)
34 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
38 wpa_printf(MSG_DEBUG
, "Data frame from unknown STA " MACSTR
39 " - adding a new STA", MAC2STR(addr
));
40 sta
= ap_sta_add(hapd
, addr
);
42 hostapd_new_assoc_sta(hapd
, sta
, 0);
44 wpa_printf(MSG_DEBUG
, "Failed to add STA entry for " MACSTR
,
53 int hostapd_notif_assoc(struct hostapd_data
*hapd
, const u8
*addr
,
54 const u8
*ie
, size_t ielen
)
59 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_IEEE80211
,
60 HOSTAPD_LEVEL_INFO
, "associated");
62 sta
= ap_get_sta(hapd
, addr
);
64 accounting_sta_stop(hapd
, sta
);
66 sta
= ap_sta_add(hapd
, addr
);
70 sta
->flags
&= ~(WLAN_STA_WPS
| WLAN_STA_MAYBE_WPS
);
72 if (hapd
->conf
->wpa
) {
73 if (ie
== NULL
|| ielen
== 0) {
74 if (hapd
->conf
->wps_state
) {
75 wpa_printf(MSG_DEBUG
, "STA did not include "
76 "WPA/RSN IE in (Re)Association "
77 "Request - possible WPS use");
78 sta
->flags
|= WLAN_STA_MAYBE_WPS
;
82 wpa_printf(MSG_DEBUG
, "No WPA/RSN IE from STA");
85 if (hapd
->conf
->wps_state
&& ie
[0] == 0xdd && ie
[1] >= 4 &&
86 os_memcmp(ie
+ 2, "\x00\x50\xf2\x04", 4) == 0) {
87 sta
->flags
|= WLAN_STA_WPS
;
91 if (sta
->wpa_sm
== NULL
)
92 sta
->wpa_sm
= wpa_auth_sta_init(hapd
->wpa_auth
,
94 if (sta
->wpa_sm
== NULL
) {
95 wpa_printf(MSG_ERROR
, "Failed to initialize WPA state "
99 res
= wpa_validate_wpa_ie(hapd
->wpa_auth
, sta
->wpa_sm
,
101 if (res
!= WPA_IE_OK
) {
103 wpa_printf(MSG_DEBUG
, "WPA/RSN information element "
104 "rejected? (res %u)", res
);
105 wpa_hexdump(MSG_DEBUG
, "IE", ie
, ielen
);
106 if (res
== WPA_INVALID_GROUP
)
107 resp
= WLAN_REASON_GROUP_CIPHER_NOT_VALID
;
108 else if (res
== WPA_INVALID_PAIRWISE
)
109 resp
= WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID
;
110 else if (res
== WPA_INVALID_AKMP
)
111 resp
= WLAN_REASON_AKMP_NOT_VALID
;
112 #ifdef CONFIG_IEEE80211W
113 else if (res
== WPA_MGMT_FRAME_PROTECTION_VIOLATION
)
114 resp
= WLAN_REASON_INVALID_IE
;
115 else if (res
== WPA_INVALID_MGMT_GROUP_CIPHER
)
116 resp
= WLAN_REASON_GROUP_CIPHER_NOT_VALID
;
117 #endif /* CONFIG_IEEE80211W */
119 resp
= WLAN_REASON_INVALID_IE
;
120 hapd
->drv
.sta_disassoc(hapd
, sta
->addr
, resp
);
121 ap_free_sta(hapd
, sta
);
124 } else if (hapd
->conf
->wps_state
) {
125 if (ie
&& ielen
> 4 && ie
[0] == 0xdd && ie
[1] >= 4 &&
126 os_memcmp(ie
+ 2, "\x00\x50\xf2\x04", 4) == 0) {
127 sta
->flags
|= WLAN_STA_WPS
;
129 sta
->flags
|= WLAN_STA_MAYBE_WPS
;
133 new_assoc
= (sta
->flags
& WLAN_STA_ASSOC
) == 0;
134 sta
->flags
|= WLAN_STA_AUTH
| WLAN_STA_ASSOC
;
135 wpa_auth_sm_event(sta
->wpa_sm
, WPA_ASSOC
);
137 hostapd_new_assoc_sta(hapd
, sta
, !new_assoc
);
139 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, 1);
145 void hostapd_notif_disassoc(struct hostapd_data
*hapd
, const u8
*addr
)
147 struct sta_info
*sta
;
149 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_IEEE80211
,
150 HOSTAPD_LEVEL_INFO
, "disassociated");
152 sta
= ap_get_sta(hapd
, addr
);
154 wpa_printf(MSG_DEBUG
, "Disassociation notification for "
155 "unknown STA " MACSTR
, MAC2STR(addr
));
159 sta
->flags
&= ~(WLAN_STA_AUTH
| WLAN_STA_ASSOC
);
160 wpa_auth_sm_event(sta
->wpa_sm
, WPA_DISASSOC
);
161 sta
->acct_terminate_cause
= RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST
;
162 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, 0);
163 ap_free_sta(hapd
, sta
);
167 void hostapd_eapol_receive(struct hostapd_data
*hapd
, const u8
*sa
,
168 const u8
*buf
, size_t len
)
170 ieee802_1x_receive(hapd
, sa
, buf
, len
);
174 struct hostapd_data
* hostapd_sta_get_bss(struct hostapd_data
*hapd
,
177 struct hostapd_iface
*iface
= hapd
->iface
;
180 for (j
= 0; j
< iface
->num_bss
; j
++) {
181 hapd
= iface
->bss
[j
];
182 if (ap_get_sta(hapd
, addr
))
194 static const u8
* get_hdr_bssid(const struct ieee80211_hdr
*hdr
, size_t len
)
199 * PS-Poll frames are 16 bytes. All other frames are
200 * 24 bytes or longer.
205 fc
= le_to_host16(hdr
->frame_control
);
206 type
= WLAN_FC_GET_TYPE(fc
);
207 stype
= WLAN_FC_GET_STYPE(fc
);
210 case WLAN_FC_TYPE_DATA
:
213 switch (fc
& (WLAN_FC_FROMDS
| WLAN_FC_TODS
)) {
214 case WLAN_FC_FROMDS
| WLAN_FC_TODS
:
222 case WLAN_FC_TYPE_CTRL
:
223 if (stype
!= WLAN_FC_STYPE_PSPOLL
)
226 case WLAN_FC_TYPE_MGMT
:
234 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
236 static struct hostapd_data
* get_hapd_bssid(struct hostapd_iface
*iface
,
243 if (bssid
[0] == 0xff && bssid
[1] == 0xff && bssid
[2] == 0xff &&
244 bssid
[3] == 0xff && bssid
[4] == 0xff && bssid
[5] == 0xff)
245 return HAPD_BROADCAST
;
247 for (i
= 0; i
< iface
->num_bss
; i
++) {
248 if (os_memcmp(bssid
, iface
->bss
[i
]->own_addr
, ETH_ALEN
) == 0)
249 return iface
->bss
[i
];
256 static void hostapd_rx_from_unknown_sta(struct hostapd_data
*hapd
,
257 const struct ieee80211_hdr
*hdr
,
260 u16 fc
= le_to_host16(hdr
->frame_control
);
261 hapd
= get_hapd_bssid(hapd
->iface
, get_hdr_bssid(hdr
, len
));
262 if (hapd
== NULL
|| hapd
== HAPD_BROADCAST
)
265 ieee802_11_rx_from_unknown(hapd
, hdr
->addr2
,
266 (fc
& (WLAN_FC_TODS
| WLAN_FC_FROMDS
)) ==
267 (WLAN_FC_TODS
| WLAN_FC_FROMDS
));
271 static void hostapd_mgmt_rx(struct hostapd_data
*hapd
, const u8
*buf
,
272 size_t len
, struct hostapd_frame_info
*fi
)
274 struct hostapd_iface
*iface
= hapd
->iface
;
275 const struct ieee80211_hdr
*hdr
;
278 hdr
= (const struct ieee80211_hdr
*) buf
;
279 bssid
= get_hdr_bssid(hdr
, len
);
283 hapd
= get_hapd_bssid(iface
, bssid
);
286 fc
= le_to_host16(hdr
->frame_control
);
289 * Drop frames to unknown BSSIDs except for Beacon frames which
290 * could be used to update neighbor information.
292 if (WLAN_FC_GET_TYPE(fc
) == WLAN_FC_TYPE_MGMT
&&
293 WLAN_FC_GET_STYPE(fc
) == WLAN_FC_STYPE_BEACON
)
294 hapd
= iface
->bss
[0];
299 if (hapd
== HAPD_BROADCAST
) {
301 for (i
= 0; i
< iface
->num_bss
; i
++)
302 ieee802_11_mgmt(iface
->bss
[i
], buf
, len
, fi
);
304 ieee802_11_mgmt(hapd
, buf
, len
, fi
);
308 static void hostapd_mgmt_tx_cb(struct hostapd_data
*hapd
, const u8
*buf
,
309 size_t len
, u16 stype
, int ok
)
311 struct ieee80211_hdr
*hdr
;
312 hdr
= (struct ieee80211_hdr
*) buf
;
313 hapd
= get_hapd_bssid(hapd
->iface
, get_hdr_bssid(hdr
, len
));
314 if (hapd
== NULL
|| hapd
== HAPD_BROADCAST
)
316 ieee802_11_mgmt_cb(hapd
, buf
, len
, stype
, ok
);
319 #endif /* NEED_AP_MLME */
322 void wpa_supplicant_event(void *ctx
, wpa_event_type event
,
323 union wpa_event_data
*data
)
325 struct hostapd_data
*hapd
= ctx
;
328 case EVENT_MICHAEL_MIC_FAILURE
:
329 michael_mic_failure(hapd
, data
->michael_mic_failure
.src
, 1);
331 case EVENT_SCAN_RESULTS
:
332 if (hapd
->iface
->scan_cb
)
333 hapd
->iface
->scan_cb(hapd
->iface
);
335 #ifdef CONFIG_IEEE80211R
336 case EVENT_FT_RRB_RX
:
337 wpa_ft_rrb_rx(hapd
->wpa_auth
, data
->ft_rrb_rx
.src
,
338 data
->ft_rrb_rx
.data
, data
->ft_rrb_rx
.data_len
);
340 #endif /* CONFIG_IEEE80211R */
341 case EVENT_WPS_BUTTON_PUSHED
:
342 hostapd_wps_button_pushed(hapd
);
345 case EVENT_TX_STATUS
:
346 switch (data
->tx_status
.type
) {
347 case WLAN_FC_TYPE_MGMT
:
348 hostapd_mgmt_tx_cb(hapd
, data
->tx_status
.data
,
349 data
->tx_status
.data_len
,
350 data
->tx_status
.stype
,
351 data
->tx_status
.ack
);
353 case WLAN_FC_TYPE_DATA
:
354 hostapd_tx_status(hapd
, data
->tx_status
.dst
,
355 data
->tx_status
.data
,
356 data
->tx_status
.data_len
,
357 data
->tx_status
.ack
);
361 case EVENT_RX_FROM_UNKNOWN
:
362 hostapd_rx_from_unknown_sta(hapd
, data
->rx_from_unknown
.hdr
,
363 data
->rx_from_unknown
.len
);
366 hostapd_mgmt_rx(hapd
, data
->rx_mgmt
.frame
,
367 data
->rx_mgmt
.frame_len
, data
->rx_mgmt
.fi
);
369 #endif /* NEED_AP_MLME */
371 wpa_printf(MSG_DEBUG
, "Unknown event %d", event
);
379 void hostapd_probe_req_rx(struct hostapd_data
*hapd
, const u8
*sa
,
380 const u8
*ie
, size_t ie_len
)
384 for (i
= 0; hapd
->probereq_cb
&& i
< hapd
->num_probereq_cb
; i
++)
385 hapd
->probereq_cb
[i
].cb(hapd
->probereq_cb
[i
].ctx
,