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