]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/drv_callbacks.c
hostapd_driver_ops reduction: set_countermeasures
[thirdparty/hostap.git] / src / ap / 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
6226e38d 15#include "utils/includes.h"
b5b969e9 16
6226e38d 17#include "utils/common.h"
b5b969e9 18#include "radius/radius.h"
6e6e8c31 19#include "drivers/driver.h"
81f4f619 20#include "common/ieee802_11_defs.h"
c41a1095 21#include "common/ieee802_11_common.h"
9a3cb18d 22#include "common/wpa_ctrl.h"
bbb921da 23#include "crypto/random.h"
ef796391 24#include "p2p/p2p.h"
54f489be 25#include "wps/wps.h"
6226e38d
JM
26#include "hostapd.h"
27#include "ieee802_11.h"
28#include "sta_info.h"
29#include "accounting.h"
30#include "tkip_countermeasures.h"
31#include "iapp.h"
32#include "ieee802_1x.h"
33#include "wpa_auth.h"
34#include "wmm.h"
35#include "wps_hostapd.h"
8b06c1ed 36#include "ap_config.h"
b5b969e9
JM
37
38
b5b969e9
JM
39int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
40 const u8 *ie, size_t ielen)
41{
42 struct sta_info *sta;
43 int new_assoc, res;
c41a1095 44 struct ieee802_11_elems elems;
b305c684
JM
45#ifdef CONFIG_P2P
46 const u8 *all_ies = ie;
47 size_t all_ies_len = ielen;
48#endif /* CONFIG_P2P */
b5b969e9 49
68532a9c
JM
50 if (addr == NULL) {
51 /*
52 * This could potentially happen with unexpected event from the
53 * driver wrapper. This was seen at least in one case where the
54 * driver ended up being set to station mode while hostapd was
55 * running, so better make sure we stop processing such an
56 * event here.
57 */
58 wpa_printf(MSG_DEBUG, "hostapd_notif_assoc: Skip event with "
59 "no address");
60 return -1;
61 }
bbb921da 62 random_add_randomness(addr, ETH_ALEN);
68532a9c 63
b5b969e9
JM
64 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
65 HOSTAPD_LEVEL_INFO, "associated");
66
c41a1095
JM
67 ieee802_11_parse_elems(ie, ielen, &elems, 0);
68 if (elems.wps_ie) {
69 ie = elems.wps_ie - 2;
70 ielen = elems.wps_ie_len + 2;
71 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
72 } else if (elems.rsn_ie) {
73 ie = elems.rsn_ie - 2;
74 ielen = elems.rsn_ie_len + 2;
75 wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
76 } else if (elems.wpa_ie) {
77 ie = elems.wpa_ie - 2;
78 ielen = elems.wpa_ie_len + 2;
79 wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
80 } else {
81 ie = NULL;
82 ielen = 0;
83 wpa_printf(MSG_DEBUG, "STA did not include WPS/RSN/WPA IE in "
84 "(Re)AssocReq");
85 }
86
b5b969e9
JM
87 sta = ap_get_sta(hapd, addr);
88 if (sta) {
89 accounting_sta_stop(hapd, sta);
90 } else {
91 sta = ap_sta_add(hapd, addr);
92 if (sta == NULL)
93 return -1;
94 }
95 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
96
b305c684
JM
97#ifdef CONFIG_P2P
98 if (elems.p2p) {
99 wpabuf_free(sta->p2p_ie);
100 sta->p2p_ie = ieee802_11_vendor_ie_concat(all_ies, all_ies_len,
101 P2P_IE_VENDOR_TYPE);
102 }
103#endif /* CONFIG_P2P */
104
b5b969e9
JM
105 if (hapd->conf->wpa) {
106 if (ie == NULL || ielen == 0) {
107 if (hapd->conf->wps_state) {
108 wpa_printf(MSG_DEBUG, "STA did not include "
109 "WPA/RSN IE in (Re)Association "
110 "Request - possible WPS use");
111 sta->flags |= WLAN_STA_MAYBE_WPS;
112 goto skip_wpa_check;
113 }
114
115 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
116 return -1;
117 }
118 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
119 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
120 sta->flags |= WLAN_STA_WPS;
121 goto skip_wpa_check;
122 }
123
124 if (sta->wpa_sm == NULL)
125 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
126 sta->addr);
127 if (sta->wpa_sm == NULL) {
128 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
129 "machine");
130 return -1;
131 }
132 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
133 ie, ielen, NULL, 0);
134 if (res != WPA_IE_OK) {
355d36a7 135 int resp;
b5b969e9
JM
136 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
137 "rejected? (res %u)", res);
138 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
355d36a7
AT
139 if (res == WPA_INVALID_GROUP)
140 resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
141 else if (res == WPA_INVALID_PAIRWISE)
142 resp = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
143 else if (res == WPA_INVALID_AKMP)
144 resp = WLAN_REASON_AKMP_NOT_VALID;
145#ifdef CONFIG_IEEE80211W
146 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
147 resp = WLAN_REASON_INVALID_IE;
148 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
149 resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
150#endif /* CONFIG_IEEE80211W */
151 else
152 resp = WLAN_REASON_INVALID_IE;
bdee6fce 153 hapd->drv.sta_disassoc(hapd, sta->addr, resp);
355d36a7 154 ap_free_sta(hapd, sta);
b5b969e9
JM
155 return -1;
156 }
a9aca28b 157 } else if (hapd->conf->wps_state) {
54f489be
JM
158#ifdef CONFIG_WPS_STRICT
159 struct wpabuf *wps;
160 wps = ieee802_11_vendor_ie_concat(ie, ielen,
161 WPS_IE_VENDOR_TYPE);
162 if (wps && wps_validate_assoc_req(wps) < 0) {
163 hapd->drv.sta_disassoc(hapd, sta->addr,
164 WLAN_REASON_INVALID_IE);
165 ap_free_sta(hapd, sta);
166 wpabuf_free(wps);
167 return -1;
168 }
169 wpabuf_free(wps);
170#endif /* CONFIG_WPS_STRICT */
a9aca28b
JM
171 if (ie && ielen > 4 && ie[0] == 0xdd && ie[1] >= 4 &&
172 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
173 sta->flags |= WLAN_STA_WPS;
174 } else
175 sta->flags |= WLAN_STA_MAYBE_WPS;
b5b969e9
JM
176 }
177skip_wpa_check:
178
179 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
180 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
181 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
182
183 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
184
185 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
186
ef796391 187#ifdef CONFIG_P2P
3f4ce13f
JM
188 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
189 all_ies, all_ies_len);
ef796391
JM
190#endif /* CONFIG_P2P */
191
b5b969e9
JM
192 return 0;
193}
194
195
196void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
197{
198 struct sta_info *sta;
199
83e843e8
JM
200 if (addr == NULL) {
201 /*
202 * This could potentially happen with unexpected event from the
203 * driver wrapper. This was seen at least in one case where the
204 * driver ended up reporting a station mode event while hostapd
205 * was running, so better make sure we stop processing such an
206 * event here.
207 */
208 wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event "
209 "with no address");
1f4c7b6b 210 return;
83e843e8
JM
211 }
212
b5b969e9
JM
213 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
214 HOSTAPD_LEVEL_INFO, "disassociated");
215
216 sta = ap_get_sta(hapd, addr);
217 if (sta == NULL) {
218 wpa_printf(MSG_DEBUG, "Disassociation notification for "
219 "unknown STA " MACSTR, MAC2STR(addr));
220 return;
221 }
222
223 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
9a3cb18d
JM
224 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
225 MAC2STR(sta->addr));
b5b969e9
JM
226 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
227 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
228 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
229 ap_free_sta(hapd, sta);
230}
231
232
f8b1f695
JM
233#ifdef HOSTAPD
234
fe6bdb77 235#ifdef NEED_AP_MLME
f8b1f695
JM
236
237static const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len)
238{
239 u16 fc, type, stype;
240
241 /*
242 * PS-Poll frames are 16 bytes. All other frames are
243 * 24 bytes or longer.
244 */
245 if (len < 16)
246 return NULL;
247
248 fc = le_to_host16(hdr->frame_control);
249 type = WLAN_FC_GET_TYPE(fc);
250 stype = WLAN_FC_GET_STYPE(fc);
251
252 switch (type) {
253 case WLAN_FC_TYPE_DATA:
254 if (len < 24)
255 return NULL;
256 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
fbbfcbac 257 case WLAN_FC_FROMDS | WLAN_FC_TODS:
f8b1f695
JM
258 case WLAN_FC_TODS:
259 return hdr->addr1;
260 case WLAN_FC_FROMDS:
261 return hdr->addr2;
262 default:
263 return NULL;
264 }
265 case WLAN_FC_TYPE_CTRL:
266 if (stype != WLAN_FC_STYPE_PSPOLL)
267 return NULL;
268 return hdr->addr1;
269 case WLAN_FC_TYPE_MGMT:
270 return hdr->addr3;
271 default:
272 return NULL;
273 }
274}
275
276
277#define HAPD_BROADCAST ((struct hostapd_data *) -1)
278
279static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
280 const u8 *bssid)
281{
282 size_t i;
283
284 if (bssid == NULL)
285 return NULL;
286 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
287 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
288 return HAPD_BROADCAST;
289
290 for (i = 0; i < iface->num_bss; i++) {
291 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
292 return iface->bss[i];
293 }
294
295 return NULL;
296}
297
298
299static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
0d9fc3d8 300 const u8 *frame, size_t len)
f8b1f695 301{
0d9fc3d8 302 const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) frame;
fbbfcbac 303 u16 fc = le_to_host16(hdr->frame_control);
f8b1f695
JM
304 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
305 if (hapd == NULL || hapd == HAPD_BROADCAST)
306 return;
307
fbbfcbac
FF
308 ieee802_11_rx_from_unknown(hapd, hdr->addr2,
309 (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
310 (WLAN_FC_TODS | WLAN_FC_FROMDS));
f8b1f695
JM
311}
312
313
2a8b7416 314static void hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
b5b969e9 315{
4b9841d3 316 struct hostapd_iface *iface = hapd->iface;
b57e086c 317 const struct ieee80211_hdr *hdr;
4b9841d3 318 const u8 *bssid;
2a8b7416 319 struct hostapd_frame_info fi;
4b9841d3 320
2a8b7416
JM
321 hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
322 bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
4b9841d3
JM
323 if (bssid == NULL)
324 return;
325
326 hapd = get_hapd_bssid(iface, bssid);
327 if (hapd == NULL) {
328 u16 fc;
329 fc = le_to_host16(hdr->frame_control);
330
331 /*
332 * Drop frames to unknown BSSIDs except for Beacon frames which
333 * could be used to update neighbor information.
334 */
335 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
336 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
337 hapd = iface->bss[0];
338 else
339 return;
340 }
341
2a8b7416
JM
342 os_memset(&fi, 0, sizeof(fi));
343 fi.datarate = rx_mgmt->datarate;
344 fi.ssi_signal = rx_mgmt->ssi_signal;
345
4b9841d3
JM
346 if (hapd == HAPD_BROADCAST) {
347 size_t i;
348 for (i = 0; i < iface->num_bss; i++)
2a8b7416
JM
349 ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
350 rx_mgmt->frame_len, &fi);
4b9841d3 351 } else
2a8b7416 352 ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len, &fi);
bbb921da
JM
353
354 random_add_randomness(&fi, sizeof(fi));
b5b969e9
JM
355}
356
357
f8b1f695
JM
358static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
359 size_t len, u16 stype, int ok)
b5b969e9 360{
4b9841d3
JM
361 struct ieee80211_hdr *hdr;
362 hdr = (struct ieee80211_hdr *) buf;
363 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
364 if (hapd == NULL || hapd == HAPD_BROADCAST)
365 return;
b5b969e9
JM
366 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
367}
f82ef4d8 368
f8b1f695 369#endif /* NEED_AP_MLME */
ad1e68e6
JM
370
371
a0e0d3bb
JM
372static int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa,
373 const u8 *ie, size_t ie_len)
374{
375 size_t i;
376 int ret = 0;
377
bbb921da
JM
378 if (sa)
379 random_add_randomness(sa, ETH_ALEN);
a0e0d3bb
JM
380 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
381 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
382 sa, ie, ie_len) > 0) {
383 ret = 1;
384 break;
385 }
386 }
387 return ret;
388}
389
390
a8e0505b
JM
391static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
392{
393 struct sta_info *sta = ap_get_sta(hapd, addr);
394 if (sta)
395 return 0;
396
397 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
398 " - adding a new STA", MAC2STR(addr));
399 sta = ap_sta_add(hapd, addr);
400 if (sta) {
401 hostapd_new_assoc_sta(hapd, sta, 0);
402 } else {
403 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
404 MAC2STR(addr));
405 return -1;
406 }
407
408 return 0;
409}
410
411
412static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
413 const u8 *data, size_t data_len)
414{
415 struct hostapd_iface *iface = hapd->iface;
416 size_t j;
417
418 for (j = 0; j < iface->num_bss; j++) {
419 if (ap_get_sta(iface->bss[j], src)) {
420 hapd = iface->bss[j];
421 break;
422 }
423 }
424
425 ieee802_1x_receive(hapd, src, data, data_len);
426}
427
428
9646a8ab 429void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
ad1e68e6
JM
430 union wpa_event_data *data)
431{
432 struct hostapd_data *hapd = ctx;
433
434 switch (event) {
435 case EVENT_MICHAEL_MIC_FAILURE:
436 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
437 break;
438 case EVENT_SCAN_RESULTS:
439 if (hapd->iface->scan_cb)
440 hapd->iface->scan_cb(hapd->iface);
441 break;
08fd8c15 442#ifdef CONFIG_IEEE80211R
f2dab64e 443 case EVENT_FT_RRB_RX:
08fd8c15
JM
444 wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
445 data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
446 break;
447#endif /* CONFIG_IEEE80211R */
fcf0f87d
JM
448 case EVENT_WPS_BUTTON_PUSHED:
449 hostapd_wps_button_pushed(hapd);
450 break;
f8b1f695
JM
451#ifdef NEED_AP_MLME
452 case EVENT_TX_STATUS:
453 switch (data->tx_status.type) {
454 case WLAN_FC_TYPE_MGMT:
455 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
456 data->tx_status.data_len,
457 data->tx_status.stype,
458 data->tx_status.ack);
459 break;
460 case WLAN_FC_TYPE_DATA:
461 hostapd_tx_status(hapd, data->tx_status.dst,
462 data->tx_status.data,
463 data->tx_status.data_len,
464 data->tx_status.ack);
465 break;
466 }
467 break;
468 case EVENT_RX_FROM_UNKNOWN:
0d9fc3d8 469 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.frame,
f8b1f695
JM
470 data->rx_from_unknown.len);
471 break;
472 case EVENT_RX_MGMT:
2a8b7416 473 hostapd_mgmt_rx(hapd, &data->rx_mgmt);
f8b1f695
JM
474 break;
475#endif /* NEED_AP_MLME */
a0e0d3bb
JM
476 case EVENT_RX_PROBE_REQ:
477 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
478 data->rx_probe_req.ie,
479 data->rx_probe_req.ie_len);
480 break;
a70a5d6d 481 case EVENT_NEW_STA:
a8e0505b
JM
482 hostapd_event_new_sta(hapd, data->new_sta.addr);
483 break;
484 case EVENT_EAPOL_RX:
485 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
486 data->eapol_rx.data,
487 data->eapol_rx.data_len);
488 break;
1d041bec
JM
489 case EVENT_ASSOC:
490 hostapd_notif_assoc(hapd, data->assoc_info.addr,
491 data->assoc_info.req_ies,
492 data->assoc_info.req_ies_len);
493 break;
494 case EVENT_DISASSOC:
495 if (data)
496 hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
497 break;
498 case EVENT_DEAUTH:
499 if (data)
500 hostapd_notif_disassoc(hapd, data->deauth_info.addr);
501 break;
ad1e68e6
JM
502 default:
503 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
504 break;
505 }
506}
f8b1f695
JM
507
508#endif /* HOSTAPD */