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_new_sta(struct hostapd_data
*hapd
, const u8
*addr
)
36 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
40 wpa_printf(MSG_DEBUG
, "Data frame from unknown STA " MACSTR
41 " - adding a new STA", MAC2STR(addr
));
42 sta
= ap_sta_add(hapd
, addr
);
44 hostapd_new_assoc_sta(hapd
, sta
, 0);
46 wpa_printf(MSG_DEBUG
, "Failed to add STA entry for " MACSTR
,
55 int hostapd_notif_assoc(struct hostapd_data
*hapd
, const u8
*addr
,
56 const u8
*ie
, size_t ielen
)
61 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_IEEE80211
,
62 HOSTAPD_LEVEL_INFO
, "associated");
64 sta
= ap_get_sta(hapd
, addr
);
66 accounting_sta_stop(hapd
, sta
);
68 sta
= ap_sta_add(hapd
, addr
);
72 sta
->flags
&= ~(WLAN_STA_WPS
| WLAN_STA_MAYBE_WPS
);
74 if (hapd
->conf
->wpa
) {
75 if (ie
== NULL
|| ielen
== 0) {
76 if (hapd
->conf
->wps_state
) {
77 wpa_printf(MSG_DEBUG
, "STA did not include "
78 "WPA/RSN IE in (Re)Association "
79 "Request - possible WPS use");
80 sta
->flags
|= WLAN_STA_MAYBE_WPS
;
84 wpa_printf(MSG_DEBUG
, "No WPA/RSN IE from STA");
87 if (hapd
->conf
->wps_state
&& ie
[0] == 0xdd && ie
[1] >= 4 &&
88 os_memcmp(ie
+ 2, "\x00\x50\xf2\x04", 4) == 0) {
89 sta
->flags
|= WLAN_STA_WPS
;
93 if (sta
->wpa_sm
== NULL
)
94 sta
->wpa_sm
= wpa_auth_sta_init(hapd
->wpa_auth
,
96 if (sta
->wpa_sm
== NULL
) {
97 wpa_printf(MSG_ERROR
, "Failed to initialize WPA state "
101 res
= wpa_validate_wpa_ie(hapd
->wpa_auth
, sta
->wpa_sm
,
103 if (res
!= WPA_IE_OK
) {
105 wpa_printf(MSG_DEBUG
, "WPA/RSN information element "
106 "rejected? (res %u)", res
);
107 wpa_hexdump(MSG_DEBUG
, "IE", ie
, ielen
);
108 if (res
== WPA_INVALID_GROUP
)
109 resp
= WLAN_REASON_GROUP_CIPHER_NOT_VALID
;
110 else if (res
== WPA_INVALID_PAIRWISE
)
111 resp
= WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID
;
112 else if (res
== WPA_INVALID_AKMP
)
113 resp
= WLAN_REASON_AKMP_NOT_VALID
;
114 #ifdef CONFIG_IEEE80211W
115 else if (res
== WPA_MGMT_FRAME_PROTECTION_VIOLATION
)
116 resp
= WLAN_REASON_INVALID_IE
;
117 else if (res
== WPA_INVALID_MGMT_GROUP_CIPHER
)
118 resp
= WLAN_REASON_GROUP_CIPHER_NOT_VALID
;
119 #endif /* CONFIG_IEEE80211W */
121 resp
= WLAN_REASON_INVALID_IE
;
122 hapd
->drv
.sta_disassoc(hapd
, sta
->addr
, resp
);
123 ap_free_sta(hapd
, sta
);
126 } else if (hapd
->conf
->wps_state
) {
127 if (ie
&& ielen
> 4 && ie
[0] == 0xdd && ie
[1] >= 4 &&
128 os_memcmp(ie
+ 2, "\x00\x50\xf2\x04", 4) == 0) {
129 sta
->flags
|= WLAN_STA_WPS
;
131 sta
->flags
|= WLAN_STA_MAYBE_WPS
;
135 new_assoc
= (sta
->flags
& WLAN_STA_ASSOC
) == 0;
136 sta
->flags
|= WLAN_STA_AUTH
| WLAN_STA_ASSOC
;
137 wpa_auth_sm_event(sta
->wpa_sm
, WPA_ASSOC
);
139 hostapd_new_assoc_sta(hapd
, sta
, !new_assoc
);
141 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, 1);
147 void hostapd_notif_disassoc(struct hostapd_data
*hapd
, const u8
*addr
)
149 struct sta_info
*sta
;
151 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_IEEE80211
,
152 HOSTAPD_LEVEL_INFO
, "disassociated");
154 sta
= ap_get_sta(hapd
, addr
);
156 wpa_printf(MSG_DEBUG
, "Disassociation notification for "
157 "unknown STA " MACSTR
, MAC2STR(addr
));
161 sta
->flags
&= ~(WLAN_STA_AUTH
| WLAN_STA_ASSOC
);
162 wpa_auth_sm_event(sta
->wpa_sm
, WPA_DISASSOC
);
163 sta
->acct_terminate_cause
= RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST
;
164 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, 0);
165 ap_free_sta(hapd
, sta
);
169 void hostapd_eapol_receive(struct hostapd_data
*hapd
, const u8
*sa
,
170 const u8
*buf
, size_t len
)
172 ieee802_1x_receive(hapd
, sa
, buf
, len
);
176 struct hostapd_data
* hostapd_sta_get_bss(struct hostapd_data
*hapd
,
179 struct hostapd_iface
*iface
= hapd
->iface
;
182 for (j
= 0; j
< iface
->num_bss
; j
++) {
183 hapd
= iface
->bss
[j
];
184 if (ap_get_sta(hapd
, addr
))
196 static const u8
* get_hdr_bssid(const struct ieee80211_hdr
*hdr
, size_t len
)
201 * PS-Poll frames are 16 bytes. All other frames are
202 * 24 bytes or longer.
207 fc
= le_to_host16(hdr
->frame_control
);
208 type
= WLAN_FC_GET_TYPE(fc
);
209 stype
= WLAN_FC_GET_STYPE(fc
);
212 case WLAN_FC_TYPE_DATA
:
215 switch (fc
& (WLAN_FC_FROMDS
| WLAN_FC_TODS
)) {
216 case WLAN_FC_FROMDS
| WLAN_FC_TODS
:
224 case WLAN_FC_TYPE_CTRL
:
225 if (stype
!= WLAN_FC_STYPE_PSPOLL
)
228 case WLAN_FC_TYPE_MGMT
:
236 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
238 static struct hostapd_data
* get_hapd_bssid(struct hostapd_iface
*iface
,
245 if (bssid
[0] == 0xff && bssid
[1] == 0xff && bssid
[2] == 0xff &&
246 bssid
[3] == 0xff && bssid
[4] == 0xff && bssid
[5] == 0xff)
247 return HAPD_BROADCAST
;
249 for (i
= 0; i
< iface
->num_bss
; i
++) {
250 if (os_memcmp(bssid
, iface
->bss
[i
]->own_addr
, ETH_ALEN
) == 0)
251 return iface
->bss
[i
];
258 static void hostapd_rx_from_unknown_sta(struct hostapd_data
*hapd
,
259 const struct ieee80211_hdr
*hdr
,
262 u16 fc
= le_to_host16(hdr
->frame_control
);
263 hapd
= get_hapd_bssid(hapd
->iface
, get_hdr_bssid(hdr
, len
));
264 if (hapd
== NULL
|| hapd
== HAPD_BROADCAST
)
267 ieee802_11_rx_from_unknown(hapd
, hdr
->addr2
,
268 (fc
& (WLAN_FC_TODS
| WLAN_FC_FROMDS
)) ==
269 (WLAN_FC_TODS
| WLAN_FC_FROMDS
));
273 static void hostapd_mgmt_rx(struct hostapd_data
*hapd
, const u8
*buf
,
274 size_t len
, struct hostapd_frame_info
*fi
)
276 struct hostapd_iface
*iface
= hapd
->iface
;
277 const struct ieee80211_hdr
*hdr
;
280 hdr
= (const struct ieee80211_hdr
*) buf
;
281 bssid
= get_hdr_bssid(hdr
, len
);
285 hapd
= get_hapd_bssid(iface
, bssid
);
288 fc
= le_to_host16(hdr
->frame_control
);
291 * Drop frames to unknown BSSIDs except for Beacon frames which
292 * could be used to update neighbor information.
294 if (WLAN_FC_GET_TYPE(fc
) == WLAN_FC_TYPE_MGMT
&&
295 WLAN_FC_GET_STYPE(fc
) == WLAN_FC_STYPE_BEACON
)
296 hapd
= iface
->bss
[0];
301 if (hapd
== HAPD_BROADCAST
) {
303 for (i
= 0; i
< iface
->num_bss
; i
++)
304 ieee802_11_mgmt(iface
->bss
[i
], buf
, len
, fi
);
306 ieee802_11_mgmt(hapd
, buf
, len
, fi
);
310 static void hostapd_mgmt_tx_cb(struct hostapd_data
*hapd
, const u8
*buf
,
311 size_t len
, u16 stype
, int ok
)
313 struct ieee80211_hdr
*hdr
;
314 hdr
= (struct ieee80211_hdr
*) buf
;
315 hapd
= get_hapd_bssid(hapd
->iface
, get_hdr_bssid(hdr
, len
));
316 if (hapd
== NULL
|| hapd
== HAPD_BROADCAST
)
318 ieee802_11_mgmt_cb(hapd
, buf
, len
, stype
, ok
);
321 #endif /* NEED_AP_MLME */
324 void wpa_supplicant_event(void *ctx
, wpa_event_type event
,
325 union wpa_event_data
*data
)
327 struct hostapd_data
*hapd
= ctx
;
330 case EVENT_MICHAEL_MIC_FAILURE
:
331 michael_mic_failure(hapd
, data
->michael_mic_failure
.src
, 1);
333 case EVENT_SCAN_RESULTS
:
334 if (hapd
->iface
->scan_cb
)
335 hapd
->iface
->scan_cb(hapd
->iface
);
337 #ifdef CONFIG_IEEE80211R
338 case EVENT_FT_RRB_RX
:
339 wpa_ft_rrb_rx(hapd
->wpa_auth
, data
->ft_rrb_rx
.src
,
340 data
->ft_rrb_rx
.data
, data
->ft_rrb_rx
.data_len
);
342 #endif /* CONFIG_IEEE80211R */
343 case EVENT_WPS_BUTTON_PUSHED
:
344 hostapd_wps_button_pushed(hapd
);
347 case EVENT_TX_STATUS
:
348 switch (data
->tx_status
.type
) {
349 case WLAN_FC_TYPE_MGMT
:
350 hostapd_mgmt_tx_cb(hapd
, data
->tx_status
.data
,
351 data
->tx_status
.data_len
,
352 data
->tx_status
.stype
,
353 data
->tx_status
.ack
);
355 case WLAN_FC_TYPE_DATA
:
356 hostapd_tx_status(hapd
, data
->tx_status
.dst
,
357 data
->tx_status
.data
,
358 data
->tx_status
.data_len
,
359 data
->tx_status
.ack
);
363 case EVENT_RX_FROM_UNKNOWN
:
364 hostapd_rx_from_unknown_sta(hapd
, data
->rx_from_unknown
.hdr
,
365 data
->rx_from_unknown
.len
);
368 hostapd_mgmt_rx(hapd
, data
->rx_mgmt
.frame
,
369 data
->rx_mgmt
.frame_len
, data
->rx_mgmt
.fi
);
371 #endif /* NEED_AP_MLME */
373 wpa_printf(MSG_DEBUG
, "Unknown event %d", event
);
381 int hostapd_probe_req_rx(struct hostapd_data
*hapd
, const u8
*sa
,
382 const u8
*ie
, size_t ie_len
)
387 for (i
= 0; hapd
->probereq_cb
&& i
< hapd
->num_probereq_cb
; i
++) {
388 if (hapd
->probereq_cb
[i
].cb(hapd
->probereq_cb
[i
].ctx
,
389 sa
, ie
, ie_len
) > 0) {