remove @EAP_LDFLAGS@, no longer exists
[mech_eap.orig] / libeap / src / ap / drv_callbacks.c
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 "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "radius/radius.h"
19 #include "drivers/driver.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/ieee802_11_common.h"
22 #include "common/wpa_ctrl.h"
23 #include "p2p/p2p.h"
24 #include "wps/wps.h"
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"
35 #include "ap_config.h"
36
37
38 int 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;
43         struct ieee802_11_elems elems;
44 #ifdef CONFIG_P2P
45         const u8 *all_ies = ie;
46         size_t all_ies_len = ielen;
47 #endif /* CONFIG_P2P */
48
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
62         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
63                        HOSTAPD_LEVEL_INFO, "associated");
64
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
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
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
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) {
133                         int resp;
134                         wpa_printf(MSG_DEBUG, "WPA/RSN information element "
135                                    "rejected? (res %u)", res);
136                         wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
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;
151                         hapd->drv.sta_disassoc(hapd, sta->addr, resp);
152                         ap_free_sta(hapd, sta);
153                         return -1;
154                 }
155         } else if (hapd->conf->wps_state) {
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 */
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;
174         }
175 skip_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
185 #ifdef CONFIG_P2P
186         if (elems.p2p) {
187                 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
188                                       all_ies, all_ies_len);
189         }
190 #endif /* CONFIG_P2P */
191
192         return 0;
193 }
194
195
196 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
197 {
198         struct sta_info *sta;
199
200         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
201                        HOSTAPD_LEVEL_INFO, "disassociated");
202
203         sta = ap_get_sta(hapd, addr);
204         if (sta == NULL) {
205                 wpa_printf(MSG_DEBUG, "Disassociation notification for "
206                            "unknown STA " MACSTR, MAC2STR(addr));
207                 return;
208         }
209
210         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
211         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
212                 MAC2STR(sta->addr));
213         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
214         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
215         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
216         ap_free_sta(hapd, sta);
217 }
218
219
220 #ifdef HOSTAPD
221
222 #ifdef NEED_AP_MLME
223
224 static const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len)
225 {
226         u16 fc, type, stype;
227
228         /*
229          * PS-Poll frames are 16 bytes. All other frames are
230          * 24 bytes or longer.
231          */
232         if (len < 16)
233                 return NULL;
234
235         fc = le_to_host16(hdr->frame_control);
236         type = WLAN_FC_GET_TYPE(fc);
237         stype = WLAN_FC_GET_STYPE(fc);
238
239         switch (type) {
240         case WLAN_FC_TYPE_DATA:
241                 if (len < 24)
242                         return NULL;
243                 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
244                 case WLAN_FC_FROMDS | WLAN_FC_TODS:
245                 case WLAN_FC_TODS:
246                         return hdr->addr1;
247                 case WLAN_FC_FROMDS:
248                         return hdr->addr2;
249                 default:
250                         return NULL;
251                 }
252         case WLAN_FC_TYPE_CTRL:
253                 if (stype != WLAN_FC_STYPE_PSPOLL)
254                         return NULL;
255                 return hdr->addr1;
256         case WLAN_FC_TYPE_MGMT:
257                 return hdr->addr3;
258         default:
259                 return NULL;
260         }
261 }
262
263
264 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
265
266 static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
267                                             const u8 *bssid)
268 {
269         size_t i;
270
271         if (bssid == NULL)
272                 return NULL;
273         if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
274             bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
275                 return HAPD_BROADCAST;
276
277         for (i = 0; i < iface->num_bss; i++) {
278                 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
279                         return iface->bss[i];
280         }
281
282         return NULL;
283 }
284
285
286 static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
287                                         const u8 *frame, size_t len)
288 {
289         const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) frame;
290         u16 fc = le_to_host16(hdr->frame_control);
291         hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
292         if (hapd == NULL || hapd == HAPD_BROADCAST)
293                 return;
294
295         ieee802_11_rx_from_unknown(hapd, hdr->addr2,
296                                    (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
297                                    (WLAN_FC_TODS | WLAN_FC_FROMDS));
298 }
299
300
301 static void hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
302 {
303         struct hostapd_iface *iface = hapd->iface;
304         const struct ieee80211_hdr *hdr;
305         const u8 *bssid;
306         struct hostapd_frame_info fi;
307
308         hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
309         bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
310         if (bssid == NULL)
311                 return;
312
313         hapd = get_hapd_bssid(iface, bssid);
314         if (hapd == NULL) {
315                 u16 fc;
316                 fc = le_to_host16(hdr->frame_control);
317
318                 /*
319                  * Drop frames to unknown BSSIDs except for Beacon frames which
320                  * could be used to update neighbor information.
321                  */
322                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
323                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
324                         hapd = iface->bss[0];
325                 else
326                         return;
327         }
328
329         os_memset(&fi, 0, sizeof(fi));
330         fi.datarate = rx_mgmt->datarate;
331         fi.ssi_signal = rx_mgmt->ssi_signal;
332
333         if (hapd == HAPD_BROADCAST) {
334                 size_t i;
335                 for (i = 0; i < iface->num_bss; i++)
336                         ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
337                                         rx_mgmt->frame_len, &fi);
338         } else
339                 ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len, &fi);
340 }
341
342
343 static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
344                                size_t len, u16 stype, int ok)
345 {
346         struct ieee80211_hdr *hdr;
347         hdr = (struct ieee80211_hdr *) buf;
348         hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
349         if (hapd == NULL || hapd == HAPD_BROADCAST)
350                 return;
351         ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
352 }
353
354 #endif /* NEED_AP_MLME */
355
356
357 static int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa,
358                                 const u8 *ie, size_t ie_len)
359 {
360         size_t i;
361         int ret = 0;
362
363         for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
364                 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
365                                             sa, ie, ie_len) > 0) {
366                         ret = 1;
367                         break;
368                 }
369         }
370         return ret;
371 }
372
373
374 static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
375 {
376         struct sta_info *sta = ap_get_sta(hapd, addr);
377         if (sta)
378                 return 0;
379
380         wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
381                    " - adding a new STA", MAC2STR(addr));
382         sta = ap_sta_add(hapd, addr);
383         if (sta) {
384                 hostapd_new_assoc_sta(hapd, sta, 0);
385         } else {
386                 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
387                            MAC2STR(addr));
388                 return -1;
389         }
390
391         return 0;
392 }
393
394
395 static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
396                                    const u8 *data, size_t data_len)
397 {
398         struct hostapd_iface *iface = hapd->iface;
399         size_t j;
400
401         for (j = 0; j < iface->num_bss; j++) {
402                 if (ap_get_sta(iface->bss[j], src)) {
403                         hapd = iface->bss[j];
404                         break;
405                 }
406         }
407
408         ieee802_1x_receive(hapd, src, data, data_len);
409 }
410
411
412 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
413                           union wpa_event_data *data)
414 {
415         struct hostapd_data *hapd = ctx;
416
417         switch (event) {
418         case EVENT_MICHAEL_MIC_FAILURE:
419                 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
420                 break;
421         case EVENT_SCAN_RESULTS:
422                 if (hapd->iface->scan_cb)
423                         hapd->iface->scan_cb(hapd->iface);
424                 break;
425 #ifdef CONFIG_IEEE80211R
426         case EVENT_FT_RRB_RX:
427                 wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
428                               data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
429                 break;
430 #endif /* CONFIG_IEEE80211R */
431         case EVENT_WPS_BUTTON_PUSHED:
432                 hostapd_wps_button_pushed(hapd);
433                 break;
434 #ifdef NEED_AP_MLME
435         case EVENT_TX_STATUS:
436                 switch (data->tx_status.type) {
437                 case WLAN_FC_TYPE_MGMT:
438                         hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
439                                            data->tx_status.data_len,
440                                            data->tx_status.stype,
441                                            data->tx_status.ack);
442                         break;
443                 case WLAN_FC_TYPE_DATA:
444                         hostapd_tx_status(hapd, data->tx_status.dst,
445                                           data->tx_status.data,
446                                           data->tx_status.data_len,
447                                           data->tx_status.ack);
448                         break;
449                 }
450                 break;
451         case EVENT_RX_FROM_UNKNOWN:
452                 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.frame,
453                                             data->rx_from_unknown.len);
454                 break;
455         case EVENT_RX_MGMT:
456                 hostapd_mgmt_rx(hapd, &data->rx_mgmt);
457                 break;
458 #endif /* NEED_AP_MLME */
459         case EVENT_RX_PROBE_REQ:
460                 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
461                                      data->rx_probe_req.ie,
462                                      data->rx_probe_req.ie_len);
463                 break;
464         case EVENT_NEW_STA:
465                 hostapd_event_new_sta(hapd, data->new_sta.addr);
466                 break;
467         case EVENT_EAPOL_RX:
468                 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
469                                        data->eapol_rx.data,
470                                        data->eapol_rx.data_len);
471                 break;
472         case EVENT_ASSOC:
473                 hostapd_notif_assoc(hapd, data->assoc_info.addr,
474                                     data->assoc_info.req_ies,
475                                     data->assoc_info.req_ies_len);
476                 break;
477         case EVENT_DISASSOC:
478                 if (data)
479                         hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
480                 break;
481         case EVENT_DEAUTH:
482                 if (data)
483                         hostapd_notif_disassoc(hapd, data->deauth_info.addr);
484                 break;
485         default:
486                 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
487                 break;
488         }
489 }
490
491 #endif /* HOSTAPD */