WPS ER: Add preliminary PBC support
[libeap.git] / hostapd / 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 "includes.h"
16
17 #include "hostapd.h"
18 #include "driver_i.h"
19 #include "ieee802_11.h"
20 #include "radius/radius.h"
21 #include "sta_info.h"
22 #include "accounting.h"
23 #include "tkip_countermeasures.h"
24 #include "ieee802_1x.h"
25 #include "wpa.h"
26 #include "iapp.h"
27 #include "wme.h"
28 #include "wps_hostapd.h"
29
30
31 struct prune_data {
32         struct hostapd_data *hapd;
33         const u8 *addr;
34 };
35
36 static int prune_associations(struct hostapd_iface *iface, void *ctx)
37 {
38         struct prune_data *data = ctx;
39         struct sta_info *osta;
40         struct hostapd_data *ohapd;
41         size_t j;
42
43         for (j = 0; j < iface->num_bss; j++) {
44                 ohapd = iface->bss[j];
45                 if (ohapd == data->hapd)
46                         continue;
47                 osta = ap_get_sta(ohapd, data->addr);
48                 if (!osta)
49                         continue;
50
51                 ap_sta_disassociate(ohapd, osta, WLAN_REASON_UNSPECIFIED);
52         }
53
54         return 0;
55 }
56
57 /**
58  * hostapd_prune_associations - Remove extraneous associations
59  * @hapd: Pointer to BSS data for the most recent association
60  * @sta: Pointer to the associated STA data
61  *
62  * This function looks through all radios and BSS's for previous
63  * (stale) associations of STA. If any are found they are removed.
64  */
65 static void hostapd_prune_associations(struct hostapd_data *hapd,
66                                        struct sta_info *sta)
67 {
68         struct prune_data data;
69         data.hapd = hapd;
70         data.addr = sta->addr;
71         hostapd_for_each_interface(prune_associations, &data);
72 }
73
74
75 /**
76  * hostapd_new_assoc_sta - Notify that a new station associated with the AP
77  * @hapd: Pointer to BSS data
78  * @sta: Pointer to the associated STA data
79  * @reassoc: 1 to indicate this was a re-association; 0 = first association
80  *
81  * This function will be called whenever a station associates with the AP. It
82  * can be called from ieee802_11.c for drivers that export MLME to hostapd and
83  * from driver_*.c for drivers that take care of management frames (IEEE 802.11
84  * authentication and association) internally.
85  */
86 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
87                            int reassoc)
88 {
89         if (hapd->tkip_countermeasures) {
90                 hostapd_sta_deauth(hapd, sta->addr,
91                                    WLAN_REASON_MICHAEL_MIC_FAILURE);
92                 return;
93         }
94
95         hostapd_prune_associations(hapd, sta);
96
97         /* IEEE 802.11F (IAPP) */
98         if (hapd->conf->ieee802_11f)
99                 iapp_new_station(hapd->iapp, sta);
100
101         /* Start accounting here, if IEEE 802.1X and WPA are not used.
102          * IEEE 802.1X/WPA code will start accounting after the station has
103          * been authorized. */
104         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
105                 accounting_sta_start(hapd, sta);
106
107         hostapd_wmm_sta_config(hapd, sta);
108
109         /* Start IEEE 802.1X authentication process for new stations */
110         ieee802_1x_new_station(hapd, sta);
111         if (reassoc) {
112                 if (sta->auth_alg != WLAN_AUTH_FT &&
113                     !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
114                         wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
115         } else
116                 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
117 }
118
119
120 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
121                        const u8 *buf, size_t len, int ack)
122 {
123         struct sta_info *sta;
124         struct hostapd_iface *iface = hapd->iface;
125
126         sta = ap_get_sta(hapd, addr);
127         if (sta == NULL && iface->num_bss > 1) {
128                 size_t j;
129                 for (j = 0; j < iface->num_bss; j++) {
130                         hapd = iface->bss[j];
131                         sta = ap_get_sta(hapd, addr);
132                         if (sta)
133                                 break;
134                 }
135         }
136         if (sta == NULL)
137                 return;
138         if (sta->flags & WLAN_STA_PENDING_POLL) {
139                 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
140                            "activity poll", MAC2STR(sta->addr),
141                            ack ? "ACKed" : "did not ACK");
142                 if (ack)
143                         sta->flags &= ~WLAN_STA_PENDING_POLL;
144         }
145
146         ieee802_1x_tx_status(hapd, sta, buf, len, ack);
147 }
148
149
150 static const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len)
151 {
152         u16 fc, type, stype;
153
154         /*
155          * PS-Poll frames are 16 bytes. All other frames are
156          * 24 bytes or longer.
157          */
158         if (len < 16)
159                 return NULL;
160
161         fc = le_to_host16(hdr->frame_control);
162         type = WLAN_FC_GET_TYPE(fc);
163         stype = WLAN_FC_GET_STYPE(fc);
164
165         switch (type) {
166         case WLAN_FC_TYPE_DATA:
167                 if (len < 24)
168                         return NULL;
169                 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
170                 case WLAN_FC_TODS:
171                         return hdr->addr1;
172                 case WLAN_FC_FROMDS:
173                         return hdr->addr2;
174                 default:
175                         return NULL;
176                 }
177         case WLAN_FC_TYPE_CTRL:
178                 if (stype != WLAN_FC_STYPE_PSPOLL)
179                         return NULL;
180                 return hdr->addr1;
181         case WLAN_FC_TYPE_MGMT:
182                 return hdr->addr3;
183         default:
184                 return NULL;
185         }
186 }
187
188
189 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
190
191 static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
192                                             const u8 *bssid)
193 {
194         size_t i;
195
196         if (bssid == NULL)
197                 return NULL;
198         if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
199             bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
200                 return HAPD_BROADCAST;
201
202         for (i = 0; i < iface->num_bss; i++) {
203                 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
204                         return iface->bss[i];
205         }
206
207         return NULL;
208 }
209
210
211 void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
212                                  const struct ieee80211_hdr *hdr, size_t len)
213 {
214         struct sta_info *sta;
215         const u8 *addr;
216
217         hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
218         if (hapd == NULL || hapd == HAPD_BROADCAST)
219                 return;
220
221         addr = hdr->addr2;
222         sta = ap_get_sta(hapd, addr);
223         if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
224                 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated "
225                            "STA " MACSTR, MAC2STR(addr));
226                 if (sta && (sta->flags & WLAN_STA_AUTH))
227                         hostapd_sta_disassoc(
228                                 hapd, addr,
229                                 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
230                 else
231                         hostapd_sta_deauth(
232                                 hapd, addr,
233                                 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
234         }
235 }
236
237
238 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
239                         const u8 *ie, size_t ielen)
240 {
241         struct sta_info *sta;
242         int new_assoc, res;
243
244         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
245                        HOSTAPD_LEVEL_INFO, "associated");
246
247         sta = ap_get_sta(hapd, addr);
248         if (sta) {
249                 accounting_sta_stop(hapd, sta);
250         } else {
251                 sta = ap_sta_add(hapd, addr);
252                 if (sta == NULL)
253                         return -1;
254         }
255         sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
256
257         if (hapd->conf->wpa) {
258                 if (ie == NULL || ielen == 0) {
259                         if (hapd->conf->wps_state) {
260                                 wpa_printf(MSG_DEBUG, "STA did not include "
261                                            "WPA/RSN IE in (Re)Association "
262                                            "Request - possible WPS use");
263                                 sta->flags |= WLAN_STA_MAYBE_WPS;
264                                 goto skip_wpa_check;
265                         }
266
267                         wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
268                         return -1;
269                 }
270                 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
271                     os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
272                         sta->flags |= WLAN_STA_WPS;
273                         goto skip_wpa_check;
274                 }
275
276                 if (sta->wpa_sm == NULL)
277                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
278                                                         sta->addr);
279                 if (sta->wpa_sm == NULL) {
280                         wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
281                                    "machine");
282                         return -1;
283                 }
284                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
285                                           ie, ielen, NULL, 0);
286                 if (res != WPA_IE_OK) {
287                         int resp;
288                         wpa_printf(MSG_DEBUG, "WPA/RSN information element "
289                                    "rejected? (res %u)", res);
290                         wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
291                         if (res == WPA_INVALID_GROUP)
292                                 resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
293                         else if (res == WPA_INVALID_PAIRWISE)
294                                 resp = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
295                         else if (res == WPA_INVALID_AKMP)
296                                 resp = WLAN_REASON_AKMP_NOT_VALID;
297 #ifdef CONFIG_IEEE80211W
298                         else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
299                                 resp = WLAN_REASON_INVALID_IE;
300                         else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
301                                 resp = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
302 #endif /* CONFIG_IEEE80211W */
303                         else
304                                 resp = WLAN_REASON_INVALID_IE;
305                         hostapd_sta_disassoc(hapd, sta->addr, resp);
306                         ap_free_sta(hapd, sta);
307                         return -1;
308                 }
309         } else if (hapd->conf->wps_state) {
310                 if (ie && ielen > 4 && ie[0] == 0xdd && ie[1] >= 4 &&
311                     os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
312                         sta->flags |= WLAN_STA_WPS;
313                 } else
314                         sta->flags |= WLAN_STA_MAYBE_WPS;
315         }
316 skip_wpa_check:
317
318         new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
319         sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
320         wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
321
322         hostapd_new_assoc_sta(hapd, sta, !new_assoc);
323
324         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
325
326         return 0;
327 }
328
329
330 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
331 {
332         struct sta_info *sta;
333
334         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
335                        HOSTAPD_LEVEL_INFO, "disassociated");
336
337         sta = ap_get_sta(hapd, addr);
338         if (sta == NULL) {
339                 wpa_printf(MSG_DEBUG, "Disassociation notification for "
340                            "unknown STA " MACSTR, MAC2STR(addr));
341                 return;
342         }
343
344         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
345         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
346         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
347         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
348         ap_free_sta(hapd, sta);
349 }
350
351
352 void hostapd_eapol_receive(struct hostapd_data *hapd, const u8 *sa,
353                            const u8 *buf, size_t len)
354 {
355         ieee802_1x_receive(hapd, sa, buf, len);
356 }
357
358
359 #ifdef NEED_AP_MLME
360 void hostapd_mgmt_rx(struct hostapd_data *hapd, u8 *buf, size_t len,
361                      u16 stype, struct hostapd_frame_info *fi)
362 {
363         struct hostapd_iface *iface = hapd->iface;
364         struct ieee80211_hdr *hdr;
365         const u8 *bssid;
366
367         hdr = (struct ieee80211_hdr *) buf;
368         bssid = get_hdr_bssid(hdr, len);
369         if (bssid == NULL)
370                 return;
371
372         hapd = get_hapd_bssid(iface, bssid);
373         if (hapd == NULL) {
374                 u16 fc;
375                 fc = le_to_host16(hdr->frame_control);
376
377                 /*
378                  * Drop frames to unknown BSSIDs except for Beacon frames which
379                  * could be used to update neighbor information.
380                  */
381                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
382                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
383                         hapd = iface->bss[0];
384                 else
385                         return;
386         }
387
388         if (hapd == HAPD_BROADCAST) {
389                 size_t i;
390                 for (i = 0; i < iface->num_bss; i++)
391                         ieee802_11_mgmt(iface->bss[i], buf, len, stype, fi);
392         } else
393                 ieee802_11_mgmt(hapd, buf, len, stype, fi);
394 }
395
396
397 void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
398                         u16 stype, int ok)
399 {
400         struct ieee80211_hdr *hdr;
401         hdr = (struct ieee80211_hdr *) buf;
402         hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
403         if (hapd == NULL || hapd == HAPD_BROADCAST)
404                 return;
405         ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
406 }
407 #endif /* NEED_AP_MLME */
408
409
410 void hostapd_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr)
411 {
412         michael_mic_failure(hapd, addr, 1);
413 }
414
415
416 struct hostapd_data * hostapd_sta_get_bss(struct hostapd_data *hapd,
417                                           const u8 *addr)
418 {
419         struct hostapd_iface *iface = hapd->iface;
420         size_t j;
421
422         for (j = 0; j < iface->num_bss; j++) {
423                 hapd = iface->bss[j];
424                 if (ap_get_sta(hapd, addr))
425                         return hapd;
426         }
427
428         return NULL;
429 }
430
431
432 #ifndef CONFIG_AP
433 void wpa_supplicant_event(void *ctx, wpa_event_type event,
434                           union wpa_event_data *data)
435 {
436         struct hostapd_data *hapd = ctx;
437
438         switch (event) {
439         case EVENT_MICHAEL_MIC_FAILURE:
440                 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
441                 break;
442         case EVENT_SCAN_RESULTS:
443                 if (hapd->iface->scan_cb)
444                         hapd->iface->scan_cb(hapd->iface);
445                 break;
446         default:
447                 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
448                 break;
449         }
450 }
451 #endif /* CONFIG_AP */
452
453
454 void hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa,
455                          const u8 *ie, size_t ie_len)
456 {
457         size_t i;
458
459         for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
460                 hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
461                                         sa, ie, ie_len);
462 }