a4743eb1ef3c534a14b93caa1b0e31254ad9c641
[mech_eap.git] / wpa_supplicant / wnm_sta.c
1 /*
2  * wpa_supplicant - WNM
3  * Copyright (c) 2011-2013, 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 "common/ieee802_11_common.h"
14 #include "common/wpa_ctrl.h"
15 #include "rsn_supp/wpa.h"
16 #include "wpa_supplicant_i.h"
17 #include "driver_i.h"
18 #include "scan.h"
19 #include "ctrl_iface.h"
20 #include "bss.h"
21 #include "wnm_sta.h"
22 #include "hs20_supplicant.h"
23
24 #define MAX_TFS_IE_LEN  1024
25 #define WNM_MAX_NEIGHBOR_REPORT 10
26
27
28 /* get the TFS IE from driver */
29 static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
30                                    u16 *buf_len, enum wnm_oper oper)
31 {
32         wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
33
34         return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
35 }
36
37
38 /* set the TFS IE to driver */
39 static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
40                                    const u8 *addr, u8 *buf, u16 *buf_len,
41                                    enum wnm_oper oper)
42 {
43         wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
44
45         return wpa_drv_wnm_oper(wpa_s, oper, addr, buf, buf_len);
46 }
47
48
49 /* MLME-SLEEPMODE.request */
50 int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
51                                  u8 action, u16 intval, struct wpabuf *tfs_req)
52 {
53         struct ieee80211_mgmt *mgmt;
54         int res;
55         size_t len;
56         struct wnm_sleep_element *wnmsleep_ie;
57         u8 *wnmtfs_ie;
58         u8 wnmsleep_ie_len;
59         u16 wnmtfs_ie_len;  /* possibly multiple IE(s) */
60         enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
61                 WNM_SLEEP_TFS_REQ_IE_NONE;
62
63         wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
64                    "action=%s to " MACSTR,
65                    action == 0 ? "enter" : "exit",
66                    MAC2STR(wpa_s->bssid));
67
68         /* WNM-Sleep Mode IE */
69         wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
70         wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
71         if (wnmsleep_ie == NULL)
72                 return -1;
73         wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
74         wnmsleep_ie->len = wnmsleep_ie_len - 2;
75         wnmsleep_ie->action_type = action;
76         wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
77         wnmsleep_ie->intval = host_to_le16(intval);
78         wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
79                     (u8 *) wnmsleep_ie, wnmsleep_ie_len);
80
81         /* TFS IE(s) */
82         if (tfs_req) {
83                 wnmtfs_ie_len = wpabuf_len(tfs_req);
84                 wnmtfs_ie = os_malloc(wnmtfs_ie_len);
85                 if (wnmtfs_ie == NULL) {
86                         os_free(wnmsleep_ie);
87                         return -1;
88                 }
89                 os_memcpy(wnmtfs_ie, wpabuf_head(tfs_req), wnmtfs_ie_len);
90         } else {
91                 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
92                 if (wnmtfs_ie == NULL) {
93                         os_free(wnmsleep_ie);
94                         return -1;
95                 }
96                 if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
97                                             tfs_oper)) {
98                         wnmtfs_ie_len = 0;
99                         os_free(wnmtfs_ie);
100                         wnmtfs_ie = NULL;
101                 }
102         }
103         wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
104                     (u8 *) wnmtfs_ie, wnmtfs_ie_len);
105
106         mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len);
107         if (mgmt == NULL) {
108                 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
109                            "WNM-Sleep Request action frame");
110                 os_free(wnmsleep_ie);
111                 os_free(wnmtfs_ie);
112                 return -1;
113         }
114
115         os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
116         os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
117         os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
118         mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
119                                            WLAN_FC_STYPE_ACTION);
120         mgmt->u.action.category = WLAN_ACTION_WNM;
121         mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
122         mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
123         os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
124                   wnmsleep_ie_len);
125         /* copy TFS IE here */
126         if (wnmtfs_ie_len > 0) {
127                 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
128                           wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
129         }
130
131         len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
132                 wnmtfs_ie_len;
133
134         res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
135                                   wpa_s->own_addr, wpa_s->bssid,
136                                   &mgmt->u.action.category, len, 0);
137         if (res < 0)
138                 wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
139                            "(action=%d, intval=%d)", action, intval);
140
141         os_free(wnmsleep_ie);
142         os_free(wnmtfs_ie);
143         os_free(mgmt);
144
145         return res;
146 }
147
148
149 static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
150                                          u8 *tfsresp_ie_start,
151                                          u8 *tfsresp_ie_end)
152 {
153         wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
154                          wpa_s->bssid, NULL, NULL);
155         /* remove GTK/IGTK ?? */
156
157         /* set the TFS Resp IE(s) */
158         if (tfsresp_ie_start && tfsresp_ie_end &&
159             tfsresp_ie_end - tfsresp_ie_start >= 0) {
160                 u16 tfsresp_ie_len;
161                 tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
162                         tfsresp_ie_start;
163                 wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
164                 /* pass the TFS Resp IE(s) to driver for processing */
165                 if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
166                                             tfsresp_ie_start,
167                                             &tfsresp_ie_len,
168                                             WNM_SLEEP_TFS_RESP_IE_SET))
169                         wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
170         }
171 }
172
173
174 static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
175                                         const u8 *frm, u16 key_len_total)
176 {
177         u8 *ptr, *end;
178         u8 gtk_len;
179
180         wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM,  wpa_s->bssid,
181                          NULL, NULL);
182
183         /* Install GTK/IGTK */
184
185         /* point to key data field */
186         ptr = (u8 *) frm + 1 + 2;
187         end = ptr + key_len_total;
188         wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
189
190         while (ptr + 1 < end) {
191                 if (ptr + 2 + ptr[1] > end) {
192                         wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
193                                    "length");
194                         if (end > ptr) {
195                                 wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
196                                             ptr, end - ptr);
197                         }
198                         break;
199                 }
200                 if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
201                         if (ptr[1] < 11 + 5) {
202                                 wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
203                                            "subelem");
204                                 break;
205                         }
206                         gtk_len = *(ptr + 4);
207                         if (ptr[1] < 11 + gtk_len ||
208                             gtk_len < 5 || gtk_len > 32) {
209                                 wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
210                                            "subelem");
211                                 break;
212                         }
213                         wpa_wnmsleep_install_key(
214                                 wpa_s->wpa,
215                                 WNM_SLEEP_SUBELEM_GTK,
216                                 ptr);
217                         ptr += 13 + gtk_len;
218 #ifdef CONFIG_IEEE80211W
219                 } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
220                         if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
221                                 wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
222                                            "subelem");
223                                 break;
224                         }
225                         wpa_wnmsleep_install_key(wpa_s->wpa,
226                                                  WNM_SLEEP_SUBELEM_IGTK, ptr);
227                         ptr += 10 + WPA_IGTK_LEN;
228 #endif /* CONFIG_IEEE80211W */
229                 } else
230                         break; /* skip the loop */
231         }
232 }
233
234
235 static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
236                                         const u8 *frm, int len)
237 {
238         /*
239          * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
240          * WNM-Sleep Mode IE | TFS Response IE
241          */
242         u8 *pos = (u8 *) frm; /* point to payload after the action field */
243         u16 key_len_total;
244         struct wnm_sleep_element *wnmsleep_ie = NULL;
245         /* multiple TFS Resp IE (assuming consecutive) */
246         u8 *tfsresp_ie_start = NULL;
247         u8 *tfsresp_ie_end = NULL;
248
249         if (len < 3)
250                 return;
251         key_len_total = WPA_GET_LE16(frm + 1);
252
253         wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
254                    frm[0], key_len_total);
255         pos += 3 + key_len_total;
256         if (pos > frm + len) {
257                 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
258                 return;
259         }
260         while (pos - frm < len) {
261                 u8 ie_len = *(pos + 1);
262                 if (pos + 2 + ie_len > frm + len) {
263                         wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
264                         break;
265                 }
266                 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
267                 if (*pos == WLAN_EID_WNMSLEEP)
268                         wnmsleep_ie = (struct wnm_sleep_element *) pos;
269                 else if (*pos == WLAN_EID_TFS_RESP) {
270                         if (!tfsresp_ie_start)
271                                 tfsresp_ie_start = pos;
272                         tfsresp_ie_end = pos;
273                 } else
274                         wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
275                 pos += ie_len + 2;
276         }
277
278         if (!wnmsleep_ie) {
279                 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
280                 return;
281         }
282
283         if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
284             wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
285                 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
286                            "frame (action=%d, intval=%d)",
287                            wnmsleep_ie->action_type, wnmsleep_ie->intval);
288                 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
289                         wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
290                                                      tfsresp_ie_end);
291                 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
292                         wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
293                 }
294         } else {
295                 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
296                            "(action=%d, intval=%d)",
297                            wnmsleep_ie->action_type, wnmsleep_ie->intval);
298                 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
299                         wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
300                                          wpa_s->bssid, NULL, NULL);
301                 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
302                         wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
303                                          wpa_s->bssid, NULL, NULL);
304         }
305 }
306
307
308 void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
309 {
310         int i;
311
312         for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
313                 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
314                 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
315         }
316
317         wpa_s->wnm_num_neighbor_report = 0;
318         os_free(wpa_s->wnm_neighbor_report_elements);
319         wpa_s->wnm_neighbor_report_elements = NULL;
320 }
321
322
323 static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
324                                            u8 id, u8 elen, const u8 *pos)
325 {
326         switch (id) {
327         case WNM_NEIGHBOR_TSF:
328                 if (elen < 2 + 2) {
329                         wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
330                         break;
331                 }
332                 rep->tsf_offset = WPA_GET_LE16(pos);
333                 rep->beacon_int = WPA_GET_LE16(pos + 2);
334                 rep->tsf_present = 1;
335                 break;
336         case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
337                 if (elen < 2) {
338                         wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
339                                    "country string");
340                         break;
341                 }
342                 os_memcpy(rep->country, pos, 2);
343                 rep->country_present = 1;
344                 break;
345         case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
346                 if (elen < 1) {
347                         wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
348                                    "candidate");
349                         break;
350                 }
351                 rep->preference = pos[0];
352                 rep->preference_present = 1;
353                 break;
354         case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
355                 rep->bss_term_tsf = WPA_GET_LE64(pos);
356                 rep->bss_term_dur = WPA_GET_LE16(pos + 8);
357                 rep->bss_term_present = 1;
358                 break;
359         case WNM_NEIGHBOR_BEARING:
360                 if (elen < 8) {
361                         wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
362                                    "bearing");
363                         break;
364                 }
365                 rep->bearing = WPA_GET_LE16(pos);
366                 rep->distance = WPA_GET_LE32(pos + 2);
367                 rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
368                 rep->bearing_present = 1;
369                 break;
370         case WNM_NEIGHBOR_MEASUREMENT_PILOT:
371                 if (elen < 1) {
372                         wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
373                                    "pilot");
374                         break;
375                 }
376                 os_free(rep->meas_pilot);
377                 rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
378                 if (rep->meas_pilot == NULL)
379                         break;
380                 rep->meas_pilot->measurement_pilot = pos[0];
381                 rep->meas_pilot->subelem_len = elen - 1;
382                 os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
383                 break;
384         case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
385                 if (elen < 5) {
386                         wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
387                                    "capabilities");
388                         break;
389                 }
390                 os_memcpy(rep->rm_capab, pos, 5);
391                 rep->rm_capab_present = 1;
392                 break;
393         case WNM_NEIGHBOR_MULTIPLE_BSSID:
394                 if (elen < 1) {
395                         wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
396                         break;
397                 }
398                 os_free(rep->mul_bssid);
399                 rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
400                 if (rep->mul_bssid == NULL)
401                         break;
402                 rep->mul_bssid->max_bssid_indicator = pos[0];
403                 rep->mul_bssid->subelem_len = elen - 1;
404                 os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
405                 break;
406         }
407 }
408
409
410 static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
411 {
412         return ieee80211_chan_to_freq(NULL, op_class, chan);
413 }
414
415
416 static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
417                                       const u8 *pos, u8 len,
418                                       struct neighbor_report *rep)
419 {
420         u8 left = len;
421
422         if (left < 13) {
423                 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
424                 return;
425         }
426
427         os_memcpy(rep->bssid, pos, ETH_ALEN);
428         rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
429         rep->regulatory_class = *(pos + 10);
430         rep->channel_number = *(pos + 11);
431         rep->phy_type = *(pos + 12);
432
433         pos += 13;
434         left -= 13;
435
436         while (left >= 2) {
437                 u8 id, elen;
438
439                 id = *pos++;
440                 elen = *pos++;
441                 wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
442                 left -= 2;
443                 if (elen > left) {
444                         wpa_printf(MSG_DEBUG,
445                                    "WNM: Truncated neighbor report subelement");
446                         break;
447                 }
448                 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
449                 left -= elen;
450                 pos += elen;
451         }
452
453         rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
454                                      rep->channel_number);
455 }
456
457
458 static struct wpa_bss *
459 compare_scan_neighbor_results(struct wpa_supplicant *wpa_s)
460 {
461
462         u8 i;
463         struct wpa_bss *bss = wpa_s->current_bss;
464         struct wpa_bss *target;
465
466         if (!bss)
467                 return 0;
468
469         wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
470                    MAC2STR(wpa_s->bssid), bss->level);
471
472         for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
473                 struct neighbor_report *nei;
474
475                 nei = &wpa_s->wnm_neighbor_report_elements[i];
476                 if (nei->preference_present && nei->preference == 0) {
477                         wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
478                                    MAC2STR(nei->bssid));
479                         continue;
480                 }
481
482                 target = wpa_bss_get_bssid(wpa_s, nei->bssid);
483                 if (!target) {
484                         wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
485                                    " (pref %d) not found in scan results",
486                                    MAC2STR(nei->bssid),
487                                    nei->preference_present ? nei->preference :
488                                    -1);
489                         continue;
490                 }
491
492                 if (bss->ssid_len != target->ssid_len ||
493                     os_memcmp(bss->ssid, target->ssid, bss->ssid_len) != 0) {
494                         /*
495                          * TODO: Could consider allowing transition to another
496                          * ESS if PMF was enabled for the association.
497                          */
498                         wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
499                                    " (pref %d) in different ESS",
500                                    MAC2STR(nei->bssid),
501                                    nei->preference_present ? nei->preference :
502                                    -1);
503                         continue;
504                 }
505
506                 if (target->level < bss->level && target->level < -80) {
507                         wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
508                                    " (pref %d) does not have sufficient signal level (%d)",
509                                    MAC2STR(nei->bssid),
510                                    nei->preference_present ? nei->preference :
511                                    -1,
512                                    target->level);
513                         continue;
514                 }
515
516                 wpa_printf(MSG_DEBUG,
517                            "WNM: Found an acceptable preferred transition candidate BSS "
518                            MACSTR " (RSSI %d)",
519                            MAC2STR(nei->bssid), target->level);
520                 return target;
521         }
522
523         return NULL;
524 }
525
526
527 static void wnm_send_bss_transition_mgmt_resp(
528         struct wpa_supplicant *wpa_s, u8 dialog_token,
529         enum bss_trans_mgmt_status_code status, u8 delay,
530         const u8 *target_bssid)
531 {
532         u8 buf[1000], *pos;
533         struct ieee80211_mgmt *mgmt;
534         size_t len;
535         int res;
536
537         wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
538                    "to " MACSTR " dialog_token=%u status=%u delay=%d",
539                    MAC2STR(wpa_s->bssid), dialog_token, status, delay);
540         if (!wpa_s->current_bss) {
541                 wpa_printf(MSG_DEBUG,
542                            "WNM: Current BSS not known - drop response");
543                 return;
544         }
545
546         mgmt = (struct ieee80211_mgmt *) buf;
547         os_memset(&buf, 0, sizeof(buf));
548         os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
549         os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
550         os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
551         mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
552                                            WLAN_FC_STYPE_ACTION);
553         mgmt->u.action.category = WLAN_ACTION_WNM;
554         mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
555         mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
556         mgmt->u.action.u.bss_tm_resp.status_code = status;
557         mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
558         pos = mgmt->u.action.u.bss_tm_resp.variable;
559         if (target_bssid) {
560                 os_memcpy(pos, target_bssid, ETH_ALEN);
561                 pos += ETH_ALEN;
562         } else if (status == WNM_BSS_TM_ACCEPT) {
563                 /*
564                  * P802.11-REVmc clarifies that the Target BSSID field is always
565                  * present when status code is zero, so use a fake value here if
566                  * no BSSID is yet known.
567                  */
568                 os_memset(pos, 0, ETH_ALEN);
569                 pos += ETH_ALEN;
570         }
571
572         len = pos - (u8 *) &mgmt->u.action.category;
573
574         res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
575                                   wpa_s->own_addr, wpa_s->bssid,
576                                   &mgmt->u.action.category, len, 0);
577         if (res < 0) {
578                 wpa_printf(MSG_DEBUG,
579                            "WNM: Failed to send BSS Transition Management Response");
580         }
581 }
582
583
584 int wnm_scan_process(struct wpa_supplicant *wpa_s, int reply_on_fail)
585 {
586         struct wpa_bss *bss;
587         struct wpa_ssid *ssid = wpa_s->current_ssid;
588         enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
589
590         if (!wpa_s->wnm_neighbor_report_elements)
591                 return 0;
592
593         if (os_reltime_before(&wpa_s->wnm_cand_valid_until,
594                               &wpa_s->scan_trigger_time)) {
595                 wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
596                 wnm_deallocate_memory(wpa_s);
597                 return 0;
598         }
599
600         if (!wpa_s->current_bss ||
601             os_memcmp(wpa_s->wnm_cand_from_bss, wpa_s->current_bss->bssid,
602                       ETH_ALEN) != 0) {
603                 wpa_printf(MSG_DEBUG, "WNM: Stored BSS transition candidate list not from the current BSS - ignore it");
604                 return 0;
605         }
606
607         /* Compare the Neighbor Report and scan results */
608         bss = compare_scan_neighbor_results(wpa_s);
609         if (!bss) {
610                 wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
611                 status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
612                 goto send_bss_resp_fail;
613         }
614
615         /* Associate to the network */
616         /* Send the BSS Management Response - Accept */
617         if (wpa_s->wnm_reply) {
618                 wpa_s->wnm_reply = 0;
619                 wnm_send_bss_transition_mgmt_resp(wpa_s,
620                                                   wpa_s->wnm_dialog_token,
621                                                   WNM_BSS_TM_ACCEPT,
622                                                   0, bss->bssid);
623         }
624
625         if (bss == wpa_s->current_bss) {
626                 wpa_printf(MSG_DEBUG,
627                            "WNM: Already associated with the preferred candidate");
628                 return 1;
629         }
630
631         wpa_s->reassociate = 1;
632         wpa_supplicant_connect(wpa_s, bss, ssid);
633         wnm_deallocate_memory(wpa_s);
634         return 1;
635
636 send_bss_resp_fail:
637         if (!reply_on_fail)
638                 return 0;
639
640         /* Send reject response for all the failures */
641
642         if (wpa_s->wnm_reply) {
643                 wpa_s->wnm_reply = 0;
644                 wnm_send_bss_transition_mgmt_resp(wpa_s,
645                                                   wpa_s->wnm_dialog_token,
646                                                   status, 0, NULL);
647         }
648         wnm_deallocate_memory(wpa_s);
649
650         return 0;
651 }
652
653
654 static int cand_pref_compar(const void *a, const void *b)
655 {
656         const struct neighbor_report *aa = a;
657         const struct neighbor_report *bb = b;
658
659         if (!aa->preference_present && !bb->preference_present)
660                 return 0;
661         if (!aa->preference_present)
662                 return 1;
663         if (!bb->preference_present)
664                 return -1;
665         if (bb->preference > aa->preference)
666                 return 1;
667         if (bb->preference < aa->preference)
668                 return -1;
669         return 0;
670 }
671
672
673 static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
674 {
675         if (!wpa_s->wnm_neighbor_report_elements)
676                 return;
677         qsort(wpa_s->wnm_neighbor_report_elements,
678               wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
679               cand_pref_compar);
680 }
681
682
683 static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
684 {
685         unsigned int i;
686
687         wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
688         if (!wpa_s->wnm_neighbor_report_elements)
689                 return;
690         for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
691                 struct neighbor_report *nei;
692
693                 nei = &wpa_s->wnm_neighbor_report_elements[i];
694                 wpa_printf(MSG_DEBUG, "%u: " MACSTR
695                            " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
696                            i, MAC2STR(nei->bssid), nei->bssid_info,
697                            nei->regulatory_class,
698                            nei->channel_number, nei->phy_type,
699                            nei->preference_present ? nei->preference : -1,
700                            nei->freq);
701         }
702 }
703
704
705 static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
706 {
707         unsigned int i;
708
709         for (i = 0; i < wpa_s->hw.num_modes; i++) {
710                 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
711                 int j;
712
713                 for (j = 0; j < mode->num_channels; j++) {
714                         struct hostapd_channel_data *chan;
715
716                         chan = &mode->channels[j];
717                         if (chan->freq == freq &&
718                             !(chan->flag & HOSTAPD_CHAN_DISABLED))
719                                 return 1;
720                 }
721         }
722
723         return 0;
724 }
725
726
727 static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
728 {
729         int *freqs;
730         int num_freqs = 0;
731         unsigned int i;
732
733         if (!wpa_s->wnm_neighbor_report_elements)
734                 return;
735
736         if (wpa_s->hw.modes == NULL)
737                 return;
738
739         os_free(wpa_s->next_scan_freqs);
740         wpa_s->next_scan_freqs = NULL;
741
742         freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
743         if (freqs == NULL)
744                 return;
745
746         for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
747                 struct neighbor_report *nei;
748
749                 nei = &wpa_s->wnm_neighbor_report_elements[i];
750                 if (nei->freq <= 0) {
751                         wpa_printf(MSG_DEBUG,
752                                    "WNM: Unknown neighbor operating frequency for "
753                                    MACSTR " - scan all channels",
754                                    MAC2STR(nei->bssid));
755                         os_free(freqs);
756                         return;
757                 }
758                 if (chan_supported(wpa_s, nei->freq))
759                         add_freq(freqs, &num_freqs, nei->freq);
760         }
761
762         if (num_freqs == 0) {
763                 os_free(freqs);
764                 return;
765         }
766
767         wpa_printf(MSG_DEBUG,
768                    "WNM: Scan %d frequencies based on transition candidate list",
769                    num_freqs);
770         wpa_s->next_scan_freqs = freqs;
771 }
772
773
774 static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
775                                              const u8 *pos, const u8 *end,
776                                              int reply)
777 {
778         unsigned int beacon_int;
779         u8 valid_int;
780
781         if (pos + 5 > end)
782                 return;
783
784         if (wpa_s->current_bss)
785                 beacon_int = wpa_s->current_bss->beacon_int;
786         else
787                 beacon_int = 100; /* best guess */
788
789         wpa_s->wnm_dialog_token = pos[0];
790         wpa_s->wnm_mode = pos[1];
791         wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
792         valid_int = pos[4];
793         wpa_s->wnm_reply = reply;
794
795         wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
796                    "dialog_token=%u request_mode=0x%x "
797                    "disassoc_timer=%u validity_interval=%u",
798                    wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
799                    wpa_s->wnm_dissoc_timer, valid_int);
800
801         pos += 5;
802
803         if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
804                 if (pos + 12 > end) {
805                         wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
806                         return;
807                 }
808                 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
809                 pos += 12; /* BSS Termination Duration */
810         }
811
812         if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
813                 char url[256];
814
815                 if (pos + 1 > end || pos + 1 + pos[0] > end) {
816                         wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
817                                    "Management Request (URL)");
818                         return;
819                 }
820                 os_memcpy(url, pos + 1, pos[0]);
821                 url[pos[0]] = '\0';
822                 pos += 1 + pos[0];
823
824                 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
825                         wpa_sm_pmf_enabled(wpa_s->wpa),
826                         wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
827         }
828
829         if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
830                 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
831                         "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
832                 if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
833                         /* TODO: mark current BSS less preferred for
834                          * selection */
835                         wpa_printf(MSG_DEBUG, "Trying to find another BSS");
836                         wpa_supplicant_req_scan(wpa_s, 0, 0);
837                 }
838         }
839
840         if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
841                 unsigned int valid_ms;
842
843                 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
844                 wnm_deallocate_memory(wpa_s);
845                 wpa_s->wnm_neighbor_report_elements = os_zalloc(
846                         WNM_MAX_NEIGHBOR_REPORT *
847                         sizeof(struct neighbor_report));
848                 if (wpa_s->wnm_neighbor_report_elements == NULL)
849                         return;
850
851                 while (pos + 2 <= end &&
852                        wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
853                 {
854                         u8 tag = *pos++;
855                         u8 len = *pos++;
856
857                         wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
858                                    tag);
859                         if (pos + len > end) {
860                                 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
861                                 return;
862                         }
863                         if (tag == WLAN_EID_NEIGHBOR_REPORT) {
864                                 struct neighbor_report *rep;
865                                 rep = &wpa_s->wnm_neighbor_report_elements[
866                                         wpa_s->wnm_num_neighbor_report];
867                                 wnm_parse_neighbor_report(wpa_s, pos, len, rep);
868                         }
869
870                         pos += len;
871                         wpa_s->wnm_num_neighbor_report++;
872                 }
873                 wnm_sort_cand_list(wpa_s);
874                 wnm_dump_cand_list(wpa_s);
875                 valid_ms = valid_int * beacon_int * 128 / 125;
876                 wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
877                            valid_ms);
878                 os_get_reltime(&wpa_s->wnm_cand_valid_until);
879                 wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
880                 wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
881                 wpa_s->wnm_cand_valid_until.sec +=
882                         wpa_s->wnm_cand_valid_until.usec / 1000000;
883                 wpa_s->wnm_cand_valid_until.usec %= 1000000;
884                 os_memcpy(wpa_s->wnm_cand_from_bss, wpa_s->bssid, ETH_ALEN);
885
886                 if (wpa_s->last_scan_res_used > 0) {
887                         struct os_reltime now;
888
889                         os_get_reltime(&now);
890                         if (!os_reltime_expired(&now, &wpa_s->last_scan, 10)) {
891                                 wpa_printf(MSG_DEBUG,
892                                            "WNM: Try to use recent scan results");
893                                 if (wnm_scan_process(wpa_s, 0) > 0)
894                                         return;
895                                 wpa_printf(MSG_DEBUG,
896                                            "WNM: No match in previous scan results - try a new scan");
897                         }
898                 }
899
900                 wnm_set_scan_freqs(wpa_s);
901                 wpa_supplicant_req_scan(wpa_s, 0, 0);
902         } else if (reply) {
903                 enum bss_trans_mgmt_status_code status;
904                 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
905                         status = WNM_BSS_TM_ACCEPT;
906                 else {
907                         wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
908                         status = WNM_BSS_TM_REJECT_UNSPECIFIED;
909                 }
910                 wnm_send_bss_transition_mgmt_resp(wpa_s,
911                                                   wpa_s->wnm_dialog_token,
912                                                   status, 0, NULL);
913         }
914 }
915
916
917 int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
918                                        u8 query_reason)
919 {
920         u8 buf[1000], *pos;
921         struct ieee80211_mgmt *mgmt;
922         size_t len;
923         int ret;
924
925         wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
926                    MACSTR " query_reason=%u",
927                    MAC2STR(wpa_s->bssid), query_reason);
928
929         mgmt = (struct ieee80211_mgmt *) buf;
930         os_memset(&buf, 0, sizeof(buf));
931         os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
932         os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
933         os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
934         mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
935                                            WLAN_FC_STYPE_ACTION);
936         mgmt->u.action.category = WLAN_ACTION_WNM;
937         mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
938         mgmt->u.action.u.bss_tm_query.dialog_token = 1;
939         mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
940         pos = mgmt->u.action.u.bss_tm_query.variable;
941
942         len = pos - (u8 *) &mgmt->u.action.category;
943
944         ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
945                                   wpa_s->own_addr, wpa_s->bssid,
946                                   &mgmt->u.action.category, len, 0);
947
948         return ret;
949 }
950
951
952 static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
953                                             const u8 *sa, const u8 *data,
954                                             int len)
955 {
956         const u8 *pos, *end, *next;
957         u8 ie, ie_len;
958
959         pos = data;
960         end = data + len;
961
962         while (pos + 1 < end) {
963                 ie = *pos++;
964                 ie_len = *pos++;
965                 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
966                            ie, ie_len);
967                 if (ie_len > end - pos) {
968                         wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
969                                    "subelement");
970                         break;
971                 }
972                 next = pos + ie_len;
973                 if (ie_len < 4) {
974                         pos = next;
975                         continue;
976                 }
977                 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
978                            WPA_GET_BE24(pos), pos[3]);
979
980 #ifdef CONFIG_HS20
981                 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
982                     WPA_GET_BE24(pos) == OUI_WFA &&
983                     pos[3] == HS20_WNM_SUB_REM_NEEDED) {
984                         /* Subscription Remediation subelement */
985                         const u8 *ie_end;
986                         u8 url_len;
987                         char *url;
988                         u8 osu_method;
989
990                         wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
991                                    "subelement");
992                         ie_end = pos + ie_len;
993                         pos += 4;
994                         url_len = *pos++;
995                         if (url_len == 0) {
996                                 wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
997                                 url = NULL;
998                                 osu_method = 1;
999                         } else {
1000                                 if (pos + url_len + 1 > ie_end) {
1001                                         wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
1002                                                    url_len,
1003                                                    (int) (ie_end - pos));
1004                                         break;
1005                                 }
1006                                 url = os_malloc(url_len + 1);
1007                                 if (url == NULL)
1008                                         break;
1009                                 os_memcpy(url, pos, url_len);
1010                                 url[url_len] = '\0';
1011                                 osu_method = pos[url_len];
1012                         }
1013                         hs20_rx_subscription_remediation(wpa_s, url,
1014                                                          osu_method);
1015                         os_free(url);
1016                         pos = next;
1017                         continue;
1018                 }
1019
1020                 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1021                     WPA_GET_BE24(pos) == OUI_WFA &&
1022                     pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1023                         const u8 *ie_end;
1024                         u8 url_len;
1025                         char *url;
1026                         u8 code;
1027                         u16 reauth_delay;
1028
1029                         ie_end = pos + ie_len;
1030                         pos += 4;
1031                         code = *pos++;
1032                         reauth_delay = WPA_GET_LE16(pos);
1033                         pos += 2;
1034                         url_len = *pos++;
1035                         wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1036                                    "Imminent - Reason Code %u   "
1037                                    "Re-Auth Delay %u  URL Length %u",
1038                                    code, reauth_delay, url_len);
1039                         if (pos + url_len > ie_end)
1040                                 break;
1041                         url = os_malloc(url_len + 1);
1042                         if (url == NULL)
1043                                 break;
1044                         os_memcpy(url, pos, url_len);
1045                         url[url_len] = '\0';
1046                         hs20_rx_deauth_imminent_notice(wpa_s, code,
1047                                                        reauth_delay, url);
1048                         os_free(url);
1049                         pos = next;
1050                         continue;
1051                 }
1052 #endif /* CONFIG_HS20 */
1053
1054                 pos = next;
1055         }
1056 }
1057
1058
1059 static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1060                                         const u8 *sa, const u8 *frm, int len)
1061 {
1062         const u8 *pos, *end;
1063         u8 dialog_token, type;
1064
1065         /* Dialog Token [1] | Type [1] | Subelements */
1066
1067         if (len < 2 || sa == NULL)
1068                 return;
1069         end = frm + len;
1070         pos = frm;
1071         dialog_token = *pos++;
1072         type = *pos++;
1073
1074         wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1075                 "(dialog_token %u type %u sa " MACSTR ")",
1076                 dialog_token, type, MAC2STR(sa));
1077         wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1078                     pos, end - pos);
1079
1080         if (wpa_s->wpa_state != WPA_COMPLETED ||
1081             os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
1082                 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1083                         "from our AP - ignore it");
1084                 return;
1085         }
1086
1087         switch (type) {
1088         case 1:
1089                 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1090                 break;
1091         default:
1092                 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1093                         "WNM-Notification type %u", type);
1094                 break;
1095         }
1096 }
1097
1098
1099 void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
1100                               const struct ieee80211_mgmt *mgmt, size_t len)
1101 {
1102         const u8 *pos, *end;
1103         u8 act;
1104
1105         if (len < IEEE80211_HDRLEN + 2)
1106                 return;
1107
1108         pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
1109         act = *pos++;
1110         end = ((const u8 *) mgmt) + len;
1111
1112         wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
1113                    act, MAC2STR(mgmt->sa));
1114         if (wpa_s->wpa_state < WPA_ASSOCIATED ||
1115             os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
1116                 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
1117                            "frame");
1118                 return;
1119         }
1120
1121         switch (act) {
1122         case WNM_BSS_TRANS_MGMT_REQ:
1123                 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
1124                                                  !(mgmt->da[0] & 0x01));
1125                 break;
1126         case WNM_SLEEP_MODE_RESP:
1127                 ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
1128                 break;
1129         case WNM_NOTIFICATION_REQ:
1130                 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
1131                 break;
1132         default:
1133                 wpa_printf(MSG_ERROR, "WNM: Unknown request");
1134                 break;
1135         }
1136 }