]> git.ipfire.org Git - thirdparty/hostap.git/blame - hostapd/drv_callbacks.c
nl80211: Remove some of the unnecessary conditional compilation
[thirdparty/hostap.git] / hostapd / drv_callbacks.c
CommitLineData
b5b969e9
JM
1/*
2 * hostapd / Callback functions for driver wrappers
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4 *
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.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16
4dbfe5c5 17#include "common.h"
b5b969e9
JM
18#include "hostapd.h"
19#include "driver_i.h"
20#include "ieee802_11.h"
21#include "radius/radius.h"
22#include "sta_info.h"
23#include "accounting.h"
24#include "tkip_countermeasures.h"
25#include "ieee802_1x.h"
26#include "wpa.h"
27#include "iapp.h"
28#include "wme.h"
3fed6f25 29#include "wps_hostapd.h"
b5b969e9
JM
30
31
32struct prune_data {
33 struct hostapd_data *hapd;
34 const u8 *addr;
35};
36
37static int prune_associations(struct hostapd_iface *iface, void *ctx)
38{
39 struct prune_data *data = ctx;
40 struct sta_info *osta;
41 struct hostapd_data *ohapd;
42 size_t j;
43
44 for (j = 0; j < iface->num_bss; j++) {
45 ohapd = iface->bss[j];
46 if (ohapd == data->hapd)
47 continue;
48 osta = ap_get_sta(ohapd, data->addr);
49 if (!osta)
50 continue;
51
52 ap_sta_disassociate(ohapd, osta, WLAN_REASON_UNSPECIFIED);
53 }
54
55 return 0;
56}
57
58/**
59 * hostapd_prune_associations - Remove extraneous associations
60 * @hapd: Pointer to BSS data for the most recent association
61 * @sta: Pointer to the associated STA data
62 *
63 * This function looks through all radios and BSS's for previous
64 * (stale) associations of STA. If any are found they are removed.
65 */
66static void hostapd_prune_associations(struct hostapd_data *hapd,
67 struct sta_info *sta)
68{
69 struct prune_data data;
70 data.hapd = hapd;
71 data.addr = sta->addr;
72 hostapd_for_each_interface(prune_associations, &data);
73}
74
75
76/**
77 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
78 * @hapd: Pointer to BSS data
79 * @sta: Pointer to the associated STA data
80 * @reassoc: 1 to indicate this was a re-association; 0 = first association
81 *
82 * This function will be called whenever a station associates with the AP. It
83 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
84 * from driver_*.c for drivers that take care of management frames (IEEE 802.11
85 * authentication and association) internally.
86 */
87void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
88 int reassoc)
89{
90 if (hapd->tkip_countermeasures) {
91 hostapd_sta_deauth(hapd, sta->addr,
92 WLAN_REASON_MICHAEL_MIC_FAILURE);
93 return;
94 }
95
96 hostapd_prune_associations(hapd, sta);
97
98 /* IEEE 802.11F (IAPP) */
99 if (hapd->conf->ieee802_11f)
100 iapp_new_station(hapd->iapp, sta);
101
102 /* Start accounting here, if IEEE 802.1X and WPA are not used.
103 * IEEE 802.1X/WPA code will start accounting after the station has
104 * been authorized. */
105 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
106 accounting_sta_start(hapd, sta);
107
b5b969e9
JM
108 /* Start IEEE 802.1X authentication process for new stations */
109 ieee802_1x_new_station(hapd, sta);
110 if (reassoc) {
111 if (sta->auth_alg != WLAN_AUTH_FT &&
112 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
113 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
114 } else
115 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
116}
117
118
119void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
120 const u8 *buf, size_t len, int ack)
121{
122 struct sta_info *sta;
4b9841d3 123 struct hostapd_iface *iface = hapd->iface;
b5b969e9
JM
124
125 sta = ap_get_sta(hapd, addr);
4b9841d3
JM
126 if (sta == NULL && iface->num_bss > 1) {
127 size_t j;
128 for (j = 0; j < iface->num_bss; j++) {
129 hapd = iface->bss[j];
130 sta = ap_get_sta(hapd, addr);
131 if (sta)
132 break;
133 }
134 }
135 if (sta == NULL)
136 return;
137 if (sta->flags & WLAN_STA_PENDING_POLL) {
b5b969e9
JM
138 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
139 "activity poll", MAC2STR(sta->addr),
140 ack ? "ACKed" : "did not ACK");
141 if (ack)
142 sta->flags &= ~WLAN_STA_PENDING_POLL;
143 }
4b9841d3
JM
144
145 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
146}
147
148
149static const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len)
150{
151 u16 fc, type, stype;
152
153 /*
154 * PS-Poll frames are 16 bytes. All other frames are
155 * 24 bytes or longer.
156 */
157 if (len < 16)
158 return NULL;
159
160 fc = le_to_host16(hdr->frame_control);
161 type = WLAN_FC_GET_TYPE(fc);
162 stype = WLAN_FC_GET_STYPE(fc);
163
164 switch (type) {
165 case WLAN_FC_TYPE_DATA:
166 if (len < 24)
167 return NULL;
168 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
169 case WLAN_FC_TODS:
170 return hdr->addr1;
171 case WLAN_FC_FROMDS:
172 return hdr->addr2;
173 default:
174 return NULL;
175 }
176 case WLAN_FC_TYPE_CTRL:
177 if (stype != WLAN_FC_STYPE_PSPOLL)
178 return NULL;
179 return hdr->addr1;
180 case WLAN_FC_TYPE_MGMT:
181 return hdr->addr3;
182 default:
183 return NULL;
184 }
185}
186
187
188#define HAPD_BROADCAST ((struct hostapd_data *) -1)
189
190static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
191 const u8 *bssid)
192{
193 size_t i;
194
195 if (bssid == NULL)
196 return NULL;
197 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
198 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
199 return HAPD_BROADCAST;
200
201 for (i = 0; i < iface->num_bss; i++) {
202 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
203 return iface->bss[i];
204 }
205
206 return NULL;
b5b969e9
JM
207}
208
209
4b9841d3
JM
210void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
211 const struct ieee80211_hdr *hdr, size_t len)
b5b969e9
JM
212{
213 struct sta_info *sta;
4b9841d3
JM
214 const u8 *addr;
215
216 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
217 if (hapd == NULL || hapd == HAPD_BROADCAST)
218 return;
b5b969e9 219
4b9841d3 220 addr = hdr->addr2;
b5b969e9
JM
221 sta = ap_get_sta(hapd, addr);
222 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
223 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated "
224 "STA " MACSTR, MAC2STR(addr));
225 if (sta && (sta->flags & WLAN_STA_AUTH))
226 hostapd_sta_disassoc(
227 hapd, addr,
228 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
229 else
230 hostapd_sta_deauth(
231 hapd, addr,
232 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
233 }
234}
235
236
05310066
JM
237int hostapd_notif_new_sta(struct hostapd_data *hapd, const u8 *addr)
238{
239 struct sta_info *sta = ap_get_sta(hapd, addr);
240 if (sta)
241 return 0;
242
243 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
244 " - adding a new STA", MAC2STR(addr));
245 sta = ap_sta_add(hapd, addr);
246 if (sta) {
247 hostapd_new_assoc_sta(hapd, sta, 0);
248 } else {
249 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
250 MAC2STR(addr));
251 return -1;
252 }
253
254 return 0;
255}
256
257
b5b969e9
JM
258int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
259 const u8 *ie, size_t ielen)
260{
261 struct sta_info *sta;
262 int new_assoc, res;
263
264 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
265 HOSTAPD_LEVEL_INFO, "associated");
266
267 sta = ap_get_sta(hapd, addr);
268 if (sta) {
269 accounting_sta_stop(hapd, sta);
270 } else {
271 sta = ap_sta_add(hapd, addr);
272 if (sta == NULL)
273 return -1;
274 }
275 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
276
277 if (hapd->conf->wpa) {
278 if (ie == NULL || ielen == 0) {
279 if (hapd->conf->wps_state) {
280 wpa_printf(MSG_DEBUG, "STA did not include "
281 "WPA/RSN IE in (Re)Association "
282 "Request - possible WPS use");
283 sta->flags |= WLAN_STA_MAYBE_WPS;
284 goto skip_wpa_check;
285 }
286
287 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
288 return -1;
289 }
290 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
291 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
292 sta->flags |= WLAN_STA_WPS;
293 goto skip_wpa_check;
294 }
295
296 if (sta->wpa_sm == NULL)
297 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
298 sta->addr);
299 if (sta->wpa_sm == NULL) {
300 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
301 "machine");
302 return -1;
303 }
304 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
305 ie, ielen, NULL, 0);
306 if (res != WPA_IE_OK) {
355d36a7 307 int resp;
b5b969e9
JM
308 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
309 "rejected? (res %u)", res);
310 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
355d36a7
AT
311 if (res == WPA_INVALID_GROUP)
312 resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
313 else if (res == WPA_INVALID_PAIRWISE)
314 resp = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
315 else if (res == WPA_INVALID_AKMP)
316 resp = WLAN_REASON_AKMP_NOT_VALID;
317#ifdef CONFIG_IEEE80211W
318 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
319 resp = WLAN_REASON_INVALID_IE;
320 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
321 resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
322#endif /* CONFIG_IEEE80211W */
323 else
324 resp = WLAN_REASON_INVALID_IE;
325 hostapd_sta_disassoc(hapd, sta->addr, resp);
326 ap_free_sta(hapd, sta);
b5b969e9
JM
327 return -1;
328 }
a9aca28b
JM
329 } else if (hapd->conf->wps_state) {
330 if (ie && ielen > 4 && ie[0] == 0xdd && ie[1] >= 4 &&
331 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
332 sta->flags |= WLAN_STA_WPS;
333 } else
334 sta->flags |= WLAN_STA_MAYBE_WPS;
b5b969e9
JM
335 }
336skip_wpa_check:
337
338 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
339 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
340 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
341
342 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
343
344 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
345
346 return 0;
347}
348
349
350void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
351{
352 struct sta_info *sta;
353
354 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
355 HOSTAPD_LEVEL_INFO, "disassociated");
356
357 sta = ap_get_sta(hapd, addr);
358 if (sta == NULL) {
359 wpa_printf(MSG_DEBUG, "Disassociation notification for "
360 "unknown STA " MACSTR, MAC2STR(addr));
361 return;
362 }
363
364 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
365 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
366 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
367 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
368 ap_free_sta(hapd, sta);
369}
370
371
372void hostapd_eapol_receive(struct hostapd_data *hapd, const u8 *sa,
373 const u8 *buf, size_t len)
374{
375 ieee802_1x_receive(hapd, sa, buf, len);
376}
377
378
fe6bdb77 379#ifdef NEED_AP_MLME
b5b969e9
JM
380void hostapd_mgmt_rx(struct hostapd_data *hapd, u8 *buf, size_t len,
381 u16 stype, struct hostapd_frame_info *fi)
382{
4b9841d3
JM
383 struct hostapd_iface *iface = hapd->iface;
384 struct ieee80211_hdr *hdr;
385 const u8 *bssid;
386
387 hdr = (struct ieee80211_hdr *) buf;
388 bssid = get_hdr_bssid(hdr, len);
389 if (bssid == NULL)
390 return;
391
392 hapd = get_hapd_bssid(iface, bssid);
393 if (hapd == NULL) {
394 u16 fc;
395 fc = le_to_host16(hdr->frame_control);
396
397 /*
398 * Drop frames to unknown BSSIDs except for Beacon frames which
399 * could be used to update neighbor information.
400 */
401 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
402 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
403 hapd = iface->bss[0];
404 else
405 return;
406 }
407
408 if (hapd == HAPD_BROADCAST) {
409 size_t i;
410 for (i = 0; i < iface->num_bss; i++)
411 ieee802_11_mgmt(iface->bss[i], buf, len, stype, fi);
412 } else
413 ieee802_11_mgmt(hapd, buf, len, stype, fi);
b5b969e9
JM
414}
415
416
417void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
418 u16 stype, int ok)
419{
4b9841d3
JM
420 struct ieee80211_hdr *hdr;
421 hdr = (struct ieee80211_hdr *) buf;
422 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
423 if (hapd == NULL || hapd == HAPD_BROADCAST)
424 return;
b5b969e9
JM
425 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
426}
fe6bdb77 427#endif /* NEED_AP_MLME */
b5b969e9
JM
428
429
f82ef4d8
JM
430struct hostapd_data * hostapd_sta_get_bss(struct hostapd_data *hapd,
431 const u8 *addr)
432{
433 struct hostapd_iface *iface = hapd->iface;
434 size_t j;
435
436 for (j = 0; j < iface->num_bss; j++) {
437 hapd = iface->bss[j];
438 if (ap_get_sta(hapd, addr))
439 return hapd;
440 }
441
442 return NULL;
443}
ad1e68e6
JM
444
445
446#ifndef CONFIG_AP
447void wpa_supplicant_event(void *ctx, wpa_event_type event,
448 union wpa_event_data *data)
449{
450 struct hostapd_data *hapd = ctx;
451
452 switch (event) {
453 case EVENT_MICHAEL_MIC_FAILURE:
454 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
455 break;
456 case EVENT_SCAN_RESULTS:
457 if (hapd->iface->scan_cb)
458 hapd->iface->scan_cb(hapd->iface);
459 break;
08fd8c15 460#ifdef CONFIG_IEEE80211R
f2dab64e 461 case EVENT_FT_RRB_RX:
08fd8c15
JM
462 wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
463 data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
464 break;
465#endif /* CONFIG_IEEE80211R */
fcf0f87d
JM
466 case EVENT_WPS_BUTTON_PUSHED:
467 hostapd_wps_button_pushed(hapd);
468 break;
ad1e68e6
JM
469 default:
470 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
471 break;
472 }
473}
474#endif /* CONFIG_AP */
3fed6f25
JM
475
476
477void hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa,
478 const u8 *ie, size_t ie_len)
479{
fa16028d
JM
480 size_t i;
481
482 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
483 hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
484 sa, ie, ie_len);
3fed6f25 485}