LCOV - code coverage report
Current view: top level - wpa_supplicant - p2p_supplicant.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1388338050 Lines: 2177 3393 64.2 %
Date: 2013-12-29 Functions: 140 178 78.7 %
Branches: 1032 2118 48.7 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * wpa_supplicant - P2P
       3                 :            :  * Copyright (c) 2009-2010, Atheros Communications
       4                 :            :  *
       5                 :            :  * This software may be distributed under the terms of the BSD license.
       6                 :            :  * See README for more details.
       7                 :            :  */
       8                 :            : 
       9                 :            : #include "includes.h"
      10                 :            : 
      11                 :            : #include "common.h"
      12                 :            : #include "eloop.h"
      13                 :            : #include "common/ieee802_11_common.h"
      14                 :            : #include "common/ieee802_11_defs.h"
      15                 :            : #include "common/wpa_ctrl.h"
      16                 :            : #include "wps/wps_i.h"
      17                 :            : #include "p2p/p2p.h"
      18                 :            : #include "ap/hostapd.h"
      19                 :            : #include "ap/ap_config.h"
      20                 :            : #include "ap/sta_info.h"
      21                 :            : #include "ap/ap_drv_ops.h"
      22                 :            : #include "ap/p2p_hostapd.h"
      23                 :            : #include "eapol_supp/eapol_supp_sm.h"
      24                 :            : #include "rsn_supp/wpa.h"
      25                 :            : #include "wpa_supplicant_i.h"
      26                 :            : #include "driver_i.h"
      27                 :            : #include "ap.h"
      28                 :            : #include "config_ssid.h"
      29                 :            : #include "config.h"
      30                 :            : #include "notify.h"
      31                 :            : #include "scan.h"
      32                 :            : #include "bss.h"
      33                 :            : #include "offchannel.h"
      34                 :            : #include "wps_supplicant.h"
      35                 :            : #include "p2p_supplicant.h"
      36                 :            : #include "wifi_display.h"
      37                 :            : 
      38                 :            : 
      39                 :            : /*
      40                 :            :  * How many times to try to scan to find the GO before giving up on join
      41                 :            :  * request.
      42                 :            :  */
      43                 :            : #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
      44                 :            : 
      45                 :            : #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
      46                 :            : 
      47                 :            : #ifndef P2P_MAX_CLIENT_IDLE
      48                 :            : /*
      49                 :            :  * How many seconds to try to reconnect to the GO when connection in P2P client
      50                 :            :  * role has been lost.
      51                 :            :  */
      52                 :            : #define P2P_MAX_CLIENT_IDLE 10
      53                 :            : #endif /* P2P_MAX_CLIENT_IDLE */
      54                 :            : 
      55                 :            : #ifndef P2P_MAX_INITIAL_CONN_WAIT
      56                 :            : /*
      57                 :            :  * How many seconds to wait for initial 4-way handshake to get completed after
      58                 :            :  * WPS provisioning step.
      59                 :            :  */
      60                 :            : #define P2P_MAX_INITIAL_CONN_WAIT 10
      61                 :            : #endif /* P2P_MAX_INITIAL_CONN_WAIT */
      62                 :            : 
      63                 :            : #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
      64                 :            : /*
      65                 :            :  * How many seconds to wait for initial 4-way handshake to get completed after
      66                 :            :  * WPS provisioning step on the GO. This controls the extra time the P2P
      67                 :            :  * operation is considered to be in progress (e.g., to delay other scans) after
      68                 :            :  * WPS provisioning has been completed on the GO during group formation.
      69                 :            :  */
      70                 :            : #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
      71                 :            : #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
      72                 :            : 
      73                 :            : #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
      74                 :            : /*
      75                 :            :  * How many seconds to wait for initial 4-way handshake to get completed after
      76                 :            :  * re-invocation of a persistent group on the GO when the client is expected
      77                 :            :  * to connect automatically (no user interaction).
      78                 :            :  */
      79                 :            : #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
      80                 :            : #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
      81                 :            : 
      82                 :            : #ifndef P2P_CONCURRENT_SEARCH_DELAY
      83                 :            : #define P2P_CONCURRENT_SEARCH_DELAY 500
      84                 :            : #endif /* P2P_CONCURRENT_SEARCH_DELAY */
      85                 :            : 
      86                 :            : #define P2P_MGMT_DEVICE_PREFIX          "p2p-dev-"
      87                 :            : 
      88                 :            : enum p2p_group_removal_reason {
      89                 :            :         P2P_GROUP_REMOVAL_UNKNOWN,
      90                 :            :         P2P_GROUP_REMOVAL_SILENT,
      91                 :            :         P2P_GROUP_REMOVAL_FORMATION_FAILED,
      92                 :            :         P2P_GROUP_REMOVAL_REQUESTED,
      93                 :            :         P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
      94                 :            :         P2P_GROUP_REMOVAL_UNAVAILABLE,
      95                 :            :         P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
      96                 :            :         P2P_GROUP_REMOVAL_PSK_FAILURE,
      97                 :            :         P2P_GROUP_REMOVAL_FREQ_CONFLICT
      98                 :            : };
      99                 :            : 
     100                 :            : 
     101                 :            : static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
     102                 :            : static struct wpa_supplicant *
     103                 :            : wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
     104                 :            :                          int go);
     105                 :            : static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
     106                 :            : static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
     107                 :            : static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
     108                 :            : static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
     109                 :            :                          const u8 *dev_addr, enum p2p_wps_method wps_method,
     110                 :            :                          int auto_join);
     111                 :            : static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
     112                 :            : static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
     113                 :            : static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
     114                 :            : static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
     115                 :            : static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
     116                 :            :                                              void *timeout_ctx);
     117                 :            : static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
     118                 :            : static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
     119                 :            :                                         int group_added);
     120                 :            : static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
     121                 :            : 
     122                 :            : 
     123                 :            : /*
     124                 :            :  * Get the number of concurrent channels that the HW can operate, but that are
     125                 :            :  * currently not in use by any of the wpa_supplicant interfaces.
     126                 :            :  */
     127                 :         40 : static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
     128                 :            : {
     129                 :            :         int *freqs;
     130                 :            :         int num, unused;
     131                 :            : 
     132                 :         40 :         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
     133         [ -  + ]:         40 :         if (!freqs)
     134                 :          0 :                 return -1;
     135                 :            : 
     136                 :         40 :         num = get_shared_radio_freqs(wpa_s, freqs,
     137                 :            :                                      wpa_s->num_multichan_concurrent);
     138                 :         40 :         os_free(freqs);
     139                 :            : 
     140                 :         40 :         unused = wpa_s->num_multichan_concurrent - num;
     141                 :         40 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
     142                 :         40 :         return unused;
     143                 :            : }
     144                 :            : 
     145                 :            : 
     146                 :            : /*
     147                 :            :  * Get the frequencies that are currently in use by one or more of the virtual
     148                 :            :  * interfaces, and that are also valid for P2P operation.
     149                 :            :  */
     150                 :         32 : static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
     151                 :            :                                      int *p2p_freqs, unsigned int len)
     152                 :            : {
     153                 :            :         int *freqs;
     154                 :            :         unsigned int num, i, j;
     155                 :            : 
     156                 :         32 :         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
     157         [ -  + ]:         32 :         if (!freqs)
     158                 :          0 :                 return -1;
     159                 :            : 
     160                 :         32 :         num = get_shared_radio_freqs(wpa_s, freqs,
     161                 :            :                                      wpa_s->num_multichan_concurrent);
     162                 :            : 
     163                 :         32 :         os_memset(p2p_freqs, 0, sizeof(int) * len);
     164                 :            : 
     165 [ +  + ][ +  - ]:         34 :         for (i = 0, j = 0; i < num && j < len; i++) {
     166         [ +  - ]:          2 :                 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
     167                 :          2 :                         p2p_freqs[j++] = freqs[i];
     168                 :            :         }
     169                 :            : 
     170                 :         32 :         os_free(freqs);
     171                 :            : 
     172                 :         32 :         dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
     173                 :            : 
     174                 :         32 :         return j;
     175                 :            : }
     176                 :            : 
     177                 :            : 
     178                 :         86 : static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
     179                 :            :                                              int freq)
     180                 :            : {
     181 [ +  - ][ -  + ]:         86 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
     182                 :         86 :                 return;
     183 [ -  + ][ #  # ]:         86 :         if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
     184   [ #  #  #  # ]:          0 :             freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
     185                 :          0 :             wpas_p2p_num_unused_channels(wpa_s) > 0) {
     186                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
     187                 :            :                            freq);
     188                 :          0 :                 freq = 0;
     189                 :            :         }
     190                 :         86 :         p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
     191                 :            : }
     192                 :            : 
     193                 :            : 
     194                 :        440 : static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
     195                 :            :                                       struct wpa_scan_results *scan_res)
     196                 :            : {
     197                 :            :         size_t i;
     198                 :            : 
     199 [ +  - ][ -  + ]:        440 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
     200                 :        440 :                 return;
     201                 :            : 
     202                 :        440 :         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
     203                 :        440 :                    (int) scan_res->num);
     204                 :            : 
     205         [ +  + ]:       2006 :         for (i = 0; i < scan_res->num; i++) {
     206                 :       1566 :                 struct wpa_scan_res *bss = scan_res->res[i];
     207                 :            :                 struct os_reltime time_tmp_age, entry_ts;
     208                 :            :                 const u8 *ies;
     209                 :            :                 size_t ies_len;
     210                 :            : 
     211                 :       1566 :                 time_tmp_age.sec = bss->age / 1000;
     212                 :       1566 :                 time_tmp_age.usec = (bss->age % 1000) * 1000;
     213                 :       1566 :                 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
     214                 :            : 
     215                 :       1566 :                 ies = (const u8 *) (bss + 1);
     216                 :       1566 :                 ies_len = bss->ie_len;
     217   [ +  +  +  + ]:       2651 :                 if (bss->beacon_ie_len > 0 &&
     218         [ +  + ]:       1353 :                     !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
     219                 :        268 :                     wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
     220                 :          2 :                         wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
     221                 :         12 :                                    MACSTR, MAC2STR(bss->bssid));
     222                 :          2 :                         ies = ies + ies_len;
     223                 :          2 :                         ies_len = bss->beacon_ie_len;
     224                 :            :                 }
     225                 :            : 
     226                 :            : 
     227         [ -  + ]:       1566 :                 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
     228                 :            :                                          bss->freq, &entry_ts, bss->level,
     229                 :            :                                          ies, ies_len) > 0)
     230                 :          0 :                         break;
     231                 :            :         }
     232                 :            : 
     233                 :        440 :         p2p_scan_res_handled(wpa_s->global->p2p);
     234                 :            : }
     235                 :            : 
     236                 :            : 
     237                 :        427 : static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
     238                 :            :                          unsigned int num_req_dev_types,
     239                 :            :                          const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
     240                 :            : {
     241                 :        427 :         struct wpa_supplicant *wpa_s = ctx;
     242                 :            :         struct wpa_supplicant *ifs;
     243                 :            :         struct wpa_driver_scan_params params;
     244                 :            :         int ret;
     245                 :            :         struct wpabuf *wps_ie, *ies;
     246                 :        427 :         int social_channels[] = { 2412, 2437, 2462, 0, 0 };
     247                 :            :         size_t ielen;
     248                 :            : 
     249 [ +  - ][ -  + ]:        427 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
     250                 :          0 :                 return -1;
     251                 :            : 
     252         [ +  + ]:        854 :         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
     253   [ -  +  #  # ]:        427 :                 if (ifs->sta_scan_pending &&
     254   [ #  #  #  # ]:          0 :                     (wpas_scan_scheduled(ifs) || ifs->scanning) &&
     255                 :          0 :                     wpas_p2p_in_progress(wpa_s) == 2) {
     256                 :          0 :                         wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
     257                 :            :                                    "pending station mode scan to be "
     258                 :          0 :                                    "completed on interface %s", ifs->ifname);
     259                 :          0 :                         wpa_s->global->p2p_cb_on_scan_complete = 1;
     260                 :          0 :                         wpa_supplicant_req_scan(ifs, 0, 0);
     261                 :          0 :                         return 1;
     262                 :            :                 }
     263                 :            :         }
     264                 :            : 
     265                 :        427 :         os_memset(&params, 0, sizeof(params));
     266                 :            : 
     267                 :            :         /* P2P Wildcard SSID */
     268                 :        427 :         params.num_ssids = 1;
     269                 :        427 :         params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
     270                 :        427 :         params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
     271                 :            : 
     272                 :        427 :         wpa_s->wps->dev.p2p = 1;
     273                 :        427 :         wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
     274                 :        427 :                                         wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
     275                 :            :                                         num_req_dev_types, req_dev_types);
     276         [ -  + ]:        427 :         if (wps_ie == NULL)
     277                 :          0 :                 return -1;
     278                 :            : 
     279                 :        427 :         ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
     280                 :        427 :         ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
     281         [ -  + ]:        427 :         if (ies == NULL) {
     282                 :          0 :                 wpabuf_free(wps_ie);
     283                 :          0 :                 return -1;
     284                 :            :         }
     285                 :        427 :         wpabuf_put_buf(ies, wps_ie);
     286                 :        427 :         wpabuf_free(wps_ie);
     287                 :            : 
     288                 :        427 :         p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
     289                 :            : 
     290                 :        427 :         params.p2p_probe = 1;
     291                 :        427 :         params.extra_ies = wpabuf_head(ies);
     292                 :        427 :         params.extra_ies_len = wpabuf_len(ies);
     293                 :            : 
     294   [ +  +  -  - ]:        427 :         switch (type) {
     295                 :            :         case P2P_SCAN_SOCIAL:
     296                 :        413 :                 params.freqs = social_channels;
     297                 :        413 :                 break;
     298                 :            :         case P2P_SCAN_FULL:
     299                 :         14 :                 break;
     300                 :            :         case P2P_SCAN_SOCIAL_PLUS_ONE:
     301                 :          0 :                 social_channels[3] = freq;
     302                 :          0 :                 params.freqs = social_channels;
     303                 :          0 :                 break;
     304                 :            :         }
     305                 :            : 
     306                 :        427 :         ret = wpa_drv_scan(wpa_s, &params);
     307                 :            : 
     308                 :        427 :         wpabuf_free(ies);
     309                 :            : 
     310         [ +  + ]:        427 :         if (ret) {
     311         [ +  - ]:         10 :                 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
     312 [ +  + ][ +  - ]:          5 :                         if (ifs->scanning ||
     313                 :          2 :                             ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
     314                 :          5 :                                 wpa_s->global->p2p_cb_on_scan_complete = 1;
     315                 :          5 :                                 ret = 1;
     316                 :          5 :                                 break;
     317                 :            :                         }
     318                 :            :                 }
     319                 :            :         } else {
     320                 :        422 :                 os_get_reltime(&wpa_s->scan_trigger_time);
     321                 :        422 :                 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
     322                 :        422 :                 wpa_s->own_scan_requested = 1;
     323                 :            :         }
     324                 :            : 
     325                 :        427 :         return ret;
     326                 :            : }
     327                 :            : 
     328                 :            : 
     329                 :         19 : static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
     330                 :            : {
     331   [ -  +  +  - ]:         19 :         switch (p2p_group_interface) {
     332                 :            :         case P2P_GROUP_INTERFACE_PENDING:
     333                 :          0 :                 return WPA_IF_P2P_GROUP;
     334                 :            :         case P2P_GROUP_INTERFACE_GO:
     335                 :         10 :                 return WPA_IF_P2P_GO;
     336                 :            :         case P2P_GROUP_INTERFACE_CLIENT:
     337                 :          9 :                 return WPA_IF_P2P_CLIENT;
     338                 :            :         }
     339                 :            : 
     340                 :         19 :         return WPA_IF_P2P_GROUP;
     341                 :            : }
     342                 :            : 
     343                 :            : 
     344                 :         25 : static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
     345                 :            :                                                   const u8 *ssid,
     346                 :            :                                                   size_t ssid_len, int *go)
     347                 :            : {
     348                 :            :         struct wpa_ssid *s;
     349                 :            : 
     350         [ +  + ]:         50 :         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
     351         [ +  + ]:         50 :                 for (s = wpa_s->conf->ssid; s; s = s->next) {
     352 [ -  + ][ #  # ]:         25 :                         if (s->disabled != 0 || !s->p2p_group ||
                 [ #  # ]
     353         [ #  # ]:          0 :                             s->ssid_len != ssid_len ||
     354                 :          0 :                             os_memcmp(ssid, s->ssid, ssid_len) != 0)
     355                 :         25 :                                 continue;
     356 [ #  # ][ #  # ]:          0 :                         if (s->mode == WPAS_MODE_P2P_GO &&
     357                 :          0 :                             s != wpa_s->current_ssid)
     358                 :          0 :                                 continue;
     359         [ #  # ]:          0 :                         if (go)
     360                 :          0 :                                 *go = s->mode == WPAS_MODE_P2P_GO;
     361                 :          0 :                         return wpa_s;
     362                 :            :                 }
     363                 :            :         }
     364                 :            : 
     365                 :         25 :         return NULL;
     366                 :            : }
     367                 :            : 
     368                 :            : 
     369                 :        123 : static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
     370                 :            :                                  enum p2p_group_removal_reason removal_reason)
     371                 :            : {
     372                 :            :         struct wpa_ssid *ssid;
     373                 :            :         char *gtype;
     374                 :            :         const char *reason;
     375                 :            : 
     376                 :        123 :         ssid = wpa_s->current_ssid;
     377         [ +  + ]:        123 :         if (ssid == NULL) {
     378                 :            :                 /*
     379                 :            :                  * The current SSID was not known, but there may still be a
     380                 :            :                  * pending P2P group interface waiting for provisioning or a
     381                 :            :                  * P2P group that is trying to reconnect.
     382                 :            :                  */
     383                 :          9 :                 ssid = wpa_s->conf->ssid;
     384         [ -  + ]:          9 :                 while (ssid) {
     385 [ #  # ][ #  # ]:          0 :                         if (ssid->p2p_group && ssid->disabled != 2)
     386                 :          0 :                                 break;
     387                 :          0 :                         ssid = ssid->next;
     388                 :            :                 }
     389 [ +  - ][ +  - ]:          9 :                 if (ssid == NULL &&
     390                 :          9 :                         wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
     391                 :            :                 {
     392                 :          9 :                         wpa_printf(MSG_ERROR, "P2P: P2P group interface "
     393                 :            :                                    "not found");
     394                 :          9 :                         return -1;
     395                 :            :                 }
     396                 :            :         }
     397         [ +  + ]:        114 :         if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
     398                 :         10 :                 gtype = "GO";
     399 [ +  + ][ +  - ]:        104 :         else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
     400         [ +  + ]:         95 :                  (ssid && ssid->mode == WPAS_MODE_INFRA)) {
     401                 :         59 :                 wpa_s->reassociate = 0;
     402                 :         59 :                 wpa_s->disconnected = 1;
     403                 :         59 :                 wpa_supplicant_deauthenticate(wpa_s,
     404                 :            :                                               WLAN_REASON_DEAUTH_LEAVING);
     405                 :         59 :                 gtype = "client";
     406                 :            :         } else
     407                 :         45 :                 gtype = "GO";
     408         [ -  + ]:        114 :         if (wpa_s->cross_connect_in_use) {
     409                 :          0 :                 wpa_s->cross_connect_in_use = 0;
     410                 :          0 :                 wpa_msg_global(wpa_s->parent, MSG_INFO,
     411                 :            :                                P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
     412                 :          0 :                                wpa_s->ifname, wpa_s->cross_connect_uplink);
     413                 :            :         }
     414   [ +  -  -  -  :        114 :         switch (removal_reason) {
             +  +  -  - ]
     415                 :            :         case P2P_GROUP_REMOVAL_REQUESTED:
     416                 :         73 :                 reason = " reason=REQUESTED";
     417                 :         73 :                 break;
     418                 :            :         case P2P_GROUP_REMOVAL_FORMATION_FAILED:
     419                 :          0 :                 reason = " reason=FORMATION_FAILED";
     420                 :          0 :                 break;
     421                 :            :         case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
     422                 :          0 :                 reason = " reason=IDLE";
     423                 :          0 :                 break;
     424                 :            :         case P2P_GROUP_REMOVAL_UNAVAILABLE:
     425                 :          0 :                 reason = " reason=UNAVAILABLE";
     426                 :          0 :                 break;
     427                 :            :         case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
     428                 :         39 :                 reason = " reason=GO_ENDING_SESSION";
     429                 :         39 :                 break;
     430                 :            :         case P2P_GROUP_REMOVAL_PSK_FAILURE:
     431                 :          2 :                 reason = " reason=PSK_FAILURE";
     432                 :          2 :                 break;
     433                 :            :         case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
     434                 :          0 :                 reason = " reason=FREQ_CONFLICT";
     435                 :          0 :                 break;
     436                 :            :         default:
     437                 :          0 :                 reason = "";
     438                 :          0 :                 break;
     439                 :            :         }
     440         [ +  - ]:        114 :         if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
     441                 :        114 :                 wpa_msg_global(wpa_s->parent, MSG_INFO,
     442                 :            :                                P2P_EVENT_GROUP_REMOVED "%s %s%s",
     443                 :        114 :                                wpa_s->ifname, gtype, reason);
     444                 :            :         }
     445                 :            : 
     446         [ -  + ]:        114 :         if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
     447                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
     448         [ +  + ]:        114 :         if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
     449                 :         57 :                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
     450         [ +  + ]:        114 :         if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
     451                 :        114 :                                  wpa_s->parent, NULL) > 0) {
     452                 :          1 :                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
     453                 :            :                            "timeout");
     454                 :          1 :                 wpa_s->p2p_in_provisioning = 0;
     455                 :            :         }
     456                 :            : 
     457                 :            :         /*
     458                 :            :          * Make sure wait for the first client does not remain active after the
     459                 :            :          * group has been removed.
     460                 :            :          */
     461                 :        114 :         wpa_s->global->p2p_go_wait_client.sec = 0;
     462                 :            : 
     463 [ +  - ][ +  - ]:        114 :         if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
     464                 :        114 :                 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
     465                 :            : 
     466         [ +  + ]:        114 :         if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
     467                 :            :                 struct wpa_global *global;
     468                 :            :                 char *ifname;
     469                 :            :                 enum wpa_driver_if_type type;
     470                 :         19 :                 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
     471                 :         19 :                         wpa_s->ifname);
     472                 :         19 :                 global = wpa_s->global;
     473                 :         19 :                 ifname = os_strdup(wpa_s->ifname);
     474                 :         19 :                 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
     475                 :         19 :                 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
     476                 :         19 :                 wpa_s = global->ifaces;
     477 [ +  - ][ +  - ]:         19 :                 if (wpa_s && ifname)
     478                 :         19 :                         wpa_drv_if_remove(wpa_s, type, ifname);
     479                 :         19 :                 os_free(ifname);
     480                 :         19 :                 return 1;
     481                 :            :         }
     482                 :            : 
     483         [ +  + ]:         95 :         if (!wpa_s->p2p_go_group_formation_completed) {
     484                 :          1 :                 wpa_s->global->p2p_group_formation = NULL;
     485                 :          1 :                 wpa_s->p2p_in_provisioning = 0;
     486                 :            :         }
     487                 :            : 
     488                 :         95 :         wpa_s->show_group_started = 0;
     489                 :         95 :         os_free(wpa_s->go_params);
     490                 :         95 :         wpa_s->go_params = NULL;
     491                 :            : 
     492                 :         95 :         wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
     493 [ +  - ][ -  + ]:         95 :         if (ssid && (ssid->p2p_group ||
                 [ #  # ]
     494         [ #  # ]:          0 :                      ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
     495                 :         95 :                      (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
     496                 :         95 :                 int id = ssid->id;
     497         [ +  + ]:         95 :                 if (ssid == wpa_s->current_ssid) {
     498                 :         45 :                         wpa_sm_set_config(wpa_s->wpa, NULL);
     499                 :         45 :                         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
     500                 :         45 :                         wpa_s->current_ssid = NULL;
     501                 :            :                 }
     502                 :            :                 /*
     503                 :            :                  * Networks objects created during any P2P activities are not
     504                 :            :                  * exposed out as they might/will confuse certain non-P2P aware
     505                 :            :                  * applications since these network objects won't behave like
     506                 :            :                  * regular ones.
     507                 :            :                  *
     508                 :            :                  * Likewise, we don't send out network removed signals for such
     509                 :            :                  * network objects.
     510                 :            :                  */
     511                 :         95 :                 wpa_config_remove_network(wpa_s->conf, id);
     512                 :         95 :                 wpa_supplicant_clear_status(wpa_s);
     513                 :         95 :                 wpa_supplicant_cancel_sched_scan(wpa_s);
     514                 :         95 :                 wpa_s->sta_scan_pending = 0;
     515                 :            :         } else {
     516                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
     517                 :            :                            "found");
     518                 :            :         }
     519         [ +  + ]:         95 :         if (wpa_s->ap_iface)
     520                 :         45 :                 wpa_supplicant_ap_deinit(wpa_s);
     521                 :            :         else
     522                 :         50 :                 wpa_drv_deinit_p2p_cli(wpa_s);
     523                 :            : 
     524                 :        123 :         return 0;
     525                 :            : }
     526                 :            : 
     527                 :            : 
     528                 :        160 : static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
     529                 :            :                                      u8 *go_dev_addr,
     530                 :            :                                      const u8 *ssid, size_t ssid_len)
     531                 :            : {
     532                 :            :         struct wpa_bss *bss;
     533                 :            :         const u8 *bssid;
     534                 :            :         struct wpabuf *p2p;
     535                 :            :         u8 group_capab;
     536                 :            :         const u8 *addr;
     537                 :            : 
     538         [ +  + ]:        160 :         if (wpa_s->go_params)
     539                 :        150 :                 bssid = wpa_s->go_params->peer_interface_addr;
     540                 :            :         else
     541                 :         10 :                 bssid = wpa_s->bssid;
     542                 :            : 
     543                 :        160 :         bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
     544         [ -  + ]:        160 :         if (bss == NULL) {
     545                 :            :                 u8 iface_addr[ETH_ALEN];
     546         [ #  # ]:          0 :                 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
     547                 :            :                                            iface_addr) == 0)
     548                 :          0 :                         bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
     549                 :            :         }
     550         [ -  + ]:        160 :         if (bss == NULL) {
     551                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
     552                 :            :                            "group is persistent - BSS " MACSTR " not found",
     553                 :          0 :                            MAC2STR(bssid));
     554                 :          0 :                 return 0;
     555                 :            :         }
     556                 :            : 
     557                 :        160 :         p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
     558         [ +  + ]:        160 :         if (p2p == NULL)
     559                 :          1 :                 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
     560                 :            :                                                          P2P_IE_VENDOR_TYPE);
     561         [ +  + ]:        160 :         if (p2p == NULL) {
     562                 :          1 :                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
     563                 :            :                            "group is persistent - BSS " MACSTR
     564                 :          6 :                            " did not include P2P IE", MAC2STR(bssid));
     565                 :          1 :                 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
     566                 :            :                             (u8 *) (bss + 1), bss->ie_len);
     567                 :          1 :                 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
     568                 :          1 :                             ((u8 *) bss + 1) + bss->ie_len,
     569                 :            :                             bss->beacon_ie_len);
     570                 :          1 :                 return 0;
     571                 :            :         }
     572                 :            : 
     573                 :        159 :         group_capab = p2p_get_group_capab(p2p);
     574                 :        159 :         addr = p2p_get_go_dev_addr(p2p);
     575                 :        159 :         wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
     576                 :            :                    "group_capab=0x%x", group_capab);
     577         [ +  - ]:        159 :         if (addr) {
     578                 :        159 :                 os_memcpy(go_dev_addr, addr, ETH_ALEN);
     579                 :        159 :                 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
     580                 :        954 :                            MAC2STR(addr));
     581                 :            :         } else
     582                 :          0 :                 os_memset(go_dev_addr, 0, ETH_ALEN);
     583                 :        159 :         wpabuf_free(p2p);
     584                 :            : 
     585                 :        159 :         wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
     586                 :            :                    "go_dev_addr=" MACSTR,
     587                 :       1908 :                    MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
     588                 :            : 
     589                 :        160 :         return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
     590                 :            : }
     591                 :            : 
     592                 :            : 
     593                 :         38 : static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
     594                 :            :                                            struct wpa_ssid *ssid,
     595                 :            :                                            const u8 *go_dev_addr)
     596                 :            : {
     597                 :            :         struct wpa_ssid *s;
     598                 :         38 :         int changed = 0;
     599                 :            : 
     600                 :         38 :         wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
     601                 :        228 :                    "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
     602         [ +  + ]:         63 :         for (s = wpa_s->conf->ssid; s; s = s->next) {
     603 [ +  + ][ +  + ]:         48 :                 if (s->disabled == 2 &&
     604         [ +  - ]:         23 :                     os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
     605         [ +  - ]:         23 :                     s->ssid_len == ssid->ssid_len &&
     606                 :         23 :                     os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
     607                 :         23 :                         break;
     608                 :            :         }
     609                 :            : 
     610         [ +  + ]:         38 :         if (s) {
     611                 :         23 :                 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
     612                 :            :                            "entry");
     613 [ +  + ][ -  + ]:         23 :                 if (ssid->passphrase && !s->passphrase)
     614                 :          0 :                         changed = 1;
     615 [ +  + ][ +  - ]:         23 :                 else if (ssid->passphrase && s->passphrase &&
                 [ -  + ]
     616                 :          7 :                          os_strcmp(ssid->passphrase, s->passphrase) != 0)
     617                 :         23 :                         changed = 1;
     618                 :            :         } else {
     619                 :         15 :                 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
     620                 :            :                            "entry");
     621                 :         15 :                 changed = 1;
     622                 :         15 :                 s = wpa_config_add_network(wpa_s->conf);
     623         [ -  + ]:         15 :                 if (s == NULL)
     624                 :          0 :                         return -1;
     625                 :            : 
     626                 :            :                 /*
     627                 :            :                  * Instead of network_added we emit persistent_group_added
     628                 :            :                  * notification. Also to keep the defense checks in
     629                 :            :                  * persistent_group obj registration method, we set the
     630                 :            :                  * relevant flags in s to designate it as a persistent group.
     631                 :            :                  */
     632                 :         15 :                 s->p2p_group = 1;
     633                 :         15 :                 s->p2p_persistent_group = 1;
     634                 :         15 :                 wpas_notify_persistent_group_added(wpa_s, s);
     635                 :         15 :                 wpa_config_set_network_defaults(s);
     636                 :            :         }
     637                 :            : 
     638                 :         38 :         s->p2p_group = 1;
     639                 :         38 :         s->p2p_persistent_group = 1;
     640                 :         38 :         s->disabled = 2;
     641                 :         38 :         s->bssid_set = 1;
     642                 :         38 :         os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
     643                 :         38 :         s->mode = ssid->mode;
     644                 :         38 :         s->auth_alg = WPA_AUTH_ALG_OPEN;
     645                 :         38 :         s->key_mgmt = WPA_KEY_MGMT_PSK;
     646                 :         38 :         s->proto = WPA_PROTO_RSN;
     647                 :         38 :         s->pairwise_cipher = WPA_CIPHER_CCMP;
     648                 :         38 :         s->export_keys = 1;
     649         [ +  + ]:         38 :         if (ssid->passphrase) {
     650                 :         13 :                 os_free(s->passphrase);
     651                 :         13 :                 s->passphrase = os_strdup(ssid->passphrase);
     652                 :            :         }
     653         [ +  - ]:         38 :         if (ssid->psk_set) {
     654                 :         38 :                 s->psk_set = 1;
     655                 :         38 :                 os_memcpy(s->psk, ssid->psk, 32);
     656                 :            :         }
     657 [ +  + ][ -  + ]:         38 :         if (s->passphrase && !s->psk_set)
     658                 :          0 :                 wpa_config_update_psk(s);
     659 [ +  + ][ -  + ]:         38 :         if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
     660                 :         15 :                 os_free(s->ssid);
     661                 :         15 :                 s->ssid = os_malloc(ssid->ssid_len);
     662                 :            :         }
     663         [ +  - ]:         38 :         if (s->ssid) {
     664                 :         38 :                 s->ssid_len = ssid->ssid_len;
     665                 :         38 :                 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
     666                 :            :         }
     667 [ +  + ][ +  + ]:         38 :         if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
     668                 :          2 :                 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
     669                 :          2 :                 wpa_s->global->add_psk = NULL;
     670                 :          2 :                 changed = 1;
     671                 :            :         }
     672                 :            : 
     673                 :            : #ifndef CONFIG_NO_CONFIG_WRITE
     674         [ +  + ]:         38 :         if (changed && wpa_s->conf->update_config &&
           [ -  +  #  # ]
     675                 :          0 :             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
     676                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
     677                 :            :         }
     678                 :            : #endif /* CONFIG_NO_CONFIG_WRITE */
     679                 :            : 
     680                 :         38 :         return s->id;
     681                 :            : }
     682                 :            : 
     683                 :            : 
     684                 :         57 : static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
     685                 :            :                                                  const u8 *addr)
     686                 :            : {
     687                 :            :         struct wpa_ssid *ssid, *s;
     688                 :            :         u8 *n;
     689                 :            :         size_t i;
     690                 :         57 :         int found = 0;
     691                 :            : 
     692                 :         57 :         ssid = wpa_s->current_ssid;
     693 [ +  - ][ +  - ]:         57 :         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
                 [ +  + ]
     694                 :         57 :             !ssid->p2p_persistent_group)
     695                 :         41 :                 return;
     696                 :            : 
     697         [ +  - ]:         24 :         for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
     698 [ +  + ][ -  + ]:         24 :                 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
     699                 :          8 :                         continue;
     700                 :            : 
     701 [ +  - ][ +  - ]:         16 :                 if (s->ssid_len == ssid->ssid_len &&
     702                 :         16 :                     os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
     703                 :         16 :                         break;
     704                 :            :         }
     705                 :            : 
     706         [ -  + ]:         16 :         if (s == NULL)
     707                 :          0 :                 return;
     708                 :            : 
     709 [ +  + ][ +  + ]:         19 :         for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
     710         [ +  + ]:         10 :                 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
     711                 :            :                               ETH_ALEN) != 0)
     712                 :          3 :                         continue;
     713                 :            : 
     714         [ +  - ]:          7 :                 if (i == s->num_p2p_clients - 1)
     715                 :          7 :                         return; /* already the most recent entry */
     716                 :            : 
     717                 :            :                 /* move the entry to mark it most recent */
     718                 :          0 :                 os_memmove(s->p2p_client_list + i * ETH_ALEN,
     719                 :            :                            s->p2p_client_list + (i + 1) * ETH_ALEN,
     720                 :            :                            (s->num_p2p_clients - i - 1) * ETH_ALEN);
     721                 :          0 :                 os_memcpy(s->p2p_client_list +
     722                 :            :                           (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
     723                 :          0 :                 found = 1;
     724                 :          0 :                 break;
     725                 :            :         }
     726                 :            : 
     727 [ +  - ][ +  - ]:          9 :         if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
     728                 :          9 :                 n = os_realloc_array(s->p2p_client_list,
     729                 :          9 :                                      s->num_p2p_clients + 1, ETH_ALEN);
     730         [ -  + ]:          9 :                 if (n == NULL)
     731                 :          0 :                         return;
     732                 :          9 :                 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
     733                 :          9 :                 s->p2p_client_list = n;
     734                 :          9 :                 s->num_p2p_clients++;
     735         [ #  # ]:          0 :         } else if (!found) {
     736                 :            :                 /* Not enough room for an additional entry - drop the oldest
     737                 :            :                  * entry */
     738                 :          0 :                 os_memmove(s->p2p_client_list,
     739                 :            :                            s->p2p_client_list + ETH_ALEN,
     740                 :            :                            (s->num_p2p_clients - 1) * ETH_ALEN);
     741                 :          0 :                 os_memcpy(s->p2p_client_list +
     742                 :            :                           (s->num_p2p_clients - 1) * ETH_ALEN,
     743                 :            :                           addr, ETH_ALEN);
     744                 :            :         }
     745                 :            : 
     746                 :            : #ifndef CONFIG_NO_CONFIG_WRITE
     747   [ -  +  #  # ]:          9 :         if (wpa_s->parent->conf->update_config &&
     748                 :          0 :             wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
     749                 :         57 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
     750                 :            : #endif /* CONFIG_NO_CONFIG_WRITE */
     751                 :            : }
     752                 :            : 
     753                 :            : 
     754                 :         82 : static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
     755                 :            :                                            int success)
     756                 :            : {
     757                 :            :         struct wpa_ssid *ssid;
     758                 :            :         const char *ssid_txt;
     759                 :            :         int client;
     760                 :            :         int persistent;
     761                 :            :         u8 go_dev_addr[ETH_ALEN];
     762                 :         82 :         int network_id = -1;
     763                 :            : 
     764                 :            :         /*
     765                 :            :          * This callback is likely called for the main interface. Update wpa_s
     766                 :            :          * to use the group interface if a new interface was created for the
     767                 :            :          * group.
     768                 :            :          */
     769         [ +  - ]:         82 :         if (wpa_s->global->p2p_group_formation)
     770                 :         82 :                 wpa_s = wpa_s->global->p2p_group_formation;
     771         [ +  + ]:         82 :         if (wpa_s->p2p_go_group_formation_completed) {
     772                 :         50 :                 wpa_s->global->p2p_group_formation = NULL;
     773                 :         50 :                 wpa_s->p2p_in_provisioning = 0;
     774                 :            :         }
     775                 :            : 
     776         [ -  + ]:         82 :         if (!success) {
     777                 :          0 :                 wpa_msg_global(wpa_s->parent, MSG_INFO,
     778                 :            :                                P2P_EVENT_GROUP_FORMATION_FAILURE);
     779                 :          0 :                 wpas_p2p_group_delete(wpa_s,
     780                 :            :                                       P2P_GROUP_REMOVAL_FORMATION_FAILED);
     781                 :         82 :                 return;
     782                 :            :         }
     783                 :            : 
     784                 :         82 :         wpa_msg_global(wpa_s->parent, MSG_INFO,
     785                 :            :                        P2P_EVENT_GROUP_FORMATION_SUCCESS);
     786                 :            : 
     787                 :         82 :         ssid = wpa_s->current_ssid;
     788 [ +  - ][ +  + ]:         82 :         if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
     789                 :         32 :                 ssid->mode = WPAS_MODE_P2P_GO;
     790                 :         32 :                 p2p_group_notif_formation_done(wpa_s->p2p_group);
     791                 :         32 :                 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
     792                 :            :         }
     793                 :            : 
     794                 :         82 :         persistent = 0;
     795         [ +  - ]:         82 :         if (ssid) {
     796                 :         82 :                 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
     797                 :         82 :                 client = ssid->mode == WPAS_MODE_INFRA;
     798         [ +  + ]:         82 :                 if (ssid->mode == WPAS_MODE_P2P_GO) {
     799                 :         32 :                         persistent = ssid->p2p_persistent_group;
     800                 :         32 :                         os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
     801                 :            :                                   ETH_ALEN);
     802                 :            :                 } else
     803                 :         50 :                         persistent = wpas_p2p_persistent_group(wpa_s,
     804                 :            :                                                                go_dev_addr,
     805                 :         50 :                                                                ssid->ssid,
     806                 :            :                                                                ssid->ssid_len);
     807                 :            :         } else {
     808                 :          0 :                 ssid_txt = "";
     809                 :          0 :                 client = wpa_s->p2p_group_interface ==
     810                 :            :                         P2P_GROUP_INTERFACE_CLIENT;
     811                 :          0 :                 os_memset(go_dev_addr, 0, ETH_ALEN);
     812                 :            :         }
     813                 :            : 
     814                 :         82 :         wpa_s->show_group_started = 0;
     815         [ +  + ]:         82 :         if (client) {
     816                 :            :                 /*
     817                 :            :                  * Indicate event only after successfully completed 4-way
     818                 :            :                  * handshake, i.e., when the interface is ready for data
     819                 :            :                  * packets.
     820                 :            :                  */
     821                 :         50 :                 wpa_s->show_group_started = 1;
     822 [ +  - ][ -  + ]:         32 :         } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
                 [ #  # ]
     823                 :            :                 char psk[65];
     824                 :          0 :                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
     825         [ #  # ]:          0 :                 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
     826                 :            :                                "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
     827                 :            :                                MACSTR "%s",
     828                 :          0 :                                wpa_s->ifname, ssid_txt, ssid->frequency, psk,
     829                 :          0 :                                MAC2STR(go_dev_addr),
     830                 :            :                                persistent ? " [PERSISTENT]" : "");
     831                 :          0 :                 wpas_p2p_cross_connect_setup(wpa_s);
     832                 :          0 :                 wpas_p2p_set_group_idle_timeout(wpa_s);
     833                 :            :         } else {
     834 [ +  + ][ +  - ]:         64 :                 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
                 [ +  - ]
     835                 :            :                                "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
     836                 :            :                                "go_dev_addr=" MACSTR "%s",
     837                 :         32 :                                wpa_s->ifname, ssid_txt,
     838                 :            :                                ssid ? ssid->frequency : 0,
     839         [ +  - ]:         32 :                                ssid && ssid->passphrase ? ssid->passphrase : "",
     840                 :        192 :                                MAC2STR(go_dev_addr),
     841                 :            :                                persistent ? " [PERSISTENT]" : "");
     842                 :         32 :                 wpas_p2p_cross_connect_setup(wpa_s);
     843                 :         32 :                 wpas_p2p_set_group_idle_timeout(wpa_s);
     844                 :            :         }
     845                 :            : 
     846         [ +  + ]:         82 :         if (persistent)
     847                 :         14 :                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
     848                 :            :                                                              ssid, go_dev_addr);
     849                 :            :         else {
     850                 :         68 :                 os_free(wpa_s->global->add_psk);
     851                 :         68 :                 wpa_s->global->add_psk = NULL;
     852                 :            :         }
     853 [ +  + ][ +  - ]:         82 :         if (network_id < 0 && ssid)
     854                 :         68 :                 network_id = ssid->id;
     855         [ +  + ]:         82 :         if (!client) {
     856                 :         32 :                 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
     857                 :         32 :                 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
     858                 :            :         }
     859                 :            : }
     860                 :            : 
     861                 :            : 
     862                 :        215 : static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
     863                 :            :                                            unsigned int freq,
     864                 :            :                                            const u8 *dst, const u8 *src,
     865                 :            :                                            const u8 *bssid,
     866                 :            :                                            const u8 *data, size_t data_len,
     867                 :            :                                            enum offchannel_send_action_result
     868                 :            :                                            result)
     869                 :            : {
     870                 :        215 :         enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
     871                 :            : 
     872 [ +  - ][ -  + ]:        215 :         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
     873                 :          0 :                 return;
     874                 :            : 
     875   [ +  +  -  - ]:        215 :         switch (result) {
     876                 :            :         case OFFCHANNEL_SEND_ACTION_SUCCESS:
     877                 :        192 :                 res = P2P_SEND_ACTION_SUCCESS;
     878                 :        192 :                 break;
     879                 :            :         case OFFCHANNEL_SEND_ACTION_NO_ACK:
     880                 :         23 :                 res = P2P_SEND_ACTION_NO_ACK;
     881                 :         23 :                 break;
     882                 :            :         case OFFCHANNEL_SEND_ACTION_FAILED:
     883                 :          0 :                 res = P2P_SEND_ACTION_FAILED;
     884                 :          0 :                 break;
     885                 :            :         }
     886                 :            : 
     887                 :        215 :         p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
     888                 :            : 
     889 [ +  + ][ -  + ]:        215 :         if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
     890         [ #  # ]:          0 :             wpa_s->pending_pd_before_join &&
     891         [ #  # ]:          0 :             (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
     892         [ #  # ]:          0 :              os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
     893                 :            :             wpa_s->p2p_fallback_to_go_neg) {
     894                 :          0 :                 wpa_s->pending_pd_before_join = 0;
     895                 :          0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
     896                 :            :                         "during p2p_connect-auto");
     897                 :          0 :                 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
     898                 :        215 :                 return;
     899                 :            :         }
     900                 :            : }
     901                 :            : 
     902                 :            : 
     903                 :        215 : static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
     904                 :            :                             const u8 *src, const u8 *bssid, const u8 *buf,
     905                 :            :                             size_t len, unsigned int wait_time)
     906                 :            : {
     907                 :        215 :         struct wpa_supplicant *wpa_s = ctx;
     908                 :        215 :         return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
     909                 :            :                                       wait_time,
     910                 :            :                                       wpas_p2p_send_action_tx_status, 1);
     911                 :            : }
     912                 :            : 
     913                 :            : 
     914                 :        112 : static void wpas_send_action_done(void *ctx)
     915                 :            : {
     916                 :        112 :         struct wpa_supplicant *wpa_s = ctx;
     917                 :        112 :         offchannel_send_action_done(wpa_s);
     918                 :        112 : }
     919                 :            : 
     920                 :            : 
     921                 :        105 : static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
     922                 :            :                                     struct p2p_go_neg_results *params)
     923                 :            : {
     924         [ +  - ]:        105 :         if (wpa_s->go_params == NULL) {
     925                 :        105 :                 wpa_s->go_params = os_malloc(sizeof(*params));
     926         [ -  + ]:        105 :                 if (wpa_s->go_params == NULL)
     927                 :          0 :                         return -1;
     928                 :            :         }
     929                 :        105 :         os_memcpy(wpa_s->go_params, params, sizeof(*params));
     930                 :        105 :         return 0;
     931                 :            : }
     932                 :            : 
     933                 :            : 
     934                 :         50 : static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
     935                 :            :                                     struct p2p_go_neg_results *res)
     936                 :            : {
     937                 :         50 :         wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
     938                 :        300 :                    MAC2STR(res->peer_interface_addr));
     939                 :         50 :         wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
     940                 :         50 :                           res->ssid, res->ssid_len);
     941                 :         50 :         wpa_supplicant_ap_deinit(wpa_s);
     942                 :         50 :         wpas_copy_go_neg_results(wpa_s, res);
     943         [ +  + ]:         50 :         if (res->wps_method == WPS_PBC)
     944                 :          3 :                 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
     945                 :            :         else {
     946                 :         47 :                 u16 dev_pw_id = DEV_PW_DEFAULT;
     947         [ +  + ]:         47 :                 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
     948                 :         27 :                         dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
     949                 :         47 :                 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
     950                 :         47 :                                    wpa_s->p2p_pin, 1, dev_pw_id);
     951                 :            :         }
     952                 :         50 : }
     953                 :            : 
     954                 :            : 
     955                 :          8 : static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
     956                 :            :                                   struct wpa_ssid *ssid)
     957                 :            : {
     958                 :            :         struct wpa_ssid *persistent;
     959                 :            :         struct psk_list_entry *psk;
     960                 :            :         struct hostapd_data *hapd;
     961                 :            : 
     962         [ -  + ]:          8 :         if (!wpa_s->ap_iface)
     963                 :          0 :                 return;
     964                 :            : 
     965                 :          8 :         persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
     966                 :            :                                              ssid->ssid_len);
     967         [ -  + ]:          8 :         if (persistent == NULL)
     968                 :          0 :                 return;
     969                 :            : 
     970                 :          8 :         hapd = wpa_s->ap_iface->bss[0];
     971                 :            : 
     972         [ -  + ]:          8 :         dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
     973                 :            :                          list) {
     974                 :            :                 struct hostapd_wpa_psk *hpsk;
     975                 :            : 
     976                 :          0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
     977                 :            :                         MACSTR " psk=%d",
     978                 :            :                         MAC2STR(psk->addr), psk->p2p);
     979                 :          0 :                 hpsk = os_zalloc(sizeof(*hpsk));
     980         [ #  # ]:          0 :                 if (hpsk == NULL)
     981                 :          0 :                         break;
     982                 :          0 :                 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
     983         [ #  # ]:          0 :                 if (psk->p2p)
     984                 :          0 :                         os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
     985                 :            :                 else
     986                 :          0 :                         os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
     987                 :          0 :                 hpsk->next = hapd->conf->ssid.wpa_psk;
     988                 :          0 :                 hapd->conf->ssid.wpa_psk = hpsk;
     989                 :            :         }
     990                 :            : }
     991                 :            : 
     992                 :            : 
     993                 :         55 : static void p2p_go_configured(void *ctx, void *data)
     994                 :            : {
     995                 :         55 :         struct wpa_supplicant *wpa_s = ctx;
     996                 :         55 :         struct p2p_go_neg_results *params = data;
     997                 :            :         struct wpa_ssid *ssid;
     998                 :         55 :         int network_id = -1;
     999                 :            : 
    1000                 :         55 :         ssid = wpa_s->current_ssid;
    1001 [ +  - ][ +  + ]:         55 :         if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
    1002                 :         23 :                 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
    1003         [ +  + ]:         23 :                 if (wpa_s->global->p2p_group_formation == wpa_s)
    1004                 :          3 :                         wpa_s->global->p2p_group_formation = NULL;
    1005         [ +  - ]:         23 :                 if (os_strlen(params->passphrase) > 0) {
    1006         [ +  + ]:         23 :                         wpa_msg_global(wpa_s->parent, MSG_INFO,
    1007                 :            :                                        P2P_EVENT_GROUP_STARTED
    1008                 :            :                                        "%s GO ssid=\"%s\" freq=%d "
    1009                 :            :                                        "passphrase=\"%s\" go_dev_addr=" MACSTR
    1010                 :         23 :                                        "%s", wpa_s->ifname,
    1011                 :         23 :                                        wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
    1012                 :         23 :                                        ssid->frequency, params->passphrase,
    1013                 :        138 :                                        MAC2STR(wpa_s->global->p2p_dev_addr),
    1014                 :         23 :                                        params->persistent_group ?
    1015                 :            :                                        " [PERSISTENT]" : "");
    1016                 :            :                 } else {
    1017                 :            :                         char psk[65];
    1018                 :          0 :                         wpa_snprintf_hex(psk, sizeof(psk), params->psk,
    1019                 :            :                                          sizeof(params->psk));
    1020         [ #  # ]:          0 :                         wpa_msg_global(wpa_s->parent, MSG_INFO,
    1021                 :            :                                        P2P_EVENT_GROUP_STARTED
    1022                 :            :                                        "%s GO ssid=\"%s\" freq=%d psk=%s "
    1023                 :            :                                        "go_dev_addr=" MACSTR "%s",
    1024                 :          0 :                                        wpa_s->ifname,
    1025                 :          0 :                                        wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
    1026                 :            :                                        ssid->frequency, psk,
    1027                 :          0 :                                        MAC2STR(wpa_s->global->p2p_dev_addr),
    1028                 :          0 :                                        params->persistent_group ?
    1029                 :            :                                        " [PERSISTENT]" : "");
    1030                 :            :                 }
    1031                 :            : 
    1032                 :         23 :                 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
    1033         [ +  + ]:         23 :                 if (params->persistent_group) {
    1034                 :          8 :                         network_id = wpas_p2p_store_persistent_group(
    1035                 :            :                                 wpa_s->parent, ssid,
    1036                 :          8 :                                 wpa_s->global->p2p_dev_addr);
    1037                 :          8 :                         wpas_p2p_add_psk_list(wpa_s, ssid);
    1038                 :            :                 }
    1039         [ +  + ]:         23 :                 if (network_id < 0)
    1040                 :         15 :                         network_id = ssid->id;
    1041                 :         23 :                 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
    1042                 :         23 :                 wpas_p2p_cross_connect_setup(wpa_s);
    1043                 :         23 :                 wpas_p2p_set_group_idle_timeout(wpa_s);
    1044                 :            : 
    1045         [ +  + ]:         23 :                 if (wpa_s->p2p_first_connection_timeout) {
    1046                 :          7 :                         wpa_dbg(wpa_s, MSG_DEBUG,
    1047                 :            :                                 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
    1048                 :            :                                 wpa_s->p2p_first_connection_timeout);
    1049                 :          7 :                         wpa_s->p2p_go_group_formation_completed = 0;
    1050                 :          7 :                         wpa_s->global->p2p_group_formation = wpa_s;
    1051                 :          7 :                         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    1052                 :          7 :                                              wpa_s->parent, NULL);
    1053                 :          7 :                         eloop_register_timeout(
    1054                 :          7 :                                 wpa_s->p2p_first_connection_timeout, 0,
    1055                 :            :                                 wpas_p2p_group_formation_timeout,
    1056                 :          7 :                                 wpa_s->parent, NULL);
    1057                 :            :                 }
    1058                 :            : 
    1059                 :         23 :                 return;
    1060                 :            :         }
    1061                 :            : 
    1062                 :         32 :         wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
    1063         [ -  + ]:         32 :         if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
    1064                 :         32 :                                               params->peer_interface_addr)) {
    1065                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
    1066                 :            :                            "filtering");
    1067                 :          0 :                 return;
    1068                 :            :         }
    1069         [ +  + ]:         32 :         if (params->wps_method == WPS_PBC)
    1070                 :          3 :                 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
    1071                 :          3 :                                           params->peer_device_addr);
    1072         [ +  - ]:         29 :         else if (wpa_s->p2p_pin[0])
    1073                 :         29 :                 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
    1074                 :         29 :                                           wpa_s->p2p_pin, NULL, 0, 0);
    1075                 :         32 :         os_free(wpa_s->go_params);
    1076                 :         55 :         wpa_s->go_params = NULL;
    1077                 :            : }
    1078                 :            : 
    1079                 :            : 
    1080                 :         55 : static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
    1081                 :            :                               struct p2p_go_neg_results *params,
    1082                 :            :                               int group_formation)
    1083                 :            : {
    1084                 :            :         struct wpa_ssid *ssid;
    1085                 :            : 
    1086                 :         55 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
    1087         [ -  + ]:         55 :         if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
    1088                 :          0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
    1089                 :            :                         "results");
    1090                 :          0 :                 return;
    1091                 :            :         }
    1092                 :            : 
    1093                 :         55 :         ssid = wpa_config_add_network(wpa_s->conf);
    1094         [ -  + ]:         55 :         if (ssid == NULL) {
    1095                 :          0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
    1096                 :          0 :                 return;
    1097                 :            :         }
    1098                 :            : 
    1099                 :         55 :         wpa_s->show_group_started = 0;
    1100                 :            : 
    1101                 :         55 :         wpa_config_set_network_defaults(ssid);
    1102                 :         55 :         ssid->temporary = 1;
    1103                 :         55 :         ssid->p2p_group = 1;
    1104                 :         55 :         ssid->p2p_persistent_group = params->persistent_group;
    1105         [ +  + ]:         55 :         ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
    1106                 :            :                 WPAS_MODE_P2P_GO;
    1107                 :         55 :         ssid->frequency = params->freq;
    1108                 :         55 :         ssid->ht40 = params->ht40;
    1109                 :         55 :         ssid->vht = params->vht;
    1110                 :         55 :         ssid->ssid = os_zalloc(params->ssid_len + 1);
    1111         [ +  - ]:         55 :         if (ssid->ssid) {
    1112                 :         55 :                 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
    1113                 :         55 :                 ssid->ssid_len = params->ssid_len;
    1114                 :            :         }
    1115                 :         55 :         ssid->auth_alg = WPA_AUTH_ALG_OPEN;
    1116                 :         55 :         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
    1117                 :         55 :         ssid->proto = WPA_PROTO_RSN;
    1118                 :         55 :         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
    1119         [ +  - ]:         55 :         if (os_strlen(params->passphrase) > 0) {
    1120                 :         55 :                 ssid->passphrase = os_strdup(params->passphrase);
    1121         [ -  + ]:         55 :                 if (ssid->passphrase == NULL) {
    1122                 :          0 :                         wpa_msg_global(wpa_s, MSG_ERROR,
    1123                 :            :                                        "P2P: Failed to copy passphrase for GO");
    1124                 :          0 :                         wpa_config_remove_network(wpa_s->conf, ssid->id);
    1125                 :          0 :                         return;
    1126                 :            :                 }
    1127                 :            :         } else
    1128                 :          0 :                 ssid->passphrase = NULL;
    1129                 :         55 :         ssid->psk_set = params->psk_set;
    1130         [ +  + ]:         55 :         if (ssid->psk_set)
    1131                 :          8 :                 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
    1132         [ +  - ]:         47 :         else if (ssid->passphrase)
    1133                 :         47 :                 wpa_config_update_psk(ssid);
    1134                 :         55 :         ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
    1135                 :            : 
    1136                 :         55 :         wpa_s->ap_configured_cb = p2p_go_configured;
    1137                 :         55 :         wpa_s->ap_configured_cb_ctx = wpa_s;
    1138                 :         55 :         wpa_s->ap_configured_cb_data = wpa_s->go_params;
    1139                 :         55 :         wpa_s->connect_without_scan = ssid;
    1140                 :         55 :         wpa_s->reassociate = 1;
    1141                 :         55 :         wpa_s->disconnected = 0;
    1142                 :         55 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
    1143                 :            :                 "start GO)");
    1144                 :         55 :         wpa_supplicant_req_scan(wpa_s, 0, 0);
    1145                 :            : }
    1146                 :            : 
    1147                 :            : 
    1148                 :         19 : static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
    1149                 :            :                                   const struct wpa_supplicant *src)
    1150                 :            : {
    1151                 :            :         struct wpa_config *d;
    1152                 :            :         const struct wpa_config *s;
    1153                 :            : 
    1154                 :         19 :         d = dst->conf;
    1155                 :         19 :         s = src->conf;
    1156                 :            : 
    1157                 :            : #define C(n) if (s->n) d->n = os_strdup(s->n)
    1158         [ +  - ]:         19 :         C(device_name);
    1159         [ -  + ]:         19 :         C(manufacturer);
    1160         [ -  + ]:         19 :         C(model_name);
    1161         [ -  + ]:         19 :         C(model_number);
    1162         [ -  + ]:         19 :         C(serial_number);
    1163         [ -  + ]:         19 :         C(config_methods);
    1164                 :            : #undef C
    1165                 :            : 
    1166                 :         19 :         os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
    1167                 :         19 :         os_memcpy(d->sec_device_type, s->sec_device_type,
    1168                 :            :                   sizeof(d->sec_device_type));
    1169                 :         19 :         d->num_sec_device_types = s->num_sec_device_types;
    1170                 :            : 
    1171                 :         19 :         d->p2p_group_idle = s->p2p_group_idle;
    1172                 :         19 :         d->p2p_intra_bss = s->p2p_intra_bss;
    1173                 :         19 :         d->persistent_reconnect = s->persistent_reconnect;
    1174                 :         19 :         d->max_num_sta = s->max_num_sta;
    1175                 :         19 :         d->pbc_in_m1 = s->pbc_in_m1;
    1176                 :         19 :         d->ignore_old_scan_res = s->ignore_old_scan_res;
    1177                 :         19 :         d->beacon_int = s->beacon_int;
    1178                 :         19 :         d->disassoc_low_ack = s->disassoc_low_ack;
    1179                 :         19 :         d->disable_scan_offload = s->disable_scan_offload;
    1180                 :         19 : }
    1181                 :            : 
    1182                 :            : 
    1183                 :         19 : static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
    1184                 :            :                                       char *ifname, size_t len)
    1185                 :            : {
    1186                 :         19 :         char *ifname_ptr = wpa_s->ifname;
    1187                 :            : 
    1188         [ -  + ]:         19 :         if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
    1189                 :            :                        os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
    1190                 :          0 :                 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
    1191                 :            :         }
    1192                 :            : 
    1193                 :         19 :         os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
    1194 [ -  + ][ #  # ]:         19 :         if (os_strlen(ifname) >= IFNAMSIZ &&
    1195                 :          0 :             os_strlen(wpa_s->ifname) < IFNAMSIZ) {
    1196                 :            :                 /* Try to avoid going over the IFNAMSIZ length limit */
    1197                 :          0 :                 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
    1198                 :            :         }
    1199                 :         19 : }
    1200                 :            : 
    1201                 :            : 
    1202                 :         19 : static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
    1203                 :            :                                         enum wpa_driver_if_type type)
    1204                 :            : {
    1205                 :            :         char ifname[120], force_ifname[120];
    1206                 :            : 
    1207         [ -  + ]:         19 :         if (wpa_s->pending_interface_name[0]) {
    1208                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
    1209                 :            :                            "- skip creation of a new one");
    1210         [ #  # ]:          0 :                 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
    1211                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
    1212                 :            :                                    "unknown?! ifname='%s'",
    1213                 :          0 :                                    wpa_s->pending_interface_name);
    1214                 :          0 :                         return -1;
    1215                 :            :                 }
    1216                 :          0 :                 return 0;
    1217                 :            :         }
    1218                 :            : 
    1219                 :         19 :         wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
    1220                 :         19 :         force_ifname[0] = '\0';
    1221                 :            : 
    1222                 :         19 :         wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
    1223                 :            :                    ifname);
    1224                 :         19 :         wpa_s->p2p_group_idx++;
    1225                 :            : 
    1226                 :         19 :         wpa_s->pending_interface_type = type;
    1227         [ -  + ]:         19 :         if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
    1228                 :         19 :                            wpa_s->pending_interface_addr, NULL) < 0) {
    1229                 :          0 :                 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
    1230                 :            :                            "interface");
    1231                 :          0 :                 return -1;
    1232                 :            :         }
    1233                 :            : 
    1234         [ -  + ]:         19 :         if (force_ifname[0]) {
    1235                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
    1236                 :            :                            force_ifname);
    1237                 :          0 :                 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
    1238                 :            :                            sizeof(wpa_s->pending_interface_name));
    1239                 :            :         } else
    1240                 :         19 :                 os_strlcpy(wpa_s->pending_interface_name, ifname,
    1241                 :            :                            sizeof(wpa_s->pending_interface_name));
    1242                 :         19 :         wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
    1243                 :         19 :                    MACSTR, wpa_s->pending_interface_name,
    1244                 :        114 :                    MAC2STR(wpa_s->pending_interface_addr));
    1245                 :            : 
    1246                 :         19 :         return 0;
    1247                 :            : }
    1248                 :            : 
    1249                 :            : 
    1250                 :        702 : static void wpas_p2p_remove_pending_group_interface(
    1251                 :            :         struct wpa_supplicant *wpa_s)
    1252                 :            : {
    1253   [ -  +  #  # ]:        702 :         if (!wpa_s->pending_interface_name[0] ||
    1254                 :          0 :             is_zero_ether_addr(wpa_s->pending_interface_addr))
    1255                 :        702 :                 return; /* No pending virtual interface */
    1256                 :            : 
    1257                 :          0 :         wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
    1258                 :          0 :                    wpa_s->pending_interface_name);
    1259                 :          0 :         wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
    1260                 :          0 :                           wpa_s->pending_interface_name);
    1261                 :          0 :         os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
    1262                 :          0 :         wpa_s->pending_interface_name[0] = '\0';
    1263                 :            : }
    1264                 :            : 
    1265                 :            : 
    1266                 :            : static struct wpa_supplicant *
    1267                 :         19 : wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
    1268                 :            : {
    1269                 :            :         struct wpa_interface iface;
    1270                 :            :         struct wpa_supplicant *group_wpa_s;
    1271                 :            : 
    1272         [ -  + ]:         19 :         if (!wpa_s->pending_interface_name[0]) {
    1273                 :          0 :                 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
    1274         [ #  # ]:          0 :                 if (!wpas_p2p_create_iface(wpa_s))
    1275                 :          0 :                         return NULL;
    1276                 :            :                 /*
    1277                 :            :                  * Something has forced us to remove the pending interface; try
    1278                 :            :                  * to create a new one and hope for the best that we will get
    1279                 :            :                  * the same local address.
    1280                 :            :                  */
    1281 [ #  # ][ #  # ]:          0 :                 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
    1282                 :            :                                                  WPA_IF_P2P_CLIENT) < 0)
    1283                 :          0 :                         return NULL;
    1284                 :            :         }
    1285                 :            : 
    1286                 :         19 :         os_memset(&iface, 0, sizeof(iface));
    1287                 :         19 :         iface.ifname = wpa_s->pending_interface_name;
    1288                 :         19 :         iface.driver = wpa_s->driver->name;
    1289 [ -  + ][ #  # ]:         19 :         if (wpa_s->conf->ctrl_interface == NULL &&
    1290         [ #  # ]:          0 :             wpa_s->parent != wpa_s &&
    1291         [ #  # ]:          0 :             wpa_s->p2p_mgmt &&
    1292                 :          0 :             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
    1293                 :          0 :                 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
    1294                 :            :         else
    1295                 :         19 :                 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
    1296                 :         19 :         iface.driver_param = wpa_s->conf->driver_param;
    1297                 :         19 :         group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
    1298         [ -  + ]:         19 :         if (group_wpa_s == NULL) {
    1299                 :          0 :                 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
    1300                 :            :                            "wpa_supplicant interface");
    1301                 :          0 :                 return NULL;
    1302                 :            :         }
    1303                 :         19 :         wpa_s->pending_interface_name[0] = '\0';
    1304                 :         19 :         group_wpa_s->parent = wpa_s;
    1305         [ +  + ]:         19 :         group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
    1306                 :            :                 P2P_GROUP_INTERFACE_CLIENT;
    1307                 :         19 :         wpa_s->global->p2p_group_formation = group_wpa_s;
    1308                 :            : 
    1309                 :         19 :         wpas_p2p_clone_config(group_wpa_s, wpa_s);
    1310                 :            : 
    1311                 :         19 :         return group_wpa_s;
    1312                 :            : }
    1313                 :            : 
    1314                 :            : 
    1315                 :          0 : static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
    1316                 :            :                                              void *timeout_ctx)
    1317                 :            : {
    1318                 :          0 :         struct wpa_supplicant *wpa_s = eloop_ctx;
    1319                 :          0 :         wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
    1320                 :          0 :         wpas_p2p_group_formation_failed(wpa_s);
    1321                 :          0 : }
    1322                 :            : 
    1323                 :            : 
    1324                 :          0 : void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
    1325                 :            : {
    1326                 :          0 :         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    1327                 :          0 :                              wpa_s->parent, NULL);
    1328         [ #  # ]:          0 :         if (wpa_s->global->p2p)
    1329                 :          0 :                 p2p_group_formation_failed(wpa_s->global->p2p);
    1330                 :          0 :         wpas_group_formation_completed(wpa_s, 0);
    1331                 :          0 : }
    1332                 :            : 
    1333                 :            : 
    1334                 :          0 : void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
    1335                 :            : {
    1336         [ #  # ]:          0 :         if (wpa_s->global->p2p_group_formation != wpa_s)
    1337                 :          0 :                 return;
    1338                 :            :         /* Speed up group formation timeout since this cannot succeed */
    1339                 :          0 :         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    1340                 :          0 :                              wpa_s->parent, NULL);
    1341                 :          0 :         eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
    1342                 :          0 :                                wpa_s->parent, NULL);
    1343                 :            : }
    1344                 :            : 
    1345                 :            : 
    1346                 :         69 : static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
    1347                 :            : {
    1348                 :         69 :         struct wpa_supplicant *wpa_s = ctx;
    1349                 :            : 
    1350 [ +  + ][ -  + ]:         69 :         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
    1351                 :         26 :                 wpa_drv_cancel_remain_on_channel(wpa_s);
    1352                 :         26 :                 wpa_s->off_channel_freq = 0;
    1353                 :         26 :                 wpa_s->roc_waiting_drv_freq = 0;
    1354                 :            :         }
    1355                 :            : 
    1356         [ +  + ]:         69 :         if (res->status) {
    1357                 :          5 :                 wpa_msg_global(wpa_s, MSG_INFO,
    1358                 :            :                                P2P_EVENT_GO_NEG_FAILURE "status=%d",
    1359                 :            :                                res->status);
    1360                 :          5 :                 wpas_notify_p2p_go_neg_completed(wpa_s, res);
    1361                 :          5 :                 wpas_p2p_remove_pending_group_interface(wpa_s);
    1362                 :          5 :                 return;
    1363                 :            :         }
    1364                 :            : 
    1365         [ -  + ]:         64 :         if (wpa_s->p2p_go_ht40)
    1366                 :          0 :                 res->ht40 = 1;
    1367         [ -  + ]:         64 :         if (wpa_s->p2p_go_vht)
    1368                 :          0 :                 res->vht = 1;
    1369                 :            : 
    1370         [ +  + ]:         64 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
    1371                 :            :                        "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
    1372                 :            :                        " wps_method=%s",
    1373                 :         64 :                        res->role_go ? "GO" : "client", res->freq, res->ht40,
    1374                 :        384 :                        MAC2STR(res->peer_device_addr),
    1375                 :        384 :                        MAC2STR(res->peer_interface_addr),
    1376                 :            :                        p2p_wps_method_text(res->wps_method));
    1377                 :         64 :         wpas_notify_p2p_go_neg_completed(wpa_s, res);
    1378                 :            : 
    1379 [ +  + ][ -  + ]:         64 :         if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
    1380                 :            :                 struct wpa_ssid *ssid;
    1381                 :          0 :                 ssid = wpa_config_get_network(wpa_s->conf,
    1382                 :            :                                               wpa_s->p2p_persistent_id);
    1383 [ #  # ][ #  # ]:          0 :                 if (ssid && ssid->disabled == 2 &&
                 [ #  # ]
    1384         [ #  # ]:          0 :                     ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
    1385                 :          0 :                         size_t len = os_strlen(ssid->passphrase);
    1386                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
    1387                 :            :                                    "on requested persistent group");
    1388                 :          0 :                         os_memcpy(res->passphrase, ssid->passphrase, len);
    1389                 :          0 :                         res->passphrase[len] = '\0';
    1390                 :            :                 }
    1391                 :            :         }
    1392                 :            : 
    1393         [ +  + ]:         64 :         if (wpa_s->create_p2p_iface) {
    1394                 :         13 :                 struct wpa_supplicant *group_wpa_s =
    1395                 :         13 :                         wpas_p2p_init_group_interface(wpa_s, res->role_go);
    1396         [ -  + ]:         13 :                 if (group_wpa_s == NULL) {
    1397                 :          0 :                         wpas_p2p_remove_pending_group_interface(wpa_s);
    1398                 :          0 :                         return;
    1399                 :            :                 }
    1400         [ +  - ]:         13 :                 if (group_wpa_s != wpa_s) {
    1401                 :         13 :                         os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
    1402                 :            :                                   sizeof(group_wpa_s->p2p_pin));
    1403                 :         13 :                         group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
    1404                 :            :                 }
    1405                 :         13 :                 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
    1406                 :         13 :                 wpa_s->pending_interface_name[0] = '\0';
    1407                 :         13 :                 group_wpa_s->p2p_in_provisioning = 1;
    1408                 :            : 
    1409         [ +  + ]:         13 :                 if (res->role_go)
    1410                 :          7 :                         wpas_start_wps_go(group_wpa_s, res, 1);
    1411                 :            :                 else
    1412                 :          6 :                         wpas_start_wps_enrollee(group_wpa_s, res);
    1413                 :            :         } else {
    1414                 :         51 :                 wpa_s->p2p_in_provisioning = 1;
    1415                 :         51 :                 wpa_s->global->p2p_group_formation = wpa_s;
    1416                 :            : 
    1417         [ +  + ]:         51 :                 if (res->role_go)
    1418                 :         25 :                         wpas_start_wps_go(wpa_s, res, 1);
    1419                 :            :                 else
    1420                 :         26 :                         wpas_start_wps_enrollee(ctx, res);
    1421                 :            :         }
    1422                 :            : 
    1423                 :         64 :         wpa_s->p2p_long_listen = 0;
    1424                 :         64 :         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
    1425                 :            : 
    1426                 :         64 :         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
    1427                 :         69 :         eloop_register_timeout(15 + res->peer_config_timeout / 100,
    1428                 :         64 :                                (res->peer_config_timeout % 100) * 10000,
    1429                 :            :                                wpas_p2p_group_formation_timeout, wpa_s, NULL);
    1430                 :            : }
    1431                 :            : 
    1432                 :            : 
    1433                 :          5 : static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
    1434                 :            : {
    1435                 :          5 :         struct wpa_supplicant *wpa_s = ctx;
    1436                 :          5 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
    1437                 :         30 :                        " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
    1438                 :            : 
    1439                 :          5 :         wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
    1440                 :          5 : }
    1441                 :            : 
    1442                 :            : 
    1443                 :        132 : static void wpas_dev_found(void *ctx, const u8 *addr,
    1444                 :            :                            const struct p2p_peer_info *info,
    1445                 :            :                            int new_device)
    1446                 :            : {
    1447                 :            : #ifndef CONFIG_NO_STDOUT_DEBUG
    1448                 :        132 :         struct wpa_supplicant *wpa_s = ctx;
    1449                 :            :         char devtype[WPS_DEV_TYPE_BUFSIZE];
    1450                 :        132 :         char *wfd_dev_info_hex = NULL;
    1451                 :            : 
    1452                 :            : #ifdef CONFIG_WIFI_DISPLAY
    1453                 :        132 :         wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
    1454                 :            :                                                     WFD_SUBELEM_DEVICE_INFO);
    1455                 :            : #endif /* CONFIG_WIFI_DISPLAY */
    1456                 :            : 
    1457 [ +  + ][ +  + ]:        132 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
    1458                 :            :                        " p2p_dev_addr=" MACSTR
    1459                 :            :                        " pri_dev_type=%s name='%s' config_methods=0x%x "
    1460                 :            :                        "dev_capab=0x%x group_capab=0x%x%s%s",
    1461                 :       1584 :                        MAC2STR(addr), MAC2STR(info->p2p_device_addr),
    1462                 :        132 :                        wps_dev_type_bin2str(info->pri_dev_type, devtype,
    1463                 :            :                                             sizeof(devtype)),
    1464                 :        264 :                        info->device_name, info->config_methods,
    1465                 :        264 :                        info->dev_capab, info->group_capab,
    1466                 :            :                        wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
    1467                 :            :                        wfd_dev_info_hex ? wfd_dev_info_hex : "");
    1468                 :            : #endif /* CONFIG_NO_STDOUT_DEBUG */
    1469                 :            : 
    1470                 :        132 :         os_free(wfd_dev_info_hex);
    1471                 :            : 
    1472                 :        132 :         wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
    1473                 :        132 : }
    1474                 :            : 
    1475                 :            : 
    1476                 :        132 : static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
    1477                 :            : {
    1478                 :        132 :         struct wpa_supplicant *wpa_s = ctx;
    1479                 :            : 
    1480                 :        132 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
    1481                 :        792 :                        "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
    1482                 :            : 
    1483                 :        132 :         wpas_notify_p2p_device_lost(wpa_s, dev_addr);
    1484                 :        132 : }
    1485                 :            : 
    1486                 :            : 
    1487                 :         98 : static void wpas_find_stopped(void *ctx)
    1488                 :            : {
    1489                 :         98 :         struct wpa_supplicant *wpa_s = ctx;
    1490                 :         98 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
    1491                 :         98 : }
    1492                 :            : 
    1493                 :            : 
    1494                 :        494 : static int wpas_start_listen(void *ctx, unsigned int freq,
    1495                 :            :                              unsigned int duration,
    1496                 :            :                              const struct wpabuf *probe_resp_ie)
    1497                 :            : {
    1498                 :        494 :         struct wpa_supplicant *wpa_s = ctx;
    1499                 :            : 
    1500                 :        494 :         wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
    1501                 :            : 
    1502         [ -  + ]:        494 :         if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
    1503                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
    1504                 :            :                            "report received Probe Request frames");
    1505                 :          0 :                 return -1;
    1506                 :            :         }
    1507                 :            : 
    1508                 :        494 :         wpa_s->pending_listen_freq = freq;
    1509                 :        494 :         wpa_s->pending_listen_duration = duration;
    1510                 :            : 
    1511         [ -  + ]:        494 :         if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
    1512                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
    1513                 :            :                            "to remain on channel (%u MHz) for Listen "
    1514                 :            :                            "state", freq);
    1515                 :          0 :                 wpa_s->pending_listen_freq = 0;
    1516                 :          0 :                 return -1;
    1517                 :            :         }
    1518                 :        494 :         wpa_s->off_channel_freq = 0;
    1519                 :        494 :         wpa_s->roc_waiting_drv_freq = freq;
    1520                 :            : 
    1521                 :        494 :         return 0;
    1522                 :            : }
    1523                 :            : 
    1524                 :            : 
    1525                 :       1942 : static void wpas_stop_listen(void *ctx)
    1526                 :            : {
    1527                 :       1942 :         struct wpa_supplicant *wpa_s = ctx;
    1528 [ +  + ][ +  + ]:       1942 :         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
    1529                 :        117 :                 wpa_drv_cancel_remain_on_channel(wpa_s);
    1530                 :        117 :                 wpa_s->off_channel_freq = 0;
    1531                 :        117 :                 wpa_s->roc_waiting_drv_freq = 0;
    1532                 :            :         }
    1533                 :       1942 :         wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
    1534                 :       1942 :         wpa_drv_probe_req_report(wpa_s, 0);
    1535                 :       1942 : }
    1536                 :            : 
    1537                 :            : 
    1538                 :        241 : static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
    1539                 :            : {
    1540                 :        241 :         struct wpa_supplicant *wpa_s = ctx;
    1541                 :        241 :         return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
    1542                 :            : }
    1543                 :            : 
    1544                 :            : 
    1545                 :            : /*
    1546                 :            :  * DNS Header section is used only to calculate compression pointers, so the
    1547                 :            :  * contents of this data does not matter, but the length needs to be reserved
    1548                 :            :  * in the virtual packet.
    1549                 :            :  */
    1550                 :            : #define DNS_HEADER_LEN 12
    1551                 :            : 
    1552                 :            : /*
    1553                 :            :  * 27-octet in-memory packet from P2P specification containing two implied
    1554                 :            :  * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
    1555                 :            :  */
    1556                 :            : #define P2P_SD_IN_MEMORY_LEN 27
    1557                 :            : 
    1558                 :          4 : static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
    1559                 :            :                                        u8 **spos, const u8 *end)
    1560                 :            : {
    1561         [ +  - ]:         10 :         while (*spos < end) {
    1562                 :         10 :                 u8 val = ((*spos)[0] & 0xc0) >> 6;
    1563                 :            :                 int len;
    1564                 :            : 
    1565 [ +  - ][ -  + ]:         10 :                 if (val == 1 || val == 2) {
    1566                 :            :                         /* These are reserved values in RFC 1035 */
    1567                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
    1568                 :            :                                    "sequence starting with 0x%x", val);
    1569                 :          0 :                         return -1;
    1570                 :            :                 }
    1571                 :            : 
    1572         [ +  + ]:         10 :                 if (val == 3) {
    1573                 :            :                         u16 offset;
    1574                 :            :                         u8 *spos_tmp;
    1575                 :            : 
    1576                 :            :                         /* Offset */
    1577         [ -  + ]:          2 :                         if (*spos + 2 > end) {
    1578                 :          0 :                                 wpa_printf(MSG_DEBUG, "P2P: No room for full "
    1579                 :            :                                            "DNS offset field");
    1580                 :          0 :                                 return -1;
    1581                 :            :                         }
    1582                 :            : 
    1583                 :          2 :                         offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
    1584         [ -  + ]:          2 :                         if (offset >= *spos - start) {
    1585                 :          0 :                                 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
    1586                 :            :                                            "pointer offset %u", offset);
    1587                 :          0 :                                 return -1;
    1588                 :            :                         }
    1589                 :            : 
    1590                 :          2 :                         (*spos) += 2;
    1591                 :          2 :                         spos_tmp = start + offset;
    1592                 :          2 :                         return p2p_sd_dns_uncompress_label(upos, uend, start,
    1593                 :            :                                                            &spos_tmp,
    1594                 :          2 :                                                            *spos - 2);
    1595                 :            :                 }
    1596                 :            : 
    1597                 :            :                 /* Label */
    1598                 :          8 :                 len = (*spos)[0] & 0x3f;
    1599         [ +  + ]:          8 :                 if (len == 0)
    1600                 :          2 :                         return 0;
    1601                 :            : 
    1602                 :          6 :                 (*spos)++;
    1603         [ -  + ]:          6 :                 if (*spos + len > end) {
    1604                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
    1605                 :            :                                    "sequence - no room for label with length "
    1606                 :            :                                    "%u", len);
    1607                 :          0 :                         return -1;
    1608                 :            :                 }
    1609                 :            : 
    1610         [ -  + ]:          6 :                 if (*upos + len + 2 > uend)
    1611                 :          0 :                         return -2;
    1612                 :            : 
    1613                 :          6 :                 os_memcpy(*upos, *spos, len);
    1614                 :          6 :                 *spos += len;
    1615                 :          6 :                 *upos += len;
    1616                 :          6 :                 (*upos)[0] = '.';
    1617                 :          6 :                 (*upos)++;
    1618                 :          6 :                 (*upos)[0] = '\0';
    1619                 :            :         }
    1620                 :            : 
    1621                 :          4 :         return 0;
    1622                 :            : }
    1623                 :            : 
    1624                 :            : 
    1625                 :            : /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
    1626                 :            :  * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
    1627                 :            :  * not large enough */
    1628                 :          2 : static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
    1629                 :            :                                  size_t msg_len, size_t offset)
    1630                 :            : {
    1631                 :            :         /* 27-octet in-memory packet from P2P specification */
    1632                 :          2 :         const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
    1633                 :            :                 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
    1634                 :            :         u8 *tmp, *end, *spos;
    1635                 :            :         char *upos, *uend;
    1636                 :          2 :         int ret = 0;
    1637                 :            : 
    1638         [ -  + ]:          2 :         if (buf_len < 2)
    1639                 :          0 :                 return -1;
    1640         [ -  + ]:          2 :         if (offset > msg_len)
    1641                 :          0 :                 return -1;
    1642                 :            : 
    1643                 :          2 :         tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
    1644         [ -  + ]:          2 :         if (tmp == NULL)
    1645                 :          0 :                 return -1;
    1646                 :          2 :         spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
    1647                 :          2 :         end = spos + msg_len;
    1648                 :          2 :         spos += offset;
    1649                 :            : 
    1650                 :          2 :         os_memset(tmp, 0, DNS_HEADER_LEN);
    1651                 :          2 :         os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
    1652                 :          2 :         os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
    1653                 :            : 
    1654                 :          2 :         upos = buf;
    1655                 :          2 :         uend = buf + buf_len;
    1656                 :            : 
    1657                 :          2 :         ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
    1658         [ -  + ]:          2 :         if (ret) {
    1659                 :          0 :                 os_free(tmp);
    1660                 :          0 :                 return ret;
    1661                 :            :         }
    1662                 :            : 
    1663         [ -  + ]:          2 :         if (upos == buf) {
    1664                 :          0 :                 upos[0] = '.';
    1665                 :          0 :                 upos[1] = '\0';
    1666         [ +  - ]:          2 :         } else if (upos[-1] == '.')
    1667                 :          2 :                 upos[-1] = '\0';
    1668                 :            : 
    1669                 :          2 :         os_free(tmp);
    1670                 :          2 :         return 0;
    1671                 :            : }
    1672                 :            : 
    1673                 :            : 
    1674                 :            : static struct p2p_srv_bonjour *
    1675                 :          0 : wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
    1676                 :            :                              const struct wpabuf *query)
    1677                 :            : {
    1678                 :            :         struct p2p_srv_bonjour *bsrv;
    1679                 :            :         size_t len;
    1680                 :            : 
    1681                 :          0 :         len = wpabuf_len(query);
    1682         [ #  # ]:          0 :         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
    1683                 :            :                          struct p2p_srv_bonjour, list) {
    1684   [ #  #  #  # ]:          0 :                 if (len == wpabuf_len(bsrv->query) &&
    1685                 :          0 :                     os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
    1686                 :            :                               len) == 0)
    1687                 :          0 :                         return bsrv;
    1688                 :            :         }
    1689                 :          0 :         return NULL;
    1690                 :            : }
    1691                 :            : 
    1692                 :            : 
    1693                 :            : static struct p2p_srv_upnp *
    1694                 :         30 : wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
    1695                 :            :                           const char *service)
    1696                 :            : {
    1697                 :            :         struct p2p_srv_upnp *usrv;
    1698                 :            : 
    1699         [ +  + ]:         90 :         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
    1700                 :            :                          struct p2p_srv_upnp, list) {
    1701 [ +  - ][ -  + ]:         60 :                 if (version == usrv->version &&
    1702                 :         60 :                     os_strcmp(service, usrv->service) == 0)
    1703                 :          0 :                         return usrv;
    1704                 :            :         }
    1705                 :         30 :         return NULL;
    1706                 :            : }
    1707                 :            : 
    1708                 :            : 
    1709                 :          1 : static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
    1710                 :            :                                         u8 srv_trans_id)
    1711                 :            : {
    1712                 :            :         u8 *len_pos;
    1713                 :            : 
    1714         [ -  + ]:          1 :         if (wpabuf_tailroom(resp) < 5)
    1715                 :          1 :                 return;
    1716                 :            : 
    1717                 :            :         /* Length (to be filled) */
    1718                 :          1 :         len_pos = wpabuf_put(resp, 2);
    1719                 :          1 :         wpabuf_put_u8(resp, srv_proto);
    1720                 :          1 :         wpabuf_put_u8(resp, srv_trans_id);
    1721                 :            :         /* Status Code */
    1722                 :          1 :         wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
    1723                 :            :         /* Response Data: empty */
    1724                 :          1 :         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
    1725                 :            : }
    1726                 :            : 
    1727                 :            : 
    1728                 :          2 : static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
    1729                 :            :                                 struct wpabuf *resp, u8 srv_trans_id)
    1730                 :            : {
    1731                 :            :         struct p2p_srv_bonjour *bsrv;
    1732                 :            :         u8 *len_pos;
    1733                 :            : 
    1734                 :          2 :         wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
    1735                 :            : 
    1736         [ -  + ]:          2 :         if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
    1737                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
    1738                 :          0 :                 return;
    1739                 :            :         }
    1740                 :            : 
    1741         [ +  + ]:         10 :         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
    1742                 :            :                          struct p2p_srv_bonjour, list) {
    1743         [ -  + ]:         16 :                 if (wpabuf_tailroom(resp) <
    1744                 :          8 :                     5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
    1745                 :          0 :                         return;
    1746                 :            :                 /* Length (to be filled) */
    1747                 :          8 :                 len_pos = wpabuf_put(resp, 2);
    1748                 :          8 :                 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
    1749                 :          8 :                 wpabuf_put_u8(resp, srv_trans_id);
    1750                 :            :                 /* Status Code */
    1751                 :          8 :                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
    1752                 :          8 :                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
    1753                 :          8 :                                   wpabuf_head(bsrv->resp),
    1754                 :          8 :                                   wpabuf_len(bsrv->resp));
    1755                 :            :                 /* Response Data */
    1756                 :          8 :                 wpabuf_put_buf(resp, bsrv->query); /* Key */
    1757                 :          8 :                 wpabuf_put_buf(resp, bsrv->resp); /* Value */
    1758                 :          8 :                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
    1759                 :            :                              2);
    1760                 :            :         }
    1761                 :            : }
    1762                 :            : 
    1763                 :            : 
    1764                 :          4 : static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
    1765                 :            :                                size_t query_len)
    1766                 :            : {
    1767                 :            :         char str_rx[256], str_srv[256];
    1768                 :            : 
    1769 [ +  - ][ -  + ]:          4 :         if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
    1770                 :          0 :                 return 0; /* Too short to include DNS Type and Version */
    1771         [ +  + ]:          4 :         if (os_memcmp(query + query_len - 3,
    1772                 :            :                       wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
    1773                 :            :                       3) != 0)
    1774                 :          2 :                 return 0; /* Mismatch in DNS Type or Version */
    1775   [ +  +  +  - ]:          3 :         if (query_len == wpabuf_len(bsrv->query) &&
    1776                 :          1 :             os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
    1777                 :          1 :                 return 1; /* Binary match */
    1778                 :            : 
    1779         [ -  + ]:          1 :         if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
    1780                 :            :                                   0))
    1781                 :          0 :                 return 0; /* Failed to uncompress query */
    1782         [ -  + ]:          1 :         if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
    1783                 :          1 :                                   wpabuf_head(bsrv->query),
    1784                 :          1 :                                   wpabuf_len(bsrv->query) - 3, 0))
    1785                 :          0 :                 return 0; /* Failed to uncompress service */
    1786                 :            : 
    1787                 :          4 :         return os_strcmp(str_rx, str_srv) == 0;
    1788                 :            : }
    1789                 :            : 
    1790                 :            : 
    1791                 :          2 : static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
    1792                 :            :                                 struct wpabuf *resp, u8 srv_trans_id,
    1793                 :            :                                 const u8 *query, size_t query_len)
    1794                 :            : {
    1795                 :            :         struct p2p_srv_bonjour *bsrv;
    1796                 :            :         u8 *len_pos;
    1797                 :          2 :         int matches = 0;
    1798                 :            : 
    1799                 :          2 :         wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
    1800                 :            :                           query, query_len);
    1801         [ -  + ]:          2 :         if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
    1802                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
    1803                 :          0 :                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
    1804                 :            :                                             srv_trans_id);
    1805                 :          0 :                 return;
    1806                 :            :         }
    1807                 :            : 
    1808         [ +  + ]:          2 :         if (query_len == 0) {
    1809                 :          1 :                 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
    1810                 :          1 :                 return;
    1811                 :            :         }
    1812                 :            : 
    1813         [ +  + ]:          5 :         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
    1814                 :            :                          struct p2p_srv_bonjour, list) {
    1815         [ +  + ]:          4 :                 if (!match_bonjour_query(bsrv, query, query_len))
    1816                 :          3 :                         continue;
    1817                 :            : 
    1818         [ -  + ]:          2 :                 if (wpabuf_tailroom(resp) <
    1819                 :          1 :                     5 + query_len + wpabuf_len(bsrv->resp))
    1820                 :          0 :                         return;
    1821                 :            : 
    1822                 :          1 :                 matches++;
    1823                 :            : 
    1824                 :            :                 /* Length (to be filled) */
    1825                 :          1 :                 len_pos = wpabuf_put(resp, 2);
    1826                 :          1 :                 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
    1827                 :          1 :                 wpabuf_put_u8(resp, srv_trans_id);
    1828                 :            : 
    1829                 :            :                 /* Status Code */
    1830                 :          1 :                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
    1831                 :          1 :                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
    1832                 :          1 :                                   wpabuf_head(bsrv->resp),
    1833                 :          1 :                                   wpabuf_len(bsrv->resp));
    1834                 :            : 
    1835                 :            :                 /* Response Data */
    1836                 :          1 :                 wpabuf_put_data(resp, query, query_len); /* Key */
    1837                 :          1 :                 wpabuf_put_buf(resp, bsrv->resp); /* Value */
    1838                 :            : 
    1839                 :          1 :                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
    1840                 :            :         }
    1841                 :            : 
    1842         [ -  + ]:          1 :         if (matches == 0) {
    1843                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
    1844                 :            :                            "available");
    1845         [ #  # ]:          0 :                 if (wpabuf_tailroom(resp) < 5)
    1846                 :          0 :                         return;
    1847                 :            : 
    1848                 :            :                 /* Length (to be filled) */
    1849                 :          0 :                 len_pos = wpabuf_put(resp, 2);
    1850                 :          0 :                 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
    1851                 :          0 :                 wpabuf_put_u8(resp, srv_trans_id);
    1852                 :            : 
    1853                 :            :                 /* Status Code */
    1854                 :          0 :                 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
    1855                 :            :                 /* Response Data: empty */
    1856                 :          2 :                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
    1857                 :            :                              2);
    1858                 :            :         }
    1859                 :            : }
    1860                 :            : 
    1861                 :            : 
    1862                 :          2 : static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
    1863                 :            :                              struct wpabuf *resp, u8 srv_trans_id)
    1864                 :            : {
    1865                 :            :         struct p2p_srv_upnp *usrv;
    1866                 :            :         u8 *len_pos;
    1867                 :            : 
    1868                 :          2 :         wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
    1869                 :            : 
    1870         [ -  + ]:          2 :         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
    1871                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
    1872                 :          0 :                 return;
    1873                 :            :         }
    1874                 :            : 
    1875         [ +  + ]:         12 :         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
    1876                 :            :                          struct p2p_srv_upnp, list) {
    1877         [ -  + ]:         10 :                 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
    1878                 :          0 :                         return;
    1879                 :            : 
    1880                 :            :                 /* Length (to be filled) */
    1881                 :         10 :                 len_pos = wpabuf_put(resp, 2);
    1882                 :         10 :                 wpabuf_put_u8(resp, P2P_SERV_UPNP);
    1883                 :         10 :                 wpabuf_put_u8(resp, srv_trans_id);
    1884                 :            : 
    1885                 :            :                 /* Status Code */
    1886                 :         10 :                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
    1887                 :            :                 /* Response Data */
    1888                 :         10 :                 wpabuf_put_u8(resp, usrv->version);
    1889                 :         10 :                 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
    1890                 :            :                            usrv->service);
    1891                 :         10 :                 wpabuf_put_str(resp, usrv->service);
    1892                 :         10 :                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
    1893                 :            :                              2);
    1894                 :            :         }
    1895                 :            : }
    1896                 :            : 
    1897                 :            : 
    1898                 :          2 : static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
    1899                 :            :                              struct wpabuf *resp, u8 srv_trans_id,
    1900                 :            :                              const u8 *query, size_t query_len)
    1901                 :            : {
    1902                 :            :         struct p2p_srv_upnp *usrv;
    1903                 :            :         u8 *len_pos;
    1904                 :            :         u8 version;
    1905                 :            :         char *str;
    1906                 :          2 :         int count = 0;
    1907                 :            : 
    1908                 :          2 :         wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
    1909                 :            :                           query, query_len);
    1910                 :            : 
    1911         [ -  + ]:          2 :         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
    1912                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
    1913                 :          0 :                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
    1914                 :            :                                             srv_trans_id);
    1915                 :          0 :                 return;
    1916                 :            :         }
    1917                 :            : 
    1918         [ +  + ]:          2 :         if (query_len == 0) {
    1919                 :          1 :                 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
    1920                 :          1 :                 return;
    1921                 :            :         }
    1922                 :            : 
    1923         [ -  + ]:          1 :         if (wpabuf_tailroom(resp) < 5)
    1924                 :          0 :                 return;
    1925                 :            : 
    1926                 :            :         /* Length (to be filled) */
    1927                 :          1 :         len_pos = wpabuf_put(resp, 2);
    1928                 :          1 :         wpabuf_put_u8(resp, P2P_SERV_UPNP);
    1929                 :          1 :         wpabuf_put_u8(resp, srv_trans_id);
    1930                 :            : 
    1931                 :          1 :         version = query[0];
    1932                 :          1 :         str = os_malloc(query_len);
    1933         [ -  + ]:          1 :         if (str == NULL)
    1934                 :          0 :                 return;
    1935                 :          1 :         os_memcpy(str, query + 1, query_len - 1);
    1936                 :          1 :         str[query_len - 1] = '\0';
    1937                 :            : 
    1938         [ +  + ]:          6 :         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
    1939                 :            :                          struct p2p_srv_upnp, list) {
    1940         [ -  + ]:          5 :                 if (version != usrv->version)
    1941                 :          0 :                         continue;
    1942                 :            : 
    1943 [ -  + ][ #  # ]:          5 :                 if (os_strcmp(str, "ssdp:all") != 0 &&
    1944                 :          0 :                     os_strstr(usrv->service, str) == NULL)
    1945                 :          0 :                         continue;
    1946                 :            : 
    1947         [ -  + ]:          5 :                 if (wpabuf_tailroom(resp) < 2)
    1948                 :          0 :                         break;
    1949         [ +  + ]:          5 :                 if (count == 0) {
    1950                 :            :                         /* Status Code */
    1951                 :          1 :                         wpabuf_put_u8(resp, P2P_SD_SUCCESS);
    1952                 :            :                         /* Response Data */
    1953                 :          1 :                         wpabuf_put_u8(resp, version);
    1954                 :            :                 } else
    1955                 :          4 :                         wpabuf_put_u8(resp, ',');
    1956                 :            : 
    1957                 :          5 :                 count++;
    1958                 :            : 
    1959                 :          5 :                 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
    1960                 :            :                            usrv->service);
    1961         [ -  + ]:          5 :                 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
    1962                 :          0 :                         break;
    1963                 :          5 :                 wpabuf_put_str(resp, usrv->service);
    1964                 :            :         }
    1965                 :          1 :         os_free(str);
    1966                 :            : 
    1967         [ -  + ]:          1 :         if (count == 0) {
    1968                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
    1969                 :            :                            "available");
    1970                 :            :                 /* Status Code */
    1971                 :          0 :                 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
    1972                 :            :                 /* Response Data: empty */
    1973                 :            :         }
    1974                 :            : 
    1975                 :          2 :         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
    1976                 :            : }
    1977                 :            : 
    1978                 :            : 
    1979                 :            : #ifdef CONFIG_WIFI_DISPLAY
    1980                 :          0 : static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
    1981                 :            :                             struct wpabuf *resp, u8 srv_trans_id,
    1982                 :            :                             const u8 *query, size_t query_len)
    1983                 :            : {
    1984                 :            :         const u8 *pos;
    1985                 :            :         u8 role;
    1986                 :            :         u8 *len_pos;
    1987                 :            : 
    1988                 :          0 :         wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
    1989                 :            : 
    1990         [ #  # ]:          0 :         if (!wpa_s->global->wifi_display) {
    1991                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
    1992                 :          0 :                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
    1993                 :            :                                             srv_trans_id);
    1994                 :          0 :                 return;
    1995                 :            :         }
    1996                 :            : 
    1997         [ #  # ]:          0 :         if (query_len < 1) {
    1998                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
    1999                 :            :                            "Role");
    2000                 :          0 :                 return;
    2001                 :            :         }
    2002                 :            : 
    2003         [ #  # ]:          0 :         if (wpabuf_tailroom(resp) < 5)
    2004                 :          0 :                 return;
    2005                 :            : 
    2006                 :          0 :         pos = query;
    2007                 :          0 :         role = *pos++;
    2008                 :          0 :         wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
    2009                 :            : 
    2010                 :            :         /* TODO: role specific handling */
    2011                 :            : 
    2012                 :            :         /* Length (to be filled) */
    2013                 :          0 :         len_pos = wpabuf_put(resp, 2);
    2014                 :          0 :         wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
    2015                 :          0 :         wpabuf_put_u8(resp, srv_trans_id);
    2016                 :          0 :         wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
    2017                 :            : 
    2018         [ #  # ]:          0 :         while (pos < query + query_len) {
    2019 [ #  # ][ #  # ]:          0 :                 if (*pos < MAX_WFD_SUBELEMS &&
    2020         [ #  # ]:          0 :                     wpa_s->global->wfd_subelem[*pos] &&
    2021                 :          0 :                     wpabuf_tailroom(resp) >=
    2022                 :          0 :                     wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
    2023                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
    2024                 :          0 :                                    "subelement %u", *pos);
    2025                 :          0 :                         wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
    2026                 :            :                 }
    2027                 :          0 :                 pos++;
    2028                 :            :         }
    2029                 :            : 
    2030                 :          0 :         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
    2031                 :            : }
    2032                 :            : #endif /* CONFIG_WIFI_DISPLAY */
    2033                 :            : 
    2034                 :            : 
    2035                 :          6 : static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
    2036                 :            :                             u16 update_indic, const u8 *tlvs, size_t tlvs_len)
    2037                 :            : {
    2038                 :          6 :         struct wpa_supplicant *wpa_s = ctx;
    2039                 :          6 :         const u8 *pos = tlvs;
    2040                 :          6 :         const u8 *end = tlvs + tlvs_len;
    2041                 :            :         const u8 *tlv_end;
    2042                 :            :         u16 slen;
    2043                 :            :         struct wpabuf *resp;
    2044                 :            :         u8 srv_proto, srv_trans_id;
    2045                 :            :         size_t buf_len;
    2046                 :            :         char *buf;
    2047                 :            : 
    2048                 :          6 :         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
    2049                 :            :                     tlvs, tlvs_len);
    2050                 :          6 :         buf_len = 2 * tlvs_len + 1;
    2051                 :          6 :         buf = os_malloc(buf_len);
    2052         [ +  - ]:          6 :         if (buf) {
    2053                 :          6 :                 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
    2054                 :          6 :                 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
    2055                 :            :                              MACSTR " %u %u %s",
    2056                 :         36 :                              freq, MAC2STR(sa), dialog_token, update_indic,
    2057                 :            :                              buf);
    2058                 :          6 :                 os_free(buf);
    2059                 :            :         }
    2060                 :            : 
    2061         [ -  + ]:          6 :         if (wpa_s->p2p_sd_over_ctrl_iface) {
    2062                 :          0 :                 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
    2063                 :            :                                            update_indic, tlvs, tlvs_len);
    2064                 :          0 :                 return; /* to be processed by an external program */
    2065                 :            :         }
    2066                 :            : 
    2067                 :          6 :         resp = wpabuf_alloc(10000);
    2068         [ -  + ]:          6 :         if (resp == NULL)
    2069                 :          0 :                 return;
    2070                 :            : 
    2071         [ +  + ]:         12 :         while (pos + 1 < end) {
    2072                 :          6 :                 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
    2073                 :          6 :                 slen = WPA_GET_LE16(pos);
    2074                 :          6 :                 pos += 2;
    2075 [ +  - ][ -  + ]:          6 :                 if (pos + slen > end || slen < 2) {
    2076                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
    2077                 :            :                                    "length");
    2078                 :          0 :                         wpabuf_free(resp);
    2079                 :          0 :                         return;
    2080                 :            :                 }
    2081                 :          6 :                 tlv_end = pos + slen;
    2082                 :            : 
    2083                 :          6 :                 srv_proto = *pos++;
    2084                 :          6 :                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
    2085                 :            :                            srv_proto);
    2086                 :          6 :                 srv_trans_id = *pos++;
    2087                 :          6 :                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
    2088                 :            :                            srv_trans_id);
    2089                 :            : 
    2090                 :          6 :                 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
    2091                 :          6 :                             pos, tlv_end - pos);
    2092                 :            : 
    2093                 :            : 
    2094         [ -  + ]:          6 :                 if (wpa_s->force_long_sd) {
    2095                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
    2096                 :            :                                    "response");
    2097                 :          0 :                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
    2098                 :          0 :                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
    2099                 :          0 :                         goto done;
    2100                 :            :                 }
    2101                 :            : 
    2102   [ +  +  +  -  :          6 :                 switch (srv_proto) {
                      + ]
    2103                 :            :                 case P2P_SERV_ALL_SERVICES:
    2104                 :          1 :                         wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
    2105                 :            :                                    "for all services");
    2106   [ -  +  #  # ]:          1 :                         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
    2107                 :          0 :                             dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
    2108                 :          0 :                                 wpa_printf(MSG_DEBUG, "P2P: No service "
    2109                 :            :                                            "discovery protocols available");
    2110                 :          0 :                                 wpas_sd_add_proto_not_avail(
    2111                 :            :                                         resp, P2P_SERV_ALL_SERVICES,
    2112                 :            :                                         srv_trans_id);
    2113                 :          0 :                                 break;
    2114                 :            :                         }
    2115                 :          1 :                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
    2116                 :          1 :                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
    2117                 :          1 :                         break;
    2118                 :            :                 case P2P_SERV_BONJOUR:
    2119                 :          2 :                         wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
    2120                 :          2 :                                             pos, tlv_end - pos);
    2121                 :          2 :                         break;
    2122                 :            :                 case P2P_SERV_UPNP:
    2123                 :          2 :                         wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
    2124                 :          2 :                                          pos, tlv_end - pos);
    2125                 :          2 :                         break;
    2126                 :            : #ifdef CONFIG_WIFI_DISPLAY
    2127                 :            :                 case P2P_SERV_WIFI_DISPLAY:
    2128                 :          0 :                         wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
    2129                 :          0 :                                         pos, tlv_end - pos);
    2130                 :          0 :                         break;
    2131                 :            : #endif /* CONFIG_WIFI_DISPLAY */
    2132                 :            :                 default:
    2133                 :          1 :                         wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
    2134                 :            :                                    "protocol %u", srv_proto);
    2135                 :          1 :                         wpas_sd_add_proto_not_avail(resp, srv_proto,
    2136                 :            :                                                     srv_trans_id);
    2137                 :          1 :                         break;
    2138                 :            :                 }
    2139                 :            : 
    2140                 :          6 :                 pos = tlv_end;
    2141                 :            :         }
    2142                 :            : 
    2143                 :            : done:
    2144                 :          6 :         wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
    2145                 :            :                                    update_indic, tlvs, tlvs_len);
    2146                 :            : 
    2147                 :          6 :         wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
    2148                 :            : 
    2149                 :          6 :         wpabuf_free(resp);
    2150                 :            : }
    2151                 :            : 
    2152                 :            : 
    2153                 :          6 : static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
    2154                 :            :                              const u8 *tlvs, size_t tlvs_len)
    2155                 :            : {
    2156                 :          6 :         struct wpa_supplicant *wpa_s = ctx;
    2157                 :          6 :         const u8 *pos = tlvs;
    2158                 :          6 :         const u8 *end = tlvs + tlvs_len;
    2159                 :            :         const u8 *tlv_end;
    2160                 :            :         u16 slen;
    2161                 :            :         size_t buf_len;
    2162                 :            :         char *buf;
    2163                 :            : 
    2164                 :          6 :         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
    2165                 :            :                     tlvs, tlvs_len);
    2166         [ -  + ]:          6 :         if (tlvs_len > 1500) {
    2167                 :            :                 /* TODO: better way for handling this */
    2168                 :          0 :                 wpa_msg_ctrl(wpa_s, MSG_INFO,
    2169                 :            :                              P2P_EVENT_SERV_DISC_RESP MACSTR
    2170                 :            :                              " %u <long response: %u bytes>",
    2171                 :          0 :                              MAC2STR(sa), update_indic,
    2172                 :            :                              (unsigned int) tlvs_len);
    2173                 :            :         } else {
    2174                 :          6 :                 buf_len = 2 * tlvs_len + 1;
    2175                 :          6 :                 buf = os_malloc(buf_len);
    2176         [ +  - ]:          6 :                 if (buf) {
    2177                 :          6 :                         wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
    2178                 :          6 :                         wpa_msg_ctrl(wpa_s, MSG_INFO,
    2179                 :            :                                      P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
    2180                 :         36 :                                      MAC2STR(sa), update_indic, buf);
    2181                 :          6 :                         os_free(buf);
    2182                 :            :                 }
    2183                 :            :         }
    2184                 :            : 
    2185         [ +  + ]:         27 :         while (pos < end) {
    2186                 :            :                 u8 srv_proto, srv_trans_id, status;
    2187                 :            : 
    2188                 :         21 :                 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
    2189                 :         21 :                 slen = WPA_GET_LE16(pos);
    2190                 :         21 :                 pos += 2;
    2191 [ +  - ][ -  + ]:         21 :                 if (pos + slen > end || slen < 3) {
    2192                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
    2193                 :            :                                    "length");
    2194                 :          6 :                         return;
    2195                 :            :                 }
    2196                 :         21 :                 tlv_end = pos + slen;
    2197                 :            : 
    2198                 :         21 :                 srv_proto = *pos++;
    2199                 :         21 :                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
    2200                 :            :                            srv_proto);
    2201                 :         21 :                 srv_trans_id = *pos++;
    2202                 :         21 :                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
    2203                 :            :                            srv_trans_id);
    2204                 :         21 :                 status = *pos++;
    2205                 :         21 :                 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
    2206                 :            :                            status);
    2207                 :            : 
    2208                 :         21 :                 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
    2209                 :         21 :                             pos, tlv_end - pos);
    2210                 :            : 
    2211                 :         21 :                 pos = tlv_end;
    2212                 :            :         }
    2213                 :            : 
    2214                 :          6 :         wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
    2215                 :            : }
    2216                 :            : 
    2217                 :            : 
    2218                 :          6 : u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
    2219                 :            :                         const struct wpabuf *tlvs)
    2220                 :            : {
    2221 [ +  - ][ -  + ]:          6 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    2222                 :          0 :                 return 0;
    2223                 :          6 :         return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
    2224                 :            : }
    2225                 :            : 
    2226                 :            : 
    2227                 :          1 : u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
    2228                 :            :                              u8 version, const char *query)
    2229                 :            : {
    2230                 :            :         struct wpabuf *tlvs;
    2231                 :            :         u64 ret;
    2232                 :            : 
    2233                 :          1 :         tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
    2234         [ -  + ]:          1 :         if (tlvs == NULL)
    2235                 :          0 :                 return 0;
    2236                 :          1 :         wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
    2237                 :          1 :         wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
    2238                 :          1 :         wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
    2239                 :          1 :         wpabuf_put_u8(tlvs, version);
    2240                 :          1 :         wpabuf_put_str(tlvs, query);
    2241                 :          1 :         ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
    2242                 :          1 :         wpabuf_free(tlvs);
    2243                 :          1 :         return ret;
    2244                 :            : }
    2245                 :            : 
    2246                 :            : 
    2247                 :            : #ifdef CONFIG_WIFI_DISPLAY
    2248                 :            : 
    2249                 :          0 : static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
    2250                 :            :                                    const struct wpabuf *tlvs)
    2251                 :            : {
    2252 [ #  # ][ #  # ]:          0 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    2253                 :          0 :                 return 0;
    2254                 :          0 :         return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
    2255                 :            : }
    2256                 :            : 
    2257                 :            : 
    2258                 :            : #define MAX_WFD_SD_SUBELEMS 20
    2259                 :            : 
    2260                 :          0 : static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
    2261                 :            :                                 const char *subelems)
    2262                 :            : {
    2263                 :            :         u8 *len;
    2264                 :            :         const char *pos;
    2265                 :            :         int val;
    2266                 :          0 :         int count = 0;
    2267                 :            : 
    2268                 :          0 :         len = wpabuf_put(tlvs, 2);
    2269                 :          0 :         wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
    2270                 :          0 :         wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
    2271                 :            : 
    2272                 :          0 :         wpabuf_put_u8(tlvs, role);
    2273                 :            : 
    2274                 :          0 :         pos = subelems;
    2275         [ #  # ]:          0 :         while (*pos) {
    2276                 :          0 :                 val = atoi(pos);
    2277 [ #  # ][ #  # ]:          0 :                 if (val >= 0 && val < 256) {
    2278                 :          0 :                         wpabuf_put_u8(tlvs, val);
    2279                 :          0 :                         count++;
    2280         [ #  # ]:          0 :                         if (count == MAX_WFD_SD_SUBELEMS)
    2281                 :          0 :                                 break;
    2282                 :            :                 }
    2283                 :          0 :                 pos = os_strchr(pos + 1, ',');
    2284         [ #  # ]:          0 :                 if (pos == NULL)
    2285                 :          0 :                         break;
    2286                 :          0 :                 pos++;
    2287                 :            :         }
    2288                 :            : 
    2289                 :          0 :         WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
    2290                 :          0 : }
    2291                 :            : 
    2292                 :            : 
    2293                 :          0 : u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
    2294                 :            :                                      const u8 *dst, const char *role)
    2295                 :            : {
    2296                 :            :         struct wpabuf *tlvs;
    2297                 :            :         u64 ret;
    2298                 :            :         const char *subelems;
    2299                 :          0 :         u8 id = 1;
    2300                 :            : 
    2301                 :          0 :         subelems = os_strchr(role, ' ');
    2302         [ #  # ]:          0 :         if (subelems == NULL)
    2303                 :          0 :                 return 0;
    2304                 :          0 :         subelems++;
    2305                 :            : 
    2306                 :          0 :         tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
    2307         [ #  # ]:          0 :         if (tlvs == NULL)
    2308                 :          0 :                 return 0;
    2309                 :            : 
    2310         [ #  # ]:          0 :         if (os_strstr(role, "[source]"))
    2311                 :          0 :                 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
    2312         [ #  # ]:          0 :         if (os_strstr(role, "[pri-sink]"))
    2313                 :          0 :                 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
    2314         [ #  # ]:          0 :         if (os_strstr(role, "[sec-sink]"))
    2315                 :          0 :                 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
    2316         [ #  # ]:          0 :         if (os_strstr(role, "[source+sink]"))
    2317                 :          0 :                 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
    2318                 :            : 
    2319                 :          0 :         ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
    2320                 :          0 :         wpabuf_free(tlvs);
    2321                 :          0 :         return ret;
    2322                 :            : }
    2323                 :            : 
    2324                 :            : #endif /* CONFIG_WIFI_DISPLAY */
    2325                 :            : 
    2326                 :            : 
    2327                 :          0 : int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
    2328                 :            : {
    2329 [ #  # ][ #  # ]:          0 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    2330                 :          0 :                 return -1;
    2331                 :          0 :         return p2p_sd_cancel_request(wpa_s->global->p2p,
    2332                 :            :                                      (void *) (uintptr_t) req);
    2333                 :            : }
    2334                 :            : 
    2335                 :            : 
    2336                 :          6 : void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
    2337                 :            :                           const u8 *dst, u8 dialog_token,
    2338                 :            :                           const struct wpabuf *resp_tlvs)
    2339                 :            : {
    2340 [ +  - ][ -  + ]:          6 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    2341                 :          6 :                 return;
    2342                 :          6 :         p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
    2343                 :            :                         resp_tlvs);
    2344                 :            : }
    2345                 :            : 
    2346                 :            : 
    2347                 :        708 : void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
    2348                 :            : {
    2349         [ +  - ]:        708 :         if (wpa_s->global->p2p)
    2350                 :        708 :                 p2p_sd_service_update(wpa_s->global->p2p);
    2351                 :        708 : }
    2352                 :            : 
    2353                 :            : 
    2354                 :         24 : static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
    2355                 :            : {
    2356                 :         24 :         dl_list_del(&bsrv->list);
    2357                 :         24 :         wpabuf_free(bsrv->query);
    2358                 :         24 :         wpabuf_free(bsrv->resp);
    2359                 :         24 :         os_free(bsrv);
    2360                 :         24 : }
    2361                 :            : 
    2362                 :            : 
    2363                 :         30 : static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
    2364                 :            : {
    2365                 :         30 :         dl_list_del(&usrv->list);
    2366                 :         30 :         os_free(usrv->service);
    2367                 :         30 :         os_free(usrv);
    2368                 :         30 : }
    2369                 :            : 
    2370                 :            : 
    2371                 :        654 : void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
    2372                 :            : {
    2373                 :            :         struct p2p_srv_bonjour *bsrv, *bn;
    2374                 :            :         struct p2p_srv_upnp *usrv, *un;
    2375                 :            : 
    2376         [ +  + ]:        678 :         dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
    2377                 :            :                               struct p2p_srv_bonjour, list)
    2378                 :         24 :                 wpas_p2p_srv_bonjour_free(bsrv);
    2379                 :            : 
    2380         [ +  + ]:        684 :         dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
    2381                 :            :                               struct p2p_srv_upnp, list)
    2382                 :         30 :                 wpas_p2p_srv_upnp_free(usrv);
    2383                 :            : 
    2384                 :        654 :         wpas_p2p_sd_service_update(wpa_s);
    2385                 :        654 : }
    2386                 :            : 
    2387                 :            : 
    2388                 :         24 : int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
    2389                 :            :                                  struct wpabuf *query, struct wpabuf *resp)
    2390                 :            : {
    2391                 :            :         struct p2p_srv_bonjour *bsrv;
    2392                 :            : 
    2393                 :         24 :         bsrv = os_zalloc(sizeof(*bsrv));
    2394         [ -  + ]:         24 :         if (bsrv == NULL)
    2395                 :          0 :                 return -1;
    2396                 :         24 :         bsrv->query = query;
    2397                 :         24 :         bsrv->resp = resp;
    2398                 :         24 :         dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
    2399                 :            : 
    2400                 :         24 :         wpas_p2p_sd_service_update(wpa_s);
    2401                 :         24 :         return 0;
    2402                 :            : }
    2403                 :            : 
    2404                 :            : 
    2405                 :          0 : int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
    2406                 :            :                                  const struct wpabuf *query)
    2407                 :            : {
    2408                 :            :         struct p2p_srv_bonjour *bsrv;
    2409                 :            : 
    2410                 :          0 :         bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
    2411         [ #  # ]:          0 :         if (bsrv == NULL)
    2412                 :          0 :                 return -1;
    2413                 :          0 :         wpas_p2p_srv_bonjour_free(bsrv);
    2414                 :          0 :         wpas_p2p_sd_service_update(wpa_s);
    2415                 :          0 :         return 0;
    2416                 :            : }
    2417                 :            : 
    2418                 :            : 
    2419                 :         30 : int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
    2420                 :            :                               const char *service)
    2421                 :            : {
    2422                 :            :         struct p2p_srv_upnp *usrv;
    2423                 :            : 
    2424         [ -  + ]:         30 :         if (wpas_p2p_service_get_upnp(wpa_s, version, service))
    2425                 :          0 :                 return 0; /* Already listed */
    2426                 :         30 :         usrv = os_zalloc(sizeof(*usrv));
    2427         [ -  + ]:         30 :         if (usrv == NULL)
    2428                 :          0 :                 return -1;
    2429                 :         30 :         usrv->version = version;
    2430                 :         30 :         usrv->service = os_strdup(service);
    2431         [ -  + ]:         30 :         if (usrv->service == NULL) {
    2432                 :          0 :                 os_free(usrv);
    2433                 :          0 :                 return -1;
    2434                 :            :         }
    2435                 :         30 :         dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
    2436                 :            : 
    2437                 :         30 :         wpas_p2p_sd_service_update(wpa_s);
    2438                 :         30 :         return 0;
    2439                 :            : }
    2440                 :            : 
    2441                 :            : 
    2442                 :          0 : int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
    2443                 :            :                               const char *service)
    2444                 :            : {
    2445                 :            :         struct p2p_srv_upnp *usrv;
    2446                 :            : 
    2447                 :          0 :         usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
    2448         [ #  # ]:          0 :         if (usrv == NULL)
    2449                 :          0 :                 return -1;
    2450                 :          0 :         wpas_p2p_srv_upnp_free(usrv);
    2451                 :          0 :         wpas_p2p_sd_service_update(wpa_s);
    2452                 :          0 :         return 0;
    2453                 :            : }
    2454                 :            : 
    2455                 :            : 
    2456                 :         20 : static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
    2457                 :            :                                          const u8 *peer, const char *params,
    2458                 :            :                                          unsigned int generated_pin)
    2459                 :            : {
    2460                 :         20 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
    2461                 :        120 :                        " %08d%s", MAC2STR(peer), generated_pin, params);
    2462                 :         20 : }
    2463                 :            : 
    2464                 :            : 
    2465                 :          2 : static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
    2466                 :            :                                         const u8 *peer, const char *params)
    2467                 :            : {
    2468                 :          2 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
    2469                 :         12 :                        "%s", MAC2STR(peer), params);
    2470                 :          2 : }
    2471                 :            : 
    2472                 :            : 
    2473                 :         21 : static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
    2474                 :            :                                const u8 *dev_addr, const u8 *pri_dev_type,
    2475                 :            :                                const char *dev_name, u16 supp_config_methods,
    2476                 :            :                                u8 dev_capab, u8 group_capab, const u8 *group_id,
    2477                 :            :                                size_t group_id_len)
    2478                 :            : {
    2479                 :         21 :         struct wpa_supplicant *wpa_s = ctx;
    2480                 :            :         char devtype[WPS_DEV_TYPE_BUFSIZE];
    2481                 :            :         char params[300];
    2482                 :            :         u8 empty_dev_type[8];
    2483                 :         21 :         unsigned int generated_pin = 0;
    2484                 :         21 :         struct wpa_supplicant *group = NULL;
    2485                 :            : 
    2486         [ +  + ]:         21 :         if (group_id) {
    2487         [ +  - ]:         18 :                 for (group = wpa_s->global->ifaces; group; group = group->next)
    2488                 :            :                 {
    2489                 :         18 :                         struct wpa_ssid *s = group->current_ssid;
    2490 [ +  - ][ +  - ]:         18 :                         if (s != NULL &&
    2491         [ +  - ]:         18 :                             s->mode == WPAS_MODE_P2P_GO &&
    2492         [ +  - ]:         18 :                             group_id_len - ETH_ALEN == s->ssid_len &&
    2493                 :         18 :                             os_memcmp(group_id + ETH_ALEN, s->ssid,
    2494                 :            :                                       s->ssid_len) == 0)
    2495                 :         18 :                                 break;
    2496                 :            :                 }
    2497                 :            :         }
    2498                 :            : 
    2499         [ -  + ]:         21 :         if (pri_dev_type == NULL) {
    2500                 :          0 :                 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
    2501                 :          0 :                 pri_dev_type = empty_dev_type;
    2502                 :            :         }
    2503 [ +  + ][ +  + ]:         21 :         os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
    2504                 :            :                     " pri_dev_type=%s name='%s' config_methods=0x%x "
    2505                 :            :                     "dev_capab=0x%x group_capab=0x%x%s%s",
    2506                 :        126 :                     MAC2STR(dev_addr),
    2507                 :            :                     wps_dev_type_bin2str(pri_dev_type, devtype,
    2508                 :            :                                          sizeof(devtype)),
    2509                 :            :                     dev_name, supp_config_methods, dev_capab, group_capab,
    2510                 :            :                     group ? " group=" : "",
    2511                 :            :                     group ? group->ifname : "");
    2512                 :         21 :         params[sizeof(params) - 1] = '\0';
    2513                 :            : 
    2514         [ +  + ]:         21 :         if (config_methods & WPS_CONFIG_DISPLAY) {
    2515                 :         19 :                 generated_pin = wps_generate_pin();
    2516                 :         19 :                 wpas_prov_disc_local_display(wpa_s, peer, params,
    2517                 :            :                                              generated_pin);
    2518         [ +  + ]:          2 :         } else if (config_methods & WPS_CONFIG_KEYPAD)
    2519                 :          1 :                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
    2520         [ +  - ]:          1 :         else if (config_methods & WPS_CONFIG_PUSHBUTTON)
    2521                 :          1 :                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
    2522                 :          6 :                                MACSTR "%s", MAC2STR(peer), params);
    2523                 :            : 
    2524                 :         21 :         wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
    2525                 :            :                                             P2P_PROV_DISC_SUCCESS,
    2526                 :            :                                             config_methods, generated_pin);
    2527                 :         21 : }
    2528                 :            : 
    2529                 :            : 
    2530                 :         21 : static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
    2531                 :            : {
    2532                 :         21 :         struct wpa_supplicant *wpa_s = ctx;
    2533                 :         21 :         unsigned int generated_pin = 0;
    2534                 :            :         char params[20];
    2535                 :            : 
    2536 [ +  + ][ -  + ]:         21 :         if (wpa_s->pending_pd_before_join &&
    2537         [ #  # ]:          0 :             (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
    2538                 :          0 :              os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
    2539                 :         18 :                 wpa_s->pending_pd_before_join = 0;
    2540                 :         18 :                 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
    2541                 :            :                            "join-existing-group operation");
    2542                 :         18 :                 wpas_p2p_join_start(wpa_s);
    2543                 :         21 :                 return;
    2544                 :            :         }
    2545                 :            : 
    2546 [ +  - ][ -  + ]:          3 :         if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
    2547                 :          3 :             wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
    2548                 :          0 :                 os_snprintf(params, sizeof(params), " peer_go=%d",
    2549                 :          0 :                             wpa_s->pending_pd_use == AUTO_PD_JOIN);
    2550                 :            :         else
    2551                 :          3 :                 params[0] = '\0';
    2552                 :            : 
    2553         [ +  + ]:          3 :         if (config_methods & WPS_CONFIG_DISPLAY)
    2554                 :          1 :                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
    2555         [ +  + ]:          2 :         else if (config_methods & WPS_CONFIG_KEYPAD) {
    2556                 :          1 :                 generated_pin = wps_generate_pin();
    2557                 :          1 :                 wpas_prov_disc_local_display(wpa_s, peer, params,
    2558                 :            :                                              generated_pin);
    2559         [ +  - ]:          1 :         } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
    2560                 :          1 :                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
    2561                 :          6 :                                MACSTR "%s", MAC2STR(peer), params);
    2562                 :            : 
    2563                 :          3 :         wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
    2564                 :            :                                             P2P_PROV_DISC_SUCCESS,
    2565                 :            :                                             config_methods, generated_pin);
    2566                 :            : }
    2567                 :            : 
    2568                 :            : 
    2569                 :          0 : static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
    2570                 :            :                                 enum p2p_prov_disc_status status)
    2571                 :            : {
    2572                 :          0 :         struct wpa_supplicant *wpa_s = ctx;
    2573                 :            : 
    2574         [ #  # ]:          0 :         if (wpa_s->p2p_fallback_to_go_neg) {
    2575                 :          0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
    2576                 :            :                         "failed - fall back to GO Negotiation");
    2577                 :          0 :                 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
    2578                 :          0 :                 return;
    2579                 :            :         }
    2580                 :            : 
    2581         [ #  # ]:          0 :         if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
    2582                 :          0 :                 wpa_s->pending_pd_before_join = 0;
    2583                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
    2584                 :            :                            "join-existing-group operation (no ACK for PD "
    2585                 :            :                            "Req attempts)");
    2586                 :          0 :                 wpas_p2p_join_start(wpa_s);
    2587                 :          0 :                 return;
    2588                 :            :         }
    2589                 :            : 
    2590                 :          0 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
    2591                 :            :                        " p2p_dev_addr=" MACSTR " status=%d",
    2592                 :          0 :                        MAC2STR(peer), status);
    2593                 :            : 
    2594                 :          0 :         wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
    2595                 :            :                                             status, 0, 0);
    2596                 :            : }
    2597                 :            : 
    2598                 :            : 
    2599                 :         27 : static int freq_included(const struct p2p_channels *channels, unsigned int freq)
    2600                 :            : {
    2601         [ +  + ]:         27 :         if (channels == NULL)
    2602                 :         21 :                 return 1; /* Assume no restrictions */
    2603                 :         27 :         return p2p_channels_includes_freq(channels, freq);
    2604                 :            : 
    2605                 :            : }
    2606                 :            : 
    2607                 :            : 
    2608                 :         11 : static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
    2609                 :            :                                   const u8 *go_dev_addr, const u8 *ssid,
    2610                 :            :                                   size_t ssid_len, int *go, u8 *group_bssid,
    2611                 :            :                                   int *force_freq, int persistent_group,
    2612                 :            :                                   const struct p2p_channels *channels)
    2613                 :            : {
    2614                 :         11 :         struct wpa_supplicant *wpa_s = ctx;
    2615                 :            :         struct wpa_ssid *s;
    2616                 :            :         int res;
    2617                 :            :         struct wpa_supplicant *grp;
    2618                 :            : 
    2619         [ +  + ]:         11 :         if (!persistent_group) {
    2620                 :          4 :                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
    2621                 :         24 :                            " to join an active group", MAC2STR(sa));
    2622 [ +  + ][ -  + ]:          4 :                 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
    2623                 :          1 :                     (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
    2624         [ #  # ]:          0 :                      == 0 ||
    2625                 :          0 :                      os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
    2626                 :          1 :                         wpa_printf(MSG_DEBUG, "P2P: Accept previously "
    2627                 :            :                                    "authorized invitation");
    2628                 :          1 :                         goto accept_inv;
    2629                 :            :                 }
    2630                 :            :                 /*
    2631                 :            :                  * Do not accept the invitation automatically; notify user and
    2632                 :            :                  * request approval.
    2633                 :            :                  */
    2634                 :          3 :                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
    2635                 :            :         }
    2636                 :            : 
    2637                 :          7 :         grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
    2638         [ -  + ]:          7 :         if (grp) {
    2639                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
    2640                 :            :                            "running persistent group");
    2641         [ #  # ]:          0 :                 if (*go)
    2642                 :          0 :                         os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
    2643                 :          0 :                 goto accept_inv;
    2644                 :            :         }
    2645                 :            : 
    2646 [ +  + ][ +  - ]:          7 :         if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
    2647                 :          3 :             os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
    2648                 :          3 :                 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
    2649                 :            :                            "invitation to re-invoke a persistent group");
    2650         [ -  + ]:          4 :         } else if (!wpa_s->conf->persistent_reconnect)
    2651                 :          0 :                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
    2652                 :            : 
    2653         [ +  - ]:          7 :         for (s = wpa_s->conf->ssid; s; s = s->next) {
    2654 [ +  - ][ +  - ]:          7 :                 if (s->disabled == 2 &&
    2655         [ +  - ]:          7 :                     os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
    2656         [ +  - ]:          7 :                     s->ssid_len == ssid_len &&
    2657                 :          7 :                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
    2658                 :          7 :                         break;
    2659                 :            :         }
    2660                 :            : 
    2661         [ -  + ]:          7 :         if (!s) {
    2662                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
    2663                 :            :                            " requested reinvocation of an unknown group",
    2664                 :          0 :                            MAC2STR(sa));
    2665                 :          0 :                 return P2P_SC_FAIL_UNKNOWN_GROUP;
    2666                 :            :         }
    2667                 :            : 
    2668 [ +  + ][ +  - ]:          7 :         if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
    2669                 :          4 :                 *go = 1;
    2670         [ -  + ]:          4 :                 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
    2671                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: The only available "
    2672                 :            :                                    "interface is already in use - reject "
    2673                 :            :                                    "invitation");
    2674                 :          0 :                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
    2675                 :            :                 }
    2676                 :          4 :                 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
    2677         [ -  + ]:          3 :         } else if (s->mode == WPAS_MODE_P2P_GO) {
    2678                 :          0 :                 *go = 1;
    2679         [ #  # ]:          0 :                 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
    2680                 :            :                 {
    2681                 :          0 :                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
    2682                 :            :                                    "interface address for the group");
    2683                 :          0 :                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
    2684                 :            :                 }
    2685                 :          0 :                 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
    2686                 :            :                           ETH_ALEN);
    2687                 :            :         }
    2688                 :            : 
    2689                 :            : accept_inv:
    2690                 :          8 :         wpas_p2p_set_own_freq_preference(wpa_s, 0);
    2691                 :            : 
    2692                 :            :         /* Get one of the frequencies currently in use */
    2693         [ -  + ]:          8 :         if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
    2694                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
    2695                 :          0 :                 wpas_p2p_set_own_freq_preference(wpa_s, res);
    2696                 :            : 
    2697   [ #  #  #  # ]:          0 :                 if (wpa_s->num_multichan_concurrent < 2 ||
    2698                 :          0 :                     wpas_p2p_num_unused_channels(wpa_s) < 1) {
    2699                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
    2700                 :          0 :                         *force_freq = res;
    2701                 :            :                 }
    2702                 :            :         }
    2703                 :            : 
    2704         [ -  + ]:          8 :         if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
           [ #  #  #  # ]
    2705                 :          0 :             wpas_p2p_num_unused_channels(wpa_s) > 0) {
    2706         [ #  # ]:          0 :                 if (*go == 0) {
    2707                 :            :                         /* We are the client */
    2708                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
    2709                 :            :                                    "running a GO but we are capable of MCC, "
    2710                 :            :                                    "figure out the best channel to use");
    2711                 :          0 :                         *force_freq = 0;
    2712         [ #  # ]:          0 :                 } else if (!freq_included(channels, *force_freq)) {
    2713                 :            :                         /* We are the GO, and *force_freq is not in the
    2714                 :            :                          * intersection */
    2715                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
    2716                 :            :                                    "in intersection but we are capable of MCC, "
    2717                 :            :                                    "figure out the best channel to use",
    2718                 :            :                                    *force_freq);
    2719                 :          0 :                         *force_freq = 0;
    2720                 :            :                 }
    2721                 :            :         }
    2722                 :            : 
    2723                 :         11 :         return P2P_SC_SUCCESS;
    2724                 :            : }
    2725                 :            : 
    2726                 :            : 
    2727                 :         11 : static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
    2728                 :            :                                      const u8 *ssid, size_t ssid_len,
    2729                 :            :                                      const u8 *go_dev_addr, u8 status,
    2730                 :            :                                      int op_freq)
    2731                 :            : {
    2732                 :         11 :         struct wpa_supplicant *wpa_s = ctx;
    2733                 :            :         struct wpa_ssid *s;
    2734                 :            : 
    2735         [ +  + ]:         11 :         for (s = wpa_s->conf->ssid; s; s = s->next) {
    2736 [ +  - ][ +  - ]:          8 :                 if (s->disabled == 2 &&
    2737         [ +  - ]:          8 :                     s->ssid_len == ssid_len &&
    2738                 :          8 :                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
    2739                 :          8 :                         break;
    2740                 :            :         }
    2741                 :            : 
    2742         [ +  + ]:         11 :         if (status == P2P_SC_SUCCESS) {
    2743                 :          8 :                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
    2744                 :            :                            " was accepted; op_freq=%d MHz",
    2745                 :         48 :                            MAC2STR(sa), op_freq);
    2746         [ +  + ]:          8 :                 if (s) {
    2747                 :          7 :                         int go = s->mode == WPAS_MODE_P2P_GO;
    2748 [ +  + ][ +  + ]:          7 :                         wpas_p2p_group_add_persistent(
    2749                 :            :                                 wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
    2750                 :            :                                 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
    2751         [ +  - ]:          1 :                 } else if (bssid) {
    2752                 :          1 :                         wpa_s->user_initiated_pd = 0;
    2753                 :          1 :                         wpas_p2p_join(wpa_s, bssid, go_dev_addr,
    2754                 :          1 :                                       wpa_s->p2p_wps_method, 0);
    2755                 :            :                 }
    2756                 :          8 :                 return;
    2757                 :            :         }
    2758                 :            : 
    2759         [ -  + ]:          3 :         if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
    2760                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
    2761                 :          0 :                            " was rejected (status %u)", MAC2STR(sa), status);
    2762                 :          0 :                 return;
    2763                 :            :         }
    2764                 :            : 
    2765         [ +  + ]:          3 :         if (!s) {
    2766         [ +  - ]:          2 :                 if (bssid) {
    2767                 :          2 :                         wpa_msg_global(wpa_s, MSG_INFO,
    2768                 :            :                                        P2P_EVENT_INVITATION_RECEIVED
    2769                 :            :                                        "sa=" MACSTR " go_dev_addr=" MACSTR
    2770                 :            :                                        " bssid=" MACSTR " unknown-network",
    2771                 :         24 :                                        MAC2STR(sa), MAC2STR(go_dev_addr),
    2772                 :         12 :                                        MAC2STR(bssid));
    2773                 :            :                 } else {
    2774                 :          0 :                         wpa_msg_global(wpa_s, MSG_INFO,
    2775                 :            :                                        P2P_EVENT_INVITATION_RECEIVED
    2776                 :            :                                        "sa=" MACSTR " go_dev_addr=" MACSTR
    2777                 :            :                                        " unknown-network",
    2778                 :          0 :                                        MAC2STR(sa), MAC2STR(go_dev_addr));
    2779                 :            :                 }
    2780                 :          2 :                 return;
    2781                 :            :         }
    2782                 :            : 
    2783 [ -  + ][ #  # ]:          1 :         if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
    2784                 :          0 :                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
    2785                 :            :                                "sa=" MACSTR " persistent=%d freq=%d",
    2786                 :          0 :                                MAC2STR(sa), s->id, op_freq);
    2787                 :            :         } else {
    2788                 :         11 :                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
    2789                 :            :                                "sa=" MACSTR " persistent=%d",
    2790                 :          6 :                                MAC2STR(sa), s->id);
    2791                 :            :         }
    2792                 :            : }
    2793                 :            : 
    2794                 :            : 
    2795                 :          3 : static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
    2796                 :            :                                         struct wpa_ssid *ssid,
    2797                 :            :                                         const u8 *peer, int inv)
    2798                 :            : {
    2799                 :            :         size_t i;
    2800                 :            : 
    2801         [ -  + ]:          3 :         if (ssid == NULL)
    2802                 :          0 :                 return;
    2803                 :            : 
    2804 [ +  - ][ +  - ]:          4 :         for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
    2805         [ +  + ]:          4 :                 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
    2806                 :            :                               ETH_ALEN) == 0)
    2807                 :          3 :                         break;
    2808                 :            :         }
    2809         [ -  + ]:          3 :         if (i >= ssid->num_p2p_clients) {
    2810 [ #  # ][ #  # ]:          0 :                 if (ssid->mode != WPAS_MODE_P2P_GO &&
    2811                 :          0 :                     os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
    2812                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
    2813                 :            :                                    "due to invitation result", ssid->id);
    2814                 :          0 :                         wpas_notify_network_removed(wpa_s, ssid);
    2815                 :          0 :                         wpa_config_remove_network(wpa_s->conf, ssid->id);
    2816                 :          0 :                         return;
    2817                 :            :                 }
    2818                 :          0 :                 return; /* Peer not found in client list */
    2819                 :            :         }
    2820                 :            : 
    2821         [ -  + ]:          3 :         wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
    2822                 :            :                    "group %d client list%s",
    2823                 :         18 :                    MAC2STR(peer), ssid->id,
    2824                 :            :                    inv ? " due to invitation result" : "");
    2825                 :          3 :         os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
    2826                 :            :                    ssid->p2p_client_list + (i + 1) * ETH_ALEN,
    2827                 :            :                    (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
    2828                 :          3 :         ssid->num_p2p_clients--;
    2829                 :            : #ifndef CONFIG_NO_CONFIG_WRITE
    2830   [ -  +  #  # ]:          3 :         if (wpa_s->parent->conf->update_config &&
    2831                 :          0 :             wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
    2832                 :          3 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
    2833                 :            : #endif /* CONFIG_NO_CONFIG_WRITE */
    2834                 :            : }
    2835                 :            : 
    2836                 :            : 
    2837                 :          0 : static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
    2838                 :            :                                           const u8 *peer)
    2839                 :            : {
    2840                 :            :         struct wpa_ssid *ssid;
    2841                 :            : 
    2842                 :          0 :         wpa_s = wpa_s->global->p2p_invite_group;
    2843         [ #  # ]:          0 :         if (wpa_s == NULL)
    2844                 :          0 :                 return; /* No known invitation group */
    2845                 :          0 :         ssid = wpa_s->current_ssid;
    2846 [ #  # ][ #  # ]:          0 :         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
                 [ #  # ]
    2847                 :          0 :             !ssid->p2p_persistent_group)
    2848                 :          0 :                 return; /* Not operating as a GO in persistent group */
    2849                 :          0 :         ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
    2850                 :          0 :                                        ssid->ssid, ssid->ssid_len);
    2851                 :          0 :         wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
    2852                 :            : }
    2853                 :            : 
    2854                 :            : 
    2855                 :         11 : static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
    2856                 :            :                                    const struct p2p_channels *channels,
    2857                 :            :                                    const u8 *peer, int neg_freq)
    2858                 :            : {
    2859                 :         11 :         struct wpa_supplicant *wpa_s = ctx;
    2860                 :            :         struct wpa_ssid *ssid;
    2861                 :            :         int freq;
    2862                 :            : 
    2863         [ +  + ]:         11 :         if (bssid) {
    2864                 :          4 :                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
    2865                 :            :                                "status=%d " MACSTR,
    2866                 :         24 :                                status, MAC2STR(bssid));
    2867                 :            :         } else {
    2868                 :          7 :                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
    2869                 :            :                                "status=%d ", status);
    2870                 :            :         }
    2871                 :         11 :         wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
    2872                 :            : 
    2873                 :         11 :         wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
    2874                 :         66 :                    status, MAC2STR(peer));
    2875         [ +  + ]:         11 :         if (wpa_s->pending_invite_ssid_id == -1) {
    2876         [ -  + ]:          4 :                 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
    2877                 :          0 :                         wpas_remove_persistent_client(wpa_s, peer);
    2878                 :          4 :                 return; /* Invitation to active group */
    2879                 :            :         }
    2880                 :            : 
    2881         [ -  + ]:          7 :         if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
    2882                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
    2883                 :            :                            "invitation exchange to indicate readiness for "
    2884                 :            :                            "re-invocation");
    2885                 :            :         }
    2886                 :            : 
    2887         [ -  + ]:          7 :         if (status != P2P_SC_SUCCESS) {
    2888         [ #  # ]:          0 :                 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
    2889                 :          0 :                         ssid = wpa_config_get_network(
    2890                 :            :                                 wpa_s->conf, wpa_s->pending_invite_ssid_id);
    2891                 :          0 :                         wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
    2892                 :            :                 }
    2893                 :          0 :                 wpas_p2p_remove_pending_group_interface(wpa_s);
    2894                 :          0 :                 return;
    2895                 :            :         }
    2896                 :            : 
    2897                 :          7 :         ssid = wpa_config_get_network(wpa_s->conf,
    2898                 :            :                                       wpa_s->pending_invite_ssid_id);
    2899         [ -  + ]:          7 :         if (ssid == NULL) {
    2900                 :          0 :                 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
    2901                 :            :                            "data matching with invitation");
    2902                 :          0 :                 return;
    2903                 :            :         }
    2904                 :            : 
    2905                 :            :         /*
    2906                 :            :          * The peer could have missed our ctrl::ack frame for Invitation
    2907                 :            :          * Response and continue retransmitting the frame. To reduce the
    2908                 :            :          * likelihood of the peer not getting successful TX status for the
    2909                 :            :          * Invitation Response frame, wait a short time here before starting
    2910                 :            :          * the persistent group so that we will remain on the current channel to
    2911                 :            :          * acknowledge any possible retransmission from the peer.
    2912                 :            :          */
    2913                 :          7 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
    2914                 :            :                 "starting persistent group");
    2915                 :          7 :         os_sleep(0, 50000);
    2916                 :            : 
    2917                 :          7 :         freq = wpa_s->p2p_persistent_go_freq;
    2918         [ +  - ]:         10 :         if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
           [ +  +  +  - ]
    2919                 :          3 :             freq_included(channels, neg_freq)) {
    2920                 :          3 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode",
    2921                 :            :                         neg_freq);
    2922                 :          3 :                 freq = neg_freq;
    2923                 :            :         }
    2924                 :            : 
    2925         [ +  + ]:         11 :         wpas_p2p_group_add_persistent(wpa_s, ssid,
    2926                 :          7 :                                       ssid->mode == WPAS_MODE_P2P_GO,
    2927                 :            :                                       freq,
    2928                 :         14 :                                       wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
    2929                 :            :                                       channels,
    2930                 :          7 :                                       ssid->mode == WPAS_MODE_P2P_GO ?
    2931                 :            :                                       P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
    2932                 :            :                                       0);
    2933                 :            : }
    2934                 :            : 
    2935                 :            : 
    2936                 :      75249 : static int wpas_p2p_disallowed_freq(struct wpa_global *global,
    2937                 :            :                                     unsigned int freq)
    2938                 :            : {
    2939                 :      75249 :         return freq_range_list_includes(&global->p2p_disallow_freq, freq);
    2940                 :            : }
    2941                 :            : 
    2942                 :            : 
    2943                 :          0 : static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
    2944                 :            : {
    2945                 :          0 :         reg->channel[reg->channels] = chan;
    2946                 :          0 :         reg->channels++;
    2947                 :          0 : }
    2948                 :            : 
    2949                 :            : 
    2950                 :          0 : static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
    2951                 :            :                                      struct p2p_channels *chan,
    2952                 :            :                                      struct p2p_channels *cli_chan)
    2953                 :            : {
    2954                 :          0 :         int i, cla = 0;
    2955                 :            : 
    2956                 :          0 :         os_memset(cli_chan, 0, sizeof(*cli_chan));
    2957                 :            : 
    2958                 :          0 :         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
    2959                 :            :                    "band");
    2960                 :            : 
    2961                 :            :         /* Operating class 81 - 2.4 GHz band channels 1..13 */
    2962                 :          0 :         chan->reg_class[cla].reg_class = 81;
    2963                 :          0 :         chan->reg_class[cla].channels = 0;
    2964         [ #  # ]:          0 :         for (i = 0; i < 11; i++) {
    2965         [ #  # ]:          0 :                 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
    2966                 :          0 :                         wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
    2967                 :            :         }
    2968         [ #  # ]:          0 :         if (chan->reg_class[cla].channels)
    2969                 :          0 :                 cla++;
    2970                 :            : 
    2971                 :          0 :         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
    2972                 :            :                    "band");
    2973                 :            : 
    2974                 :            :         /* Operating class 115 - 5 GHz, channels 36-48 */
    2975                 :          0 :         chan->reg_class[cla].reg_class = 115;
    2976                 :          0 :         chan->reg_class[cla].channels = 0;
    2977         [ #  # ]:          0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
    2978                 :          0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
    2979         [ #  # ]:          0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
    2980                 :          0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
    2981         [ #  # ]:          0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
    2982                 :          0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
    2983         [ #  # ]:          0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
    2984                 :          0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
    2985         [ #  # ]:          0 :         if (chan->reg_class[cla].channels)
    2986                 :          0 :                 cla++;
    2987                 :            : 
    2988                 :          0 :         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
    2989                 :            :                    "band");
    2990                 :            : 
    2991                 :            :         /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
    2992                 :          0 :         chan->reg_class[cla].reg_class = 124;
    2993                 :          0 :         chan->reg_class[cla].channels = 0;
    2994         [ #  # ]:          0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
    2995                 :          0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
    2996         [ #  # ]:          0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
    2997                 :          0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
    2998         [ #  # ]:          0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
    2999                 :          0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
    3000         [ #  # ]:          0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
    3001                 :          0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
    3002         [ #  # ]:          0 :         if (chan->reg_class[cla].channels)
    3003                 :          0 :                 cla++;
    3004                 :            : 
    3005                 :          0 :         chan->reg_classes = cla;
    3006                 :          0 :         return 0;
    3007                 :            : }
    3008                 :            : 
    3009                 :            : 
    3010                 :       7920 : static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
    3011                 :            :                                           u16 num_modes,
    3012                 :            :                                           enum hostapd_hw_mode mode)
    3013                 :            : {
    3014                 :            :         u16 i;
    3015                 :            : 
    3016         [ +  - ]:      14850 :         for (i = 0; i < num_modes; i++) {
    3017         [ +  + ]:      14850 :                 if (modes[i].mode == mode)
    3018                 :       7920 :                         return &modes[i];
    3019                 :            :         }
    3020                 :            : 
    3021                 :       7920 :         return NULL;
    3022                 :            : }
    3023                 :            : 
    3024                 :            : 
    3025                 :            : enum chan_allowed {
    3026                 :            :         NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
    3027                 :            : };
    3028                 :            : 
    3029                 :      75240 : static int has_channel(struct wpa_global *global,
    3030                 :            :                        struct hostapd_hw_modes *mode, u8 chan, int *flags)
    3031                 :            : {
    3032                 :            :         int i;
    3033                 :            :         unsigned int freq;
    3034                 :            : 
    3035         [ +  + ]:      75240 :         freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
    3036                 :      75240 :                 chan * 5;
    3037         [ -  + ]:      75240 :         if (wpas_p2p_disallowed_freq(global, freq))
    3038                 :          0 :                 return NOT_ALLOWED;
    3039                 :            : 
    3040         [ +  + ]:     948420 :         for (i = 0; i < mode->num_channels; i++) {
    3041         [ +  + ]:     935550 :                 if (mode->channels[i].chan == chan) {
    3042         [ +  + ]:      62370 :                         if (flags)
    3043                 :      54450 :                                 *flags = mode->channels[i].flag;
    3044         [ +  + ]:      62370 :                         if (mode->channels[i].flag &
    3045                 :            :                             (HOSTAPD_CHAN_DISABLED |
    3046                 :            :                              HOSTAPD_CHAN_RADAR))
    3047                 :      14850 :                                 return NOT_ALLOWED;
    3048         [ +  + ]:      47520 :                         if (mode->channels[i].flag &
    3049                 :            :                             (HOSTAPD_CHAN_PASSIVE_SCAN |
    3050                 :            :                              HOSTAPD_CHAN_NO_IBSS))
    3051                 :      36630 :                                 return PASSIVE_ONLY;
    3052                 :      10890 :                         return ALLOWED;
    3053                 :            :                 }
    3054                 :            :         }
    3055                 :            : 
    3056                 :      75240 :         return NOT_ALLOWED;
    3057                 :            : }
    3058                 :            : 
    3059                 :            : 
    3060                 :            : struct p2p_oper_class_map {
    3061                 :            :         enum hostapd_hw_mode mode;
    3062                 :            :         u8 op_class;
    3063                 :            :         u8 min_chan;
    3064                 :            :         u8 max_chan;
    3065                 :            :         u8 inc;
    3066                 :            :         enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
    3067                 :            : };
    3068                 :            : 
    3069                 :            : static struct p2p_oper_class_map op_class[] = {
    3070                 :            :         { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
    3071                 :            : #if 0 /* Do not enable HT40 on 2 GHz for now */
    3072                 :            :         { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
    3073                 :            :         { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
    3074                 :            : #endif
    3075                 :            :         { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
    3076                 :            :         { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
    3077                 :            :         { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
    3078                 :            :         { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
    3079                 :            :         { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
    3080                 :            :         { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
    3081                 :            : 
    3082                 :            :         /*
    3083                 :            :          * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
    3084                 :            :          * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
    3085                 :            :          * 80 MHz, but currently use the following definition for simplicity
    3086                 :            :          * (these center frequencies are not actual channels, which makes
    3087                 :            :          * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
    3088                 :            :          * removing invalid channels.
    3089                 :            :          */
    3090                 :            :         { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
    3091                 :            :         { -1, 0, 0, 0, 0, BW20 }
    3092                 :            : };
    3093                 :            : 
    3094                 :            : 
    3095                 :      31680 : static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
    3096                 :            :                                      struct hostapd_hw_modes *mode,
    3097                 :            :                                      u8 channel)
    3098                 :            : {
    3099                 :      31680 :         u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
    3100                 :            :         unsigned int i;
    3101                 :            : 
    3102         [ -  + ]:      31680 :         if (mode->mode != HOSTAPD_MODE_IEEE80211A)
    3103                 :          0 :                 return 0;
    3104                 :            : 
    3105         [ +  + ]:     139590 :         for (i = 0; i < ARRAY_SIZE(center_channels); i++)
    3106                 :            :                 /*
    3107                 :            :                  * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
    3108                 :            :                  * so the center channel is 6 channels away from the start/end.
    3109                 :            :                  */
    3110 [ +  + ][ +  + ]:     130680 :                 if (channel >= center_channels[i] - 6 &&
    3111                 :      98010 :                     channel <= center_channels[i] + 6)
    3112                 :      22770 :                         return center_channels[i];
    3113                 :            : 
    3114                 :      31680 :         return 0;
    3115                 :            : }
    3116                 :            : 
    3117                 :            : 
    3118                 :      31680 : static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
    3119                 :            :                                                struct hostapd_hw_modes *mode,
    3120                 :            :                                                u8 channel, u8 bw)
    3121                 :            : {
    3122                 :            :         u8 center_chan;
    3123                 :            :         int i, flags;
    3124                 :      31680 :         enum chan_allowed res, ret = ALLOWED;
    3125                 :            : 
    3126                 :      31680 :         center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
    3127         [ +  + ]:      31680 :         if (!center_chan)
    3128                 :       8910 :                 return NOT_ALLOWED;
    3129 [ +  + ][ +  + ]:      22770 :         if (center_chan >= 58 && center_chan <= 138)
    3130                 :      15840 :                 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
    3131                 :            : 
    3132                 :            :         /* check all the channels are available */
    3133         [ +  - ]:       6930 :         for (i = 0; i < 4; i++) {
    3134                 :       6930 :                 int adj_chan = center_chan - 6 + i * 4;
    3135                 :            : 
    3136                 :       6930 :                 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
    3137         [ -  + ]:       6930 :                 if (res == NOT_ALLOWED)
    3138                 :          0 :                         return NOT_ALLOWED;
    3139         [ +  - ]:       6930 :                 if (res == PASSIVE_ONLY)
    3140                 :       6930 :                         ret = PASSIVE_ONLY;
    3141                 :            : 
    3142 [ +  - ][ +  - ]:       6930 :                 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
    3143                 :       6930 :                         return NOT_ALLOWED;
    3144 [ #  # ][ #  # ]:          0 :                 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
    3145                 :          0 :                         return NOT_ALLOWED;
    3146 [ #  # ][ #  # ]:          0 :                 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
    3147                 :          0 :                         return NOT_ALLOWED;
    3148 [ #  # ][ #  # ]:          0 :                 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
    3149                 :          0 :                         return NOT_ALLOWED;
    3150                 :            :         }
    3151                 :            : 
    3152                 :      31680 :         return ret;
    3153                 :            : }
    3154                 :            : 
    3155                 :            : 
    3156                 :      60390 : static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
    3157                 :            :                                                  struct hostapd_hw_modes *mode,
    3158                 :            :                                                  u8 channel, u8 bw)
    3159                 :            : {
    3160                 :            :         int flag;
    3161                 :            :         enum chan_allowed res, res2;
    3162                 :            : 
    3163                 :      60390 :         res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
    3164         [ +  + ]:      60390 :         if (bw == BW40MINUS) {
    3165         [ -  + ]:       3960 :                 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
    3166                 :          0 :                         return NOT_ALLOWED;
    3167                 :       3960 :                 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
    3168         [ +  + ]:      56430 :         } else if (bw == BW40PLUS) {
    3169         [ -  + ]:       3960 :                 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
    3170                 :          0 :                         return NOT_ALLOWED;
    3171                 :       3960 :                 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
    3172         [ +  + ]:      52470 :         } else if (bw == BW80) {
    3173                 :      31680 :                 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
    3174                 :            :         }
    3175                 :            : 
    3176 [ +  + ][ +  + ]:      60390 :         if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
    3177                 :      31680 :                 return NOT_ALLOWED;
    3178 [ +  + ][ -  + ]:      28710 :         if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
    3179                 :      17820 :                 return PASSIVE_ONLY;
    3180                 :      60390 :         return res;
    3181                 :            : }
    3182                 :            : 
    3183                 :            : 
    3184                 :        990 : static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
    3185                 :            :                                    struct p2p_channels *chan,
    3186                 :            :                                    struct p2p_channels *cli_chan)
    3187                 :            : {
    3188                 :            :         struct hostapd_hw_modes *mode;
    3189                 :            :         int cla, op, cli_cla;
    3190                 :            : 
    3191         [ -  + ]:        990 :         if (wpa_s->hw.modes == NULL) {
    3192                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
    3193                 :            :                            "of all supported channels; assume dualband "
    3194                 :            :                            "support");
    3195                 :          0 :                 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
    3196                 :            :         }
    3197                 :            : 
    3198                 :        990 :         cla = cli_cla = 0;
    3199                 :            : 
    3200         [ +  + ]:       8910 :         for (op = 0; op_class[op].op_class; op++) {
    3201                 :       7920 :                 struct p2p_oper_class_map *o = &op_class[op];
    3202                 :            :                 u8 ch;
    3203                 :       7920 :                 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
    3204                 :            : 
    3205                 :       7920 :                 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
    3206         [ -  + ]:       7920 :                 if (mode == NULL)
    3207                 :          0 :                         continue;
    3208         [ +  + ]:      68310 :                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
    3209                 :            :                         enum chan_allowed res;
    3210                 :      60390 :                         res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
    3211         [ +  + ]:      60390 :                         if (res == ALLOWED) {
    3212         [ +  + ]:      10890 :                                 if (reg == NULL) {
    3213                 :        990 :                                         wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
    3214                 :        990 :                                                    o->op_class);
    3215                 :        990 :                                         reg = &chan->reg_class[cla];
    3216                 :        990 :                                         cla++;
    3217                 :        990 :                                         reg->reg_class = o->op_class;
    3218                 :            :                                 }
    3219                 :      10890 :                                 reg->channel[reg->channels] = ch;
    3220                 :      10890 :                                 reg->channels++;
    3221 [ +  + ][ +  + ]:      49500 :                         } else if (res == PASSIVE_ONLY &&
    3222                 :      17820 :                                    wpa_s->conf->p2p_add_cli_chan) {
    3223         [ +  + ]:        252 :                                 if (cli_reg == NULL) {
    3224                 :         98 :                                         wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
    3225                 :         98 :                                                    o->op_class);
    3226                 :         98 :                                         cli_reg = &cli_chan->reg_class[cli_cla];
    3227                 :         98 :                                         cli_cla++;
    3228                 :         98 :                                         cli_reg->reg_class = o->op_class;
    3229                 :            :                                 }
    3230                 :        252 :                                 cli_reg->channel[cli_reg->channels] = ch;
    3231                 :        252 :                                 cli_reg->channels++;
    3232                 :            :                         }
    3233                 :            :                 }
    3234         [ +  + ]:       7920 :                 if (reg) {
    3235                 :        990 :                         wpa_hexdump(MSG_DEBUG, "P2P: Channels",
    3236                 :        990 :                                     reg->channel, reg->channels);
    3237                 :            :                 }
    3238         [ +  + ]:       7920 :                 if (cli_reg) {
    3239                 :         98 :                         wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
    3240                 :         98 :                                     cli_reg->channel, cli_reg->channels);
    3241                 :            :                 }
    3242                 :            :         }
    3243                 :            : 
    3244                 :        990 :         chan->reg_classes = cla;
    3245                 :        990 :         cli_chan->reg_classes = cli_cla;
    3246                 :            : 
    3247                 :        990 :         return 0;
    3248                 :            : }
    3249                 :            : 
    3250                 :            : 
    3251                 :          0 : int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
    3252                 :            :                            struct hostapd_hw_modes *mode, u8 channel)
    3253                 :            : {
    3254                 :            :         int op;
    3255                 :            :         enum chan_allowed ret;
    3256                 :            : 
    3257         [ #  # ]:          0 :         for (op = 0; op_class[op].op_class; op++) {
    3258                 :          0 :                 struct p2p_oper_class_map *o = &op_class[op];
    3259                 :            :                 u8 ch;
    3260                 :            : 
    3261         [ #  # ]:          0 :                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
    3262 [ #  # ][ #  # ]:          0 :                         if (o->mode != HOSTAPD_MODE_IEEE80211A ||
    3263         [ #  # ]:          0 :                             o->bw == BW20 || ch != channel)
    3264                 :          0 :                                 continue;
    3265                 :          0 :                         ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
    3266         [ #  # ]:          0 :                         if (ret == ALLOWED)
    3267         [ #  # ]:          0 :                                 return (o->bw == BW40MINUS) ? -1 : 1;
    3268                 :            :                 }
    3269                 :            :         }
    3270                 :          0 :         return 0;
    3271                 :            : }
    3272                 :            : 
    3273                 :            : 
    3274                 :          0 : int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
    3275                 :            :                               struct hostapd_hw_modes *mode, u8 channel)
    3276                 :            : {
    3277         [ #  # ]:          0 :         if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
    3278                 :          0 :                 return 0;
    3279                 :            : 
    3280                 :          0 :         return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
    3281                 :            : }
    3282                 :            : 
    3283                 :            : 
    3284                 :          0 : static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
    3285                 :            :                         size_t buf_len)
    3286                 :            : {
    3287                 :          0 :         struct wpa_supplicant *wpa_s = ctx;
    3288                 :            : 
    3289         [ #  # ]:          0 :         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    3290         [ #  # ]:          0 :                 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
    3291                 :          0 :                         break;
    3292                 :            :         }
    3293         [ #  # ]:          0 :         if (wpa_s == NULL)
    3294                 :          0 :                 return -1;
    3295                 :            : 
    3296                 :          0 :         return wpa_drv_get_noa(wpa_s, buf, buf_len);
    3297                 :            : }
    3298                 :            : 
    3299                 :            : 
    3300                 :          0 : static int wpas_go_connected(void *ctx, const u8 *dev_addr)
    3301                 :            : {
    3302                 :          0 :         struct wpa_supplicant *wpa_s = ctx;
    3303                 :            : 
    3304         [ #  # ]:          0 :         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    3305                 :          0 :                 struct wpa_ssid *ssid = wpa_s->current_ssid;
    3306         [ #  # ]:          0 :                 if (ssid == NULL)
    3307                 :          0 :                         continue;
    3308         [ #  # ]:          0 :                 if (ssid->mode != WPAS_MODE_INFRA)
    3309                 :          0 :                         continue;
    3310 [ #  # ][ #  # ]:          0 :                 if (wpa_s->wpa_state != WPA_COMPLETED &&
    3311                 :          0 :                     wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
    3312                 :          0 :                         continue;
    3313         [ #  # ]:          0 :                 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
    3314                 :          0 :                         return 1;
    3315                 :            :         }
    3316                 :            : 
    3317                 :          0 :         return 0;
    3318                 :            : }
    3319                 :            : 
    3320                 :            : 
    3321                 :      17995 : static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
    3322                 :            : {
    3323                 :      17995 :         struct wpa_supplicant *wpa_s = ctx;
    3324                 :      17995 :         wpa_msg_global(wpa_s, level, "P2P: %s", msg);
    3325                 :      17995 : }
    3326                 :            : 
    3327                 :            : 
    3328                 :          0 : int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
    3329                 :            : {
    3330                 :            :         struct wpa_interface iface;
    3331                 :            :         struct wpa_supplicant *p2pdev_wpa_s;
    3332                 :            :         char ifname[100];
    3333                 :            :         char force_name[100];
    3334                 :            :         int ret;
    3335                 :            : 
    3336                 :          0 :         os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
    3337                 :          0 :                     wpa_s->ifname);
    3338                 :          0 :         force_name[0] = '\0';
    3339                 :          0 :         wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
    3340                 :          0 :         ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
    3341                 :          0 :                              force_name, wpa_s->pending_interface_addr, NULL);
    3342         [ #  # ]:          0 :         if (ret < 0) {
    3343                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
    3344                 :          0 :                 return ret;
    3345                 :            :         }
    3346                 :          0 :         os_strlcpy(wpa_s->pending_interface_name, ifname,
    3347                 :            :                    sizeof(wpa_s->pending_interface_name));
    3348                 :            : 
    3349                 :          0 :         os_memset(&iface, 0, sizeof(iface));
    3350                 :          0 :         iface.p2p_mgmt = 1;
    3351                 :          0 :         iface.ifname = wpa_s->pending_interface_name;
    3352                 :          0 :         iface.driver = wpa_s->driver->name;
    3353                 :          0 :         iface.driver_param = wpa_s->conf->driver_param;
    3354                 :          0 :         iface.confname = wpa_s->confname;
    3355                 :          0 :         p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
    3356         [ #  # ]:          0 :         if (!p2pdev_wpa_s) {
    3357                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
    3358                 :          0 :                 return -1;
    3359                 :            :         }
    3360                 :          0 :         p2pdev_wpa_s->parent = wpa_s;
    3361                 :            : 
    3362                 :          0 :         wpa_s->pending_interface_name[0] = '\0';
    3363                 :          0 :         return 0;
    3364                 :            : }
    3365                 :            : 
    3366                 :            : 
    3367                 :            : /**
    3368                 :            :  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
    3369                 :            :  * @global: Pointer to global data from wpa_supplicant_init()
    3370                 :            :  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
    3371                 :            :  * Returns: 0 on success, -1 on failure
    3372                 :            :  */
    3373                 :         22 : int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
    3374                 :            : {
    3375                 :            :         struct p2p_config p2p;
    3376                 :            :         unsigned int r;
    3377                 :            :         int i;
    3378                 :            : 
    3379         [ -  + ]:         22 :         if (wpa_s->conf->p2p_disabled)
    3380                 :          0 :                 return 0;
    3381                 :            : 
    3382         [ -  + ]:         22 :         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
    3383                 :          0 :                 return 0;
    3384                 :            : 
    3385         [ +  + ]:         22 :         if (global->p2p)
    3386                 :         19 :                 return 0;
    3387                 :            : 
    3388                 :          3 :         os_memset(&p2p, 0, sizeof(p2p));
    3389                 :          3 :         p2p.cb_ctx = wpa_s;
    3390                 :          3 :         p2p.debug_print = wpas_p2p_debug_print;
    3391                 :          3 :         p2p.p2p_scan = wpas_p2p_scan;
    3392                 :          3 :         p2p.send_action = wpas_send_action;
    3393                 :          3 :         p2p.send_action_done = wpas_send_action_done;
    3394                 :          3 :         p2p.go_neg_completed = wpas_go_neg_completed;
    3395                 :          3 :         p2p.go_neg_req_rx = wpas_go_neg_req_rx;
    3396                 :          3 :         p2p.dev_found = wpas_dev_found;
    3397                 :          3 :         p2p.dev_lost = wpas_dev_lost;
    3398                 :          3 :         p2p.find_stopped = wpas_find_stopped;
    3399                 :          3 :         p2p.start_listen = wpas_start_listen;
    3400                 :          3 :         p2p.stop_listen = wpas_stop_listen;
    3401                 :          3 :         p2p.send_probe_resp = wpas_send_probe_resp;
    3402                 :          3 :         p2p.sd_request = wpas_sd_request;
    3403                 :          3 :         p2p.sd_response = wpas_sd_response;
    3404                 :          3 :         p2p.prov_disc_req = wpas_prov_disc_req;
    3405                 :          3 :         p2p.prov_disc_resp = wpas_prov_disc_resp;
    3406                 :          3 :         p2p.prov_disc_fail = wpas_prov_disc_fail;
    3407                 :          3 :         p2p.invitation_process = wpas_invitation_process;
    3408                 :          3 :         p2p.invitation_received = wpas_invitation_received;
    3409                 :          3 :         p2p.invitation_result = wpas_invitation_result;
    3410                 :          3 :         p2p.get_noa = wpas_get_noa;
    3411                 :          3 :         p2p.go_connected = wpas_go_connected;
    3412                 :            : 
    3413                 :          3 :         os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
    3414                 :          3 :         os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
    3415                 :          3 :         p2p.dev_name = wpa_s->conf->device_name;
    3416                 :          3 :         p2p.manufacturer = wpa_s->conf->manufacturer;
    3417                 :          3 :         p2p.model_name = wpa_s->conf->model_name;
    3418                 :          3 :         p2p.model_number = wpa_s->conf->model_number;
    3419                 :          3 :         p2p.serial_number = wpa_s->conf->serial_number;
    3420         [ +  - ]:          3 :         if (wpa_s->wps) {
    3421                 :          3 :                 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
    3422                 :          3 :                 p2p.config_methods = wpa_s->wps->config_methods;
    3423                 :            :         }
    3424                 :            : 
    3425 [ -  + ][ #  # ]:          3 :         if (wpa_s->conf->p2p_listen_reg_class &&
    3426                 :          0 :             wpa_s->conf->p2p_listen_channel) {
    3427                 :          0 :                 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
    3428                 :          0 :                 p2p.channel = wpa_s->conf->p2p_listen_channel;
    3429                 :            :         } else {
    3430                 :          3 :                 p2p.reg_class = 81;
    3431                 :            :                 /*
    3432                 :            :                  * Pick one of the social channels randomly as the listen
    3433                 :            :                  * channel.
    3434                 :            :                  */
    3435                 :          3 :                 os_get_random((u8 *) &r, sizeof(r));
    3436                 :          3 :                 p2p.channel = 1 + (r % 3) * 5;
    3437                 :            :         }
    3438                 :          3 :         wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
    3439                 :            : 
    3440 [ -  + ][ #  # ]:          3 :         if (wpa_s->conf->p2p_oper_reg_class &&
    3441                 :          0 :             wpa_s->conf->p2p_oper_channel) {
    3442                 :          0 :                 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
    3443                 :          0 :                 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
    3444                 :          0 :                 p2p.cfg_op_channel = 1;
    3445                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
    3446                 :          0 :                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
    3447                 :            : 
    3448                 :            :         } else {
    3449                 :          3 :                 p2p.op_reg_class = 81;
    3450                 :            :                 /*
    3451                 :            :                  * Use random operation channel from (1, 6, 11) if no other
    3452                 :            :                  * preference is indicated.
    3453                 :            :                  */
    3454                 :          3 :                 os_get_random((u8 *) &r, sizeof(r));
    3455                 :          3 :                 p2p.op_channel = 1 + (r % 3) * 5;
    3456                 :          3 :                 p2p.cfg_op_channel = 0;
    3457                 :          3 :                 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
    3458                 :          6 :                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
    3459                 :            :         }
    3460                 :            : 
    3461 [ -  + ][ #  # ]:          3 :         if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
    3462                 :          0 :                 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
    3463                 :          0 :                 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
    3464                 :            :         }
    3465                 :            : 
    3466 [ -  + ][ #  # ]:          3 :         if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
    3467                 :          0 :                 os_memcpy(p2p.country, wpa_s->conf->country, 2);
    3468                 :          0 :                 p2p.country[2] = 0x04;
    3469                 :            :         } else
    3470                 :          3 :                 os_memcpy(p2p.country, "XX\x04", 3);
    3471                 :            : 
    3472         [ -  + ]:          3 :         if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
    3473                 :          0 :                 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
    3474                 :            :                            "channel list");
    3475                 :          0 :                 return -1;
    3476                 :            :         }
    3477                 :            : 
    3478                 :          3 :         os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
    3479                 :            :                   WPS_DEV_TYPE_LEN);
    3480                 :            : 
    3481                 :          3 :         p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
    3482                 :          3 :         os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
    3483                 :            :                   p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
    3484                 :            : 
    3485                 :          3 :         p2p.concurrent_operations = !!(wpa_s->drv_flags &
    3486                 :            :                                        WPA_DRIVER_FLAGS_P2P_CONCURRENT);
    3487                 :            : 
    3488                 :          3 :         p2p.max_peers = 100;
    3489                 :            : 
    3490         [ -  + ]:          3 :         if (wpa_s->conf->p2p_ssid_postfix) {
    3491                 :          0 :                 p2p.ssid_postfix_len =
    3492                 :          0 :                         os_strlen(wpa_s->conf->p2p_ssid_postfix);
    3493         [ #  # ]:          0 :                 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
    3494                 :          0 :                         p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
    3495                 :          0 :                 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
    3496                 :            :                           p2p.ssid_postfix_len);
    3497                 :            :         }
    3498                 :            : 
    3499                 :          3 :         p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
    3500                 :            : 
    3501                 :          3 :         p2p.max_listen = wpa_s->max_remain_on_chan;
    3502                 :            : 
    3503                 :          3 :         global->p2p = p2p_init(&p2p);
    3504         [ -  + ]:          3 :         if (global->p2p == NULL)
    3505                 :          0 :                 return -1;
    3506                 :          3 :         global->p2p_init_wpa_s = wpa_s;
    3507                 :            : 
    3508         [ +  + ]:         33 :         for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
    3509         [ +  - ]:         30 :                 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
    3510                 :         30 :                         continue;
    3511                 :          0 :                 p2p_add_wps_vendor_extension(
    3512                 :          0 :                         global->p2p, wpa_s->conf->wps_vendor_ext[i]);
    3513                 :            :         }
    3514                 :            : 
    3515                 :          3 :         p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
    3516                 :            : 
    3517                 :         22 :         return 0;
    3518                 :            : }
    3519                 :            : 
    3520                 :            : 
    3521                 :            : /**
    3522                 :            :  * wpas_p2p_deinit - Deinitialize per-interface P2P data
    3523                 :            :  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
    3524                 :            :  *
    3525                 :            :  * This function deinitialize per-interface P2P data.
    3526                 :            :  */
    3527                 :         22 : void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
    3528                 :            : {
    3529 [ +  - ][ +  - ]:         22 :         if (wpa_s->driver && wpa_s->drv_priv)
    3530                 :         22 :                 wpa_drv_probe_req_report(wpa_s, 0);
    3531                 :            : 
    3532         [ +  + ]:         22 :         if (wpa_s->go_params) {
    3533                 :            :                 /* Clear any stored provisioning info */
    3534                 :         12 :                 p2p_clear_provisioning_info(
    3535                 :         12 :                         wpa_s->global->p2p,
    3536                 :         12 :                         wpa_s->go_params->peer_device_addr);
    3537                 :            :         }
    3538                 :            : 
    3539                 :         22 :         os_free(wpa_s->go_params);
    3540                 :         22 :         wpa_s->go_params = NULL;
    3541                 :         22 :         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
    3542                 :         22 :         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
    3543                 :         22 :         wpa_s->p2p_long_listen = 0;
    3544                 :         22 :         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
    3545                 :         22 :         eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
    3546                 :         22 :         wpas_p2p_remove_pending_group_interface(wpa_s);
    3547                 :         22 :         eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
    3548                 :            : 
    3549                 :            :         /* TODO: remove group interface from the driver if this wpa_s instance
    3550                 :            :          * is on top of a P2P group interface */
    3551                 :         22 : }
    3552                 :            : 
    3553                 :            : 
    3554                 :            : /**
    3555                 :            :  * wpas_p2p_deinit_global - Deinitialize global P2P module
    3556                 :            :  * @global: Pointer to global data from wpa_supplicant_init()
    3557                 :            :  *
    3558                 :            :  * This function deinitializes the global (per device) P2P module.
    3559                 :            :  */
    3560                 :          3 : void wpas_p2p_deinit_global(struct wpa_global *global)
    3561                 :            : {
    3562                 :            :         struct wpa_supplicant *wpa_s, *tmp;
    3563                 :            : 
    3564                 :          3 :         wpa_s = global->ifaces;
    3565         [ -  + ]:          3 :         if (wpa_s)
    3566                 :          0 :                 wpas_p2p_service_flush(wpa_s);
    3567                 :            : 
    3568         [ -  + ]:          3 :         if (global->p2p == NULL)
    3569                 :          3 :                 return;
    3570                 :            : 
    3571                 :            :         /* Remove remaining P2P group interfaces */
    3572 [ -  + ][ #  # ]:          3 :         while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
    3573                 :          0 :                 wpa_s = wpa_s->next;
    3574         [ -  + ]:          3 :         while (wpa_s) {
    3575                 :          0 :                 tmp = global->ifaces;
    3576 [ #  # ][ #  # ]:          0 :                 while (tmp &&
    3577         [ #  # ]:          0 :                        (tmp == wpa_s ||
    3578                 :          0 :                         tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
    3579                 :          0 :                         tmp = tmp->next;
    3580                 :            :                 }
    3581         [ #  # ]:          0 :                 if (tmp == NULL)
    3582                 :          0 :                         break;
    3583                 :            :                 /* Disconnect from the P2P group and deinit the interface */
    3584                 :          0 :                 wpas_p2p_disconnect(tmp);
    3585                 :            :         }
    3586                 :            : 
    3587                 :            :         /*
    3588                 :            :          * Deinit GO data on any possibly remaining interface (if main
    3589                 :            :          * interface is used as GO).
    3590                 :            :          */
    3591         [ -  + ]:          3 :         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    3592         [ #  # ]:          0 :                 if (wpa_s->ap_iface)
    3593                 :          0 :                         wpas_p2p_group_deinit(wpa_s);
    3594                 :            :         }
    3595                 :            : 
    3596                 :          3 :         p2p_deinit(global->p2p);
    3597                 :          3 :         global->p2p = NULL;
    3598                 :          3 :         global->p2p_init_wpa_s = NULL;
    3599                 :            : }
    3600                 :            : 
    3601                 :            : 
    3602                 :        132 : static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
    3603                 :            : {
    3604 [ +  - ][ +  + ]:        132 :         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
    3605                 :        132 :             wpa_s->conf->p2p_no_group_iface)
    3606                 :        113 :                 return 0; /* separate interface disabled per configuration */
    3607         [ +  - ]:         19 :         if (wpa_s->drv_flags &
    3608                 :            :             (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
    3609                 :            :              WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
    3610                 :         19 :                 return 1; /* P2P group requires a new interface in every case
    3611                 :            :                            */
    3612         [ #  # ]:          0 :         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
    3613                 :          0 :                 return 0; /* driver does not support concurrent operations */
    3614         [ #  # ]:          0 :         if (wpa_s->global->ifaces->next)
    3615                 :          0 :                 return 1; /* more that one interface already in use */
    3616         [ #  # ]:          0 :         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
    3617                 :          0 :                 return 1; /* this interface is already in use */
    3618                 :        132 :         return 0;
    3619                 :            : }
    3620                 :            : 
    3621                 :            : 
    3622                 :         42 : static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
    3623                 :            :                                  const u8 *peer_addr,
    3624                 :            :                                  enum p2p_wps_method wps_method,
    3625                 :            :                                  int go_intent, const u8 *own_interface_addr,
    3626                 :            :                                  unsigned int force_freq, int persistent_group,
    3627                 :            :                                  struct wpa_ssid *ssid, unsigned int pref_freq)
    3628                 :            : {
    3629 [ +  + ][ +  + ]:         42 :         if (persistent_group && wpa_s->conf->persistent_reconnect)
    3630                 :          4 :                 persistent_group = 2;
    3631                 :            : 
    3632                 :            :         /*
    3633                 :            :          * Increase GO config timeout if HT40 is used since it takes some time
    3634                 :            :          * to scan channels for coex purposes before the BSS can be started.
    3635                 :            :          */
    3636         [ -  + ]:         42 :         p2p_set_config_timeout(wpa_s->global->p2p,
    3637                 :         42 :                                wpa_s->p2p_go_ht40 ? 255 : 100, 20);
    3638                 :            : 
    3639 [ -  + ][ -  + ]:         42 :         return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
    3640                 :            :                            go_intent, own_interface_addr, force_freq,
    3641                 :            :                            persistent_group, ssid ? ssid->ssid : NULL,
    3642                 :            :                            ssid ? ssid->ssid_len : 0,
    3643                 :         42 :                            wpa_s->p2p_pd_before_go_neg, pref_freq);
    3644                 :            : }
    3645                 :            : 
    3646                 :            : 
    3647                 :         32 : static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
    3648                 :            :                                 const u8 *peer_addr,
    3649                 :            :                                 enum p2p_wps_method wps_method,
    3650                 :            :                                 int go_intent, const u8 *own_interface_addr,
    3651                 :            :                                 unsigned int force_freq, int persistent_group,
    3652                 :            :                                 struct wpa_ssid *ssid, unsigned int pref_freq)
    3653                 :            : {
    3654 [ +  + ][ +  + ]:         32 :         if (persistent_group && wpa_s->conf->persistent_reconnect)
    3655                 :          4 :                 persistent_group = 2;
    3656                 :            : 
    3657 [ -  + ][ -  + ]:         32 :         return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
    3658                 :            :                              go_intent, own_interface_addr, force_freq,
    3659                 :            :                              persistent_group, ssid ? ssid->ssid : NULL,
    3660                 :            :                              ssid ? ssid->ssid_len : 0, pref_freq);
    3661                 :            : }
    3662                 :            : 
    3663                 :            : 
    3664                 :          1 : static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
    3665                 :            : {
    3666                 :          1 :         wpa_s->p2p_join_scan_count++;
    3667                 :          1 :         wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
    3668                 :            :                    wpa_s->p2p_join_scan_count);
    3669         [ -  + ]:          1 :         if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
    3670                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
    3671                 :            :                            " for join operationg - stop join attempt",
    3672                 :          0 :                            MAC2STR(wpa_s->pending_join_iface_addr));
    3673                 :          0 :                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
    3674         [ #  # ]:          0 :                 if (wpa_s->p2p_auto_pd) {
    3675                 :          0 :                         wpa_s->p2p_auto_pd = 0;
    3676                 :          0 :                         wpa_msg_global(wpa_s, MSG_INFO,
    3677                 :            :                                        P2P_EVENT_PROV_DISC_FAILURE
    3678                 :            :                                        " p2p_dev_addr=" MACSTR " status=N/A",
    3679                 :          0 :                                        MAC2STR(wpa_s->pending_join_dev_addr));
    3680                 :          1 :                         return;
    3681                 :            :                 }
    3682                 :          0 :                 wpa_msg_global(wpa_s->parent, MSG_INFO,
    3683                 :            :                                P2P_EVENT_GROUP_FORMATION_FAILURE);
    3684                 :            :         }
    3685                 :            : }
    3686                 :            : 
    3687                 :            : 
    3688                 :         18 : static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
    3689                 :            : {
    3690                 :            :         int *freqs, res, num, i;
    3691                 :            : 
    3692         [ +  + ]:         18 :         if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
    3693                 :            :                 /* Multiple channels are supported and not all are in use */
    3694                 :         17 :                 return 0;
    3695                 :            :         }
    3696                 :            : 
    3697                 :          1 :         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
    3698         [ -  + ]:          1 :         if (!freqs)
    3699                 :          0 :                 return 1;
    3700                 :            : 
    3701                 :          1 :         num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
    3702                 :            :                                         wpa_s->num_multichan_concurrent);
    3703         [ -  + ]:          1 :         if (num < 0) {
    3704                 :          0 :                 res = 1;
    3705                 :          0 :                 goto exit_free;
    3706                 :            :         }
    3707                 :            : 
    3708         [ +  - ]:          1 :         for (i = 0; i < num; i++) {
    3709         [ +  - ]:          1 :                 if (freqs[i] == freq) {
    3710                 :          1 :                         wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
    3711                 :            :                                    freq);
    3712                 :          1 :                         res = 0;
    3713                 :          1 :                         goto exit_free;
    3714                 :            :                 }
    3715                 :            :         }
    3716                 :            : 
    3717                 :          0 :         res = 1;
    3718                 :            : 
    3719                 :            : exit_free:
    3720                 :          1 :         os_free(freqs);
    3721                 :         18 :         return res;
    3722                 :            : }
    3723                 :            : 
    3724                 :            : 
    3725                 :          0 : static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
    3726                 :            :                             const u8 *peer_dev_addr)
    3727                 :            : {
    3728                 :            :         struct wpa_bss *bss;
    3729                 :            :         int updated;
    3730                 :            : 
    3731                 :          0 :         bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
    3732         [ #  # ]:          0 :         if (bss == NULL)
    3733                 :          0 :                 return -1;
    3734         [ #  # ]:          0 :         if (bss->last_update_idx < wpa_s->bss_update_idx) {
    3735                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
    3736                 :            :                            "last scan");
    3737                 :          0 :                 return 0;
    3738                 :            :         }
    3739                 :            : 
    3740                 :          0 :         updated = os_reltime_before(&wpa_s->p2p_auto_started,
    3741                 :            :                                     &bss->last_update);
    3742         [ #  # ]:          0 :         wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
    3743                 :            :                    "%ld.%06ld (%supdated in last scan)",
    3744                 :            :                    bss->last_update.sec, bss->last_update.usec,
    3745                 :            :                    updated ? "": "not ");
    3746                 :            : 
    3747                 :          0 :         return updated;
    3748                 :            : }
    3749                 :            : 
    3750                 :            : 
    3751                 :         18 : static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
    3752                 :            :                                    struct wpa_scan_results *scan_res)
    3753                 :            : {
    3754                 :            :         struct wpa_bss *bss;
    3755                 :            :         int freq;
    3756                 :            :         u8 iface_addr[ETH_ALEN];
    3757                 :            : 
    3758                 :         18 :         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
    3759                 :            : 
    3760         [ -  + ]:         18 :         if (wpa_s->global->p2p_disabled)
    3761                 :          0 :                 return;
    3762                 :            : 
    3763 [ -  + ][ +  - ]:         18 :         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
    3764                 :         18 :                    scan_res ? (int) scan_res->num : -1,
    3765                 :         18 :                    wpa_s->p2p_auto_join ? "auto_" : "");
    3766                 :            : 
    3767         [ +  - ]:         18 :         if (scan_res)
    3768                 :         18 :                 wpas_p2p_scan_res_handler(wpa_s, scan_res);
    3769                 :            : 
    3770         [ -  + ]:         18 :         if (wpa_s->p2p_auto_pd) {
    3771                 :          0 :                 int join = wpas_p2p_peer_go(wpa_s,
    3772                 :          0 :                                             wpa_s->pending_join_dev_addr);
    3773 [ #  # ][ #  # ]:          0 :                 if (join == 0 &&
    3774                 :          0 :                     wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
    3775                 :          0 :                         wpa_s->auto_pd_scan_retry++;
    3776                 :          0 :                         bss = wpa_bss_get_bssid_latest(
    3777                 :          0 :                                 wpa_s, wpa_s->pending_join_dev_addr);
    3778         [ #  # ]:          0 :                         if (bss) {
    3779                 :          0 :                                 freq = bss->freq;
    3780                 :          0 :                                 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
    3781                 :            :                                            "the peer " MACSTR " at %d MHz",
    3782                 :            :                                            wpa_s->auto_pd_scan_retry,
    3783                 :          0 :                                            MAC2STR(wpa_s->
    3784                 :            :                                                    pending_join_dev_addr),
    3785                 :            :                                            freq);
    3786                 :          0 :                                 wpas_p2p_join_scan_req(wpa_s, freq);
    3787                 :          0 :                                 return;
    3788                 :            :                         }
    3789                 :            :                 }
    3790                 :            : 
    3791         [ #  # ]:          0 :                 if (join < 0)
    3792                 :          0 :                         join = 0;
    3793                 :            : 
    3794                 :          0 :                 wpa_s->p2p_auto_pd = 0;
    3795         [ #  # ]:          0 :                 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
    3796                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
    3797                 :          0 :                            MAC2STR(wpa_s->pending_join_dev_addr), join);
    3798         [ #  # ]:          0 :                 if (p2p_prov_disc_req(wpa_s->global->p2p,
    3799                 :          0 :                                       wpa_s->pending_join_dev_addr,
    3800                 :          0 :                                       wpa_s->pending_pd_config_methods, join,
    3801                 :          0 :                                       0, wpa_s->user_initiated_pd) < 0) {
    3802                 :          0 :                         wpa_s->p2p_auto_pd = 0;
    3803                 :          0 :                         wpa_msg_global(wpa_s, MSG_INFO,
    3804                 :            :                                        P2P_EVENT_PROV_DISC_FAILURE
    3805                 :            :                                        " p2p_dev_addr=" MACSTR " status=N/A",
    3806                 :          0 :                                        MAC2STR(wpa_s->pending_join_dev_addr));
    3807                 :            :                 }
    3808                 :          0 :                 return;
    3809                 :            :         }
    3810                 :            : 
    3811         [ -  + ]:         18 :         if (wpa_s->p2p_auto_join) {
    3812                 :          0 :                 int join = wpas_p2p_peer_go(wpa_s,
    3813                 :          0 :                                             wpa_s->pending_join_dev_addr);
    3814         [ #  # ]:          0 :                 if (join < 0) {
    3815                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
    3816                 :            :                                    "running a GO -> use GO Negotiation");
    3817                 :          0 :                         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
    3818                 :          0 :                                          wpa_s->p2p_pin, wpa_s->p2p_wps_method,
    3819                 :          0 :                                          wpa_s->p2p_persistent_group, 0, 0, 0,
    3820                 :            :                                          wpa_s->p2p_go_intent,
    3821                 :            :                                          wpa_s->p2p_connect_freq,
    3822                 :            :                                          wpa_s->p2p_persistent_id,
    3823                 :          0 :                                          wpa_s->p2p_pd_before_go_neg,
    3824                 :          0 :                                          wpa_s->p2p_go_ht40,
    3825                 :          0 :                                          wpa_s->p2p_go_vht);
    3826                 :          0 :                         return;
    3827                 :            :                 }
    3828                 :            : 
    3829         [ #  # ]:          0 :                 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
    3830                 :            :                            "try to join the group", join ? "" :
    3831                 :            :                            " in older scan");
    3832         [ #  # ]:          0 :                 if (!join)
    3833                 :          0 :                         wpa_s->p2p_fallback_to_go_neg = 1;
    3834                 :            :         }
    3835                 :            : 
    3836                 :         18 :         freq = p2p_get_oper_freq(wpa_s->global->p2p,
    3837                 :         18 :                                  wpa_s->pending_join_iface_addr);
    3838   [ +  -  +  + ]:         36 :         if (freq < 0 &&
    3839                 :         18 :             p2p_get_interface_addr(wpa_s->global->p2p,
    3840                 :         18 :                                    wpa_s->pending_join_dev_addr,
    3841         [ +  - ]:          3 :                                    iface_addr) == 0 &&
    3842                 :          3 :             os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
    3843                 :            :         {
    3844                 :          3 :                 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
    3845                 :            :                            "address for join from " MACSTR " to " MACSTR
    3846                 :            :                            " based on newly discovered P2P peer entry",
    3847                 :         18 :                            MAC2STR(wpa_s->pending_join_iface_addr),
    3848                 :         18 :                            MAC2STR(iface_addr));
    3849                 :          3 :                 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
    3850                 :            :                           ETH_ALEN);
    3851                 :            : 
    3852                 :          3 :                 freq = p2p_get_oper_freq(wpa_s->global->p2p,
    3853                 :          3 :                                          wpa_s->pending_join_iface_addr);
    3854                 :            :         }
    3855         [ -  + ]:         18 :         if (freq >= 0) {
    3856                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
    3857                 :            :                            "from P2P peer table: %d MHz", freq);
    3858                 :            :         }
    3859                 :         18 :         bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
    3860         [ +  - ]:         18 :         if (bss) {
    3861                 :         18 :                 freq = bss->freq;
    3862                 :         18 :                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
    3863                 :            :                            "from BSS table: %d MHz (SSID %s)", freq,
    3864                 :         18 :                            wpa_ssid_txt(bss->ssid, bss->ssid_len));
    3865                 :            :         }
    3866         [ +  - ]:         18 :         if (freq > 0) {
    3867                 :            :                 u16 method;
    3868                 :            : 
    3869         [ -  + ]:         18 :                 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
    3870                 :          0 :                         wpa_msg_global(wpa_s->parent, MSG_INFO,
    3871                 :            :                                        P2P_EVENT_GROUP_FORMATION_FAILURE
    3872                 :            :                                        "reason=FREQ_CONFLICT");
    3873                 :          0 :                         return;
    3874                 :            :                 }
    3875                 :            : 
    3876                 :         18 :                 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
    3877                 :            :                            "prior to joining an existing group (GO " MACSTR
    3878                 :            :                            " freq=%u MHz)",
    3879                 :        108 :                            MAC2STR(wpa_s->pending_join_dev_addr), freq);
    3880                 :         18 :                 wpa_s->pending_pd_before_join = 1;
    3881                 :            : 
    3882   [ -  +  -  - ]:         18 :                 switch (wpa_s->pending_join_wps_method) {
    3883                 :            :                 case WPS_PIN_DISPLAY:
    3884                 :          0 :                         method = WPS_CONFIG_KEYPAD;
    3885                 :          0 :                         break;
    3886                 :            :                 case WPS_PIN_KEYPAD:
    3887                 :         18 :                         method = WPS_CONFIG_DISPLAY;
    3888                 :         18 :                         break;
    3889                 :            :                 case WPS_PBC:
    3890                 :          0 :                         method = WPS_CONFIG_PUSHBUTTON;
    3891                 :          0 :                         break;
    3892                 :            :                 default:
    3893                 :          0 :                         method = 0;
    3894                 :          0 :                         break;
    3895                 :            :                 }
    3896                 :            : 
    3897         [ -  + ]:         18 :                 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
    3898                 :         18 :                                                wpa_s->pending_join_dev_addr) ==
    3899                 :            :                      method)) {
    3900                 :            :                         /*
    3901                 :            :                          * We have already performed provision discovery for
    3902                 :            :                          * joining the group. Proceed directly to join
    3903                 :            :                          * operation without duplicated provision discovery. */
    3904                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
    3905                 :            :                                    "with " MACSTR " already done - proceed to "
    3906                 :            :                                    "join",
    3907                 :          0 :                                    MAC2STR(wpa_s->pending_join_dev_addr));
    3908                 :          0 :                         wpa_s->pending_pd_before_join = 0;
    3909                 :          0 :                         goto start;
    3910                 :            :                 }
    3911                 :            : 
    3912         [ -  + ]:         18 :                 if (p2p_prov_disc_req(wpa_s->global->p2p,
    3913                 :         18 :                                       wpa_s->pending_join_dev_addr, method, 1,
    3914                 :         18 :                                       freq, wpa_s->user_initiated_pd) < 0) {
    3915                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
    3916                 :            :                                    "Discovery Request before joining an "
    3917                 :            :                                    "existing group");
    3918                 :          0 :                         wpa_s->pending_pd_before_join = 0;
    3919                 :          0 :                         goto start;
    3920                 :            :                 }
    3921                 :         18 :                 return;
    3922                 :            :         }
    3923                 :            : 
    3924                 :          0 :         wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
    3925                 :          0 :         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
    3926                 :          0 :         eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
    3927                 :          0 :         wpas_p2p_check_join_scan_limit(wpa_s);
    3928                 :          0 :         return;
    3929                 :            : 
    3930                 :            : start:
    3931                 :            :         /* Start join operation immediately */
    3932                 :         18 :         wpas_p2p_join_start(wpa_s);
    3933                 :            : }
    3934                 :            : 
    3935                 :            : 
    3936                 :         19 : static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
    3937                 :            : {
    3938                 :            :         int ret;
    3939                 :            :         struct wpa_driver_scan_params params;
    3940                 :            :         struct wpabuf *wps_ie, *ies;
    3941                 :            :         size_t ielen;
    3942                 :         19 :         int freqs[2] = { 0, 0 };
    3943                 :            : 
    3944                 :         19 :         os_memset(&params, 0, sizeof(params));
    3945                 :            : 
    3946                 :            :         /* P2P Wildcard SSID */
    3947                 :         19 :         params.num_ssids = 1;
    3948                 :         19 :         params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
    3949                 :         19 :         params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
    3950                 :            : 
    3951                 :         19 :         wpa_s->wps->dev.p2p = 1;
    3952                 :         19 :         wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
    3953                 :         19 :                                         wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
    3954                 :            :                                         NULL);
    3955         [ -  + ]:         19 :         if (wps_ie == NULL) {
    3956                 :          0 :                 wpas_p2p_scan_res_join(wpa_s, NULL);
    3957                 :          0 :                 return;
    3958                 :            :         }
    3959                 :            : 
    3960                 :         19 :         ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
    3961                 :         19 :         ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
    3962         [ -  + ]:         19 :         if (ies == NULL) {
    3963                 :          0 :                 wpabuf_free(wps_ie);
    3964                 :          0 :                 wpas_p2p_scan_res_join(wpa_s, NULL);
    3965                 :          0 :                 return;
    3966                 :            :         }
    3967                 :         19 :         wpabuf_put_buf(ies, wps_ie);
    3968                 :         19 :         wpabuf_free(wps_ie);
    3969                 :            : 
    3970                 :         19 :         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
    3971                 :            : 
    3972                 :         19 :         params.p2p_probe = 1;
    3973                 :         19 :         params.extra_ies = wpabuf_head(ies);
    3974                 :         19 :         params.extra_ies_len = wpabuf_len(ies);
    3975         [ -  + ]:         19 :         if (freq > 0) {
    3976                 :          0 :                 freqs[0] = freq;
    3977                 :          0 :                 params.freqs = freqs;
    3978                 :            :         }
    3979                 :            : 
    3980                 :            :         /*
    3981                 :            :          * Run a scan to update BSS table and start Provision Discovery once
    3982                 :            :          * the new scan results become available.
    3983                 :            :          */
    3984                 :         19 :         ret = wpa_drv_scan(wpa_s, &params);
    3985         [ +  + ]:         19 :         if (!ret) {
    3986                 :         18 :                 os_get_reltime(&wpa_s->scan_trigger_time);
    3987                 :         18 :                 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
    3988                 :         18 :                 wpa_s->own_scan_requested = 1;
    3989                 :            :         }
    3990                 :            : 
    3991                 :         19 :         wpabuf_free(ies);
    3992                 :            : 
    3993         [ +  + ]:         19 :         if (ret) {
    3994                 :          1 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
    3995                 :            :                            "try again later");
    3996                 :          1 :                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
    3997                 :          1 :                 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
    3998                 :         19 :                 wpas_p2p_check_join_scan_limit(wpa_s);
    3999                 :            :         }
    4000                 :            : }
    4001                 :            : 
    4002                 :            : 
    4003                 :         19 : static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
    4004                 :            : {
    4005                 :         19 :         struct wpa_supplicant *wpa_s = eloop_ctx;
    4006                 :         19 :         wpas_p2p_join_scan_req(wpa_s, 0);
    4007                 :         19 : }
    4008                 :            : 
    4009                 :            : 
    4010                 :         18 : static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
    4011                 :            :                          const u8 *dev_addr, enum p2p_wps_method wps_method,
    4012                 :            :                          int auto_join)
    4013                 :            : {
    4014         [ -  + ]:         18 :         wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
    4015                 :            :                    MACSTR " dev " MACSTR ")%s",
    4016                 :        216 :                    MAC2STR(iface_addr), MAC2STR(dev_addr),
    4017                 :            :                    auto_join ? " (auto_join)" : "");
    4018                 :            : 
    4019                 :         18 :         wpa_s->p2p_auto_pd = 0;
    4020                 :         18 :         wpa_s->p2p_auto_join = !!auto_join;
    4021                 :         18 :         os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
    4022                 :         18 :         os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
    4023                 :         18 :         wpa_s->pending_join_wps_method = wps_method;
    4024                 :            : 
    4025                 :            :         /* Make sure we are not running find during connection establishment */
    4026                 :         18 :         wpas_p2p_stop_find(wpa_s);
    4027                 :            : 
    4028                 :         18 :         wpa_s->p2p_join_scan_count = 0;
    4029                 :         18 :         wpas_p2p_join_scan(wpa_s, NULL);
    4030                 :         18 :         return 0;
    4031                 :            : }
    4032                 :            : 
    4033                 :            : 
    4034                 :         18 : static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
    4035                 :            : {
    4036                 :            :         struct wpa_supplicant *group;
    4037                 :            :         struct p2p_go_neg_results res;
    4038                 :            :         struct wpa_bss *bss;
    4039                 :            : 
    4040                 :         18 :         group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
    4041         [ -  + ]:         18 :         if (group == NULL)
    4042                 :          0 :                 return -1;
    4043         [ +  + ]:         18 :         if (group != wpa_s) {
    4044                 :          3 :                 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
    4045                 :            :                           sizeof(group->p2p_pin));
    4046                 :          3 :                 group->p2p_wps_method = wpa_s->p2p_wps_method;
    4047                 :            :         } else {
    4048                 :            :                 /*
    4049                 :            :                  * Need to mark the current interface for p2p_group_formation
    4050                 :            :                  * when a separate group interface is not used. This is needed
    4051                 :            :                  * to allow p2p_cancel stop a pending p2p_connect-join.
    4052                 :            :                  * wpas_p2p_init_group_interface() addresses this for the case
    4053                 :            :                  * where a separate group interface is used.
    4054                 :            :                  */
    4055                 :         15 :                 wpa_s->global->p2p_group_formation = wpa_s;
    4056                 :            :         }
    4057                 :            : 
    4058                 :         18 :         group->p2p_in_provisioning = 1;
    4059                 :         18 :         group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
    4060                 :            : 
    4061                 :         18 :         os_memset(&res, 0, sizeof(res));
    4062                 :         18 :         os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
    4063                 :            :                   ETH_ALEN);
    4064                 :         18 :         res.wps_method = wpa_s->pending_join_wps_method;
    4065                 :         18 :         bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
    4066         [ +  - ]:         18 :         if (bss) {
    4067                 :         18 :                 res.freq = bss->freq;
    4068                 :         18 :                 res.ssid_len = bss->ssid_len;
    4069                 :         18 :                 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
    4070                 :         18 :                 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
    4071                 :            :                            "from BSS table: %d MHz (SSID %s)", bss->freq,
    4072                 :         18 :                            wpa_ssid_txt(bss->ssid, bss->ssid_len));
    4073                 :            :         }
    4074                 :            : 
    4075 [ +  - ][ -  + ]:         18 :         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
    4076                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
    4077                 :            :                            "starting client");
    4078                 :          0 :                 wpa_drv_cancel_remain_on_channel(wpa_s);
    4079                 :          0 :                 wpa_s->off_channel_freq = 0;
    4080                 :          0 :                 wpa_s->roc_waiting_drv_freq = 0;
    4081                 :            :         }
    4082                 :         18 :         wpas_start_wps_enrollee(group, &res);
    4083                 :            : 
    4084                 :            :         /*
    4085                 :            :          * Allow a longer timeout for join-a-running-group than normal 15
    4086                 :            :          * second group formation timeout since the GO may not have authorized
    4087                 :            :          * our connection yet.
    4088                 :            :          */
    4089                 :         18 :         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
    4090                 :         18 :         eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
    4091                 :            :                                wpa_s, NULL);
    4092                 :            : 
    4093                 :         18 :         return 0;
    4094                 :            : }
    4095                 :            : 
    4096                 :            : 
    4097                 :         85 : static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
    4098                 :            :                                 int *force_freq, int *pref_freq, int go)
    4099                 :            : {
    4100                 :            :         int *freqs, res;
    4101                 :         85 :         unsigned int freq_in_use = 0, num, i;
    4102                 :            : 
    4103                 :         85 :         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
    4104         [ -  + ]:         85 :         if (!freqs)
    4105                 :          0 :                 return -1;
    4106                 :            : 
    4107                 :         85 :         num = get_shared_radio_freqs(wpa_s, freqs,
    4108                 :            :                                      wpa_s->num_multichan_concurrent);
    4109                 :         85 :         wpa_printf(MSG_DEBUG,
    4110                 :            :                    "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
    4111                 :            :                    freq, wpa_s->num_multichan_concurrent, num);
    4112                 :            : 
    4113         [ +  + ]:         85 :         if (freq > 0) {
    4114                 :            :                 int ret;
    4115         [ +  + ]:         18 :                 if (go)
    4116                 :          9 :                         ret = p2p_supported_freq(wpa_s->global->p2p, freq);
    4117                 :            :                 else
    4118                 :          9 :                         ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
    4119         [ -  + ]:         18 :                 if (!ret) {
    4120                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: The forced channel "
    4121                 :            :                                    "(%u MHz) is not supported for P2P uses",
    4122                 :            :                                    freq);
    4123                 :          0 :                         res = -3;
    4124                 :          0 :                         goto exit_free;
    4125                 :            :                 }
    4126                 :            : 
    4127         [ +  + ]:         23 :                 for (i = 0; i < num; i++) {
    4128         [ +  - ]:          5 :                         if (freqs[i] == freq)
    4129                 :          5 :                                 freq_in_use = 1;
    4130                 :            :                 }
    4131                 :            : 
    4132 [ +  + ][ -  + ]:         18 :                 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
    4133                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
    4134                 :            :                                    freq);
    4135                 :          0 :                         res = -2;
    4136                 :          0 :                         goto exit_free;
    4137                 :            :                 }
    4138                 :         18 :                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
    4139                 :            :                            "requested channel (%u MHz)", freq);
    4140                 :         18 :                 *force_freq = freq;
    4141                 :         18 :                 goto exit_ok;
    4142                 :            :         }
    4143                 :            : 
    4144         [ +  + ]:         67 :         for (i = 0; i < num; i++) {
    4145         [ -  + ]:          2 :                 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
    4146                 :          0 :                         continue;
    4147                 :            : 
    4148 [ +  - ][ -  + ]:          2 :                 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
    4149                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
    4150                 :          0 :                                    freqs[i]);
    4151                 :          0 :                         *pref_freq = freqs[i];
    4152                 :            :                 } else {
    4153                 :          2 :                         wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
    4154                 :          2 :                                    freqs[i]);
    4155                 :          2 :                         *force_freq = freqs[i];
    4156                 :            :                 }
    4157                 :          2 :                 break;
    4158                 :            :         }
    4159                 :            : 
    4160         [ +  + ]:         67 :         if (i == num) {
    4161 [ +  - ][ -  + ]:         65 :                 if (num < wpa_s->num_multichan_concurrent && num > 0) {
    4162                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
    4163                 :          0 :                         *force_freq = 0;
    4164         [ +  - ]:         65 :                 } else if (num < wpa_s->num_multichan_concurrent) {
    4165                 :         65 :                         wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
    4166                 :         65 :                         *force_freq = 0;
    4167                 :            :                 } else {
    4168                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
    4169                 :          0 :                         res = -2;
    4170                 :          0 :                         goto exit_free;
    4171                 :            :                 }
    4172                 :            :         }
    4173                 :            : 
    4174                 :            : exit_ok:
    4175                 :         85 :         res = 0;
    4176                 :            : exit_free:
    4177                 :         85 :         os_free(freqs);
    4178                 :         85 :         return res;
    4179                 :            : }
    4180                 :            : 
    4181                 :            : 
    4182                 :            : /**
    4183                 :            :  * wpas_p2p_connect - Request P2P Group Formation to be started
    4184                 :            :  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
    4185                 :            :  * @peer_addr: Address of the peer P2P Device
    4186                 :            :  * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
    4187                 :            :  * @persistent_group: Whether to create a persistent group
    4188                 :            :  * @auto_join: Whether to select join vs. GO Negotiation automatically
    4189                 :            :  * @join: Whether to join an existing group (as a client) instead of starting
    4190                 :            :  *      Group Owner negotiation; @peer_addr is BSSID in that case
    4191                 :            :  * @auth: Whether to only authorize the connection instead of doing that and
    4192                 :            :  *      initiating Group Owner negotiation
    4193                 :            :  * @go_intent: GO Intent or -1 to use default
    4194                 :            :  * @freq: Frequency for the group or 0 for auto-selection
    4195                 :            :  * @persistent_id: Persistent group credentials to use for forcing GO
    4196                 :            :  *      parameters or -1 to generate new values (SSID/passphrase)
    4197                 :            :  * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
    4198                 :            :  *      interoperability workaround when initiating group formation
    4199                 :            :  * @ht40: Start GO with 40 MHz channel width
    4200                 :            :  * @vht:  Start GO with VHT support
    4201                 :            :  * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
    4202                 :            :  *      failure, -2 on failure due to channel not currently available,
    4203                 :            :  *      -3 if forced channel is not supported
    4204                 :            :  */
    4205                 :         92 : int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
    4206                 :            :                      const char *pin, enum p2p_wps_method wps_method,
    4207                 :            :                      int persistent_group, int auto_join, int join, int auth,
    4208                 :            :                      int go_intent, int freq, int persistent_id, int pd,
    4209                 :            :                      int ht40, int vht)
    4210                 :            : {
    4211                 :         92 :         int force_freq = 0, pref_freq = 0;
    4212                 :         92 :         int ret = 0, res;
    4213                 :            :         enum wpa_driver_if_type iftype;
    4214                 :            :         const u8 *if_addr;
    4215                 :         92 :         struct wpa_ssid *ssid = NULL;
    4216                 :            : 
    4217 [ +  - ][ -  + ]:         92 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    4218                 :          0 :                 return -1;
    4219                 :            : 
    4220         [ -  + ]:         92 :         if (persistent_id >= 0) {
    4221                 :          0 :                 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
    4222 [ #  # ][ #  # ]:          0 :                 if (ssid == NULL || ssid->disabled != 2 ||
                 [ #  # ]
    4223                 :          0 :                     ssid->mode != WPAS_MODE_P2P_GO)
    4224                 :          0 :                         return -1;
    4225                 :            :         }
    4226                 :            : 
    4227                 :         92 :         os_free(wpa_s->global->add_psk);
    4228                 :         92 :         wpa_s->global->add_psk = NULL;
    4229                 :            : 
    4230         [ +  + ]:         92 :         if (go_intent < 0)
    4231                 :         57 :                 go_intent = wpa_s->conf->p2p_go_intent;
    4232                 :            : 
    4233         [ +  + ]:         92 :         if (!auth)
    4234                 :         59 :                 wpa_s->p2p_long_listen = 0;
    4235                 :            : 
    4236                 :         92 :         wpa_s->p2p_wps_method = wps_method;
    4237                 :         92 :         wpa_s->p2p_persistent_group = !!persistent_group;
    4238                 :         92 :         wpa_s->p2p_persistent_id = persistent_id;
    4239                 :         92 :         wpa_s->p2p_go_intent = go_intent;
    4240                 :         92 :         wpa_s->p2p_connect_freq = freq;
    4241                 :         92 :         wpa_s->p2p_fallback_to_go_neg = 0;
    4242                 :         92 :         wpa_s->p2p_pd_before_go_neg = !!pd;
    4243                 :         92 :         wpa_s->p2p_go_ht40 = !!ht40;
    4244                 :         92 :         wpa_s->p2p_go_vht = !!vht;
    4245                 :            : 
    4246         [ +  + ]:         92 :         if (pin)
    4247                 :         86 :                 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
    4248         [ -  + ]:          6 :         else if (wps_method == WPS_PIN_DISPLAY) {
    4249                 :          0 :                 ret = wps_generate_pin();
    4250                 :          0 :                 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
    4251                 :            :                             ret);
    4252                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
    4253                 :          0 :                            wpa_s->p2p_pin);
    4254                 :            :         } else
    4255                 :          6 :                 wpa_s->p2p_pin[0] = '\0';
    4256                 :            : 
    4257 [ +  + ][ -  + ]:         92 :         if (join || auto_join) {
    4258                 :            :                 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
    4259         [ +  + ]:         18 :                 if (auth) {
    4260                 :          1 :                         wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
    4261                 :            :                                    "connect a running group from " MACSTR,
    4262                 :          6 :                                    MAC2STR(peer_addr));
    4263                 :          1 :                         os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
    4264                 :          1 :                         return ret;
    4265                 :            :                 }
    4266                 :         17 :                 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
    4267         [ +  + ]:         17 :                 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
    4268                 :            :                                            iface_addr) < 0) {
    4269                 :         14 :                         os_memcpy(iface_addr, peer_addr, ETH_ALEN);
    4270                 :         14 :                         p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
    4271                 :            :                                          dev_addr);
    4272                 :            :                 }
    4273         [ -  + ]:         17 :                 if (auto_join) {
    4274                 :          0 :                         os_get_reltime(&wpa_s->p2p_auto_started);
    4275                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
    4276                 :            :                                    "%ld.%06ld",
    4277                 :            :                                    wpa_s->p2p_auto_started.sec,
    4278                 :            :                                    wpa_s->p2p_auto_started.usec);
    4279                 :            :                 }
    4280                 :         17 :                 wpa_s->user_initiated_pd = 1;
    4281         [ -  + ]:         17 :                 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
    4282                 :            :                                   auto_join) < 0)
    4283                 :          0 :                         return -1;
    4284                 :         17 :                 return ret;
    4285                 :            :         }
    4286                 :            : 
    4287                 :         74 :         res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
    4288                 :            :                                    go_intent == 15);
    4289         [ -  + ]:         74 :         if (res)
    4290                 :          0 :                 return res;
    4291         [ +  + ]:         74 :         wpas_p2p_set_own_freq_preference(wpa_s,
    4292                 :         74 :                                          force_freq ? force_freq : pref_freq);
    4293                 :            : 
    4294                 :         74 :         wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
    4295                 :            : 
    4296         [ +  + ]:         74 :         if (wpa_s->create_p2p_iface) {
    4297                 :            :                 /* Prepare to add a new interface for the group */
    4298                 :         13 :                 iftype = WPA_IF_P2P_GROUP;
    4299         [ +  + ]:         13 :                 if (go_intent == 15)
    4300                 :          6 :                         iftype = WPA_IF_P2P_GO;
    4301         [ -  + ]:         13 :                 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
    4302                 :          0 :                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
    4303                 :            :                                    "interface for the group");
    4304                 :          0 :                         return -1;
    4305                 :            :                 }
    4306                 :            : 
    4307                 :         13 :                 if_addr = wpa_s->pending_interface_addr;
    4308                 :            :         } else
    4309                 :         61 :                 if_addr = wpa_s->own_addr;
    4310                 :            : 
    4311         [ +  + ]:         74 :         if (auth) {
    4312         [ -  + ]:         32 :                 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
    4313                 :            :                                          go_intent, if_addr,
    4314                 :            :                                          force_freq, persistent_group, ssid,
    4315                 :            :                                          pref_freq) < 0)
    4316                 :          0 :                         return -1;
    4317                 :         32 :                 return ret;
    4318                 :            :         }
    4319                 :            : 
    4320         [ -  + ]:         42 :         if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
    4321                 :            :                                   go_intent, if_addr, force_freq,
    4322                 :            :                                   persistent_group, ssid, pref_freq) < 0) {
    4323         [ #  # ]:          0 :                 if (wpa_s->create_p2p_iface)
    4324                 :          0 :                         wpas_p2p_remove_pending_group_interface(wpa_s);
    4325                 :          0 :                 return -1;
    4326                 :            :         }
    4327                 :         92 :         return ret;
    4328                 :            : }
    4329                 :            : 
    4330                 :            : 
    4331                 :            : /**
    4332                 :            :  * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
    4333                 :            :  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
    4334                 :            :  * @freq: Frequency of the channel in MHz
    4335                 :            :  * @duration: Duration of the stay on the channel in milliseconds
    4336                 :            :  *
    4337                 :            :  * This callback is called when the driver indicates that it has started the
    4338                 :            :  * requested remain-on-channel duration.
    4339                 :            :  */
    4340                 :        493 : void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
    4341                 :            :                                    unsigned int freq, unsigned int duration)
    4342                 :            : {
    4343 [ +  - ][ -  + ]:        493 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    4344                 :        493 :                 return;
    4345         [ +  - ]:        493 :         if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
    4346                 :        493 :                 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
    4347                 :            :                               wpa_s->pending_listen_duration);
    4348                 :        493 :                 wpa_s->pending_listen_freq = 0;
    4349                 :            :         }
    4350                 :            : }
    4351                 :            : 
    4352                 :            : 
    4353                 :         89 : static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
    4354                 :            :                                  unsigned int timeout)
    4355                 :            : {
    4356                 :            :         /* Limit maximum Listen state time based on driver limitation. */
    4357         [ +  - ]:         89 :         if (timeout > wpa_s->max_remain_on_chan)
    4358                 :         89 :                 timeout = wpa_s->max_remain_on_chan;
    4359                 :            : 
    4360                 :         89 :         return p2p_listen(wpa_s->global->p2p, timeout);
    4361                 :            : }
    4362                 :            : 
    4363                 :            : 
    4364                 :            : /**
    4365                 :            :  * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
    4366                 :            :  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
    4367                 :            :  * @freq: Frequency of the channel in MHz
    4368                 :            :  *
    4369                 :            :  * This callback is called when the driver indicates that a remain-on-channel
    4370                 :            :  * operation has been completed, i.e., the duration on the requested channel
    4371                 :            :  * has timed out.
    4372                 :            :  */
    4373                 :        493 : void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
    4374                 :            :                                           unsigned int freq)
    4375                 :            : {
    4376                 :        493 :         wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
    4377                 :            :                    "(p2p_long_listen=%d ms pending_action_tx=%p)",
    4378                 :            :                    wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
    4379 [ +  - ][ -  + ]:        493 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    4380                 :          0 :                 return;
    4381         [ +  + ]:        493 :         if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
    4382                 :          8 :                 return; /* P2P module started a new operation */
    4383         [ +  + ]:        485 :         if (offchannel_pending_action_tx(wpa_s))
    4384                 :         22 :                 return;
    4385         [ +  + ]:        463 :         if (wpa_s->p2p_long_listen > 0)
    4386                 :          3 :                 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
    4387         [ +  + ]:        463 :         if (wpa_s->p2p_long_listen > 0) {
    4388                 :          3 :                 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
    4389                 :          3 :                 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
    4390                 :            :         } else {
    4391                 :            :                 /*
    4392                 :            :                  * When listen duration is over, stop listen & update p2p_state
    4393                 :            :                  * to IDLE.
    4394                 :            :                  */
    4395                 :        493 :                 p2p_stop_listen(wpa_s->global->p2p);
    4396                 :            :         }
    4397                 :            : }
    4398                 :            : 
    4399                 :            : 
    4400                 :            : /**
    4401                 :            :  * wpas_p2p_group_remove - Remove a P2P group
    4402                 :            :  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
    4403                 :            :  * @ifname: Network interface name of the group interface or "*" to remove all
    4404                 :            :  *      groups
    4405                 :            :  * Returns: 0 on success, -1 on failure
    4406                 :            :  *
    4407                 :            :  * This function is used to remove a P2P group. This can be used to disconnect
    4408                 :            :  * from a group in which the local end is a P2P Client or to end a P2P Group in
    4409                 :            :  * case the local end is the Group Owner. If a virtual network interface was
    4410                 :            :  * created for this group, that interface will be removed. Otherwise, only the
    4411                 :            :  * configured P2P group network will be removed from the interface.
    4412                 :            :  */
    4413                 :        734 : int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
    4414                 :            : {
    4415                 :        734 :         struct wpa_global *global = wpa_s->global;
    4416                 :            : 
    4417         [ +  + ]:        734 :         if (os_strcmp(ifname, "*") == 0) {
    4418                 :            :                 struct wpa_supplicant *prev;
    4419                 :        655 :                 wpa_s = global->ifaces;
    4420         [ +  + ]:       1310 :                 while (wpa_s) {
    4421                 :        655 :                         prev = wpa_s;
    4422                 :        655 :                         wpa_s = wpa_s->next;
    4423         [ +  - ]:        655 :                         if (prev->p2p_group_interface !=
    4424         [ +  + ]:        655 :                             NOT_P2P_GROUP_INTERFACE ||
    4425         [ +  + ]:        165 :                             (prev->current_ssid &&
    4426                 :        165 :                              prev->current_ssid->p2p_group))
    4427                 :          6 :                                 wpas_p2p_disconnect(prev);
    4428                 :            :                 }
    4429                 :        655 :                 return 0;
    4430                 :            :         }
    4431                 :            : 
    4432         [ +  + ]:         82 :         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    4433         [ +  + ]:         79 :                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
    4434                 :         76 :                         break;
    4435                 :            :         }
    4436                 :            : 
    4437                 :        734 :         return wpas_p2p_disconnect(wpa_s);
    4438                 :            : }
    4439                 :            : 
    4440                 :            : 
    4441                 :         23 : static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
    4442                 :            : {
    4443                 :            :         unsigned int r;
    4444                 :            : 
    4445         [ -  + ]:         23 :         if (freq == 2) {
    4446                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
    4447                 :            :                            "band");
    4448   [ #  #  #  # ]:          0 :                 if (wpa_s->best_24_freq > 0 &&
    4449                 :          0 :                     p2p_supported_freq_go(wpa_s->global->p2p,
    4450                 :          0 :                                           wpa_s->best_24_freq)) {
    4451                 :          0 :                         freq = wpa_s->best_24_freq;
    4452                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
    4453                 :            :                                    "channel: %d MHz", freq);
    4454                 :            :                 } else {
    4455                 :          0 :                         os_get_random((u8 *) &r, sizeof(r));
    4456                 :          0 :                         freq = 2412 + (r % 3) * 25;
    4457                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
    4458                 :            :                                    "channel: %d MHz", freq);
    4459                 :            :                 }
    4460                 :            :         }
    4461                 :            : 
    4462         [ -  + ]:         23 :         if (freq == 5) {
    4463                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
    4464                 :            :                            "band");
    4465   [ #  #  #  # ]:          0 :                 if (wpa_s->best_5_freq > 0 &&
    4466                 :          0 :                     p2p_supported_freq_go(wpa_s->global->p2p,
    4467                 :          0 :                                        wpa_s->best_5_freq)) {
    4468                 :          0 :                         freq = wpa_s->best_5_freq;
    4469                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
    4470                 :            :                                    "channel: %d MHz", freq);
    4471                 :            :                 } else {
    4472                 :          0 :                         os_get_random((u8 *) &r, sizeof(r));
    4473                 :          0 :                         freq = 5180 + (r % 4) * 20;
    4474         [ #  # ]:          0 :                         if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
    4475                 :          0 :                                 wpa_printf(MSG_DEBUG, "P2P: Could not select "
    4476                 :            :                                            "5 GHz channel for P2P group");
    4477                 :          0 :                                 return -1;
    4478                 :            :                         }
    4479                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
    4480                 :            :                                    "channel: %d MHz", freq);
    4481                 :            :                 }
    4482                 :            :         }
    4483                 :            : 
    4484 [ +  + ][ -  + ]:         23 :         if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
    4485                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
    4486                 :            :                            "(%u MHz) is not supported for P2P uses",
    4487                 :            :                            freq);
    4488                 :          0 :                 return -1;
    4489                 :            :         }
    4490                 :            : 
    4491                 :         23 :         return freq;
    4492                 :            : }
    4493                 :            : 
    4494                 :            : 
    4495                 :         23 : static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
    4496                 :            :                                    struct p2p_go_neg_results *params,
    4497                 :            :                                    int freq, int ht40, int vht,
    4498                 :            :                                    const struct p2p_channels *channels)
    4499                 :            : {
    4500                 :            :         int res, *freqs;
    4501                 :            :         unsigned int pref_freq;
    4502                 :            :         unsigned int num, i;
    4503                 :            : 
    4504                 :         23 :         os_memset(params, 0, sizeof(*params));
    4505                 :         23 :         params->role_go = 1;
    4506                 :         23 :         params->ht40 = ht40;
    4507                 :         23 :         params->vht = vht;
    4508         [ +  + ]:         23 :         if (freq) {
    4509         [ -  + ]:         14 :                 if (!freq_included(channels, freq)) {
    4510                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
    4511                 :            :                                    "accepted", freq);
    4512                 :          0 :                         return -1;
    4513                 :            :                 }
    4514                 :         14 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
    4515                 :            :                            "frequency %d MHz", freq);
    4516                 :         14 :                 params->freq = freq;
    4517 [ -  + ][ #  # ]:          9 :         } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
    4518         [ #  # ]:          0 :                    wpa_s->conf->p2p_oper_channel >= 1 &&
    4519         [ #  # ]:          0 :                    wpa_s->conf->p2p_oper_channel <= 11 &&
    4520                 :          0 :                    freq_included(channels,
    4521                 :          0 :                                  2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
    4522                 :          0 :                 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
    4523                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
    4524                 :            :                            "frequency %d MHz", params->freq);
    4525 [ +  - ][ +  - ]:          9 :         } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
    4526         [ +  - ]:          9 :                     wpa_s->conf->p2p_oper_reg_class == 116 ||
    4527         [ +  - ]:          9 :                     wpa_s->conf->p2p_oper_reg_class == 117 ||
    4528         [ +  - ]:          9 :                     wpa_s->conf->p2p_oper_reg_class == 124 ||
    4529         [ -  + ]:          9 :                     wpa_s->conf->p2p_oper_reg_class == 126 ||
    4530         [ #  # ]:          0 :                     wpa_s->conf->p2p_oper_reg_class == 127) &&
    4531                 :          0 :                    freq_included(channels,
    4532                 :          0 :                                  5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
    4533                 :          0 :                 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
    4534                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
    4535                 :            :                            "frequency %d MHz", params->freq);
    4536 [ +  - ][ -  + ]:          9 :         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
    4537         [ #  # ]:          0 :                    wpa_s->best_overall_freq > 0 &&
    4538                 :          0 :                    p2p_supported_freq_go(wpa_s->global->p2p,
    4539         [ #  # ]:          0 :                                          wpa_s->best_overall_freq) &&
    4540                 :          0 :                    freq_included(channels, wpa_s->best_overall_freq)) {
    4541                 :          0 :                 params->freq = wpa_s->best_overall_freq;
    4542                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
    4543                 :            :                            "channel %d MHz", params->freq);
    4544 [ +  - ][ -  + ]:          9 :         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
    4545         [ #  # ]:          0 :                    wpa_s->best_24_freq > 0 &&
    4546                 :          0 :                    p2p_supported_freq_go(wpa_s->global->p2p,
    4547         [ #  # ]:          0 :                                          wpa_s->best_24_freq) &&
    4548                 :          0 :                    freq_included(channels, wpa_s->best_24_freq)) {
    4549                 :          0 :                 params->freq = wpa_s->best_24_freq;
    4550                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
    4551                 :            :                            "channel %d MHz", params->freq);
    4552 [ +  - ][ -  + ]:          9 :         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
    4553         [ #  # ]:          0 :                    wpa_s->best_5_freq > 0 &&
    4554                 :          0 :                    p2p_supported_freq_go(wpa_s->global->p2p,
    4555         [ #  # ]:          0 :                                          wpa_s->best_5_freq) &&
    4556                 :          0 :                    freq_included(channels, wpa_s->best_5_freq)) {
    4557                 :          0 :                 params->freq = wpa_s->best_5_freq;
    4558                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
    4559                 :            :                            "channel %d MHz", params->freq);
    4560         [ -  + ]:          9 :         } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
    4561                 :            :                                                   channels))) {
    4562                 :          0 :                 params->freq = pref_freq;
    4563                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
    4564                 :            :                            "channels", params->freq);
    4565                 :            :         } else {
    4566                 :            :                 int chan;
    4567         [ +  - ]:          9 :                 for (chan = 0; chan < 11; chan++) {
    4568                 :          9 :                         params->freq = 2412 + chan * 5;
    4569         [ +  - ]:          9 :                         if (!wpas_p2p_disallowed_freq(wpa_s->global,
    4570         [ +  - ]:          9 :                                                       params->freq) &&
    4571                 :          9 :                             freq_included(channels, params->freq))
    4572                 :          9 :                                 break;
    4573                 :            :                 }
    4574         [ -  + ]:          9 :                 if (chan == 11) {
    4575                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
    4576                 :            :                                    "allowed");
    4577                 :          0 :                         return -1;
    4578                 :            :                 }
    4579                 :          9 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
    4580                 :            :                            "known)", params->freq);
    4581                 :            :         }
    4582                 :            : 
    4583                 :         23 :         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
    4584         [ -  + ]:         23 :         if (!freqs)
    4585                 :          0 :                 return -1;
    4586                 :            : 
    4587                 :         23 :         res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
    4588                 :            :                                         wpa_s->num_multichan_concurrent);
    4589         [ -  + ]:         23 :         if (res < 0) {
    4590                 :          0 :                 os_free(freqs);
    4591                 :          0 :                 return -1;
    4592                 :            :         }
    4593                 :         23 :         num = res;
    4594                 :            : 
    4595         [ +  + ]:         23 :         for (i = 0; i < num; i++) {
    4596 [ -  + ][ #  # ]:          1 :                 if (freq && freqs[i] == freq)
    4597                 :          0 :                         break;
    4598 [ +  - ][ +  - ]:          1 :                 if (!freq && freq_included(channels, freqs[i])) {
    4599                 :          1 :                         wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
    4600                 :          1 :                                    freqs[i]);
    4601                 :          1 :                         params->freq = freqs[i];
    4602                 :          1 :                         break;
    4603                 :            :                 }
    4604                 :            :         }
    4605                 :            : 
    4606         [ +  + ]:         23 :         if (i == num) {
    4607         [ -  + ]:         22 :                 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
    4608         [ #  # ]:          0 :                         if (freq)
    4609                 :          0 :                                 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
    4610                 :            :                         else
    4611                 :          0 :                                 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
    4612                 :          0 :                         os_free(freqs);
    4613                 :          0 :                         return -1;
    4614         [ +  - ]:         22 :                 } else if (num == 0) {
    4615                 :         22 :                         wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
    4616                 :            :                 } else {
    4617                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
    4618                 :            :                 }
    4619                 :            :         }
    4620                 :            : 
    4621                 :         23 :         os_free(freqs);
    4622                 :         23 :         return 0;
    4623                 :            : }
    4624                 :            : 
    4625                 :            : 
    4626                 :            : static struct wpa_supplicant *
    4627                 :         51 : wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
    4628                 :            :                          int go)
    4629                 :            : {
    4630                 :            :         struct wpa_supplicant *group_wpa_s;
    4631                 :            : 
    4632         [ +  + ]:         51 :         if (!wpas_p2p_create_iface(wpa_s)) {
    4633                 :         45 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
    4634                 :            :                         "operations");
    4635                 :         45 :                 wpa_s->p2p_first_connection_timeout = 0;
    4636                 :         45 :                 return wpa_s;
    4637                 :            :         }
    4638                 :            : 
    4639 [ +  + ][ -  + ]:          6 :         if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
    4640                 :            :                                          WPA_IF_P2P_CLIENT) < 0) {
    4641                 :          0 :                 wpa_msg_global(wpa_s, MSG_ERROR,
    4642                 :            :                                "P2P: Failed to add group interface");
    4643                 :          0 :                 return NULL;
    4644                 :            :         }
    4645                 :          6 :         group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
    4646         [ -  + ]:          6 :         if (group_wpa_s == NULL) {
    4647                 :          0 :                 wpa_msg_global(wpa_s, MSG_ERROR,
    4648                 :            :                                "P2P: Failed to initialize group interface");
    4649                 :          0 :                 wpas_p2p_remove_pending_group_interface(wpa_s);
    4650                 :          0 :                 return NULL;
    4651                 :            :         }
    4652                 :            : 
    4653                 :          6 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
    4654                 :            :                 group_wpa_s->ifname);
    4655                 :          6 :         group_wpa_s->p2p_first_connection_timeout = 0;
    4656                 :         51 :         return group_wpa_s;
    4657                 :            : }
    4658                 :            : 
    4659                 :            : 
    4660                 :            : /**
    4661                 :            :  * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
    4662                 :            :  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
    4663                 :            :  * @persistent_group: Whether to create a persistent group
    4664                 :            :  * @freq: Frequency for the group or 0 to indicate no hardcoding
    4665                 :            :  * @ht40: Start GO with 40 MHz channel width
    4666                 :            :  * @vht:  Start GO with VHT support
    4667                 :            :  * Returns: 0 on success, -1 on failure
    4668                 :            :  *
    4669                 :            :  * This function creates a new P2P group with the local end as the Group Owner,
    4670                 :            :  * i.e., without using Group Owner Negotiation.
    4671                 :            :  */
    4672                 :         15 : int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
    4673                 :            :                        int freq, int ht40, int vht)
    4674                 :            : {
    4675                 :            :         struct p2p_go_neg_results params;
    4676                 :            : 
    4677 [ +  - ][ -  + ]:         15 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    4678                 :          0 :                 return -1;
    4679                 :            : 
    4680                 :         15 :         os_free(wpa_s->global->add_psk);
    4681                 :         15 :         wpa_s->global->add_psk = NULL;
    4682                 :            : 
    4683                 :            :         /* Make sure we are not running find during connection establishment */
    4684                 :         15 :         wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
    4685                 :         15 :         wpas_p2p_stop_find_oper(wpa_s);
    4686                 :            : 
    4687                 :         15 :         freq = wpas_p2p_select_go_freq(wpa_s, freq);
    4688         [ -  + ]:         15 :         if (freq < 0)
    4689                 :          0 :                 return -1;
    4690                 :            : 
    4691         [ -  + ]:         15 :         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
    4692                 :          0 :                 return -1;
    4693   [ +  -  -  + ]:         30 :         if (params.freq &&
    4694                 :         15 :             !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
    4695                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
    4696                 :            :                            "(%u MHz) is not supported for P2P uses",
    4697                 :            :                            params.freq);
    4698                 :          0 :                 return -1;
    4699                 :            :         }
    4700                 :         15 :         p2p_go_params(wpa_s->global->p2p, &params);
    4701                 :         15 :         params.persistent_group = persistent_group;
    4702                 :            : 
    4703                 :         15 :         wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
    4704         [ -  + ]:         15 :         if (wpa_s == NULL)
    4705                 :          0 :                 return -1;
    4706                 :         15 :         wpas_start_wps_go(wpa_s, &params, 0);
    4707                 :            : 
    4708                 :         15 :         return 0;
    4709                 :            : }
    4710                 :            : 
    4711                 :            : 
    4712                 :         10 : static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
    4713                 :            :                                  struct wpa_ssid *params, int addr_allocated)
    4714                 :            : {
    4715                 :            :         struct wpa_ssid *ssid;
    4716                 :            : 
    4717                 :         10 :         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
    4718         [ -  + ]:         10 :         if (wpa_s == NULL)
    4719                 :          0 :                 return -1;
    4720                 :         10 :         wpa_s->p2p_last_4way_hs_fail = NULL;
    4721                 :            : 
    4722                 :         10 :         wpa_supplicant_ap_deinit(wpa_s);
    4723                 :            : 
    4724                 :         10 :         ssid = wpa_config_add_network(wpa_s->conf);
    4725         [ -  + ]:         10 :         if (ssid == NULL)
    4726                 :          0 :                 return -1;
    4727                 :         10 :         wpa_config_set_network_defaults(ssid);
    4728                 :         10 :         ssid->temporary = 1;
    4729                 :         10 :         ssid->proto = WPA_PROTO_RSN;
    4730                 :         10 :         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
    4731                 :         10 :         ssid->group_cipher = WPA_CIPHER_CCMP;
    4732                 :         10 :         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
    4733                 :         10 :         ssid->ssid = os_malloc(params->ssid_len);
    4734         [ -  + ]:         10 :         if (ssid->ssid == NULL) {
    4735                 :          0 :                 wpa_config_remove_network(wpa_s->conf, ssid->id);
    4736                 :          0 :                 return -1;
    4737                 :            :         }
    4738                 :         10 :         os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
    4739                 :         10 :         ssid->ssid_len = params->ssid_len;
    4740                 :         10 :         ssid->p2p_group = 1;
    4741                 :         10 :         ssid->export_keys = 1;
    4742         [ +  - ]:         10 :         if (params->psk_set) {
    4743                 :         10 :                 os_memcpy(ssid->psk, params->psk, 32);
    4744                 :         10 :                 ssid->psk_set = 1;
    4745                 :            :         }
    4746         [ -  + ]:         10 :         if (params->passphrase)
    4747                 :          0 :                 ssid->passphrase = os_strdup(params->passphrase);
    4748                 :            : 
    4749                 :         10 :         wpa_s->show_group_started = 1;
    4750                 :            : 
    4751                 :         10 :         wpa_supplicant_select_network(wpa_s, ssid);
    4752                 :            : 
    4753                 :         10 :         return 0;
    4754                 :            : }
    4755                 :            : 
    4756                 :            : 
    4757                 :         18 : int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
    4758                 :            :                                   struct wpa_ssid *ssid, int addr_allocated,
    4759                 :            :                                   int freq, int ht40, int vht,
    4760                 :            :                                   const struct p2p_channels *channels,
    4761                 :            :                                   int connection_timeout)
    4762                 :            : {
    4763                 :            :         struct p2p_go_neg_results params;
    4764                 :         18 :         int go = 0;
    4765                 :            : 
    4766 [ +  - ][ -  + ]:         18 :         if (ssid->disabled != 2 || ssid->ssid == NULL)
    4767                 :          0 :                 return -1;
    4768                 :            : 
    4769 [ -  + ][ #  # ]:         18 :         if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
    4770                 :          0 :             go == (ssid->mode == WPAS_MODE_P2P_GO)) {
    4771                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
    4772                 :            :                            "already running");
    4773                 :          0 :                 return 0;
    4774                 :            :         }
    4775                 :            : 
    4776                 :         18 :         os_free(wpa_s->global->add_psk);
    4777                 :         18 :         wpa_s->global->add_psk = NULL;
    4778                 :            : 
    4779                 :            :         /* Make sure we are not running find during connection establishment */
    4780                 :         18 :         wpas_p2p_stop_find_oper(wpa_s);
    4781                 :            : 
    4782                 :         18 :         wpa_s->p2p_fallback_to_go_neg = 0;
    4783                 :            : 
    4784         [ +  + ]:         18 :         if (ssid->mode == WPAS_MODE_INFRA)
    4785                 :         10 :                 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
    4786                 :            : 
    4787         [ -  + ]:          8 :         if (ssid->mode != WPAS_MODE_P2P_GO)
    4788                 :          0 :                 return -1;
    4789                 :            : 
    4790                 :          8 :         freq = wpas_p2p_select_go_freq(wpa_s, freq);
    4791         [ -  + ]:          8 :         if (freq < 0)
    4792                 :          0 :                 return -1;
    4793                 :            : 
    4794         [ -  + ]:          8 :         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
    4795                 :          0 :                 return -1;
    4796                 :            : 
    4797                 :          8 :         params.role_go = 1;
    4798                 :          8 :         params.psk_set = ssid->psk_set;
    4799         [ +  - ]:          8 :         if (params.psk_set)
    4800                 :          8 :                 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
    4801         [ +  - ]:          8 :         if (ssid->passphrase) {
    4802         [ -  + ]:          8 :                 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
    4803                 :          0 :                         wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
    4804                 :            :                                    "persistent group");
    4805                 :          0 :                         return -1;
    4806                 :            :                 }
    4807                 :          8 :                 os_strlcpy(params.passphrase, ssid->passphrase,
    4808                 :            :                            sizeof(params.passphrase));
    4809                 :            :         }
    4810                 :          8 :         os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
    4811                 :          8 :         params.ssid_len = ssid->ssid_len;
    4812                 :          8 :         params.persistent_group = 1;
    4813                 :            : 
    4814                 :          8 :         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
    4815         [ -  + ]:          8 :         if (wpa_s == NULL)
    4816                 :          0 :                 return -1;
    4817                 :            : 
    4818                 :          8 :         wpa_s->p2p_first_connection_timeout = connection_timeout;
    4819                 :          8 :         wpas_start_wps_go(wpa_s, &params, 0);
    4820                 :            : 
    4821                 :         18 :         return 0;
    4822                 :            : }
    4823                 :            : 
    4824                 :            : 
    4825                 :        295 : static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
    4826                 :            :                                struct wpabuf *proberesp_ies)
    4827                 :            : {
    4828                 :        295 :         struct wpa_supplicant *wpa_s = ctx;
    4829         [ +  - ]:        295 :         if (wpa_s->ap_iface) {
    4830                 :        295 :                 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
    4831         [ -  + ]:        295 :                 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
    4832                 :          0 :                         wpabuf_free(beacon_ies);
    4833                 :          0 :                         wpabuf_free(proberesp_ies);
    4834                 :        295 :                         return;
    4835                 :            :                 }
    4836         [ +  + ]:        295 :                 if (beacon_ies) {
    4837                 :        110 :                         wpabuf_free(hapd->p2p_beacon_ie);
    4838                 :        110 :                         hapd->p2p_beacon_ie = beacon_ies;
    4839                 :            :                 }
    4840                 :        295 :                 wpabuf_free(hapd->p2p_probe_resp_ie);
    4841                 :        295 :                 hapd->p2p_probe_resp_ie = proberesp_ies;
    4842                 :            :         } else {
    4843                 :          0 :                 wpabuf_free(beacon_ies);
    4844                 :          0 :                 wpabuf_free(proberesp_ies);
    4845                 :            :         }
    4846                 :        295 :         wpa_supplicant_ap_update_beacon(wpa_s);
    4847                 :            : }
    4848                 :            : 
    4849                 :            : 
    4850                 :        214 : static void wpas_p2p_idle_update(void *ctx, int idle)
    4851                 :            : {
    4852                 :        214 :         struct wpa_supplicant *wpa_s = ctx;
    4853         [ -  + ]:        214 :         if (!wpa_s->ap_iface)
    4854                 :        214 :                 return;
    4855         [ +  + ]:        214 :         wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
    4856         [ +  + ]:        214 :         if (idle)
    4857                 :        113 :                 wpas_p2p_set_group_idle_timeout(wpa_s);
    4858                 :            :         else
    4859                 :        101 :                 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
    4860                 :            : }
    4861                 :            : 
    4862                 :            : 
    4863                 :         55 : struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
    4864                 :            :                                        struct wpa_ssid *ssid)
    4865                 :            : {
    4866                 :            :         struct p2p_group *group;
    4867                 :            :         struct p2p_group_config *cfg;
    4868                 :            : 
    4869 [ +  - ][ -  + ]:         55 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    4870                 :          0 :                 return NULL;
    4871                 :            : 
    4872                 :         55 :         cfg = os_zalloc(sizeof(*cfg));
    4873         [ -  + ]:         55 :         if (cfg == NULL)
    4874                 :          0 :                 return NULL;
    4875                 :            : 
    4876 [ +  + ][ +  + ]:         55 :         if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
    4877                 :         12 :                 cfg->persistent_group = 2;
    4878         [ +  + ]:         43 :         else if (ssid->p2p_persistent_group)
    4879                 :          1 :                 cfg->persistent_group = 1;
    4880                 :         55 :         os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
    4881 [ -  + ][ #  # ]:         55 :         if (wpa_s->max_stations &&
    4882                 :          0 :             wpa_s->max_stations < wpa_s->conf->max_num_sta)
    4883                 :          0 :                 cfg->max_clients = wpa_s->max_stations;
    4884                 :            :         else
    4885                 :         55 :                 cfg->max_clients = wpa_s->conf->max_num_sta;
    4886                 :         55 :         os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
    4887                 :         55 :         cfg->ssid_len = ssid->ssid_len;
    4888                 :         55 :         cfg->cb_ctx = wpa_s;
    4889                 :         55 :         cfg->ie_update = wpas_p2p_ie_update;
    4890                 :         55 :         cfg->idle_update = wpas_p2p_idle_update;
    4891                 :            : 
    4892                 :         55 :         group = p2p_group_init(wpa_s->global->p2p, cfg);
    4893         [ -  + ]:         55 :         if (group == NULL)
    4894                 :          0 :                 os_free(cfg);
    4895         [ +  + ]:         55 :         if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
    4896                 :         23 :                 p2p_group_notif_formation_done(group);
    4897                 :         55 :         wpa_s->p2p_group = group;
    4898                 :         55 :         return group;
    4899                 :            : }
    4900                 :            : 
    4901                 :            : 
    4902                 :        128 : void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
    4903                 :            :                           int registrar)
    4904                 :            : {
    4905                 :        128 :         struct wpa_ssid *ssid = wpa_s->current_ssid;
    4906                 :            : 
    4907         [ +  + ]:        128 :         if (!wpa_s->p2p_in_provisioning) {
    4908                 :         46 :                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
    4909                 :            :                            "provisioning not in progress");
    4910                 :        128 :                 return;
    4911                 :            :         }
    4912                 :            : 
    4913 [ +  - ][ +  + ]:         82 :         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
    4914                 :            :                 u8 go_dev_addr[ETH_ALEN];
    4915                 :         50 :                 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
    4916                 :         50 :                 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
    4917                 :            :                                           ssid->ssid_len);
    4918                 :            :                 /* Clear any stored provisioning info */
    4919                 :         50 :                 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
    4920                 :            :         }
    4921                 :            : 
    4922                 :         82 :         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
    4923                 :            :                              NULL);
    4924                 :         82 :         wpa_s->p2p_go_group_formation_completed = 1;
    4925 [ +  - ][ +  + ]:         82 :         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
    4926                 :            :                 /*
    4927                 :            :                  * Use a separate timeout for initial data connection to
    4928                 :            :                  * complete to allow the group to be removed automatically if
    4929                 :            :                  * something goes wrong in this step before the P2P group idle
    4930                 :            :                  * timeout mechanism is taken into use.
    4931                 :            :                  */
    4932                 :         50 :                 wpa_dbg(wpa_s, MSG_DEBUG,
    4933                 :            :                         "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
    4934                 :            :                         P2P_MAX_INITIAL_CONN_WAIT);
    4935                 :         50 :                 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
    4936                 :            :                                        wpas_p2p_group_formation_timeout,
    4937                 :         50 :                                        wpa_s->parent, NULL);
    4938         [ +  - ]:         32 :         } else if (ssid) {
    4939                 :            :                 /*
    4940                 :            :                  * Use a separate timeout for initial data connection to
    4941                 :            :                  * complete to allow the group to be removed automatically if
    4942                 :            :                  * the client does not complete data connection successfully.
    4943                 :            :                  */
    4944                 :         32 :                 wpa_dbg(wpa_s, MSG_DEBUG,
    4945                 :            :                         "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
    4946                 :            :                         P2P_MAX_INITIAL_CONN_WAIT_GO);
    4947                 :         32 :                 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
    4948                 :            :                                        wpas_p2p_group_formation_timeout,
    4949                 :         32 :                                        wpa_s->parent, NULL);
    4950                 :            :                 /*
    4951                 :            :                  * Complete group formation on first successful data connection
    4952                 :            :                  */
    4953                 :         32 :                 wpa_s->p2p_go_group_formation_completed = 0;
    4954                 :            :         }
    4955         [ +  - ]:         82 :         if (wpa_s->global->p2p)
    4956                 :         82 :                 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
    4957                 :         82 :         wpas_group_formation_completed(wpa_s, 1);
    4958                 :            : }
    4959                 :            : 
    4960                 :            : 
    4961                 :         12 : void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
    4962                 :            :                          struct wps_event_fail *fail)
    4963                 :            : {
    4964         [ +  - ]:         12 :         if (!wpa_s->p2p_in_provisioning) {
    4965                 :         12 :                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
    4966                 :            :                            "provisioning not in progress");
    4967                 :         12 :                 return;
    4968                 :            :         }
    4969                 :            : 
    4970         [ #  # ]:          0 :         if (wpa_s->go_params) {
    4971                 :          0 :                 p2p_clear_provisioning_info(
    4972                 :          0 :                         wpa_s->global->p2p,
    4973                 :          0 :                         wpa_s->go_params->peer_device_addr);
    4974                 :            :         }
    4975                 :            : 
    4976                 :          0 :         wpas_notify_p2p_wps_failed(wpa_s, fail);
    4977                 :            : }
    4978                 :            : 
    4979                 :            : 
    4980                 :          3 : int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
    4981                 :            :                        const char *config_method,
    4982                 :            :                        enum wpas_p2p_prov_disc_use use)
    4983                 :            : {
    4984                 :            :         u16 config_methods;
    4985                 :            : 
    4986                 :          3 :         wpa_s->p2p_fallback_to_go_neg = 0;
    4987                 :          3 :         wpa_s->pending_pd_use = NORMAL_PD;
    4988         [ +  + ]:          3 :         if (os_strncmp(config_method, "display", 7) == 0)
    4989                 :          1 :                 config_methods = WPS_CONFIG_DISPLAY;
    4990         [ +  + ]:          2 :         else if (os_strncmp(config_method, "keypad", 6) == 0)
    4991                 :          1 :                 config_methods = WPS_CONFIG_KEYPAD;
    4992 [ -  + ][ #  # ]:          1 :         else if (os_strncmp(config_method, "pbc", 3) == 0 ||
    4993                 :          0 :                  os_strncmp(config_method, "pushbutton", 10) == 0)
    4994                 :          1 :                 config_methods = WPS_CONFIG_PUSHBUTTON;
    4995                 :            :         else {
    4996                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
    4997                 :          0 :                 return -1;
    4998                 :            :         }
    4999                 :            : 
    5000         [ -  + ]:          3 :         if (use == WPAS_P2P_PD_AUTO) {
    5001                 :          0 :                 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
    5002                 :          0 :                 wpa_s->pending_pd_config_methods = config_methods;
    5003                 :          0 :                 wpa_s->p2p_auto_pd = 1;
    5004                 :          0 :                 wpa_s->p2p_auto_join = 0;
    5005                 :          0 :                 wpa_s->pending_pd_before_join = 0;
    5006                 :          0 :                 wpa_s->auto_pd_scan_retry = 0;
    5007                 :          0 :                 wpas_p2p_stop_find(wpa_s);
    5008                 :          0 :                 wpa_s->p2p_join_scan_count = 0;
    5009                 :          0 :                 os_get_reltime(&wpa_s->p2p_auto_started);
    5010                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
    5011                 :            :                            wpa_s->p2p_auto_started.sec,
    5012                 :            :                            wpa_s->p2p_auto_started.usec);
    5013                 :          0 :                 wpas_p2p_join_scan(wpa_s, NULL);
    5014                 :          0 :                 return 0;
    5015                 :            :         }
    5016                 :            : 
    5017 [ +  - ][ -  + ]:          3 :         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
    5018                 :          0 :                 return -1;
    5019                 :            : 
    5020                 :          3 :         return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
    5021                 :            :                                  config_methods, use == WPAS_P2P_PD_FOR_JOIN,
    5022                 :            :                                  0, 1);
    5023                 :            : }
    5024                 :            : 
    5025                 :            : 
    5026                 :         29 : int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
    5027                 :            :                               char *end)
    5028                 :            : {
    5029                 :         29 :         return p2p_scan_result_text(ies, ies_len, buf, end);
    5030                 :            : }
    5031                 :            : 
    5032                 :            : 
    5033                 :        895 : static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
    5034                 :            : {
    5035         [ +  - ]:        895 :         if (!offchannel_pending_action_tx(wpa_s))
    5036                 :        895 :                 return;
    5037                 :            : 
    5038                 :          0 :         wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
    5039                 :            :                    "operation request");
    5040                 :          0 :         offchannel_clear_pending_action_tx(wpa_s);
    5041                 :            : }
    5042                 :            : 
    5043                 :            : 
    5044                 :        101 : int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
    5045                 :            :                   enum p2p_discovery_type type,
    5046                 :            :                   unsigned int num_req_dev_types, const u8 *req_dev_types,
    5047                 :            :                   const u8 *dev_id, unsigned int search_delay)
    5048                 :            : {
    5049                 :        101 :         wpas_p2p_clear_pending_action_tx(wpa_s);
    5050                 :        101 :         wpa_s->p2p_long_listen = 0;
    5051                 :            : 
    5052 [ +  - ][ +  - ]:        101 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
                 [ -  + ]
    5053                 :        101 :             wpa_s->p2p_in_provisioning)
    5054                 :          0 :                 return -1;
    5055                 :            : 
    5056                 :        101 :         wpa_supplicant_cancel_sched_scan(wpa_s);
    5057                 :            : 
    5058                 :        101 :         return p2p_find(wpa_s->global->p2p, timeout, type,
    5059                 :            :                         num_req_dev_types, req_dev_types, dev_id,
    5060                 :            :                         search_delay);
    5061                 :            : }
    5062                 :            : 
    5063                 :            : 
    5064                 :        708 : static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
    5065                 :            : {
    5066                 :        708 :         wpas_p2p_clear_pending_action_tx(wpa_s);
    5067                 :        708 :         wpa_s->p2p_long_listen = 0;
    5068                 :        708 :         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
    5069                 :        708 :         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
    5070                 :        708 :         wpa_s->global->p2p_cb_on_scan_complete = 0;
    5071                 :            : 
    5072         [ +  - ]:        708 :         if (wpa_s->global->p2p)
    5073                 :        708 :                 p2p_stop_find(wpa_s->global->p2p);
    5074                 :            : 
    5075                 :        708 :         return 0;
    5076                 :            : }
    5077                 :            : 
    5078                 :            : 
    5079                 :        675 : void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
    5080                 :            : {
    5081         [ -  + ]:        675 :         if (wpas_p2p_stop_find_oper(wpa_s) > 0)
    5082                 :        675 :                 return;
    5083                 :        675 :         wpas_p2p_remove_pending_group_interface(wpa_s);
    5084                 :            : }
    5085                 :            : 
    5086                 :            : 
    5087                 :          0 : static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
    5088                 :            : {
    5089                 :          0 :         struct wpa_supplicant *wpa_s = eloop_ctx;
    5090                 :          0 :         wpa_s->p2p_long_listen = 0;
    5091                 :          0 : }
    5092                 :            : 
    5093                 :            : 
    5094                 :         86 : int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
    5095                 :            : {
    5096                 :            :         int res;
    5097                 :            : 
    5098 [ +  - ][ -  + ]:         86 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5099                 :          0 :                 return -1;
    5100                 :            : 
    5101                 :         86 :         wpa_supplicant_cancel_sched_scan(wpa_s);
    5102                 :         86 :         wpas_p2p_clear_pending_action_tx(wpa_s);
    5103                 :            : 
    5104         [ +  - ]:         86 :         if (timeout == 0) {
    5105                 :            :                 /*
    5106                 :            :                  * This is a request for unlimited Listen state. However, at
    5107                 :            :                  * least for now, this is mapped to a Listen state for one
    5108                 :            :                  * hour.
    5109                 :            :                  */
    5110                 :         86 :                 timeout = 3600;
    5111                 :            :         }
    5112                 :         86 :         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
    5113                 :         86 :         wpa_s->p2p_long_listen = 0;
    5114                 :            : 
    5115                 :            :         /*
    5116                 :            :          * Stop previous find/listen operation to avoid trying to request a new
    5117                 :            :          * remain-on-channel operation while the driver is still running the
    5118                 :            :          * previous one.
    5119                 :            :          */
    5120         [ +  - ]:         86 :         if (wpa_s->global->p2p)
    5121                 :         86 :                 p2p_stop_find(wpa_s->global->p2p);
    5122                 :            : 
    5123                 :         86 :         res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
    5124 [ +  - ][ +  - ]:         86 :         if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
    5125                 :         86 :                 wpa_s->p2p_long_listen = timeout * 1000;
    5126                 :         86 :                 eloop_register_timeout(timeout, 0,
    5127                 :            :                                        wpas_p2p_long_listen_timeout,
    5128                 :            :                                        wpa_s, NULL);
    5129                 :            :         }
    5130                 :            : 
    5131                 :         86 :         return res;
    5132                 :            : }
    5133                 :            : 
    5134                 :            : 
    5135                 :        431 : int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
    5136                 :            :                           u8 *buf, size_t len, int p2p_group)
    5137                 :            : {
    5138                 :            :         struct wpabuf *p2p_ie;
    5139                 :            :         int ret;
    5140                 :            : 
    5141         [ +  + ]:        431 :         if (wpa_s->global->p2p_disabled)
    5142                 :          3 :                 return -1;
    5143         [ -  + ]:        428 :         if (wpa_s->global->p2p == NULL)
    5144                 :          0 :                 return -1;
    5145         [ +  + ]:        428 :         if (bss == NULL)
    5146                 :          6 :                 return -1;
    5147                 :            : 
    5148                 :        422 :         p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
    5149                 :        422 :         ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
    5150                 :            :                                p2p_group, p2p_ie);
    5151                 :        422 :         wpabuf_free(p2p_ie);
    5152                 :            : 
    5153                 :        431 :         return ret;
    5154                 :            : }
    5155                 :            : 
    5156                 :            : 
    5157                 :        365 : int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
    5158                 :            :                           const u8 *dst, const u8 *bssid,
    5159                 :            :                           const u8 *ie, size_t ie_len, int ssi_signal)
    5160                 :            : {
    5161         [ -  + ]:        365 :         if (wpa_s->global->p2p_disabled)
    5162                 :          0 :                 return 0;
    5163         [ -  + ]:        365 :         if (wpa_s->global->p2p == NULL)
    5164                 :          0 :                 return 0;
    5165                 :            : 
    5166      [ -  +  + ]:        365 :         switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
    5167                 :            :                                  ie, ie_len)) {
    5168                 :            :         case P2P_PREQ_NOT_P2P:
    5169                 :          0 :                 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
    5170                 :            :                                  ssi_signal);
    5171                 :            :                 /* fall through */
    5172                 :            :         case P2P_PREQ_MALFORMED:
    5173                 :            :         case P2P_PREQ_NOT_LISTEN:
    5174                 :            :         case P2P_PREQ_NOT_PROCESSED:
    5175                 :            :         default: /* make gcc happy */
    5176                 :        362 :                 return 0;
    5177                 :            :         case P2P_PREQ_PROCESSED:
    5178                 :        365 :                 return 1;
    5179                 :            :         }
    5180                 :            : }
    5181                 :            : 
    5182                 :            : 
    5183                 :        192 : void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
    5184                 :            :                         const u8 *sa, const u8 *bssid,
    5185                 :            :                         u8 category, const u8 *data, size_t len, int freq)
    5186                 :            : {
    5187         [ -  + ]:        192 :         if (wpa_s->global->p2p_disabled)
    5188                 :          0 :                 return;
    5189         [ -  + ]:        192 :         if (wpa_s->global->p2p == NULL)
    5190                 :          0 :                 return;
    5191                 :            : 
    5192                 :        192 :         p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
    5193                 :            :                       freq);
    5194                 :            : }
    5195                 :            : 
    5196                 :            : 
    5197                 :        434 : void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
    5198                 :            : {
    5199         [ +  + ]:        434 :         if (wpa_s->global->p2p_disabled)
    5200                 :          1 :                 return;
    5201         [ -  + ]:        433 :         if (wpa_s->global->p2p == NULL)
    5202                 :          0 :                 return;
    5203                 :            : 
    5204                 :        434 :         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
    5205                 :            : }
    5206                 :            : 
    5207                 :            : 
    5208                 :         55 : void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
    5209                 :            : {
    5210                 :         55 :         p2p_group_deinit(wpa_s->p2p_group);
    5211                 :         55 :         wpa_s->p2p_group = NULL;
    5212                 :            : 
    5213                 :         55 :         wpa_s->ap_configured_cb = NULL;
    5214                 :         55 :         wpa_s->ap_configured_cb_ctx = NULL;
    5215                 :         55 :         wpa_s->ap_configured_cb_data = NULL;
    5216                 :         55 :         wpa_s->connect_without_scan = NULL;
    5217                 :         55 : }
    5218                 :            : 
    5219                 :            : 
    5220                 :          0 : int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
    5221                 :            : {
    5222                 :          0 :         wpa_s->p2p_long_listen = 0;
    5223                 :            : 
    5224 [ #  # ][ #  # ]:          0 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5225                 :          0 :                 return -1;
    5226                 :            : 
    5227                 :          0 :         return p2p_reject(wpa_s->global->p2p, addr);
    5228                 :            : }
    5229                 :            : 
    5230                 :            : 
    5231                 :            : /* Invite to reinvoke a persistent group */
    5232                 :          7 : int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
    5233                 :            :                     struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
    5234                 :            :                     int ht40, int vht, int pref_freq)
    5235                 :            : {
    5236                 :            :         enum p2p_invite_role role;
    5237                 :          7 :         u8 *bssid = NULL;
    5238                 :          7 :         int force_freq = 0;
    5239                 :            :         int res;
    5240                 :          7 :         int no_pref_freq_given = pref_freq == 0;
    5241                 :            : 
    5242                 :          7 :         wpa_s->global->p2p_invite_group = NULL;
    5243         [ +  - ]:          7 :         if (peer_addr)
    5244                 :          7 :                 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
    5245                 :            :         else
    5246                 :          0 :                 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
    5247                 :            : 
    5248                 :          7 :         wpa_s->p2p_persistent_go_freq = freq;
    5249                 :          7 :         wpa_s->p2p_go_ht40 = !!ht40;
    5250         [ +  + ]:          7 :         if (ssid->mode == WPAS_MODE_P2P_GO) {
    5251                 :          3 :                 role = P2P_INVITE_ROLE_GO;
    5252         [ -  + ]:          3 :                 if (peer_addr == NULL) {
    5253                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Missing peer "
    5254                 :            :                                    "address in invitation command");
    5255                 :          0 :                         return -1;
    5256                 :            :                 }
    5257         [ -  + ]:          3 :                 if (wpas_p2p_create_iface(wpa_s)) {
    5258         [ #  # ]:          0 :                         if (wpas_p2p_add_group_interface(wpa_s,
    5259                 :            :                                                          WPA_IF_P2P_GO) < 0) {
    5260                 :          0 :                                 wpa_printf(MSG_ERROR, "P2P: Failed to "
    5261                 :            :                                            "allocate a new interface for the "
    5262                 :            :                                            "group");
    5263                 :          0 :                                 return -1;
    5264                 :            :                         }
    5265                 :          0 :                         bssid = wpa_s->pending_interface_addr;
    5266                 :            :                 } else
    5267                 :          3 :                         bssid = wpa_s->own_addr;
    5268                 :            :         } else {
    5269                 :          4 :                 role = P2P_INVITE_ROLE_CLIENT;
    5270                 :          4 :                 peer_addr = ssid->bssid;
    5271                 :            :         }
    5272                 :          7 :         wpa_s->pending_invite_ssid_id = ssid->id;
    5273                 :            : 
    5274                 :          7 :         res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
    5275                 :            :                                    role == P2P_INVITE_ROLE_GO);
    5276         [ -  + ]:          7 :         if (res)
    5277                 :          0 :                 return res;
    5278                 :            : 
    5279 [ +  - ][ -  + ]:          7 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5280                 :          0 :                 return -1;
    5281                 :            : 
    5282 [ -  + ][ #  # ]:          7 :         if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
    5283 [ #  # ][ #  # ]:          0 :             no_pref_freq_given && pref_freq > 0 &&
    5284         [ #  # ]:          0 :             wpa_s->num_multichan_concurrent > 1 &&
    5285                 :          0 :             wpas_p2p_num_unused_channels(wpa_s) > 0) {
    5286                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
    5287                 :            :                            pref_freq);
    5288                 :          0 :                 pref_freq = 0;
    5289                 :            :         }
    5290                 :            : 
    5291                 :          7 :         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
    5292                 :          7 :                           ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
    5293                 :            :                           1, pref_freq);
    5294                 :            : }
    5295                 :            : 
    5296                 :            : 
    5297                 :            : /* Invite to join an active group */
    5298                 :          4 : int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
    5299                 :            :                           const u8 *peer_addr, const u8 *go_dev_addr)
    5300                 :            : {
    5301                 :          4 :         struct wpa_global *global = wpa_s->global;
    5302                 :            :         enum p2p_invite_role role;
    5303                 :          4 :         u8 *bssid = NULL;
    5304                 :            :         struct wpa_ssid *ssid;
    5305                 :            :         int persistent;
    5306                 :          4 :         int freq = 0, force_freq = 0, pref_freq = 0;
    5307                 :            :         int res;
    5308                 :            : 
    5309                 :          4 :         wpa_s->p2p_persistent_go_freq = 0;
    5310                 :          4 :         wpa_s->p2p_go_ht40 = 0;
    5311                 :          4 :         wpa_s->p2p_go_vht = 0;
    5312                 :            : 
    5313         [ +  - ]:          4 :         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    5314         [ +  - ]:          4 :                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
    5315                 :          4 :                         break;
    5316                 :            :         }
    5317         [ -  + ]:          4 :         if (wpa_s == NULL) {
    5318                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
    5319                 :          0 :                 return -1;
    5320                 :            :         }
    5321                 :            : 
    5322                 :          4 :         ssid = wpa_s->current_ssid;
    5323         [ -  + ]:          4 :         if (ssid == NULL) {
    5324                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
    5325                 :            :                            "invitation");
    5326                 :          0 :                 return -1;
    5327                 :            :         }
    5328                 :            : 
    5329                 :          4 :         wpa_s->global->p2p_invite_group = wpa_s;
    5330   [ +  +  -  + ]:          6 :         persistent = ssid->p2p_persistent_group &&
    5331                 :          2 :                 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
    5332                 :          2 :                                         ssid->ssid, ssid->ssid_len);
    5333                 :            : 
    5334         [ +  - ]:          4 :         if (ssid->mode == WPAS_MODE_P2P_GO) {
    5335                 :          4 :                 role = P2P_INVITE_ROLE_ACTIVE_GO;
    5336                 :          4 :                 bssid = wpa_s->own_addr;
    5337         [ +  - ]:          4 :                 if (go_dev_addr == NULL)
    5338                 :          4 :                         go_dev_addr = wpa_s->global->p2p_dev_addr;
    5339                 :          4 :                 freq = ssid->frequency;
    5340                 :            :         } else {
    5341                 :          0 :                 role = P2P_INVITE_ROLE_CLIENT;
    5342         [ #  # ]:          0 :                 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
    5343                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
    5344                 :            :                                    "invite to current group");
    5345                 :          0 :                         return -1;
    5346                 :            :                 }
    5347                 :          0 :                 bssid = wpa_s->bssid;
    5348   [ #  #  #  # ]:          0 :                 if (go_dev_addr == NULL &&
    5349                 :          0 :                     !is_zero_ether_addr(wpa_s->go_dev_addr))
    5350                 :          0 :                         go_dev_addr = wpa_s->go_dev_addr;
    5351         [ #  # ]:          0 :                 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
    5352                 :          0 :                         (int) wpa_s->assoc_freq;
    5353                 :            :         }
    5354                 :          4 :         wpa_s->parent->pending_invite_ssid_id = -1;
    5355                 :            : 
    5356 [ +  - ][ -  + ]:          4 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5357                 :          0 :                 return -1;
    5358                 :            : 
    5359                 :          4 :         res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
    5360                 :            :                                    role == P2P_INVITE_ROLE_ACTIVE_GO);
    5361         [ -  + ]:          4 :         if (res)
    5362                 :          0 :                 return res;
    5363                 :          4 :         wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
    5364                 :            : 
    5365                 :          4 :         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
    5366                 :          4 :                           ssid->ssid, ssid->ssid_len, force_freq,
    5367                 :            :                           go_dev_addr, persistent, pref_freq);
    5368                 :            : }
    5369                 :            : 
    5370                 :            : 
    5371                 :        376 : void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
    5372                 :            : {
    5373                 :        376 :         struct wpa_ssid *ssid = wpa_s->current_ssid;
    5374                 :            :         const char *ssid_txt;
    5375                 :            :         u8 go_dev_addr[ETH_ALEN];
    5376                 :        376 :         int network_id = -1;
    5377                 :            :         int persistent;
    5378                 :            :         int freq;
    5379                 :            : 
    5380 [ +  - ][ +  + ]:        376 :         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
    5381                 :        344 :                 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    5382                 :        344 :                                      wpa_s->parent, NULL);
    5383                 :            :         }
    5384                 :            : 
    5385 [ +  + ][ +  - ]:        376 :         if (!wpa_s->show_group_started || !ssid)
    5386                 :            :                 goto done;
    5387                 :            : 
    5388                 :         58 :         wpa_s->show_group_started = 0;
    5389                 :            : 
    5390                 :         58 :         ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
    5391                 :         58 :         os_memset(go_dev_addr, 0, ETH_ALEN);
    5392         [ +  + ]:         58 :         if (ssid->bssid_set)
    5393                 :         50 :                 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
    5394                 :         58 :         persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
    5395                 :            :                                                ssid->ssid_len);
    5396                 :         58 :         os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
    5397                 :            : 
    5398         [ -  + ]:         58 :         if (wpa_s->global->p2p_group_formation == wpa_s)
    5399                 :          0 :                 wpa_s->global->p2p_group_formation = NULL;
    5400                 :            : 
    5401         [ +  - ]:         58 :         freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
    5402                 :          0 :                 (int) wpa_s->assoc_freq;
    5403 [ +  - ][ +  + ]:        115 :         if (ssid->passphrase == NULL && ssid->psk_set) {
    5404                 :            :                 char psk[65];
    5405                 :         57 :                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
    5406         [ +  + ]:         57 :                 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
    5407                 :            :                                "%s client ssid=\"%s\" freq=%d psk=%s "
    5408                 :            :                                "go_dev_addr=" MACSTR "%s",
    5409                 :         57 :                                wpa_s->ifname, ssid_txt, freq, psk,
    5410                 :        342 :                                MAC2STR(go_dev_addr),
    5411                 :            :                                persistent ? " [PERSISTENT]" : "");
    5412                 :            :         } else {
    5413 [ -  + ][ -  + ]:          1 :                 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
    5414                 :            :                                "%s client ssid=\"%s\" freq=%d "
    5415                 :            :                                "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
    5416                 :          1 :                                wpa_s->ifname, ssid_txt, freq,
    5417                 :          1 :                                ssid->passphrase ? ssid->passphrase : "",
    5418                 :          6 :                                MAC2STR(go_dev_addr),
    5419                 :            :                                persistent ? " [PERSISTENT]" : "");
    5420                 :            :         }
    5421                 :            : 
    5422         [ +  + ]:         58 :         if (persistent)
    5423                 :         16 :                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
    5424                 :            :                                                              ssid, go_dev_addr);
    5425         [ +  + ]:         58 :         if (network_id < 0)
    5426                 :         42 :                 network_id = ssid->id;
    5427                 :         58 :         wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
    5428                 :            : 
    5429                 :            : done:
    5430                 :        376 :         wpas_p2p_continue_after_scan(wpa_s);
    5431                 :        376 : }
    5432                 :            : 
    5433                 :            : 
    5434                 :          0 : int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
    5435                 :            :                           u32 interval1, u32 duration2, u32 interval2)
    5436                 :            : {
    5437 [ #  # ][ #  # ]:          0 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5438                 :          0 :                 return -1;
    5439                 :            : 
    5440 [ #  # ][ #  # ]:          0 :         if (wpa_s->wpa_state < WPA_ASSOCIATED ||
    5441         [ #  # ]:          0 :             wpa_s->current_ssid == NULL ||
    5442                 :          0 :             wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
    5443                 :          0 :                 return -1;
    5444                 :            : 
    5445                 :          0 :         return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
    5446                 :          0 :                                 wpa_s->own_addr, wpa_s->assoc_freq,
    5447                 :            :                                 duration1, interval1, duration2, interval2);
    5448                 :            : }
    5449                 :            : 
    5450                 :            : 
    5451                 :          0 : int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
    5452                 :            :                         unsigned int interval)
    5453                 :            : {
    5454 [ #  # ][ #  # ]:          0 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5455                 :          0 :                 return -1;
    5456                 :            : 
    5457                 :          0 :         return p2p_ext_listen(wpa_s->global->p2p, period, interval);
    5458                 :            : }
    5459                 :            : 
    5460                 :            : 
    5461                 :          0 : static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
    5462                 :            : {
    5463         [ #  # ]:          0 :         if (wpa_s->current_ssid == NULL) {
    5464                 :            :                 /*
    5465                 :            :                  * current_ssid can be cleared when P2P client interface gets
    5466                 :            :                  * disconnected, so assume this interface was used as P2P
    5467                 :            :                  * client.
    5468                 :            :                  */
    5469                 :          0 :                 return 1;
    5470                 :            :         }
    5471 [ #  # ][ #  # ]:          0 :         return wpa_s->current_ssid->p2p_group &&
    5472                 :          0 :                 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
    5473                 :            : }
    5474                 :            : 
    5475                 :            : 
    5476                 :          0 : static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
    5477                 :            : {
    5478                 :          0 :         struct wpa_supplicant *wpa_s = eloop_ctx;
    5479                 :            : 
    5480 [ #  # ][ #  # ]:          0 :         if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
    5481                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
    5482                 :            :                            "disabled");
    5483                 :          0 :                 return;
    5484                 :            :         }
    5485                 :            : 
    5486                 :          0 :         wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
    5487                 :            :                    "group");
    5488                 :          0 :         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
    5489                 :            : }
    5490                 :            : 
    5491                 :            : 
    5492                 :        592 : static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
    5493                 :            : {
    5494                 :            :         int timeout;
    5495                 :            : 
    5496         [ -  + ]:        592 :         if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
    5497                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
    5498                 :            : 
    5499 [ +  + ][ +  + ]:        592 :         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
    5500                 :        367 :                 return;
    5501                 :            : 
    5502                 :        225 :         timeout = wpa_s->conf->p2p_group_idle;
    5503 [ +  + ][ -  + ]:        225 :         if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
    5504         [ #  # ]:          0 :             (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
    5505                 :        112 :             timeout = P2P_MAX_CLIENT_IDLE;
    5506                 :            : 
    5507         [ +  + ]:        225 :         if (timeout == 0)
    5508                 :        113 :                 return;
    5509                 :            : 
    5510         [ -  + ]:        112 :         if (timeout < 0) {
    5511         [ #  # ]:          0 :                 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
    5512                 :          0 :                         timeout = 0; /* special client mode no-timeout */
    5513                 :            :                 else
    5514                 :          0 :                         return;
    5515                 :            :         }
    5516                 :            : 
    5517         [ -  + ]:        112 :         if (wpa_s->p2p_in_provisioning) {
    5518                 :            :                 /*
    5519                 :            :                  * Use the normal group formation timeout during the
    5520                 :            :                  * provisioning phase to avoid terminating this process too
    5521                 :            :                  * early due to group idle timeout.
    5522                 :            :                  */
    5523                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
    5524                 :            :                            "during provisioning");
    5525                 :          0 :                 return;
    5526                 :            :         }
    5527                 :            : 
    5528         [ +  + ]:        112 :         if (wpa_s->show_group_started) {
    5529                 :            :                 /*
    5530                 :            :                  * Use the normal group formation timeout between the end of
    5531                 :            :                  * the provisioning phase and completion of 4-way handshake to
    5532                 :            :                  * avoid terminating this process too early due to group idle
    5533                 :            :                  * timeout.
    5534                 :            :                  */
    5535                 :         55 :                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
    5536                 :            :                            "while waiting for initial 4-way handshake to "
    5537                 :            :                            "complete");
    5538                 :         55 :                 return;
    5539                 :            :         }
    5540                 :            : 
    5541                 :         57 :         wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
    5542                 :            :                    timeout);
    5543                 :        592 :         eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
    5544                 :            :                                wpa_s, NULL);
    5545                 :            : }
    5546                 :            : 
    5547                 :            : 
    5548                 :            : /* Returns 1 if the interface was removed */
    5549                 :        760 : int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
    5550                 :            :                           u16 reason_code, const u8 *ie, size_t ie_len,
    5551                 :            :                           int locally_generated)
    5552                 :            : {
    5553 [ +  + ][ -  + ]:        760 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5554                 :          6 :                 return 0;
    5555                 :            : 
    5556         [ +  + ]:        754 :         if (!locally_generated)
    5557                 :         53 :                 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
    5558                 :            :                                  ie_len);
    5559                 :            : 
    5560 [ +  + ][ +  + ]:        754 :         if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
                 [ +  + ]
    5561         [ +  + ]:         40 :             wpa_s->current_ssid &&
    5562         [ +  - ]:         39 :             wpa_s->current_ssid->p2p_group &&
    5563                 :         39 :             wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
    5564                 :         39 :                 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
    5565                 :            :                            "session is ending");
    5566         [ +  + ]:         39 :                 if (wpas_p2p_group_delete(wpa_s,
    5567                 :            :                                           P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
    5568                 :            :                     > 0)
    5569                 :          6 :                         return 1;
    5570                 :            :         }
    5571                 :            : 
    5572                 :        760 :         return 0;
    5573                 :            : }
    5574                 :            : 
    5575                 :            : 
    5576                 :          0 : void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
    5577                 :            :                              u16 reason_code, const u8 *ie, size_t ie_len,
    5578                 :            :                              int locally_generated)
    5579                 :            : {
    5580 [ #  # ][ #  # ]:          0 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5581                 :          0 :                 return;
    5582                 :            : 
    5583         [ #  # ]:          0 :         if (!locally_generated)
    5584                 :          0 :                 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
    5585                 :            :                                    ie_len);
    5586                 :            : }
    5587                 :            : 
    5588                 :            : 
    5589                 :       5430 : void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
    5590                 :            : {
    5591                 :       5430 :         struct p2p_data *p2p = wpa_s->global->p2p;
    5592                 :            : 
    5593         [ -  + ]:       5430 :         if (p2p == NULL)
    5594                 :          0 :                 return;
    5595                 :            : 
    5596         [ -  + ]:       5430 :         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
    5597                 :          0 :                 return;
    5598                 :            : 
    5599         [ +  + ]:       5430 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
    5600                 :          3 :                 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
    5601                 :            : 
    5602         [ -  + ]:       5430 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
    5603                 :          0 :                 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
    5604                 :            : 
    5605 [ +  - ][ -  + ]:       5430 :         if (wpa_s->wps &&
    5606                 :       5430 :             (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
    5607                 :          0 :                 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
    5608                 :            : 
    5609 [ +  - ][ -  + ]:       5430 :         if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
    5610                 :          0 :                 p2p_set_uuid(p2p, wpa_s->wps->uuid);
    5611                 :            : 
    5612         [ -  + ]:       5430 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
    5613                 :          0 :                 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
    5614                 :          0 :                 p2p_set_model_name(p2p, wpa_s->conf->model_name);
    5615                 :          0 :                 p2p_set_model_number(p2p, wpa_s->conf->model_number);
    5616                 :          0 :                 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
    5617                 :            :         }
    5618                 :            : 
    5619         [ -  + ]:       5430 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
    5620                 :          0 :                 p2p_set_sec_dev_types(p2p,
    5621                 :          0 :                                       (void *) wpa_s->conf->sec_device_type,
    5622                 :          0 :                                       wpa_s->conf->num_sec_device_types);
    5623                 :            : 
    5624         [ -  + ]:       5430 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
    5625                 :            :                 int i;
    5626                 :          0 :                 p2p_remove_wps_vendor_extensions(p2p);
    5627         [ #  # ]:          0 :                 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
    5628         [ #  # ]:          0 :                         if (wpa_s->conf->wps_vendor_ext[i] == NULL)
    5629                 :          0 :                                 continue;
    5630                 :          0 :                         p2p_add_wps_vendor_extension(
    5631                 :          0 :                                 p2p, wpa_s->conf->wps_vendor_ext[i]);
    5632                 :            :                 }
    5633                 :            :         }
    5634                 :            : 
    5635 [ -  + ][ #  # ]:       5430 :         if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
    5636         [ #  # ]:          0 :             wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
    5637                 :            :                 char country[3];
    5638                 :          0 :                 country[0] = wpa_s->conf->country[0];
    5639                 :          0 :                 country[1] = wpa_s->conf->country[1];
    5640                 :          0 :                 country[2] = 0x04;
    5641                 :          0 :                 p2p_set_country(p2p, country);
    5642                 :            :         }
    5643                 :            : 
    5644         [ -  + ]:       5430 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
    5645         [ #  # ]:          0 :                 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
    5646                 :          0 :                                      wpa_s->conf->p2p_ssid_postfix ?
    5647                 :          0 :                                      os_strlen(wpa_s->conf->p2p_ssid_postfix) :
    5648                 :            :                                      0);
    5649                 :            :         }
    5650                 :            : 
    5651         [ -  + ]:       5430 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
    5652                 :          0 :                 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
    5653                 :            : 
    5654         [ -  + ]:       5430 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
    5655                 :            :                 u8 reg_class, channel;
    5656                 :            :                 int ret;
    5657                 :            :                 unsigned int r;
    5658 [ #  # ][ #  # ]:          0 :                 if (wpa_s->conf->p2p_listen_reg_class &&
    5659                 :          0 :                     wpa_s->conf->p2p_listen_channel) {
    5660                 :          0 :                         reg_class = wpa_s->conf->p2p_listen_reg_class;
    5661                 :          0 :                         channel = wpa_s->conf->p2p_listen_channel;
    5662                 :            :                 } else {
    5663                 :          0 :                         reg_class = 81;
    5664                 :            :                         /*
    5665                 :            :                          * Pick one of the social channels randomly as the
    5666                 :            :                          * listen channel.
    5667                 :            :                          */
    5668                 :          0 :                         os_get_random((u8 *) &r, sizeof(r));
    5669                 :          0 :                         channel = 1 + (r % 3) * 5;
    5670                 :            :                 }
    5671                 :          0 :                 ret = p2p_set_listen_channel(p2p, reg_class, channel);
    5672         [ #  # ]:          0 :                 if (ret)
    5673                 :          0 :                         wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
    5674                 :            :                                    "failed: %d", ret);
    5675                 :            :         }
    5676         [ -  + ]:       5430 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
    5677                 :            :                 u8 op_reg_class, op_channel, cfg_op_channel;
    5678                 :          0 :                 int ret = 0;
    5679                 :            :                 unsigned int r;
    5680 [ #  # ][ #  # ]:          0 :                 if (wpa_s->conf->p2p_oper_reg_class &&
    5681                 :          0 :                     wpa_s->conf->p2p_oper_channel) {
    5682                 :          0 :                         op_reg_class = wpa_s->conf->p2p_oper_reg_class;
    5683                 :          0 :                         op_channel = wpa_s->conf->p2p_oper_channel;
    5684                 :          0 :                         cfg_op_channel = 1;
    5685                 :            :                 } else {
    5686                 :          0 :                         op_reg_class = 81;
    5687                 :            :                         /*
    5688                 :            :                          * Use random operation channel from (1, 6, 11)
    5689                 :            :                          *if no other preference is indicated.
    5690                 :            :                          */
    5691                 :          0 :                         os_get_random((u8 *) &r, sizeof(r));
    5692                 :          0 :                         op_channel = 1 + (r % 3) * 5;
    5693                 :          0 :                         cfg_op_channel = 0;
    5694                 :            :                 }
    5695                 :          0 :                 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
    5696                 :            :                                            cfg_op_channel);
    5697         [ #  # ]:          0 :                 if (ret)
    5698                 :          0 :                         wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
    5699                 :            :                                    "failed: %d", ret);
    5700                 :            :         }
    5701                 :            : 
    5702         [ +  + ]:       5430 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
    5703         [ -  + ]:       1312 :                 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
    5704                 :       1312 :                                       wpa_s->conf->p2p_pref_chan) < 0) {
    5705                 :          0 :                         wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
    5706                 :            :                                    "update failed");
    5707                 :            :                 }
    5708                 :            : 
    5709         [ -  + ]:       1312 :                 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
    5710                 :       5430 :                         wpa_printf(MSG_ERROR, "P2P: No GO channel list "
    5711                 :            :                                    "update failed");
    5712                 :            :                 }
    5713                 :            :         }
    5714                 :            : }
    5715                 :            : 
    5716                 :            : 
    5717                 :          0 : int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
    5718                 :            :                      int duration)
    5719                 :            : {
    5720         [ #  # ]:          0 :         if (!wpa_s->ap_iface)
    5721                 :          0 :                 return -1;
    5722                 :          0 :         return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
    5723                 :            :                                    duration);
    5724                 :            : }
    5725                 :            : 
    5726                 :            : 
    5727                 :          0 : int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
    5728                 :            : {
    5729 [ #  # ][ #  # ]:          0 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5730                 :          0 :                 return -1;
    5731                 :            : 
    5732                 :          0 :         wpa_s->global->cross_connection = enabled;
    5733                 :          0 :         p2p_set_cross_connect(wpa_s->global->p2p, enabled);
    5734                 :            : 
    5735         [ #  # ]:          0 :         if (!enabled) {
    5736                 :            :                 struct wpa_supplicant *iface;
    5737                 :            : 
    5738         [ #  # ]:          0 :                 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
    5739                 :            :                 {
    5740         [ #  # ]:          0 :                         if (iface->cross_connect_enabled == 0)
    5741                 :          0 :                                 continue;
    5742                 :            : 
    5743                 :          0 :                         iface->cross_connect_enabled = 0;
    5744                 :          0 :                         iface->cross_connect_in_use = 0;
    5745                 :          0 :                         wpa_msg_global(iface->parent, MSG_INFO,
    5746                 :            :                                        P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
    5747                 :          0 :                                        iface->ifname,
    5748                 :          0 :                                        iface->cross_connect_uplink);
    5749                 :            :                 }
    5750                 :            :         }
    5751                 :            : 
    5752                 :          0 :         return 0;
    5753                 :            : }
    5754                 :            : 
    5755                 :            : 
    5756                 :        335 : static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
    5757                 :            : {
    5758                 :            :         struct wpa_supplicant *iface;
    5759                 :            : 
    5760         [ +  - ]:        335 :         if (!uplink->global->cross_connection)
    5761                 :        335 :                 return;
    5762                 :            : 
    5763         [ #  # ]:          0 :         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
    5764         [ #  # ]:          0 :                 if (!iface->cross_connect_enabled)
    5765                 :          0 :                         continue;
    5766         [ #  # ]:          0 :                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
    5767                 :            :                     0)
    5768                 :          0 :                         continue;
    5769         [ #  # ]:          0 :                 if (iface->ap_iface == NULL)
    5770                 :          0 :                         continue;
    5771         [ #  # ]:          0 :                 if (iface->cross_connect_in_use)
    5772                 :          0 :                         continue;
    5773                 :            : 
    5774                 :          0 :                 iface->cross_connect_in_use = 1;
    5775                 :          0 :                 wpa_msg_global(iface->parent, MSG_INFO,
    5776                 :            :                                P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
    5777                 :          0 :                                iface->ifname, iface->cross_connect_uplink);
    5778                 :            :         }
    5779                 :            : }
    5780                 :            : 
    5781                 :            : 
    5782                 :        578 : static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
    5783                 :            : {
    5784                 :            :         struct wpa_supplicant *iface;
    5785                 :            : 
    5786         [ +  + ]:       1184 :         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
    5787         [ +  - ]:        606 :                 if (!iface->cross_connect_enabled)
    5788                 :        606 :                         continue;
    5789         [ #  # ]:          0 :                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
    5790                 :            :                     0)
    5791                 :          0 :                         continue;
    5792         [ #  # ]:          0 :                 if (!iface->cross_connect_in_use)
    5793                 :          0 :                         continue;
    5794                 :            : 
    5795                 :          0 :                 wpa_msg_global(iface->parent, MSG_INFO,
    5796                 :            :                                P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
    5797                 :          0 :                                iface->ifname, iface->cross_connect_uplink);
    5798                 :          0 :                 iface->cross_connect_in_use = 0;
    5799                 :            :         }
    5800                 :        578 : }
    5801                 :            : 
    5802                 :            : 
    5803                 :        399 : void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
    5804                 :            : {
    5805 [ +  + ][ +  - ]:        399 :         if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
                 [ +  + ]
    5806         [ -  + ]:        335 :             wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
    5807                 :        335 :             wpa_s->cross_connect_disallowed)
    5808                 :         64 :                 wpas_p2p_disable_cross_connect(wpa_s);
    5809                 :            :         else
    5810                 :        335 :                 wpas_p2p_enable_cross_connect(wpa_s);
    5811   [ +  +  -  + ]:        743 :         if (!wpa_s->ap_iface &&
    5812                 :        344 :             eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
    5813                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
    5814                 :        399 : }
    5815                 :            : 
    5816                 :            : 
    5817                 :        514 : void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
    5818                 :            : {
    5819                 :        514 :         wpas_p2p_disable_cross_connect(wpa_s);
    5820   [ +  +  +  - ]:        938 :         if (!wpa_s->ap_iface &&
    5821                 :        424 :             !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
    5822                 :            :                                          wpa_s, NULL))
    5823                 :        424 :                 wpas_p2p_set_group_idle_timeout(wpa_s);
    5824                 :        514 : }
    5825                 :            : 
    5826                 :            : 
    5827                 :         55 : static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
    5828                 :            : {
    5829                 :            :         struct wpa_supplicant *iface;
    5830                 :            : 
    5831         [ +  - ]:         55 :         if (!wpa_s->global->cross_connection)
    5832                 :         55 :                 return;
    5833                 :            : 
    5834         [ #  # ]:          0 :         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
    5835         [ #  # ]:          0 :                 if (iface == wpa_s)
    5836                 :          0 :                         continue;
    5837         [ #  # ]:          0 :                 if (iface->drv_flags &
    5838                 :            :                     WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
    5839                 :          0 :                         continue;
    5840         [ #  # ]:          0 :                 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
    5841                 :          0 :                         continue;
    5842                 :            : 
    5843                 :          0 :                 wpa_s->cross_connect_enabled = 1;
    5844                 :          0 :                 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
    5845                 :            :                            sizeof(wpa_s->cross_connect_uplink));
    5846                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
    5847                 :            :                            "%s to %s whenever uplink is available",
    5848                 :          0 :                            wpa_s->ifname, wpa_s->cross_connect_uplink);
    5849                 :            : 
    5850 [ #  # ][ #  # ]:          0 :                 if (iface->ap_iface || iface->current_ssid == NULL ||
                 [ #  # ]
    5851         [ #  # ]:          0 :                     iface->current_ssid->mode != WPAS_MODE_INFRA ||
    5852         [ #  # ]:          0 :                     iface->cross_connect_disallowed ||
    5853                 :          0 :                     iface->wpa_state != WPA_COMPLETED)
    5854                 :            :                         break;
    5855                 :            : 
    5856                 :          0 :                 wpa_s->cross_connect_in_use = 1;
    5857                 :          0 :                 wpa_msg_global(wpa_s->parent, MSG_INFO,
    5858                 :            :                                P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
    5859                 :          0 :                                wpa_s->ifname, wpa_s->cross_connect_uplink);
    5860                 :          0 :                 break;
    5861                 :            :         }
    5862                 :            : }
    5863                 :            : 
    5864                 :            : 
    5865                 :          3 : int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
    5866                 :            : {
    5867 [ +  - ][ +  - ]:          3 :         if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
    5868                 :          3 :             !wpa_s->p2p_in_provisioning)
    5869                 :          3 :                 return 0; /* not P2P client operation */
    5870                 :            : 
    5871                 :          0 :         wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
    5872                 :            :                    "session overlap");
    5873         [ #  # ]:          0 :         if (wpa_s != wpa_s->parent)
    5874                 :          0 :                 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
    5875                 :          0 :         wpas_p2p_group_formation_failed(wpa_s);
    5876                 :          3 :         return 1;
    5877                 :            : }
    5878                 :            : 
    5879                 :            : 
    5880                 :        987 : void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
    5881                 :            : {
    5882                 :            :         struct p2p_channels chan, cli_chan;
    5883                 :            : 
    5884 [ +  - ][ -  + ]:        987 :         if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
    5885                 :          0 :                 return;
    5886                 :            : 
    5887                 :        987 :         os_memset(&chan, 0, sizeof(chan));
    5888                 :        987 :         os_memset(&cli_chan, 0, sizeof(cli_chan));
    5889         [ -  + ]:        987 :         if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
    5890                 :          0 :                 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
    5891                 :            :                            "channel list");
    5892                 :          0 :                 return;
    5893                 :            :         }
    5894                 :            : 
    5895                 :        987 :         p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
    5896                 :            : }
    5897                 :            : 
    5898                 :            : 
    5899                 :          0 : static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
    5900                 :            :                                      struct wpa_scan_results *scan_res)
    5901                 :            : {
    5902                 :          0 :         wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
    5903                 :          0 : }
    5904                 :            : 
    5905                 :            : 
    5906                 :          0 : int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
    5907                 :            : {
    5908                 :          0 :         struct wpa_global *global = wpa_s->global;
    5909                 :          0 :         int found = 0;
    5910                 :            :         const u8 *peer;
    5911                 :            : 
    5912         [ #  # ]:          0 :         if (global->p2p == NULL)
    5913                 :          0 :                 return -1;
    5914                 :            : 
    5915                 :          0 :         wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
    5916                 :            : 
    5917   [ #  #  #  # ]:          0 :         if (wpa_s->pending_interface_name[0] &&
    5918                 :          0 :             !is_zero_ether_addr(wpa_s->pending_interface_addr))
    5919                 :          0 :                 found = 1;
    5920                 :            : 
    5921                 :          0 :         peer = p2p_get_go_neg_peer(global->p2p);
    5922         [ #  # ]:          0 :         if (peer) {
    5923                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
    5924                 :          0 :                            MACSTR, MAC2STR(peer));
    5925                 :          0 :                 p2p_unauthorize(global->p2p, peer);
    5926                 :          0 :                 found = 1;
    5927                 :            :         }
    5928                 :            : 
    5929         [ #  # ]:          0 :         if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
    5930                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
    5931                 :          0 :                 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
    5932                 :          0 :                 found = 1;
    5933                 :            :         }
    5934                 :            : 
    5935         [ #  # ]:          0 :         if (wpa_s->pending_pd_before_join) {
    5936                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
    5937                 :          0 :                 wpa_s->pending_pd_before_join = 0;
    5938                 :          0 :                 found = 1;
    5939                 :            :         }
    5940                 :            : 
    5941                 :          0 :         wpas_p2p_stop_find(wpa_s);
    5942                 :            : 
    5943         [ #  # ]:          0 :         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    5944 [ #  # ][ #  # ]:          0 :                 if (wpa_s == global->p2p_group_formation &&
    5945         [ #  # ]:          0 :                     (wpa_s->p2p_in_provisioning ||
    5946                 :          0 :                      wpa_s->parent->pending_interface_type ==
    5947                 :            :                      WPA_IF_P2P_CLIENT)) {
    5948                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
    5949                 :            :                                    "formation found - cancelling",
    5950                 :          0 :                                    wpa_s->ifname);
    5951                 :          0 :                         found = 1;
    5952                 :          0 :                         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    5953                 :          0 :                                              wpa_s->parent, NULL);
    5954         [ #  # ]:          0 :                         if (wpa_s->p2p_in_provisioning) {
    5955                 :          0 :                                 wpas_group_formation_completed(wpa_s, 0);
    5956                 :          0 :                                 break;
    5957                 :            :                         }
    5958                 :          0 :                         wpas_p2p_group_delete(wpa_s,
    5959                 :            :                                               P2P_GROUP_REMOVAL_REQUESTED);
    5960                 :          0 :                         break;
    5961                 :            :                 }
    5962                 :            :         }
    5963                 :            : 
    5964         [ #  # ]:          0 :         if (!found) {
    5965                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
    5966                 :          0 :                 return -1;
    5967                 :            :         }
    5968                 :            : 
    5969                 :          0 :         return 0;
    5970                 :            : }
    5971                 :            : 
    5972                 :            : 
    5973                 :          0 : void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
    5974                 :            : {
    5975 [ #  # ][ #  # ]:          0 :         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
    5976                 :          0 :                 return;
    5977                 :            : 
    5978                 :          0 :         wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
    5979                 :            :                    "being available anymore");
    5980                 :          0 :         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
    5981                 :            : }
    5982                 :            : 
    5983                 :            : 
    5984                 :          0 : void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
    5985                 :            :                                    int freq_24, int freq_5, int freq_overall)
    5986                 :            : {
    5987                 :          0 :         struct p2p_data *p2p = wpa_s->global->p2p;
    5988         [ #  # ]:          0 :         if (p2p == NULL)
    5989                 :          0 :                 return;
    5990                 :          0 :         p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
    5991                 :            : }
    5992                 :            : 
    5993                 :            : 
    5994                 :          0 : int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
    5995                 :            : {
    5996                 :            :         u8 peer[ETH_ALEN];
    5997                 :          0 :         struct p2p_data *p2p = wpa_s->global->p2p;
    5998                 :            : 
    5999         [ #  # ]:          0 :         if (p2p == NULL)
    6000                 :          0 :                 return -1;
    6001                 :            : 
    6002         [ #  # ]:          0 :         if (hwaddr_aton(addr, peer))
    6003                 :          0 :                 return -1;
    6004                 :            : 
    6005                 :          0 :         return p2p_unauthorize(p2p, peer);
    6006                 :            : }
    6007                 :            : 
    6008                 :            : 
    6009                 :            : /**
    6010                 :            :  * wpas_p2p_disconnect - Disconnect from a P2P Group
    6011                 :            :  * @wpa_s: Pointer to wpa_supplicant data
    6012                 :            :  * Returns: 0 on success, -1 on failure
    6013                 :            :  *
    6014                 :            :  * This can be used to disconnect from a group in which the local end is a P2P
    6015                 :            :  * Client or to end a P2P Group in case the local end is the Group Owner. If a
    6016                 :            :  * virtual network interface was created for this group, that interface will be
    6017                 :            :  * removed. Otherwise, only the configured P2P group network will be removed
    6018                 :            :  * from the interface.
    6019                 :            :  */
    6020                 :         85 : int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
    6021                 :            : {
    6022                 :            : 
    6023         [ +  + ]:         85 :         if (wpa_s == NULL)
    6024                 :          3 :                 return -1;
    6025                 :            : 
    6026                 :        167 :         return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
    6027         [ +  + ]:         82 :                 -1 : 0;
    6028                 :            : }
    6029                 :            : 
    6030                 :            : 
    6031                 :        491 : int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
    6032                 :            : {
    6033                 :            :         int ret;
    6034                 :            : 
    6035 [ +  + ][ -  + ]:        491 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    6036                 :          2 :                 return 0;
    6037                 :            : 
    6038                 :        489 :         ret = p2p_in_progress(wpa_s->global->p2p);
    6039         [ +  - ]:        489 :         if (ret == 0) {
    6040                 :            :                 /*
    6041                 :            :                  * Check whether there is an ongoing WPS provisioning step (or
    6042                 :            :                  * other parts of group formation) on another interface since
    6043                 :            :                  * p2p_in_progress() does not report this to avoid issues for
    6044                 :            :                  * scans during such provisioning step.
    6045                 :            :                  */
    6046 [ +  + ][ -  + ]:        489 :                 if (wpa_s->global->p2p_group_formation &&
    6047                 :        100 :                     wpa_s->global->p2p_group_formation != wpa_s) {
    6048                 :          0 :                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
    6049                 :            :                                 "in group formation",
    6050                 :            :                                 wpa_s->global->p2p_group_formation->ifname);
    6051                 :          0 :                         ret = 1;
    6052                 :            :                 }
    6053                 :            :         }
    6054                 :            : 
    6055 [ +  - ][ -  + ]:        489 :         if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
    6056                 :            :                 struct os_reltime now;
    6057                 :          0 :                 os_get_reltime(&now);
    6058         [ #  # ]:          0 :                 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
    6059                 :            :                                        P2P_MAX_INITIAL_CONN_WAIT_GO)) {
    6060                 :            :                         /* Wait for the first client has expired */
    6061                 :          0 :                         wpa_s->global->p2p_go_wait_client.sec = 0;
    6062                 :            :                 } else {
    6063                 :          0 :                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
    6064                 :          0 :                         ret = 1;
    6065                 :            :                 }
    6066                 :            :         }
    6067                 :            : 
    6068                 :        491 :         return ret;
    6069                 :            : }
    6070                 :            : 
    6071                 :            : 
    6072                 :        312 : void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
    6073                 :            :                               struct wpa_ssid *ssid)
    6074                 :            : {
    6075         [ -  + ]:        312 :         if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
           [ #  #  #  # ]
    6076                 :          0 :             eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    6077                 :          0 :                                  wpa_s->parent, NULL) > 0) {
    6078                 :            :                 /**
    6079                 :            :                  * Remove the network by scheduling the group formation
    6080                 :            :                  * timeout to happen immediately. The teardown code
    6081                 :            :                  * needs to be scheduled to run asynch later so that we
    6082                 :            :                  * don't delete data from under ourselves unexpectedly.
    6083                 :            :                  * Calling wpas_p2p_group_formation_timeout directly
    6084                 :            :                  * causes a series of crashes in WPS failure scenarios.
    6085                 :            :                  */
    6086                 :          0 :                 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
    6087                 :            :                            "P2P group network getting removed");
    6088                 :          0 :                 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
    6089                 :          0 :                                        wpa_s->parent, NULL);
    6090                 :            :         }
    6091                 :        312 : }
    6092                 :            : 
    6093                 :            : 
    6094                 :        180 : struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
    6095                 :            :                                           const u8 *addr, const u8 *ssid,
    6096                 :            :                                           size_t ssid_len)
    6097                 :            : {
    6098                 :            :         struct wpa_ssid *s;
    6099                 :            :         size_t i;
    6100                 :            : 
    6101         [ +  + ]:        203 :         for (s = wpa_s->conf->ssid; s; s = s->next) {
    6102         [ +  + ]:         53 :                 if (s->disabled != 2)
    6103                 :         19 :                         continue;
    6104 [ +  + ][ +  - ]:         34 :                 if (ssid &&
    6105         [ -  + ]:         14 :                     (ssid_len != s->ssid_len ||
    6106                 :         14 :                      os_memcmp(ssid, s->ssid, ssid_len) != 0))
    6107                 :          0 :                         continue;
    6108         [ +  + ]:         34 :                 if (addr == NULL) {
    6109         [ +  - ]:         10 :                         if (s->mode == WPAS_MODE_P2P_GO)
    6110                 :         10 :                                 return s;
    6111                 :          0 :                         continue;
    6112                 :            :                 }
    6113         [ +  + ]:         24 :                 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
    6114                 :         14 :                         return s; /* peer is GO in the persistent group */
    6115 [ +  - ][ -  + ]:         10 :                 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
    6116                 :          0 :                         continue;
    6117         [ +  + ]:         10 :                 for (i = 0; i < s->num_p2p_clients; i++) {
    6118         [ +  - ]:          6 :                         if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
    6119                 :            :                                       addr, ETH_ALEN) == 0)
    6120                 :          6 :                                 return s; /* peer is P2P client in persistent
    6121                 :            :                                            * group */
    6122                 :            :                 }
    6123                 :            :         }
    6124                 :            : 
    6125                 :        180 :         return NULL;
    6126                 :            : }
    6127                 :            : 
    6128                 :            : 
    6129                 :         60 : void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
    6130                 :            :                                        const u8 *addr)
    6131                 :            : {
    6132         [ +  + ]:         60 :         if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    6133                 :         60 :                                  wpa_s->parent, NULL) > 0) {
    6134                 :            :                 /*
    6135                 :            :                  * This can happen if WPS provisioning step is not terminated
    6136                 :            :                  * cleanly (e.g., P2P Client does not send WSC_Done). Since the
    6137                 :            :                  * peer was able to connect, there is no need to time out group
    6138                 :            :                  * formation after this, though. In addition, this is used with
    6139                 :            :                  * the initial connection wait on the GO as a separate formation
    6140                 :            :                  * timeout and as such, expected to be hit after the initial WPS
    6141                 :            :                  * provisioning step.
    6142                 :            :                  */
    6143                 :         38 :                 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
    6144                 :            :         }
    6145         [ +  + ]:         60 :         if (!wpa_s->p2p_go_group_formation_completed) {
    6146                 :         42 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
    6147                 :         42 :                 wpa_s->p2p_go_group_formation_completed = 1;
    6148                 :         42 :                 wpa_s->global->p2p_group_formation = NULL;
    6149                 :         42 :                 wpa_s->p2p_in_provisioning = 0;
    6150                 :            :         }
    6151                 :         60 :         wpa_s->global->p2p_go_wait_client.sec = 0;
    6152         [ +  + ]:         60 :         if (addr == NULL)
    6153                 :         60 :                 return;
    6154                 :         57 :         wpas_p2p_add_persistent_group_client(wpa_s, addr);
    6155                 :            : }
    6156                 :            : 
    6157                 :            : 
    6158                 :          0 : static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
    6159                 :            :                                         int group_added)
    6160                 :            : {
    6161                 :          0 :         struct wpa_supplicant *group = wpa_s;
    6162         [ #  # ]:          0 :         if (wpa_s->global->p2p_group_formation)
    6163                 :          0 :                 group = wpa_s->global->p2p_group_formation;
    6164                 :          0 :         wpa_s = wpa_s->parent;
    6165                 :          0 :         offchannel_send_action_done(wpa_s);
    6166         [ #  # ]:          0 :         if (group_added)
    6167                 :          0 :                 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
    6168                 :          0 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
    6169                 :          0 :         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
    6170                 :          0 :                          wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
    6171                 :            :                          0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
    6172                 :            :                          wpa_s->p2p_persistent_id,
    6173                 :          0 :                          wpa_s->p2p_pd_before_go_neg,
    6174                 :          0 :                          wpa_s->p2p_go_ht40,
    6175                 :          0 :                          wpa_s->p2p_go_vht);
    6176                 :          0 : }
    6177                 :            : 
    6178                 :            : 
    6179                 :        103 : int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
    6180                 :            : {
    6181 [ -  + ][ #  # ]:        103 :         if (!wpa_s->p2p_fallback_to_go_neg ||
    6182                 :          0 :             wpa_s->p2p_in_provisioning <= 5)
    6183                 :        103 :                 return 0;
    6184                 :            : 
    6185         [ #  # ]:          0 :         if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
    6186                 :          0 :                 return 0; /* peer operating as a GO */
    6187                 :            : 
    6188                 :          0 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
    6189                 :            :                 "fallback to GO Negotiation");
    6190                 :          0 :         wpas_p2p_fallback_to_go_neg(wpa_s, 1);
    6191                 :            : 
    6192                 :        103 :         return 1;
    6193                 :            : }
    6194                 :            : 
    6195                 :            : 
    6196                 :        103 : unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
    6197                 :            : {
    6198                 :            :         struct wpa_supplicant *ifs;
    6199                 :            : 
    6200         [ +  + ]:        103 :         if (wpa_s->wpa_state > WPA_SCANNING) {
    6201                 :          4 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
    6202                 :            :                         "concurrent operation",
    6203                 :            :                         P2P_CONCURRENT_SEARCH_DELAY);
    6204                 :          4 :                 return P2P_CONCURRENT_SEARCH_DELAY;
    6205                 :            :         }
    6206                 :            : 
    6207         [ +  + ]:        198 :         dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
    6208                 :            :                          radio_list) {
    6209 [ -  + ][ #  # ]:         99 :                 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
    6210                 :          0 :                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
    6211                 :            :                                 "delay due to concurrent operation on "
    6212                 :            :                                 "interface %s",
    6213                 :            :                                 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
    6214                 :          0 :                         return P2P_CONCURRENT_SEARCH_DELAY;
    6215                 :            :                 }
    6216                 :            :         }
    6217                 :            : 
    6218                 :        103 :         return 0;
    6219                 :            : }
    6220                 :            : 
    6221                 :            : 
    6222                 :       2827 : void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
    6223                 :            : {
    6224                 :       2827 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
    6225                 :            :                 "pending anymore (sta_scan_pending=%d "
    6226                 :            :                 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
    6227                 :            :                 wpa_s->global->p2p_cb_on_scan_complete);
    6228                 :       2827 :         wpa_s->sta_scan_pending = 0;
    6229                 :            : 
    6230         [ +  + ]:       2827 :         if (!wpa_s->global->p2p_cb_on_scan_complete)
    6231                 :       2825 :                 return;
    6232                 :          2 :         wpa_s->global->p2p_cb_on_scan_complete = 0;
    6233                 :            : 
    6234 [ +  - ][ -  + ]:          2 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    6235                 :          0 :                 return;
    6236                 :            : 
    6237         [ +  - ]:          2 :         if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
    6238                 :          2 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
    6239                 :            :                         "continued after successful connection");
    6240                 :       2827 :                 p2p_increase_search_delay(wpa_s->global->p2p,
    6241                 :            :                                           wpas_p2p_search_delay(wpa_s));
    6242                 :            :         }
    6243                 :            : }
    6244                 :            : 
    6245                 :            : 
    6246                 :          5 : static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
    6247                 :            :                                      struct wpa_ssid *s, const u8 *addr,
    6248                 :            :                                      int iface_addr)
    6249                 :            : {
    6250                 :            :         struct psk_list_entry *psk, *tmp;
    6251                 :          5 :         int changed = 0;
    6252                 :            : 
    6253         [ +  + ]:         10 :         dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
    6254                 :            :                               list) {
    6255 [ -  + ][ #  # ]:          5 :                 if ((iface_addr && !psk->p2p &&
                 [ #  # ]
    6256         [ +  - ]:          5 :                      os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
    6257 [ +  - ][ +  + ]:          5 :                     (!iface_addr && psk->p2p &&
    6258                 :          5 :                      os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
    6259                 :          3 :                         wpa_dbg(wpa_s, MSG_DEBUG,
    6260                 :            :                                 "P2P: Remove persistent group PSK list entry for "
    6261                 :            :                                 MACSTR " p2p=%u",
    6262                 :            :                                 MAC2STR(psk->addr), psk->p2p);
    6263                 :          3 :                         dl_list_del(&psk->list);
    6264                 :          3 :                         os_free(psk);
    6265                 :          3 :                         changed++;
    6266                 :            :                 }
    6267                 :            :         }
    6268                 :            : 
    6269                 :          5 :         return changed;
    6270                 :            : }
    6271                 :            : 
    6272                 :            : 
    6273                 :          8 : void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
    6274                 :            :                          const u8 *p2p_dev_addr,
    6275                 :            :                          const u8 *psk, size_t psk_len)
    6276                 :            : {
    6277                 :          8 :         struct wpa_ssid *ssid = wpa_s->current_ssid;
    6278                 :            :         struct wpa_ssid *persistent;
    6279                 :            :         struct psk_list_entry *p;
    6280                 :            : 
    6281         [ -  + ]:          8 :         if (psk_len != sizeof(p->psk))
    6282                 :          0 :                 return;
    6283                 :            : 
    6284         [ +  - ]:          8 :         if (p2p_dev_addr) {
    6285                 :          8 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
    6286                 :            :                         " p2p_dev_addr=" MACSTR,
    6287                 :            :                         MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
    6288         [ +  + ]:          8 :                 if (is_zero_ether_addr(p2p_dev_addr))
    6289                 :          1 :                         p2p_dev_addr = NULL;
    6290                 :            :         } else {
    6291                 :          0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
    6292                 :            :                         MAC2STR(mac_addr));
    6293                 :            :         }
    6294                 :            : 
    6295         [ +  + ]:          8 :         if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
    6296                 :          4 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
    6297                 :            :                 /* To be added to persistent group once created */
    6298         [ +  - ]:          4 :                 if (wpa_s->global->add_psk == NULL) {
    6299                 :          4 :                         wpa_s->global->add_psk = os_zalloc(sizeof(*p));
    6300         [ -  + ]:          4 :                         if (wpa_s->global->add_psk == NULL)
    6301                 :          0 :                                 return;
    6302                 :            :                 }
    6303                 :          4 :                 p = wpa_s->global->add_psk;
    6304         [ +  - ]:          4 :                 if (p2p_dev_addr) {
    6305                 :          4 :                         p->p2p = 1;
    6306                 :          4 :                         os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
    6307                 :            :                 } else {
    6308                 :          0 :                         p->p2p = 0;
    6309                 :          0 :                         os_memcpy(p->addr, mac_addr, ETH_ALEN);
    6310                 :            :                 }
    6311                 :          4 :                 os_memcpy(p->psk, psk, psk_len);
    6312                 :          4 :                 return;
    6313                 :            :         }
    6314                 :            : 
    6315 [ +  - ][ +  + ]:          4 :         if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
    6316                 :          2 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
    6317                 :          2 :                 return;
    6318                 :            :         }
    6319                 :            : 
    6320                 :          2 :         persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
    6321                 :            :                                              ssid->ssid_len);
    6322         [ -  + ]:          2 :         if (!persistent) {
    6323                 :          0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
    6324                 :          0 :                 return;
    6325                 :            :         }
    6326                 :            : 
    6327                 :          2 :         p = os_zalloc(sizeof(*p));
    6328         [ -  + ]:          2 :         if (p == NULL)
    6329                 :          0 :                 return;
    6330         [ +  - ]:          2 :         if (p2p_dev_addr) {
    6331                 :          2 :                 p->p2p = 1;
    6332                 :          2 :                 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
    6333                 :            :         } else {
    6334                 :          0 :                 p->p2p = 0;
    6335                 :          0 :                 os_memcpy(p->addr, mac_addr, ETH_ALEN);
    6336                 :            :         }
    6337                 :          2 :         os_memcpy(p->psk, psk, psk_len);
    6338                 :            : 
    6339         [ -  + ]:          2 :         if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
    6340                 :            :                 struct psk_list_entry *last;
    6341         [ #  # ]:          0 :                 last = dl_list_last(&persistent->psk_list,
    6342                 :            :                                     struct psk_list_entry, list);
    6343                 :          0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
    6344                 :            :                         MACSTR " (p2p=%u) to make room for a new one",
    6345                 :            :                         MAC2STR(last->addr), last->p2p);
    6346                 :          0 :                 dl_list_del(&last->list);
    6347                 :          0 :                 os_free(last);
    6348                 :            :         }
    6349                 :            : 
    6350         [ +  - ]:          2 :         wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
    6351                 :            :                                   p2p_dev_addr ? p2p_dev_addr : mac_addr,
    6352                 :            :                                   p2p_dev_addr == NULL);
    6353         [ +  - ]:          2 :         if (p2p_dev_addr) {
    6354                 :          2 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
    6355                 :            :                         MACSTR, MAC2STR(p2p_dev_addr));
    6356                 :            :         } else {
    6357                 :          0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
    6358                 :            :                         MAC2STR(mac_addr));
    6359                 :            :         }
    6360                 :          2 :         dl_list_add(&persistent->psk_list, &p->list);
    6361                 :            : 
    6362                 :            : #ifndef CONFIG_NO_CONFIG_WRITE
    6363   [ -  +  #  # ]:          2 :         if (wpa_s->parent->conf->update_config &&
    6364                 :          0 :             wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
    6365                 :          8 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
    6366                 :            : #endif /* CONFIG_NO_CONFIG_WRITE */
    6367                 :            : }
    6368                 :            : 
    6369                 :            : 
    6370                 :          3 : static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
    6371                 :            :                                 struct wpa_ssid *s, const u8 *addr,
    6372                 :            :                                 int iface_addr)
    6373                 :            : {
    6374                 :            :         int res;
    6375                 :            : 
    6376                 :          3 :         res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
    6377         [ +  - ]:          3 :         if (res > 0) {
    6378                 :            : #ifndef CONFIG_NO_CONFIG_WRITE
    6379   [ -  +  #  # ]:          3 :                 if (wpa_s->conf->update_config &&
    6380                 :          0 :                     wpa_config_write(wpa_s->confname, wpa_s->conf))
    6381                 :          0 :                         wpa_dbg(wpa_s, MSG_DEBUG,
    6382                 :            :                                 "P2P: Failed to update configuration");
    6383                 :            : #endif /* CONFIG_NO_CONFIG_WRITE */
    6384                 :            :         }
    6385                 :          3 : }
    6386                 :            : 
    6387                 :            : 
    6388                 :          4 : static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
    6389                 :            :                                       const u8 *peer, int iface_addr)
    6390                 :            : {
    6391                 :            :         struct hostapd_data *hapd;
    6392                 :            :         struct hostapd_wpa_psk *psk, *prev, *rem;
    6393                 :            :         struct sta_info *sta;
    6394                 :            : 
    6395 [ +  - ][ +  - ]:          4 :         if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
                 [ -  + ]
    6396                 :          4 :             wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
    6397                 :          4 :                 return;
    6398                 :            : 
    6399                 :            :         /* Remove per-station PSK entry */
    6400                 :          4 :         hapd = wpa_s->ap_iface->bss[0];
    6401                 :          4 :         prev = NULL;
    6402                 :          4 :         psk = hapd->conf->ssid.wpa_psk;
    6403         [ +  + ]:         12 :         while (psk) {
    6404 [ -  + ][ #  # ]:          8 :                 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
                 [ +  - ]
    6405         [ +  + ]:          8 :                     (!iface_addr &&
    6406                 :          8 :                      os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
    6407                 :          3 :                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
    6408                 :            :                                 MACSTR " iface_addr=%d",
    6409                 :            :                                 MAC2STR(peer), iface_addr);
    6410         [ -  + ]:          3 :                         if (prev)
    6411                 :          0 :                                 prev->next = psk->next;
    6412                 :            :                         else
    6413                 :          3 :                                 hapd->conf->ssid.wpa_psk = psk->next;
    6414                 :          3 :                         rem = psk;
    6415                 :          3 :                         psk = psk->next;
    6416                 :          3 :                         os_free(rem);
    6417                 :            :                 } else {
    6418                 :          5 :                         prev = psk;
    6419                 :          5 :                         psk = psk->next;
    6420                 :            :                 }
    6421                 :            :         }
    6422                 :            : 
    6423                 :            :         /* Disconnect from group */
    6424         [ -  + ]:          4 :         if (iface_addr)
    6425                 :          0 :                 sta = ap_get_sta(hapd, peer);
    6426                 :            :         else
    6427                 :          4 :                 sta = ap_get_sta_p2p(hapd, peer);
    6428         [ +  - ]:          4 :         if (sta) {
    6429                 :          4 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
    6430                 :            :                         " (iface_addr=%d) from group",
    6431                 :            :                         MAC2STR(peer), iface_addr);
    6432                 :          4 :                 hostapd_drv_sta_deauth(hapd, sta->addr,
    6433                 :            :                                        WLAN_REASON_DEAUTH_LEAVING);
    6434                 :          4 :                 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
    6435                 :            :         }
    6436                 :            : }
    6437                 :            : 
    6438                 :            : 
    6439                 :          4 : void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
    6440                 :            :                             int iface_addr)
    6441                 :            : {
    6442                 :            :         struct wpa_ssid *s;
    6443                 :            :         struct wpa_supplicant *w;
    6444                 :            : 
    6445                 :          4 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
    6446                 :            : 
    6447                 :            :         /* Remove from any persistent group */
    6448         [ +  + ]:         11 :         for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
    6449 [ +  + ][ -  + ]:          7 :                 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
    6450                 :          4 :                         continue;
    6451         [ +  - ]:          3 :                 if (!iface_addr)
    6452                 :          3 :                         wpas_remove_persistent_peer(wpa_s, s, peer, 0);
    6453                 :          3 :                 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
    6454                 :            :         }
    6455                 :            : 
    6456                 :            :         /* Remove from any operating group */
    6457         [ +  + ]:          8 :         for (w = wpa_s->global->ifaces; w; w = w->next)
    6458                 :          4 :                 wpas_p2p_remove_client_go(w, peer, iface_addr);
    6459                 :          4 : }
    6460                 :            : 
    6461                 :            : 
    6462                 :          2 : static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
    6463                 :            : {
    6464                 :          2 :         struct wpa_supplicant *wpa_s = eloop_ctx;
    6465                 :          2 :         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
    6466                 :          2 : }
    6467                 :            : 
    6468                 :            : 
    6469                 :          0 : static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
    6470                 :            : {
    6471                 :          0 :         struct wpa_supplicant *wpa_s = eloop_ctx;
    6472                 :            : 
    6473                 :          0 :         wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
    6474                 :          0 :         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
    6475                 :          0 : }
    6476                 :            : 
    6477                 :            : 
    6478                 :          0 : int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
    6479                 :            :                                         struct wpa_ssid *ssid)
    6480                 :            : {
    6481                 :            :         struct wpa_supplicant *iface;
    6482                 :            : 
    6483         [ #  # ]:          0 :         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
    6484 [ #  # ][ #  # ]:          0 :                 if (!iface->current_ssid ||
    6485         [ #  # ]:          0 :                     iface->current_ssid->frequency == freq ||
    6486         [ #  # ]:          0 :                     (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
    6487                 :          0 :                      !iface->current_ssid->p2p_group))
    6488                 :          0 :                         continue;
    6489                 :            : 
    6490                 :            :                 /* Remove the connection with least priority */
    6491         [ #  # ]:          0 :                 if (!wpas_is_p2p_prioritized(iface)) {
    6492                 :            :                         /* STA connection has priority over existing
    6493                 :            :                          * P2P connection, so remove the interface. */
    6494                 :          0 :                         wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
    6495                 :          0 :                         eloop_register_timeout(0, 0,
    6496                 :            :                                                wpas_p2p_group_freq_conflict,
    6497                 :            :                                                iface, NULL);
    6498                 :            :                         /* If connection in progress is P2P connection, do not
    6499                 :            :                          * proceed for the connection. */
    6500         [ #  # ]:          0 :                         if (wpa_s == iface)
    6501                 :          0 :                                 return -1;
    6502                 :            :                         else
    6503                 :          0 :                                 return 0;
    6504                 :            :                 } else {
    6505                 :            :                         /* P2P connection has priority, disable the STA network
    6506                 :            :                          */
    6507                 :          0 :                         wpa_supplicant_disable_network(wpa_s->global->ifaces,
    6508                 :            :                                                        ssid);
    6509                 :          0 :                         wpa_msg(wpa_s->global->ifaces, MSG_INFO,
    6510                 :            :                                 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
    6511                 :          0 :                         os_memset(wpa_s->global->ifaces->pending_bssid, 0,
    6512                 :            :                                   ETH_ALEN);
    6513                 :            :                         /* If P2P connection is in progress, continue
    6514                 :            :                          * connecting...*/
    6515         [ #  # ]:          0 :                         if (wpa_s == iface)
    6516                 :          0 :                                 return 0;
    6517                 :            :                         else
    6518                 :          0 :                                 return -1;
    6519                 :            :                 }
    6520                 :            :         }
    6521                 :            : 
    6522                 :          0 :         return 0;
    6523                 :            : }
    6524                 :            : 
    6525                 :            : 
    6526                 :          8 : int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
    6527                 :            : {
    6528                 :          8 :         struct wpa_ssid *ssid = wpa_s->current_ssid;
    6529                 :            : 
    6530 [ +  - ][ +  + ]:          8 :         if (ssid == NULL || !ssid->p2p_group)
    6531                 :          1 :                 return 0;
    6532                 :            : 
    6533 [ +  + ][ +  - ]:          7 :         if (wpa_s->p2p_last_4way_hs_fail &&
    6534                 :          2 :             wpa_s->p2p_last_4way_hs_fail == ssid) {
    6535                 :            :                 u8 go_dev_addr[ETH_ALEN];
    6536                 :            :                 struct wpa_ssid *persistent;
    6537                 :            : 
    6538         [ -  + ]:          2 :                 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
    6539                 :          2 :                                               ssid->ssid,
    6540                 :            :                                               ssid->ssid_len) <= 0) {
    6541                 :          0 :                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
    6542                 :          0 :                         goto disconnect;
    6543                 :            :                 }
    6544                 :            : 
    6545                 :          2 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
    6546                 :            :                         MACSTR, MAC2STR(go_dev_addr));
    6547                 :          2 :                 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
    6548                 :          2 :                                                      ssid->ssid,
    6549                 :            :                                                      ssid->ssid_len);
    6550 [ +  - ][ -  + ]:          2 :                 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
    6551                 :          0 :                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
    6552                 :          0 :                         goto disconnect;
    6553                 :            :                 }
    6554                 :          2 :                 wpa_msg_global(wpa_s->parent, MSG_INFO,
    6555                 :            :                                P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
    6556                 :            :                                persistent->id);
    6557                 :            :         disconnect:
    6558                 :          2 :                 wpa_s->p2p_last_4way_hs_fail = NULL;
    6559                 :            :                 /*
    6560                 :            :                  * Remove the group from a timeout to avoid issues with caller
    6561                 :            :                  * continuing to use the interface if this is on a P2P group
    6562                 :            :                  * interface.
    6563                 :            :                  */
    6564                 :          2 :                 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
    6565                 :            :                                        wpa_s, NULL);
    6566                 :          2 :                 return 1;
    6567                 :            :         }
    6568                 :            : 
    6569                 :          5 :         wpa_s->p2p_last_4way_hs_fail = ssid;
    6570                 :          8 :         return 0;
    6571                 :            : }

Generated by: LCOV version 1.9