Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / network / WirelessManager / wpa_supplicant / mlme.c
blobe5fbd8edc36245eb44d43de4264824838272fb65
1 /*
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
12 * license.
14 * See README and COPYING for more details.
17 #include "includes.h"
19 #include "common.h"
20 #include "eloop.h"
21 #include "config_ssid.h"
22 #include "wpa_supplicant_i.h"
23 #include "notify.h"
24 #include "driver_i.h"
25 #include "rsn_supp/wpa.h"
26 #include "common/ieee802_11_defs.h"
27 #include "common/ieee802_11_common.h"
28 #include "mlme.h"
31 /* Timeouts and intervals in milliseconds */
32 #define IEEE80211_AUTH_TIMEOUT (200)
33 #define IEEE80211_AUTH_MAX_TRIES 3
34 #define IEEE80211_ASSOC_TIMEOUT (200)
35 #define IEEE80211_ASSOC_MAX_TRIES 3
36 #define IEEE80211_MONITORING_INTERVAL (2000)
37 #define IEEE80211_PROBE_INTERVAL (60000)
38 #define IEEE80211_RETRY_AUTH_INTERVAL (1000)
39 #define IEEE80211_SCAN_INTERVAL (2000)
40 #define IEEE80211_SCAN_INTERVAL_SLOW (15000)
41 #define IEEE80211_IBSS_JOIN_TIMEOUT (20000)
43 #define IEEE80211_PROBE_DELAY (33)
44 #define IEEE80211_CHANNEL_TIME (33)
45 #define IEEE80211_PASSIVE_CHANNEL_TIME (200)
46 #define IEEE80211_SCAN_RESULT_EXPIRE (10000)
47 #define IEEE80211_IBSS_MERGE_INTERVAL (30000)
48 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60000)
50 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
53 #define IEEE80211_FC(type, stype) host_to_le16((type << 2) | (stype << 4))
56 struct ieee80211_sta_bss {
57 struct ieee80211_sta_bss *next;
58 struct ieee80211_sta_bss *hnext;
60 u8 bssid[ETH_ALEN];
61 u8 ssid[MAX_SSID_LEN];
62 size_t ssid_len;
63 u16 capability; /* host byte order */
64 int hw_mode;
65 int channel;
66 int freq;
67 int rssi;
68 u8 *ie;
69 size_t ie_len;
70 u8 *wpa_ie;
71 size_t wpa_ie_len;
72 u8 *rsn_ie;
73 size_t rsn_ie_len;
74 u8 *wmm_ie;
75 size_t wmm_ie_len;
76 u8 *mdie;
77 size_t mdie_len;
78 #define IEEE80211_MAX_SUPP_RATES 32
79 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
80 size_t supp_rates_len;
81 int beacon_int;
82 u64 timestamp;
84 int probe_resp;
85 struct os_time last_update;
89 static void ieee80211_send_probe_req(struct wpa_supplicant *wpa_s,
90 const u8 *dst,
91 const u8 *ssid, size_t ssid_len);
92 static struct ieee80211_sta_bss *
93 ieee80211_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid);
94 static int ieee80211_sta_find_ibss(struct wpa_supplicant *wpa_s);
95 static int ieee80211_sta_wep_configured(struct wpa_supplicant *wpa_s);
96 static void ieee80211_sta_timer(void *eloop_ctx, void *timeout_ctx);
97 static void ieee80211_sta_scan_timer(void *eloop_ctx, void *timeout_ctx);
98 static void ieee80211_build_tspec(struct wpabuf *buf);
99 static int ieee80211_sta_set_probe_req_ie(struct wpa_supplicant *wpa_s,
100 const u8 *ies, size_t ies_len);
103 static int ieee80211_sta_set_channel(struct wpa_supplicant *wpa_s,
104 enum hostapd_hw_mode phymode, int chan,
105 int freq)
107 size_t i;
108 struct hostapd_hw_modes *mode;
110 for (i = 0; i < wpa_s->mlme.num_modes; i++) {
111 mode = &wpa_s->mlme.modes[i];
112 if (mode->mode == phymode) {
113 wpa_s->mlme.curr_rates = mode->rates;
114 wpa_s->mlme.num_curr_rates = mode->num_rates;
115 break;
119 return wpa_drv_set_channel(wpa_s, phymode, chan, freq);
123 static int ecw2cw(int ecw)
125 int cw = 1;
126 while (ecw > 0) {
127 cw <<= 1;
128 ecw--;
130 return cw - 1;
134 static void ieee80211_sta_wmm_params(struct wpa_supplicant *wpa_s,
135 const u8 *wmm_param, size_t wmm_param_len)
137 size_t left;
138 int count;
139 const u8 *pos;
140 u8 wmm_acm;
142 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
143 return;
144 count = wmm_param[6] & 0x0f;
145 if (count == wpa_s->mlme.wmm_last_param_set)
146 return;
147 wpa_s->mlme.wmm_last_param_set = count;
149 pos = wmm_param + 8;
150 left = wmm_param_len - 8;
152 wmm_acm = 0;
153 for (; left >= 4; left -= 4, pos += 4) {
154 int aci = (pos[0] >> 5) & 0x03;
155 int acm = (pos[0] >> 4) & 0x01;
156 int aifs, cw_max, cw_min, burst_time;
158 switch (aci) {
159 case 1: /* AC_BK */
160 if (acm)
161 wmm_acm |= BIT(1) | BIT(2); /* BK/- */
162 break;
163 case 2: /* AC_VI */
164 if (acm)
165 wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
166 break;
167 case 3: /* AC_VO */
168 if (acm)
169 wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
170 break;
171 case 0: /* AC_BE */
172 default:
173 if (acm)
174 wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
175 break;
178 aifs = pos[0] & 0x0f;
179 cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
180 cw_min = ecw2cw(pos[1] & 0x0f);
181 /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
182 burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
183 wpa_printf(MSG_DEBUG, "MLME: WMM aci=%d acm=%d aifs=%d "
184 "cWmin=%d cWmax=%d burst=%d",
185 aci, acm, aifs, cw_min, cw_max, burst_time);
186 /* TODO: driver configuration */
191 static void ieee80211_set_associated(struct wpa_supplicant *wpa_s, int assoc)
193 if (wpa_s->mlme.associated == assoc && !assoc)
194 return;
196 wpa_s->mlme.associated = assoc;
198 if (assoc) {
199 union wpa_event_data data;
200 os_memset(&data, 0, sizeof(data));
201 wpa_s->mlme.prev_bssid_set = 1;
202 os_memcpy(wpa_s->mlme.prev_bssid, wpa_s->bssid, ETH_ALEN);
203 data.assoc_info.req_ies = wpa_s->mlme.assocreq_ies;
204 data.assoc_info.req_ies_len = wpa_s->mlme.assocreq_ies_len;
205 data.assoc_info.resp_ies = wpa_s->mlme.assocresp_ies;
206 data.assoc_info.resp_ies_len = wpa_s->mlme.assocresp_ies_len;
207 data.assoc_info.freq = wpa_s->mlme.freq;
208 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
209 } else {
210 wpa_supplicant_event(wpa_s, EVENT_DISASSOC, NULL);
212 os_get_time(&wpa_s->mlme.last_probe);
216 static int ieee80211_sta_tx(struct wpa_supplicant *wpa_s, const u8 *buf,
217 size_t len)
219 return wpa_drv_send_mlme(wpa_s, buf, len);
223 static void ieee80211_send_auth(struct wpa_supplicant *wpa_s,
224 int transaction, const u8 *extra,
225 size_t extra_len, int encrypt)
227 u8 *buf;
228 size_t len;
229 struct ieee80211_mgmt *mgmt;
231 buf = os_malloc(sizeof(*mgmt) + 6 + extra_len);
232 if (buf == NULL) {
233 wpa_printf(MSG_DEBUG, "MLME: failed to allocate buffer for "
234 "auth frame");
235 return;
238 mgmt = (struct ieee80211_mgmt *) buf;
239 len = 24 + 6;
240 os_memset(mgmt, 0, 24 + 6);
241 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
242 WLAN_FC_STYPE_AUTH);
243 if (encrypt)
244 mgmt->frame_control |= host_to_le16(WLAN_FC_ISWEP);
245 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
246 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
247 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
248 mgmt->u.auth.auth_alg = host_to_le16(wpa_s->mlme.auth_alg);
249 mgmt->u.auth.auth_transaction = host_to_le16(transaction);
250 wpa_s->mlme.auth_transaction = transaction + 1;
251 mgmt->u.auth.status_code = host_to_le16(0);
252 if (extra) {
253 os_memcpy(buf + len, extra, extra_len);
254 len += extra_len;
257 ieee80211_sta_tx(wpa_s, buf, len);
258 os_free(buf);
262 static void ieee80211_reschedule_timer(struct wpa_supplicant *wpa_s, int ms)
264 eloop_cancel_timeout(ieee80211_sta_timer, wpa_s, NULL);
265 eloop_register_timeout(ms / 1000, 1000 * (ms % 1000),
266 ieee80211_sta_timer, wpa_s, NULL);
270 static void ieee80211_authenticate(struct wpa_supplicant *wpa_s)
272 u8 *extra;
273 size_t extra_len;
275 wpa_s->mlme.auth_tries++;
276 if (wpa_s->mlme.auth_tries > IEEE80211_AUTH_MAX_TRIES) {
277 wpa_printf(MSG_DEBUG, "MLME: authentication with AP " MACSTR
278 " timed out", MAC2STR(wpa_s->bssid));
279 return;
282 wpa_s->mlme.state = IEEE80211_AUTHENTICATE;
283 wpa_printf(MSG_DEBUG, "MLME: authenticate with AP " MACSTR,
284 MAC2STR(wpa_s->bssid));
286 extra = NULL;
287 extra_len = 0;
289 #ifdef CONFIG_IEEE80211R
290 if ((wpa_s->mlme.key_mgmt == KEY_MGMT_FT_802_1X ||
291 wpa_s->mlme.key_mgmt == KEY_MGMT_FT_PSK) &&
292 wpa_s->mlme.ft_ies) {
293 struct ieee80211_sta_bss *bss;
294 struct rsn_mdie *mdie = NULL;
295 bss = ieee80211_bss_get(wpa_s, wpa_s->bssid);
296 if (bss && bss->mdie_len >= 2 + sizeof(*mdie))
297 mdie = (struct rsn_mdie *) (bss->mdie + 2);
298 if (mdie &&
299 os_memcmp(mdie->mobility_domain, wpa_s->mlme.current_md,
300 MOBILITY_DOMAIN_ID_LEN) == 0) {
301 wpa_printf(MSG_DEBUG, "MLME: Trying to use FT "
302 "over-the-air");
303 wpa_s->mlme.auth_alg = WLAN_AUTH_FT;
304 extra = wpa_s->mlme.ft_ies;
305 extra_len = wpa_s->mlme.ft_ies_len;
308 #endif /* CONFIG_IEEE80211R */
310 ieee80211_send_auth(wpa_s, 1, extra, extra_len, 0);
312 ieee80211_reschedule_timer(wpa_s, IEEE80211_AUTH_TIMEOUT);
316 static void ieee80211_send_assoc(struct wpa_supplicant *wpa_s)
318 struct ieee80211_mgmt *mgmt;
319 u8 *pos, *ies, *buf;
320 int i, len;
321 u16 capab;
322 struct ieee80211_sta_bss *bss;
323 int wmm = 0;
324 size_t blen, buflen;
326 if (wpa_s->mlme.curr_rates == NULL) {
327 wpa_printf(MSG_DEBUG, "MLME: curr_rates not set for assoc");
328 return;
331 buflen = sizeof(*mgmt) + 200 + wpa_s->mlme.extra_ie_len +
332 wpa_s->mlme.ssid_len;
333 #ifdef CONFIG_IEEE80211R
334 if (wpa_s->mlme.ft_ies)
335 buflen += wpa_s->mlme.ft_ies_len;
336 #endif /* CONFIG_IEEE80211R */
337 buf = os_malloc(buflen);
338 if (buf == NULL) {
339 wpa_printf(MSG_DEBUG, "MLME: failed to allocate buffer for "
340 "assoc frame");
341 return;
343 blen = 0;
345 capab = wpa_s->mlme.capab;
346 if (wpa_s->mlme.phymode == HOSTAPD_MODE_IEEE80211G) {
347 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME |
348 WLAN_CAPABILITY_SHORT_PREAMBLE;
350 bss = ieee80211_bss_get(wpa_s, wpa_s->bssid);
351 if (bss) {
352 capab |= WLAN_CAPABILITY_ESS; /* wrong, but needed by some APs */
353 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
354 capab |= WLAN_CAPABILITY_PRIVACY;
355 #if 0
356 if (bss->wmm_ie) {
357 wmm = 1;
359 #endif
362 mgmt = (struct ieee80211_mgmt *) buf;
363 blen += 24;
364 os_memset(mgmt, 0, 24);
365 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
366 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
367 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
369 if (wpa_s->mlme.prev_bssid_set) {
370 blen += 10;
371 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
372 WLAN_FC_STYPE_REASSOC_REQ);
373 mgmt->u.reassoc_req.capab_info = host_to_le16(capab);
374 mgmt->u.reassoc_req.listen_interval = host_to_le16(1);
375 os_memcpy(mgmt->u.reassoc_req.current_ap,
376 wpa_s->mlme.prev_bssid,
377 ETH_ALEN);
378 } else {
379 blen += 4;
380 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
381 WLAN_FC_STYPE_ASSOC_REQ);
382 mgmt->u.assoc_req.capab_info = host_to_le16(capab);
383 mgmt->u.assoc_req.listen_interval = host_to_le16(1);
386 /* SSID */
387 ies = pos = buf + blen;
388 blen += 2 + wpa_s->mlme.ssid_len;
389 *pos++ = WLAN_EID_SSID;
390 *pos++ = wpa_s->mlme.ssid_len;
391 os_memcpy(pos, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len);
393 len = wpa_s->mlme.num_curr_rates;
394 if (len > 8)
395 len = 8;
396 pos = buf + blen;
397 blen += len + 2;
398 *pos++ = WLAN_EID_SUPP_RATES;
399 *pos++ = len;
400 for (i = 0; i < len; i++)
401 *pos++ = (u8) (wpa_s->mlme.curr_rates[i] / 5);
403 if (wpa_s->mlme.num_curr_rates > len) {
404 pos = buf + blen;
405 blen += wpa_s->mlme.num_curr_rates - len + 2;
406 *pos++ = WLAN_EID_EXT_SUPP_RATES;
407 *pos++ = wpa_s->mlme.num_curr_rates - len;
408 for (i = len; i < wpa_s->mlme.num_curr_rates; i++)
409 *pos++ = (u8) (wpa_s->mlme.curr_rates[i] / 5);
412 if (wpa_s->mlme.extra_ie && wpa_s->mlme.auth_alg != WLAN_AUTH_FT) {
413 pos = buf + blen;
414 blen += wpa_s->mlme.extra_ie_len;
415 os_memcpy(pos, wpa_s->mlme.extra_ie, wpa_s->mlme.extra_ie_len);
418 #ifdef CONFIG_IEEE80211R
419 if ((wpa_s->mlme.key_mgmt == KEY_MGMT_FT_802_1X ||
420 wpa_s->mlme.key_mgmt == KEY_MGMT_FT_PSK) &&
421 wpa_s->mlme.auth_alg != WLAN_AUTH_FT &&
422 bss && bss->mdie &&
423 bss->mdie_len >= 2 + sizeof(struct rsn_mdie) &&
424 bss->mdie[1] >= sizeof(struct rsn_mdie)) {
425 pos = buf + blen;
426 blen += 2 + sizeof(struct rsn_mdie);
427 *pos++ = WLAN_EID_MOBILITY_DOMAIN;
428 *pos++ = sizeof(struct rsn_mdie);
429 os_memcpy(pos, bss->mdie + 2, MOBILITY_DOMAIN_ID_LEN);
430 pos += MOBILITY_DOMAIN_ID_LEN;
431 *pos++ = 0; /* FIX: copy from the target AP's MDIE */
434 if ((wpa_s->mlme.key_mgmt == KEY_MGMT_FT_802_1X ||
435 wpa_s->mlme.key_mgmt == KEY_MGMT_FT_PSK) &&
436 wpa_s->mlme.auth_alg == WLAN_AUTH_FT && wpa_s->mlme.ft_ies) {
437 pos = buf + blen;
438 os_memcpy(pos, wpa_s->mlme.ft_ies, wpa_s->mlme.ft_ies_len);
439 pos += wpa_s->mlme.ft_ies_len;
440 blen += wpa_s->mlme.ft_ies_len;
442 #endif /* CONFIG_IEEE80211R */
444 if (wmm && wpa_s->mlme.wmm_enabled) {
445 pos = buf + blen;
446 blen += 9;
447 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
448 *pos++ = 7; /* len */
449 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
450 *pos++ = 0x50;
451 *pos++ = 0xf2;
452 *pos++ = 2; /* WMM */
453 *pos++ = 0; /* WMM info */
454 *pos++ = 1; /* WMM ver */
455 *pos++ = 0;
458 os_free(wpa_s->mlme.assocreq_ies);
459 wpa_s->mlme.assocreq_ies_len = (buf + blen) - ies;
460 wpa_s->mlme.assocreq_ies = os_malloc(wpa_s->mlme.assocreq_ies_len);
461 if (wpa_s->mlme.assocreq_ies) {
462 os_memcpy(wpa_s->mlme.assocreq_ies, ies,
463 wpa_s->mlme.assocreq_ies_len);
466 ieee80211_sta_tx(wpa_s, buf, blen);
467 os_free(buf);
471 static void ieee80211_send_deauth(struct wpa_supplicant *wpa_s, u16 reason)
473 u8 *buf;
474 size_t len;
475 struct ieee80211_mgmt *mgmt;
477 buf = os_zalloc(sizeof(*mgmt));
478 if (buf == NULL) {
479 wpa_printf(MSG_DEBUG, "MLME: failed to allocate buffer for "
480 "deauth frame");
481 return;
484 mgmt = (struct ieee80211_mgmt *) buf;
485 len = 24;
486 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
487 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
488 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
489 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
490 WLAN_FC_STYPE_DEAUTH);
491 len += 2;
492 mgmt->u.deauth.reason_code = host_to_le16(reason);
494 ieee80211_sta_tx(wpa_s, buf, len);
495 os_free(buf);
499 static void ieee80211_send_disassoc(struct wpa_supplicant *wpa_s, u16 reason)
501 u8 *buf;
502 size_t len;
503 struct ieee80211_mgmt *mgmt;
505 buf = os_zalloc(sizeof(*mgmt));
506 if (buf == NULL) {
507 wpa_printf(MSG_DEBUG, "MLME: failed to allocate buffer for "
508 "disassoc frame");
509 return;
512 mgmt = (struct ieee80211_mgmt *) buf;
513 len = 24;
514 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
515 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
516 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
517 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
518 WLAN_FC_STYPE_DISASSOC);
519 len += 2;
520 mgmt->u.disassoc.reason_code = host_to_le16(reason);
522 ieee80211_sta_tx(wpa_s, buf, len);
523 os_free(buf);
527 static int ieee80211_privacy_mismatch(struct wpa_supplicant *wpa_s)
529 struct ieee80211_sta_bss *bss;
530 int res = 0;
532 if (wpa_s->mlme.mixed_cell ||
533 wpa_s->mlme.key_mgmt != KEY_MGMT_NONE)
534 return 0;
536 bss = ieee80211_bss_get(wpa_s, wpa_s->bssid);
537 if (bss == NULL)
538 return 0;
540 if (ieee80211_sta_wep_configured(wpa_s) !=
541 !!(bss->capability & WLAN_CAPABILITY_PRIVACY))
542 res = 1;
544 return res;
548 static void ieee80211_associate(struct wpa_supplicant *wpa_s)
550 wpa_s->mlme.assoc_tries++;
551 if (wpa_s->mlme.assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
552 wpa_printf(MSG_DEBUG, "MLME: association with AP " MACSTR
553 " timed out", MAC2STR(wpa_s->bssid));
554 return;
557 wpa_s->mlme.state = IEEE80211_ASSOCIATE;
558 wpa_printf(MSG_DEBUG, "MLME: associate with AP " MACSTR,
559 MAC2STR(wpa_s->bssid));
560 if (ieee80211_privacy_mismatch(wpa_s)) {
561 wpa_printf(MSG_DEBUG, "MLME: mismatch in privacy "
562 "configuration and mixed-cell disabled - abort "
563 "association");
564 return;
567 ieee80211_send_assoc(wpa_s);
569 ieee80211_reschedule_timer(wpa_s, IEEE80211_ASSOC_TIMEOUT);
573 static void ieee80211_associated(struct wpa_supplicant *wpa_s)
575 int disassoc;
577 /* TODO: start monitoring current AP signal quality and number of
578 * missed beacons. Scan other channels every now and then and search
579 * for better APs. */
580 /* TODO: remove expired BSSes */
582 wpa_s->mlme.state = IEEE80211_ASSOCIATED;
584 #if 0 /* FIX */
585 sta = sta_info_get(local, wpa_s->bssid);
586 if (sta == NULL) {
587 wpa_printf(MSG_DEBUG "MLME: No STA entry for own AP " MACSTR,
588 MAC2STR(wpa_s->bssid));
589 disassoc = 1;
590 } else {
591 disassoc = 0;
592 if (time_after(jiffies,
593 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
594 if (wpa_s->mlme.probereq_poll) {
595 wpa_printf(MSG_DEBUG "MLME: No ProbeResp from "
596 "current AP " MACSTR " - assume "
597 "out of range",
598 MAC2STR(wpa_s->bssid));
599 disassoc = 1;
600 } else {
601 ieee80211_send_probe_req(
602 wpa_s->bssid,
603 wpa_s->mlme.scan_ssid,
604 wpa_s->mlme.scan_ssid_len);
605 wpa_s->mlme.probereq_poll = 1;
607 } else {
608 wpa_s->mlme.probereq_poll = 0;
609 if (time_after(jiffies, wpa_s->mlme.last_probe +
610 IEEE80211_PROBE_INTERVAL)) {
611 wpa_s->mlme.last_probe = jiffies;
612 ieee80211_send_probe_req(wpa_s->bssid,
613 wpa_s->mlme.ssid,
614 wpa_s->mlme.ssid_len);
617 sta_info_release(local, sta);
619 #else
620 disassoc = 0;
621 #endif
622 if (disassoc) {
623 wpa_supplicant_event(wpa_s, EVENT_DISASSOC, NULL);
624 ieee80211_reschedule_timer(wpa_s,
625 IEEE80211_MONITORING_INTERVAL +
626 30000);
627 } else {
628 ieee80211_reschedule_timer(wpa_s,
629 IEEE80211_MONITORING_INTERVAL);
634 static void ieee80211_send_probe_req(struct wpa_supplicant *wpa_s,
635 const u8 *dst,
636 const u8 *ssid, size_t ssid_len)
638 u8 *buf;
639 size_t len;
640 struct ieee80211_mgmt *mgmt;
641 u8 *pos, *supp_rates;
642 u8 *esupp_rates = NULL;
643 int i;
645 buf = os_malloc(sizeof(*mgmt) + 200 + wpa_s->mlme.extra_probe_ie_len);
646 if (buf == NULL) {
647 wpa_printf(MSG_DEBUG, "MLME: failed to allocate buffer for "
648 "probe request");
649 return;
652 mgmt = (struct ieee80211_mgmt *) buf;
653 len = 24;
654 os_memset(mgmt, 0, 24);
655 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
656 WLAN_FC_STYPE_PROBE_REQ);
657 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
658 if (dst) {
659 os_memcpy(mgmt->da, dst, ETH_ALEN);
660 os_memcpy(mgmt->bssid, dst, ETH_ALEN);
661 } else {
662 os_memset(mgmt->da, 0xff, ETH_ALEN);
663 os_memset(mgmt->bssid, 0xff, ETH_ALEN);
665 pos = buf + len;
666 len += 2 + ssid_len;
667 *pos++ = WLAN_EID_SSID;
668 *pos++ = ssid_len;
669 os_memcpy(pos, ssid, ssid_len);
671 supp_rates = buf + len;
672 len += 2;
673 supp_rates[0] = WLAN_EID_SUPP_RATES;
674 supp_rates[1] = 0;
675 for (i = 0; i < wpa_s->mlme.num_curr_rates; i++) {
676 if (esupp_rates) {
677 pos = buf + len;
678 len++;
679 esupp_rates[1]++;
680 } else if (supp_rates[1] == 8) {
681 esupp_rates = pos;
682 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
683 esupp_rates[1] = 1;
684 pos = &esupp_rates[2];
685 len += 3;
686 } else {
687 pos = buf + len;
688 len++;
689 supp_rates[1]++;
691 *pos++ = wpa_s->mlme.curr_rates[i] / 5;
694 if (wpa_s->mlme.extra_probe_ie) {
695 os_memcpy(pos, wpa_s->mlme.extra_probe_ie,
696 wpa_s->mlme.extra_probe_ie_len);
697 len += wpa_s->mlme.extra_probe_ie_len;
700 ieee80211_sta_tx(wpa_s, buf, len);
701 os_free(buf);
705 extern int using_wep;
706 static int ieee80211_sta_wep_configured(struct wpa_supplicant *wpa_s)
708 #if 0 /* FIX */
709 if (sdata == NULL || sdata->default_key == NULL ||
710 sdata->default_key->alg != ALG_WEP)
711 return 0;
712 return 1;
713 #else
714 // return 0;
715 return using_wep;
716 #endif
720 static void ieee80211_auth_completed(struct wpa_supplicant *wpa_s)
722 wpa_printf(MSG_DEBUG, "MLME: authenticated");
723 wpa_s->mlme.authenticated = 1;
724 ieee80211_associate(wpa_s);
728 static void ieee80211_auth_challenge(struct wpa_supplicant *wpa_s,
729 struct ieee80211_mgmt *mgmt,
730 size_t len,
731 struct ieee80211_rx_status *rx_status)
733 u8 *pos;
734 struct ieee802_11_elems elems;
736 wpa_printf(MSG_DEBUG, "MLME: replying to auth challenge");
737 pos = mgmt->u.auth.variable;
738 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems, 0)
739 == ParseFailed) {
740 wpa_printf(MSG_DEBUG, "MLME: failed to parse Auth(challenge)");
741 return;
743 if (elems.challenge == NULL) {
744 wpa_printf(MSG_DEBUG, "MLME: no challenge IE in shared key "
745 "auth frame");
746 return;
748 ieee80211_send_auth(wpa_s, 3, elems.challenge - 2,
749 elems.challenge_len + 2, 1);
753 static void ieee80211_rx_mgmt_auth(struct wpa_supplicant *wpa_s,
754 struct ieee80211_mgmt *mgmt,
755 size_t len,
756 struct ieee80211_rx_status *rx_status)
758 struct wpa_ssid *ssid = wpa_s->current_ssid;
759 u16 auth_alg, auth_transaction, status_code;
760 int adhoc;
762 adhoc = ssid && ssid->mode == WPAS_MODE_IBSS;
764 if (wpa_s->mlme.state != IEEE80211_AUTHENTICATE && !adhoc) {
765 wpa_printf(MSG_DEBUG, "MLME: authentication frame received "
766 "from " MACSTR ", but not in authenticate state - "
767 "ignored", MAC2STR(mgmt->sa));
768 return;
771 if (len < 24 + 6) {
772 wpa_printf(MSG_DEBUG, "MLME: too short (%lu) authentication "
773 "frame received from " MACSTR " - ignored",
774 (unsigned long) len, MAC2STR(mgmt->sa));
775 return;
778 if (!adhoc && os_memcmp(wpa_s->bssid, mgmt->sa, ETH_ALEN) != 0) {
779 wpa_printf(MSG_DEBUG, "MLME: authentication frame received "
780 "from unknown AP (SA=" MACSTR " BSSID=" MACSTR
781 ") - ignored",
782 MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
783 return;
786 if (adhoc && os_memcmp(wpa_s->bssid, mgmt->bssid, ETH_ALEN) != 0) {
787 wpa_printf(MSG_DEBUG, "MLME: authentication frame received "
788 "from unknown BSSID (SA=" MACSTR " BSSID=" MACSTR
789 ") - ignored",
790 MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
791 return;
794 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
795 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
796 status_code = le_to_host16(mgmt->u.auth.status_code);
798 wpa_printf(MSG_DEBUG, "MLME: RX authentication from " MACSTR
799 " (alg=%d transaction=%d status=%d)",
800 MAC2STR(mgmt->sa), auth_alg, auth_transaction, status_code);
802 if (adhoc) {
803 /* IEEE 802.11 standard does not require authentication in IBSS
804 * networks and most implementations do not seem to use it.
805 * However, try to reply to authentication attempts if someone
806 * has actually implemented this.
807 * TODO: Could implement shared key authentication. */
808 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
809 wpa_printf(MSG_DEBUG, "MLME: unexpected IBSS "
810 "authentication frame (alg=%d "
811 "transaction=%d)",
812 auth_alg, auth_transaction);
813 return;
815 ieee80211_send_auth(wpa_s, 2, NULL, 0, 0);
818 if (auth_alg != wpa_s->mlme.auth_alg ||
819 auth_transaction != wpa_s->mlme.auth_transaction) {
820 wpa_printf(MSG_DEBUG, "MLME: unexpected authentication frame "
821 "(alg=%d transaction=%d)",
822 auth_alg, auth_transaction);
823 return;
826 if (status_code != WLAN_STATUS_SUCCESS) {
827 wpa_printf(MSG_DEBUG, "MLME: AP denied authentication "
828 "(auth_alg=%d code=%d)", wpa_s->mlme.auth_alg,
829 status_code);
830 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
831 const int num_algs = 3;
832 u8 algs[num_algs];
833 int i, pos;
834 algs[0] = algs[1] = algs[2] = 0xff;
835 if (wpa_s->mlme.auth_algs & WPA_AUTH_ALG_OPEN)
836 algs[0] = WLAN_AUTH_OPEN;
837 if (wpa_s->mlme.auth_algs & WPA_AUTH_ALG_SHARED)
838 algs[1] = WLAN_AUTH_SHARED_KEY;
839 if (wpa_s->mlme.auth_algs & WPA_AUTH_ALG_LEAP)
840 algs[2] = WLAN_AUTH_LEAP;
841 if (wpa_s->mlme.auth_alg == WLAN_AUTH_OPEN)
842 pos = 0;
843 else if (wpa_s->mlme.auth_alg == WLAN_AUTH_SHARED_KEY)
844 pos = 1;
845 else
846 pos = 2;
847 for (i = 0; i < num_algs; i++) {
848 pos++;
849 if (pos >= num_algs)
850 pos = 0;
851 if (algs[pos] == wpa_s->mlme.auth_alg ||
852 algs[pos] == 0xff)
853 continue;
854 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
855 !ieee80211_sta_wep_configured(wpa_s))
856 continue;
857 wpa_s->mlme.auth_alg = algs[pos];
858 wpa_printf(MSG_DEBUG, "MLME: set auth_alg=%d "
859 "for next try",
860 wpa_s->mlme.auth_alg);
861 break;
864 return;
867 switch (wpa_s->mlme.auth_alg) {
868 case WLAN_AUTH_OPEN:
869 case WLAN_AUTH_LEAP:
870 ieee80211_auth_completed(wpa_s);
871 break;
872 case WLAN_AUTH_SHARED_KEY:
873 if (wpa_s->mlme.auth_transaction == 4)
874 ieee80211_auth_completed(wpa_s);
875 else
876 ieee80211_auth_challenge(wpa_s, mgmt, len,
877 rx_status);
878 break;
879 #ifdef CONFIG_IEEE80211R
880 case WLAN_AUTH_FT:
882 union wpa_event_data data;
883 struct wpabuf *ric = NULL;
884 os_memset(&data, 0, sizeof(data));
885 data.ft_ies.ies = mgmt->u.auth.variable;
886 data.ft_ies.ies_len = len -
887 (mgmt->u.auth.variable - (u8 *) mgmt);
888 os_memcpy(data.ft_ies.target_ap, wpa_s->bssid, ETH_ALEN);
889 if (os_strcmp(wpa_s->driver->name, "test") == 0 &&
890 wpa_s->mlme.wmm_enabled) {
891 ric = wpabuf_alloc(200);
892 if (ric) {
893 /* Build simple RIC-Request: RDIE | TSPEC */
895 /* RIC Data (RDIE) */
896 wpabuf_put_u8(ric, WLAN_EID_RIC_DATA);
897 wpabuf_put_u8(ric, 4);
898 wpabuf_put_u8(ric, 0); /* RDIE Identifier */
899 wpabuf_put_u8(ric, 1); /* Resource Descriptor
900 * Count */
901 wpabuf_put_le16(ric, 0); /* Status Code */
903 /* WMM TSPEC */
904 ieee80211_build_tspec(ric);
906 data.ft_ies.ric_ies = wpabuf_head(ric);
907 data.ft_ies.ric_ies_len = wpabuf_len(ric);
911 wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &data);
912 wpabuf_free(ric);
913 ieee80211_auth_completed(wpa_s);
914 break;
916 #endif /* CONFIG_IEEE80211R */
921 static void ieee80211_rx_mgmt_deauth(struct wpa_supplicant *wpa_s,
922 struct ieee80211_mgmt *mgmt,
923 size_t len,
924 struct ieee80211_rx_status *rx_status)
926 u16 reason_code;
928 if (len < 24 + 2) {
929 wpa_printf(MSG_DEBUG, "MLME: too short (%lu) deauthentication "
930 "frame received from " MACSTR " - ignored",
931 (unsigned long) len, MAC2STR(mgmt->sa));
932 return;
935 if (os_memcmp(wpa_s->bssid, mgmt->sa, ETH_ALEN) != 0) {
936 wpa_printf(MSG_DEBUG, "MLME: deauthentication frame received "
937 "from unknown AP (SA=" MACSTR " BSSID=" MACSTR
938 ") - ignored",
939 MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
940 return;
943 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
945 wpa_printf(MSG_DEBUG, "MLME: RX deauthentication from " MACSTR
946 " (reason=%d)", MAC2STR(mgmt->sa), reason_code);
948 if (wpa_s->mlme.authenticated)
949 wpa_printf(MSG_DEBUG, "MLME: deauthenticated");
951 if (wpa_s->mlme.state == IEEE80211_AUTHENTICATE ||
952 wpa_s->mlme.state == IEEE80211_ASSOCIATE ||
953 wpa_s->mlme.state == IEEE80211_ASSOCIATED) {
954 wpa_s->mlme.state = IEEE80211_AUTHENTICATE;
955 ieee80211_reschedule_timer(wpa_s,
956 IEEE80211_RETRY_AUTH_INTERVAL);
959 ieee80211_set_associated(wpa_s, 0);
960 wpa_s->mlme.authenticated = 0;
964 static void ieee80211_rx_mgmt_disassoc(struct wpa_supplicant *wpa_s,
965 struct ieee80211_mgmt *mgmt,
966 size_t len,
967 struct ieee80211_rx_status *rx_status)
969 u16 reason_code;
971 if (len < 24 + 2) {
972 wpa_printf(MSG_DEBUG, "MLME: too short (%lu) disassociation "
973 "frame received from " MACSTR " - ignored",
974 (unsigned long) len, MAC2STR(mgmt->sa));
975 return;
978 if (os_memcmp(wpa_s->bssid, mgmt->sa, ETH_ALEN) != 0) {
979 wpa_printf(MSG_DEBUG, "MLME: disassociation frame received "
980 "from unknown AP (SA=" MACSTR " BSSID=" MACSTR
981 ") - ignored",
982 MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
983 return;
986 reason_code = le_to_host16(mgmt->u.disassoc.reason_code);
988 wpa_printf(MSG_DEBUG, "MLME: RX disassociation from " MACSTR
989 " (reason=%d)", MAC2STR(mgmt->sa), reason_code);
991 if (wpa_s->mlme.associated)
992 wpa_printf(MSG_DEBUG, "MLME: disassociated");
994 if (wpa_s->mlme.state == IEEE80211_ASSOCIATED) {
995 wpa_s->mlme.state = IEEE80211_ASSOCIATE;
996 ieee80211_reschedule_timer(wpa_s,
997 IEEE80211_RETRY_AUTH_INTERVAL);
1000 ieee80211_set_associated(wpa_s, 0);
1004 static void ieee80211_build_tspec(struct wpabuf *buf)
1006 struct wmm_tspec_element *tspec;
1007 int tid, up;
1009 tspec = wpabuf_put(buf, sizeof(*tspec));
1010 tspec->eid = WLAN_EID_VENDOR_SPECIFIC;
1011 tspec->length = sizeof(*tspec) - 2;
1012 tspec->oui[0] = 0x00;
1013 tspec->oui[1] = 0x50;
1014 tspec->oui[2] = 0xf2;
1015 tspec->oui_type = 2;
1016 tspec->oui_subtype = 2;
1017 tspec->version = 1;
1019 tid = 1;
1020 up = 6; /* Voice */
1021 tspec->ts_info[0] = (tid << 1) |
1022 (WMM_TSPEC_DIRECTION_BI_DIRECTIONAL << 5) |
1023 BIT(7);
1024 tspec->ts_info[1] = up << 3;
1025 tspec->nominal_msdu_size = host_to_le16(1530);
1026 tspec->mean_data_rate = host_to_le32(128000); /* bits per second */
1027 tspec->minimum_phy_rate = host_to_le32(6000000);
1028 tspec->surplus_bandwidth_allowance = host_to_le16(0x3000); /* 150% */
1032 static void ieee80211_tx_addts(struct wpa_supplicant *wpa_s)
1034 struct wpabuf *buf;
1035 struct ieee80211_mgmt *mgmt;
1036 size_t alen;
1038 wpa_printf(MSG_DEBUG, "MLME: Send ADDTS Request for Voice TSPEC");
1039 mgmt = NULL;
1040 alen = mgmt->u.action.u.wmm_action.variable - (u8 *) mgmt;
1042 buf = wpabuf_alloc(alen + sizeof(struct wmm_tspec_element));
1043 if (buf == NULL)
1044 return;
1046 mgmt = wpabuf_put(buf, alen);
1047 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
1048 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
1049 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
1050 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1051 WLAN_FC_STYPE_ACTION);
1052 mgmt->u.action.category = WLAN_ACTION_WMM;
1053 mgmt->u.action.u.wmm_action.action_code = WMM_ACTION_CODE_ADDTS_REQ;
1054 mgmt->u.action.u.wmm_action.dialog_token = 1;
1055 mgmt->u.action.u.wmm_action.status_code = 0;
1057 ieee80211_build_tspec(buf);
1059 ieee80211_sta_tx(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
1060 wpabuf_free(buf);
1064 static void ieee80211_rx_mgmt_assoc_resp(struct wpa_supplicant *wpa_s,
1065 struct ieee80211_mgmt *mgmt,
1066 size_t len,
1067 struct ieee80211_rx_status *rx_status,
1068 int reassoc)
1070 u8 rates[32];
1071 size_t rates_len;
1072 u16 capab_info, status_code, aid;
1073 struct ieee802_11_elems elems;
1074 u8 *pos;
1076 /* AssocResp and ReassocResp have identical structure, so process both
1077 * of them in this function. */
1079 if (wpa_s->mlme.state != IEEE80211_ASSOCIATE) {
1080 wpa_printf(MSG_DEBUG, "MLME: association frame received from "
1081 MACSTR ", but not in associate state - ignored",
1082 MAC2STR(mgmt->sa));
1083 return;
1086 if (len < 24 + 6) {
1087 wpa_printf(MSG_DEBUG, "MLME: too short (%lu) association "
1088 "frame received from " MACSTR " - ignored",
1089 (unsigned long) len, MAC2STR(mgmt->sa));
1090 return;
1093 if (os_memcmp(wpa_s->bssid, mgmt->sa, ETH_ALEN) != 0) {
1094 wpa_printf(MSG_DEBUG, "MLME: association frame received from "
1095 "unknown AP (SA=" MACSTR " BSSID=" MACSTR ") - "
1096 "ignored", MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
1097 return;
1100 capab_info = le_to_host16(mgmt->u.assoc_resp.capab_info);
1101 status_code = le_to_host16(mgmt->u.assoc_resp.status_code);
1102 aid = le_to_host16(mgmt->u.assoc_resp.aid);
1103 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1104 wpa_printf(MSG_DEBUG, "MLME: invalid aid value %d; bits 15:14 "
1105 "not set", aid);
1106 aid &= ~(BIT(15) | BIT(14));
1108 wpa_printf(MSG_DEBUG, "MLME: RX %sssocResp from " MACSTR
1109 " (capab=0x%x status=%d aid=%d)",
1110 reassoc ? "Rea" : "A", MAC2STR(mgmt->sa),
1111 capab_info, status_code, aid);
1113 pos = mgmt->u.assoc_resp.variable;
1114 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems, 0)
1115 == ParseFailed) {
1116 wpa_printf(MSG_DEBUG, "MLME: failed to parse AssocResp");
1117 return;
1120 if (status_code != WLAN_STATUS_SUCCESS) {
1121 wpa_printf(MSG_DEBUG, "MLME: AP denied association (code=%d)",
1122 status_code);
1123 #ifdef CONFIG_IEEE80211W
1124 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
1125 elems.timeout_int && elems.timeout_int_len == 5 &&
1126 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
1127 u32 tu, ms;
1128 tu = WPA_GET_LE32(elems.timeout_int + 1);
1129 ms = tu * 1024 / 1000;
1130 wpa_printf(MSG_DEBUG, "MLME: AP rejected association "
1131 "temporarily; comeback duration %u TU "
1132 "(%u ms)", tu, ms);
1133 if (ms > IEEE80211_ASSOC_TIMEOUT) {
1134 wpa_printf(MSG_DEBUG, "MLME: Update timer "
1135 "based on comeback duration");
1136 ieee80211_reschedule_timer(wpa_s, ms);
1139 #endif /* CONFIG_IEEE80211W */
1140 return;
1143 if (elems.supp_rates == NULL) {
1144 wpa_printf(MSG_DEBUG, "MLME: no SuppRates element in "
1145 "AssocResp");
1146 return;
1149 if (wpa_s->mlme.auth_alg == WLAN_AUTH_FT) {
1150 if (!reassoc) {
1151 wpa_printf(MSG_DEBUG, "MLME: AP tried to use "
1152 "association, not reassociation, response "
1153 "with FT");
1154 return;
1156 if (wpa_ft_validate_reassoc_resp(
1157 wpa_s->wpa, pos, len - (pos - (u8 *) mgmt),
1158 mgmt->sa) < 0) {
1159 wpa_printf(MSG_DEBUG, "MLME: FT validation of Reassoc"
1160 "Resp failed");
1161 return;
1163 } else if (wpa_sm_set_ft_params(wpa_s->wpa, pos,
1164 len - (pos - (u8 *) mgmt)) < 0)
1165 return;
1167 wpa_printf(MSG_DEBUG, "MLME: associated");
1168 wpa_s->mlme.aid = aid;
1169 wpa_s->mlme.ap_capab = capab_info;
1171 os_free(wpa_s->mlme.assocresp_ies);
1172 wpa_s->mlme.assocresp_ies_len = len - (pos - (u8 *) mgmt);
1173 wpa_s->mlme.assocresp_ies = os_malloc(wpa_s->mlme.assocresp_ies_len);
1174 if (wpa_s->mlme.assocresp_ies) {
1175 os_memcpy(wpa_s->mlme.assocresp_ies, pos,
1176 wpa_s->mlme.assocresp_ies_len);
1179 ieee80211_set_associated(wpa_s, 1);
1181 rates_len = elems.supp_rates_len;
1182 if (rates_len > sizeof(rates))
1183 rates_len = sizeof(rates);
1184 os_memcpy(rates, elems.supp_rates, rates_len);
1185 if (elems.ext_supp_rates) {
1186 size_t _len = elems.ext_supp_rates_len;
1187 if (_len > sizeof(rates) - rates_len)
1188 _len = sizeof(rates) - rates_len;
1189 os_memcpy(rates + rates_len, elems.ext_supp_rates, _len);
1190 rates_len += _len;
1193 if (wpa_drv_set_bssid(wpa_s, wpa_s->bssid) < 0) {
1194 wpa_printf(MSG_DEBUG, "MLME: failed to set BSSID for the "
1195 "netstack");
1197 if (wpa_drv_set_ssid(wpa_s, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len) <
1198 0) {
1199 wpa_printf(MSG_DEBUG, "MLME: failed to set SSID for the "
1200 "netstack");
1202 if (wpa_drv_set_associd(wpa_s, wpa_s->mlme.aid) < 0) {
1203 wpa_printf(MSG_DEBUG, "MLME: failed to set AID for the "
1204 "netstack");
1206 if (wpa_drv_set_capabilities(wpa_s, wpa_s->mlme.ap_capab) < 0) {
1207 wpa_printf(MSG_DEBUG, "MLME: failed to set negotiated"
1208 "capabilities for the netstack");
1211 /* Remove STA entry before adding a new one just in case to avoid
1212 * problems with existing configuration (e.g., keys). */
1213 wpa_drv_mlme_remove_sta(wpa_s, wpa_s->bssid);
1214 if (wpa_drv_mlme_add_sta(wpa_s, wpa_s->bssid, rates, rates_len) < 0) {
1215 wpa_printf(MSG_DEBUG, "MLME: failed to add STA entry to the "
1216 "netstack");
1219 if (elems.wmm && wpa_s->mlme.wmm_enabled)
1220 ieee80211_sta_wmm_params(wpa_s, elems.wmm, elems.wmm_len);
1222 ieee80211_associated(wpa_s);
1224 if (wpa_s->mlme.auth_alg != WLAN_AUTH_FT &&
1225 os_strcmp(wpa_s->driver->name, "test") == 0 &&
1226 elems.wmm && wpa_s->mlme.wmm_enabled) {
1227 /* Test WMM-AC - send ADDTS for WMM TSPEC */
1228 ieee80211_tx_addts(wpa_s);
1233 /* Caller must hold local->sta_bss_lock */
1234 static void __ieee80211_bss_hash_add(struct wpa_supplicant *wpa_s,
1235 struct ieee80211_sta_bss *bss)
1237 bss->hnext = wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)];
1238 wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1242 /* Caller must hold local->sta_bss_lock */
1243 static void __ieee80211_bss_hash_del(struct wpa_supplicant *wpa_s,
1244 struct ieee80211_sta_bss *bss)
1246 struct ieee80211_sta_bss *b, *prev = NULL;
1247 b = wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)];
1248 while (b) {
1249 if (b == bss) {
1250 if (prev == NULL) {
1251 wpa_s->mlme.sta_bss_hash[STA_HASH(bss->bssid)]
1252 = bss->hnext;
1253 } else {
1254 prev->hnext = bss->hnext;
1256 break;
1258 prev = b;
1259 b = b->hnext;
1264 static struct ieee80211_sta_bss *
1265 ieee80211_bss_add(struct wpa_supplicant *wpa_s, const u8 *bssid)
1267 struct ieee80211_sta_bss *bss;
1269 bss = os_zalloc(sizeof(*bss));
1270 if (bss == NULL)
1271 return NULL;
1272 os_memcpy(bss->bssid, bssid, ETH_ALEN);
1274 /* TODO: order by RSSI? */
1275 bss->next = wpa_s->mlme.sta_bss_list;
1276 wpa_s->mlme.sta_bss_list = bss;
1277 __ieee80211_bss_hash_add(wpa_s, bss);
1278 return bss;
1282 static struct ieee80211_sta_bss *
1283 ieee80211_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid)
1285 struct ieee80211_sta_bss *bss;
1287 bss = wpa_s->mlme.sta_bss_hash[STA_HASH(bssid)];
1288 while (bss) {
1289 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
1290 break;
1291 bss = bss->hnext;
1293 return bss;
1297 static void ieee80211_bss_free(struct wpa_supplicant *wpa_s,
1298 struct ieee80211_sta_bss *bss)
1300 __ieee80211_bss_hash_del(wpa_s, bss);
1301 os_free(bss->ie);
1302 os_free(bss->wpa_ie);
1303 os_free(bss->rsn_ie);
1304 os_free(bss->wmm_ie);
1305 os_free(bss->mdie);
1306 os_free(bss);
1310 static void ieee80211_bss_list_deinit(struct wpa_supplicant *wpa_s)
1312 struct ieee80211_sta_bss *bss, *prev;
1314 bss = wpa_s->mlme.sta_bss_list;
1315 wpa_s->mlme.sta_bss_list = NULL;
1316 while (bss) {
1317 prev = bss;
1318 bss = bss->next;
1319 ieee80211_bss_free(wpa_s, prev);
1324 static void ieee80211_bss_info(struct wpa_supplicant *wpa_s,
1325 struct ieee80211_mgmt *mgmt,
1326 size_t len,
1327 struct ieee80211_rx_status *rx_status,
1328 int beacon)
1330 struct ieee802_11_elems elems;
1331 size_t baselen;
1332 int channel, /*invalid = 0,*/ clen;
1333 struct ieee80211_sta_bss *bss;
1334 u64 timestamp;
1335 u8 *pos, *ie_pos;
1336 size_t ie_len;
1338 if (!beacon && os_memcmp(mgmt->da, wpa_s->own_addr, ETH_ALEN))
1339 return; /* ignore ProbeResp to foreign address */
1341 #if 0
1342 wpa_printf(MSG_MSGDUMP, "MLME: RX %s from " MACSTR " to " MACSTR,
1343 beacon ? "Beacon" : "Probe Response",
1344 MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
1345 #endif
1347 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1348 if (baselen > len)
1349 return;
1351 pos = mgmt->u.beacon.timestamp;
1352 timestamp = WPA_GET_LE64(pos);
1354 #if 0 /* FIX */
1355 if (local->conf.mode == IW_MODE_ADHOC && beacon &&
1356 os_memcmp(mgmt->bssid, local->bssid, ETH_ALEN) == 0) {
1357 #ifdef IEEE80211_IBSS_DEBUG
1358 static unsigned long last_tsf_debug = 0;
1359 u64 tsf;
1360 if (local->hw->get_tsf)
1361 tsf = local->hw->get_tsf(local->mdev);
1362 else
1363 tsf = -1LLU;
1364 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
1365 wpa_printf(MSG_DEBUG, "RX beacon SA=" MACSTR " BSSID="
1366 MACSTR " TSF=0x%llx BCN=0x%llx diff=%lld "
1367 "@%ld",
1368 MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid),
1369 tsf, timestamp, tsf - timestamp, jiffies);
1370 last_tsf_debug = jiffies;
1372 #endif /* IEEE80211_IBSS_DEBUG */
1374 #endif
1376 ie_pos = mgmt->u.beacon.variable;
1377 ie_len = len - baselen;
1378 #if 0
1379 if (ieee802_11_parse_elems(ie_pos, ie_len, &elems, 0) == ParseFailed)
1380 invalid = 1;
1381 #else
1382 ieee802_11_parse_elems(ie_pos, ie_len, &elems, 0);
1383 #endif
1385 #if 0 /* FIX */
1386 if (local->conf.mode == IW_MODE_ADHOC && elems.supp_rates &&
1387 os_memcmp(mgmt->bssid, local->bssid, ETH_ALEN) == 0 &&
1388 (sta = sta_info_get(local, mgmt->sa))) {
1389 struct ieee80211_rate *rates;
1390 size_t num_rates;
1391 u32 supp_rates, prev_rates;
1392 int i, j, oper_mode;
1394 rates = local->curr_rates;
1395 num_rates = local->num_curr_rates;
1396 oper_mode = wpa_s->mlme.sta_scanning ?
1397 local->scan_oper_phymode : local->conf.phymode;
1398 for (i = 0; i < local->hw->num_modes; i++) {
1399 struct ieee80211_hw_modes *mode = &local->hw->modes[i];
1400 if (oper_mode == mode->mode) {
1401 rates = mode->rates;
1402 num_rates = mode->num_rates;
1403 break;
1407 supp_rates = 0;
1408 for (i = 0; i < elems.supp_rates_len +
1409 elems.ext_supp_rates_len; i++) {
1410 u8 rate = 0;
1411 int own_rate;
1412 if (i < elems.supp_rates_len)
1413 rate = elems.supp_rates[i];
1414 else if (elems.ext_supp_rates)
1415 rate = elems.ext_supp_rates
1416 [i - elems.supp_rates_len];
1417 own_rate = 5 * (rate & 0x7f);
1418 if (oper_mode == MODE_ATHEROS_TURBO)
1419 own_rate *= 2;
1420 for (j = 0; j < num_rates; j++)
1421 if (rates[j].rate == own_rate)
1422 supp_rates |= BIT(j);
1425 prev_rates = sta->supp_rates;
1426 sta->supp_rates &= supp_rates;
1427 if (sta->supp_rates == 0) {
1428 /* No matching rates - this should not really happen.
1429 * Make sure that at least one rate is marked
1430 * supported to avoid issues with TX rate ctrl. */
1431 sta->supp_rates = wpa_s->mlme.supp_rates_bits;
1433 if (sta->supp_rates != prev_rates) {
1434 wpa_printf(MSG_DEBUG, "MLME: updated supp_rates set "
1435 "for " MACSTR " based on beacon info "
1436 "(0x%x & 0x%x -> 0x%x)",
1437 MAC2STR(sta->addr), prev_rates,
1438 supp_rates, sta->supp_rates);
1440 sta_info_release(local, sta);
1442 #endif
1444 if (elems.ssid == NULL)
1445 return;
1447 if (elems.ds_params && elems.ds_params_len == 1)
1448 channel = elems.ds_params[0];
1449 else
1450 channel = rx_status->channel;
1452 bss = ieee80211_bss_get(wpa_s, mgmt->bssid);
1453 if (bss == NULL) {
1454 bss = ieee80211_bss_add(wpa_s, mgmt->bssid);
1455 if (bss == NULL)
1456 return;
1457 } else {
1458 #if 0
1459 /* TODO: order by RSSI? */
1460 spin_lock_bh(&local->sta_bss_lock);
1461 list_move_tail(&bss->list, &local->sta_bss_list);
1462 spin_unlock_bh(&local->sta_bss_lock);
1463 #endif
1466 if (bss->probe_resp && beacon) {
1467 /* Do not allow beacon to override data from Probe Response. */
1468 return;
1471 bss->beacon_int = le_to_host16(mgmt->u.beacon.beacon_int);
1472 bss->capability = le_to_host16(mgmt->u.beacon.capab_info);
1474 if (bss->ie == NULL || bss->ie_len < ie_len) {
1475 os_free(bss->ie);
1476 bss->ie = os_malloc(ie_len);
1478 if (bss->ie) {
1479 os_memcpy(bss->ie, ie_pos, ie_len);
1480 bss->ie_len = ie_len;
1483 if (elems.ssid && elems.ssid_len <= MAX_SSID_LEN) {
1484 os_memcpy(bss->ssid, elems.ssid, elems.ssid_len);
1485 bss->ssid_len = elems.ssid_len;
1488 bss->supp_rates_len = 0;
1489 if (elems.supp_rates) {
1490 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1491 if (clen > elems.supp_rates_len)
1492 clen = elems.supp_rates_len;
1493 os_memcpy(&bss->supp_rates[bss->supp_rates_len],
1494 elems.supp_rates, clen);
1495 bss->supp_rates_len += clen;
1497 if (elems.ext_supp_rates) {
1498 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1499 if (clen > elems.ext_supp_rates_len)
1500 clen = elems.ext_supp_rates_len;
1501 os_memcpy(&bss->supp_rates[bss->supp_rates_len],
1502 elems.ext_supp_rates, clen);
1503 bss->supp_rates_len += clen;
1506 if (elems.wpa_ie &&
1507 (bss->wpa_ie == NULL || bss->wpa_ie_len != elems.wpa_ie_len ||
1508 os_memcmp(bss->wpa_ie, elems.wpa_ie, elems.wpa_ie_len))) {
1509 os_free(bss->wpa_ie);
1510 bss->wpa_ie = os_malloc(elems.wpa_ie_len + 2);
1511 if (bss->wpa_ie) {
1512 os_memcpy(bss->wpa_ie, elems.wpa_ie - 2,
1513 elems.wpa_ie_len + 2);
1514 bss->wpa_ie_len = elems.wpa_ie_len + 2;
1515 } else
1516 bss->wpa_ie_len = 0;
1517 } else if (!elems.wpa_ie && bss->wpa_ie) {
1518 os_free(bss->wpa_ie);
1519 bss->wpa_ie = NULL;
1520 bss->wpa_ie_len = 0;
1523 if (elems.rsn_ie &&
1524 (bss->rsn_ie == NULL || bss->rsn_ie_len != elems.rsn_ie_len ||
1525 os_memcmp(bss->rsn_ie, elems.rsn_ie, elems.rsn_ie_len))) {
1526 os_free(bss->rsn_ie);
1527 bss->rsn_ie = os_malloc(elems.rsn_ie_len + 2);
1528 if (bss->rsn_ie) {
1529 os_memcpy(bss->rsn_ie, elems.rsn_ie - 2,
1530 elems.rsn_ie_len + 2);
1531 bss->rsn_ie_len = elems.rsn_ie_len + 2;
1532 } else
1533 bss->rsn_ie_len = 0;
1534 } else if (!elems.rsn_ie && bss->rsn_ie) {
1535 os_free(bss->rsn_ie);
1536 bss->rsn_ie = NULL;
1537 bss->rsn_ie_len = 0;
1540 if (elems.wmm &&
1541 (bss->wmm_ie == NULL || bss->wmm_ie_len != elems.wmm_len ||
1542 os_memcmp(bss->wmm_ie, elems.wmm, elems.wmm_len))) {
1543 os_free(bss->wmm_ie);
1544 bss->wmm_ie = os_malloc(elems.wmm_len + 2);
1545 if (bss->wmm_ie) {
1546 os_memcpy(bss->wmm_ie, elems.wmm - 2,
1547 elems.wmm_len + 2);
1548 bss->wmm_ie_len = elems.wmm_len + 2;
1549 } else
1550 bss->wmm_ie_len = 0;
1551 } else if (!elems.wmm && bss->wmm_ie) {
1552 os_free(bss->wmm_ie);
1553 bss->wmm_ie = NULL;
1554 bss->wmm_ie_len = 0;
1557 #ifdef CONFIG_IEEE80211R
1558 if (elems.mdie &&
1559 (bss->mdie == NULL || bss->mdie_len != elems.mdie_len ||
1560 os_memcmp(bss->mdie, elems.mdie, elems.mdie_len))) {
1561 os_free(bss->mdie);
1562 bss->mdie = os_malloc(elems.mdie_len + 2);
1563 if (bss->mdie) {
1564 os_memcpy(bss->mdie, elems.mdie - 2,
1565 elems.mdie_len + 2);
1566 bss->mdie_len = elems.mdie_len + 2;
1567 } else
1568 bss->mdie_len = 0;
1569 } else if (!elems.mdie && bss->mdie) {
1570 os_free(bss->mdie);
1571 bss->mdie = NULL;
1572 bss->mdie_len = 0;
1574 #endif /* CONFIG_IEEE80211R */
1576 bss->hw_mode = wpa_s->mlme.phymode;
1577 bss->channel = channel;
1578 bss->freq = wpa_s->mlme.freq;
1579 if (channel != wpa_s->mlme.channel &&
1580 (wpa_s->mlme.phymode == HOSTAPD_MODE_IEEE80211G ||
1581 wpa_s->mlme.phymode == HOSTAPD_MODE_IEEE80211B) &&
1582 channel >= 1 && channel <= 14) {
1583 static const int freq_list[] = {
1584 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1585 2447, 2452, 2457, 2462, 2467, 2472, 2484
1587 /* IEEE 802.11g/b mode can receive packets from neighboring
1588 * channels, so map the channel into frequency. */
1589 bss->freq = freq_list[channel - 1];
1591 bss->timestamp = timestamp;
1592 os_get_time(&bss->last_update);
1593 bss->rssi = rx_status->ssi;
1594 if (!beacon)
1595 bss->probe_resp++;
1599 static void ieee80211_rx_mgmt_probe_resp(struct wpa_supplicant *wpa_s,
1600 struct ieee80211_mgmt *mgmt,
1601 size_t len,
1602 struct ieee80211_rx_status *rx_status)
1604 ieee80211_bss_info(wpa_s, mgmt, len, rx_status, 0);
1608 static void ieee80211_rx_mgmt_beacon(struct wpa_supplicant *wpa_s,
1609 struct ieee80211_mgmt *mgmt,
1610 size_t len,
1611 struct ieee80211_rx_status *rx_status)
1613 int use_protection;
1614 size_t baselen;
1615 struct ieee802_11_elems elems;
1617 ieee80211_bss_info(wpa_s, mgmt, len, rx_status, 1);
1619 if (!wpa_s->mlme.associated ||
1620 os_memcmp(wpa_s->bssid, mgmt->bssid, ETH_ALEN) != 0)
1621 return;
1623 /* Process beacon from the current BSS */
1624 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1625 if (baselen > len)
1626 return;
1628 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1629 &elems, 0) == ParseFailed)
1630 return;
1632 use_protection = 0;
1633 if (elems.erp_info && elems.erp_info_len >= 1) {
1634 use_protection =
1635 (elems.erp_info[0] & ERP_INFO_USE_PROTECTION) != 0;
1638 if (use_protection != !!wpa_s->mlme.use_protection) {
1639 wpa_printf(MSG_DEBUG, "MLME: CTS protection %s (BSSID=" MACSTR
1640 ")",
1641 use_protection ? "enabled" : "disabled",
1642 MAC2STR(wpa_s->bssid));
1643 wpa_s->mlme.use_protection = use_protection ? 1 : 0;
1644 wpa_s->mlme.cts_protect_erp_frames = use_protection;
1647 if (elems.wmm && wpa_s->mlme.wmm_enabled) {
1648 ieee80211_sta_wmm_params(wpa_s, elems.wmm,
1649 elems.wmm_len);
1654 static void ieee80211_rx_mgmt_probe_req(struct wpa_supplicant *wpa_s,
1655 struct ieee80211_mgmt *mgmt,
1656 size_t len,
1657 struct ieee80211_rx_status *rx_status)
1659 int tx_last_beacon, adhoc;
1660 #if 0 /* FIX */
1661 struct ieee80211_mgmt *resp;
1662 #endif
1663 u8 *pos, *end;
1664 struct wpa_ssid *ssid = wpa_s->current_ssid;
1666 adhoc = ssid && ssid->mode == WPAS_MODE_IBSS;
1668 if (!adhoc || wpa_s->mlme.state != IEEE80211_IBSS_JOINED ||
1669 len < 24 + 2 || wpa_s->mlme.probe_resp == NULL)
1670 return;
1672 #if 0 /* FIX */
1673 if (local->hw->tx_last_beacon)
1674 tx_last_beacon = local->hw->tx_last_beacon(local->mdev);
1675 else
1676 #endif
1677 tx_last_beacon = 1;
1679 #ifdef IEEE80211_IBSS_DEBUG
1680 wpa_printf(MSG_DEBUG, "MLME: RX ProbeReq SA=" MACSTR " DA=" MACSTR
1681 " BSSID=" MACSTR " (tx_last_beacon=%d)",
1682 MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
1683 MAC2STR(mgmt->bssid), tx_last_beacon);
1684 #endif /* IEEE80211_IBSS_DEBUG */
1686 if (!tx_last_beacon)
1687 return;
1689 if (os_memcmp(mgmt->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1690 os_memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1691 return;
1693 end = ((u8 *) mgmt) + len;
1694 pos = mgmt->u.probe_req.variable;
1695 if (pos[0] != WLAN_EID_SSID ||
1696 pos + 2 + pos[1] > end) {
1697 wpa_printf(MSG_DEBUG, "MLME: Invalid SSID IE in ProbeReq from "
1698 MACSTR, MAC2STR(mgmt->sa));
1699 return;
1701 if (pos[1] != 0 &&
1702 (pos[1] != wpa_s->mlme.ssid_len ||
1703 os_memcmp(pos + 2, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len) != 0))
1705 /* Ignore ProbeReq for foreign SSID */
1706 return;
1709 #if 0 /* FIX */
1710 /* Reply with ProbeResp */
1711 skb = skb_copy(wpa_s->mlme.probe_resp, GFP_ATOMIC);
1712 if (skb == NULL)
1713 return;
1715 resp = (struct ieee80211_mgmt *) skb->data;
1716 os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
1717 #ifdef IEEE80211_IBSS_DEBUG
1718 wpa_printf(MSG_DEBUG, "MLME: Sending ProbeResp to " MACSTR,
1719 MAC2STR(resp->da));
1720 #endif /* IEEE80211_IBSS_DEBUG */
1721 ieee80211_sta_tx(wpa_s, skb, 0, 1);
1722 #endif
1726 #ifdef CONFIG_IEEE80211R
1727 static void ieee80211_rx_mgmt_ft_action(struct wpa_supplicant *wpa_s,
1728 struct ieee80211_mgmt *mgmt,
1729 size_t len,
1730 struct ieee80211_rx_status *rx_status)
1732 union wpa_event_data data;
1733 u16 status;
1734 u8 *sta_addr, *target_ap_addr;
1736 if (len < 24 + 1 + sizeof(mgmt->u.action.u.ft_action_resp)) {
1737 wpa_printf(MSG_DEBUG, "MLME: Too short FT Action frame");
1738 return;
1742 * Only FT Action Response is needed for now since reservation
1743 * protocol is not supported.
1745 if (mgmt->u.action.u.ft_action_resp.action != 2) {
1746 wpa_printf(MSG_DEBUG, "MLME: Unexpected FT Action %d",
1747 mgmt->u.action.u.ft_action_resp.action);
1748 return;
1751 status = le_to_host16(mgmt->u.action.u.ft_action_resp.status_code);
1752 sta_addr = mgmt->u.action.u.ft_action_resp.sta_addr;
1753 target_ap_addr = mgmt->u.action.u.ft_action_resp.target_ap_addr;
1754 wpa_printf(MSG_DEBUG, "MLME: Received FT Action Response: STA " MACSTR
1755 " TargetAP " MACSTR " Status Code %d",
1756 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
1757 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
1758 wpa_printf(MSG_DEBUG, "MLME: Foreign STA Address " MACSTR
1759 " in FT Action Response", MAC2STR(sta_addr));
1760 return;
1763 if (status) {
1764 wpa_printf(MSG_DEBUG, "MLME: FT Action Response indicates "
1765 "failure (status code %d)", status);
1766 /* TODO: report error to FT code(?) */
1767 return;
1770 os_memset(&data, 0, sizeof(data));
1771 data.ft_ies.ies = mgmt->u.action.u.ft_action_resp.variable;
1772 data.ft_ies.ies_len = len - (mgmt->u.action.u.ft_action_resp.variable -
1773 (u8 *) mgmt);
1774 data.ft_ies.ft_action = 1;
1775 os_memcpy(data.ft_ies.target_ap, target_ap_addr, ETH_ALEN);
1776 wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &data);
1777 /* TODO: should only re-associate, if EVENT_FT_RESPONSE was processed
1778 * successfully */
1779 wpa_s->mlme.prev_bssid_set = 1;
1780 wpa_s->mlme.auth_alg = WLAN_AUTH_FT;
1781 os_memcpy(wpa_s->mlme.prev_bssid, wpa_s->bssid, ETH_ALEN);
1782 os_memcpy(wpa_s->bssid, target_ap_addr, ETH_ALEN);
1783 ieee80211_associate(wpa_s);
1785 #endif /* CONFIG_IEEE80211R */
1788 #ifdef CONFIG_IEEE80211W
1790 /* MLME-SAQuery.response */
1791 static int ieee80211_sta_send_sa_query_resp(struct wpa_supplicant *wpa_s,
1792 const u8 *addr, const u8 *trans_id)
1794 struct ieee80211_mgmt *mgmt;
1795 int res;
1796 size_t len;
1798 mgmt = os_zalloc(sizeof(*mgmt));
1799 if (mgmt == NULL) {
1800 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
1801 "SA Query action frame");
1802 return -1;
1805 len = 24;
1806 os_memcpy(mgmt->da, addr, ETH_ALEN);
1807 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
1808 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
1809 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1810 WLAN_FC_STYPE_ACTION);
1811 mgmt->u.action.category = WLAN_ACTION_SA_QUERY;
1812 mgmt->u.action.u.sa_query_resp.action = WLAN_SA_QUERY_RESPONSE;
1813 os_memcpy(mgmt->u.action.u.sa_query_resp.trans_id, trans_id,
1814 WLAN_SA_QUERY_TR_ID_LEN);
1815 len += 1 + sizeof(mgmt->u.action.u.sa_query_resp);
1817 res = ieee80211_sta_tx(wpa_s, (u8 *) mgmt, len);
1818 os_free(mgmt);
1820 return res;
1824 static void ieee80211_rx_mgmt_sa_query_action(
1825 struct wpa_supplicant *wpa_s, struct ieee80211_mgmt *mgmt, size_t len,
1826 struct ieee80211_rx_status *rx_status)
1828 if (len < 24 + 1 + sizeof(mgmt->u.action.u.sa_query_req)) {
1829 wpa_printf(MSG_DEBUG, "MLME: Too short SA Query Action frame");
1830 return;
1833 if (mgmt->u.action.u.sa_query_req.action != WLAN_SA_QUERY_REQUEST) {
1834 wpa_printf(MSG_DEBUG, "MLME: Unexpected SA Query Action %d",
1835 mgmt->u.action.u.sa_query_req.action);
1836 return;
1839 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
1840 wpa_printf(MSG_DEBUG, "MLME: Ignore SA Query from unknown "
1841 "source " MACSTR, MAC2STR(mgmt->sa));
1842 return;
1845 if (wpa_s->mlme.state == IEEE80211_ASSOCIATE) {
1846 wpa_printf(MSG_DEBUG, "MLME: Ignore SA query request during "
1847 "association process");
1848 return;
1851 wpa_printf(MSG_DEBUG, "MLME: Replying to SA Query request");
1852 ieee80211_sta_send_sa_query_resp(wpa_s, mgmt->sa, mgmt->u.action.u.
1853 sa_query_req.trans_id);
1856 #endif /* CONFIG_IEEE80211W */
1859 static void dump_tspec(struct wmm_tspec_element *tspec)
1861 int up, psb, dir, tid;
1862 u16 val;
1864 up = (tspec->ts_info[1] >> 3) & 0x07;
1865 psb = (tspec->ts_info[1] >> 2) & 0x01;
1866 dir = (tspec->ts_info[0] >> 5) & 0x03;
1867 tid = (tspec->ts_info[0] >> 1) & 0x0f;
1868 wpa_printf(MSG_DEBUG, "WMM: TS Info: UP=%d PSB=%d Direction=%d TID=%d",
1869 up, psb, dir, tid);
1870 val = le_to_host16(tspec->nominal_msdu_size);
1871 wpa_printf(MSG_DEBUG, "WMM: Nominal MSDU Size: %d%s",
1872 val & 0x7fff, val & 0x8000 ? " (fixed)" : "");
1873 wpa_printf(MSG_DEBUG, "WMM: Mean Data Rate: %u bps",
1874 le_to_host32(tspec->mean_data_rate));
1875 wpa_printf(MSG_DEBUG, "WMM: Minimum PHY Rate: %u bps",
1876 le_to_host32(tspec->minimum_phy_rate));
1877 val = le_to_host16(tspec->surplus_bandwidth_allowance);
1878 wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance: %u.%04u",
1879 val >> 13, 10000 * (val & 0x1fff) / 0x2000);
1880 val = le_to_host16(tspec->medium_time);
1881 wpa_printf(MSG_DEBUG, "WMM: Medium Time: %u (= %u usec/sec)",
1882 val, 32 * val);
1886 static int is_wmm_tspec(const u8 *ie, size_t len)
1888 const struct wmm_tspec_element *tspec;
1890 if (len < sizeof(*tspec))
1891 return 0;
1893 tspec = (const struct wmm_tspec_element *) ie;
1894 if (tspec->eid != WLAN_EID_VENDOR_SPECIFIC ||
1895 tspec->length < sizeof(*tspec) - 2 ||
1896 tspec->oui[0] != 0x00 || tspec->oui[1] != 0x50 ||
1897 tspec->oui[2] != 0xf2 || tspec->oui_type != 2 ||
1898 tspec->oui_subtype != 2 || tspec->version != 1)
1899 return 0;
1901 return 1;
1905 static void ieee80211_rx_addts_resp(
1906 struct wpa_supplicant *wpa_s, struct ieee80211_mgmt *mgmt, size_t len,
1907 size_t var_len)
1909 struct wmm_tspec_element *tspec;
1911 wpa_printf(MSG_DEBUG, "WMM: Received ADDTS Response");
1912 wpa_hexdump(MSG_MSGDUMP, "WMM: ADDTS Response IE(s)",
1913 mgmt->u.action.u.wmm_action.variable, var_len);
1914 if (!is_wmm_tspec(mgmt->u.action.u.wmm_action.variable, var_len))
1915 return;
1916 tspec = (struct wmm_tspec_element *)
1917 mgmt->u.action.u.wmm_action.variable;
1918 dump_tspec(tspec);
1922 static void ieee80211_rx_delts(
1923 struct wpa_supplicant *wpa_s, struct ieee80211_mgmt *mgmt, size_t len,
1924 size_t var_len)
1926 struct wmm_tspec_element *tspec;
1928 wpa_printf(MSG_DEBUG, "WMM: Received DELTS");
1929 wpa_hexdump(MSG_MSGDUMP, "WMM: DELTS IE(s)",
1930 mgmt->u.action.u.wmm_action.variable, var_len);
1931 if (!is_wmm_tspec(mgmt->u.action.u.wmm_action.variable, var_len))
1932 return;
1933 tspec = (struct wmm_tspec_element *)
1934 mgmt->u.action.u.wmm_action.variable;
1935 dump_tspec(tspec);
1939 static void ieee80211_rx_mgmt_wmm_action(
1940 struct wpa_supplicant *wpa_s, struct ieee80211_mgmt *mgmt, size_t len,
1941 struct ieee80211_rx_status *rx_status)
1943 size_t alen;
1945 alen = mgmt->u.action.u.wmm_action.variable - (u8 *) mgmt;
1946 if (len < alen) {
1947 wpa_printf(MSG_DEBUG, "WMM: Received Action frame too short");
1948 return;
1951 wpa_printf(MSG_DEBUG, "WMM: Received Action frame: Action Code %d, "
1952 "Dialog Token %d, Status Code %d",
1953 mgmt->u.action.u.wmm_action.action_code,
1954 mgmt->u.action.u.wmm_action.dialog_token,
1955 mgmt->u.action.u.wmm_action.status_code);
1957 switch (mgmt->u.action.u.wmm_action.action_code) {
1958 case WMM_ACTION_CODE_ADDTS_RESP:
1959 ieee80211_rx_addts_resp(wpa_s, mgmt, len, len - alen);
1960 break;
1961 case WMM_ACTION_CODE_DELTS:
1962 ieee80211_rx_delts(wpa_s, mgmt, len, len - alen);
1963 break;
1964 default:
1965 wpa_printf(MSG_DEBUG, "WMM: Unsupported Action Code %d",
1966 mgmt->u.action.u.wmm_action.action_code);
1967 break;
1972 static void ieee80211_rx_mgmt_action(struct wpa_supplicant *wpa_s,
1973 struct ieee80211_mgmt *mgmt,
1974 size_t len,
1975 struct ieee80211_rx_status *rx_status)
1977 wpa_printf(MSG_DEBUG, "MLME: received Action frame");
1979 if (len < 25)
1980 return;
1982 switch (mgmt->u.action.category) {
1983 #ifdef CONFIG_IEEE80211R
1984 case WLAN_ACTION_FT:
1985 ieee80211_rx_mgmt_ft_action(wpa_s, mgmt, len, rx_status);
1986 break;
1987 #endif /* CONFIG_IEEE80211R */
1988 #ifdef CONFIG_IEEE80211W
1989 case WLAN_ACTION_SA_QUERY:
1990 ieee80211_rx_mgmt_sa_query_action(wpa_s, mgmt, len, rx_status);
1991 break;
1992 #endif /* CONFIG_IEEE80211W */
1993 case WLAN_ACTION_WMM:
1994 ieee80211_rx_mgmt_wmm_action(wpa_s, mgmt, len, rx_status);
1995 break;
1996 case WLAN_ACTION_PUBLIC:
1997 if (wpa_s->mlme.public_action_cb) {
1998 wpa_s->mlme.public_action_cb(
1999 wpa_s->mlme.public_action_cb_ctx,
2000 (u8 *) mgmt, len, rx_status->freq);
2001 return;
2003 break;
2004 default:
2005 wpa_printf(MSG_DEBUG, "MLME: unknown Action Category %d",
2006 mgmt->u.action.category);
2007 break;
2012 static void ieee80211_sta_rx_mgmt(struct wpa_supplicant *wpa_s,
2013 const u8 *buf, size_t len,
2014 struct ieee80211_rx_status *rx_status)
2016 struct ieee80211_mgmt *mgmt;
2017 u16 fc;
2019 if (len < 24)
2020 return;
2022 mgmt = (struct ieee80211_mgmt *) buf;
2023 fc = le_to_host16(mgmt->frame_control);
2025 switch (WLAN_FC_GET_STYPE(fc)) {
2026 case WLAN_FC_STYPE_PROBE_REQ:
2027 ieee80211_rx_mgmt_probe_req(wpa_s, mgmt, len, rx_status);
2028 break;
2029 case WLAN_FC_STYPE_PROBE_RESP:
2030 ieee80211_rx_mgmt_probe_resp(wpa_s, mgmt, len, rx_status);
2031 break;
2032 case WLAN_FC_STYPE_BEACON:
2033 ieee80211_rx_mgmt_beacon(wpa_s, mgmt, len, rx_status);
2034 break;
2035 case WLAN_FC_STYPE_AUTH:
2036 ieee80211_rx_mgmt_auth(wpa_s, mgmt, len, rx_status);
2037 break;
2038 case WLAN_FC_STYPE_ASSOC_RESP:
2039 ieee80211_rx_mgmt_assoc_resp(wpa_s, mgmt, len, rx_status, 0);
2040 break;
2041 case WLAN_FC_STYPE_REASSOC_RESP:
2042 ieee80211_rx_mgmt_assoc_resp(wpa_s, mgmt, len, rx_status, 1);
2043 break;
2044 case WLAN_FC_STYPE_DEAUTH:
2045 ieee80211_rx_mgmt_deauth(wpa_s, mgmt, len, rx_status);
2046 break;
2047 case WLAN_FC_STYPE_DISASSOC:
2048 ieee80211_rx_mgmt_disassoc(wpa_s, mgmt, len, rx_status);
2049 break;
2050 case WLAN_FC_STYPE_ACTION:
2051 ieee80211_rx_mgmt_action(wpa_s, mgmt, len, rx_status);
2052 break;
2053 default:
2054 wpa_printf(MSG_DEBUG, "MLME: received unknown management "
2055 "frame - stype=%d", WLAN_FC_GET_STYPE(fc));
2056 break;
2061 static void ieee80211_sta_rx_scan(struct wpa_supplicant *wpa_s,
2062 const u8 *buf, size_t len,
2063 struct ieee80211_rx_status *rx_status)
2065 struct ieee80211_mgmt *mgmt;
2066 u16 fc;
2068 if (len < 24)
2069 return;
2071 mgmt = (struct ieee80211_mgmt *) buf;
2072 fc = le_to_host16(mgmt->frame_control);
2074 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT) {
2075 if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
2076 ieee80211_rx_mgmt_probe_resp(wpa_s, mgmt,
2077 len, rx_status);
2078 } else if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON) {
2079 ieee80211_rx_mgmt_beacon(wpa_s, mgmt, len, rx_status);
2085 static int ieee80211_sta_active_ibss(struct wpa_supplicant *wpa_s)
2087 int active = 0;
2089 #if 0 /* FIX */
2090 list_for_each(ptr, &local->sta_list) {
2091 sta = list_entry(ptr, struct sta_info, list);
2092 if (sta->dev == dev &&
2093 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
2094 jiffies)) {
2095 active++;
2096 break;
2099 #endif
2101 return active;
2105 static void ieee80211_sta_expire(struct wpa_supplicant *wpa_s)
2107 #if 0 /* FIX */
2108 list_for_each_safe(ptr, n, &local->sta_list) {
2109 sta = list_entry(ptr, struct sta_info, list);
2110 if (time_after(jiffies, sta->last_rx +
2111 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
2112 wpa_printf(MSG_DEBUG, "MLME: expiring inactive STA "
2113 MACSTR, MAC2STR(sta->addr));
2114 sta_info_free(local, sta, 1);
2117 #endif
2121 static void ieee80211_sta_merge_ibss(struct wpa_supplicant *wpa_s)
2123 struct wpa_driver_scan_params params;
2125 ieee80211_reschedule_timer(wpa_s, IEEE80211_IBSS_MERGE_INTERVAL);
2127 ieee80211_sta_expire(wpa_s);
2128 if (ieee80211_sta_active_ibss(wpa_s))
2129 return;
2131 wpa_printf(MSG_DEBUG, "MLME: No active IBSS STAs - trying to scan for "
2132 "other IBSS networks with same SSID (merge)");
2133 os_memset(&params, 0, sizeof(params));
2134 params.ssids[0].ssid = wpa_s->mlme.ssid;
2135 params.ssids[0].ssid_len = wpa_s->mlme.ssid_len;
2136 params.num_ssids = wpa_s->mlme.ssid_len ? 1 : 0;
2137 ieee80211_sta_req_scan(wpa_s, &params);
2141 static void ieee80211_sta_timer(void *eloop_ctx, void *timeout_ctx)
2143 struct wpa_supplicant *wpa_s = eloop_ctx;
2145 switch (wpa_s->mlme.state) {
2146 case IEEE80211_DISABLED:
2147 break;
2148 case IEEE80211_AUTHENTICATE:
2149 ieee80211_authenticate(wpa_s);
2150 break;
2151 case IEEE80211_ASSOCIATE:
2152 ieee80211_associate(wpa_s);
2153 break;
2154 case IEEE80211_ASSOCIATED:
2155 ieee80211_associated(wpa_s);
2156 break;
2157 case IEEE80211_IBSS_SEARCH:
2158 ieee80211_sta_find_ibss(wpa_s);
2159 break;
2160 case IEEE80211_IBSS_JOINED:
2161 ieee80211_sta_merge_ibss(wpa_s);
2162 break;
2163 default:
2164 wpa_printf(MSG_DEBUG, "ieee80211_sta_timer: Unknown state %d",
2165 wpa_s->mlme.state);
2166 break;
2169 if (ieee80211_privacy_mismatch(wpa_s)) {
2170 wpa_printf(MSG_DEBUG, "MLME: privacy configuration mismatch "
2171 "and mixed-cell disabled - disassociate");
2173 ieee80211_send_disassoc(wpa_s, WLAN_REASON_UNSPECIFIED);
2174 ieee80211_set_associated(wpa_s, 0);
2179 static void ieee80211_sta_new_auth(struct wpa_supplicant *wpa_s)
2181 struct wpa_ssid *ssid = wpa_s->current_ssid;
2182 if (ssid && ssid->mode != WPAS_MODE_INFRA)
2183 return;
2185 #if 0 /* FIX */
2186 if (local->hw->reset_tsf) {
2187 /* Reset own TSF to allow time synchronization work. */
2188 local->hw->reset_tsf(local->mdev);
2190 #endif
2192 wpa_s->mlme.wmm_last_param_set = -1; /* allow any WMM update */
2195 if (wpa_s->mlme.auth_algs & WPA_AUTH_ALG_OPEN)
2196 wpa_s->mlme.auth_alg = WLAN_AUTH_OPEN;
2197 else if (wpa_s->mlme.auth_algs & WPA_AUTH_ALG_SHARED)
2198 wpa_s->mlme.auth_alg = WLAN_AUTH_SHARED_KEY;
2199 else if (wpa_s->mlme.auth_algs & WPA_AUTH_ALG_LEAP)
2200 wpa_s->mlme.auth_alg = WLAN_AUTH_LEAP;
2201 else
2202 wpa_s->mlme.auth_alg = WLAN_AUTH_OPEN;
2203 wpa_printf(MSG_DEBUG, "MLME: Initial auth_alg=%d",
2204 wpa_s->mlme.auth_alg);
2205 wpa_s->mlme.auth_transaction = -1;
2206 wpa_s->mlme.auth_tries = wpa_s->mlme.assoc_tries = 0;
2207 ieee80211_authenticate(wpa_s);
2211 static int ieee80211_ibss_allowed(struct wpa_supplicant *wpa_s)
2213 #if 0 /* FIX */
2214 int m, c;
2216 for (m = 0; m < local->hw->num_modes; m++) {
2217 struct ieee80211_hw_modes *mode = &local->hw->modes[m];
2218 if (mode->mode != local->conf.phymode)
2219 continue;
2220 for (c = 0; c < mode->num_channels; c++) {
2221 struct ieee80211_channel *chan = &mode->channels[c];
2222 if (chan->flag & IEEE80211_CHAN_W_SCAN &&
2223 chan->chan == local->conf.channel) {
2224 if (chan->flag & IEEE80211_CHAN_W_IBSS)
2225 return 1;
2226 break;
2230 #endif
2232 return 0;
2236 static int ieee80211_sta_join_ibss(struct wpa_supplicant *wpa_s,
2237 struct ieee80211_sta_bss *bss)
2239 int res = 0, rates, done = 0, bssid_changed;
2240 struct ieee80211_mgmt *mgmt;
2241 #if 0 /* FIX */
2242 struct ieee80211_tx_control control;
2243 struct ieee80211_rate *rate;
2244 struct rate_control_extra extra;
2245 #endif
2246 u8 *pos, *buf;
2247 size_t len;
2249 /* Remove possible STA entries from other IBSS networks. */
2250 #if 0 /* FIX */
2251 sta_info_flush(local, NULL);
2253 if (local->hw->reset_tsf) {
2254 /* Reset own TSF to allow time synchronization work. */
2255 local->hw->reset_tsf(local->mdev);
2257 #endif
2258 bssid_changed = os_memcmp(wpa_s->bssid, bss->bssid, ETH_ALEN);
2259 os_memcpy(wpa_s->bssid, bss->bssid, ETH_ALEN);
2260 if (bssid_changed)
2261 wpas_notify_bssid_changed(wpa_s);
2263 #if 0 /* FIX */
2264 local->conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2266 sdata->drop_unencrypted = bss->capability &
2267 host_to_le16(WLAN_CAPABILITY_PRIVACY) ? 1 : 0;
2268 #endif
2270 #if 0 /* FIX */
2271 os_memset(&rq, 0, sizeof(rq));
2272 rq.m = bss->freq * 100000;
2273 rq.e = 1;
2274 res = ieee80211_ioctl_siwfreq(wpa_s, NULL, &rq, NULL);
2275 #endif
2277 if (!ieee80211_ibss_allowed(wpa_s)) {
2278 #if 0 /* FIX */
2279 wpa_printf(MSG_DEBUG, "MLME: IBSS not allowed on channel %d "
2280 "(%d MHz)", local->conf.channel,
2281 local->conf.freq);
2282 #endif
2283 return -1;
2286 /* Set beacon template based on scan results */
2287 buf = os_malloc(400);
2288 len = 0;
2289 do {
2290 if (buf == NULL)
2291 break;
2293 mgmt = (struct ieee80211_mgmt *) buf;
2294 len += 24 + sizeof(mgmt->u.beacon);
2295 os_memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2296 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2297 WLAN_FC_STYPE_BEACON);
2298 os_memset(mgmt->da, 0xff, ETH_ALEN);
2299 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
2300 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
2301 #if 0 /* FIX */
2302 mgmt->u.beacon.beacon_int =
2303 host_to_le16(local->conf.beacon_int);
2304 #endif
2305 mgmt->u.beacon.capab_info = host_to_le16(bss->capability);
2307 pos = buf + len;
2308 len += 2 + wpa_s->mlme.ssid_len;
2309 *pos++ = WLAN_EID_SSID;
2310 *pos++ = wpa_s->mlme.ssid_len;
2311 os_memcpy(pos, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len);
2313 rates = bss->supp_rates_len;
2314 if (rates > 8)
2315 rates = 8;
2316 pos = buf + len;
2317 len += 2 + rates;
2318 *pos++ = WLAN_EID_SUPP_RATES;
2319 *pos++ = rates;
2320 os_memcpy(pos, bss->supp_rates, rates);
2322 pos = buf + len;
2323 len += 2 + 1;
2324 *pos++ = WLAN_EID_DS_PARAMS;
2325 *pos++ = 1;
2326 *pos++ = bss->channel;
2328 pos = buf + len;
2329 len += 2 + 2;
2330 *pos++ = WLAN_EID_IBSS_PARAMS;
2331 *pos++ = 2;
2332 /* FIX: set ATIM window based on scan results */
2333 *pos++ = 0;
2334 *pos++ = 0;
2336 if (bss->supp_rates_len > 8) {
2337 rates = bss->supp_rates_len - 8;
2338 pos = buf + len;
2339 len += 2 + rates;
2340 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2341 *pos++ = rates;
2342 os_memcpy(pos, &bss->supp_rates[8], rates);
2345 #if 0 /* FIX */
2346 os_memset(&control, 0, sizeof(control));
2347 control.pkt_type = PKT_PROBE_RESP;
2348 os_memset(&extra, 0, sizeof(extra));
2349 extra.endidx = local->num_curr_rates;
2350 rate = rate_control_get_rate(wpa_s, skb, &extra);
2351 if (rate == NULL) {
2352 wpa_printf(MSG_DEBUG, "MLME: Failed to determine TX "
2353 "rate for IBSS beacon");
2354 break;
2356 control.tx_rate = (wpa_s->mlme.short_preamble &&
2357 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
2358 rate->val2 : rate->val;
2359 control.antenna_sel = local->conf.antenna_sel;
2360 control.power_level = local->conf.power_level;
2361 control.no_ack = 1;
2362 control.retry_limit = 1;
2363 control.rts_cts_duration = 0;
2364 #endif
2366 #if 0 /* FIX */
2367 wpa_s->mlme.probe_resp = skb_copy(skb, GFP_ATOMIC);
2368 if (wpa_s->mlme.probe_resp) {
2369 mgmt = (struct ieee80211_mgmt *)
2370 wpa_s->mlme.probe_resp->data;
2371 mgmt->frame_control =
2372 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2373 WLAN_FC_STYPE_PROBE_RESP);
2374 } else {
2375 wpa_printf(MSG_DEBUG, "MLME: Could not allocate "
2376 "ProbeResp template for IBSS");
2379 if (local->hw->beacon_update &&
2380 local->hw->beacon_update(wpa_s, skb, &control) == 0) {
2381 wpa_printf(MSG_DEBUG, "MLME: Configured IBSS beacon "
2382 "template based on scan results");
2383 skb = NULL;
2386 rates = 0;
2387 for (i = 0; i < bss->supp_rates_len; i++) {
2388 int rate = (bss->supp_rates[i] & 0x7f) * 5;
2389 if (local->conf.phymode == MODE_ATHEROS_TURBO)
2390 rate *= 2;
2391 for (j = 0; j < local->num_curr_rates; j++)
2392 if (local->curr_rates[j] == rate)
2393 rates |= BIT(j);
2395 wpa_s->mlme.supp_rates_bits = rates;
2396 #endif
2397 done = 1;
2398 } while (0);
2400 os_free(buf);
2401 if (!done) {
2402 wpa_printf(MSG_DEBUG, "MLME: Failed to configure IBSS beacon "
2403 "template");
2406 wpa_s->mlme.state = IEEE80211_IBSS_JOINED;
2407 ieee80211_reschedule_timer(wpa_s, IEEE80211_IBSS_MERGE_INTERVAL);
2409 return res;
2413 #if 0 /* FIX */
2414 static int ieee80211_sta_create_ibss(struct wpa_supplicant *wpa_s)
2416 struct ieee80211_sta_bss *bss;
2417 u8 bssid[ETH_ALEN], *pos;
2418 int i;
2420 #if 0
2421 /* Easier testing, use fixed BSSID. */
2422 os_memset(bssid, 0xfe, ETH_ALEN);
2423 #else
2424 /* Generate random, not broadcast, locally administered BSSID. Mix in
2425 * own MAC address to make sure that devices that do not have proper
2426 * random number generator get different BSSID. */
2427 os_get_random(bssid, ETH_ALEN);
2428 for (i = 0; i < ETH_ALEN; i++)
2429 bssid[i] ^= wpa_s->own_addr[i];
2430 bssid[0] &= ~0x01;
2431 bssid[0] |= 0x02;
2432 #endif
2434 wpa_printf(MSG_DEBUG, "MLME: Creating new IBSS network, BSSID "
2435 MACSTR "", MAC2STR(bssid));
2437 bss = ieee80211_bss_add(wpa_s, bssid);
2438 if (bss == NULL)
2439 return -ENOMEM;
2441 #if 0 /* FIX */
2442 if (local->conf.beacon_int == 0)
2443 local->conf.beacon_int = 100;
2444 bss->beacon_int = local->conf.beacon_int;
2445 bss->hw_mode = local->conf.phymode;
2446 bss->channel = local->conf.channel;
2447 bss->freq = local->conf.freq;
2448 #endif
2449 os_get_time(&bss->last_update);
2450 bss->capability = host_to_le16(WLAN_CAPABILITY_IBSS);
2451 #if 0 /* FIX */
2452 if (sdata->default_key) {
2453 bss->capability |= host_to_le16(WLAN_CAPABILITY_PRIVACY);
2454 } else
2455 sdata->drop_unencrypted = 0;
2456 bss->supp_rates_len = local->num_curr_rates;
2457 #endif
2458 pos = bss->supp_rates;
2459 #if 0 /* FIX */
2460 for (i = 0; i < local->num_curr_rates; i++) {
2461 int rate = local->curr_rates[i];
2462 if (local->conf.phymode == MODE_ATHEROS_TURBO)
2463 rate /= 2;
2464 *pos++ = (u8) (rate / 5);
2466 #endif
2468 return ieee80211_sta_join_ibss(wpa_s, bss);
2470 #endif
2473 static int ieee80211_sta_find_ibss(struct wpa_supplicant *wpa_s)
2475 struct ieee80211_sta_bss *bss;
2476 int found = 0;
2477 u8 bssid[ETH_ALEN];
2478 int active_ibss;
2479 struct os_time now;
2481 if (wpa_s->mlme.ssid_len == 0)
2482 return -EINVAL;
2484 active_ibss = ieee80211_sta_active_ibss(wpa_s);
2485 #ifdef IEEE80211_IBSS_DEBUG
2486 wpa_printf(MSG_DEBUG, "MLME: sta_find_ibss (active_ibss=%d)",
2487 active_ibss);
2488 #endif /* IEEE80211_IBSS_DEBUG */
2489 for (bss = wpa_s->mlme.sta_bss_list; bss; bss = bss->next) {
2490 if (wpa_s->mlme.ssid_len != bss->ssid_len ||
2491 os_memcmp(wpa_s->mlme.ssid, bss->ssid, bss->ssid_len) != 0
2492 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2493 continue;
2494 #ifdef IEEE80211_IBSS_DEBUG
2495 wpa_printf(MSG_DEBUG, " bssid=" MACSTR " found",
2496 MAC2STR(bss->bssid));
2497 #endif /* IEEE80211_IBSS_DEBUG */
2498 os_memcpy(bssid, bss->bssid, ETH_ALEN);
2499 found = 1;
2500 if (active_ibss ||
2501 os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)
2502 break;
2505 #ifdef IEEE80211_IBSS_DEBUG
2506 wpa_printf(MSG_DEBUG, " sta_find_ibss: selected " MACSTR " current "
2507 MACSTR, MAC2STR(bssid), MAC2STR(wpa_s->bssid));
2508 #endif /* IEEE80211_IBSS_DEBUG */
2509 if (found && os_memcmp(wpa_s->bssid, bssid, ETH_ALEN) != 0 &&
2510 (bss = ieee80211_bss_get(wpa_s, bssid))) {
2511 wpa_printf(MSG_DEBUG, "MLME: Selected IBSS BSSID " MACSTR
2512 " based on configured SSID",
2513 MAC2STR(bssid));
2514 return ieee80211_sta_join_ibss(wpa_s, bss);
2516 #ifdef IEEE80211_IBSS_DEBUG
2517 wpa_printf(MSG_DEBUG, " did not try to join ibss");
2518 #endif /* IEEE80211_IBSS_DEBUG */
2520 /* Selected IBSS not found in current scan results - try to scan */
2521 os_get_time(&now);
2522 #if 0 /* FIX */
2523 if (wpa_s->mlme.state == IEEE80211_IBSS_JOINED &&
2524 !ieee80211_sta_active_ibss(wpa_s)) {
2525 ieee80211_reschedule_timer(wpa_s,
2526 IEEE80211_IBSS_MERGE_INTERVAL);
2527 } else if (time_after(jiffies, wpa_s->mlme.last_scan_completed +
2528 IEEE80211_SCAN_INTERVAL)) {
2529 wpa_printf(MSG_DEBUG, "MLME: Trigger new scan to find an IBSS "
2530 "to join");
2531 return ieee80211_sta_req_scan(wpa_s->mlme.ssid,
2532 wpa_s->mlme.ssid_len);
2533 } else if (wpa_s->mlme.state != IEEE80211_IBSS_JOINED) {
2534 int interval = IEEE80211_SCAN_INTERVAL;
2536 if (time_after(jiffies, wpa_s->mlme.ibss_join_req +
2537 IEEE80211_IBSS_JOIN_TIMEOUT)) {
2538 if (wpa_s->mlme.create_ibss &&
2539 ieee80211_ibss_allowed(wpa_s))
2540 return ieee80211_sta_create_ibss(wpa_s);
2541 if (wpa_s->mlme.create_ibss) {
2542 wpa_printf(MSG_DEBUG, "MLME: IBSS not allowed "
2543 "on the configured channel %d "
2544 "(%d MHz)",
2545 local->conf.channel,
2546 local->conf.freq);
2549 /* No IBSS found - decrease scan interval and continue
2550 * scanning. */
2551 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2554 wpa_s->mlme.state = IEEE80211_IBSS_SEARCH;
2555 ieee80211_reschedule_timer(wpa_s, interval);
2556 return 0;
2558 #endif
2560 return 0;
2564 int ieee80211_sta_get_ssid(struct wpa_supplicant *wpa_s, u8 *ssid,
2565 size_t *len)
2567 os_memcpy(ssid, wpa_s->mlme.ssid, wpa_s->mlme.ssid_len);
2568 *len = wpa_s->mlme.ssid_len;
2569 return 0;
2573 int ieee80211_sta_associate(struct wpa_supplicant *wpa_s,
2574 struct wpa_driver_associate_params *params)
2576 struct ieee80211_sta_bss *bss;
2577 int bssid_changed;
2579 wpa_s->mlme.bssid_set = 0;
2580 wpa_s->mlme.freq = params->freq;
2581 if (params->bssid) {
2582 bssid_changed = os_memcmp(wpa_s->bssid, params->bssid,
2583 ETH_ALEN);
2584 os_memcpy(wpa_s->bssid, params->bssid, ETH_ALEN);
2585 if (bssid_changed)
2586 wpas_notify_bssid_changed(wpa_s);
2588 if (!is_zero_ether_addr(params->bssid))
2589 wpa_s->mlme.bssid_set = 1;
2590 bss = ieee80211_bss_get(wpa_s, wpa_s->bssid);
2591 if (bss) {
2592 wpa_s->mlme.phymode = bss->hw_mode;
2593 wpa_s->mlme.channel = bss->channel;
2594 wpa_s->mlme.freq = bss->freq;
2598 #if 0 /* FIX */
2599 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
2600 * not defined. */
2601 if (local->hw->conf_tx) {
2602 struct ieee80211_tx_queue_params qparam;
2603 int i;
2605 os_memset(&qparam, 0, sizeof(qparam));
2606 /* TODO: are these ok defaults for all hw_modes? */
2607 qparam.aifs = 2;
2608 qparam.cw_min =
2609 local->conf.phymode == MODE_IEEE80211B ? 31 : 15;
2610 qparam.cw_max = 1023;
2611 qparam.burst_time = 0;
2612 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
2614 local->hw->conf_tx(wpa_s, i + IEEE80211_TX_QUEUE_DATA0,
2615 &qparam);
2617 /* IBSS uses different parameters for Beacon sending */
2618 qparam.cw_min++;
2619 qparam.cw_min *= 2;
2620 qparam.cw_min--;
2621 local->hw->conf_tx(wpa_s, IEEE80211_TX_QUEUE_BEACON, &qparam);
2623 #endif
2625 if (wpa_s->mlme.ssid_len != params->ssid_len ||
2626 os_memcmp(wpa_s->mlme.ssid, params->ssid, params->ssid_len) != 0)
2627 wpa_s->mlme.prev_bssid_set = 0;
2628 os_memcpy(wpa_s->mlme.ssid, params->ssid, params->ssid_len);
2629 os_memset(wpa_s->mlme.ssid + params->ssid_len, 0,
2630 MAX_SSID_LEN - params->ssid_len);
2631 wpa_s->mlme.ssid_len = params->ssid_len;
2632 wpa_s->mlme.ssid_set = 1;
2634 os_free(wpa_s->mlme.extra_ie);
2635 if (params->wpa_ie == NULL || params->wpa_ie_len == 0) {
2636 wpa_s->mlme.extra_ie = NULL;
2637 wpa_s->mlme.extra_ie_len = 0;
2638 } else {
2639 wpa_s->mlme.extra_ie = os_malloc(params->wpa_ie_len);
2640 if (wpa_s->mlme.extra_ie == NULL) {
2641 wpa_s->mlme.extra_ie_len = 0;
2642 return -1;
2644 os_memcpy(wpa_s->mlme.extra_ie, params->wpa_ie,
2645 params->wpa_ie_len);
2646 wpa_s->mlme.extra_ie_len = params->wpa_ie_len;
2649 wpa_s->mlme.key_mgmt = params->key_mgmt_suite;
2651 ieee80211_sta_set_channel(wpa_s, wpa_s->mlme.phymode,
2652 wpa_s->mlme.channel, wpa_s->mlme.freq);
2654 if (params->mode == WPAS_MODE_IBSS && !wpa_s->mlme.bssid_set) {
2655 os_get_time(&wpa_s->mlme.ibss_join_req);
2656 wpa_s->mlme.state = IEEE80211_IBSS_SEARCH;
2657 return ieee80211_sta_find_ibss(wpa_s);
2660 if (wpa_s->mlme.bssid_set)
2661 ieee80211_sta_new_auth(wpa_s);
2663 return 0;
2667 static void ieee80211_sta_save_oper_chan(struct wpa_supplicant *wpa_s)
2669 wpa_s->mlme.scan_oper_channel = wpa_s->mlme.channel;
2670 wpa_s->mlme.scan_oper_freq = wpa_s->mlme.freq;
2671 wpa_s->mlme.scan_oper_phymode = wpa_s->mlme.phymode;
2675 static int ieee80211_sta_restore_oper_chan(struct wpa_supplicant *wpa_s)
2677 wpa_s->mlme.channel = wpa_s->mlme.scan_oper_channel;
2678 wpa_s->mlme.freq = wpa_s->mlme.scan_oper_freq;
2679 wpa_s->mlme.phymode = wpa_s->mlme.scan_oper_phymode;
2680 if (wpa_s->mlme.freq == 0)
2681 return 0;
2682 return ieee80211_sta_set_channel(wpa_s, wpa_s->mlme.phymode,
2683 wpa_s->mlme.channel,
2684 wpa_s->mlme.freq);
2688 static int ieee80211_active_scan(struct wpa_supplicant *wpa_s)
2690 size_t m;
2691 int c;
2693 for (m = 0; m < wpa_s->mlme.num_modes; m++) {
2694 struct hostapd_hw_modes *mode = &wpa_s->mlme.modes[m];
2695 if ((int) mode->mode != (int) wpa_s->mlme.phymode)
2696 continue;
2697 for (c = 0; c < mode->num_channels; c++) {
2698 struct hostapd_channel_data *chan = &mode->channels[c];
2699 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
2700 chan->chan == wpa_s->mlme.channel) {
2701 if (!(chan->flag & HOSTAPD_CHAN_PASSIVE_SCAN))
2702 return 1;
2703 break;
2708 return 0;
2712 static void ieee80211_sta_scan_timer(void *eloop_ctx, void *timeout_ctx)
2714 struct wpa_supplicant *wpa_s = eloop_ctx;
2715 struct hostapd_hw_modes *mode;
2716 struct hostapd_channel_data *chan;
2717 int skip = 0;
2718 int timeout = 0;
2719 struct wpa_ssid *ssid = wpa_s->current_ssid;
2720 int adhoc;
2722 if (!wpa_s->mlme.sta_scanning || wpa_s->mlme.modes == NULL)
2723 return;
2725 adhoc = ssid && ssid->mode == 1;
2727 switch (wpa_s->mlme.scan_state) {
2728 case SCAN_SET_CHANNEL:
2729 mode = &wpa_s->mlme.modes[wpa_s->mlme.scan_hw_mode_idx];
2730 if (wpa_s->mlme.scan_hw_mode_idx >=
2731 (int) wpa_s->mlme.num_modes ||
2732 (wpa_s->mlme.scan_hw_mode_idx + 1 ==
2733 (int) wpa_s->mlme.num_modes
2734 && wpa_s->mlme.scan_channel_idx >= mode->num_channels)) {
2735 if (ieee80211_sta_restore_oper_chan(wpa_s)) {
2736 wpa_printf(MSG_DEBUG, "MLME: failed to "
2737 "restore operational channel after "
2738 "scan");
2740 wpa_printf(MSG_DEBUG, "MLME: scan completed");
2741 wpa_s->mlme.sta_scanning = 0;
2742 os_get_time(&wpa_s->mlme.last_scan_completed);
2743 wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
2744 if (adhoc) {
2745 if (!wpa_s->mlme.bssid_set ||
2746 (wpa_s->mlme.state ==
2747 IEEE80211_IBSS_JOINED &&
2748 !ieee80211_sta_active_ibss(wpa_s)))
2749 ieee80211_sta_find_ibss(wpa_s);
2751 return;
2753 skip = !(wpa_s->mlme.hw_modes & (1 << mode->mode));
2754 chan = &mode->channels[wpa_s->mlme.scan_channel_idx];
2755 if ((chan->flag & HOSTAPD_CHAN_DISABLED) ||
2756 (adhoc && (chan->flag & HOSTAPD_CHAN_NO_IBSS)) ||
2757 (wpa_s->mlme.hw_modes & (1 << HOSTAPD_MODE_IEEE80211G) &&
2758 mode->mode == HOSTAPD_MODE_IEEE80211B &&
2759 wpa_s->mlme.scan_skip_11b))
2760 skip = 1;
2761 if (!skip && wpa_s->mlme.scan_freqs) {
2762 int i, found = 0;
2763 for (i = 0; wpa_s->mlme.scan_freqs[i]; i++) {
2764 if (wpa_s->mlme.scan_freqs[i] == chan->freq) {
2765 found = 1;
2766 break;
2769 if (!found)
2770 skip = 1;
2773 if (!skip) {
2774 wpa_printf(MSG_MSGDUMP,
2775 "MLME: scan channel %d (%d MHz)",
2776 chan->chan, chan->freq);
2778 wpa_s->mlme.channel = chan->chan;
2779 wpa_s->mlme.freq = chan->freq;
2780 wpa_s->mlme.phymode = mode->mode;
2781 if (ieee80211_sta_set_channel(wpa_s, mode->mode,
2782 chan->chan, chan->freq))
2784 wpa_printf(MSG_DEBUG, "MLME: failed to set "
2785 "channel %d (%d MHz) for scan",
2786 chan->chan, chan->freq);
2787 skip = 1;
2791 wpa_s->mlme.scan_channel_idx++;
2792 if (wpa_s->mlme.scan_channel_idx >=
2793 wpa_s->mlme.modes[wpa_s->mlme.scan_hw_mode_idx].
2794 num_channels) {
2795 wpa_s->mlme.scan_hw_mode_idx++;
2796 wpa_s->mlme.scan_channel_idx = 0;
2799 if (skip) {
2800 timeout = 0;
2801 break;
2804 timeout = IEEE80211_PROBE_DELAY;
2805 wpa_s->mlme.scan_state = SCAN_SEND_PROBE;
2806 break;
2807 case SCAN_SEND_PROBE:
2808 if (ieee80211_active_scan(wpa_s)) {
2809 ieee80211_send_probe_req(wpa_s, NULL,
2810 wpa_s->mlme.scan_ssid,
2811 wpa_s->mlme.scan_ssid_len);
2812 timeout = IEEE80211_CHANNEL_TIME;
2813 } else {
2814 timeout = IEEE80211_PASSIVE_CHANNEL_TIME;
2816 wpa_s->mlme.scan_state = SCAN_SET_CHANNEL;
2817 break;
2820 eloop_register_timeout(timeout / 1000, 1000 * (timeout % 1000),
2821 ieee80211_sta_scan_timer, wpa_s, NULL);
2825 int ieee80211_sta_req_scan(struct wpa_supplicant *wpa_s,
2826 struct wpa_driver_scan_params *params)
2828 const u8 *ssid = params->ssids[0].ssid;
2829 size_t ssid_len = params->ssids[0].ssid_len;
2831 if (ssid_len > MAX_SSID_LEN)
2832 return -1;
2834 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
2835 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
2836 * BSSID: MACAddress
2837 * SSID
2838 * ScanType: ACTIVE, PASSIVE
2839 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
2840 * a Probe frame during active scanning
2841 * ChannelList
2842 * MinChannelTime (>= ProbeDelay), in TU
2843 * MaxChannelTime: (>= MinChannelTime), in TU
2846 /* MLME-SCAN.confirm
2847 * BSSDescriptionSet
2848 * ResultCode: SUCCESS, INVALID_PARAMETERS
2851 /* TODO: if assoc, move to power save mode for the duration of the
2852 * scan */
2854 if (wpa_s->mlme.sta_scanning)
2855 return -1;
2857 wpa_printf(MSG_DEBUG, "MLME: starting scan");
2859 ieee80211_sta_set_probe_req_ie(wpa_s, params->extra_ies,
2860 params->extra_ies_len);
2862 os_free(wpa_s->mlme.scan_freqs);
2863 if (params->freqs) {
2864 int i;
2865 for (i = 0; params->freqs[i]; i++)
2867 wpa_s->mlme.scan_freqs = os_malloc((i + 1) * sizeof(int));
2868 if (wpa_s->mlme.scan_freqs)
2869 os_memcpy(wpa_s->mlme.scan_freqs, params->freqs,
2870 (i + 1) * sizeof(int));
2871 } else
2872 wpa_s->mlme.scan_freqs = NULL;
2874 ieee80211_sta_save_oper_chan(wpa_s);
2876 wpa_s->mlme.sta_scanning = 1;
2877 /* TODO: stop TX queue? */
2879 if (ssid) {
2880 wpa_s->mlme.scan_ssid_len = ssid_len;
2881 os_memcpy(wpa_s->mlme.scan_ssid, ssid, ssid_len);
2882 } else
2883 wpa_s->mlme.scan_ssid_len = 0;
2884 wpa_s->mlme.scan_skip_11b = 1; /* FIX: clear this if 11g is not
2885 * supported */
2886 wpa_s->mlme.scan_skip_11b = 0;
2887 wpa_s->mlme.scan_state = SCAN_SET_CHANNEL;
2888 wpa_s->mlme.scan_hw_mode_idx = 0;
2889 wpa_s->mlme.scan_channel_idx = 0;
2890 eloop_register_timeout(0, 1, ieee80211_sta_scan_timer, wpa_s, NULL);
2892 return 0;
2896 struct wpa_scan_results *
2897 ieee80211_sta_get_scan_results(struct wpa_supplicant *wpa_s)
2899 size_t ap_num = 0;
2900 struct wpa_scan_results *res;
2901 struct wpa_scan_res *r;
2902 struct ieee80211_sta_bss *bss;
2904 res = os_zalloc(sizeof(*res));
2905 for (bss = wpa_s->mlme.sta_bss_list; bss; bss = bss->next)
2906 ap_num++;
2907 res->res = os_zalloc(ap_num * sizeof(struct wpa_scan_res *));
2908 if (res->res == NULL) {
2909 os_free(res);
2910 return NULL;
2913 for (bss = wpa_s->mlme.sta_bss_list; bss; bss = bss->next) {
2914 r = os_zalloc(sizeof(*r) + bss->ie_len);
2915 if (r == NULL)
2916 break;
2917 os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
2918 r->freq = bss->freq;
2919 r->beacon_int = bss->beacon_int;
2920 r->caps = bss->capability;
2921 r->level = bss->rssi;
2922 r->tsf = bss->timestamp;
2923 if (bss->ie) {
2924 r->ie_len = bss->ie_len;
2925 os_memcpy(r + 1, bss->ie, bss->ie_len);
2928 res->res[res->num++] = r;
2931 return res;
2935 #if 0 /* FIX */
2936 struct sta_info * ieee80211_ibss_add_sta(struct wpa_supplicant *wpa_s,
2937 struct sk_buff *skb, u8 *bssid,
2938 u8 *addr)
2940 struct ieee80211_local *local = dev->priv;
2941 struct list_head *ptr;
2942 struct sta_info *sta;
2943 struct wpa_supplicant *sta_dev = NULL;
2945 /* TODO: Could consider removing the least recently used entry and
2946 * allow new one to be added. */
2947 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
2948 if (net_ratelimit()) {
2949 wpa_printf(MSG_DEBUG, "MLME: No room for a new IBSS "
2950 "STA entry " MACSTR, MAC2STR(addr));
2952 return NULL;
2955 spin_lock_bh(&local->sub_if_lock);
2956 list_for_each(ptr, &local->sub_if_list) {
2957 sdata = list_entry(ptr, struct ieee80211_sub_if_data, list);
2958 if (sdata->type == IEEE80211_SUB_IF_TYPE_STA &&
2959 os_memcmp(bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
2960 sta_dev = sdata->dev;
2961 break;
2964 spin_unlock_bh(&local->sub_if_lock);
2966 if (sta_dev == NULL)
2967 return NULL;
2969 wpa_printf(MSG_DEBUG, "MLME: Adding new IBSS station " MACSTR
2970 " (dev=%s)", MAC2STR(addr), sta_dev->name);
2972 sta = sta_info_add(wpa_s, addr);
2973 if (sta == NULL) {
2974 return NULL;
2977 sta->dev = sta_dev;
2978 sta->supp_rates = wpa_s->mlme.supp_rates_bits;
2980 rate_control_rate_init(local, sta);
2982 return sta; /* caller will call sta_info_release() */
2984 #endif
2987 int ieee80211_sta_deauthenticate(struct wpa_supplicant *wpa_s, u16 reason)
2989 wpa_printf(MSG_DEBUG, "MLME: deauthenticate(reason=%d)", reason);
2991 ieee80211_send_deauth(wpa_s, reason);
2992 ieee80211_set_associated(wpa_s, 0);
2993 return 0;
2997 int ieee80211_sta_disassociate(struct wpa_supplicant *wpa_s, u16 reason)
2999 wpa_printf(MSG_DEBUG, "MLME: disassociate(reason=%d)", reason);
3001 if (!wpa_s->mlme.associated)
3002 return -1;
3004 ieee80211_send_disassoc(wpa_s, reason);
3005 ieee80211_set_associated(wpa_s, 0);
3006 return 0;
3010 void ieee80211_sta_rx(struct wpa_supplicant *wpa_s, const u8 *buf, size_t len,
3011 struct ieee80211_rx_status *rx_status)
3013 struct ieee80211_mgmt *mgmt;
3014 u16 fc;
3015 const u8 *pos;
3017 /* wpa_hexdump(MSG_MSGDUMP, "MLME: Received frame", buf, len); */
3019 if (wpa_s->mlme.sta_scanning) {
3020 ieee80211_sta_rx_scan(wpa_s, buf, len, rx_status);
3021 return;
3024 if (len < 24)
3025 return;
3027 mgmt = (struct ieee80211_mgmt *) buf;
3028 fc = le_to_host16(mgmt->frame_control);
3030 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
3031 ieee80211_sta_rx_mgmt(wpa_s, buf, len, rx_status);
3032 else if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
3033 if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) !=
3034 WLAN_FC_FROMDS)
3035 return;
3036 /* mgmt->sa is actually BSSID for FromDS data frames */
3037 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0)
3038 return;
3039 /* Skip IEEE 802.11 and LLC headers */
3040 pos = buf + 24 + 6;
3041 if (WPA_GET_BE16(pos) != ETH_P_EAPOL)
3042 return;
3043 pos += 2;
3044 /* mgmt->bssid is actually BSSID for SA data frames */
3045 wpa_supplicant_rx_eapol(wpa_s, mgmt->bssid,
3046 pos, buf + len - pos);
3051 void ieee80211_sta_free_hw_features(struct hostapd_hw_modes *hw_features,
3052 size_t num_hw_features)
3054 size_t i;
3056 if (hw_features == NULL)
3057 return;
3059 for (i = 0; i < num_hw_features; i++) {
3060 os_free(hw_features[i].channels);
3061 os_free(hw_features[i].rates);
3064 os_free(hw_features);
3068 int ieee80211_sta_init(struct wpa_supplicant *wpa_s)
3070 u16 num_modes, flags;
3072 wpa_s->mlme.modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes,
3073 &flags);
3074 if (wpa_s->mlme.modes == NULL) {
3075 wpa_printf(MSG_ERROR, "MLME: Failed to read supported "
3076 "channels and rates from the driver");
3077 return -1;
3080 wpa_s->mlme.num_modes = num_modes;
3082 wpa_s->mlme.hw_modes = 1 << HOSTAPD_MODE_IEEE80211A;
3083 wpa_s->mlme.hw_modes |= 1 << HOSTAPD_MODE_IEEE80211B;
3084 wpa_s->mlme.hw_modes |= 1 << HOSTAPD_MODE_IEEE80211G;
3086 wpa_s->mlme.wmm_enabled = 1;
3088 return 0;
3092 void ieee80211_sta_deinit(struct wpa_supplicant *wpa_s)
3094 eloop_cancel_timeout(ieee80211_sta_timer, wpa_s, NULL);
3095 eloop_cancel_timeout(ieee80211_sta_scan_timer, wpa_s, NULL);
3096 os_free(wpa_s->mlme.extra_ie);
3097 wpa_s->mlme.extra_ie = NULL;
3098 os_free(wpa_s->mlme.extra_probe_ie);
3099 wpa_s->mlme.extra_probe_ie = NULL;
3100 os_free(wpa_s->mlme.assocreq_ies);
3101 wpa_s->mlme.assocreq_ies = NULL;
3102 os_free(wpa_s->mlme.assocresp_ies);
3103 wpa_s->mlme.assocresp_ies = NULL;
3104 ieee80211_bss_list_deinit(wpa_s);
3105 ieee80211_sta_free_hw_features(wpa_s->mlme.modes,
3106 wpa_s->mlme.num_modes);
3107 #ifdef CONFIG_IEEE80211R
3108 os_free(wpa_s->mlme.ft_ies);
3109 wpa_s->mlme.ft_ies = NULL;
3110 wpa_s->mlme.ft_ies_len = 0;
3111 #endif /* CONFIG_IEEE80211R */
3113 os_free(wpa_s->mlme.scan_freqs);
3114 wpa_s->mlme.scan_freqs = NULL;
3118 #ifdef CONFIG_IEEE80211R
3120 int ieee80211_sta_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
3121 const u8 *ies, size_t ies_len)
3123 if (md == NULL) {
3124 wpa_printf(MSG_DEBUG, "MLME: Clear FT mobility domain");
3125 os_memset(wpa_s->mlme.current_md, 0, MOBILITY_DOMAIN_ID_LEN);
3126 } else {
3127 wpa_printf(MSG_DEBUG, "MLME: Update FT IEs for MD " MACSTR,
3128 MAC2STR(md));
3129 os_memcpy(wpa_s->mlme.current_md, md, MOBILITY_DOMAIN_ID_LEN);
3132 wpa_hexdump(MSG_DEBUG, "MLME: FT IEs", ies, ies_len);
3133 os_free(wpa_s->mlme.ft_ies);
3134 wpa_s->mlme.ft_ies = os_malloc(ies_len);
3135 if (wpa_s->mlme.ft_ies == NULL)
3136 return -1;
3137 os_memcpy(wpa_s->mlme.ft_ies, ies, ies_len);
3138 wpa_s->mlme.ft_ies_len = ies_len;
3140 return 0;
3144 int ieee80211_sta_send_ft_action(struct wpa_supplicant *wpa_s, u8 action,
3145 const u8 *target_ap,
3146 const u8 *ies, size_t ies_len)
3148 u8 *buf;
3149 size_t len;
3150 struct ieee80211_mgmt *mgmt;
3151 int res;
3154 * Action frame payload:
3155 * Category[1] = 6 (Fast BSS Transition)
3156 * Action[1] = 1 (Fast BSS Transition Request)
3157 * STA Address
3158 * Target AP Address
3159 * FT IEs
3162 buf = os_zalloc(sizeof(*mgmt) + ies_len);
3163 if (buf == NULL) {
3164 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
3165 "FT action frame");
3166 return -1;
3169 mgmt = (struct ieee80211_mgmt *) buf;
3170 len = 24;
3171 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
3172 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
3173 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
3174 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
3175 WLAN_FC_STYPE_ACTION);
3176 mgmt->u.action.category = WLAN_ACTION_FT;
3177 mgmt->u.action.u.ft_action_req.action = action;
3178 os_memcpy(mgmt->u.action.u.ft_action_req.sta_addr, wpa_s->own_addr,
3179 ETH_ALEN);
3180 os_memcpy(mgmt->u.action.u.ft_action_req.target_ap_addr, target_ap,
3181 ETH_ALEN);
3182 os_memcpy(mgmt->u.action.u.ft_action_req.variable, ies, ies_len);
3183 len += 1 + sizeof(mgmt->u.action.u.ft_action_req) + ies_len;
3185 wpa_printf(MSG_DEBUG, "MLME: Send FT Action Frame: Action=%d "
3186 "Target AP=" MACSTR " body_len=%lu",
3187 action, MAC2STR(target_ap), (unsigned long) ies_len);
3189 res = ieee80211_sta_tx(wpa_s, buf, len);
3190 os_free(buf);
3192 return res;
3195 #endif /* CONFIG_IEEE80211R */
3198 static int ieee80211_sta_set_probe_req_ie(struct wpa_supplicant *wpa_s,
3199 const u8 *ies, size_t ies_len)
3201 os_free(wpa_s->mlme.extra_probe_ie);
3202 wpa_s->mlme.extra_probe_ie = NULL;
3203 wpa_s->mlme.extra_probe_ie_len = 0;
3205 if (ies == NULL)
3206 return 0;
3208 wpa_s->mlme.extra_probe_ie = os_malloc(ies_len);
3209 if (wpa_s->mlme.extra_probe_ie == NULL)
3210 return -1;
3212 os_memcpy(wpa_s->mlme.extra_probe_ie, ies, ies_len);
3213 wpa_s->mlme.extra_probe_ie_len = ies_len;
3215 return 0;