]> git.ipfire.org Git - thirdparty/hostap.git/blame - hostapd/drv_callbacks.c
Mark ieee802_11_parse_elems() input and parsed elems const
[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
05310066
JM
119int hostapd_notif_new_sta(struct hostapd_data *hapd, const u8 *addr)
120{
121 struct sta_info *sta = ap_get_sta(hapd, addr);
122 if (sta)
123 return 0;
124
125 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
126 " - adding a new STA", MAC2STR(addr));
127 sta = ap_sta_add(hapd, addr);
128 if (sta) {
129 hostapd_new_assoc_sta(hapd, sta, 0);
130 } else {
131 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
132 MAC2STR(addr));
133 return -1;
134 }
135
136 return 0;
137}
138
139
b5b969e9
JM
140int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
141 const u8 *ie, size_t ielen)
142{
143 struct sta_info *sta;
144 int new_assoc, res;
145
146 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
147 HOSTAPD_LEVEL_INFO, "associated");
148
149 sta = ap_get_sta(hapd, addr);
150 if (sta) {
151 accounting_sta_stop(hapd, sta);
152 } else {
153 sta = ap_sta_add(hapd, addr);
154 if (sta == NULL)
155 return -1;
156 }
157 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
158
159 if (hapd->conf->wpa) {
160 if (ie == NULL || ielen == 0) {
161 if (hapd->conf->wps_state) {
162 wpa_printf(MSG_DEBUG, "STA did not include "
163 "WPA/RSN IE in (Re)Association "
164 "Request - possible WPS use");
165 sta->flags |= WLAN_STA_MAYBE_WPS;
166 goto skip_wpa_check;
167 }
168
169 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
170 return -1;
171 }
172 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
173 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
174 sta->flags |= WLAN_STA_WPS;
175 goto skip_wpa_check;
176 }
177
178 if (sta->wpa_sm == NULL)
179 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
180 sta->addr);
181 if (sta->wpa_sm == NULL) {
182 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
183 "machine");
184 return -1;
185 }
186 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
187 ie, ielen, NULL, 0);
188 if (res != WPA_IE_OK) {
355d36a7 189 int resp;
b5b969e9
JM
190 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
191 "rejected? (res %u)", res);
192 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
355d36a7
AT
193 if (res == WPA_INVALID_GROUP)
194 resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
195 else if (res == WPA_INVALID_PAIRWISE)
196 resp = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
197 else if (res == WPA_INVALID_AKMP)
198 resp = WLAN_REASON_AKMP_NOT_VALID;
199#ifdef CONFIG_IEEE80211W
200 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
201 resp = WLAN_REASON_INVALID_IE;
202 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
203 resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
204#endif /* CONFIG_IEEE80211W */
205 else
206 resp = WLAN_REASON_INVALID_IE;
207 hostapd_sta_disassoc(hapd, sta->addr, resp);
208 ap_free_sta(hapd, sta);
b5b969e9
JM
209 return -1;
210 }
a9aca28b
JM
211 } else if (hapd->conf->wps_state) {
212 if (ie && ielen > 4 && ie[0] == 0xdd && ie[1] >= 4 &&
213 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
214 sta->flags |= WLAN_STA_WPS;
215 } else
216 sta->flags |= WLAN_STA_MAYBE_WPS;
b5b969e9
JM
217 }
218skip_wpa_check:
219
220 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
221 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
222 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
223
224 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
225
226 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
227
228 return 0;
229}
230
231
232void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
233{
234 struct sta_info *sta;
235
236 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
237 HOSTAPD_LEVEL_INFO, "disassociated");
238
239 sta = ap_get_sta(hapd, addr);
240 if (sta == NULL) {
241 wpa_printf(MSG_DEBUG, "Disassociation notification for "
242 "unknown STA " MACSTR, MAC2STR(addr));
243 return;
244 }
245
246 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
247 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
248 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
249 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
250 ap_free_sta(hapd, sta);
251}
252
253
254void hostapd_eapol_receive(struct hostapd_data *hapd, const u8 *sa,
255 const u8 *buf, size_t len)
256{
257 ieee802_1x_receive(hapd, sa, buf, len);
258}
259
260
f8b1f695
JM
261struct hostapd_data * hostapd_sta_get_bss(struct hostapd_data *hapd,
262 const u8 *addr)
263{
264 struct hostapd_iface *iface = hapd->iface;
265 size_t j;
266
267 for (j = 0; j < iface->num_bss; j++) {
268 hapd = iface->bss[j];
269 if (ap_get_sta(hapd, addr))
270 return hapd;
271 }
272
273 return NULL;
274}
275
276
277#ifdef HOSTAPD
278
fe6bdb77 279#ifdef NEED_AP_MLME
f8b1f695
JM
280
281static const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len)
282{
283 u16 fc, type, stype;
284
285 /*
286 * PS-Poll frames are 16 bytes. All other frames are
287 * 24 bytes or longer.
288 */
289 if (len < 16)
290 return NULL;
291
292 fc = le_to_host16(hdr->frame_control);
293 type = WLAN_FC_GET_TYPE(fc);
294 stype = WLAN_FC_GET_STYPE(fc);
295
296 switch (type) {
297 case WLAN_FC_TYPE_DATA:
298 if (len < 24)
299 return NULL;
300 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
301 case WLAN_FC_TODS:
302 return hdr->addr1;
303 case WLAN_FC_FROMDS:
304 return hdr->addr2;
305 default:
306 return NULL;
307 }
308 case WLAN_FC_TYPE_CTRL:
309 if (stype != WLAN_FC_STYPE_PSPOLL)
310 return NULL;
311 return hdr->addr1;
312 case WLAN_FC_TYPE_MGMT:
313 return hdr->addr3;
314 default:
315 return NULL;
316 }
317}
318
319
320#define HAPD_BROADCAST ((struct hostapd_data *) -1)
321
322static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
323 const u8 *bssid)
324{
325 size_t i;
326
327 if (bssid == NULL)
328 return NULL;
329 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
330 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
331 return HAPD_BROADCAST;
332
333 for (i = 0; i < iface->num_bss; i++) {
334 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
335 return iface->bss[i];
336 }
337
338 return NULL;
339}
340
341
342static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
343 const struct ieee80211_hdr *hdr,
344 size_t len)
345{
346 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
347 if (hapd == NULL || hapd == HAPD_BROADCAST)
348 return;
349
350 ieee802_11_rx_from_unknown(hapd, hdr->addr2);
351}
352
353
354static void hostapd_mgmt_rx(struct hostapd_data *hapd, u8 *buf,
355 size_t len, struct hostapd_frame_info *fi)
b5b969e9 356{
4b9841d3
JM
357 struct hostapd_iface *iface = hapd->iface;
358 struct ieee80211_hdr *hdr;
359 const u8 *bssid;
360
361 hdr = (struct ieee80211_hdr *) buf;
362 bssid = get_hdr_bssid(hdr, len);
363 if (bssid == NULL)
364 return;
365
366 hapd = get_hapd_bssid(iface, bssid);
367 if (hapd == NULL) {
368 u16 fc;
369 fc = le_to_host16(hdr->frame_control);
370
371 /*
372 * Drop frames to unknown BSSIDs except for Beacon frames which
373 * could be used to update neighbor information.
374 */
375 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
376 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
377 hapd = iface->bss[0];
378 else
379 return;
380 }
381
382 if (hapd == HAPD_BROADCAST) {
383 size_t i;
384 for (i = 0; i < iface->num_bss; i++)
f8b1f695 385 ieee802_11_mgmt(iface->bss[i], buf, len, fi);
4b9841d3 386 } else
f8b1f695 387 ieee802_11_mgmt(hapd, buf, len, fi);
b5b969e9
JM
388}
389
390
f8b1f695
JM
391static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
392 size_t len, u16 stype, int ok)
b5b969e9 393{
4b9841d3
JM
394 struct ieee80211_hdr *hdr;
395 hdr = (struct ieee80211_hdr *) buf;
396 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
397 if (hapd == NULL || hapd == HAPD_BROADCAST)
398 return;
b5b969e9
JM
399 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
400}
f82ef4d8 401
f8b1f695 402#endif /* NEED_AP_MLME */
ad1e68e6
JM
403
404
ad1e68e6
JM
405void wpa_supplicant_event(void *ctx, wpa_event_type event,
406 union wpa_event_data *data)
407{
408 struct hostapd_data *hapd = ctx;
409
410 switch (event) {
411 case EVENT_MICHAEL_MIC_FAILURE:
412 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
413 break;
414 case EVENT_SCAN_RESULTS:
415 if (hapd->iface->scan_cb)
416 hapd->iface->scan_cb(hapd->iface);
417 break;
08fd8c15 418#ifdef CONFIG_IEEE80211R
f2dab64e 419 case EVENT_FT_RRB_RX:
08fd8c15
JM
420 wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
421 data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
422 break;
423#endif /* CONFIG_IEEE80211R */
fcf0f87d
JM
424 case EVENT_WPS_BUTTON_PUSHED:
425 hostapd_wps_button_pushed(hapd);
426 break;
f8b1f695
JM
427#ifdef NEED_AP_MLME
428 case EVENT_TX_STATUS:
429 switch (data->tx_status.type) {
430 case WLAN_FC_TYPE_MGMT:
431 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
432 data->tx_status.data_len,
433 data->tx_status.stype,
434 data->tx_status.ack);
435 break;
436 case WLAN_FC_TYPE_DATA:
437 hostapd_tx_status(hapd, data->tx_status.dst,
438 data->tx_status.data,
439 data->tx_status.data_len,
440 data->tx_status.ack);
441 break;
442 }
443 break;
444 case EVENT_RX_FROM_UNKNOWN:
445 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.hdr,
446 data->rx_from_unknown.len);
447 break;
448 case EVENT_RX_MGMT:
449 hostapd_mgmt_rx(hapd, data->rx_mgmt.frame,
450 data->rx_mgmt.frame_len, data->rx_mgmt.fi);
451 break;
452#endif /* NEED_AP_MLME */
ad1e68e6
JM
453 default:
454 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
455 break;
456 }
457}
f8b1f695
JM
458
459#endif /* HOSTAPD */
3fed6f25
JM
460
461
462void hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa,
463 const u8 *ie, size_t ie_len)
464{
fa16028d
JM
465 size_t i;
466
467 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
468 hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
469 sa, ie, ie_len);
3fed6f25 470}