Use driver event, EVENT_EAPOL_RX, for EAPOL frame indication
[libeap.git] / 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 "hostapd.h"
22 #include "ieee802_11.h"
23 #include "sta_info.h"
24 #include "accounting.h"
25 #include "tkip_countermeasures.h"
26 #include "iapp.h"
27 #include "ieee802_1x.h"
28 #include "wpa_auth.h"
29 #include "wmm.h"
30 #include "wps_hostapd.h"
31 #include "ap_config.h"
32
33
34 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
35                         const u8 *ie, size_t ielen)
36 {
37         struct sta_info *sta;
38         int new_assoc, res;
39
40         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
41                        HOSTAPD_LEVEL_INFO, "associated");
42
43         sta = ap_get_sta(hapd, addr);
44         if (sta) {
45                 accounting_sta_stop(hapd, sta);
46         } else {
47                 sta = ap_sta_add(hapd, addr);
48                 if (sta == NULL)
49                         return -1;
50         }
51         sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
52
53         if (hapd->conf->wpa) {
54                 if (ie == NULL || ielen == 0) {
55                         if (hapd->conf->wps_state) {
56                                 wpa_printf(MSG_DEBUG, "STA did not include "
57                                            "WPA/RSN IE in (Re)Association "
58                                            "Request - possible WPS use");
59                                 sta->flags |= WLAN_STA_MAYBE_WPS;
60                                 goto skip_wpa_check;
61                         }
62
63                         wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
64                         return -1;
65                 }
66                 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
67                     os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
68                         sta->flags |= WLAN_STA_WPS;
69                         goto skip_wpa_check;
70                 }
71
72                 if (sta->wpa_sm == NULL)
73                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
74                                                         sta->addr);
75                 if (sta->wpa_sm == NULL) {
76                         wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
77                                    "machine");
78                         return -1;
79                 }
80                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
81                                           ie, ielen, NULL, 0);
82                 if (res != WPA_IE_OK) {
83                         int resp;
84                         wpa_printf(MSG_DEBUG, "WPA/RSN information element "
85                                    "rejected? (res %u)", res);
86                         wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
87                         if (res == WPA_INVALID_GROUP)
88                                 resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
89                         else if (res == WPA_INVALID_PAIRWISE)
90                                 resp = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
91                         else if (res == WPA_INVALID_AKMP)
92                                 resp = WLAN_REASON_AKMP_NOT_VALID;
93 #ifdef CONFIG_IEEE80211W
94                         else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
95                                 resp = WLAN_REASON_INVALID_IE;
96                         else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
97                                 resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
98 #endif /* CONFIG_IEEE80211W */
99                         else
100                                 resp = WLAN_REASON_INVALID_IE;
101                         hapd->drv.sta_disassoc(hapd, sta->addr, resp);
102                         ap_free_sta(hapd, sta);
103                         return -1;
104                 }
105         } else if (hapd->conf->wps_state) {
106                 if (ie && ielen > 4 && ie[0] == 0xdd && ie[1] >= 4 &&
107                     os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
108                         sta->flags |= WLAN_STA_WPS;
109                 } else
110                         sta->flags |= WLAN_STA_MAYBE_WPS;
111         }
112 skip_wpa_check:
113
114         new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
115         sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
116         wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
117
118         hostapd_new_assoc_sta(hapd, sta, !new_assoc);
119
120         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
121
122         return 0;
123 }
124
125
126 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
127 {
128         struct sta_info *sta;
129
130         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
131                        HOSTAPD_LEVEL_INFO, "disassociated");
132
133         sta = ap_get_sta(hapd, addr);
134         if (sta == NULL) {
135                 wpa_printf(MSG_DEBUG, "Disassociation notification for "
136                            "unknown STA " MACSTR, MAC2STR(addr));
137                 return;
138         }
139
140         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
141         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
142         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
143         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
144         ap_free_sta(hapd, sta);
145 }
146
147
148 #ifdef HOSTAPD
149
150 #ifdef NEED_AP_MLME
151
152 static const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len)
153 {
154         u16 fc, type, stype;
155
156         /*
157          * PS-Poll frames are 16 bytes. All other frames are
158          * 24 bytes or longer.
159          */
160         if (len < 16)
161                 return NULL;
162
163         fc = le_to_host16(hdr->frame_control);
164         type = WLAN_FC_GET_TYPE(fc);
165         stype = WLAN_FC_GET_STYPE(fc);
166
167         switch (type) {
168         case WLAN_FC_TYPE_DATA:
169                 if (len < 24)
170                         return NULL;
171                 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
172                 case WLAN_FC_FROMDS | WLAN_FC_TODS:
173                 case WLAN_FC_TODS:
174                         return hdr->addr1;
175                 case WLAN_FC_FROMDS:
176                         return hdr->addr2;
177                 default:
178                         return NULL;
179                 }
180         case WLAN_FC_TYPE_CTRL:
181                 if (stype != WLAN_FC_STYPE_PSPOLL)
182                         return NULL;
183                 return hdr->addr1;
184         case WLAN_FC_TYPE_MGMT:
185                 return hdr->addr3;
186         default:
187                 return NULL;
188         }
189 }
190
191
192 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
193
194 static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
195                                             const u8 *bssid)
196 {
197         size_t i;
198
199         if (bssid == NULL)
200                 return NULL;
201         if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
202             bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
203                 return HAPD_BROADCAST;
204
205         for (i = 0; i < iface->num_bss; i++) {
206                 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
207                         return iface->bss[i];
208         }
209
210         return NULL;
211 }
212
213
214 static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
215                                         const u8 *frame, size_t len)
216 {
217         const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) frame;
218         u16 fc = le_to_host16(hdr->frame_control);
219         hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
220         if (hapd == NULL || hapd == HAPD_BROADCAST)
221                 return;
222
223         ieee802_11_rx_from_unknown(hapd, hdr->addr2,
224                                    (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
225                                    (WLAN_FC_TODS | WLAN_FC_FROMDS));
226 }
227
228
229 static void hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
230 {
231         struct hostapd_iface *iface = hapd->iface;
232         const struct ieee80211_hdr *hdr;
233         const u8 *bssid;
234         struct hostapd_frame_info fi;
235
236         hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
237         bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
238         if (bssid == NULL)
239                 return;
240
241         hapd = get_hapd_bssid(iface, bssid);
242         if (hapd == NULL) {
243                 u16 fc;
244                 fc = le_to_host16(hdr->frame_control);
245
246                 /*
247                  * Drop frames to unknown BSSIDs except for Beacon frames which
248                  * could be used to update neighbor information.
249                  */
250                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
251                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
252                         hapd = iface->bss[0];
253                 else
254                         return;
255         }
256
257         os_memset(&fi, 0, sizeof(fi));
258         fi.datarate = rx_mgmt->datarate;
259         fi.ssi_signal = rx_mgmt->ssi_signal;
260
261         if (hapd == HAPD_BROADCAST) {
262                 size_t i;
263                 for (i = 0; i < iface->num_bss; i++)
264                         ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
265                                         rx_mgmt->frame_len, &fi);
266         } else
267                 ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len, &fi);
268 }
269
270
271 static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
272                                size_t len, u16 stype, int ok)
273 {
274         struct ieee80211_hdr *hdr;
275         hdr = (struct ieee80211_hdr *) buf;
276         hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
277         if (hapd == NULL || hapd == HAPD_BROADCAST)
278                 return;
279         ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
280 }
281
282 #endif /* NEED_AP_MLME */
283
284
285 static int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa,
286                                 const u8 *ie, size_t ie_len)
287 {
288         size_t i;
289         int ret = 0;
290
291         for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
292                 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
293                                             sa, ie, ie_len) > 0) {
294                         ret = 1;
295                         break;
296                 }
297         }
298         return ret;
299 }
300
301
302 static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
303 {
304         struct sta_info *sta = ap_get_sta(hapd, addr);
305         if (sta)
306                 return 0;
307
308         wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
309                    " - adding a new STA", MAC2STR(addr));
310         sta = ap_sta_add(hapd, addr);
311         if (sta) {
312                 hostapd_new_assoc_sta(hapd, sta, 0);
313         } else {
314                 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
315                            MAC2STR(addr));
316                 return -1;
317         }
318
319         return 0;
320 }
321
322
323 static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
324                                    const u8 *data, size_t data_len)
325 {
326         struct hostapd_iface *iface = hapd->iface;
327         size_t j;
328
329         for (j = 0; j < iface->num_bss; j++) {
330                 if (ap_get_sta(iface->bss[j], src)) {
331                         hapd = iface->bss[j];
332                         break;
333                 }
334         }
335
336         ieee802_1x_receive(hapd, src, data, data_len);
337 }
338
339
340 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
341                           union wpa_event_data *data)
342 {
343         struct hostapd_data *hapd = ctx;
344
345         switch (event) {
346         case EVENT_MICHAEL_MIC_FAILURE:
347                 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
348                 break;
349         case EVENT_SCAN_RESULTS:
350                 if (hapd->iface->scan_cb)
351                         hapd->iface->scan_cb(hapd->iface);
352                 break;
353 #ifdef CONFIG_IEEE80211R
354         case EVENT_FT_RRB_RX:
355                 wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
356                               data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
357                 break;
358 #endif /* CONFIG_IEEE80211R */
359         case EVENT_WPS_BUTTON_PUSHED:
360                 hostapd_wps_button_pushed(hapd);
361                 break;
362 #ifdef NEED_AP_MLME
363         case EVENT_TX_STATUS:
364                 switch (data->tx_status.type) {
365                 case WLAN_FC_TYPE_MGMT:
366                         hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
367                                            data->tx_status.data_len,
368                                            data->tx_status.stype,
369                                            data->tx_status.ack);
370                         break;
371                 case WLAN_FC_TYPE_DATA:
372                         hostapd_tx_status(hapd, data->tx_status.dst,
373                                           data->tx_status.data,
374                                           data->tx_status.data_len,
375                                           data->tx_status.ack);
376                         break;
377                 }
378                 break;
379         case EVENT_RX_FROM_UNKNOWN:
380                 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.frame,
381                                             data->rx_from_unknown.len);
382                 break;
383         case EVENT_RX_MGMT:
384                 hostapd_mgmt_rx(hapd, &data->rx_mgmt);
385                 break;
386 #endif /* NEED_AP_MLME */
387         case EVENT_RX_PROBE_REQ:
388                 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
389                                      data->rx_probe_req.ie,
390                                      data->rx_probe_req.ie_len);
391                 break;
392         case EVENT_NEW_STA:
393                 hostapd_event_new_sta(hapd, data->new_sta.addr);
394                 break;
395         case EVENT_EAPOL_RX:
396                 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
397                                        data->eapol_rx.data,
398                                        data->eapol_rx.data_len);
399                 break;
400         default:
401                 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
402                 break;
403         }
404 }
405
406 #endif /* HOSTAPD */