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