LCOV - code coverage report
Current view: top level - src/p2p - p2p_go_neg.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1475438200 Lines: 642 849 75.6 %
Date: 2016-10-02 Functions: 15 17 88.2 %

          Line data    Source code
       1             : /*
       2             :  * Wi-Fi Direct - P2P Group Owner Negotiation
       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 "utils/eloop.h"
      13             : #include "common/ieee802_11_defs.h"
      14             : #include "common/wpa_ctrl.h"
      15             : #include "wps/wps_defs.h"
      16             : #include "p2p_i.h"
      17             : #include "p2p.h"
      18             : 
      19             : 
      20         304 : static int p2p_go_det(u8 own_intent, u8 peer_value)
      21             : {
      22         304 :         u8 peer_intent = peer_value >> 1;
      23         304 :         if (own_intent == peer_intent) {
      24          52 :                 if (own_intent == P2P_MAX_GO_INTENT)
      25           2 :                         return -1; /* both devices want to become GO */
      26             : 
      27             :                 /* Use tie breaker bit to determine GO */
      28          50 :                 return (peer_value & 0x01) ? 0 : 1;
      29             :         }
      30             : 
      31         252 :         return own_intent > peer_intent;
      32             : }
      33             : 
      34             : 
      35         473 : int p2p_peer_channels_check(struct p2p_data *p2p, struct p2p_channels *own,
      36             :                             struct p2p_device *dev,
      37             :                             const u8 *channel_list, size_t channel_list_len)
      38             : {
      39             :         const u8 *pos, *end;
      40             :         struct p2p_channels *ch;
      41             :         u8 channels;
      42             :         struct p2p_channels intersection;
      43             : 
      44         473 :         ch = &dev->channels;
      45         473 :         os_memset(ch, 0, sizeof(*ch));
      46         473 :         pos = channel_list;
      47         473 :         end = channel_list + channel_list_len;
      48             : 
      49         473 :         if (end - pos < 3)
      50           0 :                 return -1;
      51         473 :         os_memcpy(dev->country, pos, 3);
      52         473 :         wpa_hexdump_ascii(MSG_DEBUG, "P2P: Peer country", pos, 3);
      53         473 :         if (pos[2] != 0x04 && os_memcmp(pos, p2p->cfg->country, 2) != 0) {
      54           0 :                 p2p_info(p2p, "Mismatching country (ours=%c%c peer's=%c%c)",
      55           0 :                         p2p->cfg->country[0], p2p->cfg->country[1],
      56           0 :                         pos[0], pos[1]);
      57           0 :                 return -1;
      58             :         }
      59         473 :         pos += 3;
      60             : 
      61        1564 :         while (end - pos > 2) {
      62         619 :                 struct p2p_reg_class *cl = &ch->reg_class[ch->reg_classes];
      63         619 :                 cl->reg_class = *pos++;
      64         619 :                 channels = *pos++;
      65         619 :                 if (channels > end - pos) {
      66           1 :                         p2p_info(p2p, "Invalid peer Channel List");
      67           1 :                         return -1;
      68             :                 }
      69         618 :                 cl->channels = channels > P2P_MAX_REG_CLASS_CHANNELS ?
      70             :                         P2P_MAX_REG_CLASS_CHANNELS : channels;
      71         618 :                 os_memcpy(cl->channel, pos, cl->channels);
      72         618 :                 pos += channels;
      73         618 :                 ch->reg_classes++;
      74         618 :                 if (ch->reg_classes == P2P_MAX_REG_CLASSES)
      75           0 :                         break;
      76             :         }
      77             : 
      78         472 :         p2p_channels_intersect(own, &dev->channels, &intersection);
      79        1416 :         p2p_dbg(p2p, "Own reg_classes %d peer reg_classes %d intersection reg_classes %d",
      80         472 :                 (int) own->reg_classes,
      81         472 :                 (int) dev->channels.reg_classes,
      82         472 :                 (int) intersection.reg_classes);
      83         472 :         if (intersection.reg_classes == 0) {
      84           8 :                 p2p_info(p2p, "No common channels found");
      85           8 :                 return -1;
      86             :         }
      87         464 :         return 0;
      88             : }
      89             : 
      90             : 
      91         299 : static int p2p_peer_channels(struct p2p_data *p2p, struct p2p_device *dev,
      92             :                              const u8 *channel_list, size_t channel_list_len)
      93             : {
      94         299 :         return p2p_peer_channels_check(p2p, &p2p->channels, dev,
      95             :                                        channel_list, channel_list_len);
      96             : }
      97             : 
      98             : 
      99        1558 : u16 p2p_wps_method_pw_id(enum p2p_wps_method wps_method)
     100             : {
     101        1558 :         switch (wps_method) {
     102             :         case WPS_PIN_DISPLAY:
     103         883 :                 return DEV_PW_REGISTRAR_SPECIFIED;
     104             :         case WPS_PIN_KEYPAD:
     105         150 :                 return DEV_PW_USER_SPECIFIED;
     106             :         case WPS_PBC:
     107         418 :                 return DEV_PW_PUSHBUTTON;
     108             :         case WPS_NFC:
     109          27 :                 return DEV_PW_NFC_CONNECTION_HANDOVER;
     110             :         case WPS_P2PS:
     111          30 :                 return DEV_PW_P2PS_DEFAULT;
     112             :         default:
     113          50 :                 return DEV_PW_DEFAULT;
     114             :         }
     115             : }
     116             : 
     117             : 
     118           4 : static const char * p2p_wps_method_str(enum p2p_wps_method wps_method)
     119             : {
     120           4 :         switch (wps_method) {
     121             :         case WPS_PIN_DISPLAY:
     122           2 :                 return "Display";
     123             :         case WPS_PIN_KEYPAD:
     124           1 :                 return "Keypad";
     125             :         case WPS_PBC:
     126           1 :                 return "PBC";
     127             :         case WPS_NFC:
     128           0 :                 return "NFC";
     129             :         case WPS_P2PS:
     130           0 :                 return "P2PS";
     131             :         default:
     132           0 :                 return "??";
     133             :         }
     134             : }
     135             : 
     136             : 
     137         385 : static struct wpabuf * p2p_build_go_neg_req(struct p2p_data *p2p,
     138             :                                             struct p2p_device *peer)
     139             : {
     140             :         struct wpabuf *buf;
     141             :         u8 *len;
     142             :         u8 group_capab;
     143         385 :         size_t extra = 0;
     144             :         u16 pw_id;
     145             : 
     146             : #ifdef CONFIG_WIFI_DISPLAY
     147         385 :         if (p2p->wfd_ie_go_neg)
     148           2 :                 extra = wpabuf_len(p2p->wfd_ie_go_neg);
     149             : #endif /* CONFIG_WIFI_DISPLAY */
     150             : 
     151         385 :         if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ])
     152           1 :                 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]);
     153             : 
     154         385 :         buf = wpabuf_alloc(1000 + extra);
     155         385 :         if (buf == NULL)
     156           0 :                 return NULL;
     157             : 
     158         385 :         p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_REQ, peer->dialog_token);
     159             : 
     160         385 :         len = p2p_buf_add_ie_hdr(buf);
     161         385 :         group_capab = 0;
     162         385 :         if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
     163          50 :                 group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
     164          50 :                 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
     165          25 :                         group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN;
     166             :         }
     167         385 :         if (p2p->cross_connect)
     168           0 :                 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
     169         385 :         if (p2p->cfg->p2p_intra_bss)
     170         385 :                 group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
     171         385 :         p2p_buf_add_capability(buf, p2p->dev_capab &
     172             :                                ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
     173             :                                group_capab);
     174         385 :         p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | peer->tie_breaker);
     175         385 :         p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
     176         385 :         p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
     177         385 :                                    p2p->cfg->channel);
     178         385 :         if (p2p->ext_listen_interval)
     179           4 :                 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
     180           4 :                                               p2p->ext_listen_interval);
     181         385 :         p2p_buf_add_intended_addr(buf, p2p->intended_addr);
     182         385 :         p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
     183         385 :         p2p_buf_add_device_info(buf, p2p, peer);
     184         770 :         p2p_buf_add_operating_channel(buf, p2p->cfg->country,
     185         770 :                                       p2p->op_reg_class, p2p->op_channel);
     186         385 :         p2p_buf_update_ie_hdr(buf, len);
     187             : 
     188         385 :         p2p_buf_add_pref_channel_list(buf, p2p->pref_freq_list,
     189             :                                       p2p->num_pref_freq);
     190             : 
     191             :         /* WPS IE with Device Password ID attribute */
     192         385 :         pw_id = p2p_wps_method_pw_id(peer->wps_method);
     193         385 :         if (peer->oob_pw_id)
     194           9 :                 pw_id = peer->oob_pw_id;
     195         385 :         if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) {
     196           0 :                 p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Request");
     197           0 :                 wpabuf_free(buf);
     198           0 :                 return NULL;
     199             :         }
     200             : 
     201             : #ifdef CONFIG_WIFI_DISPLAY
     202         385 :         if (p2p->wfd_ie_go_neg)
     203           2 :                 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
     204             : #endif /* CONFIG_WIFI_DISPLAY */
     205             : 
     206         385 :         if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ])
     207           1 :                 wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]);
     208             : 
     209         385 :         return buf;
     210             : }
     211             : 
     212             : 
     213         387 : int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
     214             : {
     215             :         struct wpabuf *req;
     216             :         int freq;
     217             : 
     218         387 :         if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
     219             :                 u16 config_method;
     220          12 :                 p2p_dbg(p2p, "Use PD-before-GO-Neg workaround for " MACSTR,
     221          12 :                         MAC2STR(dev->info.p2p_device_addr));
     222           2 :                 if (dev->wps_method == WPS_PIN_DISPLAY)
     223           0 :                         config_method = WPS_CONFIG_KEYPAD;
     224           2 :                 else if (dev->wps_method == WPS_PIN_KEYPAD)
     225           0 :                         config_method = WPS_CONFIG_DISPLAY;
     226           2 :                 else if (dev->wps_method == WPS_PBC)
     227           2 :                         config_method = WPS_CONFIG_PUSHBUTTON;
     228           0 :                 else if (dev->wps_method == WPS_P2PS)
     229           0 :                         config_method = WPS_CONFIG_P2PS;
     230             :                 else
     231           0 :                         return -1;
     232           2 :                 return p2p_prov_disc_req(p2p, dev->info.p2p_device_addr,
     233             :                                          NULL, config_method, 0, 0, 1);
     234             :         }
     235             : 
     236         385 :         freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
     237         385 :         if (dev->oob_go_neg_freq > 0)
     238           9 :                 freq = dev->oob_go_neg_freq;
     239         385 :         if (freq <= 0) {
     240           0 :                 p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
     241             :                         MACSTR " to send GO Negotiation Request",
     242           0 :                         MAC2STR(dev->info.p2p_device_addr));
     243           0 :                 return -1;
     244             :         }
     245             : 
     246         385 :         req = p2p_build_go_neg_req(p2p, dev);
     247         385 :         if (req == NULL)
     248           0 :                 return -1;
     249         385 :         p2p_dbg(p2p, "Sending GO Negotiation Request");
     250         385 :         p2p_set_state(p2p, P2P_CONNECT);
     251         385 :         p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
     252         385 :         p2p->go_neg_peer = dev;
     253         385 :         eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
     254         385 :         dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
     255         385 :         dev->connect_reqs++;
     256         770 :         if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
     257         385 :                             p2p->cfg->dev_addr, dev->info.p2p_device_addr,
     258         385 :                             wpabuf_head(req), wpabuf_len(req), 500) < 0) {
     259           2 :                 p2p_dbg(p2p, "Failed to send Action frame");
     260             :                 /* Use P2P find to recover and retry */
     261           2 :                 p2p_set_timeout(p2p, 0, 0);
     262             :         } else
     263         383 :                 dev->go_neg_req_sent++;
     264             : 
     265         385 :         wpabuf_free(req);
     266             : 
     267         385 :         return 0;
     268             : }
     269             : 
     270             : 
     271         207 : static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
     272             :                                              struct p2p_device *peer,
     273             :                                              u8 dialog_token, u8 status,
     274             :                                              u8 tie_breaker)
     275             : {
     276             :         struct wpabuf *buf;
     277             :         u8 *len;
     278             :         u8 group_capab;
     279         207 :         size_t extra = 0;
     280             :         u16 pw_id;
     281             : 
     282         207 :         p2p_dbg(p2p, "Building GO Negotiation Response");
     283             : 
     284             : #ifdef CONFIG_WIFI_DISPLAY
     285         207 :         if (p2p->wfd_ie_go_neg)
     286           2 :                 extra = wpabuf_len(p2p->wfd_ie_go_neg);
     287             : #endif /* CONFIG_WIFI_DISPLAY */
     288             : 
     289         207 :         if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP])
     290           1 :                 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]);
     291             : 
     292         207 :         buf = wpabuf_alloc(1000 + extra);
     293         207 :         if (buf == NULL)
     294           0 :                 return NULL;
     295             : 
     296         207 :         p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
     297             : 
     298         207 :         len = p2p_buf_add_ie_hdr(buf);
     299         207 :         p2p_buf_add_status(buf, status);
     300         207 :         group_capab = 0;
     301         207 :         if (peer && peer->go_state == LOCAL_GO) {
     302          45 :                 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
     303          13 :                         group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
     304          13 :                         if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
     305          10 :                                 group_capab |=
     306             :                                         P2P_GROUP_CAPAB_PERSISTENT_RECONN;
     307             :                 }
     308          45 :                 if (p2p->cross_connect)
     309           0 :                         group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
     310          45 :                 if (p2p->cfg->p2p_intra_bss)
     311          45 :                         group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
     312             :         }
     313         207 :         p2p_buf_add_capability(buf, p2p->dev_capab &
     314             :                                ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
     315             :                                group_capab);
     316         207 :         p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
     317         207 :         p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
     318         207 :         if (peer && peer->go_state == REMOTE_GO && !p2p->num_pref_freq) {
     319         104 :                 p2p_dbg(p2p, "Omit Operating Channel attribute");
     320             :         } else {
     321         206 :                 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
     322         103 :                                               p2p->op_reg_class,
     323         103 :                                               p2p->op_channel);
     324             :         }
     325         207 :         p2p_buf_add_intended_addr(buf, p2p->intended_addr);
     326         207 :         if (status || peer == NULL) {
     327          62 :                 p2p_buf_add_channel_list(buf, p2p->cfg->country,
     328             :                                          &p2p->channels);
     329         145 :         } else if (peer->go_state == REMOTE_GO) {
     330         102 :                 p2p_buf_add_channel_list(buf, p2p->cfg->country,
     331             :                                          &p2p->channels);
     332             :         } else {
     333             :                 struct p2p_channels res;
     334          43 :                 p2p_channels_intersect(&p2p->channels, &peer->channels,
     335             :                                        &res);
     336          43 :                 p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
     337             :         }
     338         207 :         p2p_buf_add_device_info(buf, p2p, peer);
     339         207 :         if (peer && peer->go_state == LOCAL_GO) {
     340          45 :                 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
     341             :                                      p2p->ssid_len);
     342             :         }
     343         207 :         p2p_buf_update_ie_hdr(buf, len);
     344             : 
     345             :         /* WPS IE with Device Password ID attribute */
     346         207 :         pw_id = p2p_wps_method_pw_id(peer ? peer->wps_method : WPS_NOT_READY);
     347         207 :         if (peer && peer->oob_pw_id)
     348           9 :                 pw_id = peer->oob_pw_id;
     349         207 :         if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) {
     350           0 :                 p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Response");
     351           0 :                 wpabuf_free(buf);
     352           0 :                 return NULL;
     353             :         }
     354             : 
     355             : #ifdef CONFIG_WIFI_DISPLAY
     356         207 :         if (p2p->wfd_ie_go_neg)
     357           2 :                 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
     358             : #endif /* CONFIG_WIFI_DISPLAY */
     359             : 
     360         207 :         if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP])
     361           1 :                 wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]);
     362             : 
     363         207 :         return buf;
     364             : }
     365             : 
     366             : 
     367             : /**
     368             :  * p2p_reselect_channel - Re-select operating channel based on peer information
     369             :  * @p2p: P2P module context from p2p_init()
     370             :  * @intersection: Support channel list intersection from local and peer
     371             :  *
     372             :  * This function is used to re-select the best channel after having received
     373             :  * information from the peer to allow supported channel lists to be intersected.
     374             :  * This can be used to improve initial channel selection done in
     375             :  * p2p_prepare_channel() prior to the start of GO Negotiation. In addition, this
     376             :  * can be used for Invitation case.
     377             :  */
     378         150 : void p2p_reselect_channel(struct p2p_data *p2p,
     379             :                           struct p2p_channels *intersection)
     380             : {
     381             :         struct p2p_reg_class *cl;
     382             :         int freq;
     383             :         u8 op_reg_class, op_channel;
     384             :         unsigned int i;
     385         150 :         const int op_classes_5ghz[] = { 124, 125, 115, 0 };
     386         150 :         const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
     387         150 :         const int op_classes_vht[] = { 128, 129, 130, 0 };
     388             : 
     389         151 :         if (p2p->own_freq_preference > 0 &&
     390           1 :             p2p_freq_to_channel(p2p->own_freq_preference,
     391           1 :                                 &op_reg_class, &op_channel) == 0 &&
     392           1 :             p2p_channels_includes(intersection, op_reg_class, op_channel)) {
     393           0 :                 p2p_dbg(p2p, "Pick own channel preference (reg_class %u channel %u) from intersection",
     394             :                         op_reg_class, op_channel);
     395           0 :                 p2p->op_reg_class = op_reg_class;
     396           0 :                 p2p->op_channel = op_channel;
     397         142 :                 return;
     398             :         }
     399             : 
     400         150 :         if (p2p->best_freq_overall > 0 &&
     401           0 :             p2p_freq_to_channel(p2p->best_freq_overall,
     402           0 :                                 &op_reg_class, &op_channel) == 0 &&
     403           0 :             p2p_channels_includes(intersection, op_reg_class, op_channel)) {
     404           0 :                 p2p_dbg(p2p, "Pick best overall channel (reg_class %u channel %u) from intersection",
     405             :                         op_reg_class, op_channel);
     406           0 :                 p2p->op_reg_class = op_reg_class;
     407           0 :                 p2p->op_channel = op_channel;
     408           0 :                 return;
     409             :         }
     410             : 
     411             :         /* First, try to pick the best channel from another band */
     412         150 :         freq = p2p_channel_to_freq(p2p->op_reg_class, p2p->op_channel);
     413         150 :         if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
     414           0 :             !p2p_channels_includes(intersection, p2p->op_reg_class,
     415           0 :                                    p2p->op_channel) &&
     416           0 :             p2p_freq_to_channel(p2p->best_freq_5,
     417           0 :                                 &op_reg_class, &op_channel) == 0 &&
     418           0 :             p2p_channels_includes(intersection, op_reg_class, op_channel)) {
     419           0 :                 p2p_dbg(p2p, "Pick best 5 GHz channel (reg_class %u channel %u) from intersection",
     420             :                         op_reg_class, op_channel);
     421           0 :                 p2p->op_reg_class = op_reg_class;
     422           0 :                 p2p->op_channel = op_channel;
     423           0 :                 return;
     424             :         }
     425             : 
     426         150 :         if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
     427           0 :             !p2p_channels_includes(intersection, p2p->op_reg_class,
     428           0 :                                    p2p->op_channel) &&
     429           0 :             p2p_freq_to_channel(p2p->best_freq_24,
     430           0 :                                 &op_reg_class, &op_channel) == 0 &&
     431           0 :             p2p_channels_includes(intersection, op_reg_class, op_channel)) {
     432           0 :                 p2p_dbg(p2p, "Pick best 2.4 GHz channel (reg_class %u channel %u) from intersection",
     433             :                         op_reg_class, op_channel);
     434           0 :                 p2p->op_reg_class = op_reg_class;
     435           0 :                 p2p->op_channel = op_channel;
     436           0 :                 return;
     437             :         }
     438             : 
     439             :         /* Select channel with highest preference if the peer supports it */
     440         151 :         for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
     441           4 :                 if (p2p_channels_includes(intersection,
     442           2 :                                           p2p->cfg->pref_chan[i].op_class,
     443           2 :                                           p2p->cfg->pref_chan[i].chan)) {
     444           1 :                         p2p->op_reg_class = p2p->cfg->pref_chan[i].op_class;
     445           1 :                         p2p->op_channel = p2p->cfg->pref_chan[i].chan;
     446           2 :                         p2p_dbg(p2p, "Pick highest preferred channel (op_class %u channel %u) from intersection",
     447           2 :                                 p2p->op_reg_class, p2p->op_channel);
     448           1 :                         return;
     449             :                 }
     450             :         }
     451             : 
     452             :         /* Try a channel where we might be able to use VHT */
     453         149 :         if (p2p_channel_select(intersection, op_classes_vht,
     454             :                                &p2p->op_reg_class, &p2p->op_channel) == 0) {
     455           8 :                 p2p_dbg(p2p, "Pick possible VHT channel (op_class %u channel %u) from intersection",
     456           8 :                         p2p->op_reg_class, p2p->op_channel);
     457           4 :                 return;
     458             :         }
     459             : 
     460             :         /* Try a channel where we might be able to use HT40 */
     461         145 :         if (p2p_channel_select(intersection, op_classes_ht40,
     462             :                                &p2p->op_reg_class, &p2p->op_channel) == 0) {
     463           2 :                 p2p_dbg(p2p, "Pick possible HT40 channel (op_class %u channel %u) from intersection",
     464           2 :                         p2p->op_reg_class, p2p->op_channel);
     465           1 :                 return;
     466             :         }
     467             : 
     468             :         /* Prefer a 5 GHz channel */
     469         144 :         if (p2p_channel_select(intersection, op_classes_5ghz,
     470             :                                &p2p->op_reg_class, &p2p->op_channel) == 0) {
     471           0 :                 p2p_dbg(p2p, "Pick possible 5 GHz channel (op_class %u channel %u) from intersection",
     472           0 :                         p2p->op_reg_class, p2p->op_channel);
     473           0 :                 return;
     474             :         }
     475             : 
     476             :         /*
     477             :          * Try to see if the original channel is in the intersection. If
     478             :          * so, no need to change anything, as it already contains some
     479             :          * randomness.
     480             :          */
     481         144 :         if (p2p_channels_includes(intersection, p2p->op_reg_class,
     482         144 :                                   p2p->op_channel)) {
     483         272 :                 p2p_dbg(p2p, "Using original operating class and channel (op_class %u channel %u) from intersection",
     484         272 :                         p2p->op_reg_class, p2p->op_channel);
     485         136 :                 return;
     486             :         }
     487             : 
     488             :         /*
     489             :          * Fall back to whatever is included in the channel intersection since
     490             :          * no better options seems to be available.
     491             :          */
     492           8 :         cl = &intersection->reg_class[0];
     493          16 :         p2p_dbg(p2p, "Pick another channel (reg_class %u channel %u) from intersection",
     494          16 :                 cl->reg_class, cl->channel[0]);
     495           8 :         p2p->op_reg_class = cl->reg_class;
     496           8 :         p2p->op_channel = cl->channel[0];
     497             : }
     498             : 
     499             : 
     500         161 : int p2p_go_select_channel(struct p2p_data *p2p, struct p2p_device *dev,
     501             :                           u8 *status)
     502             : {
     503             :         struct p2p_channels tmp, intersection;
     504             : 
     505         161 :         p2p_channels_dump(p2p, "own channels", &p2p->channels);
     506         161 :         p2p_channels_dump(p2p, "peer channels", &dev->channels);
     507         161 :         p2p_channels_intersect(&p2p->channels, &dev->channels, &tmp);
     508         161 :         p2p_channels_dump(p2p, "intersection", &tmp);
     509         161 :         p2p_channels_remove_freqs(&tmp, &p2p->no_go_freq);
     510         161 :         p2p_channels_dump(p2p, "intersection after no-GO removal", &tmp);
     511         161 :         p2p_channels_intersect(&tmp, &p2p->cfg->channels, &intersection);
     512         161 :         p2p_channels_dump(p2p, "intersection with local channel list",
     513             :                           &intersection);
     514         321 :         if (intersection.reg_classes == 0 ||
     515         160 :             intersection.reg_class[0].channels == 0) {
     516           1 :                 *status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
     517           1 :                 p2p_dbg(p2p, "No common channels found");
     518           1 :                 return -1;
     519             :         }
     520             : 
     521         160 :         if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
     522         160 :                                    p2p->op_channel)) {
     523           8 :                 if (dev->flags & P2P_DEV_FORCE_FREQ) {
     524           0 :                         *status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
     525           0 :                         p2p_dbg(p2p, "Peer does not support the forced channel");
     526           0 :                         return -1;
     527             :                 }
     528             : 
     529          16 :                 p2p_dbg(p2p, "Selected operating channel (op_class %u channel %u) not acceptable to the peer",
     530          16 :                         p2p->op_reg_class, p2p->op_channel);
     531           8 :                 p2p_reselect_channel(p2p, &intersection);
     532         278 :         } else if (!(dev->flags & P2P_DEV_FORCE_FREQ) &&
     533         126 :                    !p2p->cfg->cfg_op_channel) {
     534         250 :                 p2p_dbg(p2p, "Try to optimize channel selection with peer information received; previously selected op_class %u channel %u",
     535         250 :                         p2p->op_reg_class, p2p->op_channel);
     536         125 :                 p2p_reselect_channel(p2p, &intersection);
     537             :         }
     538             : 
     539         160 :         if (!p2p->ssid_set) {
     540         147 :                 p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
     541         147 :                 p2p->ssid_set = 1;
     542             :         }
     543             : 
     544         160 :         return 0;
     545             : }
     546             : 
     547             : 
     548           0 : static void p2p_check_pref_chan_no_recv(struct p2p_data *p2p, int go,
     549             :                                         struct p2p_device *dev,
     550             :                                         struct p2p_message *msg,
     551             :                                         unsigned freq_list[], unsigned int size)
     552             : {
     553             :         u8 op_class, op_channel;
     554           0 :         unsigned int oper_freq = 0, i, j;
     555           0 :         int found = 0;
     556             : 
     557           0 :         p2p_dbg(p2p,
     558             :                 "Peer didn't provide a preferred frequency list, see if any of our preferred channels are supported by peer device");
     559             : 
     560             :         /*
     561             :          * Search for a common channel in our preferred frequency list which is
     562             :          * also supported by the peer device.
     563             :          */
     564           0 :         for (i = 0; i < size && !found; i++) {
     565             :                 /*
     566             :                  * Make sure that the common frequency is:
     567             :                  * 1. Supported by peer
     568             :                  * 2. Allowed for P2P use.
     569             :                  */
     570           0 :                 oper_freq = freq_list[i];
     571           0 :                 if (p2p_freq_to_channel(oper_freq, &op_class,
     572             :                                         &op_channel) < 0) {
     573           0 :                         p2p_dbg(p2p, "Unsupported frequency %u MHz", oper_freq);
     574           0 :                         continue;
     575             :                 }
     576           0 :                 if (!p2p_channels_includes(&p2p->cfg->channels,
     577           0 :                                            op_class, op_channel) &&
     578           0 :                     (go || !p2p_channels_includes(&p2p->cfg->cli_channels,
     579             :                                                   op_class, op_channel))) {
     580           0 :                         p2p_dbg(p2p,
     581             :                                 "Freq %u MHz (oper_class %u channel %u) not allowed for P2P",
     582             :                                 oper_freq, op_class, op_channel);
     583           0 :                         break;
     584             :                 }
     585           0 :                 for (j = 0; j < msg->channel_list_len; j++) {
     586             : 
     587           0 :                         if (op_channel != msg->channel_list[j])
     588           0 :                                 continue;
     589             : 
     590           0 :                         p2p->op_reg_class = op_class;
     591           0 :                         p2p->op_channel = op_channel;
     592           0 :                         os_memcpy(&p2p->channels, &p2p->cfg->channels,
     593             :                                   sizeof(struct p2p_channels));
     594           0 :                         found = 1;
     595           0 :                         break;
     596             :                 }
     597             :         }
     598             : 
     599           0 :         if (found) {
     600           0 :                 p2p_dbg(p2p,
     601             :                         "Freq %d MHz is a preferred channel and is also supported by peer, use it as the operating channel",
     602             :                         oper_freq);
     603             :         } else {
     604           0 :                 p2p_dbg(p2p,
     605             :                         "None of our preferred channels are supported by peer!. Use: %d MHz for oper_channel",
     606             :                         dev->oper_freq);
     607             :         }
     608           0 : }
     609             : 
     610             : 
     611           0 : static void p2p_check_pref_chan_recv(struct p2p_data *p2p, int go,
     612             :                                      struct p2p_device *dev,
     613             :                                      struct p2p_message *msg,
     614             :                                      unsigned freq_list[], unsigned int size)
     615             : {
     616             :         u8 op_class, op_channel;
     617           0 :         unsigned int oper_freq = 0, i, j;
     618           0 :         int found = 0;
     619             : 
     620             :         /*
     621             :          * Peer device supports a Preferred Frequency List.
     622             :          * Search for a common channel in the preferred frequency lists
     623             :          * of both peer and local devices.
     624             :          */
     625           0 :         for (i = 0; i < size && !found; i++) {
     626           0 :                 for (j = 2; j < (msg->pref_freq_list_len / 2); j++) {
     627           0 :                         oper_freq = p2p_channel_to_freq(
     628           0 :                                 msg->pref_freq_list[2 * j],
     629           0 :                                 msg->pref_freq_list[2 * j + 1]);
     630           0 :                         if (freq_list[i] != oper_freq)
     631           0 :                                 continue;
     632             : 
     633             :                         /*
     634             :                          * Make sure that the found frequency is:
     635             :                          * 1. Supported
     636             :                          * 2. Allowed for P2P use.
     637             :                          */
     638           0 :                         if (p2p_freq_to_channel(oper_freq, &op_class,
     639             :                                                 &op_channel) < 0) {
     640           0 :                                 p2p_dbg(p2p, "Unsupported frequency %u MHz",
     641             :                                         oper_freq);
     642           0 :                                 continue;
     643             :                         }
     644             : 
     645           0 :                         if (!p2p_channels_includes(&p2p->cfg->channels,
     646           0 :                                                    op_class, op_channel) &&
     647           0 :                             (go ||
     648           0 :                              !p2p_channels_includes(&p2p->cfg->cli_channels,
     649             :                                                     op_class, op_channel))) {
     650           0 :                                 p2p_dbg(p2p,
     651             :                                         "Freq %u MHz (oper_class %u channel %u) not allowed for P2P",
     652             :                                         oper_freq, op_class, op_channel);
     653           0 :                                 break;
     654             :                         }
     655           0 :                         p2p->op_reg_class = op_class;
     656           0 :                         p2p->op_channel = op_channel;
     657           0 :                         os_memcpy(&p2p->channels, &p2p->cfg->channels,
     658             :                                   sizeof(struct p2p_channels));
     659           0 :                         found = 1;
     660           0 :                         break;
     661             :                 }
     662             :         }
     663             : 
     664           0 :         if (found) {
     665           0 :                 p2p_dbg(p2p,
     666             :                         "Freq %d MHz is a common preferred channel for both peer and local, use it as operating channel",
     667             :                         oper_freq);
     668             :         } else {
     669           0 :                 p2p_dbg(p2p,
     670             :                         "No common preferred channels found! Use: %d MHz for oper_channel",
     671             :                         dev->oper_freq);
     672             :         }
     673           0 : }
     674             : 
     675             : 
     676         379 : void p2p_check_pref_chan(struct p2p_data *p2p, int go,
     677             :                          struct p2p_device *dev, struct p2p_message *msg)
     678             : {
     679             :         unsigned int freq_list[P2P_MAX_PREF_CHANNELS], size;
     680             :         unsigned int i;
     681             :         u8 op_class, op_channel;
     682             : 
     683             :         /*
     684             :          * Use the preferred channel list from the driver only if there is no
     685             :          * forced_freq, e.g., P2P_CONNECT freq=..., and no preferred operating
     686             :          * channel hardcoded in the configuration file.
     687             :          */
     688         756 :         if (!p2p->cfg->get_pref_freq_list || p2p->cfg->num_pref_chan ||
     689         703 :             (dev->flags & P2P_DEV_FORCE_FREQ) || p2p->cfg->cfg_op_channel)
     690         433 :                 return;
     691             : 
     692             :         /* Obtain our preferred frequency list from driver based on P2P role. */
     693         325 :         size = P2P_MAX_PREF_CHANNELS;
     694         325 :         if (p2p->cfg->get_pref_freq_list(p2p->cfg->cb_ctx, go, &size,
     695             :                                          freq_list))
     696         325 :                 return;
     697             : 
     698             :         /*
     699             :          * Check if peer's preference of operating channel is in
     700             :          * our preferred channel list.
     701             :          */
     702           0 :         for (i = 0; i < size; i++) {
     703           0 :                 if (freq_list[i] == (unsigned int) dev->oper_freq)
     704           0 :                         break;
     705             :         }
     706           0 :         if (i != size) {
     707             :                 /* Peer operating channel preference matches our preference */
     708           0 :                 if (p2p_freq_to_channel(freq_list[i], &op_class, &op_channel) <
     709             :                     0) {
     710           0 :                         p2p_dbg(p2p,
     711             :                                 "Peer operating channel preference is unsupported frequency %u MHz",
     712             :                                 freq_list[i]);
     713             :                 } else {
     714           0 :                         p2p->op_reg_class = op_class;
     715           0 :                         p2p->op_channel = op_channel;
     716           0 :                         os_memcpy(&p2p->channels, &p2p->cfg->channels,
     717             :                                   sizeof(struct p2p_channels));
     718           0 :                         return;
     719             :                 }
     720             :         }
     721             : 
     722           0 :         p2p_dbg(p2p,
     723             :                 "Peer operating channel preference: %d MHz is not in our preferred channel list",
     724             :                 dev->oper_freq);
     725             : 
     726             :         /*
     727             :           Check if peer's preferred channel list is
     728             :           * _not_ included in the GO Negotiation Request or Invitation Request.
     729             :           */
     730           0 :         if (msg->pref_freq_list_len == 0)
     731           0 :                 p2p_check_pref_chan_no_recv(p2p, go, dev, msg, freq_list, size);
     732             :         else
     733           0 :                 p2p_check_pref_chan_recv(p2p, go, dev, msg, freq_list, size);
     734             : }
     735             : 
     736             : 
     737         248 : void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
     738             :                             const u8 *data, size_t len, int rx_freq)
     739             : {
     740         248 :         struct p2p_device *dev = NULL;
     741             :         struct wpabuf *resp;
     742             :         struct p2p_message msg;
     743         248 :         u8 status = P2P_SC_FAIL_INVALID_PARAMS;
     744         248 :         int tie_breaker = 0;
     745             :         int freq;
     746             : 
     747        1488 :         p2p_dbg(p2p, "Received GO Negotiation Request from " MACSTR "(freq=%d)",
     748        1488 :                 MAC2STR(sa), rx_freq);
     749             : 
     750         248 :         if (p2p_parse(data, len, &msg))
     751          42 :                 return;
     752             : 
     753         247 :         if (!msg.capability) {
     754           3 :                 p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Request");
     755             : #ifdef CONFIG_P2P_STRICT
     756             :                 goto fail;
     757             : #endif /* CONFIG_P2P_STRICT */
     758             :         }
     759             : 
     760         247 :         if (msg.go_intent)
     761         245 :                 tie_breaker = *msg.go_intent & 0x01;
     762             :         else {
     763           2 :                 p2p_dbg(p2p, "Mandatory GO Intent attribute missing from GO Negotiation Request");
     764             : #ifdef CONFIG_P2P_STRICT
     765             :                 goto fail;
     766             : #endif /* CONFIG_P2P_STRICT */
     767             :         }
     768             : 
     769         247 :         if (!msg.config_timeout) {
     770           3 :                 p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Request");
     771             : #ifdef CONFIG_P2P_STRICT
     772             :                 goto fail;
     773             : #endif /* CONFIG_P2P_STRICT */
     774             :         }
     775             : 
     776         247 :         if (!msg.listen_channel) {
     777           1 :                 p2p_dbg(p2p, "No Listen Channel attribute received");
     778           1 :                 goto fail;
     779             :         }
     780         246 :         if (!msg.operating_channel) {
     781           1 :                 p2p_dbg(p2p, "No Operating Channel attribute received");
     782           1 :                 goto fail;
     783             :         }
     784         245 :         if (!msg.channel_list) {
     785           1 :                 p2p_dbg(p2p, "No Channel List attribute received");
     786           1 :                 goto fail;
     787             :         }
     788         244 :         if (!msg.intended_addr) {
     789           1 :                 p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
     790           1 :                 goto fail;
     791             :         }
     792         243 :         if (!msg.p2p_device_info) {
     793           1 :                 p2p_dbg(p2p, "No P2P Device Info attribute received");
     794           1 :                 goto fail;
     795             :         }
     796             : 
     797         242 :         if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
     798          12 :                 p2p_dbg(p2p, "Unexpected GO Negotiation Request SA=" MACSTR
     799             :                         " != dev_addr=" MACSTR,
     800          12 :                         MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
     801           1 :                 goto fail;
     802             :         }
     803             : 
     804         241 :         dev = p2p_get_device(p2p, sa);
     805             : 
     806         241 :         if (msg.status && *msg.status) {
     807           2 :                 p2p_dbg(p2p, "Unexpected Status attribute (%d) in GO Negotiation Request",
     808           2 :                         *msg.status);
     809           3 :                 if (dev && p2p->go_neg_peer == dev &&
     810           1 :                     *msg.status == P2P_SC_FAIL_REJECTED_BY_USER) {
     811             :                         /*
     812             :                          * This mechanism for using Status attribute in GO
     813             :                          * Negotiation Request is not compliant with the P2P
     814             :                          * specification, but some deployed devices use it to
     815             :                          * indicate rejection of GO Negotiation in a case where
     816             :                          * they have sent out GO Negotiation Response with
     817             :                          * status 1. The P2P specification explicitly disallows
     818             :                          * this. To avoid unnecessary interoperability issues
     819             :                          * and extra frames, mark the pending negotiation as
     820             :                          * failed and do not reply to this GO Negotiation
     821             :                          * Request frame.
     822             :                          */
     823           1 :                         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
     824           1 :                         p2p_go_neg_failed(p2p, *msg.status);
     825           1 :                         p2p_parse_free(&msg);
     826           1 :                         return;
     827             :                 }
     828           1 :                 goto fail;
     829             :         }
     830             : 
     831         239 :         if (dev == NULL)
     832           6 :                 dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
     833         442 :         else if ((dev->flags & P2P_DEV_PROBE_REQ_ONLY) ||
     834         209 :                   !(dev->flags & P2P_DEV_REPORTED))
     835          32 :                 p2p_add_dev_info(p2p, sa, dev, &msg);
     836         201 :         else if (!dev->listen_freq && !dev->oper_freq) {
     837             :                 /*
     838             :                  * This may happen if the peer entry was added based on PD
     839             :                  * Request and no Probe Request/Response frame has been received
     840             :                  * from this peer (or that information has timed out).
     841             :                  */
     842          36 :                 p2p_dbg(p2p, "Update peer " MACSTR
     843             :                         " based on GO Neg Req since listen/oper freq not known",
     844          36 :                         MAC2STR(dev->info.p2p_device_addr));
     845           6 :                 p2p_add_dev_info(p2p, sa, dev, &msg);
     846             :         }
     847             : 
     848         239 :         if (p2p->go_neg_peer && p2p->go_neg_peer == dev)
     849          64 :                 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
     850             : 
     851         239 :         if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
     852           2 :                 p2p_dbg(p2p, "User has rejected this peer");
     853           2 :                 status = P2P_SC_FAIL_REJECTED_BY_USER;
     854         474 :         } else if (dev == NULL ||
     855         282 :                    (dev->wps_method == WPS_NOT_READY &&
     856          57 :                     (p2p->authorized_oob_dev_pw_id == 0 ||
     857          12 :                      p2p->authorized_oob_dev_pw_id !=
     858          12 :                      msg.dev_password_id))) {
     859         246 :                 p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
     860         246 :                         MAC2STR(sa));
     861          41 :                 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
     862         163 :                 p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
     863          41 :                                         msg.dev_password_id,
     864          81 :                                         msg.go_intent ? (*msg.go_intent >> 1) :
     865             :                                         0);
     866         196 :         } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
     867           1 :                 p2p_dbg(p2p, "Already in Group Formation with another peer");
     868           1 :                 status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
     869             :         } else {
     870             :                 int go;
     871             : 
     872         195 :                 if (!p2p->go_neg_peer) {
     873         131 :                         p2p_dbg(p2p, "Starting GO Negotiation with previously authorized peer");
     874         131 :                         if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
     875         124 :                                 p2p_dbg(p2p, "Use default channel settings");
     876         124 :                                 p2p->op_reg_class = p2p->cfg->op_reg_class;
     877         124 :                                 p2p->op_channel = p2p->cfg->op_channel;
     878         124 :                                 os_memcpy(&p2p->channels, &p2p->cfg->channels,
     879             :                                           sizeof(struct p2p_channels));
     880             :                         } else {
     881           7 :                                 p2p_dbg(p2p, "Use previously configured forced channel settings");
     882             :                         }
     883             :                 }
     884             : 
     885         195 :                 dev->flags &= ~P2P_DEV_NOT_YET_READY;
     886             : 
     887         195 :                 if (!msg.go_intent) {
     888           1 :                         p2p_dbg(p2p, "No GO Intent attribute received");
     889           1 :                         goto fail;
     890             :                 }
     891         194 :                 if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
     892           1 :                         p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
     893           1 :                                 *msg.go_intent >> 1);
     894           1 :                         goto fail;
     895             :                 }
     896             : 
     897         234 :                 if (dev->go_neg_req_sent &&
     898          41 :                     os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
     899          38 :                         p2p_dbg(p2p, "Do not reply since peer has higher address and GO Neg Request already sent");
     900          38 :                         p2p_parse_free(&msg);
     901          38 :                         return;
     902             :                 }
     903             : 
     904         158 :                 if (dev->go_neg_req_sent &&
     905           3 :                     (dev->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
     906           1 :                         p2p_dbg(p2p,
     907             :                                 "Do not reply since peer is waiting for us to start a new GO Negotiation and GO Neg Request already sent");
     908           1 :                         p2p_parse_free(&msg);
     909           1 :                         return;
     910             :                 }
     911             : 
     912         154 :                 go = p2p_go_det(p2p->go_intent, *msg.go_intent);
     913         154 :                 if (go < 0) {
     914           1 :                         p2p_dbg(p2p, "Incompatible GO Intent");
     915           1 :                         status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
     916           1 :                         goto fail;
     917             :                 }
     918             : 
     919         153 :                 if (p2p_peer_channels(p2p, dev, msg.channel_list,
     920         153 :                                       msg.channel_list_len) < 0) {
     921           2 :                         p2p_dbg(p2p, "No common channels found");
     922           2 :                         status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
     923           2 :                         goto fail;
     924             :                 }
     925             : 
     926         151 :                 switch (msg.dev_password_id) {
     927             :                 case DEV_PW_REGISTRAR_SPECIFIED:
     928          18 :                         p2p_dbg(p2p, "PIN from peer Display");
     929          18 :                         if (dev->wps_method != WPS_PIN_KEYPAD) {
     930           2 :                                 p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
     931             :                                         p2p_wps_method_str(dev->wps_method));
     932           2 :                                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
     933           2 :                                 goto fail;
     934             :                         }
     935          16 :                         break;
     936             :                 case DEV_PW_USER_SPECIFIED:
     937          88 :                         p2p_dbg(p2p, "Peer entered PIN on Keypad");
     938          88 :                         if (dev->wps_method != WPS_PIN_DISPLAY) {
     939           1 :                                 p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
     940             :                                         p2p_wps_method_str(dev->wps_method));
     941           1 :                                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
     942           1 :                                 goto fail;
     943             :                         }
     944          87 :                         break;
     945             :                 case DEV_PW_PUSHBUTTON:
     946          25 :                         p2p_dbg(p2p, "Peer using pushbutton");
     947          25 :                         if (dev->wps_method != WPS_PBC) {
     948           1 :                                 p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
     949             :                                         p2p_wps_method_str(dev->wps_method));
     950           1 :                                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
     951           1 :                                 goto fail;
     952             :                         }
     953          24 :                         break;
     954             :                 case DEV_PW_P2PS_DEFAULT:
     955          10 :                         p2p_dbg(p2p, "Peer using P2PS pin");
     956          10 :                         if (dev->wps_method != WPS_P2PS) {
     957           0 :                                 p2p_dbg(p2p,
     958             :                                         "We have wps_method=%s -> incompatible",
     959             :                                         p2p_wps_method_str(dev->wps_method));
     960           0 :                                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
     961           0 :                                 goto fail;
     962             :                         }
     963          10 :                         break;
     964             :                 default:
     965          19 :                         if (msg.dev_password_id &&
     966           9 :                             msg.dev_password_id == dev->oob_pw_id) {
     967           5 :                                 p2p_dbg(p2p, "Peer using NFC");
     968           5 :                                 if (dev->wps_method != WPS_NFC) {
     969           0 :                                         p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
     970             :                                                 p2p_wps_method_str(
     971             :                                                         dev->wps_method));
     972           0 :                                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
     973           0 :                                         goto fail;
     974             :                                 }
     975           5 :                                 break;
     976             :                         }
     977             : #ifdef CONFIG_WPS_NFC
     978           9 :                         if (p2p->authorized_oob_dev_pw_id &&
     979           4 :                             msg.dev_password_id ==
     980           4 :                             p2p->authorized_oob_dev_pw_id) {
     981           4 :                                 p2p_dbg(p2p, "Using static handover with our device password from NFC Tag");
     982           4 :                                 dev->wps_method = WPS_NFC;
     983           4 :                                 dev->oob_pw_id = p2p->authorized_oob_dev_pw_id;
     984           4 :                                 break;
     985             :                         }
     986             : #endif /* CONFIG_WPS_NFC */
     987           1 :                         p2p_dbg(p2p, "Unsupported Device Password ID %d",
     988           1 :                                 msg.dev_password_id);
     989           1 :                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
     990           1 :                         goto fail;
     991             :                 }
     992             : 
     993         146 :                 if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
     994           1 :                         goto fail;
     995             : 
     996         145 :                 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
     997         145 :                 dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
     998         145 :                                                      msg.operating_channel[4]);
     999         145 :                 p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
    1000             :                         dev->oper_freq);
    1001             : 
    1002             :                 /*
    1003             :                  * Use the driver preferred frequency list extension if
    1004             :                  * supported.
    1005             :                  */
    1006         145 :                 p2p_check_pref_chan(p2p, go, dev, &msg);
    1007             : 
    1008         145 :                 if (msg.config_timeout) {
    1009         145 :                         dev->go_timeout = msg.config_timeout[0];
    1010         145 :                         dev->client_timeout = msg.config_timeout[1];
    1011             :                 }
    1012             : 
    1013         145 :                 p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa));
    1014         145 :                 if (p2p->state != P2P_IDLE)
    1015         145 :                         p2p_stop_find_for_freq(p2p, rx_freq);
    1016         145 :                 p2p_set_state(p2p, P2P_GO_NEG);
    1017         145 :                 p2p_clear_timeout(p2p);
    1018         145 :                 dev->dialog_token = msg.dialog_token;
    1019         145 :                 os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
    1020         145 :                 p2p->go_neg_peer = dev;
    1021         145 :                 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
    1022         145 :                 status = P2P_SC_SUCCESS;
    1023             :         }
    1024             : 
    1025             : fail:
    1026         207 :         if (dev)
    1027         200 :                 dev->status = status;
    1028         207 :         resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
    1029             :                                      !tie_breaker);
    1030         207 :         p2p_parse_free(&msg);
    1031         207 :         if (resp == NULL)
    1032           0 :                 return;
    1033         207 :         p2p_dbg(p2p, "Sending GO Negotiation Response");
    1034         207 :         if (rx_freq > 0)
    1035         207 :                 freq = rx_freq;
    1036             :         else
    1037           0 :                 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
    1038           0 :                                            p2p->cfg->channel);
    1039         207 :         if (freq < 0) {
    1040           0 :                 p2p_dbg(p2p, "Unknown regulatory class/channel");
    1041           0 :                 wpabuf_free(resp);
    1042           0 :                 return;
    1043             :         }
    1044         207 :         if (status == P2P_SC_SUCCESS) {
    1045         145 :                 p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
    1046         145 :                 dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
    1047         145 :                 if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) {
    1048             :                         /*
    1049             :                          * Peer has smaller address, so the GO Negotiation
    1050             :                          * Response from us is expected to complete
    1051             :                          * negotiation. Ignore a GO Negotiation Response from
    1052             :                          * the peer if it happens to be received after this
    1053             :                          * point due to a race condition in GO Negotiation
    1054             :                          * Request transmission and processing.
    1055             :                          */
    1056          95 :                         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
    1057             :                 }
    1058             :         } else
    1059          62 :                 p2p->pending_action_state =
    1060             :                         P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
    1061         414 :         if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
    1062         207 :                             p2p->cfg->dev_addr,
    1063         207 :                             wpabuf_head(resp), wpabuf_len(resp), 100) < 0) {
    1064           0 :                 p2p_dbg(p2p, "Failed to send Action frame");
    1065             :         }
    1066             : 
    1067         207 :         wpabuf_free(resp);
    1068             : }
    1069             : 
    1070             : 
    1071         154 : static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
    1072             :                                              struct p2p_device *peer,
    1073             :                                              u8 dialog_token, u8 status,
    1074             :                                              const u8 *resp_chan, int go)
    1075             : {
    1076             :         struct wpabuf *buf;
    1077             :         u8 *len;
    1078             :         struct p2p_channels res;
    1079             :         u8 group_capab;
    1080         154 :         size_t extra = 0;
    1081             : 
    1082         154 :         p2p_dbg(p2p, "Building GO Negotiation Confirm");
    1083             : 
    1084             : #ifdef CONFIG_WIFI_DISPLAY
    1085         154 :         if (p2p->wfd_ie_go_neg)
    1086           2 :                 extra = wpabuf_len(p2p->wfd_ie_go_neg);
    1087             : #endif /* CONFIG_WIFI_DISPLAY */
    1088             : 
    1089         154 :         if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
    1090           1 :                 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
    1091             : 
    1092         154 :         buf = wpabuf_alloc(1000 + extra);
    1093         154 :         if (buf == NULL)
    1094           0 :                 return NULL;
    1095             : 
    1096         154 :         p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
    1097             : 
    1098         154 :         len = p2p_buf_add_ie_hdr(buf);
    1099         154 :         p2p_buf_add_status(buf, status);
    1100         154 :         group_capab = 0;
    1101         154 :         if (peer->go_state == LOCAL_GO) {
    1102           0 :                 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
    1103           0 :                         group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
    1104           0 :                         if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
    1105           0 :                                 group_capab |=
    1106             :                                         P2P_GROUP_CAPAB_PERSISTENT_RECONN;
    1107             :                 }
    1108           0 :                 if (p2p->cross_connect)
    1109           0 :                         group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
    1110           0 :                 if (p2p->cfg->p2p_intra_bss)
    1111           0 :                         group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
    1112             :         }
    1113         154 :         p2p_buf_add_capability(buf, p2p->dev_capab &
    1114             :                                ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
    1115             :                                group_capab);
    1116         154 :         if (go || resp_chan == NULL)
    1117         216 :                 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
    1118         108 :                                               p2p->op_reg_class,
    1119         108 :                                               p2p->op_channel);
    1120             :         else
    1121          92 :                 p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
    1122          92 :                                               resp_chan[3], resp_chan[4]);
    1123         154 :         p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
    1124         154 :         p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
    1125         154 :         if (go) {
    1126         107 :                 p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
    1127             :                                      p2p->ssid_len);
    1128             :         }
    1129         154 :         p2p_buf_update_ie_hdr(buf, len);
    1130             : 
    1131             : #ifdef CONFIG_WIFI_DISPLAY
    1132         154 :         if (p2p->wfd_ie_go_neg)
    1133           2 :                 wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
    1134             : #endif /* CONFIG_WIFI_DISPLAY */
    1135             : 
    1136         154 :         if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
    1137           1 :                 wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
    1138             : 
    1139         154 :         return buf;
    1140             : }
    1141             : 
    1142             : 
    1143         204 : void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
    1144             :                              const u8 *data, size_t len, int rx_freq)
    1145             : {
    1146             :         struct p2p_device *dev;
    1147         204 :         int go = -1;
    1148             :         struct p2p_message msg;
    1149         204 :         u8 status = P2P_SC_SUCCESS;
    1150             :         int freq;
    1151             : 
    1152        1224 :         p2p_dbg(p2p, "Received GO Negotiation Response from " MACSTR
    1153        1224 :                 " (freq=%d)", MAC2STR(sa), rx_freq);
    1154         204 :         dev = p2p_get_device(p2p, sa);
    1155         407 :         if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
    1156         203 :             dev != p2p->go_neg_peer) {
    1157           6 :                 p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
    1158           6 :                         MAC2STR(sa));
    1159           1 :                 return;
    1160             :         }
    1161             : 
    1162         203 :         if (p2p_parse(data, len, &msg))
    1163           1 :                 return;
    1164             : 
    1165         202 :         if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
    1166           1 :                 p2p_dbg(p2p, "Was not expecting GO Negotiation Response - ignore");
    1167           1 :                 p2p_parse_free(&msg);
    1168           1 :                 return;
    1169             :         }
    1170         201 :         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
    1171             : 
    1172         201 :         if (msg.dialog_token != dev->dialog_token) {
    1173           2 :                 p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
    1174           2 :                         msg.dialog_token, dev->dialog_token);
    1175           1 :                 p2p_parse_free(&msg);
    1176           1 :                 return;
    1177             :         }
    1178             : 
    1179         200 :         if (!msg.status) {
    1180           1 :                 p2p_dbg(p2p, "No Status attribute received");
    1181           1 :                 status = P2P_SC_FAIL_INVALID_PARAMS;
    1182           1 :                 goto fail;
    1183             :         }
    1184         199 :         if (*msg.status) {
    1185          46 :                 p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status);
    1186          46 :                 dev->go_neg_req_sent = 0;
    1187          46 :                 if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
    1188          36 :                         p2p_dbg(p2p, "Wait for the peer to become ready for GO Negotiation");
    1189          36 :                         dev->flags |= P2P_DEV_NOT_YET_READY;
    1190          36 :                         eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p,
    1191             :                                              NULL);
    1192          36 :                         eloop_register_timeout(120, 0, p2p_go_neg_wait_timeout,
    1193             :                                                p2p, NULL);
    1194          36 :                         if (p2p->state == P2P_CONNECT_LISTEN)
    1195           0 :                                 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
    1196             :                         else
    1197          36 :                                 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
    1198          36 :                         p2p_set_timeout(p2p, 0, 0);
    1199             :                 } else {
    1200          10 :                         p2p_dbg(p2p, "Stop GO Negotiation attempt");
    1201          10 :                         p2p_go_neg_failed(p2p, *msg.status);
    1202             :                 }
    1203          46 :                 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    1204          46 :                 p2p_parse_free(&msg);
    1205          46 :                 return;
    1206             :         }
    1207             : 
    1208         153 :         if (!msg.capability) {
    1209           1 :                 p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Response");
    1210             : #ifdef CONFIG_P2P_STRICT
    1211             :                 status = P2P_SC_FAIL_INVALID_PARAMS;
    1212             :                 goto fail;
    1213             : #endif /* CONFIG_P2P_STRICT */
    1214             :         }
    1215             : 
    1216         153 :         if (!msg.p2p_device_info) {
    1217           1 :                 p2p_dbg(p2p, "Mandatory P2P Device Info attribute missing from GO Negotiation Response");
    1218             : #ifdef CONFIG_P2P_STRICT
    1219             :                 status = P2P_SC_FAIL_INVALID_PARAMS;
    1220             :                 goto fail;
    1221             : #endif /* CONFIG_P2P_STRICT */
    1222             :         }
    1223             : 
    1224         153 :         if (!msg.intended_addr) {
    1225           1 :                 p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
    1226           1 :                 status = P2P_SC_FAIL_INVALID_PARAMS;
    1227           1 :                 goto fail;
    1228             :         }
    1229             : 
    1230         152 :         if (!msg.go_intent) {
    1231           1 :                 p2p_dbg(p2p, "No GO Intent attribute received");
    1232           1 :                 status = P2P_SC_FAIL_INVALID_PARAMS;
    1233           1 :                 goto fail;
    1234             :         }
    1235         151 :         if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
    1236           1 :                 p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
    1237           1 :                         *msg.go_intent >> 1);
    1238           1 :                 status = P2P_SC_FAIL_INVALID_PARAMS;
    1239           1 :                 goto fail;
    1240             :         }
    1241             : 
    1242         150 :         go = p2p_go_det(p2p->go_intent, *msg.go_intent);
    1243         150 :         if (go < 0) {
    1244           1 :                 p2p_dbg(p2p, "Incompatible GO Intent");
    1245           1 :                 status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
    1246           1 :                 goto fail;
    1247             :         }
    1248             : 
    1249         149 :         if (!go && msg.group_id) {
    1250             :                 /* Store SSID for Provisioning step */
    1251          46 :                 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
    1252          46 :                 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
    1253         103 :         } else if (!go) {
    1254           1 :                 p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Response");
    1255           1 :                 p2p->ssid_len = 0;
    1256           1 :                 status = P2P_SC_FAIL_INVALID_PARAMS;
    1257           1 :                 goto fail;
    1258             :         }
    1259             : 
    1260         148 :         if (!msg.config_timeout) {
    1261           1 :                 p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Response");
    1262             : #ifdef CONFIG_P2P_STRICT
    1263             :                 status = P2P_SC_FAIL_INVALID_PARAMS;
    1264             :                 goto fail;
    1265             : #endif /* CONFIG_P2P_STRICT */
    1266             :         } else {
    1267         147 :                 dev->go_timeout = msg.config_timeout[0];
    1268         147 :                 dev->client_timeout = msg.config_timeout[1];
    1269             :         }
    1270             : 
    1271         148 :         if (msg.wfd_subelems) {
    1272           2 :                 wpabuf_free(dev->info.wfd_subelems);
    1273           2 :                 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
    1274             :         }
    1275             : 
    1276         148 :         if (!msg.operating_channel && !go) {
    1277             :                 /*
    1278             :                  * Note: P2P Client may omit Operating Channel attribute to
    1279             :                  * indicate it does not have a preference.
    1280             :                  */
    1281           1 :                 p2p_dbg(p2p, "No Operating Channel attribute received");
    1282           1 :                 status = P2P_SC_FAIL_INVALID_PARAMS;
    1283           1 :                 goto fail;
    1284             :         }
    1285         147 :         if (!msg.channel_list) {
    1286           1 :                 p2p_dbg(p2p, "No Channel List attribute received");
    1287           1 :                 status = P2P_SC_FAIL_INVALID_PARAMS;
    1288           1 :                 goto fail;
    1289             :         }
    1290             : 
    1291         146 :         if (p2p_peer_channels(p2p, dev, msg.channel_list,
    1292         146 :                               msg.channel_list_len) < 0) {
    1293           1 :                 p2p_dbg(p2p, "No common channels found");
    1294           1 :                 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
    1295           1 :                 goto fail;
    1296             :         }
    1297             : 
    1298         145 :         if (msg.operating_channel) {
    1299          43 :                 dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
    1300          43 :                                                      msg.operating_channel[4]);
    1301          43 :                 p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
    1302             :                         dev->oper_freq);
    1303             :         } else
    1304         102 :                 dev->oper_freq = 0;
    1305             : 
    1306         145 :         switch (msg.dev_password_id) {
    1307             :         case DEV_PW_REGISTRAR_SPECIFIED:
    1308          86 :                 p2p_dbg(p2p, "PIN from peer Display");
    1309          86 :                 if (dev->wps_method != WPS_PIN_KEYPAD) {
    1310           0 :                         p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
    1311             :                                 p2p_wps_method_str(dev->wps_method));
    1312           0 :                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
    1313           0 :                         goto fail;
    1314             :                 }
    1315          86 :                 break;
    1316             :         case DEV_PW_USER_SPECIFIED:
    1317          16 :                 p2p_dbg(p2p, "Peer entered PIN on Keypad");
    1318          16 :                 if (dev->wps_method != WPS_PIN_DISPLAY) {
    1319           0 :                         p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
    1320             :                                 p2p_wps_method_str(dev->wps_method));
    1321           0 :                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
    1322           0 :                         goto fail;
    1323             :                 }
    1324          16 :                 break;
    1325             :         case DEV_PW_PUSHBUTTON:
    1326          24 :                 p2p_dbg(p2p, "Peer using pushbutton");
    1327          24 :                 if (dev->wps_method != WPS_PBC) {
    1328           0 :                         p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
    1329             :                                 p2p_wps_method_str(dev->wps_method));
    1330           0 :                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
    1331           0 :                         goto fail;
    1332             :                 }
    1333          24 :                 break;
    1334             :         case DEV_PW_P2PS_DEFAULT:
    1335          10 :                 p2p_dbg(p2p, "P2P: Peer using P2PS default pin");
    1336          10 :                 if (dev->wps_method != WPS_P2PS) {
    1337           0 :                         p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
    1338             :                                 p2p_wps_method_str(dev->wps_method));
    1339           0 :                         status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
    1340           0 :                         goto fail;
    1341             :                 }
    1342          10 :                 break;
    1343             :         default:
    1344          18 :                 if (msg.dev_password_id &&
    1345           9 :                     msg.dev_password_id == dev->oob_pw_id) {
    1346           9 :                         p2p_dbg(p2p, "Peer using NFC");
    1347           9 :                         if (dev->wps_method != WPS_NFC) {
    1348           0 :                                 p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
    1349             :                                         p2p_wps_method_str(dev->wps_method));
    1350           0 :                                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
    1351           0 :                                 goto fail;
    1352             :                         }
    1353           9 :                         break;
    1354             :                 }
    1355           0 :                 p2p_dbg(p2p, "Unsupported Device Password ID %d",
    1356           0 :                         msg.dev_password_id);
    1357           0 :                 status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
    1358           0 :                 goto fail;
    1359             :         }
    1360             : 
    1361         145 :         if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
    1362           0 :                 goto fail;
    1363             : 
    1364             :         /*
    1365             :          * Use the driver preferred frequency list extension if local device is
    1366             :          * GO.
    1367             :          */
    1368         145 :         if (go)
    1369         102 :                 p2p_check_pref_chan(p2p, go, dev, &msg);
    1370             : 
    1371         145 :         p2p_set_state(p2p, P2P_GO_NEG);
    1372         145 :         p2p_clear_timeout(p2p);
    1373             : 
    1374         145 :         p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa));
    1375         145 :         os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
    1376             : 
    1377             : fail:
    1378             :         /* Store GO Negotiation Confirmation to allow retransmission */
    1379         154 :         wpabuf_free(dev->go_neg_conf);
    1380         154 :         dev->go_neg_conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token,
    1381             :                                                  status, msg.operating_channel,
    1382             :                                                  go);
    1383         154 :         p2p_parse_free(&msg);
    1384         154 :         if (dev->go_neg_conf == NULL)
    1385           0 :                 return;
    1386         154 :         p2p_dbg(p2p, "Sending GO Negotiation Confirm");
    1387         154 :         if (status == P2P_SC_SUCCESS) {
    1388         145 :                 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
    1389         145 :                 dev->go_state = go ? LOCAL_GO : REMOTE_GO;
    1390             :         } else
    1391           9 :                 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
    1392         154 :         if (rx_freq > 0)
    1393         154 :                 freq = rx_freq;
    1394             :         else
    1395           0 :                 freq = dev->listen_freq;
    1396             : 
    1397         154 :         dev->go_neg_conf_freq = freq;
    1398         154 :         dev->go_neg_conf_sent = 0;
    1399             : 
    1400         308 :         if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
    1401         154 :                             wpabuf_head(dev->go_neg_conf),
    1402         154 :                             wpabuf_len(dev->go_neg_conf), 50) < 0) {
    1403           0 :                 p2p_dbg(p2p, "Failed to send Action frame");
    1404           0 :                 p2p_go_neg_failed(p2p, -1);
    1405           0 :                 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    1406             :         } else
    1407         154 :                 dev->go_neg_conf_sent++;
    1408         154 :         if (status != P2P_SC_SUCCESS) {
    1409           9 :                 p2p_dbg(p2p, "GO Negotiation failed");
    1410           9 :                 p2p_go_neg_failed(p2p, status);
    1411             :         }
    1412             : }
    1413             : 
    1414             : 
    1415         145 : void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
    1416             :                              const u8 *data, size_t len)
    1417             : {
    1418             :         struct p2p_device *dev;
    1419             :         struct p2p_message msg;
    1420             : 
    1421         870 :         p2p_dbg(p2p, "Received GO Negotiation Confirm from " MACSTR,
    1422         870 :                 MAC2STR(sa));
    1423         145 :         dev = p2p_get_device(p2p, sa);
    1424         290 :         if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
    1425         145 :             dev != p2p->go_neg_peer) {
    1426           0 :                 p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
    1427           0 :                         MAC2STR(sa));
    1428           0 :                 return;
    1429             :         }
    1430             : 
    1431         145 :         if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
    1432           0 :                 p2p_dbg(p2p, "Stopped waiting for TX status on GO Negotiation Response since we already received Confirmation");
    1433           0 :                 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
    1434             :         }
    1435             : 
    1436         145 :         if (p2p_parse(data, len, &msg))
    1437           0 :                 return;
    1438             : 
    1439         145 :         if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
    1440           0 :                 p2p_dbg(p2p, "Was not expecting GO Negotiation Confirm - ignore");
    1441           0 :                 p2p_parse_free(&msg);
    1442           0 :                 return;
    1443             :         }
    1444         145 :         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
    1445         145 :         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    1446             : 
    1447         145 :         if (msg.dialog_token != dev->dialog_token) {
    1448           0 :                 p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
    1449           0 :                         msg.dialog_token, dev->dialog_token);
    1450           0 :                 p2p_parse_free(&msg);
    1451           0 :                 return;
    1452             :         }
    1453             : 
    1454         145 :         if (!msg.status) {
    1455           0 :                 p2p_dbg(p2p, "No Status attribute received");
    1456           0 :                 p2p_parse_free(&msg);
    1457           0 :                 return;
    1458             :         }
    1459         145 :         if (*msg.status) {
    1460           0 :                 p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status);
    1461           0 :                 p2p_go_neg_failed(p2p, *msg.status);
    1462           0 :                 p2p_parse_free(&msg);
    1463           0 :                 return;
    1464             :         }
    1465             : 
    1466         145 :         if (dev->go_state == REMOTE_GO && msg.group_id) {
    1467             :                 /* Store SSID for Provisioning step */
    1468         102 :                 p2p->ssid_len = msg.group_id_len - ETH_ALEN;
    1469         102 :                 os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
    1470          43 :         } else if (dev->go_state == REMOTE_GO) {
    1471           0 :                 p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Confirmation");
    1472           0 :                 p2p->ssid_len = 0;
    1473           0 :                 p2p_go_neg_failed(p2p, P2P_SC_FAIL_INVALID_PARAMS);
    1474           0 :                 p2p_parse_free(&msg);
    1475           0 :                 return;
    1476             :         }
    1477             : 
    1478         145 :         if (!msg.operating_channel) {
    1479           0 :                 p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
    1480             : #ifdef CONFIG_P2P_STRICT
    1481             :                 p2p_parse_free(&msg);
    1482             :                 return;
    1483             : #endif /* CONFIG_P2P_STRICT */
    1484         145 :         } else if (dev->go_state == REMOTE_GO) {
    1485         102 :                 int oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
    1486         102 :                                                     msg.operating_channel[4]);
    1487         102 :                 if (oper_freq != dev->oper_freq) {
    1488           3 :                         p2p_dbg(p2p, "Updated peer (GO) operating channel preference from %d MHz to %d MHz",
    1489             :                                 dev->oper_freq, oper_freq);
    1490           3 :                         dev->oper_freq = oper_freq;
    1491             :                 }
    1492             :         }
    1493             : 
    1494         145 :         if (!msg.channel_list) {
    1495           0 :                 p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
    1496             : #ifdef CONFIG_P2P_STRICT
    1497             :                 p2p_parse_free(&msg);
    1498             :                 return;
    1499             : #endif /* CONFIG_P2P_STRICT */
    1500             :         }
    1501             : 
    1502         145 :         p2p_parse_free(&msg);
    1503             : 
    1504         145 :         if (dev->go_state == UNKNOWN_GO) {
    1505             :                 /*
    1506             :                  * This should not happen since GO negotiation has already
    1507             :                  * been completed.
    1508             :                  */
    1509           0 :                 p2p_dbg(p2p, "Unexpected GO Neg state - do not know which end becomes GO");
    1510           0 :                 return;
    1511             :         }
    1512             : 
    1513             :         /*
    1514             :          * The peer could have missed our ctrl::ack frame for GO Negotiation
    1515             :          * Confirm and continue retransmitting the frame. To reduce the
    1516             :          * likelihood of the peer not getting successful TX status for the
    1517             :          * GO Negotiation Confirm frame, wait a short time here before starting
    1518             :          * the group so that we will remain on the current channel to
    1519             :          * acknowledge any possible retransmission from the peer.
    1520             :          */
    1521         145 :         p2p_dbg(p2p, "20 ms wait on current channel before starting group");
    1522         145 :         os_sleep(0, 20000);
    1523             : 
    1524         145 :         p2p_go_complete(p2p, dev);
    1525             : }

Generated by: LCOV version 1.10