1f05cddf9SRui Paulo /*
2f05cddf9SRui Paulo * Wi-Fi Direct - P2P Group Owner Negotiation
3f05cddf9SRui Paulo * Copyright (c) 2009-2010, Atheros Communications
4f05cddf9SRui Paulo *
5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo * See README for more details.
7f05cddf9SRui Paulo */
8f05cddf9SRui Paulo
9f05cddf9SRui Paulo #include "includes.h"
10f05cddf9SRui Paulo
11f05cddf9SRui Paulo #include "common.h"
125b9c547cSRui Paulo #include "utils/eloop.h"
13f05cddf9SRui Paulo #include "common/ieee802_11_defs.h"
14*a90b9d01SCy Schubert #include "common/ieee802_11_common.h"
155b9c547cSRui Paulo #include "common/wpa_ctrl.h"
16f05cddf9SRui Paulo #include "wps/wps_defs.h"
17f05cddf9SRui Paulo #include "p2p_i.h"
18f05cddf9SRui Paulo #include "p2p.h"
19f05cddf9SRui Paulo
20f05cddf9SRui Paulo
p2p_go_det(u8 own_intent,u8 peer_value)21f05cddf9SRui Paulo static int p2p_go_det(u8 own_intent, u8 peer_value)
22f05cddf9SRui Paulo {
23f05cddf9SRui Paulo u8 peer_intent = peer_value >> 1;
24f05cddf9SRui Paulo if (own_intent == peer_intent) {
25f05cddf9SRui Paulo if (own_intent == P2P_MAX_GO_INTENT)
26f05cddf9SRui Paulo return -1; /* both devices want to become GO */
27f05cddf9SRui Paulo
28f05cddf9SRui Paulo /* Use tie breaker bit to determine GO */
29f05cddf9SRui Paulo return (peer_value & 0x01) ? 0 : 1;
30f05cddf9SRui Paulo }
31f05cddf9SRui Paulo
32f05cddf9SRui Paulo return own_intent > peer_intent;
33f05cddf9SRui Paulo }
34f05cddf9SRui Paulo
35f05cddf9SRui Paulo
p2p_peer_channels_check(struct p2p_data * p2p,struct p2p_channels * own,struct p2p_device * dev,const u8 * channel_list,size_t channel_list_len)36f05cddf9SRui Paulo int p2p_peer_channels_check(struct p2p_data *p2p, struct p2p_channels *own,
37f05cddf9SRui Paulo struct p2p_device *dev,
38f05cddf9SRui Paulo const u8 *channel_list, size_t channel_list_len)
39f05cddf9SRui Paulo {
40f05cddf9SRui Paulo const u8 *pos, *end;
41f05cddf9SRui Paulo struct p2p_channels *ch;
42780fb4a2SCy Schubert u8 channels;
43f05cddf9SRui Paulo struct p2p_channels intersection;
44f05cddf9SRui Paulo
45f05cddf9SRui Paulo ch = &dev->channels;
46f05cddf9SRui Paulo os_memset(ch, 0, sizeof(*ch));
47f05cddf9SRui Paulo pos = channel_list;
48f05cddf9SRui Paulo end = channel_list + channel_list_len;
49f05cddf9SRui Paulo
50f05cddf9SRui Paulo if (end - pos < 3)
51f05cddf9SRui Paulo return -1;
52f05cddf9SRui Paulo os_memcpy(dev->country, pos, 3);
53f05cddf9SRui Paulo wpa_hexdump_ascii(MSG_DEBUG, "P2P: Peer country", pos, 3);
54f05cddf9SRui Paulo if (pos[2] != 0x04 && os_memcmp(pos, p2p->cfg->country, 2) != 0) {
555b9c547cSRui Paulo p2p_info(p2p, "Mismatching country (ours=%c%c peer's=%c%c)",
56f05cddf9SRui Paulo p2p->cfg->country[0], p2p->cfg->country[1],
57f05cddf9SRui Paulo pos[0], pos[1]);
58f05cddf9SRui Paulo return -1;
59f05cddf9SRui Paulo }
60f05cddf9SRui Paulo pos += 3;
61f05cddf9SRui Paulo
62780fb4a2SCy Schubert while (end - pos > 2) {
63f05cddf9SRui Paulo struct p2p_reg_class *cl = &ch->reg_class[ch->reg_classes];
64f05cddf9SRui Paulo cl->reg_class = *pos++;
65780fb4a2SCy Schubert channels = *pos++;
66780fb4a2SCy Schubert if (channels > end - pos) {
675b9c547cSRui Paulo p2p_info(p2p, "Invalid peer Channel List");
68f05cddf9SRui Paulo return -1;
69f05cddf9SRui Paulo }
70f05cddf9SRui Paulo cl->channels = channels > P2P_MAX_REG_CLASS_CHANNELS ?
71f05cddf9SRui Paulo P2P_MAX_REG_CLASS_CHANNELS : channels;
72f05cddf9SRui Paulo os_memcpy(cl->channel, pos, cl->channels);
73f05cddf9SRui Paulo pos += channels;
74f05cddf9SRui Paulo ch->reg_classes++;
75f05cddf9SRui Paulo if (ch->reg_classes == P2P_MAX_REG_CLASSES)
76f05cddf9SRui Paulo break;
77f05cddf9SRui Paulo }
78f05cddf9SRui Paulo
79f05cddf9SRui Paulo p2p_channels_intersect(own, &dev->channels, &intersection);
805b9c547cSRui Paulo p2p_dbg(p2p, "Own reg_classes %d peer reg_classes %d intersection reg_classes %d",
81f05cddf9SRui Paulo (int) own->reg_classes,
82f05cddf9SRui Paulo (int) dev->channels.reg_classes,
83f05cddf9SRui Paulo (int) intersection.reg_classes);
84f05cddf9SRui Paulo if (intersection.reg_classes == 0) {
855b9c547cSRui Paulo p2p_info(p2p, "No common channels found");
86f05cddf9SRui Paulo return -1;
87f05cddf9SRui Paulo }
88f05cddf9SRui Paulo return 0;
89f05cddf9SRui Paulo }
90f05cddf9SRui Paulo
91f05cddf9SRui Paulo
p2p_peer_channels(struct p2p_data * p2p,struct p2p_device * dev,const u8 * channel_list,size_t channel_list_len)92f05cddf9SRui Paulo static int p2p_peer_channels(struct p2p_data *p2p, struct p2p_device *dev,
93f05cddf9SRui Paulo const u8 *channel_list, size_t channel_list_len)
94f05cddf9SRui Paulo {
95f05cddf9SRui Paulo return p2p_peer_channels_check(p2p, &p2p->channels, dev,
96f05cddf9SRui Paulo channel_list, channel_list_len);
97f05cddf9SRui Paulo }
98f05cddf9SRui Paulo
99f05cddf9SRui Paulo
p2p_wps_method_pw_id(enum p2p_wps_method wps_method)100f05cddf9SRui Paulo u16 p2p_wps_method_pw_id(enum p2p_wps_method wps_method)
101f05cddf9SRui Paulo {
102f05cddf9SRui Paulo switch (wps_method) {
103f05cddf9SRui Paulo case WPS_PIN_DISPLAY:
104f05cddf9SRui Paulo return DEV_PW_REGISTRAR_SPECIFIED;
105f05cddf9SRui Paulo case WPS_PIN_KEYPAD:
106f05cddf9SRui Paulo return DEV_PW_USER_SPECIFIED;
107f05cddf9SRui Paulo case WPS_PBC:
108f05cddf9SRui Paulo return DEV_PW_PUSHBUTTON;
1095b9c547cSRui Paulo case WPS_NFC:
1105b9c547cSRui Paulo return DEV_PW_NFC_CONNECTION_HANDOVER;
1115b9c547cSRui Paulo case WPS_P2PS:
1125b9c547cSRui Paulo return DEV_PW_P2PS_DEFAULT;
113f05cddf9SRui Paulo default:
114f05cddf9SRui Paulo return DEV_PW_DEFAULT;
115f05cddf9SRui Paulo }
116f05cddf9SRui Paulo }
117f05cddf9SRui Paulo
118f05cddf9SRui Paulo
p2p_wps_method_str(enum p2p_wps_method wps_method)119f05cddf9SRui Paulo static const char * p2p_wps_method_str(enum p2p_wps_method wps_method)
120f05cddf9SRui Paulo {
121f05cddf9SRui Paulo switch (wps_method) {
122f05cddf9SRui Paulo case WPS_PIN_DISPLAY:
123f05cddf9SRui Paulo return "Display";
124f05cddf9SRui Paulo case WPS_PIN_KEYPAD:
125f05cddf9SRui Paulo return "Keypad";
126f05cddf9SRui Paulo case WPS_PBC:
127f05cddf9SRui Paulo return "PBC";
1285b9c547cSRui Paulo case WPS_NFC:
1295b9c547cSRui Paulo return "NFC";
1305b9c547cSRui Paulo case WPS_P2PS:
1315b9c547cSRui Paulo return "P2PS";
132f05cddf9SRui Paulo default:
133f05cddf9SRui Paulo return "??";
134f05cddf9SRui Paulo }
135f05cddf9SRui Paulo }
136f05cddf9SRui Paulo
137f05cddf9SRui Paulo
p2p_build_go_neg_req(struct p2p_data * p2p,struct p2p_device * peer)138f05cddf9SRui Paulo static struct wpabuf * p2p_build_go_neg_req(struct p2p_data *p2p,
139f05cddf9SRui Paulo struct p2p_device *peer)
140f05cddf9SRui Paulo {
141f05cddf9SRui Paulo struct wpabuf *buf;
142f05cddf9SRui Paulo u8 *len;
143f05cddf9SRui Paulo u8 group_capab;
144f05cddf9SRui Paulo size_t extra = 0;
1455b9c547cSRui Paulo u16 pw_id;
146*a90b9d01SCy Schubert bool is_6ghz_capab;
147f05cddf9SRui Paulo
148f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
149f05cddf9SRui Paulo if (p2p->wfd_ie_go_neg)
150f05cddf9SRui Paulo extra = wpabuf_len(p2p->wfd_ie_go_neg);
151f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
152f05cddf9SRui Paulo
1535b9c547cSRui Paulo if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ])
1545b9c547cSRui Paulo extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]);
1555b9c547cSRui Paulo
156f05cddf9SRui Paulo buf = wpabuf_alloc(1000 + extra);
157f05cddf9SRui Paulo if (buf == NULL)
158f05cddf9SRui Paulo return NULL;
159f05cddf9SRui Paulo
160f05cddf9SRui Paulo p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_REQ, peer->dialog_token);
161f05cddf9SRui Paulo
162f05cddf9SRui Paulo len = p2p_buf_add_ie_hdr(buf);
163f05cddf9SRui Paulo group_capab = 0;
164f05cddf9SRui Paulo if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
165f05cddf9SRui Paulo group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
166f05cddf9SRui Paulo if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
167f05cddf9SRui Paulo group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN;
168f05cddf9SRui Paulo }
169f05cddf9SRui Paulo if (p2p->cross_connect)
170f05cddf9SRui Paulo group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
171f05cddf9SRui Paulo if (p2p->cfg->p2p_intra_bss)
172f05cddf9SRui Paulo group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
173f05cddf9SRui Paulo p2p_buf_add_capability(buf, p2p->dev_capab &
174f05cddf9SRui Paulo ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
175f05cddf9SRui Paulo group_capab);
1765b9c547cSRui Paulo p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | peer->tie_breaker);
177f05cddf9SRui Paulo p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
178f05cddf9SRui Paulo p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
179f05cddf9SRui Paulo p2p->cfg->channel);
180f05cddf9SRui Paulo if (p2p->ext_listen_interval)
181f05cddf9SRui Paulo p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
182f05cddf9SRui Paulo p2p->ext_listen_interval);
183f05cddf9SRui Paulo p2p_buf_add_intended_addr(buf, p2p->intended_addr);
184*a90b9d01SCy Schubert is_6ghz_capab = is_p2p_6ghz_capable(p2p) &&
185*a90b9d01SCy Schubert p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr);
186*a90b9d01SCy Schubert if (p2p->num_pref_freq) {
187*a90b9d01SCy Schubert bool go = p2p->go_intent == 15;
188*a90b9d01SCy Schubert struct p2p_channels pref_chanlist;
189*a90b9d01SCy Schubert
190*a90b9d01SCy Schubert p2p_pref_channel_filter(&p2p->channels, p2p->pref_freq_list,
191*a90b9d01SCy Schubert p2p->num_pref_freq, &pref_chanlist, go);
192*a90b9d01SCy Schubert p2p_channels_dump(p2p, "channel list after filtering",
193*a90b9d01SCy Schubert &pref_chanlist);
194*a90b9d01SCy Schubert p2p_buf_add_channel_list(buf, p2p->cfg->country,
195*a90b9d01SCy Schubert &pref_chanlist, is_6ghz_capab);
196*a90b9d01SCy Schubert } else {
197*a90b9d01SCy Schubert p2p_buf_add_channel_list(buf, p2p->cfg->country,
198*a90b9d01SCy Schubert &p2p->channels, is_6ghz_capab);
199*a90b9d01SCy Schubert }
200f05cddf9SRui Paulo p2p_buf_add_device_info(buf, p2p, peer);
201f05cddf9SRui Paulo p2p_buf_add_operating_channel(buf, p2p->cfg->country,
202f05cddf9SRui Paulo p2p->op_reg_class, p2p->op_channel);
203f05cddf9SRui Paulo p2p_buf_update_ie_hdr(buf, len);
204f05cddf9SRui Paulo
205325151a3SRui Paulo p2p_buf_add_pref_channel_list(buf, p2p->pref_freq_list,
206325151a3SRui Paulo p2p->num_pref_freq);
207325151a3SRui Paulo
208f05cddf9SRui Paulo /* WPS IE with Device Password ID attribute */
2095b9c547cSRui Paulo pw_id = p2p_wps_method_pw_id(peer->wps_method);
2105b9c547cSRui Paulo if (peer->oob_pw_id)
2115b9c547cSRui Paulo pw_id = peer->oob_pw_id;
2125b9c547cSRui Paulo if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) {
2135b9c547cSRui Paulo p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Request");
2145b9c547cSRui Paulo wpabuf_free(buf);
2155b9c547cSRui Paulo return NULL;
2165b9c547cSRui Paulo }
217f05cddf9SRui Paulo
218f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
219f05cddf9SRui Paulo if (p2p->wfd_ie_go_neg)
220f05cddf9SRui Paulo wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
221f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
222f05cddf9SRui Paulo
2235b9c547cSRui Paulo if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ])
2245b9c547cSRui Paulo wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]);
2255b9c547cSRui Paulo
226f05cddf9SRui Paulo return buf;
227f05cddf9SRui Paulo }
228f05cddf9SRui Paulo
229f05cddf9SRui Paulo
p2p_connect_send(struct p2p_data * p2p,struct p2p_device * dev)230f05cddf9SRui Paulo int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
231f05cddf9SRui Paulo {
232f05cddf9SRui Paulo struct wpabuf *req;
233f05cddf9SRui Paulo int freq;
234f05cddf9SRui Paulo
235f05cddf9SRui Paulo if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
236f05cddf9SRui Paulo u16 config_method;
2375b9c547cSRui Paulo p2p_dbg(p2p, "Use PD-before-GO-Neg workaround for " MACSTR,
238f05cddf9SRui Paulo MAC2STR(dev->info.p2p_device_addr));
239f05cddf9SRui Paulo if (dev->wps_method == WPS_PIN_DISPLAY)
240f05cddf9SRui Paulo config_method = WPS_CONFIG_KEYPAD;
241f05cddf9SRui Paulo else if (dev->wps_method == WPS_PIN_KEYPAD)
242f05cddf9SRui Paulo config_method = WPS_CONFIG_DISPLAY;
243f05cddf9SRui Paulo else if (dev->wps_method == WPS_PBC)
244f05cddf9SRui Paulo config_method = WPS_CONFIG_PUSHBUTTON;
2455b9c547cSRui Paulo else if (dev->wps_method == WPS_P2PS)
2465b9c547cSRui Paulo config_method = WPS_CONFIG_P2PS;
247f05cddf9SRui Paulo else
248f05cddf9SRui Paulo return -1;
249f05cddf9SRui Paulo return p2p_prov_disc_req(p2p, dev->info.p2p_device_addr,
2505b9c547cSRui Paulo NULL, config_method, 0, 0, 1);
251f05cddf9SRui Paulo }
252f05cddf9SRui Paulo
253f05cddf9SRui Paulo freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
2545b9c547cSRui Paulo if (dev->oob_go_neg_freq > 0)
2555b9c547cSRui Paulo freq = dev->oob_go_neg_freq;
256f05cddf9SRui Paulo if (freq <= 0) {
2575b9c547cSRui Paulo p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
2585b9c547cSRui Paulo MACSTR " to send GO Negotiation Request",
259f05cddf9SRui Paulo MAC2STR(dev->info.p2p_device_addr));
260f05cddf9SRui Paulo return -1;
261f05cddf9SRui Paulo }
262f05cddf9SRui Paulo
263f05cddf9SRui Paulo req = p2p_build_go_neg_req(p2p, dev);
264f05cddf9SRui Paulo if (req == NULL)
265f05cddf9SRui Paulo return -1;
2665b9c547cSRui Paulo p2p_dbg(p2p, "Sending GO Negotiation Request");
267f05cddf9SRui Paulo p2p_set_state(p2p, P2P_CONNECT);
268f05cddf9SRui Paulo p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
269f05cddf9SRui Paulo p2p->go_neg_peer = dev;
2705b9c547cSRui Paulo eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
271f05cddf9SRui Paulo dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
272f05cddf9SRui Paulo dev->connect_reqs++;
273f05cddf9SRui Paulo if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
274f05cddf9SRui Paulo p2p->cfg->dev_addr, dev->info.p2p_device_addr,
2755b9c547cSRui Paulo wpabuf_head(req), wpabuf_len(req), 500) < 0) {
2765b9c547cSRui Paulo p2p_dbg(p2p, "Failed to send Action frame");
277f05cddf9SRui Paulo /* Use P2P find to recover and retry */
278f05cddf9SRui Paulo p2p_set_timeout(p2p, 0, 0);
279f05cddf9SRui Paulo } else
280f05cddf9SRui Paulo dev->go_neg_req_sent++;
281f05cddf9SRui Paulo
282f05cddf9SRui Paulo wpabuf_free(req);
283f05cddf9SRui Paulo
284f05cddf9SRui Paulo return 0;
285f05cddf9SRui Paulo }
286f05cddf9SRui Paulo
287f05cddf9SRui Paulo
p2p_build_go_neg_resp(struct p2p_data * p2p,struct p2p_device * peer,u8 dialog_token,u8 status,u8 tie_breaker)288f05cddf9SRui Paulo static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
289f05cddf9SRui Paulo struct p2p_device *peer,
290f05cddf9SRui Paulo u8 dialog_token, u8 status,
291f05cddf9SRui Paulo u8 tie_breaker)
292f05cddf9SRui Paulo {
293f05cddf9SRui Paulo struct wpabuf *buf;
294f05cddf9SRui Paulo u8 *len;
295f05cddf9SRui Paulo u8 group_capab;
296f05cddf9SRui Paulo size_t extra = 0;
2975b9c547cSRui Paulo u16 pw_id;
298*a90b9d01SCy Schubert bool is_6ghz_capab;
299*a90b9d01SCy Schubert struct p2p_channels pref_chanlist;
300f05cddf9SRui Paulo
3015b9c547cSRui Paulo p2p_dbg(p2p, "Building GO Negotiation Response");
302f05cddf9SRui Paulo
303f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
304f05cddf9SRui Paulo if (p2p->wfd_ie_go_neg)
305f05cddf9SRui Paulo extra = wpabuf_len(p2p->wfd_ie_go_neg);
306f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
307f05cddf9SRui Paulo
3085b9c547cSRui Paulo if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP])
3095b9c547cSRui Paulo extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]);
3105b9c547cSRui Paulo
311f05cddf9SRui Paulo buf = wpabuf_alloc(1000 + extra);
312f05cddf9SRui Paulo if (buf == NULL)
313f05cddf9SRui Paulo return NULL;
314f05cddf9SRui Paulo
315f05cddf9SRui Paulo p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
316f05cddf9SRui Paulo
317f05cddf9SRui Paulo len = p2p_buf_add_ie_hdr(buf);
318f05cddf9SRui Paulo p2p_buf_add_status(buf, status);
319f05cddf9SRui Paulo group_capab = 0;
320f05cddf9SRui Paulo if (peer && peer->go_state == LOCAL_GO) {
321f05cddf9SRui Paulo if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
322f05cddf9SRui Paulo group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
323f05cddf9SRui Paulo if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
324f05cddf9SRui Paulo group_capab |=
325f05cddf9SRui Paulo P2P_GROUP_CAPAB_PERSISTENT_RECONN;
326f05cddf9SRui Paulo }
327f05cddf9SRui Paulo if (p2p->cross_connect)
328f05cddf9SRui Paulo group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
329f05cddf9SRui Paulo if (p2p->cfg->p2p_intra_bss)
330f05cddf9SRui Paulo group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
331f05cddf9SRui Paulo }
332f05cddf9SRui Paulo p2p_buf_add_capability(buf, p2p->dev_capab &
333f05cddf9SRui Paulo ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
334f05cddf9SRui Paulo group_capab);
335f05cddf9SRui Paulo p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
336f05cddf9SRui Paulo p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
33785732ac8SCy Schubert if (p2p->override_pref_op_class) {
33885732ac8SCy Schubert p2p_dbg(p2p, "Override operating channel preference");
33985732ac8SCy Schubert p2p_buf_add_operating_channel(buf, p2p->cfg->country,
34085732ac8SCy Schubert p2p->override_pref_op_class,
34185732ac8SCy Schubert p2p->override_pref_channel);
34285732ac8SCy Schubert } else if (peer && peer->go_state == REMOTE_GO && !p2p->num_pref_freq) {
3435b9c547cSRui Paulo p2p_dbg(p2p, "Omit Operating Channel attribute");
344f05cddf9SRui Paulo } else {
345f05cddf9SRui Paulo p2p_buf_add_operating_channel(buf, p2p->cfg->country,
346f05cddf9SRui Paulo p2p->op_reg_class,
347f05cddf9SRui Paulo p2p->op_channel);
348f05cddf9SRui Paulo }
349f05cddf9SRui Paulo p2p_buf_add_intended_addr(buf, p2p->intended_addr);
350*a90b9d01SCy Schubert if (p2p->num_pref_freq) {
351*a90b9d01SCy Schubert bool go = (peer && peer->go_state == LOCAL_GO) ||
352*a90b9d01SCy Schubert p2p->go_intent == 15;
353*a90b9d01SCy Schubert
354*a90b9d01SCy Schubert p2p_pref_channel_filter(&p2p->channels, p2p->pref_freq_list,
355*a90b9d01SCy Schubert p2p->num_pref_freq, &pref_chanlist, go);
356*a90b9d01SCy Schubert p2p_channels_dump(p2p, "channel list after filtering",
357*a90b9d01SCy Schubert &pref_chanlist);
358*a90b9d01SCy Schubert } else {
359*a90b9d01SCy Schubert p2p_copy_channels(&pref_chanlist, &p2p->channels,
360*a90b9d01SCy Schubert p2p->allow_6ghz);
361*a90b9d01SCy Schubert }
362f05cddf9SRui Paulo if (status || peer == NULL) {
363f05cddf9SRui Paulo p2p_buf_add_channel_list(buf, p2p->cfg->country,
364*a90b9d01SCy Schubert &pref_chanlist, false);
365f05cddf9SRui Paulo } else if (peer->go_state == REMOTE_GO) {
366*a90b9d01SCy Schubert is_6ghz_capab = is_p2p_6ghz_capable(p2p) &&
367*a90b9d01SCy Schubert p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr);
368f05cddf9SRui Paulo p2p_buf_add_channel_list(buf, p2p->cfg->country,
369*a90b9d01SCy Schubert &pref_chanlist, is_6ghz_capab);
370f05cddf9SRui Paulo } else {
371f05cddf9SRui Paulo struct p2p_channels res;
372*a90b9d01SCy Schubert
373*a90b9d01SCy Schubert is_6ghz_capab = is_p2p_6ghz_capable(p2p) &&
374*a90b9d01SCy Schubert p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr);
375*a90b9d01SCy Schubert p2p_channels_intersect(&pref_chanlist, &peer->channels,
376f05cddf9SRui Paulo &res);
377*a90b9d01SCy Schubert p2p_buf_add_channel_list(buf, p2p->cfg->country, &res,
378*a90b9d01SCy Schubert is_6ghz_capab);
379f05cddf9SRui Paulo }
380f05cddf9SRui Paulo p2p_buf_add_device_info(buf, p2p, peer);
381f05cddf9SRui Paulo if (peer && peer->go_state == LOCAL_GO) {
382f05cddf9SRui Paulo p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
383f05cddf9SRui Paulo p2p->ssid_len);
384f05cddf9SRui Paulo }
385f05cddf9SRui Paulo p2p_buf_update_ie_hdr(buf, len);
386f05cddf9SRui Paulo
387f05cddf9SRui Paulo /* WPS IE with Device Password ID attribute */
3885b9c547cSRui Paulo pw_id = p2p_wps_method_pw_id(peer ? peer->wps_method : WPS_NOT_READY);
3895b9c547cSRui Paulo if (peer && peer->oob_pw_id)
3905b9c547cSRui Paulo pw_id = peer->oob_pw_id;
3915b9c547cSRui Paulo if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) {
3925b9c547cSRui Paulo p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Response");
3935b9c547cSRui Paulo wpabuf_free(buf);
3945b9c547cSRui Paulo return NULL;
3955b9c547cSRui Paulo }
396f05cddf9SRui Paulo
397f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
398f05cddf9SRui Paulo if (p2p->wfd_ie_go_neg)
399f05cddf9SRui Paulo wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
400f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
401f05cddf9SRui Paulo
4025b9c547cSRui Paulo if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP])
4035b9c547cSRui Paulo wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]);
404f05cddf9SRui Paulo
405f05cddf9SRui Paulo return buf;
406f05cddf9SRui Paulo }
407f05cddf9SRui Paulo
408f05cddf9SRui Paulo
409f05cddf9SRui Paulo /**
410f05cddf9SRui Paulo * p2p_reselect_channel - Re-select operating channel based on peer information
411f05cddf9SRui Paulo * @p2p: P2P module context from p2p_init()
412f05cddf9SRui Paulo * @intersection: Support channel list intersection from local and peer
413f05cddf9SRui Paulo *
414f05cddf9SRui Paulo * This function is used to re-select the best channel after having received
415f05cddf9SRui Paulo * information from the peer to allow supported channel lists to be intersected.
416f05cddf9SRui Paulo * This can be used to improve initial channel selection done in
417f05cddf9SRui Paulo * p2p_prepare_channel() prior to the start of GO Negotiation. In addition, this
418f05cddf9SRui Paulo * can be used for Invitation case.
419f05cddf9SRui Paulo */
p2p_reselect_channel(struct p2p_data * p2p,struct p2p_channels * intersection)420f05cddf9SRui Paulo void p2p_reselect_channel(struct p2p_data *p2p,
421f05cddf9SRui Paulo struct p2p_channels *intersection)
422f05cddf9SRui Paulo {
423f05cddf9SRui Paulo struct p2p_reg_class *cl;
424f05cddf9SRui Paulo int freq;
425f05cddf9SRui Paulo u8 op_reg_class, op_channel;
426f05cddf9SRui Paulo unsigned int i;
427325151a3SRui Paulo const int op_classes_5ghz[] = { 124, 125, 115, 0 };
4285b9c547cSRui Paulo const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
429780fb4a2SCy Schubert const int op_classes_vht[] = { 128, 129, 130, 0 };
430c1d255d3SCy Schubert const int op_classes_edmg[] = { 181, 182, 183, 0 };
431f05cddf9SRui Paulo
4325b9c547cSRui Paulo if (p2p->own_freq_preference > 0 &&
4335b9c547cSRui Paulo p2p_freq_to_channel(p2p->own_freq_preference,
434f05cddf9SRui Paulo &op_reg_class, &op_channel) == 0 &&
435f05cddf9SRui Paulo p2p_channels_includes(intersection, op_reg_class, op_channel)) {
4365b9c547cSRui Paulo p2p_dbg(p2p, "Pick own channel preference (reg_class %u channel %u) from intersection",
4375b9c547cSRui Paulo op_reg_class, op_channel);
4385b9c547cSRui Paulo p2p->op_reg_class = op_reg_class;
4395b9c547cSRui Paulo p2p->op_channel = op_channel;
4405b9c547cSRui Paulo return;
4415b9c547cSRui Paulo }
4425b9c547cSRui Paulo
4435b9c547cSRui Paulo if (p2p->best_freq_overall > 0 &&
4445b9c547cSRui Paulo p2p_freq_to_channel(p2p->best_freq_overall,
4455b9c547cSRui Paulo &op_reg_class, &op_channel) == 0 &&
4465b9c547cSRui Paulo p2p_channels_includes(intersection, op_reg_class, op_channel)) {
4475b9c547cSRui Paulo p2p_dbg(p2p, "Pick best overall channel (reg_class %u channel %u) from intersection",
4485b9c547cSRui Paulo op_reg_class, op_channel);
4495b9c547cSRui Paulo p2p->op_reg_class = op_reg_class;
4505b9c547cSRui Paulo p2p->op_channel = op_channel;
4515b9c547cSRui Paulo return;
4525b9c547cSRui Paulo }
4535b9c547cSRui Paulo
4545b9c547cSRui Paulo /* First, try to pick the best channel from another band */
4555b9c547cSRui Paulo freq = p2p_channel_to_freq(p2p->op_reg_class, p2p->op_channel);
4565b9c547cSRui Paulo if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
4575b9c547cSRui Paulo !p2p_channels_includes(intersection, p2p->op_reg_class,
4585b9c547cSRui Paulo p2p->op_channel) &&
4595b9c547cSRui Paulo p2p_freq_to_channel(p2p->best_freq_5,
4605b9c547cSRui Paulo &op_reg_class, &op_channel) == 0 &&
4615b9c547cSRui Paulo p2p_channels_includes(intersection, op_reg_class, op_channel)) {
4625b9c547cSRui Paulo p2p_dbg(p2p, "Pick best 5 GHz channel (reg_class %u channel %u) from intersection",
463f05cddf9SRui Paulo op_reg_class, op_channel);
464f05cddf9SRui Paulo p2p->op_reg_class = op_reg_class;
465f05cddf9SRui Paulo p2p->op_channel = op_channel;
466f05cddf9SRui Paulo return;
467f05cddf9SRui Paulo }
468f05cddf9SRui Paulo
469f05cddf9SRui Paulo if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
4705b9c547cSRui Paulo !p2p_channels_includes(intersection, p2p->op_reg_class,
4715b9c547cSRui Paulo p2p->op_channel) &&
4725b9c547cSRui Paulo p2p_freq_to_channel(p2p->best_freq_24,
473f05cddf9SRui Paulo &op_reg_class, &op_channel) == 0 &&
474f05cddf9SRui Paulo p2p_channels_includes(intersection, op_reg_class, op_channel)) {
4755b9c547cSRui Paulo p2p_dbg(p2p, "Pick best 2.4 GHz channel (reg_class %u channel %u) from intersection",
476f05cddf9SRui Paulo op_reg_class, op_channel);
477f05cddf9SRui Paulo p2p->op_reg_class = op_reg_class;
478f05cddf9SRui Paulo p2p->op_channel = op_channel;
479f05cddf9SRui Paulo return;
480f05cddf9SRui Paulo }
481f05cddf9SRui Paulo
482f05cddf9SRui Paulo /* Select channel with highest preference if the peer supports it */
483f05cddf9SRui Paulo for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
484f05cddf9SRui Paulo if (p2p_channels_includes(intersection,
485f05cddf9SRui Paulo p2p->cfg->pref_chan[i].op_class,
486f05cddf9SRui Paulo p2p->cfg->pref_chan[i].chan)) {
487f05cddf9SRui Paulo p2p->op_reg_class = p2p->cfg->pref_chan[i].op_class;
488f05cddf9SRui Paulo p2p->op_channel = p2p->cfg->pref_chan[i].chan;
4895b9c547cSRui Paulo p2p_dbg(p2p, "Pick highest preferred channel (op_class %u channel %u) from intersection",
490f05cddf9SRui Paulo p2p->op_reg_class, p2p->op_channel);
491f05cddf9SRui Paulo return;
492f05cddf9SRui Paulo }
493f05cddf9SRui Paulo }
494f05cddf9SRui Paulo
495c1d255d3SCy Schubert /* Try a channel where we might be able to use EDMG */
496c1d255d3SCy Schubert if (p2p_channel_select(intersection, op_classes_edmg,
497c1d255d3SCy Schubert &p2p->op_reg_class, &p2p->op_channel) == 0) {
498c1d255d3SCy Schubert p2p_dbg(p2p, "Pick possible EDMG channel (op_class %u channel %u) from intersection",
499c1d255d3SCy Schubert p2p->op_reg_class, p2p->op_channel);
500c1d255d3SCy Schubert return;
501c1d255d3SCy Schubert }
502c1d255d3SCy Schubert
5035b9c547cSRui Paulo /* Try a channel where we might be able to use VHT */
5045b9c547cSRui Paulo if (p2p_channel_select(intersection, op_classes_vht,
5055b9c547cSRui Paulo &p2p->op_reg_class, &p2p->op_channel) == 0) {
5065b9c547cSRui Paulo p2p_dbg(p2p, "Pick possible VHT channel (op_class %u channel %u) from intersection",
5075b9c547cSRui Paulo p2p->op_reg_class, p2p->op_channel);
508f05cddf9SRui Paulo return;
509f05cddf9SRui Paulo }
5105b9c547cSRui Paulo
5115b9c547cSRui Paulo /* Try a channel where we might be able to use HT40 */
5125b9c547cSRui Paulo if (p2p_channel_select(intersection, op_classes_ht40,
5135b9c547cSRui Paulo &p2p->op_reg_class, &p2p->op_channel) == 0) {
5145b9c547cSRui Paulo p2p_dbg(p2p, "Pick possible HT40 channel (op_class %u channel %u) from intersection",
5155b9c547cSRui Paulo p2p->op_reg_class, p2p->op_channel);
5165b9c547cSRui Paulo return;
5175b9c547cSRui Paulo }
5185b9c547cSRui Paulo
5195b9c547cSRui Paulo /* Prefer a 5 GHz channel */
5205b9c547cSRui Paulo if (p2p_channel_select(intersection, op_classes_5ghz,
5215b9c547cSRui Paulo &p2p->op_reg_class, &p2p->op_channel) == 0) {
5225b9c547cSRui Paulo p2p_dbg(p2p, "Pick possible 5 GHz channel (op_class %u channel %u) from intersection",
5235b9c547cSRui Paulo p2p->op_reg_class, p2p->op_channel);
5245b9c547cSRui Paulo return;
525f05cddf9SRui Paulo }
526f05cddf9SRui Paulo
527f05cddf9SRui Paulo /*
528f05cddf9SRui Paulo * Try to see if the original channel is in the intersection. If
529f05cddf9SRui Paulo * so, no need to change anything, as it already contains some
530f05cddf9SRui Paulo * randomness.
531f05cddf9SRui Paulo */
532f05cddf9SRui Paulo if (p2p_channels_includes(intersection, p2p->op_reg_class,
533f05cddf9SRui Paulo p2p->op_channel)) {
5345b9c547cSRui Paulo p2p_dbg(p2p, "Using original operating class and channel (op_class %u channel %u) from intersection",
535f05cddf9SRui Paulo p2p->op_reg_class, p2p->op_channel);
536f05cddf9SRui Paulo return;
537f05cddf9SRui Paulo }
538f05cddf9SRui Paulo
539f05cddf9SRui Paulo /*
540f05cddf9SRui Paulo * Fall back to whatever is included in the channel intersection since
541f05cddf9SRui Paulo * no better options seems to be available.
542f05cddf9SRui Paulo */
543f05cddf9SRui Paulo cl = &intersection->reg_class[0];
5445b9c547cSRui Paulo p2p_dbg(p2p, "Pick another channel (reg_class %u channel %u) from intersection",
545f05cddf9SRui Paulo cl->reg_class, cl->channel[0]);
546f05cddf9SRui Paulo p2p->op_reg_class = cl->reg_class;
547f05cddf9SRui Paulo p2p->op_channel = cl->channel[0];
548f05cddf9SRui Paulo }
549f05cddf9SRui Paulo
550f05cddf9SRui Paulo
p2p_go_select_channel(struct p2p_data * p2p,struct p2p_device * dev,u8 * status)5515b9c547cSRui Paulo int p2p_go_select_channel(struct p2p_data *p2p, struct p2p_device *dev,
552f05cddf9SRui Paulo u8 *status)
553f05cddf9SRui Paulo {
5545b9c547cSRui Paulo struct p2p_channels tmp, intersection;
555f05cddf9SRui Paulo
5565b9c547cSRui Paulo p2p_channels_dump(p2p, "own channels", &p2p->channels);
5575b9c547cSRui Paulo p2p_channels_dump(p2p, "peer channels", &dev->channels);
5585b9c547cSRui Paulo p2p_channels_intersect(&p2p->channels, &dev->channels, &tmp);
5595b9c547cSRui Paulo p2p_channels_dump(p2p, "intersection", &tmp);
5605b9c547cSRui Paulo p2p_channels_remove_freqs(&tmp, &p2p->no_go_freq);
5615b9c547cSRui Paulo p2p_channels_dump(p2p, "intersection after no-GO removal", &tmp);
5625b9c547cSRui Paulo p2p_channels_intersect(&tmp, &p2p->cfg->channels, &intersection);
5635b9c547cSRui Paulo p2p_channels_dump(p2p, "intersection with local channel list",
5645b9c547cSRui Paulo &intersection);
565f05cddf9SRui Paulo if (intersection.reg_classes == 0 ||
566f05cddf9SRui Paulo intersection.reg_class[0].channels == 0) {
567f05cddf9SRui Paulo *status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
5685b9c547cSRui Paulo p2p_dbg(p2p, "No common channels found");
569f05cddf9SRui Paulo return -1;
570f05cddf9SRui Paulo }
571f05cddf9SRui Paulo
572f05cddf9SRui Paulo if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
573f05cddf9SRui Paulo p2p->op_channel)) {
574f05cddf9SRui Paulo if (dev->flags & P2P_DEV_FORCE_FREQ) {
575f05cddf9SRui Paulo *status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
5765b9c547cSRui Paulo p2p_dbg(p2p, "Peer does not support the forced channel");
577f05cddf9SRui Paulo return -1;
578f05cddf9SRui Paulo }
579f05cddf9SRui Paulo
5805b9c547cSRui Paulo p2p_dbg(p2p, "Selected operating channel (op_class %u channel %u) not acceptable to the peer",
5815b9c547cSRui Paulo p2p->op_reg_class, p2p->op_channel);
582f05cddf9SRui Paulo p2p_reselect_channel(p2p, &intersection);
583f05cddf9SRui Paulo } else if (!(dev->flags & P2P_DEV_FORCE_FREQ) &&
584f05cddf9SRui Paulo !p2p->cfg->cfg_op_channel) {
5855b9c547cSRui Paulo p2p_dbg(p2p, "Try to optimize channel selection with peer information received; previously selected op_class %u channel %u",
586f05cddf9SRui Paulo p2p->op_reg_class, p2p->op_channel);
587f05cddf9SRui Paulo p2p_reselect_channel(p2p, &intersection);
588f05cddf9SRui Paulo }
589f05cddf9SRui Paulo
590f05cddf9SRui Paulo if (!p2p->ssid_set) {
591f05cddf9SRui Paulo p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
592f05cddf9SRui Paulo p2p->ssid_set = 1;
593f05cddf9SRui Paulo }
594f05cddf9SRui Paulo
595f05cddf9SRui Paulo return 0;
596f05cddf9SRui Paulo }
597f05cddf9SRui Paulo
598f05cddf9SRui Paulo
p2p_check_pref_chan_no_recv(struct p2p_data * p2p,int go,struct p2p_device * dev,struct p2p_message * msg,const struct weighted_pcl freq_list[],unsigned int size)599325151a3SRui Paulo static void p2p_check_pref_chan_no_recv(struct p2p_data *p2p, int go,
600325151a3SRui Paulo struct p2p_device *dev,
601325151a3SRui Paulo struct p2p_message *msg,
602*a90b9d01SCy Schubert const struct weighted_pcl freq_list[],
603*a90b9d01SCy Schubert unsigned int size)
604325151a3SRui Paulo {
605325151a3SRui Paulo u8 op_class, op_channel;
606325151a3SRui Paulo unsigned int oper_freq = 0, i, j;
607325151a3SRui Paulo int found = 0;
608325151a3SRui Paulo
609325151a3SRui Paulo p2p_dbg(p2p,
610325151a3SRui Paulo "Peer didn't provide a preferred frequency list, see if any of our preferred channels are supported by peer device");
611325151a3SRui Paulo
612325151a3SRui Paulo /*
613325151a3SRui Paulo * Search for a common channel in our preferred frequency list which is
614325151a3SRui Paulo * also supported by the peer device.
615325151a3SRui Paulo */
616325151a3SRui Paulo for (i = 0; i < size && !found; i++) {
61785732ac8SCy Schubert /* Make sure that the common frequency is supported by peer. */
618*a90b9d01SCy Schubert oper_freq = freq_list[i].freq;
619325151a3SRui Paulo if (p2p_freq_to_channel(oper_freq, &op_class,
620*a90b9d01SCy Schubert &op_channel) < 0 ||
621*a90b9d01SCy Schubert !p2p_pref_freq_allowed(&freq_list[i], go))
622*a90b9d01SCy Schubert continue;
623325151a3SRui Paulo for (j = 0; j < msg->channel_list_len; j++) {
6244b72b91aSCy Schubert if (!msg->channel_list ||
6254b72b91aSCy Schubert op_channel != msg->channel_list[j])
626325151a3SRui Paulo continue;
627325151a3SRui Paulo
628325151a3SRui Paulo p2p->op_reg_class = op_class;
629325151a3SRui Paulo p2p->op_channel = op_channel;
630325151a3SRui Paulo os_memcpy(&p2p->channels, &p2p->cfg->channels,
631325151a3SRui Paulo sizeof(struct p2p_channels));
632325151a3SRui Paulo found = 1;
633325151a3SRui Paulo break;
634325151a3SRui Paulo }
635325151a3SRui Paulo }
636325151a3SRui Paulo
637325151a3SRui Paulo if (found) {
638325151a3SRui Paulo p2p_dbg(p2p,
639325151a3SRui Paulo "Freq %d MHz is a preferred channel and is also supported by peer, use it as the operating channel",
640325151a3SRui Paulo oper_freq);
641325151a3SRui Paulo } else {
642325151a3SRui Paulo p2p_dbg(p2p,
64385732ac8SCy Schubert "None of our preferred channels are supported by peer!");
644325151a3SRui Paulo }
645325151a3SRui Paulo }
646325151a3SRui Paulo
647325151a3SRui Paulo
p2p_check_pref_chan_recv(struct p2p_data * p2p,int go,struct p2p_device * dev,struct p2p_message * msg,const struct weighted_pcl freq_list[],unsigned int size)648325151a3SRui Paulo static void p2p_check_pref_chan_recv(struct p2p_data *p2p, int go,
649325151a3SRui Paulo struct p2p_device *dev,
650325151a3SRui Paulo struct p2p_message *msg,
651*a90b9d01SCy Schubert const struct weighted_pcl freq_list[],
652*a90b9d01SCy Schubert unsigned int size)
653325151a3SRui Paulo {
654325151a3SRui Paulo u8 op_class, op_channel;
655325151a3SRui Paulo unsigned int oper_freq = 0, i, j;
656325151a3SRui Paulo int found = 0;
657325151a3SRui Paulo
658325151a3SRui Paulo /*
659325151a3SRui Paulo * Peer device supports a Preferred Frequency List.
660325151a3SRui Paulo * Search for a common channel in the preferred frequency lists
661325151a3SRui Paulo * of both peer and local devices.
662325151a3SRui Paulo */
663325151a3SRui Paulo for (i = 0; i < size && !found; i++) {
664325151a3SRui Paulo for (j = 2; j < (msg->pref_freq_list_len / 2); j++) {
665325151a3SRui Paulo oper_freq = p2p_channel_to_freq(
666325151a3SRui Paulo msg->pref_freq_list[2 * j],
667325151a3SRui Paulo msg->pref_freq_list[2 * j + 1]);
668*a90b9d01SCy Schubert if (freq_list[i].freq != oper_freq)
669325151a3SRui Paulo continue;
670325151a3SRui Paulo if (p2p_freq_to_channel(oper_freq, &op_class,
67185732ac8SCy Schubert &op_channel) < 0)
67285732ac8SCy Schubert continue; /* cannot happen */
673*a90b9d01SCy Schubert if (!p2p_pref_freq_allowed(&freq_list[i], go))
674*a90b9d01SCy Schubert break;
675325151a3SRui Paulo p2p->op_reg_class = op_class;
676325151a3SRui Paulo p2p->op_channel = op_channel;
677325151a3SRui Paulo os_memcpy(&p2p->channels, &p2p->cfg->channels,
678325151a3SRui Paulo sizeof(struct p2p_channels));
679325151a3SRui Paulo found = 1;
680325151a3SRui Paulo break;
681325151a3SRui Paulo }
682325151a3SRui Paulo }
683325151a3SRui Paulo
684325151a3SRui Paulo if (found) {
685325151a3SRui Paulo p2p_dbg(p2p,
686325151a3SRui Paulo "Freq %d MHz is a common preferred channel for both peer and local, use it as operating channel",
687325151a3SRui Paulo oper_freq);
688325151a3SRui Paulo } else {
68985732ac8SCy Schubert p2p_dbg(p2p, "No common preferred channels found!");
690325151a3SRui Paulo }
691325151a3SRui Paulo }
692325151a3SRui Paulo
693325151a3SRui Paulo
p2p_check_pref_chan(struct p2p_data * p2p,int go,struct p2p_device * dev,struct p2p_message * msg)694325151a3SRui Paulo void p2p_check_pref_chan(struct p2p_data *p2p, int go,
695325151a3SRui Paulo struct p2p_device *dev, struct p2p_message *msg)
696325151a3SRui Paulo {
697*a90b9d01SCy Schubert unsigned int size;
698325151a3SRui Paulo unsigned int i;
699325151a3SRui Paulo u8 op_class, op_channel;
70085732ac8SCy Schubert char txt[100], *pos, *end;
701*a90b9d01SCy Schubert bool is_6ghz_capab;
70285732ac8SCy Schubert int res;
703325151a3SRui Paulo
704325151a3SRui Paulo /*
705325151a3SRui Paulo * Use the preferred channel list from the driver only if there is no
706325151a3SRui Paulo * forced_freq, e.g., P2P_CONNECT freq=..., and no preferred operating
707325151a3SRui Paulo * channel hardcoded in the configuration file.
708325151a3SRui Paulo */
709325151a3SRui Paulo if (!p2p->cfg->get_pref_freq_list || p2p->cfg->num_pref_chan ||
710325151a3SRui Paulo (dev->flags & P2P_DEV_FORCE_FREQ) || p2p->cfg->cfg_op_channel)
711325151a3SRui Paulo return;
712325151a3SRui Paulo
713325151a3SRui Paulo /* Obtain our preferred frequency list from driver based on P2P role. */
714325151a3SRui Paulo size = P2P_MAX_PREF_CHANNELS;
715*a90b9d01SCy Schubert if (p2p->cfg->get_pref_freq_list(p2p->cfg->cb_ctx, go,
716*a90b9d01SCy Schubert &p2p->num_pref_freq,
717*a90b9d01SCy Schubert p2p->pref_freq_list))
718*a90b9d01SCy Schubert return;
719*a90b9d01SCy Schubert size = p2p->num_pref_freq;
720*a90b9d01SCy Schubert if (!size)
721325151a3SRui Paulo return;
72285732ac8SCy Schubert /* Filter out frequencies that are not acceptable for P2P use */
723*a90b9d01SCy Schubert is_6ghz_capab = is_p2p_6ghz_capable(p2p) &&
724*a90b9d01SCy Schubert p2p_is_peer_6ghz_capab(p2p, dev->info.p2p_device_addr);
72585732ac8SCy Schubert i = 0;
72685732ac8SCy Schubert while (i < size) {
727*a90b9d01SCy Schubert if (p2p_freq_to_channel(p2p->pref_freq_list[i].freq,
728*a90b9d01SCy Schubert &op_class, &op_channel) < 0 ||
72985732ac8SCy Schubert (!p2p_channels_includes(&p2p->cfg->channels,
73085732ac8SCy Schubert op_class, op_channel) &&
73185732ac8SCy Schubert (go || !p2p_channels_includes(&p2p->cfg->cli_channels,
732*a90b9d01SCy Schubert op_class, op_channel))) ||
733*a90b9d01SCy Schubert (is_6ghz_freq(p2p->pref_freq_list[i].freq) &&
734*a90b9d01SCy Schubert !is_6ghz_capab)) {
73585732ac8SCy Schubert p2p_dbg(p2p,
73685732ac8SCy Schubert "Ignore local driver frequency preference %u MHz since it is not acceptable for P2P use (go=%d)",
737*a90b9d01SCy Schubert p2p->pref_freq_list[i].freq, go);
73885732ac8SCy Schubert if (size - i - 1 > 0)
739*a90b9d01SCy Schubert os_memmove(&p2p->pref_freq_list[i],
740*a90b9d01SCy Schubert &p2p->pref_freq_list[i + 1],
741206b73d0SCy Schubert (size - i - 1) *
742*a90b9d01SCy Schubert sizeof(struct weighted_pcl));
74385732ac8SCy Schubert size--;
74485732ac8SCy Schubert continue;
74585732ac8SCy Schubert }
74685732ac8SCy Schubert
74785732ac8SCy Schubert /* Preferred frequency is acceptable for P2P use */
74885732ac8SCy Schubert i++;
74985732ac8SCy Schubert }
75085732ac8SCy Schubert
75185732ac8SCy Schubert pos = txt;
75285732ac8SCy Schubert end = pos + sizeof(txt);
75385732ac8SCy Schubert for (i = 0; i < size; i++) {
754*a90b9d01SCy Schubert res = os_snprintf(pos, end - pos, " %u",
755*a90b9d01SCy Schubert p2p->pref_freq_list[i].freq);
75685732ac8SCy Schubert if (os_snprintf_error(end - pos, res))
75785732ac8SCy Schubert break;
75885732ac8SCy Schubert pos += res;
75985732ac8SCy Schubert }
76085732ac8SCy Schubert *pos = '\0';
76185732ac8SCy Schubert p2p_dbg(p2p, "Local driver frequency preference (size=%u):%s",
76285732ac8SCy Schubert size, txt);
763325151a3SRui Paulo
764325151a3SRui Paulo /*
765325151a3SRui Paulo * Check if peer's preference of operating channel is in
766325151a3SRui Paulo * our preferred channel list.
767325151a3SRui Paulo */
768325151a3SRui Paulo for (i = 0; i < size; i++) {
769*a90b9d01SCy Schubert if (p2p->pref_freq_list[i].freq ==
770*a90b9d01SCy Schubert (unsigned int) dev->oper_freq &&
771*a90b9d01SCy Schubert p2p_pref_freq_allowed(&p2p->pref_freq_list[i], go))
772325151a3SRui Paulo break;
773325151a3SRui Paulo }
77485732ac8SCy Schubert if (i != size &&
775*a90b9d01SCy Schubert p2p_freq_to_channel(p2p->pref_freq_list[i].freq, &op_class,
776*a90b9d01SCy Schubert &op_channel) == 0) {
777325151a3SRui Paulo /* Peer operating channel preference matches our preference */
778325151a3SRui Paulo p2p->op_reg_class = op_class;
779325151a3SRui Paulo p2p->op_channel = op_channel;
780325151a3SRui Paulo os_memcpy(&p2p->channels, &p2p->cfg->channels,
781325151a3SRui Paulo sizeof(struct p2p_channels));
782325151a3SRui Paulo return;
783325151a3SRui Paulo }
784325151a3SRui Paulo
785325151a3SRui Paulo p2p_dbg(p2p,
786325151a3SRui Paulo "Peer operating channel preference: %d MHz is not in our preferred channel list",
787325151a3SRui Paulo dev->oper_freq);
788325151a3SRui Paulo
789325151a3SRui Paulo /*
790325151a3SRui Paulo Check if peer's preferred channel list is
791325151a3SRui Paulo * _not_ included in the GO Negotiation Request or Invitation Request.
792325151a3SRui Paulo */
793325151a3SRui Paulo if (msg->pref_freq_list_len == 0)
794*a90b9d01SCy Schubert p2p_check_pref_chan_no_recv(p2p, go, dev, msg,
795*a90b9d01SCy Schubert p2p->pref_freq_list, size);
796325151a3SRui Paulo else
797*a90b9d01SCy Schubert p2p_check_pref_chan_recv(p2p, go, dev, msg,
798*a90b9d01SCy Schubert p2p->pref_freq_list, size);
799325151a3SRui Paulo }
800325151a3SRui Paulo
801325151a3SRui Paulo
p2p_process_go_neg_req(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len,int rx_freq)802f05cddf9SRui Paulo void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
803f05cddf9SRui Paulo const u8 *data, size_t len, int rx_freq)
804f05cddf9SRui Paulo {
805f05cddf9SRui Paulo struct p2p_device *dev = NULL;
806f05cddf9SRui Paulo struct wpabuf *resp;
807f05cddf9SRui Paulo struct p2p_message msg;
808f05cddf9SRui Paulo u8 status = P2P_SC_FAIL_INVALID_PARAMS;
809f05cddf9SRui Paulo int tie_breaker = 0;
810f05cddf9SRui Paulo int freq;
811f05cddf9SRui Paulo
8125b9c547cSRui Paulo p2p_dbg(p2p, "Received GO Negotiation Request from " MACSTR "(freq=%d)",
8135b9c547cSRui Paulo MAC2STR(sa), rx_freq);
814f05cddf9SRui Paulo
815f05cddf9SRui Paulo if (p2p_parse(data, len, &msg))
816f05cddf9SRui Paulo return;
817f05cddf9SRui Paulo
818f05cddf9SRui Paulo if (!msg.capability) {
8195b9c547cSRui Paulo p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Request");
820f05cddf9SRui Paulo #ifdef CONFIG_P2P_STRICT
821f05cddf9SRui Paulo goto fail;
822f05cddf9SRui Paulo #endif /* CONFIG_P2P_STRICT */
823f05cddf9SRui Paulo }
824f05cddf9SRui Paulo
825f05cddf9SRui Paulo if (msg.go_intent)
826f05cddf9SRui Paulo tie_breaker = *msg.go_intent & 0x01;
827f05cddf9SRui Paulo else {
8285b9c547cSRui Paulo p2p_dbg(p2p, "Mandatory GO Intent attribute missing from GO Negotiation Request");
829f05cddf9SRui Paulo #ifdef CONFIG_P2P_STRICT
830f05cddf9SRui Paulo goto fail;
831f05cddf9SRui Paulo #endif /* CONFIG_P2P_STRICT */
832f05cddf9SRui Paulo }
833f05cddf9SRui Paulo
834f05cddf9SRui Paulo if (!msg.config_timeout) {
8355b9c547cSRui Paulo p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Request");
836f05cddf9SRui Paulo #ifdef CONFIG_P2P_STRICT
837f05cddf9SRui Paulo goto fail;
838f05cddf9SRui Paulo #endif /* CONFIG_P2P_STRICT */
839f05cddf9SRui Paulo }
840f05cddf9SRui Paulo
841f05cddf9SRui Paulo if (!msg.listen_channel) {
8425b9c547cSRui Paulo p2p_dbg(p2p, "No Listen Channel attribute received");
843f05cddf9SRui Paulo goto fail;
844f05cddf9SRui Paulo }
845f05cddf9SRui Paulo if (!msg.operating_channel) {
8465b9c547cSRui Paulo p2p_dbg(p2p, "No Operating Channel attribute received");
847f05cddf9SRui Paulo goto fail;
848f05cddf9SRui Paulo }
849f05cddf9SRui Paulo if (!msg.channel_list) {
8505b9c547cSRui Paulo p2p_dbg(p2p, "No Channel List attribute received");
851f05cddf9SRui Paulo goto fail;
852f05cddf9SRui Paulo }
853f05cddf9SRui Paulo if (!msg.intended_addr) {
8545b9c547cSRui Paulo p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
855f05cddf9SRui Paulo goto fail;
856f05cddf9SRui Paulo }
857f05cddf9SRui Paulo if (!msg.p2p_device_info) {
8585b9c547cSRui Paulo p2p_dbg(p2p, "No P2P Device Info attribute received");
859f05cddf9SRui Paulo goto fail;
860f05cddf9SRui Paulo }
861f05cddf9SRui Paulo
862*a90b9d01SCy Schubert if (!ether_addr_equal(msg.p2p_device_addr, sa)) {
8635b9c547cSRui Paulo p2p_dbg(p2p, "Unexpected GO Negotiation Request SA=" MACSTR
864f05cddf9SRui Paulo " != dev_addr=" MACSTR,
865f05cddf9SRui Paulo MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
866f05cddf9SRui Paulo goto fail;
867f05cddf9SRui Paulo }
868f05cddf9SRui Paulo
869f05cddf9SRui Paulo dev = p2p_get_device(p2p, sa);
870f05cddf9SRui Paulo
871f05cddf9SRui Paulo if (msg.status && *msg.status) {
8725b9c547cSRui Paulo p2p_dbg(p2p, "Unexpected Status attribute (%d) in GO Negotiation Request",
8735b9c547cSRui Paulo *msg.status);
8745b9c547cSRui Paulo if (dev && p2p->go_neg_peer == dev &&
8755b9c547cSRui Paulo *msg.status == P2P_SC_FAIL_REJECTED_BY_USER) {
8765b9c547cSRui Paulo /*
8775b9c547cSRui Paulo * This mechanism for using Status attribute in GO
8785b9c547cSRui Paulo * Negotiation Request is not compliant with the P2P
8795b9c547cSRui Paulo * specification, but some deployed devices use it to
8805b9c547cSRui Paulo * indicate rejection of GO Negotiation in a case where
8815b9c547cSRui Paulo * they have sent out GO Negotiation Response with
8825b9c547cSRui Paulo * status 1. The P2P specification explicitly disallows
8835b9c547cSRui Paulo * this. To avoid unnecessary interoperability issues
8845b9c547cSRui Paulo * and extra frames, mark the pending negotiation as
8855b9c547cSRui Paulo * failed and do not reply to this GO Negotiation
8865b9c547cSRui Paulo * Request frame.
8875b9c547cSRui Paulo */
8885b9c547cSRui Paulo p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
8895b9c547cSRui Paulo p2p_go_neg_failed(p2p, *msg.status);
8905b9c547cSRui Paulo p2p_parse_free(&msg);
8915b9c547cSRui Paulo return;
8925b9c547cSRui Paulo }
893f05cddf9SRui Paulo goto fail;
894f05cddf9SRui Paulo }
895f05cddf9SRui Paulo
896f05cddf9SRui Paulo if (dev == NULL)
897f05cddf9SRui Paulo dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
8985b9c547cSRui Paulo else if ((dev->flags & P2P_DEV_PROBE_REQ_ONLY) ||
8995b9c547cSRui Paulo !(dev->flags & P2P_DEV_REPORTED))
900f05cddf9SRui Paulo p2p_add_dev_info(p2p, sa, dev, &msg);
9015b9c547cSRui Paulo else if (!dev->listen_freq && !dev->oper_freq) {
9025b9c547cSRui Paulo /*
9035b9c547cSRui Paulo * This may happen if the peer entry was added based on PD
9045b9c547cSRui Paulo * Request and no Probe Request/Response frame has been received
9055b9c547cSRui Paulo * from this peer (or that information has timed out).
9065b9c547cSRui Paulo */
9075b9c547cSRui Paulo p2p_dbg(p2p, "Update peer " MACSTR
9085b9c547cSRui Paulo " based on GO Neg Req since listen/oper freq not known",
9095b9c547cSRui Paulo MAC2STR(dev->info.p2p_device_addr));
9105b9c547cSRui Paulo p2p_add_dev_info(p2p, sa, dev, &msg);
9115b9c547cSRui Paulo }
9125b9c547cSRui Paulo
913*a90b9d01SCy Schubert if (dev)
914*a90b9d01SCy Schubert p2p_update_peer_6ghz_capab(dev, &msg);
915*a90b9d01SCy Schubert
9165b9c547cSRui Paulo if (p2p->go_neg_peer && p2p->go_neg_peer == dev)
9175b9c547cSRui Paulo eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
9185b9c547cSRui Paulo
919f05cddf9SRui Paulo if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
9205b9c547cSRui Paulo p2p_dbg(p2p, "User has rejected this peer");
921f05cddf9SRui Paulo status = P2P_SC_FAIL_REJECTED_BY_USER;
9225b9c547cSRui Paulo } else if (dev == NULL ||
9235b9c547cSRui Paulo (dev->wps_method == WPS_NOT_READY &&
9245b9c547cSRui Paulo (p2p->authorized_oob_dev_pw_id == 0 ||
9255b9c547cSRui Paulo p2p->authorized_oob_dev_pw_id !=
9265b9c547cSRui Paulo msg.dev_password_id))) {
9275b9c547cSRui Paulo p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
928f05cddf9SRui Paulo MAC2STR(sa));
929f05cddf9SRui Paulo status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
930f05cddf9SRui Paulo p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
931325151a3SRui Paulo msg.dev_password_id,
932325151a3SRui Paulo msg.go_intent ? (*msg.go_intent >> 1) :
933325151a3SRui Paulo 0);
934f05cddf9SRui Paulo } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
9355b9c547cSRui Paulo p2p_dbg(p2p, "Already in Group Formation with another peer");
936f05cddf9SRui Paulo status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
937f05cddf9SRui Paulo } else {
938f05cddf9SRui Paulo int go;
939f05cddf9SRui Paulo
940f05cddf9SRui Paulo if (!p2p->go_neg_peer) {
9415b9c547cSRui Paulo p2p_dbg(p2p, "Starting GO Negotiation with previously authorized peer");
942f05cddf9SRui Paulo if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
9435b9c547cSRui Paulo p2p_dbg(p2p, "Use default channel settings");
944f05cddf9SRui Paulo p2p->op_reg_class = p2p->cfg->op_reg_class;
945f05cddf9SRui Paulo p2p->op_channel = p2p->cfg->op_channel;
946f05cddf9SRui Paulo os_memcpy(&p2p->channels, &p2p->cfg->channels,
947f05cddf9SRui Paulo sizeof(struct p2p_channels));
948f05cddf9SRui Paulo } else {
9495b9c547cSRui Paulo p2p_dbg(p2p, "Use previously configured forced channel settings");
950f05cddf9SRui Paulo }
951f05cddf9SRui Paulo }
952f05cddf9SRui Paulo
953f05cddf9SRui Paulo dev->flags &= ~P2P_DEV_NOT_YET_READY;
954f05cddf9SRui Paulo
955f05cddf9SRui Paulo if (!msg.go_intent) {
9565b9c547cSRui Paulo p2p_dbg(p2p, "No GO Intent attribute received");
957f05cddf9SRui Paulo goto fail;
958f05cddf9SRui Paulo }
959f05cddf9SRui Paulo if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
9605b9c547cSRui Paulo p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
961f05cddf9SRui Paulo *msg.go_intent >> 1);
962f05cddf9SRui Paulo goto fail;
963f05cddf9SRui Paulo }
964f05cddf9SRui Paulo
965f05cddf9SRui Paulo if (dev->go_neg_req_sent &&
966f05cddf9SRui Paulo os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
9675b9c547cSRui Paulo p2p_dbg(p2p, "Do not reply since peer has higher address and GO Neg Request already sent");
968f05cddf9SRui Paulo p2p_parse_free(&msg);
969f05cddf9SRui Paulo return;
970f05cddf9SRui Paulo }
971f05cddf9SRui Paulo
972780fb4a2SCy Schubert if (dev->go_neg_req_sent &&
973780fb4a2SCy Schubert (dev->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
974780fb4a2SCy Schubert p2p_dbg(p2p,
975780fb4a2SCy Schubert "Do not reply since peer is waiting for us to start a new GO Negotiation and GO Neg Request already sent");
976780fb4a2SCy Schubert p2p_parse_free(&msg);
977780fb4a2SCy Schubert return;
978780fb4a2SCy Schubert }
979780fb4a2SCy Schubert
980f05cddf9SRui Paulo go = p2p_go_det(p2p->go_intent, *msg.go_intent);
981f05cddf9SRui Paulo if (go < 0) {
9825b9c547cSRui Paulo p2p_dbg(p2p, "Incompatible GO Intent");
983f05cddf9SRui Paulo status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
984f05cddf9SRui Paulo goto fail;
985f05cddf9SRui Paulo }
986f05cddf9SRui Paulo
987f05cddf9SRui Paulo if (p2p_peer_channels(p2p, dev, msg.channel_list,
988f05cddf9SRui Paulo msg.channel_list_len) < 0) {
9895b9c547cSRui Paulo p2p_dbg(p2p, "No common channels found");
990f05cddf9SRui Paulo status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
991f05cddf9SRui Paulo goto fail;
992f05cddf9SRui Paulo }
993f05cddf9SRui Paulo
994f05cddf9SRui Paulo switch (msg.dev_password_id) {
995f05cddf9SRui Paulo case DEV_PW_REGISTRAR_SPECIFIED:
9965b9c547cSRui Paulo p2p_dbg(p2p, "PIN from peer Display");
997f05cddf9SRui Paulo if (dev->wps_method != WPS_PIN_KEYPAD) {
9985b9c547cSRui Paulo p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
999f05cddf9SRui Paulo p2p_wps_method_str(dev->wps_method));
1000f05cddf9SRui Paulo status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1001f05cddf9SRui Paulo goto fail;
1002f05cddf9SRui Paulo }
1003f05cddf9SRui Paulo break;
1004f05cddf9SRui Paulo case DEV_PW_USER_SPECIFIED:
10055b9c547cSRui Paulo p2p_dbg(p2p, "Peer entered PIN on Keypad");
1006f05cddf9SRui Paulo if (dev->wps_method != WPS_PIN_DISPLAY) {
10075b9c547cSRui Paulo p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1008f05cddf9SRui Paulo p2p_wps_method_str(dev->wps_method));
1009f05cddf9SRui Paulo status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1010f05cddf9SRui Paulo goto fail;
1011f05cddf9SRui Paulo }
1012f05cddf9SRui Paulo break;
1013f05cddf9SRui Paulo case DEV_PW_PUSHBUTTON:
10145b9c547cSRui Paulo p2p_dbg(p2p, "Peer using pushbutton");
1015f05cddf9SRui Paulo if (dev->wps_method != WPS_PBC) {
10165b9c547cSRui Paulo p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
10175b9c547cSRui Paulo p2p_wps_method_str(dev->wps_method));
10185b9c547cSRui Paulo status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
10195b9c547cSRui Paulo goto fail;
10205b9c547cSRui Paulo }
10215b9c547cSRui Paulo break;
10225b9c547cSRui Paulo case DEV_PW_P2PS_DEFAULT:
10235b9c547cSRui Paulo p2p_dbg(p2p, "Peer using P2PS pin");
10245b9c547cSRui Paulo if (dev->wps_method != WPS_P2PS) {
10255b9c547cSRui Paulo p2p_dbg(p2p,
10265b9c547cSRui Paulo "We have wps_method=%s -> incompatible",
1027f05cddf9SRui Paulo p2p_wps_method_str(dev->wps_method));
1028f05cddf9SRui Paulo status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1029f05cddf9SRui Paulo goto fail;
1030f05cddf9SRui Paulo }
1031f05cddf9SRui Paulo break;
1032f05cddf9SRui Paulo default:
10335b9c547cSRui Paulo if (msg.dev_password_id &&
10345b9c547cSRui Paulo msg.dev_password_id == dev->oob_pw_id) {
10355b9c547cSRui Paulo p2p_dbg(p2p, "Peer using NFC");
10365b9c547cSRui Paulo if (dev->wps_method != WPS_NFC) {
10375b9c547cSRui Paulo p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
10385b9c547cSRui Paulo p2p_wps_method_str(
10395b9c547cSRui Paulo dev->wps_method));
10405b9c547cSRui Paulo status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
10415b9c547cSRui Paulo goto fail;
10425b9c547cSRui Paulo }
10435b9c547cSRui Paulo break;
10445b9c547cSRui Paulo }
10455b9c547cSRui Paulo #ifdef CONFIG_WPS_NFC
10465b9c547cSRui Paulo if (p2p->authorized_oob_dev_pw_id &&
10475b9c547cSRui Paulo msg.dev_password_id ==
10485b9c547cSRui Paulo p2p->authorized_oob_dev_pw_id) {
10495b9c547cSRui Paulo p2p_dbg(p2p, "Using static handover with our device password from NFC Tag");
10505b9c547cSRui Paulo dev->wps_method = WPS_NFC;
10515b9c547cSRui Paulo dev->oob_pw_id = p2p->authorized_oob_dev_pw_id;
10525b9c547cSRui Paulo break;
10535b9c547cSRui Paulo }
10545b9c547cSRui Paulo #endif /* CONFIG_WPS_NFC */
10555b9c547cSRui Paulo p2p_dbg(p2p, "Unsupported Device Password ID %d",
1056f05cddf9SRui Paulo msg.dev_password_id);
1057f05cddf9SRui Paulo status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1058f05cddf9SRui Paulo goto fail;
1059f05cddf9SRui Paulo }
1060f05cddf9SRui Paulo
1061f05cddf9SRui Paulo if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
1062f05cddf9SRui Paulo goto fail;
1063f05cddf9SRui Paulo
1064f05cddf9SRui Paulo dev->go_state = go ? LOCAL_GO : REMOTE_GO;
10655b9c547cSRui Paulo dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
1066f05cddf9SRui Paulo msg.operating_channel[4]);
10675b9c547cSRui Paulo p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
10685b9c547cSRui Paulo dev->oper_freq);
1069f05cddf9SRui Paulo
1070325151a3SRui Paulo /*
1071325151a3SRui Paulo * Use the driver preferred frequency list extension if
1072325151a3SRui Paulo * supported.
1073325151a3SRui Paulo */
1074325151a3SRui Paulo p2p_check_pref_chan(p2p, go, dev, &msg);
1075325151a3SRui Paulo
1076f05cddf9SRui Paulo if (msg.config_timeout) {
1077f05cddf9SRui Paulo dev->go_timeout = msg.config_timeout[0];
1078f05cddf9SRui Paulo dev->client_timeout = msg.config_timeout[1];
1079f05cddf9SRui Paulo }
1080f05cddf9SRui Paulo
10815b9c547cSRui Paulo p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa));
1082f05cddf9SRui Paulo if (p2p->state != P2P_IDLE)
1083f05cddf9SRui Paulo p2p_stop_find_for_freq(p2p, rx_freq);
1084f05cddf9SRui Paulo p2p_set_state(p2p, P2P_GO_NEG);
1085f05cddf9SRui Paulo p2p_clear_timeout(p2p);
1086f05cddf9SRui Paulo dev->dialog_token = msg.dialog_token;
1087f05cddf9SRui Paulo os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1088f05cddf9SRui Paulo p2p->go_neg_peer = dev;
10895b9c547cSRui Paulo eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
1090f05cddf9SRui Paulo status = P2P_SC_SUCCESS;
1091f05cddf9SRui Paulo }
1092f05cddf9SRui Paulo
1093f05cddf9SRui Paulo fail:
1094f05cddf9SRui Paulo if (dev)
1095f05cddf9SRui Paulo dev->status = status;
1096f05cddf9SRui Paulo resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
1097f05cddf9SRui Paulo !tie_breaker);
1098f05cddf9SRui Paulo p2p_parse_free(&msg);
1099f05cddf9SRui Paulo if (resp == NULL)
1100f05cddf9SRui Paulo return;
11015b9c547cSRui Paulo p2p_dbg(p2p, "Sending GO Negotiation Response");
1102f05cddf9SRui Paulo if (rx_freq > 0)
1103f05cddf9SRui Paulo freq = rx_freq;
1104f05cddf9SRui Paulo else
11055b9c547cSRui Paulo freq = p2p_channel_to_freq(p2p->cfg->reg_class,
1106f05cddf9SRui Paulo p2p->cfg->channel);
1107f05cddf9SRui Paulo if (freq < 0) {
11085b9c547cSRui Paulo p2p_dbg(p2p, "Unknown regulatory class/channel");
1109f05cddf9SRui Paulo wpabuf_free(resp);
1110f05cddf9SRui Paulo return;
1111f05cddf9SRui Paulo }
1112f05cddf9SRui Paulo if (status == P2P_SC_SUCCESS) {
1113f05cddf9SRui Paulo p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
1114f05cddf9SRui Paulo dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
1115f05cddf9SRui Paulo if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) {
1116f05cddf9SRui Paulo /*
1117f05cddf9SRui Paulo * Peer has smaller address, so the GO Negotiation
1118f05cddf9SRui Paulo * Response from us is expected to complete
1119f05cddf9SRui Paulo * negotiation. Ignore a GO Negotiation Response from
1120f05cddf9SRui Paulo * the peer if it happens to be received after this
1121f05cddf9SRui Paulo * point due to a race condition in GO Negotiation
1122f05cddf9SRui Paulo * Request transmission and processing.
1123f05cddf9SRui Paulo */
1124f05cddf9SRui Paulo dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1125f05cddf9SRui Paulo }
1126f05cddf9SRui Paulo } else
1127f05cddf9SRui Paulo p2p->pending_action_state =
1128f05cddf9SRui Paulo P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
1129f05cddf9SRui Paulo if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
1130f05cddf9SRui Paulo p2p->cfg->dev_addr,
1131780fb4a2SCy Schubert wpabuf_head(resp), wpabuf_len(resp), 100) < 0) {
11325b9c547cSRui Paulo p2p_dbg(p2p, "Failed to send Action frame");
1133f05cddf9SRui Paulo }
1134f05cddf9SRui Paulo
1135f05cddf9SRui Paulo wpabuf_free(resp);
1136f05cddf9SRui Paulo }
1137f05cddf9SRui Paulo
1138f05cddf9SRui Paulo
p2p_build_go_neg_conf(struct p2p_data * p2p,struct p2p_device * peer,u8 dialog_token,u8 status,const u8 * resp_chan,int go)1139f05cddf9SRui Paulo static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
1140f05cddf9SRui Paulo struct p2p_device *peer,
1141f05cddf9SRui Paulo u8 dialog_token, u8 status,
1142f05cddf9SRui Paulo const u8 *resp_chan, int go)
1143f05cddf9SRui Paulo {
1144f05cddf9SRui Paulo struct wpabuf *buf;
1145f05cddf9SRui Paulo u8 *len;
1146f05cddf9SRui Paulo struct p2p_channels res;
1147f05cddf9SRui Paulo u8 group_capab;
1148f05cddf9SRui Paulo size_t extra = 0;
1149*a90b9d01SCy Schubert bool is_6ghz_capab;
1150f05cddf9SRui Paulo
11515b9c547cSRui Paulo p2p_dbg(p2p, "Building GO Negotiation Confirm");
1152f05cddf9SRui Paulo
1153f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
1154f05cddf9SRui Paulo if (p2p->wfd_ie_go_neg)
1155f05cddf9SRui Paulo extra = wpabuf_len(p2p->wfd_ie_go_neg);
1156f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
1157f05cddf9SRui Paulo
11585b9c547cSRui Paulo if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
11595b9c547cSRui Paulo extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
11605b9c547cSRui Paulo
1161f05cddf9SRui Paulo buf = wpabuf_alloc(1000 + extra);
1162f05cddf9SRui Paulo if (buf == NULL)
1163f05cddf9SRui Paulo return NULL;
1164f05cddf9SRui Paulo
1165f05cddf9SRui Paulo p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
1166f05cddf9SRui Paulo
1167f05cddf9SRui Paulo len = p2p_buf_add_ie_hdr(buf);
1168f05cddf9SRui Paulo p2p_buf_add_status(buf, status);
1169f05cddf9SRui Paulo group_capab = 0;
1170f05cddf9SRui Paulo if (peer->go_state == LOCAL_GO) {
1171f05cddf9SRui Paulo if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1172f05cddf9SRui Paulo group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
1173f05cddf9SRui Paulo if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1174f05cddf9SRui Paulo group_capab |=
1175f05cddf9SRui Paulo P2P_GROUP_CAPAB_PERSISTENT_RECONN;
1176f05cddf9SRui Paulo }
1177f05cddf9SRui Paulo if (p2p->cross_connect)
1178f05cddf9SRui Paulo group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
1179f05cddf9SRui Paulo if (p2p->cfg->p2p_intra_bss)
1180f05cddf9SRui Paulo group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
1181f05cddf9SRui Paulo }
1182f05cddf9SRui Paulo p2p_buf_add_capability(buf, p2p->dev_capab &
1183f05cddf9SRui Paulo ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
1184f05cddf9SRui Paulo group_capab);
1185f05cddf9SRui Paulo if (go || resp_chan == NULL)
1186f05cddf9SRui Paulo p2p_buf_add_operating_channel(buf, p2p->cfg->country,
1187f05cddf9SRui Paulo p2p->op_reg_class,
1188f05cddf9SRui Paulo p2p->op_channel);
1189f05cddf9SRui Paulo else
1190f05cddf9SRui Paulo p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
1191f05cddf9SRui Paulo resp_chan[3], resp_chan[4]);
1192f05cddf9SRui Paulo p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
1193*a90b9d01SCy Schubert is_6ghz_capab = is_p2p_6ghz_capable(p2p) &&
1194*a90b9d01SCy Schubert p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr);
1195*a90b9d01SCy Schubert p2p_buf_add_channel_list(buf, p2p->cfg->country, &res, is_6ghz_capab);
1196f05cddf9SRui Paulo if (go) {
1197f05cddf9SRui Paulo p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
1198f05cddf9SRui Paulo p2p->ssid_len);
1199f05cddf9SRui Paulo }
1200f05cddf9SRui Paulo p2p_buf_update_ie_hdr(buf, len);
1201f05cddf9SRui Paulo
1202f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
1203f05cddf9SRui Paulo if (p2p->wfd_ie_go_neg)
1204f05cddf9SRui Paulo wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
1205f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
1206f05cddf9SRui Paulo
12075b9c547cSRui Paulo if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
12085b9c547cSRui Paulo wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
12095b9c547cSRui Paulo
1210f05cddf9SRui Paulo return buf;
1211f05cddf9SRui Paulo }
1212f05cddf9SRui Paulo
1213f05cddf9SRui Paulo
p2p_process_go_neg_resp(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len,int rx_freq)1214f05cddf9SRui Paulo void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
1215f05cddf9SRui Paulo const u8 *data, size_t len, int rx_freq)
1216f05cddf9SRui Paulo {
1217f05cddf9SRui Paulo struct p2p_device *dev;
1218f05cddf9SRui Paulo int go = -1;
1219f05cddf9SRui Paulo struct p2p_message msg;
1220f05cddf9SRui Paulo u8 status = P2P_SC_SUCCESS;
1221f05cddf9SRui Paulo int freq;
1222f05cddf9SRui Paulo
12235b9c547cSRui Paulo p2p_dbg(p2p, "Received GO Negotiation Response from " MACSTR
1224f05cddf9SRui Paulo " (freq=%d)", MAC2STR(sa), rx_freq);
1225f05cddf9SRui Paulo dev = p2p_get_device(p2p, sa);
1226f05cddf9SRui Paulo if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1227f05cddf9SRui Paulo dev != p2p->go_neg_peer) {
12285b9c547cSRui Paulo p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
1229f05cddf9SRui Paulo MAC2STR(sa));
1230f05cddf9SRui Paulo return;
1231f05cddf9SRui Paulo }
1232f05cddf9SRui Paulo
1233f05cddf9SRui Paulo if (p2p_parse(data, len, &msg))
1234f05cddf9SRui Paulo return;
1235f05cddf9SRui Paulo
1236f05cddf9SRui Paulo if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
12375b9c547cSRui Paulo p2p_dbg(p2p, "Was not expecting GO Negotiation Response - ignore");
1238f05cddf9SRui Paulo p2p_parse_free(&msg);
1239f05cddf9SRui Paulo return;
1240f05cddf9SRui Paulo }
1241f05cddf9SRui Paulo dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1242*a90b9d01SCy Schubert p2p_update_peer_6ghz_capab(dev, &msg);
1243f05cddf9SRui Paulo
1244f05cddf9SRui Paulo if (msg.dialog_token != dev->dialog_token) {
12455b9c547cSRui Paulo p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
1246f05cddf9SRui Paulo msg.dialog_token, dev->dialog_token);
1247f05cddf9SRui Paulo p2p_parse_free(&msg);
1248f05cddf9SRui Paulo return;
1249f05cddf9SRui Paulo }
1250f05cddf9SRui Paulo
1251f05cddf9SRui Paulo if (!msg.status) {
12525b9c547cSRui Paulo p2p_dbg(p2p, "No Status attribute received");
1253f05cddf9SRui Paulo status = P2P_SC_FAIL_INVALID_PARAMS;
1254f05cddf9SRui Paulo goto fail;
1255f05cddf9SRui Paulo }
1256f05cddf9SRui Paulo if (*msg.status) {
12575b9c547cSRui Paulo p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status);
1258f05cddf9SRui Paulo dev->go_neg_req_sent = 0;
1259f05cddf9SRui Paulo if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
12605b9c547cSRui Paulo p2p_dbg(p2p, "Wait for the peer to become ready for GO Negotiation");
1261f05cddf9SRui Paulo dev->flags |= P2P_DEV_NOT_YET_READY;
12625b9c547cSRui Paulo eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p,
12635b9c547cSRui Paulo NULL);
12645b9c547cSRui Paulo eloop_register_timeout(120, 0, p2p_go_neg_wait_timeout,
12655b9c547cSRui Paulo p2p, NULL);
12665b9c547cSRui Paulo if (p2p->state == P2P_CONNECT_LISTEN)
12675b9c547cSRui Paulo p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
12685b9c547cSRui Paulo else
1269f05cddf9SRui Paulo p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
1270f05cddf9SRui Paulo p2p_set_timeout(p2p, 0, 0);
1271f05cddf9SRui Paulo } else {
12725b9c547cSRui Paulo p2p_dbg(p2p, "Stop GO Negotiation attempt");
12735b9c547cSRui Paulo p2p_go_neg_failed(p2p, *msg.status);
1274f05cddf9SRui Paulo }
1275f05cddf9SRui Paulo p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1276f05cddf9SRui Paulo p2p_parse_free(&msg);
1277f05cddf9SRui Paulo return;
1278f05cddf9SRui Paulo }
1279f05cddf9SRui Paulo
1280f05cddf9SRui Paulo if (!msg.capability) {
12815b9c547cSRui Paulo p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Response");
1282f05cddf9SRui Paulo #ifdef CONFIG_P2P_STRICT
1283f05cddf9SRui Paulo status = P2P_SC_FAIL_INVALID_PARAMS;
1284f05cddf9SRui Paulo goto fail;
1285f05cddf9SRui Paulo #endif /* CONFIG_P2P_STRICT */
1286f05cddf9SRui Paulo }
1287f05cddf9SRui Paulo
1288f05cddf9SRui Paulo if (!msg.p2p_device_info) {
12895b9c547cSRui Paulo p2p_dbg(p2p, "Mandatory P2P Device Info attribute missing from GO Negotiation Response");
1290f05cddf9SRui Paulo #ifdef CONFIG_P2P_STRICT
1291f05cddf9SRui Paulo status = P2P_SC_FAIL_INVALID_PARAMS;
1292f05cddf9SRui Paulo goto fail;
1293f05cddf9SRui Paulo #endif /* CONFIG_P2P_STRICT */
1294f05cddf9SRui Paulo }
1295f05cddf9SRui Paulo
1296f05cddf9SRui Paulo if (!msg.intended_addr) {
12975b9c547cSRui Paulo p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
1298f05cddf9SRui Paulo status = P2P_SC_FAIL_INVALID_PARAMS;
1299f05cddf9SRui Paulo goto fail;
1300f05cddf9SRui Paulo }
1301f05cddf9SRui Paulo
1302f05cddf9SRui Paulo if (!msg.go_intent) {
13035b9c547cSRui Paulo p2p_dbg(p2p, "No GO Intent attribute received");
1304f05cddf9SRui Paulo status = P2P_SC_FAIL_INVALID_PARAMS;
1305f05cddf9SRui Paulo goto fail;
1306f05cddf9SRui Paulo }
1307f05cddf9SRui Paulo if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
13085b9c547cSRui Paulo p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
1309f05cddf9SRui Paulo *msg.go_intent >> 1);
1310f05cddf9SRui Paulo status = P2P_SC_FAIL_INVALID_PARAMS;
1311f05cddf9SRui Paulo goto fail;
1312f05cddf9SRui Paulo }
1313f05cddf9SRui Paulo
1314f05cddf9SRui Paulo go = p2p_go_det(p2p->go_intent, *msg.go_intent);
1315f05cddf9SRui Paulo if (go < 0) {
13165b9c547cSRui Paulo p2p_dbg(p2p, "Incompatible GO Intent");
1317f05cddf9SRui Paulo status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
1318f05cddf9SRui Paulo goto fail;
1319f05cddf9SRui Paulo }
1320f05cddf9SRui Paulo
1321f05cddf9SRui Paulo if (!go && msg.group_id) {
1322f05cddf9SRui Paulo /* Store SSID for Provisioning step */
1323f05cddf9SRui Paulo p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1324f05cddf9SRui Paulo os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1325f05cddf9SRui Paulo } else if (!go) {
13265b9c547cSRui Paulo p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Response");
1327f05cddf9SRui Paulo p2p->ssid_len = 0;
1328f05cddf9SRui Paulo status = P2P_SC_FAIL_INVALID_PARAMS;
1329f05cddf9SRui Paulo goto fail;
1330f05cddf9SRui Paulo }
1331f05cddf9SRui Paulo
1332f05cddf9SRui Paulo if (!msg.config_timeout) {
13335b9c547cSRui Paulo p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Response");
1334f05cddf9SRui Paulo #ifdef CONFIG_P2P_STRICT
1335f05cddf9SRui Paulo status = P2P_SC_FAIL_INVALID_PARAMS;
1336f05cddf9SRui Paulo goto fail;
1337f05cddf9SRui Paulo #endif /* CONFIG_P2P_STRICT */
1338f05cddf9SRui Paulo } else {
1339f05cddf9SRui Paulo dev->go_timeout = msg.config_timeout[0];
1340f05cddf9SRui Paulo dev->client_timeout = msg.config_timeout[1];
1341f05cddf9SRui Paulo }
1342f05cddf9SRui Paulo
1343780fb4a2SCy Schubert if (msg.wfd_subelems) {
1344780fb4a2SCy Schubert wpabuf_free(dev->info.wfd_subelems);
1345780fb4a2SCy Schubert dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
1346780fb4a2SCy Schubert }
1347780fb4a2SCy Schubert
1348f05cddf9SRui Paulo if (!msg.operating_channel && !go) {
1349f05cddf9SRui Paulo /*
1350f05cddf9SRui Paulo * Note: P2P Client may omit Operating Channel attribute to
1351f05cddf9SRui Paulo * indicate it does not have a preference.
1352f05cddf9SRui Paulo */
13535b9c547cSRui Paulo p2p_dbg(p2p, "No Operating Channel attribute received");
1354f05cddf9SRui Paulo status = P2P_SC_FAIL_INVALID_PARAMS;
1355f05cddf9SRui Paulo goto fail;
1356f05cddf9SRui Paulo }
1357f05cddf9SRui Paulo if (!msg.channel_list) {
13585b9c547cSRui Paulo p2p_dbg(p2p, "No Channel List attribute received");
1359f05cddf9SRui Paulo status = P2P_SC_FAIL_INVALID_PARAMS;
1360f05cddf9SRui Paulo goto fail;
1361f05cddf9SRui Paulo }
1362f05cddf9SRui Paulo
1363f05cddf9SRui Paulo if (p2p_peer_channels(p2p, dev, msg.channel_list,
1364f05cddf9SRui Paulo msg.channel_list_len) < 0) {
13655b9c547cSRui Paulo p2p_dbg(p2p, "No common channels found");
1366f05cddf9SRui Paulo status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
1367f05cddf9SRui Paulo goto fail;
1368f05cddf9SRui Paulo }
1369f05cddf9SRui Paulo
1370f05cddf9SRui Paulo if (msg.operating_channel) {
13715b9c547cSRui Paulo dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
1372f05cddf9SRui Paulo msg.operating_channel[4]);
13735b9c547cSRui Paulo p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
13745b9c547cSRui Paulo dev->oper_freq);
1375f05cddf9SRui Paulo } else
1376f05cddf9SRui Paulo dev->oper_freq = 0;
1377f05cddf9SRui Paulo
1378f05cddf9SRui Paulo switch (msg.dev_password_id) {
1379f05cddf9SRui Paulo case DEV_PW_REGISTRAR_SPECIFIED:
13805b9c547cSRui Paulo p2p_dbg(p2p, "PIN from peer Display");
1381f05cddf9SRui Paulo if (dev->wps_method != WPS_PIN_KEYPAD) {
13825b9c547cSRui Paulo p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1383f05cddf9SRui Paulo p2p_wps_method_str(dev->wps_method));
1384f05cddf9SRui Paulo status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1385f05cddf9SRui Paulo goto fail;
1386f05cddf9SRui Paulo }
1387f05cddf9SRui Paulo break;
1388f05cddf9SRui Paulo case DEV_PW_USER_SPECIFIED:
13895b9c547cSRui Paulo p2p_dbg(p2p, "Peer entered PIN on Keypad");
1390f05cddf9SRui Paulo if (dev->wps_method != WPS_PIN_DISPLAY) {
13915b9c547cSRui Paulo p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1392f05cddf9SRui Paulo p2p_wps_method_str(dev->wps_method));
1393f05cddf9SRui Paulo status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1394f05cddf9SRui Paulo goto fail;
1395f05cddf9SRui Paulo }
1396f05cddf9SRui Paulo break;
1397f05cddf9SRui Paulo case DEV_PW_PUSHBUTTON:
13985b9c547cSRui Paulo p2p_dbg(p2p, "Peer using pushbutton");
1399f05cddf9SRui Paulo if (dev->wps_method != WPS_PBC) {
14005b9c547cSRui Paulo p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
14015b9c547cSRui Paulo p2p_wps_method_str(dev->wps_method));
14025b9c547cSRui Paulo status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
14035b9c547cSRui Paulo goto fail;
14045b9c547cSRui Paulo }
14055b9c547cSRui Paulo break;
14065b9c547cSRui Paulo case DEV_PW_P2PS_DEFAULT:
14075b9c547cSRui Paulo p2p_dbg(p2p, "P2P: Peer using P2PS default pin");
14085b9c547cSRui Paulo if (dev->wps_method != WPS_P2PS) {
14095b9c547cSRui Paulo p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
1410f05cddf9SRui Paulo p2p_wps_method_str(dev->wps_method));
1411f05cddf9SRui Paulo status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1412f05cddf9SRui Paulo goto fail;
1413f05cddf9SRui Paulo }
1414f05cddf9SRui Paulo break;
1415f05cddf9SRui Paulo default:
14165b9c547cSRui Paulo if (msg.dev_password_id &&
14175b9c547cSRui Paulo msg.dev_password_id == dev->oob_pw_id) {
14185b9c547cSRui Paulo p2p_dbg(p2p, "Peer using NFC");
14195b9c547cSRui Paulo if (dev->wps_method != WPS_NFC) {
14205b9c547cSRui Paulo p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
14215b9c547cSRui Paulo p2p_wps_method_str(dev->wps_method));
14225b9c547cSRui Paulo status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
14235b9c547cSRui Paulo goto fail;
14245b9c547cSRui Paulo }
14255b9c547cSRui Paulo break;
14265b9c547cSRui Paulo }
14275b9c547cSRui Paulo p2p_dbg(p2p, "Unsupported Device Password ID %d",
1428f05cddf9SRui Paulo msg.dev_password_id);
1429f05cddf9SRui Paulo status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
1430f05cddf9SRui Paulo goto fail;
1431f05cddf9SRui Paulo }
1432f05cddf9SRui Paulo
1433f05cddf9SRui Paulo if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
1434f05cddf9SRui Paulo goto fail;
1435f05cddf9SRui Paulo
1436325151a3SRui Paulo /*
1437325151a3SRui Paulo * Use the driver preferred frequency list extension if local device is
1438325151a3SRui Paulo * GO.
1439325151a3SRui Paulo */
1440325151a3SRui Paulo if (go)
1441325151a3SRui Paulo p2p_check_pref_chan(p2p, go, dev, &msg);
1442325151a3SRui Paulo
1443f05cddf9SRui Paulo p2p_set_state(p2p, P2P_GO_NEG);
1444f05cddf9SRui Paulo p2p_clear_timeout(p2p);
1445f05cddf9SRui Paulo
14465b9c547cSRui Paulo p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa));
1447f05cddf9SRui Paulo os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
1448f05cddf9SRui Paulo
1449f05cddf9SRui Paulo fail:
14505b9c547cSRui Paulo /* Store GO Negotiation Confirmation to allow retransmission */
14515b9c547cSRui Paulo wpabuf_free(dev->go_neg_conf);
14525b9c547cSRui Paulo dev->go_neg_conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token,
14535b9c547cSRui Paulo status, msg.operating_channel,
14545b9c547cSRui Paulo go);
1455f05cddf9SRui Paulo p2p_parse_free(&msg);
14565b9c547cSRui Paulo if (dev->go_neg_conf == NULL)
1457f05cddf9SRui Paulo return;
14585b9c547cSRui Paulo p2p_dbg(p2p, "Sending GO Negotiation Confirm");
1459f05cddf9SRui Paulo if (status == P2P_SC_SUCCESS) {
1460f05cddf9SRui Paulo p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
1461f05cddf9SRui Paulo dev->go_state = go ? LOCAL_GO : REMOTE_GO;
1462f05cddf9SRui Paulo } else
1463f05cddf9SRui Paulo p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1464f05cddf9SRui Paulo if (rx_freq > 0)
1465f05cddf9SRui Paulo freq = rx_freq;
1466f05cddf9SRui Paulo else
1467f05cddf9SRui Paulo freq = dev->listen_freq;
14685b9c547cSRui Paulo
14695b9c547cSRui Paulo dev->go_neg_conf_freq = freq;
14705b9c547cSRui Paulo dev->go_neg_conf_sent = 0;
14715b9c547cSRui Paulo
1472f05cddf9SRui Paulo if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
14735b9c547cSRui Paulo wpabuf_head(dev->go_neg_conf),
1474780fb4a2SCy Schubert wpabuf_len(dev->go_neg_conf), 50) < 0) {
14755b9c547cSRui Paulo p2p_dbg(p2p, "Failed to send Action frame");
14765b9c547cSRui Paulo p2p_go_neg_failed(p2p, -1);
14775b9c547cSRui Paulo p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
14785b9c547cSRui Paulo } else
14795b9c547cSRui Paulo dev->go_neg_conf_sent++;
14805b9c547cSRui Paulo if (status != P2P_SC_SUCCESS) {
14815b9c547cSRui Paulo p2p_dbg(p2p, "GO Negotiation failed");
14825b9c547cSRui Paulo p2p_go_neg_failed(p2p, status);
1483f05cddf9SRui Paulo }
1484f05cddf9SRui Paulo }
1485f05cddf9SRui Paulo
1486f05cddf9SRui Paulo
p2p_process_go_neg_conf(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len)1487f05cddf9SRui Paulo void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
1488f05cddf9SRui Paulo const u8 *data, size_t len)
1489f05cddf9SRui Paulo {
1490f05cddf9SRui Paulo struct p2p_device *dev;
1491f05cddf9SRui Paulo struct p2p_message msg;
1492f05cddf9SRui Paulo
14935b9c547cSRui Paulo p2p_dbg(p2p, "Received GO Negotiation Confirm from " MACSTR,
1494f05cddf9SRui Paulo MAC2STR(sa));
1495f05cddf9SRui Paulo dev = p2p_get_device(p2p, sa);
1496f05cddf9SRui Paulo if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
1497f05cddf9SRui Paulo dev != p2p->go_neg_peer) {
14985b9c547cSRui Paulo p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
1499f05cddf9SRui Paulo MAC2STR(sa));
1500f05cddf9SRui Paulo return;
1501f05cddf9SRui Paulo }
1502f05cddf9SRui Paulo
1503f05cddf9SRui Paulo if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
15045b9c547cSRui Paulo p2p_dbg(p2p, "Stopped waiting for TX status on GO Negotiation Response since we already received Confirmation");
1505f05cddf9SRui Paulo p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1506f05cddf9SRui Paulo }
1507f05cddf9SRui Paulo
1508f05cddf9SRui Paulo if (p2p_parse(data, len, &msg))
1509f05cddf9SRui Paulo return;
1510f05cddf9SRui Paulo
1511f05cddf9SRui Paulo if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
15125b9c547cSRui Paulo p2p_dbg(p2p, "Was not expecting GO Negotiation Confirm - ignore");
15135b9c547cSRui Paulo p2p_parse_free(&msg);
1514f05cddf9SRui Paulo return;
1515f05cddf9SRui Paulo }
1516f05cddf9SRui Paulo dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
15175b9c547cSRui Paulo p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
1518f05cddf9SRui Paulo
1519f05cddf9SRui Paulo if (msg.dialog_token != dev->dialog_token) {
15205b9c547cSRui Paulo p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
1521f05cddf9SRui Paulo msg.dialog_token, dev->dialog_token);
1522f05cddf9SRui Paulo p2p_parse_free(&msg);
1523f05cddf9SRui Paulo return;
1524f05cddf9SRui Paulo }
1525f05cddf9SRui Paulo
1526f05cddf9SRui Paulo if (!msg.status) {
15275b9c547cSRui Paulo p2p_dbg(p2p, "No Status attribute received");
1528f05cddf9SRui Paulo p2p_parse_free(&msg);
1529f05cddf9SRui Paulo return;
1530f05cddf9SRui Paulo }
1531f05cddf9SRui Paulo if (*msg.status) {
15325b9c547cSRui Paulo p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status);
15335b9c547cSRui Paulo p2p_go_neg_failed(p2p, *msg.status);
1534f05cddf9SRui Paulo p2p_parse_free(&msg);
1535f05cddf9SRui Paulo return;
1536f05cddf9SRui Paulo }
1537f05cddf9SRui Paulo
1538*a90b9d01SCy Schubert p2p_update_peer_6ghz_capab(dev, &msg);
1539*a90b9d01SCy Schubert
1540f05cddf9SRui Paulo if (dev->go_state == REMOTE_GO && msg.group_id) {
1541f05cddf9SRui Paulo /* Store SSID for Provisioning step */
1542f05cddf9SRui Paulo p2p->ssid_len = msg.group_id_len - ETH_ALEN;
1543f05cddf9SRui Paulo os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
1544f05cddf9SRui Paulo } else if (dev->go_state == REMOTE_GO) {
15455b9c547cSRui Paulo p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Confirmation");
1546f05cddf9SRui Paulo p2p->ssid_len = 0;
15475b9c547cSRui Paulo p2p_go_neg_failed(p2p, P2P_SC_FAIL_INVALID_PARAMS);
1548f05cddf9SRui Paulo p2p_parse_free(&msg);
1549f05cddf9SRui Paulo return;
1550f05cddf9SRui Paulo }
1551f05cddf9SRui Paulo
1552f05cddf9SRui Paulo if (!msg.operating_channel) {
15535b9c547cSRui Paulo p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
1554f05cddf9SRui Paulo #ifdef CONFIG_P2P_STRICT
1555f05cddf9SRui Paulo p2p_parse_free(&msg);
1556f05cddf9SRui Paulo return;
1557f05cddf9SRui Paulo #endif /* CONFIG_P2P_STRICT */
15585b9c547cSRui Paulo } else if (dev->go_state == REMOTE_GO) {
15595b9c547cSRui Paulo int oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
15605b9c547cSRui Paulo msg.operating_channel[4]);
15615b9c547cSRui Paulo if (oper_freq != dev->oper_freq) {
15625b9c547cSRui Paulo p2p_dbg(p2p, "Updated peer (GO) operating channel preference from %d MHz to %d MHz",
15635b9c547cSRui Paulo dev->oper_freq, oper_freq);
15645b9c547cSRui Paulo dev->oper_freq = oper_freq;
15655b9c547cSRui Paulo }
1566f05cddf9SRui Paulo }
1567f05cddf9SRui Paulo
1568f05cddf9SRui Paulo if (!msg.channel_list) {
15695b9c547cSRui Paulo p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
1570f05cddf9SRui Paulo #ifdef CONFIG_P2P_STRICT
1571f05cddf9SRui Paulo p2p_parse_free(&msg);
1572f05cddf9SRui Paulo return;
1573f05cddf9SRui Paulo #endif /* CONFIG_P2P_STRICT */
1574f05cddf9SRui Paulo }
1575f05cddf9SRui Paulo
1576f05cddf9SRui Paulo p2p_parse_free(&msg);
1577f05cddf9SRui Paulo
1578f05cddf9SRui Paulo if (dev->go_state == UNKNOWN_GO) {
1579f05cddf9SRui Paulo /*
1580f05cddf9SRui Paulo * This should not happen since GO negotiation has already
1581f05cddf9SRui Paulo * been completed.
1582f05cddf9SRui Paulo */
15835b9c547cSRui Paulo p2p_dbg(p2p, "Unexpected GO Neg state - do not know which end becomes GO");
1584f05cddf9SRui Paulo return;
1585f05cddf9SRui Paulo }
1586f05cddf9SRui Paulo
1587f05cddf9SRui Paulo /*
1588f05cddf9SRui Paulo * The peer could have missed our ctrl::ack frame for GO Negotiation
1589f05cddf9SRui Paulo * Confirm and continue retransmitting the frame. To reduce the
1590f05cddf9SRui Paulo * likelihood of the peer not getting successful TX status for the
1591f05cddf9SRui Paulo * GO Negotiation Confirm frame, wait a short time here before starting
1592f05cddf9SRui Paulo * the group so that we will remain on the current channel to
1593f05cddf9SRui Paulo * acknowledge any possible retransmission from the peer.
1594f05cddf9SRui Paulo */
15955b9c547cSRui Paulo p2p_dbg(p2p, "20 ms wait on current channel before starting group");
1596f05cddf9SRui Paulo os_sleep(0, 20000);
1597f05cddf9SRui Paulo
1598f05cddf9SRui Paulo p2p_go_complete(p2p, dev);
1599f05cddf9SRui Paulo }
1600