LCOV - code coverage report
Current view: top level - wpa_supplicant - wnm_sta.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1393793999 Lines: 232 522 44.4 %
Date: 2014-03-02 Functions: 11 17 64.7 %
Branches: 68 235 28.9 %

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

Generated by: LCOV version 1.9