2 * WPA Supplicant - Client mode MLME
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Alternatively, this software may be distributed under the terms of BSD
14 * See README and COPYING for more details.
21 #include "config_ssid.h"
22 #include "wpa_supplicant_i.h"
24 #include "drivers/driver.h"
25 #include "ieee802_11_defs.h"
26 #include "ieee802_11_common.h"
30 /* Timeouts and intervals in milliseconds */
31 #define IEEE80211_AUTH_TIMEOUT (200)
32 #define IEEE80211_AUTH_MAX_TRIES 3
33 #define IEEE80211_ASSOC_TIMEOUT (200)
34 #define IEEE80211_ASSOC_MAX_TRIES 3
35 #define IEEE80211_MONITORING_INTERVAL (2000)
36 #define IEEE80211_PROBE_INTERVAL (60000)
37 #define IEEE80211_RETRY_AUTH_INTERVAL (1000)
38 #define IEEE80211_SCAN_INTERVAL (2000)
39 #define IEEE80211_SCAN_INTERVAL_SLOW (15000)
40 #define IEEE80211_IBSS_JOIN_TIMEOUT (20000)
42 #define IEEE80211_PROBE_DELAY (33)
43 #define IEEE80211_CHANNEL_TIME (33)
44 #define IEEE80211_PASSIVE_CHANNEL_TIME (200)
45 #define IEEE80211_SCAN_RESULT_EXPIRE (10000)
46 #define IEEE80211_IBSS_MERGE_INTERVAL (30000)
47 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60000)
49 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
52 #define IEEE80211_FC(type, stype) host_to_le16((type << 2) | (stype << 4))
55 struct ieee80211_sta_bss {
56 struct ieee80211_sta_bss *next;
57 struct ieee80211_sta_bss *hnext;
60 u8 ssid[MAX_SSID_LEN];
62 u16 capability; /* host byte order */
77 #define IEEE80211_MAX_SUPP_RATES 32
78 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
79 size_t supp_rates_len;
84 struct os_time last_update;
88 static void ieee80211_send_probe_req(struct wpa_supplicant *wpa_s,
90 const u8 *ssid, size_t ssid_len);
91 static struct ieee80211_sta_bss *
92 ieee80211_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid);
93 static int ieee80211_sta_find_ibss(struct wpa_supplicant *wpa_s);
94 static int ieee80211_sta_wep_configured(struct wpa_supplicant *wpa_s);
95 static void ieee80211_sta_timer(void *eloop_ctx, void *timeout_ctx);
96 static void ieee80211_sta_scan_timer(void *eloop_ctx, void *timeout_ctx);
99 static int ieee80211_sta_set_channel(struct wpa_supplicant *wpa_s,
100 wpa_hw_mode phymode, int chan,
104 struct wpa_hw_modes *mode;
106 for (i = 0; i < wpa_s->mlme.num_modes; i++) {
107 mode = &wpa_s->mlme.modes[i];
108 if (mode->mode == phymode) {
109 wpa_s->mlme.curr_rates = mode->rates;
110 wpa_s->mlme.num_curr_rates = mode->num_rates;
115 return wpa_drv_set_channel(wpa_s, phymode, chan, freq);
119 static int ecw2cw(int ecw)
130 static void ieee80211_sta_wmm_params(struct wpa_supplicant *wpa_s,
131 u8 *wmm_param, size_t wmm_param_len)
138 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
140 count = wmm_param[6] & 0x0f;
141 if (count == wpa_s->mlme.wmm_last_param_set)
143 wpa_s->mlme.wmm_last_param_set = count;
146 left = wmm_param_len - 8;
149 for (; left >= 4; left -= 4, pos += 4) {
150 int aci = (pos[0] >> 5) & 0x03;
151 int acm = (pos[0] >> 4) & 0x01;
152 int aifs, cw_max, cw_min, burst_time;
157 wmm_acm |= BIT(1) | BIT(2); /* BK/- */
161 wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
165 wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
170 wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
174 aifs = pos[0] & 0x0f;
175 cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
176 cw_min = ecw2cw(pos[1] & 0x0f);
177 /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
178 burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
179 wpa_printf(MSG_DEBUG, "MLME: WMM aci=%d acm=%d aifs=%d "
180 "cWmin=%d cWmax=%d burst=%d",
181 aci, acm, aifs, cw_min, cw_max, burst_time);
182 /* TODO: driver configuration */
187 static void ieee80211_set_associated(struct wpa_supplicant *wpa_s, int assoc)
189 if (wpa_s->mlme.associated == assoc && !assoc)
192 wpa_s->mlme.associated = assoc;
195 union wpa_event_data data;
196 os_memset(&data, 0, sizeof(data));
197 wpa_s->mlme.prev_bssid_set = 1;
198 os_memcpy(wpa_s->mlme.prev_bssid, wpa_s->bssid, ETH_ALEN);
199 data.assoc_info.req_ies = wpa_s->mlme.assocreq_ies;
200 data.assoc_info.req_ies_len = wpa_s->mlme.assocreq_ies_len;
201 data.assoc_info.resp_ies = wpa_s->mlme.assocresp_ies;
202 data.assoc_info.resp_ies_len = wpa_s->mlme.assocresp_ies_len;
203 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
205 wpa_supplicant_event(wpa_s, EVENT_DISASSOC, NULL);
207 os_get_time(&wpa_s->mlme.last_probe);
211 static int ieee80211_sta_tx(struct wpa_supplicant *wpa_s, const u8 *buf,
214 return wpa_drv_send_mlme(wpa_s, buf, len);
218 static void ieee80211_send_auth(struct wpa_supplicant *wpa_s,
219 int transaction, u8 *extra, size_t extra_len,
224 struct ieee80211_mgmt *mgmt;
226 buf = os_malloc(sizeof(*mgmt) + 6 + extra_len);
228 wpa_printf(MSG_DEBUG, "MLME: failed to allocate buffer for "
233 mgmt = (struct ieee80211_mgmt *) buf;
235 os_memset(mgmt, 0, 24 + 6);
236 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
239 mgmt->frame_control |= host_to_le16(WLAN_FC_ISWEP);
240 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
241 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
242 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
243 mgmt->u.auth.auth_alg = host_to_le16(wpa_s->mlme.auth_alg);
244 mgmt->u.auth.auth_transaction = host_to_le16(transaction);
245 wpa_s->mlme.auth_transaction = transaction + 1;
246 mgmt->u.auth.status_code = host_to_le16(0);
248 os_memcpy(buf + len, extra, extra_len);
252 ieee80211_sta_tx(wpa_s, buf, len);
257 static void ieee80211_reschedule_timer(struct wpa_supplicant *wpa_s, int ms)
259 eloop_cancel_timeout(ieee80211_sta_timer, wpa_s, NULL);
260 eloop_register_timeout(ms / 1000, 1000 * (ms % 1000),
261 ieee80211_sta_timer, wpa_s, NULL);
265 static void ieee80211_authenticate(struct wpa_supplicant *wpa_s)
270 wpa_s->mlme.auth_tries++;
271 if (wpa_s->mlme.auth_tries > IEEE80211_AUTH_MAX_TRIES) {
272 wpa_printf(MSG_DEBUG, "MLME: authentication with AP " MACSTR
273 " timed out", MAC2STR(wpa_s->bssid));
277 wpa_s->mlme.state = IEEE80211_AUTHENTICATE;
278 wpa_printf(MSG_DEBUG, "MLME: authenticate with AP " MACSTR,
279 MAC2STR(wpa_s->bssid));
284 #ifdef CONFIG_IEEE80211R
285 if ((wpa_s->mlme.key_mgmt == KEY_MGMT_FT_802_1X ||
286 wpa_s->mlme.key_mgmt == KEY_MGMT_FT_PSK) &&
287 wpa_s->mlme.ft_ies) {
288 struct ieee80211_sta_bss *bss;
289 struct rsn_mdie *mdie = NULL;
290 bss = ieee80211_bss_get(wpa_s, wpa_s->bssid);
291 if (bss && bss->mdie_len >= 2 + sizeof(*mdie))
292 mdie = (struct rsn_mdie *) (bss->mdie + 2);
294 os_memcmp(mdie->mobility_domain, wpa_s->mlme.current_md,
295 MOBILITY_DOMAIN_ID_LEN) == 0) {
296 wpa_printf(MSG_DEBUG, "MLME: Trying to use FT "
298 wpa_s->mlme.auth_alg = WLAN_AUTH_FT;
299 extra = wpa_s->mlme.ft_ies;
300 extra_len = wpa_s->mlme.ft_ies_len;
303 #endif /* CONFIG_IEEE80211R */
305 ieee80211_send_auth(wpa_s, 1, extra, extra_len, 0);
307 ieee80211_reschedule_timer(wpa_s, IEEE80211_AUTH_TIMEOUT);
311 static void ieee80211_send_assoc(struct wpa_supplicant *wpa_s)
313 struct ieee80211_mgmt *mgmt;
317 struct ieee80211_sta_bss *bss;
321 if (wpa_s->mlme.curr_rates == NULL) {
322 wpa_printf(MSG_DEBUG, "MLME: curr_rates not set for assoc");
326 buflen = sizeof(*mgmt) + 200 + wpa_s->mlme.extra_ie_len +
327 wpa_s->mlme.ssid_len;
328 #ifdef CONFIG_IEEE80211R
329 if (wpa_s->mlme.ft_ies)
330 buflen += wpa_s->mlme.ft_ies_len;
331 #endif /* CONFIG_IEEE80211R */
332 buf = os_malloc(buflen);
334 wpa_printf(MSG_DEBUG, "MLME: failed to allocate buffer for "
340 capab = wpa_s->mlme.capab;
341 if (wpa_s->mlme.phymode == WPA_MODE_IEEE80211G) {
342 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME |
343 WLAN_CAPABILITY_SHORT_PREAMBLE;
345 bss = ieee80211_bss_get(wpa_s, wpa_s->bssid);
347 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
348 capab |= WLAN_CAPABILITY_PRIVACY;
354 mgmt = (struct ieee80211_mgmt *) buf;
356 os_memset(mgmt, 0, 24);
357 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
358 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
359 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
361 if (wpa_s->mlme.prev_bssid_set) {
363 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
364 WLAN_FC_STYPE_REASSOC_REQ);
365 mgmt->u.reassoc_req.capab_info = host_to_le16(capab);
366 mgmt->u.reassoc_req.listen_interval = host_to_le16(1);
367 os_memcpy(mgmt->u.reassoc_req.current_ap,
368 wpa_s->mlme.prev_bssid,
372 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
373 WLAN_FC_STYPE_ASSOC_REQ);
374 mgmt->u.assoc_req.capab_info = host_to_le16(capab);
375 mgmt->u.assoc_req.listen_interval = host_to_le16(1);
379 ies = pos = buf + blen;
380 blen += 2 + wpa_s->mlme.ssid_len;
381 *pos++ = WLAN_EID_SSID;
382 *pos++ = wpa_s->mlme.ssid_len;
383 os_memcpy(pos, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len);
385 len = wpa_s->mlme.num_curr_rates;
390 *pos++ = WLAN_EID_SUPP_RATES;
392 for (i = 0; i < len; i++) {
393 int rate = wpa_s->mlme.curr_rates[i].rate;
394 *pos++ = (u8) (rate / 5);
397 if (wpa_s->mlme.num_curr_rates > len) {
399 blen += wpa_s->mlme.num_curr_rates - len + 2;
400 *pos++ = WLAN_EID_EXT_SUPP_RATES;
401 *pos++ = wpa_s->mlme.num_curr_rates - len;
402 for (i = len; i < wpa_s->mlme.num_curr_rates; i++) {
403 int rate = wpa_s->mlme.curr_rates[i].rate;
404 *pos++ = (u8) (rate / 5);
408 if (wpa_s->mlme.extra_ie && wpa_s->mlme.auth_alg != WLAN_AUTH_FT) {
410 blen += wpa_s->mlme.extra_ie_len;
411 os_memcpy(pos, wpa_s->mlme.extra_ie, wpa_s->mlme.extra_ie_len);
414 #ifdef CONFIG_IEEE80211R
415 if ((wpa_s->mlme.key_mgmt == KEY_MGMT_FT_802_1X ||
416 wpa_s->mlme.key_mgmt == KEY_MGMT_FT_PSK) &&
417 wpa_s->mlme.auth_alg != WLAN_AUTH_FT &&
419 bss->mdie_len >= 2 + sizeof(struct rsn_mdie) &&
420 bss->mdie[1] >= sizeof(struct rsn_mdie)) {
422 blen += 2 + sizeof(struct rsn_mdie);
423 *pos++ = WLAN_EID_MOBILITY_DOMAIN;
424 *pos++ = sizeof(struct rsn_mdie);
425 os_memcpy(pos, bss->mdie + 2, MOBILITY_DOMAIN_ID_LEN);
426 pos += MOBILITY_DOMAIN_ID_LEN;
427 *pos++ = 0; /* FIX: copy from the target AP's MDIE */
430 if ((wpa_s->mlme.key_mgmt == KEY_MGMT_FT_802_1X ||
431 wpa_s->mlme.key_mgmt == KEY_MGMT_FT_PSK) &&
432 wpa_s->mlme.auth_alg == WLAN_AUTH_FT && wpa_s->mlme.ft_ies) {
434 os_memcpy(pos, wpa_s->mlme.ft_ies, wpa_s->mlme.ft_ies_len);
435 pos += wpa_s->mlme.ft_ies_len;
436 blen += wpa_s->mlme.ft_ies_len;
438 #endif /* CONFIG_IEEE80211R */
440 if (wmm && wpa_s->mlme.wmm_enabled) {
443 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
444 *pos++ = 7; /* len */
445 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
448 *pos++ = 2; /* WMM */
449 *pos++ = 0; /* WMM info */
450 *pos++ = 1; /* WMM ver */
454 os_free(wpa_s->mlme.assocreq_ies);
455 wpa_s->mlme.assocreq_ies_len = (buf + blen) - ies;
456 wpa_s->mlme.assocreq_ies = os_malloc(wpa_s->mlme.assocreq_ies_len);
457 if (wpa_s->mlme.assocreq_ies) {
458 os_memcpy(wpa_s->mlme.assocreq_ies, ies,
459 wpa_s->mlme.assocreq_ies_len);
462 ieee80211_sta_tx(wpa_s, buf, blen);
467 static void ieee80211_send_deauth(struct wpa_supplicant *wpa_s, u16 reason)
471 struct ieee80211_mgmt *mgmt;
473 buf = os_zalloc(sizeof(*mgmt));
475 wpa_printf(MSG_DEBUG, "MLME: failed to allocate buffer for "
480 mgmt = (struct ieee80211_mgmt *) buf;
482 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
483 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
484 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
485 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
486 WLAN_FC_STYPE_DEAUTH);
488 mgmt->u.deauth.reason_code = host_to_le16(reason);
490 ieee80211_sta_tx(wpa_s, buf, len);
495 static void ieee80211_send_disassoc(struct wpa_supplicant *wpa_s, u16 reason)
499 struct ieee80211_mgmt *mgmt;
501 buf = os_zalloc(sizeof(*mgmt));
503 wpa_printf(MSG_DEBUG, "MLME: failed to allocate buffer for "
508 mgmt = (struct ieee80211_mgmt *) buf;
510 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
511 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
512 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
513 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
514 WLAN_FC_STYPE_DISASSOC);
516 mgmt->u.disassoc.reason_code = host_to_le16(reason);
518 ieee80211_sta_tx(wpa_s, buf, len);
523 static int ieee80211_privacy_mismatch(struct wpa_supplicant *wpa_s)
525 struct ieee80211_sta_bss *bss;
528 if (wpa_s->mlme.mixed_cell ||
529 wpa_s->mlme.key_mgmt != KEY_MGMT_NONE)
532 bss = ieee80211_bss_get(wpa_s, wpa_s->bssid);
536 if (ieee80211_sta_wep_configured(wpa_s) !=
537 !!(bss->capability & WLAN_CAPABILITY_PRIVACY))
544 static void ieee80211_associate(struct wpa_supplicant *wpa_s)
546 wpa_s->mlme.assoc_tries++;
547 if (wpa_s->mlme.assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
548 wpa_printf(MSG_DEBUG, "MLME: association with AP " MACSTR
549 " timed out", MAC2STR(wpa_s->bssid));
553 wpa_s->mlme.state = IEEE80211_ASSOCIATE;
554 wpa_printf(MSG_DEBUG, "MLME: associate with AP " MACSTR,
555 MAC2STR(wpa_s->bssid));
556 if (ieee80211_privacy_mismatch(wpa_s)) {
557 wpa_printf(MSG_DEBUG, "MLME: mismatch in privacy "
558 "configuration and mixed-cell disabled - abort "
563 ieee80211_send_assoc(wpa_s);
565 ieee80211_reschedule_timer(wpa_s, IEEE80211_ASSOC_TIMEOUT);
569 static void ieee80211_associated(struct wpa_supplicant *wpa_s)
573 /* TODO: start monitoring current AP signal quality and number of
574 * missed beacons. Scan other channels every now and then and search
576 /* TODO: remove expired BSSes */
578 wpa_s->mlme.state = IEEE80211_ASSOCIATED;
581 sta = sta_info_get(local, wpa_s->bssid);
583 wpa_printf(MSG_DEBUG "MLME: No STA entry for own AP " MACSTR,
584 MAC2STR(wpa_s->bssid));
588 if (time_after(jiffies,
589 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
590 if (wpa_s->mlme.probereq_poll) {
591 wpa_printf(MSG_DEBUG "MLME: No ProbeResp from "
592 "current AP " MACSTR " - assume "
594 MAC2STR(wpa_s->bssid));
597 ieee80211_send_probe_req(
599 wpa_s->mlme.scan_ssid,
600 wpa_s->mlme.scan_ssid_len);
601 wpa_s->mlme.probereq_poll = 1;
604 wpa_s->mlme.probereq_poll = 0;
605 if (time_after(jiffies, wpa_s->mlme.last_probe +
606 IEEE80211_PROBE_INTERVAL)) {
607 wpa_s->mlme.last_probe = jiffies;
608 ieee80211_send_probe_req(wpa_s->bssid,
610 wpa_s->mlme.ssid_len);
613 sta_info_release(local, sta);
619 wpa_supplicant_event(wpa_s, EVENT_DISASSOC, NULL);
620 ieee80211_reschedule_timer(wpa_s,
621 IEEE80211_MONITORING_INTERVAL +
624 ieee80211_reschedule_timer(wpa_s,
625 IEEE80211_MONITORING_INTERVAL);
630 static void ieee80211_send_probe_req(struct wpa_supplicant *wpa_s,
632 const u8 *ssid, size_t ssid_len)
636 struct ieee80211_mgmt *mgmt;
637 u8 *pos, *supp_rates;
638 u8 *esupp_rates = NULL;
641 buf = os_malloc(sizeof(*mgmt) + 200 + wpa_s->mlme.extra_probe_ie_len);
643 wpa_printf(MSG_DEBUG, "MLME: failed to allocate buffer for "
648 mgmt = (struct ieee80211_mgmt *) buf;
650 os_memset(mgmt, 0, 24);
651 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
652 WLAN_FC_STYPE_PROBE_REQ);
653 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
655 os_memcpy(mgmt->da, dst, ETH_ALEN);
656 os_memcpy(mgmt->bssid, dst, ETH_ALEN);
658 os_memset(mgmt->da, 0xff, ETH_ALEN);
659 os_memset(mgmt->bssid, 0xff, ETH_ALEN);
663 *pos++ = WLAN_EID_SSID;
665 os_memcpy(pos, ssid, ssid_len);
667 supp_rates = buf + len;
669 supp_rates[0] = WLAN_EID_SUPP_RATES;
671 for (i = 0; i < wpa_s->mlme.num_curr_rates; i++) {
672 struct wpa_rate_data *rate = &wpa_s->mlme.curr_rates[i];
677 } else if (supp_rates[1] == 8) {
679 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
681 pos = &esupp_rates[2];
688 *pos++ = rate->rate / 5;
691 if (wpa_s->mlme.extra_probe_ie) {
692 os_memcpy(pos, wpa_s->mlme.extra_probe_ie,
693 wpa_s->mlme.extra_probe_ie_len);
694 len += wpa_s->mlme.extra_probe_ie_len;
697 ieee80211_sta_tx(wpa_s, buf, len);
702 static int ieee80211_sta_wep_configured(struct wpa_supplicant *wpa_s)
705 if (sdata == NULL || sdata->default_key == NULL ||
706 sdata->default_key->alg != ALG_WEP)
715 static void ieee80211_auth_completed(struct wpa_supplicant *wpa_s)
717 wpa_printf(MSG_DEBUG, "MLME: authenticated");
718 wpa_s->mlme.authenticated = 1;
719 ieee80211_associate(wpa_s);
723 static void ieee80211_auth_challenge(struct wpa_supplicant *wpa_s,
724 struct ieee80211_mgmt *mgmt,
726 struct ieee80211_rx_status *rx_status)
729 struct ieee802_11_elems elems;
731 wpa_printf(MSG_DEBUG, "MLME: replying to auth challenge");
732 pos = mgmt->u.auth.variable;
733 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems, 0)
735 wpa_printf(MSG_DEBUG, "MLME: failed to parse Auth(challenge)");
738 if (elems.challenge == NULL) {
739 wpa_printf(MSG_DEBUG, "MLME: no challenge IE in shared key "
743 ieee80211_send_auth(wpa_s, 3, elems.challenge - 2,
744 elems.challenge_len + 2, 1);
748 static void ieee80211_rx_mgmt_auth(struct wpa_supplicant *wpa_s,
749 struct ieee80211_mgmt *mgmt,
751 struct ieee80211_rx_status *rx_status)
753 struct wpa_ssid *ssid = wpa_s->current_ssid;
754 u16 auth_alg, auth_transaction, status_code;
757 adhoc = ssid && ssid->mode == 1;
759 if (wpa_s->mlme.state != IEEE80211_AUTHENTICATE && !adhoc) {
760 wpa_printf(MSG_DEBUG, "MLME: authentication frame received "
761 "from " MACSTR ", but not in authenticate state - "
762 "ignored", MAC2STR(mgmt->sa));
767 wpa_printf(MSG_DEBUG, "MLME: too short (%lu) authentication "
768 "frame received from " MACSTR " - ignored",
769 (unsigned long) len, MAC2STR(mgmt->sa));
773 if (!adhoc && os_memcmp(wpa_s->bssid, mgmt->sa, ETH_ALEN) != 0) {
774 wpa_printf(MSG_DEBUG, "MLME: authentication frame received "
775 "from unknown AP (SA=" MACSTR " BSSID=" MACSTR
777 MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
781 if (adhoc && os_memcmp(wpa_s->bssid, mgmt->bssid, ETH_ALEN) != 0) {
782 wpa_printf(MSG_DEBUG, "MLME: authentication frame received "
783 "from unknown BSSID (SA=" MACSTR " BSSID=" MACSTR
785 MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
789 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
790 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
791 status_code = le_to_host16(mgmt->u.auth.status_code);
793 wpa_printf(MSG_DEBUG, "MLME: RX authentication from " MACSTR
794 " (alg=%d transaction=%d status=%d)",
795 MAC2STR(mgmt->sa), auth_alg, auth_transaction, status_code);
798 /* IEEE 802.11 standard does not require authentication in IBSS
799 * networks and most implementations do not seem to use it.
800 * However, try to reply to authentication attempts if someone
801 * has actually implemented this.
802 * TODO: Could implement shared key authentication. */
803 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
804 wpa_printf(MSG_DEBUG, "MLME: unexpected IBSS "
805 "authentication frame (alg=%d "
807 auth_alg, auth_transaction);
810 ieee80211_send_auth(wpa_s, 2, NULL, 0, 0);
813 if (auth_alg != wpa_s->mlme.auth_alg ||
814 auth_transaction != wpa_s->mlme.auth_transaction) {
815 wpa_printf(MSG_DEBUG, "MLME: unexpected authentication frame "
816 "(alg=%d transaction=%d)",
817 auth_alg, auth_transaction);
821 if (status_code != WLAN_STATUS_SUCCESS) {
822 wpa_printf(MSG_DEBUG, "MLME: AP denied authentication "
823 "(auth_alg=%d code=%d)", wpa_s->mlme.auth_alg,
825 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
826 const int num_algs = 3;
829 algs[0] = algs[1] = algs[2] = 0xff;
830 if (wpa_s->mlme.auth_algs & IEEE80211_AUTH_ALG_OPEN)
831 algs[0] = WLAN_AUTH_OPEN;
832 if (wpa_s->mlme.auth_algs &
833 IEEE80211_AUTH_ALG_SHARED_KEY)
834 algs[1] = WLAN_AUTH_SHARED_KEY;
835 if (wpa_s->mlme.auth_algs & IEEE80211_AUTH_ALG_LEAP)
836 algs[2] = WLAN_AUTH_LEAP;
837 if (wpa_s->mlme.auth_alg == WLAN_AUTH_OPEN)
839 else if (wpa_s->mlme.auth_alg == WLAN_AUTH_SHARED_KEY)
843 for (i = 0; i < num_algs; i++) {
847 if (algs[pos] == wpa_s->mlme.auth_alg ||
850 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
851 !ieee80211_sta_wep_configured(wpa_s))
853 wpa_s->mlme.auth_alg = algs[pos];
854 wpa_printf(MSG_DEBUG, "MLME: set auth_alg=%d "
856 wpa_s->mlme.auth_alg);
863 switch (wpa_s->mlme.auth_alg) {
866 ieee80211_auth_completed(wpa_s);
868 case WLAN_AUTH_SHARED_KEY:
869 if (wpa_s->mlme.auth_transaction == 4)
870 ieee80211_auth_completed(wpa_s);
872 ieee80211_auth_challenge(wpa_s, mgmt, len,
875 #ifdef CONFIG_IEEE80211R
878 union wpa_event_data data;
879 os_memset(&data, 0, sizeof(data));
880 data.ft_ies.ies = mgmt->u.auth.variable;
881 data.ft_ies.ies_len = len -
882 (mgmt->u.auth.variable - (u8 *) mgmt);
883 os_memcpy(data.ft_ies.target_ap, wpa_s->bssid, ETH_ALEN);
884 wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &data);
885 ieee80211_auth_completed(wpa_s);
888 #endif /* CONFIG_IEEE80211R */
893 static void ieee80211_rx_mgmt_deauth(struct wpa_supplicant *wpa_s,
894 struct ieee80211_mgmt *mgmt,
896 struct ieee80211_rx_status *rx_status)
901 wpa_printf(MSG_DEBUG, "MLME: too short (%lu) deauthentication "
902 "frame received from " MACSTR " - ignored",
903 (unsigned long) len, MAC2STR(mgmt->sa));
907 if (os_memcmp(wpa_s->bssid, mgmt->sa, ETH_ALEN) != 0) {
908 wpa_printf(MSG_DEBUG, "MLME: deauthentication frame received "
909 "from unknown AP (SA=" MACSTR " BSSID=" MACSTR
911 MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
915 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
917 wpa_printf(MSG_DEBUG, "MLME: RX deauthentication from " MACSTR
918 " (reason=%d)", MAC2STR(mgmt->sa), reason_code);
920 if (wpa_s->mlme.authenticated)
921 wpa_printf(MSG_DEBUG, "MLME: deauthenticated");
923 if (wpa_s->mlme.state == IEEE80211_AUTHENTICATE ||
924 wpa_s->mlme.state == IEEE80211_ASSOCIATE ||
925 wpa_s->mlme.state == IEEE80211_ASSOCIATED) {
926 wpa_s->mlme.state = IEEE80211_AUTHENTICATE;
927 ieee80211_reschedule_timer(wpa_s,
928 IEEE80211_RETRY_AUTH_INTERVAL);
931 ieee80211_set_associated(wpa_s, 0);
932 wpa_s->mlme.authenticated = 0;
936 static void ieee80211_rx_mgmt_disassoc(struct wpa_supplicant *wpa_s,
937 struct ieee80211_mgmt *mgmt,
939 struct ieee80211_rx_status *rx_status)
944 wpa_printf(MSG_DEBUG, "MLME: too short (%lu) disassociation "
945 "frame received from " MACSTR " - ignored",
946 (unsigned long) len, MAC2STR(mgmt->sa));
950 if (os_memcmp(wpa_s->bssid, mgmt->sa, ETH_ALEN) != 0) {
951 wpa_printf(MSG_DEBUG, "MLME: disassociation frame received "
952 "from unknown AP (SA=" MACSTR " BSSID=" MACSTR
954 MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
958 reason_code = le_to_host16(mgmt->u.disassoc.reason_code);
960 wpa_printf(MSG_DEBUG, "MLME: RX disassociation from " MACSTR
961 " (reason=%d)", MAC2STR(mgmt->sa), reason_code);
963 if (wpa_s->mlme.associated)
964 wpa_printf(MSG_DEBUG, "MLME: disassociated");
966 if (wpa_s->mlme.state == IEEE80211_ASSOCIATED) {
967 wpa_s->mlme.state = IEEE80211_ASSOCIATE;
968 ieee80211_reschedule_timer(wpa_s,
969 IEEE80211_RETRY_AUTH_INTERVAL);
972 ieee80211_set_associated(wpa_s, 0);
976 static int ieee80211_ft_assoc_resp(struct wpa_supplicant *wpa_s,
977 struct ieee802_11_elems *elems)
979 #ifdef CONFIG_IEEE80211R
980 const u8 *mobility_domain = NULL;
981 const u8 *r0kh_id = NULL;
982 size_t r0kh_id_len = 0;
983 const u8 *r1kh_id = NULL;
984 struct rsn_ftie *hdr;
987 if (elems->mdie && elems->mdie_len >= MOBILITY_DOMAIN_ID_LEN)
988 mobility_domain = elems->mdie;
989 if (elems->ftie && elems->ftie_len >= sizeof(struct rsn_ftie)) {
990 end = elems->ftie + elems->ftie_len;
991 hdr = (struct rsn_ftie *) elems->ftie;
992 pos = (const u8 *) (hdr + 1);
993 while (pos + 1 < end) {
994 if (pos + 2 + pos[1] > end)
996 if (pos[0] == FTIE_SUBELEM_R1KH_ID &&
997 pos[1] == FT_R1KH_ID_LEN)
999 else if (pos[0] == FTIE_SUBELEM_R0KH_ID &&
1000 pos[1] >= 1 && pos[1] <= FT_R0KH_ID_MAX_LEN) {
1002 r0kh_id_len = pos[1];
1007 return wpa_sm_set_ft_params(wpa_s->wpa, mobility_domain, r0kh_id,
1008 r0kh_id_len, r1kh_id);
1009 #else /* CONFIG_IEEE80211R */
1011 #endif /* CONFIG_IEEE80211R */
1015 static void ieee80211_rx_mgmt_assoc_resp(struct wpa_supplicant *wpa_s,
1016 struct ieee80211_mgmt *mgmt,
1018 struct ieee80211_rx_status *rx_status,
1023 u16 capab_info, status_code, aid;
1024 struct ieee802_11_elems elems;
1027 /* AssocResp and ReassocResp have identical structure, so process both
1028 * of them in this function. */
1030 if (wpa_s->mlme.state != IEEE80211_ASSOCIATE) {
1031 wpa_printf(MSG_DEBUG, "MLME: association frame received from "
1032 MACSTR ", but not in associate state - ignored",
1038 wpa_printf(MSG_DEBUG, "MLME: too short (%lu) association "
1039 "frame received from " MACSTR " - ignored",
1040 (unsigned long) len, MAC2STR(mgmt->sa));
1044 if (os_memcmp(wpa_s->bssid, mgmt->sa, ETH_ALEN) != 0) {
1045 wpa_printf(MSG_DEBUG, "MLME: association frame received from "
1046 "unknown AP (SA=" MACSTR " BSSID=" MACSTR ") - "
1047 "ignored", MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
1051 capab_info = le_to_host16(mgmt->u.assoc_resp.capab_info);
1052 status_code = le_to_host16(mgmt->u.assoc_resp.status_code);
1053 aid = le_to_host16(mgmt->u.assoc_resp.aid);
1054 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1055 wpa_printf(MSG_DEBUG, "MLME: invalid aid value %d; bits 15:14 "
1057 aid &= ~(BIT(15) | BIT(14));
1059 wpa_printf(MSG_DEBUG, "MLME: RX %sssocResp from " MACSTR
1060 " (capab=0x%x status=%d aid=%d)",
1061 reassoc ? "Rea" : "A", MAC2STR(mgmt->sa),
1062 capab_info, status_code, aid);
1064 pos = mgmt->u.assoc_resp.variable;
1065 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems, 0)
1067 wpa_printf(MSG_DEBUG, "MLME: failed to parse AssocResp");
1071 if (status_code != WLAN_STATUS_SUCCESS) {
1072 wpa_printf(MSG_DEBUG, "MLME: AP denied association (code=%d)",
1074 #ifdef CONFIG_IEEE80211W
1075 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
1076 elems.timeout_int && elems.timeout_int_len == 5 &&
1077 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
1079 tu = WPA_GET_LE32(elems.timeout_int + 1);
1080 ms = tu * 1024 / 1000;
1081 wpa_printf(MSG_DEBUG, "MLME: AP rejected association "
1082 "temporarily; comeback duration %u TU "
1084 if (ms > IEEE80211_ASSOC_TIMEOUT) {
1085 wpa_printf(MSG_DEBUG, "MLME: Update timer "
1086 "based on comeback duration");
1087 ieee80211_reschedule_timer(wpa_s, ms);
1090 #endif /* CONFIG_IEEE80211W */
1094 if (elems.supp_rates == NULL) {
1095 wpa_printf(MSG_DEBUG, "MLME: no SuppRates element in "
1100 if (wpa_s->mlme.auth_alg == WLAN_AUTH_FT) {
1102 wpa_printf(MSG_DEBUG, "MLME: AP tried to use "
1103 "association, not reassociation, response "
1107 if (wpa_ft_validate_reassoc_resp(
1108 wpa_s->wpa, pos, len - (pos - (u8 *) mgmt),
1110 wpa_printf(MSG_DEBUG, "MLME: FT validation of Reassoc"
1114 } else if (ieee80211_ft_assoc_resp(wpa_s, &elems) < 0)
1117 wpa_printf(MSG_DEBUG, "MLME: associated");
1118 wpa_s->mlme.aid = aid;
1119 wpa_s->mlme.ap_capab = capab_info;
1121 os_free(wpa_s->mlme.assocresp_ies);
1122 wpa_s->mlme.assocresp_ies_len = len - (pos - (u8 *) mgmt);
1123 wpa_s->mlme.assocresp_ies = os_malloc(wpa_s->mlme.assocresp_ies_len);
1124 if (wpa_s->mlme.assocresp_ies) {
1125 os_memcpy(wpa_s->mlme.assocresp_ies, pos,
1126 wpa_s->mlme.assocresp_ies_len);
1129 ieee80211_set_associated(wpa_s, 1);
1131 rates_len = elems.supp_rates_len;
1132 if (rates_len > sizeof(rates))
1133 rates_len = sizeof(rates);
1134 os_memcpy(rates, elems.supp_rates, rates_len);
1135 if (elems.ext_supp_rates) {
1136 size_t _len = elems.ext_supp_rates_len;
1137 if (_len > sizeof(rates) - rates_len)
1138 _len = sizeof(rates) - rates_len;
1139 os_memcpy(rates + rates_len, elems.ext_supp_rates, _len);
1143 if (wpa_drv_set_bssid(wpa_s, wpa_s->bssid) < 0) {
1144 wpa_printf(MSG_DEBUG, "MLME: failed to set BSSID for the "
1147 if (wpa_drv_set_ssid(wpa_s, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len) <
1149 wpa_printf(MSG_DEBUG, "MLME: failed to set SSID for the "
1153 /* Remove STA entry before adding a new one just in case to avoid
1154 * problems with existing configuration (e.g., keys). */
1155 wpa_drv_mlme_remove_sta(wpa_s, wpa_s->bssid);
1156 if (wpa_drv_mlme_add_sta(wpa_s, wpa_s->bssid, rates, rates_len) < 0) {
1157 wpa_printf(MSG_DEBUG, "MLME: failed to add STA entry to the "
1161 if (elems.wmm && wpa_s->mlme.wmm_enabled)
1162 ieee80211_sta_wmm_params(wpa_s, elems.wmm, elems.wmm_len);
1164 ieee80211_associated(wpa_s);
1168 /* Caller must hold local->sta_bss_lock */
1169 static void __ieee80211_bss_hash_add(struct wpa_supplicant *wpa_s,
1170 struct ieee80211_sta_bss *bss)
1172 bss->hnext = wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)];
1173 wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1177 /* Caller must hold local->sta_bss_lock */
1178 static void __ieee80211_bss_hash_del(struct wpa_supplicant *wpa_s,
1179 struct ieee80211_sta_bss *bss)
1181 struct ieee80211_sta_bss *b, *prev = NULL;
1182 b = wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)];
1186 wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)]
1189 prev->hnext = bss->hnext;
1199 static struct ieee80211_sta_bss *
1200 ieee80211_bss_add(struct wpa_supplicant *wpa_s, const u8 *bssid)
1202 struct ieee80211_sta_bss *bss;
1204 bss = os_zalloc(sizeof(*bss));
1207 os_memcpy(bss->bssid, bssid, ETH_ALEN);
1209 /* TODO: order by RSSI? */
1210 bss->next = wpa_s->mlme.sta_bss_list;
1211 wpa_s->mlme.sta_bss_list = bss;
1212 __ieee80211_bss_hash_add(wpa_s, bss);
1217 static struct ieee80211_sta_bss *
1218 ieee80211_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid)
1220 struct ieee80211_sta_bss *bss;
1222 bss = wpa_s->mlme.sta_bss_hash[STA_HASH(bssid)];
1224 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
1232 static void ieee80211_bss_free(struct wpa_supplicant *wpa_s,
1233 struct ieee80211_sta_bss *bss)
1235 __ieee80211_bss_hash_del(wpa_s, bss);
1237 os_free(bss->wpa_ie);
1238 os_free(bss->rsn_ie);
1239 os_free(bss->wmm_ie);
1245 static void ieee80211_bss_list_deinit(struct wpa_supplicant *wpa_s)
1247 struct ieee80211_sta_bss *bss, *prev;
1249 bss = wpa_s->mlme.sta_bss_list;
1250 wpa_s->mlme.sta_bss_list = NULL;
1254 ieee80211_bss_free(wpa_s, prev);
1259 static void ieee80211_bss_info(struct wpa_supplicant *wpa_s,
1260 struct ieee80211_mgmt *mgmt,
1262 struct ieee80211_rx_status *rx_status,
1265 struct ieee802_11_elems elems;
1267 int channel, invalid = 0, clen;
1268 struct ieee80211_sta_bss *bss;
1273 if (!beacon && os_memcmp(mgmt->da, wpa_s->own_addr, ETH_ALEN))
1274 return; /* ignore ProbeResp to foreign address */
1277 wpa_printf(MSG_MSGDUMP, "MLME: RX %s from " MACSTR " to " MACSTR,
1278 beacon ? "Beacon" : "Probe Response",
1279 MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
1282 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1286 pos = mgmt->u.beacon.timestamp;
1287 timestamp = WPA_GET_LE64(pos);
1290 if (local->conf.mode == IW_MODE_ADHOC && beacon &&
1291 os_memcmp(mgmt->bssid, local->bssid, ETH_ALEN) == 0) {
1292 #ifdef IEEE80211_IBSS_DEBUG
1293 static unsigned long last_tsf_debug = 0;
1295 if (local->hw->get_tsf)
1296 tsf = local->hw->get_tsf(local->mdev);
1299 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
1300 wpa_printf(MSG_DEBUG, "RX beacon SA=" MACSTR " BSSID="
1301 MACSTR " TSF=0x%llx BCN=0x%llx diff=%lld "
1303 MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid),
1304 tsf, timestamp, tsf - timestamp, jiffies);
1305 last_tsf_debug = jiffies;
1307 #endif /* IEEE80211_IBSS_DEBUG */
1311 ie_pos = mgmt->u.beacon.variable;
1312 ie_len = len - baselen;
1313 if (ieee802_11_parse_elems(ie_pos, ie_len, &elems, 0) == ParseFailed)
1317 if (local->conf.mode == IW_MODE_ADHOC && elems.supp_rates &&
1318 os_memcmp(mgmt->bssid, local->bssid, ETH_ALEN) == 0 &&
1319 (sta = sta_info_get(local, mgmt->sa))) {
1320 struct ieee80211_rate *rates;
1322 u32 supp_rates, prev_rates;
1323 int i, j, oper_mode;
1325 rates = local->curr_rates;
1326 num_rates = local->num_curr_rates;
1327 oper_mode = wpa_s->mlme.sta_scanning ?
1328 local->scan_oper_phymode : local->conf.phymode;
1329 for (i = 0; i < local->hw->num_modes; i++) {
1330 struct ieee80211_hw_modes *mode = &local->hw->modes[i];
1331 if (oper_mode == mode->mode) {
1332 rates = mode->rates;
1333 num_rates = mode->num_rates;
1339 for (i = 0; i < elems.supp_rates_len +
1340 elems.ext_supp_rates_len; i++) {
1343 if (i < elems.supp_rates_len)
1344 rate = elems.supp_rates[i];
1345 else if (elems.ext_supp_rates)
1346 rate = elems.ext_supp_rates
1347 [i - elems.supp_rates_len];
1348 own_rate = 5 * (rate & 0x7f);
1349 if (oper_mode == MODE_ATHEROS_TURBO)
1351 for (j = 0; j < num_rates; j++)
1352 if (rates[j].rate == own_rate)
1353 supp_rates |= BIT(j);
1356 prev_rates = sta->supp_rates;
1357 sta->supp_rates &= supp_rates;
1358 if (sta->supp_rates == 0) {
1359 /* No matching rates - this should not really happen.
1360 * Make sure that at least one rate is marked
1361 * supported to avoid issues with TX rate ctrl. */
1362 sta->supp_rates = wpa_s->mlme.supp_rates_bits;
1364 if (sta->supp_rates != prev_rates) {
1365 wpa_printf(MSG_DEBUG, "MLME: updated supp_rates set "
1366 "for " MACSTR " based on beacon info "
1367 "(0x%x & 0x%x -> 0x%x)",
1368 MAC2STR(sta->addr), prev_rates,
1369 supp_rates, sta->supp_rates);
1371 sta_info_release(local, sta);
1375 if (elems.ssid == NULL)
1378 if (elems.ds_params && elems.ds_params_len == 1)
1379 channel = elems.ds_params[0];
1381 channel = rx_status->channel;
1383 bss = ieee80211_bss_get(wpa_s, mgmt->bssid);
1385 bss = ieee80211_bss_add(wpa_s, mgmt->bssid);
1390 /* TODO: order by RSSI? */
1391 spin_lock_bh(&local->sta_bss_lock);
1392 list_move_tail(&bss->list, &local->sta_bss_list);
1393 spin_unlock_bh(&local->sta_bss_lock);
1397 if (bss->probe_resp && beacon) {
1398 /* Do not allow beacon to override data from Probe Response. */
1402 bss->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
1403 bss->capability = le_to_host16(mgmt->u.beacon.capab_info);
1405 if (bss->ie == NULL || bss->ie_len < ie_len) {
1407 bss->ie = os_malloc(ie_len);
1410 os_memcpy(bss->ie, ie_pos, ie_len);
1411 bss->ie_len = ie_len;
1414 if (elems.ssid && elems.ssid_len <= MAX_SSID_LEN) {
1415 os_memcpy(bss->ssid, elems.ssid, elems.ssid_len);
1416 bss->ssid_len = elems.ssid_len;
1419 bss->supp_rates_len = 0;
1420 if (elems.supp_rates) {
1421 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1422 if (clen > elems.supp_rates_len)
1423 clen = elems.supp_rates_len;
1424 os_memcpy(&bss->supp_rates[bss->supp_rates_len],
1425 elems.supp_rates, clen);
1426 bss->supp_rates_len += clen;
1428 if (elems.ext_supp_rates) {
1429 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1430 if (clen > elems.ext_supp_rates_len)
1431 clen = elems.ext_supp_rates_len;
1432 os_memcpy(&bss->supp_rates[bss->supp_rates_len],
1433 elems.ext_supp_rates, clen);
1434 bss->supp_rates_len += clen;
1438 (bss->wpa_ie == NULL || bss->wpa_ie_len != elems.wpa_ie_len ||
1439 os_memcmp(bss->wpa_ie, elems.wpa_ie, elems.wpa_ie_len))) {
1440 os_free(bss->wpa_ie);
1441 bss->wpa_ie = os_malloc(elems.wpa_ie_len + 2);
1443 os_memcpy(bss->wpa_ie, elems.wpa_ie - 2,
1444 elems.wpa_ie_len + 2);
1445 bss->wpa_ie_len = elems.wpa_ie_len + 2;
1447 bss->wpa_ie_len = 0;
1448 } else if (!elems.wpa_ie && bss->wpa_ie) {
1449 os_free(bss->wpa_ie);
1451 bss->wpa_ie_len = 0;
1455 (bss->rsn_ie == NULL || bss->rsn_ie_len != elems.rsn_ie_len ||
1456 os_memcmp(bss->rsn_ie, elems.rsn_ie, elems.rsn_ie_len))) {
1457 os_free(bss->rsn_ie);
1458 bss->rsn_ie = os_malloc(elems.rsn_ie_len + 2);
1460 os_memcpy(bss->rsn_ie, elems.rsn_ie - 2,
1461 elems.rsn_ie_len + 2);
1462 bss->rsn_ie_len = elems.rsn_ie_len + 2;
1464 bss->rsn_ie_len = 0;
1465 } else if (!elems.rsn_ie && bss->rsn_ie) {
1466 os_free(bss->rsn_ie);
1468 bss->rsn_ie_len = 0;
1472 (bss->wmm_ie == NULL || bss->wmm_ie_len != elems.wmm_len ||
1473 os_memcmp(bss->wmm_ie, elems.wmm, elems.wmm_len))) {
1474 os_free(bss->wmm_ie);
1475 bss->wmm_ie = os_malloc(elems.wmm_len + 2);
1477 os_memcpy(bss->wmm_ie, elems.wmm - 2,
1479 bss->wmm_ie_len = elems.wmm_len + 2;
1481 bss->wmm_ie_len = 0;
1482 } else if (!elems.wmm && bss->wmm_ie) {
1483 os_free(bss->wmm_ie);
1485 bss->wmm_ie_len = 0;
1488 #ifdef CONFIG_IEEE80211R
1490 (bss->mdie == NULL || bss->mdie_len != elems.mdie_len ||
1491 os_memcmp(bss->mdie, elems.mdie, elems.mdie_len))) {
1493 bss->mdie = os_malloc(elems.mdie_len + 2);
1495 os_memcpy(bss->mdie, elems.mdie - 2,
1496 elems.mdie_len + 2);
1497 bss->mdie_len = elems.mdie_len + 2;
1500 } else if (!elems.mdie && bss->mdie) {
1505 #endif /* CONFIG_IEEE80211R */
1507 bss->hw_mode = wpa_s->mlme.phymode;
1508 bss->channel = channel;
1509 bss->freq = wpa_s->mlme.freq;
1510 if (channel != wpa_s->mlme.channel &&
1511 (wpa_s->mlme.phymode == WPA_MODE_IEEE80211G ||
1512 wpa_s->mlme.phymode == WPA_MODE_IEEE80211B) &&
1513 channel >= 1 && channel <= 14) {
1514 static const int freq_list[] = {
1515 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1516 2447, 2452, 2457, 2462, 2467, 2472, 2484
1518 /* IEEE 802.11g/b mode can receive packets from neighboring
1519 * channels, so map the channel into frequency. */
1520 bss->freq = freq_list[channel - 1];
1522 bss->timestamp = timestamp;
1523 os_get_time(&bss->last_update);
1524 bss->rssi = rx_status->ssi;
1530 static void ieee80211_rx_mgmt_probe_resp(struct wpa_supplicant *wpa_s,
1531 struct ieee80211_mgmt *mgmt,
1533 struct ieee80211_rx_status *rx_status)
1535 ieee80211_bss_info(wpa_s, mgmt, len, rx_status, 0);
1539 static void ieee80211_rx_mgmt_beacon(struct wpa_supplicant *wpa_s,
1540 struct ieee80211_mgmt *mgmt,
1542 struct ieee80211_rx_status *rx_status)
1546 struct ieee802_11_elems elems;
1548 ieee80211_bss_info(wpa_s, mgmt, len, rx_status, 1);
1550 if (!wpa_s->mlme.associated ||
1551 os_memcmp(wpa_s->bssid, mgmt->bssid, ETH_ALEN) != 0)
1554 /* Process beacon from the current BSS */
1555 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1559 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1560 &elems, 0) == ParseFailed)
1564 if (elems.erp_info && elems.erp_info_len >= 1) {
1566 (elems.erp_info[0] & ERP_INFO_USE_PROTECTION) != 0;
1569 if (use_protection != !!wpa_s->mlme.use_protection) {
1570 wpa_printf(MSG_DEBUG, "MLME: CTS protection %s (BSSID=" MACSTR
1572 use_protection ? "enabled" : "disabled",
1573 MAC2STR(wpa_s->bssid));
1574 wpa_s->mlme.use_protection = use_protection ? 1 : 0;
1575 wpa_s->mlme.cts_protect_erp_frames = use_protection;
1578 if (elems.wmm && wpa_s->mlme.wmm_enabled) {
1579 ieee80211_sta_wmm_params(wpa_s, elems.wmm,
1585 static void ieee80211_rx_mgmt_probe_req(struct wpa_supplicant *wpa_s,
1586 struct ieee80211_mgmt *mgmt,
1588 struct ieee80211_rx_status *rx_status)
1590 int tx_last_beacon, adhoc;
1592 struct ieee80211_mgmt *resp;
1595 struct wpa_ssid *ssid = wpa_s->current_ssid;
1597 adhoc = ssid && ssid->mode == 1;
1599 if (!adhoc || wpa_s->mlme.state != IEEE80211_IBSS_JOINED ||
1600 len < 24 + 2 || wpa_s->mlme.probe_resp == NULL)
1604 if (local->hw->tx_last_beacon)
1605 tx_last_beacon = local->hw->tx_last_beacon(local->mdev);
1610 #ifdef IEEE80211_IBSS_DEBUG
1611 wpa_printf(MSG_DEBUG, "MLME: RX ProbeReq SA=" MACSTR " DA=" MACSTR
1612 " BSSID=" MACSTR " (tx_last_beacon=%d)",
1613 MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
1614 MAC2STR(mgmt->bssid), tx_last_beacon);
1615 #endif /* IEEE80211_IBSS_DEBUG */
1617 if (!tx_last_beacon)
1620 if (os_memcmp(mgmt->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1621 os_memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1624 end = ((u8 *) mgmt) + len;
1625 pos = mgmt->u.probe_req.variable;
1626 if (pos[0] != WLAN_EID_SSID ||
1627 pos + 2 + pos[1] > end) {
1628 wpa_printf(MSG_DEBUG, "MLME: Invalid SSID IE in ProbeReq from "
1629 MACSTR, MAC2STR(mgmt->sa));
1633 (pos[1] != wpa_s->mlme.ssid_len ||
1634 os_memcmp(pos + 2, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len) != 0))
1636 /* Ignore ProbeReq for foreign SSID */
1641 /* Reply with ProbeResp */
1642 skb = skb_copy(wpa_s->mlme.probe_resp, GFP_ATOMIC);
1646 resp = (struct ieee80211_mgmt *) skb->data;
1647 os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
1648 #ifdef IEEE80211_IBSS_DEBUG
1649 wpa_printf(MSG_DEBUG, "MLME: Sending ProbeResp to " MACSTR,
1651 #endif /* IEEE80211_IBSS_DEBUG */
1652 ieee80211_sta_tx(wpa_s, skb, 0, 1);
1657 #ifdef CONFIG_IEEE80211R
1658 static void ieee80211_rx_mgmt_ft_action(struct wpa_supplicant *wpa_s,
1659 struct ieee80211_mgmt *mgmt,
1661 struct ieee80211_rx_status *rx_status)
1663 union wpa_event_data data;
1665 u8 *sta_addr, *target_ap_addr;
1667 if (len < 24 + 1 + sizeof(mgmt->u.action.u.ft_action_resp)) {
1668 wpa_printf(MSG_DEBUG, "MLME: Too short FT Action frame");
1673 * Only FT Action Response is needed for now since reservation
1674 * protocol is not supported.
1676 if (mgmt->u.action.u.ft_action_resp.action != 2) {
1677 wpa_printf(MSG_DEBUG, "MLME: Unexpected FT Action %d",
1678 mgmt->u.action.u.ft_action_resp.action);
1682 status = le_to_host16(mgmt->u.action.u.ft_action_resp.status_code);
1683 sta_addr = mgmt->u.action.u.ft_action_resp.sta_addr;
1684 target_ap_addr = mgmt->u.action.u.ft_action_resp.target_ap_addr;
1685 wpa_printf(MSG_DEBUG, "MLME: Received FT Action Response: STA " MACSTR
1686 " TargetAP " MACSTR " Status Code %d",
1687 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
1688 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
1689 wpa_printf(MSG_DEBUG, "MLME: Foreign STA Address " MACSTR
1690 " in FT Action Response", MAC2STR(sta_addr));
1696 wpa_printf(MSG_DEBUG, "MLME: FT Action Response indicates "
1697 "failure (status code %d)", status);
1698 /* TODO: report error to FT code(?) */
1702 os_memset(&data, 0, sizeof(data));
1703 data.ft_ies.ies = mgmt->u.action.u.ft_action_resp.variable;
1704 data.ft_ies.ies_len = len - (mgmt->u.action.u.ft_action_resp.variable -
1706 data.ft_ies.ft_action = 1;
1707 os_memcpy(data.ft_ies.target_ap, target_ap_addr, ETH_ALEN);
1708 wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &data);
1709 /* TODO: should only re-associate, if EVENT_FT_RESPONSE was processed
1711 wpa_s->mlme.prev_bssid_set = 1;
1712 wpa_s->mlme.auth_alg = WLAN_AUTH_FT;
1713 os_memcpy(wpa_s->mlme.prev_bssid, wpa_s->bssid, ETH_ALEN);
1714 os_memcpy(wpa_s->bssid, target_ap_addr, ETH_ALEN);
1715 ieee80211_associate(wpa_s);
1717 #endif /* CONFIG_IEEE80211R */
1720 #ifdef CONFIG_IEEE80211W
1722 /* MLME-SAQuery.response */
1723 static int ieee80211_sta_send_sa_query_resp(struct wpa_supplicant *wpa_s,
1724 const u8 *addr, const u8 *trans_id)
1726 struct ieee80211_mgmt *mgmt;
1730 mgmt = os_zalloc(sizeof(*mgmt));
1732 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
1733 "SA Query action frame");
1738 os_memcpy(mgmt->da, addr, ETH_ALEN);
1739 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
1740 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
1741 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1742 WLAN_FC_STYPE_ACTION);
1743 mgmt->u.action.category = WLAN_ACTION_SA_QUERY;
1744 mgmt->u.action.u.sa_query_resp.action = WLAN_SA_QUERY_RESPONSE;
1745 os_memcpy(mgmt->u.action.u.sa_query_resp.trans_id, trans_id,
1746 WLAN_SA_QUERY_TR_ID_LEN);
1747 len += 1 + sizeof(mgmt->u.action.u.sa_query_resp);
1749 res = ieee80211_sta_tx(wpa_s, (u8 *) mgmt, len);
1756 static void ieee80211_rx_mgmt_sa_query_action(
1757 struct wpa_supplicant *wpa_s, struct ieee80211_mgmt *mgmt, size_t len,
1758 struct ieee80211_rx_status *rx_status)
1760 if (len < 24 + 1 + sizeof(mgmt->u.action.u.sa_query_req)) {
1761 wpa_printf(MSG_DEBUG, "MLME: Too short SA Query Action frame");
1765 if (mgmt->u.action.u.sa_query_req.action != WLAN_SA_QUERY_REQUEST) {
1766 wpa_printf(MSG_DEBUG, "MLME: Unexpected SA Query Action %d",
1767 mgmt->u.action.u.sa_query_req.action);
1771 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
1772 wpa_printf(MSG_DEBUG, "MLME: Ignore SA Query from unknown "
1773 "source " MACSTR, MAC2STR(mgmt->sa));
1777 if (wpa_s->mlme.state == IEEE80211_ASSOCIATE) {
1778 wpa_printf(MSG_DEBUG, "MLME: Ignore SA query request during "
1779 "association process");
1783 wpa_printf(MSG_DEBUG, "MLME: Replying to SA Query request");
1784 ieee80211_sta_send_sa_query_resp(wpa_s, mgmt->sa, mgmt->u.action.u.
1785 sa_query_req.trans_id);
1788 #endif /* CONFIG_IEEE80211W */
1791 static void ieee80211_rx_mgmt_action(struct wpa_supplicant *wpa_s,
1792 struct ieee80211_mgmt *mgmt,
1794 struct ieee80211_rx_status *rx_status)
1796 wpa_printf(MSG_DEBUG, "MLME: received Action frame");
1801 switch (mgmt->u.action.category) {
1802 #ifdef CONFIG_IEEE80211R
1803 case WLAN_ACTION_FT:
1804 ieee80211_rx_mgmt_ft_action(wpa_s, mgmt, len, rx_status);
1806 #endif /* CONFIG_IEEE80211R */
1807 #ifdef CONFIG_IEEE80211W
1808 case WLAN_ACTION_SA_QUERY:
1809 ieee80211_rx_mgmt_sa_query_action(wpa_s, mgmt, len, rx_status);
1811 #endif /* CONFIG_IEEE80211W */
1813 wpa_printf(MSG_DEBUG, "MLME: unknown Action Category %d",
1814 mgmt->u.action.category);
1820 static void ieee80211_sta_rx_mgmt(struct wpa_supplicant *wpa_s,
1821 const u8 *buf, size_t len,
1822 struct ieee80211_rx_status *rx_status)
1824 struct ieee80211_mgmt *mgmt;
1830 mgmt = (struct ieee80211_mgmt *) buf;
1831 fc = le_to_host16(mgmt->frame_control);
1833 switch (WLAN_FC_GET_STYPE(fc)) {
1834 case WLAN_FC_STYPE_PROBE_REQ:
1835 ieee80211_rx_mgmt_probe_req(wpa_s, mgmt, len, rx_status);
1837 case WLAN_FC_STYPE_PROBE_RESP:
1838 ieee80211_rx_mgmt_probe_resp(wpa_s, mgmt, len, rx_status);
1840 case WLAN_FC_STYPE_BEACON:
1841 ieee80211_rx_mgmt_beacon(wpa_s, mgmt, len, rx_status);
1843 case WLAN_FC_STYPE_AUTH:
1844 ieee80211_rx_mgmt_auth(wpa_s, mgmt, len, rx_status);
1846 case WLAN_FC_STYPE_ASSOC_RESP:
1847 ieee80211_rx_mgmt_assoc_resp(wpa_s, mgmt, len, rx_status, 0);
1849 case WLAN_FC_STYPE_REASSOC_RESP:
1850 ieee80211_rx_mgmt_assoc_resp(wpa_s, mgmt, len, rx_status, 1);
1852 case WLAN_FC_STYPE_DEAUTH:
1853 ieee80211_rx_mgmt_deauth(wpa_s, mgmt, len, rx_status);
1855 case WLAN_FC_STYPE_DISASSOC:
1856 ieee80211_rx_mgmt_disassoc(wpa_s, mgmt, len, rx_status);
1858 case WLAN_FC_STYPE_ACTION:
1859 ieee80211_rx_mgmt_action(wpa_s, mgmt, len, rx_status);
1862 wpa_printf(MSG_DEBUG, "MLME: received unknown management "
1863 "frame - stype=%d", WLAN_FC_GET_STYPE(fc));
1869 static void ieee80211_sta_rx_scan(struct wpa_supplicant *wpa_s,
1870 const u8 *buf, size_t len,
1871 struct ieee80211_rx_status *rx_status)
1873 struct ieee80211_mgmt *mgmt;
1879 mgmt = (struct ieee80211_mgmt *) buf;
1880 fc = le_to_host16(mgmt->frame_control);
1882 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT) {
1883 if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
1884 ieee80211_rx_mgmt_probe_resp(wpa_s, mgmt,
1886 } else if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON) {
1887 ieee80211_rx_mgmt_beacon(wpa_s, mgmt, len, rx_status);
1893 static int ieee80211_sta_active_ibss(struct wpa_supplicant *wpa_s)
1898 list_for_each(ptr, &local->sta_list) {
1899 sta = list_entry(ptr, struct sta_info, list);
1900 if (sta->dev == dev &&
1901 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1913 static void ieee80211_sta_expire(struct wpa_supplicant *wpa_s)
1916 list_for_each_safe(ptr, n, &local->sta_list) {
1917 sta = list_entry(ptr, struct sta_info, list);
1918 if (time_after(jiffies, sta->last_rx +
1919 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
1920 wpa_printf(MSG_DEBUG, "MLME: expiring inactive STA "
1921 MACSTR, MAC2STR(sta->addr));
1922 sta_info_free(local, sta, 1);
1929 static void ieee80211_sta_merge_ibss(struct wpa_supplicant *wpa_s)
1931 ieee80211_reschedule_timer(wpa_s, IEEE80211_IBSS_MERGE_INTERVAL);
1933 ieee80211_sta_expire(wpa_s);
1934 if (ieee80211_sta_active_ibss(wpa_s))
1937 wpa_printf(MSG_DEBUG, "MLME: No active IBSS STAs - trying to scan for "
1938 "other IBSS networks with same SSID (merge)");
1939 ieee80211_sta_req_scan(wpa_s, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len);
1943 static void ieee80211_sta_timer(void *eloop_ctx, void *timeout_ctx)
1945 struct wpa_supplicant *wpa_s = eloop_ctx;
1947 switch (wpa_s->mlme.state) {
1948 case IEEE80211_DISABLED:
1950 case IEEE80211_AUTHENTICATE:
1951 ieee80211_authenticate(wpa_s);
1953 case IEEE80211_ASSOCIATE:
1954 ieee80211_associate(wpa_s);
1956 case IEEE80211_ASSOCIATED:
1957 ieee80211_associated(wpa_s);
1959 case IEEE80211_IBSS_SEARCH:
1960 ieee80211_sta_find_ibss(wpa_s);
1962 case IEEE80211_IBSS_JOINED:
1963 ieee80211_sta_merge_ibss(wpa_s);
1966 wpa_printf(MSG_DEBUG, "ieee80211_sta_timer: Unknown state %d",
1971 if (ieee80211_privacy_mismatch(wpa_s)) {
1972 wpa_printf(MSG_DEBUG, "MLME: privacy configuration mismatch "
1973 "and mixed-cell disabled - disassociate");
1975 ieee80211_send_disassoc(wpa_s, WLAN_REASON_UNSPECIFIED);
1976 ieee80211_set_associated(wpa_s, 0);
1981 static void ieee80211_sta_new_auth(struct wpa_supplicant *wpa_s)
1983 struct wpa_ssid *ssid = wpa_s->current_ssid;
1984 if (ssid && ssid->mode != 0)
1988 if (local->hw->reset_tsf) {
1989 /* Reset own TSF to allow time synchronization work. */
1990 local->hw->reset_tsf(local->mdev);
1994 wpa_s->mlme.wmm_last_param_set = -1; /* allow any WMM update */
1997 if (wpa_s->mlme.auth_algs & IEEE80211_AUTH_ALG_OPEN)
1998 wpa_s->mlme.auth_alg = WLAN_AUTH_OPEN;
1999 else if (wpa_s->mlme.auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2000 wpa_s->mlme.auth_alg = WLAN_AUTH_SHARED_KEY;
2001 else if (wpa_s->mlme.auth_algs & IEEE80211_AUTH_ALG_LEAP)
2002 wpa_s->mlme.auth_alg = WLAN_AUTH_LEAP;
2004 wpa_s->mlme.auth_alg = WLAN_AUTH_OPEN;
2005 wpa_printf(MSG_DEBUG, "MLME: Initial auth_alg=%d",
2006 wpa_s->mlme.auth_alg);
2007 wpa_s->mlme.auth_transaction = -1;
2008 wpa_s->mlme.auth_tries = wpa_s->mlme.assoc_tries = 0;
2009 ieee80211_authenticate(wpa_s);
2013 static int ieee80211_ibss_allowed(struct wpa_supplicant *wpa_s)
2018 for (m = 0; m < local->hw->num_modes; m++) {
2019 struct ieee80211_hw_modes *mode = &local->hw->modes[m];
2020 if (mode->mode != local->conf.phymode)
2022 for (c = 0; c < mode->num_channels; c++) {
2023 struct ieee80211_channel *chan = &mode->channels[c];
2024 if (chan->flag & IEEE80211_CHAN_W_SCAN &&
2025 chan->chan == local->conf.channel) {
2026 if (chan->flag & IEEE80211_CHAN_W_IBSS)
2038 static int ieee80211_sta_join_ibss(struct wpa_supplicant *wpa_s,
2039 struct ieee80211_sta_bss *bss)
2041 int res = 0, rates, done = 0;
2042 struct ieee80211_mgmt *mgmt;
2044 struct ieee80211_tx_control control;
2045 struct ieee80211_rate *rate;
2046 struct rate_control_extra extra;
2051 /* Remove possible STA entries from other IBSS networks. */
2053 sta_info_flush(local, NULL);
2055 if (local->hw->reset_tsf) {
2056 /* Reset own TSF to allow time synchronization work. */
2057 local->hw->reset_tsf(local->mdev);
2060 os_memcpy(wpa_s->bssid, bss->bssid, ETH_ALEN);
2063 local->conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2065 sdata->drop_unencrypted = bss->capability &
2066 host_to_le16(WLAN_CAPABILITY_PRIVACY) ? 1 : 0;
2070 os_memset(&rq, 0, sizeof(rq));
2071 rq.m = bss->freq * 100000;
2073 res = ieee80211_ioctl_siwfreq(wpa_s, NULL, &rq, NULL);
2076 if (!ieee80211_ibss_allowed(wpa_s)) {
2078 wpa_printf(MSG_DEBUG, "MLME: IBSS not allowed on channel %d "
2079 "(%d MHz)", local->conf.channel,
2085 /* Set beacon template based on scan results */
2086 buf = os_malloc(400);
2092 mgmt = (struct ieee80211_mgmt *) buf;
2093 len += 24 + sizeof(mgmt->u.beacon);
2094 os_memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2095 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2096 WLAN_FC_STYPE_BEACON);
2097 os_memset(mgmt->da, 0xff, ETH_ALEN);
2098 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
2099 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
2101 mgmt->u.beacon.beacon_int =
2102 host_to_le16(local->conf.beacon_int);
2104 mgmt->u.beacon.capab_info = host_to_le16(bss->capability);
2107 len += 2 + wpa_s->mlme.ssid_len;
2108 *pos++ = WLAN_EID_SSID;
2109 *pos++ = wpa_s->mlme.ssid_len;
2110 os_memcpy(pos, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len);
2112 rates = bss->supp_rates_len;
2117 *pos++ = WLAN_EID_SUPP_RATES;
2119 os_memcpy(pos, bss->supp_rates, rates);
2123 *pos++ = WLAN_EID_DS_PARAMS;
2125 *pos++ = bss->channel;
2129 *pos++ = WLAN_EID_IBSS_PARAMS;
2131 /* FIX: set ATIM window based on scan results */
2135 if (bss->supp_rates_len > 8) {
2136 rates = bss->supp_rates_len - 8;
2139 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2141 os_memcpy(pos, &bss->supp_rates[8], rates);
2145 os_memset(&control, 0, sizeof(control));
2146 control.pkt_type = PKT_PROBE_RESP;
2147 os_memset(&extra, 0, sizeof(extra));
2148 extra.endidx = local->num_curr_rates;
2149 rate = rate_control_get_rate(wpa_s, skb, &extra);
2151 wpa_printf(MSG_DEBUG, "MLME: Failed to determine TX "
2152 "rate for IBSS beacon");
2155 control.tx_rate = (wpa_s->mlme.short_preamble &&
2156 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
2157 rate->val2 : rate->val;
2158 control.antenna_sel = local->conf.antenna_sel;
2159 control.power_level = local->conf.power_level;
2161 control.retry_limit = 1;
2162 control.rts_cts_duration = 0;
2166 wpa_s->mlme.probe_resp = skb_copy(skb, GFP_ATOMIC);
2167 if (wpa_s->mlme.probe_resp) {
2168 mgmt = (struct ieee80211_mgmt *)
2169 wpa_s->mlme.probe_resp->data;
2170 mgmt->frame_control =
2171 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2172 WLAN_FC_STYPE_PROBE_RESP);
2174 wpa_printf(MSG_DEBUG, "MLME: Could not allocate "
2175 "ProbeResp template for IBSS");
2178 if (local->hw->beacon_update &&
2179 local->hw->beacon_update(wpa_s, skb, &control) == 0) {
2180 wpa_printf(MSG_DEBUG, "MLME: Configured IBSS beacon "
2181 "template based on scan results");
2186 for (i = 0; i < bss->supp_rates_len; i++) {
2187 int rate = (bss->supp_rates[i] & 0x7f) * 5;
2188 if (local->conf.phymode == MODE_ATHEROS_TURBO)
2190 for (j = 0; j < local->num_curr_rates; j++)
2191 if (local->curr_rates[j].rate == rate)
2194 wpa_s->mlme.supp_rates_bits = rates;
2201 wpa_printf(MSG_DEBUG, "MLME: Failed to configure IBSS beacon "
2205 wpa_s->mlme.state = IEEE80211_IBSS_JOINED;
2206 ieee80211_reschedule_timer(wpa_s, IEEE80211_IBSS_MERGE_INTERVAL);
2213 static int ieee80211_sta_create_ibss(struct wpa_supplicant *wpa_s)
2215 struct ieee80211_sta_bss *bss;
2216 u8 bssid[ETH_ALEN], *pos;
2220 /* Easier testing, use fixed BSSID. */
2221 os_memset(bssid, 0xfe, ETH_ALEN);
2223 /* Generate random, not broadcast, locally administered BSSID. Mix in
2224 * own MAC address to make sure that devices that do not have proper
2225 * random number generator get different BSSID. */
2226 os_get_random(bssid, ETH_ALEN);
2227 for (i = 0; i < ETH_ALEN; i++)
2228 bssid[i] ^= wpa_s->own_addr[i];
2233 wpa_printf(MSG_DEBUG, "MLME: Creating new IBSS network, BSSID "
2234 MACSTR "", MAC2STR(bssid));
2236 bss = ieee80211_bss_add(wpa_s, bssid);
2241 if (local->conf.beacon_int == 0)
2242 local->conf.beacon_int = 100;
2243 bss->beacon_int = local->conf.beacon_int;
2244 bss->hw_mode = local->conf.phymode;
2245 bss->channel = local->conf.channel;
2246 bss->freq = local->conf.freq;
2248 os_get_time(&bss->last_update);
2249 bss->capability = host_to_le16(WLAN_CAPABILITY_IBSS);
2251 if (sdata->default_key) {
2252 bss->capability |= host_to_le16(WLAN_CAPABILITY_PRIVACY);
2254 sdata->drop_unencrypted = 0;
2255 bss->supp_rates_len = local->num_curr_rates;
2257 pos = bss->supp_rates;
2259 for (i = 0; i < local->num_curr_rates; i++) {
2260 int rate = local->curr_rates[i].rate;
2261 if (local->conf.phymode == MODE_ATHEROS_TURBO)
2263 *pos++ = (u8) (rate / 5);
2267 return ieee80211_sta_join_ibss(wpa_s, bss);
2272 static int ieee80211_sta_find_ibss(struct wpa_supplicant *wpa_s)
2274 struct ieee80211_sta_bss *bss;
2280 if (wpa_s->mlme.ssid_len == 0)
2283 active_ibss = ieee80211_sta_active_ibss(wpa_s);
2284 #ifdef IEEE80211_IBSS_DEBUG
2285 wpa_printf(MSG_DEBUG, "MLME: sta_find_ibss (active_ibss=%d)",
2287 #endif /* IEEE80211_IBSS_DEBUG */
2288 for (bss = wpa_s->mlme.sta_bss_list; bss; bss = bss->next) {
2289 if (wpa_s->mlme.ssid_len != bss->ssid_len ||
2290 os_memcmp(wpa_s->mlme.ssid, bss->ssid, bss->ssid_len) != 0
2291 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2293 #ifdef IEEE80211_IBSS_DEBUG
2294 wpa_printf(MSG_DEBUG, " bssid=" MACSTR " found",
2295 MAC2STR(bss->bssid));
2296 #endif /* IEEE80211_IBSS_DEBUG */
2297 os_memcpy(bssid, bss->bssid, ETH_ALEN);
2300 os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)
2304 #ifdef IEEE80211_IBSS_DEBUG
2305 wpa_printf(MSG_DEBUG, " sta_find_ibss: selected " MACSTR " current "
2306 MACSTR, MAC2STR(bssid), MAC2STR(wpa_s->bssid));
2307 #endif /* IEEE80211_IBSS_DEBUG */
2308 if (found && os_memcmp(wpa_s->bssid, bssid, ETH_ALEN) != 0 &&
2309 (bss = ieee80211_bss_get(wpa_s, bssid))) {
2310 wpa_printf(MSG_DEBUG, "MLME: Selected IBSS BSSID " MACSTR
2311 " based on configured SSID",
2313 return ieee80211_sta_join_ibss(wpa_s, bss);
2315 #ifdef IEEE80211_IBSS_DEBUG
2316 wpa_printf(MSG_DEBUG, " did not try to join ibss");
2317 #endif /* IEEE80211_IBSS_DEBUG */
2319 /* Selected IBSS not found in current scan results - try to scan */
2322 if (wpa_s->mlme.state == IEEE80211_IBSS_JOINED &&
2323 !ieee80211_sta_active_ibss(wpa_s)) {
2324 ieee80211_reschedule_timer(wpa_s,
2325 IEEE80211_IBSS_MERGE_INTERVAL);
2326 } else if (time_after(jiffies, wpa_s->mlme.last_scan_completed +
2327 IEEE80211_SCAN_INTERVAL)) {
2328 wpa_printf(MSG_DEBUG, "MLME: Trigger new scan to find an IBSS "
2330 return ieee80211_sta_req_scan(wpa_s->mlme.ssid,
2331 wpa_s->mlme.ssid_len);
2332 } else if (wpa_s->mlme.state != IEEE80211_IBSS_JOINED) {
2333 int interval = IEEE80211_SCAN_INTERVAL;
2335 if (time_after(jiffies, wpa_s->mlme.ibss_join_req +
2336 IEEE80211_IBSS_JOIN_TIMEOUT)) {
2337 if (wpa_s->mlme.create_ibss &&
2338 ieee80211_ibss_allowed(wpa_s))
2339 return ieee80211_sta_create_ibss(wpa_s);
2340 if (wpa_s->mlme.create_ibss) {
2341 wpa_printf(MSG_DEBUG, "MLME: IBSS not allowed "
2342 "on the configured channel %d "
2344 local->conf.channel,
2348 /* No IBSS found - decrease scan interval and continue
2350 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2353 wpa_s->mlme.state = IEEE80211_IBSS_SEARCH;
2354 ieee80211_reschedule_timer(wpa_s, interval);
2363 int ieee80211_sta_get_ssid(struct wpa_supplicant *wpa_s, u8 *ssid,
2366 os_memcpy(ssid, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len);
2367 *len = wpa_s->mlme.ssid_len;
2372 int ieee80211_sta_associate(struct wpa_supplicant *wpa_s,
2373 struct wpa_driver_associate_params *params)
2375 struct ieee80211_sta_bss *bss;
2377 wpa_s->mlme.bssid_set = 0;
2378 wpa_s->mlme.freq = params->freq;
2379 if (params->bssid) {
2380 os_memcpy(wpa_s->bssid, params->bssid, ETH_ALEN);
2381 if (!is_zero_ether_addr(params->bssid))
2382 wpa_s->mlme.bssid_set = 1;
2383 bss = ieee80211_bss_get(wpa_s, wpa_s->bssid);
2385 wpa_s->mlme.phymode = bss->hw_mode;
2386 wpa_s->mlme.channel = bss->channel;
2387 wpa_s->mlme.freq = bss->freq;
2392 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
2394 if (local->hw->conf_tx) {
2395 struct ieee80211_tx_queue_params qparam;
2398 os_memset(&qparam, 0, sizeof(qparam));
2399 /* TODO: are these ok defaults for all hw_modes? */
2402 local->conf.phymode == MODE_IEEE80211B ? 31 : 15;
2403 qparam.cw_max = 1023;
2404 qparam.burst_time = 0;
2405 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
2407 local->hw->conf_tx(wpa_s, i + IEEE80211_TX_QUEUE_DATA0,
2410 /* IBSS uses different parameters for Beacon sending */
2414 local->hw->conf_tx(wpa_s, IEEE80211_TX_QUEUE_BEACON, &qparam);
2418 if (wpa_s->mlme.ssid_len != params->ssid_len ||
2419 os_memcmp(wpa_s->mlme.ssid, params->ssid, params->ssid_len) != 0)
2420 wpa_s->mlme.prev_bssid_set = 0;
2421 os_memcpy(wpa_s->mlme.ssid, params->ssid, params->ssid_len);
2422 os_memset(wpa_s->mlme.ssid + params->ssid_len, 0,
2423 MAX_SSID_LEN - params->ssid_len);
2424 wpa_s->mlme.ssid_len = params->ssid_len;
2425 wpa_s->mlme.ssid_set = 1;
2427 os_free(wpa_s->mlme.extra_ie);
2428 if (params->wpa_ie == NULL || params->wpa_ie_len == 0) {
2429 wpa_s->mlme.extra_ie = NULL;
2430 wpa_s->mlme.extra_ie_len = 0;
2432 wpa_s->mlme.extra_ie = os_malloc(params->wpa_ie_len);
2433 if (wpa_s->mlme.extra_ie == NULL) {
2434 wpa_s->mlme.extra_ie_len = 0;
2437 os_memcpy(wpa_s->mlme.extra_ie, params->wpa_ie,
2438 params->wpa_ie_len);
2439 wpa_s->mlme.extra_ie_len = params->wpa_ie_len;
2442 wpa_s->mlme.key_mgmt = params->key_mgmt_suite;
2444 ieee80211_sta_set_channel(wpa_s, wpa_s->mlme.phymode,
2445 wpa_s->mlme.channel, wpa_s->mlme.freq);
2447 if (params->mode == 1 && !wpa_s->mlme.bssid_set) {
2448 os_get_time(&wpa_s->mlme.ibss_join_req);
2449 wpa_s->mlme.state = IEEE80211_IBSS_SEARCH;
2450 return ieee80211_sta_find_ibss(wpa_s);
2453 if (wpa_s->mlme.bssid_set)
2454 ieee80211_sta_new_auth(wpa_s);
2460 static void ieee80211_sta_save_oper_chan(struct wpa_supplicant *wpa_s)
2462 wpa_s->mlme.scan_oper_channel = wpa_s->mlme.channel;
2463 wpa_s->mlme.scan_oper_freq = wpa_s->mlme.freq;
2464 wpa_s->mlme.scan_oper_phymode = wpa_s->mlme.phymode;
2468 static int ieee80211_sta_restore_oper_chan(struct wpa_supplicant *wpa_s)
2470 wpa_s->mlme.channel = wpa_s->mlme.scan_oper_channel;
2471 wpa_s->mlme.freq = wpa_s->mlme.scan_oper_freq;
2472 wpa_s->mlme.phymode = wpa_s->mlme.scan_oper_phymode;
2473 if (wpa_s->mlme.freq == 0)
2475 return ieee80211_sta_set_channel(wpa_s, wpa_s->mlme.phymode,
2476 wpa_s->mlme.channel,
2481 static int ieee80211_active_scan(struct wpa_supplicant *wpa_s)
2486 for (m = 0; m < wpa_s->mlme.num_modes; m++) {
2487 struct wpa_hw_modes *mode = &wpa_s->mlme.modes[m];
2488 if ((int) mode->mode != (int) wpa_s->mlme.phymode)
2490 for (c = 0; c < mode->num_channels; c++) {
2491 struct wpa_channel_data *chan = &mode->channels[c];
2492 if (chan->flag & WPA_CHAN_W_SCAN &&
2493 chan->chan == wpa_s->mlme.channel) {
2494 if (chan->flag & WPA_CHAN_W_ACTIVE_SCAN)
2505 static void ieee80211_sta_scan_timer(void *eloop_ctx, void *timeout_ctx)
2507 struct wpa_supplicant *wpa_s = eloop_ctx;
2508 struct wpa_hw_modes *mode;
2509 struct wpa_channel_data *chan;
2512 struct wpa_ssid *ssid = wpa_s->current_ssid;
2515 if (!wpa_s->mlme.sta_scanning || wpa_s->mlme.modes == NULL)
2518 adhoc = ssid && ssid->mode == 1;
2520 switch (wpa_s->mlme.scan_state) {
2521 case SCAN_SET_CHANNEL:
2522 mode = &wpa_s->mlme.modes[wpa_s->mlme.scan_hw_mode_idx];
2523 if (wpa_s->mlme.scan_hw_mode_idx >=
2524 (int) wpa_s->mlme.num_modes ||
2525 (wpa_s->mlme.scan_hw_mode_idx + 1 ==
2526 (int) wpa_s->mlme.num_modes
2527 && wpa_s->mlme.scan_channel_idx >= mode->num_channels)) {
2528 if (ieee80211_sta_restore_oper_chan(wpa_s)) {
2529 wpa_printf(MSG_DEBUG, "MLME: failed to "
2530 "restore operational channel after "
2533 wpa_printf(MSG_DEBUG, "MLME: scan completed");
2534 wpa_s->mlme.sta_scanning = 0;
2535 os_get_time(&wpa_s->mlme.last_scan_completed);
2536 wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
2538 if (!wpa_s->mlme.bssid_set ||
2539 (wpa_s->mlme.state ==
2540 IEEE80211_IBSS_JOINED &&
2541 !ieee80211_sta_active_ibss(wpa_s)))
2542 ieee80211_sta_find_ibss(wpa_s);
2546 skip = !(wpa_s->mlme.hw_modes & (1 << mode->mode));
2547 chan = &mode->channels[wpa_s->mlme.scan_channel_idx];
2548 if (!(chan->flag & WPA_CHAN_W_SCAN) ||
2549 (adhoc && !(chan->flag & WPA_CHAN_W_IBSS)) ||
2550 (wpa_s->mlme.hw_modes & (1 << WPA_MODE_IEEE80211G) &&
2551 mode->mode == WPA_MODE_IEEE80211B &&
2552 wpa_s->mlme.scan_skip_11b))
2556 wpa_printf(MSG_MSGDUMP,
2557 "MLME: scan channel %d (%d MHz)",
2558 chan->chan, chan->freq);
2560 wpa_s->mlme.channel = chan->chan;
2561 wpa_s->mlme.freq = chan->freq;
2562 wpa_s->mlme.phymode = mode->mode;
2563 if (ieee80211_sta_set_channel(wpa_s, mode->mode,
2564 chan->chan, chan->freq))
2566 wpa_printf(MSG_DEBUG, "MLME: failed to set "
2567 "channel %d (%d MHz) for scan",
2568 chan->chan, chan->freq);
2573 wpa_s->mlme.scan_channel_idx++;
2574 if (wpa_s->mlme.scan_channel_idx >=
2575 wpa_s->mlme.modes[wpa_s->mlme.scan_hw_mode_idx].
2577 wpa_s->mlme.scan_hw_mode_idx++;
2578 wpa_s->mlme.scan_channel_idx = 0;
2586 timeout = IEEE80211_PROBE_DELAY;
2587 wpa_s->mlme.scan_state = SCAN_SEND_PROBE;
2589 case SCAN_SEND_PROBE:
2590 if (ieee80211_active_scan(wpa_s)) {
2591 ieee80211_send_probe_req(wpa_s, NULL,
2592 wpa_s->mlme.scan_ssid,
2593 wpa_s->mlme.scan_ssid_len);
2594 timeout = IEEE80211_CHANNEL_TIME;
2596 timeout = IEEE80211_PASSIVE_CHANNEL_TIME;
2598 wpa_s->mlme.scan_state = SCAN_SET_CHANNEL;
2602 eloop_register_timeout(timeout / 1000, 1000 * (timeout % 1000),
2603 ieee80211_sta_scan_timer, wpa_s, NULL);
2607 int ieee80211_sta_req_scan(struct wpa_supplicant *wpa_s, const u8 *ssid,
2610 if (ssid_len > MAX_SSID_LEN)
2613 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
2614 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
2617 * ScanType: ACTIVE, PASSIVE
2618 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
2619 * a Probe frame during active scanning
2621 * MinChannelTime (>= ProbeDelay), in TU
2622 * MaxChannelTime: (>= MinChannelTime), in TU
2625 /* MLME-SCAN.confirm
2627 * ResultCode: SUCCESS, INVALID_PARAMETERS
2630 /* TODO: if assoc, move to power save mode for the duration of the
2633 if (wpa_s->mlme.sta_scanning)
2636 wpa_printf(MSG_DEBUG, "MLME: starting scan");
2638 ieee80211_sta_save_oper_chan(wpa_s);
2640 wpa_s->mlme.sta_scanning = 1;
2641 /* TODO: stop TX queue? */
2644 wpa_s->mlme.scan_ssid_len = ssid_len;
2645 os_memcpy(wpa_s->mlme.scan_ssid, ssid, ssid_len);
2647 wpa_s->mlme.scan_ssid_len = 0;
2648 wpa_s->mlme.scan_skip_11b = 1; /* FIX: clear this is 11g is not
2650 wpa_s->mlme.scan_state = SCAN_SET_CHANNEL;
2651 wpa_s->mlme.scan_hw_mode_idx = 0;
2652 wpa_s->mlme.scan_channel_idx = 0;
2653 eloop_register_timeout(0, 1, ieee80211_sta_scan_timer, wpa_s, NULL);
2659 struct wpa_scan_results *
2660 ieee80211_sta_get_scan_results(struct wpa_supplicant *wpa_s)
2663 struct wpa_scan_results *res;
2664 struct wpa_scan_res *r;
2665 struct ieee80211_sta_bss *bss;
2667 res = os_zalloc(sizeof(*res));
2668 for (bss = wpa_s->mlme.sta_bss_list; bss; bss = bss->next)
2670 res->res = os_zalloc(ap_num * sizeof(struct wpa_scan_res *));
2671 if (res->res == NULL) {
2676 for (bss = wpa_s->mlme.sta_bss_list; bss; bss = bss->next) {
2677 r = os_zalloc(sizeof(*r) + bss->ie_len);
2680 os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
2681 r->freq = bss->freq;
2682 r->beacon_int = bss->beacon_int;
2683 r->caps = bss->capability;
2684 r->level = bss->rssi;
2685 r->tsf = bss->timestamp;
2687 r->ie_len = bss->ie_len;
2688 os_memcpy(r + 1, bss->ie, bss->ie_len);
2691 res->res[res->num++] = r;
2699 struct sta_info * ieee80211_ibss_add_sta(struct wpa_supplicant *wpa_s,
2700 struct sk_buff *skb, u8 *bssid,
2703 struct ieee80211_local *local = dev->priv;
2704 struct list_head *ptr;
2705 struct sta_info *sta;
2706 struct wpa_supplicant *sta_dev = NULL;
2708 /* TODO: Could consider removing the least recently used entry and
2709 * allow new one to be added. */
2710 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
2711 if (net_ratelimit()) {
2712 wpa_printf(MSG_DEBUG, "MLME: No room for a new IBSS "
2713 "STA entry " MACSTR, MAC2STR(addr));
2718 spin_lock_bh(&local->sub_if_lock);
2719 list_for_each(ptr, &local->sub_if_list) {
2720 sdata = list_entry(ptr, struct ieee80211_sub_if_data, list);
2721 if (sdata->type == IEEE80211_SUB_IF_TYPE_STA &&
2722 os_memcmp(bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
2723 sta_dev = sdata->dev;
2727 spin_unlock_bh(&local->sub_if_lock);
2729 if (sta_dev == NULL)
2732 wpa_printf(MSG_DEBUG, "MLME: Adding new IBSS station " MACSTR
2733 " (dev=%s)", MAC2STR(addr), sta_dev->name);
2735 sta = sta_info_add(wpa_s, addr);
2741 sta->supp_rates = wpa_s->mlme.supp_rates_bits;
2743 rate_control_rate_init(local, sta);
2745 return sta; /* caller will call sta_info_release() */
2750 int ieee80211_sta_deauthenticate(struct wpa_supplicant *wpa_s, u16 reason)
2752 wpa_printf(MSG_DEBUG, "MLME: deauthenticate(reason=%d)", reason);
2754 ieee80211_send_deauth(wpa_s, reason);
2755 ieee80211_set_associated(wpa_s, 0);
2760 int ieee80211_sta_disassociate(struct wpa_supplicant *wpa_s, u16 reason)
2762 wpa_printf(MSG_DEBUG, "MLME: disassociate(reason=%d)", reason);
2764 if (!wpa_s->mlme.associated)
2767 ieee80211_send_disassoc(wpa_s, reason);
2768 ieee80211_set_associated(wpa_s, 0);
2773 void ieee80211_sta_rx(struct wpa_supplicant *wpa_s, const u8 *buf, size_t len,
2774 struct ieee80211_rx_status *rx_status)
2776 struct ieee80211_mgmt *mgmt;
2780 /* wpa_hexdump(MSG_MSGDUMP, "MLME: Received frame", buf, len); */
2782 if (wpa_s->mlme.sta_scanning) {
2783 ieee80211_sta_rx_scan(wpa_s, buf, len, rx_status);
2790 mgmt = (struct ieee80211_mgmt *) buf;
2791 fc = le_to_host16(mgmt->frame_control);
2793 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
2794 ieee80211_sta_rx_mgmt(wpa_s, buf, len, rx_status);
2795 else if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
2796 if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) !=
2799 /* mgmt->sa is actually BSSID for FromDS data frames */
2800 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0)
2802 /* Skip IEEE 802.11 and LLC headers */
2804 if (WPA_GET_BE16(pos) != ETH_P_EAPOL)
2807 /* mgmt->bssid is actually BSSID for SA data frames */
2808 wpa_supplicant_rx_eapol(wpa_s, mgmt->bssid,
2809 pos, buf + len - pos);
2814 void ieee80211_sta_free_hw_features(struct wpa_hw_modes *hw_features,
2815 size_t num_hw_features)
2819 if (hw_features == NULL)
2822 for (i = 0; i < num_hw_features; i++) {
2823 os_free(hw_features[i].channels);
2824 os_free(hw_features[i].rates);
2827 os_free(hw_features);
2831 int ieee80211_sta_init(struct wpa_supplicant *wpa_s)
2833 u16 num_modes, flags;
2835 wpa_s->mlme.modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes,
2837 if (wpa_s->mlme.modes == NULL) {
2838 wpa_printf(MSG_ERROR, "MLME: Failed to read supported "
2839 "channels and rates from the driver");
2843 wpa_s->mlme.num_modes = num_modes;
2845 wpa_s->mlme.hw_modes = 1 << WPA_MODE_IEEE80211A;
2846 wpa_s->mlme.hw_modes |= 1 << WPA_MODE_IEEE80211B;
2847 wpa_s->mlme.hw_modes |= 1 << WPA_MODE_IEEE80211G;
2849 wpa_s->mlme.wmm_enabled = 1;
2855 void ieee80211_sta_deinit(struct wpa_supplicant *wpa_s)
2857 eloop_cancel_timeout(ieee80211_sta_timer, wpa_s, NULL);
2858 eloop_cancel_timeout(ieee80211_sta_scan_timer, wpa_s, NULL);
2859 os_free(wpa_s->mlme.extra_ie);
2860 wpa_s->mlme.extra_ie = NULL;
2861 os_free(wpa_s->mlme.extra_probe_ie);
2862 wpa_s->mlme.extra_probe_ie = NULL;
2863 os_free(wpa_s->mlme.assocreq_ies);
2864 wpa_s->mlme.assocreq_ies = NULL;
2865 os_free(wpa_s->mlme.assocresp_ies);
2866 wpa_s->mlme.assocresp_ies = NULL;
2867 ieee80211_bss_list_deinit(wpa_s);
2868 ieee80211_sta_free_hw_features(wpa_s->mlme.modes,
2869 wpa_s->mlme.num_modes);
2870 #ifdef CONFIG_IEEE80211R
2871 os_free(wpa_s->mlme.ft_ies);
2872 wpa_s->mlme.ft_ies = NULL;
2873 wpa_s->mlme.ft_ies_len = 0;
2874 #endif /* CONFIG_IEEE80211R */
2878 #ifdef CONFIG_IEEE80211R
2880 int ieee80211_sta_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
2881 const u8 *ies, size_t ies_len)
2884 wpa_printf(MSG_DEBUG, "MLME: Clear FT mobility domain");
2885 os_memset(wpa_s->mlme.current_md, 0, MOBILITY_DOMAIN_ID_LEN);
2887 wpa_printf(MSG_DEBUG, "MLME: Update FT IEs for MD " MACSTR,
2889 os_memcpy(wpa_s->mlme.current_md, md, MOBILITY_DOMAIN_ID_LEN);
2892 wpa_hexdump(MSG_DEBUG, "MLME: FT IEs", ies, ies_len);
2893 os_free(wpa_s->mlme.ft_ies);
2894 wpa_s->mlme.ft_ies = os_malloc(ies_len);
2895 if (wpa_s->mlme.ft_ies == NULL)
2897 os_memcpy(wpa_s->mlme.ft_ies, ies, ies_len);
2898 wpa_s->mlme.ft_ies_len = ies_len;
2904 int ieee80211_sta_send_ft_action(struct wpa_supplicant *wpa_s, u8 action,
2905 const u8 *target_ap,
2906 const u8 *ies, size_t ies_len)
2910 struct ieee80211_mgmt *mgmt;
2914 * Action frame payload:
2915 * Category[1] = 6 (Fast BSS Transition)
2916 * Action[1] = 1 (Fast BSS Transition Request)
2922 buf = os_zalloc(sizeof(*mgmt) + ies_len);
2924 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
2929 mgmt = (struct ieee80211_mgmt *) buf;
2931 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
2932 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
2933 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
2934 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2935 WLAN_FC_STYPE_ACTION);
2936 mgmt->u.action.category = WLAN_ACTION_FT;
2937 mgmt->u.action.u.ft_action_req.action = action;
2938 os_memcpy(mgmt->u.action.u.ft_action_req.sta_addr, wpa_s->own_addr,
2940 os_memcpy(mgmt->u.action.u.ft_action_req.target_ap_addr, target_ap,
2942 os_memcpy(mgmt->u.action.u.ft_action_req.variable, ies, ies_len);
2943 len += 1 + sizeof(mgmt->u.action.u.ft_action_req) + ies_len;
2945 wpa_printf(MSG_DEBUG, "MLME: Send FT Action Frame: Action=%d "
2946 "Target AP=" MACSTR " body_len=%lu",
2947 action, MAC2STR(target_ap), (unsigned long) ies_len);
2949 res = ieee80211_sta_tx(wpa_s, buf, len);
2955 #endif /* CONFIG_IEEE80211R */
2958 int ieee80211_sta_set_probe_req_ie(struct wpa_supplicant *wpa_s, const u8 *ies,
2961 os_free(wpa_s->mlme.extra_probe_ie);
2962 wpa_s->mlme.extra_probe_ie = NULL;
2963 wpa_s->mlme.extra_probe_ie_len = 0;
2968 wpa_s->mlme.extra_probe_ie = os_malloc(ies_len);
2969 if (wpa_s->mlme.extra_probe_ie == NULL)
2972 os_memcpy(wpa_s->mlme.extra_probe_ie, ies, ies_len);
2973 wpa_s->mlme.extra_probe_ie_len = ies_len;