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