Merge hostapd/sta_flags.h into sta_info.h
[libeap.git] / hostapd / ieee802_11.c
1 /*
2  * hostapd / IEEE 802.11 Management
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 #ifndef CONFIG_NATIVE_WINDOWS
18
19 #include <net/if.h>
20
21 #include "common.h"
22 #include "eloop.h"
23 #include "crypto/crypto.h"
24 #include "common/wpa_ctrl.h"
25 #include "radius/radius.h"
26 #include "radius/radius_client.h"
27 #include "hostapd.h"
28 #include "ieee802_11.h"
29 #include "beacon.h"
30 #include "hw_features.h"
31 #include "ieee802_11_auth.h"
32 #include "sta_info.h"
33 #include "ieee802_1x.h"
34 #include "wpa.h"
35 #include "wme.h"
36 #include "ap_list.h"
37 #include "accounting.h"
38 #include "driver_i.h"
39 #include "mlme.h"
40
41
42 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
43 {
44         u8 *pos = eid;
45         int i, num, count;
46
47         if (hapd->iface->current_rates == NULL)
48                 return eid;
49
50         *pos++ = WLAN_EID_SUPP_RATES;
51         num = hapd->iface->num_rates;
52         if (num > 8) {
53                 /* rest of the rates are encoded in Extended supported
54                  * rates element */
55                 num = 8;
56         }
57
58         *pos++ = num;
59         count = 0;
60         for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
61              i++) {
62                 count++;
63                 *pos = hapd->iface->current_rates[i].rate / 5;
64                 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
65                         *pos |= 0x80;
66                 pos++;
67         }
68
69         return pos;
70 }
71
72
73 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
74 {
75         u8 *pos = eid;
76         int i, num, count;
77
78         if (hapd->iface->current_rates == NULL)
79                 return eid;
80
81         num = hapd->iface->num_rates;
82         if (num <= 8)
83                 return eid;
84         num -= 8;
85
86         *pos++ = WLAN_EID_EXT_SUPP_RATES;
87         *pos++ = num;
88         count = 0;
89         for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
90              i++) {
91                 count++;
92                 if (count <= 8)
93                         continue; /* already in SuppRates IE */
94                 *pos = hapd->iface->current_rates[i].rate / 5;
95                 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
96                         *pos |= 0x80;
97                 pos++;
98         }
99
100         return pos;
101 }
102
103
104 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
105                            int probe)
106 {
107         int capab = WLAN_CAPABILITY_ESS;
108         int privacy;
109
110         if (hapd->iface->num_sta_no_short_preamble == 0 &&
111             hapd->iconf->preamble == SHORT_PREAMBLE)
112                 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
113
114         privacy = hapd->conf->ssid.wep.keys_set;
115
116         if (hapd->conf->ieee802_1x &&
117             (hapd->conf->default_wep_key_len ||
118              hapd->conf->individual_wep_key_len))
119                 privacy = 1;
120
121         if (hapd->conf->wpa)
122                 privacy = 1;
123
124         if (sta) {
125                 int policy, def_klen;
126                 if (probe && sta->ssid_probe) {
127                         policy = sta->ssid_probe->security_policy;
128                         def_klen = sta->ssid_probe->wep.default_len;
129                 } else {
130                         policy = sta->ssid->security_policy;
131                         def_klen = sta->ssid->wep.default_len;
132                 }
133                 privacy = policy != SECURITY_PLAINTEXT;
134                 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
135                         privacy = 0;
136         }
137
138         if (privacy)
139                 capab |= WLAN_CAPABILITY_PRIVACY;
140
141         if (hapd->iface->current_mode &&
142             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
143             hapd->iface->num_sta_no_short_slot_time == 0)
144                 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
145
146         return capab;
147 }
148
149
150 #ifdef CONFIG_IEEE80211W
151 static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
152                                             struct sta_info *sta, u8 *eid)
153 {
154         u8 *pos = eid;
155         u32 timeout, tu;
156         struct os_time now, passed;
157
158         *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
159         *pos++ = 5;
160         *pos++ = WLAN_TIMEOUT_ASSOC_COMEBACK;
161         os_get_time(&now);
162         os_time_sub(&now, &sta->sa_query_start, &passed);
163         tu = (passed.sec * 1000000 + passed.usec) / 1024;
164         if (hapd->conf->assoc_sa_query_max_timeout > tu)
165                 timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
166         else
167                 timeout = 0;
168         if (timeout < hapd->conf->assoc_sa_query_max_timeout)
169                 timeout++; /* add some extra time for local timers */
170         WPA_PUT_LE32(pos, timeout);
171         pos += 4;
172
173         return pos;
174 }
175 #endif /* CONFIG_IEEE80211W */
176
177
178 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
179 {
180         int i;
181         if (len > HOSTAPD_MAX_SSID_LEN)
182                 len = HOSTAPD_MAX_SSID_LEN;
183         for (i = 0; i < len; i++) {
184                 if (ssid[i] >= 32 && ssid[i] < 127)
185                         buf[i] = ssid[i];
186                 else
187                         buf[i] = '.';
188         }
189         buf[len] = '\0';
190 }
191
192
193 /**
194  * ieee802_11_send_deauth - Send Deauthentication frame
195  * @hapd: hostapd BSS data
196  * @addr: Address of the destination STA
197  * @reason: Reason code for Deauthentication
198  */
199 void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
200 {
201         struct ieee80211_mgmt mgmt;
202
203         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
204                        HOSTAPD_LEVEL_DEBUG,
205                        "deauthenticate - reason %d", reason);
206         os_memset(&mgmt, 0, sizeof(mgmt));
207         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
208                                           WLAN_FC_STYPE_DEAUTH);
209         os_memcpy(mgmt.da, addr, ETH_ALEN);
210         os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
211         os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
212         mgmt.u.deauth.reason_code = host_to_le16(reason);
213         if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
214                                     sizeof(mgmt.u.deauth)) < 0)
215                 perror("ieee802_11_send_deauth: send");
216 }
217
218
219 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
220                            u16 auth_transaction, u8 *challenge, int iswep)
221 {
222         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
223                        HOSTAPD_LEVEL_DEBUG,
224                        "authentication (shared key, transaction %d)",
225                        auth_transaction);
226
227         if (auth_transaction == 1) {
228                 if (!sta->challenge) {
229                         /* Generate a pseudo-random challenge */
230                         u8 key[8];
231                         time_t now;
232                         int r;
233                         sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
234                         if (sta->challenge == NULL)
235                                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
236
237                         now = time(NULL);
238                         r = random();
239                         os_memcpy(key, &now, 4);
240                         os_memcpy(key + 4, &r, 4);
241                         rc4_skip(key, sizeof(key), 0,
242                                  sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
243                 }
244                 return 0;
245         }
246
247         if (auth_transaction != 3)
248                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
249
250         /* Transaction 3 */
251         if (!iswep || !sta->challenge || !challenge ||
252             os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
253                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
254                                HOSTAPD_LEVEL_INFO,
255                                "shared key authentication - invalid "
256                                "challenge-response");
257                 return WLAN_STATUS_CHALLENGE_FAIL;
258         }
259
260         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
261                        HOSTAPD_LEVEL_DEBUG,
262                        "authentication OK (shared key)");
263 #ifdef IEEE80211_REQUIRE_AUTH_ACK
264         /* Station will be marked authenticated if it ACKs the
265          * authentication reply. */
266 #else
267         sta->flags |= WLAN_STA_AUTH;
268         wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
269 #endif
270         os_free(sta->challenge);
271         sta->challenge = NULL;
272
273         return 0;
274 }
275
276
277 static void send_auth_reply(struct hostapd_data *hapd,
278                             const u8 *dst, const u8 *bssid,
279                             u16 auth_alg, u16 auth_transaction, u16 resp,
280                             const u8 *ies, size_t ies_len)
281 {
282         struct ieee80211_mgmt *reply;
283         u8 *buf;
284         size_t rlen;
285
286         rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
287         buf = os_zalloc(rlen);
288         if (buf == NULL)
289                 return;
290
291         reply = (struct ieee80211_mgmt *) buf;
292         reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
293                                             WLAN_FC_STYPE_AUTH);
294         os_memcpy(reply->da, dst, ETH_ALEN);
295         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
296         os_memcpy(reply->bssid, bssid, ETH_ALEN);
297
298         reply->u.auth.auth_alg = host_to_le16(auth_alg);
299         reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
300         reply->u.auth.status_code = host_to_le16(resp);
301
302         if (ies && ies_len)
303                 os_memcpy(reply->u.auth.variable, ies, ies_len);
304
305         wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
306                    " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
307                    MAC2STR(dst), auth_alg, auth_transaction,
308                    resp, (unsigned long) ies_len);
309         if (hostapd_send_mgmt_frame(hapd, reply, rlen) < 0)
310                 perror("send_auth_reply: send");
311
312         os_free(buf);
313 }
314
315
316 #ifdef CONFIG_IEEE80211R
317 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
318                                   u16 auth_transaction, u16 status,
319                                   const u8 *ies, size_t ies_len)
320 {
321         struct hostapd_data *hapd = ctx;
322         struct sta_info *sta;
323
324         send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
325                         status, ies, ies_len);
326
327         if (status != WLAN_STATUS_SUCCESS)
328                 return;
329
330         sta = ap_get_sta(hapd, dst);
331         if (sta == NULL)
332                 return;
333
334         hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
335                        HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
336         sta->flags |= WLAN_STA_AUTH;
337         mlme_authenticate_indication(hapd, sta);
338 }
339 #endif /* CONFIG_IEEE80211R */
340
341
342 static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
343                         size_t len)
344 {
345         u16 auth_alg, auth_transaction, status_code;
346         u16 resp = WLAN_STATUS_SUCCESS;
347         struct sta_info *sta = NULL;
348         int res;
349         u16 fc;
350         u8 *challenge = NULL;
351         u32 session_timeout, acct_interim_interval;
352         int vlan_id = 0;
353         u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
354         size_t resp_ies_len = 0;
355
356         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
357                 printf("handle_auth - too short payload (len=%lu)\n",
358                        (unsigned long) len);
359                 return;
360         }
361
362         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
363         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
364         status_code = le_to_host16(mgmt->u.auth.status_code);
365         fc = le_to_host16(mgmt->frame_control);
366
367         if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
368             2 + WLAN_AUTH_CHALLENGE_LEN &&
369             mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
370             mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
371                 challenge = &mgmt->u.auth.variable[2];
372
373         wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
374                    "auth_transaction=%d status_code=%d wep=%d%s",
375                    MAC2STR(mgmt->sa), auth_alg, auth_transaction,
376                    status_code, !!(fc & WLAN_FC_ISWEP),
377                    challenge ? " challenge" : "");
378
379         if (hapd->tkip_countermeasures) {
380                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
381                 goto fail;
382         }
383
384         if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
385                auth_alg == WLAN_AUTH_OPEN) ||
386 #ifdef CONFIG_IEEE80211R
387               (hapd->conf->wpa &&
388                (hapd->conf->wpa_key_mgmt &
389                 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
390                auth_alg == WLAN_AUTH_FT) ||
391 #endif /* CONFIG_IEEE80211R */
392               ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
393                auth_alg == WLAN_AUTH_SHARED_KEY))) {
394                 printf("Unsupported authentication algorithm (%d)\n",
395                        auth_alg);
396                 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
397                 goto fail;
398         }
399
400         if (!(auth_transaction == 1 ||
401               (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
402                 printf("Unknown authentication transaction number (%d)\n",
403                        auth_transaction);
404                 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
405                 goto fail;
406         }
407
408         if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
409                 printf("Station " MACSTR " not allowed to authenticate.\n",
410                        MAC2STR(mgmt->sa));
411                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
412                 goto fail;
413         }
414
415         res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
416                                       &session_timeout,
417                                       &acct_interim_interval, &vlan_id);
418         if (res == HOSTAPD_ACL_REJECT) {
419                 printf("Station " MACSTR " not allowed to authenticate.\n",
420                        MAC2STR(mgmt->sa));
421                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
422                 goto fail;
423         }
424         if (res == HOSTAPD_ACL_PENDING) {
425                 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
426                            " waiting for an external authentication",
427                            MAC2STR(mgmt->sa));
428                 /* Authentication code will re-send the authentication frame
429                  * after it has received (and cached) information from the
430                  * external source. */
431                 return;
432         }
433
434         sta = ap_sta_add(hapd, mgmt->sa);
435         if (!sta) {
436                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
437                 goto fail;
438         }
439
440         if (vlan_id > 0) {
441                 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
442                                                vlan_id) == NULL) {
443                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
444                                        HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
445                                        "%d received from RADIUS server",
446                                        vlan_id);
447                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
448                         goto fail;
449                 }
450                 sta->vlan_id = vlan_id;
451                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
452                                HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
453         }
454
455         sta->flags &= ~WLAN_STA_PREAUTH;
456         ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
457
458         if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
459                 sta->acct_interim_interval = acct_interim_interval;
460         if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
461                 ap_sta_session_timeout(hapd, sta, session_timeout);
462         else
463                 ap_sta_no_session_timeout(hapd, sta);
464
465         switch (auth_alg) {
466         case WLAN_AUTH_OPEN:
467                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
468                                HOSTAPD_LEVEL_DEBUG,
469                                "authentication OK (open system)");
470 #ifdef IEEE80211_REQUIRE_AUTH_ACK
471                 /* Station will be marked authenticated if it ACKs the
472                  * authentication reply. */
473 #else
474                 sta->flags |= WLAN_STA_AUTH;
475                 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
476                 sta->auth_alg = WLAN_AUTH_OPEN;
477                 mlme_authenticate_indication(hapd, sta);
478 #endif
479                 break;
480         case WLAN_AUTH_SHARED_KEY:
481                 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
482                                        fc & WLAN_FC_ISWEP);
483                 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
484                 mlme_authenticate_indication(hapd, sta);
485                 if (sta->challenge && auth_transaction == 1) {
486                         resp_ies[0] = WLAN_EID_CHALLENGE;
487                         resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
488                         os_memcpy(resp_ies + 2, sta->challenge,
489                                   WLAN_AUTH_CHALLENGE_LEN);
490                         resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
491                 }
492                 break;
493 #ifdef CONFIG_IEEE80211R
494         case WLAN_AUTH_FT:
495                 sta->auth_alg = WLAN_AUTH_FT;
496                 if (sta->wpa_sm == NULL)
497                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
498                                                         sta->addr);
499                 if (sta->wpa_sm == NULL) {
500                         wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
501                                    "state machine");
502                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
503                         goto fail;
504                 }
505                 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
506                                     auth_transaction, mgmt->u.auth.variable,
507                                     len - IEEE80211_HDRLEN -
508                                     sizeof(mgmt->u.auth),
509                                     handle_auth_ft_finish, hapd);
510                 /* handle_auth_ft_finish() callback will complete auth. */
511                 return;
512 #endif /* CONFIG_IEEE80211R */
513         }
514
515  fail:
516         send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
517                         auth_transaction + 1, resp, resp_ies, resp_ies_len);
518 }
519
520
521 static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
522 {
523         int i, j = 32, aid;
524
525         /* get a unique AID */
526         if (sta->aid > 0) {
527                 wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
528                 return 0;
529         }
530
531         for (i = 0; i < AID_WORDS; i++) {
532                 if (hapd->sta_aid[i] == (u32) -1)
533                         continue;
534                 for (j = 0; j < 32; j++) {
535                         if (!(hapd->sta_aid[i] & BIT(j)))
536                                 break;
537                 }
538                 if (j < 32)
539                         break;
540         }
541         if (j == 32)
542                 return -1;
543         aid = i * 32 + j + 1;
544         if (aid > 2007)
545                 return -1;
546
547         sta->aid = aid;
548         hapd->sta_aid[i] |= BIT(j);
549         wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
550         return 0;
551 }
552
553
554 static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
555                       const u8 *ssid_ie, size_t ssid_ie_len)
556 {
557         if (ssid_ie == NULL)
558                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
559
560         if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
561             os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
562                 char ssid_txt[33];
563                 ieee802_11_print_ssid(ssid_txt, ssid_ie, ssid_ie_len);
564                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
565                                HOSTAPD_LEVEL_INFO,
566                                "Station tried to associate with unknown SSID "
567                                "'%s'", ssid_txt);
568                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
569         }
570
571         return WLAN_STATUS_SUCCESS;
572 }
573
574
575 static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
576                      const u8 *wmm_ie, size_t wmm_ie_len)
577 {
578         sta->flags &= ~WLAN_STA_WMM;
579         if (wmm_ie && hapd->conf->wmm_enabled) {
580                 if (hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len))
581                         hostapd_logger(hapd, sta->addr,
582                                        HOSTAPD_MODULE_WPA,
583                                        HOSTAPD_LEVEL_DEBUG,
584                                        "invalid WMM element in association "
585                                        "request");
586                 else
587                         sta->flags |= WLAN_STA_WMM;
588         }
589         return WLAN_STATUS_SUCCESS;
590 }
591
592
593 static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
594                            struct ieee802_11_elems *elems)
595 {
596         if (!elems->supp_rates) {
597                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
598                                HOSTAPD_LEVEL_DEBUG,
599                                "No supported rates element in AssocReq");
600                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
601         }
602
603         if (elems->supp_rates_len > sizeof(sta->supported_rates)) {
604                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
605                                HOSTAPD_LEVEL_DEBUG,
606                                "Invalid supported rates element length %d",
607                                elems->supp_rates_len);
608                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
609         }
610
611         os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
612         os_memcpy(sta->supported_rates, elems->supp_rates,
613                   elems->supp_rates_len);
614         sta->supported_rates_len = elems->supp_rates_len;
615
616         if (elems->ext_supp_rates) {
617                 if (elems->supp_rates_len + elems->ext_supp_rates_len >
618                     sizeof(sta->supported_rates)) {
619                         hostapd_logger(hapd, sta->addr,
620                                        HOSTAPD_MODULE_IEEE80211,
621                                        HOSTAPD_LEVEL_DEBUG,
622                                        "Invalid supported rates element length"
623                                        " %d+%d", elems->supp_rates_len,
624                                        elems->ext_supp_rates_len);
625                         return WLAN_STATUS_UNSPECIFIED_FAILURE;
626                 }
627
628                 os_memcpy(sta->supported_rates + elems->supp_rates_len,
629                           elems->ext_supp_rates, elems->ext_supp_rates_len);
630                 sta->supported_rates_len += elems->ext_supp_rates_len;
631         }
632
633         return WLAN_STATUS_SUCCESS;
634 }
635
636
637 static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
638                            u8 *ies, size_t ies_len, int reassoc)
639 {
640         struct ieee802_11_elems elems;
641         u16 resp;
642         u8 *wpa_ie;
643         size_t wpa_ie_len;
644
645         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
646                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
647                                HOSTAPD_LEVEL_INFO, "Station sent an invalid "
648                                "association request");
649                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
650         }
651
652         resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
653         if (resp != WLAN_STATUS_SUCCESS)
654                 return resp;
655         resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
656         if (resp != WLAN_STATUS_SUCCESS)
657                 return resp;
658         resp = copy_supp_rates(hapd, sta, &elems);
659         if (resp != WLAN_STATUS_SUCCESS)
660                 return resp;
661 #ifdef CONFIG_IEEE80211N
662         resp = copy_sta_ht_capab(sta, elems.ht_capabilities,
663                                  elems.ht_capabilities_len);
664         if (resp != WLAN_STATUS_SUCCESS)
665                 return resp;
666 #endif /* CONFIG_IEEE80211N */
667
668         if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
669                 wpa_ie = elems.rsn_ie;
670                 wpa_ie_len = elems.rsn_ie_len;
671         } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
672                    elems.wpa_ie) {
673                 wpa_ie = elems.wpa_ie;
674                 wpa_ie_len = elems.wpa_ie_len;
675         } else {
676                 wpa_ie = NULL;
677                 wpa_ie_len = 0;
678         }
679
680 #ifdef CONFIG_WPS
681         sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
682         if (hapd->conf->wps_state && wpa_ie == NULL) {
683                 if (elems.wps_ie) {
684                         wpa_printf(MSG_DEBUG, "STA included WPS IE in "
685                                    "(Re)Association Request - assume WPS is "
686                                    "used");
687                         sta->flags |= WLAN_STA_WPS;
688                         wpabuf_free(sta->wps_ie);
689                         sta->wps_ie = wpabuf_alloc_copy(elems.wps_ie + 4,
690                                                         elems.wps_ie_len - 4);
691                 } else {
692                         wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE "
693                                    "in (Re)Association Request - possible WPS "
694                                    "use");
695                         sta->flags |= WLAN_STA_MAYBE_WPS;
696                 }
697         } else
698 #endif /* CONFIG_WPS */
699         if (hapd->conf->wpa && wpa_ie == NULL) {
700                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
701                                HOSTAPD_LEVEL_INFO,
702                                "No WPA/RSN IE in association request");
703                 return WLAN_STATUS_INVALID_IE;
704         }
705
706         if (hapd->conf->wpa && wpa_ie) {
707                 int res;
708                 wpa_ie -= 2;
709                 wpa_ie_len += 2;
710                 if (sta->wpa_sm == NULL)
711                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
712                                                         sta->addr);
713                 if (sta->wpa_sm == NULL) {
714                         wpa_printf(MSG_WARNING, "Failed to initialize WPA "
715                                    "state machine");
716                         return WLAN_STATUS_UNSPECIFIED_FAILURE;
717                 }
718                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
719                                           wpa_ie, wpa_ie_len,
720                                           elems.mdie, elems.mdie_len);
721                 if (res == WPA_INVALID_GROUP)
722                         resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
723                 else if (res == WPA_INVALID_PAIRWISE)
724                         resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
725                 else if (res == WPA_INVALID_AKMP)
726                         resp = WLAN_STATUS_AKMP_NOT_VALID;
727                 else if (res == WPA_ALLOC_FAIL)
728                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
729 #ifdef CONFIG_IEEE80211W
730                 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
731                         resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
732                 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
733                         resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
734 #endif /* CONFIG_IEEE80211W */
735                 else if (res == WPA_INVALID_MDIE)
736                         resp = WLAN_STATUS_INVALID_MDIE;
737                 else if (res != WPA_IE_OK)
738                         resp = WLAN_STATUS_INVALID_IE;
739                 if (resp != WLAN_STATUS_SUCCESS)
740                         return resp;
741 #ifdef CONFIG_IEEE80211W
742                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
743                     sta->sa_query_count > 0)
744                         ap_check_sa_query_timeout(hapd, sta);
745                 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
746                     (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
747                         /*
748                          * STA has already been associated with MFP and SA
749                          * Query timeout has not been reached. Reject the
750                          * association attempt temporarily and start SA Query,
751                          * if one is not pending.
752                          */
753
754                         if (sta->sa_query_count == 0)
755                                 ap_sta_start_sa_query(hapd, sta);
756
757                         return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
758                 }
759
760                 if (wpa_auth_uses_mfp(sta->wpa_sm))
761                         sta->flags |= WLAN_STA_MFP;
762                 else
763                         sta->flags &= ~WLAN_STA_MFP;
764 #endif /* CONFIG_IEEE80211W */
765
766 #ifdef CONFIG_IEEE80211R
767                 if (sta->auth_alg == WLAN_AUTH_FT) {
768                         if (!reassoc) {
769                                 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
770                                            "to use association (not "
771                                            "re-association) with FT auth_alg",
772                                            MAC2STR(sta->addr));
773                                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
774                         }
775
776                         resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
777                                                        ies_len);
778                         if (resp != WLAN_STATUS_SUCCESS)
779                                 return resp;
780                 }
781 #endif /* CONFIG_IEEE80211R */
782
783 #ifdef CONFIG_IEEE80211N
784                 if ((sta->flags & WLAN_STA_HT) &&
785                     wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
786                         hostapd_logger(hapd, sta->addr,
787                                        HOSTAPD_MODULE_IEEE80211,
788                                        HOSTAPD_LEVEL_INFO,
789                                        "Station tried to use TKIP with HT "
790                                        "association");
791                         return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
792                 }
793 #endif /* CONFIG_IEEE80211N */
794         } else
795                 wpa_auth_sta_no_wpa(sta->wpa_sm);
796
797         return WLAN_STATUS_SUCCESS;
798 }
799
800
801 static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
802                         u16 reason_code)
803 {
804         int send_len;
805         struct ieee80211_mgmt reply;
806
807         os_memset(&reply, 0, sizeof(reply));
808         reply.frame_control =
809                 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
810         os_memcpy(reply.da, addr, ETH_ALEN);
811         os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
812         os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
813
814         send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
815         reply.u.deauth.reason_code = host_to_le16(reason_code);
816
817         if (hostapd_send_mgmt_frame(hapd, &reply, send_len) < 0)
818                 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
819                            strerror(errno));
820 }
821
822
823 static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
824                             u16 status_code, int reassoc, u8 *ies,
825                             size_t ies_len)
826 {
827         int send_len;
828         u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
829         struct ieee80211_mgmt *reply;
830         u8 *p;
831
832         os_memset(buf, 0, sizeof(buf));
833         reply = (struct ieee80211_mgmt *) buf;
834         reply->frame_control =
835                 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
836                              (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
837                               WLAN_FC_STYPE_ASSOC_RESP));
838         os_memcpy(reply->da, sta->addr, ETH_ALEN);
839         os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
840         os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
841
842         send_len = IEEE80211_HDRLEN;
843         send_len += sizeof(reply->u.assoc_resp);
844         reply->u.assoc_resp.capab_info =
845                 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
846         reply->u.assoc_resp.status_code = host_to_le16(status_code);
847         reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
848                                                | BIT(14) | BIT(15));
849         /* Supported rates */
850         p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
851         /* Extended supported rates */
852         p = hostapd_eid_ext_supp_rates(hapd, p);
853         if (sta->flags & WLAN_STA_WMM)
854                 p = hostapd_eid_wmm(hapd, p);
855
856 #ifdef CONFIG_IEEE80211N
857         p = hostapd_eid_ht_capabilities(hapd, p);
858         p = hostapd_eid_ht_operation(hapd, p);
859 #endif /* CONFIG_IEEE80211N */
860
861 #ifdef CONFIG_IEEE80211R
862         if (status_code == WLAN_STATUS_SUCCESS) {
863                 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
864                  * Transition Information, RSN, [RIC Response] */
865                 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
866                                                 buf + sizeof(buf) - p,
867                                                 sta->auth_alg, ies, ies_len);
868         }
869 #endif /* CONFIG_IEEE80211R */
870
871 #ifdef CONFIG_IEEE80211W
872         if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
873                 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
874 #endif /* CONFIG_IEEE80211W */
875
876         send_len += p - reply->u.assoc_resp.variable;
877
878         if (hostapd_send_mgmt_frame(hapd, reply, send_len) < 0)
879                 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
880                            strerror(errno));
881 }
882
883
884 static void handle_assoc(struct hostapd_data *hapd,
885                          struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
886 {
887         u16 capab_info, listen_interval;
888         u16 resp = WLAN_STATUS_SUCCESS;
889         u8 *pos;
890         int left, i;
891         struct sta_info *sta;
892
893         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
894                                       sizeof(mgmt->u.assoc_req))) {
895                 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
896                        "\n", reassoc, (unsigned long) len);
897                 return;
898         }
899
900         if (reassoc) {
901                 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
902                 listen_interval = le_to_host16(
903                         mgmt->u.reassoc_req.listen_interval);
904                 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
905                            " capab_info=0x%02x listen_interval=%d current_ap="
906                            MACSTR,
907                            MAC2STR(mgmt->sa), capab_info, listen_interval,
908                            MAC2STR(mgmt->u.reassoc_req.current_ap));
909                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
910                 pos = mgmt->u.reassoc_req.variable;
911         } else {
912                 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
913                 listen_interval = le_to_host16(
914                         mgmt->u.assoc_req.listen_interval);
915                 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
916                            " capab_info=0x%02x listen_interval=%d",
917                            MAC2STR(mgmt->sa), capab_info, listen_interval);
918                 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
919                 pos = mgmt->u.assoc_req.variable;
920         }
921
922         sta = ap_get_sta(hapd, mgmt->sa);
923 #ifdef CONFIG_IEEE80211R
924         if (sta && sta->auth_alg == WLAN_AUTH_FT &&
925             (sta->flags & WLAN_STA_AUTH) == 0) {
926                 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
927                            "prior to authentication since it is using "
928                            "over-the-DS FT", MAC2STR(mgmt->sa));
929         } else
930 #endif /* CONFIG_IEEE80211R */
931         if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
932                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
933                                HOSTAPD_LEVEL_INFO, "Station tried to "
934                                "associate before authentication "
935                                "(aid=%d flags=0x%x)",
936                                sta ? sta->aid : -1,
937                                sta ? sta->flags : 0);
938                 send_deauth(hapd, mgmt->sa,
939                             WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
940                 return;
941         }
942
943         if (hapd->tkip_countermeasures) {
944                 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
945                 goto fail;
946         }
947
948         if (listen_interval > hapd->conf->max_listen_interval) {
949                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
950                                HOSTAPD_LEVEL_DEBUG,
951                                "Too large Listen Interval (%d)",
952                                listen_interval);
953                 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
954                 goto fail;
955         }
956
957         /* followed by SSID and Supported rates; and HT capabilities if 802.11n
958          * is used */
959         resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
960         if (resp != WLAN_STATUS_SUCCESS)
961                 goto fail;
962
963         if (hostapd_get_aid(hapd, sta) < 0) {
964                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
965                                HOSTAPD_LEVEL_INFO, "No room for more AIDs");
966                 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
967                 goto fail;
968         }
969
970         sta->capability = capab_info;
971         sta->listen_interval = listen_interval;
972
973         if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
974                 sta->flags |= WLAN_STA_NONERP;
975         for (i = 0; i < sta->supported_rates_len; i++) {
976                 if ((sta->supported_rates[i] & 0x7f) > 22) {
977                         sta->flags &= ~WLAN_STA_NONERP;
978                         break;
979                 }
980         }
981         if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
982                 sta->nonerp_set = 1;
983                 hapd->iface->num_sta_non_erp++;
984                 if (hapd->iface->num_sta_non_erp == 1)
985                         ieee802_11_set_beacons(hapd->iface);
986         }
987
988         if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
989             !sta->no_short_slot_time_set) {
990                 sta->no_short_slot_time_set = 1;
991                 hapd->iface->num_sta_no_short_slot_time++;
992                 if (hapd->iface->current_mode->mode ==
993                     HOSTAPD_MODE_IEEE80211G &&
994                     hapd->iface->num_sta_no_short_slot_time == 1)
995                         ieee802_11_set_beacons(hapd->iface);
996         }
997
998         if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
999                 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1000         else
1001                 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1002
1003         if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1004             !sta->no_short_preamble_set) {
1005                 sta->no_short_preamble_set = 1;
1006                 hapd->iface->num_sta_no_short_preamble++;
1007                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1008                     && hapd->iface->num_sta_no_short_preamble == 1)
1009                         ieee802_11_set_beacons(hapd->iface);
1010         }
1011
1012 #ifdef CONFIG_IEEE80211N
1013         update_ht_state(hapd, sta);
1014 #endif /* CONFIG_IEEE80211N */
1015
1016         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1017                        HOSTAPD_LEVEL_DEBUG,
1018                        "association OK (aid %d)", sta->aid);
1019         /* Station will be marked associated, after it acknowledges AssocResp
1020          */
1021
1022 #ifdef CONFIG_IEEE80211W
1023         if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1024                 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1025                            "SA Query procedure", reassoc ? "re" : "");
1026                 /* TODO: Send a protected Disassociate frame to the STA using
1027                  * the old key and Reason Code "Previous Authentication no
1028                  * longer valid". Make sure this is only sent protected since
1029                  * unprotected frame would be received by the STA that is now
1030                  * trying to associate.
1031                  */
1032         }
1033 #endif /* CONFIG_IEEE80211W */
1034
1035         if (reassoc) {
1036                 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1037                           ETH_ALEN);
1038         }
1039
1040         if (sta->last_assoc_req)
1041                 os_free(sta->last_assoc_req);
1042         sta->last_assoc_req = os_malloc(len);
1043         if (sta->last_assoc_req)
1044                 os_memcpy(sta->last_assoc_req, mgmt, len);
1045
1046         /* Make sure that the previously registered inactivity timer will not
1047          * remove the STA immediately. */
1048         sta->timeout_next = STA_NULLFUNC;
1049
1050  fail:
1051         send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
1052 }
1053
1054
1055 static void handle_disassoc(struct hostapd_data *hapd,
1056                             struct ieee80211_mgmt *mgmt, size_t len)
1057 {
1058         struct sta_info *sta;
1059
1060         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1061                 printf("handle_disassoc - too short payload (len=%lu)\n",
1062                        (unsigned long) len);
1063                 return;
1064         }
1065
1066         wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1067                    MAC2STR(mgmt->sa),
1068                    le_to_host16(mgmt->u.disassoc.reason_code));
1069
1070         sta = ap_get_sta(hapd, mgmt->sa);
1071         if (sta == NULL) {
1072                 printf("Station " MACSTR " trying to disassociate, but it "
1073                        "is not associated.\n", MAC2STR(mgmt->sa));
1074                 return;
1075         }
1076
1077         sta->flags &= ~WLAN_STA_ASSOC;
1078         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1079                 MAC2STR(sta->addr));
1080         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1081         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1082                        HOSTAPD_LEVEL_INFO, "disassociated");
1083         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1084         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1085         /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1086          * authenticated. */
1087         accounting_sta_stop(hapd, sta);
1088         ieee802_1x_free_station(sta);
1089         hostapd_sta_remove(hapd, sta->addr);
1090
1091         if (sta->timeout_next == STA_NULLFUNC ||
1092             sta->timeout_next == STA_DISASSOC) {
1093                 sta->timeout_next = STA_DEAUTH;
1094                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1095                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1096                                        hapd, sta);
1097         }
1098
1099         mlme_disassociate_indication(
1100                 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1101 }
1102
1103
1104 static void handle_deauth(struct hostapd_data *hapd,
1105                           struct ieee80211_mgmt *mgmt, size_t len)
1106 {
1107         struct sta_info *sta;
1108
1109         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1110                 printf("handle_deauth - too short payload (len=%lu)\n",
1111                        (unsigned long) len);
1112                 return;
1113         }
1114
1115         wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1116                    " reason_code=%d",
1117                    MAC2STR(mgmt->sa),
1118                    le_to_host16(mgmt->u.deauth.reason_code));
1119
1120         sta = ap_get_sta(hapd, mgmt->sa);
1121         if (sta == NULL) {
1122                 printf("Station " MACSTR " trying to deauthenticate, but it "
1123                        "is not authenticated.\n", MAC2STR(mgmt->sa));
1124                 return;
1125         }
1126
1127         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1128         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1129                 MAC2STR(sta->addr));
1130         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1131         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1132                        HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1133         mlme_deauthenticate_indication(
1134                 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1135         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1136         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1137         ap_free_sta(hapd, sta);
1138 }
1139
1140
1141 static void handle_beacon(struct hostapd_data *hapd,
1142                           struct ieee80211_mgmt *mgmt, size_t len,
1143                           struct hostapd_frame_info *fi)
1144 {
1145         struct ieee802_11_elems elems;
1146
1147         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1148                 printf("handle_beacon - too short payload (len=%lu)\n",
1149                        (unsigned long) len);
1150                 return;
1151         }
1152
1153         (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1154                                       len - (IEEE80211_HDRLEN +
1155                                              sizeof(mgmt->u.beacon)), &elems,
1156                                       0);
1157
1158         ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1159 }
1160
1161
1162 #ifdef CONFIG_IEEE80211W
1163
1164 /* MLME-SAQuery.request */
1165 void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
1166                                   const u8 *addr, const u8 *trans_id)
1167 {
1168         struct ieee80211_mgmt mgmt;
1169         u8 *end;
1170
1171         wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
1172                    MACSTR, MAC2STR(addr));
1173         wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1174                     trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1175
1176         os_memset(&mgmt, 0, sizeof(mgmt));
1177         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1178                                           WLAN_FC_STYPE_ACTION);
1179         os_memcpy(mgmt.da, addr, ETH_ALEN);
1180         os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
1181         os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
1182         mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
1183         mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
1184         os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
1185                   WLAN_SA_QUERY_TR_ID_LEN);
1186         end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1187         if (hostapd_send_mgmt_frame(hapd, &mgmt, end - (u8 *) &mgmt) < 0)
1188                 perror("ieee802_11_send_sa_query_req: send");
1189 }
1190
1191
1192 static void hostapd_sa_query_action(struct hostapd_data *hapd,
1193                                     struct ieee80211_mgmt *mgmt, size_t len)
1194 {
1195         struct sta_info *sta;
1196         u8 *end;
1197         int i;
1198
1199         end = mgmt->u.action.u.sa_query_resp.trans_id +
1200                 WLAN_SA_QUERY_TR_ID_LEN;
1201         if (((u8 *) mgmt) + len < end) {
1202                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1203                            "frame (len=%lu)", (unsigned long) len);
1204                 return;
1205         }
1206
1207         if (mgmt->u.action.u.sa_query_resp.action != WLAN_SA_QUERY_RESPONSE) {
1208                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
1209                            "Action %d", mgmt->u.action.u.sa_query_resp.action);
1210                 return;
1211         }
1212
1213         wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
1214                    MACSTR, MAC2STR(mgmt->sa));
1215         wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1216                     mgmt->u.action.u.sa_query_resp.trans_id,
1217                     WLAN_SA_QUERY_TR_ID_LEN);
1218
1219         /* MLME-SAQuery.confirm */
1220
1221         sta = ap_get_sta(hapd, mgmt->sa);
1222         if (sta == NULL || sta->sa_query_trans_id == NULL) {
1223                 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
1224                            "pending SA Query request found");
1225                 return;
1226         }
1227
1228         for (i = 0; i < sta->sa_query_count; i++) {
1229                 if (os_memcmp(sta->sa_query_trans_id +
1230                               i * WLAN_SA_QUERY_TR_ID_LEN,
1231                               mgmt->u.action.u.sa_query_resp.trans_id,
1232                               WLAN_SA_QUERY_TR_ID_LEN) == 0)
1233                         break;
1234         }
1235
1236         if (i >= sta->sa_query_count) {
1237                 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
1238                            "transaction identifier found");
1239                 return;
1240         }
1241
1242         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1243                        HOSTAPD_LEVEL_DEBUG,
1244                        "Reply to pending SA Query received");
1245         ap_sta_stop_sa_query(hapd, sta);
1246 }
1247
1248
1249 static int robust_action_frame(u8 category)
1250 {
1251         return category != WLAN_ACTION_PUBLIC &&
1252                 category != WLAN_ACTION_HT;
1253 }
1254 #endif /* CONFIG_IEEE80211W */
1255
1256
1257 static void handle_action(struct hostapd_data *hapd,
1258                           struct ieee80211_mgmt *mgmt, size_t len)
1259 {
1260         struct sta_info *sta;
1261
1262         if (len < IEEE80211_HDRLEN + 1) {
1263                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1264                                HOSTAPD_LEVEL_DEBUG,
1265                                "handle_action - too short payload (len=%lu)",
1266                                (unsigned long) len);
1267                 return;
1268         }
1269
1270         sta = ap_get_sta(hapd, mgmt->sa);
1271 #ifdef CONFIG_IEEE80211W
1272         if (sta && (sta->flags & WLAN_STA_MFP) &&
1273             !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP) &&
1274               robust_action_frame(mgmt->u.action.category))) {
1275                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1276                                HOSTAPD_LEVEL_DEBUG,
1277                                "Dropped unprotected Robust Action frame from "
1278                                "an MFP STA");
1279                 return;
1280         }
1281 #endif /* CONFIG_IEEE80211W */
1282
1283         switch (mgmt->u.action.category) {
1284 #ifdef CONFIG_IEEE80211R
1285         case WLAN_ACTION_FT:
1286         {
1287                 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1288                         wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1289                                    "frame from unassociated STA " MACSTR,
1290                                    MAC2STR(mgmt->sa));
1291                         return;
1292                 }
1293
1294                 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1295                                      len - IEEE80211_HDRLEN))
1296                         break;
1297
1298                 return;
1299         }
1300 #endif /* CONFIG_IEEE80211R */
1301         case WLAN_ACTION_WMM:
1302                 hostapd_wmm_action(hapd, mgmt, len);
1303                 return;
1304 #ifdef CONFIG_IEEE80211W
1305         case WLAN_ACTION_SA_QUERY:
1306                 hostapd_sa_query_action(hapd, mgmt, len);
1307                 return;
1308 #endif /* CONFIG_IEEE80211W */
1309         }
1310
1311         hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1312                        HOSTAPD_LEVEL_DEBUG,
1313                        "handle_action - unknown action category %d or invalid "
1314                        "frame",
1315                        mgmt->u.action.category);
1316         if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1317             !(mgmt->sa[0] & 0x01)) {
1318                 /*
1319                  * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1320                  * Return the Action frame to the source without change
1321                  * except that MSB of the Category set to 1.
1322                  */
1323                 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1324                            "frame back to sender");
1325                 os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
1326                 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
1327                 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
1328                 mgmt->u.action.category |= 0x80;
1329
1330                 hostapd_send_mgmt_frame(hapd, mgmt, len);
1331         }
1332 }
1333
1334
1335 /**
1336  * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1337  * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1338  * sent to)
1339  * @buf: management frame data (starting from IEEE 802.11 header)
1340  * @len: length of frame data in octets
1341  * @stype: management frame subtype from frame control field
1342  * @fi: meta data about received frame (signal level, etc.)
1343  *
1344  * Process all incoming IEEE 802.11 management frames. This will be called for
1345  * each frame received from the kernel driver through wlan#ap interface. In
1346  * addition, it can be called to re-inserted pending frames (e.g., when using
1347  * external RADIUS server as an MAC ACL).
1348  */
1349 void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
1350                      struct hostapd_frame_info *fi)
1351 {
1352         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1353         int broadcast;
1354
1355         if (stype == WLAN_FC_STYPE_BEACON) {
1356                 handle_beacon(hapd, mgmt, len, fi);
1357                 return;
1358         }
1359
1360         broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1361                 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1362                 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1363
1364         if (!broadcast &&
1365             os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1366                 printf("MGMT: BSSID=" MACSTR " not our address\n",
1367                        MAC2STR(mgmt->bssid));
1368                 return;
1369         }
1370
1371
1372         if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1373                 handle_probe_req(hapd, mgmt, len);
1374                 return;
1375         }
1376
1377         if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1378                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1379                                HOSTAPD_LEVEL_DEBUG,
1380                                "MGMT: DA=" MACSTR " not our address",
1381                                MAC2STR(mgmt->da));
1382                 return;
1383         }
1384
1385         switch (stype) {
1386         case WLAN_FC_STYPE_AUTH:
1387                 wpa_printf(MSG_DEBUG, "mgmt::auth");
1388                 handle_auth(hapd, mgmt, len);
1389                 break;
1390         case WLAN_FC_STYPE_ASSOC_REQ:
1391                 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1392                 handle_assoc(hapd, mgmt, len, 0);
1393                 break;
1394         case WLAN_FC_STYPE_REASSOC_REQ:
1395                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1396                 handle_assoc(hapd, mgmt, len, 1);
1397                 break;
1398         case WLAN_FC_STYPE_DISASSOC:
1399                 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1400                 handle_disassoc(hapd, mgmt, len);
1401                 break;
1402         case WLAN_FC_STYPE_DEAUTH:
1403                 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1404                 handle_deauth(hapd, mgmt, len);
1405                 break;
1406         case WLAN_FC_STYPE_ACTION:
1407                 wpa_printf(MSG_DEBUG, "mgmt::action");
1408                 handle_action(hapd, mgmt, len);
1409                 break;
1410         default:
1411                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1412                                HOSTAPD_LEVEL_DEBUG,
1413                                "unknown mgmt frame subtype %d", stype);
1414                 break;
1415         }
1416 }
1417
1418
1419 static void handle_auth_cb(struct hostapd_data *hapd,
1420                            struct ieee80211_mgmt *mgmt,
1421                            size_t len, int ok)
1422 {
1423         u16 auth_alg, auth_transaction, status_code;
1424         struct sta_info *sta;
1425
1426         if (!ok) {
1427                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1428                                HOSTAPD_LEVEL_NOTICE,
1429                                "did not acknowledge authentication response");
1430                 return;
1431         }
1432
1433         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1434                 printf("handle_auth_cb - too short payload (len=%lu)\n",
1435                        (unsigned long) len);
1436                 return;
1437         }
1438
1439         auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1440         auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1441         status_code = le_to_host16(mgmt->u.auth.status_code);
1442
1443         sta = ap_get_sta(hapd, mgmt->da);
1444         if (!sta) {
1445                 printf("handle_auth_cb: STA " MACSTR " not found\n",
1446                        MAC2STR(mgmt->da));
1447                 return;
1448         }
1449
1450         if (status_code == WLAN_STATUS_SUCCESS &&
1451             ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1452              (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1453                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1454                                HOSTAPD_LEVEL_INFO, "authenticated");
1455                 sta->flags |= WLAN_STA_AUTH;
1456         }
1457 }
1458
1459
1460 static void handle_assoc_cb(struct hostapd_data *hapd,
1461                             struct ieee80211_mgmt *mgmt,
1462                             size_t len, int reassoc, int ok)
1463 {
1464         u16 status;
1465         struct sta_info *sta;
1466         int new_assoc = 1;
1467         struct ieee80211_ht_capabilities ht_cap;
1468         int set_flags, total_flags, flags_and, flags_or;
1469
1470         if (!ok) {
1471                 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1472                                HOSTAPD_LEVEL_DEBUG,
1473                                "did not acknowledge association response");
1474                 return;
1475         }
1476
1477         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1478                                       sizeof(mgmt->u.assoc_resp))) {
1479                 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1480                        "(len=%lu)\n", reassoc, (unsigned long) len);
1481                 return;
1482         }
1483
1484         if (reassoc)
1485                 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1486         else
1487                 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1488
1489         sta = ap_get_sta(hapd, mgmt->da);
1490         if (!sta) {
1491                 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1492                        MAC2STR(mgmt->da));
1493                 return;
1494         }
1495
1496         if (status != WLAN_STATUS_SUCCESS)
1497                 goto fail;
1498
1499         /* Stop previous accounting session, if one is started, and allocate
1500          * new session id for the new session. */
1501         accounting_sta_stop(hapd, sta);
1502
1503         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1504                        HOSTAPD_LEVEL_INFO,
1505                        "associated (aid %d)",
1506                        sta->aid);
1507
1508         if (sta->flags & WLAN_STA_ASSOC)
1509                 new_assoc = 0;
1510         sta->flags |= WLAN_STA_ASSOC;
1511         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
1512                 /* Open or static WEP; no separate authorization */
1513                 sta->flags |= WLAN_STA_AUTHORIZED;
1514                 wpa_msg(hapd->msg_ctx, MSG_INFO,
1515                         AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
1516         }
1517
1518         if (reassoc)
1519                 mlme_reassociate_indication(hapd, sta);
1520         else
1521                 mlme_associate_indication(hapd, sta);
1522
1523 #ifdef CONFIG_IEEE80211W
1524         sta->sa_query_timed_out = 0;
1525 #endif /* CONFIG_IEEE80211W */
1526
1527         /*
1528          * Remove the STA entry in order to make sure the STA PS state gets
1529          * cleared and configuration gets updated in case of reassociation back
1530          * to the same AP.
1531          */
1532         hostapd_sta_remove(hapd, sta->addr);
1533
1534 #ifdef CONFIG_IEEE80211N
1535         if (sta->flags & WLAN_STA_HT)
1536                 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
1537 #endif /* CONFIG_IEEE80211N */
1538
1539         if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
1540                             sta->capability, sta->supported_rates,
1541                             sta->supported_rates_len, sta->listen_interval,
1542                             sta->flags & WLAN_STA_HT ? &ht_cap : NULL))
1543         {
1544                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1545                                HOSTAPD_LEVEL_NOTICE,
1546                                "Could not add STA to kernel driver");
1547         }
1548
1549         if (sta->eapol_sm == NULL) {
1550                 /*
1551                  * This STA does not use RADIUS server for EAP authentication,
1552                  * so bind it to the selected VLAN interface now, since the
1553                  * interface selection is not going to change anymore.
1554                  */
1555                 ap_sta_bind_vlan(hapd, sta, 0);
1556         } else if (sta->vlan_id) {
1557                 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1558                 ap_sta_bind_vlan(hapd, sta, 0);
1559         }
1560
1561         total_flags = hostapd_sta_flags_to_drv(sta->flags);
1562         set_flags = WPA_STA_SHORT_PREAMBLE | WPA_STA_WMM | WPA_STA_MFP;
1563         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
1564             sta->flags & WLAN_STA_AUTHORIZED)
1565                 set_flags |= WPA_STA_AUTHORIZED;
1566         flags_or = total_flags & set_flags;
1567         flags_and = total_flags | ~set_flags;
1568         hostapd_sta_set_flags(hapd, sta->addr, total_flags,
1569                               flags_or, flags_and);
1570
1571         if (sta->auth_alg == WLAN_AUTH_FT)
1572                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1573         else
1574                 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1575         hostapd_new_assoc_sta(hapd, sta, !new_assoc);
1576
1577         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1578
1579  fail:
1580         /* Copy of the association request is not needed anymore */
1581         if (sta->last_assoc_req) {
1582                 os_free(sta->last_assoc_req);
1583                 sta->last_assoc_req = NULL;
1584         }
1585 }
1586
1587
1588 /**
1589  * ieee802_11_mgmt_cb - Process management frame TX status callback
1590  * @hapd: hostapd BSS data structure (the BSS from which the management frame
1591  * was sent from)
1592  * @buf: management frame data (starting from IEEE 802.11 header)
1593  * @len: length of frame data in octets
1594  * @stype: management frame subtype from frame control field
1595  * @ok: Whether the frame was ACK'ed
1596  */
1597 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
1598                         u16 stype, int ok)
1599 {
1600         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
1601
1602         switch (stype) {
1603         case WLAN_FC_STYPE_AUTH:
1604                 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1605                 handle_auth_cb(hapd, mgmt, len, ok);
1606                 break;
1607         case WLAN_FC_STYPE_ASSOC_RESP:
1608                 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1609                 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1610                 break;
1611         case WLAN_FC_STYPE_REASSOC_RESP:
1612                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1613                 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1614                 break;
1615         case WLAN_FC_STYPE_PROBE_RESP:
1616                 wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
1617                 break;
1618         case WLAN_FC_STYPE_DEAUTH:
1619                 /* ignore */
1620                 break;
1621         case WLAN_FC_STYPE_ACTION:
1622                 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1623                 break;
1624         default:
1625                 printf("unknown mgmt cb frame subtype %d\n", stype);
1626                 break;
1627         }
1628 }
1629
1630
1631 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1632 {
1633         /* TODO */
1634         return 0;
1635 }
1636
1637
1638 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1639                            char *buf, size_t buflen)
1640 {
1641         /* TODO */
1642         return 0;
1643 }
1644
1645 #endif /* CONFIG_NATIVE_WINDOWS */