3 * Copyright (c) 2009-2010, Atheros Communications
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #include "common/ieee802_11_common.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/wpa_ctrl.h"
22 #include "wps/wps_i.h"
24 #include "ap/hostapd.h"
25 #include "ap/p2p_hostapd.h"
26 #include "wpa_supplicant_i.h"
29 #include "config_ssid.h"
35 #include "wps_supplicant.h"
36 #include "p2p_supplicant.h"
40 * How many times to try to scan to find the GO before giving up on join
43 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
46 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
47 static struct wpa_supplicant *
48 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
50 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
51 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
52 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
53 const u8 *dev_addr, enum p2p_wps_method wps_method);
54 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
55 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
56 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
57 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
60 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
61 struct wpa_scan_results *scan_res)
65 if (wpa_s->global->p2p_disabled)
68 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
71 for (i = 0; i < scan_res->num; i++) {
72 struct wpa_scan_res *bss = scan_res->res[i];
73 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
74 bss->freq, bss->level,
75 (const u8 *) (bss + 1),
80 p2p_scan_res_handled(wpa_s->global->p2p);
84 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq)
86 struct wpa_supplicant *wpa_s = ctx;
87 struct wpa_driver_scan_params params;
89 struct wpabuf *wps_ie, *ies;
90 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
92 os_memset(¶ms, 0, sizeof(params));
94 /* P2P Wildcard SSID */
96 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
97 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
99 wpa_s->wps->dev.p2p = 1;
100 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
105 ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
110 wpabuf_put_buf(ies, wps_ie);
113 p2p_scan_ie(wpa_s->global->p2p, ies);
115 params.extra_ies = wpabuf_head(ies);
116 params.extra_ies_len = wpabuf_len(ies);
119 case P2P_SCAN_SOCIAL:
120 params.freqs = social_channels;
124 case P2P_SCAN_SPECIFIC:
125 social_channels[0] = freq;
126 social_channels[1] = 0;
127 params.freqs = social_channels;
129 case P2P_SCAN_SOCIAL_PLUS_ONE:
130 social_channels[3] = freq;
131 params.freqs = social_channels;
135 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
136 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
137 ret = ieee80211_sta_req_scan(wpa_s, ¶ms);
139 ret = wpa_drv_scan(wpa_s, ¶ms);
147 #ifdef CONFIG_CLIENT_MLME
148 static void p2p_rx_action_mlme(void *ctx, const u8 *buf, size_t len, int freq)
150 struct wpa_supplicant *wpa_s = ctx;
151 const struct ieee80211_mgmt *mgmt;
154 if (wpa_s->global->p2p_disabled)
156 mgmt = (const struct ieee80211_mgmt *) buf;
157 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
160 p2p_rx_action(wpa_s->global->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
161 mgmt->u.action.category,
162 &mgmt->u.action.u.vs_public_action.action,
163 len - hdr_len, freq);
165 #endif /* CONFIG_CLIENT_MLME */
168 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
170 switch (p2p_group_interface) {
171 case P2P_GROUP_INTERFACE_PENDING:
172 return WPA_IF_P2P_GROUP;
173 case P2P_GROUP_INTERFACE_GO:
174 return WPA_IF_P2P_GO;
175 case P2P_GROUP_INTERFACE_CLIENT:
176 return WPA_IF_P2P_CLIENT;
179 return WPA_IF_P2P_GROUP;
183 static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
185 struct wpa_ssid *ssid;
189 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
191 ssid = wpa_s->current_ssid;
194 * The current SSID was not known, but there may still be a
195 * pending P2P group interface waiting for provisioning.
197 ssid = wpa_s->conf->ssid;
199 if (ssid->p2p_group &&
200 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
201 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
206 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
208 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
209 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
210 wpa_s->reassociate = 0;
211 wpa_s->disconnected = 1;
212 wpa_supplicant_deauthenticate(wpa_s,
213 WLAN_REASON_DEAUTH_LEAVING);
217 if (wpa_s->cross_connect_in_use) {
218 wpa_s->cross_connect_in_use = 0;
219 wpa_msg(wpa_s->parent, MSG_INFO,
220 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
221 wpa_s->ifname, wpa_s->cross_connect_uplink);
223 switch (wpa_s->removal_reason) {
224 case P2P_GROUP_REMOVAL_REQUESTED:
225 reason = " reason=REQUESTED";
227 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
228 reason = " reason=IDLE";
230 case P2P_GROUP_REMOVAL_UNAVAILABLE:
231 reason = " reason=UNAVAILABLE";
237 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s%s",
238 wpa_s->ifname, gtype, reason);
239 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
240 struct wpa_global *global;
242 enum wpa_driver_if_type type;
243 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
245 global = wpa_s->global;
246 ifname = os_strdup(wpa_s->ifname);
247 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
248 wpa_supplicant_remove_iface(wpa_s->global, wpa_s);
249 wpa_s = global->ifaces;
251 wpa_drv_if_remove(wpa_s, type, ifname);
256 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
257 if (ssid && (ssid->p2p_group ||
258 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
259 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
261 if (ssid == wpa_s->current_ssid)
262 wpa_s->current_ssid = NULL;
263 wpas_notify_network_removed(wpa_s, ssid);
264 wpa_config_remove_network(wpa_s->conf, id);
265 wpa_supplicant_clear_status(wpa_s);
267 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
270 wpa_supplicant_ap_deinit(wpa_s);
274 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
276 const u8 *ssid, size_t ssid_len)
284 if (wpa_s->go_params)
285 bssid = wpa_s->go_params->peer_interface_addr;
287 bssid = wpa_s->bssid;
289 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
291 u8 iface_addr[ETH_ALEN];
292 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
294 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
297 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
298 "group is persistent - BSS " MACSTR " not found",
303 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
305 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
306 "group is persistent - BSS " MACSTR
307 " did not include P2P IE", MAC2STR(bssid));
308 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
309 (u8 *) (bss + 1), bss->ie_len);
310 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
311 ((u8 *) bss + 1) + bss->ie_len,
316 group_capab = p2p_get_group_capab(p2p);
317 addr = p2p_get_go_dev_addr(p2p);
318 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
319 "group_capab=0x%x", group_capab);
321 os_memcpy(go_dev_addr, addr, ETH_ALEN);
322 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
325 os_memset(go_dev_addr, 0, ETH_ALEN);
328 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
329 "go_dev_addr=" MACSTR,
330 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
332 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
336 static void wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
337 struct wpa_ssid *ssid,
338 const u8 *go_dev_addr)
343 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
344 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
345 for (s = wpa_s->conf->ssid; s; s = s->next) {
346 if (s->disabled == 2 &&
347 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
348 s->ssid_len == ssid->ssid_len &&
349 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
354 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
356 if (ssid->passphrase && !s->passphrase)
358 else if (ssid->passphrase && s->passphrase &&
359 os_strcmp(ssid->passphrase, s->passphrase) != 0)
362 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
365 s = wpa_config_add_network(wpa_s->conf);
368 wpa_config_set_network_defaults(s);
372 s->p2p_persistent_group = 1;
375 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
376 s->mode = ssid->mode;
377 s->auth_alg = WPA_AUTH_ALG_OPEN;
378 s->key_mgmt = WPA_KEY_MGMT_PSK;
379 s->proto = WPA_PROTO_RSN;
380 s->pairwise_cipher = WPA_CIPHER_CCMP;
381 if (ssid->passphrase) {
382 os_free(s->passphrase);
383 s->passphrase = os_strdup(ssid->passphrase);
387 os_memcpy(s->psk, ssid->psk, 32);
389 if (s->passphrase && !s->psk_set)
390 wpa_config_update_psk(s);
391 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
393 s->ssid = os_malloc(ssid->ssid_len);
396 s->ssid_len = ssid->ssid_len;
397 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
400 #ifndef CONFIG_NO_CONFIG_WRITE
401 if (changed && wpa_s->conf->update_config &&
402 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
403 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
405 #endif /* CONFIG_NO_CONFIG_WRITE */
409 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
412 struct wpa_ssid *ssid;
413 const char *ssid_txt;
416 u8 go_dev_addr[ETH_ALEN];
419 * This callback is likely called for the main interface. Update wpa_s
420 * to use the group interface if a new interface was created for the
423 if (wpa_s->global->p2p_group_formation)
424 wpa_s = wpa_s->global->p2p_group_formation;
425 wpa_s->p2p_in_provisioning = 0;
428 wpa_msg(wpa_s->parent, MSG_INFO,
429 P2P_EVENT_GROUP_FORMATION_FAILURE);
430 wpas_p2p_group_delete(wpa_s);
434 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
436 ssid = wpa_s->current_ssid;
437 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
438 ssid->mode = WPAS_MODE_P2P_GO;
439 p2p_group_notif_formation_done(wpa_s->p2p_group);
440 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
445 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
446 client = ssid->mode == WPAS_MODE_INFRA;
447 if (ssid->mode == WPAS_MODE_P2P_GO) {
448 persistent = ssid->p2p_persistent_group;
449 os_memcpy(go_dev_addr, wpa_s->parent->own_addr,
452 persistent = wpas_p2p_persistent_group(wpa_s,
458 client = wpa_s->p2p_group_interface ==
459 P2P_GROUP_INTERFACE_CLIENT;
462 wpa_s->show_group_started = 0;
465 * Indicate event only after successfully completed 4-way
466 * handshake, i.e., when the interface is ready for data
469 wpa_s->show_group_started = 1;
470 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
472 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
473 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
474 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
476 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
477 MAC2STR(go_dev_addr),
478 persistent ? " [PERSISTENT]" : "");
479 wpas_p2p_cross_connect_setup(wpa_s);
480 wpas_p2p_set_group_idle_timeout(wpa_s);
482 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
483 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
484 "go_dev_addr=" MACSTR "%s",
485 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
486 ssid && ssid->passphrase ? ssid->passphrase : "",
487 MAC2STR(go_dev_addr),
488 persistent ? " [PERSISTENT]" : "");
489 wpas_p2p_cross_connect_setup(wpa_s);
490 wpas_p2p_set_group_idle_timeout(wpa_s);
494 wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
499 static struct wpa_supplicant *
500 wpas_get_tx_interface(struct wpa_supplicant *wpa_s, const u8 *src)
502 struct wpa_supplicant *iface;
504 if (os_memcmp(src, wpa_s->own_addr, ETH_ALEN) == 0)
508 * Try to find a group interface that matches with the source address.
510 iface = wpa_s->global->ifaces;
512 if (os_memcmp(wpa_s->pending_action_src,
513 iface->own_addr, ETH_ALEN) == 0)
518 wpa_printf(MSG_DEBUG, "P2P: Use group interface %s "
519 "instead of interface %s for Action TX",
520 iface->ifname, wpa_s->ifname);
528 static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
530 struct wpa_supplicant *wpa_s = eloop_ctx;
531 struct wpa_supplicant *iface;
535 without_roc = wpa_s->pending_action_without_roc;
536 wpa_s->pending_action_without_roc = 0;
537 wpa_printf(MSG_DEBUG, "P2P: Send Action callback (without_roc=%d "
538 "pending_action_tx=%p)",
539 without_roc, wpa_s->pending_action_tx);
541 if (wpa_s->pending_action_tx == NULL)
545 * This call is likely going to be on the P2P device instance if the
546 * driver uses a separate interface for that purpose. However, some
547 * Action frames are actually sent within a P2P Group and when that is
548 * the case, we need to follow power saving (e.g., GO buffering the
549 * frame for a client in PS mode or a client following the advertised
550 * NoA from its GO). To make that easier for the driver, select the
551 * correct group interface here.
553 iface = wpas_get_tx_interface(wpa_s, wpa_s->pending_action_src);
555 if (wpa_s->off_channel_freq != wpa_s->pending_action_freq &&
556 wpa_s->pending_action_freq != 0 &&
557 wpa_s->pending_action_freq != iface->assoc_freq) {
558 wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX "
559 "waiting for another freq=%u (off_channel_freq=%u "
561 wpa_s->pending_action_freq,
562 wpa_s->off_channel_freq,
564 if (without_roc && wpa_s->off_channel_freq == 0) {
566 * We may get here if wpas_send_action() found us to be
567 * on the correct channel, but remain-on-channel cancel
568 * event was received before getting here.
570 wpa_printf(MSG_DEBUG, "P2P: Schedule "
571 "remain-on-channel to send Action frame");
572 if (wpa_drv_remain_on_channel(
573 wpa_s, wpa_s->pending_action_freq, 200) <
575 wpa_printf(MSG_DEBUG, "P2P: Failed to request "
576 "driver to remain on channel (%u "
577 "MHz) for Action Frame TX",
578 wpa_s->pending_action_freq);
580 wpa_s->off_channel_freq = 0;
581 wpa_s->roc_waiting_drv_freq =
582 wpa_s->pending_action_freq;
588 wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to "
589 MACSTR " using interface %s",
590 MAC2STR(wpa_s->pending_action_dst), iface->ifname);
591 res = wpa_drv_send_action(iface, wpa_s->pending_action_freq,
592 wpa_s->pending_action_dst,
593 wpa_s->pending_action_src,
594 wpa_s->pending_action_bssid,
595 wpabuf_head(wpa_s->pending_action_tx),
596 wpabuf_len(wpa_s->pending_action_tx));
598 wpa_printf(MSG_DEBUG, "P2P: Failed to send the pending "
601 * Use fake TX status event to allow P2P state machine to
604 wpas_send_action_tx_status(
605 wpa_s, wpa_s->pending_action_dst,
606 wpabuf_head(wpa_s->pending_action_tx),
607 wpabuf_len(wpa_s->pending_action_tx),
608 P2P_SEND_ACTION_FAILED);
613 void wpas_send_action_tx_status(struct wpa_supplicant *wpa_s, const u8 *dst,
614 const u8 *data, size_t data_len,
615 enum p2p_send_action_result result)
617 if (wpa_s->global->p2p_disabled)
620 if (wpa_s->pending_action_tx == NULL) {
621 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - no "
622 "pending operation");
626 if (os_memcmp(dst, wpa_s->pending_action_dst, ETH_ALEN) != 0) {
627 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - unknown "
628 "destination address");
632 wpabuf_free(wpa_s->pending_action_tx);
633 wpa_s->pending_action_tx = NULL;
635 p2p_send_action_cb(wpa_s->global->p2p, wpa_s->pending_action_freq,
636 wpa_s->pending_action_dst,
637 wpa_s->pending_action_src,
638 wpa_s->pending_action_bssid,
641 if (wpa_s->pending_pd_before_join &&
642 (os_memcmp(wpa_s->pending_action_dst, wpa_s->pending_join_dev_addr,
644 os_memcmp(wpa_s->pending_action_dst,
645 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
646 wpa_s->pending_pd_before_join = 0;
647 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
648 "join-existing-group operation");
649 wpas_p2p_join_start(wpa_s);
654 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
655 const u8 *src, const u8 *bssid, const u8 *buf,
656 size_t len, unsigned int wait_time)
658 struct wpa_supplicant *wpa_s = ctx;
660 wpa_printf(MSG_DEBUG, "P2P: Send action frame: freq=%d dst=" MACSTR
661 " src=" MACSTR " bssid=" MACSTR " len=%d",
662 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
665 if (wpa_s->pending_action_tx) {
666 wpa_printf(MSG_DEBUG, "P2P: Dropped pending Action frame TX "
667 "to " MACSTR, MAC2STR(wpa_s->pending_action_dst));
668 wpabuf_free(wpa_s->pending_action_tx);
670 wpa_s->pending_action_tx = wpabuf_alloc(len);
671 if (wpa_s->pending_action_tx == NULL) {
672 wpa_printf(MSG_DEBUG, "P2P: Failed to allocate Action frame "
673 "TX buffer (len=%llu)", (unsigned long long) len);
676 wpabuf_put_data(wpa_s->pending_action_tx, buf, len);
677 os_memcpy(wpa_s->pending_action_src, src, ETH_ALEN);
678 os_memcpy(wpa_s->pending_action_dst, dst, ETH_ALEN);
679 os_memcpy(wpa_s->pending_action_bssid, bssid, ETH_ALEN);
680 wpa_s->pending_action_freq = freq;
683 struct wpa_supplicant *tx_iface;
684 tx_iface = wpas_get_tx_interface(wpa_s, src);
685 if (tx_iface->assoc_freq == freq) {
686 wpa_printf(MSG_DEBUG, "P2P: Already on requested "
687 "channel (TX interface operating channel)");
692 if (wpa_s->off_channel_freq == freq || freq == 0) {
693 wpa_printf(MSG_DEBUG, "P2P: Already on requested channel; "
694 "send Action frame immediately");
695 /* TODO: Would there ever be need to extend the current
696 * duration on the channel? */
697 wpa_s->pending_action_without_roc = 1;
698 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
699 eloop_register_timeout(0, 0, wpas_send_action_cb, wpa_s, NULL);
702 wpa_s->pending_action_without_roc = 0;
704 if (wpa_s->roc_waiting_drv_freq == freq) {
705 wpa_printf(MSG_DEBUG, "P2P: Already waiting for driver to get "
706 "to frequency %u MHz; continue waiting to send the "
707 "Action frame", freq);
711 wpa_printf(MSG_DEBUG, "P2P: Schedule Action frame to be transmitted "
712 "once the driver gets to the requested channel");
713 if (wait_time > wpa_s->max_remain_on_chan)
714 wait_time = wpa_s->max_remain_on_chan;
715 if (wpa_drv_remain_on_channel(wpa_s, freq, wait_time) < 0) {
716 wpa_printf(MSG_DEBUG, "P2P: Failed to request driver "
717 "to remain on channel (%u MHz) for Action "
721 wpa_s->off_channel_freq = 0;
722 wpa_s->roc_waiting_drv_freq = freq;
728 static void wpas_send_action_done(void *ctx)
730 struct wpa_supplicant *wpa_s = ctx;
731 wpa_printf(MSG_DEBUG, "P2P: Action frame sequence done notification");
732 wpabuf_free(wpa_s->pending_action_tx);
733 wpa_s->pending_action_tx = NULL;
734 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
735 wpa_drv_cancel_remain_on_channel(wpa_s);
736 wpa_s->off_channel_freq = 0;
737 wpa_s->roc_waiting_drv_freq = 0;
742 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
743 struct p2p_go_neg_results *params)
745 if (wpa_s->go_params == NULL) {
746 wpa_s->go_params = os_malloc(sizeof(*params));
747 if (wpa_s->go_params == NULL)
750 os_memcpy(wpa_s->go_params, params, sizeof(*params));
755 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
756 struct p2p_go_neg_results *res)
758 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
759 MAC2STR(res->peer_interface_addr));
760 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
761 res->ssid, res->ssid_len);
762 wpa_supplicant_ap_deinit(wpa_s);
763 wpas_copy_go_neg_results(wpa_s, res);
764 if (res->wps_method == WPS_PBC)
765 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
767 u16 dev_pw_id = DEV_PW_DEFAULT;
768 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
769 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
770 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
771 wpa_s->p2p_pin, 1, dev_pw_id);
776 static void p2p_go_configured(void *ctx, void *data)
778 struct wpa_supplicant *wpa_s = ctx;
779 struct p2p_go_neg_results *params = data;
780 struct wpa_ssid *ssid;
782 ssid = wpa_s->current_ssid;
783 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
784 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
785 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
786 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
787 "go_dev_addr=" MACSTR "%s",
789 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
791 params->passphrase ? params->passphrase : "",
792 MAC2STR(wpa_s->parent->own_addr),
793 params->persistent_group ? " [PERSISTENT]" : "");
794 if (params->persistent_group)
795 wpas_p2p_store_persistent_group(
797 wpa_s->parent->own_addr);
798 wpas_p2p_cross_connect_setup(wpa_s);
799 wpas_p2p_set_group_idle_timeout(wpa_s);
803 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
804 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
805 params->peer_interface_addr)) {
806 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
810 if (params->wps_method == WPS_PBC)
811 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr);
812 else if (wpa_s->p2p_pin[0])
813 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
814 wpa_s->p2p_pin, NULL, 0);
815 os_free(wpa_s->go_params);
816 wpa_s->go_params = NULL;
820 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
821 struct p2p_go_neg_results *params,
824 struct wpa_ssid *ssid;
826 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
829 ssid = wpa_config_add_network(wpa_s->conf);
833 wpas_notify_network_added(wpa_s, ssid);
834 wpa_config_set_network_defaults(ssid);
837 ssid->p2p_persistent_group = params->persistent_group;
838 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
840 ssid->frequency = params->freq;
841 ssid->ssid = os_zalloc(params->ssid_len + 1);
843 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
844 ssid->ssid_len = params->ssid_len;
846 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
847 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
848 ssid->proto = WPA_PROTO_RSN;
849 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
850 ssid->passphrase = os_strdup(params->passphrase);
852 wpa_s->ap_configured_cb = p2p_go_configured;
853 wpa_s->ap_configured_cb_ctx = wpa_s;
854 wpa_s->ap_configured_cb_data = wpa_s->go_params;
855 wpa_s->connect_without_scan = 1;
856 wpa_s->reassociate = 1;
857 wpa_s->disconnected = 0;
858 wpa_supplicant_req_scan(wpa_s, 0, 0);
862 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
863 const struct wpa_supplicant *src)
865 struct wpa_config *d;
866 const struct wpa_config *s;
871 #define C(n) if (s->n) d->n = os_strdup(s->n)
881 d->p2p_group_idle = s->p2p_group_idle;
882 d->p2p_intra_bss = s->p2p_intra_bss;
886 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
887 enum wpa_driver_if_type type)
889 char ifname[120], force_ifname[120];
891 if (wpa_s->pending_interface_name[0]) {
892 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
893 "- skip creation of a new one");
894 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
895 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
896 "unknown?! ifname='%s'",
897 wpa_s->pending_interface_name);
903 os_snprintf(ifname, sizeof(ifname), "%s-p2p-%d", wpa_s->ifname,
904 wpa_s->p2p_group_idx);
905 force_ifname[0] = '\0';
907 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
909 wpa_s->p2p_group_idx++;
911 wpa_s->pending_interface_type = type;
912 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
913 wpa_s->pending_interface_addr) < 0) {
914 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
919 if (force_ifname[0]) {
920 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
922 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
923 sizeof(wpa_s->pending_interface_name));
925 os_strlcpy(wpa_s->pending_interface_name, ifname,
926 sizeof(wpa_s->pending_interface_name));
927 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
928 MACSTR, wpa_s->pending_interface_name,
929 MAC2STR(wpa_s->pending_interface_addr));
935 static void wpas_p2p_remove_pending_group_interface(
936 struct wpa_supplicant *wpa_s)
938 if (!wpa_s->pending_interface_name[0] ||
939 is_zero_ether_addr(wpa_s->pending_interface_addr))
940 return; /* No pending virtual interface */
942 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
943 wpa_s->pending_interface_name);
944 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
945 wpa_s->pending_interface_name);
946 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
947 wpa_s->pending_interface_name[0] = '\0';
951 static struct wpa_supplicant *
952 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
954 struct wpa_interface iface;
955 struct wpa_supplicant *group_wpa_s;
957 if (!wpa_s->pending_interface_name[0]) {
958 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
959 if (!wpas_p2p_create_iface(wpa_s))
962 * Something has forced us to remove the pending interface; try
963 * to create a new one and hope for the best that we will get
964 * the same local address.
966 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
967 WPA_IF_P2P_CLIENT) < 0)
971 os_memset(&iface, 0, sizeof(iface));
972 iface.ifname = wpa_s->pending_interface_name;
973 iface.driver = wpa_s->driver->name;
974 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
975 iface.driver_param = wpa_s->conf->driver_param;
976 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
977 if (group_wpa_s == NULL) {
978 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
979 "wpa_supplicant interface");
982 wpa_s->pending_interface_name[0] = '\0';
983 group_wpa_s->parent = wpa_s;
984 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
985 P2P_GROUP_INTERFACE_CLIENT;
986 wpa_s->global->p2p_group_formation = group_wpa_s;
988 wpas_p2p_clone_config(group_wpa_s, wpa_s);
994 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
997 struct wpa_supplicant *wpa_s = eloop_ctx;
998 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
999 if (wpa_s->global->p2p)
1000 p2p_group_formation_failed(wpa_s->global->p2p);
1001 wpas_group_formation_completed(wpa_s, 0);
1005 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1007 struct wpa_supplicant *wpa_s = ctx;
1009 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1010 wpa_drv_cancel_remain_on_channel(wpa_s);
1011 wpa_s->off_channel_freq = 0;
1012 wpa_s->roc_waiting_drv_freq = 0;
1016 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1018 wpas_p2p_remove_pending_group_interface(wpa_s);
1022 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1024 if (wpa_s->create_p2p_iface) {
1025 struct wpa_supplicant *group_wpa_s =
1026 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1027 if (group_wpa_s == NULL) {
1028 wpas_p2p_remove_pending_group_interface(wpa_s);
1031 if (group_wpa_s != wpa_s) {
1032 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1033 sizeof(group_wpa_s->p2p_pin));
1034 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1036 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1037 wpa_s->pending_interface_name[0] = '\0';
1038 group_wpa_s->p2p_in_provisioning = 1;
1041 wpas_start_wps_go(group_wpa_s, res, 1);
1043 wpas_start_wps_enrollee(group_wpa_s, res);
1045 wpa_s->p2p_in_provisioning = 1;
1046 wpa_s->global->p2p_group_formation = wpa_s;
1049 wpas_start_wps_go(wpa_s, res, 1);
1051 wpas_start_wps_enrollee(ctx, res);
1054 wpa_s->p2p_long_listen = 0;
1055 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1057 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1058 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1059 (res->peer_config_timeout % 100) * 10000,
1060 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1064 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1066 struct wpa_supplicant *wpa_s = ctx;
1067 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1068 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1072 void wpas_dev_found(void *ctx, const u8 *addr, const u8 *dev_addr,
1073 const u8 *pri_dev_type, const char *dev_name,
1074 u16 config_methods, u8 dev_capab, u8 group_capab)
1076 struct wpa_supplicant *wpa_s = ctx;
1077 char devtype[WPS_DEV_TYPE_BUFSIZE];
1078 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1079 " p2p_dev_addr=" MACSTR
1080 " pri_dev_type=%s name='%s' config_methods=0x%x "
1081 "dev_capab=0x%x group_capab=0x%x",
1082 MAC2STR(addr), MAC2STR(dev_addr),
1083 wps_dev_type_bin2str(pri_dev_type, devtype, sizeof(devtype)),
1084 dev_name, config_methods, dev_capab, group_capab);
1088 static int wpas_start_listen(void *ctx, unsigned int freq,
1089 unsigned int duration,
1090 const struct wpabuf *probe_resp_ie)
1092 struct wpa_supplicant *wpa_s = ctx;
1094 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1096 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1097 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1098 "report received Probe Request frames");
1102 wpa_s->pending_listen_freq = freq;
1103 wpa_s->pending_listen_duration = duration;
1105 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1106 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1107 "to remain on channel (%u MHz) for Listen "
1109 wpa_s->pending_listen_freq = 0;
1112 wpa_s->off_channel_freq = 0;
1113 wpa_s->roc_waiting_drv_freq = freq;
1119 static void wpas_stop_listen(void *ctx)
1121 struct wpa_supplicant *wpa_s = ctx;
1122 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1123 wpa_drv_cancel_remain_on_channel(wpa_s);
1124 wpa_s->off_channel_freq = 0;
1125 wpa_s->roc_waiting_drv_freq = 0;
1127 wpa_drv_probe_req_report(wpa_s, 0);
1131 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1133 struct wpa_supplicant *wpa_s = ctx;
1134 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
1138 static struct p2p_srv_bonjour *
1139 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1140 const struct wpabuf *query)
1142 struct p2p_srv_bonjour *bsrv;
1145 len = wpabuf_len(query);
1146 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1147 struct p2p_srv_bonjour, list) {
1148 if (len == wpabuf_len(bsrv->query) &&
1149 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1157 static struct p2p_srv_upnp *
1158 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1159 const char *service)
1161 struct p2p_srv_upnp *usrv;
1163 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1164 struct p2p_srv_upnp, list) {
1165 if (version == usrv->version &&
1166 os_strcmp(service, usrv->service) == 0)
1173 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1178 if (wpabuf_tailroom(resp) < 5)
1181 /* Length (to be filled) */
1182 len_pos = wpabuf_put(resp, 2);
1183 wpabuf_put_u8(resp, srv_proto);
1184 wpabuf_put_u8(resp, srv_trans_id);
1186 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1187 /* Response Data: empty */
1188 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1192 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1193 struct wpabuf *resp, u8 srv_trans_id)
1195 struct p2p_srv_bonjour *bsrv;
1198 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1200 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1201 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1205 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1206 struct p2p_srv_bonjour, list) {
1207 if (wpabuf_tailroom(resp) <
1208 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1210 /* Length (to be filled) */
1211 len_pos = wpabuf_put(resp, 2);
1212 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1213 wpabuf_put_u8(resp, srv_trans_id);
1215 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1216 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1217 wpabuf_head(bsrv->resp),
1218 wpabuf_len(bsrv->resp));
1220 wpabuf_put_buf(resp, bsrv->query); /* Key */
1221 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1222 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1228 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1229 struct wpabuf *resp, u8 srv_trans_id,
1230 const u8 *query, size_t query_len)
1232 struct p2p_srv_bonjour *bsrv;
1236 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1238 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1239 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1240 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1245 if (query_len == 0) {
1246 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1250 if (wpabuf_tailroom(resp) < 5)
1252 /* Length (to be filled) */
1253 len_pos = wpabuf_put(resp, 2);
1254 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1255 wpabuf_put_u8(resp, srv_trans_id);
1257 wpabuf_set(&buf, query, query_len);
1258 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1260 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1264 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1265 /* Response Data: empty */
1266 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1272 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1273 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1274 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1276 if (wpabuf_tailroom(resp) >=
1277 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1279 wpabuf_put_buf(resp, bsrv->query); /* Key */
1280 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1282 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1286 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1287 struct wpabuf *resp, u8 srv_trans_id)
1289 struct p2p_srv_upnp *usrv;
1292 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1294 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1295 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1299 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1300 struct p2p_srv_upnp, list) {
1301 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1304 /* Length (to be filled) */
1305 len_pos = wpabuf_put(resp, 2);
1306 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1307 wpabuf_put_u8(resp, srv_trans_id);
1310 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1312 wpabuf_put_u8(resp, usrv->version);
1313 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1315 wpabuf_put_str(resp, usrv->service);
1316 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1322 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1323 struct wpabuf *resp, u8 srv_trans_id,
1324 const u8 *query, size_t query_len)
1326 struct p2p_srv_upnp *usrv;
1332 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1335 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1336 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1337 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1342 if (query_len == 0) {
1343 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1347 if (wpabuf_tailroom(resp) < 5)
1350 /* Length (to be filled) */
1351 len_pos = wpabuf_put(resp, 2);
1352 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1353 wpabuf_put_u8(resp, srv_trans_id);
1356 str = os_malloc(query_len);
1359 os_memcpy(str, query + 1, query_len - 1);
1360 str[query_len - 1] = '\0';
1362 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1363 struct p2p_srv_upnp, list) {
1364 if (version != usrv->version)
1367 if (os_strcmp(str, "ssdp:all") != 0 &&
1368 os_strstr(usrv->service, str) == NULL)
1371 if (wpabuf_tailroom(resp) < 2)
1375 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1377 wpabuf_put_u8(resp, version);
1379 wpabuf_put_u8(resp, ',');
1383 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1385 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1387 wpabuf_put_str(resp, usrv->service);
1391 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1394 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1395 /* Response Data: empty */
1398 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1402 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1403 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1405 struct wpa_supplicant *wpa_s = ctx;
1406 const u8 *pos = tlvs;
1407 const u8 *end = tlvs + tlvs_len;
1410 struct wpabuf *resp;
1411 u8 srv_proto, srv_trans_id;
1415 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1417 buf_len = 2 * tlvs_len + 1;
1418 buf = os_malloc(buf_len);
1420 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1421 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1423 freq, MAC2STR(sa), dialog_token, update_indic,
1428 if (wpa_s->p2p_sd_over_ctrl_iface)
1429 return; /* to be processed by an external program */
1431 resp = wpabuf_alloc(10000);
1435 while (pos + 1 < end) {
1436 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1437 slen = WPA_GET_LE16(pos);
1439 if (pos + slen > end || slen < 2) {
1440 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1445 tlv_end = pos + slen;
1448 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1450 srv_trans_id = *pos++;
1451 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1454 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1455 pos, tlv_end - pos);
1458 if (wpa_s->force_long_sd) {
1459 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1461 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1462 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1466 switch (srv_proto) {
1467 case P2P_SERV_ALL_SERVICES:
1468 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1469 "for all services");
1470 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1471 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1472 wpa_printf(MSG_DEBUG, "P2P: No service "
1473 "discovery protocols available");
1474 wpas_sd_add_proto_not_avail(
1475 resp, P2P_SERV_ALL_SERVICES,
1479 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1480 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1482 case P2P_SERV_BONJOUR:
1483 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1484 pos, tlv_end - pos);
1487 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1488 pos, tlv_end - pos);
1491 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1492 "protocol %u", srv_proto);
1493 wpas_sd_add_proto_not_avail(resp, srv_proto,
1502 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1508 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1509 const u8 *tlvs, size_t tlvs_len)
1511 struct wpa_supplicant *wpa_s = ctx;
1512 const u8 *pos = tlvs;
1513 const u8 *end = tlvs + tlvs_len;
1519 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1521 if (tlvs_len > 1500) {
1522 /* TODO: better way for handling this */
1523 wpa_msg_ctrl(wpa_s, MSG_INFO,
1524 P2P_EVENT_SERV_DISC_RESP MACSTR
1525 " %u <long response: %u bytes>",
1526 MAC2STR(sa), update_indic,
1527 (unsigned int) tlvs_len);
1529 buf_len = 2 * tlvs_len + 1;
1530 buf = os_malloc(buf_len);
1532 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1533 wpa_msg_ctrl(wpa_s, MSG_INFO,
1534 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1535 MAC2STR(sa), update_indic, buf);
1541 u8 srv_proto, srv_trans_id, status;
1543 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1544 slen = WPA_GET_LE16(pos);
1546 if (pos + slen > end || slen < 3) {
1547 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1551 tlv_end = pos + slen;
1554 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1556 srv_trans_id = *pos++;
1557 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1560 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1563 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1564 pos, tlv_end - pos);
1571 void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1572 const struct wpabuf *tlvs)
1574 return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1578 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1579 u8 version, const char *query)
1581 struct wpabuf *tlvs;
1584 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1587 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1588 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1589 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1590 wpabuf_put_u8(tlvs, version);
1591 wpabuf_put_str(tlvs, query);
1592 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1598 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
1600 return p2p_sd_cancel_request(wpa_s->global->p2p, req);
1604 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1605 const u8 *dst, u8 dialog_token,
1606 const struct wpabuf *resp_tlvs)
1608 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1613 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1615 p2p_sd_service_update(wpa_s->global->p2p);
1619 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1621 dl_list_del(&bsrv->list);
1622 wpabuf_free(bsrv->query);
1623 wpabuf_free(bsrv->resp);
1628 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1630 dl_list_del(&usrv->list);
1631 os_free(usrv->service);
1636 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1638 struct p2p_srv_bonjour *bsrv, *bn;
1639 struct p2p_srv_upnp *usrv, *un;
1641 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1642 struct p2p_srv_bonjour, list)
1643 wpas_p2p_srv_bonjour_free(bsrv);
1645 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1646 struct p2p_srv_upnp, list)
1647 wpas_p2p_srv_upnp_free(usrv);
1649 wpas_p2p_sd_service_update(wpa_s);
1653 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1654 struct wpabuf *query, struct wpabuf *resp)
1656 struct p2p_srv_bonjour *bsrv;
1658 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1661 wpabuf_free(bsrv->resp);
1666 bsrv = os_zalloc(sizeof(*bsrv));
1669 bsrv->query = query;
1671 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1673 wpas_p2p_sd_service_update(wpa_s);
1678 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1679 const struct wpabuf *query)
1681 struct p2p_srv_bonjour *bsrv;
1683 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1686 wpas_p2p_srv_bonjour_free(bsrv);
1687 wpas_p2p_sd_service_update(wpa_s);
1692 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1693 const char *service)
1695 struct p2p_srv_upnp *usrv;
1697 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1698 return 0; /* Already listed */
1699 usrv = os_zalloc(sizeof(*usrv));
1702 usrv->version = version;
1703 usrv->service = os_strdup(service);
1704 if (usrv->service == NULL) {
1708 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1710 wpas_p2p_sd_service_update(wpa_s);
1715 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1716 const char *service)
1718 struct p2p_srv_upnp *usrv;
1720 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1723 wpas_p2p_srv_upnp_free(usrv);
1724 wpas_p2p_sd_service_update(wpa_s);
1729 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1730 const u8 *peer, const char *params)
1732 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1733 MAC2STR(peer), wps_generate_pin(), params);
1737 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1738 const u8 *peer, const char *params)
1740 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1741 MAC2STR(peer), params);
1745 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1746 const u8 *dev_addr, const u8 *pri_dev_type,
1747 const char *dev_name, u16 supp_config_methods,
1748 u8 dev_capab, u8 group_capab)
1750 struct wpa_supplicant *wpa_s = ctx;
1751 char devtype[WPS_DEV_TYPE_BUFSIZE];
1753 u8 empty_dev_type[8];
1755 if (pri_dev_type == NULL) {
1756 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1757 pri_dev_type = empty_dev_type;
1759 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1760 " pri_dev_type=%s name='%s' config_methods=0x%x "
1761 "dev_capab=0x%x group_capab=0x%x",
1763 wps_dev_type_bin2str(pri_dev_type, devtype,
1765 dev_name, supp_config_methods, dev_capab, group_capab);
1766 params[sizeof(params) - 1] = '\0';
1768 if (config_methods & WPS_CONFIG_DISPLAY)
1769 wpas_prov_disc_local_display(wpa_s, peer, params);
1770 else if (config_methods & WPS_CONFIG_KEYPAD)
1771 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1772 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1773 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1774 "%s", MAC2STR(peer), params);
1778 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1780 struct wpa_supplicant *wpa_s = ctx;
1781 if (config_methods & WPS_CONFIG_DISPLAY)
1782 wpas_prov_disc_local_keypad(wpa_s, peer, "");
1783 else if (config_methods & WPS_CONFIG_KEYPAD)
1784 wpas_prov_disc_local_display(wpa_s, peer, "");
1785 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1786 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
1789 if (wpa_s->pending_pd_before_join &&
1790 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1791 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1792 wpa_s->pending_pd_before_join = 0;
1793 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1794 "join-existing-group operation");
1795 wpas_p2p_join_start(wpa_s);
1800 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1801 const u8 *go_dev_addr, const u8 *ssid,
1802 size_t ssid_len, int *go, u8 *group_bssid,
1803 int *force_freq, int persistent_group)
1805 struct wpa_supplicant *wpa_s = ctx;
1807 u8 cur_bssid[ETH_ALEN];
1810 if (!persistent_group) {
1811 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1812 " to join an active group", MAC2STR(sa));
1813 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
1814 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
1816 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
1817 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1818 "authorized invitation");
1822 * Do not accept the invitation automatically; notify user and
1825 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1828 if (!wpa_s->conf->persistent_reconnect)
1829 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1831 for (s = wpa_s->conf->ssid; s; s = s->next) {
1832 if (s->disabled == 2 &&
1833 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1834 s->ssid_len == ssid_len &&
1835 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1840 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1841 " requested reinvocation of an unknown group",
1843 return P2P_SC_FAIL_UNKNOWN_GROUP;
1846 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1848 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1849 wpa_printf(MSG_DEBUG, "P2P: The only available "
1850 "interface is already in use - reject "
1852 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1854 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
1855 } else if (s->mode == WPAS_MODE_P2P_GO) {
1857 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
1859 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
1860 "interface address for the group");
1861 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1863 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
1868 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
1869 wpa_s->assoc_freq) {
1870 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1871 "the channel we are already using");
1872 *force_freq = wpa_s->assoc_freq;
1875 res = wpa_drv_shared_freq(wpa_s);
1877 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1878 "with the channel we are already using on a "
1879 "shared interface");
1883 return P2P_SC_SUCCESS;
1887 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
1888 const u8 *ssid, size_t ssid_len,
1889 const u8 *go_dev_addr, u8 status,
1892 struct wpa_supplicant *wpa_s = ctx;
1895 for (s = wpa_s->conf->ssid; s; s = s->next) {
1896 if (s->disabled == 2 &&
1897 s->ssid_len == ssid_len &&
1898 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1902 if (status == P2P_SC_SUCCESS) {
1903 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1904 " was accepted; op_freq=%d MHz",
1905 MAC2STR(sa), op_freq);
1907 wpas_p2p_group_add_persistent(
1908 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
1910 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
1911 wpa_s->p2p_wps_method);
1916 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
1917 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1918 " was rejected (status %u)", MAC2STR(sa), status);
1924 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1925 "sa=" MACSTR " go_dev_addr=" MACSTR
1926 " bssid=" MACSTR " unknown-network",
1927 MAC2STR(sa), MAC2STR(go_dev_addr),
1930 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1931 "sa=" MACSTR " go_dev_addr=" MACSTR
1933 MAC2STR(sa), MAC2STR(go_dev_addr));
1938 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
1939 " persistent=%d", MAC2STR(sa), s->id);
1943 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
1945 struct wpa_supplicant *wpa_s = ctx;
1946 struct wpa_ssid *ssid;
1949 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1950 "status=%d " MACSTR,
1951 status, MAC2STR(bssid));
1953 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1954 "status=%d ", status);
1957 if (wpa_s->pending_invite_ssid_id == -1)
1958 return; /* Invitation to active group */
1960 if (status != P2P_SC_SUCCESS) {
1961 wpas_p2p_remove_pending_group_interface(wpa_s);
1965 ssid = wpa_config_get_network(wpa_s->conf,
1966 wpa_s->pending_invite_ssid_id);
1968 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
1969 "data matching with invitation");
1973 wpas_p2p_group_add_persistent(wpa_s, ssid,
1974 ssid->mode == WPAS_MODE_P2P_GO, 0);
1978 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
1979 struct p2p_channels *chan)
1983 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
1986 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1987 chan->reg_class[cla].reg_class = 81;
1988 chan->reg_class[cla].channels = 11;
1989 for (i = 0; i < 11; i++)
1990 chan->reg_class[cla].channel[i] = i + 1;
1993 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
1996 /* Operating class 115 - 5 GHz, channels 36-48 */
1997 chan->reg_class[cla].reg_class = 115;
1998 chan->reg_class[cla].channels = 4;
1999 chan->reg_class[cla].channel[0] = 36;
2000 chan->reg_class[cla].channel[1] = 40;
2001 chan->reg_class[cla].channel[2] = 44;
2002 chan->reg_class[cla].channel[3] = 48;
2005 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2008 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2009 chan->reg_class[cla].reg_class = 124;
2010 chan->reg_class[cla].channels = 4;
2011 chan->reg_class[cla].channel[0] = 149;
2012 chan->reg_class[cla].channel[1] = 153;
2013 chan->reg_class[cla].channel[2] = 157;
2014 chan->reg_class[cla].channel[3] = 161;
2017 chan->reg_classes = cla;
2022 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2024 enum hostapd_hw_mode mode)
2028 for (i = 0; i < num_modes; i++) {
2029 if (modes[i].mode == mode)
2037 static int has_channel(struct hostapd_hw_modes *mode, u8 chan, int *flags)
2041 for (i = 0; i < mode->num_channels; i++) {
2042 if (mode->channels[i].chan == chan) {
2044 *flags = mode->channels[i].flag;
2045 return !(mode->channels[i].flag &
2046 (HOSTAPD_CHAN_DISABLED |
2047 HOSTAPD_CHAN_PASSIVE_SCAN |
2048 HOSTAPD_CHAN_NO_IBSS |
2049 HOSTAPD_CHAN_RADAR));
2057 struct p2p_oper_class_map {
2058 enum hostapd_hw_mode mode;
2063 enum { BW20, BW40PLUS, BW40MINUS } bw;
2066 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2067 struct p2p_channels *chan)
2069 struct hostapd_hw_modes *modes, *mode;
2070 u16 num_modes, flags;
2072 struct p2p_oper_class_map op_class[] = {
2073 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2074 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2075 #if 0 /* Do not enable HT40 on 2 GHz for now */
2076 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2077 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2079 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2080 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2081 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2082 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2083 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2084 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2085 { -1, 0, 0, 0, 0, BW20 }
2088 modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
2089 if (modes == NULL) {
2090 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2091 "of all supported channels; assume dualband "
2093 return wpas_p2p_default_channels(wpa_s, chan);
2098 for (op = 0; op_class[op].op_class; op++) {
2099 struct p2p_oper_class_map *o = &op_class[op];
2101 struct p2p_reg_class *reg = NULL;
2103 mode = get_mode(modes, num_modes, o->mode);
2106 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2108 if (!has_channel(mode, ch, &flag))
2110 if (o->bw == BW40MINUS &&
2111 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2112 !has_channel(mode, ch - 4, NULL)))
2114 if (o->bw == BW40PLUS &&
2115 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2116 !has_channel(mode, ch + 4, NULL)))
2119 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2120 "class %u", o->op_class);
2121 reg = &chan->reg_class[cla];
2123 reg->reg_class = o->op_class;
2125 reg->channel[reg->channels] = ch;
2129 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2130 reg->channel, reg->channels);
2134 chan->reg_classes = cla;
2136 ieee80211_sta_free_hw_features(modes, num_modes);
2142 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2145 struct wpa_supplicant *wpa_s = ctx;
2147 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2148 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2154 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2159 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2160 * @global: Pointer to global data from wpa_supplicant_init()
2161 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2162 * Returns: 0 on success, -1 on failure
2164 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2166 struct p2p_config p2p;
2170 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2173 #ifdef CONFIG_CLIENT_MLME
2174 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
2175 wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
2176 wpa_s->mlme.public_action_cb_ctx = wpa_s;
2178 #endif /* CONFIG_CLIENT_MLME */
2180 if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
2181 wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
2182 /* Continue anyway; this is not really a fatal error */
2188 os_memset(&p2p, 0, sizeof(p2p));
2189 p2p.msg_ctx = wpa_s;
2191 p2p.p2p_scan = wpas_p2p_scan;
2192 p2p.send_action = wpas_send_action;
2193 p2p.send_action_done = wpas_send_action_done;
2194 p2p.go_neg_completed = wpas_go_neg_completed;
2195 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2196 p2p.dev_found = wpas_dev_found;
2197 p2p.start_listen = wpas_start_listen;
2198 p2p.stop_listen = wpas_stop_listen;
2199 p2p.send_probe_resp = wpas_send_probe_resp;
2200 p2p.sd_request = wpas_sd_request;
2201 p2p.sd_response = wpas_sd_response;
2202 p2p.prov_disc_req = wpas_prov_disc_req;
2203 p2p.prov_disc_resp = wpas_prov_disc_resp;
2204 p2p.invitation_process = wpas_invitation_process;
2205 p2p.invitation_received = wpas_invitation_received;
2206 p2p.invitation_result = wpas_invitation_result;
2207 p2p.get_noa = wpas_get_noa;
2209 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2210 os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
2211 p2p.dev_name = wpa_s->conf->device_name;
2213 if (wpa_s->conf->p2p_listen_reg_class &&
2214 wpa_s->conf->p2p_listen_channel) {
2215 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2216 p2p.channel = wpa_s->conf->p2p_listen_channel;
2220 * Pick one of the social channels randomly as the listen
2223 os_get_random((u8 *) &r, sizeof(r));
2224 p2p.channel = 1 + (r % 3) * 5;
2226 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2228 if (wpa_s->conf->p2p_oper_reg_class &&
2229 wpa_s->conf->p2p_oper_channel) {
2230 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2231 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2232 p2p.cfg_op_channel = 1;
2233 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2234 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2237 p2p.op_reg_class = 81;
2239 * Use random operation channel from (1, 6, 11) if no other
2240 * preference is indicated.
2242 os_get_random((u8 *) &r, sizeof(r));
2243 p2p.op_channel = 1 + (r % 3) * 5;
2244 p2p.cfg_op_channel = 0;
2245 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2246 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2248 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2249 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2250 p2p.country[2] = 0x04;
2252 os_memcpy(p2p.country, "XX\x04", 3);
2254 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2255 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2260 if (wpa_s->conf->device_type &&
2261 wps_dev_type_str2bin(wpa_s->conf->device_type, p2p.pri_dev_type) <
2263 wpa_printf(MSG_ERROR, "P2P: Invalid device_type");
2267 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
2268 if (wpa_s->conf->sec_device_type[i] == NULL)
2270 if (wps_dev_type_str2bin(
2271 wpa_s->conf->sec_device_type[i],
2272 p2p.sec_dev_type[p2p.num_sec_dev_types]) < 0) {
2273 wpa_printf(MSG_ERROR, "P2P: Invalid sec_device_type");
2276 p2p.num_sec_dev_types++;
2277 if (p2p.num_sec_dev_types == P2P_SEC_DEVICE_TYPES)
2281 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2282 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2284 p2p.max_peers = 100;
2286 if (wpa_s->conf->p2p_ssid_postfix) {
2287 p2p.ssid_postfix_len =
2288 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2289 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2290 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2291 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2292 p2p.ssid_postfix_len);
2295 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2297 global->p2p = p2p_init(&p2p);
2298 if (global->p2p == NULL)
2306 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2307 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2309 * This function deinitialize per-interface P2P data.
2311 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2313 if (wpa_s->driver && wpa_s->drv_priv)
2314 wpa_drv_probe_req_report(wpa_s, 0);
2315 os_free(wpa_s->go_params);
2316 wpa_s->go_params = NULL;
2317 wpabuf_free(wpa_s->pending_action_tx);
2318 wpa_s->pending_action_tx = NULL;
2319 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
2320 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2321 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2322 wpa_s->p2p_long_listen = 0;
2323 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2324 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2325 wpas_p2p_remove_pending_group_interface(wpa_s);
2327 /* TODO: remove group interface from the driver if this wpa_s instance
2328 * is on top of a P2P group interface */
2333 * wpas_p2p_deinit_global - Deinitialize global P2P module
2334 * @global: Pointer to global data from wpa_supplicant_init()
2336 * This function deinitializes the global (per device) P2P module.
2338 void wpas_p2p_deinit_global(struct wpa_global *global)
2340 struct wpa_supplicant *wpa_s, *tmp;
2343 if (global->p2p == NULL)
2346 /* Remove remaining P2P group interfaces */
2347 wpa_s = global->ifaces;
2349 wpas_p2p_service_flush(wpa_s);
2350 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2351 wpa_s = wpa_s->next;
2353 enum wpa_driver_if_type type;
2354 tmp = global->ifaces;
2357 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2362 ifname = os_strdup(tmp->ifname);
2363 type = wpas_p2p_if_type(tmp->p2p_group_interface);
2364 wpa_supplicant_remove_iface(global, tmp);
2366 wpa_drv_if_remove(wpa_s, type, ifname);
2371 * Deinit GO data on any possibly remaining interface (if main
2372 * interface is used as GO).
2374 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2375 if (wpa_s->ap_iface)
2376 wpas_p2p_group_deinit(wpa_s);
2379 p2p_deinit(global->p2p);
2384 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2386 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
2387 return 1; /* P2P group requires a new interface in every case
2389 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2390 return 0; /* driver does not support concurrent operations */
2391 if (wpa_s->global->ifaces->next)
2392 return 1; /* more that one interface already in use */
2393 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2394 return 1; /* this interface is already in use */
2399 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2400 const u8 *peer_addr,
2401 enum p2p_wps_method wps_method,
2402 int go_intent, const u8 *own_interface_addr,
2403 unsigned int force_freq, int persistent_group)
2405 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2406 go_intent, own_interface_addr, force_freq,
2411 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2412 const u8 *peer_addr,
2413 enum p2p_wps_method wps_method,
2414 int go_intent, const u8 *own_interface_addr,
2415 unsigned int force_freq, int persistent_group)
2417 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2418 go_intent, own_interface_addr, force_freq,
2423 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2425 wpa_s->p2p_join_scan_count++;
2426 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2427 wpa_s->p2p_join_scan_count);
2428 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2429 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2430 " for join operationg - stop join attempt",
2431 MAC2STR(wpa_s->pending_join_iface_addr));
2432 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2433 wpa_msg(wpa_s->parent, MSG_INFO,
2434 P2P_EVENT_GROUP_FORMATION_FAILURE);
2439 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2440 struct wpa_scan_results *scan_res)
2442 struct wpa_bss *bss;
2444 u8 iface_addr[ETH_ALEN];
2446 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2448 if (wpa_s->global->p2p_disabled)
2451 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
2452 scan_res ? (int) scan_res->num : -1);
2455 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2457 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2458 wpa_s->pending_join_iface_addr);
2460 p2p_get_interface_addr(wpa_s->global->p2p,
2461 wpa_s->pending_join_dev_addr,
2463 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2465 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2466 "address for join from " MACSTR " to " MACSTR
2467 " based on newly discovered P2P peer entry",
2468 MAC2STR(wpa_s->pending_join_iface_addr),
2469 MAC2STR(iface_addr));
2470 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2473 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2474 wpa_s->pending_join_iface_addr);
2477 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2478 "from P2P peer table: %d MHz", freq);
2480 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2483 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2484 "from BSS table: %d MHz", freq);
2489 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2490 "prior to joining an existing group (GO " MACSTR
2492 MAC2STR(wpa_s->pending_join_dev_addr), freq);
2493 wpa_s->pending_pd_before_join = 1;
2495 switch (wpa_s->pending_join_wps_method) {
2497 case WPS_PIN_DISPLAY:
2498 method = WPS_CONFIG_KEYPAD;
2500 case WPS_PIN_KEYPAD:
2501 method = WPS_CONFIG_DISPLAY;
2504 method = WPS_CONFIG_PUSHBUTTON;
2511 if (p2p_prov_disc_req(wpa_s->global->p2p,
2512 wpa_s->pending_join_dev_addr, method, 1)
2514 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2515 "Discovery Request before joining an "
2517 wpa_s->pending_pd_before_join = 0;
2522 * Actual join operation will be started from the Action frame
2523 * TX status callback.
2528 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
2529 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2530 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2531 wpas_p2p_check_join_scan_limit(wpa_s);
2535 /* Start join operation immediately */
2536 wpas_p2p_join_start(wpa_s);
2540 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
2542 struct wpa_supplicant *wpa_s = eloop_ctx;
2544 struct wpa_driver_scan_params params;
2545 struct wpabuf *wps_ie, *ies;
2547 os_memset(¶ms, 0, sizeof(params));
2549 /* P2P Wildcard SSID */
2550 params.num_ssids = 1;
2551 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2552 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2554 wpa_s->wps->dev.p2p = 1;
2555 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
2557 if (wps_ie == NULL) {
2558 wpas_p2p_scan_res_join(wpa_s, NULL);
2562 ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
2564 wpabuf_free(wps_ie);
2565 wpas_p2p_scan_res_join(wpa_s, NULL);
2568 wpabuf_put_buf(ies, wps_ie);
2569 wpabuf_free(wps_ie);
2571 p2p_scan_ie(wpa_s->global->p2p, ies);
2573 params.extra_ies = wpabuf_head(ies);
2574 params.extra_ies_len = wpabuf_len(ies);
2577 * Run a scan to update BSS table and start Provision Discovery once
2578 * the new scan results become available.
2580 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
2581 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
2582 ret = ieee80211_sta_req_scan(wpa_s, ¶ms);
2584 ret = wpa_drv_scan(wpa_s, ¶ms);
2589 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
2591 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2592 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2593 wpas_p2p_check_join_scan_limit(wpa_s);
2598 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2599 const u8 *dev_addr, enum p2p_wps_method wps_method)
2601 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2602 MACSTR " dev " MACSTR ")",
2603 MAC2STR(iface_addr), MAC2STR(dev_addr));
2605 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2606 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2607 wpa_s->pending_join_wps_method = wps_method;
2609 /* Make sure we are not running find during connection establishment */
2610 wpas_p2p_stop_find(wpa_s);
2612 wpa_s->p2p_join_scan_count = 0;
2613 wpas_p2p_join_scan(wpa_s, NULL);
2618 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2620 struct wpa_supplicant *group;
2621 struct p2p_go_neg_results res;
2623 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2626 if (group != wpa_s) {
2627 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2628 sizeof(group->p2p_pin));
2629 group->p2p_wps_method = wpa_s->p2p_wps_method;
2632 group->p2p_in_provisioning = 1;
2634 os_memset(&res, 0, sizeof(res));
2635 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2637 res.wps_method = wpa_s->pending_join_wps_method;
2638 wpas_start_wps_enrollee(group, &res);
2641 * Allow a longer timeout for join-a-running-group than normal 15
2642 * second group formation timeout since the GO may not have authorized
2643 * our connection yet.
2645 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2646 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
2654 * wpas_p2p_connect - Request P2P Group Formation to be started
2655 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2656 * @peer_addr: Address of the peer P2P Device
2657 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2658 * @persistent_group: Whether to create a persistent group
2659 * @join: Whether to join an existing group (as a client) instead of starting
2660 * Group Owner negotiation; @peer_addr is BSSID in that case
2661 * @auth: Whether to only authorize the connection instead of doing that and
2662 * initiating Group Owner negotiation
2663 * @go_intent: GO Intent or -1 to use default
2664 * @freq: Frequency for the group or 0 for auto-selection
2665 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2666 * failure, -2 on failure due to channel not currently available,
2667 * -3 if forced channel is not supported
2669 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2670 const char *pin, enum p2p_wps_method wps_method,
2671 int persistent_group, int join, int auth, int go_intent,
2674 int force_freq = 0, oper_freq = 0;
2677 enum wpa_driver_if_type iftype;
2680 go_intent = wpa_s->conf->p2p_go_intent;
2683 wpa_s->p2p_long_listen = 0;
2685 wpa_s->p2p_wps_method = wps_method;
2688 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
2689 else if (wps_method == WPS_PIN_DISPLAY) {
2690 ret = wps_generate_pin();
2691 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
2693 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
2696 wpa_s->p2p_pin[0] = '\0';
2699 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
2701 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
2702 "connect a running group from " MACSTR,
2703 MAC2STR(peer_addr));
2704 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
2707 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
2708 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
2710 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
2711 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
2714 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) <
2720 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2722 oper_freq = wpa_s->assoc_freq;
2724 oper_freq = wpa_drv_shared_freq(wpa_s);
2730 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
2731 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
2732 "(%u MHz) is not supported for P2P uses",
2737 if (oper_freq > 0 && freq != oper_freq &&
2738 !(wpa_s->drv_flags &
2739 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2740 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2741 "on %u MHz while connected on another "
2742 "channel (%u MHz)", freq, oper_freq);
2745 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2746 "requested channel (%u MHz)", freq);
2748 } else if (oper_freq > 0 &&
2749 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
2750 if (!(wpa_s->drv_flags &
2751 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2752 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2753 "while connected on non-P2P supported "
2754 "channel (%u MHz)", oper_freq);
2757 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
2758 "(%u MHz) not available for P2P - try to use "
2759 "another channel", oper_freq);
2761 } else if (oper_freq > 0) {
2762 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2763 "channel we are already using (%u MHz) on another "
2764 "interface", oper_freq);
2765 force_freq = oper_freq;
2768 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
2770 if (!wpa_s->create_p2p_iface) {
2772 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2773 go_intent, wpa_s->own_addr,
2774 force_freq, persistent_group)
2779 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
2780 go_intent, wpa_s->own_addr,
2781 force_freq, persistent_group) < 0)
2786 /* Prepare to add a new interface for the group */
2787 iftype = WPA_IF_P2P_GROUP;
2789 iftype = WPA_IF_P2P_CLIENT;
2790 else if (go_intent == 15)
2791 iftype = WPA_IF_P2P_GO;
2792 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
2793 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2794 "interface for the group");
2799 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2801 wpa_s->pending_interface_addr,
2802 force_freq, persistent_group) < 0)
2806 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
2807 wpa_s->pending_interface_addr,
2808 force_freq, persistent_group) < 0) {
2809 wpas_p2p_remove_pending_group_interface(wpa_s);
2817 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2818 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2819 * @freq: Frequency of the channel in MHz
2820 * @duration: Duration of the stay on the channel in milliseconds
2822 * This callback is called when the driver indicates that it has started the
2823 * requested remain-on-channel duration.
2825 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2826 unsigned int freq, unsigned int duration)
2828 wpa_s->roc_waiting_drv_freq = 0;
2829 wpa_s->off_channel_freq = freq;
2830 wpas_send_action_cb(wpa_s, NULL);
2831 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
2832 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
2833 wpa_s->pending_listen_duration);
2834 wpa_s->pending_listen_freq = 0;
2839 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
2840 unsigned int timeout)
2842 /* Limit maximum Listen state time based on driver limitation. */
2843 if (timeout > wpa_s->max_remain_on_chan)
2844 timeout = wpa_s->max_remain_on_chan;
2846 return p2p_listen(wpa_s->global->p2p, timeout);
2851 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
2852 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2853 * @freq: Frequency of the channel in MHz
2855 * This callback is called when the driver indicates that a remain-on-channel
2856 * operation has been completed, i.e., the duration on the requested channel
2859 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2862 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
2863 "(p2p_long_listen=%d pending_action_tx=%p)",
2864 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
2865 wpa_s->off_channel_freq = 0;
2866 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
2867 return; /* P2P module started a new operation */
2868 if (wpa_s->pending_action_tx)
2870 if (wpa_s->p2p_long_listen > 0)
2871 wpa_s->p2p_long_listen -= 5;
2872 if (wpa_s->p2p_long_listen > 0) {
2873 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
2874 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen * 1000);
2880 * wpas_p2p_group_remove - Remove a P2P group
2881 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2882 * @ifname: Network interface name of the group interface or "*" to remove all
2884 * Returns: 0 on success, -1 on failure
2886 * This function is used to remove a P2P group. This can be used to disconnect
2887 * from a group in which the local end is a P2P Client or to end a P2P Group in
2888 * case the local end is the Group Owner. If a virtual network interface was
2889 * created for this group, that interface will be removed. Otherwise, only the
2890 * configured P2P group network will be removed from the interface.
2892 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
2894 struct wpa_global *global = wpa_s->global;
2896 if (os_strcmp(ifname, "*") == 0) {
2897 struct wpa_supplicant *prev;
2898 wpa_s = global->ifaces;
2901 wpa_s = wpa_s->next;
2902 prev->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
2903 wpas_p2p_group_delete(prev);
2908 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2909 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2916 wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
2917 wpas_p2p_group_delete(wpa_s);
2923 static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
2924 struct p2p_go_neg_results *params,
2930 os_memset(params, 0, sizeof(*params));
2931 params->role_go = 1;
2933 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
2934 "frequency %d MHz", freq);
2935 params->freq = freq;
2936 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
2937 wpa_s->conf->p2p_oper_channel >= 1 &&
2938 wpa_s->conf->p2p_oper_channel <= 11) {
2939 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
2940 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
2941 "frequency %d MHz", params->freq);
2942 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
2943 wpa_s->conf->p2p_oper_reg_class == 118) {
2944 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
2945 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
2946 "frequency %d MHz", params->freq);
2947 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
2948 wpa_s->best_overall_freq > 0 &&
2949 p2p_supported_freq(wpa_s->global->p2p,
2950 wpa_s->best_overall_freq)) {
2951 params->freq = wpa_s->best_overall_freq;
2952 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
2953 "channel %d MHz", params->freq);
2954 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
2955 wpa_s->best_24_freq > 0 &&
2956 p2p_supported_freq(wpa_s->global->p2p,
2957 wpa_s->best_24_freq)) {
2958 params->freq = wpa_s->best_24_freq;
2959 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
2960 "channel %d MHz", params->freq);
2961 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
2962 wpa_s->best_5_freq > 0 &&
2963 p2p_supported_freq(wpa_s->global->p2p,
2964 wpa_s->best_5_freq)) {
2965 params->freq = wpa_s->best_5_freq;
2966 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
2967 "channel %d MHz", params->freq);
2969 params->freq = 2412;
2970 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
2971 "known)", params->freq);
2974 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2975 wpa_s->assoc_freq && !freq) {
2976 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2978 params->freq = wpa_s->assoc_freq;
2981 res = wpa_drv_shared_freq(wpa_s);
2982 if (res > 0 && !freq) {
2983 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2984 "already using on a shared interface");
2990 static struct wpa_supplicant *
2991 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
2994 struct wpa_supplicant *group_wpa_s;
2996 if (!wpas_p2p_create_iface(wpa_s))
2999 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3000 WPA_IF_P2P_CLIENT) < 0)
3002 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3003 if (group_wpa_s == NULL) {
3004 wpas_p2p_remove_pending_group_interface(wpa_s);
3013 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3014 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3015 * @persistent_group: Whether to create a persistent group
3016 * @freq: Frequency for the group or 0 to indicate no hardcoding
3017 * Returns: 0 on success, -1 on failure
3019 * This function creates a new P2P group with the local end as the Group Owner,
3020 * i.e., without using Group Owner Negotiation.
3022 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3025 struct p2p_go_neg_results params;
3029 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3031 if (wpa_s->best_24_freq > 0 &&
3032 p2p_supported_freq(wpa_s->global->p2p,
3033 wpa_s->best_24_freq)) {
3034 freq = wpa_s->best_24_freq;
3035 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3036 "channel: %d MHz", freq);
3038 os_get_random((u8 *) &r, sizeof(r));
3039 freq = 2412 + (r % 3) * 25;
3040 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3041 "channel: %d MHz", freq);
3046 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3048 if (wpa_s->best_5_freq > 0 &&
3049 p2p_supported_freq(wpa_s->global->p2p,
3050 wpa_s->best_5_freq)) {
3051 freq = wpa_s->best_5_freq;
3052 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3053 "channel: %d MHz", freq);
3055 os_get_random((u8 *) &r, sizeof(r));
3056 freq = 5180 + (r % 4) * 20;
3057 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3058 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3059 "5 GHz channel for P2P group");
3062 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3063 "channel: %d MHz", freq);
3067 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3068 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3069 "(%u MHz) is not supported for P2P uses",
3074 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
3075 p2p_go_params(wpa_s->global->p2p, ¶ms);
3076 params.persistent_group = persistent_group;
3078 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3081 wpas_start_wps_go(wpa_s, ¶ms, 0);
3087 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3088 struct wpa_ssid *params, int addr_allocated)
3090 struct wpa_ssid *ssid;
3092 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3096 wpa_supplicant_ap_deinit(wpa_s);
3098 ssid = wpa_config_add_network(wpa_s->conf);
3101 wpas_notify_network_added(wpa_s, ssid);
3102 wpa_config_set_network_defaults(ssid);
3103 ssid->temporary = 1;
3104 ssid->proto = WPA_PROTO_RSN;
3105 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3106 ssid->group_cipher = WPA_CIPHER_CCMP;
3107 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3108 ssid->ssid = os_malloc(params->ssid_len);
3109 if (ssid->ssid == NULL) {
3110 wpas_notify_network_removed(wpa_s, ssid);
3111 wpa_config_remove_network(wpa_s->conf, ssid->id);
3114 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3115 ssid->ssid_len = params->ssid_len;
3116 ssid->p2p_group = 1;
3117 if (params->psk_set) {
3118 os_memcpy(ssid->psk, params->psk, 32);
3121 if (params->passphrase)
3122 ssid->passphrase = os_strdup(params->passphrase);
3124 wpa_supplicant_select_network(wpa_s, ssid);
3126 wpa_s->show_group_started = 1;
3132 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3133 struct wpa_ssid *ssid, int addr_allocated,
3136 struct p2p_go_neg_results params;
3138 if (ssid->disabled != 2 || ssid->ssid == NULL)
3141 wpa_s->p2p_long_listen = 0;
3142 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3144 if (ssid->mode == WPAS_MODE_INFRA)
3145 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3147 if (ssid->mode != WPAS_MODE_P2P_GO)
3150 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
3153 if (ssid->passphrase == NULL ||
3154 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3155 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3159 os_strlcpy(params.passphrase, ssid->passphrase,
3160 sizeof(params.passphrase));
3161 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3162 params.ssid_len = ssid->ssid_len;
3163 params.persistent_group = 1;
3165 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3169 wpas_start_wps_go(wpa_s, ¶ms, 0);
3175 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3176 struct wpabuf *proberesp_ies)
3178 struct wpa_supplicant *wpa_s = ctx;
3179 if (wpa_s->ap_iface) {
3180 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
3182 wpabuf_free(hapd->p2p_beacon_ie);
3183 hapd->p2p_beacon_ie = beacon_ies;
3185 wpabuf_free(hapd->p2p_probe_resp_ie);
3186 hapd->p2p_probe_resp_ie = proberesp_ies;
3188 wpabuf_free(beacon_ies);
3189 wpabuf_free(proberesp_ies);
3191 wpa_supplicant_ap_update_beacon(wpa_s);
3195 static void wpas_p2p_idle_update(void *ctx, int idle)
3197 struct wpa_supplicant *wpa_s = ctx;
3198 if (!wpa_s->ap_iface)
3200 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3202 wpas_p2p_set_group_idle_timeout(wpa_s);
3204 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3208 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
3209 int persistent_group,
3210 int group_formation)
3212 struct p2p_group *group;
3213 struct p2p_group_config *cfg;
3215 cfg = os_zalloc(sizeof(*cfg));
3219 cfg->persistent_group = persistent_group;
3220 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3221 if (wpa_s->max_stations &&
3222 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3223 cfg->max_clients = wpa_s->max_stations;
3225 cfg->max_clients = wpa_s->conf->max_num_sta;
3226 cfg->cb_ctx = wpa_s;
3227 cfg->ie_update = wpas_p2p_ie_update;
3228 cfg->idle_update = wpas_p2p_idle_update;
3230 group = p2p_group_init(wpa_s->global->p2p, cfg);
3233 if (!group_formation)
3234 p2p_group_notif_formation_done(group);
3235 wpa_s->p2p_group = group;
3240 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3243 if (!wpa_s->p2p_in_provisioning) {
3244 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3245 "provisioning not in progress");
3249 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3251 if (wpa_s->global->p2p)
3252 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3253 wpas_group_formation_completed(wpa_s, 1);
3257 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3258 const char *config_method)
3262 if (os_strcmp(config_method, "display") == 0)
3263 config_methods = WPS_CONFIG_DISPLAY;
3264 else if (os_strcmp(config_method, "keypad") == 0)
3265 config_methods = WPS_CONFIG_KEYPAD;
3266 else if (os_strcmp(config_method, "pbc") == 0 ||
3267 os_strcmp(config_method, "pushbutton") == 0)
3268 config_methods = WPS_CONFIG_PUSHBUTTON;
3272 if (wpa_s->global->p2p == NULL)
3275 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
3280 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3283 return p2p_scan_result_text(ies, ies_len, buf, end);
3287 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3289 if (!wpa_s->pending_action_tx)
3292 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3293 "operation request");
3294 wpabuf_free(wpa_s->pending_action_tx);
3295 wpa_s->pending_action_tx = NULL;
3299 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3300 enum p2p_discovery_type type)
3302 wpas_p2p_clear_pending_action_tx(wpa_s);
3303 wpa_s->p2p_long_listen = 0;
3305 return p2p_find(wpa_s->global->p2p, timeout, type);
3309 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3311 wpas_p2p_clear_pending_action_tx(wpa_s);
3312 wpa_s->p2p_long_listen = 0;
3313 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3315 p2p_stop_find(wpa_s->global->p2p);
3317 wpas_p2p_remove_pending_group_interface(wpa_s);
3321 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3323 struct wpa_supplicant *wpa_s = eloop_ctx;
3324 wpa_s->p2p_long_listen = 0;
3328 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3332 wpas_p2p_clear_pending_action_tx(wpa_s);
3336 * This is a request for unlimited Listen state. However, at
3337 * least for now, this is mapped to a Listen state for one
3342 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3343 wpa_s->p2p_long_listen = 0;
3345 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
3346 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
3347 wpa_s->p2p_long_listen = timeout;
3348 eloop_register_timeout(timeout, 0,
3349 wpas_p2p_long_listen_timeout,
3357 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3358 u8 *buf, size_t len, int p2p_group)
3360 struct wpabuf *p2p_ie;
3363 if (wpa_s->global->p2p_disabled)
3365 if (wpa_s->global->p2p == NULL)
3370 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
3371 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
3373 wpabuf_free(p2p_ie);
3379 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
3380 const u8 *ie, size_t ie_len)
3382 if (wpa_s->global->p2p_disabled)
3384 if (wpa_s->global->p2p == NULL)
3387 return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len);
3391 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
3392 const u8 *sa, const u8 *bssid,
3393 u8 category, const u8 *data, size_t len, int freq)
3395 if (wpa_s->global->p2p_disabled)
3397 if (wpa_s->global->p2p == NULL)
3400 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
3405 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
3407 if (wpa_s->global->p2p_disabled)
3409 if (wpa_s->global->p2p == NULL)
3412 p2p_scan_ie(wpa_s->global->p2p, ies);
3416 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
3418 p2p_group_deinit(wpa_s->p2p_group);
3419 wpa_s->p2p_group = NULL;
3423 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
3425 wpa_s->p2p_long_listen = 0;
3427 return p2p_reject(wpa_s->global->p2p, addr);
3431 /* Invite to reinvoke a persistent group */
3432 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3433 struct wpa_ssid *ssid, const u8 *go_dev_addr)
3435 enum p2p_invite_role role;
3438 if (ssid->mode == WPAS_MODE_P2P_GO) {
3439 role = P2P_INVITE_ROLE_GO;
3440 if (peer_addr == NULL) {
3441 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
3442 "address in invitation command");
3445 if (wpas_p2p_create_iface(wpa_s)) {
3446 if (wpas_p2p_add_group_interface(wpa_s,
3447 WPA_IF_P2P_GO) < 0) {
3448 wpa_printf(MSG_ERROR, "P2P: Failed to "
3449 "allocate a new interface for the "
3453 bssid = wpa_s->pending_interface_addr;
3455 bssid = wpa_s->own_addr;
3457 role = P2P_INVITE_ROLE_CLIENT;
3458 peer_addr = ssid->bssid;
3460 wpa_s->pending_invite_ssid_id = ssid->id;
3462 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3463 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
3467 /* Invite to join an active group */
3468 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
3469 const u8 *peer_addr, const u8 *go_dev_addr)
3471 struct wpa_global *global = wpa_s->global;
3472 enum p2p_invite_role role;
3474 struct wpa_ssid *ssid;
3476 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3477 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3480 if (wpa_s == NULL) {
3481 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
3485 ssid = wpa_s->current_ssid;
3487 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
3492 if (ssid->mode == WPAS_MODE_P2P_GO) {
3493 role = P2P_INVITE_ROLE_ACTIVE_GO;
3494 bssid = wpa_s->own_addr;
3495 if (go_dev_addr == NULL)
3496 go_dev_addr = wpa_s->parent->own_addr;
3498 role = P2P_INVITE_ROLE_CLIENT;
3499 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
3500 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
3501 "invite to current group");
3504 bssid = wpa_s->bssid;
3505 if (go_dev_addr == NULL &&
3506 !is_zero_ether_addr(wpa_s->go_dev_addr))
3507 go_dev_addr = wpa_s->go_dev_addr;
3509 wpa_s->parent->pending_invite_ssid_id = -1;
3511 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3512 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
3517 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
3519 struct wpa_ssid *ssid = wpa_s->current_ssid;
3520 const char *ssid_txt;
3521 u8 go_dev_addr[ETH_ALEN];
3524 if (!wpa_s->show_group_started || !ssid)
3527 wpa_s->show_group_started = 0;
3529 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
3530 os_memset(go_dev_addr, 0, ETH_ALEN);
3531 if (ssid->bssid_set)
3532 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
3533 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3535 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
3537 if (wpa_s->global->p2p_group_formation == wpa_s)
3538 wpa_s->global->p2p_group_formation = NULL;
3540 if (ssid->passphrase == NULL && ssid->psk_set) {
3542 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
3543 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3544 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3546 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
3547 MAC2STR(go_dev_addr),
3548 persistent ? " [PERSISTENT]" : "");
3550 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3551 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3552 "go_dev_addr=" MACSTR "%s",
3553 wpa_s->ifname, ssid_txt, ssid->frequency,
3554 ssid->passphrase ? ssid->passphrase : "",
3555 MAC2STR(go_dev_addr),
3556 persistent ? " [PERSISTENT]" : "");
3560 wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
3565 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
3566 u32 interval1, u32 duration2, u32 interval2)
3568 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3569 wpa_s->current_ssid == NULL ||
3570 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
3573 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
3574 wpa_s->own_addr, wpa_s->assoc_freq,
3575 duration1, interval1, duration2, interval2);
3579 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
3580 unsigned int interval)
3582 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
3586 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
3588 struct wpa_supplicant *wpa_s = eloop_ctx;
3590 if (wpa_s->conf->p2p_group_idle == 0) {
3591 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
3596 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
3598 wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
3599 wpas_p2p_group_delete(wpa_s);
3603 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
3605 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3606 if (wpa_s->conf->p2p_group_idle == 0)
3609 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
3612 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
3613 wpa_s->conf->p2p_group_idle);
3614 eloop_register_timeout(wpa_s->conf->p2p_group_idle, 0,
3615 wpas_p2p_group_idle_timeout, wpa_s, NULL);
3619 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3620 u16 reason_code, const u8 *ie, size_t ie_len)
3622 if (wpa_s->global->p2p_disabled)
3625 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3629 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3630 u16 reason_code, const u8 *ie, size_t ie_len)
3632 if (wpa_s->global->p2p_disabled)
3635 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3639 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
3641 struct p2p_data *p2p = wpa_s->global->p2p;
3646 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3649 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
3650 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
3652 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
3654 if (wpa_s->conf->device_type) {
3655 if (wps_dev_type_str2bin(wpa_s->conf->device_type,
3656 pri_dev_type) < 0) {
3657 wpa_printf(MSG_ERROR, "P2P: Invalid "
3660 p2p_set_pri_dev_type(p2p, pri_dev_type);
3664 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
3665 u8 sec_dev_type[P2P_SEC_DEVICE_TYPES][8];
3668 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
3669 if (wpa_s->conf->sec_device_type[i] == NULL)
3671 if (wps_dev_type_str2bin(
3672 wpa_s->conf->sec_device_type[i],
3673 sec_dev_type[num]) < 0) {
3674 wpa_printf(MSG_ERROR, "P2P: Invalid "
3679 if (num == P2P_SEC_DEVICE_TYPES)
3682 p2p_set_sec_dev_types(p2p, (void *) sec_dev_type, num);
3685 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
3686 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3688 country[0] = wpa_s->conf->country[0];
3689 country[1] = wpa_s->conf->country[1];
3691 p2p_set_country(p2p, country);
3694 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
3695 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
3696 wpa_s->conf->p2p_ssid_postfix ?
3697 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
3701 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
3702 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
3706 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
3709 if (!wpa_s->ap_iface)
3711 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
3716 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
3718 if (wpa_s->global->p2p_disabled)
3720 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3723 wpa_s->global->cross_connection = enabled;
3724 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
3727 struct wpa_supplicant *iface;
3729 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
3731 if (iface->cross_connect_enabled == 0)
3734 iface->cross_connect_enabled = 0;
3735 iface->cross_connect_in_use = 0;
3736 wpa_msg(iface->parent, MSG_INFO,
3737 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
3738 iface->ifname, iface->cross_connect_uplink);
3746 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
3748 struct wpa_supplicant *iface;
3750 if (!uplink->global->cross_connection)
3753 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
3754 if (!iface->cross_connect_enabled)
3756 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
3759 if (iface->ap_iface == NULL)
3761 if (iface->cross_connect_in_use)
3764 iface->cross_connect_in_use = 1;
3765 wpa_msg(iface->parent, MSG_INFO,
3766 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
3767 iface->ifname, iface->cross_connect_uplink);
3772 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
3774 struct wpa_supplicant *iface;
3776 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
3777 if (!iface->cross_connect_enabled)
3779 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
3782 if (!iface->cross_connect_in_use)
3785 wpa_msg(iface->parent, MSG_INFO,
3786 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
3787 iface->ifname, iface->cross_connect_uplink);
3788 iface->cross_connect_in_use = 0;
3793 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
3795 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
3796 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
3797 wpa_s->cross_connect_disallowed)
3798 wpas_p2p_disable_cross_connect(wpa_s);
3800 wpas_p2p_enable_cross_connect(wpa_s);
3801 if (!wpa_s->ap_iface)
3802 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3806 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
3808 wpas_p2p_disable_cross_connect(wpa_s);
3809 if (!wpa_s->ap_iface)
3810 wpas_p2p_set_group_idle_timeout(wpa_s);
3814 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
3816 struct wpa_supplicant *iface;
3818 if (!wpa_s->global->cross_connection)
3821 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3824 if (iface->drv_flags &
3825 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
3827 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
3830 wpa_s->cross_connect_enabled = 1;
3831 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
3832 sizeof(wpa_s->cross_connect_uplink));
3833 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
3834 "%s to %s whenever uplink is available",
3835 wpa_s->ifname, wpa_s->cross_connect_uplink);
3837 if (iface->ap_iface || iface->current_ssid == NULL ||
3838 iface->current_ssid->mode != WPAS_MODE_INFRA ||
3839 iface->cross_connect_disallowed ||
3840 iface->wpa_state != WPA_COMPLETED)
3843 wpa_s->cross_connect_in_use = 1;
3844 wpa_msg(wpa_s->parent, MSG_INFO,
3845 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
3846 wpa_s->ifname, wpa_s->cross_connect_uplink);
3852 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
3854 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
3855 !wpa_s->p2p_in_provisioning)
3856 return 0; /* not P2P client operation */
3858 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
3860 if (wpa_s != wpa_s->parent)
3861 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
3862 wpas_group_formation_completed(wpa_s, 0);
3867 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
3869 struct p2p_channels chan;
3871 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
3874 os_memset(&chan, 0, sizeof(chan));
3875 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
3876 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
3881 p2p_update_channel_list(wpa_s->global->p2p, &chan);
3885 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
3887 struct wpa_global *global = wpa_s->global;
3890 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
3892 if (wpa_s->pending_interface_name[0] &&
3893 !is_zero_ether_addr(wpa_s->pending_interface_addr))
3896 wpas_p2p_stop_find(wpa_s);
3898 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3899 if (wpa_s == global->p2p_group_formation &&
3900 (wpa_s->p2p_in_provisioning ||
3901 wpa_s->parent->pending_interface_type ==
3902 WPA_IF_P2P_CLIENT)) {
3903 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
3904 "formation found - cancelling",
3907 wpas_p2p_group_delete(wpa_s);
3913 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
3921 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
3923 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
3926 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
3927 "being available anymore");
3928 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
3929 wpas_p2p_group_delete(wpa_s);
3933 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
3934 int freq_24, int freq_5, int freq_overall)
3936 struct p2p_data *p2p = wpa_s->global->p2p;
3937 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
3939 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);