Coverage Report

Created: 2026-04-12 06:28

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