LCOV - code coverage report
Current view: top level - wpa_supplicant - ctrl_iface.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1475438200 Lines: 5113 5659 90.4 %
Date: 2016-10-02 Functions: 209 210 99.5 %

          Line data    Source code
       1             : /*
       2             :  * WPA Supplicant / Control interface (shared code for all backends)
       3             :  * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
       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             : #ifdef CONFIG_TESTING_OPTIONS
      11             : #include <net/ethernet.h>
      12             : #include <netinet/ip.h>
      13             : #endif /* CONFIG_TESTING_OPTIONS */
      14             : 
      15             : #include "utils/common.h"
      16             : #include "utils/eloop.h"
      17             : #include "utils/uuid.h"
      18             : #include "utils/module_tests.h"
      19             : #include "common/version.h"
      20             : #include "common/ieee802_11_defs.h"
      21             : #include "common/ieee802_11_common.h"
      22             : #include "common/wpa_ctrl.h"
      23             : #include "crypto/tls.h"
      24             : #include "ap/hostapd.h"
      25             : #include "eap_peer/eap.h"
      26             : #include "eapol_supp/eapol_supp_sm.h"
      27             : #include "rsn_supp/wpa.h"
      28             : #include "rsn_supp/preauth.h"
      29             : #include "rsn_supp/pmksa_cache.h"
      30             : #include "l2_packet/l2_packet.h"
      31             : #include "wps/wps.h"
      32             : #include "fst/fst.h"
      33             : #include "fst/fst_ctrl_iface.h"
      34             : #include "config.h"
      35             : #include "wpa_supplicant_i.h"
      36             : #include "driver_i.h"
      37             : #include "wps_supplicant.h"
      38             : #include "ibss_rsn.h"
      39             : #include "ap.h"
      40             : #include "p2p_supplicant.h"
      41             : #include "p2p/p2p.h"
      42             : #include "hs20_supplicant.h"
      43             : #include "wifi_display.h"
      44             : #include "notify.h"
      45             : #include "bss.h"
      46             : #include "scan.h"
      47             : #include "ctrl_iface.h"
      48             : #include "interworking.h"
      49             : #include "blacklist.h"
      50             : #include "autoscan.h"
      51             : #include "wnm_sta.h"
      52             : #include "offchannel.h"
      53             : #include "drivers/driver.h"
      54             : #include "mesh.h"
      55             : 
      56             : static int wpa_supplicant_global_iface_list(struct wpa_global *global,
      57             :                                             char *buf, int len);
      58             : static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
      59             :                                                   const char *input,
      60             :                                                   char *buf, int len);
      61             : static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
      62             :                                         char *val);
      63             : 
      64           6 : static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
      65             : {
      66             :         char *pos;
      67           6 :         u8 addr[ETH_ALEN], *filter = NULL, *n;
      68           6 :         size_t count = 0;
      69             : 
      70           6 :         pos = val;
      71          14 :         while (pos) {
      72           7 :                 if (*pos == '\0')
      73           3 :                         break;
      74           4 :                 if (hwaddr_aton(pos, addr)) {
      75           1 :                         os_free(filter);
      76           1 :                         return -1;
      77             :                 }
      78           3 :                 n = os_realloc_array(filter, count + 1, ETH_ALEN);
      79           3 :                 if (n == NULL) {
      80           1 :                         os_free(filter);
      81           1 :                         return -1;
      82             :                 }
      83           2 :                 filter = n;
      84           2 :                 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
      85           2 :                 count++;
      86             : 
      87           2 :                 pos = os_strchr(pos, ' ');
      88           2 :                 if (pos)
      89           1 :                         pos++;
      90             :         }
      91             : 
      92           4 :         wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
      93           4 :         os_free(wpa_s->bssid_filter);
      94           4 :         wpa_s->bssid_filter = filter;
      95           4 :         wpa_s->bssid_filter_count = count;
      96             : 
      97           4 :         return 0;
      98             : }
      99             : 
     100             : 
     101          18 : static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
     102             : {
     103             :         char *pos;
     104          18 :         u8 addr[ETH_ALEN], *bssid = NULL, *n;
     105          18 :         struct wpa_ssid_value *ssid = NULL, *ns;
     106          18 :         size_t count = 0, ssid_count = 0;
     107             :         struct wpa_ssid *c;
     108             : 
     109             :         /*
     110             :          * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
     111             :          * SSID_SPEC ::= ssid <SSID_HEX>
     112             :          * BSSID_SPEC ::= bssid <BSSID_HEX>
     113             :          */
     114             : 
     115          18 :         pos = val;
     116          46 :         while (pos) {
     117          21 :                 if (*pos == '\0')
     118           2 :                         break;
     119          19 :                 if (os_strncmp(pos, "bssid ", 6) == 0) {
     120             :                         int res;
     121          10 :                         pos += 6;
     122          10 :                         res = hwaddr_aton2(pos, addr);
     123          10 :                         if (res < 0) {
     124           2 :                                 os_free(ssid);
     125           2 :                                 os_free(bssid);
     126           2 :                                 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
     127             :                                            "BSSID value '%s'", pos);
     128           2 :                                 return -1;
     129             :                         }
     130           8 :                         pos += res;
     131           8 :                         n = os_realloc_array(bssid, count + 1, ETH_ALEN);
     132           8 :                         if (n == NULL) {
     133           1 :                                 os_free(ssid);
     134           1 :                                 os_free(bssid);
     135           1 :                                 return -1;
     136             :                         }
     137           7 :                         bssid = n;
     138           7 :                         os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
     139           7 :                         count++;
     140           9 :                 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
     141             :                         char *end;
     142           8 :                         pos += 5;
     143             : 
     144           8 :                         end = pos;
     145         122 :                         while (*end) {
     146         107 :                                 if (*end == '\0' || *end == ' ')
     147             :                                         break;
     148         106 :                                 end++;
     149             :                         }
     150             : 
     151           8 :                         ns = os_realloc_array(ssid, ssid_count + 1,
     152             :                                               sizeof(struct wpa_ssid_value));
     153           8 :                         if (ns == NULL) {
     154           1 :                                 os_free(ssid);
     155           1 :                                 os_free(bssid);
     156           1 :                                 return -1;
     157             :                         }
     158           7 :                         ssid = ns;
     159             : 
     160          12 :                         if ((end - pos) & 0x01 ||
     161           9 :                             end - pos > 2 * SSID_MAX_LEN ||
     162           4 :                             hexstr2bin(pos, ssid[ssid_count].ssid,
     163           4 :                                        (end - pos) / 2) < 0) {
     164           4 :                                 os_free(ssid);
     165           4 :                                 os_free(bssid);
     166           4 :                                 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
     167             :                                            "SSID value '%s'", pos);
     168           4 :                                 return -1;
     169             :                         }
     170           3 :                         ssid[ssid_count].ssid_len = (end - pos) / 2;
     171           6 :                         wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
     172           3 :                                           ssid[ssid_count].ssid,
     173           3 :                                           ssid[ssid_count].ssid_len);
     174           3 :                         ssid_count++;
     175           3 :                         pos = end;
     176             :                 } else {
     177           1 :                         wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
     178             :                                    "'%s'", pos);
     179           1 :                         os_free(ssid);
     180           1 :                         os_free(bssid);
     181           1 :                         return -1;
     182             :                 }
     183             : 
     184          10 :                 pos = os_strchr(pos, ' ');
     185          10 :                 if (pos)
     186           3 :                         pos++;
     187             :         }
     188             : 
     189           9 :         wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
     190           9 :         os_free(wpa_s->disallow_aps_bssid);
     191           9 :         wpa_s->disallow_aps_bssid = bssid;
     192           9 :         wpa_s->disallow_aps_bssid_count = count;
     193             : 
     194           9 :         wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
     195           9 :         os_free(wpa_s->disallow_aps_ssid);
     196           9 :         wpa_s->disallow_aps_ssid = ssid;
     197           9 :         wpa_s->disallow_aps_ssid_count = ssid_count;
     198             : 
     199           9 :         if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
     200           4 :                 return 0;
     201             : 
     202           5 :         c = wpa_s->current_ssid;
     203           5 :         if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
     204           1 :                 return 0;
     205             : 
     206           6 :         if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
     207           2 :             !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
     208           1 :                 return 0;
     209             : 
     210           3 :         wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
     211             :                    "because current AP was marked disallowed");
     212             : 
     213             : #ifdef CONFIG_SME
     214           3 :         wpa_s->sme.prev_bssid_set = 0;
     215             : #endif /* CONFIG_SME */
     216           3 :         wpa_s->reassociate = 1;
     217           3 :         wpa_s->own_disconnect_req = 1;
     218           3 :         wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
     219           3 :         wpa_supplicant_req_scan(wpa_s, 0, 0);
     220             : 
     221           3 :         return 0;
     222             : }
     223             : 
     224             : 
     225             : #ifndef CONFIG_NO_CONFIG_BLOBS
     226          87 : static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
     227             : {
     228          87 :         char *name = pos;
     229             :         struct wpa_config_blob *blob;
     230             :         size_t len;
     231             : 
     232          87 :         pos = os_strchr(pos, ' ');
     233          87 :         if (pos == NULL)
     234           1 :                 return -1;
     235          86 :         *pos++ = '\0';
     236          86 :         len = os_strlen(pos);
     237          86 :         if (len & 1)
     238           1 :                 return -1;
     239             : 
     240          85 :         wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
     241          85 :         blob = os_zalloc(sizeof(*blob));
     242          85 :         if (blob == NULL)
     243           1 :                 return -1;
     244          84 :         blob->name = os_strdup(name);
     245          84 :         blob->data = os_malloc(len / 2);
     246          84 :         if (blob->name == NULL || blob->data == NULL) {
     247           2 :                 wpa_config_free_blob(blob);
     248           2 :                 return -1;
     249             :         }
     250             : 
     251          82 :         if (hexstr2bin(pos, blob->data, len / 2) < 0) {
     252           1 :                 wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
     253           1 :                 wpa_config_free_blob(blob);
     254           1 :                 return -1;
     255             :         }
     256          81 :         blob->len = len / 2;
     257             : 
     258          81 :         wpa_config_set_blob(wpa_s->conf, blob);
     259             : 
     260          81 :         return 0;
     261             : }
     262             : #endif /* CONFIG_NO_CONFIG_BLOBS */
     263             : 
     264             : 
     265           5 : static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
     266             : {
     267             :         char *params;
     268             :         char *pos;
     269           5 :         int *freqs = NULL;
     270             :         int ret;
     271             : 
     272           5 :         if (atoi(cmd)) {
     273           4 :                 params = os_strchr(cmd, ' ');
     274           4 :                 os_free(wpa_s->manual_sched_scan_freqs);
     275           4 :                 if (params) {
     276           2 :                         params++;
     277           2 :                         pos = os_strstr(params, "freq=");
     278           2 :                         if (pos)
     279           2 :                                 freqs = freq_range_to_channel_list(wpa_s,
     280             :                                                                    pos + 5);
     281             :                 }
     282           4 :                 wpa_s->manual_sched_scan_freqs = freqs;
     283           4 :                 ret = wpas_start_pno(wpa_s);
     284             :         } else {
     285           1 :                 ret = wpas_stop_pno(wpa_s);
     286             :         }
     287           5 :         return ret;
     288             : }
     289             : 
     290             : 
     291           9 : static int wpas_ctrl_set_band(struct wpa_supplicant *wpa_s, char *band)
     292             : {
     293             :         union wpa_event_data event;
     294             : 
     295           9 :         if (os_strcmp(band, "AUTO") == 0)
     296           5 :                 wpa_s->setband = WPA_SETBAND_AUTO;
     297           4 :         else if (os_strcmp(band, "5G") == 0)
     298           1 :                 wpa_s->setband = WPA_SETBAND_5G;
     299           3 :         else if (os_strcmp(band, "2G") == 0)
     300           2 :                 wpa_s->setband = WPA_SETBAND_2G;
     301             :         else
     302           1 :                 return -1;
     303             : 
     304           8 :         if (wpa_drv_setband(wpa_s, wpa_s->setband) == 0) {
     305           0 :                 os_memset(&event, 0, sizeof(event));
     306           0 :                 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
     307           0 :                 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
     308           0 :                 wpa_supplicant_event(wpa_s, EVENT_CHANNEL_LIST_CHANGED, &event);
     309             :         }
     310             : 
     311           8 :         return 0;
     312             : }
     313             : 
     314             : 
     315           2 : static int wpas_ctrl_iface_set_lci(struct wpa_supplicant *wpa_s,
     316             :                                    const char *cmd)
     317             : {
     318             :         struct wpabuf *lci;
     319             : 
     320           2 :         if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
     321           1 :                 wpabuf_free(wpa_s->lci);
     322           1 :                 wpa_s->lci = NULL;
     323           1 :                 return 0;
     324             :         }
     325             : 
     326           1 :         lci = wpabuf_parse_bin(cmd);
     327           1 :         if (!lci)
     328           0 :                 return -1;
     329             : 
     330           1 :         if (os_get_reltime(&wpa_s->lci_time)) {
     331           0 :                 wpabuf_free(lci);
     332           0 :                 return -1;
     333             :         }
     334             : 
     335           1 :         wpabuf_free(wpa_s->lci);
     336           1 :         wpa_s->lci = lci;
     337             : 
     338           1 :         return 0;
     339             : }
     340             : 
     341             : 
     342        7906 : static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
     343             :                                          char *cmd)
     344             : {
     345             :         char *value;
     346        7906 :         int ret = 0;
     347             : 
     348        7906 :         value = os_strchr(cmd, ' ');
     349        7906 :         if (value == NULL)
     350           2 :                 return -1;
     351        7904 :         *value++ = '\0';
     352             : 
     353        7904 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
     354        7904 :         if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
     355           3 :                 eapol_sm_configure(wpa_s->eapol,
     356             :                                    atoi(value), -1, -1, -1);
     357        7901 :         } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
     358           5 :                 eapol_sm_configure(wpa_s->eapol,
     359             :                                    -1, atoi(value), -1, -1);
     360        7896 :         } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
     361           5 :                 eapol_sm_configure(wpa_s->eapol,
     362             :                                    -1, -1, atoi(value), -1);
     363        7891 :         } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
     364           5 :                 eapol_sm_configure(wpa_s->eapol,
     365             :                                    -1, -1, -1, atoi(value));
     366        7886 :         } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
     367           3 :                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
     368           3 :                                      atoi(value)))
     369           1 :                         ret = -1;
     370        7883 :         } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
     371             :                    0) {
     372           2 :                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
     373           2 :                                      atoi(value)))
     374           1 :                         ret = -1;
     375        7881 :         } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
     376           4 :                 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
     377           1 :                         ret = -1;
     378        7877 :         } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
     379           4 :                 wpa_s->wps_fragment_size = atoi(value);
     380             : #ifdef CONFIG_WPS_TESTING
     381        7873 :         } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
     382             :                 long int val;
     383           5 :                 val = strtol(value, NULL, 0);
     384           5 :                 if (val < 0 || val > 0xff) {
     385           2 :                         ret = -1;
     386           2 :                         wpa_printf(MSG_DEBUG, "WPS: Invalid "
     387             :                                    "wps_version_number %ld", val);
     388             :                 } else {
     389           3 :                         wps_version_number = val;
     390           6 :                         wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
     391             :                                    "version %u.%u",
     392           3 :                                    (wps_version_number & 0xf0) >> 4,
     393             :                                    wps_version_number & 0x0f);
     394             :                 }
     395        7868 :         } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
     396           1 :                 wps_testing_dummy_cred = atoi(value);
     397           1 :                 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
     398             :                            wps_testing_dummy_cred);
     399        7867 :         } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
     400           3 :                 wps_corrupt_pkhash = atoi(value);
     401           3 :                 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
     402             :                            wps_corrupt_pkhash);
     403        7864 :         } else if (os_strcasecmp(cmd, "wps_force_auth_types") == 0) {
     404           3 :                 if (value[0] == '\0') {
     405           1 :                         wps_force_auth_types_in_use = 0;
     406             :                 } else {
     407           2 :                         wps_force_auth_types = strtol(value, NULL, 0);
     408           2 :                         wps_force_auth_types_in_use = 1;
     409             :                 }
     410        7861 :         } else if (os_strcasecmp(cmd, "wps_force_encr_types") == 0) {
     411           2 :                 if (value[0] == '\0') {
     412           1 :                         wps_force_encr_types_in_use = 0;
     413             :                 } else {
     414           1 :                         wps_force_encr_types = strtol(value, NULL, 0);
     415           1 :                         wps_force_encr_types_in_use = 1;
     416             :                 }
     417             : #endif /* CONFIG_WPS_TESTING */
     418        7859 :         } else if (os_strcasecmp(cmd, "ampdu") == 0) {
     419           1 :                 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
     420           1 :                         ret = -1;
     421             : #ifdef CONFIG_TDLS
     422             : #ifdef CONFIG_TDLS_TESTING
     423        7858 :         } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
     424          10 :                 tdls_testing = strtol(value, NULL, 0);
     425          10 :                 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
     426             : #endif /* CONFIG_TDLS_TESTING */
     427        7848 :         } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
     428           2 :                 int disabled = atoi(value);
     429           2 :                 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
     430           2 :                 if (disabled) {
     431           1 :                         if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
     432           0 :                                 ret = -1;
     433           1 :                 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
     434           0 :                         ret = -1;
     435           2 :                 wpa_tdls_enable(wpa_s->wpa, !disabled);
     436             : #endif /* CONFIG_TDLS */
     437        7846 :         } else if (os_strcasecmp(cmd, "pno") == 0) {
     438           5 :                 ret = wpas_ctrl_pno(wpa_s, value);
     439        7841 :         } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
     440           0 :                 int disabled = atoi(value);
     441           0 :                 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
     442           0 :                         ret = -1;
     443           0 :                 else if (disabled)
     444           0 :                         wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
     445        7841 :         } else if (os_strcasecmp(cmd, "uapsd") == 0) {
     446           7 :                 if (os_strcmp(value, "disable") == 0)
     447           1 :                         wpa_s->set_sta_uapsd = 0;
     448             :                 else {
     449             :                         int be, bk, vi, vo;
     450             :                         char *pos;
     451             :                         /* format: BE,BK,VI,VO;max SP Length */
     452           6 :                         be = atoi(value);
     453           6 :                         pos = os_strchr(value, ',');
     454           6 :                         if (pos == NULL)
     455           2 :                                 return -1;
     456           4 :                         pos++;
     457           4 :                         bk = atoi(pos);
     458           4 :                         pos = os_strchr(pos, ',');
     459           4 :                         if (pos == NULL)
     460           1 :                                 return -1;
     461           3 :                         pos++;
     462           3 :                         vi = atoi(pos);
     463           3 :                         pos = os_strchr(pos, ',');
     464           3 :                         if (pos == NULL)
     465           1 :                                 return -1;
     466           2 :                         pos++;
     467           2 :                         vo = atoi(pos);
     468             :                         /* ignore max SP Length for now */
     469             : 
     470           2 :                         wpa_s->set_sta_uapsd = 1;
     471           2 :                         wpa_s->sta_uapsd = 0;
     472           2 :                         if (be)
     473           1 :                                 wpa_s->sta_uapsd |= BIT(0);
     474           2 :                         if (bk)
     475           1 :                                 wpa_s->sta_uapsd |= BIT(1);
     476           2 :                         if (vi)
     477           1 :                                 wpa_s->sta_uapsd |= BIT(2);
     478           2 :                         if (vo)
     479           1 :                                 wpa_s->sta_uapsd |= BIT(3);
     480             :                 }
     481        7834 :         } else if (os_strcasecmp(cmd, "ps") == 0) {
     482           4 :                 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
     483             : #ifdef CONFIG_WIFI_DISPLAY
     484        7830 :         } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
     485          25 :                 int enabled = !!atoi(value);
     486          25 :                 if (enabled && !wpa_s->global->p2p)
     487           0 :                         ret = -1;
     488             :                 else
     489          25 :                         wifi_display_enable(wpa_s->global, enabled);
     490             : #endif /* CONFIG_WIFI_DISPLAY */
     491        7805 :         } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
     492           6 :                 ret = set_bssid_filter(wpa_s, value);
     493        7799 :         } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
     494          18 :                 ret = set_disallow_aps(wpa_s, value);
     495        7781 :         } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
     496           3 :                 wpa_s->no_keep_alive = !!atoi(value);
     497             : #ifdef CONFIG_TESTING_OPTIONS
     498        7778 :         } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
     499          54 :                 wpa_s->ext_mgmt_frame_handling = !!atoi(value);
     500        7724 :         } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
     501         180 :                 wpa_s->ext_eapol_frame_io = !!atoi(value);
     502             : #ifdef CONFIG_AP
     503         180 :                 if (wpa_s->ap_iface) {
     504           4 :                         wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
     505           2 :                                 wpa_s->ext_eapol_frame_io;
     506             :                 }
     507             : #endif /* CONFIG_AP */
     508        7544 :         } else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
     509           2 :                 wpa_s->extra_roc_dur = atoi(value);
     510        7542 :         } else if (os_strcasecmp(cmd, "test_failure") == 0) {
     511           4 :                 wpa_s->test_failure = atoi(value);
     512        7538 :         } else if (os_strcasecmp(cmd, "p2p_go_csa_on_inv") == 0) {
     513           1 :                 wpa_s->p2p_go_csa_on_inv = !!atoi(value);
     514        7537 :         } else if (os_strcasecmp(cmd, "ignore_auth_resp") == 0) {
     515           2 :                 wpa_s->ignore_auth_resp = !!atoi(value);
     516        7535 :         } else if (os_strcasecmp(cmd, "ignore_assoc_disallow") == 0) {
     517           1 :                 wpa_s->ignore_assoc_disallow = !!atoi(value);
     518        7534 :         } else if (os_strcasecmp(cmd, "reject_btm_req_reason") == 0) {
     519           2 :                 wpa_s->reject_btm_req_reason = atoi(value);
     520             : #endif /* CONFIG_TESTING_OPTIONS */
     521             : #ifndef CONFIG_NO_CONFIG_BLOBS
     522        7532 :         } else if (os_strcmp(cmd, "blob") == 0) {
     523          87 :                 ret = wpas_ctrl_set_blob(wpa_s, value);
     524             : #endif /* CONFIG_NO_CONFIG_BLOBS */
     525        7445 :         } else if (os_strcasecmp(cmd, "setband") == 0) {
     526           9 :                 ret = wpas_ctrl_set_band(wpa_s, value);
     527             : #ifdef CONFIG_MBO
     528        7436 :         } else if (os_strcasecmp(cmd, "non_pref_chan") == 0) {
     529          12 :                 ret = wpas_mbo_update_non_pref_chan(wpa_s, value);
     530        7424 :         } else if (os_strcasecmp(cmd, "mbo_cell_capa") == 0) {
     531         265 :                 wpas_mbo_update_cell_capa(wpa_s, atoi(value));
     532             : #endif /* CONFIG_MBO */
     533        7159 :         } else if (os_strcasecmp(cmd, "lci") == 0) {
     534           2 :                 ret = wpas_ctrl_iface_set_lci(wpa_s, value);
     535             :         } else {
     536        7157 :                 value[-1] = '=';
     537        7157 :                 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
     538        7157 :                 if (ret == 0)
     539        7117 :                         wpa_supplicant_update_config(wpa_s);
     540             :         }
     541             : 
     542        7900 :         return ret;
     543             : }
     544             : 
     545             : 
     546         189 : static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
     547             :                                          char *cmd, char *buf, size_t buflen)
     548             : {
     549         189 :         int res = -1;
     550             : 
     551         189 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
     552             : 
     553         189 :         if (os_strcmp(cmd, "version") == 0) {
     554           1 :                 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
     555         188 :         } else if (os_strcasecmp(cmd, "country") == 0) {
     556           2 :                 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
     557           4 :                         res = os_snprintf(buf, buflen, "%c%c",
     558           2 :                                           wpa_s->conf->country[0],
     559           2 :                                           wpa_s->conf->country[1]);
     560             : #ifdef CONFIG_WIFI_DISPLAY
     561         186 :         } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
     562             :                 int enabled;
     563           2 :                 if (wpa_s->global->p2p == NULL ||
     564           1 :                     wpa_s->global->p2p_disabled)
     565           0 :                         enabled = 0;
     566             :                 else
     567           1 :                         enabled = wpa_s->global->wifi_display;
     568           1 :                 res = os_snprintf(buf, buflen, "%d", enabled);
     569             : #endif /* CONFIG_WIFI_DISPLAY */
     570             : #ifdef CONFIG_TESTING_GET_GTK
     571             :         } else if (os_strcmp(cmd, "gtk") == 0) {
     572             :                 if (wpa_s->last_gtk_len == 0)
     573             :                         return -1;
     574             :                 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
     575             :                                        wpa_s->last_gtk_len);
     576             :                 return res;
     577             : #endif /* CONFIG_TESTING_GET_GTK */
     578         185 :         } else if (os_strcmp(cmd, "tls_library") == 0) {
     579          77 :                 res = tls_get_library_version(buf, buflen);
     580             :         } else {
     581         108 :                 res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
     582             :         }
     583             : 
     584         189 :         if (os_snprintf_error(buflen, res))
     585          22 :                 return -1;
     586         167 :         return res;
     587             : }
     588             : 
     589             : 
     590             : #ifdef IEEE8021X_EAPOL
     591          16 : static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
     592             :                                              char *addr)
     593             : {
     594             :         u8 bssid[ETH_ALEN];
     595          16 :         struct wpa_ssid *ssid = wpa_s->current_ssid;
     596             : 
     597          16 :         if (hwaddr_aton(addr, bssid)) {
     598           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
     599             :                            "'%s'", addr);
     600           1 :                 return -1;
     601             :         }
     602             : 
     603          15 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
     604          15 :         rsn_preauth_deinit(wpa_s->wpa);
     605          15 :         if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
     606           6 :                 return -1;
     607             : 
     608           9 :         return 0;
     609             : }
     610             : #endif /* IEEE8021X_EAPOL */
     611             : 
     612             : 
     613             : #ifdef CONFIG_PEERKEY
     614             : /* MLME-STKSTART.request(peer) */
     615           6 : static int wpa_supplicant_ctrl_iface_stkstart(
     616             :         struct wpa_supplicant *wpa_s, char *addr)
     617             : {
     618             :         u8 peer[ETH_ALEN];
     619             : 
     620           6 :         if (hwaddr_aton(addr, peer)) {
     621           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
     622             :                            "address '%s'", addr);
     623           1 :                 return -1;
     624             :         }
     625             : 
     626          30 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
     627          30 :                    MAC2STR(peer));
     628             : 
     629           5 :         return wpa_sm_stkstart(wpa_s->wpa, peer);
     630             : }
     631             : #endif /* CONFIG_PEERKEY */
     632             : 
     633             : 
     634             : #ifdef CONFIG_TDLS
     635             : 
     636           3 : static int wpa_supplicant_ctrl_iface_tdls_discover(
     637             :         struct wpa_supplicant *wpa_s, char *addr)
     638             : {
     639             :         u8 peer[ETH_ALEN];
     640             :         int ret;
     641             : 
     642           3 :         if (hwaddr_aton(addr, peer)) {
     643           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
     644             :                            "address '%s'", addr);
     645           1 :                 return -1;
     646             :         }
     647             : 
     648          12 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
     649          12 :                    MAC2STR(peer));
     650             : 
     651           2 :         if (wpa_tdls_is_external_setup(wpa_s->wpa))
     652           2 :                 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
     653             :         else
     654           0 :                 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
     655             : 
     656           2 :         return ret;
     657             : }
     658             : 
     659             : 
     660          33 : static int wpa_supplicant_ctrl_iface_tdls_setup(
     661             :         struct wpa_supplicant *wpa_s, char *addr)
     662             : {
     663             :         u8 peer[ETH_ALEN];
     664             :         int ret;
     665             : 
     666          33 :         if (hwaddr_aton(addr, peer)) {
     667           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
     668             :                            "address '%s'", addr);
     669           1 :                 return -1;
     670             :         }
     671             : 
     672         192 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
     673         192 :                    MAC2STR(peer));
     674             : 
     675          32 :         if ((wpa_s->conf->tdls_external_control) &&
     676           0 :             wpa_tdls_is_external_setup(wpa_s->wpa))
     677           0 :                 return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
     678             : 
     679          32 :         wpa_tdls_remove(wpa_s->wpa, peer);
     680             : 
     681          32 :         if (wpa_tdls_is_external_setup(wpa_s->wpa))
     682          32 :                 ret = wpa_tdls_start(wpa_s->wpa, peer);
     683             :         else
     684           0 :                 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
     685             : 
     686          32 :         return ret;
     687             : }
     688             : 
     689             : 
     690          14 : static int wpa_supplicant_ctrl_iface_tdls_teardown(
     691             :         struct wpa_supplicant *wpa_s, char *addr)
     692             : {
     693             :         u8 peer[ETH_ALEN];
     694             :         int ret;
     695             : 
     696          14 :         if (os_strcmp(addr, "*") == 0) {
     697             :                 /* remove everyone */
     698           2 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
     699           2 :                 wpa_tdls_teardown_peers(wpa_s->wpa);
     700           2 :                 return 0;
     701             :         }
     702             : 
     703          12 :         if (hwaddr_aton(addr, peer)) {
     704           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
     705             :                            "address '%s'", addr);
     706           1 :                 return -1;
     707             :         }
     708             : 
     709          66 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
     710          66 :                    MAC2STR(peer));
     711             : 
     712          11 :         if ((wpa_s->conf->tdls_external_control) &&
     713           0 :             wpa_tdls_is_external_setup(wpa_s->wpa))
     714           0 :                 return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
     715             : 
     716          11 :         if (wpa_tdls_is_external_setup(wpa_s->wpa))
     717          11 :                 ret = wpa_tdls_teardown_link(
     718             :                         wpa_s->wpa, peer,
     719             :                         WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
     720             :         else
     721           0 :                 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
     722             : 
     723          11 :         return ret;
     724             : }
     725             : 
     726             : 
     727           1 : static int ctrl_iface_get_capability_tdls(
     728             :         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
     729             : {
     730             :         int ret;
     731             : 
     732           2 :         ret = os_snprintf(buf, buflen, "%s\n",
     733           1 :                           wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
     734           1 :                           (wpa_s->drv_flags &
     735             :                            WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
     736           1 :                            "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
     737           1 :         if (os_snprintf_error(buflen, ret))
     738           0 :                 return -1;
     739           1 :         return ret;
     740             : }
     741             : 
     742             : 
     743           8 : static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
     744             :         struct wpa_supplicant *wpa_s, char *cmd)
     745             : {
     746             :         u8 peer[ETH_ALEN];
     747             :         struct hostapd_freq_params freq_params;
     748             :         u8 oper_class;
     749             :         char *pos, *end;
     750             : 
     751           8 :         if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
     752           0 :                 wpa_printf(MSG_INFO,
     753             :                            "tdls_chanswitch: Only supported with external setup");
     754           0 :                 return -1;
     755             :         }
     756             : 
     757           8 :         os_memset(&freq_params, 0, sizeof(freq_params));
     758             : 
     759           8 :         pos = os_strchr(cmd, ' ');
     760           8 :         if (pos == NULL)
     761           1 :                 return -1;
     762           7 :         *pos++ = '\0';
     763             : 
     764           7 :         oper_class = strtol(pos, &end, 10);
     765           7 :         if (pos == end) {
     766           3 :                 wpa_printf(MSG_INFO,
     767             :                            "tdls_chanswitch: Invalid op class provided");
     768           3 :                 return -1;
     769             :         }
     770             : 
     771           4 :         pos = end;
     772           4 :         freq_params.freq = atoi(pos);
     773           4 :         if (freq_params.freq == 0) {
     774           1 :                 wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
     775           1 :                 return -1;
     776             :         }
     777             : 
     778             : #define SET_FREQ_SETTING(str) \
     779             :         do { \
     780             :                 const char *pos2 = os_strstr(pos, " " #str "="); \
     781             :                 if (pos2) { \
     782             :                         pos2 += sizeof(" " #str "=") - 1; \
     783             :                         freq_params.str = atoi(pos2); \
     784             :                 } \
     785             :         } while (0)
     786             : 
     787           3 :         SET_FREQ_SETTING(center_freq1);
     788           3 :         SET_FREQ_SETTING(center_freq2);
     789           3 :         SET_FREQ_SETTING(bandwidth);
     790           3 :         SET_FREQ_SETTING(sec_channel_offset);
     791             : #undef SET_FREQ_SETTING
     792             : 
     793           3 :         freq_params.ht_enabled = !!os_strstr(pos, " ht");
     794           3 :         freq_params.vht_enabled = !!os_strstr(pos, " vht");
     795             : 
     796           3 :         if (hwaddr_aton(cmd, peer)) {
     797           0 :                 wpa_printf(MSG_DEBUG,
     798             :                            "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
     799             :                            cmd);
     800           0 :                 return -1;
     801             :         }
     802             : 
     803          24 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
     804             :                    " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
     805          18 :                    MAC2STR(peer), oper_class, freq_params.freq,
     806             :                    freq_params.center_freq1, freq_params.center_freq2,
     807             :                    freq_params.bandwidth, freq_params.sec_channel_offset,
     808           3 :                    freq_params.ht_enabled ? " HT" : "",
     809           3 :                    freq_params.vht_enabled ? " VHT" : "");
     810             : 
     811           3 :         return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
     812             :                                            &freq_params);
     813             : }
     814             : 
     815             : 
     816           4 : static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
     817             :         struct wpa_supplicant *wpa_s, char *cmd)
     818             : {
     819             :         u8 peer[ETH_ALEN];
     820             : 
     821           4 :         if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
     822           0 :                 wpa_printf(MSG_INFO,
     823             :                            "tdls_chanswitch: Only supported with external setup");
     824           0 :                 return -1;
     825             :         }
     826             : 
     827           4 :         if (hwaddr_aton(cmd, peer)) {
     828           1 :                 wpa_printf(MSG_DEBUG,
     829             :                            "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
     830             :                            cmd);
     831           1 :                 return -1;
     832             :         }
     833             : 
     834          18 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
     835          18 :                    MAC2STR(peer));
     836             : 
     837           3 :         return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
     838             : }
     839             : 
     840             : 
     841           7 : static int wpa_supplicant_ctrl_iface_tdls_link_status(
     842             :         struct wpa_supplicant *wpa_s, const char *addr,
     843             :         char *buf, size_t buflen)
     844             : {
     845             :         u8 peer[ETH_ALEN];
     846             :         const char *tdls_status;
     847             :         int ret;
     848             : 
     849           7 :         if (hwaddr_aton(addr, peer)) {
     850           1 :                 wpa_printf(MSG_DEBUG,
     851             :                            "CTRL_IFACE TDLS_LINK_STATUS: Invalid address '%s'",
     852             :                            addr);
     853           1 :                 return -1;
     854             :         }
     855          36 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS " MACSTR,
     856          36 :                    MAC2STR(peer));
     857             : 
     858           6 :         tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
     859           6 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS: %s", tdls_status);
     860           6 :         ret = os_snprintf(buf, buflen, "TDLS link status: %s\n", tdls_status);
     861           6 :         if (os_snprintf_error(buflen, ret))
     862           0 :                 return -1;
     863             : 
     864           6 :         return ret;
     865             : }
     866             : 
     867             : #endif /* CONFIG_TDLS */
     868             : 
     869             : 
     870          16 : static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
     871             : {
     872          16 :         char *token, *context = NULL;
     873          16 :         struct wmm_ac_ts_setup_params params = {
     874             :                 .tsid = 0xff,
     875             :                 .direction = 0xff,
     876             :         };
     877             : 
     878         128 :         while ((token = str_token(cmd, " ", &context))) {
     879         177 :                 if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
     880         149 :                     sscanf(token, "up=%i", &params.user_priority) == 1 ||
     881          68 :                     sscanf(token, "nominal_msdu_size=%i",
     882          55 :                            &params.nominal_msdu_size) == 1 ||
     883          55 :                     sscanf(token, "mean_data_rate=%i",
     884          42 :                            &params.mean_data_rate) == 1 ||
     885          42 :                     sscanf(token, "min_phy_rate=%i",
     886          29 :                            &params.minimum_phy_rate) == 1 ||
     887          29 :                     sscanf(token, "sba=%i",
     888             :                            &params.surplus_bandwidth_allowance) == 1)
     889          80 :                         continue;
     890             : 
     891          16 :                 if (os_strcasecmp(token, "downlink") == 0) {
     892          13 :                         params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
     893           3 :                 } else if (os_strcasecmp(token, "uplink") == 0) {
     894           1 :                         params.direction = WMM_TSPEC_DIRECTION_UPLINK;
     895           2 :                 } else if (os_strcasecmp(token, "bidi") == 0) {
     896           1 :                         params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
     897           1 :                 } else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
     898           1 :                         params.fixed_nominal_msdu = 1;
     899             :                 } else {
     900           0 :                         wpa_printf(MSG_DEBUG,
     901             :                                    "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
     902             :                                    token);
     903           0 :                         return -1;
     904             :                 }
     905             : 
     906             :         }
     907             : 
     908          16 :         return wpas_wmm_ac_addts(wpa_s, &params);
     909             : }
     910             : 
     911             : 
     912           3 : static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
     913             : {
     914           3 :         u8 tsid = atoi(cmd);
     915             : 
     916           3 :         return wpas_wmm_ac_delts(wpa_s, tsid);
     917             : }
     918             : 
     919             : 
     920             : #ifdef CONFIG_IEEE80211R
     921         118 : static int wpa_supplicant_ctrl_iface_ft_ds(
     922             :         struct wpa_supplicant *wpa_s, char *addr)
     923             : {
     924             :         u8 target_ap[ETH_ALEN];
     925             :         struct wpa_bss *bss;
     926             :         const u8 *mdie;
     927             : 
     928         118 :         if (hwaddr_aton(addr, target_ap)) {
     929           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
     930             :                            "address '%s'", addr);
     931           1 :                 return -1;
     932             :         }
     933             : 
     934         117 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
     935             : 
     936         117 :         bss = wpa_bss_get_bssid(wpa_s, target_ap);
     937         117 :         if (bss)
     938         116 :                 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
     939             :         else
     940           1 :                 mdie = NULL;
     941             : 
     942         117 :         return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
     943             : }
     944             : #endif /* CONFIG_IEEE80211R */
     945             : 
     946             : 
     947             : #ifdef CONFIG_WPS
     948          92 : static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
     949             :                                              char *cmd)
     950             : {
     951          92 :         u8 bssid[ETH_ALEN], *_bssid = bssid;
     952             : #ifdef CONFIG_P2P
     953             :         u8 p2p_dev_addr[ETH_ALEN];
     954             : #endif /* CONFIG_P2P */
     955             : #ifdef CONFIG_AP
     956          92 :         u8 *_p2p_dev_addr = NULL;
     957             : #endif /* CONFIG_AP */
     958             : 
     959          92 :         if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
     960          17 :                 _bssid = NULL;
     961             : #ifdef CONFIG_P2P
     962          75 :         } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
     963           2 :                 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
     964           1 :                         wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
     965             :                                    "P2P Device Address '%s'",
     966             :                                    cmd + 13);
     967           1 :                         return -1;
     968             :                 }
     969           1 :                 _p2p_dev_addr = p2p_dev_addr;
     970             : #endif /* CONFIG_P2P */
     971          73 :         } else if (hwaddr_aton(cmd, bssid)) {
     972           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
     973             :                            cmd);
     974           1 :                 return -1;
     975             :         }
     976             : 
     977             : #ifdef CONFIG_AP
     978          90 :         if (wpa_s->ap_iface)
     979           8 :                 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
     980             : #endif /* CONFIG_AP */
     981             : 
     982          82 :         return wpas_wps_start_pbc(wpa_s, _bssid, 0);
     983             : }
     984             : 
     985             : 
     986         472 : static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
     987             :                                              char *cmd, char *buf,
     988             :                                              size_t buflen)
     989             : {
     990         472 :         u8 bssid[ETH_ALEN], *_bssid = bssid;
     991             :         char *pin;
     992             :         int ret;
     993             : 
     994         472 :         pin = os_strchr(cmd, ' ');
     995         472 :         if (pin)
     996         259 :                 *pin++ = '\0';
     997             : 
     998         472 :         if (os_strcmp(cmd, "any") == 0)
     999          80 :                 _bssid = NULL;
    1000         392 :         else if (os_strcmp(cmd, "get") == 0) {
    1001         210 :                 if (wps_generate_pin((unsigned int *) &ret) < 0)
    1002           0 :                         return -1;
    1003         210 :                 goto done;
    1004         182 :         } else if (hwaddr_aton(cmd, bssid)) {
    1005           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
    1006             :                            cmd);
    1007           1 :                 return -1;
    1008             :         }
    1009             : 
    1010             : #ifdef CONFIG_AP
    1011         261 :         if (wpa_s->ap_iface) {
    1012          73 :                 int timeout = 0;
    1013             :                 char *pos;
    1014             : 
    1015          73 :                 if (pin) {
    1016          73 :                         pos = os_strchr(pin, ' ');
    1017          73 :                         if (pos) {
    1018           1 :                                 *pos++ = '\0';
    1019           1 :                                 timeout = atoi(pos);
    1020             :                         }
    1021             :                 }
    1022             : 
    1023          73 :                 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
    1024             :                                                  buf, buflen, timeout);
    1025             :         }
    1026             : #endif /* CONFIG_AP */
    1027             : 
    1028         188 :         if (pin) {
    1029         186 :                 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
    1030             :                                          DEV_PW_DEFAULT);
    1031         186 :                 if (ret < 0)
    1032           0 :                         return -1;
    1033         186 :                 ret = os_snprintf(buf, buflen, "%s", pin);
    1034         186 :                 if (os_snprintf_error(buflen, ret))
    1035           0 :                         return -1;
    1036         186 :                 return ret;
    1037             :         }
    1038             : 
    1039           2 :         ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
    1040           2 :         if (ret < 0)
    1041           0 :                 return -1;
    1042             : 
    1043             : done:
    1044             :         /* Return the generated PIN */
    1045         212 :         ret = os_snprintf(buf, buflen, "%08d", ret);
    1046         212 :         if (os_snprintf_error(buflen, ret))
    1047           0 :                 return -1;
    1048         212 :         return ret;
    1049             : }
    1050             : 
    1051             : 
    1052          17 : static int wpa_supplicant_ctrl_iface_wps_check_pin(
    1053             :         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
    1054             : {
    1055             :         char pin[9];
    1056             :         size_t len;
    1057             :         char *pos;
    1058             :         int ret;
    1059             : 
    1060          17 :         wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
    1061             :                               (u8 *) cmd, os_strlen(cmd));
    1062         156 :         for (pos = cmd, len = 0; *pos != '\0'; pos++) {
    1063         140 :                 if (*pos < '0' || *pos > '9')
    1064           6 :                         continue;
    1065         134 :                 pin[len++] = *pos;
    1066         134 :                 if (len == 9) {
    1067           1 :                         wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
    1068           1 :                         return -1;
    1069             :                 }
    1070             :         }
    1071          16 :         if (len != 4 && len != 8) {
    1072           1 :                 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
    1073           1 :                 return -1;
    1074             :         }
    1075          15 :         pin[len] = '\0';
    1076             : 
    1077          15 :         if (len == 8) {
    1078             :                 unsigned int pin_val;
    1079          15 :                 pin_val = atoi(pin);
    1080          15 :                 if (!wps_pin_valid(pin_val)) {
    1081           1 :                         wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
    1082           1 :                         ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
    1083           1 :                         if (os_snprintf_error(buflen, ret))
    1084           0 :                                 return -1;
    1085           1 :                         return ret;
    1086             :                 }
    1087             :         }
    1088             : 
    1089          14 :         ret = os_snprintf(buf, buflen, "%s", pin);
    1090          14 :         if (os_snprintf_error(buflen, ret))
    1091           0 :                 return -1;
    1092             : 
    1093          14 :         return ret;
    1094             : }
    1095             : 
    1096             : 
    1097             : #ifdef CONFIG_WPS_NFC
    1098             : 
    1099           4 : static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
    1100             :                                              char *cmd)
    1101             : {
    1102           4 :         u8 bssid[ETH_ALEN], *_bssid = bssid;
    1103             : 
    1104           4 :         if (cmd == NULL || cmd[0] == '\0')
    1105           3 :                 _bssid = NULL;
    1106           1 :         else if (hwaddr_aton(cmd, bssid))
    1107           1 :                 return -1;
    1108             : 
    1109           3 :         return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
    1110             :                                   0, 0);
    1111             : }
    1112             : 
    1113             : 
    1114           3 : static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
    1115             :         struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
    1116             : {
    1117             :         int ndef;
    1118             :         struct wpabuf *buf;
    1119             :         int res;
    1120             :         char *pos;
    1121             : 
    1122           3 :         pos = os_strchr(cmd, ' ');
    1123           3 :         if (pos)
    1124           1 :                 *pos++ = '\0';
    1125           3 :         if (os_strcmp(cmd, "WPS") == 0)
    1126           1 :                 ndef = 0;
    1127           2 :         else if (os_strcmp(cmd, "NDEF") == 0)
    1128           1 :                 ndef = 1;
    1129             :         else
    1130           1 :                 return -1;
    1131             : 
    1132           2 :         buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
    1133           2 :         if (buf == NULL)
    1134           1 :                 return -1;
    1135             : 
    1136           1 :         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
    1137             :                                          wpabuf_len(buf));
    1138           1 :         reply[res++] = '\n';
    1139           1 :         reply[res] = '\0';
    1140             : 
    1141           1 :         wpabuf_free(buf);
    1142             : 
    1143           1 :         return res;
    1144             : }
    1145             : 
    1146             : 
    1147          21 : static int wpa_supplicant_ctrl_iface_wps_nfc_token(
    1148             :         struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
    1149             : {
    1150             :         int ndef;
    1151             :         struct wpabuf *buf;
    1152             :         int res;
    1153             : 
    1154          21 :         if (os_strcmp(cmd, "WPS") == 0)
    1155           1 :                 ndef = 0;
    1156          20 :         else if (os_strcmp(cmd, "NDEF") == 0)
    1157          19 :                 ndef = 1;
    1158             :         else
    1159           1 :                 return -1;
    1160             : 
    1161          20 :         buf = wpas_wps_nfc_token(wpa_s, ndef);
    1162          20 :         if (buf == NULL)
    1163           6 :                 return -1;
    1164             : 
    1165          14 :         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
    1166             :                                          wpabuf_len(buf));
    1167          14 :         reply[res++] = '\n';
    1168          14 :         reply[res] = '\0';
    1169             : 
    1170          14 :         wpabuf_free(buf);
    1171             : 
    1172          14 :         return res;
    1173             : }
    1174             : 
    1175             : 
    1176          50 : static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
    1177             :         struct wpa_supplicant *wpa_s, char *pos)
    1178             : {
    1179             :         size_t len;
    1180             :         struct wpabuf *buf;
    1181             :         int ret;
    1182             :         char *freq;
    1183          50 :         int forced_freq = 0;
    1184             : 
    1185          50 :         freq = strstr(pos, " freq=");
    1186          50 :         if (freq) {
    1187           1 :                 *freq = '\0';
    1188           1 :                 freq += 6;
    1189           1 :                 forced_freq = atoi(freq);
    1190             :         }
    1191             : 
    1192          50 :         len = os_strlen(pos);
    1193          50 :         if (len & 0x01)
    1194           1 :                 return -1;
    1195          49 :         len /= 2;
    1196             : 
    1197          49 :         buf = wpabuf_alloc(len);
    1198          49 :         if (buf == NULL)
    1199           1 :                 return -1;
    1200          48 :         if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
    1201           1 :                 wpabuf_free(buf);
    1202           1 :                 return -1;
    1203             :         }
    1204             : 
    1205          47 :         ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
    1206          47 :         wpabuf_free(buf);
    1207             : 
    1208          47 :         return ret;
    1209             : }
    1210             : 
    1211             : 
    1212          14 : static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
    1213             :                                               char *reply, size_t max_len,
    1214             :                                               int ndef)
    1215             : {
    1216             :         struct wpabuf *buf;
    1217             :         int res;
    1218             : 
    1219          14 :         buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
    1220          14 :         if (buf == NULL)
    1221           2 :                 return -1;
    1222             : 
    1223          12 :         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
    1224             :                                          wpabuf_len(buf));
    1225          12 :         reply[res++] = '\n';
    1226          12 :         reply[res] = '\0';
    1227             : 
    1228          12 :         wpabuf_free(buf);
    1229             : 
    1230          12 :         return res;
    1231             : }
    1232             : 
    1233             : 
    1234             : #ifdef CONFIG_P2P
    1235          16 : static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
    1236             :                                               char *reply, size_t max_len,
    1237             :                                               int ndef)
    1238             : {
    1239             :         struct wpabuf *buf;
    1240             :         int res;
    1241             : 
    1242          16 :         buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
    1243          16 :         if (buf == NULL) {
    1244           1 :                 wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
    1245           1 :                 return -1;
    1246             :         }
    1247             : 
    1248          15 :         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
    1249             :                                          wpabuf_len(buf));
    1250          15 :         reply[res++] = '\n';
    1251          15 :         reply[res] = '\0';
    1252             : 
    1253          15 :         wpabuf_free(buf);
    1254             : 
    1255          15 :         return res;
    1256             : }
    1257             : #endif /* CONFIG_P2P */
    1258             : 
    1259             : 
    1260          36 : static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
    1261             :                                           char *cmd, char *reply,
    1262             :                                           size_t max_len)
    1263             : {
    1264             :         char *pos;
    1265             :         int ndef;
    1266             : 
    1267          36 :         pos = os_strchr(cmd, ' ');
    1268          36 :         if (pos == NULL)
    1269           1 :                 return -1;
    1270          35 :         *pos++ = '\0';
    1271             : 
    1272          35 :         if (os_strcmp(cmd, "WPS") == 0)
    1273           4 :                 ndef = 0;
    1274          31 :         else if (os_strcmp(cmd, "NDEF") == 0)
    1275          30 :                 ndef = 1;
    1276             :         else
    1277           1 :                 return -1;
    1278             : 
    1279          34 :         if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
    1280          16 :                 if (!ndef)
    1281           2 :                         return -1;
    1282          14 :                 return wpas_ctrl_nfc_get_handover_req_wps(
    1283             :                         wpa_s, reply, max_len, ndef);
    1284             :         }
    1285             : 
    1286             : #ifdef CONFIG_P2P
    1287          18 :         if (os_strcmp(pos, "P2P-CR") == 0) {
    1288          16 :                 return wpas_ctrl_nfc_get_handover_req_p2p(
    1289             :                         wpa_s, reply, max_len, ndef);
    1290             :         }
    1291             : #endif /* CONFIG_P2P */
    1292             : 
    1293           2 :         return -1;
    1294             : }
    1295             : 
    1296             : 
    1297           6 : static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
    1298             :                                               char *reply, size_t max_len,
    1299             :                                               int ndef, int cr, char *uuid)
    1300             : {
    1301             :         struct wpabuf *buf;
    1302             :         int res;
    1303             : 
    1304           6 :         buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
    1305           6 :         if (buf == NULL)
    1306           2 :                 return -1;
    1307             : 
    1308           4 :         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
    1309             :                                          wpabuf_len(buf));
    1310           4 :         reply[res++] = '\n';
    1311           4 :         reply[res] = '\0';
    1312             : 
    1313           4 :         wpabuf_free(buf);
    1314             : 
    1315           4 :         return res;
    1316             : }
    1317             : 
    1318             : 
    1319             : #ifdef CONFIG_P2P
    1320          25 : static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
    1321             :                                               char *reply, size_t max_len,
    1322             :                                               int ndef, int tag)
    1323             : {
    1324             :         struct wpabuf *buf;
    1325             :         int res;
    1326             : 
    1327          25 :         buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
    1328          25 :         if (buf == NULL)
    1329           1 :                 return -1;
    1330             : 
    1331          24 :         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
    1332             :                                          wpabuf_len(buf));
    1333          24 :         reply[res++] = '\n';
    1334          24 :         reply[res] = '\0';
    1335             : 
    1336          24 :         wpabuf_free(buf);
    1337             : 
    1338          24 :         return res;
    1339             : }
    1340             : #endif /* CONFIG_P2P */
    1341             : 
    1342             : 
    1343          37 : static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
    1344             :                                           char *cmd, char *reply,
    1345             :                                           size_t max_len)
    1346             : {
    1347             :         char *pos, *pos2;
    1348             :         int ndef;
    1349             : 
    1350          37 :         pos = os_strchr(cmd, ' ');
    1351          37 :         if (pos == NULL)
    1352           1 :                 return -1;
    1353          36 :         *pos++ = '\0';
    1354             : 
    1355          36 :         if (os_strcmp(cmd, "WPS") == 0)
    1356           5 :                 ndef = 0;
    1357          31 :         else if (os_strcmp(cmd, "NDEF") == 0)
    1358          30 :                 ndef = 1;
    1359             :         else
    1360           1 :                 return -1;
    1361             : 
    1362          35 :         pos2 = os_strchr(pos, ' ');
    1363          35 :         if (pos2)
    1364           4 :                 *pos2++ = '\0';
    1365          35 :         if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
    1366           8 :                 if (!ndef)
    1367           2 :                         return -1;
    1368           6 :                 return wpas_ctrl_nfc_get_handover_sel_wps(
    1369             :                         wpa_s, reply, max_len, ndef,
    1370           6 :                         os_strcmp(pos, "WPS-CR") == 0, pos2);
    1371             :         }
    1372             : 
    1373             : #ifdef CONFIG_P2P
    1374          27 :         if (os_strcmp(pos, "P2P-CR") == 0) {
    1375          12 :                 return wpas_ctrl_nfc_get_handover_sel_p2p(
    1376             :                         wpa_s, reply, max_len, ndef, 0);
    1377             :         }
    1378             : 
    1379          15 :         if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
    1380          13 :                 return wpas_ctrl_nfc_get_handover_sel_p2p(
    1381             :                         wpa_s, reply, max_len, ndef, 1);
    1382             :         }
    1383             : #endif /* CONFIG_P2P */
    1384             : 
    1385           2 :         return -1;
    1386             : }
    1387             : 
    1388             : 
    1389          44 : static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
    1390             :                                          char *cmd)
    1391             : {
    1392             :         size_t len;
    1393             :         struct wpabuf *req, *sel;
    1394             :         int ret;
    1395             :         char *pos, *role, *type, *pos2;
    1396             : #ifdef CONFIG_P2P
    1397             :         char *freq;
    1398          44 :         int forced_freq = 0;
    1399             : 
    1400          44 :         freq = strstr(cmd, " freq=");
    1401          44 :         if (freq) {
    1402           1 :                 *freq = '\0';
    1403           1 :                 freq += 6;
    1404           1 :                 forced_freq = atoi(freq);
    1405             :         }
    1406             : #endif /* CONFIG_P2P */
    1407             : 
    1408          44 :         role = cmd;
    1409          44 :         pos = os_strchr(role, ' ');
    1410          44 :         if (pos == NULL) {
    1411           2 :                 wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
    1412           2 :                 return -1;
    1413             :         }
    1414          42 :         *pos++ = '\0';
    1415             : 
    1416          42 :         type = pos;
    1417          42 :         pos = os_strchr(type, ' ');
    1418          42 :         if (pos == NULL) {
    1419           1 :                 wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
    1420           1 :                 return -1;
    1421             :         }
    1422          41 :         *pos++ = '\0';
    1423             : 
    1424          41 :         pos2 = os_strchr(pos, ' ');
    1425          41 :         if (pos2 == NULL) {
    1426           1 :                 wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
    1427           1 :                 return -1;
    1428             :         }
    1429          40 :         *pos2++ = '\0';
    1430             : 
    1431          40 :         len = os_strlen(pos);
    1432          40 :         if (len & 0x01) {
    1433           1 :                 wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
    1434           1 :                 return -1;
    1435             :         }
    1436          39 :         len /= 2;
    1437             : 
    1438          39 :         req = wpabuf_alloc(len);
    1439          39 :         if (req == NULL) {
    1440           1 :                 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
    1441           1 :                 return -1;
    1442             :         }
    1443          38 :         if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
    1444           1 :                 wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
    1445           1 :                 wpabuf_free(req);
    1446           1 :                 return -1;
    1447             :         }
    1448             : 
    1449          37 :         len = os_strlen(pos2);
    1450          37 :         if (len & 0x01) {
    1451           1 :                 wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
    1452           1 :                 wpabuf_free(req);
    1453           1 :                 return -1;
    1454             :         }
    1455          36 :         len /= 2;
    1456             : 
    1457          36 :         sel = wpabuf_alloc(len);
    1458          36 :         if (sel == NULL) {
    1459           1 :                 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
    1460           1 :                 wpabuf_free(req);
    1461           1 :                 return -1;
    1462             :         }
    1463          35 :         if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
    1464           1 :                 wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
    1465           1 :                 wpabuf_free(req);
    1466           1 :                 wpabuf_free(sel);
    1467           1 :                 return -1;
    1468             :         }
    1469             : 
    1470          68 :         wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
    1471          68 :                    role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
    1472             : 
    1473          34 :         if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
    1474          11 :                 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
    1475             : #ifdef CONFIG_AP
    1476          23 :         } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
    1477             :         {
    1478           4 :                 ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
    1479           8 :                 if (ret < 0)
    1480           3 :                         ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
    1481             : #endif /* CONFIG_AP */
    1482             : #ifdef CONFIG_P2P
    1483          19 :         } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
    1484             :         {
    1485           9 :                 ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
    1486          10 :         } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
    1487             :         {
    1488           9 :                 ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
    1489             :                                                    forced_freq);
    1490             : #endif /* CONFIG_P2P */
    1491             :         } else {
    1492           1 :                 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
    1493             :                            "reported: role=%s type=%s", role, type);
    1494           1 :                 ret = -1;
    1495             :         }
    1496          34 :         wpabuf_free(req);
    1497          34 :         wpabuf_free(sel);
    1498             : 
    1499          34 :         if (ret)
    1500           1 :                 wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
    1501             : 
    1502          34 :         return ret;
    1503             : }
    1504             : 
    1505             : #endif /* CONFIG_WPS_NFC */
    1506             : 
    1507             : 
    1508          70 : static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
    1509             :                                              char *cmd)
    1510             : {
    1511             :         u8 bssid[ETH_ALEN];
    1512             :         char *pin;
    1513             :         char *new_ssid;
    1514             :         char *new_auth;
    1515             :         char *new_encr;
    1516             :         char *new_key;
    1517             :         struct wps_new_ap_settings ap;
    1518             : 
    1519          70 :         pin = os_strchr(cmd, ' ');
    1520          70 :         if (pin == NULL)
    1521           1 :                 return -1;
    1522          69 :         *pin++ = '\0';
    1523             : 
    1524          69 :         if (hwaddr_aton(cmd, bssid)) {
    1525           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
    1526             :                            cmd);
    1527           1 :                 return -1;
    1528             :         }
    1529             : 
    1530          68 :         new_ssid = os_strchr(pin, ' ');
    1531          68 :         if (new_ssid == NULL)
    1532          42 :                 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
    1533          26 :         *new_ssid++ = '\0';
    1534             : 
    1535          26 :         new_auth = os_strchr(new_ssid, ' ');
    1536          26 :         if (new_auth == NULL)
    1537           1 :                 return -1;
    1538          25 :         *new_auth++ = '\0';
    1539             : 
    1540          25 :         new_encr = os_strchr(new_auth, ' ');
    1541          25 :         if (new_encr == NULL)
    1542           1 :                 return -1;
    1543          24 :         *new_encr++ = '\0';
    1544             : 
    1545          24 :         new_key = os_strchr(new_encr, ' ');
    1546          24 :         if (new_key == NULL)
    1547           1 :                 return -1;
    1548          23 :         *new_key++ = '\0';
    1549             : 
    1550          23 :         os_memset(&ap, 0, sizeof(ap));
    1551          23 :         ap.ssid_hex = new_ssid;
    1552          23 :         ap.auth = new_auth;
    1553          23 :         ap.encr = new_encr;
    1554          23 :         ap.key_hex = new_key;
    1555          23 :         return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
    1556             : }
    1557             : 
    1558             : 
    1559             : #ifdef CONFIG_AP
    1560          16 : static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
    1561             :                                                 char *cmd, char *buf,
    1562             :                                                 size_t buflen)
    1563             : {
    1564          16 :         int timeout = 300;
    1565             :         char *pos;
    1566             :         const char *pin_txt;
    1567             : 
    1568          16 :         if (!wpa_s->ap_iface)
    1569           1 :                 return -1;
    1570             : 
    1571          15 :         pos = os_strchr(cmd, ' ');
    1572          15 :         if (pos)
    1573           3 :                 *pos++ = '\0';
    1574             : 
    1575          15 :         if (os_strcmp(cmd, "disable") == 0) {
    1576           1 :                 wpas_wps_ap_pin_disable(wpa_s);
    1577           1 :                 return os_snprintf(buf, buflen, "OK\n");
    1578             :         }
    1579             : 
    1580          14 :         if (os_strcmp(cmd, "random") == 0) {
    1581           2 :                 if (pos)
    1582           1 :                         timeout = atoi(pos);
    1583           2 :                 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
    1584           2 :                 if (pin_txt == NULL)
    1585           0 :                         return -1;
    1586           2 :                 return os_snprintf(buf, buflen, "%s", pin_txt);
    1587             :         }
    1588             : 
    1589          12 :         if (os_strcmp(cmd, "get") == 0) {
    1590           8 :                 pin_txt = wpas_wps_ap_pin_get(wpa_s);
    1591           8 :                 if (pin_txt == NULL)
    1592           2 :                         return -1;
    1593           6 :                 return os_snprintf(buf, buflen, "%s", pin_txt);
    1594             :         }
    1595             : 
    1596           4 :         if (os_strcmp(cmd, "set") == 0) {
    1597             :                 char *pin;
    1598           3 :                 if (pos == NULL)
    1599           1 :                         return -1;
    1600           2 :                 pin = pos;
    1601           2 :                 pos = os_strchr(pos, ' ');
    1602           2 :                 if (pos) {
    1603           1 :                         *pos++ = '\0';
    1604           1 :                         timeout = atoi(pos);
    1605             :                 }
    1606           2 :                 if (os_strlen(pin) > buflen)
    1607           0 :                         return -1;
    1608           2 :                 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
    1609           0 :                         return -1;
    1610           2 :                 return os_snprintf(buf, buflen, "%s", pin);
    1611             :         }
    1612             : 
    1613           1 :         return -1;
    1614             : }
    1615             : #endif /* CONFIG_AP */
    1616             : 
    1617             : 
    1618             : #ifdef CONFIG_WPS_ER
    1619           9 : static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
    1620             :                                                 char *cmd)
    1621             : {
    1622           9 :         char *uuid = cmd, *pin, *pos;
    1623           9 :         u8 addr_buf[ETH_ALEN], *addr = NULL;
    1624           9 :         pin = os_strchr(uuid, ' ');
    1625           9 :         if (pin == NULL)
    1626           1 :                 return -1;
    1627           8 :         *pin++ = '\0';
    1628           8 :         pos = os_strchr(pin, ' ');
    1629           8 :         if (pos) {
    1630           7 :                 *pos++ = '\0';
    1631           7 :                 if (hwaddr_aton(pos, addr_buf) == 0)
    1632           7 :                         addr = addr_buf;
    1633             :         }
    1634           8 :         return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
    1635             : }
    1636             : 
    1637             : 
    1638          16 : static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
    1639             :                                                   char *cmd)
    1640             : {
    1641          16 :         char *uuid = cmd, *pin;
    1642          16 :         pin = os_strchr(uuid, ' ');
    1643          16 :         if (pin == NULL)
    1644           1 :                 return -1;
    1645          15 :         *pin++ = '\0';
    1646          15 :         return wpas_wps_er_learn(wpa_s, uuid, pin);
    1647             : }
    1648             : 
    1649             : 
    1650           9 : static int wpa_supplicant_ctrl_iface_wps_er_set_config(
    1651             :         struct wpa_supplicant *wpa_s, char *cmd)
    1652             : {
    1653           9 :         char *uuid = cmd, *id;
    1654           9 :         id = os_strchr(uuid, ' ');
    1655           9 :         if (id == NULL)
    1656           1 :                 return -1;
    1657           8 :         *id++ = '\0';
    1658           8 :         return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
    1659             : }
    1660             : 
    1661             : 
    1662           6 : static int wpa_supplicant_ctrl_iface_wps_er_config(
    1663             :         struct wpa_supplicant *wpa_s, char *cmd)
    1664             : {
    1665             :         char *pin;
    1666             :         char *new_ssid;
    1667             :         char *new_auth;
    1668             :         char *new_encr;
    1669             :         char *new_key;
    1670             :         struct wps_new_ap_settings ap;
    1671             : 
    1672           6 :         pin = os_strchr(cmd, ' ');
    1673           6 :         if (pin == NULL)
    1674           1 :                 return -1;
    1675           5 :         *pin++ = '\0';
    1676             : 
    1677           5 :         new_ssid = os_strchr(pin, ' ');
    1678           5 :         if (new_ssid == NULL)
    1679           1 :                 return -1;
    1680           4 :         *new_ssid++ = '\0';
    1681             : 
    1682           4 :         new_auth = os_strchr(new_ssid, ' ');
    1683           4 :         if (new_auth == NULL)
    1684           1 :                 return -1;
    1685           3 :         *new_auth++ = '\0';
    1686             : 
    1687           3 :         new_encr = os_strchr(new_auth, ' ');
    1688           3 :         if (new_encr == NULL)
    1689           1 :                 return -1;
    1690           2 :         *new_encr++ = '\0';
    1691             : 
    1692           2 :         new_key = os_strchr(new_encr, ' ');
    1693           2 :         if (new_key == NULL)
    1694           1 :                 return -1;
    1695           1 :         *new_key++ = '\0';
    1696             : 
    1697           1 :         os_memset(&ap, 0, sizeof(ap));
    1698           1 :         ap.ssid_hex = new_ssid;
    1699           1 :         ap.auth = new_auth;
    1700           1 :         ap.encr = new_encr;
    1701           1 :         ap.key_hex = new_key;
    1702           1 :         return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
    1703             : }
    1704             : 
    1705             : 
    1706             : #ifdef CONFIG_WPS_NFC
    1707           5 : static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
    1708             :         struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
    1709             : {
    1710             :         int ndef;
    1711             :         struct wpabuf *buf;
    1712             :         int res;
    1713             :         char *uuid;
    1714             : 
    1715           5 :         uuid = os_strchr(cmd, ' ');
    1716           5 :         if (uuid == NULL)
    1717           1 :                 return -1;
    1718           4 :         *uuid++ = '\0';
    1719             : 
    1720           4 :         if (os_strcmp(cmd, "WPS") == 0)
    1721           1 :                 ndef = 0;
    1722           3 :         else if (os_strcmp(cmd, "NDEF") == 0)
    1723           2 :                 ndef = 1;
    1724             :         else
    1725           1 :                 return -1;
    1726             : 
    1727           3 :         buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
    1728           3 :         if (buf == NULL)
    1729           1 :                 return -1;
    1730             : 
    1731           2 :         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
    1732             :                                          wpabuf_len(buf));
    1733           2 :         reply[res++] = '\n';
    1734           2 :         reply[res] = '\0';
    1735             : 
    1736           2 :         wpabuf_free(buf);
    1737             : 
    1738           2 :         return res;
    1739             : }
    1740             : #endif /* CONFIG_WPS_NFC */
    1741             : #endif /* CONFIG_WPS_ER */
    1742             : 
    1743             : #endif /* CONFIG_WPS */
    1744             : 
    1745             : 
    1746             : #ifdef CONFIG_IBSS_RSN
    1747           4 : static int wpa_supplicant_ctrl_iface_ibss_rsn(
    1748             :         struct wpa_supplicant *wpa_s, char *addr)
    1749             : {
    1750             :         u8 peer[ETH_ALEN];
    1751             : 
    1752           4 :         if (hwaddr_aton(addr, peer)) {
    1753           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
    1754             :                            "address '%s'", addr);
    1755           1 :                 return -1;
    1756             :         }
    1757             : 
    1758          18 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
    1759          18 :                    MAC2STR(peer));
    1760             : 
    1761           3 :         return ibss_rsn_start(wpa_s->ibss_rsn, peer);
    1762             : }
    1763             : #endif /* CONFIG_IBSS_RSN */
    1764             : 
    1765             : 
    1766          87 : static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
    1767             :                                               char *rsp)
    1768             : {
    1769             : #ifdef IEEE8021X_EAPOL
    1770             :         char *pos, *id_pos;
    1771             :         int id;
    1772             :         struct wpa_ssid *ssid;
    1773             : 
    1774          87 :         pos = os_strchr(rsp, '-');
    1775          87 :         if (pos == NULL)
    1776           1 :                 return -1;
    1777          86 :         *pos++ = '\0';
    1778          86 :         id_pos = pos;
    1779          86 :         pos = os_strchr(pos, ':');
    1780          86 :         if (pos == NULL)
    1781           2 :                 return -1;
    1782          84 :         *pos++ = '\0';
    1783          84 :         id = atoi(id_pos);
    1784          84 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
    1785          84 :         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
    1786             :                               (u8 *) pos, os_strlen(pos));
    1787             : 
    1788          84 :         ssid = wpa_config_get_network(wpa_s->conf, id);
    1789          84 :         if (ssid == NULL) {
    1790           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
    1791             :                            "to update", id);
    1792           1 :                 return -1;
    1793             :         }
    1794             : 
    1795          83 :         return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
    1796             :                                                          pos);
    1797             : #else /* IEEE8021X_EAPOL */
    1798             :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
    1799             :         return -1;
    1800             : #endif /* IEEE8021X_EAPOL */
    1801             : }
    1802             : 
    1803             : 
    1804       12696 : static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
    1805             :                                             const char *params,
    1806             :                                             char *buf, size_t buflen)
    1807             : {
    1808             :         char *pos, *end, tmp[30];
    1809             :         int res, verbose, wps, ret;
    1810             : #ifdef CONFIG_HS20
    1811             :         const u8 *hs20;
    1812             : #endif /* CONFIG_HS20 */
    1813             :         const u8 *sess_id;
    1814             :         size_t sess_id_len;
    1815             : 
    1816       12696 :         if (os_strcmp(params, "-DRIVER") == 0)
    1817        8126 :                 return wpa_drv_status(wpa_s, buf, buflen);
    1818        4570 :         verbose = os_strcmp(params, "-VERBOSE") == 0;
    1819        4570 :         wps = os_strcmp(params, "-WPS") == 0;
    1820        4570 :         pos = buf;
    1821        4570 :         end = buf + buflen;
    1822        4570 :         if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
    1823        3529 :                 struct wpa_ssid *ssid = wpa_s->current_ssid;
    1824       21174 :                 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
    1825       21174 :                                   MAC2STR(wpa_s->bssid));
    1826        3529 :                 if (os_snprintf_error(end - pos, ret))
    1827           0 :                         return pos - buf;
    1828        3529 :                 pos += ret;
    1829        3529 :                 ret = os_snprintf(pos, end - pos, "freq=%u\n",
    1830             :                                   wpa_s->assoc_freq);
    1831        3529 :                 if (os_snprintf_error(end - pos, ret))
    1832           0 :                         return pos - buf;
    1833        3529 :                 pos += ret;
    1834        3529 :                 if (ssid) {
    1835        3529 :                         u8 *_ssid = ssid->ssid;
    1836        3529 :                         size_t ssid_len = ssid->ssid_len;
    1837             :                         u8 ssid_buf[SSID_MAX_LEN];
    1838        3529 :                         if (ssid_len == 0) {
    1839          94 :                                 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
    1840          94 :                                 if (_res < 0)
    1841           0 :                                         ssid_len = 0;
    1842             :                                 else
    1843          94 :                                         ssid_len = _res;
    1844          94 :                                 _ssid = ssid_buf;
    1845             :                         }
    1846        3529 :                         ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
    1847             :                                           wpa_ssid_txt(_ssid, ssid_len),
    1848             :                                           ssid->id);
    1849        3529 :                         if (os_snprintf_error(end - pos, ret))
    1850           0 :                                 return pos - buf;
    1851        3529 :                         pos += ret;
    1852             : 
    1853        3531 :                         if (wps && ssid->passphrase &&
    1854           4 :                             wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
    1855           4 :                             (ssid->mode == WPAS_MODE_AP ||
    1856           2 :                              ssid->mode == WPAS_MODE_P2P_GO)) {
    1857           2 :                                 ret = os_snprintf(pos, end - pos,
    1858             :                                                   "passphrase=%s\n",
    1859             :                                                   ssid->passphrase);
    1860           2 :                                 if (os_snprintf_error(end - pos, ret))
    1861           0 :                                         return pos - buf;
    1862           2 :                                 pos += ret;
    1863             :                         }
    1864        3529 :                         if (ssid->id_str) {
    1865           1 :                                 ret = os_snprintf(pos, end - pos,
    1866             :                                                   "id_str=%s\n",
    1867             :                                                   ssid->id_str);
    1868           1 :                                 if (os_snprintf_error(end - pos, ret))
    1869           0 :                                         return pos - buf;
    1870           1 :                                 pos += ret;
    1871             :                         }
    1872             : 
    1873        3529 :                         switch (ssid->mode) {
    1874             :                         case WPAS_MODE_INFRA:
    1875        3105 :                                 ret = os_snprintf(pos, end - pos,
    1876             :                                                   "mode=station\n");
    1877        3105 :                                 break;
    1878             :                         case WPAS_MODE_IBSS:
    1879          50 :                                 ret = os_snprintf(pos, end - pos,
    1880             :                                                   "mode=IBSS\n");
    1881          50 :                                 break;
    1882             :                         case WPAS_MODE_AP:
    1883          13 :                                 ret = os_snprintf(pos, end - pos,
    1884             :                                                   "mode=AP\n");
    1885          13 :                                 break;
    1886             :                         case WPAS_MODE_P2P_GO:
    1887         295 :                                 ret = os_snprintf(pos, end - pos,
    1888             :                                                   "mode=P2P GO\n");
    1889         295 :                                 break;
    1890             :                         case WPAS_MODE_P2P_GROUP_FORMATION:
    1891           2 :                                 ret = os_snprintf(pos, end - pos,
    1892             :                                                   "mode=P2P GO - group "
    1893             :                                                   "formation\n");
    1894           2 :                                 break;
    1895             :                         case WPAS_MODE_MESH:
    1896          64 :                                 ret = os_snprintf(pos, end - pos,
    1897             :                                                   "mode=mesh\n");
    1898          64 :                                 break;
    1899             :                         default:
    1900           0 :                                 ret = 0;
    1901           0 :                                 break;
    1902             :                         }
    1903        3529 :                         if (os_snprintf_error(end - pos, ret))
    1904           0 :                                 return pos - buf;
    1905        3529 :                         pos += ret;
    1906             :                 }
    1907             : 
    1908             : #ifdef CONFIG_AP
    1909        3529 :                 if (wpa_s->ap_iface) {
    1910         310 :                         pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
    1911         310 :                                                             end - pos,
    1912             :                                                             verbose);
    1913             :                 } else
    1914             : #endif /* CONFIG_AP */
    1915        3219 :                 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
    1916             :         }
    1917             : #ifdef CONFIG_SAE
    1918        8099 :         if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
    1919             : #ifdef CONFIG_AP
    1920        6748 :             !wpa_s->ap_iface &&
    1921             : #endif /* CONFIG_AP */
    1922        3219 :             wpa_s->sme.sae.state == SAE_ACCEPTED) {
    1923          34 :                 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
    1924             :                                   wpa_s->sme.sae.group);
    1925          34 :                 if (os_snprintf_error(end - pos, ret))
    1926           0 :                         return pos - buf;
    1927          34 :                 pos += ret;
    1928             :         }
    1929             : #endif /* CONFIG_SAE */
    1930        4570 :         ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
    1931             :                           wpa_supplicant_state_txt(wpa_s->wpa_state));
    1932        4570 :         if (os_snprintf_error(end - pos, ret))
    1933           0 :                 return pos - buf;
    1934        4570 :         pos += ret;
    1935             : 
    1936        9140 :         if (wpa_s->l2 &&
    1937        4570 :             l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
    1938           2 :                 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
    1939           2 :                 if (os_snprintf_error(end - pos, ret))
    1940           0 :                         return pos - buf;
    1941           2 :                 pos += ret;
    1942             :         }
    1943             : 
    1944             : #ifdef CONFIG_P2P
    1945        4570 :         if (wpa_s->global->p2p) {
    1946       27360 :                 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
    1947       27360 :                                   "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
    1948        4560 :                 if (os_snprintf_error(end - pos, ret))
    1949           0 :                         return pos - buf;
    1950        4560 :                 pos += ret;
    1951             :         }
    1952             : #endif /* CONFIG_P2P */
    1953             : 
    1954       27420 :         ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
    1955       27420 :                           MAC2STR(wpa_s->own_addr));
    1956        4570 :         if (os_snprintf_error(end - pos, ret))
    1957           0 :                 return pos - buf;
    1958        4570 :         pos += ret;
    1959             : 
    1960             : #ifdef CONFIG_HS20
    1961        7751 :         if (wpa_s->current_bss &&
    1962        3181 :             (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
    1963          51 :                                           HS20_IE_VENDOR_TYPE)) &&
    1964         102 :             wpa_s->wpa_proto == WPA_PROTO_RSN &&
    1965          51 :             wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
    1966          51 :                 int release = 1;
    1967          51 :                 if (hs20[1] >= 5) {
    1968          51 :                         u8 rel_num = (hs20[6] & 0xf0) >> 4;
    1969          51 :                         release = rel_num + 1;
    1970             :                 }
    1971          51 :                 ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
    1972          51 :                 if (os_snprintf_error(end - pos, ret))
    1973           0 :                         return pos - buf;
    1974          51 :                 pos += ret;
    1975             :         }
    1976             : 
    1977        4570 :         if (wpa_s->current_ssid) {
    1978             :                 struct wpa_cred *cred;
    1979             :                 char *type;
    1980             : 
    1981        7306 :                 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
    1982             :                         size_t i;
    1983             : 
    1984          52 :                         if (wpa_s->current_ssid->parent_cred != cred)
    1985           3 :                                 continue;
    1986             : 
    1987          49 :                         if (cred->provisioning_sp) {
    1988           6 :                                 ret = os_snprintf(pos, end - pos,
    1989             :                                                   "provisioning_sp=%s\n",
    1990             :                                                   cred->provisioning_sp);
    1991           6 :                                 if (os_snprintf_error(end - pos, ret))
    1992           0 :                                         return pos - buf;
    1993           6 :                                 pos += ret;
    1994             :                         }
    1995             : 
    1996          49 :                         if (!cred->domain)
    1997          22 :                                 goto no_domain;
    1998             : 
    1999          27 :                         i = 0;
    2000          27 :                         if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
    2001          27 :                                 struct wpabuf *names =
    2002          27 :                                         wpa_s->current_bss->anqp->domain_name;
    2003          29 :                                 for (i = 0; names && i < cred->num_domain; i++)
    2004             :                                 {
    2005          23 :                                         if (domain_name_list_contains(
    2006          23 :                                                     names, cred->domain[i], 1))
    2007          21 :                                                 break;
    2008             :                                 }
    2009          27 :                                 if (i == cred->num_domain)
    2010           2 :                                         i = 0; /* show first entry by default */
    2011             :                         }
    2012          27 :                         ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
    2013          27 :                                           cred->domain[i]);
    2014          27 :                         if (os_snprintf_error(end - pos, ret))
    2015           0 :                                 return pos - buf;
    2016          27 :                         pos += ret;
    2017             : 
    2018             :                 no_domain:
    2019          98 :                         if (wpa_s->current_bss == NULL ||
    2020          49 :                             wpa_s->current_bss->anqp == NULL)
    2021           0 :                                 res = -1;
    2022             :                         else
    2023          49 :                                 res = interworking_home_sp_cred(
    2024             :                                         wpa_s, cred,
    2025          49 :                                         wpa_s->current_bss->anqp->domain_name);
    2026          49 :                         if (res > 0)
    2027          25 :                                 type = "home";
    2028          24 :                         else if (res == 0)
    2029           4 :                                 type = "roaming";
    2030             :                         else
    2031          20 :                                 type = "unknown";
    2032             : 
    2033          49 :                         ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
    2034          49 :                         if (os_snprintf_error(end - pos, ret))
    2035           0 :                                 return pos - buf;
    2036          49 :                         pos += ret;
    2037             : 
    2038          49 :                         break;
    2039             :                 }
    2040             :         }
    2041             : #endif /* CONFIG_HS20 */
    2042             : 
    2043        8233 :         if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
    2044        3663 :             wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
    2045         948 :                 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
    2046             :                                           verbose);
    2047         948 :                 if (res >= 0)
    2048         948 :                         pos += res;
    2049             :         }
    2050             : 
    2051        4570 :         sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
    2052        4570 :         if (sess_id) {
    2053         426 :                 char *start = pos;
    2054             : 
    2055         426 :                 ret = os_snprintf(pos, end - pos, "eap_session_id=");
    2056         426 :                 if (os_snprintf_error(end - pos, ret))
    2057           0 :                         return start - buf;
    2058         426 :                 pos += ret;
    2059         426 :                 ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
    2060         426 :                 if (ret <= 0)
    2061           0 :                         return start - buf;
    2062         426 :                 pos += ret;
    2063         426 :                 ret = os_snprintf(pos, end - pos, "\n");
    2064         426 :                 if (os_snprintf_error(end - pos, ret))
    2065           0 :                         return start - buf;
    2066         426 :                 pos += ret;
    2067             :         }
    2068             : 
    2069        4570 :         res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
    2070        4570 :         if (res >= 0)
    2071        4570 :                 pos += res;
    2072             : 
    2073             : #ifdef CONFIG_WPS
    2074             :         {
    2075             :                 char uuid_str[100];
    2076        4570 :                 uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
    2077        4570 :                 ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
    2078        4570 :                 if (os_snprintf_error(end - pos, ret))
    2079           0 :                         return pos - buf;
    2080        4570 :                 pos += ret;
    2081             :         }
    2082             : #endif /* CONFIG_WPS */
    2083             : 
    2084             : #ifdef ANDROID
    2085             :         /*
    2086             :          * Allow using the STATUS command with default behavior, say for debug,
    2087             :          * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
    2088             :          * events with STATUS-NO_EVENTS.
    2089             :          */
    2090             :         if (os_strcmp(params, "-NO_EVENTS")) {
    2091             :                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
    2092             :                              "id=%d state=%d BSSID=" MACSTR " SSID=%s",
    2093             :                              wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
    2094             :                              wpa_s->wpa_state,
    2095             :                              MAC2STR(wpa_s->bssid),
    2096             :                              wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
    2097             :                              wpa_ssid_txt(wpa_s->current_ssid->ssid,
    2098             :                                           wpa_s->current_ssid->ssid_len) : "");
    2099             :                 if (wpa_s->wpa_state == WPA_COMPLETED) {
    2100             :                         struct wpa_ssid *ssid = wpa_s->current_ssid;
    2101             :                         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
    2102             :                                      "- connection to " MACSTR
    2103             :                                      " completed %s [id=%d id_str=%s]",
    2104             :                                      MAC2STR(wpa_s->bssid), "(auth)",
    2105             :                                      ssid ? ssid->id : -1,
    2106             :                                      ssid && ssid->id_str ? ssid->id_str : "");
    2107             :                 }
    2108             :         }
    2109             : #endif /* ANDROID */
    2110             : 
    2111        4570 :         return pos - buf;
    2112             : }
    2113             : 
    2114             : 
    2115           5 : static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
    2116             :                                            char *cmd)
    2117             : {
    2118             :         char *pos;
    2119             :         int id;
    2120             :         struct wpa_ssid *ssid;
    2121             :         u8 bssid[ETH_ALEN];
    2122             : 
    2123             :         /* cmd: "<network id> <BSSID>" */
    2124           5 :         pos = os_strchr(cmd, ' ');
    2125           5 :         if (pos == NULL)
    2126           1 :                 return -1;
    2127           4 :         *pos++ = '\0';
    2128           4 :         id = atoi(cmd);
    2129           4 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
    2130           4 :         if (hwaddr_aton(pos, bssid)) {
    2131           1 :                 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
    2132           1 :                 return -1;
    2133             :         }
    2134             : 
    2135           3 :         ssid = wpa_config_get_network(wpa_s->conf, id);
    2136           3 :         if (ssid == NULL) {
    2137           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
    2138             :                            "to update", id);
    2139           1 :                 return -1;
    2140             :         }
    2141             : 
    2142           2 :         os_memcpy(ssid->bssid, bssid, ETH_ALEN);
    2143           2 :         ssid->bssid_set = !is_zero_ether_addr(bssid);
    2144             : 
    2145           2 :         return 0;
    2146             : }
    2147             : 
    2148             : 
    2149          17 : static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
    2150             :                                                char *cmd, char *buf,
    2151             :                                                size_t buflen)
    2152             : {
    2153             :         u8 bssid[ETH_ALEN];
    2154             :         struct wpa_blacklist *e;
    2155             :         char *pos, *end;
    2156             :         int ret;
    2157             : 
    2158             :         /* cmd: "BLACKLIST [<BSSID>]" */
    2159          17 :         if (*cmd == '\0') {
    2160           9 :                 pos = buf;
    2161           9 :                 end = buf + buflen;
    2162           9 :                 e = wpa_s->blacklist;
    2163          26 :                 while (e) {
    2164          48 :                         ret = os_snprintf(pos, end - pos, MACSTR "\n",
    2165          48 :                                           MAC2STR(e->bssid));
    2166           8 :                         if (os_snprintf_error(end - pos, ret))
    2167           0 :                                 return pos - buf;
    2168           8 :                         pos += ret;
    2169           8 :                         e = e->next;
    2170             :                 }
    2171           9 :                 return pos - buf;
    2172             :         }
    2173             : 
    2174           8 :         cmd++;
    2175           8 :         if (os_strncmp(cmd, "clear", 5) == 0) {
    2176           2 :                 wpa_blacklist_clear(wpa_s);
    2177           2 :                 os_memcpy(buf, "OK\n", 3);
    2178           2 :                 return 3;
    2179             :         }
    2180             : 
    2181           6 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
    2182           6 :         if (hwaddr_aton(cmd, bssid)) {
    2183           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
    2184           1 :                 return -1;
    2185             :         }
    2186             : 
    2187             :         /*
    2188             :          * Add the BSSID twice, so its count will be 2, causing it to be
    2189             :          * skipped when processing scan results.
    2190             :          */
    2191           5 :         ret = wpa_blacklist_add(wpa_s, bssid);
    2192           5 :         if (ret < 0)
    2193           1 :                 return -1;
    2194           4 :         ret = wpa_blacklist_add(wpa_s, bssid);
    2195           4 :         if (ret < 0)
    2196           0 :                 return -1;
    2197           4 :         os_memcpy(buf, "OK\n", 3);
    2198           4 :         return 3;
    2199             : }
    2200             : 
    2201             : 
    2202          21 : static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
    2203             :                                                char *cmd, char *buf,
    2204             :                                                size_t buflen)
    2205             : {
    2206             :         char *pos, *end, *stamp;
    2207             :         int ret;
    2208             : 
    2209             :         /* cmd: "LOG_LEVEL [<level>]" */
    2210          21 :         if (*cmd == '\0') {
    2211          10 :                 pos = buf;
    2212          10 :                 end = buf + buflen;
    2213          10 :                 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
    2214             :                                   "Timestamp: %d\n",
    2215             :                                   debug_level_str(wpa_debug_level),
    2216             :                                   wpa_debug_timestamp);
    2217          10 :                 if (os_snprintf_error(end - pos, ret))
    2218           0 :                         ret = 0;
    2219             : 
    2220          10 :                 return ret;
    2221             :         }
    2222             : 
    2223          36 :         while (*cmd == ' ')
    2224          14 :                 cmd++;
    2225             : 
    2226          11 :         stamp = os_strchr(cmd, ' ');
    2227          11 :         if (stamp) {
    2228           3 :                 *stamp++ = '\0';
    2229           9 :                 while (*stamp == ' ') {
    2230           3 :                         stamp++;
    2231             :                 }
    2232             :         }
    2233             : 
    2234          11 :         if (os_strlen(cmd)) {
    2235          11 :                 int level = str_to_debug_level(cmd);
    2236          11 :                 if (level < 0)
    2237           1 :                         return -1;
    2238          10 :                 wpa_debug_level = level;
    2239             :         }
    2240             : 
    2241          10 :         if (stamp && os_strlen(stamp))
    2242           3 :                 wpa_debug_timestamp = atoi(stamp);
    2243             : 
    2244          10 :         os_memcpy(buf, "OK\n", 3);
    2245          10 :         return 3;
    2246             : }
    2247             : 
    2248             : 
    2249          36 : static int wpa_supplicant_ctrl_iface_list_networks(
    2250             :         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
    2251             : {
    2252             :         char *pos, *end, *prev;
    2253             :         struct wpa_ssid *ssid;
    2254             :         int ret;
    2255             : 
    2256          36 :         pos = buf;
    2257          36 :         end = buf + buflen;
    2258          36 :         ret = os_snprintf(pos, end - pos,
    2259             :                           "network id / ssid / bssid / flags\n");
    2260          36 :         if (os_snprintf_error(end - pos, ret))
    2261           0 :                 return pos - buf;
    2262          36 :         pos += ret;
    2263             : 
    2264          36 :         ssid = wpa_s->conf->ssid;
    2265             : 
    2266             :         /* skip over ssids until we find next one */
    2267          36 :         if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
    2268           1 :                 int last_id = atoi(cmd + 8);
    2269           1 :                 if (last_id != -1) {
    2270        1000 :                         while (ssid != NULL && ssid->id <= last_id) {
    2271         998 :                                 ssid = ssid->next;
    2272             :                         }
    2273             :                 }
    2274             :         }
    2275             : 
    2276         319 :         while (ssid) {
    2277         248 :                 prev = pos;
    2278         496 :                 ret = os_snprintf(pos, end - pos, "%d\t%s",
    2279             :                                   ssid->id,
    2280         248 :                                   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
    2281         248 :                 if (os_snprintf_error(end - pos, ret))
    2282           0 :                         return prev - buf;
    2283         248 :                 pos += ret;
    2284         248 :                 if (ssid->bssid_set) {
    2285          78 :                         ret = os_snprintf(pos, end - pos, "\t" MACSTR,
    2286          78 :                                           MAC2STR(ssid->bssid));
    2287             :                 } else {
    2288         235 :                         ret = os_snprintf(pos, end - pos, "\tany");
    2289             :                 }
    2290         248 :                 if (os_snprintf_error(end - pos, ret))
    2291           0 :                         return prev - buf;
    2292         248 :                 pos += ret;
    2293         992 :                 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
    2294         248 :                                   ssid == wpa_s->current_ssid ?
    2295             :                                   "[CURRENT]" : "",
    2296         248 :                                   ssid->disabled ? "[DISABLED]" : "",
    2297         248 :                                   ssid->disabled_until.sec ?
    2298             :                                   "[TEMP-DISABLED]" : "",
    2299         248 :                                   ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
    2300             :                                   "");
    2301         248 :                 if (os_snprintf_error(end - pos, ret))
    2302           1 :                         return prev - buf;
    2303         247 :                 pos += ret;
    2304         247 :                 ret = os_snprintf(pos, end - pos, "\n");
    2305         247 :                 if (os_snprintf_error(end - pos, ret))
    2306           0 :                         return prev - buf;
    2307         247 :                 pos += ret;
    2308             : 
    2309         247 :                 ssid = ssid->next;
    2310             :         }
    2311             : 
    2312          35 :         return pos - buf;
    2313             : }
    2314             : 
    2315             : 
    2316        1930 : static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
    2317             : {
    2318             :         int ret;
    2319        1930 :         ret = os_snprintf(pos, end - pos, "-");
    2320        1930 :         if (os_snprintf_error(end - pos, ret))
    2321           0 :                 return pos;
    2322        1930 :         pos += ret;
    2323        1930 :         ret = wpa_write_ciphers(pos, end, cipher, "+");
    2324        1930 :         if (ret < 0)
    2325           0 :                 return pos;
    2326        1930 :         pos += ret;
    2327        1930 :         return pos;
    2328             : }
    2329             : 
    2330             : 
    2331        1930 : static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
    2332             :                                     const u8 *ie, size_t ie_len)
    2333             : {
    2334             :         struct wpa_ie_data data;
    2335             :         char *start;
    2336             :         int ret;
    2337             : 
    2338        1930 :         ret = os_snprintf(pos, end - pos, "[%s-", proto);
    2339        1930 :         if (os_snprintf_error(end - pos, ret))
    2340           0 :                 return pos;
    2341        1930 :         pos += ret;
    2342             : 
    2343        1930 :         if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
    2344           0 :                 ret = os_snprintf(pos, end - pos, "?]");
    2345           0 :                 if (os_snprintf_error(end - pos, ret))
    2346           0 :                         return pos;
    2347           0 :                 pos += ret;
    2348           0 :                 return pos;
    2349             :         }
    2350             : 
    2351        1930 :         start = pos;
    2352        1930 :         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
    2353         341 :                 ret = os_snprintf(pos, end - pos, "%sEAP",
    2354             :                                   pos == start ? "" : "+");
    2355         341 :                 if (os_snprintf_error(end - pos, ret))
    2356           0 :                         return pos;
    2357         341 :                 pos += ret;
    2358             :         }
    2359        1930 :         if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
    2360        1523 :                 ret = os_snprintf(pos, end - pos, "%sPSK",
    2361             :                                   pos == start ? "" : "+");
    2362        1523 :                 if (os_snprintf_error(end - pos, ret))
    2363           0 :                         return pos;
    2364        1523 :                 pos += ret;
    2365             :         }
    2366        1930 :         if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
    2367           1 :                 ret = os_snprintf(pos, end - pos, "%sNone",
    2368             :                                   pos == start ? "" : "+");
    2369           1 :                 if (os_snprintf_error(end - pos, ret))
    2370           0 :                         return pos;
    2371           1 :                 pos += ret;
    2372             :         }
    2373        1930 :         if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
    2374          23 :                 ret = os_snprintf(pos, end - pos, "%sSAE",
    2375             :                                   pos == start ? "" : "+");
    2376          23 :                 if (os_snprintf_error(end - pos, ret))
    2377           0 :                         return pos;
    2378          23 :                 pos += ret;
    2379             :         }
    2380             : #ifdef CONFIG_IEEE80211R
    2381        1930 :         if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
    2382           7 :                 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
    2383             :                                   pos == start ? "" : "+");
    2384           7 :                 if (os_snprintf_error(end - pos, ret))
    2385           0 :                         return pos;
    2386           7 :                 pos += ret;
    2387             :         }
    2388        1930 :         if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
    2389          38 :                 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
    2390             :                                   pos == start ? "" : "+");
    2391          38 :                 if (os_snprintf_error(end - pos, ret))
    2392           0 :                         return pos;
    2393          38 :                 pos += ret;
    2394             :         }
    2395        1930 :         if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
    2396           4 :                 ret = os_snprintf(pos, end - pos, "%sFT/SAE",
    2397             :                                   pos == start ? "" : "+");
    2398           4 :                 if (os_snprintf_error(end - pos, ret))
    2399           0 :                         return pos;
    2400           4 :                 pos += ret;
    2401             :         }
    2402             : #endif /* CONFIG_IEEE80211R */
    2403             : #ifdef CONFIG_IEEE80211W
    2404        1930 :         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
    2405           3 :                 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
    2406             :                                   pos == start ? "" : "+");
    2407           3 :                 if (os_snprintf_error(end - pos, ret))
    2408           0 :                         return pos;
    2409           3 :                 pos += ret;
    2410             :         }
    2411        1930 :         if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
    2412           3 :                 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
    2413             :                                   pos == start ? "" : "+");
    2414           3 :                 if (os_snprintf_error(end - pos, ret))
    2415           0 :                         return pos;
    2416           3 :                 pos += ret;
    2417             :         }
    2418             : #endif /* CONFIG_IEEE80211W */
    2419             : 
    2420             : #ifdef CONFIG_SUITEB
    2421        1930 :         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
    2422           1 :                 ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
    2423             :                                   pos == start ? "" : "+");
    2424           1 :                 if (os_snprintf_error(end - pos, ret))
    2425           0 :                         return pos;
    2426           1 :                 pos += ret;
    2427             :         }
    2428             : #endif /* CONFIG_SUITEB */
    2429             : 
    2430             : #ifdef CONFIG_SUITEB192
    2431        1930 :         if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
    2432           1 :                 ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
    2433             :                                   pos == start ? "" : "+");
    2434           1 :                 if (os_snprintf_error(end - pos, ret))
    2435           0 :                         return pos;
    2436           1 :                 pos += ret;
    2437             :         }
    2438             : #endif /* CONFIG_SUITEB192 */
    2439             : 
    2440        1930 :         if (data.key_mgmt & WPA_KEY_MGMT_OSEN) {
    2441           2 :                 ret = os_snprintf(pos, end - pos, "%sOSEN",
    2442             :                                   pos == start ? "" : "+");
    2443           2 :                 if (os_snprintf_error(end - pos, ret))
    2444           0 :                         return pos;
    2445           2 :                 pos += ret;
    2446             :         }
    2447             : 
    2448        1930 :         pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
    2449             : 
    2450        1930 :         if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
    2451           9 :                 ret = os_snprintf(pos, end - pos, "-preauth");
    2452           9 :                 if (os_snprintf_error(end - pos, ret))
    2453           0 :                         return pos;
    2454           9 :                 pos += ret;
    2455             :         }
    2456             : 
    2457        1930 :         ret = os_snprintf(pos, end - pos, "]");
    2458        1930 :         if (os_snprintf_error(end - pos, ret))
    2459           0 :                 return pos;
    2460        1930 :         pos += ret;
    2461             : 
    2462        1930 :         return pos;
    2463             : }
    2464             : 
    2465             : 
    2466             : #ifdef CONFIG_WPS
    2467        2426 : static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
    2468             :                                             char *pos, char *end,
    2469             :                                             struct wpabuf *wps_ie)
    2470             : {
    2471             :         int ret;
    2472             :         const char *txt;
    2473             : 
    2474        2426 :         if (wps_ie == NULL)
    2475         909 :                 return pos;
    2476        1517 :         if (wps_is_selected_pbc_registrar(wps_ie))
    2477          74 :                 txt = "[WPS-PBC]";
    2478        1443 :         else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
    2479        1324 :                 txt = "[WPS-AUTH]";
    2480         119 :         else if (wps_is_selected_pin_registrar(wps_ie))
    2481           1 :                 txt = "[WPS-PIN]";
    2482             :         else
    2483         118 :                 txt = "[WPS]";
    2484             : 
    2485        1517 :         ret = os_snprintf(pos, end - pos, "%s", txt);
    2486        1517 :         if (!os_snprintf_error(end - pos, ret))
    2487        1517 :                 pos += ret;
    2488        1517 :         wpabuf_free(wps_ie);
    2489        1517 :         return pos;
    2490             : }
    2491             : #endif /* CONFIG_WPS */
    2492             : 
    2493             : 
    2494        2426 : static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
    2495             :                                         char *pos, char *end,
    2496             :                                         const struct wpa_bss *bss)
    2497             : {
    2498             : #ifdef CONFIG_WPS
    2499             :         struct wpabuf *wps_ie;
    2500        2426 :         wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
    2501        2426 :         return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
    2502             : #else /* CONFIG_WPS */
    2503             :         return pos;
    2504             : #endif /* CONFIG_WPS */
    2505             : }
    2506             : 
    2507             : 
    2508             : /* Format one result on one text line into a buffer. */
    2509          84 : static int wpa_supplicant_ctrl_iface_scan_result(
    2510             :         struct wpa_supplicant *wpa_s,
    2511             :         const struct wpa_bss *bss, char *buf, size_t buflen)
    2512             : {
    2513             :         char *pos, *end;
    2514             :         int ret;
    2515             :         const u8 *ie, *ie2, *osen_ie, *p2p, *mesh;
    2516             : 
    2517          84 :         mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
    2518          84 :         p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
    2519          84 :         if (!p2p)
    2520          77 :                 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
    2521          84 :         if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
    2522           0 :             os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
    2523             :             0)
    2524           0 :                 return 0; /* Do not show P2P listen discovery results here */
    2525             : 
    2526          84 :         pos = buf;
    2527          84 :         end = buf + buflen;
    2528             : 
    2529         588 :         ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
    2530         504 :                           MAC2STR(bss->bssid), bss->freq, bss->level);
    2531          84 :         if (os_snprintf_error(end - pos, ret))
    2532           0 :                 return -1;
    2533          84 :         pos += ret;
    2534          84 :         ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
    2535          84 :         if (ie)
    2536           2 :                 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
    2537          84 :         ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
    2538          84 :         if (ie2) {
    2539          29 :                 pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
    2540          29 :                                             ie2, 2 + ie2[1]);
    2541             :         }
    2542          84 :         osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
    2543          84 :         if (osen_ie)
    2544           1 :                 pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
    2545           1 :                                             osen_ie, 2 + osen_ie[1]);
    2546          84 :         pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
    2547          84 :         if (!ie && !ie2 && !osen_ie && (bss->caps & IEEE80211_CAP_PRIVACY)) {
    2548           2 :                 ret = os_snprintf(pos, end - pos, "[WEP]");
    2549           2 :                 if (os_snprintf_error(end - pos, ret))
    2550           0 :                         return -1;
    2551           2 :                 pos += ret;
    2552             :         }
    2553          84 :         if (mesh) {
    2554           2 :                 ret = os_snprintf(pos, end - pos, "[MESH]");
    2555           2 :                 if (os_snprintf_error(end - pos, ret))
    2556           0 :                         return -1;
    2557           2 :                 pos += ret;
    2558             :         }
    2559          84 :         if (bss_is_dmg(bss)) {
    2560             :                 const char *s;
    2561           0 :                 ret = os_snprintf(pos, end - pos, "[DMG]");
    2562           0 :                 if (os_snprintf_error(end - pos, ret))
    2563           0 :                         return -1;
    2564           0 :                 pos += ret;
    2565           0 :                 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
    2566             :                 case IEEE80211_CAP_DMG_IBSS:
    2567           0 :                         s = "[IBSS]";
    2568           0 :                         break;
    2569             :                 case IEEE80211_CAP_DMG_AP:
    2570           0 :                         s = "[ESS]";
    2571           0 :                         break;
    2572             :                 case IEEE80211_CAP_DMG_PBSS:
    2573           0 :                         s = "[PBSS]";
    2574           0 :                         break;
    2575             :                 default:
    2576           0 :                         s = "";
    2577           0 :                         break;
    2578             :                 }
    2579           0 :                 ret = os_snprintf(pos, end - pos, "%s", s);
    2580           0 :                 if (os_snprintf_error(end - pos, ret))
    2581           0 :                         return -1;
    2582           0 :                 pos += ret;
    2583             :         } else {
    2584          84 :                 if (bss->caps & IEEE80211_CAP_IBSS) {
    2585           1 :                         ret = os_snprintf(pos, end - pos, "[IBSS]");
    2586           1 :                         if (os_snprintf_error(end - pos, ret))
    2587           0 :                                 return -1;
    2588           1 :                         pos += ret;
    2589             :                 }
    2590          84 :                 if (bss->caps & IEEE80211_CAP_ESS) {
    2591          81 :                         ret = os_snprintf(pos, end - pos, "[ESS]");
    2592          81 :                         if (os_snprintf_error(end - pos, ret))
    2593           0 :                                 return -1;
    2594          81 :                         pos += ret;
    2595             :                 }
    2596             :         }
    2597          84 :         if (p2p) {
    2598           8 :                 ret = os_snprintf(pos, end - pos, "[P2P]");
    2599           8 :                 if (os_snprintf_error(end - pos, ret))
    2600           0 :                         return -1;
    2601           8 :                 pos += ret;
    2602             :         }
    2603             : #ifdef CONFIG_HS20
    2604          84 :         if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
    2605           1 :                 ret = os_snprintf(pos, end - pos, "[HS20]");
    2606           1 :                 if (os_snprintf_error(end - pos, ret))
    2607           0 :                         return -1;
    2608           1 :                 pos += ret;
    2609             :         }
    2610             : #endif /* CONFIG_HS20 */
    2611             : #ifdef CONFIG_FST
    2612          84 :         if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
    2613           1 :                 ret = os_snprintf(pos, end - pos, "[FST]");
    2614           1 :                 if (os_snprintf_error(end - pos, ret))
    2615           0 :                         return -1;
    2616           1 :                 pos += ret;
    2617             :         }
    2618             : #endif /* CONFIG_FST */
    2619             : 
    2620         168 :         ret = os_snprintf(pos, end - pos, "\t%s",
    2621          84 :                           wpa_ssid_txt(bss->ssid, bss->ssid_len));
    2622          84 :         if (os_snprintf_error(end - pos, ret))
    2623           0 :                 return -1;
    2624          84 :         pos += ret;
    2625             : 
    2626          84 :         ret = os_snprintf(pos, end - pos, "\n");
    2627          84 :         if (os_snprintf_error(end - pos, ret))
    2628           0 :                 return -1;
    2629          84 :         pos += ret;
    2630             : 
    2631          84 :         return pos - buf;
    2632             : }
    2633             : 
    2634             : 
    2635         329 : static int wpa_supplicant_ctrl_iface_scan_results(
    2636             :         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
    2637             : {
    2638             :         char *pos, *end;
    2639             :         struct wpa_bss *bss;
    2640             :         int ret;
    2641             : 
    2642         329 :         pos = buf;
    2643         329 :         end = buf + buflen;
    2644         329 :         ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
    2645             :                           "flags / ssid\n");
    2646         329 :         if (os_snprintf_error(end - pos, ret))
    2647           0 :                 return pos - buf;
    2648         329 :         pos += ret;
    2649             : 
    2650         413 :         dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
    2651          84 :                 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
    2652          84 :                                                             end - pos);
    2653          84 :                 if (ret < 0 || ret >= end - pos)
    2654           0 :                         return pos - buf;
    2655          84 :                 pos += ret;
    2656             :         }
    2657             : 
    2658         329 :         return pos - buf;
    2659             : }
    2660             : 
    2661             : 
    2662             : #ifdef CONFIG_MESH
    2663             : 
    2664           7 : static int wpa_supplicant_ctrl_iface_mesh_interface_add(
    2665             :         struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
    2666             : {
    2667             :         char *pos, ifname[IFNAMSIZ + 1];
    2668             : 
    2669           7 :         ifname[0] = '\0';
    2670             : 
    2671           7 :         pos = os_strstr(cmd, "ifname=");
    2672           7 :         if (pos) {
    2673           2 :                 pos += 7;
    2674           2 :                 os_strlcpy(ifname, pos, sizeof(ifname));
    2675             :         }
    2676             : 
    2677           7 :         if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
    2678           3 :                 return -1;
    2679             : 
    2680           4 :         os_strlcpy(reply, ifname, max_len);
    2681           4 :         return os_strlen(ifname);
    2682             : }
    2683             : 
    2684             : 
    2685         194 : static int wpa_supplicant_ctrl_iface_mesh_group_add(
    2686             :         struct wpa_supplicant *wpa_s, char *cmd)
    2687             : {
    2688             :         int id;
    2689             :         struct wpa_ssid *ssid;
    2690             : 
    2691         194 :         id = atoi(cmd);
    2692         194 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
    2693             : 
    2694         194 :         ssid = wpa_config_get_network(wpa_s->conf, id);
    2695         194 :         if (ssid == NULL) {
    2696           1 :                 wpa_printf(MSG_DEBUG,
    2697             :                            "CTRL_IFACE: Could not find network id=%d", id);
    2698           1 :                 return -1;
    2699             :         }
    2700         193 :         if (ssid->mode != WPAS_MODE_MESH) {
    2701           1 :                 wpa_printf(MSG_DEBUG,
    2702             :                            "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
    2703           1 :                 return -1;
    2704             :         }
    2705         266 :         if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
    2706          74 :             ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
    2707           1 :                 wpa_printf(MSG_ERROR,
    2708             :                            "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
    2709           1 :                 return -1;
    2710             :         }
    2711             : 
    2712             :         /*
    2713             :          * TODO: If necessary write our own group_add function,
    2714             :          * for now we can reuse select_network
    2715             :          */
    2716         191 :         wpa_supplicant_select_network(wpa_s, ssid);
    2717             : 
    2718         191 :         return 0;
    2719             : }
    2720             : 
    2721             : 
    2722          42 : static int wpa_supplicant_ctrl_iface_mesh_group_remove(
    2723             :         struct wpa_supplicant *wpa_s, char *cmd)
    2724             : {
    2725             :         struct wpa_supplicant *orig;
    2726             :         struct wpa_global *global;
    2727          42 :         int found = 0;
    2728             : 
    2729          42 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
    2730             : 
    2731          42 :         global = wpa_s->global;
    2732          42 :         orig = wpa_s;
    2733             : 
    2734          49 :         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    2735          44 :                 if (os_strcmp(wpa_s->ifname, cmd) == 0) {
    2736          37 :                         found = 1;
    2737          37 :                         break;
    2738             :                 }
    2739             :         }
    2740          42 :         if (!found) {
    2741           5 :                 wpa_printf(MSG_ERROR,
    2742             :                            "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
    2743             :                            cmd);
    2744           5 :                 return -1;
    2745             :         }
    2746          37 :         if (wpa_s->mesh_if_created && wpa_s == orig) {
    2747           2 :                 wpa_printf(MSG_ERROR,
    2748             :                            "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
    2749           2 :                 return -1;
    2750             :         }
    2751             : 
    2752          35 :         wpa_s->reassociate = 0;
    2753          35 :         wpa_s->disconnected = 1;
    2754          35 :         wpa_supplicant_cancel_sched_scan(wpa_s);
    2755          35 :         wpa_supplicant_cancel_scan(wpa_s);
    2756             : 
    2757             :         /*
    2758             :          * TODO: If necessary write our own group_remove function,
    2759             :          * for now we can reuse deauthenticate
    2760             :          */
    2761          35 :         wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
    2762             : 
    2763          35 :         if (wpa_s->mesh_if_created)
    2764           4 :                 wpa_supplicant_remove_iface(global, wpa_s, 0);
    2765             : 
    2766          35 :         return 0;
    2767             : }
    2768             : 
    2769             : 
    2770           9 : static int wpa_supplicant_ctrl_iface_mesh_peer_remove(
    2771             :         struct wpa_supplicant *wpa_s, char *cmd)
    2772             : {
    2773             :         u8 addr[ETH_ALEN];
    2774             : 
    2775           9 :         if (hwaddr_aton(cmd, addr) < 0)
    2776           0 :                 return -1;
    2777             : 
    2778           9 :         return wpas_mesh_peer_remove(wpa_s, addr);
    2779             : }
    2780             : 
    2781             : 
    2782          13 : static int wpa_supplicant_ctrl_iface_mesh_peer_add(
    2783             :         struct wpa_supplicant *wpa_s, char *cmd)
    2784             : {
    2785             :         u8 addr[ETH_ALEN];
    2786             :         int duration;
    2787             :         char *pos;
    2788             : 
    2789          13 :         pos = os_strstr(cmd, " duration=");
    2790          13 :         if (pos) {
    2791           0 :                 *pos = '\0';
    2792           0 :                 duration = atoi(pos + 10);
    2793             :         } else {
    2794          13 :                 duration = -1;
    2795             :         }
    2796             : 
    2797          13 :         if (hwaddr_aton(cmd, addr))
    2798           0 :                 return -1;
    2799             : 
    2800          13 :         return wpas_mesh_peer_add(wpa_s, addr, duration);
    2801             : }
    2802             : 
    2803             : #endif /* CONFIG_MESH */
    2804             : 
    2805             : 
    2806        3331 : static int wpa_supplicant_ctrl_iface_select_network(
    2807             :         struct wpa_supplicant *wpa_s, char *cmd)
    2808             : {
    2809             :         int id;
    2810             :         struct wpa_ssid *ssid;
    2811             :         char *pos;
    2812             : 
    2813             :         /* cmd: "<network id>" or "any" */
    2814        3331 :         if (os_strncmp(cmd, "any", 3) == 0) {
    2815           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
    2816           1 :                 ssid = NULL;
    2817             :         } else {
    2818        3330 :                 id = atoi(cmd);
    2819        3330 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
    2820             : 
    2821        3330 :                 ssid = wpa_config_get_network(wpa_s->conf, id);
    2822        3330 :                 if (ssid == NULL) {
    2823           1 :                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
    2824             :                                    "network id=%d", id);
    2825           1 :                         return -1;
    2826             :                 }
    2827        3329 :                 if (ssid->disabled == 2) {
    2828           1 :                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
    2829             :                                    "SELECT_NETWORK with persistent P2P group");
    2830           1 :                         return -1;
    2831             :                 }
    2832             :         }
    2833             : 
    2834        3329 :         pos = os_strstr(cmd, " freq=");
    2835        3329 :         if (pos) {
    2836          65 :                 int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
    2837          65 :                 if (freqs) {
    2838          65 :                         wpa_s->scan_req = MANUAL_SCAN_REQ;
    2839          65 :                         os_free(wpa_s->manual_scan_freqs);
    2840          65 :                         wpa_s->manual_scan_freqs = freqs;
    2841             :                 }
    2842             :         }
    2843             : 
    2844        3329 :         wpa_s->scan_min_time.sec = 0;
    2845        3329 :         wpa_s->scan_min_time.usec = 0;
    2846        3329 :         wpa_supplicant_select_network(wpa_s, ssid);
    2847             : 
    2848        3329 :         return 0;
    2849             : }
    2850             : 
    2851             : 
    2852          46 : static int wpa_supplicant_ctrl_iface_enable_network(
    2853             :         struct wpa_supplicant *wpa_s, char *cmd)
    2854             : {
    2855             :         int id;
    2856             :         struct wpa_ssid *ssid;
    2857             : 
    2858             :         /* cmd: "<network id>" or "all" */
    2859          46 :         if (os_strcmp(cmd, "all") == 0) {
    2860           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
    2861           1 :                 ssid = NULL;
    2862             :         } else {
    2863          45 :                 id = atoi(cmd);
    2864          45 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
    2865             : 
    2866          45 :                 ssid = wpa_config_get_network(wpa_s->conf, id);
    2867          45 :                 if (ssid == NULL) {
    2868           1 :                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
    2869             :                                    "network id=%d", id);
    2870           1 :                         return -1;
    2871             :                 }
    2872          44 :                 if (ssid->disabled == 2) {
    2873           1 :                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
    2874             :                                    "ENABLE_NETWORK with persistent P2P group");
    2875           1 :                         return -1;
    2876             :                 }
    2877             : 
    2878          43 :                 if (os_strstr(cmd, " no-connect")) {
    2879          36 :                         ssid->disabled = 0;
    2880          36 :                         return 0;
    2881             :                 }
    2882             :         }
    2883           8 :         wpa_s->scan_min_time.sec = 0;
    2884           8 :         wpa_s->scan_min_time.usec = 0;
    2885           8 :         wpa_supplicant_enable_network(wpa_s, ssid);
    2886             : 
    2887           8 :         return 0;
    2888             : }
    2889             : 
    2890             : 
    2891           9 : static int wpa_supplicant_ctrl_iface_disable_network(
    2892             :         struct wpa_supplicant *wpa_s, char *cmd)
    2893             : {
    2894             :         int id;
    2895             :         struct wpa_ssid *ssid;
    2896             : 
    2897             :         /* cmd: "<network id>" or "all" */
    2898           9 :         if (os_strcmp(cmd, "all") == 0) {
    2899           2 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
    2900           2 :                 ssid = NULL;
    2901             :         } else {
    2902           7 :                 id = atoi(cmd);
    2903           7 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
    2904             : 
    2905           7 :                 ssid = wpa_config_get_network(wpa_s->conf, id);
    2906           7 :                 if (ssid == NULL) {
    2907           1 :                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
    2908             :                                    "network id=%d", id);
    2909           1 :                         return -1;
    2910             :                 }
    2911           6 :                 if (ssid->disabled == 2) {
    2912           1 :                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
    2913             :                                    "DISABLE_NETWORK with persistent P2P "
    2914             :                                    "group");
    2915           1 :                         return -1;
    2916             :                 }
    2917             :         }
    2918           7 :         wpa_supplicant_disable_network(wpa_s, ssid);
    2919             : 
    2920           7 :         return 0;
    2921             : }
    2922             : 
    2923             : 
    2924        4459 : static int wpa_supplicant_ctrl_iface_add_network(
    2925             :         struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
    2926             : {
    2927             :         struct wpa_ssid *ssid;
    2928             :         int ret;
    2929             : 
    2930        4459 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
    2931             : 
    2932        4459 :         ssid = wpa_supplicant_add_network(wpa_s);
    2933        4459 :         if (ssid == NULL)
    2934           0 :                 return -1;
    2935             : 
    2936        4459 :         ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
    2937        4459 :         if (os_snprintf_error(buflen, ret))
    2938           0 :                 return -1;
    2939        4459 :         return ret;
    2940             : }
    2941             : 
    2942             : 
    2943       14102 : static int wpa_supplicant_ctrl_iface_remove_network(
    2944             :         struct wpa_supplicant *wpa_s, char *cmd)
    2945             : {
    2946             :         int id;
    2947             :         struct wpa_ssid *ssid;
    2948             :         int result;
    2949             : 
    2950             :         /* cmd: "<network id>" or "all" */
    2951       14102 :         if (os_strcmp(cmd, "all") == 0) {
    2952       13969 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
    2953       13969 :                 if (wpa_s->sched_scanning)
    2954           0 :                         wpa_supplicant_cancel_sched_scan(wpa_s);
    2955             : 
    2956       13969 :                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
    2957       13969 :                 if (wpa_s->current_ssid) {
    2958             : #ifdef CONFIG_SME
    2959        2666 :                         wpa_s->sme.prev_bssid_set = 0;
    2960             : #endif /* CONFIG_SME */
    2961        2666 :                         wpa_sm_set_config(wpa_s->wpa, NULL);
    2962        2666 :                         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
    2963        2666 :                         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
    2964        1685 :                                 wpa_s->own_disconnect_req = 1;
    2965        2666 :                         wpa_supplicant_deauthenticate(
    2966             :                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
    2967             :                 }
    2968       13969 :                 ssid = wpa_s->conf->ssid;
    2969       32307 :                 while (ssid) {
    2970        4369 :                         struct wpa_ssid *remove_ssid = ssid;
    2971        4369 :                         id = ssid->id;
    2972        4369 :                         ssid = ssid->next;
    2973        4369 :                         if (wpa_s->last_ssid == remove_ssid)
    2974        3017 :                                 wpa_s->last_ssid = NULL;
    2975        4369 :                         wpas_notify_network_removed(wpa_s, remove_ssid);
    2976        4369 :                         wpa_config_remove_network(wpa_s->conf, id);
    2977             :                 }
    2978       13969 :                 return 0;
    2979             :         }
    2980             : 
    2981         133 :         id = atoi(cmd);
    2982         133 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
    2983             : 
    2984         133 :         result = wpa_supplicant_remove_network(wpa_s, id);
    2985         133 :         if (result == -1) {
    2986           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
    2987             :                            "id=%d", id);
    2988           1 :                 return -1;
    2989             :         }
    2990         132 :         if (result == -2) {
    2991           0 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
    2992             :                            "network id=%d", id);
    2993           0 :                 return -1;
    2994             :         }
    2995         132 :         return 0;
    2996             : }
    2997             : 
    2998             : 
    2999       20115 : static int wpa_supplicant_ctrl_iface_update_network(
    3000             :         struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
    3001             :         char *name, char *value)
    3002             : {
    3003             :         int ret;
    3004             : 
    3005       20115 :         ret = wpa_config_set(ssid, name, value, 0);
    3006       20115 :         if (ret < 0) {
    3007          42 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
    3008             :                            "variable '%s'", name);
    3009          42 :                 return -1;
    3010             :         }
    3011       20073 :         if (ret == 1)
    3012          59 :                 return 0; /* No change to the previously configured value */
    3013             : 
    3014       39987 :         if (os_strcmp(name, "bssid") != 0 &&
    3015       19973 :             os_strcmp(name, "priority") != 0) {
    3016       19972 :                 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
    3017             : 
    3018       39914 :                 if (wpa_s->current_ssid == ssid ||
    3019       19942 :                     wpa_s->current_ssid == NULL) {
    3020             :                         /*
    3021             :                          * Invalidate the EAP session cache if anything in the
    3022             :                          * current or previously used configuration changes.
    3023             :                          */
    3024       19702 :                         eapol_sm_invalidate_cached_session(wpa_s->eapol);
    3025             :                 }
    3026             :         }
    3027             : 
    3028       20550 :         if ((os_strcmp(name, "psk") == 0 &&
    3029       20553 :              value[0] == '"' && ssid->ssid_len) ||
    3030       22966 :             (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
    3031         495 :                 wpa_config_update_psk(ssid);
    3032       19519 :         else if (os_strcmp(name, "priority") == 0)
    3033           1 :                 wpa_config_update_prio_list(wpa_s->conf);
    3034             : 
    3035       20014 :         return 0;
    3036             : }
    3037             : 
    3038             : 
    3039       20110 : static int wpa_supplicant_ctrl_iface_set_network(
    3040             :         struct wpa_supplicant *wpa_s, char *cmd)
    3041             : {
    3042             :         int id, ret, prev_bssid_set, prev_disabled;
    3043             :         struct wpa_ssid *ssid;
    3044             :         char *name, *value;
    3045             :         u8 prev_bssid[ETH_ALEN];
    3046             : 
    3047             :         /* cmd: "<network id> <variable name> <value>" */
    3048       20110 :         name = os_strchr(cmd, ' ');
    3049       20110 :         if (name == NULL)
    3050           1 :                 return -1;
    3051       20109 :         *name++ = '\0';
    3052             : 
    3053       20109 :         value = os_strchr(name, ' ');
    3054       20109 :         if (value == NULL)
    3055           1 :                 return -1;
    3056       20108 :         *value++ = '\0';
    3057             : 
    3058       20108 :         id = atoi(cmd);
    3059       20108 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
    3060             :                    id, name);
    3061       20108 :         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
    3062             :                               (u8 *) value, os_strlen(value));
    3063             : 
    3064       20108 :         ssid = wpa_config_get_network(wpa_s->conf, id);
    3065       20108 :         if (ssid == NULL) {
    3066           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
    3067             :                            "id=%d", id);
    3068           1 :                 return -1;
    3069             :         }
    3070             : 
    3071       20107 :         prev_bssid_set = ssid->bssid_set;
    3072       20107 :         prev_disabled = ssid->disabled;
    3073       20107 :         os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
    3074       20107 :         ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
    3075             :                                                        value);
    3076       40172 :         if (ret == 0 &&
    3077       40091 :             (ssid->bssid_set != prev_bssid_set ||
    3078       20026 :              os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
    3079          40 :                 wpas_notify_network_bssid_set_changed(wpa_s, ssid);
    3080             : 
    3081       20107 :         if (prev_disabled != ssid->disabled &&
    3082          21 :             (prev_disabled == 2 || ssid->disabled == 2))
    3083           2 :                 wpas_notify_network_type_changed(wpa_s, ssid);
    3084             : 
    3085       20107 :         return ret;
    3086             : }
    3087             : 
    3088             : 
    3089          69 : static int wpa_supplicant_ctrl_iface_get_network(
    3090             :         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
    3091             : {
    3092             :         int id;
    3093             :         size_t res;
    3094             :         struct wpa_ssid *ssid;
    3095             :         char *name, *value;
    3096             : 
    3097             :         /* cmd: "<network id> <variable name>" */
    3098          69 :         name = os_strchr(cmd, ' ');
    3099          69 :         if (name == NULL || buflen == 0)
    3100           1 :                 return -1;
    3101          68 :         *name++ = '\0';
    3102             : 
    3103          68 :         id = atoi(cmd);
    3104          68 :         wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
    3105             :                    id, name);
    3106             : 
    3107          68 :         ssid = wpa_config_get_network(wpa_s->conf, id);
    3108          68 :         if (ssid == NULL) {
    3109           1 :                 wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Could not find network "
    3110             :                            "id=%d", id);
    3111           1 :                 return -1;
    3112             :         }
    3113             : 
    3114          67 :         value = wpa_config_get_no_key(ssid, name);
    3115          67 :         if (value == NULL) {
    3116           7 :                 wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Failed to get network "
    3117             :                            "variable '%s'", name);
    3118           7 :                 return -1;
    3119             :         }
    3120             : 
    3121          60 :         res = os_strlcpy(buf, value, buflen);
    3122          60 :         if (res >= buflen) {
    3123           0 :                 os_free(value);
    3124           0 :                 return -1;
    3125             :         }
    3126             : 
    3127          60 :         os_free(value);
    3128             : 
    3129          60 :         return res;
    3130             : }
    3131             : 
    3132             : 
    3133          16 : static int wpa_supplicant_ctrl_iface_dup_network(
    3134             :         struct wpa_supplicant *wpa_s, char *cmd,
    3135             :         struct wpa_supplicant *dst_wpa_s)
    3136             : {
    3137             :         struct wpa_ssid *ssid_s, *ssid_d;
    3138             :         char *name, *id, *value;
    3139             :         int id_s, id_d, ret;
    3140             : 
    3141             :         /* cmd: "<src network id> <dst network id> <variable name>" */
    3142          16 :         id = os_strchr(cmd, ' ');
    3143          16 :         if (id == NULL)
    3144           2 :                 return -1;
    3145          14 :         *id++ = '\0';
    3146             : 
    3147          14 :         name = os_strchr(id, ' ');
    3148          14 :         if (name == NULL)
    3149           3 :                 return -1;
    3150          11 :         *name++ = '\0';
    3151             : 
    3152          11 :         id_s = atoi(cmd);
    3153          11 :         id_d = atoi(id);
    3154             : 
    3155          11 :         wpa_printf(MSG_DEBUG,
    3156             :                    "CTRL_IFACE: DUP_NETWORK ifname=%s->%s id=%d->%d name='%s'",
    3157          11 :                    wpa_s->ifname, dst_wpa_s->ifname, id_s, id_d, name);
    3158             : 
    3159          11 :         ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
    3160          11 :         if (ssid_s == NULL) {
    3161           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
    3162             :                            "network id=%d", id_s);
    3163           1 :                 return -1;
    3164             :         }
    3165             : 
    3166          10 :         ssid_d = wpa_config_get_network(dst_wpa_s->conf, id_d);
    3167          10 :         if (ssid_d == NULL) {
    3168           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
    3169             :                            "network id=%d", id_d);
    3170           1 :                 return -1;
    3171             :         }
    3172             : 
    3173           9 :         value = wpa_config_get(ssid_s, name);
    3174           9 :         if (value == NULL) {
    3175           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
    3176             :                            "variable '%s'", name);
    3177           1 :                 return -1;
    3178             :         }
    3179             : 
    3180           8 :         ret = wpa_supplicant_ctrl_iface_update_network(dst_wpa_s, ssid_d, name,
    3181             :                                                        value);
    3182             : 
    3183           8 :         os_free(value);
    3184             : 
    3185           8 :         return ret;
    3186             : }
    3187             : 
    3188             : 
    3189           8 : static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
    3190             :                                                 char *buf, size_t buflen)
    3191             : {
    3192             :         char *pos, *end;
    3193             :         struct wpa_cred *cred;
    3194             :         int ret;
    3195             : 
    3196           8 :         pos = buf;
    3197           8 :         end = buf + buflen;
    3198           8 :         ret = os_snprintf(pos, end - pos,
    3199             :                           "cred id / realm / username / domain / imsi\n");
    3200           8 :         if (os_snprintf_error(end - pos, ret))
    3201           0 :                 return pos - buf;
    3202           8 :         pos += ret;
    3203             : 
    3204           8 :         cred = wpa_s->conf->cred;
    3205         111 :         while (cred) {
    3206         388 :                 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
    3207          96 :                                   cred->id, cred->realm ? cred->realm : "",
    3208          96 :                                   cred->username ? cred->username : "",
    3209         100 :                                   cred->domain ? cred->domain[0] : "",
    3210          96 :                                   cred->imsi ? cred->imsi : "");
    3211          96 :                 if (os_snprintf_error(end - pos, ret))
    3212           1 :                         return pos - buf;
    3213          95 :                 pos += ret;
    3214             : 
    3215          95 :                 cred = cred->next;
    3216             :         }
    3217             : 
    3218           7 :         return pos - buf;
    3219             : }
    3220             : 
    3221             : 
    3222         288 : static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
    3223             :                                               char *buf, size_t buflen)
    3224             : {
    3225             :         struct wpa_cred *cred;
    3226             :         int ret;
    3227             : 
    3228         288 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
    3229             : 
    3230         288 :         cred = wpa_config_add_cred(wpa_s->conf);
    3231         288 :         if (cred == NULL)
    3232           0 :                 return -1;
    3233             : 
    3234         288 :         wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
    3235             : 
    3236         288 :         ret = os_snprintf(buf, buflen, "%d\n", cred->id);
    3237         288 :         if (os_snprintf_error(buflen, ret))
    3238           0 :                 return -1;
    3239         288 :         return ret;
    3240             : }
    3241             : 
    3242             : 
    3243         284 : static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
    3244             :                                  struct wpa_cred *cred)
    3245             : {
    3246             :         struct wpa_ssid *ssid;
    3247             :         char str[20];
    3248             :         int id;
    3249             : 
    3250         284 :         if (cred == NULL) {
    3251           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
    3252           1 :                 return -1;
    3253             :         }
    3254             : 
    3255         283 :         id = cred->id;
    3256         283 :         if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
    3257           0 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
    3258           0 :                 return -1;
    3259             :         }
    3260             : 
    3261         283 :         wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
    3262             : 
    3263             :         /* Remove any network entry created based on the removed credential */
    3264         283 :         ssid = wpa_s->conf->ssid;
    3265         595 :         while (ssid) {
    3266          29 :                 if (ssid->parent_cred == cred) {
    3267             :                         int res;
    3268             : 
    3269          26 :                         wpa_printf(MSG_DEBUG, "Remove network id %d since it "
    3270             :                                    "used the removed credential", ssid->id);
    3271          26 :                         res = os_snprintf(str, sizeof(str), "%d", ssid->id);
    3272          26 :                         if (os_snprintf_error(sizeof(str), res))
    3273           0 :                                 str[sizeof(str) - 1] = '\0';
    3274          26 :                         ssid = ssid->next;
    3275          26 :                         wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
    3276             :                 } else
    3277           3 :                         ssid = ssid->next;
    3278             :         }
    3279             : 
    3280         283 :         return 0;
    3281             : }
    3282             : 
    3283             : 
    3284        6239 : static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
    3285             :                                                  char *cmd)
    3286             : {
    3287             :         int id;
    3288             :         struct wpa_cred *cred, *prev;
    3289             : 
    3290             :         /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
    3291             :          * "provisioning_sp=<FQDN> */
    3292        6239 :         if (os_strcmp(cmd, "all") == 0) {
    3293        6062 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
    3294        6062 :                 cred = wpa_s->conf->cred;
    3295       12229 :                 while (cred) {
    3296         105 :                         prev = cred;
    3297         105 :                         cred = cred->next;
    3298         105 :                         wpas_ctrl_remove_cred(wpa_s, prev);
    3299             :                 }
    3300        6062 :                 return 0;
    3301             :         }
    3302             : 
    3303         177 :         if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
    3304           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
    3305             :                            cmd + 8);
    3306           1 :                 cred = wpa_s->conf->cred;
    3307           5 :                 while (cred) {
    3308           3 :                         prev = cred;
    3309           3 :                         cred = cred->next;
    3310           3 :                         if (prev->domain) {
    3311             :                                 size_t i;
    3312           8 :                                 for (i = 0; i < prev->num_domain; i++) {
    3313           3 :                                         if (os_strcmp(prev->domain[i], cmd + 8)
    3314             :                                             != 0)
    3315           1 :                                                 continue;
    3316           2 :                                         wpas_ctrl_remove_cred(wpa_s, prev);
    3317           2 :                                         break;
    3318             :                                 }
    3319             :                         }
    3320             :                 }
    3321           1 :                 return 0;
    3322             :         }
    3323             : 
    3324         176 :         if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
    3325           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
    3326             :                            cmd + 16);
    3327           1 :                 cred = wpa_s->conf->cred;
    3328           5 :                 while (cred) {
    3329           3 :                         prev = cred;
    3330           3 :                         cred = cred->next;
    3331           6 :                         if (prev->provisioning_sp &&
    3332           3 :                             os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
    3333           2 :                                 wpas_ctrl_remove_cred(wpa_s, prev);
    3334             :                 }
    3335           1 :                 return 0;
    3336             :         }
    3337             : 
    3338         175 :         id = atoi(cmd);
    3339         175 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
    3340             : 
    3341         175 :         cred = wpa_config_get_cred(wpa_s->conf, id);
    3342         175 :         return wpas_ctrl_remove_cred(wpa_s, cred);
    3343             : }
    3344             : 
    3345             : 
    3346        1098 : static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
    3347             :                                               char *cmd)
    3348             : {
    3349             :         int id;
    3350             :         struct wpa_cred *cred;
    3351             :         char *name, *value;
    3352             : 
    3353             :         /* cmd: "<cred id> <variable name> <value>" */
    3354        1098 :         name = os_strchr(cmd, ' ');
    3355        1098 :         if (name == NULL)
    3356           1 :                 return -1;
    3357        1097 :         *name++ = '\0';
    3358             : 
    3359        1097 :         value = os_strchr(name, ' ');
    3360        1097 :         if (value == NULL)
    3361           1 :                 return -1;
    3362        1096 :         *value++ = '\0';
    3363             : 
    3364        1096 :         id = atoi(cmd);
    3365        1096 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
    3366             :                    id, name);
    3367        1096 :         wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
    3368             :                               (u8 *) value, os_strlen(value));
    3369             : 
    3370        1096 :         cred = wpa_config_get_cred(wpa_s->conf, id);
    3371        1096 :         if (cred == NULL) {
    3372           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
    3373             :                            id);
    3374           1 :                 return -1;
    3375             :         }
    3376             : 
    3377        1095 :         if (wpa_config_set_cred(cred, name, value, 0) < 0) {
    3378          27 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
    3379             :                            "variable '%s'", name);
    3380          27 :                 return -1;
    3381             :         }
    3382             : 
    3383        1068 :         wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
    3384             : 
    3385        1068 :         return 0;
    3386             : }
    3387             : 
    3388             : 
    3389          39 : static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
    3390             :                                               char *cmd, char *buf,
    3391             :                                               size_t buflen)
    3392             : {
    3393             :         int id;
    3394             :         size_t res;
    3395             :         struct wpa_cred *cred;
    3396             :         char *name, *value;
    3397             : 
    3398             :         /* cmd: "<cred id> <variable name>" */
    3399          39 :         name = os_strchr(cmd, ' ');
    3400          39 :         if (name == NULL)
    3401           1 :                 return -1;
    3402          38 :         *name++ = '\0';
    3403             : 
    3404          38 :         id = atoi(cmd);
    3405          38 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
    3406             :                    id, name);
    3407             : 
    3408          38 :         cred = wpa_config_get_cred(wpa_s->conf, id);
    3409          38 :         if (cred == NULL) {
    3410           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
    3411             :                            id);
    3412           1 :                 return -1;
    3413             :         }
    3414             : 
    3415          37 :         value = wpa_config_get_cred_no_key(cred, name);
    3416          37 :         if (value == NULL) {
    3417           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
    3418             :                            name);
    3419           1 :                 return -1;
    3420             :         }
    3421             : 
    3422          36 :         res = os_strlcpy(buf, value, buflen);
    3423          36 :         if (res >= buflen) {
    3424           0 :                 os_free(value);
    3425           0 :                 return -1;
    3426             :         }
    3427             : 
    3428          36 :         os_free(value);
    3429             : 
    3430          36 :         return res;
    3431             : }
    3432             : 
    3433             : 
    3434             : #ifndef CONFIG_NO_CONFIG_WRITE
    3435           7 : static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
    3436             : {
    3437             :         int ret;
    3438             : 
    3439           7 :         if (!wpa_s->conf->update_config) {
    3440           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
    3441             :                            "to update configuration (update_config=0)");
    3442           1 :                 return -1;
    3443             :         }
    3444             : 
    3445           6 :         ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
    3446           6 :         if (ret) {
    3447           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
    3448             :                            "update configuration");
    3449             :         } else {
    3450           5 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
    3451             :                            " updated");
    3452             :         }
    3453             : 
    3454           6 :         return ret;
    3455             : }
    3456             : #endif /* CONFIG_NO_CONFIG_WRITE */
    3457             : 
    3458             : 
    3459             : struct cipher_info {
    3460             :         unsigned int capa;
    3461             :         const char *name;
    3462             :         int group_only;
    3463             : };
    3464             : 
    3465             : static const struct cipher_info ciphers[] = {
    3466             :         { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
    3467             :         { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
    3468             :         { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
    3469             :         { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
    3470             :         { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
    3471             :         { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
    3472             :         { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
    3473             :         { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
    3474             : };
    3475             : 
    3476             : static const struct cipher_info ciphers_group_mgmt[] = {
    3477             :         { WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
    3478             :         { WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
    3479             :         { WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
    3480             :         { WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
    3481             : };
    3482             : 
    3483             : 
    3484          19 : static int ctrl_iface_get_capability_pairwise(int res, char *strict,
    3485             :                                               struct wpa_driver_capa *capa,
    3486             :                                               char *buf, size_t buflen)
    3487             : {
    3488             :         int ret;
    3489             :         char *pos, *end;
    3490             :         size_t len;
    3491             :         unsigned int i;
    3492             : 
    3493          19 :         pos = buf;
    3494          19 :         end = pos + buflen;
    3495             : 
    3496          19 :         if (res < 0) {
    3497           0 :                 if (strict)
    3498           0 :                         return 0;
    3499           0 :                 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
    3500           0 :                 if (len >= buflen)
    3501           0 :                         return -1;
    3502           0 :                 return len;
    3503             :         }
    3504             : 
    3505         171 :         for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
    3506         152 :                 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
    3507          95 :                         ret = os_snprintf(pos, end - pos, "%s%s",
    3508             :                                           pos == buf ? "" : " ",
    3509             :                                           ciphers[i].name);
    3510          95 :                         if (os_snprintf_error(end - pos, ret))
    3511           0 :                                 return pos - buf;
    3512          95 :                         pos += ret;
    3513             :                 }
    3514             :         }
    3515             : 
    3516          19 :         return pos - buf;
    3517             : }
    3518             : 
    3519             : 
    3520           1 : static int ctrl_iface_get_capability_group(int res, char *strict,
    3521             :                                            struct wpa_driver_capa *capa,
    3522             :                                            char *buf, size_t buflen)
    3523             : {
    3524             :         int ret;
    3525             :         char *pos, *end;
    3526             :         size_t len;
    3527             :         unsigned int i;
    3528             : 
    3529           1 :         pos = buf;
    3530           1 :         end = pos + buflen;
    3531             : 
    3532           1 :         if (res < 0) {
    3533           0 :                 if (strict)
    3534           0 :                         return 0;
    3535           0 :                 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
    3536           0 :                 if (len >= buflen)
    3537           0 :                         return -1;
    3538           0 :                 return len;
    3539             :         }
    3540             : 
    3541           9 :         for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
    3542           8 :                 if (capa->enc & ciphers[i].capa) {
    3543           7 :                         ret = os_snprintf(pos, end - pos, "%s%s",
    3544             :                                           pos == buf ? "" : " ",
    3545             :                                           ciphers[i].name);
    3546           7 :                         if (os_snprintf_error(end - pos, ret))
    3547           0 :                                 return pos - buf;
    3548           7 :                         pos += ret;
    3549             :                 }
    3550             :         }
    3551             : 
    3552           1 :         return pos - buf;
    3553             : }
    3554             : 
    3555             : 
    3556          12 : static int ctrl_iface_get_capability_group_mgmt(int res, char *strict,
    3557             :                                                 struct wpa_driver_capa *capa,
    3558             :                                                 char *buf, size_t buflen)
    3559             : {
    3560             :         int ret;
    3561             :         char *pos, *end;
    3562             :         unsigned int i;
    3563             : 
    3564          12 :         pos = buf;
    3565          12 :         end = pos + buflen;
    3566             : 
    3567          12 :         if (res < 0)
    3568           0 :                 return 0;
    3569             : 
    3570          60 :         for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
    3571          48 :                 if (capa->enc & ciphers_group_mgmt[i].capa) {
    3572          48 :                         ret = os_snprintf(pos, end - pos, "%s%s",
    3573             :                                           pos == buf ? "" : " ",
    3574             :                                           ciphers_group_mgmt[i].name);
    3575          48 :                         if (os_snprintf_error(end - pos, ret))
    3576           0 :                                 return pos - buf;
    3577          48 :                         pos += ret;
    3578             :                 }
    3579             :         }
    3580             : 
    3581          12 :         return pos - buf;
    3582             : }
    3583             : 
    3584             : 
    3585           9 : static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
    3586             :                                               struct wpa_driver_capa *capa,
    3587             :                                               char *buf, size_t buflen)
    3588             : {
    3589             :         int ret;
    3590             :         char *pos, *end;
    3591             :         size_t len;
    3592             : 
    3593           9 :         pos = buf;
    3594           9 :         end = pos + buflen;
    3595             : 
    3596           9 :         if (res < 0) {
    3597           0 :                 if (strict)
    3598           0 :                         return 0;
    3599           0 :                 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
    3600             :                                  "NONE", buflen);
    3601           0 :                 if (len >= buflen)
    3602           0 :                         return -1;
    3603           0 :                 return len;
    3604             :         }
    3605             : 
    3606           9 :         ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
    3607           9 :         if (os_snprintf_error(end - pos, ret))
    3608           0 :                 return pos - buf;
    3609           9 :         pos += ret;
    3610             : 
    3611           9 :         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
    3612             :                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
    3613           9 :                 ret = os_snprintf(pos, end - pos, " WPA-EAP");
    3614           9 :                 if (os_snprintf_error(end - pos, ret))
    3615           0 :                         return pos - buf;
    3616           9 :                 pos += ret;
    3617             :         }
    3618             : 
    3619           9 :         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
    3620             :                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
    3621           9 :                 ret = os_snprintf(pos, end - pos, " WPA-PSK");
    3622           9 :                 if (os_snprintf_error(end - pos, ret))
    3623           0 :                         return pos - buf;
    3624           9 :                 pos += ret;
    3625             :         }
    3626             : 
    3627           9 :         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
    3628           0 :                 ret = os_snprintf(pos, end - pos, " WPA-NONE");
    3629           0 :                 if (os_snprintf_error(end - pos, ret))
    3630           0 :                         return pos - buf;
    3631           0 :                 pos += ret;
    3632             :         }
    3633             : 
    3634             : #ifdef CONFIG_SUITEB
    3635           9 :         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
    3636           9 :                 ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
    3637           9 :                 if (os_snprintf_error(end - pos, ret))
    3638           0 :                         return pos - buf;
    3639           9 :                 pos += ret;
    3640             :         }
    3641             : #endif /* CONFIG_SUITEB */
    3642             : #ifdef CONFIG_SUITEB192
    3643           9 :         if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
    3644           9 :                 ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
    3645           9 :                 if (os_snprintf_error(end - pos, ret))
    3646           0 :                         return pos - buf;
    3647           9 :                 pos += ret;
    3648             :         }
    3649             : #endif /* CONFIG_SUITEB192 */
    3650             : 
    3651           9 :         return pos - buf;
    3652             : }
    3653             : 
    3654             : 
    3655           1 : static int ctrl_iface_get_capability_proto(int res, char *strict,
    3656             :                                            struct wpa_driver_capa *capa,
    3657             :                                            char *buf, size_t buflen)
    3658             : {
    3659             :         int ret;
    3660             :         char *pos, *end;
    3661             :         size_t len;
    3662             : 
    3663           1 :         pos = buf;
    3664           1 :         end = pos + buflen;
    3665             : 
    3666           1 :         if (res < 0) {
    3667           0 :                 if (strict)
    3668           0 :                         return 0;
    3669           0 :                 len = os_strlcpy(buf, "RSN WPA", buflen);
    3670           0 :                 if (len >= buflen)
    3671           0 :                         return -1;
    3672           0 :                 return len;
    3673             :         }
    3674             : 
    3675           1 :         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
    3676             :                               WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
    3677           1 :                 ret = os_snprintf(pos, end - pos, "%sRSN",
    3678             :                                   pos == buf ? "" : " ");
    3679           1 :                 if (os_snprintf_error(end - pos, ret))
    3680           0 :                         return pos - buf;
    3681           1 :                 pos += ret;
    3682             :         }
    3683             : 
    3684           1 :         if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
    3685             :                               WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
    3686           1 :                 ret = os_snprintf(pos, end - pos, "%sWPA",
    3687             :                                   pos == buf ? "" : " ");
    3688           1 :                 if (os_snprintf_error(end - pos, ret))
    3689           0 :                         return pos - buf;
    3690           1 :                 pos += ret;
    3691             :         }
    3692             : 
    3693           1 :         return pos - buf;
    3694             : }
    3695             : 
    3696             : 
    3697          56 : static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
    3698             :                                               int res, char *strict,
    3699             :                                               struct wpa_driver_capa *capa,
    3700             :                                               char *buf, size_t buflen)
    3701             : {
    3702             :         int ret;
    3703             :         char *pos, *end;
    3704             :         size_t len;
    3705             : 
    3706          56 :         pos = buf;
    3707          56 :         end = pos + buflen;
    3708             : 
    3709          56 :         if (res < 0) {
    3710           0 :                 if (strict)
    3711           0 :                         return 0;
    3712           0 :                 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
    3713           0 :                 if (len >= buflen)
    3714           0 :                         return -1;
    3715           0 :                 return len;
    3716             :         }
    3717             : 
    3718          56 :         if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
    3719          56 :                 ret = os_snprintf(pos, end - pos, "%sOPEN",
    3720             :                                   pos == buf ? "" : " ");
    3721          56 :                 if (os_snprintf_error(end - pos, ret))
    3722           0 :                         return pos - buf;
    3723          56 :                 pos += ret;
    3724             :         }
    3725             : 
    3726          56 :         if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
    3727          56 :                 ret = os_snprintf(pos, end - pos, "%sSHARED",
    3728             :                                   pos == buf ? "" : " ");
    3729          56 :                 if (os_snprintf_error(end - pos, ret))
    3730           0 :                         return pos - buf;
    3731          56 :                 pos += ret;
    3732             :         }
    3733             : 
    3734          56 :         if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
    3735          56 :                 ret = os_snprintf(pos, end - pos, "%sLEAP",
    3736             :                                   pos == buf ? "" : " ");
    3737          56 :                 if (os_snprintf_error(end - pos, ret))
    3738           0 :                         return pos - buf;
    3739          56 :                 pos += ret;
    3740             :         }
    3741             : 
    3742             : #ifdef CONFIG_SAE
    3743          56 :         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
    3744          56 :                 ret = os_snprintf(pos, end - pos, "%sSAE",
    3745             :                                   pos == buf ? "" : " ");
    3746          56 :                 if (os_snprintf_error(end - pos, ret))
    3747           0 :                         return pos - buf;
    3748          56 :                 pos += ret;
    3749             :         }
    3750             : #endif /* CONFIG_SAE */
    3751             : 
    3752          56 :         return pos - buf;
    3753             : }
    3754             : 
    3755             : 
    3756          58 : static int ctrl_iface_get_capability_modes(int res, char *strict,
    3757             :                                            struct wpa_driver_capa *capa,
    3758             :                                            char *buf, size_t buflen)
    3759             : {
    3760             :         int ret;
    3761             :         char *pos, *end;
    3762             :         size_t len;
    3763             : 
    3764          58 :         pos = buf;
    3765          58 :         end = pos + buflen;
    3766             : 
    3767          58 :         if (res < 0) {
    3768           0 :                 if (strict)
    3769           0 :                         return 0;
    3770           0 :                 len = os_strlcpy(buf, "IBSS AP", buflen);
    3771           0 :                 if (len >= buflen)
    3772           0 :                         return -1;
    3773           0 :                 return len;
    3774             :         }
    3775             : 
    3776          58 :         if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
    3777          58 :                 ret = os_snprintf(pos, end - pos, "%sIBSS",
    3778             :                                   pos == buf ? "" : " ");
    3779          58 :                 if (os_snprintf_error(end - pos, ret))
    3780           0 :                         return pos - buf;
    3781          58 :                 pos += ret;
    3782             :         }
    3783             : 
    3784          58 :         if (capa->flags & WPA_DRIVER_FLAGS_AP) {
    3785          58 :                 ret = os_snprintf(pos, end - pos, "%sAP",
    3786             :                                   pos == buf ? "" : " ");
    3787          58 :                 if (os_snprintf_error(end - pos, ret))
    3788           0 :                         return pos - buf;
    3789          58 :                 pos += ret;
    3790             :         }
    3791             : 
    3792             : #ifdef CONFIG_MESH
    3793          58 :         if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
    3794          58 :                 ret = os_snprintf(pos, end - pos, "%sMESH",
    3795             :                                   pos == buf ? "" : " ");
    3796          58 :                 if (os_snprintf_error(end - pos, ret))
    3797           0 :                         return pos - buf;
    3798          58 :                 pos += ret;
    3799             :         }
    3800             : #endif /* CONFIG_MESH */
    3801             : 
    3802          58 :         return pos - buf;
    3803             : }
    3804             : 
    3805             : 
    3806           1 : static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
    3807             :                                               char *buf, size_t buflen)
    3808             : {
    3809             :         struct hostapd_channel_data *chnl;
    3810             :         int ret, i, j;
    3811             :         char *pos, *end, *hmode;
    3812             : 
    3813           1 :         pos = buf;
    3814           1 :         end = pos + buflen;
    3815             : 
    3816           4 :         for (j = 0; j < wpa_s->hw.num_modes; j++) {
    3817           3 :                 switch (wpa_s->hw.modes[j].mode) {
    3818             :                 case HOSTAPD_MODE_IEEE80211B:
    3819           1 :                         hmode = "B";
    3820           1 :                         break;
    3821             :                 case HOSTAPD_MODE_IEEE80211G:
    3822           1 :                         hmode = "G";
    3823           1 :                         break;
    3824             :                 case HOSTAPD_MODE_IEEE80211A:
    3825           1 :                         hmode = "A";
    3826           1 :                         break;
    3827             :                 case HOSTAPD_MODE_IEEE80211AD:
    3828           0 :                         hmode = "AD";
    3829           0 :                         break;
    3830             :                 default:
    3831           0 :                         continue;
    3832             :                 }
    3833           3 :                 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
    3834           3 :                 if (os_snprintf_error(end - pos, ret))
    3835           0 :                         return pos - buf;
    3836           3 :                 pos += ret;
    3837           3 :                 chnl = wpa_s->hw.modes[j].channels;
    3838          55 :                 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
    3839          52 :                         if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
    3840           0 :                                 continue;
    3841          52 :                         ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
    3842          52 :                         if (os_snprintf_error(end - pos, ret))
    3843           0 :                                 return pos - buf;
    3844          52 :                         pos += ret;
    3845             :                 }
    3846           3 :                 ret = os_snprintf(pos, end - pos, "\n");
    3847           3 :                 if (os_snprintf_error(end - pos, ret))
    3848           0 :                         return pos - buf;
    3849           3 :                 pos += ret;
    3850             :         }
    3851             : 
    3852           1 :         return pos - buf;
    3853             : }
    3854             : 
    3855             : 
    3856           1 : static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
    3857             :                                           char *buf, size_t buflen)
    3858             : {
    3859             :         struct hostapd_channel_data *chnl;
    3860             :         int ret, i, j;
    3861             :         char *pos, *end, *hmode;
    3862             : 
    3863           1 :         pos = buf;
    3864           1 :         end = pos + buflen;
    3865             : 
    3866           4 :         for (j = 0; j < wpa_s->hw.num_modes; j++) {
    3867           3 :                 switch (wpa_s->hw.modes[j].mode) {
    3868             :                 case HOSTAPD_MODE_IEEE80211B:
    3869           1 :                         hmode = "B";
    3870           1 :                         break;
    3871             :                 case HOSTAPD_MODE_IEEE80211G:
    3872           1 :                         hmode = "G";
    3873           1 :                         break;
    3874             :                 case HOSTAPD_MODE_IEEE80211A:
    3875           1 :                         hmode = "A";
    3876           1 :                         break;
    3877             :                 case HOSTAPD_MODE_IEEE80211AD:
    3878           0 :                         hmode = "AD";
    3879           0 :                         break;
    3880             :                 default:
    3881           0 :                         continue;
    3882             :                 }
    3883           3 :                 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
    3884             :                                   hmode);
    3885           3 :                 if (os_snprintf_error(end - pos, ret))
    3886           0 :                         return pos - buf;
    3887           3 :                 pos += ret;
    3888           3 :                 chnl = wpa_s->hw.modes[j].channels;
    3889          55 :                 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
    3890          52 :                         if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
    3891           0 :                                 continue;
    3892         208 :                         ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
    3893         104 :                                           chnl[i].chan, chnl[i].freq,
    3894          52 :                                           chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
    3895             :                                           " (NO_IR)" : "",
    3896          52 :                                           chnl[i].flag & HOSTAPD_CHAN_RADAR ?
    3897             :                                           " (DFS)" : "");
    3898             : 
    3899          52 :                         if (os_snprintf_error(end - pos, ret))
    3900           0 :                                 return pos - buf;
    3901          52 :                         pos += ret;
    3902             :                 }
    3903           3 :                 ret = os_snprintf(pos, end - pos, "\n");
    3904           3 :                 if (os_snprintf_error(end - pos, ret))
    3905           0 :                         return pos - buf;
    3906           3 :                 pos += ret;
    3907             :         }
    3908             : 
    3909           1 :         return pos - buf;
    3910             : }
    3911             : 
    3912             : 
    3913         382 : static int wpa_supplicant_ctrl_iface_get_capability(
    3914             :         struct wpa_supplicant *wpa_s, const char *_field, char *buf,
    3915             :         size_t buflen)
    3916             : {
    3917             :         struct wpa_driver_capa capa;
    3918             :         int res;
    3919             :         char *strict;
    3920             :         char field[30];
    3921             :         size_t len;
    3922             : 
    3923             :         /* Determine whether or not strict checking was requested */
    3924         382 :         len = os_strlcpy(field, _field, sizeof(field));
    3925         382 :         if (len >= sizeof(field))
    3926           1 :                 return -1;
    3927         381 :         strict = os_strchr(field, ' ');
    3928         381 :         if (strict != NULL) {
    3929           2 :                 *strict++ = '\0';
    3930           2 :                 if (os_strcmp(strict, "strict") != 0)
    3931           1 :                         return -1;
    3932             :         }
    3933             : 
    3934         380 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
    3935             :                 field, strict ? strict : "");
    3936             : 
    3937         380 :         if (os_strcmp(field, "eap") == 0) {
    3938         161 :                 return eap_get_names(buf, buflen);
    3939             :         }
    3940             : 
    3941         219 :         res = wpa_drv_get_capa(wpa_s, &capa);
    3942             : 
    3943         219 :         if (os_strcmp(field, "pairwise") == 0)
    3944          19 :                 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
    3945             :                                                           buf, buflen);
    3946             : 
    3947         200 :         if (os_strcmp(field, "group") == 0)
    3948           1 :                 return ctrl_iface_get_capability_group(res, strict, &capa,
    3949             :                                                        buf, buflen);
    3950             : 
    3951         199 :         if (os_strcmp(field, "group_mgmt") == 0)
    3952          12 :                 return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
    3953             :                                                             buf, buflen);
    3954             : 
    3955         187 :         if (os_strcmp(field, "key_mgmt") == 0)
    3956           9 :                 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
    3957             :                                                           buf, buflen);
    3958             : 
    3959         178 :         if (os_strcmp(field, "proto") == 0)
    3960           1 :                 return ctrl_iface_get_capability_proto(res, strict, &capa,
    3961             :                                                        buf, buflen);
    3962             : 
    3963         177 :         if (os_strcmp(field, "auth_alg") == 0)
    3964          56 :                 return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
    3965             :                                                           &capa, buf, buflen);
    3966             : 
    3967         121 :         if (os_strcmp(field, "modes") == 0)
    3968          58 :                 return ctrl_iface_get_capability_modes(res, strict, &capa,
    3969             :                                                        buf, buflen);
    3970             : 
    3971          63 :         if (os_strcmp(field, "channels") == 0)
    3972           1 :                 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
    3973             : 
    3974          62 :         if (os_strcmp(field, "freq") == 0)
    3975           1 :                 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
    3976             : 
    3977             : #ifdef CONFIG_TDLS
    3978          61 :         if (os_strcmp(field, "tdls") == 0)
    3979           1 :                 return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
    3980             : #endif /* CONFIG_TDLS */
    3981             : 
    3982             : #ifdef CONFIG_ERP
    3983          60 :         if (os_strcmp(field, "erp") == 0) {
    3984          10 :                 res = os_snprintf(buf, buflen, "ERP");
    3985          10 :                 if (os_snprintf_error(buflen, res))
    3986           0 :                         return -1;
    3987          10 :                 return res;
    3988             :         }
    3989             : #endif /* CONFIG_EPR */
    3990             : 
    3991             : #ifdef CONFIG_FIPS
    3992             :         if (os_strcmp(field, "fips") == 0) {
    3993             :                 res = os_snprintf(buf, buflen, "FIPS");
    3994             :                 if (os_snprintf_error(buflen, res))
    3995             :                         return -1;
    3996             :                 return res;
    3997             :         }
    3998             : #endif /* CONFIG_FIPS */
    3999             : 
    4000             : #ifdef CONFIG_ACS
    4001          50 :         if (os_strcmp(field, "acs") == 0) {
    4002           1 :                 res = os_snprintf(buf, buflen, "ACS");
    4003           1 :                 if (os_snprintf_error(buflen, res))
    4004           0 :                         return -1;
    4005           1 :                 return res;
    4006             :         }
    4007             : #endif /* CONFIG_ACS */
    4008             : 
    4009          49 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
    4010             :                    field);
    4011             : 
    4012          49 :         return -1;
    4013             : }
    4014             : 
    4015             : 
    4016             : #ifdef CONFIG_INTERWORKING
    4017         916 : static char * anqp_add_hex(char *pos, char *end, const char *title,
    4018             :                            struct wpabuf *data)
    4019             : {
    4020         916 :         char *start = pos;
    4021             :         size_t i;
    4022             :         int ret;
    4023             :         const u8 *d;
    4024             : 
    4025         916 :         if (data == NULL)
    4026         625 :                 return start;
    4027             : 
    4028         291 :         ret = os_snprintf(pos, end - pos, "%s=", title);
    4029         291 :         if (os_snprintf_error(end - pos, ret))
    4030           0 :                 return start;
    4031         291 :         pos += ret;
    4032             : 
    4033         291 :         d = wpabuf_head_u8(data);
    4034        8130 :         for (i = 0; i < wpabuf_len(data); i++) {
    4035        7839 :                 ret = os_snprintf(pos, end - pos, "%02x", *d++);
    4036        7839 :                 if (os_snprintf_error(end - pos, ret))
    4037           0 :                         return start;
    4038        7839 :                 pos += ret;
    4039             :         }
    4040             : 
    4041         291 :         ret = os_snprintf(pos, end - pos, "\n");
    4042         291 :         if (os_snprintf_error(end - pos, ret))
    4043           0 :                 return start;
    4044         291 :         pos += ret;
    4045             : 
    4046         291 :         return pos;
    4047             : }
    4048             : #endif /* CONFIG_INTERWORKING */
    4049             : 
    4050             : 
    4051        2507 : static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
    4052             :                           unsigned long mask, char *buf, size_t buflen)
    4053             : {
    4054             :         size_t i;
    4055             :         int ret;
    4056             :         char *pos, *end;
    4057             :         const u8 *ie, *ie2, *osen_ie;
    4058             : 
    4059        2507 :         pos = buf;
    4060        2507 :         end = buf + buflen;
    4061             : 
    4062        2507 :         if (mask & WPA_BSS_MASK_ID) {
    4063        2366 :                 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
    4064        2366 :                 if (os_snprintf_error(end - pos, ret))
    4065           0 :                         return 0;
    4066        2366 :                 pos += ret;
    4067             :         }
    4068             : 
    4069        2507 :         if (mask & WPA_BSS_MASK_BSSID) {
    4070       14898 :                 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
    4071       14898 :                                   MAC2STR(bss->bssid));
    4072        2483 :                 if (os_snprintf_error(end - pos, ret))
    4073           0 :                         return 0;
    4074        2483 :                 pos += ret;
    4075             :         }
    4076             : 
    4077        2507 :         if (mask & WPA_BSS_MASK_FREQ) {
    4078        2342 :                 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
    4079        2342 :                 if (os_snprintf_error(end - pos, ret))
    4080           0 :                         return 0;
    4081        2342 :                 pos += ret;
    4082             :         }
    4083             : 
    4084        2507 :         if (mask & WPA_BSS_MASK_BEACON_INT) {
    4085        2342 :                 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
    4086        2342 :                                   bss->beacon_int);
    4087        2342 :                 if (os_snprintf_error(end - pos, ret))
    4088           0 :                         return 0;
    4089        2342 :                 pos += ret;
    4090             :         }
    4091             : 
    4092        2507 :         if (mask & WPA_BSS_MASK_CAPABILITIES) {
    4093        2342 :                 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
    4094        2342 :                                   bss->caps);
    4095        2342 :                 if (os_snprintf_error(end - pos, ret))
    4096           0 :                         return 0;
    4097        2342 :                 pos += ret;
    4098             :         }
    4099             : 
    4100        2507 :         if (mask & WPA_BSS_MASK_QUAL) {
    4101        2342 :                 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
    4102        2342 :                 if (os_snprintf_error(end - pos, ret))
    4103           0 :                         return 0;
    4104        2342 :                 pos += ret;
    4105             :         }
    4106             : 
    4107        2507 :         if (mask & WPA_BSS_MASK_NOISE) {
    4108        2342 :                 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
    4109        2342 :                 if (os_snprintf_error(end - pos, ret))
    4110           0 :                         return 0;
    4111        2342 :                 pos += ret;
    4112             :         }
    4113             : 
    4114        2507 :         if (mask & WPA_BSS_MASK_LEVEL) {
    4115        2342 :                 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
    4116        2342 :                 if (os_snprintf_error(end - pos, ret))
    4117           0 :                         return 0;
    4118        2342 :                 pos += ret;
    4119             :         }
    4120             : 
    4121        2507 :         if (mask & WPA_BSS_MASK_TSF) {
    4122        2342 :                 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
    4123        2342 :                                   (unsigned long long) bss->tsf);
    4124        2342 :                 if (os_snprintf_error(end - pos, ret))
    4125           0 :                         return 0;
    4126        2342 :                 pos += ret;
    4127             :         }
    4128             : 
    4129        2507 :         if (mask & WPA_BSS_MASK_AGE) {
    4130             :                 struct os_reltime now;
    4131             : 
    4132        2342 :                 os_get_reltime(&now);
    4133        4684 :                 ret = os_snprintf(pos, end - pos, "age=%d\n",
    4134        4684 :                                   (int) (now.sec - bss->last_update.sec));
    4135        2342 :                 if (os_snprintf_error(end - pos, ret))
    4136           0 :                         return 0;
    4137        2342 :                 pos += ret;
    4138             :         }
    4139             : 
    4140        2507 :         if (mask & WPA_BSS_MASK_IE) {
    4141        2342 :                 ret = os_snprintf(pos, end - pos, "ie=");
    4142        2342 :                 if (os_snprintf_error(end - pos, ret))
    4143           0 :                         return 0;
    4144        2342 :                 pos += ret;
    4145             : 
    4146        2342 :                 ie = (const u8 *) (bss + 1);
    4147      559557 :                 for (i = 0; i < bss->ie_len; i++) {
    4148      557215 :                         ret = os_snprintf(pos, end - pos, "%02x", *ie++);
    4149      557215 :                         if (os_snprintf_error(end - pos, ret))
    4150           0 :                                 return 0;
    4151      557215 :                         pos += ret;
    4152             :                 }
    4153             : 
    4154        2342 :                 ret = os_snprintf(pos, end - pos, "\n");
    4155        2342 :                 if (os_snprintf_error(end - pos, ret))
    4156           0 :                         return 0;
    4157        2342 :                 pos += ret;
    4158             :         }
    4159             : 
    4160        2507 :         if (mask & WPA_BSS_MASK_FLAGS) {
    4161        2342 :                 ret = os_snprintf(pos, end - pos, "flags=");
    4162        2342 :                 if (os_snprintf_error(end - pos, ret))
    4163           0 :                         return 0;
    4164        2342 :                 pos += ret;
    4165             : 
    4166        2342 :                 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
    4167        2342 :                 if (ie)
    4168          26 :                         pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
    4169          26 :                                                     2 + ie[1]);
    4170        2342 :                 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
    4171        2342 :                 if (ie2)
    4172        1871 :                         pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
    4173        1871 :                                                     2 + ie2[1]);
    4174        2342 :                 osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
    4175        2342 :                 if (osen_ie)
    4176           1 :                         pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
    4177           1 :                                                     osen_ie, 2 + osen_ie[1]);
    4178        2342 :                 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
    4179        2810 :                 if (!ie && !ie2 && !osen_ie &&
    4180         468 :                     (bss->caps & IEEE80211_CAP_PRIVACY)) {
    4181           2 :                         ret = os_snprintf(pos, end - pos, "[WEP]");
    4182           2 :                         if (os_snprintf_error(end - pos, ret))
    4183           0 :                                 return 0;
    4184           2 :                         pos += ret;
    4185             :                 }
    4186        2342 :                 if (bss_is_dmg(bss)) {
    4187             :                         const char *s;
    4188           0 :                         ret = os_snprintf(pos, end - pos, "[DMG]");
    4189           0 :                         if (os_snprintf_error(end - pos, ret))
    4190           0 :                                 return 0;
    4191           0 :                         pos += ret;
    4192           0 :                         switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
    4193             :                         case IEEE80211_CAP_DMG_IBSS:
    4194           0 :                                 s = "[IBSS]";
    4195           0 :                                 break;
    4196             :                         case IEEE80211_CAP_DMG_AP:
    4197           0 :                                 s = "[ESS]";
    4198           0 :                                 break;
    4199             :                         case IEEE80211_CAP_DMG_PBSS:
    4200           0 :                                 s = "[PBSS]";
    4201           0 :                                 break;
    4202             :                         default:
    4203           0 :                                 s = "";
    4204           0 :                                 break;
    4205             :                         }
    4206           0 :                         ret = os_snprintf(pos, end - pos, "%s", s);
    4207           0 :                         if (os_snprintf_error(end - pos, ret))
    4208           0 :                                 return 0;
    4209           0 :                         pos += ret;
    4210             :                 } else {
    4211        2342 :                         if (bss->caps & IEEE80211_CAP_IBSS) {
    4212           5 :                                 ret = os_snprintf(pos, end - pos, "[IBSS]");
    4213           5 :                                 if (os_snprintf_error(end - pos, ret))
    4214           0 :                                         return 0;
    4215           5 :                                 pos += ret;
    4216             :                         }
    4217        2342 :                         if (bss->caps & IEEE80211_CAP_ESS) {
    4218        2333 :                                 ret = os_snprintf(pos, end - pos, "[ESS]");
    4219        2333 :                                 if (os_snprintf_error(end - pos, ret))
    4220           0 :                                         return 0;
    4221        2333 :                                 pos += ret;
    4222             :                         }
    4223             :                 }
    4224        4653 :                 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
    4225        2311 :                     wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
    4226          31 :                         ret = os_snprintf(pos, end - pos, "[P2P]");
    4227          31 :                         if (os_snprintf_error(end - pos, ret))
    4228           0 :                                 return 0;
    4229          31 :                         pos += ret;
    4230             :                 }
    4231             : #ifdef CONFIG_HS20
    4232        2342 :                 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
    4233         258 :                         ret = os_snprintf(pos, end - pos, "[HS20]");
    4234         258 :                         if (os_snprintf_error(end - pos, ret))
    4235           0 :                                 return 0;
    4236         258 :                         pos += ret;
    4237             :                 }
    4238             : #endif /* CONFIG_HS20 */
    4239             : 
    4240        2342 :                 ret = os_snprintf(pos, end - pos, "\n");
    4241        2342 :                 if (os_snprintf_error(end - pos, ret))
    4242           0 :                         return 0;
    4243        2342 :                 pos += ret;
    4244             :         }
    4245             : 
    4246        2507 :         if (mask & WPA_BSS_MASK_SSID) {
    4247        4684 :                 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
    4248        2342 :                                   wpa_ssid_txt(bss->ssid, bss->ssid_len));
    4249        2342 :                 if (os_snprintf_error(end - pos, ret))
    4250           0 :                         return 0;
    4251        2342 :                 pos += ret;
    4252             :         }
    4253             : 
    4254             : #ifdef CONFIG_WPS
    4255        2507 :         if (mask & WPA_BSS_MASK_WPS_SCAN) {
    4256        2342 :                 ie = (const u8 *) (bss + 1);
    4257        2342 :                 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
    4258        2342 :                 if (ret >= end - pos)
    4259           0 :                         return 0;
    4260        2342 :                 if (ret > 0)
    4261        1501 :                         pos += ret;
    4262             :         }
    4263             : #endif /* CONFIG_WPS */
    4264             : 
    4265             : #ifdef CONFIG_P2P
    4266        2507 :         if (mask & WPA_BSS_MASK_P2P_SCAN) {
    4267        2342 :                 ie = (const u8 *) (bss + 1);
    4268        2342 :                 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
    4269        2342 :                 if (ret >= end - pos)
    4270           0 :                         return 0;
    4271        2342 :                 if (ret > 0)
    4272          31 :                         pos += ret;
    4273             :         }
    4274             : #endif /* CONFIG_P2P */
    4275             : 
    4276             : #ifdef CONFIG_WIFI_DISPLAY
    4277        2507 :         if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
    4278             :                 struct wpabuf *wfd;
    4279        2342 :                 ie = (const u8 *) (bss + 1);
    4280        2342 :                 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
    4281             :                                                   WFD_IE_VENDOR_TYPE);
    4282        2342 :                 if (wfd) {
    4283           5 :                         ret = os_snprintf(pos, end - pos, "wfd_subelems=");
    4284           5 :                         if (os_snprintf_error(end - pos, ret)) {
    4285           0 :                                 wpabuf_free(wfd);
    4286           0 :                                 return 0;
    4287             :                         }
    4288           5 :                         pos += ret;
    4289             : 
    4290          10 :                         pos += wpa_snprintf_hex(pos, end - pos,
    4291           5 :                                                 wpabuf_head(wfd),
    4292             :                                                 wpabuf_len(wfd));
    4293           5 :                         wpabuf_free(wfd);
    4294             : 
    4295           5 :                         ret = os_snprintf(pos, end - pos, "\n");
    4296           5 :                         if (os_snprintf_error(end - pos, ret))
    4297           0 :                                 return 0;
    4298           5 :                         pos += ret;
    4299             :                 }
    4300             :         }
    4301             : #endif /* CONFIG_WIFI_DISPLAY */
    4302             : 
    4303             : #ifdef CONFIG_INTERWORKING
    4304        2507 :         if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
    4305          65 :                 struct wpa_bss_anqp *anqp = bss->anqp;
    4306             :                 struct wpa_bss_anqp_elem *elem;
    4307             : 
    4308          65 :                 pos = anqp_add_hex(pos, end, "anqp_capability_list",
    4309             :                                    anqp->capability_list);
    4310          65 :                 pos = anqp_add_hex(pos, end, "anqp_venue_name",
    4311             :                                    anqp->venue_name);
    4312          65 :                 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
    4313             :                                    anqp->network_auth_type);
    4314          65 :                 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
    4315             :                                    anqp->roaming_consortium);
    4316          65 :                 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
    4317             :                                    anqp->ip_addr_type_availability);
    4318          65 :                 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
    4319             :                                    anqp->nai_realm);
    4320          65 :                 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
    4321          65 :                 pos = anqp_add_hex(pos, end, "anqp_domain_name",
    4322             :                                    anqp->domain_name);
    4323             : #ifdef CONFIG_HS20
    4324          65 :                 pos = anqp_add_hex(pos, end, "hs20_capability_list",
    4325             :                                    anqp->hs20_capability_list);
    4326          65 :                 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
    4327             :                                    anqp->hs20_operator_friendly_name);
    4328          65 :                 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
    4329             :                                    anqp->hs20_wan_metrics);
    4330          65 :                 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
    4331             :                                    anqp->hs20_connection_capability);
    4332          65 :                 pos = anqp_add_hex(pos, end, "hs20_operating_class",
    4333             :                                    anqp->hs20_operating_class);
    4334          65 :                 pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
    4335             :                                    anqp->hs20_osu_providers_list);
    4336             : #endif /* CONFIG_HS20 */
    4337             : 
    4338          71 :                 dl_list_for_each(elem, &anqp->anqp_elems,
    4339             :                                  struct wpa_bss_anqp_elem, list) {
    4340             :                         char title[20];
    4341             : 
    4342           6 :                         os_snprintf(title, sizeof(title), "anqp[%u]",
    4343           6 :                                     elem->infoid);
    4344           6 :                         pos = anqp_add_hex(pos, end, title, elem->payload);
    4345             :                 }
    4346             :         }
    4347             : #endif /* CONFIG_INTERWORKING */
    4348             : 
    4349             : #ifdef CONFIG_MESH
    4350        2507 :         if (mask & WPA_BSS_MASK_MESH_SCAN) {
    4351        2342 :                 ie = (const u8 *) (bss + 1);
    4352        2342 :                 ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
    4353        2342 :                 if (ret >= end - pos)
    4354           0 :                         return 0;
    4355        2342 :                 if (ret > 0)
    4356           2 :                         pos += ret;
    4357             :         }
    4358             : #endif /* CONFIG_MESH */
    4359             : 
    4360        2507 :         if (mask & WPA_BSS_MASK_SNR) {
    4361        2342 :                 ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
    4362        2342 :                 if (os_snprintf_error(end - pos, ret))
    4363           0 :                         return 0;
    4364        2342 :                 pos += ret;
    4365             :         }
    4366             : 
    4367        2507 :         if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
    4368        2342 :                 ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
    4369             :                                   bss->est_throughput);
    4370        2342 :                 if (os_snprintf_error(end - pos, ret))
    4371           0 :                         return 0;
    4372        2342 :                 pos += ret;
    4373             :         }
    4374             : 
    4375             : #ifdef CONFIG_FST
    4376        2507 :         if (mask & WPA_BSS_MASK_FST) {
    4377        2342 :                 ret = fst_ctrl_iface_mb_info(bss->bssid, pos, end - pos);
    4378        2342 :                 if (ret < 0 || ret >= end - pos)
    4379           0 :                         return 0;
    4380        2342 :                 pos += ret;
    4381             :         }
    4382             : #endif /* CONFIG_FST */
    4383             : 
    4384        2507 :         if (mask & WPA_BSS_MASK_DELIM) {
    4385           2 :                 ret = os_snprintf(pos, end - pos, "====\n");
    4386           2 :                 if (os_snprintf_error(end - pos, ret))
    4387           0 :                         return 0;
    4388           2 :                 pos += ret;
    4389             :         }
    4390             : 
    4391        2507 :         return pos - buf;
    4392             : }
    4393             : 
    4394             : 
    4395        2989 : static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
    4396             :                                          const char *cmd, char *buf,
    4397             :                                          size_t buflen)
    4398             : {
    4399             :         u8 bssid[ETH_ALEN];
    4400             :         size_t i;
    4401             :         struct wpa_bss *bss;
    4402        2989 :         struct wpa_bss *bsslast = NULL;
    4403             :         struct dl_list *next;
    4404        2989 :         int ret = 0;
    4405             :         int len;
    4406        2989 :         char *ctmp, *end = buf + buflen;
    4407        2989 :         unsigned long mask = WPA_BSS_MASK_ALL;
    4408             : 
    4409        2989 :         if (os_strncmp(cmd, "RANGE=", 6) == 0) {
    4410         113 :                 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
    4411         104 :                         bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
    4412             :                                             list_id);
    4413         104 :                         bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
    4414             :                                                list_id);
    4415             :                 } else { /* N1-N2 */
    4416             :                         unsigned int id1, id2;
    4417             : 
    4418           9 :                         if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
    4419           1 :                                 wpa_printf(MSG_INFO, "Wrong BSS range "
    4420             :                                            "format");
    4421           1 :                                 return 0;
    4422             :                         }
    4423             : 
    4424           8 :                         if (*(cmd + 6) == '-')
    4425           1 :                                 id1 = 0;
    4426             :                         else
    4427           7 :                                 id1 = atoi(cmd + 6);
    4428           8 :                         ctmp++;
    4429           8 :                         if (*ctmp >= '0' && *ctmp <= '9')
    4430           6 :                                 id2 = atoi(ctmp);
    4431             :                         else
    4432           2 :                                 id2 = (unsigned int) -1;
    4433           8 :                         bss = wpa_bss_get_id_range(wpa_s, id1, id2);
    4434           8 :                         if (id2 == (unsigned int) -1)
    4435           2 :                                 bsslast = dl_list_last(&wpa_s->bss_id,
    4436             :                                                        struct wpa_bss,
    4437             :                                                        list_id);
    4438             :                         else {
    4439           6 :                                 bsslast = wpa_bss_get_id(wpa_s, id2);
    4440           6 :                                 if (bsslast == NULL && bss && id2 > id1) {
    4441           1 :                                         struct wpa_bss *tmp = bss;
    4442             :                                         for (;;) {
    4443           2 :                                                 next = tmp->list_id.next;
    4444           2 :                                                 if (next == &wpa_s->bss_id)
    4445           1 :                                                         break;
    4446           1 :                                                 tmp = dl_list_entry(
    4447             :                                                         next, struct wpa_bss,
    4448             :                                                         list_id);
    4449           1 :                                                 if (tmp->id > id2)
    4450           0 :                                                         break;
    4451           1 :                                                 bsslast = tmp;
    4452           1 :                                         }
    4453             :                                 }
    4454             :                         }
    4455             :                 }
    4456        2876 :         } else if (os_strncmp(cmd, "FIRST", 5) == 0)
    4457           2 :                 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
    4458        2874 :         else if (os_strncmp(cmd, "LAST", 4) == 0)
    4459           2 :                 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
    4460        2872 :         else if (os_strncmp(cmd, "ID-", 3) == 0) {
    4461           2 :                 i = atoi(cmd + 3);
    4462           2 :                 bss = wpa_bss_get_id(wpa_s, i);
    4463        2870 :         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
    4464           2 :                 i = atoi(cmd + 5);
    4465           2 :                 bss = wpa_bss_get_id(wpa_s, i);
    4466           2 :                 if (bss) {
    4467           2 :                         next = bss->list_id.next;
    4468           2 :                         if (next == &wpa_s->bss_id)
    4469           1 :                                 bss = NULL;
    4470             :                         else
    4471           1 :                                 bss = dl_list_entry(next, struct wpa_bss,
    4472             :                                                     list_id);
    4473             :                 }
    4474             : #ifdef CONFIG_P2P
    4475        2868 :         } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
    4476           5 :                 if (hwaddr_aton(cmd + 13, bssid) == 0)
    4477           4 :                         bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
    4478             :                 else
    4479           1 :                         bss = NULL;
    4480             : #endif /* CONFIG_P2P */
    4481        2863 :         } else if (hwaddr_aton(cmd, bssid) == 0)
    4482        2619 :                 bss = wpa_bss_get_bssid(wpa_s, bssid);
    4483             :         else {
    4484             :                 struct wpa_bss *tmp;
    4485         244 :                 i = atoi(cmd);
    4486         244 :                 bss = NULL;
    4487         244 :                 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
    4488             :                 {
    4489         244 :                         if (i-- == 0) {
    4490         244 :                                 bss = tmp;
    4491         244 :                                 break;
    4492             :                         }
    4493             :                 }
    4494             :         }
    4495             : 
    4496        2988 :         if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
    4497         101 :                 mask = strtoul(ctmp + 5, NULL, 0x10);
    4498         101 :                 if (mask == 0)
    4499           1 :                         mask = WPA_BSS_MASK_ALL;
    4500             :         }
    4501             : 
    4502        2988 :         if (bss == NULL)
    4503         569 :                 return 0;
    4504             : 
    4505        2419 :         if (bsslast == NULL)
    4506        2323 :                 bsslast = bss;
    4507             :         do {
    4508        2507 :                 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
    4509        2507 :                 ret += len;
    4510        2507 :                 buf += len;
    4511        2507 :                 buflen -= len;
    4512        2507 :                 if (bss == bsslast) {
    4513        2420 :                         if ((mask & WPA_BSS_MASK_DELIM) && len &&
    4514           1 :                             (bss == dl_list_last(&wpa_s->bss_id,
    4515             :                                                  struct wpa_bss, list_id))) {
    4516             :                                 int res;
    4517             : 
    4518           1 :                                 res = os_snprintf(buf - 5, end - buf + 5,
    4519             :                                                   "####\n");
    4520           1 :                                 if (os_snprintf_error(end - buf + 5, res)) {
    4521           0 :                                         wpa_printf(MSG_DEBUG,
    4522             :                                                    "Could not add end delim");
    4523             :                                 }
    4524             :                         }
    4525        2419 :                         break;
    4526             :                 }
    4527          88 :                 next = bss->list_id.next;
    4528          88 :                 if (next == &wpa_s->bss_id)
    4529           0 :                         break;
    4530          88 :                 bss = dl_list_entry(next, struct wpa_bss, list_id);
    4531          88 :         } while (bss && len);
    4532             : 
    4533        2419 :         return ret;
    4534             : }
    4535             : 
    4536             : 
    4537          17 : static int wpa_supplicant_ctrl_iface_ap_scan(
    4538             :         struct wpa_supplicant *wpa_s, char *cmd)
    4539             : {
    4540          17 :         int ap_scan = atoi(cmd);
    4541          17 :         return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
    4542             : }
    4543             : 
    4544             : 
    4545          25 : static int wpa_supplicant_ctrl_iface_scan_interval(
    4546             :         struct wpa_supplicant *wpa_s, char *cmd)
    4547             : {
    4548          25 :         int scan_int = atoi(cmd);
    4549          25 :         return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
    4550             : }
    4551             : 
    4552             : 
    4553           3 : static int wpa_supplicant_ctrl_iface_bss_expire_age(
    4554             :         struct wpa_supplicant *wpa_s, char *cmd)
    4555             : {
    4556           3 :         int expire_age = atoi(cmd);
    4557           3 :         return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
    4558             : }
    4559             : 
    4560             : 
    4561           4 : static int wpa_supplicant_ctrl_iface_bss_expire_count(
    4562             :         struct wpa_supplicant *wpa_s, char *cmd)
    4563             : {
    4564           4 :         int expire_count = atoi(cmd);
    4565           4 :         return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
    4566             : }
    4567             : 
    4568             : 
    4569        1072 : static void wpa_supplicant_ctrl_iface_bss_flush(
    4570             :         struct wpa_supplicant *wpa_s, char *cmd)
    4571             : {
    4572        1072 :         int flush_age = atoi(cmd);
    4573             : 
    4574        1072 :         if (flush_age == 0)
    4575        1071 :                 wpa_bss_flush(wpa_s);
    4576             :         else
    4577           1 :                 wpa_bss_flush_by_age(wpa_s, flush_age);
    4578        1072 : }
    4579             : 
    4580             : 
    4581             : #ifdef CONFIG_TESTING_OPTIONS
    4582           2 : static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
    4583             : {
    4584           2 :         wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
    4585             :         /* MLME-DELETEKEYS.request */
    4586           2 :         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
    4587           2 :         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
    4588           2 :         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
    4589           2 :         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
    4590             : #ifdef CONFIG_IEEE80211W
    4591           2 :         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
    4592           2 :         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
    4593             : #endif /* CONFIG_IEEE80211W */
    4594             : 
    4595           2 :         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
    4596             :                         0);
    4597             :         /* MLME-SETPROTECTION.request(None) */
    4598           2 :         wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
    4599             :                                    MLME_SETPROTECTION_PROTECT_TYPE_NONE,
    4600             :                                    MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
    4601           2 :         wpa_sm_drop_sa(wpa_s->wpa);
    4602           2 : }
    4603             : #endif /* CONFIG_TESTING_OPTIONS */
    4604             : 
    4605             : 
    4606         185 : static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
    4607             :                                           char *addr)
    4608             : {
    4609             : #ifdef CONFIG_NO_SCAN_PROCESSING
    4610             :         return -1;
    4611             : #else /* CONFIG_NO_SCAN_PROCESSING */
    4612             :         u8 bssid[ETH_ALEN];
    4613             :         struct wpa_bss *bss;
    4614         185 :         struct wpa_ssid *ssid = wpa_s->current_ssid;
    4615             : 
    4616         185 :         if (hwaddr_aton(addr, bssid)) {
    4617           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
    4618             :                            "address '%s'", addr);
    4619           1 :                 return -1;
    4620             :         }
    4621             : 
    4622         184 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
    4623             : 
    4624         184 :         if (!ssid) {
    4625           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
    4626             :                            "configuration known for the target AP");
    4627           1 :                 return -1;
    4628             :         }
    4629             : 
    4630         183 :         bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
    4631         183 :         if (!bss) {
    4632           1 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
    4633             :                            "from BSS table");
    4634           1 :                 return -1;
    4635             :         }
    4636             : 
    4637             :         /*
    4638             :          * TODO: Find best network configuration block from configuration to
    4639             :          * allow roaming to other networks
    4640             :          */
    4641             : 
    4642         182 :         wpa_s->reassociate = 1;
    4643         182 :         wpa_supplicant_connect(wpa_s, bss, ssid);
    4644             : 
    4645         182 :         return 0;
    4646             : #endif /* CONFIG_NO_SCAN_PROCESSING */
    4647             : }
    4648             : 
    4649             : 
    4650             : #ifdef CONFIG_P2P
    4651         493 : static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
    4652             : {
    4653         493 :         unsigned int timeout = atoi(cmd);
    4654         493 :         enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
    4655         493 :         u8 dev_id[ETH_ALEN], *_dev_id = NULL;
    4656         493 :         u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
    4657             :         char *pos;
    4658             :         unsigned int search_delay;
    4659         493 :         const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
    4660         493 :         u8 seek_count = 0;
    4661         493 :         int freq = 0;
    4662             : 
    4663         493 :         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
    4664           5 :                 wpa_dbg(wpa_s, MSG_INFO,
    4665             :                         "Reject P2P_FIND since interface is disabled");
    4666           5 :                 return -1;
    4667             :         }
    4668         488 :         if (os_strstr(cmd, "type=social"))
    4669         449 :                 type = P2P_FIND_ONLY_SOCIAL;
    4670          39 :         else if (os_strstr(cmd, "type=progressive"))
    4671           1 :                 type = P2P_FIND_PROGRESSIVE;
    4672             : 
    4673         488 :         pos = os_strstr(cmd, "dev_id=");
    4674         488 :         if (pos) {
    4675           5 :                 pos += 7;
    4676           5 :                 if (hwaddr_aton(pos, dev_id))
    4677           1 :                         return -1;
    4678           4 :                 _dev_id = dev_id;
    4679             :         }
    4680             : 
    4681         487 :         pos = os_strstr(cmd, "dev_type=");
    4682         487 :         if (pos) {
    4683           7 :                 pos += 9;
    4684           7 :                 if (wps_dev_type_str2bin(pos, dev_type) < 0)
    4685           3 :                         return -1;
    4686           4 :                 _dev_type = dev_type;
    4687             :         }
    4688             : 
    4689         484 :         pos = os_strstr(cmd, "delay=");
    4690         484 :         if (pos) {
    4691           1 :                 pos += 6;
    4692           1 :                 search_delay = atoi(pos);
    4693             :         } else
    4694         483 :                 search_delay = wpas_p2p_search_delay(wpa_s);
    4695             : 
    4696         484 :         pos = os_strstr(cmd, "freq=");
    4697         484 :         if (pos) {
    4698          42 :                 pos += 5;
    4699          42 :                 freq = atoi(pos);
    4700          42 :                 if (freq <= 0)
    4701           0 :                         return -1;
    4702             :         }
    4703             : 
    4704             :         /* Must be searched for last, because it adds nul termination */
    4705         484 :         pos = os_strstr(cmd, " seek=");
    4706         484 :         if (pos)
    4707          57 :                 pos += 6;
    4708         981 :         while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
    4709             :                 char *term;
    4710             : 
    4711          70 :                 _seek[seek_count++] = pos;
    4712          70 :                 seek = _seek;
    4713          70 :                 term = os_strchr(pos, ' ');
    4714          70 :                 if (!term)
    4715          57 :                         break;
    4716          13 :                 *term = '\0';
    4717          13 :                 pos = os_strstr(term + 1, "seek=");
    4718          13 :                 if (pos)
    4719          13 :                         pos += 5;
    4720             :         }
    4721         484 :         if (seek_count > P2P_MAX_QUERY_HASH) {
    4722           1 :                 seek[0] = NULL;
    4723           1 :                 seek_count = 1;
    4724             :         }
    4725             : 
    4726         484 :         return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
    4727             :                              _dev_id, search_delay, seek_count, seek, freq);
    4728             : }
    4729             : 
    4730             : 
    4731          15 : static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
    4732             : {
    4733          15 :         const char *last = NULL;
    4734             :         const char *token;
    4735             :         long int token_len;
    4736             :         unsigned int i;
    4737             : 
    4738             :         /* Expected predefined CPT names delimited by ':' */
    4739          42 :         for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
    4740          27 :                 if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
    4741           0 :                         wpa_printf(MSG_ERROR,
    4742             :                                    "P2PS: CPT name list is too long, expected up to %d names",
    4743             :                                    P2PS_FEATURE_CAPAB_CPT_MAX);
    4744           0 :                         cpt[0] = 0;
    4745           0 :                         return -1;
    4746             :                 }
    4747             : 
    4748          27 :                 token_len = last - token;
    4749             : 
    4750          54 :                 if (token_len  == 3 &&
    4751          27 :                     os_memcmp(token, "UDP", token_len) == 0) {
    4752          12 :                         cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
    4753          30 :                 } else if (token_len == 3 &&
    4754          15 :                            os_memcmp(token, "MAC", token_len) == 0) {
    4755          15 :                         cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
    4756             :                 } else {
    4757           0 :                         wpa_printf(MSG_ERROR,
    4758             :                                    "P2PS: Unsupported CPT name '%s'", token);
    4759           0 :                         cpt[0] = 0;
    4760           0 :                         return -1;
    4761             :                 }
    4762             : 
    4763          27 :                 if (isblank((unsigned char) *last)) {
    4764           0 :                         i++;
    4765           0 :                         break;
    4766             :                 }
    4767             :         }
    4768          15 :         cpt[i] = 0;
    4769          15 :         return 0;
    4770             : }
    4771             : 
    4772             : 
    4773          53 : static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
    4774             : {
    4775             :         struct p2ps_provision *p2ps_prov;
    4776             :         char *pos;
    4777          53 :         size_t info_len = 0;
    4778          53 :         char *info = NULL;
    4779          53 :         u8 role = P2PS_SETUP_NONE;
    4780             :         long long unsigned val;
    4781             :         int i;
    4782             : 
    4783          53 :         pos = os_strstr(cmd, "info=");
    4784          53 :         if (pos) {
    4785          40 :                 pos += 5;
    4786          40 :                 info_len = os_strlen(pos);
    4787             : 
    4788          40 :                 if (info_len) {
    4789          40 :                         info = os_malloc(info_len + 1);
    4790          40 :                         if (info) {
    4791          40 :                                 info_len = utf8_unescape(pos, info_len,
    4792             :                                                          info, info_len + 1);
    4793             :                         } else
    4794           0 :                                 info_len = 0;
    4795             :                 }
    4796             :         }
    4797             : 
    4798          53 :         p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
    4799          53 :         if (p2ps_prov == NULL) {
    4800           1 :                 os_free(info);
    4801           1 :                 return NULL;
    4802             :         }
    4803             : 
    4804          52 :         if (info) {
    4805          40 :                 os_memcpy(p2ps_prov->info, info, info_len);
    4806          40 :                 p2ps_prov->info[info_len] = '\0';
    4807          40 :                 os_free(info);
    4808             :         }
    4809             : 
    4810          52 :         pos = os_strstr(cmd, "status=");
    4811          52 :         if (pos)
    4812          12 :                 p2ps_prov->status = atoi(pos + 7);
    4813             :         else
    4814          40 :                 p2ps_prov->status = -1;
    4815             : 
    4816          52 :         pos = os_strstr(cmd, "adv_id=");
    4817          52 :         if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
    4818             :                 goto invalid_args;
    4819          52 :         p2ps_prov->adv_id = val;
    4820             : 
    4821          52 :         pos = os_strstr(cmd, "method=");
    4822          52 :         if (pos)
    4823          40 :                 p2ps_prov->method = strtol(pos + 7, NULL, 16);
    4824             :         else
    4825          12 :                 p2ps_prov->method = 0;
    4826             : 
    4827          52 :         pos = os_strstr(cmd, "session=");
    4828          52 :         if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
    4829             :                 goto invalid_args;
    4830          52 :         p2ps_prov->session_id = val;
    4831             : 
    4832          52 :         pos = os_strstr(cmd, "adv_mac=");
    4833          52 :         if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
    4834             :                 goto invalid_args;
    4835             : 
    4836          52 :         pos = os_strstr(cmd, "session_mac=");
    4837          52 :         if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
    4838             :                 goto invalid_args;
    4839             : 
    4840          52 :         pos = os_strstr(cmd, "cpt=");
    4841          52 :         if (pos) {
    4842           9 :                 if (p2ps_ctrl_parse_cpt_priority(pos + 4,
    4843           9 :                                                  p2ps_prov->cpt_priority))
    4844           0 :                         goto invalid_args;
    4845             :         } else {
    4846          43 :                 p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
    4847             :         }
    4848             : 
    4849         112 :         for (i = 0; p2ps_prov->cpt_priority[i]; i++)
    4850          60 :                 p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
    4851             : 
    4852             :         /* force conncap with tstCap (no sanity checks) */
    4853          52 :         pos = os_strstr(cmd, "tstCap=");
    4854          52 :         if (pos) {
    4855           0 :                 role = strtol(pos + 7, NULL, 16);
    4856             :         } else {
    4857          52 :                 pos = os_strstr(cmd, "role=");
    4858          52 :                 if (pos) {
    4859           2 :                         role = strtol(pos + 5, NULL, 16);
    4860           2 :                         if (role != P2PS_SETUP_CLIENT &&
    4861             :                             role != P2PS_SETUP_GROUP_OWNER)
    4862           0 :                                 role = P2PS_SETUP_NONE;
    4863             :                 }
    4864             :         }
    4865          52 :         p2ps_prov->role = role;
    4866             : 
    4867          52 :         return p2ps_prov;
    4868             : 
    4869             : invalid_args:
    4870           0 :         os_free(p2ps_prov);
    4871           0 :         return NULL;
    4872             : }
    4873             : 
    4874             : 
    4875          13 : static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
    4876             : {
    4877             :         u8 addr[ETH_ALEN];
    4878             :         struct p2ps_provision *p2ps_prov;
    4879             :         char *pos;
    4880             : 
    4881             :         /* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
    4882             : 
    4883          13 :         wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
    4884             : 
    4885          13 :         if (hwaddr_aton(cmd, addr))
    4886           0 :                 return -1;
    4887             : 
    4888          13 :         pos = cmd + 17;
    4889          13 :         if (*pos != ' ')
    4890           0 :                 return -1;
    4891             : 
    4892          13 :         p2ps_prov = p2p_parse_asp_provision_cmd(pos);
    4893          13 :         if (!p2ps_prov)
    4894           1 :                 return -1;
    4895             : 
    4896          12 :         if (p2ps_prov->status < 0) {
    4897           0 :                 os_free(p2ps_prov);
    4898           0 :                 return -1;
    4899             :         }
    4900             : 
    4901          12 :         return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
    4902             :                                   p2ps_prov);
    4903             : }
    4904             : 
    4905             : 
    4906          40 : static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
    4907             : {
    4908             :         u8 addr[ETH_ALEN];
    4909             :         struct p2ps_provision *p2ps_prov;
    4910             :         char *pos;
    4911             : 
    4912             :         /* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
    4913             :          *        session=<ses_id> mac=<ses_mac> [info=<infodata>]
    4914             :          */
    4915             : 
    4916          40 :         wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
    4917          40 :         if (hwaddr_aton(cmd, addr))
    4918           0 :                 return -1;
    4919             : 
    4920          40 :         pos = cmd + 17;
    4921          40 :         if (*pos != ' ')
    4922           0 :                 return -1;
    4923             : 
    4924          40 :         p2ps_prov = p2p_parse_asp_provision_cmd(pos);
    4925          40 :         if (!p2ps_prov)
    4926           0 :                 return -1;
    4927             : 
    4928          40 :         p2ps_prov->pd_seeker = 1;
    4929             : 
    4930          40 :         return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
    4931             :                                   p2ps_prov);
    4932             : }
    4933             : 
    4934             : 
    4935         660 : static int parse_freq(int chwidth, int freq2)
    4936             : {
    4937         660 :         if (freq2 < 0)
    4938           0 :                 return -1;
    4939         660 :         if (freq2)
    4940           3 :                 return VHT_CHANWIDTH_80P80MHZ;
    4941             : 
    4942         657 :         switch (chwidth) {
    4943             :         case 0:
    4944             :         case 20:
    4945             :         case 40:
    4946         656 :                 return VHT_CHANWIDTH_USE_HT;
    4947             :         case 80:
    4948           1 :                 return VHT_CHANWIDTH_80MHZ;
    4949             :         case 160:
    4950           0 :                 return VHT_CHANWIDTH_160MHZ;
    4951             :         default:
    4952           0 :                 wpa_printf(MSG_DEBUG, "Unknown max oper bandwidth: %d",
    4953             :                            chwidth);
    4954           0 :                 return -1;
    4955             :         }
    4956             : }
    4957             : 
    4958             : 
    4959         413 : static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
    4960             :                             char *buf, size_t buflen)
    4961             : {
    4962             :         u8 addr[ETH_ALEN];
    4963             :         char *pos, *pos2;
    4964         413 :         char *pin = NULL;
    4965             :         enum p2p_wps_method wps_method;
    4966             :         int new_pin;
    4967             :         int ret;
    4968         413 :         int persistent_group, persistent_id = -1;
    4969             :         int join;
    4970             :         int auth;
    4971             :         int automatic;
    4972         413 :         int go_intent = -1;
    4973         413 :         int freq = 0;
    4974             :         int pd;
    4975         413 :         int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
    4976         413 :         u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
    4977         413 :         size_t group_ssid_len = 0;
    4978             : 
    4979         413 :         if (!wpa_s->global->p2p_init_wpa_s)
    4980           0 :                 return -1;
    4981         413 :         if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
    4982           1 :                 wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
    4983             :                         wpa_s->global->p2p_init_wpa_s->ifname);
    4984           1 :                 wpa_s = wpa_s->global->p2p_init_wpa_s;
    4985             :         }
    4986             : 
    4987             :         /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad|p2ps]
    4988             :          * [persistent|persistent=<network id>]
    4989             :          * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
    4990             :          * [ht40] [vht] [auto] [ssid=<hexdump>] */
    4991             : 
    4992         413 :         if (hwaddr_aton(cmd, addr))
    4993           1 :                 return -1;
    4994             : 
    4995         412 :         pos = cmd + 17;
    4996         412 :         if (*pos != ' ')
    4997           1 :                 return -1;
    4998         411 :         pos++;
    4999             : 
    5000         411 :         persistent_group = os_strstr(pos, " persistent") != NULL;
    5001         411 :         pos2 = os_strstr(pos, " persistent=");
    5002         411 :         if (pos2) {
    5003             :                 struct wpa_ssid *ssid;
    5004           3 :                 persistent_id = atoi(pos2 + 12);
    5005           3 :                 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
    5006           4 :                 if (ssid == NULL || ssid->disabled != 2 ||
    5007           1 :                     ssid->mode != WPAS_MODE_P2P_GO) {
    5008           2 :                         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
    5009             :                                    "SSID id=%d for persistent P2P group (GO)",
    5010             :                                    persistent_id);
    5011           2 :                         return -1;
    5012             :                 }
    5013             :         }
    5014         409 :         join = os_strstr(pos, " join") != NULL;
    5015         409 :         auth = os_strstr(pos, " auth") != NULL;
    5016         409 :         automatic = os_strstr(pos, " auto") != NULL;
    5017         409 :         pd = os_strstr(pos, " provdisc") != NULL;
    5018         409 :         vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
    5019         409 :         ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
    5020             :                 vht;
    5021             : 
    5022         409 :         pos2 = os_strstr(pos, " go_intent=");
    5023         409 :         if (pos2) {
    5024         164 :                 pos2 += 11;
    5025         164 :                 go_intent = atoi(pos2);
    5026         164 :                 if (go_intent < 0 || go_intent > 15)
    5027           2 :                         return -1;
    5028             :         }
    5029             : 
    5030         407 :         pos2 = os_strstr(pos, " freq=");
    5031         407 :         if (pos2) {
    5032          91 :                 pos2 += 6;
    5033          91 :                 freq = atoi(pos2);
    5034          91 :                 if (freq <= 0)
    5035           1 :                         return -1;
    5036             :         }
    5037             : 
    5038         406 :         pos2 = os_strstr(pos, " freq2=");
    5039         406 :         if (pos2)
    5040           1 :                 freq2 = atoi(pos2 + 7);
    5041             : 
    5042         406 :         pos2 = os_strstr(pos, " max_oper_chwidth=");
    5043         406 :         if (pos2)
    5044           1 :                 chwidth = atoi(pos2 + 18);
    5045             : 
    5046         406 :         max_oper_chwidth = parse_freq(chwidth, freq2);
    5047         406 :         if (max_oper_chwidth < 0)
    5048           0 :                 return -1;
    5049             : 
    5050         406 :         pos2 = os_strstr(pos, " ssid=");
    5051         406 :         if (pos2) {
    5052             :                 char *end;
    5053             : 
    5054          18 :                 pos2 += 6;
    5055          18 :                 end = os_strchr(pos2, ' ');
    5056          18 :                 if (!end)
    5057          18 :                         group_ssid_len = os_strlen(pos2) / 2;
    5058             :                 else
    5059           0 :                         group_ssid_len = (end - pos2) / 2;
    5060          36 :                 if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
    5061          18 :                     hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
    5062           0 :                         return -1;
    5063          18 :                 group_ssid = _group_ssid;
    5064             :         }
    5065             : 
    5066         406 :         if (os_strncmp(pos, "pin", 3) == 0) {
    5067             :                 /* Request random PIN (to be displayed) and enable the PIN */
    5068           3 :                 wps_method = WPS_PIN_DISPLAY;
    5069         403 :         } else if (os_strncmp(pos, "pbc", 3) == 0) {
    5070          79 :                 wps_method = WPS_PBC;
    5071         324 :         } else if (os_strstr(pos, "p2ps") != NULL) {
    5072          35 :                 wps_method = WPS_P2PS;
    5073             :         } else {
    5074         289 :                 pin = pos;
    5075         289 :                 pos = os_strchr(pin, ' ');
    5076         289 :                 wps_method = WPS_PIN_KEYPAD;
    5077         289 :                 if (pos) {
    5078         287 :                         *pos++ = '\0';
    5079         287 :                         if (os_strncmp(pos, "display", 7) == 0)
    5080         115 :                                 wps_method = WPS_PIN_DISPLAY;
    5081             :                 }
    5082         289 :                 if (!wps_pin_str_valid(pin)) {
    5083           2 :                         os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
    5084           2 :                         return 17;
    5085             :                 }
    5086             :         }
    5087             : 
    5088         404 :         new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
    5089             :                                    persistent_group, automatic, join,
    5090             :                                    auth, go_intent, freq, freq2, persistent_id,
    5091             :                                    pd, ht40, vht, max_oper_chwidth,
    5092             :                                    group_ssid, group_ssid_len);
    5093         404 :         if (new_pin == -2) {
    5094           0 :                 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
    5095           0 :                 return 25;
    5096             :         }
    5097         404 :         if (new_pin == -3) {
    5098           2 :                 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
    5099           2 :                 return 25;
    5100             :         }
    5101         402 :         if (new_pin < 0)
    5102           1 :                 return -1;
    5103         401 :         if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
    5104           1 :                 ret = os_snprintf(buf, buflen, "%08d", new_pin);
    5105           1 :                 if (os_snprintf_error(buflen, ret))
    5106           0 :                         return -1;
    5107           1 :                 return ret;
    5108             :         }
    5109             : 
    5110         400 :         os_memcpy(buf, "OK\n", 3);
    5111         400 :         return 3;
    5112             : }
    5113             : 
    5114             : 
    5115         530 : static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
    5116             : {
    5117         530 :         unsigned int timeout = atoi(cmd);
    5118         530 :         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
    5119           5 :                 wpa_dbg(wpa_s, MSG_INFO,
    5120             :                         "Reject P2P_LISTEN since interface is disabled");
    5121           5 :                 return -1;
    5122             :         }
    5123         525 :         return wpas_p2p_listen(wpa_s, timeout);
    5124             : }
    5125             : 
    5126             : 
    5127          20 : static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
    5128             : {
    5129             :         u8 addr[ETH_ALEN];
    5130             :         char *pos;
    5131          20 :         enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
    5132             : 
    5133             :         /* <addr> <config method> [join|auto] */
    5134             : 
    5135          20 :         if (hwaddr_aton(cmd, addr))
    5136           1 :                 return -1;
    5137             : 
    5138          19 :         pos = cmd + 17;
    5139          19 :         if (*pos != ' ')
    5140           1 :                 return -1;
    5141          18 :         pos++;
    5142             : 
    5143          18 :         if (os_strstr(pos, " join") != NULL)
    5144           2 :                 use = WPAS_P2P_PD_FOR_JOIN;
    5145          16 :         else if (os_strstr(pos, " auto") != NULL)
    5146           2 :                 use = WPAS_P2P_PD_AUTO;
    5147             : 
    5148          18 :         return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
    5149             : }
    5150             : 
    5151             : 
    5152           2 : static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
    5153             :                               size_t buflen)
    5154             : {
    5155           2 :         struct wpa_ssid *ssid = wpa_s->current_ssid;
    5156             : 
    5157           3 :         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
    5158           1 :             ssid->passphrase == NULL)
    5159           1 :                 return -1;
    5160             : 
    5161           1 :         os_strlcpy(buf, ssid->passphrase, buflen);
    5162           1 :         return os_strlen(buf);
    5163             : }
    5164             : 
    5165             : 
    5166          93 : static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
    5167             :                                   char *buf, size_t buflen)
    5168             : {
    5169             :         u64 ref;
    5170             :         int res;
    5171             :         u8 dst_buf[ETH_ALEN], *dst;
    5172             :         struct wpabuf *tlvs;
    5173             :         char *pos;
    5174             :         size_t len;
    5175             : 
    5176          93 :         if (hwaddr_aton(cmd, dst_buf))
    5177           1 :                 return -1;
    5178          92 :         dst = dst_buf;
    5179         155 :         if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
    5180         126 :             dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
    5181          63 :                 dst = NULL;
    5182          92 :         pos = cmd + 17;
    5183          92 :         if (*pos != ' ')
    5184           1 :                 return -1;
    5185          91 :         pos++;
    5186             : 
    5187          91 :         if (os_strncmp(pos, "upnp ", 5) == 0) {
    5188             :                 u8 version;
    5189           4 :                 pos += 5;
    5190           4 :                 if (hexstr2bin(pos, &version, 1) < 0)
    5191           3 :                         return -1;
    5192           3 :                 pos += 2;
    5193           3 :                 if (*pos != ' ')
    5194           1 :                         return -1;
    5195           2 :                 pos++;
    5196           2 :                 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
    5197             : #ifdef CONFIG_WIFI_DISPLAY
    5198          87 :         } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
    5199           1 :                 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
    5200             : #endif /* CONFIG_WIFI_DISPLAY */
    5201          86 :         } else if (os_strncmp(pos, "asp ", 4) == 0) {
    5202             :                 char *svc_str;
    5203          44 :                 char *svc_info = NULL;
    5204             :                 u32 id;
    5205             : 
    5206          44 :                 pos += 4;
    5207          44 :                 if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
    5208           0 :                         return -1;
    5209             : 
    5210          44 :                 pos = os_strchr(pos, ' ');
    5211          44 :                 if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
    5212           0 :                         return -1;
    5213             : 
    5214          44 :                 svc_str = pos + 1;
    5215             : 
    5216          44 :                 pos = os_strchr(svc_str, ' ');
    5217             : 
    5218          44 :                 if (pos)
    5219          44 :                         *pos++ = '\0';
    5220             : 
    5221             :                 /* All remaining data is the svc_info string */
    5222          44 :                 if (pos && pos[0] && pos[0] != ' ') {
    5223          44 :                         len = os_strlen(pos);
    5224             : 
    5225             :                         /* Unescape in place */
    5226          44 :                         len = utf8_unescape(pos, len, pos, len);
    5227          44 :                         if (len > 0xff)
    5228           0 :                                 return -1;
    5229             : 
    5230          44 :                         svc_info = pos;
    5231             :                 }
    5232             : 
    5233          44 :                 ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
    5234             :                                               svc_str, svc_info);
    5235             :         } else {
    5236          42 :                 len = os_strlen(pos);
    5237          42 :                 if (len & 1)
    5238           1 :                         return -1;
    5239          41 :                 len /= 2;
    5240          41 :                 tlvs = wpabuf_alloc(len);
    5241          41 :                 if (tlvs == NULL)
    5242           1 :                         return -1;
    5243          40 :                 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
    5244           1 :                         wpabuf_free(tlvs);
    5245           1 :                         return -1;
    5246             :                 }
    5247             : 
    5248          39 :                 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
    5249          39 :                 wpabuf_free(tlvs);
    5250             :         }
    5251          86 :         if (ref == 0)
    5252           0 :                 return -1;
    5253          86 :         res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
    5254          86 :         if (os_snprintf_error(buflen, res))
    5255           0 :                 return -1;
    5256          86 :         return res;
    5257             : }
    5258             : 
    5259             : 
    5260           9 : static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
    5261             :                                          char *cmd)
    5262             : {
    5263             :         long long unsigned val;
    5264             :         u64 req;
    5265           9 :         if (sscanf(cmd, "%llx", &val) != 1)
    5266           1 :                 return -1;
    5267           8 :         req = val;
    5268           8 :         return wpas_p2p_sd_cancel_request(wpa_s, req);
    5269             : }
    5270             : 
    5271             : 
    5272          10 : static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
    5273             : {
    5274             :         int freq;
    5275             :         u8 dst[ETH_ALEN];
    5276             :         u8 dialog_token;
    5277             :         struct wpabuf *resp_tlvs;
    5278             :         char *pos, *pos2;
    5279             :         size_t len;
    5280             : 
    5281          10 :         pos = os_strchr(cmd, ' ');
    5282          10 :         if (pos == NULL)
    5283           1 :                 return -1;
    5284           9 :         *pos++ = '\0';
    5285           9 :         freq = atoi(cmd);
    5286           9 :         if (freq == 0)
    5287           1 :                 return -1;
    5288             : 
    5289           8 :         if (hwaddr_aton(pos, dst))
    5290           1 :                 return -1;
    5291           7 :         pos += 17;
    5292           7 :         if (*pos != ' ')
    5293           1 :                 return -1;
    5294           6 :         pos++;
    5295             : 
    5296           6 :         pos2 = os_strchr(pos, ' ');
    5297           6 :         if (pos2 == NULL)
    5298           1 :                 return -1;
    5299           5 :         *pos2++ = '\0';
    5300           5 :         dialog_token = atoi(pos);
    5301             : 
    5302           5 :         len = os_strlen(pos2);
    5303           5 :         if (len & 1)
    5304           1 :                 return -1;
    5305           4 :         len /= 2;
    5306           4 :         resp_tlvs = wpabuf_alloc(len);
    5307           4 :         if (resp_tlvs == NULL)
    5308           1 :                 return -1;
    5309           3 :         if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
    5310           1 :                 wpabuf_free(resp_tlvs);
    5311           1 :                 return -1;
    5312             :         }
    5313             : 
    5314           2 :         wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
    5315           2 :         wpabuf_free(resp_tlvs);
    5316           2 :         return 0;
    5317             : }
    5318             : 
    5319             : 
    5320           4 : static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
    5321             :                                        char *cmd)
    5322             : {
    5323           4 :         if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
    5324           1 :                 return -1;
    5325           3 :         wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
    5326           3 :         return 0;
    5327             : }
    5328             : 
    5329             : 
    5330         121 : static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
    5331             :                                         char *cmd)
    5332             : {
    5333             :         char *pos;
    5334             :         size_t len;
    5335             :         struct wpabuf *query, *resp;
    5336             : 
    5337         121 :         pos = os_strchr(cmd, ' ');
    5338         121 :         if (pos == NULL)
    5339           1 :                 return -1;
    5340         120 :         *pos++ = '\0';
    5341             : 
    5342         120 :         len = os_strlen(cmd);
    5343         120 :         if (len & 1)
    5344           1 :                 return -1;
    5345         119 :         len /= 2;
    5346         119 :         query = wpabuf_alloc(len);
    5347         119 :         if (query == NULL)
    5348           1 :                 return -1;
    5349         118 :         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
    5350           1 :                 wpabuf_free(query);
    5351           1 :                 return -1;
    5352             :         }
    5353             : 
    5354         117 :         len = os_strlen(pos);
    5355         117 :         if (len & 1) {
    5356           1 :                 wpabuf_free(query);
    5357           1 :                 return -1;
    5358             :         }
    5359         116 :         len /= 2;
    5360         116 :         resp = wpabuf_alloc(len);
    5361         116 :         if (resp == NULL) {
    5362           1 :                 wpabuf_free(query);
    5363           1 :                 return -1;
    5364             :         }
    5365         115 :         if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
    5366           1 :                 wpabuf_free(query);
    5367           1 :                 wpabuf_free(resp);
    5368           1 :                 return -1;
    5369             :         }
    5370             : 
    5371         114 :         if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
    5372           1 :                 wpabuf_free(query);
    5373           1 :                 wpabuf_free(resp);
    5374           1 :                 return -1;
    5375             :         }
    5376         113 :         return 0;
    5377             : }
    5378             : 
    5379             : 
    5380         542 : static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
    5381             : {
    5382             :         char *pos;
    5383             :         u8 version;
    5384             : 
    5385         542 :         pos = os_strchr(cmd, ' ');
    5386         542 :         if (pos == NULL)
    5387           1 :                 return -1;
    5388         541 :         *pos++ = '\0';
    5389             : 
    5390         541 :         if (hexstr2bin(cmd, &version, 1) < 0)
    5391           1 :                 return -1;
    5392             : 
    5393         540 :         return wpas_p2p_service_add_upnp(wpa_s, version, pos);
    5394             : }
    5395             : 
    5396             : 
    5397          61 : static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
    5398             :                                     u8 replace, char *cmd)
    5399             : {
    5400             :         char *pos;
    5401             :         char *adv_str;
    5402             :         u32 auto_accept, adv_id, svc_state, config_methods;
    5403          61 :         char *svc_info = NULL;
    5404             :         char *cpt_prio_str;
    5405             :         u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
    5406             : 
    5407          61 :         pos = os_strchr(cmd, ' ');
    5408          61 :         if (pos == NULL)
    5409           0 :                 return -1;
    5410          61 :         *pos++ = '\0';
    5411             : 
    5412             :         /* Auto-Accept value is mandatory, and must be one of the
    5413             :          * single values (0, 1, 2, 4) */
    5414          61 :         auto_accept = atoi(cmd);
    5415          61 :         switch (auto_accept) {
    5416             :         case P2PS_SETUP_NONE: /* No auto-accept */
    5417             :         case P2PS_SETUP_NEW:
    5418             :         case P2PS_SETUP_CLIENT:
    5419             :         case P2PS_SETUP_GROUP_OWNER:
    5420          61 :                 break;
    5421             :         default:
    5422           0 :                 return -1;
    5423             :         }
    5424             : 
    5425             :         /* Advertisement ID is mandatory */
    5426          61 :         cmd = pos;
    5427          61 :         pos = os_strchr(cmd, ' ');
    5428          61 :         if (pos == NULL)
    5429           0 :                 return -1;
    5430          61 :         *pos++ = '\0';
    5431             : 
    5432             :         /* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
    5433          61 :         if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
    5434           0 :                 return -1;
    5435             : 
    5436             :         /* Only allow replacements if exist, and adds if not */
    5437          61 :         if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
    5438           2 :                 if (!replace)
    5439           0 :                         return -1;
    5440             :         } else {
    5441          59 :                 if (replace)
    5442           0 :                         return -1;
    5443             :         }
    5444             : 
    5445             :         /* svc_state between 0 - 0xff is mandatory */
    5446          61 :         if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
    5447           0 :                 return -1;
    5448             : 
    5449          61 :         pos = os_strchr(pos, ' ');
    5450          61 :         if (pos == NULL)
    5451           0 :                 return -1;
    5452             : 
    5453             :         /* config_methods is mandatory */
    5454          61 :         pos++;
    5455          61 :         if (sscanf(pos, "%x", &config_methods) != 1)
    5456           0 :                 return -1;
    5457             : 
    5458          61 :         if (!(config_methods &
    5459             :               (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
    5460           0 :                 return -1;
    5461             : 
    5462          61 :         pos = os_strchr(pos, ' ');
    5463          61 :         if (pos == NULL)
    5464           0 :                 return -1;
    5465             : 
    5466          61 :         pos++;
    5467          61 :         adv_str = pos;
    5468             : 
    5469             :         /* Advertisement string is mandatory */
    5470          61 :         if (!pos[0] || pos[0] == ' ')
    5471           0 :                 return -1;
    5472             : 
    5473             :         /* Terminate svc string */
    5474          61 :         pos = os_strchr(pos, ' ');
    5475          61 :         if (pos != NULL)
    5476          61 :                 *pos++ = '\0';
    5477             : 
    5478          61 :         cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
    5479          61 :         if (cpt_prio_str) {
    5480           6 :                 pos = os_strchr(pos, ' ');
    5481           6 :                 if (pos != NULL)
    5482           6 :                         *pos++ = '\0';
    5483             : 
    5484           6 :                 if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
    5485           0 :                         return -1;
    5486             :         } else {
    5487          55 :                 cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
    5488          55 :                 cpt_prio[1] = 0;
    5489             :         }
    5490             : 
    5491             :         /* Service and Response Information are optional */
    5492          61 :         if (pos && pos[0]) {
    5493             :                 size_t len;
    5494             : 
    5495             :                 /* Note the bare ' included, which cannot exist legally
    5496             :                  * in unescaped string. */
    5497          61 :                 svc_info = os_strstr(pos, "svc_info='");
    5498             : 
    5499          61 :                 if (svc_info) {
    5500          61 :                         svc_info += 9;
    5501          61 :                         len = os_strlen(svc_info);
    5502          61 :                         utf8_unescape(svc_info, len, svc_info, len);
    5503             :                 }
    5504             :         }
    5505             : 
    5506         122 :         return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
    5507         122 :                                         (u8) svc_state, (u16) config_methods,
    5508             :                                         svc_info, cpt_prio);
    5509             : }
    5510             : 
    5511             : 
    5512         724 : static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
    5513             : {
    5514             :         char *pos;
    5515             : 
    5516         724 :         pos = os_strchr(cmd, ' ');
    5517         724 :         if (pos == NULL)
    5518           1 :                 return -1;
    5519         723 :         *pos++ = '\0';
    5520             : 
    5521         723 :         if (os_strcmp(cmd, "bonjour") == 0)
    5522         121 :                 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
    5523         602 :         if (os_strcmp(cmd, "upnp") == 0)
    5524         542 :                 return p2p_ctrl_service_add_upnp(wpa_s, pos);
    5525          60 :         if (os_strcmp(cmd, "asp") == 0)
    5526          59 :                 return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
    5527           1 :         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
    5528           1 :         return -1;
    5529             : }
    5530             : 
    5531             : 
    5532          41 : static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
    5533             :                                         char *cmd)
    5534             : {
    5535             :         size_t len;
    5536             :         struct wpabuf *query;
    5537             :         int ret;
    5538             : 
    5539          41 :         len = os_strlen(cmd);
    5540          41 :         if (len & 1)
    5541           1 :                 return -1;
    5542          40 :         len /= 2;
    5543          40 :         query = wpabuf_alloc(len);
    5544          40 :         if (query == NULL)
    5545           1 :                 return -1;
    5546          39 :         if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
    5547           1 :                 wpabuf_free(query);
    5548           1 :                 return -1;
    5549             :         }
    5550             : 
    5551          38 :         ret = wpas_p2p_service_del_bonjour(wpa_s, query);
    5552          38 :         wpabuf_free(query);
    5553          38 :         return ret;
    5554             : }
    5555             : 
    5556             : 
    5557          41 : static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
    5558             : {
    5559             :         char *pos;
    5560             :         u8 version;
    5561             : 
    5562          41 :         pos = os_strchr(cmd, ' ');
    5563          41 :         if (pos == NULL)
    5564           1 :                 return -1;
    5565          40 :         *pos++ = '\0';
    5566             : 
    5567          40 :         if (hexstr2bin(cmd, &version, 1) < 0)
    5568           2 :                 return -1;
    5569             : 
    5570          38 :         return wpas_p2p_service_del_upnp(wpa_s, version, pos);
    5571             : }
    5572             : 
    5573             : 
    5574          48 : static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
    5575             : {
    5576             :         u32 adv_id;
    5577             : 
    5578          48 :         if (os_strcmp(cmd, "all") == 0) {
    5579          11 :                 wpas_p2p_service_flush_asp(wpa_s);
    5580          11 :                 return 0;
    5581             :         }
    5582             : 
    5583          37 :         if (sscanf(cmd, "%x", &adv_id) != 1)
    5584           0 :                 return -1;
    5585             : 
    5586          37 :         return wpas_p2p_service_del_asp(wpa_s, adv_id);
    5587             : }
    5588             : 
    5589             : 
    5590         132 : static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
    5591             : {
    5592             :         char *pos;
    5593             : 
    5594         132 :         pos = os_strchr(cmd, ' ');
    5595         132 :         if (pos == NULL)
    5596           1 :                 return -1;
    5597         131 :         *pos++ = '\0';
    5598             : 
    5599         131 :         if (os_strcmp(cmd, "bonjour") == 0)
    5600          41 :                 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
    5601          90 :         if (os_strcmp(cmd, "upnp") == 0)
    5602          41 :                 return p2p_ctrl_service_del_upnp(wpa_s, pos);
    5603          49 :         if (os_strcmp(cmd, "asp") == 0)
    5604          48 :                 return p2p_ctrl_service_del_asp(wpa_s, pos);
    5605           1 :         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
    5606           1 :         return -1;
    5607             : }
    5608             : 
    5609             : 
    5610           2 : static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
    5611             : {
    5612             :         char *pos;
    5613             : 
    5614           2 :         pos = os_strchr(cmd, ' ');
    5615           2 :         if (pos == NULL)
    5616           0 :                 return -1;
    5617           2 :         *pos++ = '\0';
    5618             : 
    5619           2 :         if (os_strcmp(cmd, "asp") == 0)
    5620           2 :                 return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
    5621             : 
    5622           0 :         wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
    5623           0 :         return -1;
    5624             : }
    5625             : 
    5626             : 
    5627           3 : static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
    5628             : {
    5629             :         u8 addr[ETH_ALEN];
    5630             : 
    5631             :         /* <addr> */
    5632             : 
    5633           3 :         if (hwaddr_aton(cmd, addr))
    5634           1 :                 return -1;
    5635             : 
    5636           2 :         return wpas_p2p_reject(wpa_s, addr);
    5637             : }
    5638             : 
    5639             : 
    5640          61 : static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
    5641             : {
    5642             :         char *pos;
    5643             :         int id;
    5644             :         struct wpa_ssid *ssid;
    5645          61 :         u8 *_peer = NULL, peer[ETH_ALEN];
    5646          61 :         int freq = 0, pref_freq = 0;
    5647          61 :         int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
    5648             : 
    5649          61 :         id = atoi(cmd);
    5650          61 :         pos = os_strstr(cmd, " peer=");
    5651          61 :         if (pos) {
    5652          59 :                 pos += 6;
    5653          59 :                 if (hwaddr_aton(pos, peer))
    5654           1 :                         return -1;
    5655          58 :                 _peer = peer;
    5656             :         }
    5657          60 :         ssid = wpa_config_get_network(wpa_s->conf, id);
    5658          60 :         if (ssid == NULL || ssid->disabled != 2) {
    5659           2 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
    5660             :                            "for persistent P2P group",
    5661             :                            id);
    5662           2 :                 return -1;
    5663             :         }
    5664             : 
    5665          58 :         pos = os_strstr(cmd, " freq=");
    5666          58 :         if (pos) {
    5667           8 :                 pos += 6;
    5668           8 :                 freq = atoi(pos);
    5669           8 :                 if (freq <= 0)
    5670           1 :                         return -1;
    5671             :         }
    5672             : 
    5673          57 :         pos = os_strstr(cmd, " pref=");
    5674          57 :         if (pos) {
    5675           4 :                 pos += 6;
    5676           4 :                 pref_freq = atoi(pos);
    5677           4 :                 if (pref_freq <= 0)
    5678           1 :                         return -1;
    5679             :         }
    5680             : 
    5681          56 :         vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
    5682          56 :         ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
    5683             :                 vht;
    5684             : 
    5685          56 :         pos = os_strstr(cmd, "freq2=");
    5686          56 :         if (pos)
    5687           1 :                 freq2 = atoi(pos + 6);
    5688             : 
    5689          56 :         pos = os_strstr(cmd, " max_oper_chwidth=");
    5690          56 :         if (pos)
    5691           0 :                 chwidth = atoi(pos + 18);
    5692             : 
    5693          56 :         max_oper_chwidth = parse_freq(chwidth, freq2);
    5694          56 :         if (max_oper_chwidth < 0)
    5695           0 :                 return -1;
    5696             : 
    5697          56 :         return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
    5698             :                                max_oper_chwidth, pref_freq);
    5699             : }
    5700             : 
    5701             : 
    5702          12 : static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
    5703             : {
    5704             :         char *pos;
    5705          12 :         u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
    5706             : 
    5707          12 :         pos = os_strstr(cmd, " peer=");
    5708          12 :         if (!pos)
    5709           1 :                 return -1;
    5710             : 
    5711          11 :         *pos = '\0';
    5712          11 :         pos += 6;
    5713          11 :         if (hwaddr_aton(pos, peer)) {
    5714           1 :                 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
    5715           1 :                 return -1;
    5716             :         }
    5717             : 
    5718          10 :         pos = os_strstr(pos, " go_dev_addr=");
    5719          10 :         if (pos) {
    5720           1 :                 pos += 13;
    5721           1 :                 if (hwaddr_aton(pos, go_dev_addr)) {
    5722           1 :                         wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
    5723             :                                    pos);
    5724           1 :                         return -1;
    5725             :                 }
    5726           0 :                 go_dev = go_dev_addr;
    5727             :         }
    5728             : 
    5729           9 :         return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
    5730             : }
    5731             : 
    5732             : 
    5733          74 : static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
    5734             : {
    5735          74 :         if (os_strncmp(cmd, "persistent=", 11) == 0)
    5736          61 :                 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
    5737          13 :         if (os_strncmp(cmd, "group=", 6) == 0)
    5738          12 :                 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
    5739             : 
    5740           1 :         return -1;
    5741             : }
    5742             : 
    5743             : 
    5744          18 : static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
    5745             :                                          int id, int freq, int vht_center_freq2,
    5746             :                                          int ht40, int vht, int vht_chwidth)
    5747             : {
    5748             :         struct wpa_ssid *ssid;
    5749             : 
    5750          18 :         ssid = wpa_config_get_network(wpa_s->conf, id);
    5751          18 :         if (ssid == NULL || ssid->disabled != 2) {
    5752           2 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
    5753             :                            "for persistent P2P group",
    5754             :                            id);
    5755           2 :                 return -1;
    5756             :         }
    5757             : 
    5758          16 :         return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq,
    5759             :                                              vht_center_freq2, 0, ht40, vht,
    5760             :                                              vht_chwidth, NULL, 0, 0);
    5761             : }
    5762             : 
    5763             : 
    5764         199 : static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
    5765             : {
    5766         199 :         int freq = 0, persistent = 0, group_id = -1;
    5767         199 :         int vht = wpa_s->conf->p2p_go_vht;
    5768         199 :         int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
    5769         199 :         int max_oper_chwidth, chwidth = 0, freq2 = 0;
    5770         199 :         char *token, *context = NULL;
    5771             : 
    5772         572 :         while ((token = str_token(cmd, " ", &context))) {
    5773         210 :                 if (sscanf(token, "freq=%d", &freq) == 1 ||
    5774          69 :                     sscanf(token, "freq2=%d", &freq2) == 1 ||
    5775          50 :                     sscanf(token, "persistent=%d", &group_id) == 1 ||
    5776          16 :                     sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1) {
    5777         160 :                         continue;
    5778          15 :                 } else if (os_strcmp(token, "ht40") == 0) {
    5779           3 :                         ht40 = 1;
    5780          12 :                 } else if (os_strcmp(token, "vht") == 0) {
    5781           4 :                         vht = 1;
    5782           4 :                         ht40 = 1;
    5783           8 :                 } else if (os_strcmp(token, "persistent") == 0) {
    5784           7 :                         persistent = 1;
    5785             :                 } else {
    5786           1 :                         wpa_printf(MSG_DEBUG,
    5787             :                                    "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
    5788             :                                    token);
    5789           1 :                         return -1;
    5790             :                 }
    5791             :         }
    5792             : 
    5793         198 :         max_oper_chwidth = parse_freq(chwidth, freq2);
    5794         198 :         if (max_oper_chwidth < 0)
    5795           0 :                 return -1;
    5796             : 
    5797         198 :         if (group_id >= 0)
    5798          18 :                 return p2p_ctrl_group_add_persistent(wpa_s, group_id,
    5799             :                                                      freq, freq2, ht40, vht,
    5800             :                                                      max_oper_chwidth);
    5801             : 
    5802         180 :         return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
    5803             :                                   max_oper_chwidth);
    5804             : }
    5805             : 
    5806             : 
    5807           3 : static int p2p_ctrl_group_member(struct wpa_supplicant *wpa_s, const char *cmd,
    5808             :                                  char *buf, size_t buflen)
    5809             : {
    5810             :         u8 dev_addr[ETH_ALEN];
    5811             :         struct wpa_ssid *ssid;
    5812             :         int res;
    5813             :         const u8 *iaddr;
    5814             : 
    5815           3 :         ssid = wpa_s->current_ssid;
    5816           6 :         if (!wpa_s->global->p2p || !ssid || ssid->mode != WPAS_MODE_P2P_GO ||
    5817           3 :             hwaddr_aton(cmd, dev_addr))
    5818           1 :                 return -1;
    5819             : 
    5820           2 :         iaddr = p2p_group_get_client_interface_addr(wpa_s->p2p_group, dev_addr);
    5821           2 :         if (!iaddr)
    5822           1 :                 return -1;
    5823           1 :         res = os_snprintf(buf, buflen, MACSTR, MAC2STR(iaddr));
    5824           1 :         if (os_snprintf_error(buflen, res))
    5825           0 :                 return -1;
    5826           1 :         return res;
    5827             : }
    5828             : 
    5829             : 
    5830        1264 : static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
    5831             :                          char *buf, size_t buflen)
    5832             : {
    5833             :         u8 addr[ETH_ALEN], *addr_ptr;
    5834             :         int next, res;
    5835             :         const struct p2p_peer_info *info;
    5836             :         char *pos, *end;
    5837             :         char devtype[WPS_DEV_TYPE_BUFSIZE];
    5838             :         struct wpa_ssid *ssid;
    5839             :         size_t i;
    5840             : 
    5841        1264 :         if (!wpa_s->global->p2p)
    5842           0 :                 return -1;
    5843             : 
    5844        1264 :         if (os_strcmp(cmd, "FIRST") == 0) {
    5845           1 :                 addr_ptr = NULL;
    5846           1 :                 next = 0;
    5847        1263 :         } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
    5848           2 :                 if (hwaddr_aton(cmd + 5, addr) < 0)
    5849           1 :                         return -1;
    5850           1 :                 addr_ptr = addr;
    5851           1 :                 next = 1;
    5852             :         } else {
    5853        1261 :                 if (hwaddr_aton(cmd, addr) < 0)
    5854           1 :                         return -1;
    5855        1260 :                 addr_ptr = addr;
    5856        1260 :                 next = 0;
    5857             :         }
    5858             : 
    5859        1262 :         info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
    5860        1262 :         if (info == NULL)
    5861         530 :                 return -1;
    5862             : 
    5863         732 :         pos = buf;
    5864         732 :         end = buf + buflen;
    5865             : 
    5866        8052 :         res = os_snprintf(pos, end - pos, MACSTR "\n"
    5867             :                           "pri_dev_type=%s\n"
    5868             :                           "device_name=%s\n"
    5869             :                           "manufacturer=%s\n"
    5870             :                           "model_name=%s\n"
    5871             :                           "model_number=%s\n"
    5872             :                           "serial_number=%s\n"
    5873             :                           "config_methods=0x%x\n"
    5874             :                           "dev_capab=0x%x\n"
    5875             :                           "group_capab=0x%x\n"
    5876             :                           "level=%d\n",
    5877        4392 :                           MAC2STR(info->p2p_device_addr),
    5878         732 :                           wps_dev_type_bin2str(info->pri_dev_type,
    5879             :                                                devtype, sizeof(devtype)),
    5880         732 :                           info->device_name,
    5881         732 :                           info->manufacturer,
    5882         732 :                           info->model_name,
    5883         732 :                           info->model_number,
    5884         732 :                           info->serial_number,
    5885         732 :                           info->config_methods,
    5886         732 :                           info->dev_capab,
    5887         732 :                           info->group_capab,
    5888             :                           info->level);
    5889         732 :         if (os_snprintf_error(end - pos, res))
    5890           0 :                 return pos - buf;
    5891         732 :         pos += res;
    5892             : 
    5893         761 :         for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
    5894             :         {
    5895             :                 const u8 *t;
    5896          29 :                 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
    5897          29 :                 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
    5898             :                                   wps_dev_type_bin2str(t, devtype,
    5899             :                                                        sizeof(devtype)));
    5900          29 :                 if (os_snprintf_error(end - pos, res))
    5901           0 :                         return pos - buf;
    5902          29 :                 pos += res;
    5903             :         }
    5904             : 
    5905         732 :         ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
    5906         732 :         if (ssid) {
    5907         128 :                 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
    5908         128 :                 if (os_snprintf_error(end - pos, res))
    5909           0 :                         return pos - buf;
    5910         128 :                 pos += res;
    5911             :         }
    5912             : 
    5913         732 :         res = p2p_get_peer_info_txt(info, pos, end - pos);
    5914         732 :         if (res < 0)
    5915           0 :                 return pos - buf;
    5916         732 :         pos += res;
    5917             : 
    5918         732 :         if (info->vendor_elems) {
    5919           4 :                 res = os_snprintf(pos, end - pos, "vendor_elems=");
    5920           4 :                 if (os_snprintf_error(end - pos, res))
    5921           0 :                         return pos - buf;
    5922           4 :                 pos += res;
    5923             : 
    5924           8 :                 pos += wpa_snprintf_hex(pos, end - pos,
    5925           4 :                                         wpabuf_head(info->vendor_elems),
    5926           4 :                                         wpabuf_len(info->vendor_elems));
    5927             : 
    5928           4 :                 res = os_snprintf(pos, end - pos, "\n");
    5929           4 :                 if (os_snprintf_error(end - pos, res))
    5930           0 :                         return pos - buf;
    5931           4 :                 pos += res;
    5932             :         }
    5933             : 
    5934         732 :         return pos - buf;
    5935             : }
    5936             : 
    5937             : 
    5938          41 : static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
    5939             :                                   const char *param)
    5940             : {
    5941             :         unsigned int i;
    5942             : 
    5943          41 :         if (wpa_s->global->p2p == NULL)
    5944           0 :                 return -1;
    5945             : 
    5946          41 :         if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
    5947           1 :                 return -1;
    5948             : 
    5949          65 :         for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
    5950             :                 struct wpa_freq_range *freq;
    5951          25 :                 freq = &wpa_s->global->p2p_disallow_freq.range[i];
    5952          25 :                 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
    5953             :                            freq->min, freq->max);
    5954             :         }
    5955             : 
    5956          40 :         wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
    5957          40 :         return 0;
    5958             : }
    5959             : 
    5960             : 
    5961         145 : static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
    5962             : {
    5963             :         char *param;
    5964             : 
    5965         145 :         if (wpa_s->global->p2p == NULL)
    5966           0 :                 return -1;
    5967             : 
    5968         145 :         param = os_strchr(cmd, ' ');
    5969         145 :         if (param == NULL)
    5970           1 :                 return -1;
    5971         144 :         *param++ = '\0';
    5972             : 
    5973         144 :         if (os_strcmp(cmd, "discoverability") == 0) {
    5974           2 :                 p2p_set_client_discoverability(wpa_s->global->p2p,
    5975             :                                                atoi(param));
    5976           2 :                 return 0;
    5977             :         }
    5978             : 
    5979         142 :         if (os_strcmp(cmd, "managed") == 0) {
    5980           2 :                 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
    5981           2 :                 return 0;
    5982             :         }
    5983             : 
    5984         140 :         if (os_strcmp(cmd, "listen_channel") == 0) {
    5985             :                 char *pos;
    5986             :                 u8 channel, op_class;
    5987             : 
    5988          16 :                 channel = atoi(param);
    5989          16 :                 pos = os_strchr(param, ' ');
    5990          16 :                 op_class = pos ? atoi(pos) : 81;
    5991             : 
    5992          16 :                 return p2p_set_listen_channel(wpa_s->global->p2p, op_class,
    5993             :                                               channel, 1);
    5994             :         }
    5995             : 
    5996         124 :         if (os_strcmp(cmd, "ssid_postfix") == 0) {
    5997           4 :                 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
    5998             :                                             os_strlen(param));
    5999             :         }
    6000             : 
    6001         120 :         if (os_strcmp(cmd, "noa") == 0) {
    6002             :                 char *pos;
    6003             :                 int count, start, duration;
    6004             :                 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
    6005           9 :                 count = atoi(param);
    6006           9 :                 pos = os_strchr(param, ',');
    6007           9 :                 if (pos == NULL)
    6008           1 :                         return -1;
    6009           8 :                 pos++;
    6010           8 :                 start = atoi(pos);
    6011           8 :                 pos = os_strchr(pos, ',');
    6012           8 :                 if (pos == NULL)
    6013           1 :                         return -1;
    6014           7 :                 pos++;
    6015           7 :                 duration = atoi(pos);
    6016           7 :                 if (count < 0 || count > 255 || start < 0 || duration < 0)
    6017           4 :                         return -1;
    6018           3 :                 if (count == 0 && duration > 0)
    6019           1 :                         return -1;
    6020           2 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
    6021             :                            "start=%d duration=%d", count, start, duration);
    6022           2 :                 return wpas_p2p_set_noa(wpa_s, count, start, duration);
    6023             :         }
    6024             : 
    6025         111 :         if (os_strcmp(cmd, "ps") == 0)
    6026           2 :                 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
    6027             : 
    6028         109 :         if (os_strcmp(cmd, "oppps") == 0)
    6029           1 :                 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
    6030             : 
    6031         108 :         if (os_strcmp(cmd, "ctwindow") == 0)
    6032           1 :                 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
    6033             : 
    6034         107 :         if (os_strcmp(cmd, "disabled") == 0) {
    6035          26 :                 wpa_s->global->p2p_disabled = atoi(param);
    6036          26 :                 wpa_printf(MSG_DEBUG, "P2P functionality %s",
    6037          26 :                            wpa_s->global->p2p_disabled ?
    6038             :                            "disabled" : "enabled");
    6039          26 :                 if (wpa_s->global->p2p_disabled) {
    6040          15 :                         wpas_p2p_stop_find(wpa_s);
    6041          15 :                         os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
    6042          15 :                         p2p_flush(wpa_s->global->p2p);
    6043             :                 }
    6044          26 :                 return 0;
    6045             :         }
    6046             : 
    6047          81 :         if (os_strcmp(cmd, "conc_pref") == 0) {
    6048           1 :                 if (os_strcmp(param, "sta") == 0)
    6049           0 :                         wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
    6050           1 :                 else if (os_strcmp(param, "p2p") == 0)
    6051           0 :                         wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
    6052             :                 else {
    6053           1 :                         wpa_printf(MSG_INFO, "Invalid conc_pref value");
    6054           1 :                         return -1;
    6055             :                 }
    6056           0 :                 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
    6057             :                            "%s", param);
    6058           0 :                 return 0;
    6059             :         }
    6060             : 
    6061          80 :         if (os_strcmp(cmd, "force_long_sd") == 0) {
    6062           0 :                 wpa_s->force_long_sd = atoi(param);
    6063           0 :                 return 0;
    6064             :         }
    6065             : 
    6066          80 :         if (os_strcmp(cmd, "peer_filter") == 0) {
    6067             :                 u8 addr[ETH_ALEN];
    6068           3 :                 if (hwaddr_aton(param, addr))
    6069           1 :                         return -1;
    6070           2 :                 p2p_set_peer_filter(wpa_s->global->p2p, addr);
    6071           2 :                 return 0;
    6072             :         }
    6073             : 
    6074          77 :         if (os_strcmp(cmd, "cross_connect") == 0)
    6075           6 :                 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
    6076             : 
    6077          71 :         if (os_strcmp(cmd, "go_apsd") == 0) {
    6078           0 :                 if (os_strcmp(param, "disable") == 0)
    6079           0 :                         wpa_s->set_ap_uapsd = 0;
    6080             :                 else {
    6081           0 :                         wpa_s->set_ap_uapsd = 1;
    6082           0 :                         wpa_s->ap_uapsd = atoi(param);
    6083             :                 }
    6084           0 :                 return 0;
    6085             :         }
    6086             : 
    6087          71 :         if (os_strcmp(cmd, "client_apsd") == 0) {
    6088           3 :                 if (os_strcmp(param, "disable") == 0)
    6089           0 :                         wpa_s->set_sta_uapsd = 0;
    6090             :                 else {
    6091             :                         int be, bk, vi, vo;
    6092             :                         char *pos;
    6093             :                         /* format: BE,BK,VI,VO;max SP Length */
    6094           3 :                         be = atoi(param);
    6095           3 :                         pos = os_strchr(param, ',');
    6096           3 :                         if (pos == NULL)
    6097           1 :                                 return -1;
    6098           2 :                         pos++;
    6099           2 :                         bk = atoi(pos);
    6100           2 :                         pos = os_strchr(pos, ',');
    6101           2 :                         if (pos == NULL)
    6102           1 :                                 return -1;
    6103           1 :                         pos++;
    6104           1 :                         vi = atoi(pos);
    6105           1 :                         pos = os_strchr(pos, ',');
    6106           1 :                         if (pos == NULL)
    6107           1 :                                 return -1;
    6108           0 :                         pos++;
    6109           0 :                         vo = atoi(pos);
    6110             :                         /* ignore max SP Length for now */
    6111             : 
    6112           0 :                         wpa_s->set_sta_uapsd = 1;
    6113           0 :                         wpa_s->sta_uapsd = 0;
    6114           0 :                         if (be)
    6115           0 :                                 wpa_s->sta_uapsd |= BIT(0);
    6116           0 :                         if (bk)
    6117           0 :                                 wpa_s->sta_uapsd |= BIT(1);
    6118           0 :                         if (vi)
    6119           0 :                                 wpa_s->sta_uapsd |= BIT(2);
    6120           0 :                         if (vo)
    6121           0 :                                 wpa_s->sta_uapsd |= BIT(3);
    6122             :                 }
    6123           0 :                 return 0;
    6124             :         }
    6125             : 
    6126          68 :         if (os_strcmp(cmd, "disallow_freq") == 0)
    6127          41 :                 return p2p_ctrl_disallow_freq(wpa_s, param);
    6128             : 
    6129          27 :         if (os_strcmp(cmd, "disc_int") == 0) {
    6130             :                 int min_disc_int, max_disc_int, max_disc_tu;
    6131             :                 char *pos;
    6132             : 
    6133           5 :                 pos = param;
    6134             : 
    6135           5 :                 min_disc_int = atoi(pos);
    6136           5 :                 pos = os_strchr(pos, ' ');
    6137           5 :                 if (pos == NULL)
    6138           1 :                         return -1;
    6139           4 :                 *pos++ = '\0';
    6140             : 
    6141           4 :                 max_disc_int = atoi(pos);
    6142           4 :                 pos = os_strchr(pos, ' ');
    6143           4 :                 if (pos == NULL)
    6144           1 :                         return -1;
    6145           3 :                 *pos++ = '\0';
    6146             : 
    6147           3 :                 max_disc_tu = atoi(pos);
    6148             : 
    6149           3 :                 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
    6150             :                                         max_disc_int, max_disc_tu);
    6151             :         }
    6152             : 
    6153          22 :         if (os_strcmp(cmd, "per_sta_psk") == 0) {
    6154           4 :                 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
    6155           4 :                 return 0;
    6156             :         }
    6157             : 
    6158             : #ifdef CONFIG_WPS_NFC
    6159          18 :         if (os_strcmp(cmd, "nfc_tag") == 0)
    6160          15 :                 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
    6161             : #endif /* CONFIG_WPS_NFC */
    6162             : 
    6163           3 :         if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
    6164           2 :                 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
    6165           2 :                 return 0;
    6166             :         }
    6167             : 
    6168           1 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
    6169             :                    cmd);
    6170             : 
    6171           1 :         return -1;
    6172             : }
    6173             : 
    6174             : 
    6175       12147 : static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
    6176             : {
    6177       12147 :         os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
    6178       12147 :         wpa_s->force_long_sd = 0;
    6179       12147 :         wpas_p2p_stop_find(wpa_s);
    6180       12147 :         wpa_s->parent->p2ps_method_config_any = 0;
    6181       12147 :         if (wpa_s->global->p2p)
    6182       12147 :                 p2p_flush(wpa_s->global->p2p);
    6183       12147 : }
    6184             : 
    6185             : 
    6186           6 : static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
    6187             : {
    6188             :         char *pos, *pos2;
    6189           6 :         unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
    6190             : 
    6191           6 :         if (cmd[0]) {
    6192           5 :                 pos = os_strchr(cmd, ' ');
    6193           5 :                 if (pos == NULL)
    6194           1 :                         return -1;
    6195           4 :                 *pos++ = '\0';
    6196           4 :                 dur1 = atoi(cmd);
    6197             : 
    6198           4 :                 pos2 = os_strchr(pos, ' ');
    6199           4 :                 if (pos2)
    6200           2 :                         *pos2++ = '\0';
    6201           4 :                 int1 = atoi(pos);
    6202             :         } else
    6203           1 :                 pos2 = NULL;
    6204             : 
    6205           5 :         if (pos2) {
    6206           2 :                 pos = os_strchr(pos2, ' ');
    6207           2 :                 if (pos == NULL)
    6208           1 :                         return -1;
    6209           1 :                 *pos++ = '\0';
    6210           1 :                 dur2 = atoi(pos2);
    6211           1 :                 int2 = atoi(pos);
    6212             :         }
    6213             : 
    6214           4 :         return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
    6215             : }
    6216             : 
    6217             : 
    6218          35 : static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
    6219             : {
    6220             :         char *pos;
    6221          35 :         unsigned int period = 0, interval = 0;
    6222             : 
    6223          35 :         if (cmd[0]) {
    6224          18 :                 pos = os_strchr(cmd, ' ');
    6225          18 :                 if (pos == NULL)
    6226           1 :                         return -1;
    6227          17 :                 *pos++ = '\0';
    6228          17 :                 period = atoi(cmd);
    6229          17 :                 interval = atoi(pos);
    6230             :         }
    6231             : 
    6232          34 :         return wpas_p2p_ext_listen(wpa_s, period, interval);
    6233             : }
    6234             : 
    6235             : 
    6236           7 : static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
    6237             : {
    6238             :         const char *pos;
    6239             :         u8 peer[ETH_ALEN];
    6240           7 :         int iface_addr = 0;
    6241             : 
    6242           7 :         pos = cmd;
    6243           7 :         if (os_strncmp(pos, "iface=", 6) == 0) {
    6244           2 :                 iface_addr = 1;
    6245           2 :                 pos += 6;
    6246             :         }
    6247           7 :         if (hwaddr_aton(pos, peer))
    6248           1 :                 return -1;
    6249             : 
    6250           6 :         wpas_p2p_remove_client(wpa_s, peer, iface_addr);
    6251           6 :         return 0;
    6252             : }
    6253             : 
    6254             : 
    6255           3 : static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
    6256             : {
    6257           3 :         int freq = 0, period = 0, interval = 0, count = 0;
    6258             : 
    6259           3 :         if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
    6260             :         {
    6261           2 :                 wpa_printf(MSG_DEBUG,
    6262             :                            "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
    6263           2 :                 return -1;
    6264             :         }
    6265             : 
    6266           1 :         return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
    6267             : }
    6268             : 
    6269             : #endif /* CONFIG_P2P */
    6270             : 
    6271             : 
    6272        2231 : static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
    6273             : {
    6274             :         struct wpa_freq_range_list ranges;
    6275        2231 :         int *freqs = NULL;
    6276             :         struct hostapd_hw_modes *mode;
    6277             :         u16 i;
    6278             : 
    6279        2231 :         if (wpa_s->hw.modes == NULL)
    6280           0 :                 return NULL;
    6281             : 
    6282        2231 :         os_memset(&ranges, 0, sizeof(ranges));
    6283        2231 :         if (freq_range_list_parse(&ranges, val) < 0)
    6284           2 :                 return NULL;
    6285             : 
    6286        8916 :         for (i = 0; i < wpa_s->hw.num_modes; i++) {
    6287             :                 int j;
    6288             : 
    6289        6687 :                 mode = &wpa_s->hw.modes[i];
    6290      122595 :                 for (j = 0; j < mode->num_channels; j++) {
    6291             :                         unsigned int freq;
    6292             : 
    6293      115908 :                         if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
    6294        2332 :                                 continue;
    6295             : 
    6296      113576 :                         freq = mode->channels[j].freq;
    6297      113576 :                         if (!freq_range_list_includes(&ranges, freq))
    6298      108991 :                                 continue;
    6299             : 
    6300        4585 :                         int_array_add_unique(&freqs, freq);
    6301             :                 }
    6302             :         }
    6303             : 
    6304        2229 :         os_free(ranges.range);
    6305        2229 :         return freqs;
    6306             : }
    6307             : 
    6308             : 
    6309             : #ifdef CONFIG_INTERWORKING
    6310             : 
    6311         238 : static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
    6312             : {
    6313         238 :         int auto_sel = 0;
    6314         238 :         int *freqs = NULL;
    6315             : 
    6316         238 :         if (param) {
    6317             :                 char *pos;
    6318             : 
    6319         237 :                 auto_sel = os_strstr(param, "auto") != NULL;
    6320             : 
    6321         237 :                 pos = os_strstr(param, "freq=");
    6322         237 :                 if (pos) {
    6323         236 :                         freqs = freq_range_to_channel_list(wpa_s, pos + 5);
    6324         236 :                         if (freqs == NULL)
    6325           1 :                                 return -1;
    6326             :                 }
    6327             : 
    6328             :         }
    6329             : 
    6330         237 :         return interworking_select(wpa_s, auto_sel, freqs);
    6331             : }
    6332             : 
    6333             : 
    6334         106 : static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
    6335             :                                      int only_add)
    6336             : {
    6337             :         u8 bssid[ETH_ALEN];
    6338             :         struct wpa_bss *bss;
    6339             : 
    6340         106 :         if (hwaddr_aton(dst, bssid)) {
    6341           1 :                 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
    6342           1 :                 return -1;
    6343             :         }
    6344             : 
    6345         105 :         bss = wpa_bss_get_bssid(wpa_s, bssid);
    6346         105 :         if (bss == NULL) {
    6347           6 :                 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
    6348           6 :                            MAC2STR(bssid));
    6349           1 :                 return -1;
    6350             :         }
    6351             : 
    6352         104 :         if (bss->ssid_len == 0) {
    6353           1 :                 int found = 0;
    6354             : 
    6355           6 :                 wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
    6356           6 :                            " does not have SSID information", MAC2STR(bssid));
    6357             : 
    6358           2 :                 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
    6359             :                                          list) {
    6360           4 :                         if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
    6361           2 :                             bss->ssid_len > 0) {
    6362           1 :                                 found = 1;
    6363           1 :                                 break;
    6364             :                         }
    6365             :                 }
    6366             : 
    6367           1 :                 if (!found)
    6368           0 :                         return -1;
    6369           1 :                 wpa_printf(MSG_DEBUG,
    6370             :                            "Found another matching BSS entry with SSID");
    6371             :         }
    6372             : 
    6373         104 :         return interworking_connect(wpa_s, bss, only_add);
    6374             : }
    6375             : 
    6376             : 
    6377          63 : static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
    6378             : {
    6379             :         u8 dst_addr[ETH_ALEN];
    6380             :         int used;
    6381             :         char *pos;
    6382             : #define MAX_ANQP_INFO_ID 100
    6383             :         u16 id[MAX_ANQP_INFO_ID];
    6384          63 :         size_t num_id = 0;
    6385          63 :         u32 subtypes = 0;
    6386          63 :         int get_cell_pref = 0;
    6387             : 
    6388          63 :         used = hwaddr_aton2(dst, dst_addr);
    6389          63 :         if (used < 0)
    6390           2 :                 return -1;
    6391          61 :         pos = dst + used;
    6392          61 :         if (*pos == ' ')
    6393          60 :                 pos++;
    6394         174 :         while (num_id < MAX_ANQP_INFO_ID) {
    6395         113 :                 if (os_strncmp(pos, "hs20:", 5) == 0) {
    6396             : #ifdef CONFIG_HS20
    6397          10 :                         int num = atoi(pos + 5);
    6398          10 :                         if (num <= 0 || num > 31)
    6399           6 :                                 return -1;
    6400           4 :                         subtypes |= BIT(num);
    6401             : #else /* CONFIG_HS20 */
    6402             :                         return -1;
    6403             : #endif /* CONFIG_HS20 */
    6404         103 :                 } else if (os_strncmp(pos, "mbo:", 4) == 0) {
    6405             : #ifdef CONFIG_MBO
    6406           4 :                         int num = atoi(pos + 4);
    6407           4 :                         if (num != MBO_ANQP_SUBTYPE_CELL_CONN_PREF)
    6408           3 :                                 return -1;
    6409           1 :                         get_cell_pref = 1;
    6410             : #else /* CONFIG_MBO */
    6411             :                         return -1;
    6412             : #endif /* CONFIG_MBO */
    6413             :                 } else {
    6414          99 :                         id[num_id] = atoi(pos);
    6415          99 :                         if (id[num_id])
    6416          96 :                                 num_id++;
    6417             :                 }
    6418         104 :                 pos = os_strchr(pos + 1, ',');
    6419         104 :                 if (pos == NULL)
    6420          52 :                         break;
    6421          52 :                 pos++;
    6422             :         }
    6423             : 
    6424          52 :         if (num_id == 0)
    6425           3 :                 return -1;
    6426             : 
    6427          49 :         return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes,
    6428             :                              get_cell_pref);
    6429             : }
    6430             : 
    6431             : 
    6432          25 : static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
    6433             : {
    6434             :         u8 dst_addr[ETH_ALEN];
    6435          25 :         struct wpabuf *advproto, *query = NULL;
    6436          25 :         int used, ret = -1;
    6437             :         char *pos, *end;
    6438             :         size_t len;
    6439             : 
    6440          25 :         used = hwaddr_aton2(cmd, dst_addr);
    6441          25 :         if (used < 0)
    6442           1 :                 return -1;
    6443             : 
    6444          24 :         pos = cmd + used;
    6445          72 :         while (*pos == ' ')
    6446          24 :                 pos++;
    6447             : 
    6448             :         /* Advertisement Protocol ID */
    6449          24 :         end = os_strchr(pos, ' ');
    6450          24 :         if (end)
    6451          16 :                 len = end - pos;
    6452             :         else
    6453           8 :                 len = os_strlen(pos);
    6454          24 :         if (len & 0x01)
    6455           2 :                 return -1;
    6456          22 :         len /= 2;
    6457          22 :         if (len == 0)
    6458           3 :                 return -1;
    6459          19 :         advproto = wpabuf_alloc(len);
    6460          19 :         if (advproto == NULL)
    6461           1 :                 return -1;
    6462          18 :         if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
    6463           2 :                 goto fail;
    6464             : 
    6465          16 :         if (end) {
    6466             :                 /* Optional Query Request */
    6467          14 :                 pos = end + 1;
    6468          33 :                 while (*pos == ' ')
    6469           5 :                         pos++;
    6470             : 
    6471          14 :                 len = os_strlen(pos);
    6472          14 :                 if (len) {
    6473          13 :                         if (len & 0x01)
    6474           2 :                                 goto fail;
    6475          11 :                         len /= 2;
    6476          11 :                         if (len == 0)
    6477           0 :                                 goto fail;
    6478          11 :                         query = wpabuf_alloc(len);
    6479          11 :                         if (query == NULL)
    6480           1 :                                 goto fail;
    6481          10 :                         if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
    6482           1 :                                 goto fail;
    6483             :                 }
    6484             :         }
    6485             : 
    6486          12 :         ret = gas_send_request(wpa_s, dst_addr, advproto, query);
    6487             : 
    6488             : fail:
    6489          18 :         wpabuf_free(advproto);
    6490          18 :         wpabuf_free(query);
    6491             : 
    6492          18 :         return ret;
    6493             : }
    6494             : 
    6495             : 
    6496          14 : static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
    6497             :                             size_t buflen)
    6498             : {
    6499             :         u8 addr[ETH_ALEN];
    6500             :         int dialog_token;
    6501             :         int used;
    6502             :         char *pos;
    6503             :         size_t resp_len, start, requested_len;
    6504             :         struct wpabuf *resp;
    6505             :         int ret;
    6506             : 
    6507          14 :         used = hwaddr_aton2(cmd, addr);
    6508          14 :         if (used < 0)
    6509           1 :                 return -1;
    6510             : 
    6511          13 :         pos = cmd + used;
    6512          39 :         while (*pos == ' ')
    6513          13 :                 pos++;
    6514          13 :         dialog_token = atoi(pos);
    6515             : 
    6516          26 :         if (wpa_s->last_gas_resp &&
    6517          26 :             os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
    6518          13 :             dialog_token == wpa_s->last_gas_dialog_token)
    6519          11 :                 resp = wpa_s->last_gas_resp;
    6520           3 :         else if (wpa_s->prev_gas_resp &&
    6521           2 :                  os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
    6522           1 :                  dialog_token == wpa_s->prev_gas_dialog_token)
    6523           1 :                 resp = wpa_s->prev_gas_resp;
    6524             :         else
    6525           1 :                 return -1;
    6526             : 
    6527          12 :         resp_len = wpabuf_len(resp);
    6528          12 :         start = 0;
    6529          12 :         requested_len = resp_len;
    6530             : 
    6531          12 :         pos = os_strchr(pos, ' ');
    6532          12 :         if (pos) {
    6533           5 :                 start = atoi(pos);
    6534           5 :                 if (start > resp_len)
    6535           1 :                         return os_snprintf(buf, buflen, "FAIL-Invalid range");
    6536           4 :                 pos = os_strchr(pos, ',');
    6537           4 :                 if (pos == NULL)
    6538           1 :                         return -1;
    6539           3 :                 pos++;
    6540           3 :                 requested_len = atoi(pos);
    6541           3 :                 if (start + requested_len > resp_len)
    6542           1 :                         return os_snprintf(buf, buflen, "FAIL-Invalid range");
    6543             :         }
    6544             : 
    6545           9 :         if (requested_len * 2 + 1 > buflen)
    6546           0 :                 return os_snprintf(buf, buflen, "FAIL-Too long response");
    6547             : 
    6548           9 :         ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
    6549             :                                requested_len);
    6550             : 
    6551           9 :         if (start + requested_len == resp_len) {
    6552             :                 /*
    6553             :                  * Free memory by dropping the response after it has been
    6554             :                  * fetched.
    6555             :                  */
    6556           7 :                 if (resp == wpa_s->prev_gas_resp) {
    6557           1 :                         wpabuf_free(wpa_s->prev_gas_resp);
    6558           1 :                         wpa_s->prev_gas_resp = NULL;
    6559             :                 } else {
    6560           6 :                         wpabuf_free(wpa_s->last_gas_resp);
    6561           6 :                         wpa_s->last_gas_resp = NULL;
    6562             :                 }
    6563             :         }
    6564             : 
    6565           9 :         return ret;
    6566             : }
    6567             : #endif /* CONFIG_INTERWORKING */
    6568             : 
    6569             : 
    6570             : #ifdef CONFIG_HS20
    6571             : 
    6572          24 : static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
    6573             : {
    6574             :         u8 dst_addr[ETH_ALEN];
    6575             :         int used;
    6576             :         char *pos;
    6577          24 :         u32 subtypes = 0;
    6578             : 
    6579          24 :         used = hwaddr_aton2(dst, dst_addr);
    6580          24 :         if (used < 0)
    6581           2 :                 return -1;
    6582          22 :         pos = dst + used;
    6583          22 :         if (*pos == ' ')
    6584          21 :                 pos++;
    6585             :         for (;;) {
    6586          35 :                 int num = atoi(pos);
    6587          35 :                 if (num <= 0 || num > 31)
    6588           6 :                         return -1;
    6589          29 :                 subtypes |= BIT(num);
    6590          29 :                 pos = os_strchr(pos + 1, ',');
    6591          29 :                 if (pos == NULL)
    6592          16 :                         break;
    6593          13 :                 pos++;
    6594          13 :         }
    6595             : 
    6596          16 :         if (subtypes == 0)
    6597           0 :                 return -1;
    6598             : 
    6599          16 :         return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
    6600             : }
    6601             : 
    6602             : 
    6603           3 : static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
    6604             :                                     const u8 *addr, const char *realm)
    6605             : {
    6606             :         u8 *buf;
    6607             :         size_t rlen, len;
    6608             :         int ret;
    6609             : 
    6610           3 :         rlen = os_strlen(realm);
    6611           3 :         len = 3 + rlen;
    6612           3 :         buf = os_malloc(len);
    6613           3 :         if (buf == NULL)
    6614           1 :                 return -1;
    6615           2 :         buf[0] = 1; /* NAI Home Realm Count */
    6616           2 :         buf[1] = 0; /* Formatted in accordance with RFC 4282 */
    6617           2 :         buf[2] = rlen;
    6618           2 :         os_memcpy(buf + 3, realm, rlen);
    6619             : 
    6620           2 :         ret = hs20_anqp_send_req(wpa_s, addr,
    6621             :                                  BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
    6622             :                                  buf, len, 0);
    6623             : 
    6624           2 :         os_free(buf);
    6625             : 
    6626           2 :         return ret;
    6627             : }
    6628             : 
    6629             : 
    6630           9 : static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
    6631             :                                         char *dst)
    6632             : {
    6633           9 :         struct wpa_cred *cred = wpa_s->conf->cred;
    6634             :         u8 dst_addr[ETH_ALEN];
    6635             :         int used;
    6636             :         u8 *buf;
    6637             :         size_t len;
    6638             :         int ret;
    6639             : 
    6640           9 :         used = hwaddr_aton2(dst, dst_addr);
    6641           9 :         if (used < 0)
    6642           1 :                 return -1;
    6643             : 
    6644          22 :         while (dst[used] == ' ')
    6645           6 :                 used++;
    6646           8 :         if (os_strncmp(dst + used, "realm=", 6) == 0)
    6647           2 :                 return hs20_nai_home_realm_list(wpa_s, dst_addr,
    6648           2 :                                                 dst + used + 6);
    6649             : 
    6650           6 :         len = os_strlen(dst + used);
    6651             : 
    6652           6 :         if (len == 0 && cred && cred->realm)
    6653           1 :                 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
    6654             : 
    6655           5 :         if (len & 1)
    6656           1 :                 return -1;
    6657           4 :         len /= 2;
    6658           4 :         buf = os_malloc(len);
    6659           4 :         if (buf == NULL)
    6660           1 :                 return -1;
    6661           3 :         if (hexstr2bin(dst + used, buf, len) < 0) {
    6662           1 :                 os_free(buf);
    6663           1 :                 return -1;
    6664             :         }
    6665             : 
    6666           2 :         ret = hs20_anqp_send_req(wpa_s, dst_addr,
    6667             :                                  BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
    6668             :                                  buf, len, 0);
    6669           2 :         os_free(buf);
    6670             : 
    6671           2 :         return ret;
    6672             : }
    6673             : 
    6674             : 
    6675          32 : static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
    6676             :                          int buflen)
    6677             : {
    6678             :         u8 dst_addr[ETH_ALEN];
    6679             :         int used;
    6680          32 :         char *ctx = NULL, *icon, *poffset, *psize;
    6681             : 
    6682          32 :         used = hwaddr_aton2(cmd, dst_addr);
    6683          32 :         if (used < 0)
    6684           0 :                 return -1;
    6685          32 :         cmd += used;
    6686             : 
    6687          32 :         icon = str_token(cmd, " ", &ctx);
    6688          32 :         poffset = str_token(cmd, " ", &ctx);
    6689          32 :         psize = str_token(cmd, " ", &ctx);
    6690          32 :         if (!icon || !poffset || !psize)
    6691           0 :                 return -1;
    6692             : 
    6693          32 :         wpa_s->fetch_osu_icon_in_progress = 0;
    6694          32 :         return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
    6695             :                              reply, buflen);
    6696             : }
    6697             : 
    6698             : 
    6699          12 : static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
    6700             : {
    6701             :         u8 dst_addr[ETH_ALEN];
    6702             :         int used;
    6703             :         char *icon;
    6704             : 
    6705          12 :         if (!cmd[0])
    6706           4 :                 return hs20_del_icon(wpa_s, NULL, NULL);
    6707             : 
    6708           8 :         used = hwaddr_aton2(cmd, dst_addr);
    6709           8 :         if (used < 0)
    6710           1 :                 return -1;
    6711             : 
    6712          20 :         while (cmd[used] == ' ')
    6713           6 :                 used++;
    6714           7 :         icon = cmd[used] ? &cmd[used] : NULL;
    6715             : 
    6716           7 :         return hs20_del_icon(wpa_s, dst_addr, icon);
    6717             : }
    6718             : 
    6719             : 
    6720          27 : static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
    6721             : {
    6722             :         u8 dst_addr[ETH_ALEN];
    6723             :         int used;
    6724             :         char *icon;
    6725             : 
    6726          27 :         used = hwaddr_aton2(cmd, dst_addr);
    6727          27 :         if (used < 0)
    6728           1 :                 return -1;
    6729             : 
    6730          78 :         while (cmd[used] == ' ')
    6731          26 :                 used++;
    6732          26 :         icon = &cmd[used];
    6733             : 
    6734          26 :         wpa_s->fetch_osu_icon_in_progress = 0;
    6735          26 :         return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
    6736             :                                   (u8 *) icon, os_strlen(icon), inmem);
    6737             : }
    6738             : 
    6739             : #endif /* CONFIG_HS20 */
    6740             : 
    6741             : 
    6742             : #ifdef CONFIG_AUTOSCAN
    6743             : 
    6744          12 : static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
    6745             :                                               char *cmd)
    6746             : {
    6747          12 :         enum wpa_states state = wpa_s->wpa_state;
    6748          12 :         char *new_params = NULL;
    6749             : 
    6750          12 :         if (os_strlen(cmd) > 0) {
    6751           6 :                 new_params = os_strdup(cmd);
    6752           6 :                 if (new_params == NULL)
    6753           1 :                         return -1;
    6754             :         }
    6755             : 
    6756          11 :         os_free(wpa_s->conf->autoscan);
    6757          11 :         wpa_s->conf->autoscan = new_params;
    6758             : 
    6759          11 :         if (wpa_s->conf->autoscan == NULL)
    6760           6 :                 autoscan_deinit(wpa_s);
    6761           5 :         else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
    6762           4 :                 autoscan_init(wpa_s, 1);
    6763           1 :         else if (state == WPA_SCANNING)
    6764           1 :                 wpa_supplicant_reinit_autoscan(wpa_s);
    6765             : 
    6766          11 :         return 0;
    6767             : }
    6768             : 
    6769             : #endif /* CONFIG_AUTOSCAN */
    6770             : 
    6771             : 
    6772             : #ifdef CONFIG_WNM
    6773             : 
    6774          24 : static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
    6775             : {
    6776             :         int enter;
    6777          24 :         int intval = 0;
    6778             :         char *pos;
    6779             :         int ret;
    6780          24 :         struct wpabuf *tfs_req = NULL;
    6781             : 
    6782          24 :         if (os_strncmp(cmd, "enter", 5) == 0)
    6783          16 :                 enter = 1;
    6784           8 :         else if (os_strncmp(cmd, "exit", 4) == 0)
    6785           7 :                 enter = 0;
    6786             :         else
    6787           1 :                 return -1;
    6788             : 
    6789          23 :         pos = os_strstr(cmd, " interval=");
    6790          23 :         if (pos)
    6791           3 :                 intval = atoi(pos + 10);
    6792             : 
    6793          23 :         pos = os_strstr(cmd, " tfs_req=");
    6794          23 :         if (pos) {
    6795             :                 char *end;
    6796             :                 size_t len;
    6797           8 :                 pos += 9;
    6798           8 :                 end = os_strchr(pos, ' ');
    6799           8 :                 if (end)
    6800           2 :                         len = end - pos;
    6801             :                 else
    6802           6 :                         len = os_strlen(pos);
    6803           8 :                 if (len & 1)
    6804           1 :                         return -1;
    6805           7 :                 len /= 2;
    6806           7 :                 tfs_req = wpabuf_alloc(len);
    6807           7 :                 if (tfs_req == NULL)
    6808           1 :                         return -1;
    6809           6 :                 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
    6810           1 :                         wpabuf_free(tfs_req);
    6811           1 :                         return -1;
    6812             :                 }
    6813             :         }
    6814             : 
    6815          20 :         ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
    6816             :                                            WNM_SLEEP_MODE_EXIT, intval,
    6817             :                                            tfs_req);
    6818          20 :         wpabuf_free(tfs_req);
    6819             : 
    6820          20 :         return ret;
    6821             : }
    6822             : 
    6823             : 
    6824           2 : static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
    6825             : {
    6826           2 :         int query_reason, list = 0;
    6827             : 
    6828           2 :         query_reason = atoi(cmd);
    6829             : 
    6830           2 :         cmd = os_strchr(cmd, ' ');
    6831           2 :         if (cmd) {
    6832           1 :                 cmd++;
    6833           1 :                 if (os_strncmp(cmd, "list", 4) == 0) {
    6834           1 :                         list = 1;
    6835             :                 } else {
    6836           0 :                         wpa_printf(MSG_DEBUG, "WNM Query: Invalid option %s",
    6837             :                                    cmd);
    6838           0 :                         return -1;
    6839             :                 }
    6840             :         }
    6841             : 
    6842           2 :         wpa_printf(MSG_DEBUG,
    6843             :                    "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
    6844             :                    query_reason, list ? " candidate list" : "");
    6845             : 
    6846           2 :         return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason, list);
    6847             : }
    6848             : 
    6849             : #endif /* CONFIG_WNM */
    6850             : 
    6851             : 
    6852          20 : static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
    6853             :                                       size_t buflen)
    6854             : {
    6855             :         struct wpa_signal_info si;
    6856             :         int ret;
    6857             :         char *pos, *end;
    6858             : 
    6859          20 :         ret = wpa_drv_signal_poll(wpa_s, &si);
    6860          20 :         if (ret)
    6861           0 :                 return -1;
    6862             : 
    6863          20 :         pos = buf;
    6864          20 :         end = buf + buflen;
    6865             : 
    6866          40 :         ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
    6867             :                           "NOISE=%d\nFREQUENCY=%u\n",
    6868          20 :                           si.current_signal, si.current_txrate / 1000,
    6869             :                           si.current_noise, si.frequency);
    6870          20 :         if (os_snprintf_error(end - pos, ret))
    6871           0 :                 return -1;
    6872          20 :         pos += ret;
    6873             : 
    6874          20 :         if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
    6875          19 :                 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
    6876             :                                   channel_width_to_string(si.chanwidth));
    6877          19 :                 if (os_snprintf_error(end - pos, ret))
    6878           0 :                         return -1;
    6879          19 :                 pos += ret;
    6880             :         }
    6881             : 
    6882          20 :         if (si.center_frq1 > 0 && si.center_frq2 > 0) {
    6883           9 :                 ret = os_snprintf(pos, end - pos,
    6884             :                                   "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
    6885             :                                   si.center_frq1, si.center_frq2);
    6886           9 :                 if (os_snprintf_error(end - pos, ret))
    6887           0 :                         return -1;
    6888           9 :                 pos += ret;
    6889             :         }
    6890             : 
    6891          20 :         if (si.avg_signal) {
    6892          13 :                 ret = os_snprintf(pos, end - pos,
    6893             :                                   "AVG_RSSI=%d\n", si.avg_signal);
    6894          13 :                 if (os_snprintf_error(end - pos, ret))
    6895           0 :                         return -1;
    6896          13 :                 pos += ret;
    6897             :         }
    6898             : 
    6899          20 :         if (si.avg_beacon_signal) {
    6900          13 :                 ret = os_snprintf(pos, end - pos,
    6901             :                                   "AVG_BEACON_RSSI=%d\n", si.avg_beacon_signal);
    6902          13 :                 if (os_snprintf_error(end - pos, ret))
    6903           0 :                         return -1;
    6904          13 :                 pos += ret;
    6905             :         }
    6906             : 
    6907          20 :         return pos - buf;
    6908             : }
    6909             : 
    6910             : 
    6911           8 : static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
    6912             :                                           const char *cmd)
    6913             : {
    6914             :         const char *pos;
    6915           8 :         int threshold = 0;
    6916           8 :         int hysteresis = 0;
    6917             : 
    6918           8 :         if (wpa_s->bgscan && wpa_s->bgscan_priv) {
    6919           2 :                 wpa_printf(MSG_DEBUG,
    6920             :                            "Reject SIGNAL_MONITOR command - bgscan is active");
    6921           2 :                 return -1;
    6922             :         }
    6923           6 :         pos = os_strstr(cmd, "THRESHOLD=");
    6924           6 :         if (pos)
    6925           3 :                 threshold = atoi(pos + 10);
    6926           6 :         pos = os_strstr(cmd, "HYSTERESIS=");
    6927           6 :         if (pos)
    6928           2 :                 hysteresis = atoi(pos + 11);
    6929           6 :         return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
    6930             : }
    6931             : 
    6932             : 
    6933           0 : static int wpas_ctrl_iface_get_pref_freq_list(
    6934             :         struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
    6935             : {
    6936           0 :         unsigned int freq_list[100], num = 100, i;
    6937             :         int ret;
    6938             :         enum wpa_driver_if_type iface_type;
    6939             :         char *pos, *end;
    6940             : 
    6941           0 :         pos = buf;
    6942           0 :         end = buf + buflen;
    6943             : 
    6944             :         /* buf: "<interface_type>" */
    6945           0 :         if (os_strcmp(cmd, "STATION") == 0)
    6946           0 :                 iface_type = WPA_IF_STATION;
    6947           0 :         else if (os_strcmp(cmd, "AP") == 0)
    6948           0 :                 iface_type = WPA_IF_AP_BSS;
    6949           0 :         else if (os_strcmp(cmd, "P2P_GO") == 0)
    6950           0 :                 iface_type = WPA_IF_P2P_GO;
    6951           0 :         else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
    6952           0 :                 iface_type = WPA_IF_P2P_CLIENT;
    6953           0 :         else if (os_strcmp(cmd, "IBSS") == 0)
    6954           0 :                 iface_type = WPA_IF_IBSS;
    6955           0 :         else if (os_strcmp(cmd, "TDLS") == 0)
    6956           0 :                 iface_type = WPA_IF_TDLS;
    6957             :         else
    6958           0 :                 return -1;
    6959             : 
    6960           0 :         wpa_printf(MSG_DEBUG,
    6961             :                    "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
    6962             :                    iface_type, buf);
    6963             : 
    6964           0 :         ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
    6965           0 :         if (ret)
    6966           0 :                 return -1;
    6967             : 
    6968           0 :         for (i = 0; i < num; i++) {
    6969           0 :                 ret = os_snprintf(pos, end - pos, "%s%u",
    6970             :                                   i > 0 ? "," : "", freq_list[i]);
    6971           0 :                 if (os_snprintf_error(end - pos, ret))
    6972           0 :                         return -1;
    6973           0 :                 pos += ret;
    6974             :         }
    6975             : 
    6976           0 :         return pos - buf;
    6977             : }
    6978             : 
    6979             : 
    6980           1 : static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
    6981             :                                         char *buf, size_t buflen)
    6982             : {
    6983             :         int ret, i;
    6984             :         char *pos, *end;
    6985             : 
    6986           1 :         ret = os_snprintf(buf, buflen, "%016llX:\n",
    6987           1 :                           (long long unsigned) wpa_s->drv_flags);
    6988           1 :         if (os_snprintf_error(buflen, ret))
    6989           0 :                 return -1;
    6990             : 
    6991           1 :         pos = buf + ret;
    6992           1 :         end = buf + buflen;
    6993             : 
    6994          65 :         for (i = 0; i < 64; i++) {
    6995          64 :                 if (wpa_s->drv_flags & (1LLU << i)) {
    6996          26 :                         ret = os_snprintf(pos, end - pos, "%s\n",
    6997          26 :                                           driver_flag_to_string(1LLU << i));
    6998          26 :                         if (os_snprintf_error(end - pos, ret))
    6999           0 :                                 return -1;
    7000          26 :                         pos += ret;
    7001             :                 }
    7002             :         }
    7003             : 
    7004           1 :         return pos - buf;
    7005             : }
    7006             : 
    7007             : 
    7008           1 : static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
    7009             :                                       size_t buflen)
    7010             : {
    7011             :         struct hostap_sta_driver_data sta;
    7012             :         int ret;
    7013             : 
    7014           1 :         ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
    7015           1 :         if (ret)
    7016           0 :                 return -1;
    7017             : 
    7018           1 :         ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
    7019             :                           sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
    7020           1 :         if (os_snprintf_error(buflen, ret))
    7021           0 :                 return -1;
    7022           1 :         return ret;
    7023             : }
    7024             : 
    7025             : 
    7026             : #ifdef ANDROID
    7027             : static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
    7028             :                                      char *buf, size_t buflen)
    7029             : {
    7030             :         int ret;
    7031             : 
    7032             :         ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
    7033             :         if (ret == 0) {
    7034             :                 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
    7035             :                         struct p2p_data *p2p = wpa_s->global->p2p;
    7036             :                         if (p2p) {
    7037             :                                 char country[3];
    7038             :                                 country[0] = cmd[8];
    7039             :                                 country[1] = cmd[9];
    7040             :                                 country[2] = 0x04;
    7041             :                                 p2p_set_country(p2p, country);
    7042             :                         }
    7043             :                 }
    7044             :                 ret = os_snprintf(buf, buflen, "%s\n", "OK");
    7045             :                 if (os_snprintf_error(buflen, ret))
    7046             :                         ret = -1;
    7047             :         }
    7048             :         return ret;
    7049             : }
    7050             : #endif /* ANDROID */
    7051             : 
    7052             : 
    7053          12 : static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
    7054             :                                      char *buf, size_t buflen)
    7055             : {
    7056             :         int ret;
    7057             :         char *pos;
    7058          12 :         u8 *data = NULL;
    7059             :         unsigned int vendor_id, subcmd;
    7060             :         struct wpabuf *reply;
    7061          12 :         size_t data_len = 0;
    7062             : 
    7063             :         /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
    7064          12 :         vendor_id = strtoul(cmd, &pos, 16);
    7065          12 :         if (!isblank((unsigned char) *pos))
    7066           2 :                 return -EINVAL;
    7067             : 
    7068          10 :         subcmd = strtoul(pos, &pos, 10);
    7069             : 
    7070          10 :         if (*pos != '\0') {
    7071          10 :                 if (!isblank((unsigned char) *pos++))
    7072           1 :                         return -EINVAL;
    7073           9 :                 data_len = os_strlen(pos);
    7074             :         }
    7075             : 
    7076           9 :         if (data_len) {
    7077           9 :                 data_len /= 2;
    7078           9 :                 data = os_malloc(data_len);
    7079           9 :                 if (!data)
    7080           1 :                         return -1;
    7081             : 
    7082           8 :                 if (hexstr2bin(pos, data, data_len)) {
    7083           2 :                         wpa_printf(MSG_DEBUG,
    7084             :                                    "Vendor command: wrong parameter format");
    7085           2 :                         os_free(data);
    7086           2 :                         return -EINVAL;
    7087             :                 }
    7088             :         }
    7089             : 
    7090           6 :         reply = wpabuf_alloc((buflen - 1) / 2);
    7091           6 :         if (!reply) {
    7092           1 :                 os_free(data);
    7093           1 :                 return -1;
    7094             :         }
    7095             : 
    7096           5 :         ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
    7097             :                                  reply);
    7098             : 
    7099           5 :         if (ret == 0)
    7100           5 :                 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
    7101             :                                        wpabuf_len(reply));
    7102             : 
    7103           5 :         wpabuf_free(reply);
    7104           5 :         os_free(data);
    7105             : 
    7106           5 :         return ret;
    7107             : }
    7108             : 
    7109             : 
    7110        6060 : static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
    7111             : {
    7112             : #ifdef CONFIG_P2P
    7113       12120 :         struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
    7114        6060 :                 wpa_s->global->p2p_init_wpa_s : wpa_s;
    7115             : #endif /* CONFIG_P2P */
    7116             : 
    7117        6060 :         wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
    7118             : 
    7119        6060 :         wpas_abort_ongoing_scan(wpa_s);
    7120             : 
    7121        6060 :         if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
    7122             :                 /*
    7123             :                  * Avoid possible auto connect re-connection on getting
    7124             :                  * disconnected due to state flush.
    7125             :                  */
    7126         960 :                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
    7127             :         }
    7128             : 
    7129             : #ifdef CONFIG_P2P
    7130        6060 :         wpas_p2p_group_remove(p2p_wpa_s, "*");
    7131        6060 :         wpas_p2p_cancel(p2p_wpa_s);
    7132        6060 :         p2p_ctrl_flush(p2p_wpa_s);
    7133        6060 :         wpas_p2p_service_flush(p2p_wpa_s);
    7134        6060 :         p2p_wpa_s->global->p2p_disabled = 0;
    7135        6060 :         p2p_wpa_s->global->p2p_per_sta_psk = 0;
    7136        6060 :         p2p_wpa_s->conf->num_sec_device_types = 0;
    7137        6060 :         p2p_wpa_s->p2p_disable_ip_addr_req = 0;
    7138        6060 :         os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
    7139        6060 :         p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
    7140        6060 :         p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
    7141        6060 :         p2p_wpa_s->global->pending_p2ps_group = 0;
    7142        6060 :         p2p_wpa_s->global->pending_p2ps_group_freq = 0;
    7143             : #endif /* CONFIG_P2P */
    7144             : 
    7145             : #ifdef CONFIG_WPS_TESTING
    7146        6060 :         wps_version_number = 0x20;
    7147        6060 :         wps_testing_dummy_cred = 0;
    7148        6060 :         wps_corrupt_pkhash = 0;
    7149        6060 :         wps_force_auth_types_in_use = 0;
    7150        6060 :         wps_force_encr_types_in_use = 0;
    7151             : #endif /* CONFIG_WPS_TESTING */
    7152             : #ifdef CONFIG_WPS
    7153        6060 :         wpa_s->wps_fragment_size = 0;
    7154        6060 :         wpas_wps_cancel(wpa_s);
    7155        6060 :         wps_registrar_flush(wpa_s->wps->registrar);
    7156             : #endif /* CONFIG_WPS */
    7157        6060 :         wpa_s->after_wps = 0;
    7158        6060 :         wpa_s->known_wps_freq = 0;
    7159             : 
    7160             : #ifdef CONFIG_TDLS
    7161             : #ifdef CONFIG_TDLS_TESTING
    7162        6060 :         tdls_testing = 0;
    7163             : #endif /* CONFIG_TDLS_TESTING */
    7164        6060 :         wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
    7165        6060 :         wpa_tdls_enable(wpa_s->wpa, 1);
    7166             : #endif /* CONFIG_TDLS */
    7167             : 
    7168        6060 :         eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
    7169        6060 :         wpa_supplicant_stop_countermeasures(wpa_s, NULL);
    7170             : 
    7171        6060 :         wpa_s->no_keep_alive = 0;
    7172        6060 :         wpa_s->own_disconnect_req = 0;
    7173             : 
    7174        6060 :         os_free(wpa_s->disallow_aps_bssid);
    7175        6060 :         wpa_s->disallow_aps_bssid = NULL;
    7176        6060 :         wpa_s->disallow_aps_bssid_count = 0;
    7177        6060 :         os_free(wpa_s->disallow_aps_ssid);
    7178        6060 :         wpa_s->disallow_aps_ssid = NULL;
    7179        6060 :         wpa_s->disallow_aps_ssid_count = 0;
    7180             : 
    7181        6060 :         wpa_s->set_sta_uapsd = 0;
    7182        6060 :         wpa_s->sta_uapsd = 0;
    7183             : 
    7184        6060 :         wpa_drv_radio_disable(wpa_s, 0);
    7185        6060 :         wpa_blacklist_clear(wpa_s);
    7186        6060 :         wpa_s->extra_blacklist_count = 0;
    7187        6060 :         wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
    7188        6060 :         wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
    7189        6060 :         wpa_config_flush_blobs(wpa_s->conf);
    7190        6060 :         wpa_s->conf->auto_interworking = 0;
    7191        6060 :         wpa_s->conf->okc = 0;
    7192             : 
    7193        6060 :         wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
    7194        6060 :         rsn_preauth_deinit(wpa_s->wpa);
    7195             : 
    7196        6060 :         wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
    7197        6060 :         wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
    7198        6060 :         wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
    7199        6060 :         eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
    7200             : 
    7201        6060 :         radio_remove_works(wpa_s, NULL, 1);
    7202        6060 :         wpa_s->ext_work_in_progress = 0;
    7203             : 
    7204        6060 :         wpa_s->next_ssid = NULL;
    7205             : 
    7206             : #ifdef CONFIG_INTERWORKING
    7207             : #ifdef CONFIG_HS20
    7208        6060 :         hs20_cancel_fetch_osu(wpa_s);
    7209        6060 :         hs20_del_icon(wpa_s, NULL, NULL);
    7210             : #endif /* CONFIG_HS20 */
    7211             : #endif /* CONFIG_INTERWORKING */
    7212             : 
    7213        6060 :         wpa_s->ext_mgmt_frame_handling = 0;
    7214        6060 :         wpa_s->ext_eapol_frame_io = 0;
    7215             : #ifdef CONFIG_TESTING_OPTIONS
    7216        6060 :         wpa_s->extra_roc_dur = 0;
    7217        6060 :         wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
    7218        6060 :         wpa_s->p2p_go_csa_on_inv = 0;
    7219        6060 :         wpa_s->ignore_auth_resp = 0;
    7220        6060 :         wpa_s->ignore_assoc_disallow = 0;
    7221        6060 :         wpa_s->reject_btm_req_reason = 0;
    7222        6060 :         wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
    7223             : #endif /* CONFIG_TESTING_OPTIONS */
    7224             : 
    7225        6060 :         wpa_s->disconnected = 0;
    7226        6060 :         os_free(wpa_s->next_scan_freqs);
    7227        6060 :         wpa_s->next_scan_freqs = NULL;
    7228             : 
    7229        6060 :         wpa_bss_flush(wpa_s);
    7230        6060 :         if (!dl_list_empty(&wpa_s->bss)) {
    7231          12 :                 wpa_printf(MSG_DEBUG,
    7232             :                            "BSS table not empty after flush: %u entries, current_bss=%p bssid="
    7233             :                            MACSTR " pending_bssid=" MACSTR,
    7234             :                            dl_list_len(&wpa_s->bss), wpa_s->current_bss,
    7235           6 :                            MAC2STR(wpa_s->bssid),
    7236           6 :                            MAC2STR(wpa_s->pending_bssid));
    7237             :         }
    7238             : 
    7239        6060 :         eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
    7240        6060 :         wpa_s->wnmsleep_used = 0;
    7241             : 
    7242             : #ifdef CONFIG_SME
    7243        6060 :         wpa_s->sme.last_unprot_disconnect.sec = 0;
    7244             : #endif /* CONFIG_SME */
    7245        6060 : }
    7246             : 
    7247             : 
    7248           8 : static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
    7249             :                                      char *buf, size_t buflen)
    7250             : {
    7251             :         struct wpa_radio_work *work;
    7252             :         char *pos, *end;
    7253             :         struct os_reltime now, diff;
    7254             : 
    7255           8 :         pos = buf;
    7256           8 :         end = buf + buflen;
    7257             : 
    7258           8 :         os_get_reltime(&now);
    7259             : 
    7260          17 :         dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
    7261             :         {
    7262             :                 int ret;
    7263             : 
    7264           9 :                 os_reltime_sub(&now, &work->time, &diff);
    7265          27 :                 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
    7266           9 :                                   work->type, work->wpa_s->ifname, work->freq,
    7267           9 :                                   work->started, diff.sec, diff.usec);
    7268           9 :                 if (os_snprintf_error(end - pos, ret))
    7269           0 :                         break;
    7270           9 :                 pos += ret;
    7271             :         }
    7272             : 
    7273           8 :         return pos - buf;
    7274             : }
    7275             : 
    7276             : 
    7277           1 : static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
    7278             : {
    7279           1 :         struct wpa_radio_work *work = eloop_ctx;
    7280           1 :         struct wpa_external_work *ework = work->ctx;
    7281             : 
    7282           1 :         wpa_dbg(work->wpa_s, MSG_DEBUG,
    7283             :                 "Timing out external radio work %u (%s)",
    7284             :                 ework->id, work->type);
    7285           1 :         wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
    7286           1 :         work->wpa_s->ext_work_in_progress = 0;
    7287           1 :         radio_work_done(work);
    7288           1 :         os_free(ework);
    7289           1 : }
    7290             : 
    7291             : 
    7292          22 : static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
    7293             : {
    7294          22 :         struct wpa_external_work *ework = work->ctx;
    7295             : 
    7296          22 :         if (deinit) {
    7297           5 :                 if (work->started)
    7298           1 :                         eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
    7299             :                                              work, NULL);
    7300             : 
    7301             :                 /*
    7302             :                  * work->type points to a buffer in ework, so need to replace
    7303             :                  * that here with a fixed string to avoid use of freed memory
    7304             :                  * in debug prints.
    7305             :                  */
    7306           5 :                 work->type = "freed-ext-work";
    7307           5 :                 work->ctx = NULL;
    7308           5 :                 os_free(ework);
    7309          27 :                 return;
    7310             :         }
    7311             : 
    7312          17 :         wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
    7313             :                 ework->id, ework->type);
    7314          17 :         wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
    7315          17 :         work->wpa_s->ext_work_in_progress = 1;
    7316          17 :         if (!ework->timeout)
    7317          16 :                 ework->timeout = 10;
    7318          17 :         eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
    7319             :                                work, NULL);
    7320             : }
    7321             : 
    7322             : 
    7323          24 : static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
    7324             :                                     char *buf, size_t buflen)
    7325             : {
    7326             :         struct wpa_external_work *ework;
    7327             :         char *pos, *pos2;
    7328             :         size_t type_len;
    7329             :         int ret;
    7330          24 :         unsigned int freq = 0;
    7331             : 
    7332             :         /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
    7333             : 
    7334          24 :         ework = os_zalloc(sizeof(*ework));
    7335          24 :         if (ework == NULL)
    7336           1 :                 return -1;
    7337             : 
    7338          23 :         pos = os_strchr(cmd, ' ');
    7339          23 :         if (pos) {
    7340           2 :                 type_len = pos - cmd;
    7341           2 :                 pos++;
    7342             : 
    7343           2 :                 pos2 = os_strstr(pos, "freq=");
    7344           2 :                 if (pos2)
    7345           1 :                         freq = atoi(pos2 + 5);
    7346             : 
    7347           2 :                 pos2 = os_strstr(pos, "timeout=");
    7348           2 :                 if (pos2)
    7349           1 :                         ework->timeout = atoi(pos2 + 8);
    7350             :         } else {
    7351          21 :                 type_len = os_strlen(cmd);
    7352             :         }
    7353          23 :         if (4 + type_len >= sizeof(ework->type))
    7354           5 :                 type_len = sizeof(ework->type) - 4 - 1;
    7355          23 :         os_strlcpy(ework->type, "ext:", sizeof(ework->type));
    7356          23 :         os_memcpy(ework->type + 4, cmd, type_len);
    7357          23 :         ework->type[4 + type_len] = '\0';
    7358             : 
    7359          23 :         wpa_s->ext_work_id++;
    7360          23 :         if (wpa_s->ext_work_id == 0)
    7361           0 :                 wpa_s->ext_work_id++;
    7362          23 :         ework->id = wpa_s->ext_work_id;
    7363             : 
    7364          23 :         if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
    7365             :                            ework) < 0) {
    7366           1 :                 os_free(ework);
    7367           1 :                 return -1;
    7368             :         }
    7369             : 
    7370          22 :         ret = os_snprintf(buf, buflen, "%u", ework->id);
    7371          22 :         if (os_snprintf_error(buflen, ret))
    7372           0 :                 return -1;
    7373          22 :         return ret;
    7374             : }
    7375             : 
    7376             : 
    7377          14 : static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
    7378             : {
    7379             :         struct wpa_radio_work *work;
    7380          14 :         unsigned int id = atoi(cmd);
    7381             : 
    7382          29 :         dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
    7383             :         {
    7384             :                 struct wpa_external_work *ework;
    7385             : 
    7386          28 :                 if (os_strncmp(work->type, "ext:", 4) != 0)
    7387           9 :                         continue;
    7388          19 :                 ework = work->ctx;
    7389          19 :                 if (id && ework->id != id)
    7390           6 :                         continue;
    7391          13 :                 wpa_dbg(wpa_s, MSG_DEBUG,
    7392             :                         "Completed external radio work %u (%s)",
    7393             :                         ework->id, ework->type);
    7394          13 :                 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
    7395          13 :                 wpa_s->ext_work_in_progress = 0;
    7396          13 :                 radio_work_done(work);
    7397          13 :                 os_free(ework);
    7398          13 :                 return 3; /* "OK\n" */
    7399             :         }
    7400             : 
    7401           1 :         return -1;
    7402             : }
    7403             : 
    7404             : 
    7405          47 : static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
    7406             :                                 char *buf, size_t buflen)
    7407             : {
    7408          47 :         if (os_strcmp(cmd, "show") == 0)
    7409           8 :                 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
    7410          39 :         if (os_strncmp(cmd, "add ", 4) == 0)
    7411          24 :                 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
    7412          15 :         if (os_strncmp(cmd, "done ", 5) == 0)
    7413          14 :                 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
    7414           1 :         return -1;
    7415             : }
    7416             : 
    7417             : 
    7418         733 : void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
    7419             : {
    7420             :         struct wpa_radio_work *work, *tmp;
    7421             : 
    7422         733 :         if (!wpa_s || !wpa_s->radio)
    7423         756 :                 return;
    7424             : 
    7425         724 :         dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
    7426             :                               struct wpa_radio_work, list) {
    7427             :                 struct wpa_external_work *ework;
    7428             : 
    7429          14 :                 if (os_strncmp(work->type, "ext:", 4) != 0)
    7430          11 :                         continue;
    7431           3 :                 ework = work->ctx;
    7432           3 :                 wpa_dbg(wpa_s, MSG_DEBUG,
    7433             :                         "Flushing%s external radio work %u (%s)",
    7434             :                         work->started ? " started" : "", ework->id,
    7435             :                         ework->type);
    7436           3 :                 if (work->started)
    7437           3 :                         eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
    7438             :                                              work, NULL);
    7439           3 :                 radio_work_done(work);
    7440           3 :                 os_free(ework);
    7441             :         }
    7442             : }
    7443             : 
    7444             : 
    7445          82 : static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
    7446             : {
    7447          82 :         struct wpa_supplicant *wpa_s = eloop_ctx;
    7448          82 :         eapol_sm_notify_ctrl_response(wpa_s->eapol);
    7449          82 : }
    7450             : 
    7451             : 
    7452           4 : static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
    7453             :                               unsigned int *scan_id_count, int scan_id[])
    7454             : {
    7455           4 :         const char *pos = value;
    7456             : 
    7457          31 :         while (pos) {
    7458          24 :                 if (*pos == ' ' || *pos == '\0')
    7459             :                         break;
    7460          24 :                 if (*scan_id_count == MAX_SCAN_ID)
    7461           1 :                         return -1;
    7462          23 :                 scan_id[(*scan_id_count)++] = atoi(pos);
    7463          23 :                 pos = os_strchr(pos, ',');
    7464          23 :                 if (pos)
    7465          20 :                         pos++;
    7466             :         }
    7467             : 
    7468           3 :         return 0;
    7469             : }
    7470             : 
    7471             : 
    7472        1980 : static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
    7473             :                            char *reply, int reply_size, int *reply_len)
    7474             : {
    7475             :         char *pos;
    7476        1980 :         unsigned int manual_scan_passive = 0;
    7477        1980 :         unsigned int manual_scan_use_id = 0;
    7478        1980 :         unsigned int manual_scan_only_new = 0;
    7479        1980 :         unsigned int scan_only = 0;
    7480        1980 :         unsigned int scan_id_count = 0;
    7481             :         int scan_id[MAX_SCAN_ID];
    7482             :         void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
    7483             :                                  struct wpa_scan_results *scan_res);
    7484        1980 :         int *manual_scan_freqs = NULL;
    7485        1980 :         struct wpa_ssid_value *ssid = NULL, *ns;
    7486        1980 :         unsigned int ssid_count = 0;
    7487             : 
    7488        1980 :         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
    7489           1 :                 *reply_len = -1;
    7490          14 :                 return;
    7491             :         }
    7492             : 
    7493        1979 :         if (radio_work_pending(wpa_s, "scan")) {
    7494          11 :                 wpa_printf(MSG_DEBUG,
    7495             :                            "Pending scan scheduled - reject new request");
    7496          11 :                 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
    7497          11 :                 return;
    7498             :         }
    7499             : 
    7500             : #ifdef CONFIG_INTERWORKING
    7501        1968 :         if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
    7502           1 :                 wpa_printf(MSG_DEBUG,
    7503             :                            "Interworking select in progress - reject new scan");
    7504           1 :                 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
    7505           1 :                 return;
    7506             :         }
    7507             : #endif /* CONFIG_INTERWORKING */
    7508             : 
    7509        1967 :         if (params) {
    7510        1943 :                 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
    7511        1271 :                         scan_only = 1;
    7512             : 
    7513        1943 :                 pos = os_strstr(params, "freq=");
    7514        1943 :                 if (pos) {
    7515        1928 :                         manual_scan_freqs = freq_range_to_channel_list(wpa_s,
    7516             :                                                                        pos + 5);
    7517        1928 :                         if (manual_scan_freqs == NULL) {
    7518           1 :                                 *reply_len = -1;
    7519           1 :                                 goto done;
    7520             :                         }
    7521             :                 }
    7522             : 
    7523        1942 :                 pos = os_strstr(params, "passive=");
    7524        1942 :                 if (pos)
    7525          20 :                         manual_scan_passive = !!atoi(pos + 8);
    7526             : 
    7527        1942 :                 pos = os_strstr(params, "use_id=");
    7528        1942 :                 if (pos)
    7529          33 :                         manual_scan_use_id = atoi(pos + 7);
    7530             : 
    7531        1942 :                 pos = os_strstr(params, "only_new=1");
    7532        1942 :                 if (pos)
    7533         951 :                         manual_scan_only_new = 1;
    7534             : 
    7535        1942 :                 pos = os_strstr(params, "scan_id=");
    7536        1942 :                 if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
    7537             :                                               scan_id) < 0) {
    7538           1 :                         *reply_len = -1;
    7539           1 :                         goto done;
    7540             :                 }
    7541             : 
    7542        1941 :                 pos = params;
    7543        8095 :                 while (pos && *pos != '\0') {
    7544        4215 :                         if (os_strncmp(pos, "ssid ", 5) == 0) {
    7545             :                                 char *end;
    7546             : 
    7547          10 :                                 pos += 5;
    7548          10 :                                 end = pos;
    7549          99 :                                 while (*end) {
    7550          86 :                                         if (*end == '\0' || *end == ' ')
    7551             :                                                 break;
    7552          79 :                                         end++;
    7553             :                                 }
    7554             : 
    7555          10 :                                 ns = os_realloc_array(
    7556          10 :                                         ssid, ssid_count + 1,
    7557             :                                         sizeof(struct wpa_ssid_value));
    7558          10 :                                 if (ns == NULL) {
    7559           1 :                                         *reply_len = -1;
    7560           1 :                                         goto done;
    7561             :                                 }
    7562           9 :                                 ssid = ns;
    7563             : 
    7564          17 :                                 if ((end - pos) & 0x01 ||
    7565          16 :                                     end - pos > 2 * SSID_MAX_LEN ||
    7566           8 :                                     hexstr2bin(pos, ssid[ssid_count].ssid,
    7567           8 :                                                (end - pos) / 2) < 0) {
    7568           1 :                                         wpa_printf(MSG_DEBUG,
    7569             :                                                    "Invalid SSID value '%s'",
    7570             :                                                    pos);
    7571           1 :                                         *reply_len = -1;
    7572           1 :                                         goto done;
    7573             :                                 }
    7574           8 :                                 ssid[ssid_count].ssid_len = (end - pos) / 2;
    7575          16 :                                 wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
    7576           8 :                                                   ssid[ssid_count].ssid,
    7577           8 :                                                   ssid[ssid_count].ssid_len);
    7578           8 :                                 ssid_count++;
    7579           8 :                                 pos = end;
    7580             :                         }
    7581             : 
    7582        4213 :                         pos = os_strchr(pos, ' ');
    7583        4213 :                         if (pos)
    7584        2274 :                                 pos++;
    7585             :                 }
    7586             :         }
    7587             : 
    7588        1963 :         wpa_s->num_ssids_from_scan_req = ssid_count;
    7589        1963 :         os_free(wpa_s->ssids_from_scan_req);
    7590        1963 :         if (ssid_count) {
    7591           6 :                 wpa_s->ssids_from_scan_req = ssid;
    7592           6 :                 ssid = NULL;
    7593             :         } else {
    7594        1957 :                 wpa_s->ssids_from_scan_req = NULL;
    7595             :         }
    7596             : 
    7597        1963 :         if (scan_only)
    7598        1271 :                 scan_res_handler = scan_only_handler;
    7599         692 :         else if (wpa_s->scan_res_handler == scan_only_handler)
    7600           0 :                 scan_res_handler = NULL;
    7601             :         else
    7602         692 :                 scan_res_handler = wpa_s->scan_res_handler;
    7603             : 
    7604        3926 :         if (!wpa_s->sched_scanning && !wpa_s->scanning &&
    7605        2058 :             ((wpa_s->wpa_state <= WPA_SCANNING) ||
    7606          95 :              (wpa_s->wpa_state == WPA_COMPLETED))) {
    7607        1963 :                 wpa_s->manual_scan_passive = manual_scan_passive;
    7608        1963 :                 wpa_s->manual_scan_use_id = manual_scan_use_id;
    7609        1963 :                 wpa_s->manual_scan_only_new = manual_scan_only_new;
    7610        1963 :                 wpa_s->scan_id_count = scan_id_count;
    7611        1963 :                 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
    7612        1963 :                 wpa_s->scan_res_handler = scan_res_handler;
    7613        1963 :                 os_free(wpa_s->manual_scan_freqs);
    7614        1963 :                 wpa_s->manual_scan_freqs = manual_scan_freqs;
    7615        1963 :                 manual_scan_freqs = NULL;
    7616             : 
    7617        1963 :                 wpa_s->normal_scans = 0;
    7618        1963 :                 wpa_s->scan_req = MANUAL_SCAN_REQ;
    7619        1963 :                 wpa_s->after_wps = 0;
    7620        1963 :                 wpa_s->known_wps_freq = 0;
    7621        1963 :                 wpa_supplicant_req_scan(wpa_s, 0, 0);
    7622        3926 :                 if (wpa_s->manual_scan_use_id) {
    7623          33 :                         wpa_s->manual_scan_id++;
    7624          33 :                         wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
    7625             :                                 wpa_s->manual_scan_id);
    7626          33 :                         *reply_len = os_snprintf(reply, reply_size, "%u\n",
    7627             :                                                  wpa_s->manual_scan_id);
    7628             :                 }
    7629           0 :         } else if (wpa_s->sched_scanning) {
    7630           0 :                 wpa_s->manual_scan_passive = manual_scan_passive;
    7631           0 :                 wpa_s->manual_scan_use_id = manual_scan_use_id;
    7632           0 :                 wpa_s->manual_scan_only_new = manual_scan_only_new;
    7633           0 :                 wpa_s->scan_id_count = scan_id_count;
    7634           0 :                 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
    7635           0 :                 wpa_s->scan_res_handler = scan_res_handler;
    7636           0 :                 os_free(wpa_s->manual_scan_freqs);
    7637           0 :                 wpa_s->manual_scan_freqs = manual_scan_freqs;
    7638           0 :                 manual_scan_freqs = NULL;
    7639             : 
    7640           0 :                 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
    7641           0 :                 wpa_supplicant_cancel_sched_scan(wpa_s);
    7642           0 :                 wpa_s->scan_req = MANUAL_SCAN_REQ;
    7643           0 :                 wpa_supplicant_req_scan(wpa_s, 0, 0);
    7644           0 :                 if (wpa_s->manual_scan_use_id) {
    7645           0 :                         wpa_s->manual_scan_id++;
    7646           0 :                         *reply_len = os_snprintf(reply, reply_size, "%u\n",
    7647             :                                                  wpa_s->manual_scan_id);
    7648           0 :                         wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
    7649             :                                 wpa_s->manual_scan_id);
    7650             :                 }
    7651             :         } else {
    7652           0 :                 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
    7653           0 :                 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
    7654             :         }
    7655             : 
    7656             : done:
    7657        1967 :         os_free(manual_scan_freqs);
    7658        1967 :         os_free(ssid);
    7659             : }
    7660             : 
    7661             : 
    7662             : #ifdef CONFIG_TESTING_OPTIONS
    7663             : 
    7664          63 : static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
    7665             :                                        unsigned int freq, const u8 *dst,
    7666             :                                        const u8 *src, const u8 *bssid,
    7667             :                                        const u8 *data, size_t data_len,
    7668             :                                        enum offchannel_send_action_result
    7669             :                                        result)
    7670             : {
    7671        1199 :         wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
    7672             :                 " src=" MACSTR " bssid=" MACSTR " result=%s",
    7673        1134 :                 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
    7674             :                 result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
    7675             :                 "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
    7676           2 :                              "NO_ACK" : "FAILED"));
    7677          63 : }
    7678             : 
    7679             : 
    7680          69 : static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
    7681             : {
    7682             :         char *pos, *param;
    7683             :         size_t len;
    7684             :         u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
    7685             :         int res, used;
    7686          69 :         int freq = 0, no_cck = 0, wait_time = 0;
    7687             : 
    7688             :         /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
    7689             :          *    <action=Action frame payload> */
    7690             : 
    7691          69 :         wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
    7692             : 
    7693          69 :         pos = cmd;
    7694          69 :         used = hwaddr_aton2(pos, da);
    7695          69 :         if (used < 0)
    7696           1 :                 return -1;
    7697          68 :         pos += used;
    7698         204 :         while (*pos == ' ')
    7699          68 :                 pos++;
    7700          68 :         used = hwaddr_aton2(pos, bssid);
    7701          68 :         if (used < 0)
    7702           1 :                 return -1;
    7703          67 :         pos += used;
    7704             : 
    7705          67 :         param = os_strstr(pos, " freq=");
    7706          67 :         if (param) {
    7707          64 :                 param += 6;
    7708          64 :                 freq = atoi(param);
    7709             :         }
    7710             : 
    7711          67 :         param = os_strstr(pos, " no_cck=");
    7712          67 :         if (param) {
    7713          32 :                 param += 8;
    7714          32 :                 no_cck = atoi(param);
    7715             :         }
    7716             : 
    7717          67 :         param = os_strstr(pos, " wait_time=");
    7718          67 :         if (param) {
    7719          42 :                 param += 11;
    7720          42 :                 wait_time = atoi(param);
    7721             :         }
    7722             : 
    7723          67 :         param = os_strstr(pos, " action=");
    7724          67 :         if (param == NULL)
    7725           1 :                 return -1;
    7726          66 :         param += 8;
    7727             : 
    7728          66 :         len = os_strlen(param);
    7729          66 :         if (len & 1)
    7730           1 :                 return -1;
    7731          65 :         len /= 2;
    7732             : 
    7733          65 :         buf = os_malloc(len);
    7734          65 :         if (buf == NULL)
    7735           1 :                 return -1;
    7736             : 
    7737          64 :         if (hexstr2bin(param, buf, len) < 0) {
    7738           1 :                 os_free(buf);
    7739           1 :                 return -1;
    7740             :         }
    7741             : 
    7742          63 :         res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
    7743             :                                      buf, len, wait_time,
    7744             :                                      wpas_ctrl_iface_mgmt_tx_cb, no_cck);
    7745          63 :         os_free(buf);
    7746          63 :         return res;
    7747             : }
    7748             : 
    7749             : 
    7750           1 : static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
    7751             : {
    7752           1 :         wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
    7753           1 :         offchannel_send_action_done(wpa_s);
    7754           1 : }
    7755             : 
    7756             : 
    7757          44 : static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
    7758             :                                            char *cmd)
    7759             : {
    7760             :         char *pos, *param;
    7761             :         size_t len;
    7762             :         u8 *buf;
    7763          44 :         int freq = 0, datarate = 0, ssi_signal = 0;
    7764             :         union wpa_event_data event;
    7765             : 
    7766          44 :         if (!wpa_s->ext_mgmt_frame_handling)
    7767           0 :                 return -1;
    7768             : 
    7769             :         /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
    7770             : 
    7771          44 :         wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
    7772             : 
    7773          44 :         pos = cmd;
    7774          44 :         param = os_strstr(pos, "freq=");
    7775          44 :         if (param) {
    7776          44 :                 param += 5;
    7777          44 :                 freq = atoi(param);
    7778             :         }
    7779             : 
    7780          44 :         param = os_strstr(pos, " datarate=");
    7781          44 :         if (param) {
    7782          44 :                 param += 10;
    7783          44 :                 datarate = atoi(param);
    7784             :         }
    7785             : 
    7786          44 :         param = os_strstr(pos, " ssi_signal=");
    7787          44 :         if (param) {
    7788          44 :                 param += 12;
    7789          44 :                 ssi_signal = atoi(param);
    7790             :         }
    7791             : 
    7792          44 :         param = os_strstr(pos, " frame=");
    7793          44 :         if (param == NULL)
    7794           0 :                 return -1;
    7795          44 :         param += 7;
    7796             : 
    7797          44 :         len = os_strlen(param);
    7798          44 :         if (len & 1)
    7799           0 :                 return -1;
    7800          44 :         len /= 2;
    7801             : 
    7802          44 :         buf = os_malloc(len);
    7803          44 :         if (buf == NULL)
    7804           0 :                 return -1;
    7805             : 
    7806          44 :         if (hexstr2bin(param, buf, len) < 0) {
    7807           0 :                 os_free(buf);
    7808           0 :                 return -1;
    7809             :         }
    7810             : 
    7811          44 :         os_memset(&event, 0, sizeof(event));
    7812          44 :         event.rx_mgmt.freq = freq;
    7813          44 :         event.rx_mgmt.frame = buf;
    7814          44 :         event.rx_mgmt.frame_len = len;
    7815          44 :         event.rx_mgmt.ssi_signal = ssi_signal;
    7816          44 :         event.rx_mgmt.datarate = datarate;
    7817          44 :         wpa_s->ext_mgmt_frame_handling = 0;
    7818          44 :         wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
    7819          44 :         wpa_s->ext_mgmt_frame_handling = 1;
    7820             : 
    7821          44 :         os_free(buf);
    7822             : 
    7823          44 :         return 0;
    7824             : }
    7825             : 
    7826             : 
    7827          17 : static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
    7828             : {
    7829             :         char *pos, *param;
    7830             :         union wpa_event_data event;
    7831             :         enum wpa_event_type ev;
    7832             : 
    7833             :         /* <event name> [parameters..] */
    7834             : 
    7835          17 :         wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
    7836             : 
    7837          17 :         pos = cmd;
    7838          17 :         param = os_strchr(pos, ' ');
    7839          17 :         if (param)
    7840           4 :                 *param++ = '\0';
    7841             : 
    7842          17 :         os_memset(&event, 0, sizeof(event));
    7843             : 
    7844          17 :         if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
    7845           6 :                 ev = EVENT_INTERFACE_ENABLED;
    7846          11 :         } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
    7847           3 :                 ev = EVENT_INTERFACE_DISABLED;
    7848           8 :         } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
    7849           7 :                 ev = EVENT_AVOID_FREQUENCIES;
    7850           7 :                 if (param == NULL)
    7851           3 :                         param = "";
    7852           7 :                 if (freq_range_list_parse(&event.freq_range, param) < 0)
    7853           1 :                         return -1;
    7854           6 :                 wpa_supplicant_event(wpa_s, ev, &event);
    7855           6 :                 os_free(event.freq_range.range);
    7856           6 :                 return 0;
    7857             :         } else {
    7858           1 :                 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
    7859             :                         cmd);
    7860           1 :                 return -1;
    7861             :         }
    7862             : 
    7863           9 :         wpa_supplicant_event(wpa_s, ev, &event);
    7864             : 
    7865           9 :         return 0;
    7866             : }
    7867             : 
    7868             : 
    7869         495 : static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
    7870             : {
    7871             :         char *pos;
    7872             :         u8 src[ETH_ALEN], *buf;
    7873             :         int used;
    7874             :         size_t len;
    7875             : 
    7876         495 :         wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
    7877             : 
    7878         495 :         pos = cmd;
    7879         495 :         used = hwaddr_aton2(pos, src);
    7880         495 :         if (used < 0)
    7881           1 :                 return -1;
    7882         494 :         pos += used;
    7883        1482 :         while (*pos == ' ')
    7884         494 :                 pos++;
    7885             : 
    7886         494 :         len = os_strlen(pos);
    7887         494 :         if (len & 1)
    7888           1 :                 return -1;
    7889         493 :         len /= 2;
    7890             : 
    7891         493 :         buf = os_malloc(len);
    7892         493 :         if (buf == NULL)
    7893           1 :                 return -1;
    7894             : 
    7895         492 :         if (hexstr2bin(pos, buf, len) < 0) {
    7896           1 :                 os_free(buf);
    7897           1 :                 return -1;
    7898             :         }
    7899             : 
    7900         491 :         wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
    7901         491 :         os_free(buf);
    7902             : 
    7903         491 :         return 0;
    7904             : }
    7905             : 
    7906             : 
    7907        3141 : static u16 ipv4_hdr_checksum(const void *buf, size_t len)
    7908             : {
    7909             :         size_t i;
    7910        3141 :         u32 sum = 0;
    7911        3141 :         const u16 *pos = buf;
    7912             : 
    7913       34551 :         for (i = 0; i < len / 2; i++)
    7914       31410 :                 sum += *pos++;
    7915             : 
    7916        9423 :         while (sum >> 16)
    7917        3141 :                 sum = (sum & 0xffff) + (sum >> 16);
    7918             : 
    7919        3141 :         return sum ^ 0xffff;
    7920             : }
    7921             : 
    7922             : 
    7923             : #define HWSIM_PACKETLEN 1500
    7924             : #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
    7925             : 
    7926        3128 : static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
    7927             :                               size_t len)
    7928             : {
    7929        3128 :         struct wpa_supplicant *wpa_s = ctx;
    7930             :         const struct ether_header *eth;
    7931             :         struct iphdr ip;
    7932             :         const u8 *pos;
    7933             :         unsigned int i;
    7934             : 
    7935        3128 :         if (len != HWSIM_PACKETLEN)
    7936           0 :                 return;
    7937             : 
    7938        3128 :         eth = (const struct ether_header *) buf;
    7939        3128 :         os_memcpy(&ip, eth + 1, sizeof(ip));
    7940        3128 :         pos = &buf[sizeof(*eth) + sizeof(ip)];
    7941             : 
    7942        6256 :         if (ip.ihl != 5 || ip.version != 4 ||
    7943        3128 :             ntohs(ip.tot_len) != HWSIM_IP_LEN)
    7944           0 :                 return;
    7945             : 
    7946     4588776 :         for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
    7947     4585648 :                 if (*pos != (u8) i)
    7948           0 :                         return;
    7949     4585648 :                 pos++;
    7950             :         }
    7951             : 
    7952       37536 :         wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
    7953       37536 :                 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
    7954             : }
    7955             : 
    7956             : 
    7957        3068 : static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
    7958             :                                             char *cmd)
    7959             : {
    7960        3068 :         int enabled = atoi(cmd);
    7961             :         char *pos;
    7962             :         const char *ifname;
    7963             : 
    7964        3068 :         if (!enabled) {
    7965        1534 :                 if (wpa_s->l2_test) {
    7966        1533 :                         l2_packet_deinit(wpa_s->l2_test);
    7967        1533 :                         wpa_s->l2_test = NULL;
    7968        1533 :                         wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
    7969             :                 }
    7970        1534 :                 return 0;
    7971             :         }
    7972             : 
    7973        1534 :         if (wpa_s->l2_test)
    7974           1 :                 return 0;
    7975             : 
    7976        1533 :         pos = os_strstr(cmd, " ifname=");
    7977        1533 :         if (pos)
    7978           6 :                 ifname = pos + 8;
    7979             :         else
    7980        1527 :                 ifname = wpa_s->ifname;
    7981             : 
    7982        1533 :         wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
    7983             :                                         ETHERTYPE_IP, wpas_data_test_rx,
    7984             :                                         wpa_s, 1);
    7985        1533 :         if (wpa_s->l2_test == NULL)
    7986           0 :                 return -1;
    7987             : 
    7988        1533 :         wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
    7989             : 
    7990        1533 :         return 0;
    7991             : }
    7992             : 
    7993             : 
    7994        3146 : static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
    7995             : {
    7996             :         u8 dst[ETH_ALEN], src[ETH_ALEN];
    7997             :         char *pos;
    7998             :         int used;
    7999             :         long int val;
    8000             :         u8 tos;
    8001             :         u8 buf[2 + HWSIM_PACKETLEN];
    8002             :         struct ether_header *eth;
    8003             :         struct iphdr *ip;
    8004             :         u8 *dpos;
    8005             :         unsigned int i;
    8006             : 
    8007        3146 :         if (wpa_s->l2_test == NULL)
    8008           1 :                 return -1;
    8009             : 
    8010             :         /* format: <dst> <src> <tos> */
    8011             : 
    8012        3145 :         pos = cmd;
    8013        3145 :         used = hwaddr_aton2(pos, dst);
    8014        3145 :         if (used < 0)
    8015           1 :                 return -1;
    8016        3144 :         pos += used;
    8017        9432 :         while (*pos == ' ')
    8018        3144 :                 pos++;
    8019        3144 :         used = hwaddr_aton2(pos, src);
    8020        3144 :         if (used < 0)
    8021           1 :                 return -1;
    8022        3143 :         pos += used;
    8023             : 
    8024        3143 :         val = strtol(pos, NULL, 0);
    8025        3143 :         if (val < 0 || val > 0xff)
    8026           2 :                 return -1;
    8027        3141 :         tos = val;
    8028             : 
    8029        3141 :         eth = (struct ether_header *) &buf[2];
    8030        3141 :         os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
    8031        3141 :         os_memcpy(eth->ether_shost, src, ETH_ALEN);
    8032        3141 :         eth->ether_type = htons(ETHERTYPE_IP);
    8033        3141 :         ip = (struct iphdr *) (eth + 1);
    8034        3141 :         os_memset(ip, 0, sizeof(*ip));
    8035        3141 :         ip->ihl = 5;
    8036        3141 :         ip->version = 4;
    8037        3141 :         ip->ttl = 64;
    8038        3141 :         ip->tos = tos;
    8039        3141 :         ip->tot_len = htons(HWSIM_IP_LEN);
    8040        3141 :         ip->protocol = 1;
    8041        3141 :         ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
    8042        3141 :         ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
    8043        3141 :         ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
    8044        3141 :         dpos = (u8 *) (ip + 1);
    8045     4607847 :         for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
    8046     4604706 :                 *dpos++ = i;
    8047             : 
    8048        3141 :         if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
    8049             :                            HWSIM_PACKETLEN) < 0)
    8050           0 :                 return -1;
    8051             : 
    8052        3141 :         wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
    8053             :                 " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
    8054             : 
    8055        3141 :         return 0;
    8056             : }
    8057             : 
    8058             : 
    8059         115 : static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
    8060             :                                            char *cmd)
    8061             : {
    8062             :         u8 *buf;
    8063             :         struct ether_header *eth;
    8064         115 :         struct l2_packet_data *l2 = NULL;
    8065             :         size_t len;
    8066             :         u16 ethertype;
    8067         115 :         int res = -1;
    8068             : 
    8069         115 :         len = os_strlen(cmd);
    8070         115 :         if (len & 1 || len < ETH_HLEN * 2)
    8071           3 :                 return -1;
    8072         112 :         len /= 2;
    8073             : 
    8074         112 :         buf = os_malloc(len);
    8075         112 :         if (buf == NULL)
    8076           1 :                 return -1;
    8077             : 
    8078         111 :         if (hexstr2bin(cmd, buf, len) < 0)
    8079           1 :                 goto done;
    8080             : 
    8081         110 :         eth = (struct ether_header *) buf;
    8082         110 :         ethertype = ntohs(eth->ether_type);
    8083             : 
    8084         110 :         l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
    8085             :                             wpas_data_test_rx, wpa_s, 1);
    8086         110 :         if (l2 == NULL)
    8087           1 :                 goto done;
    8088             : 
    8089         109 :         res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
    8090         109 :         wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
    8091             : done:
    8092         111 :         if (l2)
    8093         109 :                 l2_packet_deinit(l2);
    8094         111 :         os_free(buf);
    8095             : 
    8096         111 :         return res < 0 ? -1 : 0;
    8097             : }
    8098             : 
    8099             : 
    8100         971 : static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
    8101             : {
    8102             : #ifdef WPA_TRACE_BFD
    8103             :         char *pos;
    8104             : 
    8105         971 :         wpa_trace_fail_after = atoi(cmd);
    8106         971 :         pos = os_strchr(cmd, ':');
    8107         971 :         if (pos) {
    8108         971 :                 pos++;
    8109         971 :                 os_strlcpy(wpa_trace_fail_func, pos,
    8110             :                            sizeof(wpa_trace_fail_func));
    8111             :         } else {
    8112           0 :                 wpa_trace_fail_after = 0;
    8113             :         }
    8114         971 :         return 0;
    8115             : #else /* WPA_TRACE_BFD */
    8116             :         return -1;
    8117             : #endif /* WPA_TRACE_BFD */
    8118             : }
    8119             : 
    8120             : 
    8121        3668 : static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
    8122             :                                     char *buf, size_t buflen)
    8123             : {
    8124             : #ifdef WPA_TRACE_BFD
    8125        3668 :         return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
    8126             :                            wpa_trace_fail_func);
    8127             : #else /* WPA_TRACE_BFD */
    8128             :         return -1;
    8129             : #endif /* WPA_TRACE_BFD */
    8130             : }
    8131             : 
    8132             : 
    8133         249 : static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd)
    8134             : {
    8135             : #ifdef WPA_TRACE_BFD
    8136             :         char *pos;
    8137             : 
    8138         249 :         wpa_trace_test_fail_after = atoi(cmd);
    8139         249 :         pos = os_strchr(cmd, ':');
    8140         249 :         if (pos) {
    8141         249 :                 pos++;
    8142         249 :                 os_strlcpy(wpa_trace_test_fail_func, pos,
    8143             :                            sizeof(wpa_trace_test_fail_func));
    8144             :         } else {
    8145           0 :                 wpa_trace_test_fail_after = 0;
    8146             :         }
    8147         249 :         return 0;
    8148             : #else /* WPA_TRACE_BFD */
    8149             :         return -1;
    8150             : #endif /* WPA_TRACE_BFD */
    8151             : }
    8152             : 
    8153             : 
    8154         623 : static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s,
    8155             :                                     char *buf, size_t buflen)
    8156             : {
    8157             : #ifdef WPA_TRACE_BFD
    8158         623 :         return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
    8159             :                            wpa_trace_test_fail_func);
    8160             : #else /* WPA_TRACE_BFD */
    8161             :         return -1;
    8162             : #endif /* WPA_TRACE_BFD */
    8163             : }
    8164             : 
    8165             : 
    8166           1 : static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
    8167             : {
    8168           1 :         struct wpa_supplicant *wpa_s = eloop_ctx;
    8169           1 :         int i, count = (intptr_t) timeout_ctx;
    8170             : 
    8171           1 :         wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
    8172             :                    count);
    8173        1001 :         for (i = 0; i < count; i++) {
    8174        1000 :                 wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
    8175             :                              i + 1, count);
    8176             :         }
    8177           1 : }
    8178             : 
    8179             : 
    8180           1 : static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
    8181             : {
    8182             :         int count;
    8183             : 
    8184           1 :         count = atoi(cmd);
    8185           1 :         if (count <= 0)
    8186           0 :                 return -1;
    8187             : 
    8188           1 :         return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
    8189           1 :                                       (void *) (intptr_t) count);
    8190             : }
    8191             : 
    8192             : 
    8193          20 : static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
    8194             :                                    const char *cmd)
    8195             : {
    8196             :         struct wpabuf *buf;
    8197             :         size_t len;
    8198             : 
    8199          20 :         len = os_strlen(cmd);
    8200          20 :         if (len & 1)
    8201           0 :                 return -1;
    8202          20 :         len /= 2;
    8203             : 
    8204          20 :         if (len == 0) {
    8205           0 :                 buf = NULL;
    8206             :         } else {
    8207          20 :                 buf = wpabuf_alloc(len);
    8208          20 :                 if (buf == NULL)
    8209           0 :                         return -1;
    8210             : 
    8211          20 :                 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
    8212           0 :                         wpabuf_free(buf);
    8213           0 :                         return -1;
    8214             :                 }
    8215             :         }
    8216             : 
    8217          20 :         wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
    8218          20 :         return 0;
    8219             : }
    8220             : 
    8221             : #endif /* CONFIG_TESTING_OPTIONS */
    8222             : 
    8223             : 
    8224          67 : static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
    8225             : {
    8226          67 :         char *pos = cmd;
    8227             :         int frame;
    8228             :         size_t len;
    8229             :         struct wpabuf *buf;
    8230             :         struct ieee802_11_elems elems;
    8231             : 
    8232          67 :         frame = atoi(pos);
    8233          67 :         if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
    8234           2 :                 return -1;
    8235          65 :         wpa_s = wpas_vendor_elem(wpa_s, frame);
    8236             : 
    8237          65 :         pos = os_strchr(pos, ' ');
    8238          65 :         if (pos == NULL)
    8239           1 :                 return -1;
    8240          64 :         pos++;
    8241             : 
    8242          64 :         len = os_strlen(pos);
    8243          64 :         if (len == 0)
    8244           1 :                 return 0;
    8245          63 :         if (len & 1)
    8246           1 :                 return -1;
    8247          62 :         len /= 2;
    8248             : 
    8249          62 :         buf = wpabuf_alloc(len);
    8250          62 :         if (buf == NULL)
    8251           1 :                 return -1;
    8252             : 
    8253          61 :         if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
    8254           1 :                 wpabuf_free(buf);
    8255           1 :                 return -1;
    8256             :         }
    8257             : 
    8258          60 :         if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
    8259             :             ParseFailed) {
    8260           1 :                 wpabuf_free(buf);
    8261           1 :                 return -1;
    8262             :         }
    8263             : 
    8264          59 :         if (wpa_s->vendor_elem[frame] == NULL) {
    8265          57 :                 wpa_s->vendor_elem[frame] = buf;
    8266          57 :                 wpas_vendor_elem_update(wpa_s);
    8267          57 :                 return 0;
    8268             :         }
    8269             : 
    8270           2 :         if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
    8271           1 :                 wpabuf_free(buf);
    8272           1 :                 return -1;
    8273             :         }
    8274             : 
    8275           1 :         wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
    8276           1 :         wpabuf_free(buf);
    8277           1 :         wpas_vendor_elem_update(wpa_s);
    8278             : 
    8279           1 :         return 0;
    8280             : }
    8281             : 
    8282             : 
    8283           9 : static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
    8284             :                                      char *buf, size_t buflen)
    8285             : {
    8286           9 :         int frame = atoi(cmd);
    8287             : 
    8288           9 :         if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
    8289           2 :                 return -1;
    8290           7 :         wpa_s = wpas_vendor_elem(wpa_s, frame);
    8291             : 
    8292           7 :         if (wpa_s->vendor_elem[frame] == NULL)
    8293           2 :                 return 0;
    8294             : 
    8295          10 :         return wpa_snprintf_hex(buf, buflen,
    8296           5 :                                 wpabuf_head_u8(wpa_s->vendor_elem[frame]),
    8297           5 :                                 wpabuf_len(wpa_s->vendor_elem[frame]));
    8298             : }
    8299             : 
    8300             : 
    8301          83 : static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
    8302             : {
    8303          83 :         char *pos = cmd;
    8304             :         int frame;
    8305             :         size_t len;
    8306             :         u8 *buf;
    8307             :         struct ieee802_11_elems elems;
    8308             :         int res;
    8309             : 
    8310          83 :         frame = atoi(pos);
    8311          83 :         if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
    8312           4 :                 return -1;
    8313          79 :         wpa_s = wpas_vendor_elem(wpa_s, frame);
    8314             : 
    8315          79 :         pos = os_strchr(pos, ' ');
    8316          79 :         if (pos == NULL)
    8317           3 :                 return -1;
    8318          76 :         pos++;
    8319             : 
    8320          76 :         if (*pos == '*') {
    8321          60 :                 wpabuf_free(wpa_s->vendor_elem[frame]);
    8322          60 :                 wpa_s->vendor_elem[frame] = NULL;
    8323          60 :                 wpas_vendor_elem_update(wpa_s);
    8324          60 :                 return 0;
    8325             :         }
    8326             : 
    8327          16 :         if (wpa_s->vendor_elem[frame] == NULL)
    8328           4 :                 return -1;
    8329             : 
    8330          12 :         len = os_strlen(pos);
    8331          12 :         if (len == 0)
    8332           1 :                 return 0;
    8333          11 :         if (len & 1)
    8334           1 :                 return -1;
    8335          10 :         len /= 2;
    8336             : 
    8337          10 :         buf = os_malloc(len);
    8338          10 :         if (buf == NULL)
    8339           1 :                 return -1;
    8340             : 
    8341           9 :         if (hexstr2bin(pos, buf, len) < 0) {
    8342           1 :                 os_free(buf);
    8343           1 :                 return -1;
    8344             :         }
    8345             : 
    8346           8 :         if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
    8347           2 :                 os_free(buf);
    8348           2 :                 return -1;
    8349             :         }
    8350             : 
    8351           6 :         res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
    8352           6 :         os_free(buf);
    8353           6 :         return res;
    8354             : }
    8355             : 
    8356             : 
    8357          17 : static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
    8358             : {
    8359          17 :         struct wpa_supplicant *wpa_s = ctx;
    8360             :         size_t len;
    8361             :         const u8 *data;
    8362             : 
    8363             :         /*
    8364             :          * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
    8365             :          * BSSID[6]
    8366             :          * BSSID Information[4]
    8367             :          * Operating Class[1]
    8368             :          * Channel Number[1]
    8369             :          * PHY Type[1]
    8370             :          * Optional Subelements[variable]
    8371             :          */
    8372             : #define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
    8373             : 
    8374          17 :         if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
    8375           2 :                 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
    8376           2 :                 goto out;
    8377             :         }
    8378             : 
    8379          15 :         data = wpabuf_head_u8(neighbor_rep);
    8380          15 :         len = wpabuf_len(neighbor_rep);
    8381             : 
    8382          15 :         while (len >= 2 + NR_IE_MIN_LEN) {
    8383             :                 const u8 *nr;
    8384             :                 char lci[256 * 2 + 1];
    8385             :                 char civic[256 * 2 + 1];
    8386          19 :                 u8 nr_len = data[1];
    8387          19 :                 const u8 *pos = data, *end;
    8388             : 
    8389          19 :                 if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
    8390             :                     nr_len < NR_IE_MIN_LEN) {
    8391           0 :                         wpa_printf(MSG_DEBUG,
    8392             :                                    "CTRL: Invalid Neighbor Report element: id=%u len=%u",
    8393           0 :                                    data[0], nr_len);
    8394           0 :                         goto out;
    8395             :                 }
    8396             : 
    8397          19 :                 if (2U + nr_len > len) {
    8398           0 :                         wpa_printf(MSG_DEBUG,
    8399             :                                    "CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
    8400           0 :                                    data[0], len, nr_len);
    8401           0 :                         goto out;
    8402             :                 }
    8403          19 :                 pos += 2;
    8404          19 :                 end = pos + nr_len;
    8405             : 
    8406          19 :                 nr = pos;
    8407          19 :                 pos += NR_IE_MIN_LEN;
    8408             : 
    8409          19 :                 lci[0] = '\0';
    8410          19 :                 civic[0] = '\0';
    8411          55 :                 while (end - pos > 2) {
    8412             :                         u8 s_id, s_len;
    8413             : 
    8414          17 :                         s_id = *pos++;
    8415          17 :                         s_len = *pos++;
    8416          17 :                         if (s_len > end - pos)
    8417           0 :                                 goto out;
    8418          17 :                         if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
    8419             :                                 /* Measurement Token[1] */
    8420             :                                 /* Measurement Report Mode[1] */
    8421             :                                 /* Measurement Type[1] */
    8422             :                                 /* Measurement Report[variable] */
    8423          14 :                                 switch (pos[2]) {
    8424             :                                 case MEASURE_TYPE_LCI:
    8425           8 :                                         if (lci[0])
    8426           0 :                                                 break;
    8427           8 :                                         wpa_snprintf_hex(lci, sizeof(lci),
    8428             :                                                          pos, s_len);
    8429           8 :                                         break;
    8430             :                                 case MEASURE_TYPE_LOCATION_CIVIC:
    8431           6 :                                         if (civic[0])
    8432           0 :                                                 break;
    8433           6 :                                         wpa_snprintf_hex(civic, sizeof(civic),
    8434             :                                                          pos, s_len);
    8435           6 :                                         break;
    8436             :                                 }
    8437             :                         }
    8438             : 
    8439          17 :                         pos += s_len;
    8440             :                 }
    8441             : 
    8442         209 :                 wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
    8443             :                         "bssid=" MACSTR
    8444             :                         " info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
    8445         114 :                         MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
    8446          38 :                         nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
    8447          19 :                         nr[ETH_ALEN + 6],
    8448          19 :                         lci[0] ? " lci=" : "", lci,
    8449          19 :                         civic[0] ? " civic=" : "", civic);
    8450             : 
    8451          19 :                 data = end;
    8452          19 :                 len -= 2 + nr_len;
    8453             :         }
    8454             : 
    8455             : out:
    8456          17 :         wpabuf_free(neighbor_rep);
    8457          17 : }
    8458             : 
    8459             : 
    8460          19 : static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
    8461             :                                              char *cmd)
    8462             : {
    8463          19 :         struct wpa_ssid_value ssid, *ssid_p = NULL;
    8464          19 :         int ret, lci = 0, civic = 0;
    8465             :         char *ssid_s;
    8466             : 
    8467          19 :         ssid_s = os_strstr(cmd, "ssid=");
    8468          19 :         if (ssid_s) {
    8469          15 :                 if (ssid_parse(ssid_s + 5, &ssid)) {
    8470           0 :                         wpa_printf(MSG_ERROR,
    8471             :                                    "CTRL: Send Neighbor Report: bad SSID");
    8472           0 :                         return -1;
    8473             :                 }
    8474             : 
    8475          15 :                 ssid_p = &ssid;
    8476             : 
    8477             :                 /*
    8478             :                  * Move cmd after the SSID text that may include "lci" or
    8479             :                  * "civic".
    8480             :                  */
    8481          15 :                 cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
    8482          15 :                 if (cmd)
    8483          15 :                         cmd++;
    8484             : 
    8485             :         }
    8486             : 
    8487          19 :         if (cmd && os_strstr(cmd, "lci"))
    8488           9 :                 lci = 1;
    8489             : 
    8490          19 :         if (cmd && os_strstr(cmd, "civic"))
    8491           8 :                 civic = 1;
    8492             : 
    8493          19 :         ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
    8494             :                                                  wpas_ctrl_neighbor_rep_cb,
    8495             :                                                  wpa_s);
    8496             : 
    8497          19 :         return ret;
    8498             : }
    8499             : 
    8500             : 
    8501          52 : static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
    8502             : {
    8503          52 :         eapol_sm_erp_flush(wpa_s->eapol);
    8504          52 :         return 0;
    8505             : }
    8506             : 
    8507             : 
    8508          16 : static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
    8509             :                                          char *cmd)
    8510             : {
    8511          16 :         char *token, *context = NULL;
    8512          16 :         unsigned int enable = ~0, type = 0;
    8513             :         u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
    8514          16 :         u8 *addr = NULL, *mask = NULL;
    8515             : 
    8516          68 :         while ((token = str_token(cmd, " ", &context))) {
    8517          39 :                 if (os_strcasecmp(token, "scan") == 0) {
    8518           1 :                         type |= MAC_ADDR_RAND_SCAN;
    8519          38 :                 } else if (os_strcasecmp(token, "sched") == 0) {
    8520           1 :                         type |= MAC_ADDR_RAND_SCHED_SCAN;
    8521          37 :                 } else if (os_strcasecmp(token, "pno") == 0) {
    8522           2 :                         type |= MAC_ADDR_RAND_PNO;
    8523          35 :                 } else if (os_strcasecmp(token, "all") == 0) {
    8524          10 :                         type = wpa_s->mac_addr_rand_supported;
    8525          25 :                 } else if (os_strncasecmp(token, "enable=", 7) == 0) {
    8526          12 :                         enable = atoi(token + 7);
    8527          13 :                 } else if (os_strncasecmp(token, "addr=", 5) == 0) {
    8528           6 :                         addr = _addr;
    8529           6 :                         if (hwaddr_aton(token + 5, addr)) {
    8530           1 :                                 wpa_printf(MSG_INFO,
    8531             :                                            "CTRL: Invalid MAC address: %s",
    8532             :                                            token);
    8533           1 :                                 return -1;
    8534             :                         }
    8535           7 :                 } else if (os_strncasecmp(token, "mask=", 5) == 0) {
    8536           6 :                         mask = _mask;
    8537           6 :                         if (hwaddr_aton(token + 5, mask)) {
    8538           1 :                                 wpa_printf(MSG_INFO,
    8539             :                                            "CTRL: Invalid MAC address mask: %s",
    8540             :                                            token);
    8541           1 :                                 return -1;
    8542             :                         }
    8543             :                 } else {
    8544           1 :                         wpa_printf(MSG_INFO,
    8545             :                                    "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
    8546             :                                    token);
    8547           1 :                         return -1;
    8548             :                 }
    8549             :         }
    8550             : 
    8551          13 :         if (!type) {
    8552           2 :                 wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
    8553           2 :                 return -1;
    8554             :         }
    8555             : 
    8556          11 :         if ((wpa_s->mac_addr_rand_supported & type) != type) {
    8557           1 :                 wpa_printf(MSG_INFO,
    8558             :                            "CTRL: MAC_RAND_SCAN types=%u != supported=%u",
    8559             :                            type, wpa_s->mac_addr_rand_supported);
    8560           1 :                 return -1;
    8561             :         }
    8562             : 
    8563          10 :         if (enable > 1) {
    8564           2 :                 wpa_printf(MSG_INFO,
    8565             :                            "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
    8566           2 :                 return -1;
    8567             :         }
    8568             : 
    8569           8 :         if (!enable) {
    8570           1 :                 wpas_mac_addr_rand_scan_clear(wpa_s, type);
    8571           1 :                 if (wpa_s->pno) {
    8572           0 :                         if (type & MAC_ADDR_RAND_PNO) {
    8573           0 :                                 wpas_stop_pno(wpa_s);
    8574           0 :                                 wpas_start_pno(wpa_s);
    8575             :                         }
    8576           1 :                 } else if (wpa_s->sched_scanning &&
    8577           0 :                            (type & MAC_ADDR_RAND_SCHED_SCAN)) {
    8578           0 :                         wpas_scan_restart_sched_scan(wpa_s);
    8579             :                 }
    8580           1 :                 return 0;
    8581             :         }
    8582             : 
    8583           7 :         if ((addr && !mask) || (!addr && mask)) {
    8584           2 :                 wpa_printf(MSG_INFO,
    8585             :                            "CTRL: MAC_RAND_SCAN invalid addr/mask combination");
    8586           2 :                 return -1;
    8587             :         }
    8588             : 
    8589           5 :         if (addr && mask && (!(mask[0] & 0x01) || (addr[0] & 0x01))) {
    8590           2 :                 wpa_printf(MSG_INFO,
    8591             :                            "CTRL: MAC_RAND_SCAN cannot allow multicast address");
    8592           2 :                 return -1;
    8593             :         }
    8594             : 
    8595           3 :         if (type & MAC_ADDR_RAND_SCAN) {
    8596           3 :                 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCAN,
    8597             :                                             addr, mask);
    8598             :         }
    8599             : 
    8600           3 :         if (type & MAC_ADDR_RAND_SCHED_SCAN) {
    8601           0 :                 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCHED_SCAN,
    8602             :                                             addr, mask);
    8603             : 
    8604           0 :                 if (wpa_s->sched_scanning && !wpa_s->pno)
    8605           0 :                         wpas_scan_restart_sched_scan(wpa_s);
    8606             :         }
    8607             : 
    8608           3 :         if (type & MAC_ADDR_RAND_PNO) {
    8609           0 :                 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_PNO,
    8610             :                                             addr, mask);
    8611           0 :                 if (wpa_s->pno) {
    8612           0 :                         wpas_stop_pno(wpa_s);
    8613           0 :                         wpas_start_pno(wpa_s);
    8614             :                 }
    8615             :         }
    8616             : 
    8617           3 :         return 0;
    8618             : }
    8619             : 
    8620             : 
    8621         277 : static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
    8622             :                                  char *buf, size_t buflen)
    8623             : {
    8624             :         size_t reply_len;
    8625             : 
    8626         277 :         reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
    8627             : #ifdef CONFIG_AP
    8628         277 :         reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
    8629             :                                               buflen - reply_len);
    8630             : #endif /* CONFIG_AP */
    8631         277 :         return reply_len;
    8632             : }
    8633             : 
    8634             : 
    8635          14 : static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
    8636             : {
    8637          14 :         wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
    8638             : #ifdef CONFIG_AP
    8639          14 :         wpas_ap_pmksa_cache_flush(wpa_s);
    8640             : #endif /* CONFIG_AP */
    8641          14 : }
    8642             : 
    8643             : 
    8644      103197 : static int wpas_ctrl_cmd_debug_level(const char *cmd)
    8645             : {
    8646      194296 :         if (os_strcmp(cmd, "PING") == 0 ||
    8647      179209 :             os_strncmp(cmd, "BSS ", 4) == 0 ||
    8648      176151 :             os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
    8649      163386 :             os_strncmp(cmd, "STATUS", 6) == 0 ||
    8650      150689 :             os_strncmp(cmd, "STA ", 4) == 0 ||
    8651       75344 :             os_strncmp(cmd, "STA-", 4) == 0)
    8652       27857 :                 return MSG_EXCESSIVE;
    8653       75340 :         return MSG_DEBUG;
    8654             : }
    8655             : 
    8656             : 
    8657      123488 : char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
    8658             :                                          char *buf, size_t *resp_len)
    8659             : {
    8660             :         char *reply;
    8661      123488 :         const int reply_size = 4096;
    8662             :         int reply_len;
    8663             : 
    8664      246889 :         if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
    8665      123401 :             os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
    8666       40394 :                 if (wpa_debug_show_keys)
    8667       20197 :                         wpa_dbg(wpa_s, MSG_DEBUG,
    8668             :                                 "Control interface command '%s'", buf);
    8669             :                 else
    8670           0 :                         wpa_dbg(wpa_s, MSG_DEBUG,
    8671             :                                 "Control interface command '%s [REMOVED]'",
    8672             :                                 os_strncmp(buf, WPA_CTRL_RSP,
    8673             :                                            os_strlen(WPA_CTRL_RSP)) == 0 ?
    8674             :                                 WPA_CTRL_RSP : "SET_NETWORK");
    8675      206532 :         } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
    8676      103241 :                    os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
    8677          94 :                 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
    8678             :                                       (const u8 *) buf, os_strlen(buf));
    8679             :         } else {
    8680      103197 :                 int level = wpas_ctrl_cmd_debug_level(buf);
    8681      103197 :                 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
    8682             :         }
    8683             : 
    8684      123488 :         reply = os_malloc(reply_size);
    8685      123488 :         if (reply == NULL) {
    8686           1 :                 *resp_len = 1;
    8687           1 :                 return NULL;
    8688             :         }
    8689             : 
    8690      123487 :         os_memcpy(reply, "OK\n", 3);
    8691      123487 :         reply_len = 3;
    8692             : 
    8693      123487 :         if (os_strcmp(buf, "PING") == 0) {
    8694       12098 :                 os_memcpy(reply, "PONG\n", 5);
    8695       12098 :                 reply_len = 5;
    8696      111389 :         } else if (os_strcmp(buf, "IFNAME") == 0) {
    8697           1 :                 reply_len = os_strlen(wpa_s->ifname);
    8698           1 :                 os_memcpy(reply, wpa_s->ifname, reply_len);
    8699      111388 :         } else if (os_strncmp(buf, "RELOG", 5) == 0) {
    8700           1 :                 if (wpa_debug_reopen_file() < 0)
    8701           0 :                         reply_len = -1;
    8702      111387 :         } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
    8703       12116 :                 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
    8704       99271 :         } else if (os_strcmp(buf, "MIB") == 0) {
    8705          51 :                 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
    8706          51 :                 if (reply_len >= 0) {
    8707          51 :                         reply_len += eapol_sm_get_mib(wpa_s->eapol,
    8708             :                                                       reply + reply_len,
    8709          51 :                                                       reply_size - reply_len);
    8710             :                 }
    8711       99220 :         } else if (os_strncmp(buf, "STATUS", 6) == 0) {
    8712       12696 :                 reply_len = wpa_supplicant_ctrl_iface_status(
    8713             :                         wpa_s, buf + 6, reply, reply_size);
    8714       86524 :         } else if (os_strcmp(buf, "PMKSA") == 0) {
    8715         277 :                 reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
    8716       86247 :         } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
    8717          14 :                 wpas_ctrl_iface_pmksa_flush(wpa_s);
    8718       86233 :         } else if (os_strncmp(buf, "SET ", 4) == 0) {
    8719        7906 :                 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
    8720          82 :                         reply_len = -1;
    8721       78327 :         } else if (os_strncmp(buf, "DUMP", 4) == 0) {
    8722           1 :                 reply_len = wpa_config_dump_values(wpa_s->conf,
    8723             :                                                    reply, reply_size);
    8724       78326 :         } else if (os_strncmp(buf, "GET ", 4) == 0) {
    8725         189 :                 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
    8726             :                                                           reply, reply_size);
    8727       78137 :         } else if (os_strcmp(buf, "LOGON") == 0) {
    8728           1 :                 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
    8729       78136 :         } else if (os_strcmp(buf, "LOGOFF") == 0) {
    8730           1 :                 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
    8731       78135 :         } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
    8732          22 :                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
    8733           1 :                         reply_len = -1;
    8734             :                 else
    8735          21 :                         wpas_request_connection(wpa_s);
    8736       78113 :         } else if (os_strcmp(buf, "REATTACH") == 0) {
    8737           7 :                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
    8738           3 :                     !wpa_s->current_ssid)
    8739           2 :                         reply_len = -1;
    8740             :                 else {
    8741           2 :                         wpa_s->reattach = 1;
    8742           2 :                         wpas_request_connection(wpa_s);
    8743             :                 }
    8744       78109 :         } else if (os_strcmp(buf, "RECONNECT") == 0) {
    8745          57 :                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
    8746           1 :                         reply_len = -1;
    8747          56 :                 else if (wpa_s->disconnected)
    8748          49 :                         wpas_request_connection(wpa_s);
    8749             : #ifdef IEEE8021X_EAPOL
    8750       78052 :         } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
    8751          16 :                 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
    8752           7 :                         reply_len = -1;
    8753             : #endif /* IEEE8021X_EAPOL */
    8754             : #ifdef CONFIG_PEERKEY
    8755       78036 :         } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
    8756           6 :                 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
    8757           2 :                         reply_len = -1;
    8758             : #endif /* CONFIG_PEERKEY */
    8759             : #ifdef CONFIG_IEEE80211R
    8760       78030 :         } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
    8761         118 :                 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
    8762           1 :                         reply_len = -1;
    8763             : #endif /* CONFIG_IEEE80211R */
    8764             : #ifdef CONFIG_WPS
    8765       77912 :         } else if (os_strcmp(buf, "WPS_PBC") == 0) {
    8766          16 :                 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
    8767          16 :                 if (res == -2) {
    8768           1 :                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
    8769           1 :                         reply_len = 17;
    8770          15 :                 } else if (res)
    8771           0 :                         reply_len = -1;
    8772       77896 :         } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
    8773          76 :                 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
    8774          76 :                 if (res == -2) {
    8775           1 :                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
    8776           1 :                         reply_len = 17;
    8777          75 :                 } else if (res)
    8778           2 :                         reply_len = -1;
    8779       77820 :         } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
    8780         472 :                 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
    8781             :                                                               reply,
    8782             :                                                               reply_size);
    8783       77348 :         } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
    8784          17 :                 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
    8785             :                         wpa_s, buf + 14, reply, reply_size);
    8786       77331 :         } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
    8787         185 :                 if (wpas_wps_cancel(wpa_s))
    8788           1 :                         reply_len = -1;
    8789             : #ifdef CONFIG_WPS_NFC
    8790       77146 :         } else if (os_strcmp(buf, "WPS_NFC") == 0) {
    8791           3 :                 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
    8792           0 :                         reply_len = -1;
    8793       77143 :         } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
    8794           1 :                 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
    8795           1 :                         reply_len = -1;
    8796       77142 :         } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
    8797           3 :                 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
    8798             :                         wpa_s, buf + 21, reply, reply_size);
    8799       77139 :         } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
    8800          21 :                 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
    8801             :                         wpa_s, buf + 14, reply, reply_size);
    8802       77118 :         } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
    8803          50 :                 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
    8804             :                                                                buf + 17))
    8805          34 :                         reply_len = -1;
    8806       77068 :         } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
    8807          36 :                 reply_len = wpas_ctrl_nfc_get_handover_req(
    8808             :                         wpa_s, buf + 21, reply, reply_size);
    8809       77032 :         } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
    8810          37 :                 reply_len = wpas_ctrl_nfc_get_handover_sel(
    8811             :                         wpa_s, buf + 21, reply, reply_size);
    8812       76995 :         } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
    8813          44 :                 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
    8814          11 :                         reply_len = -1;
    8815             : #endif /* CONFIG_WPS_NFC */
    8816       76951 :         } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
    8817          70 :                 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
    8818           5 :                         reply_len = -1;
    8819             : #ifdef CONFIG_AP
    8820       76881 :         } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
    8821          16 :                 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
    8822             :                         wpa_s, buf + 11, reply, reply_size);
    8823             : #endif /* CONFIG_AP */
    8824             : #ifdef CONFIG_WPS_ER
    8825       76865 :         } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
    8826           1 :                 if (wpas_wps_er_start(wpa_s, NULL))
    8827           0 :                         reply_len = -1;
    8828       76864 :         } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
    8829          56 :                 if (wpas_wps_er_start(wpa_s, buf + 13))
    8830           7 :                         reply_len = -1;
    8831       76808 :         } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
    8832          66 :                 wpas_wps_er_stop(wpa_s);
    8833       76742 :         } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
    8834           9 :                 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
    8835           1 :                         reply_len = -1;
    8836       76733 :         } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
    8837          10 :                 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
    8838          10 :                 if (ret == -2) {
    8839           1 :                         os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
    8840           1 :                         reply_len = 17;
    8841           9 :                 } else if (ret == -3) {
    8842           1 :                         os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
    8843           1 :                         reply_len = 18;
    8844           8 :                 } else if (ret == -4) {
    8845           1 :                         os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
    8846           1 :                         reply_len = 20;
    8847           7 :                 } else if (ret)
    8848           1 :                         reply_len = -1;
    8849       76723 :         } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
    8850          16 :                 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
    8851           2 :                         reply_len = -1;
    8852       76707 :         } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
    8853           9 :                 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
    8854             :                                                                 buf + 18))
    8855           1 :                         reply_len = -1;
    8856       76698 :         } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
    8857           6 :                 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
    8858           5 :                         reply_len = -1;
    8859             : #ifdef CONFIG_WPS_NFC
    8860       76692 :         } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
    8861           5 :                 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
    8862             :                         wpa_s, buf + 24, reply, reply_size);
    8863             : #endif /* CONFIG_WPS_NFC */
    8864             : #endif /* CONFIG_WPS_ER */
    8865             : #endif /* CONFIG_WPS */
    8866             : #ifdef CONFIG_IBSS_RSN
    8867       76687 :         } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
    8868           4 :                 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
    8869           2 :                         reply_len = -1;
    8870             : #endif /* CONFIG_IBSS_RSN */
    8871             : #ifdef CONFIG_MESH
    8872       76683 :         } else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
    8873           2 :                 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
    8874             :                         wpa_s, buf + 19, reply, reply_size);
    8875       76681 :         } else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
    8876           5 :                 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
    8877             :                         wpa_s, "", reply, reply_size);
    8878       76676 :         } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
    8879         194 :                 if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
    8880           3 :                         reply_len = -1;
    8881       76482 :         } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
    8882          42 :                 if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
    8883             :                                                                 buf + 18))
    8884           7 :                         reply_len = -1;
    8885       76440 :         } else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
    8886           9 :                 if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
    8887           2 :                         reply_len = -1;
    8888       76431 :         } else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
    8889          13 :                 if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
    8890           4 :                         reply_len = -1;
    8891             : #endif /* CONFIG_MESH */
    8892             : #ifdef CONFIG_P2P
    8893       76418 :         } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
    8894         470 :                 if (p2p_ctrl_find(wpa_s, buf + 8))
    8895           4 :                         reply_len = -1;
    8896       75948 :         } else if (os_strcmp(buf, "P2P_FIND") == 0) {
    8897          23 :                 if (p2p_ctrl_find(wpa_s, ""))
    8898           5 :                         reply_len = -1;
    8899       75925 :         } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
    8900         311 :                 wpas_p2p_stop_find(wpa_s);
    8901       75614 :         } else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
    8902          40 :                 if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
    8903           0 :                         reply_len = -1;
    8904       75574 :         } else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
    8905          13 :                 if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
    8906           1 :                         reply_len = -1;
    8907       75561 :         } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
    8908         413 :                 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
    8909             :                                              reply_size);
    8910       75148 :         } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
    8911           3 :                 if (p2p_ctrl_listen(wpa_s, buf + 11))
    8912           1 :                         reply_len = -1;
    8913       75145 :         } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
    8914         527 :                 if (p2p_ctrl_listen(wpa_s, ""))
    8915           4 :                         reply_len = -1;
    8916       74618 :         } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
    8917         426 :                 if (wpas_p2p_group_remove(wpa_s, buf + 17))
    8918          19 :                         reply_len = -1;
    8919       74192 :         } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
    8920          46 :                 if (p2p_ctrl_group_add(wpa_s, ""))
    8921           0 :                         reply_len = -1;
    8922       74146 :         } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
    8923         153 :                 if (p2p_ctrl_group_add(wpa_s, buf + 14))
    8924           5 :                         reply_len = -1;
    8925       73993 :         } else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
    8926           3 :                 reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
    8927             :                                                   reply_size);
    8928       73990 :         } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
    8929          20 :                 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
    8930           6 :                         reply_len = -1;
    8931       73970 :         } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
    8932           2 :                 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
    8933       73968 :         } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
    8934          93 :                 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
    8935             :                                                    reply_size);
    8936       73875 :         } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
    8937           9 :                 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
    8938           2 :                         reply_len = -1;
    8939       73866 :         } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
    8940          10 :                 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
    8941           8 :                         reply_len = -1;
    8942       73856 :         } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
    8943           1 :                 wpas_p2p_sd_service_update(wpa_s);
    8944       73855 :         } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
    8945           4 :                 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
    8946           1 :                         reply_len = -1;
    8947       73851 :         } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
    8948           1 :                 wpas_p2p_service_flush(wpa_s);
    8949       73850 :         } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
    8950         724 :                 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
    8951          12 :                         reply_len = -1;
    8952       73126 :         } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
    8953         132 :                 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
    8954          46 :                         reply_len = -1;
    8955       72994 :         } else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
    8956           2 :                 if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
    8957           1 :                         reply_len = -1;
    8958       72992 :         } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
    8959           3 :                 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
    8960           1 :                         reply_len = -1;
    8961       72989 :         } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
    8962          74 :                 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
    8963          10 :                         reply_len = -1;
    8964       72915 :         } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
    8965        1264 :                 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
    8966             :                                               reply_size);
    8967       71651 :         } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
    8968         145 :                 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
    8969          27 :                         reply_len = -1;
    8970       71506 :         } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
    8971        6087 :                 p2p_ctrl_flush(wpa_s);
    8972       65419 :         } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
    8973           3 :                 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
    8974           2 :                         reply_len = -1;
    8975       65416 :         } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
    8976          14 :                 if (wpas_p2p_cancel(wpa_s))
    8977          10 :                         reply_len = -1;
    8978       65402 :         } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
    8979           5 :                 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
    8980           2 :                         reply_len = -1;
    8981       65397 :         } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
    8982           1 :                 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
    8983           0 :                         reply_len = -1;
    8984       65396 :         } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
    8985          18 :                 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
    8986           1 :                         reply_len = -1;
    8987       65378 :         } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
    8988          17 :                 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
    8989           0 :                         reply_len = -1;
    8990       65361 :         } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
    8991           7 :                 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
    8992           1 :                         reply_len = -1;
    8993       65354 :         } else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
    8994           3 :                 if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
    8995           3 :                         reply_len = -1;
    8996       65351 :         } else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
    8997           2 :                 if (wpas_p2p_lo_stop(wpa_s))
    8998           0 :                         reply_len = -1;
    8999             : #endif /* CONFIG_P2P */
    9000             : #ifdef CONFIG_WIFI_DISPLAY
    9001       65349 :         } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
    9002          38 :                 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
    9003           8 :                         reply_len = -1;
    9004       65311 :         } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
    9005          16 :                 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
    9006             :                                                      reply, reply_size);
    9007             : #endif /* CONFIG_WIFI_DISPLAY */
    9008             : #ifdef CONFIG_INTERWORKING
    9009       65295 :         } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
    9010          12 :                 if (interworking_fetch_anqp(wpa_s) < 0)
    9011           0 :                         reply_len = -1;
    9012       65283 :         } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
    9013           2 :                 interworking_stop_fetch_anqp(wpa_s);
    9014       65281 :         } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
    9015           1 :                 if (ctrl_interworking_select(wpa_s, NULL) < 0)
    9016           0 :                         reply_len = -1;
    9017       65280 :         } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
    9018         237 :                 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
    9019           1 :                         reply_len = -1;
    9020       65043 :         } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
    9021         105 :                 if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
    9022          44 :                         reply_len = -1;
    9023       64938 :         } else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
    9024             :                 int id;
    9025             : 
    9026           1 :                 id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
    9027           1 :                 if (id < 0)
    9028           0 :                         reply_len = -1;
    9029             :                 else {
    9030           1 :                         reply_len = os_snprintf(reply, reply_size, "%d\n", id);
    9031           1 :                         if (os_snprintf_error(reply_size, reply_len))
    9032           0 :                                 reply_len = -1;
    9033             :                 }
    9034       64937 :         } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
    9035          63 :                 if (get_anqp(wpa_s, buf + 9) < 0)
    9036          19 :                         reply_len = -1;
    9037       64874 :         } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
    9038          25 :                 if (gas_request(wpa_s, buf + 12) < 0)
    9039          16 :                         reply_len = -1;
    9040       64849 :         } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
    9041          14 :                 reply_len = gas_response_get(wpa_s, buf + 17, reply,
    9042             :                                              reply_size);
    9043             : #endif /* CONFIG_INTERWORKING */
    9044             : #ifdef CONFIG_HS20
    9045       64835 :         } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
    9046          24 :                 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
    9047          11 :                         reply_len = -1;
    9048       64811 :         } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
    9049           9 :                 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
    9050           5 :                         reply_len = -1;
    9051       64802 :         } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
    9052          10 :                 if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
    9053           1 :                         reply_len = -1;
    9054       64792 :         } else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
    9055          17 :                 if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
    9056           7 :                         reply_len = -1;
    9057       64775 :         } else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
    9058          32 :                 reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
    9059       64743 :         } else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
    9060          12 :                 if (del_hs20_icon(wpa_s, buf + 14) < 0)
    9061           4 :                         reply_len = -1;
    9062       64731 :         } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
    9063          11 :                 if (hs20_fetch_osu(wpa_s, 0) < 0)
    9064           6 :                         reply_len = -1;
    9065       64720 :         } else if (os_strcmp(buf, "FETCH_OSU no-scan") == 0) {
    9066          32 :                 if (hs20_fetch_osu(wpa_s, 1) < 0)
    9067           0 :                         reply_len = -1;
    9068       64688 :         } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
    9069           2 :                 hs20_cancel_fetch_osu(wpa_s);
    9070             : #endif /* CONFIG_HS20 */
    9071       64686 :         } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
    9072             :         {
    9073          87 :                 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
    9074             :                             wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
    9075           5 :                         reply_len = -1;
    9076             :                 else {
    9077             :                         /*
    9078             :                          * Notify response from timeout to allow the control
    9079             :                          * interface response to be sent first.
    9080             :                          */
    9081          82 :                         eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
    9082             :                                                wpa_s, NULL);
    9083             :                 }
    9084       64599 :         } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
    9085           1 :                 if (wpa_supplicant_reload_configuration(wpa_s))
    9086           0 :                         reply_len = -1;
    9087       64598 :         } else if (os_strcmp(buf, "TERMINATE") == 0) {
    9088           1 :                 wpa_supplicant_terminate_proc(wpa_s->global);
    9089       64597 :         } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
    9090           5 :                 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
    9091           3 :                         reply_len = -1;
    9092       64592 :         } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
    9093          17 :                 reply_len = wpa_supplicant_ctrl_iface_blacklist(
    9094             :                         wpa_s, buf + 9, reply, reply_size);
    9095       64575 :         } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
    9096          21 :                 reply_len = wpa_supplicant_ctrl_iface_log_level(
    9097             :                         wpa_s, buf + 9, reply, reply_size);
    9098       64554 :         } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
    9099           1 :                 reply_len = wpa_supplicant_ctrl_iface_list_networks(
    9100             :                         wpa_s, buf + 14, reply, reply_size);
    9101       64553 :         } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
    9102          35 :                 reply_len = wpa_supplicant_ctrl_iface_list_networks(
    9103             :                         wpa_s, NULL, reply, reply_size);
    9104       64518 :         } else if (os_strcmp(buf, "DISCONNECT") == 0) {
    9105         670 :                 wpas_request_disconnection(wpa_s);
    9106       63848 :         } else if (os_strcmp(buf, "SCAN") == 0) {
    9107          32 :                 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
    9108       63816 :         } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
    9109        1948 :                 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
    9110       61868 :         } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
    9111         329 :                 reply_len = wpa_supplicant_ctrl_iface_scan_results(
    9112             :                         wpa_s, reply, reply_size);
    9113       61539 :         } else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
    9114          10 :                 if (wpas_abort_ongoing_scan(wpa_s) < 0)
    9115           0 :                         reply_len = -1;
    9116       61529 :         } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
    9117        3331 :                 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
    9118           2 :                         reply_len = -1;
    9119       58198 :         } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
    9120          46 :                 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
    9121           2 :                         reply_len = -1;
    9122       58152 :         } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
    9123           9 :                 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
    9124           2 :                         reply_len = -1;
    9125       58143 :         } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
    9126        4459 :                 reply_len = wpa_supplicant_ctrl_iface_add_network(
    9127             :                         wpa_s, reply, reply_size);
    9128       53684 :         } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
    9129        8016 :                 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
    9130           1 :                         reply_len = -1;
    9131       45668 :         } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
    9132       20110 :                 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
    9133          45 :                         reply_len = -1;
    9134       25558 :         } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
    9135          69 :                 reply_len = wpa_supplicant_ctrl_iface_get_network(
    9136             :                         wpa_s, buf + 12, reply, reply_size);
    9137       25489 :         } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
    9138          10 :                 if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
    9139             :                                                           wpa_s))
    9140           6 :                         reply_len = -1;
    9141       25479 :         } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
    9142           8 :                 reply_len = wpa_supplicant_ctrl_iface_list_creds(
    9143             :                         wpa_s, reply, reply_size);
    9144       25471 :         } else if (os_strcmp(buf, "ADD_CRED") == 0) {
    9145         288 :                 reply_len = wpa_supplicant_ctrl_iface_add_cred(
    9146             :                         wpa_s, reply, reply_size);
    9147       25183 :         } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
    9148         179 :                 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
    9149           1 :                         reply_len = -1;
    9150       25004 :         } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
    9151        1098 :                 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
    9152          30 :                         reply_len = -1;
    9153       23906 :         } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
    9154          39 :                 reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
    9155             :                                                                reply,
    9156             :                                                                reply_size);
    9157             : #ifndef CONFIG_NO_CONFIG_WRITE
    9158       23867 :         } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
    9159           7 :                 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
    9160           2 :                         reply_len = -1;
    9161             : #endif /* CONFIG_NO_CONFIG_WRITE */
    9162       23860 :         } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
    9163         382 :                 reply_len = wpa_supplicant_ctrl_iface_get_capability(
    9164             :                         wpa_s, buf + 15, reply, reply_size);
    9165       23478 :         } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
    9166          17 :                 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
    9167           2 :                         reply_len = -1;
    9168       23461 :         } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
    9169          25 :                 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
    9170           1 :                         reply_len = -1;
    9171       23436 :         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
    9172           1 :                 reply_len = wpa_supplicant_global_iface_list(
    9173             :                         wpa_s->global, reply, reply_size);
    9174       23435 :         } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
    9175           3 :                 reply_len = wpa_supplicant_global_iface_interfaces(
    9176             :                         wpa_s->global, buf + 10, reply, reply_size);
    9177       23432 :         } else if (os_strncmp(buf, "BSS ", 4) == 0) {
    9178        2989 :                 reply_len = wpa_supplicant_ctrl_iface_bss(
    9179             :                         wpa_s, buf + 4, reply, reply_size);
    9180             : #ifdef CONFIG_AP
    9181       20443 :         } else if (os_strcmp(buf, "STA-FIRST") == 0) {
    9182           2 :                 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
    9183       20441 :         } else if (os_strncmp(buf, "STA ", 4) == 0) {
    9184           1 :                 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
    9185             :                                               reply_size);
    9186       20440 :         } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
    9187           2 :                 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
    9188             :                                                    reply_size);
    9189       20438 :         } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
    9190          25 :                 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
    9191           2 :                         reply_len = -1;
    9192       20413 :         } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
    9193          23 :                 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
    9194           2 :                         reply_len = -1;
    9195       20390 :         } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
    9196           2 :                 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
    9197           1 :                         reply_len = -1;
    9198       20388 :         } else if (os_strcmp(buf, "STOP_AP") == 0) {
    9199           2 :                 if (wpas_ap_stop_ap(wpa_s))
    9200           0 :                         reply_len = -1;
    9201             : #endif /* CONFIG_AP */
    9202       20386 :         } else if (os_strcmp(buf, "SUSPEND") == 0) {
    9203           1 :                 wpas_notify_suspend(wpa_s->global);
    9204       20385 :         } else if (os_strcmp(buf, "RESUME") == 0) {
    9205           1 :                 wpas_notify_resume(wpa_s->global);
    9206             : #ifdef CONFIG_TESTING_OPTIONS
    9207       20384 :         } else if (os_strcmp(buf, "DROP_SA") == 0) {
    9208           2 :                 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
    9209             : #endif /* CONFIG_TESTING_OPTIONS */
    9210       20382 :         } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
    9211         185 :                 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
    9212           3 :                         reply_len = -1;
    9213       20197 :         } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
    9214           2 :                 wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
    9215       20195 :         } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
    9216           3 :                 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
    9217           1 :                         reply_len = -1;
    9218       20192 :         } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
    9219           4 :                 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
    9220             :                                                                buf + 17))
    9221           1 :                         reply_len = -1;
    9222       20188 :         } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
    9223        1072 :                 wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
    9224             : #ifdef CONFIG_TDLS
    9225       19116 :         } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
    9226           3 :                 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
    9227           2 :                         reply_len = -1;
    9228       19113 :         } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
    9229          33 :                 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
    9230           1 :                         reply_len = -1;
    9231       19080 :         } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
    9232          14 :                 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
    9233           1 :                         reply_len = -1;
    9234       19066 :         } else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
    9235           8 :                 if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
    9236             :                                                                buf + 17))
    9237           7 :                         reply_len = -1;
    9238       19058 :         } else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
    9239           4 :                 if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
    9240             :                                                                       buf + 24))
    9241           3 :                         reply_len = -1;
    9242       19054 :         } else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
    9243           7 :                 reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
    9244             :                         wpa_s, buf + 17, reply, reply_size);
    9245             : #endif /* CONFIG_TDLS */
    9246       19047 :         } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
    9247          13 :                 reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
    9248       19034 :         } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
    9249          16 :                 if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
    9250           9 :                         reply_len = -1;
    9251       19018 :         } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
    9252           3 :                 if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
    9253           2 :                         reply_len = -1;
    9254       19015 :         } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
    9255          20 :                 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
    9256             :                                                        reply_size);
    9257       18995 :         } else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
    9258           8 :                 if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
    9259           2 :                         reply_len = -1;
    9260       18987 :         } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
    9261           1 :                 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
    9262             :                                                        reply_size);
    9263             : #ifdef CONFIG_AUTOSCAN
    9264       18986 :         } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
    9265          12 :                 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
    9266           1 :                         reply_len = -1;
    9267             : #endif /* CONFIG_AUTOSCAN */
    9268       18974 :         } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
    9269           1 :                 reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
    9270             :                                                          reply_size);
    9271             : #ifdef ANDROID
    9272             :         } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
    9273             :                 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
    9274             :                                                       reply_size);
    9275             : #endif /* ANDROID */
    9276       18973 :         } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
    9277          12 :                 reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
    9278             :                                                       reply_size);
    9279       18961 :         } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
    9280          94 :                 pmksa_cache_clear_current(wpa_s->wpa);
    9281          94 :                 eapol_sm_request_reauth(wpa_s->eapol);
    9282             : #ifdef CONFIG_WNM
    9283       18867 :         } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
    9284          24 :                 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
    9285           9 :                         reply_len = -1;
    9286       18843 :         } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
    9287           2 :                 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
    9288           0 :                                 reply_len = -1;
    9289             : #endif /* CONFIG_WNM */
    9290       18841 :         } else if (os_strcmp(buf, "FLUSH") == 0) {
    9291        6060 :                 wpa_supplicant_ctrl_iface_flush(wpa_s);
    9292       12781 :         } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
    9293          47 :                 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
    9294             :                                                  reply_size);
    9295             : #ifdef CONFIG_TESTING_OPTIONS
    9296       12734 :         } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
    9297          69 :                 if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
    9298           6 :                         reply_len = -1;
    9299       12665 :         } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
    9300           1 :                 wpas_ctrl_iface_mgmt_tx_done(wpa_s);
    9301       12664 :         } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
    9302          44 :                 if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
    9303           0 :                         reply_len = -1;
    9304       12620 :         } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
    9305          17 :                 if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
    9306           2 :                         reply_len = -1;
    9307       12603 :         } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
    9308         495 :                 if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
    9309           4 :                         reply_len = -1;
    9310       12108 :         } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
    9311        3068 :                 if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
    9312           0 :                         reply_len = -1;
    9313        9040 :         } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
    9314        3146 :                 if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
    9315           5 :                         reply_len = -1;
    9316        5894 :         } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
    9317         115 :                 if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
    9318           6 :                         reply_len = -1;
    9319        5779 :         } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
    9320         971 :                 if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
    9321           0 :                         reply_len = -1;
    9322        4808 :         } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
    9323        3668 :                 reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
    9324        1140 :         } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
    9325         249 :                 if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0)
    9326           0 :                         reply_len = -1;
    9327         891 :         } else if (os_strcmp(buf, "GET_FAIL") == 0) {
    9328         623 :                 reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size);
    9329         268 :         } else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
    9330           1 :                 if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
    9331           0 :                         reply_len = -1;
    9332         267 :         } else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
    9333          20 :                 if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
    9334           0 :                         reply_len = -1;
    9335             : #endif /* CONFIG_TESTING_OPTIONS */
    9336         247 :         } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
    9337          67 :                 if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
    9338           8 :                         reply_len = -1;
    9339         180 :         } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
    9340           9 :                 reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
    9341             :                                                       reply_size);
    9342         171 :         } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
    9343          83 :                 if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
    9344          18 :                         reply_len = -1;
    9345          88 :         } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
    9346          19 :                 if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
    9347           2 :                         reply_len = -1;
    9348          69 :         } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
    9349          52 :                 wpas_ctrl_iface_erp_flush(wpa_s);
    9350          17 :         } else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
    9351          16 :                 if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
    9352          12 :                         reply_len = -1;
    9353           1 :         } else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
    9354           0 :                 reply_len = wpas_ctrl_iface_get_pref_freq_list(
    9355             :                         wpa_s, buf + 19, reply, reply_size);
    9356             :         } else {
    9357           1 :                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
    9358           1 :                 reply_len = 16;
    9359             :         }
    9360             : 
    9361      123487 :         if (reply_len < 0) {
    9362        1404 :                 os_memcpy(reply, "FAIL\n", 5);
    9363        1404 :                 reply_len = 5;
    9364             :         }
    9365             : 
    9366      123487 :         *resp_len = reply_len;
    9367      123487 :         return reply;
    9368             : }
    9369             : 
    9370             : 
    9371         424 : static int wpa_supplicant_global_iface_add(struct wpa_global *global,
    9372             :                                            char *cmd)
    9373             : {
    9374             :         struct wpa_interface iface;
    9375             :         char *pos, *extra;
    9376             :         struct wpa_supplicant *wpa_s;
    9377         424 :         unsigned int create_iface = 0;
    9378             :         u8 mac_addr[ETH_ALEN];
    9379         424 :         enum wpa_driver_if_type type = WPA_IF_STATION;
    9380             : 
    9381             :         /*
    9382             :          * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
    9383             :          * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
    9384             :          */
    9385         424 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
    9386             : 
    9387         424 :         os_memset(&iface, 0, sizeof(iface));
    9388             : 
    9389             :         do {
    9390         424 :                 iface.ifname = pos = cmd;
    9391         424 :                 pos = os_strchr(pos, '\t');
    9392         424 :                 if (pos)
    9393         422 :                         *pos++ = '\0';
    9394         424 :                 if (iface.ifname[0] == '\0')
    9395           1 :                         return -1;
    9396         423 :                 if (pos == NULL)
    9397           1 :                         break;
    9398             : 
    9399         422 :                 iface.confname = pos;
    9400         422 :                 pos = os_strchr(pos, '\t');
    9401         422 :                 if (pos)
    9402         421 :                         *pos++ = '\0';
    9403         422 :                 if (iface.confname[0] == '\0')
    9404         404 :                         iface.confname = NULL;
    9405         422 :                 if (pos == NULL)
    9406           1 :                         break;
    9407             : 
    9408         421 :                 iface.driver = pos;
    9409         421 :                 pos = os_strchr(pos, '\t');
    9410         421 :                 if (pos)
    9411         420 :                         *pos++ = '\0';
    9412         421 :                 if (iface.driver[0] == '\0')
    9413           1 :                         iface.driver = NULL;
    9414         421 :                 if (pos == NULL)
    9415           1 :                         break;
    9416             : 
    9417         420 :                 iface.ctrl_interface = pos;
    9418         420 :                 pos = os_strchr(pos, '\t');
    9419         420 :                 if (pos)
    9420         303 :                         *pos++ = '\0';
    9421         420 :                 if (iface.ctrl_interface[0] == '\0')
    9422           1 :                         iface.ctrl_interface = NULL;
    9423         420 :                 if (pos == NULL)
    9424         117 :                         break;
    9425             : 
    9426         303 :                 iface.driver_param = pos;
    9427         303 :                 pos = os_strchr(pos, '\t');
    9428         303 :                 if (pos)
    9429          22 :                         *pos++ = '\0';
    9430         303 :                 if (iface.driver_param[0] == '\0')
    9431          19 :                         iface.driver_param = NULL;
    9432         303 :                 if (pos == NULL)
    9433         281 :                         break;
    9434             : 
    9435          22 :                 iface.bridge_ifname = pos;
    9436          22 :                 pos = os_strchr(pos, '\t');
    9437          22 :                 if (pos)
    9438          16 :                         *pos++ = '\0';
    9439          22 :                 if (iface.bridge_ifname[0] == '\0')
    9440          15 :                         iface.bridge_ifname = NULL;
    9441          22 :                 if (pos == NULL)
    9442           6 :                         break;
    9443             : 
    9444          16 :                 extra = pos;
    9445          16 :                 pos = os_strchr(pos, '\t');
    9446          16 :                 if (pos)
    9447           3 :                         *pos++ = '\0';
    9448          16 :                 if (!extra[0])
    9449           1 :                         break;
    9450             : 
    9451          15 :                 if (os_strcmp(extra, "create") == 0) {
    9452          14 :                         create_iface = 1;
    9453          14 :                         if (!pos)
    9454          11 :                                 break;
    9455             : 
    9456           3 :                         if (os_strcmp(pos, "sta") == 0) {
    9457           1 :                                 type = WPA_IF_STATION;
    9458           2 :                         } else if (os_strcmp(pos, "ap") == 0) {
    9459           1 :                                 type = WPA_IF_AP_BSS;
    9460             :                         } else {
    9461           1 :                                 wpa_printf(MSG_DEBUG,
    9462             :                                            "INTERFACE_ADD unsupported interface type: '%s'",
    9463             :                                            pos);
    9464           1 :                                 return -1;
    9465             :                         }
    9466             :                 } else {
    9467           1 :                         wpa_printf(MSG_DEBUG,
    9468             :                                    "INTERFACE_ADD unsupported extra parameter: '%s'",
    9469             :                                    extra);
    9470           1 :                         return -1;
    9471             :                 }
    9472             :         } while (0);
    9473             : 
    9474         421 :         if (create_iface) {
    9475          13 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
    9476             :                            iface.ifname);
    9477          13 :                 if (!global->ifaces)
    9478           0 :                         return -1;
    9479          13 :                 if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
    9480             :                                    NULL, NULL, NULL, mac_addr, NULL) < 0) {
    9481           0 :                         wpa_printf(MSG_ERROR,
    9482             :                                    "CTRL_IFACE interface creation failed");
    9483           0 :                         return -1;
    9484             :                 }
    9485             : 
    9486          78 :                 wpa_printf(MSG_DEBUG,
    9487             :                            "CTRL_IFACE interface '%s' created with MAC addr: "
    9488          78 :                            MACSTR, iface.ifname, MAC2STR(mac_addr));
    9489             :         }
    9490             : 
    9491         421 :         if (wpa_supplicant_get_iface(global, iface.ifname))
    9492           0 :                 goto fail;
    9493             : 
    9494         421 :         wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
    9495         421 :         if (!wpa_s)
    9496           9 :                 goto fail;
    9497         412 :         wpa_s->added_vif = create_iface;
    9498         412 :         return 0;
    9499             : 
    9500             : fail:
    9501           9 :         if (create_iface)
    9502           0 :                 wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
    9503           9 :         return -1;
    9504             : }
    9505             : 
    9506             : 
    9507         413 : static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
    9508             :                                               char *cmd)
    9509             : {
    9510             :         struct wpa_supplicant *wpa_s;
    9511             :         int ret;
    9512             :         unsigned int delete_iface;
    9513             : 
    9514         413 :         wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
    9515             : 
    9516         413 :         wpa_s = wpa_supplicant_get_iface(global, cmd);
    9517         413 :         if (wpa_s == NULL)
    9518           1 :                 return -1;
    9519         412 :         delete_iface = wpa_s->added_vif;
    9520         412 :         ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
    9521         412 :         if (!ret && delete_iface) {
    9522          13 :                 wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
    9523             :                            cmd);
    9524          13 :                 ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
    9525             :         }
    9526         412 :         return ret;
    9527             : }
    9528             : 
    9529             : 
    9530           2 : static void wpa_free_iface_info(struct wpa_interface_info *iface)
    9531             : {
    9532             :         struct wpa_interface_info *prev;
    9533             : 
    9534           4 :         while (iface) {
    9535           0 :                 prev = iface;
    9536           0 :                 iface = iface->next;
    9537             : 
    9538           0 :                 os_free(prev->ifname);
    9539           0 :                 os_free(prev->desc);
    9540           0 :                 os_free(prev);
    9541             :         }
    9542           2 : }
    9543             : 
    9544             : 
    9545           2 : static int wpa_supplicant_global_iface_list(struct wpa_global *global,
    9546             :                                             char *buf, int len)
    9547             : {
    9548             :         int i, res;
    9549           2 :         struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
    9550             :         char *pos, *end;
    9551             : 
    9552           8 :         for (i = 0; wpa_drivers[i]; i++) {
    9553           6 :                 const struct wpa_driver_ops *drv = wpa_drivers[i];
    9554           6 :                 if (drv->get_interfaces == NULL)
    9555           6 :                         continue;
    9556           0 :                 tmp = drv->get_interfaces(global->drv_priv[i]);
    9557           0 :                 if (tmp == NULL)
    9558           0 :                         continue;
    9559             : 
    9560           0 :                 if (last == NULL)
    9561           0 :                         iface = last = tmp;
    9562             :                 else
    9563           0 :                         last->next = tmp;
    9564           0 :                 while (last->next)
    9565           0 :                         last = last->next;
    9566             :         }
    9567             : 
    9568           2 :         pos = buf;
    9569           2 :         end = buf + len;
    9570           2 :         for (tmp = iface; tmp; tmp = tmp->next) {
    9571           0 :                 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
    9572             :                                   tmp->drv_name, tmp->ifname,
    9573           0 :                                   tmp->desc ? tmp->desc : "");
    9574           0 :                 if (os_snprintf_error(end - pos, res)) {
    9575           0 :                         *pos = '\0';
    9576           0 :                         break;
    9577             :                 }
    9578           0 :                 pos += res;
    9579             :         }
    9580             : 
    9581           2 :         wpa_free_iface_info(iface);
    9582             : 
    9583           2 :         return pos - buf;
    9584             : }
    9585             : 
    9586             : 
    9587        2020 : static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
    9588             :                                                   const char *input,
    9589             :                                                   char *buf, int len)
    9590             : {
    9591             :         int res;
    9592             :         char *pos, *end;
    9593             :         struct wpa_supplicant *wpa_s;
    9594        2020 :         int show_ctrl = 0;
    9595             : 
    9596        2020 :         if (input)
    9597        2020 :                 show_ctrl = !!os_strstr(input, "ctrl");
    9598             : 
    9599        2020 :         wpa_s = global->ifaces;
    9600        2020 :         pos = buf;
    9601        2020 :         end = buf + len;
    9602             : 
    9603        4250 :         while (wpa_s) {
    9604         210 :                 if (show_ctrl)
    9605           0 :                         res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
    9606           0 :                                           wpa_s->ifname,
    9607           0 :                                           wpa_s->conf->ctrl_interface ?
    9608           0 :                                           wpa_s->conf->ctrl_interface : "N/A");
    9609             :                 else
    9610         210 :                         res = os_snprintf(pos, end - pos, "%s\n",
    9611         210 :                                           wpa_s->ifname);
    9612             : 
    9613         210 :                 if (os_snprintf_error(end - pos, res)) {
    9614           0 :                         *pos = '\0';
    9615           0 :                         break;
    9616             :                 }
    9617         210 :                 pos += res;
    9618         210 :                 wpa_s = wpa_s->next;
    9619             :         }
    9620        2020 :         return pos - buf;
    9621             : }
    9622             : 
    9623             : 
    9624        6409 : static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
    9625             :                                             const char *ifname,
    9626             :                                             char *cmd, size_t *resp_len)
    9627             : {
    9628             :         struct wpa_supplicant *wpa_s;
    9629             : 
    9630       12804 :         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    9631        6409 :                 if (os_strcmp(ifname, wpa_s->ifname) == 0)
    9632          14 :                         break;
    9633             :         }
    9634             : 
    9635        6409 :         if (wpa_s == NULL) {
    9636        6395 :                 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
    9637        6395 :                 if (resp)
    9638        6394 :                         *resp_len = os_strlen(resp);
    9639             :                 else
    9640           1 :                         *resp_len = 1;
    9641        6395 :                 return resp;
    9642             :         }
    9643             : 
    9644          14 :         return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
    9645             : }
    9646             : 
    9647             : 
    9648       42595 : static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
    9649             :                                                char *buf, size_t *resp_len)
    9650             : {
    9651             : #ifdef CONFIG_P2P
    9652             :         static const char * cmd[] = {
    9653             :                 "LIST_NETWORKS",
    9654             :                 "P2P_FIND",
    9655             :                 "P2P_STOP_FIND",
    9656             :                 "P2P_LISTEN",
    9657             :                 "P2P_GROUP_ADD",
    9658             :                 "P2P_GET_PASSPHRASE",
    9659             :                 "P2P_SERVICE_UPDATE",
    9660             :                 "P2P_SERVICE_FLUSH",
    9661             :                 "P2P_FLUSH",
    9662             :                 "P2P_CANCEL",
    9663             :                 "P2P_PRESENCE_REQ",
    9664             :                 "P2P_EXT_LISTEN",
    9665             :                 NULL
    9666             :         };
    9667             :         static const char * prefix[] = {
    9668             : #ifdef ANDROID
    9669             :                 "DRIVER ",
    9670             : #endif /* ANDROID */
    9671             :                 "GET_NETWORK ",
    9672             :                 "REMOVE_NETWORK ",
    9673             :                 "P2P_FIND ",
    9674             :                 "P2P_CONNECT ",
    9675             :                 "P2P_LISTEN ",
    9676             :                 "P2P_GROUP_REMOVE ",
    9677             :                 "P2P_GROUP_ADD ",
    9678             :                 "P2P_GROUP_MEMBER ",
    9679             :                 "P2P_PROV_DISC ",
    9680             :                 "P2P_SERV_DISC_REQ ",
    9681             :                 "P2P_SERV_DISC_CANCEL_REQ ",
    9682             :                 "P2P_SERV_DISC_RESP ",
    9683             :                 "P2P_SERV_DISC_EXTERNAL ",
    9684             :                 "P2P_SERVICE_ADD ",
    9685             :                 "P2P_SERVICE_DEL ",
    9686             :                 "P2P_SERVICE_REP ",
    9687             :                 "P2P_REJECT ",
    9688             :                 "P2P_INVITE ",
    9689             :                 "P2P_PEER ",
    9690             :                 "P2P_SET ",
    9691             :                 "P2P_UNAUTHORIZE ",
    9692             :                 "P2P_PRESENCE_REQ ",
    9693             :                 "P2P_EXT_LISTEN ",
    9694             :                 "P2P_REMOVE_CLIENT ",
    9695             :                 "WPS_NFC_TOKEN ",
    9696             :                 "WPS_NFC_TAG_READ ",
    9697             :                 "NFC_GET_HANDOVER_SEL ",
    9698             :                 "NFC_GET_HANDOVER_REQ ",
    9699             :                 "NFC_REPORT_HANDOVER ",
    9700             :                 "P2P_ASP_PROVISION ",
    9701             :                 "P2P_ASP_PROVISION_RESP ",
    9702             :                 NULL
    9703             :         };
    9704       42595 :         int found = 0;
    9705             :         int i;
    9706             : 
    9707       42595 :         if (global->p2p_init_wpa_s == NULL)
    9708        4757 :                 return NULL;
    9709             : 
    9710      465987 :         for (i = 0; !found && cmd[i]; i++) {
    9711      428149 :                 if (os_strcmp(buf, cmd[i]) == 0)
    9712        7018 :                         found = 1;
    9713             :         }
    9714             : 
    9715      747467 :         for (i = 0; !found && prefix[i]; i++) {
    9716      709629 :                 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
    9717        9924 :                         found = 1;
    9718             :         }
    9719             : 
    9720       37838 :         if (found)
    9721       16942 :                 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
    9722             :                                                          buf, resp_len);
    9723             : #endif /* CONFIG_P2P */
    9724       20896 :         return NULL;
    9725             : }
    9726             : 
    9727             : 
    9728       25653 : static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
    9729             :                                                char *buf, size_t *resp_len)
    9730             : {
    9731             : #ifdef CONFIG_WIFI_DISPLAY
    9732       25653 :         if (global->p2p_init_wpa_s == NULL)
    9733        4757 :                 return NULL;
    9734       41791 :         if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
    9735       20895 :             os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
    9736           2 :                 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
    9737             :                                                          buf, resp_len);
    9738             : #endif /* CONFIG_WIFI_DISPLAY */
    9739       20894 :         return NULL;
    9740             : }
    9741             : 
    9742             : 
    9743       42595 : static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
    9744             :                                            char *buf, size_t *resp_len)
    9745             : {
    9746             :         char *ret;
    9747             : 
    9748       42595 :         ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
    9749       42595 :         if (ret)
    9750       16942 :                 return ret;
    9751             : 
    9752       25653 :         ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
    9753       25653 :         if (ret)
    9754           2 :                 return ret;
    9755             : 
    9756       25651 :         return NULL;
    9757             : }
    9758             : 
    9759             : 
    9760        6346 : static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
    9761             : {
    9762             :         char *value;
    9763             : 
    9764        6346 :         value = os_strchr(cmd, ' ');
    9765        6346 :         if (value == NULL)
    9766           1 :                 return -1;
    9767        6345 :         *value++ = '\0';
    9768             : 
    9769        6345 :         wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
    9770             : 
    9771             : #ifdef CONFIG_WIFI_DISPLAY
    9772        6345 :         if (os_strcasecmp(cmd, "wifi_display") == 0) {
    9773           4 :                 wifi_display_enable(global, !!atoi(value));
    9774           4 :                 return 0;
    9775             :         }
    9776             : #endif /* CONFIG_WIFI_DISPLAY */
    9777             : 
    9778             :         /* Restore cmd to its original value to allow redirection */
    9779        6341 :         value[-1] = ' ';
    9780             : 
    9781        6341 :         return -1;
    9782             : }
    9783             : 
    9784             : 
    9785           9 : static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
    9786             :                                               char *cmd)
    9787             : {
    9788             :         struct wpa_supplicant *wpa_s[2]; /* src, dst */
    9789             :         char *p;
    9790             :         unsigned int i;
    9791             : 
    9792             :         /* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
    9793             :          * <variable name> */
    9794             : 
    9795          22 :         for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
    9796          16 :                 p = os_strchr(cmd, ' ');
    9797          16 :                 if (p == NULL)
    9798           3 :                         return -1;
    9799          13 :                 *p = '\0';
    9800             : 
    9801          13 :                 wpa_s[i] = global->ifaces;
    9802          13 :                 for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
    9803          13 :                         if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
    9804          13 :                                 break;
    9805             :                 }
    9806             : 
    9807          13 :                 if (!wpa_s[i]) {
    9808           0 :                         wpa_printf(MSG_DEBUG,
    9809             :                                    "CTRL_IFACE: Could not find iface=%s", cmd);
    9810           0 :                         return -1;
    9811             :                 }
    9812             : 
    9813          13 :                 cmd = p + 1;
    9814             :         }
    9815             : 
    9816           6 :         return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
    9817             : }
    9818             : 
    9819             : 
    9820             : #ifndef CONFIG_NO_CONFIG_WRITE
    9821           4 : static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
    9822             : {
    9823           4 :         int ret = 0, saved = 0;
    9824             :         struct wpa_supplicant *wpa_s;
    9825             : 
    9826           8 :         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    9827           4 :                 if (!wpa_s->conf->update_config) {
    9828           2 :                         wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
    9829           2 :                         continue;
    9830             :                 }
    9831             : 
    9832           2 :                 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
    9833           1 :                         wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
    9834           1 :                         ret = 1;
    9835             :                 } else {
    9836           1 :                         wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
    9837           1 :                         saved++;
    9838             :                 }
    9839             :         }
    9840             : 
    9841           4 :         if (!saved && !ret) {
    9842           2 :                 wpa_dbg(wpa_s, MSG_DEBUG,
    9843             :                         "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
    9844           2 :                 ret = 1;
    9845             :         }
    9846             : 
    9847           4 :         return ret;
    9848             : }
    9849             : #endif /* CONFIG_NO_CONFIG_WRITE */
    9850             : 
    9851             : 
    9852           9 : static int wpas_global_ctrl_iface_status(struct wpa_global *global,
    9853             :                                          char *buf, size_t buflen)
    9854             : {
    9855             :         char *pos, *end;
    9856             :         int ret;
    9857             :         struct wpa_supplicant *wpa_s;
    9858             : 
    9859           9 :         pos = buf;
    9860           9 :         end = buf + buflen;
    9861             : 
    9862             : #ifdef CONFIG_P2P
    9863           9 :         if (global->p2p && !global->p2p_disabled) {
    9864          56 :                 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
    9865             :                                   "\n"
    9866             :                                   "p2p_state=%s\n",
    9867          48 :                                   MAC2STR(global->p2p_dev_addr),
    9868             :                                   p2p_get_state_txt(global->p2p));
    9869           8 :                 if (os_snprintf_error(end - pos, ret))
    9870           0 :                         return pos - buf;
    9871           8 :                 pos += ret;
    9872           1 :         } else if (global->p2p) {
    9873           1 :                 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
    9874           1 :                 if (os_snprintf_error(end - pos, ret))
    9875           0 :                         return pos - buf;
    9876           1 :                 pos += ret;
    9877             :         }
    9878             : #endif /* CONFIG_P2P */
    9879             : 
    9880             : #ifdef CONFIG_WIFI_DISPLAY
    9881           9 :         ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
    9882           9 :                           !!global->wifi_display);
    9883           9 :         if (os_snprintf_error(end - pos, ret))
    9884           0 :                 return pos - buf;
    9885           9 :         pos += ret;
    9886             : #endif /* CONFIG_WIFI_DISPLAY */
    9887             : 
    9888          18 :         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    9889          63 :                 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
    9890             :                                   "address=" MACSTR "\n",
    9891          63 :                                   wpa_s->ifname, MAC2STR(wpa_s->own_addr));
    9892           9 :                 if (os_snprintf_error(end - pos, ret))
    9893           0 :                         return pos - buf;
    9894           9 :                 pos += ret;
    9895             :         }
    9896             : 
    9897           9 :         return pos - buf;
    9898             : }
    9899             : 
    9900             : 
    9901             : #ifdef CONFIG_FST
    9902             : 
    9903         264 : static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
    9904             :                                              char *cmd, char *buf,
    9905             :                                              size_t reply_size)
    9906             : {
    9907             :         char ifname[IFNAMSIZ + 1];
    9908             :         struct fst_iface_cfg cfg;
    9909             :         struct wpa_supplicant *wpa_s;
    9910             :         struct fst_wpa_obj iface_obj;
    9911             : 
    9912         264 :         if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
    9913         264 :                 wpa_s = wpa_supplicant_get_iface(global, ifname);
    9914         264 :                 if (wpa_s) {
    9915         263 :                         if (wpa_s->fst) {
    9916           1 :                                 wpa_printf(MSG_INFO, "FST: Already attached");
    9917           1 :                                 return -1;
    9918             :                         }
    9919         262 :                         fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
    9920         262 :                         wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
    9921             :                                                 &iface_obj, &cfg);
    9922         262 :                         if (wpa_s->fst)
    9923         262 :                                 return os_snprintf(buf, reply_size, "OK\n");
    9924             :                 }
    9925             :         }
    9926             : 
    9927           1 :         return -1;
    9928             : }
    9929             : 
    9930             : 
    9931         254 : static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
    9932             :                                              char *cmd, char *buf,
    9933             :                                              size_t reply_size)
    9934             : {
    9935             :         char ifname[IFNAMSIZ + 1];
    9936             :         struct wpa_supplicant *wpa_s;
    9937             : 
    9938         254 :         if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
    9939         254 :                 wpa_s = wpa_supplicant_get_iface(global, ifname);
    9940         254 :                 if (wpa_s) {
    9941         254 :                         if (!fst_iface_detach(ifname)) {
    9942         254 :                                 wpa_s->fst = NULL;
    9943         254 :                                 return os_snprintf(buf, reply_size, "OK\n");
    9944             :                         }
    9945             :                 }
    9946             :         }
    9947             : 
    9948           0 :         return -1;
    9949             : }
    9950             : 
    9951             : #endif /* CONFIG_FST */
    9952             : 
    9953             : 
    9954       49004 : char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
    9955             :                                                 char *buf, size_t *resp_len)
    9956             : {
    9957             :         char *reply;
    9958       49004 :         const int reply_size = 2048;
    9959             :         int reply_len;
    9960       49004 :         int level = MSG_DEBUG;
    9961             : 
    9962       49004 :         if (os_strncmp(buf, "IFNAME=", 7) == 0) {
    9963        6409 :                 char *pos = os_strchr(buf + 7, ' ');
    9964        6409 :                 if (pos) {
    9965        6409 :                         *pos++ = '\0';
    9966        6409 :                         return wpas_global_ctrl_iface_ifname(global,
    9967             :                                                              buf + 7, pos,
    9968             :                                                              resp_len);
    9969             :                 }
    9970             :         }
    9971             : 
    9972       42595 :         reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
    9973       42595 :         if (reply)
    9974       16944 :                 return reply;
    9975             : 
    9976       25651 :         if (os_strcmp(buf, "PING") == 0)
    9977        6292 :                 level = MSG_EXCESSIVE;
    9978       25651 :         wpa_hexdump_ascii(level, "RX global ctrl_iface",
    9979             :                           (const u8 *) buf, os_strlen(buf));
    9980             : 
    9981       25651 :         reply = os_malloc(reply_size);
    9982       25651 :         if (reply == NULL) {
    9983           1 :                 *resp_len = 1;
    9984           1 :                 return NULL;
    9985             :         }
    9986             : 
    9987       25650 :         os_memcpy(reply, "OK\n", 3);
    9988       25650 :         reply_len = 3;
    9989             : 
    9990       25650 :         if (os_strcmp(buf, "PING") == 0) {
    9991        6292 :                 os_memcpy(reply, "PONG\n", 5);
    9992        6292 :                 reply_len = 5;
    9993       19358 :         } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
    9994         424 :                 if (wpa_supplicant_global_iface_add(global, buf + 14))
    9995          12 :                         reply_len = -1;
    9996       18934 :         } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
    9997         413 :                 if (wpa_supplicant_global_iface_remove(global, buf + 17))
    9998           1 :                         reply_len = -1;
    9999       18521 :         } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
   10000           1 :                 reply_len = wpa_supplicant_global_iface_list(
   10001             :                         global, reply, reply_size);
   10002       18520 :         } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
   10003        2017 :                 reply_len = wpa_supplicant_global_iface_interfaces(
   10004             :                         global, buf + 10, reply, reply_size);
   10005             : #ifdef CONFIG_FST
   10006       16503 :         } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
   10007         264 :                 reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
   10008             :                                                               reply,
   10009             :                                                               reply_size);
   10010       16239 :         } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
   10011         254 :                 reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
   10012             :                                                               reply,
   10013             :                                                               reply_size);
   10014       15985 :         } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
   10015        1578 :                 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
   10016             : #endif /* CONFIG_FST */
   10017       14407 :         } else if (os_strcmp(buf, "TERMINATE") == 0) {
   10018           0 :                 wpa_supplicant_terminate_proc(global);
   10019       14407 :         } else if (os_strcmp(buf, "SUSPEND") == 0) {
   10020           1 :                 wpas_notify_suspend(global);
   10021       14406 :         } else if (os_strcmp(buf, "RESUME") == 0) {
   10022           1 :                 wpas_notify_resume(global);
   10023       14405 :         } else if (os_strncmp(buf, "SET ", 4) == 0) {
   10024        6346 :                 if (wpas_global_ctrl_iface_set(global, buf + 4)) {
   10025             : #ifdef CONFIG_P2P
   10026        6342 :                         if (global->p2p_init_wpa_s) {
   10027        6342 :                                 os_free(reply);
   10028             :                                 /* Check if P2P redirection would work for this
   10029             :                                  * command. */
   10030        6342 :                                 return wpa_supplicant_ctrl_iface_process(
   10031             :                                         global->p2p_init_wpa_s,
   10032             :                                         buf, resp_len);
   10033             :                         }
   10034             : #endif /* CONFIG_P2P */
   10035           0 :                         reply_len = -1;
   10036             :                 }
   10037        8059 :         } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
   10038           9 :                 if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
   10039           5 :                         reply_len = -1;
   10040             : #ifndef CONFIG_NO_CONFIG_WRITE
   10041        8050 :         } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
   10042           4 :                 if (wpas_global_ctrl_iface_save_config(global))
   10043           3 :                         reply_len = -1;
   10044             : #endif /* CONFIG_NO_CONFIG_WRITE */
   10045        8046 :         } else if (os_strcmp(buf, "STATUS") == 0) {
   10046           9 :                 reply_len = wpas_global_ctrl_iface_status(global, reply,
   10047             :                                                           reply_size);
   10048             : #ifdef CONFIG_MODULE_TESTS
   10049        8037 :         } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
   10050           1 :                 if (wpas_module_tests() < 0)
   10051           0 :                         reply_len = -1;
   10052             : #endif /* CONFIG_MODULE_TESTS */
   10053        8036 :         } else if (os_strncmp(buf, "RELOG", 5) == 0) {
   10054        8035 :                 if (wpa_debug_reopen_file() < 0)
   10055           0 :                         reply_len = -1;
   10056             :         } else {
   10057           1 :                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
   10058           1 :                 reply_len = 16;
   10059             :         }
   10060             : 
   10061       19308 :         if (reply_len < 0) {
   10062          23 :                 os_memcpy(reply, "FAIL\n", 5);
   10063          23 :                 reply_len = 5;
   10064             :         }
   10065             : 
   10066       19308 :         *resp_len = reply_len;
   10067       19308 :         return reply;
   10068             : }

Generated by: LCOV version 1.10