WNM: Use defines for BSS Trans Mgmt field values
[mech_eap.git] / wpa_supplicant / wnm_sta.c
1 /*
2  * wpa_supplicant - WNM
3  * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "rsn_supp/wpa.h"
14 #include "wpa_supplicant_i.h"
15 #include "driver_i.h"
16 #include "scan.h"
17 #include "ctrl_iface.h"
18 #include "bss.h"
19 #include "wnm_sta.h"
20
21 #define MAX_TFS_IE_LEN  1024
22 #define WNM_MAX_NEIGHBOR_REPORT 10
23
24
25 /* get the TFS IE from driver */
26 static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
27                                    u16 *buf_len, enum wnm_oper oper)
28 {
29         wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
30
31         return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
32 }
33
34
35 /* set the TFS IE to driver */
36 static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
37                                    const u8 *addr, u8 *buf, u16 *buf_len,
38                                    enum wnm_oper oper)
39 {
40         wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
41
42         return wpa_drv_wnm_oper(wpa_s, oper, addr, buf, buf_len);
43 }
44
45
46 /* MLME-SLEEPMODE.request */
47 int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
48                                  u8 action, u16 intval, struct wpabuf *tfs_req)
49 {
50         struct ieee80211_mgmt *mgmt;
51         int res;
52         size_t len;
53         struct wnm_sleep_element *wnmsleep_ie;
54         u8 *wnmtfs_ie;
55         u8 wnmsleep_ie_len;
56         u16 wnmtfs_ie_len;  /* possibly multiple IE(s) */
57         enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
58                 WNM_SLEEP_TFS_REQ_IE_NONE;
59
60         wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
61                    "action=%s to " MACSTR,
62                    action == 0 ? "enter" : "exit",
63                    MAC2STR(wpa_s->bssid));
64
65         /* WNM-Sleep Mode IE */
66         wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
67         wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
68         if (wnmsleep_ie == NULL)
69                 return -1;
70         wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
71         wnmsleep_ie->len = wnmsleep_ie_len - 2;
72         wnmsleep_ie->action_type = action;
73         wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
74         wnmsleep_ie->intval = host_to_le16(intval);
75         wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
76                     (u8 *) wnmsleep_ie, wnmsleep_ie_len);
77
78         /* TFS IE(s) */
79         if (tfs_req) {
80                 wnmtfs_ie_len = wpabuf_len(tfs_req);
81                 wnmtfs_ie = os_malloc(wnmtfs_ie_len);
82                 if (wnmtfs_ie == NULL) {
83                         os_free(wnmsleep_ie);
84                         return -1;
85                 }
86                 os_memcpy(wnmtfs_ie, wpabuf_head(tfs_req), wnmtfs_ie_len);
87         } else {
88                 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
89                 if (wnmtfs_ie == NULL) {
90                         os_free(wnmsleep_ie);
91                         return -1;
92                 }
93                 if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
94                                             tfs_oper)) {
95                         wnmtfs_ie_len = 0;
96                         os_free(wnmtfs_ie);
97                         wnmtfs_ie = NULL;
98                 }
99         }
100         wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
101                     (u8 *) wnmtfs_ie, wnmtfs_ie_len);
102
103         mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len);
104         if (mgmt == NULL) {
105                 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
106                            "WNM-Sleep Request action frame");
107                 os_free(wnmsleep_ie);
108                 os_free(wnmtfs_ie);
109                 return -1;
110         }
111
112         os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
113         os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
114         os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
115         mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
116                                            WLAN_FC_STYPE_ACTION);
117         mgmt->u.action.category = WLAN_ACTION_WNM;
118         mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
119         mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
120         os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
121                   wnmsleep_ie_len);
122         /* copy TFS IE here */
123         if (wnmtfs_ie_len > 0) {
124                 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
125                           wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
126         }
127
128         len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
129                 wnmtfs_ie_len;
130
131         res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
132                                   wpa_s->own_addr, wpa_s->bssid,
133                                   &mgmt->u.action.category, len, 0);
134         if (res < 0)
135                 wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
136                            "(action=%d, intval=%d)", action, intval);
137
138         os_free(wnmsleep_ie);
139         os_free(wnmtfs_ie);
140         os_free(mgmt);
141
142         return res;
143 }
144
145
146 static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
147                                          u8 *tfsresp_ie_start,
148                                          u8 *tfsresp_ie_end)
149 {
150         wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
151                          wpa_s->bssid, NULL, NULL);
152         /* remove GTK/IGTK ?? */
153
154         /* set the TFS Resp IE(s) */
155         if (tfsresp_ie_start && tfsresp_ie_end &&
156             tfsresp_ie_end - tfsresp_ie_start >= 0) {
157                 u16 tfsresp_ie_len;
158                 tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
159                         tfsresp_ie_start;
160                 wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
161                 /* pass the TFS Resp IE(s) to driver for processing */
162                 if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
163                                             tfsresp_ie_start,
164                                             &tfsresp_ie_len,
165                                             WNM_SLEEP_TFS_RESP_IE_SET))
166                         wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
167         }
168 }
169
170
171 static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
172                                         const u8 *frm, u16 key_len_total)
173 {
174         u8 *ptr, *end;
175         u8 gtk_len;
176
177         wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM,  wpa_s->bssid,
178                          NULL, NULL);
179
180         /* Install GTK/IGTK */
181
182         /* point to key data field */
183         ptr = (u8 *) frm + 1 + 1 + 2;
184         end = ptr + key_len_total;
185         wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
186
187         while (ptr + 1 < end) {
188                 if (ptr + 2 + ptr[1] > end) {
189                         wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
190                                    "length");
191                         if (end > ptr) {
192                                 wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
193                                             ptr, end - ptr);
194                         }
195                         break;
196                 }
197                 if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
198                         if (ptr[1] < 11 + 5) {
199                                 wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
200                                            "subelem");
201                                 break;
202                         }
203                         gtk_len = *(ptr + 4);
204                         if (ptr[1] < 11 + gtk_len ||
205                             gtk_len < 5 || gtk_len > 32) {
206                                 wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
207                                            "subelem");
208                                 break;
209                         }
210                         wpa_wnmsleep_install_key(
211                                 wpa_s->wpa,
212                                 WNM_SLEEP_SUBELEM_GTK,
213                                 ptr);
214                         ptr += 13 + gtk_len;
215 #ifdef CONFIG_IEEE80211W
216                 } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
217                         if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
218                                 wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
219                                            "subelem");
220                                 break;
221                         }
222                         wpa_wnmsleep_install_key(wpa_s->wpa,
223                                                  WNM_SLEEP_SUBELEM_IGTK, ptr);
224                         ptr += 10 + WPA_IGTK_LEN;
225 #endif /* CONFIG_IEEE80211W */
226                 } else
227                         break; /* skip the loop */
228         }
229 }
230
231
232 static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
233                                         const u8 *frm, int len)
234 {
235         /*
236          * Action [1] | Diaglog Token [1] | Key Data Len [2] | Key Data |
237          * WNM-Sleep Mode IE | TFS Response IE
238          */
239         u8 *pos = (u8 *) frm; /* point to action field */
240         u16 key_len_total = le_to_host16(*((u16 *)(frm+2)));
241         struct wnm_sleep_element *wnmsleep_ie = NULL;
242         /* multiple TFS Resp IE (assuming consecutive) */
243         u8 *tfsresp_ie_start = NULL;
244         u8 *tfsresp_ie_end = NULL;
245
246         wpa_printf(MSG_DEBUG, "action=%d token = %d key_len_total = %d",
247                    frm[0], frm[1], key_len_total);
248         pos += 4 + key_len_total;
249         if (pos > frm + len) {
250                 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
251                 return;
252         }
253         while (pos - frm < len) {
254                 u8 ie_len = *(pos + 1);
255                 if (pos + 2 + ie_len > frm + len) {
256                         wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
257                         break;
258                 }
259                 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
260                 if (*pos == WLAN_EID_WNMSLEEP)
261                         wnmsleep_ie = (struct wnm_sleep_element *) pos;
262                 else if (*pos == WLAN_EID_TFS_RESP) {
263                         if (!tfsresp_ie_start)
264                                 tfsresp_ie_start = pos;
265                         tfsresp_ie_end = pos;
266                 } else
267                         wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
268                 pos += ie_len + 2;
269         }
270
271         if (!wnmsleep_ie) {
272                 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
273                 return;
274         }
275
276         if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
277             wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
278                 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
279                            "frame (action=%d, intval=%d)",
280                            wnmsleep_ie->action_type, wnmsleep_ie->intval);
281                 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
282                         wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
283                                                      tfsresp_ie_end);
284                 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
285                         wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
286                 }
287         } else {
288                 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
289                            "(action=%d, intval=%d)",
290                            wnmsleep_ie->action_type, wnmsleep_ie->intval);
291                 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
292                         wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
293                                          wpa_s->bssid, NULL, NULL);
294                 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
295                         wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
296                                          wpa_s->bssid, NULL, NULL);
297         }
298 }
299
300
301 void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
302 {
303         int i;
304
305         for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
306                 os_free(wpa_s->wnm_neighbor_report_elements[i].tsf_info);
307                 os_free(wpa_s->wnm_neighbor_report_elements[i].con_coun_str);
308                 os_free(wpa_s->wnm_neighbor_report_elements[i].bss_tran_can);
309                 os_free(wpa_s->wnm_neighbor_report_elements[i].bss_term_dur);
310                 os_free(wpa_s->wnm_neighbor_report_elements[i].bearing);
311                 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
312                 os_free(wpa_s->wnm_neighbor_report_elements[i].rrm_cap);
313                 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
314         }
315
316         os_free(wpa_s->wnm_neighbor_report_elements);
317         wpa_s->wnm_neighbor_report_elements = NULL;
318 }
319
320
321 static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
322                                            u8 id, u8 elen, const u8 *pos)
323 {
324         switch (id) {
325         case WNM_NEIGHBOR_TSF:
326                 if (elen < 2 + 2) {
327                         wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
328                         break;
329                 }
330                 rep->tsf_info = os_zalloc(sizeof(struct tsf_info));
331                 if (rep->tsf_info == NULL)
332                         break;
333                 rep->tsf_info->present = 1;
334                 os_memcpy(rep->tsf_info->tsf_offset, pos, 2);
335                 os_memcpy(rep->tsf_info->beacon_interval, pos + 2, 2);
336                 break;
337         case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
338                 if (elen < 2) {
339                         wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
340                                    "country string");
341                         break;
342                 }
343                 rep->con_coun_str =
344                         os_zalloc(sizeof(struct condensed_country_string));
345                 if (rep->con_coun_str == NULL)
346                         break;
347                 rep->con_coun_str->present = 1;
348                 os_memcpy(rep->con_coun_str->country_string, pos, 2);
349                 break;
350         case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
351                 if (elen < 1) {
352                         wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
353                                    "candidate");
354                         break;
355                 }
356                 rep->bss_tran_can =
357                         os_zalloc(sizeof(struct bss_transition_candidate));
358                 if (rep->bss_tran_can == NULL)
359                         break;
360                 rep->bss_tran_can->present = 1;
361                 rep->bss_tran_can->preference = pos[0];
362                 break;
363         case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
364                 if (elen < 12) {
365                         wpa_printf(MSG_DEBUG, "WNM: Too short BSS termination "
366                                    "duration");
367                         break;
368                 }
369                 rep->bss_term_dur =
370                         os_zalloc(sizeof(struct bss_termination_duration));
371                 if (rep->bss_term_dur == NULL)
372                         break;
373                 rep->bss_term_dur->present = 1;
374                 os_memcpy(rep->bss_term_dur->duration, pos, 12);
375                 break;
376         case WNM_NEIGHBOR_BEARING:
377                 if (elen < 8) {
378                         wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
379                                    "bearing");
380                         break;
381                 }
382                 rep->bearing = os_zalloc(sizeof(struct bearing));
383                 if (rep->bearing == NULL)
384                         break;
385                 rep->bearing->present = 1;
386                 os_memcpy(rep->bearing->bearing, pos, 8);
387                 break;
388         case WNM_NEIGHBOR_MEASUREMENT_PILOT:
389                 if (elen < 2) {
390                         wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
391                                    "pilot");
392                         break;
393                 }
394                 rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
395                 if (rep->meas_pilot == NULL)
396                         break;
397                 rep->meas_pilot->present = 1;
398                 rep->meas_pilot->measurement_pilot = pos[0];
399                 rep->meas_pilot->num_vendor_specific = pos[1];
400                 os_memcpy(rep->meas_pilot->vendor_specific, pos + 2, elen - 2);
401                 break;
402         case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
403                 if (elen < 4) {
404                         wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
405                                    "capabilities");
406                         break;
407                 }
408                 rep->rrm_cap =
409                         os_zalloc(sizeof(struct rrm_enabled_capabilities));
410                 if (rep->rrm_cap == NULL)
411                         break;
412                 rep->rrm_cap->present = 1;
413                 os_memcpy(rep->rrm_cap->capabilities, pos, 4);
414                 break;
415         case WNM_NEIGHBOR_MULTIPLE_BSSID:
416                 if (elen < 2) {
417                         wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
418                         break;
419                 }
420                 rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
421                 if (rep->mul_bssid == NULL)
422                         break;
423                 rep->mul_bssid->present = 1;
424                 rep->mul_bssid->max_bssid_indicator = pos[0];
425                 rep->mul_bssid->num_vendor_specific = pos[1];
426                 os_memcpy(rep->mul_bssid->vendor_specific, pos + 2, elen - 2);
427                 break;
428         }
429 }
430
431
432 static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
433                                       const u8 *pos, u8 len,
434                                       struct neighbor_report *rep)
435 {
436         u8 left = len;
437
438         if (left < 13) {
439                 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
440                 return;
441         }
442
443         os_memcpy(rep->bssid, pos, ETH_ALEN);
444         os_memcpy(rep->bssid_information, pos + ETH_ALEN, 4);
445         rep->regulatory_class = *(pos + 10);
446         rep->channel_number = *(pos + 11);
447         rep->phy_type = *(pos + 12);
448
449         pos += 13;
450         left -= 13;
451
452         while (left >= 2) {
453                 u8 id, elen;
454
455                 id = *pos++;
456                 elen = *pos++;
457                 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
458                 left -= 2 + elen;
459                 pos += elen;
460         }
461 }
462
463
464 static int compare_scan_neighbor_results(struct wpa_supplicant *wpa_s,
465                                          struct wpa_scan_results *scan_res,
466                                          struct neighbor_report *neigh_rep,
467                                          u8 num_neigh_rep, u8 *bssid_to_connect)
468 {
469
470         u8 i, j;
471
472         if (scan_res == NULL || num_neigh_rep == 0)
473                 return 0;
474
475         for (i = 0; i < num_neigh_rep; i++) {
476                 for (j = 0; j < scan_res->num; j++) {
477                         /* Check for a better RSSI AP */
478                         if (os_memcmp(scan_res->res[j]->bssid,
479                                       neigh_rep[i].bssid, ETH_ALEN) == 0 &&
480                             scan_res->res[j]->level >
481                             wpa_s->current_bss->level) {
482                                 /* Got a BSSID with better RSSI value */
483                                 os_memcpy(bssid_to_connect, neigh_rep[i].bssid,
484                                           ETH_ALEN);
485                                 return 1;
486                         }
487                 }
488         }
489
490         return 0;
491 }
492
493
494 static void wnm_send_bss_transition_mgmt_resp(
495         struct wpa_supplicant *wpa_s, u8 dialog_token,
496         enum bss_trans_mgmt_status_code status, u8 delay,
497         const u8 *target_bssid)
498 {
499         u8 buf[1000], *pos;
500         struct ieee80211_mgmt *mgmt;
501         size_t len;
502
503         wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
504                    "to " MACSTR " dialog_token=%u status=%u delay=%d",
505                    MAC2STR(wpa_s->bssid), dialog_token, status, delay);
506
507         mgmt = (struct ieee80211_mgmt *) buf;
508         os_memset(&buf, 0, sizeof(buf));
509         os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
510         os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
511         os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
512         mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
513                                            WLAN_FC_STYPE_ACTION);
514         mgmt->u.action.category = WLAN_ACTION_WNM;
515         mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
516         mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
517         mgmt->u.action.u.bss_tm_resp.status_code = status;
518         mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
519         pos = mgmt->u.action.u.bss_tm_resp.variable;
520         if (target_bssid) {
521                 os_memcpy(pos, target_bssid, ETH_ALEN);
522                 pos += ETH_ALEN;
523         }
524
525         len = pos - (u8 *) &mgmt->u.action.category;
526
527         wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
528                             wpa_s->own_addr, wpa_s->bssid,
529                             &mgmt->u.action.category, len, 0);
530 }
531
532
533 void wnm_scan_response(struct wpa_supplicant *wpa_s,
534                        struct wpa_scan_results *scan_res)
535 {
536         u8 bssid[ETH_ALEN];
537
538         if (scan_res == NULL) {
539                 wpa_printf(MSG_ERROR, "Scan result is NULL");
540                 goto send_bss_resp_fail;
541         }
542
543         /* Compare the Neighbor Report and scan results */
544         if (compare_scan_neighbor_results(wpa_s, scan_res,
545                                           wpa_s->wnm_neighbor_report_elements,
546                                           wpa_s->wnm_num_neighbor_report,
547                                           bssid) == 1) {
548                 /* Associate to the network */
549                 struct wpa_bss *bss;
550                 struct wpa_ssid *ssid = wpa_s->current_ssid;
551
552                 bss = wpa_bss_get_bssid(wpa_s, bssid);
553                 if (!bss) {
554                         wpa_printf(MSG_DEBUG, "WNM: Target AP not found from "
555                                    "BSS table");
556                         goto send_bss_resp_fail;
557                 }
558
559                 /* Send the BSS Management Response - Accept */
560                 if (wpa_s->wnm_reply) {
561                         wnm_send_bss_transition_mgmt_resp(wpa_s,
562                                                   wpa_s->wnm_dialog_token,
563                                                   WNM_BSS_TM_ACCEPT,
564                                                   0, NULL);
565                 }
566
567                 wpa_s->reassociate = 1;
568                 wpa_supplicant_connect(wpa_s, bss, ssid);
569                 wnm_deallocate_memory(wpa_s);
570                 return;
571         }
572
573         /* Send reject response for all the failures */
574 send_bss_resp_fail:
575         wnm_deallocate_memory(wpa_s);
576         if (wpa_s->wnm_reply) {
577                 wnm_send_bss_transition_mgmt_resp(wpa_s,
578                                                   wpa_s->wnm_dialog_token,
579                                                   WNM_BSS_TM_REJECT_UNSPECIFIED,
580                                                   0, NULL);
581         }
582         return;
583 }
584
585
586 static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
587                                              const u8 *pos, const u8 *end,
588                                              int reply)
589 {
590         if (pos + 5 > end)
591                 return;
592
593         wpa_s->wnm_dialog_token = pos[0];
594         wpa_s->wnm_mode = pos[1];
595         wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
596         wpa_s->wnm_validity_interval = pos[4];
597         wpa_s->wnm_reply = reply;
598
599         wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
600                    "dialog_token=%u request_mode=0x%x "
601                    "disassoc_timer=%u validity_interval=%u",
602                    wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
603                    wpa_s->wnm_dissoc_timer, wpa_s->wnm_validity_interval);
604
605         pos += 5;
606
607         if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
608                 if (pos + 12 > end) {
609                         wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
610                         return;
611                 }
612                 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
613                 pos += 12; /* BSS Termination Duration */
614         }
615
616         if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
617                 char url[256];
618                 if (pos + 1 > end || pos + 1 + pos[0] > end) {
619                         wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
620                                    "Management Request (URL)");
621                         return;
622                 }
623                 os_memcpy(url, pos + 1, pos[0]);
624                 url[pos[0]] = '\0';
625                 pos += 1 + pos[0];
626                 wpa_msg(wpa_s, MSG_INFO, "WNM: ESS Disassociation Imminent - "
627                         "session_info_url=%s", url);
628         }
629
630         if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
631                 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
632                         "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
633                 if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
634                         /* TODO: mark current BSS less preferred for
635                          * selection */
636                         wpa_printf(MSG_DEBUG, "Trying to find another BSS");
637                         wpa_supplicant_req_scan(wpa_s, 0, 0);
638                 }
639         }
640
641         if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
642                 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
643                 wpa_s->wnm_num_neighbor_report = 0;
644                 os_free(wpa_s->wnm_neighbor_report_elements);
645                 wpa_s->wnm_neighbor_report_elements = os_zalloc(
646                         WNM_MAX_NEIGHBOR_REPORT *
647                         sizeof(struct neighbor_report));
648                 if (wpa_s->wnm_neighbor_report_elements == NULL)
649                         return;
650
651                 while (pos + 2 <= end &&
652                        wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
653                 {
654                         u8 tag = *pos++;
655                         u8 len = *pos++;
656
657                         wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
658                                    tag);
659                         if (pos + len > end) {
660                                 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
661                                 return;
662                         }
663                         wnm_parse_neighbor_report(
664                                 wpa_s, pos, len,
665                                 &wpa_s->wnm_neighbor_report_elements[
666                                         wpa_s->wnm_num_neighbor_report]);
667
668                         pos += len;
669                         wpa_s->wnm_num_neighbor_report++;
670                 }
671
672                 wpa_s->scan_res_handler = wnm_scan_response;
673                 wpa_supplicant_req_scan(wpa_s, 0, 0);
674         } else if (reply) {
675                 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management "
676                         "Request Mode is zero");
677                 wnm_send_bss_transition_mgmt_resp(wpa_s,
678                                                   wpa_s->wnm_dialog_token,
679                                                   WNM_BSS_TM_REJECT_UNSPECIFIED,
680                                                   0, NULL);
681         }
682 }
683
684
685 int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
686                                        u8 query_reason)
687 {
688         u8 buf[1000], *pos;
689         struct ieee80211_mgmt *mgmt;
690         size_t len;
691         int ret;
692
693         wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
694                    MACSTR " query_reason=%u",
695                    MAC2STR(wpa_s->bssid), query_reason);
696
697         mgmt = (struct ieee80211_mgmt *) buf;
698         os_memset(&buf, 0, sizeof(buf));
699         os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
700         os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
701         os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
702         mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
703                                            WLAN_FC_STYPE_ACTION);
704         mgmt->u.action.category = WLAN_ACTION_WNM;
705         mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
706         mgmt->u.action.u.bss_tm_query.dialog_token = 0;
707         mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
708         pos = mgmt->u.action.u.bss_tm_query.variable;
709
710         len = pos - (u8 *) &mgmt->u.action.category;
711
712         ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
713                                   wpa_s->own_addr, wpa_s->bssid,
714                                   &mgmt->u.action.category, len, 0);
715
716         return ret;
717 }
718
719
720 void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
721                               struct rx_action *action)
722 {
723         const u8 *pos, *end;
724         u8 act;
725
726         if (action->data == NULL || action->len == 0)
727                 return;
728
729         pos = action->data;
730         end = pos + action->len;
731         act = *pos++;
732
733         wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
734                    act, MAC2STR(action->sa));
735         if (wpa_s->wpa_state < WPA_ASSOCIATED ||
736             os_memcmp(action->sa, wpa_s->bssid, ETH_ALEN) != 0) {
737                 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
738                            "frame");
739                 return;
740         }
741
742         switch (act) {
743         case WNM_BSS_TRANS_MGMT_REQ:
744                 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
745                                                  !(action->da[0] & 0x01));
746                 break;
747         case WNM_SLEEP_MODE_RESP:
748                 ieee802_11_rx_wnmsleep_resp(wpa_s, action->data, action->len);
749                 break;
750         default:
751                 wpa_printf(MSG_ERROR, "WNM: Unknown request");
752                 break;
753         }
754 }