1  /*
2   * wpa_supplicant - P2P
3   * Copyright (c) 2009-2010, Atheros Communications
4   * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
5   *
6   * This software may be distributed under the terms of the BSD license.
7   * See README for more details.
8   */
9  
10  #include "includes.h"
11  
12  #include "common.h"
13  #include "eloop.h"
14  #include "common/ieee802_11_common.h"
15  #include "common/ieee802_11_defs.h"
16  #include "common/wpa_ctrl.h"
17  #include "wps/wps_i.h"
18  #include "p2p/p2p.h"
19  #include "ap/hostapd.h"
20  #include "ap/ap_config.h"
21  #include "ap/sta_info.h"
22  #include "ap/ap_drv_ops.h"
23  #include "ap/wps_hostapd.h"
24  #include "ap/p2p_hostapd.h"
25  #include "ap/dfs.h"
26  #include "ap/wpa_auth.h"
27  #include "eapol_supp/eapol_supp_sm.h"
28  #include "rsn_supp/wpa.h"
29  #include "rsn_supp/pmksa_cache.h"
30  #include "wpa_supplicant_i.h"
31  #include "driver_i.h"
32  #include "ap.h"
33  #include "config_ssid.h"
34  #include "config.h"
35  #include "notify.h"
36  #include "scan.h"
37  #include "bss.h"
38  #include "offchannel.h"
39  #include "wps_supplicant.h"
40  #include "p2p_supplicant.h"
41  #include "wifi_display.h"
42  
43  
44  /*
45   * How many times to try to scan to find the GO before giving up on join
46   * request.
47   */
48  #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
49  
50  #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
51  
52  /**
53   * Defines time interval in seconds when a GO needs to evacuate a frequency that
54   * it is currently using, but is no longer valid for P2P use cases.
55   */
56  #define P2P_GO_FREQ_CHANGE_TIME 5
57  
58  /**
59   * Defines CSA parameters which are used when GO evacuates the no longer valid
60   * channel (and if the driver supports channel switch).
61   */
62  #define P2P_GO_CSA_COUNT 7
63  #define P2P_GO_CSA_BLOCK_TX 0
64  
65  #ifndef P2P_MAX_CLIENT_IDLE
66  /*
67   * How many seconds to try to reconnect to the GO when connection in P2P client
68   * role has been lost.
69   */
70  #define P2P_MAX_CLIENT_IDLE 10
71  #endif /* P2P_MAX_CLIENT_IDLE */
72  
73  #ifndef P2P_MAX_INITIAL_CONN_WAIT
74  /*
75   * How many seconds to wait for initial 4-way handshake to get completed after
76   * WPS provisioning step or after the re-invocation of a persistent group on a
77   * P2P Client.
78   */
79  #define P2P_MAX_INITIAL_CONN_WAIT 10
80  #endif /* P2P_MAX_INITIAL_CONN_WAIT */
81  
82  #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
83  /*
84   * How many seconds to wait for initial 4-way handshake to get completed after
85   * WPS provisioning step on the GO. This controls the extra time the P2P
86   * operation is considered to be in progress (e.g., to delay other scans) after
87   * WPS provisioning has been completed on the GO during group formation.
88   */
89  #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
90  #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
91  
92  #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
93  /*
94   * How many seconds to wait for initial 4-way handshake to get completed after
95   * re-invocation of a persistent group on the GO when the client is expected
96   * to connect automatically (no user interaction).
97   */
98  #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
99  #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
100  
101  #define P2P_MGMT_DEVICE_PREFIX		"p2p-dev-"
102  
103  /*
104   * How many seconds to wait to re-attempt to move GOs, in case previous attempt
105   * was not possible.
106   */
107  #define P2P_RECONSIDER_GO_MOVE_DELAY 30
108  
109  enum p2p_group_removal_reason {
110  	P2P_GROUP_REMOVAL_UNKNOWN,
111  	P2P_GROUP_REMOVAL_SILENT,
112  	P2P_GROUP_REMOVAL_FORMATION_FAILED,
113  	P2P_GROUP_REMOVAL_REQUESTED,
114  	P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
115  	P2P_GROUP_REMOVAL_UNAVAILABLE,
116  	P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
117  	P2P_GROUP_REMOVAL_PSK_FAILURE,
118  	P2P_GROUP_REMOVAL_FREQ_CONFLICT,
119  	P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
120  };
121  
122  
123  static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
124  static struct wpa_supplicant *
125  wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
126  			 int go);
127  static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
128  			       const u8 *ssid, size_t ssid_len);
129  static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
130  				int *force_freq, int *pref_freq, int go,
131  				struct weighted_pcl *pref_freq_list,
132  				unsigned int *num_pref_freq);
133  static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
134  				   const u8 *ssid, size_t ssid_len);
135  static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
136  static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
137  			 const u8 *dev_addr, enum p2p_wps_method wps_method,
138  			 int auto_join, int freq,
139  			 const u8 *ssid, size_t ssid_len);
140  static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
141  static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
142  static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
143  static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
144  static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
145  					     void *timeout_ctx);
146  static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
147  static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
148  				       int group_added);
149  static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
150  static void wpas_stop_listen(void *ctx);
151  static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
152  static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
153  static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
154  					enum wpa_driver_if_type type);
155  static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
156  					    int already_deleted,
157  					    const char *reason);
158  static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
159  					     struct wpa_used_freq_data *freqs,
160  					     unsigned int num);
161  static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
162  static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
163  static void
164  wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
165  			     struct wpa_used_freq_data *freqs, unsigned int num,
166  			     enum wpas_p2p_channel_update_trig trig);
167  static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
168  static int wpas_p2p_disallowed_freq(struct wpa_global *global,
169  				    unsigned int freq);
170  
171  
wpas_get_6ghz_he_chwidth_capab(struct hostapd_hw_modes * mode)172  static int wpas_get_6ghz_he_chwidth_capab(struct hostapd_hw_modes *mode)
173  {
174  	int he_capab = 0;
175  
176  	if (mode)
177  		he_capab = mode->he_capab[WPAS_MODE_INFRA].phy_cap[
178  			HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
179  	return he_capab;
180  }
181  
182  
183  /*
184   * Get the number of concurrent channels that the HW can operate, but that are
185   * currently not in use by any of the wpa_supplicant interfaces.
186   */
wpas_p2p_num_unused_channels(struct wpa_supplicant * wpa_s)187  static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
188  {
189  	int *freqs;
190  	int num, unused;
191  
192  	freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
193  	if (!freqs)
194  		return -1;
195  
196  	num = get_shared_radio_freqs(wpa_s, freqs,
197  				     wpa_s->num_multichan_concurrent, false);
198  	os_free(freqs);
199  
200  	unused = wpa_s->num_multichan_concurrent - num;
201  	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
202  	return unused;
203  }
204  
205  
206  /*
207   * Get the frequencies that are currently in use by one or more of the virtual
208   * interfaces, and that are also valid for P2P operation.
209   */
210  static unsigned int
wpas_p2p_valid_oper_freqs(struct wpa_supplicant * wpa_s,struct wpa_used_freq_data * p2p_freqs,unsigned int len)211  wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
212  			  struct wpa_used_freq_data *p2p_freqs,
213  			  unsigned int len)
214  {
215  	struct wpa_used_freq_data *freqs;
216  	unsigned int num, i, j;
217  
218  	freqs = os_calloc(wpa_s->num_multichan_concurrent,
219  			  sizeof(struct wpa_used_freq_data));
220  	if (!freqs)
221  		return 0;
222  
223  	num = get_shared_radio_freqs_data(wpa_s, freqs,
224  					  wpa_s->num_multichan_concurrent,
225  					  false);
226  
227  	os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
228  
229  	for (i = 0, j = 0; i < num && j < len; i++) {
230  		if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
231  			p2p_freqs[j++] = freqs[i];
232  	}
233  
234  	os_free(freqs);
235  
236  	dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
237  
238  	return j;
239  }
240  
241  
wpas_p2p_set_own_freq_preference(struct wpa_supplicant * wpa_s,int freq)242  static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
243  					     int freq)
244  {
245  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
246  		return;
247  
248  	/* Use the wpa_s used to control the P2P Device operation */
249  	wpa_s = wpa_s->global->p2p_init_wpa_s;
250  
251  	if (wpa_s->conf->p2p_ignore_shared_freq &&
252  	    freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
253  	    wpas_p2p_num_unused_channels(wpa_s) > 0) {
254  		wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
255  			   freq);
256  		freq = 0;
257  	}
258  	p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
259  }
260  
261  
wpas_p2p_scan_res_handled(struct wpa_supplicant * wpa_s)262  static void wpas_p2p_scan_res_handled(struct wpa_supplicant *wpa_s)
263  {
264  	unsigned int delay = wpas_p2p_search_delay(wpa_s);
265  
266  	/* In case of concurrent P2P and external scans, delay P2P search. */
267  	if (external_scan_running(wpa_s->radio)) {
268  		delay = wpa_s->conf->p2p_search_delay;
269  		wpa_printf(MSG_DEBUG,
270  			   "P2P: Delay next P2P search by %d ms to let externally triggered scan complete",
271  			   delay);
272  	}
273  
274  	p2p_scan_res_handled(wpa_s->global->p2p, delay);
275  }
276  
277  
wpas_p2p_scan_res_handler(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res)278  static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
279  				      struct wpa_scan_results *scan_res)
280  {
281  	size_t i;
282  
283  	if (wpa_s->p2p_scan_work) {
284  		struct wpa_radio_work *work = wpa_s->p2p_scan_work;
285  		wpa_s->p2p_scan_work = NULL;
286  		radio_work_done(work);
287  	}
288  
289  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
290  		return;
291  
292  	wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
293  		   (int) scan_res->num);
294  
295  	for (i = 0; i < scan_res->num; i++) {
296  		struct wpa_scan_res *bss = scan_res->res[i];
297  		struct os_reltime time_tmp_age, entry_ts;
298  		const u8 *ies;
299  		size_t ies_len;
300  
301  		time_tmp_age.sec = bss->age / 1000;
302  		time_tmp_age.usec = (bss->age % 1000) * 1000;
303  		os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
304  
305  		ies = (const u8 *) (bss + 1);
306  		ies_len = bss->ie_len;
307  		if (bss->beacon_ie_len > 0 &&
308  		    !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
309  		    wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
310  			wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
311  				   MACSTR, MAC2STR(bss->bssid));
312  			ies = ies + ies_len;
313  			ies_len = bss->beacon_ie_len;
314  		}
315  
316  
317  		if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
318  					 bss->freq, &entry_ts, bss->level,
319  					 ies, ies_len) > 0)
320  			break;
321  	}
322  
323  	wpas_p2p_scan_res_handled(wpa_s);
324  }
325  
326  
wpas_p2p_scan_res_fail_handler(struct wpa_supplicant * wpa_s)327  static void wpas_p2p_scan_res_fail_handler(struct wpa_supplicant *wpa_s)
328  {
329  	if (wpa_s->p2p_scan_work) {
330  		struct wpa_radio_work *work = wpa_s->p2p_scan_work;
331  
332  		wpa_s->p2p_scan_work = NULL;
333  		radio_work_done(work);
334  	}
335  
336  	if (wpa_s->global->p2p_disabled || !wpa_s->global->p2p)
337  		return;
338  
339  	wpa_dbg(wpa_s, MSG_DEBUG,
340  		"P2P: Failed to get scan results - try to continue");
341  	wpas_p2p_scan_res_handled(wpa_s);
342  }
343  
344  
wpas_p2p_scan_freqs(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params,bool include_6ghz)345  void wpas_p2p_scan_freqs(struct wpa_supplicant *wpa_s,
346  			 struct wpa_driver_scan_params *params,
347  			 bool include_6ghz)
348  {
349  	wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
350  				params, false, false, false);
351  	wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
352  				params, false, false, false);
353  	wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211AD,
354  				params, false, false, false);
355  	if (!wpa_s->conf->p2p_6ghz_disable &&
356  	    is_p2p_allow_6ghz(wpa_s->global->p2p) && include_6ghz)
357  		wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
358  					params, true, true, false);
359  }
360  
361  
wpas_p2p_trigger_scan_cb(struct wpa_radio_work * work,int deinit)362  static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
363  {
364  	struct wpa_supplicant *wpa_s = work->wpa_s;
365  	struct wpa_driver_scan_params *params = work->ctx;
366  	int ret;
367  
368  	if (deinit) {
369  		if (!work->started) {
370  			wpa_scan_free_params(params);
371  			return;
372  		}
373  
374  		wpa_s->p2p_scan_work = NULL;
375  		return;
376  	}
377  
378  	if (wpa_s->clear_driver_scan_cache) {
379  		wpa_printf(MSG_DEBUG,
380  			   "Request driver to clear scan cache due to local BSS flush");
381  		params->only_new_results = 1;
382  	}
383  
384  	if (!params->freqs)
385  		wpas_p2p_scan_freqs(wpa_s, params, params->p2p_include_6ghz);
386  
387  	ret = wpa_drv_scan(wpa_s, params);
388  	if (ret == 0)
389  		wpa_s->curr_scan_cookie = params->scan_cookie;
390  	wpa_scan_free_params(params);
391  	work->ctx = NULL;
392  	if (ret) {
393  		radio_work_done(work);
394  		p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
395  		return;
396  	}
397  
398  	p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
399  	os_get_reltime(&wpa_s->scan_trigger_time);
400  	wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
401  	wpa_s->scan_res_fail_handler = wpas_p2p_scan_res_fail_handler;
402  	wpa_s->own_scan_requested = 1;
403  	wpa_s->clear_driver_scan_cache = 0;
404  	wpa_s->p2p_scan_work = work;
405  }
406  
407  
wpas_p2p_search_social_channel(struct wpa_supplicant * wpa_s,int freq)408  static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
409  					  int freq)
410  {
411  	if (wpa_s->global->p2p_24ghz_social_channels &&
412  	    (freq == 2412 || freq == 2437 || freq == 2462)) {
413  		/*
414  		 * Search all social channels regardless of whether these have
415  		 * been disabled for P2P operating channel use to avoid missing
416  		 * peers.
417  		 */
418  		return 1;
419  	}
420  	return p2p_supported_freq(wpa_s->global->p2p, freq);
421  }
422  
423  
wpas_p2p_scan(void * ctx,enum p2p_scan_type type,int freq,unsigned int num_req_dev_types,const u8 * req_dev_types,const u8 * dev_id,u16 pw_id,bool include_6ghz)424  static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
425  			 unsigned int num_req_dev_types,
426  			 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id,
427  			 bool include_6ghz)
428  {
429  	struct wpa_supplicant *wpa_s = ctx;
430  	struct wpa_driver_scan_params *params = NULL;
431  	struct wpabuf *wps_ie, *ies;
432  	unsigned int num_channels = 0;
433  	int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
434  	size_t ielen;
435  	u8 *n, i;
436  	unsigned int bands;
437  
438  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
439  		return -1;
440  
441  	if (wpa_s->p2p_scan_work) {
442  		wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
443  		return -1;
444  	}
445  
446  	params = os_zalloc(sizeof(*params));
447  	if (params == NULL)
448  		return -1;
449  
450  	/* P2P Wildcard SSID */
451  	params->num_ssids = 1;
452  	n = os_malloc(P2P_WILDCARD_SSID_LEN);
453  	if (n == NULL)
454  		goto fail;
455  	os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
456  	params->ssids[0].ssid = n;
457  	params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
458  
459  	wpa_s->wps->dev.p2p = 1;
460  	wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
461  					wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
462  					num_req_dev_types, req_dev_types);
463  	if (wps_ie == NULL)
464  		goto fail;
465  
466  	/*
467  	 * In case 6 GHz channels are requested as part of the P2P scan, only
468  	 * the PSCs would be included as P2P GOs are not expected to be
469  	 * collocated, i.e., they would not be announced in the RNR element of
470  	 * other APs.
471  	 */
472  	if (!wpa_s->conf->p2p_6ghz_disable)
473  		params->p2p_include_6ghz = include_6ghz;
474  	switch (type) {
475  	case P2P_SCAN_SOCIAL:
476  		params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
477  					  sizeof(int));
478  		if (params->freqs == NULL)
479  			goto fail;
480  		for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
481  			if (wpas_p2p_search_social_channel(
482  				    wpa_s, social_channels_freq[i]))
483  				params->freqs[num_channels++] =
484  					social_channels_freq[i];
485  		}
486  		params->freqs[num_channels++] = 0;
487  		break;
488  	case P2P_SCAN_FULL:
489  		break;
490  	case P2P_SCAN_SPECIFIC:
491  		params->freqs = os_calloc(2, sizeof(int));
492  		if (params->freqs == NULL)
493  			goto fail;
494  		params->freqs[0] = freq;
495  		params->freqs[1] = 0;
496  		break;
497  	case P2P_SCAN_SOCIAL_PLUS_ONE:
498  		params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
499  					  sizeof(int));
500  		if (params->freqs == NULL)
501  			goto fail;
502  		for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
503  			if (wpas_p2p_search_social_channel(
504  				    wpa_s, social_channels_freq[i]))
505  				params->freqs[num_channels++] =
506  					social_channels_freq[i];
507  		}
508  		if (p2p_supported_freq(wpa_s->global->p2p, freq))
509  			params->freqs[num_channels++] = freq;
510  		params->freqs[num_channels++] = 0;
511  		break;
512  	}
513  
514  	ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
515  	ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
516  	if (ies == NULL) {
517  		wpabuf_free(wps_ie);
518  		goto fail;
519  	}
520  	wpabuf_put_buf(ies, wps_ie);
521  	wpabuf_free(wps_ie);
522  
523  	bands = wpas_get_bands(wpa_s, params->freqs);
524  	p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
525  
526  	params->p2p_probe = 1;
527  	n = os_malloc(wpabuf_len(ies));
528  	if (n == NULL) {
529  		wpabuf_free(ies);
530  		goto fail;
531  	}
532  	os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
533  	params->extra_ies = n;
534  	params->extra_ies_len = wpabuf_len(ies);
535  	wpabuf_free(ies);
536  
537  	radio_remove_works(wpa_s, "p2p-scan", 0);
538  	if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
539  			   params) < 0)
540  		goto fail;
541  	return 0;
542  
543  fail:
544  	wpa_scan_free_params(params);
545  	return -1;
546  }
547  
548  
wpas_p2p_if_type(int p2p_group_interface)549  static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
550  {
551  	switch (p2p_group_interface) {
552  	case P2P_GROUP_INTERFACE_PENDING:
553  		return WPA_IF_P2P_GROUP;
554  	case P2P_GROUP_INTERFACE_GO:
555  		return WPA_IF_P2P_GO;
556  	case P2P_GROUP_INTERFACE_CLIENT:
557  		return WPA_IF_P2P_CLIENT;
558  	default:
559  		return WPA_IF_P2P_GROUP;
560  	}
561  }
562  
563  
wpas_get_p2p_group(struct wpa_supplicant * wpa_s,const u8 * ssid,size_t ssid_len,int * go)564  static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
565  						  const u8 *ssid,
566  						  size_t ssid_len, int *go)
567  {
568  	struct wpa_ssid *s;
569  
570  	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
571  		for (s = wpa_s->conf->ssid; s; s = s->next) {
572  			if (s->disabled != 0 || !s->p2p_group ||
573  			    s->ssid_len != ssid_len ||
574  			    os_memcmp(ssid, s->ssid, ssid_len) != 0)
575  				continue;
576  			if (s->mode == WPAS_MODE_P2P_GO &&
577  			    s != wpa_s->current_ssid)
578  				continue;
579  			if (go)
580  				*go = s->mode == WPAS_MODE_P2P_GO;
581  			return wpa_s;
582  		}
583  	}
584  
585  	return NULL;
586  }
587  
588  
run_wpas_p2p_disconnect(void * eloop_ctx,void * timeout_ctx)589  static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
590  {
591  	struct wpa_supplicant *wpa_s = eloop_ctx;
592  	wpa_printf(MSG_DEBUG,
593  		   "P2P: Complete previously requested removal of %s",
594  		   wpa_s->ifname);
595  	wpas_p2p_disconnect(wpa_s);
596  }
597  
598  
wpas_p2p_disconnect_safely(struct wpa_supplicant * wpa_s,struct wpa_supplicant * calling_wpa_s)599  static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
600  				      struct wpa_supplicant *calling_wpa_s)
601  {
602  	if (calling_wpa_s == wpa_s && wpa_s &&
603  	    wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
604  		/*
605  		 * The calling wpa_s instance is going to be removed. Do that
606  		 * from an eloop callback to keep the instance available until
607  		 * the caller has returned. This may be needed, e.g., to provide
608  		 * control interface responses on the per-interface socket.
609  		 */
610  		if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
611  					   wpa_s, NULL) < 0)
612  			return -1;
613  		return 0;
614  	}
615  
616  	return wpas_p2p_disconnect(wpa_s);
617  }
618  
619  
620  /* Determine total number of clients in active groups where we are the GO */
p2p_group_go_member_count(struct wpa_supplicant * wpa_s)621  static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
622  {
623  	unsigned int count = 0;
624  	struct wpa_ssid *s;
625  
626  	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
627  		for (s = wpa_s->conf->ssid; s; s = s->next) {
628  			wpa_printf(MSG_DEBUG,
629  				   "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
630  				   wpa_s, s, s->disabled, s->p2p_group,
631  				   s->mode);
632  			if (!s->disabled && s->p2p_group &&
633  			    s->mode == WPAS_MODE_P2P_GO) {
634  				count += p2p_get_group_num_members(
635  					wpa_s->p2p_group);
636  			}
637  		}
638  	}
639  
640  	return count;
641  }
642  
643  
p2p_is_active_persistent_group(struct wpa_supplicant * wpa_s)644  static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
645  {
646  	return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
647  		!wpa_s->current_ssid->disabled &&
648  		wpa_s->current_ssid->p2p_group &&
649  		wpa_s->current_ssid->p2p_persistent_group;
650  }
651  
652  
p2p_is_active_persistent_go(struct wpa_supplicant * wpa_s)653  static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
654  {
655  	return p2p_is_active_persistent_group(wpa_s) &&
656  		wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
657  }
658  
659  
660  /* Find an interface for a P2P group where we are the GO */
661  static struct wpa_supplicant *
wpas_p2p_get_go_group(struct wpa_supplicant * wpa_s)662  wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
663  {
664  	struct wpa_supplicant *save = NULL;
665  
666  	if (!wpa_s)
667  		return NULL;
668  
669  	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
670  		if (!p2p_is_active_persistent_go(wpa_s))
671  			continue;
672  
673  		/* Prefer a group with connected clients */
674  		if (p2p_get_group_num_members(wpa_s->p2p_group))
675  			return wpa_s;
676  		save = wpa_s;
677  	}
678  
679  	/* No group with connected clients, so pick the one without (if any) */
680  	return save;
681  }
682  
683  
p2p_is_active_persistent_cli(struct wpa_supplicant * wpa_s)684  static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
685  {
686  	return p2p_is_active_persistent_group(wpa_s) &&
687  		wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
688  }
689  
690  
691  /* Find an interface for a P2P group where we are the P2P Client */
692  static struct wpa_supplicant *
wpas_p2p_get_cli_group(struct wpa_supplicant * wpa_s)693  wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
694  {
695  	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
696  		if (p2p_is_active_persistent_cli(wpa_s))
697  			return wpa_s;
698  	}
699  
700  	return NULL;
701  }
702  
703  
704  /* Find a persistent group where we are the GO */
705  static struct wpa_ssid *
wpas_p2p_get_persistent_go(struct wpa_supplicant * wpa_s)706  wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
707  {
708  	struct wpa_ssid *s;
709  
710  	for (s = wpa_s->conf->ssid; s; s = s->next) {
711  		if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
712  			return s;
713  	}
714  
715  	return NULL;
716  }
717  
718  
p2ps_group_capability(void * ctx,u8 incoming,u8 role,unsigned int * force_freq,unsigned int * pref_freq)719  static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
720  				unsigned int *force_freq,
721  				unsigned int *pref_freq)
722  {
723  	struct wpa_supplicant *wpa_s = ctx;
724  	struct wpa_ssid *s;
725  	u8 conncap = P2PS_SETUP_NONE;
726  	unsigned int owned_members = 0;
727  	struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
728  	struct wpa_ssid *persistent_go;
729  	int p2p_no_group_iface;
730  	struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
731  	unsigned int size;
732  
733  	wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
734  
735  	if (force_freq)
736  		*force_freq = 0;
737  	if (pref_freq)
738  		*pref_freq = 0;
739  
740  	size = P2P_MAX_PREF_CHANNELS;
741  	if (force_freq && pref_freq &&
742  	    !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
743  				  (int *) pref_freq, 0, pref_freq_list, &size))
744  		wpas_p2p_set_own_freq_preference(wpa_s,
745  						 *force_freq ? *force_freq :
746  						 *pref_freq);
747  
748  	/*
749  	 * For non-concurrent capable devices:
750  	 * If persistent_go, then no new.
751  	 * If GO, then no client.
752  	 * If client, then no GO.
753  	 */
754  	go_wpa_s = wpas_p2p_get_go_group(wpa_s);
755  	if (go_wpa_s)
756  		owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
757  	persistent_go = wpas_p2p_get_persistent_go(wpa_s);
758  	p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
759  	cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
760  
761  	wpa_printf(MSG_DEBUG,
762  		   "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
763  		   go_wpa_s, owned_members, cli_wpa_s, persistent_go);
764  
765  	/* If not concurrent, restrict our choices */
766  	if (p2p_no_group_iface) {
767  		wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
768  
769  		if (cli_wpa_s)
770  			return P2PS_SETUP_NONE;
771  
772  		if (go_wpa_s) {
773  			if (role == P2PS_SETUP_CLIENT ||
774  			    incoming == P2PS_SETUP_GROUP_OWNER ||
775  			    p2p_client_limit_reached(go_wpa_s->p2p_group))
776  				return P2PS_SETUP_NONE;
777  
778  			return P2PS_SETUP_GROUP_OWNER;
779  		}
780  
781  		if (persistent_go) {
782  			if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
783  				if (!incoming)
784  					return P2PS_SETUP_GROUP_OWNER |
785  						P2PS_SETUP_CLIENT;
786  				if (incoming == P2PS_SETUP_NEW) {
787  					u8 r;
788  
789  					if (os_get_random(&r, sizeof(r)) < 0 ||
790  					    (r & 1))
791  						return P2PS_SETUP_CLIENT;
792  					return P2PS_SETUP_GROUP_OWNER;
793  				}
794  			}
795  		}
796  	}
797  
798  	/* If a required role has been specified, handle it here */
799  	if (role && role != P2PS_SETUP_NEW) {
800  		switch (incoming) {
801  		case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
802  		case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
803  			/*
804  			 * Peer has an active GO, so if the role allows it and
805  			 * we do not have any active roles, become client.
806  			 */
807  			if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
808  			    !cli_wpa_s)
809  				return P2PS_SETUP_CLIENT;
810  
811  			/* fall through */
812  
813  		case P2PS_SETUP_NONE:
814  		case P2PS_SETUP_NEW:
815  			conncap = role;
816  			goto grp_owner;
817  
818  		case P2PS_SETUP_GROUP_OWNER:
819  			/*
820  			 * Must be a complimentary role - cannot be a client to
821  			 * more than one peer.
822  			 */
823  			if (incoming == role || cli_wpa_s)
824  				return P2PS_SETUP_NONE;
825  
826  			return P2PS_SETUP_CLIENT;
827  
828  		case P2PS_SETUP_CLIENT:
829  			/* Must be a complimentary role */
830  			if (incoming != role) {
831  				conncap = P2PS_SETUP_GROUP_OWNER;
832  				goto grp_owner;
833  			}
834  			/* fall through */
835  
836  		default:
837  			return P2PS_SETUP_NONE;
838  		}
839  	}
840  
841  	/*
842  	 * For now, we only will support ownership of one group, and being a
843  	 * client of one group. Therefore, if we have either an existing GO
844  	 * group, or an existing client group, we will not do a new GO
845  	 * negotiation, but rather try to re-use the existing groups.
846  	 */
847  	switch (incoming) {
848  	case P2PS_SETUP_NONE:
849  	case P2PS_SETUP_NEW:
850  		if (cli_wpa_s)
851  			conncap = P2PS_SETUP_GROUP_OWNER;
852  		else if (!owned_members)
853  			conncap = P2PS_SETUP_NEW;
854  		else if (incoming == P2PS_SETUP_NONE)
855  			conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
856  		else
857  			conncap = P2PS_SETUP_CLIENT;
858  		break;
859  
860  	case P2PS_SETUP_CLIENT:
861  		conncap = P2PS_SETUP_GROUP_OWNER;
862  		break;
863  
864  	case P2PS_SETUP_GROUP_OWNER:
865  		if (!cli_wpa_s)
866  			conncap = P2PS_SETUP_CLIENT;
867  		break;
868  
869  	case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
870  	case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
871  		if (cli_wpa_s)
872  			conncap = P2PS_SETUP_GROUP_OWNER;
873  		else {
874  			u8 r;
875  
876  			if (os_get_random(&r, sizeof(r)) < 0 ||
877  			    (r & 1))
878  				conncap = P2PS_SETUP_CLIENT;
879  			else
880  				conncap = P2PS_SETUP_GROUP_OWNER;
881  		}
882  		break;
883  
884  	default:
885  		return P2PS_SETUP_NONE;
886  	}
887  
888  grp_owner:
889  	if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
890  	    (!incoming && (conncap & P2PS_SETUP_NEW))) {
891  		if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
892  			conncap &= ~P2PS_SETUP_GROUP_OWNER;
893  
894  		s = wpas_p2p_get_persistent_go(wpa_s);
895  		if (!s && !go_wpa_s && p2p_no_group_iface) {
896  			p2p_set_intended_addr(wpa_s->global->p2p,
897  					      wpa_s->p2p_mgmt ?
898  					      wpa_s->parent->own_addr :
899  					      wpa_s->own_addr);
900  		} else if (!s && !go_wpa_s) {
901  			if (wpas_p2p_add_group_interface(wpa_s,
902  							 WPA_IF_P2P_GROUP) < 0) {
903  				wpa_printf(MSG_ERROR,
904  					   "P2P: Failed to allocate a new interface for the group");
905  				return P2PS_SETUP_NONE;
906  			}
907  			wpa_s->global->pending_group_iface_for_p2ps = 1;
908  			p2p_set_intended_addr(wpa_s->global->p2p,
909  					      wpa_s->pending_interface_addr);
910  		}
911  	}
912  
913  	return conncap;
914  }
915  
916  
wpas_p2p_group_delete(struct wpa_supplicant * wpa_s,enum p2p_group_removal_reason removal_reason)917  static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
918  				 enum p2p_group_removal_reason removal_reason)
919  {
920  	struct wpa_ssid *ssid;
921  	char *gtype;
922  	const char *reason;
923  
924  	ssid = wpa_s->current_ssid;
925  	if (ssid == NULL) {
926  		/*
927  		 * The current SSID was not known, but there may still be a
928  		 * pending P2P group interface waiting for provisioning or a
929  		 * P2P group that is trying to reconnect.
930  		 */
931  		ssid = wpa_s->conf->ssid;
932  		while (ssid) {
933  			if (ssid->p2p_group && ssid->disabled != 2)
934  				break;
935  			ssid = ssid->next;
936  		}
937  		if (ssid == NULL &&
938  			wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
939  		{
940  			wpa_printf(MSG_ERROR, "P2P: P2P group interface "
941  				   "not found");
942  			return -1;
943  		}
944  	}
945  	if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
946  		gtype = "GO";
947  	else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
948  		 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
949  		wpa_s->reassociate = 0;
950  		wpa_s->disconnected = 1;
951  		gtype = "client";
952  	} else
953  		gtype = "GO";
954  
955  	if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
956  		wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
957  
958  	if (os_strcmp(gtype, "client") == 0) {
959  		wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
960  		if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
961  						wpa_s, NULL)) {
962  			wpa_printf(MSG_DEBUG,
963  				   "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
964  			removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
965  			eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
966  					     wpa_s, NULL);
967  		}
968  	}
969  
970  	if (wpa_s->cross_connect_in_use) {
971  		wpa_s->cross_connect_in_use = 0;
972  		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
973  			       P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
974  			       wpa_s->ifname, wpa_s->cross_connect_uplink);
975  	}
976  	switch (removal_reason) {
977  	case P2P_GROUP_REMOVAL_REQUESTED:
978  		reason = " reason=REQUESTED";
979  		break;
980  	case P2P_GROUP_REMOVAL_FORMATION_FAILED:
981  		reason = " reason=FORMATION_FAILED";
982  		break;
983  	case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
984  		reason = " reason=IDLE";
985  		break;
986  	case P2P_GROUP_REMOVAL_UNAVAILABLE:
987  		reason = " reason=UNAVAILABLE";
988  		break;
989  	case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
990  		reason = " reason=GO_ENDING_SESSION";
991  		break;
992  	case P2P_GROUP_REMOVAL_PSK_FAILURE:
993  		reason = " reason=PSK_FAILURE";
994  		break;
995  	case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
996  		reason = " reason=FREQ_CONFLICT";
997  		break;
998  	default:
999  		reason = "";
1000  		break;
1001  	}
1002  	if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
1003  		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
1004  			       P2P_EVENT_GROUP_REMOVED "%s %s%s",
1005  			       wpa_s->ifname, gtype, reason);
1006  	}
1007  
1008  	if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
1009  		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
1010  	if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
1011  		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
1012  	if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1013  				 wpa_s->p2pdev, NULL) > 0) {
1014  		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
1015  			   "timeout");
1016  		wpa_s->p2p_in_provisioning = 0;
1017  		wpas_p2p_group_formation_failed(wpa_s, 1, reason);
1018  	}
1019  
1020  	wpa_s->p2p_in_invitation = 0;
1021  	wpa_s->p2p_retry_limit = 0;
1022  	eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
1023  	eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
1024  
1025  	/*
1026  	 * Make sure wait for the first client does not remain active after the
1027  	 * group has been removed.
1028  	 */
1029  	wpa_s->global->p2p_go_wait_client.sec = 0;
1030  
1031  	if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
1032  		struct wpa_global *global;
1033  		char *ifname;
1034  		enum wpa_driver_if_type type;
1035  		wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
1036  			wpa_s->ifname);
1037  		global = wpa_s->global;
1038  		ifname = os_strdup(wpa_s->ifname);
1039  		type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
1040  		eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
1041  		wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
1042  		wpa_s = global->ifaces;
1043  		if (wpa_s && ifname)
1044  			wpa_drv_if_remove(wpa_s, type, ifname);
1045  		os_free(ifname);
1046  		return 1;
1047  	}
1048  
1049  	/*
1050  	 * The primary interface was used for P2P group operations, so
1051  	 * need to reset its p2pdev.
1052  	 */
1053  	wpa_s->p2pdev = wpa_s->parent;
1054  
1055  	if (!wpa_s->p2p_go_group_formation_completed) {
1056  		wpa_s->global->p2p_group_formation = NULL;
1057  		wpa_s->p2p_in_provisioning = 0;
1058  	}
1059  
1060  	wpa_s->show_group_started = 0;
1061  	os_free(wpa_s->go_params);
1062  	wpa_s->go_params = NULL;
1063  
1064  	os_free(wpa_s->p2p_group_common_freqs);
1065  	wpa_s->p2p_group_common_freqs = NULL;
1066  	wpa_s->p2p_group_common_freqs_num = 0;
1067  	wpa_s->p2p_go_do_acs = 0;
1068  	wpa_s->p2p_go_allow_dfs = 0;
1069  
1070  	wpa_s->waiting_presence_resp = 0;
1071  
1072  	wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
1073  	if (ssid && (ssid->p2p_group ||
1074  		     ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
1075  		     (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
1076  		int id = ssid->id;
1077  		if (ssid == wpa_s->current_ssid) {
1078  			wpa_sm_set_config(wpa_s->wpa, NULL);
1079  			eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1080  			wpa_s->current_ssid = NULL;
1081  		}
1082  		/*
1083  		 * Networks objects created during any P2P activities are not
1084  		 * exposed out as they might/will confuse certain non-P2P aware
1085  		 * applications since these network objects won't behave like
1086  		 * regular ones.
1087  		 *
1088  		 * Likewise, we don't send out network removed signals for such
1089  		 * network objects.
1090  		 */
1091  		wpas_notify_network_removed(wpa_s, ssid);
1092  		wpa_config_remove_network(wpa_s->conf, id);
1093  		wpa_supplicant_clear_status(wpa_s);
1094  		wpa_supplicant_cancel_sched_scan(wpa_s);
1095  	} else {
1096  		wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1097  			   "found");
1098  	}
1099  	if (wpa_s->ap_iface)
1100  		wpa_supplicant_ap_deinit(wpa_s);
1101  	else
1102  		wpa_drv_deinit_p2p_cli(wpa_s);
1103  
1104  	os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1105  
1106  	wpa_s->p2p_go_no_pri_sec_switch = 0;
1107  
1108  	return 0;
1109  }
1110  
1111  
wpas_p2p_persistent_group(struct wpa_supplicant * wpa_s,u8 * go_dev_addr,const u8 * ssid,size_t ssid_len)1112  static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1113  				     u8 *go_dev_addr,
1114  				     const u8 *ssid, size_t ssid_len)
1115  {
1116  	struct wpa_bss *bss;
1117  	const u8 *bssid;
1118  	struct wpabuf *p2p;
1119  	u8 group_capab;
1120  	const u8 *addr;
1121  
1122  	if (wpa_s->go_params)
1123  		bssid = wpa_s->go_params->peer_interface_addr;
1124  	else
1125  		bssid = wpa_s->bssid;
1126  
1127  	bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
1128  	if (bss == NULL && wpa_s->go_params &&
1129  	    !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1130  		bss = wpa_bss_get_p2p_dev_addr(
1131  			wpa_s, wpa_s->go_params->peer_device_addr);
1132  	if (bss == NULL) {
1133  		u8 iface_addr[ETH_ALEN];
1134  		if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1135  					   iface_addr) == 0)
1136  			bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1137  	}
1138  	if (bss == NULL) {
1139  		wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1140  			   "group is persistent - BSS " MACSTR " not found",
1141  			   MAC2STR(bssid));
1142  		return 0;
1143  	}
1144  
1145  	p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1146  	if (p2p == NULL)
1147  		p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1148  							 P2P_IE_VENDOR_TYPE);
1149  	if (p2p == NULL) {
1150  		wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1151  			   "group is persistent - BSS " MACSTR
1152  			   " did not include P2P IE", MAC2STR(bssid));
1153  		wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
1154  			    wpa_bss_ie_ptr(bss), bss->ie_len);
1155  		wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1156  			    wpa_bss_ie_ptr(bss) + bss->ie_len,
1157  			    bss->beacon_ie_len);
1158  		return 0;
1159  	}
1160  
1161  	group_capab = p2p_get_group_capab(p2p);
1162  	addr = p2p_get_go_dev_addr(p2p);
1163  	wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1164  		   "group_capab=0x%x", group_capab);
1165  	if (addr) {
1166  		os_memcpy(go_dev_addr, addr, ETH_ALEN);
1167  		wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1168  			   MAC2STR(addr));
1169  	} else
1170  		os_memset(go_dev_addr, 0, ETH_ALEN);
1171  	wpabuf_free(p2p);
1172  
1173  	wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1174  		   "go_dev_addr=" MACSTR,
1175  		   MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1176  
1177  	return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
1178  }
1179  
1180  
wpas_p2p_store_persistent_group(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,const u8 * go_dev_addr,int dik_id)1181  static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1182  					   struct wpa_ssid *ssid,
1183  					   const u8 *go_dev_addr, int dik_id)
1184  {
1185  	struct wpa_ssid *s;
1186  	int changed = 0;
1187  
1188  	wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1189  		   "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1190  	for (s = wpa_s->conf->ssid; s; s = s->next) {
1191  		if (s->disabled == 2 &&
1192  		    ether_addr_equal(go_dev_addr, s->bssid) &&
1193  		    s->ssid_len == ssid->ssid_len &&
1194  		    os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1195  			break;
1196  
1197  		if (dik_id && s->go_dik_id == dik_id)
1198  			break;
1199  	}
1200  
1201  	if (s) {
1202  		wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1203  			   "entry");
1204  		if (ssid->passphrase && !s->passphrase)
1205  			changed = 1;
1206  		else if (ssid->passphrase && s->passphrase &&
1207  			 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1208  			changed = 1;
1209  	} else {
1210  		wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1211  			   "entry");
1212  		changed = 1;
1213  		s = wpa_config_add_network(wpa_s->conf);
1214  		if (s == NULL)
1215  			return -1;
1216  
1217  		/*
1218  		 * Instead of network_added we emit persistent_group_added
1219  		 * notification. Also to keep the defense checks in
1220  		 * persistent_group obj registration method, we set the
1221  		 * relevant flags in s to designate it as a persistent group.
1222  		 */
1223  		s->p2p_group = 1;
1224  		s->p2p_persistent_group = 1;
1225  		wpas_notify_persistent_group_added(wpa_s, s);
1226  		wpa_config_set_network_defaults(s);
1227  	}
1228  
1229  	s->p2p_group = 1;
1230  	s->p2p_persistent_group = 1;
1231  	s->disabled = 2;
1232  	s->bssid_set = 1;
1233  	os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1234  	s->mode = ssid->mode;
1235  	s->auth_alg = ssid->auth_alg;
1236  	s->key_mgmt = ssid->key_mgmt;
1237  	s->proto = ssid->proto;
1238  	s->pbss = ssid->pbss;
1239  	s->pmk_valid = ssid->pmk_valid;
1240  	s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
1241  	s->export_keys = 1;
1242  	s->go_dik_id = dik_id;
1243  
1244  	if (ssid->sae_password) {
1245  		os_free(s->sae_password);
1246  		s->sae_password = os_strdup(ssid->sae_password);
1247  	}
1248  	if (ssid->passphrase) {
1249  		os_free(s->passphrase);
1250  		s->passphrase = os_strdup(ssid->passphrase);
1251  	}
1252  	if (ssid->psk_set) {
1253  		s->psk_set = 1;
1254  		os_memcpy(s->psk, ssid->psk, 32);
1255  	}
1256  	if (s->passphrase && !s->psk_set)
1257  		wpa_config_update_psk(s);
1258  	if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1259  		os_free(s->ssid);
1260  		s->ssid = os_malloc(ssid->ssid_len);
1261  	}
1262  	if (s->ssid) {
1263  		s->ssid_len = ssid->ssid_len;
1264  		os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1265  	}
1266  	if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1267  		dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1268  		wpa_s->global->add_psk = NULL;
1269  		changed = 1;
1270  	}
1271  
1272  	if (changed && wpa_s->conf->update_config &&
1273  	    wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1274  		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1275  	}
1276  
1277  	return s->id;
1278  }
1279  
1280  
wpas_p2p2_add_group_client_dik_id(struct wpa_ssid * s,int dik_id)1281  static void wpas_p2p2_add_group_client_dik_id(struct wpa_ssid *s, int dik_id)
1282  {
1283  	size_t i;
1284  	bool found = false;
1285  	size_t num = int_array_len(s->p2p2_client_list);
1286  
1287  	for (i = 0; i < num; i++) {
1288  		if (s->p2p2_client_list[i] != dik_id)
1289  			continue;
1290  
1291  		if (i == num - 1)
1292  			return; /* already the most recent entry */
1293  
1294  		/* Move the entry to mark it most recent */
1295  		os_memmove(s->p2p2_client_list + i,
1296  			   s->p2p2_client_list + i + 1,
1297  			   (num - i - 1) * sizeof(int));
1298  		s->p2p2_client_list[num - 1] = dik_id;
1299  		found = true;
1300  		break;
1301  	}
1302  
1303  	if (!found && num < P2P_MAX_STORED_CLIENTS) {
1304  		int_array_add_unique(&s->p2p2_client_list, dik_id);
1305  	} else if (!found && s->p2p2_client_list) {
1306  		/* Not enough room for an additional entry - drop the oldest
1307  		 * entry
1308  		 */
1309  		os_memmove(s->p2p2_client_list, s->p2p2_client_list + 1,
1310  			   (num - 1) * sizeof(int));
1311  		s->p2p2_client_list[num - 1] = dik_id;
1312  	}
1313  }
1314  
1315  
wpas_p2p_add_persistent_group_client(struct wpa_supplicant * wpa_s,const u8 * addr,int dik_id)1316  static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1317  						 const u8 *addr, int dik_id)
1318  {
1319  	struct wpa_ssid *ssid, *s;
1320  	u8 *n;
1321  	size_t i;
1322  	int found = 0;
1323  	struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
1324  
1325  	ssid = wpa_s->current_ssid;
1326  	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1327  	    !ssid->p2p_persistent_group)
1328  		return;
1329  
1330  	for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
1331  		if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1332  			continue;
1333  
1334  		if (s->ssid_len == ssid->ssid_len &&
1335  		    os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1336  			break;
1337  	}
1338  
1339  	if (s == NULL)
1340  		return;
1341  
1342  	if (dik_id) {
1343  		wpas_p2p2_add_group_client_dik_id(s, dik_id);
1344  		goto done;
1345  	}
1346  
1347  	for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
1348  		if (!ether_addr_equal(s->p2p_client_list + i * 2 * ETH_ALEN,
1349  				      addr))
1350  			continue;
1351  
1352  		if (i == s->num_p2p_clients - 1)
1353  			return; /* already the most recent entry */
1354  
1355  		/* move the entry to mark it most recent */
1356  		os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1357  			   s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1358  			   (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
1359  		os_memcpy(s->p2p_client_list +
1360  			  (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1361  			  ETH_ALEN);
1362  		os_memset(s->p2p_client_list +
1363  			  (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1364  			  0xff, ETH_ALEN);
1365  		found = 1;
1366  		break;
1367  	}
1368  
1369  	if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1370  		n = os_realloc_array(s->p2p_client_list,
1371  				     s->num_p2p_clients + 1, 2 * ETH_ALEN);
1372  		if (n == NULL)
1373  			return;
1374  		os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1375  			  ETH_ALEN);
1376  		os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1377  			  0xff, ETH_ALEN);
1378  		s->p2p_client_list = n;
1379  		s->num_p2p_clients++;
1380  	} else if (!found && s->p2p_client_list) {
1381  		/* Not enough room for an additional entry - drop the oldest
1382  		 * entry */
1383  		os_memmove(s->p2p_client_list,
1384  			   s->p2p_client_list + 2 * ETH_ALEN,
1385  			   (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
1386  		os_memcpy(s->p2p_client_list +
1387  			  (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
1388  			  addr, ETH_ALEN);
1389  		os_memset(s->p2p_client_list +
1390  			  (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1391  			  0xff, ETH_ALEN);
1392  	}
1393  
1394  done:
1395  	if (p2p_wpa_s->conf->update_config &&
1396  	    wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
1397  		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1398  }
1399  
1400  
wpas_p2p_group_started(struct wpa_supplicant * wpa_s,int go,struct wpa_ssid * ssid,int freq,const u8 * psk,const char * passphrase,const u8 * go_dev_addr,int persistent,const char * extra)1401  static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1402  				   int go, struct wpa_ssid *ssid, int freq,
1403  				   const u8 *psk, const char *passphrase,
1404  				   const u8 *go_dev_addr, int persistent,
1405  				   const char *extra)
1406  {
1407  	const char *ssid_txt;
1408  	char psk_txt[65];
1409  
1410  	if (psk)
1411  		wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1412  	else
1413  		psk_txt[0] = '\0';
1414  
1415  	if (ssid)
1416  		ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1417  	else
1418  		ssid_txt = "";
1419  
1420  	if (passphrase && passphrase[0] == '\0')
1421  		passphrase = NULL;
1422  
1423  	/*
1424  	 * Include PSK/passphrase only in the control interface message and
1425  	 * leave it out from the debug log entry.
1426  	 */
1427  	wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
1428  			    P2P_EVENT_GROUP_STARTED
1429  			    "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1430  			    MACSTR "%s%s",
1431  			    wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1432  			    psk ? " psk=" : "", psk_txt,
1433  			    passphrase ? " passphrase=\"" : "",
1434  			    passphrase ? passphrase : "",
1435  			    passphrase ? "\"" : "",
1436  			    MAC2STR(go_dev_addr),
1437  			    persistent ? " [PERSISTENT]" : "", extra);
1438  	wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1439  		   "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1440  		   wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1441  		   MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1442  		   extra);
1443  }
1444  
1445  
wpas_p2p_remove_all_identity(struct wpa_supplicant * wpa_s)1446  int wpas_p2p_remove_all_identity(struct wpa_supplicant *wpa_s)
1447  {
1448  	struct wpa_dev_ik *ik;
1449  
1450  	for (ik = wpa_s->conf->identity; ik; ik = ik->next)
1451  		wpa_config_remove_identity(wpa_s->conf, ik->id);
1452  
1453  	if (wpa_s->conf->update_config &&
1454  	    wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1455  		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1456  		return -1;
1457  	}
1458  	return 0;
1459  }
1460  
1461  
wpas_p2p_store_identity(struct wpa_supplicant * wpa_s,u8 cipher,const u8 * dik_data,size_t dik_len,const u8 * pmk,size_t pmk_len,const u8 * pmkid)1462  static int wpas_p2p_store_identity(struct wpa_supplicant *wpa_s, u8 cipher,
1463  				   const u8 *dik_data, size_t dik_len,
1464  				   const u8 *pmk, size_t pmk_len,
1465  				   const u8 *pmkid)
1466  {
1467  	struct wpa_dev_ik *ik;
1468  
1469  	for (ik = wpa_s->conf->identity; ik; ik = ik->next) {
1470  		if (dik_len == wpabuf_len(ik->dik) &&
1471  		    os_memcmp(dik_data, wpabuf_head(ik->dik), dik_len) == 0) {
1472  			wpa_printf(MSG_DEBUG,
1473  				   "P2P: Remove previous device identity entry for matching DIK");
1474  			wpa_config_remove_identity(wpa_s->conf, ik->id);
1475  			break;
1476  		}
1477  	}
1478  
1479  	wpa_printf(MSG_DEBUG, "P2P: Create a new device identity entry");
1480  	ik = wpa_config_add_identity(wpa_s->conf);
1481  	if (!ik)
1482  		return 0;
1483  
1484  	ik->dik = wpabuf_alloc_copy(dik_data, dik_len);
1485  	if (!ik->dik)
1486  		goto fail;
1487  	ik->pmk = wpabuf_alloc_copy(pmk, pmk_len);
1488  	if (!ik->pmk)
1489  		goto fail;
1490  	ik->pmkid = wpabuf_alloc_copy(pmkid, PMKID_LEN);
1491  	if (!ik->pmkid)
1492  		goto fail;
1493  
1494  	ik->dik_cipher = cipher;
1495  
1496  	if (wpa_s->conf->update_config &&
1497  	    wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1498  		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1499  		return 0;
1500  	}
1501  	return ik->id;
1502  
1503  fail:
1504  	wpa_config_remove_identity(wpa_s->conf, ik->id);
1505  	return 0;
1506  }
1507  
1508  
wpas_p2p_store_go_identity(struct wpa_supplicant * wpa_s,const u8 * go_dev_addr,const u8 * bssid)1509  static int wpas_p2p_store_go_identity(struct wpa_supplicant *wpa_s,
1510  				      const u8 *go_dev_addr, const u8 *bssid)
1511  {
1512  	int ret;
1513  	u8 cipher;
1514  	const u8 *dik_data, *pmk, *pmkid;
1515  	size_t dik_len, pmk_len;
1516  	u8 iface_addr[ETH_ALEN];
1517  	struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
1518  
1519  	if (!wpa_s->p2p2)
1520  		return 0;
1521  
1522  	ret = p2p_get_dev_identity_key(p2p_wpa_s->global->p2p, go_dev_addr,
1523  				       &dik_data, &dik_len, &cipher);
1524  	if (ret)
1525  		return 0;
1526  
1527  	ret = p2p_get_interface_addr(p2p_wpa_s->global->p2p, go_dev_addr,
1528  				     iface_addr);
1529  	if (ret) {
1530  		wpa_printf(MSG_DEBUG,
1531  			   "P2P: Fetch PMK for GO BSSID " MACSTR,
1532  			   MAC2STR(bssid));
1533  		os_memcpy(iface_addr, bssid, ETH_ALEN);
1534  	}
1535  	ret = wpa_sm_pmksa_get_pmk(wpa_s->wpa, iface_addr, &pmk, &pmk_len,
1536  				   &pmkid);
1537  	if (ret)
1538  		return 0;
1539  
1540  	wpa_printf(MSG_DEBUG,
1541  		   "P2P: Storing Device identity of GO (Interface Addr " MACSTR
1542  		   ")",
1543  		   MAC2STR(iface_addr));
1544  	return wpas_p2p_store_identity(p2p_wpa_s, cipher, dik_data, dik_len,
1545  				       pmk, pmk_len, pmkid);
1546  }
1547  
1548  
wpas_group_formation_completed(struct wpa_supplicant * wpa_s,int already_deleted,const char * failure_reason)1549  static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
1550  					   int already_deleted,
1551  					   const char *failure_reason)
1552  {
1553  	struct wpa_ssid *ssid;
1554  	int client;
1555  	int persistent;
1556  	u8 go_dev_addr[ETH_ALEN];
1557  
1558  	/*
1559  	 * This callback is likely called for the main interface. Update wpa_s
1560  	 * to use the group interface if a new interface was created for the
1561  	 * group.
1562  	 */
1563  	if (wpa_s->global->p2p_group_formation)
1564  		wpa_s = wpa_s->global->p2p_group_formation;
1565  	if (wpa_s->p2p_go_group_formation_completed) {
1566  		wpa_s->global->p2p_group_formation = NULL;
1567  		wpa_s->p2p_in_provisioning = 0;
1568  	} else if (wpa_s->p2p_in_provisioning && failure_reason) {
1569  		wpa_msg(wpa_s, MSG_DEBUG,
1570  			"P2P: Stop provisioning state due to failure");
1571  		wpa_s->p2p_in_provisioning = 0;
1572  	}
1573  	wpa_s->p2p_in_invitation = 0;
1574  	wpa_s->p2p_retry_limit = 0;
1575  	wpa_s->group_formation_reported = 1;
1576  
1577  	if (failure_reason) {
1578  		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
1579  			       P2P_EVENT_GROUP_FORMATION_FAILURE);
1580  		wpas_notify_p2p_group_formation_failure(wpa_s, failure_reason);
1581  		if (already_deleted)
1582  			return;
1583  		wpas_p2p_group_delete(wpa_s,
1584  				      P2P_GROUP_REMOVAL_FORMATION_FAILED);
1585  		return;
1586  	}
1587  
1588  	wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
1589  		       P2P_EVENT_GROUP_FORMATION_SUCCESS);
1590  
1591  	ssid = wpa_s->current_ssid;
1592  	if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1593  		ssid->mode = WPAS_MODE_P2P_GO;
1594  		p2p_group_notif_formation_done(wpa_s->p2p_group);
1595  		wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1596  	}
1597  
1598  	persistent = 0;
1599  	if (ssid) {
1600  		client = ssid->mode == WPAS_MODE_INFRA;
1601  		if (ssid->mode == WPAS_MODE_P2P_GO) {
1602  			persistent = ssid->p2p_persistent_group;
1603  			os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1604  				  ETH_ALEN);
1605  		} else {
1606  			os_memset(go_dev_addr, 0, ETH_ALEN);
1607  			persistent = wpas_p2p_persistent_group(wpa_s,
1608  							       go_dev_addr,
1609  							       ssid->ssid,
1610  							       ssid->ssid_len);
1611  		}
1612  	} else {
1613  		client = wpa_s->p2p_group_interface ==
1614  			P2P_GROUP_INTERFACE_CLIENT;
1615  		os_memset(go_dev_addr, 0, ETH_ALEN);
1616  	}
1617  
1618  	wpa_s->show_group_started = 0;
1619  	if (client) {
1620  		/*
1621  		 * Indicate event only after successfully completed 4-way
1622  		 * handshake, i.e., when the interface is ready for data
1623  		 * packets.
1624  		 */
1625  		wpa_s->show_group_started = 1;
1626  	} else {
1627  		wpas_p2p_group_started(wpa_s, 1, ssid,
1628  				       ssid ? ssid->frequency : 0,
1629  				       ssid && ssid->passphrase == NULL &&
1630  				       ssid->psk_set ? ssid->psk : NULL,
1631  				       ssid ? ssid->passphrase : NULL,
1632  				       go_dev_addr, persistent, "");
1633  		wpas_p2p_cross_connect_setup(wpa_s);
1634  		wpas_p2p_set_group_idle_timeout(wpa_s);
1635  	}
1636  
1637  	if (persistent)
1638  		wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1639  						ssid, go_dev_addr, 0);
1640  	else {
1641  		os_free(wpa_s->global->add_psk);
1642  		wpa_s->global->add_psk = NULL;
1643  	}
1644  
1645  	if (!client) {
1646  		wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
1647  		os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1648  	}
1649  }
1650  
1651  
1652  struct send_action_work {
1653  	unsigned int freq;
1654  	u8 dst[ETH_ALEN];
1655  	u8 src[ETH_ALEN];
1656  	u8 bssid[ETH_ALEN];
1657  	size_t len;
1658  	unsigned int wait_time;
1659  	u8 buf[0];
1660  };
1661  
1662  
wpas_p2p_free_send_action_work(struct wpa_supplicant * wpa_s)1663  static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1664  {
1665  	struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1666  
1667  	wpa_printf(MSG_DEBUG,
1668  		   "P2P: Free Action frame radio work @%p (freq=%u dst="
1669  		   MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1670  		   wpa_s->p2p_send_action_work, awork->freq,
1671  		   MAC2STR(awork->dst), MAC2STR(awork->src),
1672  		   MAC2STR(awork->bssid), awork->wait_time);
1673  	wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1674  		    awork->buf, awork->len);
1675  	os_free(awork);
1676  	wpa_s->p2p_send_action_work->ctx = NULL;
1677  	radio_work_done(wpa_s->p2p_send_action_work);
1678  	wpa_s->p2p_send_action_work = NULL;
1679  }
1680  
1681  
wpas_p2p_send_action_work_timeout(void * eloop_ctx,void * timeout_ctx)1682  static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1683  					      void *timeout_ctx)
1684  {
1685  	struct wpa_supplicant *wpa_s = eloop_ctx;
1686  
1687  	if (!wpa_s->p2p_send_action_work)
1688  		return;
1689  
1690  	wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1691  	wpas_p2p_free_send_action_work(wpa_s);
1692  }
1693  
1694  
wpas_p2p_action_tx_clear(struct wpa_supplicant * wpa_s)1695  static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1696  {
1697  	if (wpa_s->p2p_send_action_work) {
1698  		struct send_action_work *awork;
1699  
1700  		awork = wpa_s->p2p_send_action_work->ctx;
1701  		wpa_printf(MSG_DEBUG,
1702  			   "P2P: Clear Action TX work @%p (wait_time=%u)",
1703  			   wpa_s->p2p_send_action_work, awork->wait_time);
1704  		if (awork->wait_time == 0) {
1705  			wpas_p2p_free_send_action_work(wpa_s);
1706  		} else {
1707  			/*
1708  			 * In theory, this should not be needed, but number of
1709  			 * places in the P2P code is still using non-zero wait
1710  			 * time for the last Action frame in the sequence and
1711  			 * some of these do not call send_action_done().
1712  			 */
1713  			eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1714  					     wpa_s, NULL);
1715  			eloop_register_timeout(
1716  				0, awork->wait_time * 1000,
1717  				wpas_p2p_send_action_work_timeout,
1718  				wpa_s, NULL);
1719  		}
1720  	}
1721  }
1722  
1723  
wpas_p2p_send_action_tx_status(struct wpa_supplicant * wpa_s,unsigned int freq,const u8 * dst,const u8 * src,const u8 * bssid,const u8 * data,size_t data_len,enum offchannel_send_action_result result)1724  static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1725  					   unsigned int freq,
1726  					   const u8 *dst, const u8 *src,
1727  					   const u8 *bssid,
1728  					   const u8 *data, size_t data_len,
1729  					   enum offchannel_send_action_result
1730  					   result)
1731  {
1732  	enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1733  
1734  	wpas_p2p_action_tx_clear(wpa_s);
1735  
1736  	if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1737  		return;
1738  
1739  	switch (result) {
1740  	case OFFCHANNEL_SEND_ACTION_SUCCESS:
1741  		res = P2P_SEND_ACTION_SUCCESS;
1742  		break;
1743  	case OFFCHANNEL_SEND_ACTION_NO_ACK:
1744  		res = P2P_SEND_ACTION_NO_ACK;
1745  		break;
1746  	case OFFCHANNEL_SEND_ACTION_FAILED:
1747  		res = P2P_SEND_ACTION_FAILED;
1748  		break;
1749  	}
1750  
1751  	p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1752  
1753  	if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1754  	    wpa_s->pending_pd_before_join &&
1755  	    (ether_addr_equal(dst, wpa_s->pending_join_dev_addr) ||
1756  	     ether_addr_equal(dst, wpa_s->pending_join_iface_addr)) &&
1757  	    wpa_s->p2p_fallback_to_go_neg) {
1758  		wpa_s->pending_pd_before_join = 0;
1759  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1760  			"during p2p_connect-auto");
1761  		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
1762  			       P2P_EVENT_FALLBACK_TO_GO_NEG
1763  			       "reason=no-ACK-to-PD-Req");
1764  		wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1765  		return;
1766  	}
1767  }
1768  
1769  
wpas_send_action_cb(struct wpa_radio_work * work,int deinit)1770  static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1771  {
1772  	struct wpa_supplicant *wpa_s = work->wpa_s;
1773  	struct send_action_work *awork = work->ctx;
1774  
1775  	if (deinit) {
1776  		if (work->started) {
1777  			eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1778  					     wpa_s, NULL);
1779  			wpa_s->p2p_send_action_work = NULL;
1780  			offchannel_send_action_done(wpa_s);
1781  		}
1782  		os_free(awork);
1783  		return;
1784  	}
1785  
1786  	if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1787  				   awork->bssid, awork->buf, awork->len,
1788  				   awork->wait_time,
1789  				   wpas_p2p_send_action_tx_status, 1) < 0) {
1790  		os_free(awork);
1791  		radio_work_done(work);
1792  		return;
1793  	}
1794  	wpa_s->p2p_send_action_work = work;
1795  }
1796  
1797  
wpas_send_action_work(struct wpa_supplicant * wpa_s,unsigned int freq,const u8 * dst,const u8 * src,const u8 * bssid,const u8 * buf,size_t len,unsigned int wait_time)1798  static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1799  				 unsigned int freq, const u8 *dst,
1800  				 const u8 *src, const u8 *bssid, const u8 *buf,
1801  				 size_t len, unsigned int wait_time)
1802  {
1803  	struct send_action_work *awork;
1804  
1805  	if (radio_work_pending(wpa_s, "p2p-send-action")) {
1806  		wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1807  		return -1;
1808  	}
1809  
1810  	awork = os_zalloc(sizeof(*awork) + len);
1811  	if (awork == NULL)
1812  		return -1;
1813  
1814  	awork->freq = freq;
1815  	os_memcpy(awork->dst, dst, ETH_ALEN);
1816  	os_memcpy(awork->src, src, ETH_ALEN);
1817  	os_memcpy(awork->bssid, bssid, ETH_ALEN);
1818  	awork->len = len;
1819  	awork->wait_time = wait_time;
1820  	os_memcpy(awork->buf, buf, len);
1821  
1822  	if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
1823  			   wpas_send_action_cb, awork) < 0) {
1824  		os_free(awork);
1825  		return -1;
1826  	}
1827  
1828  	return 0;
1829  }
1830  
1831  
wpas_send_action(void * ctx,unsigned int freq,const u8 * dst,const u8 * src,const u8 * bssid,const u8 * buf,size_t len,unsigned int wait_time,int * scheduled)1832  static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1833  			    const u8 *src, const u8 *bssid, const u8 *buf,
1834  			    size_t len, unsigned int wait_time, int *scheduled)
1835  {
1836  	struct wpa_supplicant *wpa_s = ctx;
1837  	int listen_freq = -1, send_freq = -1;
1838  
1839  	if (scheduled)
1840  		*scheduled = 0;
1841  	if (wpa_s->p2p_listen_work)
1842  		listen_freq = wpa_s->p2p_listen_work->freq;
1843  	if (wpa_s->p2p_send_action_work)
1844  		send_freq = wpa_s->p2p_send_action_work->freq;
1845  	if (listen_freq != (int) freq && send_freq != (int) freq) {
1846  		int res;
1847  
1848  		wpa_printf(MSG_DEBUG,
1849  			   "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u dst="
1850  			   MACSTR " src=" MACSTR " bssid=" MACSTR,
1851  			   listen_freq, send_freq, freq, MAC2STR(dst),
1852  			   MAC2STR(src), MAC2STR(bssid));
1853  		res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1854  					    len, wait_time);
1855  		if (res == 0 && scheduled)
1856  			*scheduled = 1;
1857  		return res;
1858  	}
1859  
1860  	wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1861  	return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1862  				      wait_time,
1863  				      wpas_p2p_send_action_tx_status, 1);
1864  }
1865  
1866  
wpas_send_action_done(void * ctx)1867  static void wpas_send_action_done(void *ctx)
1868  {
1869  	struct wpa_supplicant *wpa_s = ctx;
1870  
1871  	if (wpa_s->p2p_send_action_work) {
1872  		eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1873  				     wpa_s, NULL);
1874  		os_free(wpa_s->p2p_send_action_work->ctx);
1875  		radio_work_done(wpa_s->p2p_send_action_work);
1876  		wpa_s->p2p_send_action_work = NULL;
1877  	}
1878  
1879  	offchannel_send_action_done(wpa_s);
1880  }
1881  
1882  
1883  #ifdef CONFIG_PASN
1884  
1885  struct wpa_p2p_pasn_auth_work {
1886  	u8 peer_addr[ETH_ALEN];
1887  	int freq;
1888  	bool verify;
1889  	int force_freq;
1890  	int pref_freq;
1891  	enum p2p_invite_role role;
1892  	u8 *ssid;
1893  	size_t ssid_len;
1894  	u8 bssid[ETH_ALEN];
1895  	u8 go_dev_addr[ETH_ALEN];
1896  };
1897  
1898  
wpas_p2p_pasn_free_auth_work(struct wpa_p2p_pasn_auth_work * awork)1899  static void wpas_p2p_pasn_free_auth_work(struct wpa_p2p_pasn_auth_work *awork)
1900  {
1901  	if (!awork)
1902  		return;
1903  	os_free(awork->ssid);
1904  	os_free(awork);
1905  }
1906  
1907  
wpas_p2p_pasn_cancel_auth_work(struct wpa_supplicant * wpa_s)1908  static void wpas_p2p_pasn_cancel_auth_work(struct wpa_supplicant *wpa_s)
1909  {
1910  	wpa_printf(MSG_DEBUG, "P2P PASN: Cancel p2p-pasn-start-auth work");
1911  
1912  	/* Remove pending/started work */
1913  	radio_remove_works(wpa_s, "p2p-pasn-start-auth", 0);
1914  }
1915  
1916  
wpas_p2p_pasn_auth_start_cb(struct wpa_radio_work * work,int deinit)1917  static void wpas_p2p_pasn_auth_start_cb(struct wpa_radio_work *work, int deinit)
1918  {
1919  	int ret;
1920  	struct wpa_supplicant *wpa_s = work->wpa_s;
1921  	struct wpa_p2p_pasn_auth_work *awork = work->ctx;
1922  	struct p2p_data *p2p = wpa_s->global->p2p;
1923  	const u8 *peer_addr = NULL;
1924  	const u8 *bssid = NULL;
1925  	const u8 *go_dev_addr = NULL;
1926  
1927  	if (deinit) {
1928  		if (!work->started) {
1929  			eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1930  					     wpa_s->p2pdev, NULL);
1931  		}
1932  		wpas_p2p_pasn_free_auth_work(awork);
1933  		return;
1934  	}
1935  
1936  	if (!is_zero_ether_addr(awork->peer_addr))
1937  		peer_addr = awork->peer_addr;
1938  	if (!is_zero_ether_addr(awork->bssid))
1939  		bssid = awork->bssid;
1940  	if (!is_zero_ether_addr(awork->go_dev_addr))
1941  		go_dev_addr = awork->go_dev_addr;
1942  
1943  
1944  	if (awork->verify)
1945  		ret = p2p_initiate_pasn_verify(p2p, peer_addr, awork->freq,
1946  					       awork->role, bssid, awork->ssid,
1947  					       awork->ssid_len,
1948  					       awork->force_freq, go_dev_addr,
1949  					       awork->pref_freq);
1950  	else
1951  		ret = p2p_initiate_pasn_auth(p2p, peer_addr, awork->freq);
1952  
1953  	if (ret) {
1954  		wpa_printf(MSG_DEBUG,
1955  			   "P2P PASN: Failed to start PASN authentication");
1956  		goto fail;
1957  	}
1958  	eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1959  			     wpa_s->p2pdev, NULL);
1960  	eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
1961  			       wpas_p2p_group_formation_timeout,
1962  			       wpa_s->p2pdev, NULL);
1963  	wpa_s->p2p_pasn_auth_work = work;
1964  	return;
1965  
1966  fail:
1967  	wpas_p2p_pasn_free_auth_work(awork);
1968  	work->ctx = NULL;
1969  	radio_work_done(work);
1970  }
1971  
1972  
wpas_p2p_initiate_pasn_auth(struct wpa_supplicant * wpa_s,const u8 * peer_addr,int freq)1973  static int wpas_p2p_initiate_pasn_auth(struct wpa_supplicant *wpa_s,
1974  				       const u8 *peer_addr, int freq)
1975  {
1976  	struct wpa_p2p_pasn_auth_work *awork;
1977  
1978  	wpas_p2p_pasn_cancel_auth_work(wpa_s);
1979  	wpa_s->p2p_pasn_auth_work = NULL;
1980  
1981  	awork = os_zalloc(sizeof(*awork));
1982  	if (!awork)
1983  		return -1;
1984  
1985  	awork->freq = freq;
1986  	os_memcpy(awork->peer_addr, peer_addr, ETH_ALEN);
1987  
1988  	if (radio_add_work(wpa_s, freq, "p2p-pasn-start-auth", 1,
1989  			   wpas_p2p_pasn_auth_start_cb, awork) < 0) {
1990  		wpas_p2p_pasn_free_auth_work(awork);
1991  		return -1;
1992  	}
1993  
1994  	wpa_printf(MSG_DEBUG,
1995  		   "P2P PASN: Authentication work successfully added");
1996  	return 0;
1997  }
1998  
1999  #endif /* CONFIG_PASN */
2000  
2001  
wpas_copy_go_neg_results(struct wpa_supplicant * wpa_s,struct p2p_go_neg_results * params)2002  static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
2003  				    struct p2p_go_neg_results *params)
2004  {
2005  	if (wpa_s->go_params == NULL) {
2006  		wpa_s->go_params = os_malloc(sizeof(*params));
2007  		if (wpa_s->go_params == NULL)
2008  			return -1;
2009  	}
2010  	os_memcpy(wpa_s->go_params, params, sizeof(*params));
2011  	return 0;
2012  }
2013  
2014  
wpas_start_gc(struct wpa_supplicant * wpa_s,struct p2p_go_neg_results * res)2015  static void wpas_start_gc(struct wpa_supplicant *wpa_s,
2016  			  struct p2p_go_neg_results *res)
2017  {
2018  	struct os_reltime now;
2019  	struct wpa_ssid *ssid;
2020  	struct rsn_pmksa_cache_entry *entry;
2021  
2022  	if (!res->ssid_len) {
2023  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: SSID info not present");
2024  		return;
2025  	}
2026  
2027  	wpa_s->group_formation_reported = 0;
2028  	wpa_printf(MSG_DEBUG, "P2P: Start connect for peer " MACSTR
2029  		   " dev_addr " MACSTR,
2030  		   MAC2STR(res->peer_interface_addr),
2031  		   MAC2STR(res->peer_device_addr));
2032  	wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start connect for SSID",
2033  			  res->ssid, res->ssid_len);
2034  	wpa_supplicant_ap_deinit(wpa_s);
2035  	wpas_copy_go_neg_results(wpa_s, res);
2036  
2037  	ssid = wpa_config_add_network(wpa_s->conf);
2038  	if (!ssid) {
2039  		wpa_dbg(wpa_s, MSG_DEBUG,
2040  			"P2P: Could not add network for client");
2041  		return;
2042  	}
2043  	os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
2044  	wpa_config_set_network_defaults(ssid);
2045  	ssid->temporary = 1;
2046  	ssid->p2p_group = 1;
2047  
2048  	ssid->ssid = os_memdup(res->ssid, res->ssid_len);
2049  	if (!ssid->ssid)
2050  		return;
2051  	ssid->ssid_len = res->ssid_len;
2052  
2053  	os_memcpy(ssid->bssid, res->peer_interface_addr, ETH_ALEN);
2054  
2055  	if (res->akmp == WPA_KEY_MGMT_PASN && res->sae_password[0]) {
2056  		ssid->auth_alg = WPA_AUTH_ALG_SAE;
2057  		ssid->sae_password = os_strdup(res->sae_password);
2058  		if (!ssid->sae_password)
2059  			return;
2060  	} else if (res->akmp == WPA_KEY_MGMT_SAE && res->pmk_len) {
2061  		ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2062  		entry = os_zalloc(sizeof(*entry));
2063  		if (!entry)
2064  			return;
2065  		os_memcpy(entry->aa, res->peer_interface_addr, ETH_ALEN);
2066  		os_memcpy(entry->pmkid, res->pmkid, PMKID_LEN);
2067  		entry->pmk_len = res->pmk_len;
2068  		os_memcpy(entry->pmk, res->pmk, res->pmk_len);
2069  		entry->akmp = res->akmp;
2070  		os_get_reltime(&now);
2071  		entry->expiration = now.sec + 43200;
2072  		entry->reauth_time = now.sec + 43200 * 70 / 100;
2073  		entry->network_ctx = ssid;
2074  		os_memcpy(entry->spa, wpa_s->own_addr, ETH_ALEN);
2075  
2076  		wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
2077  		ssid->pmk_valid = true;
2078  	} else if (res->akmp == WPA_KEY_MGMT_SAE && res->sae_password[0]) {
2079  		ssid->auth_alg = WPA_AUTH_ALG_SAE;
2080  		ssid->sae_password = os_strdup(res->sae_password);
2081  		if (!ssid->sae_password)
2082  			return;
2083  	}
2084  
2085  	if (res->psk_set) {
2086  		os_memcpy(ssid->psk, res->psk, 32);
2087  		ssid->psk_set = 1;
2088  	}
2089  	ssid->proto = WPA_PROTO_RSN;
2090  	ssid->key_mgmt = WPA_KEY_MGMT_SAE;
2091  	ssid->pairwise_cipher = WPA_CIPHER_CCMP;
2092  	ssid->group_cipher = WPA_CIPHER_CCMP;
2093  	if (res->cipher)
2094  		ssid->pairwise_cipher |= res->cipher;
2095  	ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
2096  	ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
2097  	ssid->disabled = 0;
2098  	wpa_s->show_group_started = 1;
2099  	wpa_s->p2p_in_invitation = 1;
2100  	wpa_s->p2p_go_group_formation_completed = 0;
2101  	wpa_s->global->p2p_group_formation = wpa_s;
2102  	ssid->rsn_overriding = RSN_OVERRIDING_ENABLED;
2103  
2104  	wpa_s->current_ssid = ssid;
2105  	wpa_supplicant_update_scan_results(wpa_s, res->peer_interface_addr);
2106  	wpa_supplicant_select_network(wpa_s, ssid);
2107  }
2108  
2109  
wpas_start_wps_enrollee(struct wpa_supplicant * wpa_s,struct p2p_go_neg_results * res)2110  static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
2111  				    struct p2p_go_neg_results *res)
2112  {
2113  	wpa_s->group_formation_reported = 0;
2114  	wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
2115  		   " dev_addr " MACSTR " wps_method %d",
2116  		   MAC2STR(res->peer_interface_addr),
2117  		   MAC2STR(res->peer_device_addr), res->wps_method);
2118  	wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
2119  			  res->ssid, res->ssid_len);
2120  	wpa_supplicant_ap_deinit(wpa_s);
2121  	wpas_copy_go_neg_results(wpa_s, res);
2122  	if (res->wps_method == WPS_PBC) {
2123  		wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
2124  #ifdef CONFIG_WPS_NFC
2125  	} else if (res->wps_method == WPS_NFC) {
2126  		wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
2127  				   res->peer_interface_addr,
2128  				   wpa_s->p2pdev->p2p_oob_dev_pw,
2129  				   wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
2130  				   wpa_s->p2pdev->p2p_oob_dev_pw_id ==
2131  				   DEV_PW_NFC_CONNECTION_HANDOVER ?
2132  				   wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
2133  				   NULL,
2134  				   NULL, 0, 0);
2135  #endif /* CONFIG_WPS_NFC */
2136  	} else {
2137  		u16 dev_pw_id = DEV_PW_DEFAULT;
2138  		if (wpa_s->p2p_wps_method == WPS_P2PS)
2139  			dev_pw_id = DEV_PW_P2PS_DEFAULT;
2140  		if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
2141  			dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
2142  		wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
2143  				   wpa_s->p2p_pin, 1, dev_pw_id);
2144  	}
2145  }
2146  
2147  
wpas_p2p_add_psk_list(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)2148  static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
2149  				  struct wpa_ssid *ssid)
2150  {
2151  	struct wpa_ssid *persistent;
2152  	struct psk_list_entry *psk;
2153  	struct hostapd_data *hapd;
2154  
2155  	if (!wpa_s->ap_iface)
2156  		return;
2157  
2158  	persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
2159  					     ssid->ssid_len);
2160  	if (persistent == NULL)
2161  		return;
2162  
2163  	hapd = wpa_s->ap_iface->bss[0];
2164  
2165  	dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
2166  			 list) {
2167  		struct hostapd_wpa_psk *hpsk;
2168  
2169  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
2170  			MACSTR " psk=%d",
2171  			MAC2STR(psk->addr), psk->p2p);
2172  		hpsk = os_zalloc(sizeof(*hpsk));
2173  		if (hpsk == NULL)
2174  			break;
2175  		os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
2176  		if (psk->p2p)
2177  			os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
2178  		else
2179  			os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
2180  		hpsk->next = hapd->conf->ssid.wpa_psk;
2181  		hapd->conf->ssid.wpa_psk = hpsk;
2182  	}
2183  }
2184  
2185  
p2p_go_dump_common_freqs(struct wpa_supplicant * wpa_s)2186  static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
2187  {
2188  	char buf[20 + P2P_MAX_CHANNELS * 6];
2189  	char *pos, *end;
2190  	unsigned int i;
2191  	int res;
2192  
2193  	pos = buf;
2194  	end = pos + sizeof(buf);
2195  	for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2196  		res = os_snprintf(pos, end - pos, " %d",
2197  				  wpa_s->p2p_group_common_freqs[i]);
2198  		if (os_snprintf_error(end - pos, res))
2199  			break;
2200  		pos += res;
2201  	}
2202  	*pos = '\0';
2203  
2204  	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
2205  }
2206  
2207  
p2p_go_save_group_common_freqs(struct wpa_supplicant * wpa_s,struct p2p_go_neg_results * params)2208  static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
2209  					   struct p2p_go_neg_results *params)
2210  {
2211  	unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
2212  
2213  	wpa_s->p2p_group_common_freqs_num = 0;
2214  	os_free(wpa_s->p2p_group_common_freqs);
2215  	wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
2216  	if (!wpa_s->p2p_group_common_freqs)
2217  		return;
2218  
2219  	for (i = 0; i < len; i++) {
2220  		if (!wpa_s->go_params->freq_list[i])
2221  			break;
2222  		wpa_s->p2p_group_common_freqs[i] =
2223  			wpa_s->go_params->freq_list[i];
2224  	}
2225  	wpa_s->p2p_group_common_freqs_num = i;
2226  }
2227  
2228  
p2p_config_write(struct wpa_supplicant * wpa_s)2229  static void p2p_config_write(struct wpa_supplicant *wpa_s)
2230  {
2231  #ifndef CONFIG_NO_CONFIG_WRITE
2232  	if (wpa_s->p2pdev->conf->update_config &&
2233  	    wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
2234  		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2235  #endif /* CONFIG_NO_CONFIG_WRITE */
2236  }
2237  
2238  
p2p_go_configured(void * ctx,void * data)2239  static void p2p_go_configured(void *ctx, void *data)
2240  {
2241  	struct wpa_supplicant *wpa_s = ctx;
2242  	struct p2p_go_neg_results *params = data;
2243  	struct wpa_ssid *ssid;
2244  
2245  	wpa_s->ap_configured_cb = NULL;
2246  	wpa_s->ap_configured_cb_ctx = NULL;
2247  	wpa_s->ap_configured_cb_data = NULL;
2248  	if (!wpa_s->go_params) {
2249  		wpa_printf(MSG_ERROR,
2250  			   "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
2251  		return;
2252  	}
2253  
2254  	if (wpa_s->ap_iface && params->p2p2 &&
2255  	    params->akmp == WPA_KEY_MGMT_SAE) {
2256  		struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
2257  
2258  		wpa_auth_pmksa_add_sae(hapd->wpa_auth,
2259  				       params->peer_device_addr,
2260  				       params->pmk, params->pmk_len,
2261  				       params->pmkid, WPA_KEY_MGMT_SAE,
2262  				       false);
2263  		hostapd_add_pmkid(hapd, params->peer_device_addr,
2264  				  params->pmk, params->pmk_len,
2265  				  params->pmkid, WPA_KEY_MGMT_SAE);
2266  	}
2267  
2268  	p2p_go_save_group_common_freqs(wpa_s, params);
2269  	p2p_go_dump_common_freqs(wpa_s);
2270  
2271  	ssid = wpa_s->current_ssid;
2272  	if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
2273  		wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
2274  		if (wpa_s->global->p2p_group_formation == wpa_s)
2275  			wpa_s->global->p2p_group_formation = NULL;
2276  		wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
2277  				       params->passphrase[0] == '\0' ?
2278  				       params->psk : NULL,
2279  				       params->passphrase,
2280  				       wpa_s->global->p2p_dev_addr,
2281  				       params->persistent_group, "");
2282  		wpa_s->group_formation_reported = 1;
2283  
2284  		if (wpa_s->p2pdev->p2ps_method_config_any) {
2285  			if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
2286  				wpa_dbg(wpa_s, MSG_DEBUG,
2287  					"P2PS: Setting default PIN for ANY");
2288  				wpa_supplicant_ap_wps_pin(wpa_s, NULL,
2289  							  "12345670", NULL, 0,
2290  							  0);
2291  			} else {
2292  				wpa_dbg(wpa_s, MSG_DEBUG,
2293  					"P2PS: Setting default PIN for " MACSTR,
2294  					MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
2295  				wpa_supplicant_ap_wps_pin(
2296  					wpa_s, wpa_s->p2pdev->p2ps_join_addr,
2297  					"12345670", NULL, 0, 0);
2298  			}
2299  			wpa_s->p2pdev->p2ps_method_config_any = 0;
2300  		}
2301  
2302  		os_get_reltime(&wpa_s->global->p2p_go_wait_client);
2303  		if (params->persistent_group) {
2304  			wpas_p2p_store_persistent_group(
2305  				wpa_s->p2pdev, ssid,
2306  				wpa_s->global->p2p_dev_addr, 0);
2307  			wpas_p2p_add_psk_list(wpa_s, ssid);
2308  		}
2309  
2310  		wpas_notify_p2p_group_started(wpa_s, ssid,
2311  					      params->persistent_group, 0,
2312  					      NULL);
2313  		wpas_p2p_cross_connect_setup(wpa_s);
2314  		wpas_p2p_set_group_idle_timeout(wpa_s);
2315  
2316  		if (wpa_s->p2p_first_connection_timeout) {
2317  			wpa_dbg(wpa_s, MSG_DEBUG,
2318  				"P2P: Start group formation timeout of %d seconds until first data connection on GO",
2319  				wpa_s->p2p_first_connection_timeout);
2320  			wpa_s->p2p_go_group_formation_completed = 0;
2321  			wpa_s->global->p2p_group_formation = wpa_s;
2322  			eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2323  					     wpa_s->p2pdev, NULL);
2324  			eloop_register_timeout(
2325  				wpa_s->p2p_first_connection_timeout, 0,
2326  				wpas_p2p_group_formation_timeout,
2327  				wpa_s->p2pdev, NULL);
2328  		}
2329  
2330  		return;
2331  	}
2332  
2333  	if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
2334  					      params->peer_interface_addr)) {
2335  		wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
2336  			   "filtering");
2337  		return;
2338  	}
2339  
2340  	if (params->p2p2) {
2341  		wpas_group_formation_completed(wpa_s, 0, NULL);
2342  		wpa_printf(MSG_DEBUG,
2343  			   "P2P2: Group formation completed - first connection in progress");
2344  		goto out;
2345  	}
2346  
2347  	wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
2348  	if (params->wps_method == WPS_PBC) {
2349  		wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
2350  					  params->peer_device_addr);
2351  #ifdef CONFIG_WPS_NFC
2352  	} else if (params->wps_method == WPS_NFC) {
2353  		if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
2354  		    DEV_PW_NFC_CONNECTION_HANDOVER &&
2355  		    !wpa_s->p2pdev->p2p_oob_dev_pw) {
2356  			wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
2357  			return;
2358  		}
2359  		wpas_ap_wps_add_nfc_pw(
2360  			wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
2361  			wpa_s->p2pdev->p2p_oob_dev_pw,
2362  			wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
2363  			wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
2364  #endif /* CONFIG_WPS_NFC */
2365  	} else if (wpa_s->p2p_pin[0])
2366  		wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
2367  					  wpa_s->p2p_pin, NULL, 0, 0);
2368  out:
2369  	os_free(wpa_s->go_params);
2370  	wpa_s->go_params = NULL;
2371  }
2372  
2373  
2374  /**
2375   * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
2376   * @freq: Frequency (MHz) to convert
2377   * @op_class: Buffer for returning operating class
2378   * @op_edmg_channel: Buffer for returning channel number
2379   * Returns: 0 on success, -1 on failure
2380   *
2381   * This can be used to find the highest channel bonding which includes the
2382   * specified frequency.
2383   */
wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant * wpa_s,unsigned int freq,u8 * op_class,u8 * op_edmg_channel)2384  static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
2385  					 unsigned int freq,
2386  					 u8 *op_class, u8 *op_edmg_channel)
2387  {
2388  	struct hostapd_hw_modes *hwmode;
2389  	struct ieee80211_edmg_config edmg;
2390  	unsigned int i;
2391  	enum chan_width chanwidth[] = {
2392  		CHAN_WIDTH_8640,
2393  		CHAN_WIDTH_6480,
2394  		CHAN_WIDTH_4320,
2395  	};
2396  
2397  	if (!wpa_s->hw.modes)
2398  		return -1;
2399  
2400  	hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
2401  			  HOSTAPD_MODE_IEEE80211AD, false);
2402  	if (!hwmode) {
2403  		wpa_printf(MSG_ERROR,
2404  			   "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
2405  		return -1;
2406  	}
2407  
2408  	/* Find the highest EDMG channel bandwidth to start the P2P GO */
2409  	for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
2410  		if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
2411  						  op_class,
2412  						  op_edmg_channel) < 0)
2413  			continue;
2414  
2415  		hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
2416  		if (edmg.channels &&
2417  		    ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
2418  			wpa_printf(MSG_DEBUG,
2419  				   "Freq %u to EDMG channel %u at opclass %u",
2420  				   freq, *op_edmg_channel, *op_class);
2421  			return 0;
2422  		}
2423  	}
2424  
2425  	return -1;
2426  }
2427  
2428  
wpas_p2p_try_edmg_channel(struct wpa_supplicant * wpa_s,struct p2p_go_neg_results * params)2429  int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
2430  			      struct p2p_go_neg_results *params)
2431  {
2432  	u8 op_channel, op_class;
2433  	int freq;
2434  
2435  	/* Try social channel as primary channel frequency */
2436  	freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
2437  
2438  	if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
2439  					  &op_channel) == 0) {
2440  		wpa_printf(MSG_DEBUG,
2441  			   "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2442  			   freq, op_channel, op_class);
2443  		params->freq = freq;
2444  		return 0;
2445  	}
2446  
2447  	return -1;
2448  }
2449  
2450  
wpas_start_go(struct wpa_supplicant * wpa_s,struct p2p_go_neg_results * params,int group_formation,enum wpa_p2p_mode p2p_mode)2451  static void wpas_start_go(struct wpa_supplicant *wpa_s,
2452  			  struct p2p_go_neg_results *params,
2453  			  int group_formation, enum wpa_p2p_mode p2p_mode)
2454  {
2455  	struct wpa_ssid *ssid;
2456  
2457  	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2458  	if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2459  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2460  			"results");
2461  		return;
2462  	}
2463  
2464  	ssid = wpa_config_add_network(wpa_s->conf);
2465  	if (ssid == NULL) {
2466  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
2467  		return;
2468  	}
2469  
2470  	wpa_s->show_group_started = 0;
2471  	wpa_s->p2p_go_group_formation_completed = 0;
2472  	wpa_s->group_formation_reported = 0;
2473  	os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
2474  
2475  	wpa_config_set_network_defaults(ssid);
2476  	ssid->temporary = 1;
2477  	ssid->p2p_group = 1;
2478  	ssid->p2p_persistent_group = !!params->persistent_group;
2479  	ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2480  		WPAS_MODE_P2P_GO;
2481  	ssid->frequency = params->freq;
2482  	ssid->ht40 = params->ht40;
2483  	ssid->vht = params->vht;
2484  	ssid->max_oper_chwidth = params->max_oper_chwidth;
2485  	ssid->vht_center_freq2 = params->vht_center_freq2;
2486  	ssid->he = params->he;
2487  	if (params->edmg) {
2488  		u8 op_channel, op_class;
2489  
2490  		if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2491  						   &op_class, &op_channel)) {
2492  			ssid->edmg_channel = op_channel;
2493  			ssid->enable_edmg = params->edmg;
2494  		} else {
2495  			wpa_dbg(wpa_s, MSG_DEBUG,
2496  				"P2P: Could not match EDMG channel, freq %d, for GO",
2497  				params->freq);
2498  		}
2499  	}
2500  
2501  	ssid->ssid = os_zalloc(params->ssid_len + 1);
2502  	if (ssid->ssid) {
2503  		os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2504  		ssid->ssid_len = params->ssid_len;
2505  	}
2506  	ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2507  	ssid->key_mgmt = WPA_KEY_MGMT_PSK;
2508  	if (is_6ghz_freq(ssid->frequency) &&
2509  	    is_p2p_6ghz_capable(wpa_s->global->p2p)) {
2510  		ssid->auth_alg |= WPA_AUTH_ALG_SAE;
2511  		ssid->key_mgmt = WPA_KEY_MGMT_SAE;
2512  		ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
2513  		ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
2514  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use SAE auth_alg and key_mgmt");
2515  	} else {
2516  		p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
2517  	}
2518  	ssid->proto = WPA_PROTO_RSN;
2519  	ssid->pairwise_cipher = WPA_CIPHER_CCMP;
2520  	ssid->group_cipher = WPA_CIPHER_CCMP;
2521  	if (params->freq > 56160) {
2522  		/*
2523  		 * Enable GCMP instead of CCMP as pairwise_cipher and
2524  		 * group_cipher in 60 GHz.
2525  		 */
2526  		ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2527  		ssid->group_cipher = WPA_CIPHER_GCMP;
2528  		/* P2P GO in 60 GHz is always a PCP (PBSS) */
2529  		ssid->pbss = 1;
2530  	}
2531  	if (os_strlen(params->passphrase) > 0) {
2532  		ssid->passphrase = os_strdup(params->passphrase);
2533  		if (ssid->passphrase == NULL) {
2534  			wpa_msg_global(wpa_s, MSG_ERROR,
2535  				       "P2P: Failed to copy passphrase for GO");
2536  			wpa_config_remove_network(wpa_s->conf, ssid->id);
2537  			return;
2538  		}
2539  	} else
2540  		ssid->passphrase = NULL;
2541  	ssid->psk_set = params->psk_set;
2542  	if (ssid->psk_set)
2543  		os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
2544  	else if (ssid->passphrase)
2545  		wpa_config_update_psk(ssid);
2546  	ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
2547  
2548  	ssid->p2p_mode = p2p_mode;
2549  	if (params->p2p2) {
2550  		if (params->akmp == WPA_KEY_MGMT_SAE)
2551  			ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2552  		else
2553  			ssid->auth_alg |= WPA_AUTH_ALG_SAE;
2554  
2555  		ssid->key_mgmt = WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_PASN;
2556  		ssid->sae_password = os_strdup(params->sae_password);
2557  		/* In PCC, RSNE indicates PMF to be disabled while RSNOE/RSNO2E
2558  		 * requires PMF for SAE. */
2559  		if (ssid->p2p_mode != WPA_P2P_MODE_WFD_PCC)
2560  			ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
2561  		ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
2562  		if (params->cipher)
2563  			ssid->pairwise_cipher |= params->cipher;
2564  	}
2565  
2566  	wpa_s->ap_configured_cb = p2p_go_configured;
2567  	wpa_s->ap_configured_cb_ctx = wpa_s;
2568  	wpa_s->ap_configured_cb_data = wpa_s->go_params;
2569  	wpa_s->scan_req = NORMAL_SCAN_REQ;
2570  	wpa_s->connect_without_scan = ssid;
2571  	wpa_s->reassociate = 1;
2572  	wpa_s->disconnected = 0;
2573  	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2574  		"start GO)");
2575  	wpa_supplicant_req_scan(wpa_s, 0, 0);
2576  }
2577  
2578  
wpas_p2p_clone_config(struct wpa_supplicant * dst,const struct wpa_supplicant * src)2579  static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2580  				  const struct wpa_supplicant *src)
2581  {
2582  	struct wpa_config *d;
2583  	const struct wpa_config *s;
2584  
2585  	d = dst->conf;
2586  	s = src->conf;
2587  
2588  #define C(n)                            \
2589  do {                                    \
2590  	if (s->n && !d->n)              \
2591  		d->n = os_strdup(s->n); \
2592  } while (0)
2593  
2594  	C(device_name);
2595  	C(manufacturer);
2596  	C(model_name);
2597  	C(model_number);
2598  	C(serial_number);
2599  	C(config_methods);
2600  #undef C
2601  
2602  	os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2603  	os_memcpy(d->sec_device_type, s->sec_device_type,
2604  		  sizeof(d->sec_device_type));
2605  	d->num_sec_device_types = s->num_sec_device_types;
2606  
2607  	d->p2p_group_idle = s->p2p_group_idle;
2608  	d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
2609  	d->p2p_intra_bss = s->p2p_intra_bss;
2610  	d->persistent_reconnect = s->persistent_reconnect;
2611  	d->max_num_sta = s->max_num_sta;
2612  	d->pbc_in_m1 = s->pbc_in_m1;
2613  	d->ignore_old_scan_res = s->ignore_old_scan_res;
2614  	d->beacon_int = s->beacon_int;
2615  	d->dtim_period = s->dtim_period;
2616  	d->p2p_go_ctwindow = s->p2p_go_ctwindow;
2617  	d->disassoc_low_ack = s->disassoc_low_ack;
2618  	d->disable_scan_offload = s->disable_scan_offload;
2619  	d->passive_scan = s->passive_scan;
2620  	d->pmf = s->pmf;
2621  	d->p2p_6ghz_disable = s->p2p_6ghz_disable;
2622  	d->sae_pwe = s->sae_pwe;
2623  
2624  	if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2625  	    !d->wps_nfc_pw_from_config) {
2626  		wpabuf_free(d->wps_nfc_dh_privkey);
2627  		wpabuf_free(d->wps_nfc_dh_pubkey);
2628  		d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2629  		d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2630  	}
2631  	d->p2p_cli_probe = s->p2p_cli_probe;
2632  	d->go_interworking = s->go_interworking;
2633  	d->go_access_network_type = s->go_access_network_type;
2634  	d->go_internet = s->go_internet;
2635  	d->go_venue_group = s->go_venue_group;
2636  	d->go_venue_type = s->go_venue_type;
2637  	d->p2p_add_cli_chan = s->p2p_add_cli_chan;
2638  }
2639  
2640  
wpas_p2p_get_group_ifname(struct wpa_supplicant * wpa_s,char * ifname,size_t len)2641  static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2642  				      char *ifname, size_t len)
2643  {
2644  	char *ifname_ptr = wpa_s->ifname;
2645  
2646  	if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2647  		       os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2648  		ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2649  	}
2650  
2651  	os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2652  	if (os_strlen(ifname) >= IFNAMSIZ &&
2653  	    os_strlen(wpa_s->ifname) < IFNAMSIZ) {
2654  		int res;
2655  
2656  		/* Try to avoid going over the IFNAMSIZ length limit */
2657  		res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2658  		if (os_snprintf_error(len, res) && len)
2659  			ifname[len - 1] = '\0';
2660  	}
2661  }
2662  
2663  
wpas_p2p_add_group_interface(struct wpa_supplicant * wpa_s,enum wpa_driver_if_type type)2664  static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2665  					enum wpa_driver_if_type type)
2666  {
2667  	char ifname[120], force_ifname[120];
2668  
2669  	if (wpa_s->pending_interface_name[0]) {
2670  		wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2671  			   "- skip creation of a new one");
2672  		if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2673  			wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2674  				   "unknown?! ifname='%s'",
2675  				   wpa_s->pending_interface_name);
2676  			return -1;
2677  		}
2678  		return 0;
2679  	}
2680  
2681  	wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
2682  	force_ifname[0] = '\0';
2683  
2684  	wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2685  		   ifname);
2686  	wpa_s->p2p_group_idx++;
2687  
2688  	wpa_s->pending_interface_type = type;
2689  	if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2690  			   wpa_s->pending_interface_addr, NULL) < 0) {
2691  		wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2692  			   "interface");
2693  		return -1;
2694  	}
2695  
2696  	if (wpa_s->conf->p2p_interface_random_mac_addr) {
2697  		random_mac_addr(wpa_s->pending_interface_addr);
2698  		wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2699  			   " for the group",
2700  			   MAC2STR(wpa_s->pending_interface_addr));
2701  	}
2702  
2703  	if (force_ifname[0]) {
2704  		wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2705  			   force_ifname);
2706  		os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2707  			   sizeof(wpa_s->pending_interface_name));
2708  	} else
2709  		os_strlcpy(wpa_s->pending_interface_name, ifname,
2710  			   sizeof(wpa_s->pending_interface_name));
2711  	wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2712  		   MACSTR, wpa_s->pending_interface_name,
2713  		   MAC2STR(wpa_s->pending_interface_addr));
2714  
2715  	return 0;
2716  }
2717  
2718  
wpas_p2p_remove_pending_group_interface(struct wpa_supplicant * wpa_s)2719  static void wpas_p2p_remove_pending_group_interface(
2720  	struct wpa_supplicant *wpa_s)
2721  {
2722  	if (!wpa_s->pending_interface_name[0] ||
2723  	    is_zero_ether_addr(wpa_s->pending_interface_addr))
2724  		return; /* No pending virtual interface */
2725  
2726  	wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2727  		   wpa_s->pending_interface_name);
2728  	wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2729  			  wpa_s->pending_interface_name);
2730  	os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2731  	wpa_s->pending_interface_name[0] = '\0';
2732  	wpa_s->global->pending_group_iface_for_p2ps = 0;
2733  }
2734  
2735  
2736  static struct wpa_supplicant *
wpas_p2p_init_group_interface(struct wpa_supplicant * wpa_s,int go)2737  wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2738  {
2739  	struct wpa_interface iface;
2740  	struct wpa_supplicant *group_wpa_s;
2741  
2742  	if (!wpa_s->pending_interface_name[0]) {
2743  		wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2744  		if (!wpas_p2p_create_iface(wpa_s))
2745  			return NULL;
2746  		/*
2747  		 * Something has forced us to remove the pending interface; try
2748  		 * to create a new one and hope for the best that we will get
2749  		 * the same local address.
2750  		 */
2751  		if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2752  						 WPA_IF_P2P_CLIENT) < 0)
2753  			return NULL;
2754  	}
2755  
2756  	os_memset(&iface, 0, sizeof(iface));
2757  	iface.ifname = wpa_s->pending_interface_name;
2758  	iface.driver = wpa_s->driver->name;
2759  	if (wpa_s->conf->ctrl_interface == NULL &&
2760  	    wpa_s->parent != wpa_s &&
2761  	    wpa_s->p2p_mgmt &&
2762  	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2763  		iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2764  	else
2765  		iface.ctrl_interface = wpa_s->conf->ctrl_interface;
2766  	iface.driver_param = wpa_s->conf->driver_param;
2767  	group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
2768  	if (group_wpa_s == NULL) {
2769  		wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2770  			   "wpa_supplicant interface");
2771  		return NULL;
2772  	}
2773  	wpa_s->pending_interface_name[0] = '\0';
2774  	group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2775  		P2P_GROUP_INTERFACE_CLIENT;
2776  	wpa_s->global->p2p_group_formation = group_wpa_s;
2777  	wpa_s->global->pending_group_iface_for_p2ps = 0;
2778  
2779  	wpas_p2p_clone_config(group_wpa_s, wpa_s);
2780  	group_wpa_s->p2p2 = wpa_s->p2p2;
2781  
2782  	if (wpa_s->conf->p2p_interface_random_mac_addr) {
2783  		if (wpa_drv_set_mac_addr(group_wpa_s,
2784  					 wpa_s->pending_interface_addr) < 0) {
2785  			wpa_msg(group_wpa_s, MSG_INFO,
2786  				"Failed to set random MAC address");
2787  			wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2788  						    0);
2789  			return NULL;
2790  		}
2791  
2792  		if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2793  			wpa_msg(group_wpa_s, MSG_INFO,
2794  				"Could not update MAC address information");
2795  			wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2796  						    0);
2797  			return NULL;
2798  		}
2799  
2800  		wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2801  			   " for the group",
2802  			   MAC2STR(wpa_s->pending_interface_addr));
2803  	}
2804  
2805  	return group_wpa_s;
2806  }
2807  
2808  
wpas_p2p_group_formation_timeout(void * eloop_ctx,void * timeout_ctx)2809  static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2810  					     void *timeout_ctx)
2811  {
2812  	struct wpa_supplicant *wpa_s = eloop_ctx;
2813  
2814  #ifdef CONFIG_PASN
2815  	if (wpa_s->p2p_pasn_auth_work) {
2816  		wpas_p2p_pasn_cancel_auth_work(wpa_s);
2817  		wpa_s->p2p_pasn_auth_work = NULL;
2818  	}
2819  #endif /* CONFIG_PASN */
2820  
2821  	wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
2822  	wpas_p2p_group_formation_failed(wpa_s, 0, "Group formation timed out");
2823  }
2824  
2825  
wpas_p2p_group_formation_failed(struct wpa_supplicant * wpa_s,int already_deleted,const char * reason)2826  static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2827  					    int already_deleted,
2828  					    const char *reason)
2829  {
2830  	 /* reason == NULL would indicate success in
2831  	  * wpas_group_formation_completed(), so make sure that is not the case
2832  	  * here. */
2833  	if (!reason)
2834  		reason = "";
2835  
2836  	eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2837  			     wpa_s->p2pdev, NULL);
2838  	if (wpa_s->global->p2p)
2839  		p2p_group_formation_failed(wpa_s->global->p2p);
2840  	wpas_group_formation_completed(wpa_s, already_deleted, reason);
2841  }
2842  
2843  
wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant * wpa_s)2844  static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2845  {
2846  	wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2847  	eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2848  			     wpa_s->p2pdev, NULL);
2849  	eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2850  			       wpa_s->p2pdev, NULL);
2851  	wpa_s->global->p2p_fail_on_wps_complete = 0;
2852  }
2853  
2854  
wpas_p2p_ap_setup_failed(struct wpa_supplicant * wpa_s)2855  void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2856  {
2857  	if (wpa_s->global->p2p_group_formation != wpa_s)
2858  		return;
2859  	/* Speed up group formation timeout since this cannot succeed */
2860  	eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2861  			     wpa_s->p2pdev, NULL);
2862  	eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2863  			       wpa_s->p2pdev, NULL);
2864  }
2865  
2866  
wpas_p2p_retry_limit_exceeded(struct wpa_supplicant * wpa_s)2867  bool wpas_p2p_retry_limit_exceeded(struct wpa_supplicant *wpa_s)
2868  {
2869  	if (!wpa_s->p2p_in_invitation || !wpa_s->p2p_retry_limit ||
2870  	    wpa_s->p2p_in_invitation <= wpa_s->p2p_retry_limit)
2871  		return false;
2872  
2873  	wpa_printf(MSG_DEBUG, "P2P: Group join retry limit exceeded");
2874  	eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2875  			     wpa_s->p2pdev, NULL);
2876  	eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2877  			       wpa_s->p2pdev, NULL);
2878  	return true;
2879  }
2880  
2881  
wpas_set_go_security_config(void * ctx,struct p2p_go_neg_results * params)2882  static void wpas_set_go_security_config(void *ctx,
2883  					struct p2p_go_neg_results *params)
2884  {
2885  	struct wpa_supplicant *wpa_s = ctx;
2886  	struct wpa_supplicant *tmp, *ifs = NULL;
2887  	struct hostapd_data *hapd;
2888  
2889  	if (!params->p2p2)
2890  		return;
2891  
2892  	dl_list_for_each(tmp, &wpa_s->radio->ifaces, struct wpa_supplicant,
2893  			 radio_list) {
2894  		struct wpa_ssid *ssid = tmp->current_ssid;
2895  
2896  		if (ssid && ssid->mode == WPAS_MODE_P2P_GO &&
2897  		    ssid->ssid && ssid->ssid_len == params->ssid_len &&
2898  		    os_memcmp(ssid->ssid, params->ssid, params->ssid_len) == 0)
2899  		{
2900  			ifs = tmp;
2901  			break;
2902  		}
2903  	}
2904  
2905  	if (!ifs || !ifs->ap_iface)
2906  		return;
2907  
2908  	hapd = ifs->ap_iface->bss[0];
2909  	hapd->conf->wps_state = 0;
2910  
2911  	if (params->akmp == WPA_KEY_MGMT_SAE) {
2912  		wpa_printf(MSG_DEBUG, "P2P: Adding PMK for peer: " MACSTR,
2913  			   MAC2STR(params->peer_device_addr));
2914  		wpa_auth_pmksa_add_sae(hapd->wpa_auth,
2915  				       params->peer_device_addr,
2916  				       params->pmk, params->pmk_len,
2917  				       params->pmkid, WPA_KEY_MGMT_SAE,
2918  				       false);
2919  		hostapd_add_pmkid(hapd, params->peer_device_addr,
2920  				  params->pmk, params->pmk_len,
2921  				  params->pmkid, WPA_KEY_MGMT_SAE);
2922  	}
2923  }
2924  
2925  
wpas_go_neg_completed(void * ctx,struct p2p_go_neg_results * res)2926  static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
2927  {
2928  	struct wpa_supplicant *wpa_s = ctx;
2929  	struct wpa_supplicant *group_wpa_s;
2930  
2931  	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2932  		wpa_drv_cancel_remain_on_channel(wpa_s);
2933  		wpa_s->off_channel_freq = 0;
2934  		wpa_s->roc_waiting_drv_freq = 0;
2935  	}
2936  
2937  #ifdef CONFIG_PASN
2938  	if (wpa_s->p2p_pasn_auth_work) {
2939  		wpas_p2p_pasn_cancel_auth_work(wpa_s);
2940  		wpa_s->p2p_pasn_auth_work = NULL;
2941  	}
2942  #endif /* CONFIG_PASN */
2943  
2944  	if (res->status) {
2945  		wpa_msg_global(wpa_s, MSG_INFO,
2946  			       P2P_EVENT_GO_NEG_FAILURE "status=%d",
2947  			       res->status);
2948  		wpas_notify_p2p_go_neg_completed(wpa_s, res);
2949  		wpas_p2p_remove_pending_group_interface(wpa_s);
2950  		return;
2951  	}
2952  
2953  	if (!res->role_go) {
2954  		/* Inform driver of the operating channel of GO. */
2955  		wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2956  	}
2957  
2958  	if (wpa_s->p2p_go_ht40)
2959  		res->ht40 = 1;
2960  	if (wpa_s->p2p_go_vht)
2961  		res->vht = 1;
2962  	if (wpa_s->p2p_go_he)
2963  		res->he = 1;
2964  	if (wpa_s->p2p_go_edmg)
2965  		res->edmg = 1;
2966  	res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2967  	res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
2968  
2969  	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2970  		       "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2971  		       " wps_method=%s",
2972  		       res->role_go ? "GO" : "client", res->freq, res->ht40,
2973  		       MAC2STR(res->peer_device_addr),
2974  		       MAC2STR(res->peer_interface_addr),
2975  		       p2p_wps_method_text(res->wps_method));
2976  	wpas_notify_p2p_go_neg_completed(wpa_s, res);
2977  
2978  	if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2979  		struct wpa_ssid *ssid;
2980  		ssid = wpa_config_get_network(wpa_s->conf,
2981  					      wpa_s->p2p_persistent_id);
2982  		if (ssid && ssid->disabled == 2 &&
2983  		    ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2984  			size_t len = os_strlen(ssid->passphrase);
2985  			wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2986  				   "on requested persistent group");
2987  			os_memcpy(res->passphrase, ssid->passphrase, len);
2988  			res->passphrase[len] = '\0';
2989  		}
2990  	}
2991  
2992  	if (wpa_s->create_p2p_iface) {
2993  		group_wpa_s =
2994  			wpas_p2p_init_group_interface(wpa_s, res->role_go);
2995  		if (group_wpa_s == NULL) {
2996  			wpas_p2p_remove_pending_group_interface(wpa_s);
2997  			eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2998  					     wpa_s, NULL);
2999  			wpas_p2p_group_formation_failed(wpa_s, 1,
3000  							"Could not initialize group interface");
3001  			return;
3002  		}
3003  		os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
3004  		wpa_s->pending_interface_name[0] = '\0';
3005  	} else {
3006  		group_wpa_s = wpa_s->parent;
3007  		wpa_s->global->p2p_group_formation = group_wpa_s;
3008  		if (group_wpa_s != wpa_s)
3009  			wpas_p2p_clone_config(group_wpa_s, wpa_s);
3010  	}
3011  
3012  	group_wpa_s->p2p_in_provisioning = 1;
3013  	group_wpa_s->p2pdev = wpa_s;
3014  	if (group_wpa_s != wpa_s) {
3015  		os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
3016  			  sizeof(group_wpa_s->p2p_pin));
3017  		group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
3018  		group_wpa_s->p2p2 = res->p2p2;
3019  		group_wpa_s->p2p_bootstrap = wpa_s->p2p_bootstrap;
3020  	}
3021  
3022  	if (res->role_go) {
3023  		wpas_start_go(group_wpa_s, res, 1, group_wpa_s->p2p_mode);
3024  	} else {
3025  		os_get_reltime(&group_wpa_s->scan_min_time);
3026  		if (res->p2p2)
3027  			wpas_start_gc(group_wpa_s, res);
3028  		else
3029  			wpas_start_wps_enrollee(group_wpa_s, res);
3030  	}
3031  
3032  	wpa_s->global->p2p_long_listen = 0;
3033  	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3034  
3035  	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3036  	eloop_register_timeout(15 + res->peer_config_timeout / 100,
3037  			       (res->peer_config_timeout % 100) * 10000,
3038  			       wpas_p2p_group_formation_timeout, wpa_s, NULL);
3039  }
3040  
3041  
wpas_go_neg_req_rx(void * ctx,const u8 * src,u16 dev_passwd_id,u8 go_intent)3042  static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
3043  			       u8 go_intent)
3044  {
3045  	struct wpa_supplicant *wpa_s = ctx;
3046  	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
3047  		       " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
3048  		       dev_passwd_id, go_intent);
3049  
3050  	wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
3051  }
3052  
3053  
wpas_dev_found(void * ctx,const u8 * addr,const struct p2p_peer_info * info,int new_device)3054  static void wpas_dev_found(void *ctx, const u8 *addr,
3055  			   const struct p2p_peer_info *info,
3056  			   int new_device)
3057  {
3058  #ifndef CONFIG_NO_STDOUT_DEBUG
3059  	struct wpa_supplicant *wpa_s = ctx;
3060  	char devtype[WPS_DEV_TYPE_BUFSIZE];
3061  	char *wfd_dev_info_hex = NULL;
3062  
3063  #ifdef CONFIG_WIFI_DISPLAY
3064  	wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
3065  						    WFD_SUBELEM_DEVICE_INFO);
3066  #endif /* CONFIG_WIFI_DISPLAY */
3067  
3068  	if (info->p2ps_instance) {
3069  		char str[256];
3070  		const u8 *buf = wpabuf_head(info->p2ps_instance);
3071  		size_t len = wpabuf_len(info->p2ps_instance);
3072  
3073  		while (len) {
3074  			u32 id;
3075  			u16 methods;
3076  			u8 str_len;
3077  
3078  			if (len < 4 + 2 + 1)
3079  				break;
3080  			id = WPA_GET_LE32(buf);
3081  			buf += sizeof(u32);
3082  			methods = WPA_GET_BE16(buf);
3083  			buf += sizeof(u16);
3084  			str_len = *buf++;
3085  			if (str_len > len - 4 - 2 - 1)
3086  				break;
3087  			os_memcpy(str, buf, str_len);
3088  			str[str_len] = '\0';
3089  			buf += str_len;
3090  			len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
3091  
3092  			wpa_msg_global(wpa_s, MSG_INFO,
3093  				       P2P_EVENT_DEVICE_FOUND MACSTR
3094  				       " p2p_dev_addr=" MACSTR
3095  				       " pri_dev_type=%s name='%s'"
3096  				       " config_methods=0x%x"
3097  				       " dev_capab=0x%x"
3098  				       " group_capab=0x%x"
3099  				       " adv_id=%x asp_svc=%s%s",
3100  				       MAC2STR(addr),
3101  				       MAC2STR(info->p2p_device_addr),
3102  				       wps_dev_type_bin2str(
3103  					       info->pri_dev_type,
3104  					       devtype, sizeof(devtype)),
3105  				       info->device_name, methods,
3106  				       info->dev_capab, info->group_capab,
3107  				       id, str,
3108  				       info->vendor_elems ?
3109  				       " vendor_elems=1" : "");
3110  		}
3111  		goto done;
3112  	}
3113  
3114  	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
3115  		       " p2p_dev_addr=" MACSTR
3116  		       " pri_dev_type=%s name='%s' config_methods=0x%x dev_capab=0x%x group_capab=0x%x%s%s%s new=%d pcea_cap_info=0x%x bootstrap_methods=0x%x pasn_type=0x%x",
3117  		       MAC2STR(addr), MAC2STR(info->p2p_device_addr),
3118  		       wps_dev_type_bin2str(info->pri_dev_type, devtype,
3119  					    sizeof(devtype)),
3120  		       info->device_name, info->config_methods,
3121  		       info->dev_capab, info->group_capab,
3122  		       wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
3123  		       wfd_dev_info_hex ? wfd_dev_info_hex : "",
3124  		       info->vendor_elems ? " vendor_elems=1" : "",
3125  		       new_device, info->pcea_cap_info,
3126  		       info->pairing_config.bootstrap_methods,
3127  		       info->pairing_config.pasn_type);
3128  
3129  done:
3130  	os_free(wfd_dev_info_hex);
3131  #endif /* CONFIG_NO_STDOUT_DEBUG */
3132  
3133  	wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
3134  }
3135  
3136  
wpas_dev_lost(void * ctx,const u8 * dev_addr)3137  static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
3138  {
3139  	struct wpa_supplicant *wpa_s = ctx;
3140  
3141  	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
3142  		       "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
3143  
3144  	wpas_notify_p2p_device_lost(wpa_s, dev_addr);
3145  }
3146  
3147  
wpas_find_stopped(void * ctx)3148  static void wpas_find_stopped(void *ctx)
3149  {
3150  	struct wpa_supplicant *wpa_s = ctx;
3151  
3152  	if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
3153  		wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
3154  
3155  	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
3156  	wpas_notify_p2p_find_stopped(wpa_s);
3157  }
3158  
3159  
3160  struct wpas_p2p_listen_work {
3161  	unsigned int freq;
3162  	unsigned int duration;
3163  	struct wpabuf *probe_resp_ie;
3164  };
3165  
3166  
wpas_p2p_listen_work_free(struct wpas_p2p_listen_work * lwork)3167  static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
3168  {
3169  	if (lwork == NULL)
3170  		return;
3171  	wpabuf_free(lwork->probe_resp_ie);
3172  	os_free(lwork);
3173  }
3174  
3175  
wpas_p2p_listen_work_done(struct wpa_supplicant * wpa_s)3176  static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
3177  {
3178  	struct wpas_p2p_listen_work *lwork;
3179  
3180  	if (!wpa_s->p2p_listen_work)
3181  		return;
3182  
3183  	lwork = wpa_s->p2p_listen_work->ctx;
3184  	wpas_p2p_listen_work_free(lwork);
3185  	radio_work_done(wpa_s->p2p_listen_work);
3186  	wpa_s->p2p_listen_work = NULL;
3187  }
3188  
3189  
wpas_start_listen_cb(struct wpa_radio_work * work,int deinit)3190  static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
3191  {
3192  	struct wpa_supplicant *wpa_s = work->wpa_s;
3193  	struct wpas_p2p_listen_work *lwork = work->ctx;
3194  	unsigned int duration;
3195  
3196  	if (deinit) {
3197  		if (work->started && !wpa_s->p2p_removing_listen_work) {
3198  			wpa_s->p2p_listen_work = NULL;
3199  			wpas_stop_listen(wpa_s);
3200  		}
3201  		wpas_p2p_listen_work_free(lwork);
3202  		return;
3203  	}
3204  
3205  	wpa_s->p2p_listen_work = work;
3206  
3207  	wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
3208  
3209  	if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
3210  		wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
3211  			   "report received Probe Request frames");
3212  		p2p_listen_failed(wpa_s->global->p2p, lwork->freq);
3213  		wpas_p2p_listen_work_done(wpa_s);
3214  		return;
3215  	}
3216  
3217  	wpa_s->pending_listen_freq = lwork->freq;
3218  	wpa_s->pending_listen_duration = lwork->duration;
3219  
3220  	duration = lwork->duration;
3221  #ifdef CONFIG_TESTING_OPTIONS
3222  	if (wpa_s->extra_roc_dur) {
3223  		wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
3224  			   duration, duration + wpa_s->extra_roc_dur);
3225  		duration += wpa_s->extra_roc_dur;
3226  	}
3227  #endif /* CONFIG_TESTING_OPTIONS */
3228  
3229  	if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
3230  		wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
3231  			   "to remain on channel (%u MHz) for Listen "
3232  			   "state", lwork->freq);
3233  		p2p_listen_failed(wpa_s->global->p2p, lwork->freq);
3234  		wpas_p2p_listen_work_done(wpa_s);
3235  		wpa_s->pending_listen_freq = 0;
3236  		return;
3237  	}
3238  	wpa_s->off_channel_freq = 0;
3239  	wpa_s->roc_waiting_drv_freq = lwork->freq;
3240  }
3241  
3242  
wpas_start_listen(void * ctx,unsigned int freq,unsigned int duration,const struct wpabuf * probe_resp_ie)3243  static int wpas_start_listen(void *ctx, unsigned int freq,
3244  			     unsigned int duration,
3245  			     const struct wpabuf *probe_resp_ie)
3246  {
3247  	struct wpa_supplicant *wpa_s = ctx;
3248  	struct wpas_p2p_listen_work *lwork;
3249  
3250  	if (wpa_s->p2p_listen_work) {
3251  		wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
3252  		return -1;
3253  	}
3254  
3255  	lwork = os_zalloc(sizeof(*lwork));
3256  	if (lwork == NULL)
3257  		return -1;
3258  	lwork->freq = freq;
3259  	lwork->duration = duration;
3260  	if (probe_resp_ie) {
3261  		lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
3262  		if (lwork->probe_resp_ie == NULL) {
3263  			wpas_p2p_listen_work_free(lwork);
3264  			return -1;
3265  		}
3266  	}
3267  
3268  	if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
3269  			   lwork) < 0) {
3270  		wpas_p2p_listen_work_free(lwork);
3271  		return -1;
3272  	}
3273  
3274  	return 0;
3275  }
3276  
3277  
wpas_stop_listen(void * ctx)3278  static void wpas_stop_listen(void *ctx)
3279  {
3280  	struct wpa_supplicant *wpa_s = ctx;
3281  	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3282  		wpa_drv_cancel_remain_on_channel(wpa_s);
3283  		wpa_s->off_channel_freq = 0;
3284  		wpa_s->roc_waiting_drv_freq = 0;
3285  	}
3286  	wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
3287  
3288  	/*
3289  	 * Don't cancel Probe Request RX reporting for a connected P2P Client
3290  	 * handling Probe Request frames.
3291  	 */
3292  	if (!wpa_s->p2p_cli_probe)
3293  		wpa_drv_probe_req_report(wpa_s, 0);
3294  
3295  	wpas_p2p_listen_work_done(wpa_s);
3296  
3297  	if (!wpa_s->p2p_removing_listen_work &&
3298  	    radio_work_pending(wpa_s, "p2p-listen")) {
3299  		wpa_s->p2p_removing_listen_work = true;
3300  		wpa_printf(MSG_DEBUG,
3301  			   "P2P: p2p-listen is still pending - remove it");
3302  		radio_remove_works(wpa_s, "p2p-listen", 0);
3303  		wpa_s->p2p_removing_listen_work = false;
3304  	}
3305  }
3306  
3307  
wpas_send_probe_resp(void * ctx,const struct wpabuf * buf,unsigned int freq)3308  static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
3309  				unsigned int freq)
3310  {
3311  	struct wpa_supplicant *wpa_s = ctx;
3312  	return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
3313  				 freq, 0);
3314  }
3315  
3316  
wpas_prov_disc_local_display(struct wpa_supplicant * wpa_s,const u8 * peer,const char * params,unsigned int generated_pin)3317  static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
3318  					 const u8 *peer, const char *params,
3319  					 unsigned int generated_pin)
3320  {
3321  	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
3322  		       " %08d%s", MAC2STR(peer), generated_pin, params);
3323  }
3324  
3325  
wpas_prov_disc_local_keypad(struct wpa_supplicant * wpa_s,const u8 * peer,const char * params)3326  static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
3327  					const u8 *peer, const char *params)
3328  {
3329  	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
3330  		       "%s", MAC2STR(peer), params);
3331  }
3332  
3333  
wpas_prov_disc_req(void * ctx,const u8 * peer,u16 config_methods,const u8 * dev_addr,const u8 * pri_dev_type,const char * dev_name,u16 supp_config_methods,u8 dev_capab,u8 group_capab,const u8 * group_id,size_t group_id_len)3334  static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
3335  			       const u8 *dev_addr, const u8 *pri_dev_type,
3336  			       const char *dev_name, u16 supp_config_methods,
3337  			       u8 dev_capab, u8 group_capab, const u8 *group_id,
3338  			       size_t group_id_len)
3339  {
3340  	struct wpa_supplicant *wpa_s = ctx;
3341  	char devtype[WPS_DEV_TYPE_BUFSIZE];
3342  	char params[300];
3343  	u8 empty_dev_type[8];
3344  	unsigned int generated_pin = 0;
3345  	struct wpa_supplicant *group = NULL;
3346  	int res;
3347  
3348  	if (group_id) {
3349  		for (group = wpa_s->global->ifaces; group; group = group->next)
3350  		{
3351  			struct wpa_ssid *s = group->current_ssid;
3352  			if (s != NULL &&
3353  			    s->mode == WPAS_MODE_P2P_GO &&
3354  			    group_id_len - ETH_ALEN == s->ssid_len &&
3355  			    os_memcmp(group_id + ETH_ALEN, s->ssid,
3356  				      s->ssid_len) == 0)
3357  				break;
3358  		}
3359  	}
3360  
3361  	if (pri_dev_type == NULL) {
3362  		os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
3363  		pri_dev_type = empty_dev_type;
3364  	}
3365  	res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
3366  			  " pri_dev_type=%s name='%s' config_methods=0x%x "
3367  			  "dev_capab=0x%x group_capab=0x%x%s%s",
3368  			  MAC2STR(dev_addr),
3369  			  wps_dev_type_bin2str(pri_dev_type, devtype,
3370  					       sizeof(devtype)),
3371  			  dev_name, supp_config_methods, dev_capab, group_capab,
3372  			  group ? " group=" : "",
3373  			  group ? group->ifname : "");
3374  	if (os_snprintf_error(sizeof(params), res))
3375  		wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
3376  	params[sizeof(params) - 1] = '\0';
3377  
3378  	if (config_methods & WPS_CONFIG_DISPLAY) {
3379  		if (wps_generate_pin(&generated_pin) < 0) {
3380  			wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
3381  			wpas_notify_p2p_provision_discovery(
3382  				wpa_s, peer, 0 /* response */,
3383  				P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
3384  			return;
3385  		}
3386  		wpas_prov_disc_local_display(wpa_s, peer, params,
3387  					     generated_pin);
3388  	} else if (config_methods & WPS_CONFIG_KEYPAD)
3389  		wpas_prov_disc_local_keypad(wpa_s, peer, params);
3390  	else if (config_methods & WPS_CONFIG_PUSHBUTTON)
3391  		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
3392  			       MACSTR "%s", MAC2STR(peer), params);
3393  
3394  	wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
3395  					    P2P_PROV_DISC_SUCCESS,
3396  					    config_methods, generated_pin);
3397  }
3398  
3399  
wpas_prov_disc_resp(void * ctx,const u8 * peer,u16 config_methods)3400  static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
3401  {
3402  	struct wpa_supplicant *wpa_s = ctx;
3403  	unsigned int generated_pin = 0;
3404  	char params[20];
3405  
3406  	if (wpa_s->pending_pd_before_join &&
3407  	    (ether_addr_equal(peer, wpa_s->pending_join_dev_addr) ||
3408  	     ether_addr_equal(peer, wpa_s->pending_join_iface_addr))) {
3409  		wpa_s->pending_pd_before_join = 0;
3410  		wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3411  			   "join-existing-group operation");
3412  		wpas_p2p_join_start(wpa_s, 0, NULL, 0);
3413  		return;
3414  	}
3415  
3416  	if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
3417  	    wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
3418  		int res;
3419  
3420  		res = os_snprintf(params, sizeof(params), " peer_go=%d",
3421  				  wpa_s->pending_pd_use == AUTO_PD_JOIN);
3422  		if (os_snprintf_error(sizeof(params), res))
3423  			params[sizeof(params) - 1] = '\0';
3424  	} else
3425  		params[0] = '\0';
3426  
3427  	if (config_methods & WPS_CONFIG_DISPLAY)
3428  		wpas_prov_disc_local_keypad(wpa_s, peer, params);
3429  	else if (config_methods & WPS_CONFIG_KEYPAD) {
3430  		if (wps_generate_pin(&generated_pin) < 0) {
3431  			wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
3432  			wpas_notify_p2p_provision_discovery(
3433  				wpa_s, peer, 0 /* response */,
3434  				P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
3435  			return;
3436  		}
3437  		wpas_prov_disc_local_display(wpa_s, peer, params,
3438  					     generated_pin);
3439  	} else if (config_methods & WPS_CONFIG_PUSHBUTTON)
3440  		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
3441  			       MACSTR "%s", MAC2STR(peer), params);
3442  
3443  	wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3444  					    P2P_PROV_DISC_SUCCESS,
3445  					    config_methods, generated_pin);
3446  }
3447  
3448  
wpas_prov_disc_fail(void * ctx,const u8 * peer,enum p2p_prov_disc_status status,u32 adv_id,const u8 * adv_mac,const char * deferred_session_resp)3449  static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
3450  				enum p2p_prov_disc_status status,
3451  				u32 adv_id, const u8 *adv_mac,
3452  				const char *deferred_session_resp)
3453  {
3454  	struct wpa_supplicant *wpa_s = ctx;
3455  
3456  	if (wpa_s->p2p_fallback_to_go_neg) {
3457  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
3458  			"failed - fall back to GO Negotiation");
3459  		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
3460  			       P2P_EVENT_FALLBACK_TO_GO_NEG
3461  			       "reason=PD-failed");
3462  		wpas_p2p_fallback_to_go_neg(wpa_s, 0);
3463  		return;
3464  	}
3465  
3466  	if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
3467  		wpa_s->pending_pd_before_join = 0;
3468  		wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3469  			   "join-existing-group operation (no ACK for PD "
3470  			   "Req attempts)");
3471  		wpas_p2p_join_start(wpa_s, 0, NULL, 0);
3472  		return;
3473  	}
3474  
3475  	if (adv_id && adv_mac && deferred_session_resp) {
3476  		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3477  			       " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
3478  			       " deferred_session_resp='%s'",
3479  			       MAC2STR(peer), status, adv_id,
3480  			       deferred_session_resp);
3481  	} else if (adv_id && adv_mac) {
3482  		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3483  			       " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
3484  			       MAC2STR(peer), status, adv_id);
3485  	} else {
3486  		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3487  			       " p2p_dev_addr=" MACSTR " status=%d",
3488  			       MAC2STR(peer), status);
3489  	}
3490  
3491  	wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3492  					    status, 0, 0);
3493  }
3494  
3495  
freq_included(struct wpa_supplicant * wpa_s,const struct p2p_channels * channels,unsigned int freq)3496  static int freq_included(struct wpa_supplicant *wpa_s,
3497  			 const struct p2p_channels *channels,
3498  			 unsigned int freq)
3499  {
3500  	if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
3501  	    wpas_p2p_go_is_peer_freq(wpa_s, freq))
3502  		return 1;
3503  	return 0;
3504  }
3505  
3506  
wpas_p2p_go_update_common_freqs(struct wpa_supplicant * wpa_s)3507  static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
3508  {
3509  	unsigned int num = P2P_MAX_CHANNELS;
3510  	int *common_freqs;
3511  	int ret;
3512  
3513  	p2p_go_dump_common_freqs(wpa_s);
3514  	common_freqs = os_calloc(num, sizeof(int));
3515  	if (!common_freqs)
3516  		return;
3517  
3518  	ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
3519  	if (ret < 0) {
3520  		wpa_dbg(wpa_s, MSG_DEBUG,
3521  			"P2P: Failed to get group common freqs");
3522  		os_free(common_freqs);
3523  		return;
3524  	}
3525  
3526  	os_free(wpa_s->p2p_group_common_freqs);
3527  	wpa_s->p2p_group_common_freqs = common_freqs;
3528  	wpa_s->p2p_group_common_freqs_num = num;
3529  	p2p_go_dump_common_freqs(wpa_s);
3530  }
3531  
3532  
3533  /*
3534   * Check if the given frequency is one of the possible operating frequencies
3535   * set after the completion of the GO Negotiation.
3536   */
wpas_p2p_go_is_peer_freq(struct wpa_supplicant * wpa_s,int freq)3537  static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
3538  {
3539  	unsigned int i;
3540  
3541  	p2p_go_dump_common_freqs(wpa_s);
3542  
3543  	/* assume no restrictions */
3544  	if (!wpa_s->p2p_group_common_freqs_num)
3545  		return 1;
3546  
3547  	for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
3548  		if (wpa_s->p2p_group_common_freqs[i] == freq)
3549  			return 1;
3550  	}
3551  	return 0;
3552  }
3553  
3554  
wpas_sta_check_ecsa(struct hostapd_data * hapd,struct sta_info * sta,void * ctx)3555  static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
3556  			       struct sta_info *sta, void *ctx)
3557  {
3558  	int *ecsa_support = ctx;
3559  
3560  	*ecsa_support &= sta->ecsa_supported;
3561  
3562  	return 0;
3563  }
3564  
3565  
3566  /* Check if all the peers support eCSA */
wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant * wpa_s)3567  static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3568  {
3569  	int ecsa_support = 1;
3570  
3571  	ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3572  			&ecsa_support);
3573  
3574  	return ecsa_support;
3575  }
3576  
3577  
3578  /**
3579   * Pick the best frequency to use from all the currently used frequencies.
3580   */
wpas_p2p_pick_best_used_freq(struct wpa_supplicant * wpa_s,struct wpa_used_freq_data * freqs,unsigned int num)3581  static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3582  					struct wpa_used_freq_data *freqs,
3583  					unsigned int num)
3584  {
3585  	unsigned int i, c;
3586  
3587  	/* find a candidate freq that is supported by P2P */
3588  	for (c = 0; c < num; c++)
3589  		if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3590  			break;
3591  
3592  	if (c == num)
3593  		return 0;
3594  
3595  	/* once we have a candidate, try to find a 'better' one */
3596  	for (i = c + 1; i < num; i++) {
3597  		if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3598  			continue;
3599  
3600  		/*
3601  		 * 1. Infrastructure station interfaces have higher preference.
3602  		 * 2. P2P Clients have higher preference.
3603  		 * 3. All others.
3604  		 */
3605  		if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3606  			c = i;
3607  			break;
3608  		}
3609  
3610  		if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3611  			c = i;
3612  	}
3613  	return freqs[c].freq;
3614  }
3615  
3616  
3617  /**
3618   * Pick the best frequency the driver suggests.
3619   *
3620   * num_pref_freq is used as both input and output
3621   * - input: the max size of pref_freq_list,
3622   * - output: the valid size of pref_freq_list filled with data.
3623   */
wpas_p2p_pick_best_pref_freq(struct wpa_supplicant * wpa_s,bool go,struct weighted_pcl * pref_freq_list,unsigned int * num_pref_freq)3624  static int wpas_p2p_pick_best_pref_freq(struct wpa_supplicant *wpa_s, bool go,
3625  					struct weighted_pcl *pref_freq_list,
3626  					unsigned int *num_pref_freq)
3627  {
3628  	int best_freq = 0;
3629  	unsigned int max_pref_freq, i;
3630  	int res;
3631  	enum wpa_driver_if_type iface_type;
3632  
3633  	max_pref_freq = *num_pref_freq;
3634  	*num_pref_freq = 0;
3635  
3636  	if (go)
3637  		iface_type = WPA_IF_P2P_GO;
3638  	else
3639  		iface_type = WPA_IF_P2P_CLIENT;
3640  
3641  	res = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &max_pref_freq,
3642  					 pref_freq_list);
3643  	if (!res && !is_p2p_allow_6ghz(wpa_s->global->p2p))
3644  		max_pref_freq = p2p_remove_6ghz_channels(pref_freq_list,
3645  							 max_pref_freq);
3646  	if (res || !max_pref_freq) {
3647  		wpa_printf(MSG_DEBUG,
3648  			   "P2P: No preferred frequency list available");
3649  		return 0;
3650  	}
3651  
3652  	*num_pref_freq = max_pref_freq;
3653  	i = 0;
3654  	while (i < *num_pref_freq &&
3655  	       (!p2p_supported_freq(wpa_s->global->p2p,
3656  				    pref_freq_list[i].freq) ||
3657  		wpas_p2p_disallowed_freq(wpa_s->global,
3658  					 pref_freq_list[i].freq) ||
3659  		!p2p_pref_freq_allowed(&pref_freq_list[i], go))) {
3660  		wpa_printf(MSG_DEBUG,
3661  			   "P2P: preferred_freq_list[%d]=%d is disallowed",
3662  			   i, pref_freq_list[i].freq);
3663  		i++;
3664  	}
3665  	if (i != *num_pref_freq) {
3666  		best_freq = pref_freq_list[i].freq;
3667  		wpa_printf(MSG_DEBUG, "P2P: Using preferred_freq_list[%d]=%d",
3668  			   i, best_freq);
3669  	} else {
3670  		wpa_printf(MSG_DEBUG,
3671  			   "P2P: All driver preferred frequencies are disallowed for P2P use");
3672  		*num_pref_freq = 0;
3673  	}
3674  
3675  	return best_freq;
3676  }
3677  
3678  
wpas_invitation_process(void * ctx,const u8 * sa,const u8 * bssid,const u8 * go_dev_addr,const u8 * ssid,size_t ssid_len,int * go,u8 * group_bssid,int * force_freq,int persistent_group,const struct p2p_channels * channels,int dev_pw_id,bool p2p2,const u8 ** new_ssid,size_t * new_ssid_len)3679  static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3680  				  const u8 *go_dev_addr, const u8 *ssid,
3681  				  size_t ssid_len, int *go, u8 *group_bssid,
3682  				  int *force_freq, int persistent_group,
3683  				  const struct p2p_channels *channels,
3684  				  int dev_pw_id, bool p2p2, const u8 **new_ssid,
3685  				  size_t *new_ssid_len)
3686  {
3687  	struct wpa_supplicant *wpa_s = ctx;
3688  	struct wpa_ssid *s;
3689  	struct wpa_used_freq_data *freqs;
3690  	struct wpa_supplicant *grp;
3691  	int best_freq;
3692  	struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
3693  	unsigned int num_pref_freq;
3694  	int res;
3695  
3696  
3697  	if (!persistent_group) {
3698  		wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3699  			   " to join an active group (SSID: %s)",
3700  			   MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
3701  		if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3702  		    (ether_addr_equal(go_dev_addr, wpa_s->p2p_auth_invite) ||
3703  		     ether_addr_equal(sa, wpa_s->p2p_auth_invite))) {
3704  			wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3705  				   "authorized invitation");
3706  			goto accept_inv;
3707  		}
3708  
3709  #ifdef CONFIG_WPS_NFC
3710  		if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3711  		    dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
3712  			wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
3713  			wpa_s->p2p_wps_method = WPS_NFC;
3714  			wpa_s->pending_join_wps_method = WPS_NFC;
3715  			os_memcpy(wpa_s->pending_join_dev_addr,
3716  				  go_dev_addr, ETH_ALEN);
3717  			os_memcpy(wpa_s->pending_join_iface_addr,
3718  				  bssid, ETH_ALEN);
3719  			goto accept_inv;
3720  		}
3721  #endif /* CONFIG_WPS_NFC */
3722  
3723  		/*
3724  		 * Do not accept the invitation automatically; notify user and
3725  		 * request approval.
3726  		 */
3727  		return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3728  	}
3729  
3730  	grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3731  	if (grp) {
3732  		wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3733  			   "running persistent group");
3734  		if (*go)
3735  			os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3736  		goto accept_inv;
3737  	}
3738  
3739  	if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3740  	    ether_addr_equal(sa, wpa_s->p2p_auth_invite)) {
3741  		wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3742  			   "invitation to re-invoke a persistent group");
3743  		os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3744  	} else if (!wpa_s->conf->persistent_reconnect)
3745  		return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3746  
3747  	for (s = wpa_s->conf->ssid; s; s = s->next) {
3748  		if (s->disabled == 2 &&
3749  		    (p2p2 || ether_addr_equal(s->bssid, go_dev_addr)) &&
3750  		    s->ssid_len == ssid_len &&
3751  		    os_memcmp(ssid, s->ssid, ssid_len) == 0)
3752  			break;
3753  	}
3754  
3755  	if (p2p2) {
3756  		int dik_id;
3757  		u8 go_ssid[SSID_MAX_LEN];
3758  
3759  		dik_id = p2p_get_dik_id(wpa_s->global->p2p, sa);
3760  		s = wpa_config_get_network_with_dik_id(wpa_s->conf, dik_id);
3761  		if (!s) {
3762  			wpa_printf(MSG_DEBUG, "P2P2: Invitation from " MACSTR
3763  				   " requested reinvocation of an unknown group",
3764  				   MAC2STR(sa));
3765  			return P2P_SC_FAIL_UNKNOWN_GROUP;
3766  		}
3767  		os_free(s->ssid);
3768  		if (s->mode == WPAS_MODE_P2P_GO) {
3769  			p2p_build_ssid(wpa_s->global->p2p, go_ssid,
3770  				       &s->ssid_len);
3771  			s->ssid = os_memdup(go_ssid, s->ssid_len);
3772  			if (!s->ssid) {
3773  				s->ssid_len = 0;
3774  				return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3775  			}
3776  			wpa_printf(MSG_DEBUG,
3777  				   "P2P: New random SSID for the group: %s",
3778  				   wpa_ssid_txt(s->ssid, s->ssid_len));
3779  			*new_ssid = s->ssid;
3780  			*new_ssid_len = s->ssid_len;
3781  		} else {
3782  			s->ssid_len = ssid_len;
3783  			s->ssid = os_memdup(ssid, ssid_len);
3784  			if (!s->ssid) {
3785  				s->ssid_len = 0;
3786  				return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3787  			}
3788  		}
3789  	} else if (!s) {
3790  		wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3791  			   " requested reinvocation of an unknown group",
3792  			   MAC2STR(sa));
3793  		return P2P_SC_FAIL_UNKNOWN_GROUP;
3794  	}
3795  
3796  	if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3797  		*go = 1;
3798  		if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3799  			wpa_printf(MSG_DEBUG, "P2P: The only available "
3800  				   "interface is already in use - reject "
3801  				   "invitation");
3802  			return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3803  		}
3804  		if (wpa_s->p2p_mgmt)
3805  			os_memcpy(group_bssid, wpa_s->parent->own_addr,
3806  				  ETH_ALEN);
3807  		else
3808  			os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3809  	} else if (s->mode == WPAS_MODE_P2P_GO) {
3810  		*go = 1;
3811  		if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3812  		{
3813  			wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3814  				   "interface address for the group");
3815  			return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3816  		}
3817  		os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3818  			  ETH_ALEN);
3819  	}
3820  
3821  accept_inv:
3822  	wpas_p2p_set_own_freq_preference(wpa_s, 0);
3823  
3824  	best_freq = 0;
3825  	freqs = os_calloc(wpa_s->num_multichan_concurrent,
3826  			  sizeof(struct wpa_used_freq_data));
3827  	if (freqs) {
3828  		int num_channels = wpa_s->num_multichan_concurrent;
3829  		int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3830  		best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3831  		os_free(freqs);
3832  	}
3833  
3834  	num_pref_freq = P2P_MAX_PREF_CHANNELS;
3835  	res = wpas_p2p_pick_best_pref_freq(wpa_s, *go, pref_freq_list,
3836  					   &num_pref_freq);
3837  	if (res > 0)
3838  		best_freq = res;
3839  
3840  	/* Get one of the frequencies currently in use */
3841  	if (best_freq > 0) {
3842  		wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3843  		wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
3844  
3845  		if (wpa_s->num_multichan_concurrent < 2 ||
3846  		    wpas_p2p_num_unused_channels(wpa_s) < 1) {
3847  			wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3848  			*force_freq = best_freq;
3849  		}
3850  	}
3851  
3852  	if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3853  	    wpas_p2p_num_unused_channels(wpa_s) > 0) {
3854  		if (*go == 0) {
3855  			/* We are the client */
3856  			wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3857  				   "running a GO but we are capable of MCC, "
3858  				   "figure out the best channel to use");
3859  			*force_freq = 0;
3860  		} else if (!freq_included(wpa_s, channels, *force_freq)) {
3861  			/* We are the GO, and *force_freq is not in the
3862  			 * intersection */
3863  			wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3864  				   "in intersection but we are capable of MCC, "
3865  				   "figure out the best channel to use",
3866  				   *force_freq);
3867  			*force_freq = 0;
3868  		}
3869  	}
3870  
3871  	return P2P_SC_SUCCESS;
3872  }
3873  
3874  
wpas_invitation_received(void * ctx,const u8 * sa,const u8 * bssid,const u8 * ssid,size_t ssid_len,const u8 * go_dev_addr,u8 status,int op_freq,const u8 * pmkid,const u8 * pmk,size_t pmk_len)3875  static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3876  				     const u8 *ssid, size_t ssid_len,
3877  				     const u8 *go_dev_addr, u8 status,
3878  				     int op_freq, const u8 *pmkid,
3879  				     const u8 *pmk, size_t pmk_len)
3880  {
3881  	struct wpa_supplicant *wpa_s = ctx;
3882  	struct wpa_ssid *s;
3883  
3884  	for (s = wpa_s->conf->ssid; s; s = s->next) {
3885  		if (s->disabled == 2 &&
3886  		    s->ssid_len == ssid_len &&
3887  		    os_memcmp(ssid, s->ssid, ssid_len) == 0)
3888  			break;
3889  	}
3890  
3891  	if (status == P2P_SC_SUCCESS) {
3892  		wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3893  			   " was accepted; op_freq=%d MHz, SSID=%s",
3894  			   MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3895  		if (s) {
3896  			const char *ssid_txt;
3897  
3898  			ssid_txt = wpa_ssid_txt(s->ssid, s->ssid_len);
3899  			int go = s->mode == WPAS_MODE_P2P_GO;
3900  			if (go) {
3901  				wpa_msg_global(wpa_s, MSG_INFO,
3902  					       P2P_EVENT_INVITATION_ACCEPTED
3903  					       "sa=" MACSTR
3904  					       " persistent=%d freq=%d ssid=\"%s\" go_dev_addr="
3905  					       MACSTR, MAC2STR(sa), s->id,
3906  					       op_freq, ssid_txt,
3907  					       MAC2STR(go_dev_addr));
3908  			} else {
3909  				wpa_msg_global(wpa_s, MSG_INFO,
3910  					       P2P_EVENT_INVITATION_ACCEPTED
3911  					       "sa=" MACSTR
3912  					       " persistent=%d ssid=\"%s\" go_dev_addr=" MACSTR,
3913  					       MAC2STR(sa), s->id, ssid_txt,
3914  					       MAC2STR(go_dev_addr));
3915  			}
3916  			wpas_p2p_group_add_persistent(
3917  				wpa_s, s, go, 0, op_freq, 0,
3918  				wpa_s->conf->p2p_go_ht40,
3919  				wpa_s->conf->p2p_go_vht,
3920  				0,
3921  				wpa_s->conf->p2p_go_he,
3922  				wpa_s->conf->p2p_go_edmg, NULL,
3923  				go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3924  				1, is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
3925  				bssid, sa, pmkid, pmk, pmk_len);
3926  		} else if (bssid) {
3927  			wpa_s->user_initiated_pd = 0;
3928  			wpa_msg_global(wpa_s, MSG_INFO,
3929  				       P2P_EVENT_INVITATION_ACCEPTED
3930  				       "sa=" MACSTR " go_dev_addr=" MACSTR
3931  				       " bssid=" MACSTR " unknown-network",
3932  				       MAC2STR(sa), MAC2STR(go_dev_addr),
3933  				       MAC2STR(bssid));
3934  			wpas_p2p_join(wpa_s, bssid, go_dev_addr,
3935  				      wpa_s->p2p_wps_method, 0, op_freq,
3936  				      ssid, ssid_len);
3937  		}
3938  		return;
3939  	}
3940  
3941  	if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3942  		wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3943  			   " was rejected (status %u)", MAC2STR(sa), status);
3944  		return;
3945  	}
3946  
3947  	if (!s) {
3948  		if (bssid) {
3949  			wpa_msg_global(wpa_s, MSG_INFO,
3950  				       P2P_EVENT_INVITATION_RECEIVED
3951  				       "sa=" MACSTR " go_dev_addr=" MACSTR
3952  				       " bssid=" MACSTR " unknown-network",
3953  				       MAC2STR(sa), MAC2STR(go_dev_addr),
3954  				       MAC2STR(bssid));
3955  		} else {
3956  			wpa_msg_global(wpa_s, MSG_INFO,
3957  				       P2P_EVENT_INVITATION_RECEIVED
3958  				       "sa=" MACSTR " go_dev_addr=" MACSTR
3959  				       " unknown-network",
3960  				       MAC2STR(sa), MAC2STR(go_dev_addr));
3961  		}
3962  		wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3963  						    bssid, 0, op_freq);
3964  		return;
3965  	}
3966  
3967  	if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
3968  		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3969  			       "sa=" MACSTR " persistent=%d freq=%d",
3970  			       MAC2STR(sa), s->id, op_freq);
3971  	} else {
3972  		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3973  			       "sa=" MACSTR " persistent=%d",
3974  			       MAC2STR(sa), s->id);
3975  	}
3976  	wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3977  					    s->id, op_freq);
3978  }
3979  
3980  
wpas_remove_persistent_peer(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,const u8 * peer,int inv)3981  static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3982  					struct wpa_ssid *ssid,
3983  					const u8 *peer, int inv)
3984  {
3985  	size_t i;
3986  	struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
3987  
3988  	if (ssid == NULL)
3989  		return;
3990  
3991  	for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3992  		if (ether_addr_equal(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3993  				     peer))
3994  			break;
3995  	}
3996  	if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
3997  		if (ssid->mode != WPAS_MODE_P2P_GO &&
3998  		    ether_addr_equal(ssid->bssid, peer)) {
3999  			wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
4000  				   "due to invitation result", ssid->id);
4001  			wpas_notify_network_removed(wpa_s, ssid);
4002  			wpa_config_remove_network(wpa_s->conf, ssid->id);
4003  			return;
4004  		}
4005  		return; /* Peer not found in client list */
4006  	}
4007  
4008  	wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
4009  		   "group %d client list%s",
4010  		   MAC2STR(peer), ssid->id,
4011  		   inv ? " due to invitation result" : "");
4012  	os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
4013  		   ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4014  		   (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4015  	ssid->num_p2p_clients--;
4016  	if (p2p_wpa_s->conf->update_config &&
4017  	    wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
4018  		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
4019  }
4020  
4021  
wpas_remove_persistent_client(struct wpa_supplicant * wpa_s,const u8 * peer)4022  static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
4023  					  const u8 *peer)
4024  {
4025  	struct wpa_ssid *ssid;
4026  
4027  	wpa_s = wpa_s->global->p2p_invite_group;
4028  	if (wpa_s == NULL)
4029  		return; /* No known invitation group */
4030  	ssid = wpa_s->current_ssid;
4031  	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
4032  	    !ssid->p2p_persistent_group)
4033  		return; /* Not operating as a GO in persistent group */
4034  	ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
4035  				       ssid->ssid, ssid->ssid_len);
4036  	wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
4037  }
4038  
4039  
wpas_msg_p2p_invitation_result(struct wpa_supplicant * wpa_s,int status,const u8 * new_ssid,size_t new_ssid_len,const u8 * bssid,const u8 * go_dev_addr)4040  static void wpas_msg_p2p_invitation_result(struct wpa_supplicant *wpa_s,
4041  					   int status, const u8 *new_ssid,
4042  					   size_t new_ssid_len, const u8 *bssid,
4043  					   const u8 *go_dev_addr)
4044  {
4045  	int res;
4046  	char buf[500];
4047  	char *pos, *end;
4048  	const char *ssid_txt = NULL;
4049  
4050  	pos = buf;
4051  	end = buf + sizeof(buf);
4052  
4053  	if (go_dev_addr && new_ssid && new_ssid_len) {
4054  		ssid_txt = wpa_ssid_txt(new_ssid, new_ssid_len);
4055  	} else if (go_dev_addr) {
4056  		struct wpa_ssid *ssid;
4057  
4058  		ssid = wpa_config_get_network(wpa_s->conf,
4059  					      wpa_s->pending_invite_ssid_id);
4060  		if (ssid)
4061  			ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4062  	}
4063  
4064  	res = os_snprintf(pos, end - pos, "status=%d", status);
4065  	if (os_snprintf_error(end - pos, res))
4066  		goto fail;
4067  	pos += res;
4068  
4069  	if (bssid) {
4070  		res = os_snprintf(pos, end - pos, " " MACSTR, MAC2STR(bssid));
4071  		if (os_snprintf_error(end - pos, res))
4072  			goto fail;
4073  		pos += res;
4074  	}
4075  
4076  	if (ssid_txt) {
4077  		res = os_snprintf(pos, end - pos, " ssid=\"%s\"", ssid_txt);
4078  		if (os_snprintf_error(end - pos, res))
4079  			goto fail;
4080  		pos += res;
4081  	}
4082  
4083  	if (go_dev_addr) {
4084  		res = os_snprintf(pos, end - pos, " go_dev_addr=" MACSTR,
4085  				  MAC2STR(go_dev_addr));
4086  		if (os_snprintf_error(end - pos, res))
4087  			goto fail;
4088  		pos += res;
4089  	}
4090  
4091  	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT "%s", buf);
4092  	return;
4093  
4094  fail:
4095  	wpa_printf(MSG_DEBUG,
4096  		   "P2P: Failed to send P2P-INVITATION-RESULT message");
4097  }
4098  
4099  
wpas_invitation_result(void * ctx,int status,const u8 * new_ssid,size_t new_ssid_len,const u8 * bssid,const struct p2p_channels * channels,const u8 * peer,int neg_freq,int peer_oper_freq,const u8 * pmkid,const u8 * pmk,size_t pmk_len,const u8 * go_dev_addr)4100  static void wpas_invitation_result(void *ctx, int status, const u8 *new_ssid,
4101  				   size_t new_ssid_len, const u8 *bssid,
4102  				   const struct p2p_channels *channels,
4103  				   const u8 *peer, int neg_freq,
4104  				   int peer_oper_freq, const u8 *pmkid,
4105  				   const u8 *pmk, size_t pmk_len,
4106  				   const u8 *go_dev_addr)
4107  {
4108  	struct wpa_supplicant *wpa_s = ctx;
4109  	struct wpa_ssid *ssid;
4110  	int freq;
4111  
4112  #ifdef CONFIG_PASN
4113  	if (wpa_s->p2p_pasn_auth_work) {
4114  		wpas_p2p_pasn_cancel_auth_work(wpa_s);
4115  		wpa_s->p2p_pasn_auth_work = NULL;
4116  	}
4117  #endif /* CONFIG_PASN */
4118  
4119  	wpas_msg_p2p_invitation_result(wpa_s, status, new_ssid, new_ssid_len,
4120  				       bssid, go_dev_addr);
4121  	wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
4122  
4123  	wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
4124  		   status, MAC2STR(peer));
4125  	if (wpa_s->pending_invite_ssid_id == -1) {
4126  		struct wpa_supplicant *group_if =
4127  			wpa_s->global->p2p_invite_group;
4128  
4129  		if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
4130  			wpas_remove_persistent_client(wpa_s, peer);
4131  
4132  		/*
4133  		 * Invitation to an active group. If this is successful and we
4134  		 * are the GO, set the client wait to postpone some concurrent
4135  		 * operations and to allow provisioning and connection to happen
4136  		 * more quickly.
4137  		 */
4138  		if (status == P2P_SC_SUCCESS &&
4139  		    group_if && group_if->current_ssid &&
4140  		    group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
4141  			os_get_reltime(&wpa_s->global->p2p_go_wait_client);
4142  #ifdef CONFIG_TESTING_OPTIONS
4143  			if (group_if->p2p_go_csa_on_inv) {
4144  				wpa_printf(MSG_DEBUG,
4145  					   "Testing: force P2P GO CSA after invitation");
4146  				eloop_cancel_timeout(
4147  					wpas_p2p_reconsider_moving_go,
4148  					wpa_s, NULL);
4149  				eloop_register_timeout(
4150  					0, 50000,
4151  					wpas_p2p_reconsider_moving_go,
4152  					wpa_s, NULL);
4153  			}
4154  #endif /* CONFIG_TESTING_OPTIONS */
4155  		}
4156  		return;
4157  	}
4158  
4159  	if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
4160  		wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
4161  			   "invitation exchange to indicate readiness for "
4162  			   "re-invocation");
4163  	}
4164  
4165  	if (status != P2P_SC_SUCCESS) {
4166  		if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
4167  			ssid = wpa_config_get_network(
4168  				wpa_s->conf, wpa_s->pending_invite_ssid_id);
4169  			wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
4170  		}
4171  		wpas_p2p_remove_pending_group_interface(wpa_s);
4172  		return;
4173  	}
4174  
4175  	ssid = wpa_config_get_network(wpa_s->conf,
4176  				      wpa_s->pending_invite_ssid_id);
4177  	if (ssid == NULL) {
4178  		wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
4179  			   "data matching with invitation");
4180  		return;
4181  	}
4182  
4183  	if (new_ssid && new_ssid_len) {
4184  		os_free(ssid->ssid);
4185  		ssid->ssid = os_memdup(new_ssid, new_ssid_len);
4186  		if (!ssid->ssid) {
4187  			ssid->ssid_len = 0;
4188  			return;
4189  		}
4190  		ssid->ssid_len = new_ssid_len;
4191  	}
4192  
4193  	/*
4194  	 * The peer could have missed our ctrl::ack frame for Invitation
4195  	 * Response and continue retransmitting the frame. To reduce the
4196  	 * likelihood of the peer not getting successful TX status for the
4197  	 * Invitation Response frame, wait a short time here before starting
4198  	 * the persistent group so that we will remain on the current channel to
4199  	 * acknowledge any possible retransmission from the peer.
4200  	 */
4201  	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
4202  		"starting persistent group");
4203  	os_sleep(0, 50000);
4204  
4205  	if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
4206  	    freq_included(wpa_s, channels, neg_freq))
4207  		freq = neg_freq;
4208  	else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
4209  		 freq_included(wpa_s, channels, peer_oper_freq))
4210  		freq = peer_oper_freq;
4211  	else
4212  		freq = 0;
4213  
4214  	wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
4215  		   freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4216  	wpas_p2p_group_add_persistent(wpa_s, ssid,
4217  				      ssid->mode == WPAS_MODE_P2P_GO,
4218  				      wpa_s->p2p_persistent_go_freq,
4219  				      freq,
4220  				      wpa_s->p2p_go_vht_center_freq2,
4221  				      wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
4222  				      wpa_s->p2p_go_max_oper_chwidth,
4223  				      wpa_s->p2p_go_he,
4224  				      wpa_s->p2p_go_edmg,
4225  				      channels,
4226  				      ssid->mode == WPAS_MODE_P2P_GO ?
4227  				      P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
4228  				      0, 1,
4229  				      is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
4230  				      bssid, peer, pmkid, pmk, pmk_len);
4231  }
4232  
4233  
wpas_p2p_disallowed_freq(struct wpa_global * global,unsigned int freq)4234  static int wpas_p2p_disallowed_freq(struct wpa_global *global,
4235  				    unsigned int freq)
4236  {
4237  	if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
4238  		return 1;
4239  	return freq_range_list_includes(&global->p2p_disallow_freq, freq);
4240  }
4241  
4242  
wpas_p2p_add_chan(struct p2p_reg_class * reg,u8 chan)4243  static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
4244  {
4245  	reg->channel[reg->channels] = chan;
4246  	reg->channels++;
4247  }
4248  
4249  
wpas_p2p_default_channels(struct wpa_supplicant * wpa_s,struct p2p_channels * chan,struct p2p_channels * cli_chan)4250  static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
4251  				     struct p2p_channels *chan,
4252  				     struct p2p_channels *cli_chan)
4253  {
4254  	int i, cla = 0;
4255  
4256  	wpa_s->global->p2p_24ghz_social_channels = 1;
4257  
4258  	os_memset(cli_chan, 0, sizeof(*cli_chan));
4259  
4260  	wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
4261  		   "band");
4262  
4263  	/* Operating class 81 - 2.4 GHz band channels 1..13 */
4264  	chan->reg_class[cla].reg_class = 81;
4265  	chan->reg_class[cla].channels = 0;
4266  	for (i = 0; i < 11; i++) {
4267  		if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
4268  			wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
4269  	}
4270  	if (chan->reg_class[cla].channels)
4271  		cla++;
4272  
4273  	wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
4274  		   "band");
4275  
4276  	/* Operating class 115 - 5 GHz, channels 36-48 */
4277  	chan->reg_class[cla].reg_class = 115;
4278  	chan->reg_class[cla].channels = 0;
4279  	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
4280  		wpas_p2p_add_chan(&chan->reg_class[cla], 36);
4281  	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
4282  		wpas_p2p_add_chan(&chan->reg_class[cla], 40);
4283  	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
4284  		wpas_p2p_add_chan(&chan->reg_class[cla], 44);
4285  	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
4286  		wpas_p2p_add_chan(&chan->reg_class[cla], 48);
4287  	if (chan->reg_class[cla].channels)
4288  		cla++;
4289  
4290  	wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
4291  		   "band");
4292  
4293  	/* Operating class 124 - 5 GHz, channels 149,153,157,161 */
4294  	chan->reg_class[cla].reg_class = 124;
4295  	chan->reg_class[cla].channels = 0;
4296  	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
4297  		wpas_p2p_add_chan(&chan->reg_class[cla], 149);
4298  	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
4299  		wpas_p2p_add_chan(&chan->reg_class[cla], 153);
4300  	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
4301  		wpas_p2p_add_chan(&chan->reg_class[cla], 157);
4302  	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
4303  		wpas_p2p_add_chan(&chan->reg_class[cla], 161);
4304  	if (chan->reg_class[cla].channels)
4305  		cla++;
4306  
4307  	chan->reg_classes = cla;
4308  	return 0;
4309  }
4310  
4311  
has_channel(struct wpa_global * global,struct hostapd_hw_modes * mode,u8 op_class,u8 chan,int * flags)4312  static enum chan_allowed has_channel(struct wpa_global *global,
4313  				     struct hostapd_hw_modes *mode, u8 op_class,
4314  				     u8 chan, int *flags)
4315  {
4316  	int i;
4317  	unsigned int freq;
4318  
4319  	freq = ieee80211_chan_to_freq(NULL, op_class, chan);
4320  	if (wpas_p2p_disallowed_freq(global, freq))
4321  		return NOT_ALLOWED;
4322  
4323  	for (i = 0; i < mode->num_channels; i++) {
4324  		if ((unsigned int) mode->channels[i].freq == freq) {
4325  			if (flags)
4326  				*flags = mode->channels[i].flag;
4327  			if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
4328  				return NOT_ALLOWED;
4329  			if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
4330  				return NO_IR;
4331  			if (mode->channels[i].flag & HOSTAPD_CHAN_RADAR)
4332  				return RADAR;
4333  			return ALLOWED;
4334  		}
4335  	}
4336  
4337  	return NOT_ALLOWED;
4338  }
4339  
4340  
wpas_p2p_get_center_80mhz(struct wpa_supplicant * wpa_s,struct hostapd_hw_modes * mode,u8 channel,const u8 * center_channels,size_t num_chan)4341  static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
4342  				     struct hostapd_hw_modes *mode,
4343  				     u8 channel, const u8 *center_channels,
4344  				     size_t num_chan)
4345  {
4346  	size_t i;
4347  
4348  	if (mode->mode != HOSTAPD_MODE_IEEE80211A)
4349  		return 0;
4350  
4351  	for (i = 0; i < num_chan; i++)
4352  		/*
4353  		 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
4354  		 * so the center channel is 6 channels away from the start/end.
4355  		 */
4356  		if (channel >= center_channels[i] - 6 &&
4357  		    channel <= center_channels[i] + 6)
4358  			return center_channels[i];
4359  
4360  	return 0;
4361  }
4362  
4363  
4364  static const u8 center_channels_5ghz_80mhz[] = { 42, 58, 106, 122, 138,
4365  						 155, 171 };
4366  static const u8 center_channels_6ghz_80mhz[] = { 7, 23, 39, 55, 71, 87, 103,
4367  						 119, 135, 151, 167, 183, 199,
4368  						 215 };
4369  
wpas_p2p_verify_80mhz(struct wpa_supplicant * wpa_s,struct hostapd_hw_modes * mode,u8 op_class,u8 channel,u8 bw)4370  static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
4371  					       struct hostapd_hw_modes *mode,
4372  					       u8 op_class, u8 channel, u8 bw)
4373  {
4374  	u8 center_chan;
4375  	int i, flags;
4376  	enum chan_allowed res, ret = ALLOWED;
4377  	const u8 *chans;
4378  	size_t num_chans;
4379  	bool is_6ghz = is_6ghz_op_class(op_class);
4380  
4381  	if (is_6ghz) {
4382  		chans = center_channels_6ghz_80mhz;
4383  		num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4384  	} else {
4385  		chans = center_channels_5ghz_80mhz;
4386  		num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4387  	}
4388  	center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4389  						chans, num_chans);
4390  	if (!center_chan)
4391  		return NOT_ALLOWED;
4392  	if (!wpa_s->p2p_go_allow_dfs &&
4393  	    !is_6ghz && center_chan >= 58 && center_chan <= 138)
4394  		return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
4395  
4396  	/* check all the channels are available */
4397  	for (i = 0; i < 4; i++) {
4398  		int adj_chan = center_chan - 6 + i * 4;
4399  
4400  		res = has_channel(wpa_s->global, mode, op_class, adj_chan,
4401  				  &flags);
4402  		if (res == NOT_ALLOWED)
4403  			return NOT_ALLOWED;
4404  		if (res == RADAR)
4405  			ret = RADAR;
4406  		if (res == NO_IR)
4407  			ret = NO_IR;
4408  		if (!is_6ghz) {
4409  			if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL))
4410  				return NOT_ALLOWED;
4411  		} else if (is_6ghz &&
4412  			   (!(wpas_get_6ghz_he_chwidth_capab(mode) &
4413  			      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))) {
4414  			return NOT_ALLOWED;
4415  		}
4416  	}
4417  
4418  	return ret;
4419  }
4420  
4421  
wpas_p2p_get_center_160mhz(struct wpa_supplicant * wpa_s,struct hostapd_hw_modes * mode,u8 channel,const u8 * center_channels,size_t num_chan)4422  static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
4423  				     struct hostapd_hw_modes *mode,
4424  				     u8 channel, const u8 *center_channels,
4425  				     size_t num_chan)
4426  {
4427  	unsigned int i;
4428  
4429  	if (mode->mode != HOSTAPD_MODE_IEEE80211A)
4430  		return 0;
4431  
4432  	for (i = 0; i < num_chan; i++)
4433  		/*
4434  		 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
4435  		 * so the center channel is 14 channels away from the start/end.
4436  		 */
4437  		if (channel >= center_channels[i] - 14 &&
4438  		    channel <= center_channels[i] + 14)
4439  			return center_channels[i];
4440  
4441  	return 0;
4442  }
4443  
4444  
4445  static const u8 center_channels_5ghz_160mhz[] = { 50, 114, 163 };
4446  static const u8 center_channels_6ghz_160mhz[] = { 15, 47, 79, 111, 143, 175,
4447  						  207 };
4448  
wpas_p2p_verify_160mhz(struct wpa_supplicant * wpa_s,struct hostapd_hw_modes * mode,u8 op_class,u8 channel,u8 bw)4449  static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
4450  					       struct hostapd_hw_modes *mode,
4451  					       u8 op_class, u8 channel, u8 bw)
4452  {
4453  	u8 center_chan;
4454  	int i, flags;
4455  	enum chan_allowed res, ret = ALLOWED;
4456  	const u8 *chans;
4457  	size_t num_chans;
4458  
4459  	if (is_6ghz_op_class(op_class)) {
4460  		chans = center_channels_6ghz_160mhz;
4461  		num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4462  	} else {
4463  		chans = center_channels_5ghz_160mhz;
4464  		num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4465  	}
4466  	center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4467  						 chans, num_chans);
4468  	if (!center_chan)
4469  		return NOT_ALLOWED;
4470  	/* VHT 160 MHz uses DFS channels in most countries. */
4471  
4472  	/* Check all the channels are available */
4473  	for (i = 0; i < 8; i++) {
4474  		int adj_chan = center_chan - 14 + i * 4;
4475  
4476  		res = has_channel(wpa_s->global, mode, op_class, adj_chan,
4477  				  &flags);
4478  		if (res == NOT_ALLOWED)
4479  			return NOT_ALLOWED;
4480  
4481  		if (res == RADAR)
4482  			ret = RADAR;
4483  		if (res == NO_IR)
4484  			ret = NO_IR;
4485  
4486  		if (!is_6ghz_op_class(op_class)) {
4487  			if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL) ||
4488  			    !(flags & HOSTAPD_CHAN_VHT_160MHZ_SUBCHANNEL))
4489  				return NOT_ALLOWED;
4490  		} else if (is_6ghz_op_class(op_class) &&
4491  			   (!(wpas_get_6ghz_he_chwidth_capab(mode) &
4492  			      HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G))) {
4493  			return NOT_ALLOWED;
4494  		}
4495  	}
4496  
4497  	return ret;
4498  }
4499  
4500  
wpas_p2p_verify_edmg(struct wpa_supplicant * wpa_s,struct hostapd_hw_modes * mode,u8 channel)4501  static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
4502  					      struct hostapd_hw_modes *mode,
4503  					      u8 channel)
4504  {
4505  	struct ieee80211_edmg_config edmg;
4506  
4507  	hostapd_encode_edmg_chan(1, channel, 0, &edmg);
4508  	if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
4509  		return ALLOWED;
4510  
4511  	return NOT_ALLOWED;
4512  }
4513  
4514  
wpas_p2p_verify_channel(struct wpa_supplicant * wpa_s,struct hostapd_hw_modes * mode,u8 op_class,u8 channel,u8 bw)4515  static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
4516  						 struct hostapd_hw_modes *mode,
4517  						 u8 op_class, u8 channel, u8 bw)
4518  {
4519  	int flag = 0;
4520  	enum chan_allowed res, res2;
4521  
4522  	if (is_6ghz_op_class(op_class) && !is_6ghz_psc_frequency(
4523  			p2p_channel_to_freq(op_class, channel)))
4524  		return NOT_ALLOWED;
4525  
4526  	res2 = res = has_channel(wpa_s->global, mode, op_class, channel, &flag);
4527  	if (bw == BW40MINUS) {
4528  		if (!(flag & HOSTAPD_CHAN_HT40MINUS))
4529  			return NOT_ALLOWED;
4530  		res2 = has_channel(wpa_s->global, mode, op_class, channel - 4,
4531  				   NULL);
4532  	} else if (bw == BW40PLUS) {
4533  		if (!(flag & HOSTAPD_CHAN_HT40PLUS))
4534  			return NOT_ALLOWED;
4535  		res2 = has_channel(wpa_s->global, mode, op_class, channel + 4,
4536  				   NULL);
4537  	} else if (is_6ghz_op_class(op_class) && bw == BW40) {
4538  		if (mode->mode != HOSTAPD_MODE_IEEE80211A)
4539  			return NOT_ALLOWED;
4540  		if (get_6ghz_sec_channel(channel) < 0)
4541  			res2 = has_channel(wpa_s->global, mode, op_class,
4542  					   channel - 4, NULL);
4543  		else
4544  			res2 = has_channel(wpa_s->global, mode, op_class,
4545  					   channel + 4, NULL);
4546  	} else if (bw == BW80) {
4547  		res2 = wpas_p2p_verify_80mhz(wpa_s, mode, op_class, channel,
4548  					     bw);
4549  	} else if (bw == BW160) {
4550  		res2 = wpas_p2p_verify_160mhz(wpa_s, mode, op_class, channel,
4551  					      bw);
4552  	} else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
4553  		return wpas_p2p_verify_edmg(wpa_s, mode, channel);
4554  	}
4555  
4556  	if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
4557  		return NOT_ALLOWED;
4558  	if (res == NO_IR || res2 == NO_IR)
4559  		return NO_IR;
4560  	if (res == RADAR || res2 == RADAR)
4561  		return RADAR;
4562  	return res;
4563  }
4564  
4565  
wpas_p2p_setup_channels(struct wpa_supplicant * wpa_s,struct p2p_channels * chan,struct p2p_channels * cli_chan,bool p2p_disable_6ghz)4566  static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
4567  				   struct p2p_channels *chan,
4568  				   struct p2p_channels *cli_chan,
4569  				   bool p2p_disable_6ghz)
4570  {
4571  	struct hostapd_hw_modes *mode;
4572  	int cla, op, cli_cla;
4573  
4574  	if (wpa_s->hw.modes == NULL) {
4575  		wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
4576  			   "of all supported channels; assume dualband "
4577  			   "support");
4578  		return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
4579  	}
4580  
4581  	cla = cli_cla = 0;
4582  
4583  	for (op = 0; global_op_class[op].op_class; op++) {
4584  		const struct oper_class_map *o = &global_op_class[op];
4585  		unsigned int ch;
4586  		struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
4587  
4588  		if (o->p2p == NO_P2P_SUPP ||
4589  		    (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
4590  			continue;
4591  
4592  		mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
4593  				is_6ghz_op_class(o->op_class));
4594  		if (mode == NULL)
4595  			continue;
4596  		if (mode->mode == HOSTAPD_MODE_IEEE80211G)
4597  			wpa_s->global->p2p_24ghz_social_channels = 1;
4598  		for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
4599  			enum chan_allowed res;
4600  
4601  			/* Check for non-continuous jump in channel index
4602  			 * incrementation */
4603  			if ((o->op_class >= 128 && o->op_class <= 130) &&
4604  			    ch < 149 && ch + o->inc > 149)
4605  				ch = 149;
4606  
4607  			res = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4608  						      ch, o->bw);
4609  			if (res == ALLOWED) {
4610  				if (reg == NULL) {
4611  					if (cla == P2P_MAX_REG_CLASSES)
4612  						continue;
4613  					wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
4614  						   o->op_class);
4615  					reg = &chan->reg_class[cla];
4616  					cla++;
4617  					reg->reg_class = o->op_class;
4618  				}
4619  				if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
4620  					continue;
4621  				reg->channel[reg->channels] = ch;
4622  				reg->channels++;
4623  			} else if (res == NO_IR &&
4624  				   wpa_s->conf->p2p_add_cli_chan) {
4625  				if (cli_reg == NULL) {
4626  					if (cli_cla == P2P_MAX_REG_CLASSES)
4627  						continue;
4628  					wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
4629  						   o->op_class);
4630  					cli_reg = &cli_chan->reg_class[cli_cla];
4631  					cli_cla++;
4632  					cli_reg->reg_class = o->op_class;
4633  				}
4634  				if (cli_reg->channels ==
4635  				    P2P_MAX_REG_CLASS_CHANNELS)
4636  					continue;
4637  				cli_reg->channel[cli_reg->channels] = ch;
4638  				cli_reg->channels++;
4639  			}
4640  		}
4641  		if (reg) {
4642  			wpa_hexdump(MSG_DEBUG, "P2P: Channels",
4643  				    reg->channel, reg->channels);
4644  		}
4645  		if (cli_reg) {
4646  			wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
4647  				    cli_reg->channel, cli_reg->channels);
4648  		}
4649  	}
4650  
4651  	chan->reg_classes = cla;
4652  	cli_chan->reg_classes = cli_cla;
4653  
4654  	return 0;
4655  }
4656  
4657  
wpas_p2p_get_sec_channel_offset_40mhz(struct wpa_supplicant * wpa_s,struct hostapd_hw_modes * mode,u8 channel)4658  int wpas_p2p_get_sec_channel_offset_40mhz(struct wpa_supplicant *wpa_s,
4659  					  struct hostapd_hw_modes *mode,
4660  					  u8 channel)
4661  {
4662  	int op;
4663  	enum chan_allowed ret;
4664  
4665  	for (op = 0; global_op_class[op].op_class; op++) {
4666  		const struct oper_class_map *o = &global_op_class[op];
4667  		u16 ch = 0;
4668  
4669  		/* Allow DFS channels marked as NO_P2P_SUPP to be used with
4670  		 * driver offloaded DFS. */
4671  		if ((o->p2p == NO_P2P_SUPP &&
4672  		     (!is_dfs_global_op_class(o->op_class) ||
4673  		      !wpa_s->p2p_go_allow_dfs)) ||
4674  		    (is_6ghz_op_class(o->op_class) &&
4675  		     wpa_s->conf->p2p_6ghz_disable))
4676  			continue;
4677  
4678  		/* IEEE Std 802.11ax-2021 26.17.2.3.2: "A 6 GHz-only AP should
4679  		 * set up the BSS with a primary 20 MHz channel that coincides
4680  		 * with a preferred scanning channel (PSC)."
4681  		 * 6 GHz BW40 operation class 132 in wpa_supplicant uses the
4682  		 * lowest 20 MHz channel for simplicity, so increase ch by 4 to
4683  		 * match the PSC.
4684  		 */
4685  		if (is_6ghz_op_class(o->op_class) && o->bw == BW40 &&
4686  		    get_6ghz_sec_channel(channel) < 0)
4687  			ch = 4;
4688  
4689  		for (ch += o->min_chan; ch <= o->max_chan; ch += o->inc) {
4690  			if (o->mode != HOSTAPD_MODE_IEEE80211A ||
4691  			    (o->bw != BW40PLUS && o->bw != BW40MINUS &&
4692  			     o->bw != BW40) ||
4693  			    ch != channel)
4694  				continue;
4695  			ret = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4696  						      ch, o->bw);
4697  			if (ret == ALLOWED) {
4698  				if (is_6ghz_op_class(o->op_class) &&
4699  				    o->bw == BW40)
4700  					return get_6ghz_sec_channel(channel);
4701  				return (o->bw == BW40MINUS) ? -1 : 1;
4702  			}
4703  			if (ret == RADAR && wpa_s->p2p_go_allow_dfs) {
4704  				/* Allow RADAR channels used for driver
4705  				 * offloaded DFS */
4706  				return (o->bw == BW40MINUS) ? -1 : 1;
4707  			}
4708  		}
4709  	}
4710  	return 0;
4711  }
4712  
4713  
wpas_p2p_get_vht80_center(struct wpa_supplicant * wpa_s,struct hostapd_hw_modes * mode,u8 channel,u8 op_class)4714  int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
4715  			      struct hostapd_hw_modes *mode, u8 channel,
4716  			      u8 op_class)
4717  {
4718  	const u8 *chans;
4719  	size_t num_chans;
4720  	enum chan_allowed ret;
4721  
4722  	ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW80);
4723  	if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
4724  		return 0;
4725  
4726  	if (is_6ghz_op_class(op_class)) {
4727  		chans = center_channels_6ghz_80mhz;
4728  		num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4729  	} else {
4730  		chans = center_channels_5ghz_80mhz;
4731  		num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4732  	}
4733  	return wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4734  					 chans, num_chans);
4735  }
4736  
4737  
wpas_p2p_get_vht160_center(struct wpa_supplicant * wpa_s,struct hostapd_hw_modes * mode,u8 channel,u8 op_class)4738  int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
4739  			       struct hostapd_hw_modes *mode, u8 channel,
4740  			       u8 op_class)
4741  {
4742  	const u8 *chans;
4743  	size_t num_chans;
4744  	enum chan_allowed ret;
4745  
4746  	ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW160);
4747  	if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
4748  		return 0;
4749  	if (is_6ghz_op_class(op_class)) {
4750  		chans = center_channels_6ghz_160mhz;
4751  		num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4752  	} else {
4753  		chans = center_channels_5ghz_160mhz;
4754  		num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4755  	}
4756  	return wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4757  					  chans, num_chans);
4758  }
4759  
4760  
wpas_get_noa(void * ctx,const u8 * interface_addr,u8 * buf,size_t buf_len)4761  static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4762  			size_t buf_len)
4763  {
4764  	struct wpa_supplicant *wpa_s = ctx;
4765  
4766  	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4767  		if (ether_addr_equal(wpa_s->own_addr, interface_addr))
4768  			break;
4769  	}
4770  	if (wpa_s == NULL)
4771  		return -1;
4772  
4773  	return wpa_drv_get_noa(wpa_s, buf, buf_len);
4774  }
4775  
4776  
wpas_get_p2p_go_iface(struct wpa_supplicant * wpa_s,const u8 * ssid,size_t ssid_len)4777  struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4778  					      const u8 *ssid, size_t ssid_len)
4779  {
4780  	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4781  		struct wpa_ssid *s = wpa_s->current_ssid;
4782  		if (s == NULL)
4783  			continue;
4784  		if (s->mode != WPAS_MODE_P2P_GO &&
4785  		    s->mode != WPAS_MODE_AP &&
4786  		    s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4787  			continue;
4788  		if (s->ssid_len != ssid_len ||
4789  		    os_memcmp(ssid, s->ssid, ssid_len) != 0)
4790  			continue;
4791  		return wpa_s;
4792  	}
4793  
4794  	return NULL;
4795  
4796  }
4797  
4798  
wpas_get_p2p_client_iface(struct wpa_supplicant * wpa_s,const u8 * peer_dev_addr)4799  struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4800  						  const u8 *peer_dev_addr)
4801  {
4802  	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4803  		struct wpa_ssid *ssid = wpa_s->current_ssid;
4804  		if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
4805  			continue;
4806  		if (ether_addr_equal(wpa_s->go_dev_addr, peer_dev_addr))
4807  			return wpa_s;
4808  	}
4809  
4810  	return NULL;
4811  }
4812  
4813  
wpas_go_connected(void * ctx,const u8 * dev_addr)4814  static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4815  {
4816  	struct wpa_supplicant *wpa_s = ctx;
4817  
4818  	return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
4819  }
4820  
4821  
wpas_is_concurrent_session_active(void * ctx)4822  static int wpas_is_concurrent_session_active(void *ctx)
4823  {
4824  	struct wpa_supplicant *wpa_s = ctx;
4825  	struct wpa_supplicant *ifs;
4826  
4827  	for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4828  		if (ifs == wpa_s)
4829  			continue;
4830  		if (ifs->wpa_state > WPA_ASSOCIATED)
4831  			return 1;
4832  	}
4833  	return 0;
4834  }
4835  
4836  
wpas_p2p_debug_print(void * ctx,int level,const char * msg)4837  static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4838  {
4839  	struct wpa_supplicant *wpa_s = ctx;
4840  	wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4841  }
4842  
4843  
wpas_p2p_add_p2pdev_interface(struct wpa_supplicant * wpa_s,const char * conf_p2p_dev)4844  int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4845  				  const char *conf_p2p_dev)
4846  {
4847  	struct wpa_interface iface;
4848  	struct wpa_supplicant *p2pdev_wpa_s;
4849  	char ifname[100];
4850  	char force_name[100];
4851  	int ret;
4852  	const u8 *if_addr = NULL;
4853  
4854  	ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4855  			  wpa_s->ifname);
4856  	if (os_snprintf_error(sizeof(ifname), ret))
4857  		return -1;
4858  	/* Cut length at the maximum size. Note that we don't need to ensure
4859  	 * collision free names here as the created interface is not a netdev.
4860  	 */
4861  	ifname[IFNAMSIZ - 1] = '\0';
4862  	force_name[0] = '\0';
4863  	wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
4864  
4865  	if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4866  	    !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4867  		if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4868  
4869  	ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
4870  			     force_name, wpa_s->pending_interface_addr, NULL);
4871  	if (ret < 0) {
4872  		wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4873  		return ret;
4874  	}
4875  	os_strlcpy(wpa_s->pending_interface_name, ifname,
4876  		   sizeof(wpa_s->pending_interface_name));
4877  
4878  	os_memset(&iface, 0, sizeof(iface));
4879  	iface.p2p_mgmt = 1;
4880  	iface.ifname = wpa_s->pending_interface_name;
4881  	iface.driver = wpa_s->driver->name;
4882  	iface.driver_param = wpa_s->conf->driver_param;
4883  
4884  	/*
4885  	 * If a P2P Device configuration file was given, use it as the interface
4886  	 * configuration file (instead of using parent's configuration file.
4887  	 */
4888  	if (conf_p2p_dev) {
4889  		iface.confname = conf_p2p_dev;
4890  		iface.ctrl_interface = NULL;
4891  	} else {
4892  		iface.confname = wpa_s->confname;
4893  		iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4894  	}
4895  
4896  	p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
4897  	if (!p2pdev_wpa_s) {
4898  		wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4899  		return -1;
4900  	}
4901  
4902  	p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
4903  	wpa_s->pending_interface_name[0] = '\0';
4904  	return 0;
4905  }
4906  
4907  
wpas_presence_resp(void * ctx,const u8 * src,u8 status,const u8 * noa,size_t noa_len)4908  static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4909  			       const u8 *noa, size_t noa_len)
4910  {
4911  	struct wpa_supplicant *wpa_s, *intf = ctx;
4912  	char hex[100];
4913  
4914  	for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4915  		if (wpa_s->waiting_presence_resp)
4916  			break;
4917  	}
4918  	if (!wpa_s) {
4919  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4920  		return;
4921  	}
4922  	wpa_s->waiting_presence_resp = 0;
4923  
4924  	wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4925  	wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4926  		" status=%u noa=%s", MAC2STR(src), status, hex);
4927  }
4928  
4929  
wpas_get_persistent_group(void * ctx,const u8 * addr,const u8 * ssid,size_t ssid_len,u8 * go_dev_addr,u8 * ret_ssid,size_t * ret_ssid_len,u8 * intended_iface_addr)4930  static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4931  				     size_t ssid_len, u8 *go_dev_addr,
4932  				     u8 *ret_ssid, size_t *ret_ssid_len,
4933  				     u8 *intended_iface_addr)
4934  {
4935  	struct wpa_supplicant *wpa_s = ctx;
4936  	struct wpa_ssid *s;
4937  
4938  	s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4939  	if (s) {
4940  		os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4941  		*ret_ssid_len = s->ssid_len;
4942  		os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
4943  
4944  		if (s->mode != WPAS_MODE_P2P_GO) {
4945  			os_memset(intended_iface_addr, 0, ETH_ALEN);
4946  		} else if (wpas_p2p_create_iface(wpa_s)) {
4947  			if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4948  				return 0;
4949  
4950  			os_memcpy(intended_iface_addr,
4951  				  wpa_s->pending_interface_addr, ETH_ALEN);
4952  		} else {
4953  			os_memcpy(intended_iface_addr, wpa_s->own_addr,
4954  				  ETH_ALEN);
4955  		}
4956  		return 1;
4957  	}
4958  
4959  	return 0;
4960  }
4961  
4962  
wpas_get_go_info(void * ctx,u8 * intended_addr,u8 * ssid,size_t * ssid_len,int * group_iface,unsigned int * freq)4963  static int wpas_get_go_info(void *ctx, u8 *intended_addr,
4964  			    u8 *ssid, size_t *ssid_len, int *group_iface,
4965  			    unsigned int *freq)
4966  {
4967  	struct wpa_supplicant *wpa_s = ctx;
4968  	struct wpa_supplicant *go;
4969  	struct wpa_ssid *s;
4970  
4971  	/*
4972  	 * group_iface will be set to 1 only if a dedicated interface for P2P
4973  	 * role is required. First, we try to reuse an active GO. However,
4974  	 * if it is not present, we will try to reactivate an existing
4975  	 * persistent group and set group_iface to 1, so the caller will know
4976  	 * that the pending interface should be used.
4977  	 */
4978  	*group_iface = 0;
4979  
4980  	if (freq)
4981  		*freq = 0;
4982  
4983  	go = wpas_p2p_get_go_group(wpa_s);
4984  	if (!go) {
4985  		s = wpas_p2p_get_persistent_go(wpa_s);
4986  		*group_iface = wpas_p2p_create_iface(wpa_s);
4987  		if (s)
4988  			os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4989  		else
4990  			return 0;
4991  	} else {
4992  		s = go->current_ssid;
4993  		os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4994  		if (freq)
4995  			*freq = go->assoc_freq;
4996  	}
4997  
4998  	os_memcpy(ssid, s->ssid, s->ssid_len);
4999  	*ssid_len = s->ssid_len;
5000  
5001  	return 1;
5002  }
5003  
5004  
wpas_remove_stale_groups(void * ctx,const u8 * peer,const u8 * go,const u8 * ssid,size_t ssid_len)5005  static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
5006  				    const u8 *ssid, size_t ssid_len)
5007  {
5008  	struct wpa_supplicant *wpa_s = ctx;
5009  	struct wpa_ssid *s;
5010  	int save_config = 0;
5011  	size_t i;
5012  
5013  	/* Start with our first choice of Persistent Groups */
5014  	while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
5015  		if (go && ssid && ssid_len &&
5016  		    s->ssid_len == ssid_len &&
5017  		    ether_addr_equal(go, s->bssid) &&
5018  		    os_memcmp(ssid, s->ssid, ssid_len) == 0)
5019  			break;
5020  
5021  		/* Remove stale persistent group */
5022  		if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
5023  			wpa_dbg(wpa_s, MSG_DEBUG,
5024  				"P2P: Remove stale persistent group id=%d",
5025  				s->id);
5026  			wpas_notify_persistent_group_removed(wpa_s, s);
5027  			wpa_config_remove_network(wpa_s->conf, s->id);
5028  			save_config = 1;
5029  			continue;
5030  		}
5031  
5032  		for (i = 0; i < s->num_p2p_clients; i++) {
5033  			if (!ether_addr_equal(s->p2p_client_list +
5034  					      i * 2 * ETH_ALEN, peer))
5035  				continue;
5036  
5037  			os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
5038  				   s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
5039  				   (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
5040  			break;
5041  		}
5042  		s->num_p2p_clients--;
5043  		save_config = 1;
5044  	}
5045  
5046  	if (save_config)
5047  		p2p_config_write(wpa_s);
5048  
5049  	/* Return TRUE if valid SSID remains */
5050  	return s != NULL;
5051  }
5052  
5053  
wpas_p2ps_get_feat_cap_str(char * buf,size_t buf_len,const u8 * feat_cap,size_t feat_cap_len)5054  static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
5055  				       const u8 *feat_cap, size_t feat_cap_len)
5056  {
5057  	static const char pref[] = " feature_cap=";
5058  	int ret;
5059  
5060  	buf[0] = '\0';
5061  
5062  	/*
5063  	 * We expect a feature capability to contain at least one byte to be
5064  	 * reported. The string buffer provided by the caller function is
5065  	 * expected to be big enough to contain all bytes of the attribute for
5066  	 * known specifications. This function truncates the reported bytes if
5067  	 * the feature capability data exceeds the string buffer size.
5068  	 */
5069  	if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
5070  		return;
5071  
5072  	os_memcpy(buf, pref, sizeof(pref));
5073  	ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
5074  			       buf_len - sizeof(pref) + 1,
5075  			       feat_cap, feat_cap_len);
5076  
5077  	if (ret != (2 * (int) feat_cap_len))
5078  		wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
5079  }
5080  
5081  
wpas_p2ps_prov_complete(void * ctx,enum p2p_status_code status,const u8 * dev,const u8 * adv_mac,const u8 * ses_mac,const u8 * grp_mac,u32 adv_id,u32 ses_id,u8 conncap,int passwd_id,const u8 * persist_ssid,size_t persist_ssid_size,int response_done,int prov_start,const char * session_info,const u8 * feat_cap,size_t feat_cap_len,unsigned int freq,const u8 * group_ssid,size_t group_ssid_len)5082  static void wpas_p2ps_prov_complete(void *ctx, enum p2p_status_code status,
5083  				    const u8 *dev,
5084  				    const u8 *adv_mac, const u8 *ses_mac,
5085  				    const u8 *grp_mac, u32 adv_id, u32 ses_id,
5086  				    u8 conncap, int passwd_id,
5087  				    const u8 *persist_ssid,
5088  				    size_t persist_ssid_size, int response_done,
5089  				    int prov_start, const char *session_info,
5090  				    const u8 *feat_cap, size_t feat_cap_len,
5091  				    unsigned int freq,
5092  				    const u8 *group_ssid, size_t group_ssid_len)
5093  {
5094  	struct wpa_supplicant *wpa_s = ctx;
5095  	u8 mac[ETH_ALEN];
5096  	struct wpa_ssid *persistent_go, *stale, *s = NULL;
5097  	int save_config = 0;
5098  	struct wpa_supplicant *go_wpa_s;
5099  	char feat_cap_str[256];
5100  
5101  	if (!dev)
5102  		return;
5103  
5104  	os_memset(mac, 0, ETH_ALEN);
5105  	if (!adv_mac)
5106  		adv_mac = mac;
5107  	if (!ses_mac)
5108  		ses_mac = mac;
5109  	if (!grp_mac)
5110  		grp_mac = mac;
5111  
5112  	wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
5113  				   feat_cap, feat_cap_len);
5114  
5115  	if (prov_start) {
5116  		if (session_info == NULL) {
5117  			wpa_msg_global(wpa_s, MSG_INFO,
5118  				       P2P_EVENT_P2PS_PROVISION_START MACSTR
5119  				       " adv_id=%x conncap=%x"
5120  				       " adv_mac=" MACSTR
5121  				       " session=%x mac=" MACSTR
5122  				       " dev_passwd_id=%d%s",
5123  				       MAC2STR(dev), adv_id, conncap,
5124  				       MAC2STR(adv_mac),
5125  				       ses_id, MAC2STR(ses_mac),
5126  				       passwd_id, feat_cap_str);
5127  		} else {
5128  			wpa_msg_global(wpa_s, MSG_INFO,
5129  				       P2P_EVENT_P2PS_PROVISION_START MACSTR
5130  				       " adv_id=%x conncap=%x"
5131  				       " adv_mac=" MACSTR
5132  				       " session=%x mac=" MACSTR
5133  				       " dev_passwd_id=%d info='%s'%s",
5134  				       MAC2STR(dev), adv_id, conncap,
5135  				       MAC2STR(adv_mac),
5136  				       ses_id, MAC2STR(ses_mac),
5137  				       passwd_id, session_info, feat_cap_str);
5138  		}
5139  		return;
5140  	}
5141  
5142  	go_wpa_s = wpas_p2p_get_go_group(wpa_s);
5143  	persistent_go = wpas_p2p_get_persistent_go(wpa_s);
5144  
5145  	if (status && status != P2P_SC_SUCCESS_DEFERRED) {
5146  		if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
5147  			wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
5148  
5149  		if (persistent_go && !persistent_go->num_p2p_clients) {
5150  			/* remove empty persistent GO */
5151  			wpa_dbg(wpa_s, MSG_DEBUG,
5152  				"P2P: Remove empty persistent group id=%d",
5153  				persistent_go->id);
5154  			wpas_notify_persistent_group_removed(wpa_s,
5155  							     persistent_go);
5156  			wpa_config_remove_network(wpa_s->conf,
5157  						  persistent_go->id);
5158  		}
5159  
5160  		wpa_msg_global(wpa_s, MSG_INFO,
5161  			       P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5162  			       " status=%d"
5163  			       " adv_id=%x adv_mac=" MACSTR
5164  			       " session=%x mac=" MACSTR "%s",
5165  			       MAC2STR(dev), status,
5166  			       adv_id, MAC2STR(adv_mac),
5167  			       ses_id, MAC2STR(ses_mac), feat_cap_str);
5168  		return;
5169  	}
5170  
5171  	/* Clean up stale persistent groups with this device */
5172  	if (persist_ssid && persist_ssid_size)
5173  		s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
5174  					    persist_ssid_size);
5175  
5176  	if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
5177  	    is_zero_ether_addr(grp_mac)) {
5178  		wpa_dbg(wpa_s, MSG_ERROR,
5179  			"P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
5180  		return;
5181  	}
5182  
5183  	for (;;) {
5184  		stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
5185  		if (!stale)
5186  			break;
5187  
5188  		if (s && s->ssid_len == stale->ssid_len &&
5189  		    ether_addr_equal(stale->bssid, s->bssid) &&
5190  		    os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
5191  			break;
5192  
5193  		/* Remove stale persistent group */
5194  		if (stale->mode != WPAS_MODE_P2P_GO ||
5195  		    stale->num_p2p_clients <= 1) {
5196  			wpa_dbg(wpa_s, MSG_DEBUG,
5197  				"P2P: Remove stale persistent group id=%d",
5198  				stale->id);
5199  			wpas_notify_persistent_group_removed(wpa_s, stale);
5200  			wpa_config_remove_network(wpa_s->conf, stale->id);
5201  		} else {
5202  			size_t i;
5203  
5204  			for (i = 0; i < stale->num_p2p_clients; i++) {
5205  				if (ether_addr_equal(stale->p2p_client_list +
5206  						     i * ETH_ALEN, dev)) {
5207  					os_memmove(stale->p2p_client_list +
5208  						   i * ETH_ALEN,
5209  						   stale->p2p_client_list +
5210  						   (i + 1) * ETH_ALEN,
5211  						   (stale->num_p2p_clients -
5212  						    i - 1) * ETH_ALEN);
5213  					break;
5214  				}
5215  			}
5216  			stale->num_p2p_clients--;
5217  		}
5218  		save_config = 1;
5219  	}
5220  
5221  	if (save_config)
5222  		p2p_config_write(wpa_s);
5223  
5224  	if (s) {
5225  		if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
5226  			wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
5227  
5228  		if (persistent_go && s != persistent_go &&
5229  		    !persistent_go->num_p2p_clients) {
5230  			/* remove empty persistent GO */
5231  			wpa_dbg(wpa_s, MSG_DEBUG,
5232  				"P2P: Remove empty persistent group id=%d",
5233  				persistent_go->id);
5234  			wpas_notify_persistent_group_removed(wpa_s,
5235  							     persistent_go);
5236  			wpa_config_remove_network(wpa_s->conf,
5237  						  persistent_go->id);
5238  			/* Save config */
5239  		}
5240  
5241  		wpa_msg_global(wpa_s, MSG_INFO,
5242  			       P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5243  			       " status=%d"
5244  			       " adv_id=%x adv_mac=" MACSTR
5245  			       " session=%x mac=" MACSTR
5246  			       " persist=%d%s",
5247  			       MAC2STR(dev), status,
5248  			       adv_id, MAC2STR(adv_mac),
5249  			       ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
5250  		return;
5251  	}
5252  
5253  	wpa_s->global->pending_p2ps_group = 0;
5254  	wpa_s->global->pending_p2ps_group_freq = 0;
5255  
5256  	if (conncap == P2PS_SETUP_GROUP_OWNER) {
5257  		/*
5258  		 * We need to copy the interface name. Simply saving a
5259  		 * pointer isn't enough, since if we use pending_interface_name
5260  		 * it will be overwritten when the group is added.
5261  		 */
5262  		char go_ifname[100];
5263  
5264  		go_ifname[0] = '\0';
5265  		if (!go_wpa_s) {
5266  			if (!response_done) {
5267  				wpa_s->global->pending_p2ps_group = 1;
5268  				wpa_s->global->pending_p2ps_group_freq = freq;
5269  			}
5270  
5271  			if (!wpas_p2p_create_iface(wpa_s))
5272  				os_memcpy(go_ifname, wpa_s->ifname,
5273  					  sizeof(go_ifname));
5274  			else if (wpa_s->pending_interface_name[0])
5275  				os_memcpy(go_ifname,
5276  					  wpa_s->pending_interface_name,
5277  					  sizeof(go_ifname));
5278  
5279  			if (!go_ifname[0]) {
5280  				wpas_p2ps_prov_complete(
5281  					wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
5282  					dev, adv_mac, ses_mac,
5283  					grp_mac, adv_id, ses_id, 0, 0,
5284  					NULL, 0, 0, 0, NULL, NULL, 0, 0,
5285  					NULL, 0);
5286  				return;
5287  			}
5288  
5289  			/* If PD Resp complete, start up the GO */
5290  			if (response_done && persistent_go) {
5291  				wpas_p2p_group_add_persistent(
5292  					wpa_s, persistent_go,
5293  					0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
5294  					persistent_go->mode ==
5295  					WPAS_MODE_P2P_GO ?
5296  					P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
5297  					0, 0, false, 0, NULL, NULL, NULL, NULL,
5298  					0);
5299  			} else if (response_done) {
5300  				wpas_p2p_group_add(wpa_s, 1, freq,
5301  						   0, 0, 0, 0, 0, 0, false,
5302  						   wpa_s->p2p2,
5303  						   WPA_P2P_MODE_WFD_R1);
5304  			}
5305  
5306  			if (passwd_id == DEV_PW_P2PS_DEFAULT) {
5307  				os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
5308  					  ETH_ALEN);
5309  				wpa_s->p2ps_method_config_any = 1;
5310  			}
5311  		} else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
5312  			os_memcpy(go_ifname, go_wpa_s->ifname,
5313  				  sizeof(go_ifname));
5314  
5315  			if (is_zero_ether_addr(grp_mac)) {
5316  				wpa_dbg(go_wpa_s, MSG_DEBUG,
5317  					"P2P: Setting PIN-1 for ANY");
5318  				wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
5319  							  "12345670", NULL, 0,
5320  							  0);
5321  			} else {
5322  				wpa_dbg(go_wpa_s, MSG_DEBUG,
5323  					"P2P: Setting PIN-1 for " MACSTR,
5324  					MAC2STR(grp_mac));
5325  				wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
5326  							  "12345670", NULL, 0,
5327  							  0);
5328  			}
5329  
5330  			os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
5331  			wpa_s->p2ps_method_config_any = 1;
5332  		}
5333  
5334  		wpa_msg_global(wpa_s, MSG_INFO,
5335  			       P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5336  			       " status=%d conncap=%x"
5337  			       " adv_id=%x adv_mac=" MACSTR
5338  			       " session=%x mac=" MACSTR
5339  			       " dev_passwd_id=%d go=%s%s",
5340  			       MAC2STR(dev), status, conncap,
5341  			       adv_id, MAC2STR(adv_mac),
5342  			       ses_id, MAC2STR(ses_mac),
5343  			       passwd_id, go_ifname, feat_cap_str);
5344  		return;
5345  	}
5346  
5347  	if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
5348  		wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
5349  
5350  	if (persistent_go && !persistent_go->num_p2p_clients) {
5351  		/* remove empty persistent GO */
5352  		wpa_dbg(wpa_s, MSG_DEBUG,
5353  			"P2P: Remove empty persistent group id=%d",
5354  			persistent_go->id);
5355  		wpas_notify_persistent_group_removed(wpa_s, persistent_go);
5356  		wpa_config_remove_network(wpa_s->conf, persistent_go->id);
5357  	}
5358  
5359  	if (conncap == P2PS_SETUP_CLIENT) {
5360  		char ssid_hex[32 * 2 + 1];
5361  
5362  		if (group_ssid)
5363  			wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
5364  					 group_ssid, group_ssid_len);
5365  		else
5366  			ssid_hex[0] = '\0';
5367  		wpa_msg_global(wpa_s, MSG_INFO,
5368  			       P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5369  			       " status=%d conncap=%x"
5370  			       " adv_id=%x adv_mac=" MACSTR
5371  			       " session=%x mac=" MACSTR
5372  			       " dev_passwd_id=%d join=" MACSTR "%s%s%s",
5373  			       MAC2STR(dev), status, conncap,
5374  			       adv_id, MAC2STR(adv_mac),
5375  			       ses_id, MAC2STR(ses_mac),
5376  			       passwd_id, MAC2STR(grp_mac), feat_cap_str,
5377  			       group_ssid ? " group_ssid=" : "", ssid_hex);
5378  	} else {
5379  		wpa_msg_global(wpa_s, MSG_INFO,
5380  			       P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5381  			       " status=%d conncap=%x"
5382  			       " adv_id=%x adv_mac=" MACSTR
5383  			       " session=%x mac=" MACSTR
5384  			       " dev_passwd_id=%d%s",
5385  			       MAC2STR(dev), status, conncap,
5386  			       adv_id, MAC2STR(adv_mac),
5387  			       ses_id, MAC2STR(ses_mac),
5388  			       passwd_id, feat_cap_str);
5389  	}
5390  }
5391  
5392  
_wpas_p2p_in_progress(void * ctx)5393  static int _wpas_p2p_in_progress(void *ctx)
5394  {
5395  	struct wpa_supplicant *wpa_s = ctx;
5396  	return wpas_p2p_in_progress(wpa_s);
5397  }
5398  
5399  
wpas_prov_disc_resp_cb(void * ctx)5400  static int wpas_prov_disc_resp_cb(void *ctx)
5401  {
5402  	struct wpa_supplicant *wpa_s = ctx;
5403  	struct wpa_ssid *persistent_go;
5404  	unsigned int freq;
5405  
5406  	if (!wpa_s->global->pending_p2ps_group)
5407  		return 0;
5408  
5409  	freq = wpa_s->global->pending_p2ps_group_freq;
5410  	wpa_s->global->pending_p2ps_group_freq = 0;
5411  	wpa_s->global->pending_p2ps_group = 0;
5412  
5413  	if (wpas_p2p_get_go_group(wpa_s))
5414  		return 0;
5415  	persistent_go = wpas_p2p_get_persistent_go(wpa_s);
5416  
5417  	if (persistent_go) {
5418  		wpas_p2p_group_add_persistent(
5419  			wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5420  			NULL,
5421  			persistent_go->mode == WPAS_MODE_P2P_GO ?
5422  			P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0,
5423  			is_p2p_allow_6ghz(wpa_s->global->p2p), 0, NULL, NULL,
5424  			NULL, NULL, 0);
5425  	} else {
5426  		wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0,
5427  				   is_p2p_allow_6ghz(wpa_s->global->p2p),
5428  				   wpa_s->p2p2, WPA_P2P_MODE_WFD_R1);
5429  	}
5430  
5431  	return 1;
5432  }
5433  
5434  
wpas_p2p_get_pref_freq_list(void * ctx,int go,unsigned int * len,struct weighted_pcl * freq_list)5435  static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
5436  				       unsigned int *len,
5437  				       struct weighted_pcl *freq_list)
5438  {
5439  	struct wpa_supplicant *wpa_s = ctx;
5440  
5441  	return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
5442  					  WPA_IF_P2P_CLIENT, len, freq_list);
5443  }
5444  
5445  
wpas_p2p_send_bootstrap_comeback(void * eloop_ctx,void * timeout_ctx)5446  static void wpas_p2p_send_bootstrap_comeback(void *eloop_ctx, void *timeout_ctx)
5447  {
5448  	struct wpa_supplicant *wpa_s = eloop_ctx;
5449  
5450  	wpa_printf(MSG_DEBUG, "P2P2: Send bootstrapping comeback PD Request");
5451  
5452  	if (wpa_s->p2p_pd_before_go_neg) {
5453  		wpas_p2p_connect(wpa_s, wpa_s->p2p_bootstrap_dev_addr,
5454  				 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5455  				 wpa_s->p2p_persistent_group, 0, 0, 0,
5456  				 wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5457  				 wpa_s->p2p_go_vht_center_freq2,
5458  				 wpa_s->p2p_persistent_id, 1,
5459  				 wpa_s->p2p_go_ht40,
5460  				 wpa_s->p2p_go_vht,
5461  				 wpa_s->p2p_go_max_oper_chwidth,
5462  				 wpa_s->p2p_go_he,
5463  				 wpa_s->p2p_go_edmg,
5464  				 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p),
5465  				 wpa_s->p2p2, wpa_s->p2p_bootstrap, NULL,
5466  				 false);
5467  	} else {
5468  		p2p_prov_disc_req(wpa_s->global->p2p,
5469  				  wpa_s->p2p_bootstrap_dev_addr, NULL,
5470  				  0, 0, 0, 1);
5471  	}
5472  }
5473  
5474  
wpas_p2p_register_bootstrap_comeback(void * ctx,const u8 * addr,u16 comeback_after)5475  static void wpas_p2p_register_bootstrap_comeback(void *ctx, const u8 *addr,
5476  						 u16 comeback_after)
5477  {
5478  	unsigned int timeout_us;
5479  	struct wpa_supplicant *wpa_s = ctx;
5480  
5481  	timeout_us = comeback_after * 1024;
5482  	os_memcpy(wpa_s->p2p_bootstrap_dev_addr, addr, ETH_ALEN);
5483  
5484  	eloop_cancel_timeout(wpas_p2p_send_bootstrap_comeback, wpa_s, NULL);
5485  	eloop_register_timeout(0, timeout_us, wpas_p2p_send_bootstrap_comeback,
5486  			       wpa_s, NULL);
5487  }
5488  
5489  
wpas_bootstrap_req_rx(void * ctx,const u8 * addr,u16 bootstrap_method)5490  static void wpas_bootstrap_req_rx(void *ctx, const u8 *addr,
5491  				  u16 bootstrap_method)
5492  {
5493  	struct wpa_supplicant *wpa_s = ctx;
5494  
5495  	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_BOOTSTRAP_REQUEST MACSTR
5496  		       " bootstrap_method=%u", MAC2STR(addr), bootstrap_method);
5497  
5498  	wpas_notify_p2p_bootstrap_req(wpa_s, addr, bootstrap_method);
5499  }
5500  
5501  
wpas_bootstrap_rsp_rx(void * ctx,const u8 * addr,enum p2p_status_code status,int freq,u16 bootstrap_method)5502  static void wpas_bootstrap_rsp_rx(void *ctx, const u8 *addr,
5503  				  enum p2p_status_code status, int freq,
5504  				  u16 bootstrap_method)
5505  {
5506  	struct wpa_supplicant *wpa_s = ctx;
5507  
5508  	wpas_notify_p2p_bootstrap_rsp(wpa_s, addr, status, bootstrap_method);
5509  
5510  	if (status) {
5511  		wpa_msg_global(wpa_s, MSG_INFO,
5512  			       P2P_EVENT_BOOTSTRAP_FAILURE MACSTR " status=%d",
5513  			       MAC2STR(addr), status);
5514  		return;
5515  	}
5516  
5517  	wpa_msg_global(wpa_s, MSG_INFO,
5518  		       P2P_EVENT_BOOTSTRAP_SUCCESS MACSTR " status=%d",
5519  		       MAC2STR(addr), status);
5520  
5521  #ifdef CONFIG_PASN
5522  	if (wpa_s->p2p_pd_before_go_neg)
5523  		wpas_p2p_initiate_pasn_auth(wpa_s, addr, freq);
5524  #endif /* CONFIG_PASN */
5525  }
5526  
5527  
wpas_validate_dira(void * ctx,const u8 * peer_addr,const u8 * dira_nonce,const u8 * dira_tag)5528  static int wpas_validate_dira(void *ctx, const u8 *peer_addr,
5529  			      const u8 *dira_nonce, const u8 *dira_tag)
5530  {
5531  	struct wpa_supplicant *wpa_s = ctx;
5532  	int ret;
5533  	u8 tag[DEVICE_MAX_HASH_LEN];
5534  	struct wpa_dev_ik *ik;
5535  	const u8 *addr[3];
5536  	size_t len[3];
5537  	const char *label = "DIR";
5538  
5539  	addr[0] = (const u8 *) label;
5540  	len[0] = DIR_STR_LEN;
5541  	addr[1] = peer_addr;
5542  	len[1] = ETH_ALEN;
5543  	addr[2] = dira_nonce;
5544  	len[2] = DEVICE_IDENTITY_NONCE_LEN;
5545  
5546  	for (ik = wpa_s->conf->identity; ik; ik = ik->next) {
5547  		if (wpabuf_len(ik->dik) != DEVICE_IDENTITY_KEY_LEN ||
5548  		    ik->dik_cipher != DIRA_CIPHER_VERSION_128)
5549  			continue;
5550  
5551  		ret = hmac_sha256_vector(wpabuf_head(ik->dik),
5552  					 DEVICE_IDENTITY_KEY_LEN,
5553  					 3, addr, len, tag);
5554  		if (ret < 0) {
5555  			wpa_printf(MSG_ERROR,
5556  				   "P2P2: Failed to derive DIRA Tag");
5557  			return 0;
5558  		}
5559  
5560  		if (os_memcmp(tag, dira_tag, DEVICE_IDENTITY_TAG_LEN) == 0) {
5561  			wpa_printf(MSG_DEBUG, "P2P2: DIRA Tag matched");
5562  			break;
5563  		}
5564  	}
5565  
5566  	if (!ik)
5567  		return 0;
5568  
5569  #ifdef CONFIG_PASN
5570  	p2p_pasn_pmksa_set_pmk(wpa_s->global->p2p, wpa_s->global->p2p_dev_addr,
5571  			       peer_addr,
5572  			       wpabuf_head(ik->pmk), wpabuf_len(ik->pmk),
5573  			       wpabuf_head(ik->pmkid));
5574  #endif /* CONFIG_PASN */
5575  
5576  	return ik->id;
5577  }
5578  
5579  
5580  #ifdef CONFIG_PASN
5581  
wpas_p2p_initiate_pasn_verify(struct wpa_supplicant * wpa_s,const u8 * peer,enum p2p_invite_role role,const u8 * bssid,const u8 * ssid,size_t ssid_len,unsigned int force_freq,const u8 * go_dev_addr,unsigned int pref_freq)5582  static int wpas_p2p_initiate_pasn_verify(struct wpa_supplicant *wpa_s,
5583  					 const u8 *peer,
5584  					 enum p2p_invite_role role,
5585  					 const u8 *bssid, const u8 *ssid,
5586  					 size_t ssid_len,
5587  					 unsigned int force_freq,
5588  					 const u8 *go_dev_addr,
5589  					 unsigned int pref_freq)
5590  {
5591  	int freq;
5592  	struct wpa_p2p_pasn_auth_work *awork;
5593  
5594  	wpas_p2p_pasn_cancel_auth_work(wpa_s);
5595  	wpa_s->p2p_pasn_auth_work = NULL;
5596  
5597  	freq = p2p_get_listen_freq(wpa_s->global->p2p, peer);
5598  	if (freq == -1)
5599  		return -1;
5600  
5601  	awork = os_zalloc(sizeof(*awork));
5602  	if (!awork)
5603  		return -1;
5604  
5605  	awork->verify = 1;
5606  	awork->role = role;
5607  	awork->freq = freq;
5608  	awork->force_freq = force_freq;
5609  	awork->pref_freq = pref_freq;
5610  	os_memcpy(awork->peer_addr, peer, ETH_ALEN);
5611  	if (go_dev_addr)
5612  		os_memcpy(awork->go_dev_addr, go_dev_addr, ETH_ALEN);
5613  	if (bssid)
5614  		os_memcpy(awork->bssid, bssid, ETH_ALEN);
5615  	if (ssid_len) {
5616  		awork->ssid = os_zalloc(ssid_len);
5617  		if (!awork->ssid) {
5618  			os_free(awork);
5619  			return -1;
5620  		}
5621  		os_memcpy(awork->ssid, ssid, ssid_len);
5622  		awork->ssid_len = ssid_len;
5623  	}
5624  
5625  	if (radio_add_work(wpa_s, freq, "p2p-pasn-start-auth", 1,
5626  			   wpas_p2p_pasn_auth_start_cb, awork) < 0) {
5627  		wpas_p2p_pasn_free_auth_work(awork);
5628  		return -1;
5629  	}
5630  
5631  	wpa_printf(MSG_DEBUG, "P2P PASN: Auth work successfully added");
5632  	return 0;
5633  }
5634  
5635  
wpas_p2p_pasn_send_mgmt(void * ctx,const u8 * data,size_t data_len,int noack,unsigned int freq,unsigned int wait)5636  static int wpas_p2p_pasn_send_mgmt(void *ctx, const u8 *data, size_t data_len,
5637  				   int noack, unsigned int freq,
5638  				   unsigned int wait)
5639  {
5640  	struct wpa_supplicant *wpa_s = ctx;
5641  
5642  	return wpa_drv_send_mlme(wpa_s, data, data_len, noack, freq, wait);
5643  }
5644  
5645  
wpas_p2p_prepare_data_element(void * ctx,const u8 * peer_addr)5646  static int wpas_p2p_prepare_data_element(void *ctx, const u8 *peer_addr)
5647  {
5648  	struct wpa_supplicant *wpa_s = ctx;
5649  	struct p2p_data *p2p = wpa_s->global->p2p;
5650  
5651  	return p2p_prepare_data_element(p2p, peer_addr);
5652  }
5653  
5654  
wpas_p2p_parse_data_element(void * ctx,const u8 * data,size_t len)5655  static int wpas_p2p_parse_data_element(void *ctx, const u8 *data, size_t len)
5656  {
5657  	struct wpa_supplicant *wpa_s = ctx;
5658  	struct p2p_data *p2p = wpa_s->global->p2p;
5659  
5660  	return p2p_parse_data_element(p2p, data, len);
5661  }
5662  
5663  
wpas_p2p_pasn_validate_pmkid(void * ctx,const u8 * addr,const u8 * rsn_pmkid)5664  static int wpas_p2p_pasn_validate_pmkid(void *ctx, const u8 *addr,
5665  					const u8 *rsn_pmkid)
5666  {
5667  	struct wpa_supplicant *wpa_s = ctx;
5668  
5669  	return p2p_pasn_validate_and_update_pmkid(wpa_s->global->p2p, addr,
5670  						  rsn_pmkid);
5671  }
5672  
5673  #endif /* CONFIG_PASN */
5674  
5675  
wpas_p2p_mac_setup(struct wpa_supplicant * wpa_s)5676  int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
5677  {
5678  	u8 addr[ETH_ALEN] = {0};
5679  
5680  	if (wpa_s->conf->p2p_device_random_mac_addr == 0)
5681  		return 0;
5682  
5683  	if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
5684  		if (is_zero_ether_addr(
5685  			    wpa_s->conf->p2p_device_persistent_mac_addr) &&
5686  		    !is_zero_ether_addr(wpa_s->own_addr)) {
5687  			os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
5688  				  wpa_s->own_addr, ETH_ALEN);
5689  		}
5690  		return 0;
5691  	}
5692  
5693  	if (!wpa_s->conf->ssid) {
5694  		if (random_mac_addr(addr) < 0) {
5695  			wpa_msg(wpa_s, MSG_INFO,
5696  				"Failed to generate random MAC address");
5697  			return -EINVAL;
5698  		}
5699  
5700  		/* Store generated MAC address. */
5701  		os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
5702  			  ETH_ALEN);
5703  	} else {
5704  		/* If there are existing saved groups, restore last MAC address.
5705  		 * if there is no last used MAC address, the last one is
5706  		 * factory MAC. */
5707  		if (is_zero_ether_addr(
5708  			    wpa_s->conf->p2p_device_persistent_mac_addr))
5709  			return 0;
5710  		os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
5711  			  ETH_ALEN);
5712  		wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
5713  	}
5714  
5715  	if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
5716  		wpa_msg(wpa_s, MSG_INFO,
5717  			"Failed to set random MAC address");
5718  		return -EINVAL;
5719  	}
5720  
5721  	if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
5722  		wpa_msg(wpa_s, MSG_INFO,
5723  			"Could not update MAC address information");
5724  		return -EINVAL;
5725  	}
5726  
5727  	wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
5728  		MAC2STR(addr));
5729  
5730  	return 0;
5731  }
5732  
5733  
5734  /**
5735   * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
5736   * @global: Pointer to global data from wpa_supplicant_init()
5737   * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5738   * Returns: 0 on success, -1 on failure
5739   */
wpas_p2p_init(struct wpa_global * global,struct wpa_supplicant * wpa_s)5740  int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
5741  {
5742  	struct p2p_config p2p;
5743  	int i;
5744  
5745  	if (wpa_s->conf->p2p_disabled)
5746  		return 0;
5747  
5748  	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5749  		return 0;
5750  
5751  	if (global->p2p)
5752  		return 0;
5753  
5754  	if (wpas_p2p_mac_setup(wpa_s) < 0) {
5755  		wpa_msg(wpa_s, MSG_ERROR,
5756  			"Failed to initialize P2P random MAC address.");
5757  		return -1;
5758  	}
5759  
5760  	os_memset(&p2p, 0, sizeof(p2p));
5761  	p2p.cb_ctx = wpa_s;
5762  	p2p.debug_print = wpas_p2p_debug_print;
5763  	p2p.p2p_scan = wpas_p2p_scan;
5764  	p2p.send_action = wpas_send_action;
5765  	p2p.send_action_done = wpas_send_action_done;
5766  	p2p.go_neg_completed = wpas_go_neg_completed;
5767  	p2p.set_go_security_config = wpas_set_go_security_config;
5768  	p2p.go_neg_req_rx = wpas_go_neg_req_rx;
5769  	p2p.dev_found = wpas_dev_found;
5770  	p2p.dev_lost = wpas_dev_lost;
5771  	p2p.find_stopped = wpas_find_stopped;
5772  	p2p.start_listen = wpas_start_listen;
5773  	p2p.stop_listen = wpas_stop_listen;
5774  	p2p.send_probe_resp = wpas_send_probe_resp;
5775  	p2p.sd_request = wpas_sd_request;
5776  	p2p.sd_response = wpas_sd_response;
5777  	p2p.prov_disc_req = wpas_prov_disc_req;
5778  	p2p.prov_disc_resp = wpas_prov_disc_resp;
5779  	p2p.prov_disc_fail = wpas_prov_disc_fail;
5780  	p2p.invitation_process = wpas_invitation_process;
5781  	p2p.invitation_received = wpas_invitation_received;
5782  	p2p.invitation_result = wpas_invitation_result;
5783  	p2p.get_noa = wpas_get_noa;
5784  	p2p.go_connected = wpas_go_connected;
5785  	p2p.presence_resp = wpas_presence_resp;
5786  	p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
5787  	p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
5788  	p2p.get_persistent_group = wpas_get_persistent_group;
5789  	p2p.get_go_info = wpas_get_go_info;
5790  	p2p.remove_stale_groups = wpas_remove_stale_groups;
5791  	p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
5792  	p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
5793  	p2p.p2ps_group_capability = p2ps_group_capability;
5794  	p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
5795  	p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
5796  	p2p.register_bootstrap_comeback = wpas_p2p_register_bootstrap_comeback;
5797  	p2p.bootstrap_req_rx = wpas_bootstrap_req_rx;
5798  	p2p.bootstrap_rsp_rx = wpas_bootstrap_rsp_rx;
5799  	p2p.validate_dira = wpas_validate_dira;
5800  #ifdef CONFIG_PASN
5801  	p2p.pasn_send_mgmt = wpas_p2p_pasn_send_mgmt;
5802  	p2p.prepare_data_element = wpas_p2p_prepare_data_element;
5803  	p2p.parse_data_element = wpas_p2p_parse_data_element;
5804  	p2p.pasn_validate_pmkid = wpas_p2p_pasn_validate_pmkid;
5805  #endif /* CONFIG_PASN */
5806  
5807  	os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
5808  	os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
5809  	p2p.dev_name = wpa_s->conf->device_name;
5810  	p2p.manufacturer = wpa_s->conf->manufacturer;
5811  	p2p.model_name = wpa_s->conf->model_name;
5812  	p2p.model_number = wpa_s->conf->model_number;
5813  	p2p.serial_number = wpa_s->conf->serial_number;
5814  	if (wpa_s->wps) {
5815  		os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
5816  		p2p.config_methods = wpa_s->wps->config_methods;
5817  	}
5818  
5819  	if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
5820  				    p2p.p2p_6ghz_disable)) {
5821  		wpa_printf(MSG_ERROR,
5822  			   "P2P: Failed to configure supported channel list");
5823  		return -1;
5824  	}
5825  
5826  	if (wpa_s->conf->p2p_listen_reg_class &&
5827  	    wpa_s->conf->p2p_listen_channel) {
5828  		p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
5829  		p2p.channel = wpa_s->conf->p2p_listen_channel;
5830  		p2p.channel_forced = 1;
5831  	} else {
5832  		/*
5833  		 * Pick one of the social channels randomly as the listen
5834  		 * channel.
5835  		 */
5836  		if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
5837  						 &p2p.channel,
5838  						 &global->p2p_go_avoid_freq,
5839  						 &global->p2p_disallow_freq) !=
5840  		    0) {
5841  			wpa_printf(MSG_INFO,
5842  				   "P2P: No social channels supported by the driver - do not enable P2P");
5843  			return 0;
5844  		}
5845  		p2p.channel_forced = 0;
5846  	}
5847  	wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
5848  		   p2p.reg_class, p2p.channel);
5849  
5850  	if (wpa_s->conf->p2p_oper_reg_class &&
5851  	    wpa_s->conf->p2p_oper_channel) {
5852  		p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5853  		p2p.op_channel = wpa_s->conf->p2p_oper_channel;
5854  		p2p.cfg_op_channel = 1;
5855  		wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
5856  			   "%d:%d", p2p.op_reg_class, p2p.op_channel);
5857  
5858  	} else {
5859  		/*
5860  		 * Use random operation channel from 2.4 GHz band social
5861  		 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
5862  		 * other preference is indicated.
5863  		 */
5864  		if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
5865  						 &p2p.op_channel, NULL,
5866  						 NULL) != 0) {
5867  			wpa_printf(MSG_INFO,
5868  				   "P2P: Failed to select random social channel as operation channel");
5869  			p2p.op_reg_class = 0;
5870  			p2p.op_channel = 0;
5871  			/* This will be overridden during group setup in
5872  			 * p2p_prepare_channel(), so allow setup to continue. */
5873  		}
5874  		p2p.cfg_op_channel = 0;
5875  		wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
5876  			   "%d:%d", p2p.op_reg_class, p2p.op_channel);
5877  	}
5878  
5879  	if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
5880  		p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
5881  		p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
5882  	}
5883  
5884  	if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5885  		os_memcpy(p2p.country, wpa_s->conf->country, 2);
5886  		p2p.country[2] = 0x04;
5887  	} else
5888  		os_memcpy(p2p.country, "XX\x04", 3);
5889  
5890  	os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
5891  		  WPS_DEV_TYPE_LEN);
5892  
5893  	p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
5894  	os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
5895  		  p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
5896  
5897  	p2p.concurrent_operations = !!(wpa_s->drv_flags &
5898  				       WPA_DRIVER_FLAGS_P2P_CONCURRENT);
5899  
5900  	p2p.max_peers = 100;
5901  
5902  	if (wpa_s->conf->p2p_ssid_postfix) {
5903  		p2p.ssid_postfix_len =
5904  			os_strlen(wpa_s->conf->p2p_ssid_postfix);
5905  		if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
5906  			p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
5907  		os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
5908  			  p2p.ssid_postfix_len);
5909  	}
5910  
5911  	p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
5912  
5913  	p2p.max_listen = wpa_s->max_remain_on_chan;
5914  
5915  	if (wpa_s->conf->p2p_passphrase_len >= 8 &&
5916  	    wpa_s->conf->p2p_passphrase_len <= 63)
5917  		p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
5918  	else
5919  		p2p.passphrase_len = 8;
5920  
5921  	if (wpa_s->conf->dik &&
5922  	    wpabuf_len(wpa_s->conf->dik) <= DEVICE_IDENTITY_KEY_MAX_LEN) {
5923  		p2p.pairing_config.dik_cipher = wpa_s->conf->dik_cipher;
5924  		p2p.pairing_config.dik_len = wpabuf_len(wpa_s->conf->dik);
5925  		os_memcpy(p2p.pairing_config.dik_data,
5926  			  wpabuf_head(wpa_s->conf->dik),
5927  			  p2p.pairing_config.dik_len);
5928  	} else {
5929  		p2p.pairing_config.dik_cipher = DIRA_CIPHER_VERSION_128;
5930  		p2p.pairing_config.dik_len = DEVICE_IDENTITY_KEY_LEN;
5931  		if (os_get_random(p2p.pairing_config.dik_data,
5932  				  p2p.pairing_config.dik_len) < 0)
5933  			return -1;
5934  
5935  		wpa_s->conf->dik =
5936  			wpabuf_alloc_copy(p2p.pairing_config.dik_data,
5937  					  p2p.pairing_config.dik_len);
5938  		if (!wpa_s->conf->dik)
5939  			return -1;
5940  
5941  		wpa_s->conf->dik_cipher = p2p.pairing_config.dik_cipher;
5942  
5943  		if (wpa_s->conf->update_config &&
5944  		    wpa_config_write(wpa_s->confname, wpa_s->conf))
5945  			wpa_printf(MSG_DEBUG,
5946  				   "P2P: Failed to update configuration");
5947  	}
5948  
5949  	p2p.pairing_config.enable_pairing_setup =
5950  		wpa_s->conf->p2p_pairing_setup;
5951  	p2p.pairing_config.enable_pairing_cache =
5952  		wpa_s->conf->p2p_pairing_cache;
5953  	p2p.pairing_config.bootstrap_methods =
5954  		wpa_s->conf->p2p_bootstrap_methods;
5955  	p2p.pairing_config.pasn_type = wpa_s->conf->p2p_pasn_type;
5956  	p2p.comeback_after = wpa_s->conf->p2p_comeback_after;
5957  	p2p.reg_info = wpa_s->conf->p2p_reg_info;
5958  	p2p.twt_power_mgmt = wpa_s->conf->p2p_twt_power_mgmt;
5959  	p2p.chan_switch_req_enable = wpa_s->conf->p2p_chan_switch_req_enable;
5960  
5961  	global->p2p = p2p_init(&p2p);
5962  	if (global->p2p == NULL)
5963  		return -1;
5964  	global->p2p_init_wpa_s = wpa_s;
5965  
5966  	for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5967  		if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5968  			continue;
5969  		p2p_add_wps_vendor_extension(
5970  			global->p2p, wpa_s->conf->wps_vendor_ext[i]);
5971  	}
5972  
5973  	p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
5974  
5975  	return 0;
5976  }
5977  
5978  
5979  /**
5980   * wpas_p2p_deinit - Deinitialize per-interface P2P data
5981   * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5982   *
5983   * This function deinitialize per-interface P2P data.
5984   */
wpas_p2p_deinit(struct wpa_supplicant * wpa_s)5985  void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5986  {
5987  	if (wpa_s->driver && wpa_s->drv_priv)
5988  		wpa_drv_probe_req_report(wpa_s, 0);
5989  
5990  	if (wpa_s->go_params) {
5991  		/* Clear any stored provisioning info */
5992  		p2p_clear_provisioning_info(
5993  			wpa_s->global->p2p,
5994  			wpa_s->go_params->peer_device_addr);
5995  	}
5996  
5997  	os_free(wpa_s->go_params);
5998  	wpa_s->go_params = NULL;
5999  	eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
6000  	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
6001  	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6002  	wpa_s->global->p2p_long_listen = 0;
6003  	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6004  	eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6005  	wpas_p2p_remove_pending_group_interface(wpa_s);
6006  	eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
6007  	eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
6008  	wpas_p2p_listen_work_done(wpa_s);
6009  	if (wpa_s->p2p_send_action_work) {
6010  		os_free(wpa_s->p2p_send_action_work->ctx);
6011  		radio_work_done(wpa_s->p2p_send_action_work);
6012  		wpa_s->p2p_send_action_work = NULL;
6013  	}
6014  	eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
6015  	eloop_cancel_timeout(wpas_p2p_send_bootstrap_comeback, wpa_s, NULL);
6016  
6017  	wpabuf_free(wpa_s->p2p_oob_dev_pw);
6018  	wpa_s->p2p_oob_dev_pw = NULL;
6019  
6020  	os_free(wpa_s->p2p_group_common_freqs);
6021  	wpa_s->p2p_group_common_freqs = NULL;
6022  	wpa_s->p2p_group_common_freqs_num = 0;
6023  
6024  	/* TODO: remove group interface from the driver if this wpa_s instance
6025  	 * is on top of a P2P group interface */
6026  }
6027  
6028  
6029  /**
6030   * wpas_p2p_deinit_global - Deinitialize global P2P module
6031   * @global: Pointer to global data from wpa_supplicant_init()
6032   *
6033   * This function deinitializes the global (per device) P2P module.
6034   */
wpas_p2p_deinit_global(struct wpa_global * global)6035  static void wpas_p2p_deinit_global(struct wpa_global *global)
6036  {
6037  	struct wpa_supplicant *wpa_s, *tmp;
6038  
6039  	wpa_s = global->ifaces;
6040  
6041  	wpas_p2p_service_flush(global->p2p_init_wpa_s);
6042  
6043  	/* Remove remaining P2P group interfaces */
6044  	while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
6045  		wpa_s = wpa_s->next;
6046  	while (wpa_s) {
6047  		tmp = global->ifaces;
6048  		while (tmp &&
6049  		       (tmp == wpa_s ||
6050  			tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
6051  			tmp = tmp->next;
6052  		}
6053  		if (tmp == NULL)
6054  			break;
6055  		/* Disconnect from the P2P group and deinit the interface */
6056  		wpas_p2p_disconnect(tmp);
6057  	}
6058  
6059  	/*
6060  	 * Deinit GO data on any possibly remaining interface (if main
6061  	 * interface is used as GO).
6062  	 */
6063  	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6064  		if (wpa_s->ap_iface)
6065  			wpas_p2p_group_deinit(wpa_s);
6066  	}
6067  
6068  	p2p_deinit(global->p2p);
6069  	global->p2p = NULL;
6070  	global->p2p_init_wpa_s = NULL;
6071  }
6072  
6073  
wpas_p2p_create_iface(struct wpa_supplicant * wpa_s)6074  static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
6075  {
6076  	if (wpa_s->conf->p2p_no_group_iface)
6077  		return 0; /* separate interface disabled per configuration */
6078  	if (wpa_s->drv_flags &
6079  	    (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
6080  	     WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
6081  		return 1; /* P2P group requires a new interface in every case
6082  			   */
6083  	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
6084  		return 0; /* driver does not support concurrent operations */
6085  	if (wpa_s->global->ifaces->next)
6086  		return 1; /* more that one interface already in use */
6087  	if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
6088  		return 1; /* this interface is already in use */
6089  	return 0;
6090  }
6091  
6092  
6093  #ifdef CONFIG_PASN
wpas_p2p_config_sae_password(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)6094  static int wpas_p2p_config_sae_password(struct wpa_supplicant *wpa_s,
6095  					struct wpa_ssid *ssid)
6096  {
6097  	struct p2p_data *p2p = wpa_s->global->p2p;
6098  
6099  	if (wpa_s->global->p2p_disabled || !p2p || !ssid->sae_password)
6100  		return -2;
6101  
6102  	return p2p_config_sae_password(p2p, ssid->sae_password);
6103  }
6104  #endif /* CONFIG_PASN */
6105  
6106  
wpas_p2p_start_go_neg(struct wpa_supplicant * wpa_s,const u8 * peer_addr,enum p2p_wps_method wps_method,int go_intent,const u8 * own_interface_addr,unsigned int force_freq,int persistent_group,struct wpa_ssid * ssid,unsigned int pref_freq,bool p2p2,u16 bootstrap,const char * password)6107  static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
6108  				 const u8 *peer_addr,
6109  				 enum p2p_wps_method wps_method,
6110  				 int go_intent, const u8 *own_interface_addr,
6111  				 unsigned int force_freq, int persistent_group,
6112  				 struct wpa_ssid *ssid, unsigned int pref_freq,
6113  				 bool p2p2, u16 bootstrap, const char *password)
6114  {
6115  	if (persistent_group && wpa_s->conf->persistent_reconnect)
6116  		persistent_group = 2;
6117  
6118  	/*
6119  	 * Increase GO config timeout if HT40 is used since it takes some time
6120  	 * to scan channels for coex purposes before the BSS can be started.
6121  	 */
6122  	p2p_set_config_timeout(wpa_s->global->p2p,
6123  			       wpa_s->p2p_go_ht40 ? 255 : 100, 20);
6124  
6125  	return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
6126  			   go_intent, own_interface_addr, force_freq,
6127  			   persistent_group, ssid ? ssid->ssid : NULL,
6128  			   ssid ? ssid->ssid_len : 0,
6129  			   wpa_s->p2p_pd_before_go_neg, pref_freq,
6130  			   wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
6131  			   0, p2p2, bootstrap, password);
6132  }
6133  
6134  
wpas_p2p_auth_go_neg(struct wpa_supplicant * wpa_s,const u8 * peer_addr,enum p2p_wps_method wps_method,int go_intent,const u8 * own_interface_addr,unsigned int force_freq,int persistent_group,struct wpa_ssid * ssid,unsigned int pref_freq,u16 bootstrap,const char * password)6135  static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
6136  				const u8 *peer_addr,
6137  				enum p2p_wps_method wps_method,
6138  				int go_intent, const u8 *own_interface_addr,
6139  				unsigned int force_freq, int persistent_group,
6140  				struct wpa_ssid *ssid, unsigned int pref_freq,
6141  				u16 bootstrap, const char *password)
6142  {
6143  	if (persistent_group && wpa_s->conf->persistent_reconnect)
6144  		persistent_group = 2;
6145  
6146  	return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
6147  			     go_intent, own_interface_addr, force_freq,
6148  			     persistent_group, ssid ? ssid->ssid : NULL,
6149  			     ssid ? ssid->ssid_len : 0, pref_freq,
6150  			     wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
6151  			     0, bootstrap, password);
6152  }
6153  
6154  
wpas_p2p_check_join_scan_limit(struct wpa_supplicant * wpa_s)6155  static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
6156  {
6157  	wpa_s->p2p_join_scan_count++;
6158  	wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
6159  		   wpa_s->p2p_join_scan_count);
6160  	if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
6161  		wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
6162  			   " for join operationg - stop join attempt",
6163  			   MAC2STR(wpa_s->pending_join_iface_addr));
6164  		eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6165  		if (wpa_s->p2p_auto_pd) {
6166  			wpa_s->p2p_auto_pd = 0;
6167  			wpa_msg_global(wpa_s, MSG_INFO,
6168  				       P2P_EVENT_PROV_DISC_FAILURE
6169  				       " p2p_dev_addr=" MACSTR " status=N/A",
6170  				       MAC2STR(wpa_s->pending_join_dev_addr));
6171  			return;
6172  		}
6173  		if (wpa_s->p2p_fallback_to_go_neg) {
6174  			wpa_dbg(wpa_s, MSG_DEBUG,
6175  				"P2P: Join operation failed - fall back to GO Negotiation");
6176  			wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
6177  				       P2P_EVENT_FALLBACK_TO_GO_NEG
6178  				       "reason=join-failed");
6179  			wpas_p2p_fallback_to_go_neg(wpa_s, 0);
6180  			return;
6181  		}
6182  		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
6183  			       P2P_EVENT_GROUP_FORMATION_FAILURE);
6184  		wpas_notify_p2p_group_formation_failure(wpa_s, "");
6185  	}
6186  }
6187  
6188  
wpas_check_freq_conflict(struct wpa_supplicant * wpa_s,int freq)6189  static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
6190  {
6191  	int res;
6192  	unsigned int num, i;
6193  	struct wpa_used_freq_data *freqs;
6194  
6195  	if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
6196  		/* Multiple channels are supported and not all are in use */
6197  		return 0;
6198  	}
6199  
6200  	freqs = os_calloc(wpa_s->num_multichan_concurrent,
6201  			  sizeof(struct wpa_used_freq_data));
6202  	if (!freqs)
6203  		return 1;
6204  
6205  	num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
6206  					wpa_s->num_multichan_concurrent);
6207  
6208  	for (i = 0; i < num; i++) {
6209  		if (freqs[i].freq == freq) {
6210  			wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
6211  				   freq);
6212  			res = 0;
6213  			goto exit_free;
6214  		}
6215  	}
6216  
6217  	wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
6218  	res = 1;
6219  
6220  exit_free:
6221  	os_free(freqs);
6222  	return res;
6223  }
6224  
6225  
wpas_p2p_peer_go(struct wpa_supplicant * wpa_s,const u8 * peer_dev_addr)6226  static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
6227  			    const u8 *peer_dev_addr)
6228  {
6229  	struct wpa_bss *bss;
6230  	int updated;
6231  
6232  	bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
6233  	if (bss == NULL)
6234  		return -1;
6235  	if (bss->last_update_idx < wpa_s->bss_update_idx) {
6236  		wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
6237  			   "last scan");
6238  		return 0;
6239  	}
6240  
6241  	updated = os_reltime_before(&wpa_s->p2p_auto_started,
6242  				    &bss->last_update);
6243  	wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
6244  		   "%ld.%06ld (%supdated in last scan)",
6245  		   bss->last_update.sec, bss->last_update.usec,
6246  		   updated ? "": "not ");
6247  
6248  	return updated;
6249  }
6250  
6251  
wpas_p2p_scan_res_join(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res)6252  static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
6253  				   struct wpa_scan_results *scan_res)
6254  {
6255  	struct wpa_bss *bss = NULL;
6256  	int freq;
6257  	u8 iface_addr[ETH_ALEN];
6258  
6259  	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6260  
6261  	if (wpa_s->global->p2p_disabled)
6262  		return;
6263  
6264  	wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
6265  		   scan_res ? (int) scan_res->num : -1,
6266  		   wpa_s->p2p_auto_join ? "auto_" : "");
6267  
6268  	if (scan_res)
6269  		wpas_p2p_scan_res_handler(wpa_s, scan_res);
6270  
6271  	if (!wpa_s->p2p2 && wpa_s->p2p_auto_pd) {
6272  		int join = wpas_p2p_peer_go(wpa_s,
6273  					    wpa_s->pending_join_dev_addr);
6274  		if (join == 0 &&
6275  		    wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
6276  			wpa_s->auto_pd_scan_retry++;
6277  			bss = wpa_bss_get_bssid_latest(
6278  				wpa_s, wpa_s->pending_join_dev_addr);
6279  			if (bss) {
6280  				freq = bss->freq;
6281  				wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
6282  					   "the peer " MACSTR " at %d MHz",
6283  					   wpa_s->auto_pd_scan_retry,
6284  					   MAC2STR(wpa_s->
6285  						   pending_join_dev_addr),
6286  					   freq);
6287  				wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
6288  				return;
6289  			}
6290  		}
6291  
6292  		if (join < 0)
6293  			join = 0;
6294  
6295  		wpa_s->p2p_auto_pd = 0;
6296  		wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
6297  		wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
6298  			   MAC2STR(wpa_s->pending_join_dev_addr), join);
6299  		if (p2p_prov_disc_req(wpa_s->global->p2p,
6300  				      wpa_s->pending_join_dev_addr, NULL,
6301  				      wpa_s->pending_pd_config_methods, join,
6302  				      0, wpa_s->user_initiated_pd) < 0) {
6303  			wpa_s->p2p_auto_pd = 0;
6304  			wpa_msg_global(wpa_s, MSG_INFO,
6305  				       P2P_EVENT_PROV_DISC_FAILURE
6306  				       " p2p_dev_addr=" MACSTR " status=N/A",
6307  				       MAC2STR(wpa_s->pending_join_dev_addr));
6308  		}
6309  		return;
6310  	}
6311  
6312  	if (wpa_s->p2p2 || wpa_s->p2p_auto_join) {
6313  		int join = wpas_p2p_peer_go(wpa_s,
6314  					    wpa_s->pending_join_dev_addr);
6315  
6316  		if (wpa_s->p2p2 || join < 0) {
6317  			if (join < 0) {
6318  				wpa_printf(MSG_DEBUG,
6319  					   "P2P: Peer was not found to be running a GO -> use GO Negotiation");
6320  				wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
6321  					       P2P_EVENT_FALLBACK_TO_GO_NEG
6322  					       "reason=peer-not-running-GO");
6323  			}
6324  
6325  			if (wpa_s->p2p2)
6326  				wpa_printf(MSG_DEBUG,
6327  					   "P2P2: Initiate GO negotiation and provisioning using PASN Authentication");
6328  			wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
6329  					 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
6330  					 wpa_s->p2p_persistent_group, 0, 0, 0,
6331  					 wpa_s->p2p_go_intent,
6332  					 wpa_s->p2p_connect_freq,
6333  					 wpa_s->p2p_go_vht_center_freq2,
6334  					 wpa_s->p2p_persistent_id,
6335  					 wpa_s->p2p_pd_before_go_neg,
6336  					 wpa_s->p2p_go_ht40,
6337  					 wpa_s->p2p_go_vht,
6338  					 wpa_s->p2p_go_max_oper_chwidth,
6339  					 wpa_s->p2p_go_he,
6340  					 wpa_s->p2p_go_edmg,
6341  					 NULL, 0,
6342  					 is_p2p_allow_6ghz(wpa_s->global->p2p),
6343  					 wpa_s->p2p2, wpa_s->p2p_bootstrap,
6344  					 wpa_s->pending_join_password[0] ?
6345  					 wpa_s->pending_join_password : NULL,
6346  					 false);
6347  			return;
6348  		}
6349  
6350  		wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
6351  			   "try to join the group", join ? "" :
6352  			   " in older scan");
6353  		if (!join) {
6354  			wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
6355  				       P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
6356  			wpa_s->p2p_fallback_to_go_neg = 1;
6357  		}
6358  	}
6359  
6360  	freq = p2p_get_oper_freq(wpa_s->global->p2p,
6361  				 wpa_s->pending_join_iface_addr);
6362  	if (freq < 0 &&
6363  	    p2p_get_interface_addr(wpa_s->global->p2p,
6364  				   wpa_s->pending_join_dev_addr,
6365  				   iface_addr) == 0 &&
6366  	    !ether_addr_equal(iface_addr, wpa_s->pending_join_dev_addr) &&
6367  	    !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
6368  		wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
6369  			   "address for join from " MACSTR " to " MACSTR
6370  			   " based on newly discovered P2P peer entry",
6371  			   MAC2STR(wpa_s->pending_join_iface_addr),
6372  			   MAC2STR(iface_addr));
6373  		os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
6374  			  ETH_ALEN);
6375  
6376  		freq = p2p_get_oper_freq(wpa_s->global->p2p,
6377  					 wpa_s->pending_join_iface_addr);
6378  	}
6379  	if (freq >= 0) {
6380  		wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
6381  			   "from P2P peer table: %d MHz", freq);
6382  	}
6383  	if (wpa_s->p2p_join_ssid_len) {
6384  		wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
6385  			   MACSTR " and SSID %s",
6386  			   MAC2STR(wpa_s->pending_join_iface_addr),
6387  			   wpa_ssid_txt(wpa_s->p2p_join_ssid,
6388  					wpa_s->p2p_join_ssid_len));
6389  		bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
6390  				  wpa_s->p2p_join_ssid,
6391  				  wpa_s->p2p_join_ssid_len);
6392  	} else if (!bss) {
6393  		wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
6394  			   MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
6395  		bss = wpa_bss_get_bssid_latest(wpa_s,
6396  					       wpa_s->pending_join_iface_addr);
6397  	}
6398  	if (bss) {
6399  		u8 dev_addr[ETH_ALEN];
6400  
6401  		freq = bss->freq;
6402  		wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
6403  			   "from BSS table: %d MHz (SSID %s)", freq,
6404  			   wpa_ssid_txt(bss->ssid, bss->ssid_len));
6405  		if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
6406  				       dev_addr) == 0 &&
6407  		    ether_addr_equal(wpa_s->pending_join_dev_addr,
6408  				     wpa_s->pending_join_iface_addr) &&
6409  		    !ether_addr_equal(dev_addr, wpa_s->pending_join_dev_addr)) {
6410  			wpa_printf(MSG_DEBUG,
6411  				   "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
6412  				   MAC2STR(dev_addr),
6413  				   MAC2STR(wpa_s->pending_join_dev_addr));
6414  			os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
6415  				  ETH_ALEN);
6416  		}
6417  	}
6418  	if (freq > 0) {
6419  		u16 method;
6420  
6421  		if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
6422  			wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
6423  				       P2P_EVENT_GROUP_FORMATION_FAILURE
6424  				       "reason=FREQ_CONFLICT");
6425  			wpas_notify_p2p_group_formation_failure(
6426  				wpa_s, "FREQ_CONFLICT");
6427  			return;
6428  		}
6429  
6430  		wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
6431  			   "prior to joining an existing group (GO " MACSTR
6432  			   " freq=%u MHz)",
6433  			   MAC2STR(wpa_s->pending_join_dev_addr), freq);
6434  		wpa_s->pending_pd_before_join = 1;
6435  
6436  		switch (wpa_s->pending_join_wps_method) {
6437  		case WPS_PIN_DISPLAY:
6438  			method = WPS_CONFIG_KEYPAD;
6439  			break;
6440  		case WPS_PIN_KEYPAD:
6441  			method = WPS_CONFIG_DISPLAY;
6442  			break;
6443  		case WPS_PBC:
6444  			method = WPS_CONFIG_PUSHBUTTON;
6445  			break;
6446  		case WPS_P2PS:
6447  			method = WPS_CONFIG_P2PS;
6448  			break;
6449  		default:
6450  			method = 0;
6451  			break;
6452  		}
6453  
6454  		if ((p2p_get_provisioning_info(wpa_s->global->p2p,
6455  					       wpa_s->pending_join_dev_addr) ==
6456  		     method)) {
6457  			/*
6458  			 * We have already performed provision discovery for
6459  			 * joining the group. Proceed directly to join
6460  			 * operation without duplicated provision discovery. */
6461  			wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
6462  				   "with " MACSTR " already done - proceed to "
6463  				   "join",
6464  				   MAC2STR(wpa_s->pending_join_dev_addr));
6465  			wpa_s->pending_pd_before_join = 0;
6466  			goto start;
6467  		}
6468  
6469  		if (p2p_prov_disc_req(wpa_s->global->p2p,
6470  				      wpa_s->pending_join_dev_addr,
6471  				      NULL, method, 1,
6472  				      freq, wpa_s->user_initiated_pd) < 0) {
6473  			wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
6474  				   "Discovery Request before joining an "
6475  				   "existing group");
6476  			wpa_s->pending_pd_before_join = 0;
6477  			goto start;
6478  		}
6479  		return;
6480  	}
6481  
6482  	wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
6483  	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6484  	eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
6485  	wpas_p2p_check_join_scan_limit(wpa_s);
6486  	return;
6487  
6488  start:
6489  	/* Start join operation immediately */
6490  	wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
6491  			    wpa_s->p2p_join_ssid_len);
6492  }
6493  
6494  
wpas_p2p_join_scan_req(struct wpa_supplicant * wpa_s,int freq,const u8 * ssid,size_t ssid_len)6495  static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
6496  				   const u8 *ssid, size_t ssid_len)
6497  {
6498  	int ret;
6499  	struct wpa_driver_scan_params params;
6500  	struct wpabuf *wps_ie = NULL, *ies;
6501  	size_t ielen;
6502  	int freqs[2] = { 0, 0 };
6503  	unsigned int bands;
6504  
6505  	os_memset(&params, 0, sizeof(params));
6506  
6507  	/* P2P Wildcard SSID */
6508  	params.num_ssids = 1;
6509  	if (ssid && ssid_len) {
6510  		params.ssids[0].ssid = ssid;
6511  		params.ssids[0].ssid_len = ssid_len;
6512  		os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
6513  		wpa_s->p2p_join_ssid_len = ssid_len;
6514  	} else {
6515  		params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
6516  		params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
6517  		wpa_s->p2p_join_ssid_len = 0;
6518  	}
6519  
6520  	if (!wpa_s->p2p2) {
6521  		wpa_s->wps->dev.p2p = 1;
6522  		wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT,
6523  						&wpa_s->wps->dev,
6524  						wpa_s->wps->uuid,
6525  						WPS_REQ_ENROLLEE, 0, NULL);
6526  		if (!wps_ie) {
6527  			wpas_p2p_scan_res_join(wpa_s, NULL);
6528  			return;
6529  		}
6530  	}
6531  
6532  	if (!freq) {
6533  		int oper_freq;
6534  		/*
6535  		 * If freq is not provided, check the operating freq of the GO
6536  		 * and use a single channel scan on if possible.
6537  		 */
6538  		oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
6539  					      wpa_s->pending_join_iface_addr);
6540  		if (oper_freq > 0)
6541  			freq = oper_freq;
6542  	}
6543  	if (freq > 0) {
6544  		freqs[0] = freq;
6545  		params.freqs = freqs;
6546  	} else {
6547  		wpas_p2p_scan_freqs(wpa_s, &params, true);
6548  	}
6549  
6550  	ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
6551  
6552  	if (wps_ie)
6553  		ielen += wpabuf_len(wps_ie);
6554  
6555  	ies = wpabuf_alloc(ielen);
6556  	if (!ies) {
6557  		wpabuf_free(wps_ie);
6558  		wpas_p2p_scan_res_join(wpa_s, NULL);
6559  		return;
6560  	}
6561  
6562  	if (wps_ie) {
6563  		wpabuf_put_buf(ies, wps_ie);
6564  		wpabuf_free(wps_ie);
6565  	}
6566  
6567  	bands = wpas_get_bands(wpa_s, freqs);
6568  	p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
6569  
6570  	params.p2p_probe = 1;
6571  	params.extra_ies = wpabuf_head(ies);
6572  	params.extra_ies_len = wpabuf_len(ies);
6573  
6574  	if (wpa_s->clear_driver_scan_cache) {
6575  		wpa_printf(MSG_DEBUG,
6576  			   "Request driver to clear scan cache due to local BSS flush");
6577  		params.only_new_results = 1;
6578  	}
6579  
6580  	/*
6581  	 * Run a scan to update BSS table and start Provision Discovery once
6582  	 * the new scan results become available.
6583  	 */
6584  	ret = wpa_drv_scan(wpa_s, &params);
6585  	if (params.freqs != freqs)
6586  		os_free(params.freqs);
6587  	if (!ret) {
6588  		os_get_reltime(&wpa_s->scan_trigger_time);
6589  		wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
6590  		wpa_s->own_scan_requested = 1;
6591  		wpa_s->clear_driver_scan_cache = 0;
6592  	}
6593  
6594  	wpabuf_free(ies);
6595  
6596  	if (ret) {
6597  		wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
6598  			   "try again later");
6599  		eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6600  		eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
6601  		wpas_p2p_check_join_scan_limit(wpa_s);
6602  	}
6603  }
6604  
6605  
wpas_p2p_join_scan(void * eloop_ctx,void * timeout_ctx)6606  static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
6607  {
6608  	struct wpa_supplicant *wpa_s = eloop_ctx;
6609  	wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
6610  }
6611  
6612  
wpas_p2p_join(struct wpa_supplicant * wpa_s,const u8 * iface_addr,const u8 * dev_addr,enum p2p_wps_method wps_method,int auto_join,int op_freq,const u8 * ssid,size_t ssid_len)6613  static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
6614  			 const u8 *dev_addr, enum p2p_wps_method wps_method,
6615  			 int auto_join, int op_freq,
6616  			 const u8 *ssid, size_t ssid_len)
6617  {
6618  	wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
6619  		   MACSTR " dev " MACSTR " op_freq=%d)%s",
6620  		   MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
6621  		   auto_join ? " (auto_join)" : "");
6622  	if (ssid && ssid_len) {
6623  		wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
6624  			   wpa_ssid_txt(ssid, ssid_len));
6625  	}
6626  
6627  	wpa_s->p2p_auto_pd = 0;
6628  	wpa_s->p2p_auto_join = !!auto_join;
6629  	os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
6630  	os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
6631  	wpa_s->pending_join_wps_method = wps_method;
6632  
6633  	/* Make sure we are not running find during connection establishment */
6634  	wpas_p2p_stop_find(wpa_s);
6635  
6636  	wpa_s->p2p_join_scan_count = 0;
6637  	wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
6638  	return 0;
6639  }
6640  
6641  
wpas_p2p_join_start(struct wpa_supplicant * wpa_s,int freq,const u8 * ssid,size_t ssid_len)6642  static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
6643  			       const u8 *ssid, size_t ssid_len)
6644  {
6645  	struct wpa_supplicant *group;
6646  	struct p2p_go_neg_results res;
6647  	struct wpa_bss *bss;
6648  	const u8 *iface_addr = NULL;
6649  
6650  	group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
6651  	if (group == NULL)
6652  		return -1;
6653  	if (group != wpa_s) {
6654  		os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
6655  			  sizeof(group->p2p_pin));
6656  		group->p2p_wps_method = wpa_s->p2p_wps_method;
6657  	}
6658  
6659  	/*
6660  	 * Need to mark the current interface for p2p_group_formation
6661  	 * when a separate group interface is not used. This is needed
6662  	 * to allow p2p_cancel stop a pending p2p_connect-join.
6663  	 * wpas_p2p_init_group_interface() addresses this for the case
6664  	 * where a separate group interface is used.
6665  	 */
6666  	if (group == wpa_s->parent)
6667  		wpa_s->global->p2p_group_formation = group;
6668  
6669  	group->p2p_in_provisioning = 1;
6670  	group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
6671  
6672  	os_memset(&res, 0, sizeof(res));
6673  	os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
6674  	os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
6675  		  ETH_ALEN);
6676  	if (!is_zero_ether_addr(wpa_s->pending_join_iface_addr))
6677  		iface_addr = wpa_s->pending_join_iface_addr;
6678  
6679  	if (wpa_s->pending_join_password[0]) {
6680  		res.akmp = WPA_KEY_MGMT_SAE;
6681  		os_strlcpy(res.sae_password, wpa_s->pending_join_password,
6682  			   sizeof(res.sae_password));
6683  		os_memset(wpa_s->pending_join_password, 0,
6684  			  sizeof(wpa_s->pending_join_password));
6685  	}
6686  	res.wps_method = wpa_s->pending_join_wps_method;
6687  	res.p2p2 = wpa_s->p2p2;
6688  	res.cipher = WPA_CIPHER_CCMP;
6689  
6690  	if (freq && ssid && ssid_len) {
6691  		res.freq = freq;
6692  		res.ssid_len = ssid_len;
6693  		os_memcpy(res.ssid, ssid, ssid_len);
6694  	} else {
6695  		if (ssid && ssid_len) {
6696  			bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
6697  		} else {
6698  			bss = wpa_bss_get_bssid_latest(
6699  				wpa_s, wpa_s->pending_join_iface_addr);
6700  		}
6701  		if (bss) {
6702  			res.freq = bss->freq;
6703  			res.ssid_len = bss->ssid_len;
6704  			os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
6705  			os_memcpy(res.peer_interface_addr, bss->bssid,
6706  				  ETH_ALEN);
6707  			wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
6708  				   bss->freq,
6709  				   wpa_ssid_txt(bss->ssid, bss->ssid_len));
6710  		} else if (ssid && ssid_len) {
6711  			res.ssid_len = ssid_len;
6712  			os_memcpy(res.ssid, ssid, ssid_len);
6713  			wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
6714  				   wpa_ssid_txt(ssid, ssid_len));
6715  		}
6716  	}
6717  
6718  	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
6719  		wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
6720  			   "starting client");
6721  		wpa_drv_cancel_remain_on_channel(wpa_s);
6722  		wpa_s->off_channel_freq = 0;
6723  		wpa_s->roc_waiting_drv_freq = 0;
6724  	}
6725  	if (res.p2p2)
6726  		wpas_start_gc(group, &res);
6727  	else
6728  		wpas_start_wps_enrollee(group, &res);
6729  
6730  	/*
6731  	 * Allow a longer timeout for join-a-running-group than normal 15
6732  	 * second group formation timeout since the GO may not have authorized
6733  	 * our connection yet.
6734  	 */
6735  	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
6736  	eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
6737  			       wpa_s, NULL);
6738  
6739  	return 0;
6740  }
6741  
6742  
wpas_p2p_setup_freqs(struct wpa_supplicant * wpa_s,int freq,int * force_freq,int * pref_freq,int go,struct weighted_pcl * pref_freq_list,unsigned int * num_pref_freq)6743  static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
6744  				int *force_freq, int *pref_freq, int go,
6745  				struct weighted_pcl *pref_freq_list,
6746  				unsigned int *num_pref_freq)
6747  {
6748  	struct wpa_used_freq_data *freqs;
6749  	int res, best_freq, num_unused;
6750  	unsigned int freq_in_use = 0, num, i, max_pref_freq;
6751  
6752  	max_pref_freq = *num_pref_freq;
6753  	*num_pref_freq = 0;
6754  
6755  	freqs = os_calloc(wpa_s->num_multichan_concurrent,
6756  			  sizeof(struct wpa_used_freq_data));
6757  	if (!freqs)
6758  		return -1;
6759  
6760  	num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
6761  					wpa_s->num_multichan_concurrent);
6762  
6763  	/*
6764  	 * It is possible that the total number of used frequencies is bigger
6765  	 * than the number of frequencies used for P2P, so get the system wide
6766  	 * number of unused frequencies.
6767  	 */
6768  	num_unused = wpas_p2p_num_unused_channels(wpa_s);
6769  
6770  	wpa_printf(MSG_DEBUG,
6771  		   "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
6772  		   freq, wpa_s->num_multichan_concurrent, num, num_unused);
6773  
6774  	if (freq > 0) {
6775  		int ret;
6776  		if (go)
6777  			ret = p2p_supported_freq(wpa_s->global->p2p, freq);
6778  		else
6779  			ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
6780  		if (!ret) {
6781  			if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
6782  			    ieee80211_is_dfs(freq, wpa_s->hw.modes,
6783  					     wpa_s->hw.num_modes)) {
6784  				/*
6785  				 * If freq is a DFS channel and DFS is offloaded
6786  				 * to the driver, allow P2P GO to use it.
6787  				 */
6788  				wpa_printf(MSG_DEBUG,
6789  					   "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
6790  					   freq);
6791  			} else {
6792  				wpa_printf(MSG_DEBUG,
6793  					   "P2P: The forced channel (%u MHz) is not supported for P2P uses",
6794  					   freq);
6795  				res = -3;
6796  				goto exit_free;
6797  			}
6798  		}
6799  
6800  		for (i = 0; i < num; i++) {
6801  			if (freqs[i].freq == freq)
6802  				freq_in_use = 1;
6803  		}
6804  
6805  		if (num_unused <= 0 && !freq_in_use) {
6806  			wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
6807  				   freq);
6808  			res = -2;
6809  			goto exit_free;
6810  		}
6811  		wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
6812  			   "requested channel (%u MHz)", freq);
6813  		*force_freq = freq;
6814  		goto exit_ok;
6815  	}
6816  
6817  	best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6818  
6819  	if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
6820  		wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
6821  			   best_freq, go);
6822  
6823  		*num_pref_freq = max_pref_freq;
6824  		res = wpas_p2p_pick_best_pref_freq(wpa_s, go, pref_freq_list,
6825  						   num_pref_freq);
6826  		if (res > 0)
6827  			best_freq = res;
6828  	}
6829  
6830  	/* We have a candidate frequency to use */
6831  	if (best_freq > 0) {
6832  		if (*pref_freq == 0 && num_unused > 0) {
6833  			wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
6834  				   best_freq);
6835  			*pref_freq = best_freq;
6836  		} else {
6837  			wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
6838  				   best_freq);
6839  			*force_freq = best_freq;
6840  		}
6841  	} else if (num_unused > 0) {
6842  		wpa_printf(MSG_DEBUG,
6843  			   "P2P: Current operating channels are not available for P2P. Try to use another channel");
6844  		*force_freq = 0;
6845  	} else {
6846  		wpa_printf(MSG_DEBUG,
6847  			   "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
6848  		res = -2;
6849  		goto exit_free;
6850  	}
6851  
6852  exit_ok:
6853  	res = 0;
6854  exit_free:
6855  	os_free(freqs);
6856  	return res;
6857  }
6858  
6859  
is_p2p_6ghz_supported(struct wpa_supplicant * wpa_s,const u8 * peer_addr)6860  static bool is_p2p_6ghz_supported(struct wpa_supplicant *wpa_s,
6861  				  const u8 *peer_addr)
6862  {
6863  	if (wpa_s->conf->p2p_6ghz_disable ||
6864  	    !get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6865  		      HOSTAPD_MODE_IEEE80211A, true))
6866  		return false;
6867  
6868  	if (wpa_s->p2p2)
6869  		return true;
6870  
6871  	if (!p2p_wfd_enabled(wpa_s->global->p2p))
6872  		return false;
6873  	if (peer_addr && !p2p_peer_wfd_enabled(wpa_s->global->p2p, peer_addr))
6874  		return false;
6875  
6876  	return true;
6877  }
6878  
6879  
wpas_p2p_check_6ghz(struct wpa_supplicant * wpa_s,const u8 * peer_addr,bool allow_6ghz,int freq)6880  static int wpas_p2p_check_6ghz(struct wpa_supplicant *wpa_s,
6881  			       const u8 *peer_addr, bool allow_6ghz, int freq)
6882  {
6883  	if (allow_6ghz && is_p2p_6ghz_supported(wpa_s, peer_addr)) {
6884  		wpa_printf(MSG_DEBUG,
6885  			   "P2P: Allow connection on 6 GHz channels");
6886  		p2p_set_6ghz_dev_capab(wpa_s->global->p2p, true);
6887  	} else {
6888  		if (is_6ghz_freq(freq))
6889  			return -2;
6890  		p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
6891  	}
6892  
6893  	return 0;
6894  }
6895  
6896  
6897  /**
6898   * wpas_p2p_connect - Request P2P Group Formation to be started
6899   * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6900   * @peer_addr: Address of the peer P2P Device
6901   * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
6902   * @persistent_group: Whether to create a persistent group
6903   * @auto_join: Whether to select join vs. GO Negotiation automatically
6904   * @join: Whether to join an existing group (as a client) instead of starting
6905   *	Group Owner negotiation; @peer_addr is BSSID in that case
6906   * @auth: Whether to only authorize the connection instead of doing that and
6907   *	initiating Group Owner negotiation
6908   * @go_intent: GO Intent or -1 to use default
6909   * @freq: Frequency for the group or 0 for auto-selection
6910   * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
6911   * @persistent_id: Persistent group credentials to use for forcing GO
6912   *	parameters or -1 to generate new values (SSID/passphrase)
6913   * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
6914   *	interoperability workaround when initiating group formation
6915   * @ht40: Start GO with 40 MHz channel width
6916   * @vht:  Start GO with VHT support
6917   * @vht_chwidth: Channel width supported by GO operating with VHT support
6918   *	(CHANWIDTH_*).
6919   * @group_ssid: Specific Group SSID for join or %NULL if not set
6920   * @group_ssid_len: Length of @group_ssid in octets
6921   * @allow_6ghz: Allow P2P connection on 6 GHz channels
6922   * @p2p2: Whether device is in P2P R2 mode
6923   * @bootstrap: Requested bootstrap method for pairing in P2P2
6924   * @password: Password for pairing setup or NULL for opportunistic method
6925   *	in P2P2
6926   * @skip_prov: Connect without provisioning
6927   * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
6928   *	failure, -2 on failure due to channel not currently available,
6929   *	-3 if forced channel is not supported
6930   */
wpas_p2p_connect(struct wpa_supplicant * wpa_s,const u8 * peer_addr,const char * pin,enum p2p_wps_method wps_method,int persistent_group,int auto_join,int join,int auth,int go_intent,int freq,unsigned int vht_center_freq2,int persistent_id,int pd,int ht40,int vht,unsigned int vht_chwidth,int he,int edmg,const u8 * group_ssid,size_t group_ssid_len,bool allow_6ghz,bool p2p2,u16 bootstrap,const char * password,bool skip_prov)6931  int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6932  		     const char *pin, enum p2p_wps_method wps_method,
6933  		     int persistent_group, int auto_join, int join, int auth,
6934  		     int go_intent, int freq, unsigned int vht_center_freq2,
6935  		     int persistent_id, int pd, int ht40, int vht,
6936  		     unsigned int vht_chwidth, int he, int edmg,
6937  		     const u8 *group_ssid, size_t group_ssid_len,
6938  		     bool allow_6ghz, bool p2p2, u16 bootstrap,
6939  		     const char *password, bool skip_prov)
6940  {
6941  	int force_freq = 0, pref_freq = 0;
6942  	int ret = 0, res;
6943  	enum wpa_driver_if_type iftype;
6944  	const u8 *if_addr;
6945  	struct wpa_ssid *ssid = NULL;
6946  	struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
6947  	unsigned int size;
6948  
6949  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6950  		return -1;
6951  
6952  	if (persistent_id >= 0) {
6953  		ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6954  		if (ssid == NULL || ssid->disabled != 2 ||
6955  		    ssid->mode != WPAS_MODE_P2P_GO)
6956  			return -1;
6957  	}
6958  
6959  	wpa_s->p2p2 = p2p2;
6960  	wpa_s->p2p_mode = p2p2 ? WPA_P2P_MODE_WFD_R2 : WPA_P2P_MODE_WFD_R1;
6961  
6962  	if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
6963  		return -2;
6964  
6965  	os_free(wpa_s->global->add_psk);
6966  	wpa_s->global->add_psk = NULL;
6967  
6968  	p2p_set_go_role(wpa_s->global->p2p, false);
6969  	wpa_s->global->p2p_fail_on_wps_complete = 0;
6970  	wpa_s->global->pending_p2ps_group = 0;
6971  	wpa_s->global->pending_p2ps_group_freq = 0;
6972  	wpa_s->p2ps_method_config_any = 0;
6973  
6974  	if (go_intent < 0)
6975  		go_intent = wpa_s->conf->p2p_go_intent;
6976  
6977  	if (!auth)
6978  		wpa_s->global->p2p_long_listen = 0;
6979  
6980  	wpa_s->p2p_wps_method = wps_method;
6981  	wpa_s->p2p_persistent_group = !!persistent_group;
6982  	wpa_s->p2p_persistent_id = persistent_id;
6983  	wpa_s->p2p_go_intent = go_intent;
6984  	wpa_s->p2p_connect_freq = freq;
6985  	wpa_s->p2p_fallback_to_go_neg = 0;
6986  	wpa_s->p2p_pd_before_go_neg = !!pd;
6987  	wpa_s->p2p_go_ht40 = !!ht40;
6988  	wpa_s->p2p_go_vht = !!vht;
6989  	wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
6990  	wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
6991  	wpa_s->p2p_go_he = !!he;
6992  	wpa_s->p2p_go_edmg = !!edmg;
6993  	wpa_s->p2p_bootstrap = bootstrap;
6994  
6995  	if (pin)
6996  		os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6997  	else if (wps_method == WPS_PIN_DISPLAY) {
6998  		if (wps_generate_pin((unsigned int *) &ret) < 0)
6999  			return -1;
7000  		res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
7001  				  "%08d", ret);
7002  		if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
7003  			wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
7004  		wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
7005  			   wpa_s->p2p_pin);
7006  	} else if (wps_method == WPS_P2PS) {
7007  		/* Force the P2Ps default PIN to be used */
7008  		os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
7009  	} else
7010  		wpa_s->p2p_pin[0] = '\0';
7011  
7012  	if (!password)
7013  		os_memset(wpa_s->pending_join_password, 0,
7014  			  sizeof(wpa_s->pending_join_password));
7015  
7016  	if (join || auto_join) {
7017  		u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
7018  		if (auth) {
7019  #ifdef CONFIG_PASN
7020  			struct wpa_supplicant *ifs;
7021  #endif /* CONFIG_PASN */
7022  
7023  			wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
7024  				   "connect a running group from " MACSTR,
7025  				   MAC2STR(peer_addr));
7026  			os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7027  
7028  #ifdef CONFIG_PASN
7029  			if (!wpa_s->p2p2)
7030  				return ret;
7031  
7032  			wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7033  			if (wpa_s->create_p2p_iface) {
7034  				if_addr = wpa_s->pending_interface_addr;
7035  			} else {
7036  				if (wpa_s->p2p_mgmt)
7037  					if_addr = wpa_s->parent->own_addr;
7038  				else
7039  					if_addr = wpa_s->own_addr;
7040  				os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
7041  			}
7042  
7043  			dl_list_for_each(ifs, &wpa_s->radio->ifaces,
7044  					 struct wpa_supplicant, radio_list) {
7045  				if (!ifs->current_ssid ||
7046  				    ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
7047  					continue;
7048  
7049  				ssid = ifs->current_ssid;
7050  
7051  				if (bootstrap == P2P_PBMA_OPPORTUNISTIC &&
7052  				    wpas_p2p_config_sae_password(wpa_s, ssid)) {
7053  					ssid = NULL;
7054  					continue;
7055  				}
7056  
7057  				force_freq = ifs->ap_iface->freq;
7058  				break;
7059  			}
7060  			p2p_set_go_role(wpa_s->global->p2p, true);
7061  			return wpas_p2p_auth_go_neg(wpa_s, peer_addr,
7062  						    wps_method, 15, if_addr,
7063  						    force_freq,
7064  						    persistent_group, ssid,
7065  						    pref_freq, bootstrap,
7066  						    password);
7067  #else /* CONFIG_PASN */
7068  			return ret;
7069  #endif /* CONFIG_PASN */
7070  		}
7071  		os_memcpy(dev_addr, peer_addr, ETH_ALEN);
7072  		if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
7073  					   iface_addr) < 0) {
7074  			os_memcpy(iface_addr, peer_addr, ETH_ALEN);
7075  			p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
7076  					 dev_addr);
7077  		}
7078  		if (auto_join) {
7079  			os_get_reltime(&wpa_s->p2p_auto_started);
7080  			wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
7081  				   "%ld.%06ld",
7082  				   wpa_s->p2p_auto_started.sec,
7083  				   wpa_s->p2p_auto_started.usec);
7084  		}
7085  		wpa_s->user_initiated_pd = 1;
7086  		if (password)
7087  			os_strlcpy(wpa_s->pending_join_password, password,
7088  				   sizeof(wpa_s->pending_join_password));
7089  
7090  		if (skip_prov) {
7091  			if (!wpa_s->p2p2) {
7092  				wpa_printf(MSG_DEBUG,
7093  					   "P2P: Join without provisioning not supported");
7094  				return -1;
7095  			}
7096  			/* Start join operation immediately */
7097  			return wpas_p2p_join_start(wpa_s, 0, group_ssid,
7098  						   group_ssid_len);
7099  		}
7100  		if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
7101  				  auto_join, freq,
7102  				  group_ssid, group_ssid_len) < 0)
7103  			return -1;
7104  		return ret;
7105  	}
7106  
7107  	size = P2P_MAX_PREF_CHANNELS;
7108  	res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
7109  				   go_intent == 15, pref_freq_list, &size);
7110  	if (res)
7111  		return res;
7112  	wpas_p2p_set_own_freq_preference(wpa_s,
7113  					 force_freq ? force_freq : pref_freq);
7114  
7115  	p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7116  
7117  	wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7118  
7119  	if (wpa_s->create_p2p_iface) {
7120  		/* Prepare to add a new interface for the group */
7121  		iftype = WPA_IF_P2P_GROUP;
7122  		if (go_intent == 15)
7123  			iftype = WPA_IF_P2P_GO;
7124  		if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7125  			wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7126  				   "interface for the group");
7127  			return -1;
7128  		}
7129  
7130  		if_addr = wpa_s->pending_interface_addr;
7131  	} else {
7132  		if (wpa_s->p2p_mgmt)
7133  			if_addr = wpa_s->parent->own_addr;
7134  		else
7135  			if_addr = wpa_s->own_addr;
7136  		os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
7137  	}
7138  
7139  	if (auth) {
7140  		if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
7141  					 go_intent, if_addr,
7142  					 force_freq, persistent_group, ssid,
7143  					 pref_freq, bootstrap, password) < 0)
7144  			return -1;
7145  		return ret;
7146  	}
7147  
7148  	if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
7149  				  go_intent, if_addr, force_freq,
7150  				  persistent_group, ssid, pref_freq, p2p2,
7151  				  bootstrap, password) < 0) {
7152  		if (wpa_s->create_p2p_iface)
7153  			wpas_p2p_remove_pending_group_interface(wpa_s);
7154  		return -1;
7155  	}
7156  
7157  #ifdef CONFIG_PASN
7158  	if (wpa_s->p2p2 && !wpa_s->p2p_pd_before_go_neg)
7159  		wpas_p2p_initiate_pasn_auth(wpa_s, peer_addr, force_freq);
7160  #endif /* CONFIG_PASN */
7161  
7162  	return ret;
7163  }
7164  
7165  
7166  /**
7167   * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
7168   * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
7169   * @freq: Frequency of the channel in MHz
7170   * @duration: Duration of the stay on the channel in milliseconds
7171   *
7172   * This callback is called when the driver indicates that it has started the
7173   * requested remain-on-channel duration.
7174   */
wpas_p2p_remain_on_channel_cb(struct wpa_supplicant * wpa_s,unsigned int freq,unsigned int duration)7175  void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
7176  				   unsigned int freq, unsigned int duration)
7177  {
7178  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7179  		return;
7180  	wpa_printf(MSG_DEBUG, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)",
7181  		   wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
7182  		   wpa_s->roc_waiting_drv_freq, freq, duration);
7183  	if (wpa_s->off_channel_freq &&
7184  	    wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
7185  		p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
7186  			      wpa_s->pending_listen_duration);
7187  		wpa_s->pending_listen_freq = 0;
7188  	} else {
7189  		wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
7190  			   wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
7191  			   freq, duration);
7192  	}
7193  }
7194  
7195  
wpas_p2p_listen_start(struct wpa_supplicant * wpa_s,unsigned int timeout)7196  int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
7197  {
7198  	/* Limit maximum Listen state time based on driver limitation. */
7199  	if (timeout > wpa_s->max_remain_on_chan)
7200  		timeout = wpa_s->max_remain_on_chan;
7201  
7202  	return p2p_listen(wpa_s->global->p2p, timeout);
7203  }
7204  
7205  
7206  /**
7207   * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
7208   * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
7209   * @freq: Frequency of the channel in MHz
7210   *
7211   * This callback is called when the driver indicates that a remain-on-channel
7212   * operation has been completed, i.e., the duration on the requested channel
7213   * has timed out.
7214   */
wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant * wpa_s,unsigned int freq)7215  void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
7216  					  unsigned int freq)
7217  {
7218  	wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
7219  		   "(p2p_long_listen=%d ms pending_action_tx=%p)",
7220  		   wpa_s->global->p2p_long_listen,
7221  		   offchannel_pending_action_tx(wpa_s));
7222  	wpas_p2p_listen_work_done(wpa_s);
7223  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7224  		return;
7225  	if (wpa_s->global->p2p_long_listen > 0)
7226  		wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
7227  	if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
7228  		return; /* P2P module started a new operation */
7229  	if (offchannel_pending_action_tx(wpa_s))
7230  		return;
7231  	if (wpa_s->global->p2p_long_listen > 0) {
7232  		wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
7233  		wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
7234  	} else {
7235  		/*
7236  		 * When listen duration is over, stop listen & update p2p_state
7237  		 * to IDLE.
7238  		 */
7239  		p2p_stop_listen(wpa_s->global->p2p);
7240  	}
7241  }
7242  
7243  
7244  /**
7245   * wpas_p2p_group_remove - Remove a P2P group
7246   * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
7247   * @ifname: Network interface name of the group interface or "*" to remove all
7248   *	groups
7249   * Returns: 0 on success, -1 on failure
7250   *
7251   * This function is used to remove a P2P group. This can be used to disconnect
7252   * from a group in which the local end is a P2P Client or to end a P2P Group in
7253   * case the local end is the Group Owner. If a virtual network interface was
7254   * created for this group, that interface will be removed. Otherwise, only the
7255   * configured P2P group network will be removed from the interface.
7256   */
wpas_p2p_group_remove(struct wpa_supplicant * wpa_s,const char * ifname)7257  int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
7258  {
7259  	struct wpa_global *global = wpa_s->global;
7260  	struct wpa_supplicant *calling_wpa_s = wpa_s;
7261  
7262  	if (os_strcmp(ifname, "*") == 0) {
7263  		struct wpa_supplicant *prev;
7264  		bool calling_wpa_s_group_removed = false;
7265  
7266  		wpa_s = global->ifaces;
7267  		while (wpa_s) {
7268  			prev = wpa_s;
7269  			wpa_s = wpa_s->next;
7270  			if (prev->p2p_group_interface !=
7271  			    NOT_P2P_GROUP_INTERFACE ||
7272  			    (prev->current_ssid &&
7273  			     prev->current_ssid->p2p_group)) {
7274  				wpas_p2p_disconnect_safely(prev, calling_wpa_s);
7275  				if (prev == calling_wpa_s)
7276  					calling_wpa_s_group_removed = true;
7277  			}
7278  		}
7279  
7280  		if (!calling_wpa_s_group_removed &&
7281  		    (calling_wpa_s->p2p_group_interface !=
7282  		     NOT_P2P_GROUP_INTERFACE ||
7283  		     (calling_wpa_s->current_ssid &&
7284  		      calling_wpa_s->current_ssid->p2p_group))) {
7285  			wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
7286  			wpas_p2p_disconnect_safely(calling_wpa_s,
7287  						   calling_wpa_s);
7288  		}
7289  
7290  		return 0;
7291  	}
7292  
7293  	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7294  		if (os_strcmp(wpa_s->ifname, ifname) == 0)
7295  			break;
7296  	}
7297  
7298  	return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
7299  }
7300  
7301  
wpas_p2p_select_go_freq(struct wpa_supplicant * wpa_s,int freq)7302  static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
7303  {
7304  	unsigned int r;
7305  
7306  	if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
7307  		unsigned int i, size = P2P_MAX_PREF_CHANNELS;
7308  		struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7309  		int res;
7310  
7311  		res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
7312  						 &size, pref_freq_list);
7313  		if (!res && size > 0 && !is_p2p_allow_6ghz(wpa_s->global->p2p))
7314  			size = p2p_remove_6ghz_channels(pref_freq_list, size);
7315  
7316  		if (!res && size > 0) {
7317  			i = 0;
7318  			while (i < size &&
7319  			       (!p2p_supported_freq(wpa_s->global->p2p,
7320  						    pref_freq_list[i].freq) ||
7321  				wpas_p2p_disallowed_freq(
7322  					wpa_s->global,
7323  					pref_freq_list[i].freq) ||
7324  				!p2p_pref_freq_allowed(&pref_freq_list[i],
7325  						       true))) {
7326  				wpa_printf(MSG_DEBUG,
7327  					   "P2P: preferred_freq_list[%d]=%d is disallowed",
7328  					   i, pref_freq_list[i].freq);
7329  				i++;
7330  			}
7331  			if (i != size) {
7332  				freq = pref_freq_list[i].freq;
7333  				wpa_printf(MSG_DEBUG,
7334  					   "P2P: Using preferred_freq_list[%d]=%d",
7335  					   i, freq);
7336  			} else {
7337  				wpa_printf(MSG_DEBUG,
7338  					   "P2P: All driver preferred frequencies are disallowed for P2P use");
7339  			}
7340  		} else {
7341  			wpa_printf(MSG_DEBUG,
7342  				   "P2P: No preferred frequency list available");
7343  		}
7344  	}
7345  
7346  	if (freq == 2) {
7347  		wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
7348  			   "band");
7349  		if (wpa_s->best_24_freq > 0 &&
7350  		    p2p_supported_freq_go(wpa_s->global->p2p,
7351  					  wpa_s->best_24_freq)) {
7352  			freq = wpa_s->best_24_freq;
7353  			wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
7354  				   "channel: %d MHz", freq);
7355  		} else {
7356  			if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7357  				return -1;
7358  			freq = 2412 + (r % 3) * 25;
7359  			wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
7360  				   "channel: %d MHz", freq);
7361  		}
7362  	}
7363  
7364  	if (freq == 5) {
7365  		wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
7366  			   "band");
7367  		if (wpa_s->best_5_freq > 0 &&
7368  		    p2p_supported_freq_go(wpa_s->global->p2p,
7369  				       wpa_s->best_5_freq)) {
7370  			freq = wpa_s->best_5_freq;
7371  			wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
7372  				   "channel: %d MHz", freq);
7373  		} else {
7374  			const int freqs[] = {
7375  				/* operating class 115 */
7376  				5180, 5200, 5220, 5240,
7377  				/* operating class 124 */
7378  				5745, 5765, 5785, 5805,
7379  			};
7380  			unsigned int i, num_freqs = ARRAY_SIZE(freqs);
7381  
7382  			if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7383  				return -1;
7384  
7385  			/*
7386  			 * Most of the 5 GHz channels require DFS. Only
7387  			 * operating classes 115 and 124 are available possibly
7388  			 * without that requirement. Check these for
7389  			 * availability starting from a randomly picked
7390  			 * position.
7391  			 */
7392  			for (i = 0; i < num_freqs; i++, r++) {
7393  				freq = freqs[r % num_freqs];
7394  				if (p2p_supported_freq_go(wpa_s->global->p2p,
7395  							  freq))
7396  					break;
7397  			}
7398  
7399  			if (i >= num_freqs) {
7400  				wpa_printf(MSG_DEBUG, "P2P: Could not select "
7401  					   "5 GHz channel for P2P group");
7402  				return -1;
7403  			}
7404  			wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
7405  				   "channel: %d MHz", freq);
7406  		}
7407  	}
7408  
7409  	if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
7410  		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
7411  		    ieee80211_is_dfs(freq, wpa_s->hw.modes,
7412  				     wpa_s->hw.num_modes)) {
7413  			/*
7414  			 * If freq is a DFS channel and DFS is offloaded to the
7415  			 * driver, allow P2P GO to use it.
7416  			 */
7417  			wpa_printf(MSG_DEBUG, "P2P: "
7418  				   "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
7419  				   __func__, freq);
7420  			return freq;
7421  		}
7422  		wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
7423  			   "(%u MHz) is not supported for P2P uses",
7424  			   freq);
7425  		return -1;
7426  	}
7427  
7428  	return freq;
7429  }
7430  
7431  
wpas_p2p_supported_freq_go(struct wpa_supplicant * wpa_s,const struct p2p_channels * channels,int freq)7432  static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
7433  				      const struct p2p_channels *channels,
7434  				      int freq)
7435  {
7436  	if (is_6ghz_freq(freq) &&
7437  	    !is_p2p_6ghz_capable(wpa_s->global->p2p))
7438  		return 0;
7439  
7440  	if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
7441  	    p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
7442  	    freq_included(wpa_s, channels, freq))
7443  		return 1;
7444  	return 0;
7445  }
7446  
7447  
wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant * wpa_s,struct p2p_go_neg_results * params,const struct p2p_channels * channels)7448  static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
7449  					    struct p2p_go_neg_results *params,
7450  					    const struct p2p_channels *channels)
7451  {
7452  	unsigned int i, r;
7453  
7454  	/* try all channels in operating class 115 */
7455  	for (i = 0; i < 4; i++) {
7456  		params->freq = 5180 + i * 20;
7457  		if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
7458  			goto out;
7459  	}
7460  
7461  	/* try all channels in operating class 124 */
7462  	for (i = 0; i < 4; i++) {
7463  		params->freq = 5745 + i * 20;
7464  		if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
7465  			goto out;
7466  	}
7467  
7468  	/* try social channel class 180 channel 2 */
7469  	params->freq = 58320 + 1 * 2160;
7470  	if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
7471  		goto out;
7472  
7473  	/* try all channels in reg. class 180 */
7474  	for (i = 0; i < 4; i++) {
7475  		params->freq = 58320 + i * 2160;
7476  		if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
7477  			goto out;
7478  	}
7479  
7480  	/* try some random selection of the social channels */
7481  	if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7482  		return;
7483  
7484  	for (i = 0; i < 3; i++) {
7485  		params->freq = 2412 + ((r + i) % 3) * 25;
7486  		if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
7487  			goto out;
7488  	}
7489  
7490  	/* try all other channels in operating class 81 */
7491  	for (i = 0; i < 11; i++) {
7492  		params->freq = 2412 + i * 5;
7493  
7494  		/* skip social channels; covered in the previous loop */
7495  		if (params->freq == 2412 ||
7496  		    params->freq == 2437 ||
7497  		    params->freq == 2462)
7498  			continue;
7499  
7500  		if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
7501  			goto out;
7502  	}
7503  
7504  	params->freq = 0;
7505  	wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
7506  	return;
7507  out:
7508  	wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
7509  		   params->freq);
7510  }
7511  
7512  
wpas_same_band(int freq1,int freq2)7513  static int wpas_same_band(int freq1, int freq2)
7514  {
7515  	enum hostapd_hw_mode mode1, mode2;
7516  	u8 chan1, chan2;
7517  
7518  	mode1 = ieee80211_freq_to_chan(freq1, &chan1);
7519  	mode2 = ieee80211_freq_to_chan(freq2, &chan2);
7520  	if (mode1 == NUM_HOSTAPD_MODES)
7521  		return 0;
7522  	return mode1 == mode2;
7523  }
7524  
7525  
wpas_p2p_init_go_params(struct wpa_supplicant * wpa_s,struct p2p_go_neg_results * params,int freq,int vht_center_freq2,int ht40,int vht,int max_oper_chwidth,int he,int edmg,const struct p2p_channels * channels)7526  static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
7527  				   struct p2p_go_neg_results *params,
7528  				   int freq, int vht_center_freq2, int ht40,
7529  				   int vht, int max_oper_chwidth, int he,
7530  				   int edmg,
7531  				   const struct p2p_channels *channels)
7532  {
7533  	struct wpa_used_freq_data *freqs;
7534  	unsigned int cand;
7535  	unsigned int num, i;
7536  	int ignore_no_freqs = 0;
7537  	int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
7538  
7539  	os_memset(params, 0, sizeof(*params));
7540  	params->role_go = 1;
7541  	params->ht40 = ht40;
7542  	params->vht = vht;
7543  	params->he = he;
7544  	params->max_oper_chwidth = max_oper_chwidth;
7545  	params->vht_center_freq2 = vht_center_freq2;
7546  	params->edmg = edmg;
7547  	params->p2p2 = wpa_s->p2p2;
7548  
7549  	freqs = os_calloc(wpa_s->num_multichan_concurrent,
7550  			  sizeof(struct wpa_used_freq_data));
7551  	if (!freqs)
7552  		return -1;
7553  
7554  	num = get_shared_radio_freqs_data(wpa_s, freqs,
7555  					  wpa_s->num_multichan_concurrent,
7556  					  false);
7557  
7558  	if (wpa_s->current_ssid &&
7559  	    wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
7560  	    wpa_s->wpa_state == WPA_COMPLETED) {
7561  		wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
7562  			   __func__);
7563  
7564  		/*
7565  		 * If the frequency selection is done for an active P2P GO that
7566  		 * is not sharing a frequency, allow to select a new frequency
7567  		 * even if there are no unused frequencies as we are about to
7568  		 * move the P2P GO so its frequency can be re-used.
7569  		 */
7570  		for (i = 0; i < num; i++) {
7571  			if (freqs[i].freq == wpa_s->current_ssid->frequency &&
7572  			    freqs[i].flags == 0) {
7573  				ignore_no_freqs = 1;
7574  				break;
7575  			}
7576  		}
7577  	}
7578  
7579  	/* Try to use EDMG channel */
7580  	if (params->edmg) {
7581  		if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
7582  			goto success;
7583  		params->edmg = 0;
7584  	}
7585  
7586  	/* try using the forced freq */
7587  	if (freq) {
7588  		if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
7589  		    !freq_included(wpa_s, channels, freq)) {
7590  			wpa_printf(MSG_DEBUG,
7591  				   "P2P: Forced GO freq %d MHz disallowed",
7592  				   freq);
7593  			goto fail;
7594  		}
7595  		if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
7596  			if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
7597  			    ieee80211_is_dfs(freq, wpa_s->hw.modes,
7598  					     wpa_s->hw.num_modes)) {
7599  				/*
7600  				 * If freq is a DFS channel and DFS is offloaded
7601  				 * to the driver, allow P2P GO to use it.
7602  				 */
7603  				wpa_printf(MSG_DEBUG,
7604  					   "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
7605  					   __func__, freq);
7606  			} else {
7607  				wpa_printf(MSG_DEBUG,
7608  					   "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
7609  					   freq);
7610  				goto fail;
7611  			}
7612  		}
7613  
7614  		for (i = 0; i < num; i++) {
7615  			if (freqs[i].freq == freq) {
7616  				wpa_printf(MSG_DEBUG,
7617  					   "P2P: forced freq (%d MHz) is also shared",
7618  					   freq);
7619  				params->freq = freq;
7620  				goto success;
7621  			}
7622  		}
7623  
7624  		if (!ignore_no_freqs && !unused_channels) {
7625  			wpa_printf(MSG_DEBUG,
7626  				   "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
7627  				   freq);
7628  			goto fail;
7629  		}
7630  
7631  		wpa_printf(MSG_DEBUG,
7632  			   "P2P: force GO freq (%d MHz) on a free channel",
7633  			   freq);
7634  		params->freq = freq;
7635  		goto success;
7636  	}
7637  
7638  	/* consider using one of the shared frequencies */
7639  	if (num &&
7640  	    (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
7641  		cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
7642  		if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
7643  			wpa_printf(MSG_DEBUG,
7644  				   "P2P: Use shared freq (%d MHz) for GO",
7645  				   cand);
7646  			params->freq = cand;
7647  			goto success;
7648  		}
7649  
7650  		/* try using one of the shared freqs */
7651  		for (i = 0; i < num; i++) {
7652  			if (wpas_p2p_supported_freq_go(wpa_s, channels,
7653  						       freqs[i].freq)) {
7654  				wpa_printf(MSG_DEBUG,
7655  					   "P2P: Use shared freq (%d MHz) for GO",
7656  					   freqs[i].freq);
7657  				params->freq = freqs[i].freq;
7658  				goto success;
7659  			}
7660  		}
7661  	}
7662  
7663  	if (!ignore_no_freqs && !unused_channels) {
7664  		wpa_printf(MSG_DEBUG,
7665  			   "P2P: Cannot force GO on any of the channels we are already using");
7666  		goto fail;
7667  	}
7668  
7669  	/* try using the setting from the configuration file */
7670  	if (wpa_s->conf->p2p_oper_reg_class == 81 &&
7671  	    wpa_s->conf->p2p_oper_channel >= 1 &&
7672  	    wpa_s->conf->p2p_oper_channel <= 11 &&
7673  	    wpas_p2p_supported_freq_go(
7674  		    wpa_s, channels,
7675  		    2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
7676  		params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
7677  		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
7678  			   "frequency %d MHz", params->freq);
7679  		goto success;
7680  	}
7681  
7682  	if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
7683  	     wpa_s->conf->p2p_oper_reg_class == 116 ||
7684  	     wpa_s->conf->p2p_oper_reg_class == 117 ||
7685  	     wpa_s->conf->p2p_oper_reg_class == 124 ||
7686  	     wpa_s->conf->p2p_oper_reg_class == 125 ||
7687  	     wpa_s->conf->p2p_oper_reg_class == 126 ||
7688  	     wpa_s->conf->p2p_oper_reg_class == 127) &&
7689  	    wpas_p2p_supported_freq_go(wpa_s, channels,
7690  				       5000 +
7691  				       5 * wpa_s->conf->p2p_oper_channel)) {
7692  		params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
7693  		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
7694  			   "frequency %d MHz", params->freq);
7695  		goto success;
7696  	}
7697  
7698  	/* Try using best channels */
7699  	if (wpa_s->conf->p2p_oper_channel == 0 &&
7700  	    wpa_s->best_overall_freq > 0 &&
7701  	    wpas_p2p_supported_freq_go(wpa_s, channels,
7702  				       wpa_s->best_overall_freq)) {
7703  		params->freq = wpa_s->best_overall_freq;
7704  		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
7705  			   "channel %d MHz", params->freq);
7706  		goto success;
7707  	}
7708  
7709  	if (wpa_s->conf->p2p_oper_channel == 0 &&
7710  	    wpa_s->best_24_freq > 0 &&
7711  	    wpas_p2p_supported_freq_go(wpa_s, channels,
7712  				       wpa_s->best_24_freq)) {
7713  		params->freq = wpa_s->best_24_freq;
7714  		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
7715  			   "channel %d MHz", params->freq);
7716  		goto success;
7717  	}
7718  
7719  	if (wpa_s->conf->p2p_oper_channel == 0 &&
7720  	    wpa_s->best_5_freq > 0 &&
7721  	    wpas_p2p_supported_freq_go(wpa_s, channels,
7722  				       wpa_s->best_5_freq)) {
7723  		params->freq = wpa_s->best_5_freq;
7724  		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
7725  			   "channel %d MHz", params->freq);
7726  		goto success;
7727  	}
7728  
7729  	/* try using preferred channels */
7730  	cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
7731  	if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
7732  		params->freq = cand;
7733  		wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
7734  			   "channels", params->freq);
7735  		goto success;
7736  	}
7737  
7738  	/* Try using a channel that allows VHT to be used with 80 MHz */
7739  	if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
7740  		for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
7741  			enum hostapd_hw_mode mode;
7742  			struct hostapd_hw_modes *hwmode;
7743  			u8 chan;
7744  			u8 op_class;
7745  
7746  			cand = wpa_s->p2p_group_common_freqs[i];
7747  			op_class = is_6ghz_freq(cand) ? 133 : 128;
7748  			mode = ieee80211_freq_to_chan(cand, &chan);
7749  			hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
7750  					  mode, is_6ghz_freq(cand));
7751  			if (!hwmode ||
7752  			    wpas_p2p_verify_channel(wpa_s, hwmode, op_class,
7753  						    chan, BW80) != ALLOWED)
7754  				continue;
7755  			if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
7756  				params->freq = cand;
7757  				wpa_printf(MSG_DEBUG,
7758  					   "P2P: Use freq %d MHz common with the peer and allowing VHT80",
7759  					   params->freq);
7760  				goto success;
7761  			}
7762  		}
7763  	}
7764  
7765  	/* Try using a channel that allows HT to be used with 40 MHz on the same
7766  	 * band so that CSA can be used */
7767  	if (wpa_s->current_ssid && wpa_s->hw.modes &&
7768  	    wpa_s->p2p_group_common_freqs) {
7769  		for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
7770  			enum hostapd_hw_mode mode;
7771  			struct hostapd_hw_modes *hwmode;
7772  			u8 chan, op_class;
7773  			bool is_6ghz, supported = false;
7774  
7775  			is_6ghz = is_6ghz_freq(cand);
7776  			cand = wpa_s->p2p_group_common_freqs[i];
7777  			mode = ieee80211_freq_to_chan(cand, &chan);
7778  			hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
7779  					  mode, is_6ghz);
7780  			if (!wpas_same_band(wpa_s->current_ssid->frequency,
7781  					    cand) ||
7782  			    !hwmode)
7783  				continue;
7784  			if (is_6ghz &&
7785  			    wpas_p2p_verify_channel(wpa_s, hwmode, 132, chan,
7786  						    BW40) == ALLOWED)
7787  				supported = true;
7788  
7789  			if (!is_6ghz &&
7790  			    ieee80211_freq_to_channel_ext(
7791  				    cand, -1, CONF_OPER_CHWIDTH_USE_HT,
7792  				    &op_class, &chan) != NUM_HOSTAPD_MODES &&
7793  			    wpas_p2p_verify_channel(
7794  				    wpa_s, hwmode, op_class, chan,
7795  				    BW40MINUS) == ALLOWED)
7796  				supported = true;
7797  
7798  			if (!supported && !is_6ghz &&
7799  			    ieee80211_freq_to_channel_ext(
7800  				    cand, 1, CONF_OPER_CHWIDTH_USE_HT,
7801  				    &op_class, &chan) != NUM_HOSTAPD_MODES &&
7802  			    wpas_p2p_verify_channel(
7803  				    wpa_s, hwmode, op_class, chan,
7804  				    BW40PLUS) == ALLOWED)
7805  				supported = true;
7806  
7807  			if (!supported)
7808  				continue;
7809  
7810  			if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
7811  				params->freq = cand;
7812  				wpa_printf(MSG_DEBUG,
7813  					   "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
7814  					   params->freq);
7815  				goto success;
7816  			}
7817  		}
7818  	}
7819  
7820  	/* Try using one of the group common freqs on the same band so that CSA
7821  	 * can be used */
7822  	if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
7823  		for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
7824  			cand = wpa_s->p2p_group_common_freqs[i];
7825  			if (!wpas_same_band(wpa_s->current_ssid->frequency,
7826  					    cand))
7827  				continue;
7828  			if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
7829  				params->freq = cand;
7830  				wpa_printf(MSG_DEBUG,
7831  					   "P2P: Use freq %d MHz common with the peer and maintaining same band",
7832  					   params->freq);
7833  				goto success;
7834  			}
7835  		}
7836  	}
7837  
7838  	/* Try using one of the group common freqs */
7839  	if (wpa_s->p2p_group_common_freqs) {
7840  		for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
7841  			cand = wpa_s->p2p_group_common_freqs[i];
7842  			if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
7843  				params->freq = cand;
7844  				wpa_printf(MSG_DEBUG,
7845  					   "P2P: Use freq %d MHz common with the peer",
7846  					   params->freq);
7847  				goto success;
7848  			}
7849  		}
7850  	}
7851  
7852  	/* no preference, select some channel */
7853  	wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
7854  
7855  	if (params->freq == 0) {
7856  		wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
7857  		goto fail;
7858  	}
7859  
7860  success:
7861  	os_free(freqs);
7862  	return 0;
7863  fail:
7864  	os_free(freqs);
7865  	return -1;
7866  }
7867  
7868  
7869  static struct wpa_supplicant *
wpas_p2p_get_group_iface(struct wpa_supplicant * wpa_s,int addr_allocated,int go)7870  wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
7871  			 int go)
7872  {
7873  	struct wpa_supplicant *group_wpa_s;
7874  
7875  	if (!wpas_p2p_create_iface(wpa_s)) {
7876  		if (wpa_s->p2p_mgmt) {
7877  			/*
7878  			 * We may be called on the p2p_dev interface which
7879  			 * cannot be used for group operations, so always use
7880  			 * the primary interface.
7881  			 */
7882  			wpa_s->parent->p2pdev = wpa_s;
7883  			wpa_s = wpa_s->parent;
7884  		}
7885  		wpa_dbg(wpa_s, MSG_DEBUG,
7886  			"P2P: Use primary interface for group operations");
7887  		wpa_s->p2p_first_connection_timeout = 0;
7888  		if (wpa_s != wpa_s->p2pdev)
7889  			wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
7890  		return wpa_s;
7891  	}
7892  
7893  	if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
7894  					 WPA_IF_P2P_CLIENT) < 0) {
7895  		wpa_msg_global(wpa_s, MSG_ERROR,
7896  			       "P2P: Failed to add group interface");
7897  		return NULL;
7898  	}
7899  	group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
7900  	if (group_wpa_s == NULL) {
7901  		wpa_msg_global(wpa_s, MSG_ERROR,
7902  			       "P2P: Failed to initialize group interface");
7903  		wpas_p2p_remove_pending_group_interface(wpa_s);
7904  		return NULL;
7905  	}
7906  
7907  	if (go && wpa_s->p2p_go_do_acs) {
7908  		group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
7909  		group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
7910  		wpa_s->p2p_go_do_acs = 0;
7911  	}
7912  
7913  	if (go && wpa_s->p2p_go_allow_dfs) {
7914  		group_wpa_s->p2p_go_allow_dfs = wpa_s->p2p_go_allow_dfs;
7915  		wpa_s->p2p_go_allow_dfs = 0;
7916  	}
7917  
7918  	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
7919  		group_wpa_s->ifname);
7920  	group_wpa_s->p2p_first_connection_timeout = 0;
7921  	return group_wpa_s;
7922  }
7923  
7924  
7925  /**
7926   * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
7927   * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
7928   * @persistent_group: Whether to create a persistent group
7929   * @freq: Frequency for the group or 0 to indicate no hardcoding
7930   * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
7931   * @ht40: Start GO with 40 MHz channel width
7932   * @vht:  Start GO with VHT support
7933   * @vht_chwidth: channel bandwidth for GO operating with VHT support
7934   * @edmg: Start GO with EDMG support
7935   * @allow_6ghz: Allow P2P group creation on a 6 GHz channel
7936   * @p2p_mode: Operation mode for GO (R1/R2/PCC)
7937   * Returns: 0 on success, -1 on failure
7938   *
7939   * This function creates a new P2P group with the local end as the Group Owner,
7940   * i.e., without using Group Owner Negotiation.
7941   */
wpas_p2p_group_add(struct wpa_supplicant * wpa_s,int persistent_group,int freq,int vht_center_freq2,int ht40,int vht,int max_oper_chwidth,int he,int edmg,bool allow_6ghz,bool p2p2,enum wpa_p2p_mode p2p_mode)7942  int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
7943  		       int freq, int vht_center_freq2, int ht40, int vht,
7944  		       int max_oper_chwidth, int he, int edmg,
7945  		       bool allow_6ghz, bool p2p2, enum wpa_p2p_mode p2p_mode)
7946  {
7947  	struct p2p_go_neg_results params;
7948  	int selected_freq = 0;
7949  
7950  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7951  		return -1;
7952  	if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
7953  		return -1;
7954  
7955  	os_free(wpa_s->global->add_psk);
7956  	wpa_s->global->add_psk = NULL;
7957  	wpa_s->p2p2 = p2p2;
7958  	wpa_s->p2p_mode = p2p_mode;
7959  
7960  	/* Make sure we are not running find during connection establishment */
7961  	wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
7962  	wpas_p2p_stop_find_oper(wpa_s);
7963  
7964  	if (!wpa_s->p2p_go_do_acs) {
7965  		selected_freq = wpas_p2p_select_go_freq(wpa_s, freq);
7966  		if (selected_freq < 0)
7967  			return -1;
7968  	}
7969  
7970  	if (wpas_p2p_init_go_params(wpa_s, &params, selected_freq,
7971  				    vht_center_freq2,
7972  				    ht40, vht, max_oper_chwidth, he, edmg,
7973  				    NULL))
7974  		return -1;
7975  
7976  	p2p_go_params(wpa_s->global->p2p, &params);
7977  	params.persistent_group = persistent_group;
7978  
7979  	wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
7980  	if (wpa_s == NULL)
7981  		return -1;
7982  	if (freq > 0)
7983  		wpa_s->p2p_go_no_pri_sec_switch = 1;
7984  	params.p2p2 = wpa_s->p2p2;
7985  	wpas_start_go(wpa_s, &params, 0, p2p_mode);
7986  
7987  	return 0;
7988  }
7989  
7990  
wpas_start_p2p_client(struct wpa_supplicant * wpa_s,struct wpa_ssid * params,int addr_allocated,int freq,int force_scan,int retry_limit,const u8 * go_bssid,bool p2p2,const u8 * pmkid,const u8 * pmk,size_t pmk_len)7991  static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
7992  				 struct wpa_ssid *params, int addr_allocated,
7993  				 int freq, int force_scan, int retry_limit,
7994  				 const u8 *go_bssid, bool p2p2, const u8 *pmkid,
7995  				 const u8 *pmk, size_t pmk_len)
7996  {
7997  	struct os_reltime now;
7998  	struct wpa_ssid *ssid;
7999  	int other_iface_found = 0;
8000  	struct wpa_supplicant *ifs;
8001  	struct rsn_pmksa_cache_entry *entry;
8002  
8003  	wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
8004  	if (wpa_s == NULL)
8005  		return -1;
8006  	if (force_scan)
8007  		os_get_reltime(&wpa_s->scan_min_time);
8008  	wpa_s->p2p_last_4way_hs_fail = NULL;
8009  
8010  	wpa_supplicant_ap_deinit(wpa_s);
8011  
8012  	ssid = wpa_config_add_network(wpa_s->conf);
8013  	if (ssid == NULL)
8014  		return -1;
8015  	os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
8016  	wpa_config_set_network_defaults(ssid);
8017  	ssid->temporary = 1;
8018  	ssid->proto = WPA_PROTO_RSN;
8019  	ssid->pbss = params->pbss;
8020  	ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
8021  		WPA_CIPHER_CCMP;
8022  	ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
8023  	ssid->key_mgmt = WPA_KEY_MGMT_PSK;
8024  	if (is_6ghz_freq(freq) &&
8025  	    is_p2p_6ghz_capable(wpa_s->global->p2p)) {
8026  		ssid->auth_alg |= WPA_AUTH_ALG_SAE;
8027  		ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
8028  		ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
8029  		ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
8030  		wpa_dbg(wpa_s, MSG_DEBUG,
8031  			"P2P: Enable SAE auth_alg and key_mgmt");
8032  	}
8033  
8034  	ssid->ssid = os_malloc(params->ssid_len);
8035  	if (ssid->ssid == NULL) {
8036  		wpa_config_remove_network(wpa_s->conf, ssid->id);
8037  		return -1;
8038  	}
8039  	os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
8040  	ssid->ssid_len = params->ssid_len;
8041  	ssid->p2p_group = 1;
8042  	ssid->export_keys = 1;
8043  	if (params->psk_set) {
8044  		os_memcpy(ssid->psk, params->psk, 32);
8045  		ssid->psk_set = 1;
8046  	}
8047  	if (params->passphrase)
8048  		ssid->passphrase = os_strdup(params->passphrase);
8049  
8050  	if (go_bssid) {
8051  		ssid->bssid_set = 1;
8052  		os_memcpy(ssid->bssid, go_bssid, ETH_ALEN);
8053  	}
8054  
8055  	if (p2p2) {
8056  		ssid->key_mgmt = WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_PASN;
8057  		ssid->auth_alg = WPA_AUTH_ALG_OPEN;
8058  		ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
8059  		ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
8060  		ssid->disabled = 0;
8061  
8062  		if (pmk && pmk_len && pmkid) {
8063  			entry = os_zalloc(sizeof(*entry));
8064  			if (!entry)
8065  				return -1;
8066  			os_memcpy(entry->aa, ssid->bssid, ETH_ALEN);
8067  			os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
8068  			entry->pmk_len = pmk_len;
8069  			os_memcpy(entry->pmk, pmk, pmk_len);
8070  			entry->akmp = WPA_KEY_MGMT_SAE;
8071  			os_get_reltime(&now);
8072  			entry->expiration = now.sec + 43200;
8073  			entry->reauth_time = now.sec + 43200 * 70 / 100;
8074  			entry->network_ctx = ssid;
8075  			os_memcpy(entry->spa, wpa_s->own_addr, ETH_ALEN);
8076  
8077  			wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
8078  			ssid->pmk_valid = true;
8079  		}
8080  		wpa_s->current_ssid = ssid;
8081  	}
8082  
8083  	wpa_s->show_group_started = 1;
8084  	wpa_s->p2p_in_invitation = 1;
8085  	wpa_s->p2p_retry_limit = retry_limit;
8086  	wpa_s->p2p_invite_go_freq = freq;
8087  	wpa_s->p2p_go_group_formation_completed = 0;
8088  	wpa_s->global->p2p_group_formation = wpa_s;
8089  
8090  	/*
8091  	 * Get latest scan results from driver in case cached scan results from
8092  	 * interfaces on the same wiphy allow us to skip the next scan by fast
8093  	 * associating. Also update the scan time to the most recent scan result
8094  	 * fetch time on the same radio so it reflects the actual time the last
8095  	 * scan result event occurred.
8096  	 */
8097  	wpa_supplicant_update_scan_results(wpa_s, go_bssid);
8098  	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8099  			 radio_list) {
8100  		if (ifs == wpa_s)
8101  			continue;
8102  		if (!other_iface_found || os_reltime_before(&wpa_s->last_scan,
8103  							    &ifs->last_scan)) {
8104  			other_iface_found = 1;
8105  			wpa_s->last_scan.sec = ifs->last_scan.sec;
8106  			wpa_s->last_scan.usec = ifs->last_scan.usec;
8107  		}
8108  	}
8109  
8110  	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
8111  			     NULL);
8112  	eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
8113  			       wpas_p2p_group_formation_timeout,
8114  			       wpa_s->p2pdev, NULL);
8115  	wpa_supplicant_select_network(wpa_s, ssid);
8116  
8117  	return 0;
8118  }
8119  
8120  
wpas_p2p_group_add_persistent(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,int addr_allocated,int force_freq,int neg_freq,int vht_center_freq2,int ht40,int vht,int max_oper_chwidth,int he,int edmg,const struct p2p_channels * channels,int connection_timeout,int force_scan,bool allow_6ghz,int retry_limit,const u8 * go_bssid,const u8 * dev_addr,const u8 * pmkid,const u8 * pmk,size_t pmk_len)8121  int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
8122  				  struct wpa_ssid *ssid, int addr_allocated,
8123  				  int force_freq, int neg_freq,
8124  				  int vht_center_freq2, int ht40,
8125  				  int vht, int max_oper_chwidth, int he,
8126  				  int edmg,
8127  				  const struct p2p_channels *channels,
8128  				  int connection_timeout, int force_scan,
8129  				  bool allow_6ghz, int retry_limit,
8130  				  const u8 *go_bssid, const u8 *dev_addr,
8131  				  const u8 *pmkid, const u8 *pmk,
8132  				  size_t pmk_len)
8133  {
8134  	struct p2p_go_neg_results params;
8135  	int go = 0, freq;
8136  
8137  	if (ssid->disabled != 2 || ssid->ssid == NULL)
8138  		return -1;
8139  
8140  	if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
8141  	    go == (ssid->mode == WPAS_MODE_P2P_GO)) {
8142  		wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
8143  			   "already running");
8144  		if (go == 0 &&
8145  		    eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
8146  					 wpa_s->p2pdev, NULL)) {
8147  			/*
8148  			 * This can happen if Invitation Response frame was lost
8149  			 * and the peer (GO of a persistent group) tries to
8150  			 * invite us again. Reschedule the timeout to avoid
8151  			 * terminating the wait for the connection too early
8152  			 * since we now know that the peer is still trying to
8153  			 * invite us instead of having already started the GO.
8154  			 */
8155  			wpa_printf(MSG_DEBUG,
8156  				   "P2P: Reschedule group formation timeout since peer is still trying to invite us");
8157  			eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
8158  					       wpas_p2p_group_formation_timeout,
8159  					       wpa_s->p2pdev, NULL);
8160  		}
8161  		return 0;
8162  	}
8163  
8164  	os_free(wpa_s->global->add_psk);
8165  	wpa_s->global->add_psk = NULL;
8166  
8167  	/* Make sure we are not running find during connection establishment */
8168  	wpas_p2p_stop_find_oper(wpa_s);
8169  
8170  	wpa_s->p2p_fallback_to_go_neg = 0;
8171  
8172  	if (ssid->mode == WPAS_MODE_P2P_GO) {
8173  		if (force_freq > 0) {
8174  			freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
8175  			if (freq < 0)
8176  				return -1;
8177  			wpa_s->p2p_go_no_pri_sec_switch = 1;
8178  		} else {
8179  			freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
8180  			if (freq < 0 ||
8181  			    (freq > 0 && !freq_included(wpa_s, channels, freq)))
8182  				freq = 0;
8183  		}
8184  	} else if (ssid->mode == WPAS_MODE_INFRA) {
8185  		freq = neg_freq;
8186  		if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
8187  			struct os_reltime now;
8188  			struct wpa_bss *bss =
8189  				wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
8190  
8191  			os_get_reltime(&now);
8192  			if (bss &&
8193  			    !os_reltime_expired(&now, &bss->last_update, 5) &&
8194  			    freq_included(wpa_s, channels, bss->freq))
8195  				freq = bss->freq;
8196  			else
8197  				freq = 0;
8198  		}
8199  
8200  		return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
8201  					     force_scan, retry_limit, go_bssid,
8202  					     wpa_s->p2p2, pmkid, pmk, pmk_len);
8203  	} else {
8204  		return -1;
8205  	}
8206  
8207  	if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
8208  				    ht40, vht, max_oper_chwidth, he, edmg,
8209  				    channels))
8210  		return -1;
8211  
8212  	params.role_go = 1;
8213  	params.psk_set = ssid->psk_set;
8214  	if (params.psk_set)
8215  		os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
8216  	if (ssid->passphrase) {
8217  		if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
8218  			wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
8219  				   "persistent group");
8220  			return -1;
8221  		}
8222  		os_strlcpy(params.passphrase, ssid->passphrase,
8223  			   sizeof(params.passphrase));
8224  	}
8225  	os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
8226  	params.ssid_len = ssid->ssid_len;
8227  	params.persistent_group = 1;
8228  
8229  	if (wpa_s->p2p2 && pmk_len && pmk && pmkid) {
8230  		os_memcpy(params.peer_device_addr, dev_addr, ETH_ALEN);
8231  		os_memcpy(params.pmkid, pmkid, PMKID_LEN);
8232  		os_memcpy(params.pmk, pmk, pmk_len);
8233  		params.pmk_len = pmk_len;
8234  		params.akmp = WPA_KEY_MGMT_SAE;
8235  		params.p2p2 = true;
8236  	}
8237  
8238  	wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
8239  	if (wpa_s == NULL)
8240  		return -1;
8241  
8242  	p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
8243  
8244  	wpa_s->p2p_first_connection_timeout = connection_timeout;
8245  	params.p2p2 = wpa_s->p2p2;
8246  	wpas_start_go(wpa_s, &params, 0, wpa_s->p2p_mode);
8247  
8248  	return 0;
8249  }
8250  
8251  
wpas_p2p_ie_update(void * ctx,struct wpabuf * beacon_ies,struct wpabuf * proberesp_ies)8252  static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
8253  			       struct wpabuf *proberesp_ies)
8254  {
8255  	struct wpa_supplicant *wpa_s = ctx;
8256  	if (wpa_s->ap_iface) {
8257  		struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
8258  		if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
8259  			wpabuf_free(beacon_ies);
8260  			wpabuf_free(proberesp_ies);
8261  			return;
8262  		}
8263  		if (beacon_ies) {
8264  			wpabuf_free(hapd->p2p_beacon_ie);
8265  			hapd->p2p_beacon_ie = beacon_ies;
8266  		}
8267  		wpabuf_free(hapd->p2p_probe_resp_ie);
8268  		hapd->p2p_probe_resp_ie = proberesp_ies;
8269  
8270  		if (wpa_s->p2p2) {
8271  			hapd->iconf->peer_to_peer_twt = true;
8272  			hapd->iconf->channel_usage = true;
8273  		}
8274  
8275  	} else {
8276  		wpabuf_free(beacon_ies);
8277  		wpabuf_free(proberesp_ies);
8278  	}
8279  	wpa_supplicant_ap_update_beacon(wpa_s);
8280  }
8281  
8282  
wpas_p2p_idle_update(void * ctx,int idle)8283  static void wpas_p2p_idle_update(void *ctx, int idle)
8284  {
8285  	struct wpa_supplicant *wpa_s = ctx;
8286  	if (!wpa_s->ap_iface)
8287  		return;
8288  	wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
8289  	if (idle) {
8290  		if (wpa_s->global->p2p_fail_on_wps_complete &&
8291  		    wpa_s->p2p_in_provisioning) {
8292  			wpas_p2p_grpform_fail_after_wps(wpa_s);
8293  			return;
8294  		}
8295  		wpas_p2p_set_group_idle_timeout(wpa_s);
8296  	} else
8297  		eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
8298  }
8299  
8300  
wpas_p2p_group_init(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)8301  struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
8302  				       struct wpa_ssid *ssid)
8303  {
8304  	struct p2p_group *group;
8305  	struct p2p_group_config *cfg;
8306  
8307  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
8308  	    !ssid->p2p_group)
8309  		return NULL;
8310  
8311  	cfg = os_zalloc(sizeof(*cfg));
8312  	if (cfg == NULL)
8313  		return NULL;
8314  
8315  	if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
8316  		cfg->persistent_group = 2;
8317  	else if (ssid->p2p_persistent_group)
8318  		cfg->persistent_group = 1;
8319  	os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
8320  	if (wpa_s->max_stations &&
8321  	    wpa_s->max_stations < wpa_s->conf->max_num_sta)
8322  		cfg->max_clients = wpa_s->max_stations;
8323  	else
8324  		cfg->max_clients = wpa_s->conf->max_num_sta;
8325  	os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
8326  	cfg->ssid_len = ssid->ssid_len;
8327  	cfg->freq = ssid->frequency;
8328  	cfg->cb_ctx = wpa_s;
8329  	cfg->ie_update = wpas_p2p_ie_update;
8330  	cfg->idle_update = wpas_p2p_idle_update;
8331  	cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
8332  		!= 0;
8333  	cfg->p2p2 = wpa_s->p2p2;
8334  
8335  	group = p2p_group_init(wpa_s->global->p2p, cfg);
8336  	if (group == NULL)
8337  		os_free(cfg);
8338  	if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
8339  		p2p_group_notif_formation_done(group);
8340  	wpa_s->p2p_group = group;
8341  	return group;
8342  }
8343  
8344  
wpas_p2p_wps_success(struct wpa_supplicant * wpa_s,const u8 * peer_addr,int registrar)8345  void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
8346  			  int registrar)
8347  {
8348  	struct wpa_ssid *ssid = wpa_s->current_ssid;
8349  
8350  	if (!wpa_s->p2p_in_provisioning) {
8351  		wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
8352  			   "provisioning not in progress");
8353  		return;
8354  	}
8355  
8356  	if (ssid && ssid->mode == WPAS_MODE_INFRA) {
8357  		u8 go_dev_addr[ETH_ALEN];
8358  		os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
8359  		wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
8360  					  ssid->ssid_len);
8361  		/* Clear any stored provisioning info */
8362  		p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
8363  	}
8364  
8365  	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
8366  			     NULL);
8367  	wpa_s->p2p_go_group_formation_completed = 1;
8368  	if (ssid && ssid->mode == WPAS_MODE_INFRA) {
8369  		/*
8370  		 * Use a separate timeout for initial data connection to
8371  		 * complete to allow the group to be removed automatically if
8372  		 * something goes wrong in this step before the P2P group idle
8373  		 * timeout mechanism is taken into use.
8374  		 */
8375  		wpa_dbg(wpa_s, MSG_DEBUG,
8376  			"P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
8377  			P2P_MAX_INITIAL_CONN_WAIT);
8378  		eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
8379  				       wpas_p2p_group_formation_timeout,
8380  				       wpa_s->p2pdev, NULL);
8381  		/* Complete group formation on successful data connection. */
8382  		wpa_s->p2p_go_group_formation_completed = 0;
8383  	} else if (ssid) {
8384  		/*
8385  		 * Use a separate timeout for initial data connection to
8386  		 * complete to allow the group to be removed automatically if
8387  		 * the client does not complete data connection successfully.
8388  		 */
8389  		wpa_dbg(wpa_s, MSG_DEBUG,
8390  			"P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
8391  			P2P_MAX_INITIAL_CONN_WAIT_GO);
8392  		eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
8393  				       wpas_p2p_group_formation_timeout,
8394  				       wpa_s->p2pdev, NULL);
8395  		/*
8396  		 * Complete group formation on first successful data connection
8397  		 */
8398  		wpa_s->p2p_go_group_formation_completed = 0;
8399  	}
8400  	if (wpa_s->global->p2p)
8401  		p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
8402  	wpas_group_formation_completed(wpa_s, 0, NULL);
8403  }
8404  
8405  
wpas_p2p_wps_failed(struct wpa_supplicant * wpa_s,struct wps_event_fail * fail)8406  void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
8407  			 struct wps_event_fail *fail)
8408  {
8409  	if (!wpa_s->p2p_in_provisioning) {
8410  		wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
8411  			   "provisioning not in progress");
8412  		return;
8413  	}
8414  
8415  	if (wpa_s->go_params) {
8416  		p2p_clear_provisioning_info(
8417  			wpa_s->global->p2p,
8418  			wpa_s->go_params->peer_device_addr);
8419  	}
8420  
8421  	wpas_notify_p2p_wps_failed(wpa_s, fail);
8422  
8423  	if (wpa_s == wpa_s->global->p2p_group_formation) {
8424  		/*
8425  		 * Allow some time for the failed WPS negotiation exchange to
8426  		 * complete, but remove the group since group formation cannot
8427  		 * succeed after provisioning failure.
8428  		 */
8429  		wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
8430  		wpa_s->global->p2p_fail_on_wps_complete = 1;
8431  		eloop_deplete_timeout(0, 50000,
8432  				      wpas_p2p_group_formation_timeout,
8433  				      wpa_s->p2pdev, NULL);
8434  	}
8435  }
8436  
8437  
wpas_p2p_wps_eapol_cb(struct wpa_supplicant * wpa_s)8438  int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
8439  {
8440  	if (!wpa_s->global->p2p_fail_on_wps_complete ||
8441  	    !wpa_s->p2p_in_provisioning)
8442  		return 0;
8443  
8444  	wpas_p2p_grpform_fail_after_wps(wpa_s);
8445  
8446  	return 1;
8447  }
8448  
8449  
wpas_p2p_prov_disc(struct wpa_supplicant * wpa_s,const u8 * peer_addr,const char * config_method,u16 bootstrap,enum wpas_p2p_prov_disc_use use,struct p2ps_provision * p2ps_prov)8450  int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
8451  		       const char *config_method, u16 bootstrap,
8452  		       enum wpas_p2p_prov_disc_use use,
8453  		       struct p2ps_provision *p2ps_prov)
8454  {
8455  	u16 config_methods;
8456  
8457  	wpa_s->global->pending_p2ps_group = 0;
8458  	wpa_s->global->pending_p2ps_group_freq = 0;
8459  	wpa_s->p2p_fallback_to_go_neg = 0;
8460  	wpa_s->pending_pd_use = NORMAL_PD;
8461  	if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
8462  		p2ps_prov->conncap = p2ps_group_capability(
8463  			wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
8464  			&p2ps_prov->force_freq, &p2ps_prov->pref_freq);
8465  
8466  		wpa_printf(MSG_DEBUG,
8467  			   "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
8468  			   __func__, p2ps_prov->conncap,
8469  			   p2ps_prov->adv_id, p2ps_prov->conncap,
8470  			   p2ps_prov->status, p2ps_prov->info);
8471  
8472  		config_methods = 0;
8473  	} else if (bootstrap) {
8474  		wpa_s->p2p2 = true;
8475  		config_methods = 0;
8476  		wpa_s->p2p_bootstrap = bootstrap;
8477  		p2p_set_req_bootstrap_method(wpa_s->global->p2p, peer_addr,
8478  					     bootstrap);
8479  	} else if (os_strncmp(config_method, "display", 7) == 0)
8480  		config_methods = WPS_CONFIG_DISPLAY;
8481  	else if (os_strncmp(config_method, "keypad", 6) == 0)
8482  		config_methods = WPS_CONFIG_KEYPAD;
8483  	else if (os_strncmp(config_method, "pbc", 3) == 0 ||
8484  		 os_strncmp(config_method, "pushbutton", 10) == 0)
8485  		config_methods = WPS_CONFIG_PUSHBUTTON;
8486  	else {
8487  		wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
8488  		os_free(p2ps_prov);
8489  		return -1;
8490  	}
8491  
8492  	if (use == WPAS_P2P_PD_AUTO) {
8493  		os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
8494  		wpa_s->pending_pd_config_methods = config_methods;
8495  		wpa_s->p2p_auto_pd = 1;
8496  		wpa_s->p2p_auto_join = 0;
8497  		wpa_s->pending_pd_before_join = 0;
8498  		wpa_s->auto_pd_scan_retry = 0;
8499  		wpas_p2p_stop_find(wpa_s);
8500  		wpa_s->p2p_join_scan_count = 0;
8501  		os_get_reltime(&wpa_s->p2p_auto_started);
8502  		wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
8503  			   wpa_s->p2p_auto_started.sec,
8504  			   wpa_s->p2p_auto_started.usec);
8505  		wpas_p2p_join_scan(wpa_s, NULL);
8506  		return 0;
8507  	}
8508  
8509  	if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
8510  		os_free(p2ps_prov);
8511  		return -1;
8512  	}
8513  
8514  	return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
8515  				 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
8516  				 0, 1);
8517  }
8518  
8519  
wpas_p2p_scan_result_text(const u8 * ies,size_t ies_len,char * buf,char * end)8520  int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
8521  			      char *end)
8522  {
8523  	return p2p_scan_result_text(ies, ies_len, buf, end);
8524  }
8525  
8526  
wpas_p2p_clear_pending_action_tx(struct wpa_supplicant * wpa_s,bool force)8527  static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s,
8528  					     bool force)
8529  {
8530  	if (!offchannel_pending_action_tx(wpa_s) && !force)
8531  		return;
8532  
8533  	if (wpa_s->p2p_send_action_work) {
8534  		wpas_p2p_free_send_action_work(wpa_s);
8535  		eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
8536  				     wpa_s, NULL);
8537  		offchannel_send_action_done(wpa_s);
8538  	}
8539  
8540  	if (!offchannel_pending_action_tx(wpa_s))
8541  		return;
8542  	wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
8543  		   "operation request");
8544  	offchannel_clear_pending_action_tx(wpa_s);
8545  }
8546  
8547  
wpas_p2p_find(struct wpa_supplicant * wpa_s,unsigned int timeout,enum p2p_discovery_type type,unsigned int num_req_dev_types,const u8 * req_dev_types,const u8 * dev_id,unsigned int search_delay,u8 seek_cnt,const char ** seek_string,int freq,bool include_6ghz)8548  int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
8549  		  enum p2p_discovery_type type,
8550  		  unsigned int num_req_dev_types, const u8 *req_dev_types,
8551  		  const u8 *dev_id, unsigned int search_delay,
8552  		  u8 seek_cnt, const char **seek_string, int freq,
8553  		  bool include_6ghz)
8554  {
8555  	wpas_p2p_clear_pending_action_tx(wpa_s, false);
8556  	wpa_s->global->p2p_long_listen = 0;
8557  
8558  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
8559  	    wpa_s->p2p_in_provisioning) {
8560  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
8561  			(wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
8562  			" (P2P disabled)" : "",
8563  			wpa_s->p2p_in_provisioning ?
8564  			" (p2p_in_provisioning)" : "");
8565  		return -1;
8566  	}
8567  
8568  	wpa_supplicant_cancel_sched_scan(wpa_s);
8569  
8570  	return p2p_find(wpa_s->global->p2p, timeout, type,
8571  			num_req_dev_types, req_dev_types, dev_id,
8572  			search_delay, seek_cnt, seek_string, freq,
8573  			include_6ghz);
8574  }
8575  
8576  
wpas_p2p_scan_res_ignore_search(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res)8577  static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
8578  					    struct wpa_scan_results *scan_res)
8579  {
8580  	wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8581  
8582  	if (wpa_s->p2p_scan_work) {
8583  		struct wpa_radio_work *work = wpa_s->p2p_scan_work;
8584  		wpa_s->p2p_scan_work = NULL;
8585  		radio_work_done(work);
8586  	}
8587  
8588  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8589  		return;
8590  
8591  	/*
8592  	 * Indicate that results have been processed so that the P2P module can
8593  	 * continue pending tasks.
8594  	 */
8595  	wpas_p2p_scan_res_handled(wpa_s);
8596  }
8597  
8598  
wpas_p2p_stop_find_oper(struct wpa_supplicant * wpa_s)8599  static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
8600  {
8601  	wpas_p2p_clear_pending_action_tx(wpa_s, true);
8602  	wpa_s->global->p2p_long_listen = 0;
8603  	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
8604  	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
8605  
8606  	if (wpa_s->global->p2p)
8607  		p2p_stop_find(wpa_s->global->p2p);
8608  
8609  	if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
8610  		wpa_printf(MSG_DEBUG,
8611  			   "P2P: Do not consider the scan results after stop_find");
8612  		wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
8613  	}
8614  }
8615  
8616  
wpas_p2p_stop_find(struct wpa_supplicant * wpa_s)8617  void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
8618  {
8619  	wpas_p2p_stop_find_oper(wpa_s);
8620  	if (!wpa_s->global->pending_group_iface_for_p2ps)
8621  		wpas_p2p_remove_pending_group_interface(wpa_s);
8622  }
8623  
8624  
wpas_p2p_long_listen_timeout(void * eloop_ctx,void * timeout_ctx)8625  static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
8626  {
8627  	struct wpa_supplicant *wpa_s = eloop_ctx;
8628  	wpa_s->global->p2p_long_listen = 0;
8629  }
8630  
8631  
wpas_p2p_listen(struct wpa_supplicant * wpa_s,unsigned int timeout)8632  int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
8633  {
8634  	int res;
8635  
8636  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8637  		return -1;
8638  
8639  	if (wpa_s->p2p_lo_started) {
8640  		wpa_printf(MSG_DEBUG,
8641  			"P2P: Cannot start P2P listen, it is offloaded");
8642  		return -1;
8643  	}
8644  
8645  	wpa_supplicant_cancel_sched_scan(wpa_s);
8646  	wpas_p2p_clear_pending_action_tx(wpa_s, false);
8647  
8648  	if (timeout == 0) {
8649  		/*
8650  		 * This is a request for unlimited Listen state. However, at
8651  		 * least for now, this is mapped to a Listen state for one
8652  		 * hour.
8653  		 */
8654  		timeout = 3600;
8655  	}
8656  	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
8657  	wpa_s->global->p2p_long_listen = 0;
8658  
8659  	/*
8660  	 * Stop previous find/listen operation to avoid trying to request a new
8661  	 * remain-on-channel operation while the driver is still running the
8662  	 * previous one.
8663  	 */
8664  	if (wpa_s->global->p2p)
8665  		p2p_stop_find(wpa_s->global->p2p);
8666  
8667  	res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
8668  	if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
8669  		wpa_s->global->p2p_long_listen = timeout * 1000;
8670  		eloop_register_timeout(timeout, 0,
8671  				       wpas_p2p_long_listen_timeout,
8672  				       wpa_s, NULL);
8673  	}
8674  
8675  	return res;
8676  }
8677  
8678  
wpas_p2p_assoc_req_ie(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,u8 * buf,size_t len,int p2p_group)8679  int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
8680  			  u8 *buf, size_t len, int p2p_group)
8681  {
8682  	struct wpabuf *p2p_ie;
8683  	int ret;
8684  
8685  	if (wpa_s->global->p2p_disabled)
8686  		return -1;
8687  	/*
8688  	 * Advertize mandatory cross connection capability even on
8689  	 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
8690  	 */
8691  	if (wpa_s->conf->p2p_disabled && p2p_group)
8692  		return -1;
8693  	if (wpa_s->global->p2p == NULL)
8694  		return -1;
8695  	if (bss == NULL)
8696  		return -1;
8697  
8698  	p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
8699  	ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
8700  			       p2p_group, p2p_ie);
8701  	wpabuf_free(p2p_ie);
8702  
8703  	return ret;
8704  }
8705  
8706  
wpas_p2p_probe_req_rx(struct wpa_supplicant * wpa_s,const u8 * addr,const u8 * dst,const u8 * bssid,const u8 * ie,size_t ie_len,unsigned int rx_freq,int ssi_signal)8707  int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
8708  			  const u8 *dst, const u8 *bssid,
8709  			  const u8 *ie, size_t ie_len,
8710  			  unsigned int rx_freq, int ssi_signal)
8711  {
8712  	if (wpa_s->global->p2p_disabled)
8713  		return 0;
8714  	if (wpa_s->global->p2p == NULL)
8715  		return 0;
8716  
8717  	switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
8718  				 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
8719  	case P2P_PREQ_NOT_P2P:
8720  		wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
8721  				 ssi_signal);
8722  		/* fall through */
8723  	case P2P_PREQ_MALFORMED:
8724  	case P2P_PREQ_NOT_LISTEN:
8725  	case P2P_PREQ_NOT_PROCESSED:
8726  	default: /* make gcc happy */
8727  		return 0;
8728  	case P2P_PREQ_PROCESSED:
8729  		return 1;
8730  	}
8731  }
8732  
8733  
wpas_p2p_rx_action(struct wpa_supplicant * wpa_s,const u8 * da,const u8 * sa,const u8 * bssid,u8 category,const u8 * data,size_t len,int freq)8734  void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
8735  			const u8 *sa, const u8 *bssid,
8736  			u8 category, const u8 *data, size_t len, int freq)
8737  {
8738  	if (wpa_s->global->p2p_disabled)
8739  		return;
8740  	if (wpa_s->global->p2p == NULL)
8741  		return;
8742  
8743  	p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
8744  		      freq);
8745  }
8746  
8747  
wpas_p2p_scan_ie(struct wpa_supplicant * wpa_s,struct wpabuf * ies)8748  void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
8749  {
8750  	unsigned int bands;
8751  
8752  	if (wpa_s->global->p2p_disabled)
8753  		return;
8754  	if (wpa_s->global->p2p == NULL)
8755  		return;
8756  
8757  	bands = wpas_get_bands(wpa_s, NULL);
8758  	p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
8759  }
8760  
8761  
wpas_p2p_group_deinit(struct wpa_supplicant * wpa_s)8762  static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
8763  {
8764  	p2p_group_deinit(wpa_s->p2p_group);
8765  	wpa_s->p2p_group = NULL;
8766  
8767  	wpa_s->ap_configured_cb = NULL;
8768  	wpa_s->ap_configured_cb_ctx = NULL;
8769  	wpa_s->ap_configured_cb_data = NULL;
8770  	wpa_s->connect_without_scan = NULL;
8771  }
8772  
8773  
wpas_p2p_reject(struct wpa_supplicant * wpa_s,const u8 * addr)8774  int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
8775  {
8776  	wpa_s->global->p2p_long_listen = 0;
8777  
8778  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8779  		return -1;
8780  
8781  	return p2p_reject(wpa_s->global->p2p, addr);
8782  }
8783  
8784  
8785  /* Invite to reinvoke a persistent group */
wpas_p2p_invite(struct wpa_supplicant * wpa_s,const u8 * peer_addr,struct wpa_ssid * ssid,const u8 * go_dev_addr,int freq,int vht_center_freq2,int ht40,int vht,int max_chwidth,int pref_freq,int he,int edmg,bool allow_6ghz,bool p2p2)8786  int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
8787  		    struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
8788  		    int vht_center_freq2, int ht40, int vht, int max_chwidth,
8789  		    int pref_freq, int he, int edmg, bool allow_6ghz, bool p2p2)
8790  {
8791  	enum p2p_invite_role role;
8792  	u8 *bssid = NULL;
8793  	int force_freq = 0;
8794  	int res;
8795  	int no_pref_freq_given = pref_freq == 0;
8796  	struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
8797  	unsigned int size;
8798  
8799  	if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
8800  		return -1;
8801  
8802  	wpa_s->global->p2p_invite_group = NULL;
8803  	if (peer_addr)
8804  		os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
8805  	else
8806  		os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
8807  
8808  	if (wpa_s->global->p2p && p2p2 && !ssid && peer_addr) {
8809  		int dik_id;
8810  
8811  		dik_id = p2p_get_dik_id(wpa_s->global->p2p, peer_addr);
8812  		ssid = wpa_config_get_network_with_dik_id(wpa_s->conf, dik_id);
8813  		if (!ssid) {
8814  			wpa_printf(MSG_DEBUG,
8815  				   "P2P: Could not find SSID for P2P2 peer "
8816  				   MACSTR, MAC2STR(peer_addr));
8817  			return -1;
8818  		}
8819  	}
8820  
8821  	if (!ssid)
8822  		return -1;
8823  
8824  	wpa_s->p2p_persistent_go_freq = freq;
8825  	wpa_s->p2p_go_ht40 = !!ht40;
8826  	wpa_s->p2p_go_vht = !!vht;
8827  	wpa_s->p2p_go_he = !!he;
8828  	wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
8829  	wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
8830  	wpa_s->p2p_go_edmg = !!edmg;
8831  	wpa_s->p2p2 = p2p2;
8832  	if (ssid->mode == WPAS_MODE_P2P_GO) {
8833  		role = P2P_INVITE_ROLE_GO;
8834  		if (peer_addr == NULL) {
8835  			wpa_printf(MSG_DEBUG, "P2P: Missing peer "
8836  				   "address in invitation command");
8837  			return -1;
8838  		}
8839  		if (wpas_p2p_create_iface(wpa_s)) {
8840  			if (wpas_p2p_add_group_interface(wpa_s,
8841  							 WPA_IF_P2P_GO) < 0) {
8842  				wpa_printf(MSG_ERROR, "P2P: Failed to "
8843  					   "allocate a new interface for the "
8844  					   "group");
8845  				return -1;
8846  			}
8847  			bssid = wpa_s->pending_interface_addr;
8848  		} else if (wpa_s->p2p_mgmt)
8849  			bssid = wpa_s->parent->own_addr;
8850  		else
8851  			bssid = wpa_s->own_addr;
8852  	} else {
8853  		role = P2P_INVITE_ROLE_CLIENT;
8854  		if (!wpa_s->p2p2)
8855  			peer_addr = ssid->bssid;
8856  	}
8857  	wpa_s->pending_invite_ssid_id = ssid->id;
8858  
8859  	size = P2P_MAX_PREF_CHANNELS;
8860  	res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
8861  				   role == P2P_INVITE_ROLE_GO,
8862  				   pref_freq_list, &size);
8863  	if (res)
8864  		return res;
8865  
8866  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8867  		return -1;
8868  
8869  	p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
8870  
8871  	if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
8872  	    no_pref_freq_given && pref_freq > 0 &&
8873  	    wpa_s->num_multichan_concurrent > 1 &&
8874  	    wpas_p2p_num_unused_channels(wpa_s) > 0) {
8875  		wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
8876  			   pref_freq);
8877  		pref_freq = 0;
8878  	}
8879  
8880  	/*
8881  	 * Stop any find/listen operations before invitation and possibly
8882  	 * connection establishment.
8883  	 */
8884  	wpas_p2p_stop_find_oper(wpa_s);
8885  
8886  #ifdef CONFIG_PASN
8887  	if (p2p2) {
8888  		if (peer_addr &&
8889  		    wpas_p2p_initiate_pasn_verify(wpa_s, peer_addr, role, bssid,
8890  						  ssid->ssid, ssid->ssid_len,
8891  						  force_freq, go_dev_addr,
8892  						  pref_freq) < 0) {
8893  			if (wpa_s->create_p2p_iface)
8894  				wpas_p2p_remove_pending_group_interface(wpa_s);
8895  			return -1;
8896  		}
8897  		return 0;
8898  	}
8899  #endif /* CONFIG_PASN */
8900  
8901  	return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
8902  			  ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
8903  			  1, pref_freq, -1, false);
8904  }
8905  
8906  
8907  /* Invite to join an active group */
wpas_p2p_invite_group(struct wpa_supplicant * wpa_s,const char * ifname,const u8 * peer_addr,const u8 * go_dev_addr,bool allow_6ghz)8908  int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
8909  			  const u8 *peer_addr, const u8 *go_dev_addr,
8910  			  bool allow_6ghz)
8911  {
8912  	struct wpa_global *global = wpa_s->global;
8913  	enum p2p_invite_role role;
8914  	u8 *bssid = NULL;
8915  	struct wpa_ssid *ssid;
8916  	int persistent;
8917  	int freq = 0, force_freq = 0, pref_freq = 0;
8918  	int res;
8919  	struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
8920  	unsigned int size;
8921  
8922  	wpa_s->p2p_persistent_go_freq = 0;
8923  	wpa_s->p2p_go_ht40 = 0;
8924  	wpa_s->p2p_go_vht = 0;
8925  	wpa_s->p2p_go_vht_center_freq2 = 0;
8926  	wpa_s->p2p_go_max_oper_chwidth = 0;
8927  	wpa_s->p2p_go_edmg = 0;
8928  
8929  	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8930  		if (os_strcmp(wpa_s->ifname, ifname) == 0)
8931  			break;
8932  	}
8933  	if (wpa_s == NULL) {
8934  		wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
8935  		return -1;
8936  	}
8937  
8938  	ssid = wpa_s->current_ssid;
8939  	if (ssid == NULL) {
8940  		wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
8941  			   "invitation");
8942  		return -1;
8943  	}
8944  
8945  	wpa_s->global->p2p_invite_group = wpa_s;
8946  	persistent = ssid->p2p_persistent_group &&
8947  		wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
8948  					ssid->ssid, ssid->ssid_len);
8949  
8950  	if (ssid->mode == WPAS_MODE_P2P_GO) {
8951  		role = P2P_INVITE_ROLE_ACTIVE_GO;
8952  		bssid = wpa_s->own_addr;
8953  		if (go_dev_addr == NULL)
8954  			go_dev_addr = wpa_s->global->p2p_dev_addr;
8955  		freq = ssid->frequency;
8956  	} else {
8957  		role = P2P_INVITE_ROLE_CLIENT;
8958  		if (wpa_s->wpa_state < WPA_ASSOCIATED) {
8959  			wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
8960  				   "invite to current group");
8961  			return -1;
8962  		}
8963  		bssid = wpa_s->bssid;
8964  		if (go_dev_addr == NULL &&
8965  		    !is_zero_ether_addr(wpa_s->go_dev_addr))
8966  			go_dev_addr = wpa_s->go_dev_addr;
8967  		freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
8968  			(int) wpa_s->assoc_freq;
8969  	}
8970  	wpa_s->p2pdev->pending_invite_ssid_id = -1;
8971  
8972  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8973  		return -1;
8974  	if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
8975  		return -1;
8976  
8977  	size = P2P_MAX_PREF_CHANNELS;
8978  	res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
8979  				   role == P2P_INVITE_ROLE_ACTIVE_GO,
8980  				   pref_freq_list, &size);
8981  	if (res)
8982  		return res;
8983  	wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
8984  
8985  	return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
8986  			  ssid->ssid, ssid->ssid_len, force_freq,
8987  			  go_dev_addr, persistent, pref_freq, -1, false);
8988  }
8989  
8990  
wpas_p2p_completed(struct wpa_supplicant * wpa_s)8991  void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
8992  {
8993  	struct wpa_ssid *ssid = wpa_s->current_ssid;
8994  	const u8 *bssid;
8995  	u8 go_dev_addr[ETH_ALEN];
8996  	int persistent, dik_id;
8997  	int freq;
8998  	u8 ip[3 * 4], *ip_ptr = NULL;
8999  	char ip_addr[100];
9000  
9001  	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
9002  		eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
9003  				     wpa_s->p2pdev, NULL);
9004  	}
9005  
9006  	if (!wpa_s->show_group_started || !ssid)
9007  		return;
9008  
9009  	if (wpa_s->go_params)
9010  		bssid = wpa_s->go_params->peer_interface_addr;
9011  	else
9012  		bssid = wpa_s->bssid;
9013  
9014  	wpa_s->show_group_started = 0;
9015  	if (!wpa_s->p2p_go_group_formation_completed &&
9016  	    wpa_s->global->p2p_group_formation == wpa_s) {
9017  		wpa_dbg(wpa_s, MSG_DEBUG,
9018  			"P2P: Marking group formation completed on client on data connection");
9019  		wpa_s->p2p_go_group_formation_completed = 1;
9020  		wpa_s->global->p2p_group_formation = NULL;
9021  		wpa_s->p2p_in_provisioning = 0;
9022  		wpa_s->p2p_in_invitation = 0;
9023  		wpa_s->p2p_retry_limit = 0;
9024  	}
9025  
9026  	os_memset(go_dev_addr, 0, ETH_ALEN);
9027  	if (ssid->bssid_set)
9028  		os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
9029  	persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
9030  					       ssid->ssid_len);
9031  	os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
9032  
9033  	if (wpa_s->global->p2p_group_formation == wpa_s)
9034  		wpa_s->global->p2p_group_formation = NULL;
9035  
9036  	freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
9037  		(int) wpa_s->assoc_freq;
9038  
9039  	ip_addr[0] = '\0';
9040  	if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
9041  		int res;
9042  
9043  		res = os_snprintf(ip_addr, sizeof(ip_addr),
9044  				  " ip_addr=%u.%u.%u.%u "
9045  				  "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
9046  				  ip[0], ip[1], ip[2], ip[3],
9047  				  ip[4], ip[5], ip[6], ip[7],
9048  				  ip[8], ip[9], ip[10], ip[11]);
9049  		if (os_snprintf_error(sizeof(ip_addr), res))
9050  			ip_addr[0] = '\0';
9051  		ip_ptr = ip;
9052  	}
9053  
9054  	wpas_p2p_group_started(wpa_s, 0, ssid, freq,
9055  			       ssid->passphrase == NULL && ssid->psk_set ?
9056  			       ssid->psk : NULL,
9057  			       ssid->passphrase, go_dev_addr, persistent,
9058  			       ip_addr);
9059  
9060  	if (persistent) {
9061  		dik_id = wpas_p2p_store_go_identity(wpa_s, go_dev_addr, bssid);
9062  		wpas_p2p_store_persistent_group(wpa_s->p2pdev,
9063  						ssid, go_dev_addr, dik_id);
9064  	}
9065  
9066  	wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
9067  }
9068  
9069  
wpas_p2p_presence_req(struct wpa_supplicant * wpa_s,u32 duration1,u32 interval1,u32 duration2,u32 interval2)9070  int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
9071  			  u32 interval1, u32 duration2, u32 interval2)
9072  {
9073  	int ret;
9074  
9075  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9076  		return -1;
9077  
9078  	if (wpa_s->wpa_state < WPA_ASSOCIATED ||
9079  	    wpa_s->current_ssid == NULL ||
9080  	    wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
9081  		return -1;
9082  
9083  	ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
9084  			       wpa_s->own_addr, wpa_s->assoc_freq,
9085  			       duration1, interval1, duration2, interval2);
9086  	if (ret == 0)
9087  		wpa_s->waiting_presence_resp = 1;
9088  
9089  	return ret;
9090  }
9091  
9092  
wpas_p2p_ext_listen(struct wpa_supplicant * wpa_s,unsigned int period,unsigned int interval)9093  int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
9094  			unsigned int interval)
9095  {
9096  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9097  		return -1;
9098  
9099  	return p2p_ext_listen(wpa_s->global->p2p, period, interval);
9100  }
9101  
9102  
wpas_p2p_is_client(struct wpa_supplicant * wpa_s)9103  static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
9104  {
9105  	if (wpa_s->current_ssid == NULL) {
9106  		/*
9107  		 * current_ssid can be cleared when P2P client interface gets
9108  		 * disconnected, so assume this interface was used as P2P
9109  		 * client.
9110  		 */
9111  		return 1;
9112  	}
9113  	return wpa_s->current_ssid->p2p_group &&
9114  		wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
9115  }
9116  
9117  
wpas_p2p_group_idle_timeout(void * eloop_ctx,void * timeout_ctx)9118  static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
9119  {
9120  	struct wpa_supplicant *wpa_s = eloop_ctx;
9121  
9122  	if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
9123  		wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
9124  			   "disabled");
9125  		return;
9126  	}
9127  
9128  	wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
9129  		   "group");
9130  	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
9131  }
9132  
9133  
wpas_p2p_set_group_idle_timeout(struct wpa_supplicant * wpa_s)9134  static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
9135  {
9136  	int timeout;
9137  
9138  	if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
9139  		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
9140  
9141  	if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
9142  		return;
9143  
9144  	timeout = wpa_s->conf->p2p_group_idle;
9145  	if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
9146  	    (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
9147  	    timeout = P2P_MAX_CLIENT_IDLE;
9148  
9149  	if (timeout == 0)
9150  		return;
9151  
9152  	if (timeout < 0) {
9153  		if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
9154  			timeout = 0; /* special client mode no-timeout */
9155  		else
9156  			return;
9157  	}
9158  
9159  	if (wpa_s->p2p_in_provisioning) {
9160  		/*
9161  		 * Use the normal group formation timeout during the
9162  		 * provisioning phase to avoid terminating this process too
9163  		 * early due to group idle timeout.
9164  		 */
9165  		wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
9166  			   "during provisioning");
9167  		return;
9168  	}
9169  
9170  	if (wpa_s->show_group_started) {
9171  		/*
9172  		 * Use the normal group formation timeout between the end of
9173  		 * the provisioning phase and completion of 4-way handshake to
9174  		 * avoid terminating this process too early due to group idle
9175  		 * timeout.
9176  		 */
9177  		wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
9178  			   "while waiting for initial 4-way handshake to "
9179  			   "complete");
9180  		return;
9181  	}
9182  
9183  	wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
9184  		   timeout);
9185  	eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
9186  			       wpa_s, NULL);
9187  }
9188  
9189  
9190  /* Returns 1 if the interface was removed */
wpas_p2p_deauth_notif(struct wpa_supplicant * wpa_s,const u8 * bssid,u16 reason_code,const u8 * ie,size_t ie_len,int locally_generated)9191  int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
9192  			  u16 reason_code, const u8 *ie, size_t ie_len,
9193  			  int locally_generated)
9194  {
9195  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9196  		return 0;
9197  
9198  	if (!locally_generated)
9199  		p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
9200  				 ie_len);
9201  
9202  	if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
9203  	    wpa_s->current_ssid &&
9204  	    wpa_s->current_ssid->p2p_group &&
9205  	    wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
9206  		wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
9207  			   "session is ending");
9208  		if (wpas_p2p_group_delete(wpa_s,
9209  					  P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
9210  		    > 0)
9211  			return 1;
9212  	}
9213  
9214  	return 0;
9215  }
9216  
9217  
wpas_p2p_disassoc_notif(struct wpa_supplicant * wpa_s,const u8 * bssid,u16 reason_code,const u8 * ie,size_t ie_len,int locally_generated)9218  void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
9219  			     u16 reason_code, const u8 *ie, size_t ie_len,
9220  			     int locally_generated)
9221  {
9222  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9223  		return;
9224  
9225  	if (!locally_generated)
9226  		p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
9227  				   ie_len);
9228  }
9229  
9230  
wpas_p2p_update_config(struct wpa_supplicant * wpa_s)9231  void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
9232  {
9233  	struct p2p_data *p2p = wpa_s->global->p2p;
9234  
9235  	if (p2p == NULL)
9236  		return;
9237  
9238  	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
9239  		return;
9240  
9241  	if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
9242  		p2p_set_dev_name(p2p, wpa_s->conf->device_name);
9243  
9244  	if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
9245  		p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
9246  
9247  	if (wpa_s->wps &&
9248  	    (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
9249  		p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
9250  
9251  	if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
9252  		p2p_set_uuid(p2p, wpa_s->wps->uuid);
9253  
9254  	if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
9255  		p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
9256  		p2p_set_model_name(p2p, wpa_s->conf->model_name);
9257  		p2p_set_model_number(p2p, wpa_s->conf->model_number);
9258  		p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
9259  	}
9260  
9261  	if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
9262  		p2p_set_sec_dev_types(p2p,
9263  				      (void *) wpa_s->conf->sec_device_type,
9264  				      wpa_s->conf->num_sec_device_types);
9265  
9266  	if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
9267  		int i;
9268  		p2p_remove_wps_vendor_extensions(p2p);
9269  		for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
9270  			if (wpa_s->conf->wps_vendor_ext[i] == NULL)
9271  				continue;
9272  			p2p_add_wps_vendor_extension(
9273  				p2p, wpa_s->conf->wps_vendor_ext[i]);
9274  		}
9275  	}
9276  
9277  	if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
9278  	    wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
9279  		char country[3];
9280  		country[0] = wpa_s->conf->country[0];
9281  		country[1] = wpa_s->conf->country[1];
9282  		country[2] = 0x04;
9283  		p2p_set_country(p2p, country);
9284  	}
9285  
9286  	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
9287  		p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
9288  				     wpa_s->conf->p2p_ssid_postfix ?
9289  				     os_strlen(wpa_s->conf->p2p_ssid_postfix) :
9290  				     0);
9291  	}
9292  
9293  	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
9294  		p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
9295  
9296  	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
9297  		u8 reg_class, channel;
9298  		int ret;
9299  		unsigned int r;
9300  		u8 channel_forced;
9301  
9302  		if (wpa_s->conf->p2p_listen_reg_class &&
9303  		    wpa_s->conf->p2p_listen_channel) {
9304  			reg_class = wpa_s->conf->p2p_listen_reg_class;
9305  			channel = wpa_s->conf->p2p_listen_channel;
9306  			channel_forced = 1;
9307  		} else {
9308  			reg_class = 81;
9309  			/*
9310  			 * Pick one of the social channels randomly as the
9311  			 * listen channel.
9312  			 */
9313  			if (os_get_random((u8 *) &r, sizeof(r)) < 0)
9314  				channel = 1;
9315  			else
9316  				channel = 1 + (r % 3) * 5;
9317  			channel_forced = 0;
9318  		}
9319  		ret = p2p_set_listen_channel(p2p, reg_class, channel,
9320  					     channel_forced);
9321  		if (ret)
9322  			wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
9323  				   "failed: %d", ret);
9324  	}
9325  	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
9326  		u8 op_reg_class, op_channel, cfg_op_channel;
9327  		int ret = 0;
9328  		unsigned int r;
9329  		if (wpa_s->conf->p2p_oper_reg_class &&
9330  		    wpa_s->conf->p2p_oper_channel) {
9331  			op_reg_class = wpa_s->conf->p2p_oper_reg_class;
9332  			op_channel = wpa_s->conf->p2p_oper_channel;
9333  			cfg_op_channel = 1;
9334  		} else {
9335  			op_reg_class = 81;
9336  			/*
9337  			 * Use random operation channel from (1, 6, 11)
9338  			 *if no other preference is indicated.
9339  			 */
9340  			if (os_get_random((u8 *) &r, sizeof(r)) < 0)
9341  				op_channel = 1;
9342  			else
9343  				op_channel = 1 + (r % 3) * 5;
9344  			cfg_op_channel = 0;
9345  		}
9346  		ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
9347  					   cfg_op_channel);
9348  		if (ret)
9349  			wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
9350  				   "failed: %d", ret);
9351  	}
9352  
9353  	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
9354  		if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
9355  				      wpa_s->conf->p2p_pref_chan) < 0) {
9356  			wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
9357  				   "update failed");
9358  		}
9359  
9360  		if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
9361  			wpa_printf(MSG_ERROR, "P2P: No GO channel list "
9362  				   "update failed");
9363  		}
9364  	}
9365  
9366  	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
9367  		p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
9368  }
9369  
9370  
wpas_p2p_set_noa(struct wpa_supplicant * wpa_s,u8 count,int start,int duration)9371  int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
9372  		     int duration)
9373  {
9374  	if (!wpa_s->ap_iface)
9375  		return -1;
9376  	return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
9377  				   duration);
9378  }
9379  
9380  
wpas_p2p_set_cross_connect(struct wpa_supplicant * wpa_s,int enabled)9381  int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
9382  {
9383  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9384  		return -1;
9385  
9386  	wpa_s->global->cross_connection = enabled;
9387  	p2p_set_cross_connect(wpa_s->global->p2p, enabled);
9388  
9389  	if (!enabled) {
9390  		struct wpa_supplicant *iface;
9391  
9392  		for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9393  		{
9394  			if (iface->cross_connect_enabled == 0)
9395  				continue;
9396  
9397  			iface->cross_connect_enabled = 0;
9398  			iface->cross_connect_in_use = 0;
9399  			wpa_msg_global(iface->p2pdev, MSG_INFO,
9400  				       P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
9401  				       iface->ifname,
9402  				       iface->cross_connect_uplink);
9403  		}
9404  	}
9405  
9406  	return 0;
9407  }
9408  
9409  
wpas_p2p_enable_cross_connect(struct wpa_supplicant * uplink)9410  static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
9411  {
9412  	struct wpa_supplicant *iface;
9413  
9414  	if (!uplink->global->cross_connection)
9415  		return;
9416  
9417  	for (iface = uplink->global->ifaces; iface; iface = iface->next) {
9418  		if (!iface->cross_connect_enabled)
9419  			continue;
9420  		if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
9421  		    0)
9422  			continue;
9423  		if (iface->ap_iface == NULL)
9424  			continue;
9425  		if (iface->cross_connect_in_use)
9426  			continue;
9427  
9428  		iface->cross_connect_in_use = 1;
9429  		wpa_msg_global(iface->p2pdev, MSG_INFO,
9430  			       P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
9431  			       iface->ifname, iface->cross_connect_uplink);
9432  	}
9433  }
9434  
9435  
wpas_p2p_disable_cross_connect(struct wpa_supplicant * uplink)9436  static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
9437  {
9438  	struct wpa_supplicant *iface;
9439  
9440  	for (iface = uplink->global->ifaces; iface; iface = iface->next) {
9441  		if (!iface->cross_connect_enabled)
9442  			continue;
9443  		if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
9444  		    0)
9445  			continue;
9446  		if (!iface->cross_connect_in_use)
9447  			continue;
9448  
9449  		wpa_msg_global(iface->p2pdev, MSG_INFO,
9450  			       P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
9451  			       iface->ifname, iface->cross_connect_uplink);
9452  		iface->cross_connect_in_use = 0;
9453  	}
9454  }
9455  
9456  
wpas_p2p_notif_connected(struct wpa_supplicant * wpa_s)9457  void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
9458  {
9459  	if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
9460  	    wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
9461  	    wpa_s->cross_connect_disallowed)
9462  		wpas_p2p_disable_cross_connect(wpa_s);
9463  	else
9464  		wpas_p2p_enable_cross_connect(wpa_s);
9465  	if (!wpa_s->ap_iface &&
9466  	    eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
9467  		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
9468  }
9469  
9470  
wpas_p2p_notif_disconnected(struct wpa_supplicant * wpa_s)9471  void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
9472  {
9473  	wpas_p2p_disable_cross_connect(wpa_s);
9474  	if (!wpa_s->ap_iface &&
9475  	    !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
9476  					 wpa_s, NULL))
9477  		wpas_p2p_set_group_idle_timeout(wpa_s);
9478  }
9479  
9480  
wpas_p2p_cross_connect_setup(struct wpa_supplicant * wpa_s)9481  static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
9482  {
9483  	struct wpa_supplicant *iface;
9484  
9485  	if (!wpa_s->global->cross_connection)
9486  		return;
9487  
9488  	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9489  		if (iface == wpa_s)
9490  			continue;
9491  		if (iface->drv_flags &
9492  		    WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
9493  			continue;
9494  		if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
9495  		    iface != wpa_s->parent)
9496  			continue;
9497  
9498  		wpa_s->cross_connect_enabled = 1;
9499  		os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
9500  			   sizeof(wpa_s->cross_connect_uplink));
9501  		wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
9502  			   "%s to %s whenever uplink is available",
9503  			   wpa_s->ifname, wpa_s->cross_connect_uplink);
9504  
9505  		if (iface->ap_iface || iface->current_ssid == NULL ||
9506  		    iface->current_ssid->mode != WPAS_MODE_INFRA ||
9507  		    iface->cross_connect_disallowed ||
9508  		    iface->wpa_state != WPA_COMPLETED)
9509  			break;
9510  
9511  		wpa_s->cross_connect_in_use = 1;
9512  		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
9513  			       P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
9514  			       wpa_s->ifname, wpa_s->cross_connect_uplink);
9515  		break;
9516  	}
9517  }
9518  
9519  
wpas_p2p_notif_pbc_overlap(struct wpa_supplicant * wpa_s)9520  static int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
9521  {
9522  	if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
9523  	    !wpa_s->p2p_in_provisioning)
9524  		return 0; /* not P2P client operation */
9525  
9526  	wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
9527  		   "session overlap");
9528  	if (wpa_s != wpa_s->p2pdev)
9529  		wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
9530  	wpas_p2p_group_formation_failed(wpa_s, 0, "WPS PBC session overlap");
9531  	return 1;
9532  }
9533  
9534  
wpas_p2p_pbc_overlap_cb(void * eloop_ctx,void * timeout_ctx)9535  void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
9536  {
9537  	struct wpa_supplicant *wpa_s = eloop_ctx;
9538  	wpas_p2p_notif_pbc_overlap(wpa_s);
9539  }
9540  
9541  
wpas_p2p_update_channel_list(struct wpa_supplicant * wpa_s,enum wpas_p2p_channel_update_trig trig)9542  void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
9543  				  enum wpas_p2p_channel_update_trig trig)
9544  {
9545  	struct p2p_channels chan, cli_chan;
9546  	struct wpa_used_freq_data *freqs = NULL;
9547  	unsigned int num = wpa_s->num_multichan_concurrent;
9548  
9549  	if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
9550  		return;
9551  
9552  	freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9553  	if (!freqs)
9554  		return;
9555  
9556  	num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
9557  
9558  	os_memset(&chan, 0, sizeof(chan));
9559  	os_memset(&cli_chan, 0, sizeof(cli_chan));
9560  	if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
9561  				    is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
9562  		wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
9563  			   "channel list");
9564  		return;
9565  	}
9566  
9567  	p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
9568  
9569  	wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
9570  
9571  	/*
9572  	 * The used frequencies map changed, so it is possible that a GO is
9573  	 * using a channel that is no longer valid for P2P use. It is also
9574  	 * possible that due to policy consideration, it would be preferable to
9575  	 * move it to a frequency already used by other station interfaces.
9576  	 */
9577  	wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
9578  
9579  	os_free(freqs);
9580  }
9581  
9582  
wpas_p2p_scan_res_ignore(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res)9583  static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
9584  				     struct wpa_scan_results *scan_res)
9585  {
9586  	wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
9587  }
9588  
9589  
wpas_p2p_cancel(struct wpa_supplicant * wpa_s)9590  int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
9591  {
9592  	struct wpa_global *global = wpa_s->global;
9593  	int found = 0;
9594  	const u8 *peer;
9595  
9596  	if (global->p2p == NULL)
9597  		return -1;
9598  
9599  	wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
9600  
9601  	if (wpa_s->pending_interface_name[0] &&
9602  	    !is_zero_ether_addr(wpa_s->pending_interface_addr))
9603  		found = 1;
9604  
9605  	peer = p2p_get_go_neg_peer(global->p2p);
9606  	if (peer) {
9607  		wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
9608  			   MACSTR, MAC2STR(peer));
9609  		p2p_unauthorize(global->p2p, peer);
9610  		found = 1;
9611  	}
9612  
9613  	if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
9614  		wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
9615  		wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
9616  		found = 1;
9617  	}
9618  
9619  	if (wpa_s->pending_pd_before_join) {
9620  		wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
9621  		wpa_s->pending_pd_before_join = 0;
9622  		found = 1;
9623  	}
9624  
9625  	wpas_p2p_stop_find(wpa_s);
9626  
9627  	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9628  		if (wpa_s == global->p2p_group_formation &&
9629  		    (wpa_s->p2p_in_provisioning ||
9630  		     wpa_s->parent->pending_interface_type ==
9631  		     WPA_IF_P2P_CLIENT)) {
9632  			wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
9633  				   "formation found - cancelling",
9634  				   wpa_s->ifname);
9635  			found = 1;
9636  			eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
9637  					     wpa_s->p2pdev, NULL);
9638  			if (wpa_s->p2p_in_provisioning) {
9639  				wpas_group_formation_completed(wpa_s, 0,
9640  							       "Canceled");
9641  				break;
9642  			}
9643  			wpas_p2p_group_delete(wpa_s,
9644  					      P2P_GROUP_REMOVAL_REQUESTED);
9645  			break;
9646  		} else if (wpa_s->p2p_in_invitation) {
9647  			wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
9648  				   wpa_s->ifname);
9649  			found = 1;
9650  			wpas_p2p_group_formation_failed(wpa_s, 0, "Canceled");
9651  			break;
9652  		}
9653  	}
9654  
9655  	if (!found) {
9656  		wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
9657  		return -1;
9658  	}
9659  
9660  	return 0;
9661  }
9662  
9663  
wpas_p2p_interface_unavailable(struct wpa_supplicant * wpa_s)9664  void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
9665  {
9666  	if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
9667  		return;
9668  
9669  	wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
9670  		   "being available anymore");
9671  	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
9672  }
9673  
9674  
wpas_p2p_update_best_channels(struct wpa_supplicant * wpa_s,int freq_24,int freq_5,int freq_overall)9675  void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
9676  				   int freq_24, int freq_5, int freq_overall)
9677  {
9678  	struct p2p_data *p2p = wpa_s->global->p2p;
9679  	if (p2p == NULL)
9680  		return;
9681  	p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
9682  }
9683  
9684  
wpas_p2p_unauthorize(struct wpa_supplicant * wpa_s,const char * addr)9685  int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
9686  {
9687  	u8 peer[ETH_ALEN];
9688  	struct p2p_data *p2p = wpa_s->global->p2p;
9689  
9690  	if (p2p == NULL)
9691  		return -1;
9692  
9693  	if (hwaddr_aton(addr, peer))
9694  		return -1;
9695  
9696  	return p2p_unauthorize(p2p, peer);
9697  }
9698  
9699  
9700  /**
9701   * wpas_p2p_disconnect - Disconnect from a P2P Group
9702   * @wpa_s: Pointer to wpa_supplicant data
9703   * Returns: 0 on success, -1 on failure
9704   *
9705   * This can be used to disconnect from a group in which the local end is a P2P
9706   * Client or to end a P2P Group in case the local end is the Group Owner. If a
9707   * virtual network interface was created for this group, that interface will be
9708   * removed. Otherwise, only the configured P2P group network will be removed
9709   * from the interface.
9710   */
wpas_p2p_disconnect(struct wpa_supplicant * wpa_s)9711  int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
9712  {
9713  
9714  	if (wpa_s == NULL)
9715  		return -1;
9716  
9717  	return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
9718  		-1 : 0;
9719  }
9720  
9721  
wpas_p2p_in_progress(struct wpa_supplicant * wpa_s)9722  int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
9723  {
9724  	int ret;
9725  
9726  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9727  		return 0;
9728  
9729  	ret = p2p_in_progress(wpa_s->global->p2p);
9730  	if (ret == 0) {
9731  		/*
9732  		 * Check whether there is an ongoing WPS provisioning step (or
9733  		 * other parts of group formation) on another interface since
9734  		 * p2p_in_progress() does not report this to avoid issues for
9735  		 * scans during such provisioning step.
9736  		 */
9737  		if (wpa_s->global->p2p_group_formation &&
9738  		    wpa_s->global->p2p_group_formation != wpa_s) {
9739  			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
9740  				"in group formation",
9741  				wpa_s->global->p2p_group_formation->ifname);
9742  			ret = 1;
9743  		} else if (wpa_s->global->p2p_group_formation == wpa_s) {
9744  			wpa_dbg(wpa_s, MSG_DEBUG,
9745  				"P2P: Skip Extended Listen timeout and allow scans on current interface for group formation");
9746  			ret = 2;
9747  		}
9748  	}
9749  
9750  	if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
9751  		struct os_reltime now;
9752  		os_get_reltime(&now);
9753  		if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
9754  				       P2P_MAX_INITIAL_CONN_WAIT_GO)) {
9755  			/* Wait for the first client has expired */
9756  			wpa_s->global->p2p_go_wait_client.sec = 0;
9757  		} else {
9758  			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
9759  			ret = 1;
9760  		}
9761  	}
9762  
9763  	return ret;
9764  }
9765  
9766  
wpas_p2p_network_removed(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)9767  void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
9768  			      struct wpa_ssid *ssid)
9769  {
9770  	if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
9771  	    eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
9772  				 wpa_s->p2pdev, NULL) > 0) {
9773  		/**
9774  		 * Remove the network by scheduling the group formation
9775  		 * timeout to happen immediately. The teardown code
9776  		 * needs to be scheduled to run asynch later so that we
9777  		 * don't delete data from under ourselves unexpectedly.
9778  		 * Calling wpas_p2p_group_formation_timeout directly
9779  		 * causes a series of crashes in WPS failure scenarios.
9780  		 */
9781  		wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
9782  			   "P2P group network getting removed");
9783  		eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
9784  				       wpa_s->p2pdev, NULL);
9785  	}
9786  }
9787  
9788  
wpas_p2p_get_persistent(struct wpa_supplicant * wpa_s,const u8 * addr,const u8 * ssid,size_t ssid_len)9789  struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
9790  					  const u8 *addr, const u8 *ssid,
9791  					  size_t ssid_len)
9792  {
9793  	struct wpa_ssid *s;
9794  	size_t i;
9795  
9796  	for (s = wpa_s->conf->ssid; s; s = s->next) {
9797  		if (s->disabled != 2)
9798  			continue;
9799  		if (ssid &&
9800  		    (ssid_len != s->ssid_len ||
9801  		     os_memcmp(ssid, s->ssid, ssid_len) != 0))
9802  			continue;
9803  		if (addr == NULL) {
9804  			if (s->mode == WPAS_MODE_P2P_GO)
9805  				return s;
9806  			continue;
9807  		}
9808  		if (ether_addr_equal(s->bssid, addr))
9809  			return s; /* peer is GO in the persistent group */
9810  		if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
9811  			continue;
9812  		for (i = 0; i < s->num_p2p_clients; i++) {
9813  			if (ether_addr_equal(s->p2p_client_list +
9814  					     i * 2 * ETH_ALEN, addr))
9815  				return s; /* peer is P2P client in persistent
9816  					   * group */
9817  		}
9818  	}
9819  
9820  	return NULL;
9821  }
9822  
9823  
wpas_p2p_store_client_identity(struct wpa_supplicant * wpa_s,const u8 * addr)9824  static int wpas_p2p_store_client_identity(struct wpa_supplicant *wpa_s,
9825  					  const u8 *addr)
9826  {
9827  	u8 cipher;
9828  	size_t dik_len;
9829  	const u8 *dik_data;
9830  	const u8 *pmk, *pmkid;
9831  	size_t pmk_len;
9832  	u8 iface_addr[ETH_ALEN];
9833  	struct hostapd_data *hapd;
9834  	struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
9835  
9836  	if (!wpa_s->p2p2 || !wpa_s->ap_iface)
9837  		return 0;
9838  
9839  	hapd = wpa_s->ap_iface->bss[0];
9840  	if (!hapd)
9841  		return 0;
9842  
9843  	if (p2p_get_dev_identity_key(p2p_wpa_s->global->p2p, addr,
9844  				     &dik_data, &dik_len, &cipher))
9845  		return 0;
9846  
9847  	wpa_printf(MSG_DEBUG, "P2P: Fetch PMK from client (Device Addr " MACSTR
9848  		   ")", MAC2STR(addr));
9849  	if (wpa_auth_pmksa_get_pmk(hapd->wpa_auth, addr, &pmk, &pmk_len,
9850  				   &pmkid)) {
9851  		if (p2p_get_interface_addr(p2p_wpa_s->global->p2p, addr,
9852  					   iface_addr))
9853  			return 0;
9854  		wpa_printf(MSG_DEBUG,
9855  			   "P2P: Fetch PMK from client (Interface Addr " MACSTR
9856  			   ")", MAC2STR(iface_addr));
9857  		if (wpa_auth_pmksa_get_pmk(hapd->wpa_auth, iface_addr, &pmk,
9858  					   &pmk_len, &pmkid))
9859  			return 0;
9860  	}
9861  
9862  	wpa_printf(MSG_DEBUG,
9863  		   "P2P: Storing device identity of client (Device Addr "
9864  		   MACSTR ")", MAC2STR(addr));
9865  	return wpas_p2p_store_identity(p2p_wpa_s, cipher, dik_data, dik_len,
9866  				       pmk, pmk_len, pmkid);
9867  }
9868  
9869  
wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant * wpa_s,const u8 * addr)9870  void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
9871  				       const u8 *addr)
9872  {
9873  	int dik_id;
9874  
9875  	if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
9876  				 wpa_s->p2pdev, NULL) > 0) {
9877  		/*
9878  		 * This can happen if WPS provisioning step is not terminated
9879  		 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
9880  		 * peer was able to connect, there is no need to time out group
9881  		 * formation after this, though. In addition, this is used with
9882  		 * the initial connection wait on the GO as a separate formation
9883  		 * timeout and as such, expected to be hit after the initial WPS
9884  		 * provisioning step.
9885  		 */
9886  		wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
9887  
9888  		if (!wpa_s->p2p_go_group_formation_completed &&
9889  		    !wpa_s->group_formation_reported) {
9890  			/*
9891  			 * GO has not yet notified group formation success since
9892  			 * the WPS step was not completed cleanly. Do that
9893  			 * notification now since the P2P Client was able to
9894  			 * connect and as such, must have received the
9895  			 * credential from the WPS step.
9896  			 */
9897  			if (wpa_s->global->p2p)
9898  				p2p_wps_success_cb(wpa_s->global->p2p, addr);
9899  			wpas_group_formation_completed(wpa_s, 0, NULL);
9900  		}
9901  	}
9902  	if (!wpa_s->p2p_go_group_formation_completed) {
9903  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
9904  		wpa_s->p2p_go_group_formation_completed = 1;
9905  		wpa_s->global->p2p_group_formation = NULL;
9906  		wpa_s->p2p_in_provisioning = 0;
9907  		wpa_s->p2p_in_invitation = 0;
9908  		wpa_s->p2p_retry_limit = 0;
9909  	}
9910  	wpa_s->global->p2p_go_wait_client.sec = 0;
9911  	if (addr == NULL)
9912  		return;
9913  
9914  	dik_id = wpas_p2p_store_client_identity(wpa_s, addr);
9915  	wpas_p2p_add_persistent_group_client(wpa_s, addr, dik_id);
9916  }
9917  
9918  
wpas_p2p_fallback_to_go_neg(struct wpa_supplicant * wpa_s,int group_added)9919  static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
9920  				       int group_added)
9921  {
9922  	struct wpa_supplicant *group = wpa_s;
9923  	int ret = 0;
9924  
9925  	if (wpa_s->global->p2p_group_formation)
9926  		group = wpa_s->global->p2p_group_formation;
9927  	wpa_s = wpa_s->global->p2p_init_wpa_s;
9928  	offchannel_send_action_done(wpa_s);
9929  	if (group_added)
9930  		ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
9931  	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
9932  	wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
9933  			 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
9934  			 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
9935  			 wpa_s->p2p_go_vht_center_freq2,
9936  			 wpa_s->p2p_persistent_id,
9937  			 wpa_s->p2p_pd_before_go_neg,
9938  			 wpa_s->p2p_go_ht40,
9939  			 wpa_s->p2p_go_vht,
9940  			 wpa_s->p2p_go_max_oper_chwidth,
9941  			 wpa_s->p2p_go_he,
9942  			 wpa_s->p2p_go_edmg,
9943  			 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p),
9944  			 wpa_s->p2p2, wpa_s->p2p_bootstrap, NULL, false);
9945  	return ret;
9946  }
9947  
9948  
wpas_p2p_scan_no_go_seen(struct wpa_supplicant * wpa_s)9949  int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
9950  {
9951  	int res;
9952  
9953  	if (!wpa_s->p2p_fallback_to_go_neg ||
9954  	    wpa_s->p2p_in_provisioning <= 5)
9955  		return 0;
9956  
9957  	if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
9958  		return 0; /* peer operating as a GO */
9959  
9960  	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
9961  		"fallback to GO Negotiation");
9962  	wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
9963  		       "reason=GO-not-found");
9964  	res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
9965  
9966  	return res == 1 ? 2 : 1;
9967  }
9968  
9969  
wpas_p2p_search_delay(struct wpa_supplicant * wpa_s)9970  unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
9971  {
9972  	struct wpa_supplicant *ifs;
9973  
9974  	if (wpa_s->wpa_state > WPA_SCANNING) {
9975  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
9976  			"concurrent operation",
9977  			wpa_s->conf->p2p_search_delay);
9978  		return wpa_s->conf->p2p_search_delay;
9979  	}
9980  
9981  	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9982  			 radio_list) {
9983  		if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
9984  			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
9985  				"delay due to concurrent operation on "
9986  				"interface %s",
9987  				wpa_s->conf->p2p_search_delay,
9988  				ifs->ifname);
9989  			return wpa_s->conf->p2p_search_delay;
9990  		}
9991  	}
9992  
9993  	return 0;
9994  }
9995  
9996  
wpas_p2p_remove_psk_entry(struct wpa_supplicant * wpa_s,struct wpa_ssid * s,const u8 * addr,int iface_addr)9997  static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
9998  				     struct wpa_ssid *s, const u8 *addr,
9999  				     int iface_addr)
10000  {
10001  	struct psk_list_entry *psk, *tmp;
10002  	int changed = 0;
10003  
10004  	dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
10005  			      list) {
10006  		if ((iface_addr && !psk->p2p &&
10007  		     ether_addr_equal(addr, psk->addr)) ||
10008  		    (!iface_addr && psk->p2p &&
10009  		     ether_addr_equal(addr, psk->addr))) {
10010  			wpa_dbg(wpa_s, MSG_DEBUG,
10011  				"P2P: Remove persistent group PSK list entry for "
10012  				MACSTR " p2p=%u",
10013  				MAC2STR(psk->addr), psk->p2p);
10014  			dl_list_del(&psk->list);
10015  			os_free(psk);
10016  			changed++;
10017  		}
10018  	}
10019  
10020  	return changed;
10021  }
10022  
10023  
wpas_p2p_new_psk_cb(struct wpa_supplicant * wpa_s,const u8 * mac_addr,const u8 * p2p_dev_addr,const u8 * psk,size_t psk_len)10024  void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
10025  			 const u8 *p2p_dev_addr,
10026  			 const u8 *psk, size_t psk_len)
10027  {
10028  	struct wpa_ssid *ssid = wpa_s->current_ssid;
10029  	struct wpa_ssid *persistent;
10030  	struct psk_list_entry *p, *last;
10031  
10032  	if (psk_len != sizeof(p->psk))
10033  		return;
10034  
10035  	if (p2p_dev_addr) {
10036  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
10037  			" p2p_dev_addr=" MACSTR,
10038  			MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
10039  		if (is_zero_ether_addr(p2p_dev_addr))
10040  			p2p_dev_addr = NULL;
10041  	} else {
10042  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
10043  			MAC2STR(mac_addr));
10044  	}
10045  
10046  	if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
10047  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
10048  		/* To be added to persistent group once created */
10049  		if (wpa_s->global->add_psk == NULL) {
10050  			wpa_s->global->add_psk = os_zalloc(sizeof(*p));
10051  			if (wpa_s->global->add_psk == NULL)
10052  				return;
10053  		}
10054  		p = wpa_s->global->add_psk;
10055  		if (p2p_dev_addr) {
10056  			p->p2p = 1;
10057  			os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
10058  		} else {
10059  			p->p2p = 0;
10060  			os_memcpy(p->addr, mac_addr, ETH_ALEN);
10061  		}
10062  		os_memcpy(p->psk, psk, psk_len);
10063  		return;
10064  	}
10065  
10066  	if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
10067  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
10068  		return;
10069  	}
10070  
10071  	persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
10072  					     ssid->ssid_len);
10073  	if (!persistent) {
10074  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
10075  		return;
10076  	}
10077  
10078  	p = os_zalloc(sizeof(*p));
10079  	if (p == NULL)
10080  		return;
10081  	if (p2p_dev_addr) {
10082  		p->p2p = 1;
10083  		os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
10084  	} else {
10085  		p->p2p = 0;
10086  		os_memcpy(p->addr, mac_addr, ETH_ALEN);
10087  	}
10088  	os_memcpy(p->psk, psk, psk_len);
10089  
10090  	if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
10091  	    (last = dl_list_last(&persistent->psk_list,
10092  				 struct psk_list_entry, list))) {
10093  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
10094  			MACSTR " (p2p=%u) to make room for a new one",
10095  			MAC2STR(last->addr), last->p2p);
10096  		dl_list_del(&last->list);
10097  		os_free(last);
10098  	}
10099  
10100  	wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
10101  				  p2p_dev_addr ? p2p_dev_addr : mac_addr,
10102  				  p2p_dev_addr == NULL);
10103  	if (p2p_dev_addr) {
10104  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
10105  			MACSTR, MAC2STR(p2p_dev_addr));
10106  	} else {
10107  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
10108  			MAC2STR(mac_addr));
10109  	}
10110  	dl_list_add(&persistent->psk_list, &p->list);
10111  
10112  	if (wpa_s->p2pdev->conf->update_config &&
10113  	    wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
10114  		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
10115  }
10116  
10117  
wpas_p2p_remove_psk(struct wpa_supplicant * wpa_s,struct wpa_ssid * s,const u8 * addr,int iface_addr)10118  static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
10119  				struct wpa_ssid *s, const u8 *addr,
10120  				int iface_addr)
10121  {
10122  	int res;
10123  
10124  	res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
10125  	if (res > 0 && wpa_s->conf->update_config &&
10126  	    wpa_config_write(wpa_s->confname, wpa_s->conf))
10127  		wpa_dbg(wpa_s, MSG_DEBUG,
10128  			"P2P: Failed to update configuration");
10129  }
10130  
10131  
wpas_p2p_remove_client_go(struct wpa_supplicant * wpa_s,const u8 * peer,int iface_addr)10132  static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
10133  				      const u8 *peer, int iface_addr)
10134  {
10135  	struct hostapd_data *hapd;
10136  	struct hostapd_wpa_psk *psk, *prev, *rem;
10137  	struct sta_info *sta;
10138  
10139  	if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
10140  	    wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
10141  		return;
10142  
10143  	/* Remove per-station PSK entry */
10144  	hapd = wpa_s->ap_iface->bss[0];
10145  	prev = NULL;
10146  	psk = hapd->conf->ssid.wpa_psk;
10147  	while (psk) {
10148  		if ((iface_addr && ether_addr_equal(peer, psk->addr)) ||
10149  		    (!iface_addr &&
10150  		     ether_addr_equal(peer, psk->p2p_dev_addr))) {
10151  			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
10152  				MACSTR " iface_addr=%d",
10153  				MAC2STR(peer), iface_addr);
10154  			if (prev)
10155  				prev->next = psk->next;
10156  			else
10157  				hapd->conf->ssid.wpa_psk = psk->next;
10158  			rem = psk;
10159  			psk = psk->next;
10160  			bin_clear_free(rem, sizeof(*rem));
10161  		} else {
10162  			prev = psk;
10163  			psk = psk->next;
10164  		}
10165  	}
10166  
10167  	/* Disconnect from group */
10168  	if (iface_addr)
10169  		sta = ap_get_sta(hapd, peer);
10170  	else
10171  		sta = ap_get_sta_p2p(hapd, peer);
10172  	if (sta) {
10173  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
10174  			" (iface_addr=%d) from group",
10175  			MAC2STR(peer), iface_addr);
10176  		hostapd_drv_sta_deauth(hapd, sta->addr,
10177  				       WLAN_REASON_DEAUTH_LEAVING);
10178  		ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
10179  	}
10180  }
10181  
10182  
wpas_p2p_remove_client(struct wpa_supplicant * wpa_s,const u8 * peer,int iface_addr)10183  void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
10184  			    int iface_addr)
10185  {
10186  	struct wpa_ssid *s;
10187  	struct wpa_supplicant *w;
10188  	struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
10189  
10190  	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
10191  
10192  	/* Remove from any persistent group */
10193  	for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
10194  		if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
10195  			continue;
10196  		if (!iface_addr)
10197  			wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
10198  		wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
10199  	}
10200  
10201  	/* Remove from any operating group */
10202  	for (w = wpa_s->global->ifaces; w; w = w->next)
10203  		wpas_p2p_remove_client_go(w, peer, iface_addr);
10204  }
10205  
10206  
wpas_p2p_psk_failure_removal(void * eloop_ctx,void * timeout_ctx)10207  static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
10208  {
10209  	struct wpa_supplicant *wpa_s = eloop_ctx;
10210  	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
10211  }
10212  
10213  
wpas_p2p_group_freq_conflict(void * eloop_ctx,void * timeout_ctx)10214  static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
10215  {
10216  	struct wpa_supplicant *wpa_s = eloop_ctx;
10217  
10218  	wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
10219  	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
10220  }
10221  
10222  
wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant * wpa_s,int freq,struct wpa_ssid * ssid)10223  int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
10224  					struct wpa_ssid *ssid)
10225  {
10226  	struct wpa_supplicant *iface;
10227  
10228  	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
10229  		if (!iface->current_ssid ||
10230  		    iface->current_ssid->frequency == freq ||
10231  		    (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
10232  		     !iface->current_ssid->p2p_group))
10233  			continue;
10234  
10235  		/* Remove the connection with least priority */
10236  		if (!wpas_is_p2p_prioritized(iface)) {
10237  			/* STA connection has priority over existing
10238  			 * P2P connection, so remove the interface. */
10239  			wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
10240  			eloop_register_timeout(0, 0,
10241  					       wpas_p2p_group_freq_conflict,
10242  					       iface, NULL);
10243  			/* If connection in progress is P2P connection, do not
10244  			 * proceed for the connection. */
10245  			if (wpa_s == iface)
10246  				return -1;
10247  			else
10248  				return 0;
10249  		} else {
10250  			/* P2P connection has priority, disable the STA network
10251  			 */
10252  			wpa_supplicant_disable_network(wpa_s->global->ifaces,
10253  						       ssid);
10254  			wpa_msg(wpa_s->global->ifaces, MSG_INFO,
10255  				WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
10256  			os_memset(wpa_s->global->ifaces->pending_bssid, 0,
10257  				  ETH_ALEN);
10258  			/* If P2P connection is in progress, continue
10259  			 * connecting...*/
10260  			if (wpa_s == iface)
10261  				return 0;
10262  			else
10263  				return -1;
10264  		}
10265  	}
10266  
10267  	return 0;
10268  }
10269  
10270  
wpas_p2p_4way_hs_failed(struct wpa_supplicant * wpa_s)10271  int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
10272  {
10273  	struct wpa_ssid *ssid = wpa_s->current_ssid;
10274  
10275  	if (ssid == NULL || !ssid->p2p_group)
10276  		return 0;
10277  
10278  	if (wpa_s->p2p_last_4way_hs_fail &&
10279  	    wpa_s->p2p_last_4way_hs_fail == ssid) {
10280  		u8 go_dev_addr[ETH_ALEN];
10281  		struct wpa_ssid *persistent;
10282  
10283  		if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
10284  					      ssid->ssid,
10285  					      ssid->ssid_len) <= 0) {
10286  			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
10287  			goto disconnect;
10288  		}
10289  
10290  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
10291  			MACSTR, MAC2STR(go_dev_addr));
10292  		persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
10293  						     ssid->ssid,
10294  						     ssid->ssid_len);
10295  		if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
10296  			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
10297  			goto disconnect;
10298  		}
10299  		wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
10300  			       P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
10301  			       persistent->id);
10302  	disconnect:
10303  		wpa_s->p2p_last_4way_hs_fail = NULL;
10304  		/*
10305  		 * Remove the group from a timeout to avoid issues with caller
10306  		 * continuing to use the interface if this is on a P2P group
10307  		 * interface.
10308  		 */
10309  		eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
10310  				       wpa_s, NULL);
10311  		return 1;
10312  	}
10313  
10314  	wpa_s->p2p_last_4way_hs_fail = ssid;
10315  	return 0;
10316  }
10317  
10318  
10319  #ifdef CONFIG_WPS_NFC
10320  
wpas_p2p_nfc_handover(int ndef,struct wpabuf * wsc,struct wpabuf * p2p)10321  static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
10322  					     struct wpabuf *p2p)
10323  {
10324  	struct wpabuf *ret;
10325  	size_t wsc_len;
10326  
10327  	if (p2p == NULL) {
10328  		wpabuf_free(wsc);
10329  		wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
10330  		return NULL;
10331  	}
10332  
10333  	wsc_len = wsc ? wpabuf_len(wsc) : 0;
10334  	ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
10335  	if (ret == NULL) {
10336  		wpabuf_free(wsc);
10337  		wpabuf_free(p2p);
10338  		return NULL;
10339  	}
10340  
10341  	wpabuf_put_be16(ret, wsc_len);
10342  	if (wsc)
10343  		wpabuf_put_buf(ret, wsc);
10344  	wpabuf_put_be16(ret, wpabuf_len(p2p));
10345  	wpabuf_put_buf(ret, p2p);
10346  
10347  	wpabuf_free(wsc);
10348  	wpabuf_free(p2p);
10349  	wpa_hexdump_buf(MSG_DEBUG,
10350  			"P2P: Generated NFC connection handover message", ret);
10351  
10352  	if (ndef && ret) {
10353  		struct wpabuf *tmp;
10354  		tmp = ndef_build_p2p(ret);
10355  		wpabuf_free(ret);
10356  		if (tmp == NULL) {
10357  			wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
10358  			return NULL;
10359  		}
10360  		ret = tmp;
10361  	}
10362  
10363  	return ret;
10364  }
10365  
10366  
wpas_p2p_cli_freq(struct wpa_supplicant * wpa_s,struct wpa_ssid ** ssid,u8 * go_dev_addr)10367  static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
10368  			     struct wpa_ssid **ssid, u8 *go_dev_addr)
10369  {
10370  	struct wpa_supplicant *iface;
10371  
10372  	if (go_dev_addr)
10373  		os_memset(go_dev_addr, 0, ETH_ALEN);
10374  	if (ssid)
10375  		*ssid = NULL;
10376  	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
10377  		if (iface->wpa_state < WPA_ASSOCIATING ||
10378  		    iface->current_ssid == NULL || iface->assoc_freq == 0 ||
10379  		    !iface->current_ssid->p2p_group ||
10380  		    iface->current_ssid->mode != WPAS_MODE_INFRA)
10381  			continue;
10382  		if (ssid)
10383  			*ssid = iface->current_ssid;
10384  		if (go_dev_addr)
10385  			os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
10386  		return iface->assoc_freq;
10387  	}
10388  	return 0;
10389  }
10390  
10391  
wpas_p2p_nfc_handover_req(struct wpa_supplicant * wpa_s,int ndef)10392  struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
10393  					  int ndef)
10394  {
10395  	struct wpabuf *wsc, *p2p;
10396  	struct wpa_ssid *ssid;
10397  	u8 go_dev_addr[ETH_ALEN];
10398  	int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
10399  
10400  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
10401  		wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
10402  		return NULL;
10403  	}
10404  
10405  	if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
10406  	    wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
10407  			   &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
10408  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
10409  		return NULL;
10410  	}
10411  
10412  	wpa_s->p2p2 = false;
10413  
10414  	if (cli_freq == 0) {
10415  		wsc = wps_build_nfc_handover_req_p2p(
10416  			wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
10417  	} else
10418  		wsc = NULL;
10419  	p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
10420  					 go_dev_addr, ssid ? ssid->ssid : NULL,
10421  					 ssid ? ssid->ssid_len : 0);
10422  
10423  	return wpas_p2p_nfc_handover(ndef, wsc, p2p);
10424  }
10425  
10426  
wpas_p2p_nfc_handover_sel(struct wpa_supplicant * wpa_s,int ndef,int tag)10427  struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
10428  					  int ndef, int tag)
10429  {
10430  	struct wpabuf *wsc, *p2p;
10431  	struct wpa_ssid *ssid;
10432  	u8 go_dev_addr[ETH_ALEN];
10433  	int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
10434  
10435  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10436  		return NULL;
10437  
10438  	if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
10439  	    wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
10440  			   &wpa_s->conf->wps_nfc_dh_privkey) < 0)
10441  		return NULL;
10442  
10443  	wpa_s->p2p2 = false;
10444  
10445  	if (cli_freq == 0) {
10446  		wsc = wps_build_nfc_handover_sel_p2p(
10447  			wpa_s->parent->wps,
10448  			tag ? wpa_s->conf->wps_nfc_dev_pw_id :
10449  			DEV_PW_NFC_CONNECTION_HANDOVER,
10450  			wpa_s->conf->wps_nfc_dh_pubkey,
10451  			tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
10452  	} else
10453  		wsc = NULL;
10454  	p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
10455  					 go_dev_addr, ssid ? ssid->ssid : NULL,
10456  					 ssid ? ssid->ssid_len : 0);
10457  
10458  	return wpas_p2p_nfc_handover(ndef, wsc, p2p);
10459  }
10460  
10461  
wpas_p2p_nfc_join_group(struct wpa_supplicant * wpa_s,struct p2p_nfc_params * params)10462  static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
10463  				   struct p2p_nfc_params *params)
10464  {
10465  	wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
10466  		   "connection handover (freq=%d)",
10467  		   params->go_freq);
10468  
10469  	if (params->go_freq && params->go_ssid_len) {
10470  		wpa_s->p2p_wps_method = WPS_NFC;
10471  		wpa_s->pending_join_wps_method = WPS_NFC;
10472  		os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
10473  		os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
10474  			  ETH_ALEN);
10475  		return wpas_p2p_join_start(wpa_s, params->go_freq,
10476  					   params->go_ssid,
10477  					   params->go_ssid_len);
10478  	}
10479  
10480  	return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
10481  				WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
10482  				params->go_freq, wpa_s->p2p_go_vht_center_freq2,
10483  				-1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
10484  				wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
10485  				params->go_ssid_len ? params->go_ssid : NULL,
10486  				params->go_ssid_len, false, wpa_s->p2p2,
10487  				wpa_s->p2p_bootstrap, NULL, false);
10488  }
10489  
10490  
wpas_p2p_nfc_auth_join(struct wpa_supplicant * wpa_s,struct p2p_nfc_params * params,int tag)10491  static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
10492  				  struct p2p_nfc_params *params, int tag)
10493  {
10494  	int res, persistent;
10495  	struct wpa_ssid *ssid;
10496  
10497  	wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
10498  		   "connection handover");
10499  	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
10500  		ssid = wpa_s->current_ssid;
10501  		if (ssid == NULL)
10502  			continue;
10503  		if (ssid->mode != WPAS_MODE_P2P_GO)
10504  			continue;
10505  		if (wpa_s->ap_iface == NULL)
10506  			continue;
10507  		break;
10508  	}
10509  	if (wpa_s == NULL) {
10510  		wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
10511  		return -1;
10512  	}
10513  
10514  	if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
10515  	    DEV_PW_NFC_CONNECTION_HANDOVER &&
10516  	    !wpa_s->p2pdev->p2p_oob_dev_pw) {
10517  		wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
10518  		return -1;
10519  	}
10520  	res = wpas_ap_wps_add_nfc_pw(
10521  		wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
10522  		wpa_s->p2pdev->p2p_oob_dev_pw,
10523  		wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
10524  		wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
10525  	if (res)
10526  		return res;
10527  
10528  	if (!tag) {
10529  		wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
10530  		return 0;
10531  	}
10532  
10533  	if (!params->peer ||
10534  	    !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
10535  		return 0;
10536  
10537  	wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
10538  		   " to join", MAC2STR(params->peer->p2p_device_addr));
10539  
10540  	wpa_s->global->p2p_invite_group = wpa_s;
10541  	persistent = ssid->p2p_persistent_group &&
10542  		wpas_p2p_get_persistent(wpa_s->p2pdev,
10543  					params->peer->p2p_device_addr,
10544  					ssid->ssid, ssid->ssid_len);
10545  	wpa_s->p2pdev->pending_invite_ssid_id = -1;
10546  
10547  	return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
10548  			  P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
10549  			  ssid->ssid, ssid->ssid_len, ssid->frequency,
10550  			  wpa_s->global->p2p_dev_addr, persistent, 0,
10551  			  wpa_s->p2pdev->p2p_oob_dev_pw_id, false);
10552  }
10553  
10554  
wpas_p2p_nfc_init_go_neg(struct wpa_supplicant * wpa_s,struct p2p_nfc_params * params,int forced_freq)10555  static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
10556  				    struct p2p_nfc_params *params,
10557  				    int forced_freq)
10558  {
10559  	wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
10560  		   "connection handover");
10561  	return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
10562  				WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
10563  				forced_freq, wpa_s->p2p_go_vht_center_freq2,
10564  				-1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
10565  				wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
10566  				NULL, 0, false, wpa_s->p2p2,
10567  				wpa_s->p2p_bootstrap, NULL, false);
10568  }
10569  
10570  
wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant * wpa_s,struct p2p_nfc_params * params,int forced_freq)10571  static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
10572  				    struct p2p_nfc_params *params,
10573  				    int forced_freq)
10574  {
10575  	int res;
10576  
10577  	wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
10578  		   "connection handover");
10579  	res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
10580  			       WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
10581  			       forced_freq, wpa_s->p2p_go_vht_center_freq2,
10582  			       -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
10583  			       wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
10584  			       NULL, 0, false, wpa_s->p2p2,
10585  			       wpa_s->p2p_bootstrap, NULL, false);
10586  	if (res)
10587  		return res;
10588  
10589  	res = wpas_p2p_listen(wpa_s, 60);
10590  	if (res) {
10591  		p2p_unauthorize(wpa_s->global->p2p,
10592  				params->peer->p2p_device_addr);
10593  	}
10594  
10595  	return res;
10596  }
10597  
10598  
wpas_p2p_nfc_connection_handover(struct wpa_supplicant * wpa_s,const struct wpabuf * data,int sel,int tag,int forced_freq)10599  static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
10600  					    const struct wpabuf *data,
10601  					    int sel, int tag, int forced_freq)
10602  {
10603  	const u8 *pos, *end;
10604  	u16 len, id;
10605  	struct p2p_nfc_params params;
10606  	int res;
10607  
10608  	os_memset(&params, 0, sizeof(params));
10609  	params.sel = sel;
10610  
10611  	wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
10612  
10613  	pos = wpabuf_head(data);
10614  	end = pos + wpabuf_len(data);
10615  
10616  	if (end - pos < 2) {
10617  		wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
10618  			   "attributes");
10619  		return -1;
10620  	}
10621  	len = WPA_GET_BE16(pos);
10622  	pos += 2;
10623  	if (len > end - pos) {
10624  		wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
10625  			   "attributes");
10626  		return -1;
10627  	}
10628  	params.wsc_attr = pos;
10629  	params.wsc_len = len;
10630  	pos += len;
10631  
10632  	if (end - pos < 2) {
10633  		wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
10634  			   "attributes");
10635  		return -1;
10636  	}
10637  	len = WPA_GET_BE16(pos);
10638  	pos += 2;
10639  	if (len > end - pos) {
10640  		wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
10641  			   "attributes");
10642  		return -1;
10643  	}
10644  	params.p2p_attr = pos;
10645  	params.p2p_len = len;
10646  	pos += len;
10647  
10648  	wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
10649  		    params.wsc_attr, params.wsc_len);
10650  	wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
10651  		    params.p2p_attr, params.p2p_len);
10652  	if (pos < end) {
10653  		wpa_hexdump(MSG_DEBUG,
10654  			    "P2P: Ignored extra data after P2P attributes",
10655  			    pos, end - pos);
10656  	}
10657  
10658  	res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
10659  	if (res)
10660  		return res;
10661  
10662  	if (params.next_step == NO_ACTION)
10663  		return 0;
10664  
10665  	if (params.next_step == BOTH_GO) {
10666  		wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
10667  			MAC2STR(params.peer->p2p_device_addr));
10668  		return 0;
10669  	}
10670  
10671  	if (params.next_step == PEER_CLIENT) {
10672  		if (!is_zero_ether_addr(params.go_dev_addr)) {
10673  			wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
10674  				"peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
10675  				" ssid=\"%s\"",
10676  				MAC2STR(params.peer->p2p_device_addr),
10677  				params.go_freq,
10678  				MAC2STR(params.go_dev_addr),
10679  				wpa_ssid_txt(params.go_ssid,
10680  					     params.go_ssid_len));
10681  		} else {
10682  			wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
10683  				"peer=" MACSTR " freq=%d",
10684  				MAC2STR(params.peer->p2p_device_addr),
10685  				params.go_freq);
10686  		}
10687  		return 0;
10688  	}
10689  
10690  	if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
10691  		wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
10692  			MACSTR, MAC2STR(params.peer->p2p_device_addr));
10693  		return 0;
10694  	}
10695  
10696  	wpabuf_free(wpa_s->p2p_oob_dev_pw);
10697  	wpa_s->p2p_oob_dev_pw = NULL;
10698  
10699  	if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
10700  		wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
10701  			   "received");
10702  		return -1;
10703  	}
10704  
10705  	id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
10706  	wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
10707  	wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
10708  		    params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
10709  	os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
10710  		  params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
10711  	wpa_s->p2p_peer_oob_pk_hash_known = 1;
10712  
10713  	wpa_s->p2p2 = false;
10714  
10715  	if (tag) {
10716  		if (id < 0x10) {
10717  			wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
10718  				   "peer OOB Device Password Id %u", id);
10719  			return -1;
10720  		}
10721  		wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
10722  			   "Device Password Id %u", id);
10723  		wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
10724  				params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
10725  				params.oob_dev_pw_len -
10726  				WPS_OOB_PUBKEY_HASH_LEN - 2);
10727  		wpa_s->p2p_oob_dev_pw_id = id;
10728  		wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
10729  			params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
10730  			params.oob_dev_pw_len -
10731  			WPS_OOB_PUBKEY_HASH_LEN - 2);
10732  		if (wpa_s->p2p_oob_dev_pw == NULL)
10733  			return -1;
10734  
10735  		if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
10736  		    wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
10737  				   &wpa_s->conf->wps_nfc_dh_privkey) < 0)
10738  			return -1;
10739  	} else {
10740  		wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
10741  			   "without Device Password");
10742  		wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
10743  	}
10744  
10745  	switch (params.next_step) {
10746  	case NO_ACTION:
10747  	case BOTH_GO:
10748  	case PEER_CLIENT:
10749  		/* already covered above */
10750  		return 0;
10751  	case JOIN_GROUP:
10752  		return wpas_p2p_nfc_join_group(wpa_s, &params);
10753  	case AUTH_JOIN:
10754  		return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
10755  	case INIT_GO_NEG:
10756  		return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
10757  	case RESP_GO_NEG:
10758  		/* TODO: use own OOB Dev Pw */
10759  		return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
10760  	}
10761  
10762  	return -1;
10763  }
10764  
10765  
wpas_p2p_nfc_tag_process(struct wpa_supplicant * wpa_s,const struct wpabuf * data,int forced_freq)10766  int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
10767  			     const struct wpabuf *data, int forced_freq)
10768  {
10769  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10770  		return -1;
10771  
10772  	return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
10773  }
10774  
10775  
wpas_p2p_nfc_report_handover(struct wpa_supplicant * wpa_s,int init,const struct wpabuf * req,const struct wpabuf * sel,int forced_freq)10776  int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
10777  				 const struct wpabuf *req,
10778  				 const struct wpabuf *sel, int forced_freq)
10779  {
10780  	struct wpabuf *tmp;
10781  	int ret;
10782  
10783  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10784  		return -1;
10785  
10786  	wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
10787  
10788  	wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
10789  			  wpabuf_head(req), wpabuf_len(req));
10790  	wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
10791  			  wpabuf_head(sel), wpabuf_len(sel));
10792  	if (forced_freq)
10793  		wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
10794  	tmp = ndef_parse_p2p(init ? sel : req);
10795  	if (tmp == NULL) {
10796  		wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
10797  		return -1;
10798  	}
10799  
10800  	ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
10801  					       forced_freq);
10802  	wpabuf_free(tmp);
10803  
10804  	return ret;
10805  }
10806  
10807  
wpas_p2p_nfc_tag_enabled(struct wpa_supplicant * wpa_s,int enabled)10808  int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
10809  {
10810  	const u8 *if_addr;
10811  	int go_intent = wpa_s->conf->p2p_go_intent;
10812  	struct wpa_supplicant *iface;
10813  
10814  	if (wpa_s->global->p2p == NULL)
10815  		return -1;
10816  
10817  	if (!enabled) {
10818  		wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
10819  		for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
10820  		{
10821  			if (!iface->ap_iface)
10822  				continue;
10823  			hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
10824  		}
10825  		p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
10826  						 0, NULL);
10827  		if (wpa_s->p2p_nfc_tag_enabled)
10828  			wpas_p2p_remove_pending_group_interface(wpa_s);
10829  		wpa_s->p2p_nfc_tag_enabled = 0;
10830  		return 0;
10831  	}
10832  
10833  	if (wpa_s->global->p2p_disabled)
10834  		return -1;
10835  
10836  	if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
10837  	    wpa_s->conf->wps_nfc_dh_privkey == NULL ||
10838  	    wpa_s->conf->wps_nfc_dev_pw == NULL ||
10839  	    wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
10840  		wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
10841  			   "to allow static handover cases");
10842  		return -1;
10843  	}
10844  
10845  	wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
10846  
10847  	wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
10848  	wpabuf_free(wpa_s->p2p_oob_dev_pw);
10849  	wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
10850  	if (wpa_s->p2p_oob_dev_pw == NULL)
10851  		return -1;
10852  	wpa_s->p2p_peer_oob_pk_hash_known = 0;
10853  
10854  	if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
10855  	    wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
10856  		/*
10857  		 * P2P Group Interface present and the command came on group
10858  		 * interface, so enable the token for the current interface.
10859  		 */
10860  		wpa_s->create_p2p_iface = 0;
10861  	} else {
10862  		wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
10863  	}
10864  
10865  	if (wpa_s->create_p2p_iface) {
10866  		enum wpa_driver_if_type iftype;
10867  		/* Prepare to add a new interface for the group */
10868  		iftype = WPA_IF_P2P_GROUP;
10869  		if (go_intent == 15)
10870  			iftype = WPA_IF_P2P_GO;
10871  		if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
10872  			wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
10873  				   "interface for the group");
10874  			return -1;
10875  		}
10876  
10877  		if_addr = wpa_s->pending_interface_addr;
10878  	} else if (wpa_s->p2p_mgmt)
10879  		if_addr = wpa_s->parent->own_addr;
10880  	else
10881  		if_addr = wpa_s->own_addr;
10882  
10883  	wpa_s->p2p_nfc_tag_enabled = enabled;
10884  
10885  	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
10886  		struct hostapd_data *hapd;
10887  		if (iface->ap_iface == NULL)
10888  			continue;
10889  		hapd = iface->ap_iface->bss[0];
10890  		wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
10891  		hapd->conf->wps_nfc_dh_pubkey =
10892  			wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
10893  		wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
10894  		hapd->conf->wps_nfc_dh_privkey =
10895  			wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
10896  		wpabuf_free(hapd->conf->wps_nfc_dev_pw);
10897  		hapd->conf->wps_nfc_dev_pw =
10898  			wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
10899  		hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
10900  
10901  		if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
10902  			wpa_dbg(iface, MSG_DEBUG,
10903  				"P2P: Failed to enable NFC Tag for GO");
10904  		}
10905  	}
10906  	p2p_set_authorized_oob_dev_pw_id(
10907  		wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
10908  		if_addr);
10909  
10910  	return 0;
10911  }
10912  
10913  #endif /* CONFIG_WPS_NFC */
10914  
10915  
wpas_p2p_optimize_listen_channel(struct wpa_supplicant * wpa_s,struct wpa_used_freq_data * freqs,unsigned int num)10916  static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
10917  					     struct wpa_used_freq_data *freqs,
10918  					     unsigned int num)
10919  {
10920  	u8 curr_chan, cand, chan;
10921  	unsigned int i;
10922  
10923  	/*
10924  	 * If possible, optimize the Listen channel to be a channel that is
10925  	 * already used by one of the other interfaces.
10926  	 */
10927  	if (!wpa_s->conf->p2p_optimize_listen_chan)
10928  		return;
10929  
10930  	curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
10931  	for (i = 0, cand = 0; i < num; i++) {
10932  		ieee80211_freq_to_chan(freqs[i].freq, &chan);
10933  		if (curr_chan == chan) {
10934  			cand = 0;
10935  			break;
10936  		}
10937  
10938  		if (chan == 1 || chan == 6 || chan == 11)
10939  			cand = chan;
10940  	}
10941  
10942  	if (cand) {
10943  		wpa_dbg(wpa_s, MSG_DEBUG,
10944  			"P2P: Update Listen channel to %u based on operating channel",
10945  			cand);
10946  		p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
10947  	}
10948  }
10949  
10950  
wpas_p2p_move_go_csa(struct wpa_supplicant * wpa_s)10951  static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
10952  {
10953  	struct hostapd_config *conf;
10954  	struct p2p_go_neg_results params;
10955  	struct csa_settings csa_settings;
10956  	struct wpa_ssid *current_ssid = wpa_s->current_ssid;
10957  	int old_freq = current_ssid->frequency;
10958  	int ret;
10959  
10960  	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
10961  		wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
10962  		return -1;
10963  	}
10964  
10965  	/*
10966  	 * TODO: This function may not always work correctly. For example,
10967  	 * when we have a running GO and a BSS on a DFS channel.
10968  	 */
10969  	if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
10970  				    NULL)) {
10971  		wpa_dbg(wpa_s, MSG_DEBUG,
10972  			"P2P CSA: Failed to select new frequency for GO");
10973  		return -1;
10974  	}
10975  
10976  	if (current_ssid->frequency == params.freq) {
10977  		wpa_dbg(wpa_s, MSG_DEBUG,
10978  			"P2P CSA: Selected same frequency - not moving GO");
10979  		return 0;
10980  	}
10981  
10982  	conf = hostapd_config_defaults();
10983  	if (!conf) {
10984  		wpa_dbg(wpa_s, MSG_DEBUG,
10985  			"P2P CSA: Failed to allocate default config");
10986  		return -1;
10987  	}
10988  
10989  	current_ssid->frequency = params.freq;
10990  	if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
10991  		wpa_dbg(wpa_s, MSG_DEBUG,
10992  			"P2P CSA: Failed to create new GO config");
10993  		ret = -1;
10994  		goto out;
10995  	}
10996  
10997  	if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode &&
10998  	    (wpa_s->ap_iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A ||
10999  	     is_6ghz_freq(wpa_s->ap_iface->freq) ||
11000  	     conf->hw_mode != HOSTAPD_MODE_IEEE80211G)) {
11001  		wpa_dbg(wpa_s, MSG_INFO,
11002  			"P2P CSA: CSA from hardware mode %d%s to %d is not supported",
11003  			wpa_s->ap_iface->current_mode->mode,
11004  			is_6ghz_freq(wpa_s->ap_iface->freq) ? " (6 GHz)" : "",
11005  			conf->hw_mode);
11006  		ret = -1;
11007  		goto out;
11008  	}
11009  
11010  	os_memset(&csa_settings, 0, sizeof(csa_settings));
11011  	csa_settings.cs_count = P2P_GO_CSA_COUNT;
11012  	csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
11013  	csa_settings.link_id = -1;
11014  	csa_settings.freq_params.freq = params.freq;
11015  	csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
11016  	csa_settings.freq_params.ht_enabled = conf->ieee80211n;
11017  	csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
11018  
11019  	if (conf->ieee80211ac) {
11020  		int freq1 = 0, freq2 = 0;
11021  		u8 chan, opclass;
11022  
11023  		if (ieee80211_freq_to_channel_ext(params.freq,
11024  						  conf->secondary_channel,
11025  						  conf->vht_oper_chwidth,
11026  						  &opclass, &chan) ==
11027  		    NUM_HOSTAPD_MODES) {
11028  			wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
11029  			ret = -1;
11030  			goto out;
11031  		}
11032  
11033  		if (conf->vht_oper_centr_freq_seg0_idx)
11034  			freq1 = ieee80211_chan_to_freq(
11035  				NULL, opclass,
11036  				conf->vht_oper_centr_freq_seg0_idx);
11037  
11038  		if (conf->vht_oper_centr_freq_seg1_idx)
11039  			freq2 = ieee80211_chan_to_freq(
11040  				NULL, opclass,
11041  				conf->vht_oper_centr_freq_seg1_idx);
11042  
11043  		if (freq1 < 0 || freq2 < 0) {
11044  			wpa_dbg(wpa_s, MSG_DEBUG,
11045  				"P2P CSA: Selected invalid VHT center freqs");
11046  			ret = -1;
11047  			goto out;
11048  		}
11049  
11050  		csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
11051  		csa_settings.freq_params.center_freq1 = freq1;
11052  		csa_settings.freq_params.center_freq2 = freq2;
11053  
11054  		switch (conf->vht_oper_chwidth) {
11055  		case CONF_OPER_CHWIDTH_80MHZ:
11056  		case CONF_OPER_CHWIDTH_80P80MHZ:
11057  			csa_settings.freq_params.bandwidth = 80;
11058  			break;
11059  		case CONF_OPER_CHWIDTH_160MHZ:
11060  			csa_settings.freq_params.bandwidth = 160;
11061  			break;
11062  		default:
11063  			break;
11064  		}
11065  	}
11066  
11067  	ret = ap_switch_channel(wpa_s, &csa_settings);
11068  out:
11069  	current_ssid->frequency = old_freq;
11070  	hostapd_config_free(conf);
11071  	return ret;
11072  }
11073  
11074  
wpas_p2p_move_go_no_csa(struct wpa_supplicant * wpa_s)11075  static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
11076  {
11077  	struct p2p_go_neg_results params;
11078  	struct wpa_ssid *current_ssid = wpa_s->current_ssid;
11079  	void (*ap_configured_cb)(void *ctx, void *data);
11080  	void *ap_configured_cb_ctx, *ap_configured_cb_data;
11081  
11082  	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
11083  
11084  	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
11085  		current_ssid->frequency);
11086  
11087  	/* Stop the AP functionality */
11088  	/* TODO: Should do this in a way that does not indicated to possible
11089  	 * P2P Clients in the group that the group is terminated. */
11090  	/* If this action occurs before a group is started, the callback should
11091  	 * be preserved, or GROUP-STARTED event would be lost. If this action
11092  	 * occurs after a group is started, these pointers are all NULL and
11093  	 * harmless. */
11094  	ap_configured_cb = wpa_s->ap_configured_cb;
11095  	ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
11096  	ap_configured_cb_data = wpa_s->ap_configured_cb_data;
11097  	wpa_supplicant_ap_deinit(wpa_s);
11098  
11099  	/* Reselect the GO frequency */
11100  	if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
11101  				    NULL)) {
11102  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
11103  		wpas_p2p_group_delete(wpa_s,
11104  				      P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
11105  		return;
11106  	}
11107  	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
11108  		params.freq);
11109  
11110  	if (params.freq &&
11111  	    !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
11112  		wpa_printf(MSG_DEBUG,
11113  			   "P2P: Selected freq (%u MHz) is not valid for P2P",
11114  			   params.freq);
11115  		wpas_p2p_group_delete(wpa_s,
11116  				      P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
11117  		return;
11118  	}
11119  
11120  	/* Restore preserved callback parameters */
11121  	wpa_s->ap_configured_cb = ap_configured_cb;
11122  	wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
11123  	wpa_s->ap_configured_cb_data = ap_configured_cb_data;
11124  
11125  	/* Update the frequency */
11126  	current_ssid->frequency = params.freq;
11127  	wpa_s->connect_without_scan = current_ssid;
11128  	wpa_s->reassociate = 1;
11129  	wpa_s->disconnected = 0;
11130  	wpa_supplicant_req_scan(wpa_s, 0, 0);
11131  }
11132  
11133  
wpas_p2p_move_go(void * eloop_ctx,void * timeout_ctx)11134  static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
11135  {
11136  	struct wpa_supplicant *wpa_s = eloop_ctx;
11137  
11138  	if (!wpa_s->ap_iface || !wpa_s->current_ssid)
11139  		return;
11140  
11141  	wpas_p2p_go_update_common_freqs(wpa_s);
11142  
11143  	/* Do not move GO in the middle of a CSA */
11144  	if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
11145  		wpa_printf(MSG_DEBUG,
11146  			   "P2P: CSA is in progress - not moving GO");
11147  		return;
11148  	}
11149  
11150  	/*
11151  	 * First, try a channel switch flow. If it is not supported or fails,
11152  	 * take down the GO and bring it up again.
11153  	 */
11154  	if (wpas_p2p_move_go_csa(wpa_s) < 0)
11155  		wpas_p2p_move_go_no_csa(wpa_s);
11156  }
11157  
11158  
wpas_p2p_reconsider_moving_go(void * eloop_ctx,void * timeout_ctx)11159  static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
11160  {
11161  	struct wpa_supplicant *wpa_s = eloop_ctx;
11162  	struct wpa_used_freq_data *freqs = NULL;
11163  	unsigned int num = wpa_s->num_multichan_concurrent;
11164  
11165  	freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
11166  	if (!freqs)
11167  		return;
11168  
11169  	num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
11170  
11171  	/* Previous attempt to move a GO was not possible -- try again. */
11172  	wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
11173  				     WPAS_P2P_CHANNEL_UPDATE_ANY);
11174  
11175  	os_free(freqs);
11176  }
11177  
11178  
11179  /*
11180   * Consider moving a GO from its currently used frequency:
11181   * 1. It is possible that due to regulatory consideration the frequency
11182   *    can no longer be used and there is a need to evacuate the GO.
11183   * 2. It is possible that due to MCC considerations, it would be preferable
11184   *    to move the GO to a channel that is currently used by some other
11185   *    station interface.
11186   *
11187   * In case a frequency that became invalid is once again valid, cancel a
11188   * previously initiated GO frequency change.
11189   */
wpas_p2p_consider_moving_one_go(struct wpa_supplicant * wpa_s,struct wpa_used_freq_data * freqs,unsigned int num)11190  static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
11191  					    struct wpa_used_freq_data *freqs,
11192  					    unsigned int num)
11193  {
11194  	unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
11195  	unsigned int timeout;
11196  	int freq;
11197  	int dfs_offload;
11198  
11199  	wpas_p2p_go_update_common_freqs(wpa_s);
11200  
11201  	freq = wpa_s->current_ssid->frequency;
11202  	dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
11203  		ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
11204  	for (i = 0, invalid_freq = 0; i < num; i++) {
11205  		if (freqs[i].freq == freq) {
11206  			flags = freqs[i].flags;
11207  
11208  			/* The channel is invalid, must change it */
11209  			if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
11210  			    !dfs_offload) {
11211  				wpa_dbg(wpa_s, MSG_DEBUG,
11212  					"P2P: Freq=%d MHz no longer valid for GO",
11213  					freq);
11214  				invalid_freq = 1;
11215  			}
11216  		} else if (freqs[i].flags == 0) {
11217  			/* Freq is not used by any other station interface */
11218  			continue;
11219  		} else if (!p2p_supported_freq(wpa_s->global->p2p,
11220  					       freqs[i].freq) && !dfs_offload) {
11221  			/* Freq is not valid for P2P use cases */
11222  			continue;
11223  		} else if (wpa_s->conf->p2p_go_freq_change_policy ==
11224  			   P2P_GO_FREQ_MOVE_SCM) {
11225  			policy_move = 1;
11226  		} else if (wpa_s->conf->p2p_go_freq_change_policy ==
11227  			   P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
11228  			   wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
11229  			policy_move = 1;
11230  		} else if ((wpa_s->conf->p2p_go_freq_change_policy ==
11231  			    P2P_GO_FREQ_MOVE_SCM_ECSA) &&
11232  			   wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
11233  			if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
11234  				policy_move = 1;
11235  			} else if ((wpa_s->drv_flags &
11236  				    WPA_DRIVER_FLAGS_AP_CSA) &&
11237  				   wpas_p2p_go_clients_support_ecsa(wpa_s)) {
11238  				u8 chan;
11239  
11240  				/*
11241  				 * We do not support CSA between bands, so move
11242  				 * GO only within the same band.
11243  				 */
11244  				if (wpa_s->ap_iface->current_mode->mode ==
11245  				    ieee80211_freq_to_chan(freqs[i].freq,
11246  							   &chan))
11247  					policy_move = 1;
11248  			}
11249  		}
11250  	}
11251  
11252  	wpa_dbg(wpa_s, MSG_DEBUG,
11253  		"P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
11254  		invalid_freq, policy_move, flags);
11255  
11256  	/*
11257  	 * The channel is valid, or we are going to have a policy move, so
11258  	 * cancel timeout.
11259  	 */
11260  	if (!invalid_freq || policy_move) {
11261  		wpa_dbg(wpa_s, MSG_DEBUG,
11262  			"P2P: Cancel a GO move from freq=%d MHz", freq);
11263  		eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
11264  
11265  		if (wpas_p2p_in_progress(wpa_s)) {
11266  			wpa_dbg(wpa_s, MSG_DEBUG,
11267  				"P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
11268  			eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
11269  					     wpa_s, NULL);
11270  			eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
11271  					       wpas_p2p_reconsider_moving_go,
11272  					       wpa_s, NULL);
11273  			return;
11274  		}
11275  	}
11276  
11277  	if (!invalid_freq && (!policy_move || flags != 0)) {
11278  		wpa_dbg(wpa_s, MSG_DEBUG,
11279  			"P2P: Not initiating a GO frequency change");
11280  		return;
11281  	}
11282  
11283  	/*
11284  	 * Do not consider moving GO if it is in the middle of a CSA. When the
11285  	 * CSA is finished this flow should be retriggered.
11286  	 */
11287  	if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
11288  		wpa_dbg(wpa_s, MSG_DEBUG,
11289  			"P2P: Not initiating a GO frequency change - CSA is in progress");
11290  		return;
11291  	}
11292  
11293  	if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
11294  		timeout = P2P_GO_FREQ_CHANGE_TIME;
11295  	else
11296  		timeout = 0;
11297  
11298  	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
11299  		freq, timeout);
11300  	eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
11301  	eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
11302  }
11303  
11304  
wpas_p2p_consider_moving_gos(struct wpa_supplicant * wpa_s,struct wpa_used_freq_data * freqs,unsigned int num,enum wpas_p2p_channel_update_trig trig)11305  static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
11306  					 struct wpa_used_freq_data *freqs,
11307  					 unsigned int num,
11308  					 enum wpas_p2p_channel_update_trig trig)
11309  {
11310  	struct wpa_supplicant *ifs;
11311  
11312  	eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
11313  			     NULL);
11314  
11315  	/*
11316  	 * Travers all the radio interfaces, and for each GO interface, check
11317  	 * if there is a need to move the GO from the frequency it is using,
11318  	 * or in case the frequency is valid again, cancel the evacuation flow.
11319  	 */
11320  	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
11321  			 radio_list) {
11322  		if (ifs->current_ssid == NULL ||
11323  		    ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
11324  			continue;
11325  
11326  		/*
11327  		 * The GO was just started or completed channel switch, no need
11328  		 * to move it.
11329  		 */
11330  		if (wpa_s == ifs &&
11331  		    (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
11332  		     trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
11333  			wpa_dbg(wpa_s, MSG_DEBUG,
11334  				"P2P: GO move - schedule re-consideration");
11335  			eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
11336  					       wpas_p2p_reconsider_moving_go,
11337  					       wpa_s, NULL);
11338  			continue;
11339  		}
11340  
11341  		wpas_p2p_consider_moving_one_go(ifs, freqs, num);
11342  	}
11343  }
11344  
11345  
wpas_p2p_indicate_state_change(struct wpa_supplicant * wpa_s)11346  void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
11347  {
11348  	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
11349  		return;
11350  
11351  	wpas_p2p_update_channel_list(wpa_s,
11352  				     WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
11353  }
11354  
11355  
wpas_p2p_deinit_iface(struct wpa_supplicant * wpa_s)11356  void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
11357  {
11358  	if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
11359  		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
11360  			"the management interface is being removed");
11361  		wpas_p2p_deinit_global(wpa_s->global);
11362  	}
11363  }
11364  
11365  
wpas_p2p_ap_deinit(struct wpa_supplicant * wpa_s)11366  void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
11367  {
11368  	if (wpa_s->ap_iface->bss)
11369  		wpa_s->ap_iface->bss[0]->p2p_group = NULL;
11370  	wpas_p2p_group_deinit(wpa_s);
11371  }
11372  
11373  
wpas_p2p_lo_start(struct wpa_supplicant * wpa_s,unsigned int freq,unsigned int period,unsigned int interval,unsigned int count)11374  int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
11375  		      unsigned int period, unsigned int interval,
11376  		      unsigned int count)
11377  {
11378  	struct p2p_data *p2p = wpa_s->global->p2p;
11379  	u8 *device_types;
11380  	size_t dev_types_len;
11381  	struct wpabuf *buf;
11382  	int ret;
11383  
11384  	if (wpa_s->p2p_lo_started) {
11385  		wpa_dbg(wpa_s, MSG_DEBUG,
11386  			"P2P Listen offload is already started");
11387  		return 0;
11388  	}
11389  
11390  	if (wpa_s->global->p2p == NULL ||
11391  	    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
11392  		wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
11393  		return -1;
11394  	}
11395  
11396  	if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
11397  		wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
11398  			   freq);
11399  		return -1;
11400  	}
11401  
11402  	/* Get device type */
11403  	dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
11404  		WPS_DEV_TYPE_LEN;
11405  	device_types = os_malloc(dev_types_len);
11406  	if (!device_types)
11407  		return -1;
11408  	os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
11409  	os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
11410  		  wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
11411  
11412  	/* Get Probe Response IE(s) */
11413  	buf = p2p_build_probe_resp_template(p2p, freq);
11414  	if (!buf) {
11415  		os_free(device_types);
11416  		return -1;
11417  	}
11418  
11419  	ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
11420  				   device_types, dev_types_len,
11421  				   wpabuf_mhead_u8(buf), wpabuf_len(buf));
11422  	if (ret < 0)
11423  		wpa_dbg(wpa_s, MSG_DEBUG,
11424  			"P2P: Failed to start P2P listen offload");
11425  
11426  	os_free(device_types);
11427  	wpabuf_free(buf);
11428  
11429  	if (ret == 0) {
11430  		wpa_s->p2p_lo_started = 1;
11431  
11432  		/* Stop current P2P listen if any */
11433  		wpas_stop_listen(wpa_s);
11434  	}
11435  
11436  	return ret;
11437  }
11438  
11439  
wpas_p2p_lo_stop(struct wpa_supplicant * wpa_s)11440  int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
11441  {
11442  	int ret;
11443  
11444  	if (!wpa_s->p2p_lo_started)
11445  		return 0;
11446  
11447  	ret = wpa_drv_p2p_lo_stop(wpa_s);
11448  	if (ret < 0)
11449  		wpa_dbg(wpa_s, MSG_DEBUG,
11450  			"P2P: Failed to stop P2P listen offload");
11451  
11452  	wpa_s->p2p_lo_started = 0;
11453  	return ret;
11454  }
11455  
11456  
wpas_p2p_usd_elems(struct wpa_supplicant * wpa_s,const char * service_name)11457  struct wpabuf * wpas_p2p_usd_elems(struct wpa_supplicant *wpa_s,
11458  				   const char *service_name)
11459  {
11460  	struct p2p_data *p2p = wpa_s->global->p2p;
11461  
11462  	if (wpa_s->global->p2p_disabled || !p2p)
11463  		return NULL;
11464  	p2p_usd_service_hash(p2p, service_name);
11465  	return p2p_usd_elems(p2p);
11466  }
11467  
11468  
wpas_p2p_process_usd_elems(struct wpa_supplicant * wpa_s,const u8 * buf,u16 buf_len,const u8 * peer_addr,unsigned int freq)11469  void wpas_p2p_process_usd_elems(struct wpa_supplicant *wpa_s, const u8 *buf,
11470  				u16 buf_len, const u8 *peer_addr,
11471  				unsigned int freq)
11472  {
11473  	struct p2p_data *p2p = wpa_s->global->p2p;
11474  
11475  	if (wpa_s->global->p2p_disabled || !p2p)
11476  		return;
11477  	p2p_process_usd_elems(p2p, buf, buf_len, peer_addr, freq);
11478  }
11479  
11480  
11481  #ifdef CONFIG_PASN
11482  
wpas_p2p_pasn_auth_rx(struct wpa_supplicant * wpa_s,const struct ieee80211_mgmt * mgmt,size_t len,int freq)11483  int wpas_p2p_pasn_auth_rx(struct wpa_supplicant *wpa_s,
11484  			  const struct ieee80211_mgmt *mgmt, size_t len,
11485  			  int freq)
11486  {
11487  	struct p2p_data *p2p = wpa_s->global->p2p;
11488  
11489  	if (wpa_s->global->p2p_disabled || !p2p)
11490  		return -2;
11491  	return p2p_pasn_auth_rx(p2p, mgmt, len, freq);
11492  }
11493  
11494  
wpas_p2p_pasn_auth_tx_status(struct wpa_supplicant * wpa_s,const u8 * data,size_t data_len,bool acked)11495  int wpas_p2p_pasn_auth_tx_status(struct wpa_supplicant *wpa_s, const u8 *data,
11496  				 size_t data_len, bool acked)
11497  {
11498  	struct p2p_data *p2p = wpa_s->global->p2p;
11499  	struct wpa_p2p_pasn_auth_work *awork;
11500  
11501  	if (!wpa_s->p2p_pasn_auth_work)
11502  		return -1;
11503  	awork = wpa_s->p2p_pasn_auth_work->ctx;
11504  
11505  	return p2p_pasn_auth_tx_status(p2p, data, data_len, acked,
11506  				       awork->verify);
11507  }
11508  
11509  
11510  #ifdef CONFIG_TESTING_OPTIONS
wpas_p2p_get_pasn_ptk(struct wpa_supplicant * wpa_s,const u8 ** ptk,size_t * ptk_len)11511  int wpas_p2p_get_pasn_ptk(struct wpa_supplicant *wpa_s, const u8 **ptk,
11512  			  size_t *ptk_len)
11513  {
11514  	struct p2p_data *p2p = wpa_s->global->p2p;
11515  
11516  	if (wpa_s->global->p2p_disabled || !p2p)
11517  		return -2;
11518  	return p2p_pasn_get_ptk(p2p, ptk, ptk_len);
11519  }
11520  #endif /* CONFIG_TESTING_OPTIONS */
11521  
11522  #endif /* CONFIG_PASN */
11523  
11524  
wpas_p2p_get_dira(struct wpa_supplicant * wpa_s,char * buf,size_t buf_len)11525  int wpas_p2p_get_dira(struct wpa_supplicant *wpa_s, char *buf, size_t buf_len)
11526  {
11527  	struct p2p_data *p2p = wpa_s->global->p2p;
11528  
11529  	if (wpa_s->global->p2p_disabled || !p2p)
11530  		return 0;
11531  	return p2p_get_dira_info(p2p, buf, buf_len);
11532  }
11533  
11534  
wpas_p2p_validate_dira(struct wpa_supplicant * wpa_s,const u8 * addr,u8 cipher,const u8 * nonce,const u8 * tag)11535  int wpas_p2p_validate_dira(struct wpa_supplicant *wpa_s, const u8 *addr,
11536  			   u8 cipher, const u8 *nonce, const u8 *tag)
11537  {
11538  	if (cipher != DIRA_CIPHER_VERSION_128) {
11539  		wpa_printf(MSG_INFO, "P2P2: Unsupported DIRA cipher version %d",
11540  			   cipher);
11541  		return 0;
11542  	}
11543  
11544  	return wpas_validate_dira(wpa_s, addr, nonce, tag);
11545  }
11546  
11547  
wpas_p2p_update_dev_addr(struct wpa_supplicant * wpa_s)11548  void wpas_p2p_update_dev_addr(struct wpa_supplicant *wpa_s)
11549  {
11550  	os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
11551  	p2p_set_dev_addr(wpa_s->global->p2p, wpa_s->own_addr);
11552  }
11553