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 1426431149 Lines: 621 762 81.5 %
Date: 2015-03-15 Functions: 14 14 100.0 %

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

Generated by: LCOV version 1.10