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

Generated by: LCOV version 1.10