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 "eapol_supp/eapol_supp_sm.h"
27 #include "rsn_supp/wpa.h"
28 #include "wpa_supplicant_i.h"
31 #include "config_ssid.h"
37 #include "wps_supplicant.h"
38 #include "p2p_supplicant.h"
42 * How many times to try to scan to find the GO before giving up on join
45 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
48 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
49 static struct wpa_supplicant *
50 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
52 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
53 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
54 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
55 const u8 *dev_addr, enum p2p_wps_method wps_method);
56 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
57 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
58 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
59 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
62 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
63 struct wpa_scan_results *scan_res)
67 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
70 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
73 for (i = 0; i < scan_res->num; i++) {
74 struct wpa_scan_res *bss = scan_res->res[i];
75 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
76 bss->freq, bss->level,
77 (const u8 *) (bss + 1),
82 p2p_scan_res_handled(wpa_s->global->p2p);
86 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
87 unsigned int num_req_dev_types,
88 const u8 *req_dev_types)
90 struct wpa_supplicant *wpa_s = ctx;
91 struct wpa_driver_scan_params params;
93 struct wpabuf *wps_ie, *ies;
94 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
96 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
99 os_memset(¶ms, 0, sizeof(params));
101 /* P2P Wildcard SSID */
102 params.num_ssids = 1;
103 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
104 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
106 wpa_s->wps->dev.p2p = 1;
107 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
109 num_req_dev_types, req_dev_types);
113 ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
118 wpabuf_put_buf(ies, wps_ie);
121 p2p_scan_ie(wpa_s->global->p2p, ies);
123 params.extra_ies = wpabuf_head(ies);
124 params.extra_ies_len = wpabuf_len(ies);
127 case P2P_SCAN_SOCIAL:
128 params.freqs = social_channels;
132 case P2P_SCAN_SPECIFIC:
133 social_channels[0] = freq;
134 social_channels[1] = 0;
135 params.freqs = social_channels;
137 case P2P_SCAN_SOCIAL_PLUS_ONE:
138 social_channels[3] = freq;
139 params.freqs = social_channels;
143 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
144 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
145 ret = ieee80211_sta_req_scan(wpa_s, ¶ms);
147 ret = wpa_drv_scan(wpa_s, ¶ms);
155 #ifdef CONFIG_CLIENT_MLME
156 static void p2p_rx_action_mlme(void *ctx, const u8 *buf, size_t len, int freq)
158 struct wpa_supplicant *wpa_s = ctx;
159 const struct ieee80211_mgmt *mgmt;
162 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
164 mgmt = (const struct ieee80211_mgmt *) buf;
165 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
168 p2p_rx_action(wpa_s->global->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
169 mgmt->u.action.category,
170 &mgmt->u.action.u.vs_public_action.action,
171 len - hdr_len, freq);
173 #endif /* CONFIG_CLIENT_MLME */
176 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
178 switch (p2p_group_interface) {
179 case P2P_GROUP_INTERFACE_PENDING:
180 return WPA_IF_P2P_GROUP;
181 case P2P_GROUP_INTERFACE_GO:
182 return WPA_IF_P2P_GO;
183 case P2P_GROUP_INTERFACE_CLIENT:
184 return WPA_IF_P2P_CLIENT;
187 return WPA_IF_P2P_GROUP;
191 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
193 size_t ssid_len, int *go)
197 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
198 for (s = wpa_s->conf->ssid; s; s = s->next) {
199 if (s->disabled != 0 || !s->p2p_group ||
200 s->ssid_len != ssid_len ||
201 os_memcmp(ssid, s->ssid, ssid_len) != 0)
203 if (s->mode == WPAS_MODE_P2P_GO &&
204 s != wpa_s->current_ssid)
207 *go = s->mode == WPAS_MODE_P2P_GO;
216 static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
218 struct wpa_ssid *ssid;
222 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
224 ssid = wpa_s->current_ssid;
227 * The current SSID was not known, but there may still be a
228 * pending P2P group interface waiting for provisioning.
230 ssid = wpa_s->conf->ssid;
232 if (ssid->p2p_group &&
233 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
234 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
239 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
241 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
242 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
243 wpa_s->reassociate = 0;
244 wpa_s->disconnected = 1;
245 wpa_supplicant_deauthenticate(wpa_s,
246 WLAN_REASON_DEAUTH_LEAVING);
250 if (wpa_s->cross_connect_in_use) {
251 wpa_s->cross_connect_in_use = 0;
252 wpa_msg(wpa_s->parent, MSG_INFO,
253 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
254 wpa_s->ifname, wpa_s->cross_connect_uplink);
256 switch (wpa_s->removal_reason) {
257 case P2P_GROUP_REMOVAL_REQUESTED:
258 reason = " reason=REQUESTED";
260 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
261 reason = " reason=IDLE";
263 case P2P_GROUP_REMOVAL_UNAVAILABLE:
264 reason = " reason=UNAVAILABLE";
270 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s%s",
271 wpa_s->ifname, gtype, reason);
274 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
276 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
277 struct wpa_global *global;
279 enum wpa_driver_if_type type;
280 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
282 global = wpa_s->global;
283 ifname = os_strdup(wpa_s->ifname);
284 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
285 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
286 wpa_s = global->ifaces;
288 wpa_drv_if_remove(wpa_s, type, ifname);
293 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
294 if (ssid && (ssid->p2p_group ||
295 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
296 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
298 if (ssid == wpa_s->current_ssid) {
299 wpa_sm_set_config(wpa_s->wpa, NULL);
300 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
301 wpa_s->current_ssid = NULL;
304 * Networks objects created during any P2P activities are not
305 * exposed out as they might/will confuse certain non-P2P aware
306 * applications since these network objects won't behave like
309 * Likewise, we don't send out network removed signals for such
312 wpa_config_remove_network(wpa_s->conf, id);
313 wpa_supplicant_clear_status(wpa_s);
315 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
318 wpa_supplicant_ap_deinit(wpa_s);
322 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
324 const u8 *ssid, size_t ssid_len)
332 if (wpa_s->go_params)
333 bssid = wpa_s->go_params->peer_interface_addr;
335 bssid = wpa_s->bssid;
337 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
339 u8 iface_addr[ETH_ALEN];
340 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
342 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
345 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
346 "group is persistent - BSS " MACSTR " not found",
351 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
353 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
354 "group is persistent - BSS " MACSTR
355 " did not include P2P IE", MAC2STR(bssid));
356 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
357 (u8 *) (bss + 1), bss->ie_len);
358 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
359 ((u8 *) bss + 1) + bss->ie_len,
364 group_capab = p2p_get_group_capab(p2p);
365 addr = p2p_get_go_dev_addr(p2p);
366 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
367 "group_capab=0x%x", group_capab);
369 os_memcpy(go_dev_addr, addr, ETH_ALEN);
370 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
373 os_memset(go_dev_addr, 0, ETH_ALEN);
376 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
377 "go_dev_addr=" MACSTR,
378 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
380 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
384 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
385 struct wpa_ssid *ssid,
386 const u8 *go_dev_addr)
391 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
392 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
393 for (s = wpa_s->conf->ssid; s; s = s->next) {
394 if (s->disabled == 2 &&
395 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
396 s->ssid_len == ssid->ssid_len &&
397 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
402 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
404 if (ssid->passphrase && !s->passphrase)
406 else if (ssid->passphrase && s->passphrase &&
407 os_strcmp(ssid->passphrase, s->passphrase) != 0)
410 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
413 s = wpa_config_add_network(wpa_s->conf);
418 * Instead of network_added we emit persistent_group_added
419 * notification. Also to keep the defense checks in
420 * persistent_group obj registration method, we set the
421 * relevant flags in s to designate it as a persistent group.
424 s->p2p_persistent_group = 1;
425 wpas_notify_persistent_group_added(wpa_s, s);
426 wpa_config_set_network_defaults(s);
430 s->p2p_persistent_group = 1;
433 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
434 s->mode = ssid->mode;
435 s->auth_alg = WPA_AUTH_ALG_OPEN;
436 s->key_mgmt = WPA_KEY_MGMT_PSK;
437 s->proto = WPA_PROTO_RSN;
438 s->pairwise_cipher = WPA_CIPHER_CCMP;
440 if (ssid->passphrase) {
441 os_free(s->passphrase);
442 s->passphrase = os_strdup(ssid->passphrase);
446 os_memcpy(s->psk, ssid->psk, 32);
448 if (s->passphrase && !s->psk_set)
449 wpa_config_update_psk(s);
450 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
452 s->ssid = os_malloc(ssid->ssid_len);
455 s->ssid_len = ssid->ssid_len;
456 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
459 #ifndef CONFIG_NO_CONFIG_WRITE
460 if (changed && wpa_s->conf->update_config &&
461 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
462 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
464 #endif /* CONFIG_NO_CONFIG_WRITE */
470 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
473 struct wpa_ssid *ssid;
474 const char *ssid_txt;
477 u8 go_dev_addr[ETH_ALEN];
481 * This callback is likely called for the main interface. Update wpa_s
482 * to use the group interface if a new interface was created for the
485 if (wpa_s->global->p2p_group_formation)
486 wpa_s = wpa_s->global->p2p_group_formation;
487 wpa_s->global->p2p_group_formation = NULL;
488 wpa_s->p2p_in_provisioning = 0;
491 wpa_msg(wpa_s->parent, MSG_INFO,
492 P2P_EVENT_GROUP_FORMATION_FAILURE);
493 wpas_p2p_group_delete(wpa_s);
497 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
499 ssid = wpa_s->current_ssid;
500 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
501 ssid->mode = WPAS_MODE_P2P_GO;
502 p2p_group_notif_formation_done(wpa_s->p2p_group);
503 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
508 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
509 client = ssid->mode == WPAS_MODE_INFRA;
510 if (ssid->mode == WPAS_MODE_P2P_GO) {
511 persistent = ssid->p2p_persistent_group;
512 #ifndef ANDROID_BRCM_P2P_PATCH
513 os_memcpy(go_dev_addr, wpa_s->parent->own_addr,
516 /* P2P_ADDR: Use p2p_dev_addr instead of own mac addr */
517 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
522 persistent = wpas_p2p_persistent_group(wpa_s,
528 client = wpa_s->p2p_group_interface ==
529 P2P_GROUP_INTERFACE_CLIENT;
530 os_memset(go_dev_addr, 0, ETH_ALEN);
533 wpa_s->show_group_started = 0;
536 * Indicate event only after successfully completed 4-way
537 * handshake, i.e., when the interface is ready for data
540 wpa_s->show_group_started = 1;
541 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
543 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
544 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
545 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
547 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
548 MAC2STR(go_dev_addr),
549 persistent ? " [PERSISTENT]" : "");
550 wpas_p2p_cross_connect_setup(wpa_s);
551 wpas_p2p_set_group_idle_timeout(wpa_s);
553 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
554 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
555 "go_dev_addr=" MACSTR "%s",
556 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
557 ssid && ssid->passphrase ? ssid->passphrase : "",
558 MAC2STR(go_dev_addr),
559 persistent ? " [PERSISTENT]" : "");
560 wpas_p2p_cross_connect_setup(wpa_s);
561 wpas_p2p_set_group_idle_timeout(wpa_s);
565 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
568 network_id = ssid->id;
570 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
574 static struct wpa_supplicant *
575 wpas_get_tx_interface(struct wpa_supplicant *wpa_s, const u8 *src)
577 struct wpa_supplicant *iface;
579 if (os_memcmp(src, wpa_s->own_addr, ETH_ALEN) == 0)
583 * Try to find a group interface that matches with the source address.
585 iface = wpa_s->global->ifaces;
587 if (os_memcmp(wpa_s->pending_action_src,
588 iface->own_addr, ETH_ALEN) == 0)
593 wpa_printf(MSG_DEBUG, "P2P: Use group interface %s "
594 "instead of interface %s for Action TX",
595 iface->ifname, wpa_s->ifname);
603 static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
605 struct wpa_supplicant *wpa_s = eloop_ctx;
606 struct wpa_supplicant *iface;
610 without_roc = wpa_s->pending_action_without_roc;
611 wpa_s->pending_action_without_roc = 0;
612 wpa_printf(MSG_DEBUG, "P2P: Send Action callback (without_roc=%d "
613 "pending_action_tx=%p)",
614 without_roc, wpa_s->pending_action_tx);
616 if (wpa_s->pending_action_tx == NULL)
620 * This call is likely going to be on the P2P device instance if the
621 * driver uses a separate interface for that purpose. However, some
622 * Action frames are actually sent within a P2P Group and when that is
623 * the case, we need to follow power saving (e.g., GO buffering the
624 * frame for a client in PS mode or a client following the advertised
625 * NoA from its GO). To make that easier for the driver, select the
626 * correct group interface here.
628 iface = wpas_get_tx_interface(wpa_s, wpa_s->pending_action_src);
630 if (wpa_s->off_channel_freq != wpa_s->pending_action_freq &&
631 wpa_s->pending_action_freq != 0 &&
632 wpa_s->pending_action_freq != iface->assoc_freq) {
633 wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX "
634 "waiting for another freq=%u (off_channel_freq=%u "
636 wpa_s->pending_action_freq,
637 wpa_s->off_channel_freq,
639 if (without_roc && wpa_s->off_channel_freq == 0) {
641 * We may get here if wpas_send_action() found us to be
642 * on the correct channel, but remain-on-channel cancel
643 * event was received before getting here.
645 wpa_printf(MSG_DEBUG, "P2P: Schedule "
646 "remain-on-channel to send Action frame");
647 if (wpa_drv_remain_on_channel(
648 wpa_s, wpa_s->pending_action_freq, 200) <
650 wpa_printf(MSG_DEBUG, "P2P: Failed to request "
651 "driver to remain on channel (%u "
652 "MHz) for Action Frame TX",
653 wpa_s->pending_action_freq);
655 wpa_s->off_channel_freq = 0;
656 wpa_s->roc_waiting_drv_freq =
657 wpa_s->pending_action_freq;
663 wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to "
664 MACSTR " using interface %s",
665 MAC2STR(wpa_s->pending_action_dst), iface->ifname);
666 res = wpa_drv_send_action(iface, wpa_s->pending_action_freq, 0,
667 wpa_s->pending_action_dst,
668 wpa_s->pending_action_src,
669 wpa_s->pending_action_bssid,
670 wpabuf_head(wpa_s->pending_action_tx),
671 wpabuf_len(wpa_s->pending_action_tx));
673 wpa_printf(MSG_DEBUG, "P2P: Failed to send the pending "
676 * Use fake TX status event to allow P2P state machine to
679 wpas_send_action_tx_status(
680 wpa_s, wpa_s->pending_action_dst,
681 wpabuf_head(wpa_s->pending_action_tx),
682 wpabuf_len(wpa_s->pending_action_tx),
683 P2P_SEND_ACTION_FAILED);
688 void wpas_send_action_tx_status(struct wpa_supplicant *wpa_s, const u8 *dst,
689 const u8 *data, size_t data_len,
690 enum p2p_send_action_result result)
692 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
694 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
697 if (wpa_s->pending_action_tx == NULL) {
698 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - no "
699 "pending operation");
703 if (os_memcmp(dst, wpa_s->pending_action_dst, ETH_ALEN) != 0) {
704 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - unknown "
705 "destination address");
709 wpabuf_free(wpa_s->pending_action_tx);
710 wpa_s->pending_action_tx = NULL;
712 p2p_send_action_cb(wpa_s->global->p2p, wpa_s->pending_action_freq,
713 wpa_s->pending_action_dst,
714 wpa_s->pending_action_src,
715 wpa_s->pending_action_bssid,
718 if (wpa_s->pending_pd_before_join &&
719 (os_memcmp(wpa_s->pending_action_dst, wpa_s->pending_join_dev_addr,
721 os_memcmp(wpa_s->pending_action_dst,
722 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
723 wpa_s->pending_pd_before_join = 0;
724 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
725 "join-existing-group operation");
726 wpas_p2p_join_start(wpa_s);
731 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
732 const u8 *src, const u8 *bssid, const u8 *buf,
733 size_t len, unsigned int wait_time)
735 struct wpa_supplicant *wpa_s = ctx;
737 wpa_printf(MSG_DEBUG, "P2P: Send action frame: freq=%d dst=" MACSTR
738 " src=" MACSTR " bssid=" MACSTR " len=%d",
739 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
742 if (wpa_s->pending_action_tx) {
743 wpa_printf(MSG_DEBUG, "P2P: Dropped pending Action frame TX "
744 "to " MACSTR, MAC2STR(wpa_s->pending_action_dst));
745 wpabuf_free(wpa_s->pending_action_tx);
747 wpa_s->pending_action_tx = wpabuf_alloc(len);
748 if (wpa_s->pending_action_tx == NULL) {
749 wpa_printf(MSG_DEBUG, "P2P: Failed to allocate Action frame "
750 "TX buffer (len=%llu)", (unsigned long long) len);
753 wpabuf_put_data(wpa_s->pending_action_tx, buf, len);
754 os_memcpy(wpa_s->pending_action_src, src, ETH_ALEN);
755 os_memcpy(wpa_s->pending_action_dst, dst, ETH_ALEN);
756 os_memcpy(wpa_s->pending_action_bssid, bssid, ETH_ALEN);
757 wpa_s->pending_action_freq = freq;
759 if (freq != 0 && wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) {
760 struct wpa_supplicant *iface;
762 iface = wpas_get_tx_interface(wpa_s, wpa_s->pending_action_src);
763 wpa_s->action_tx_wait_time = wait_time;
765 return wpa_drv_send_action(iface, wpa_s->pending_action_freq,
766 wait_time, wpa_s->pending_action_dst,
767 wpa_s->pending_action_src,
768 wpa_s->pending_action_bssid,
769 wpabuf_head(wpa_s->pending_action_tx),
770 wpabuf_len(wpa_s->pending_action_tx));
774 struct wpa_supplicant *tx_iface;
775 tx_iface = wpas_get_tx_interface(wpa_s, src);
776 if (tx_iface->assoc_freq == freq) {
777 wpa_printf(MSG_DEBUG, "P2P: Already on requested "
778 "channel (TX interface operating channel)");
783 if (wpa_s->off_channel_freq == freq || freq == 0) {
784 wpa_printf(MSG_DEBUG, "P2P: Already on requested channel; "
785 "send Action frame immediately");
786 /* TODO: Would there ever be need to extend the current
787 * duration on the channel? */
788 wpa_s->pending_action_without_roc = 1;
789 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
790 eloop_register_timeout(0, 0, wpas_send_action_cb, wpa_s, NULL);
793 wpa_s->pending_action_without_roc = 0;
795 if (wpa_s->roc_waiting_drv_freq == freq) {
796 wpa_printf(MSG_DEBUG, "P2P: Already waiting for driver to get "
797 "to frequency %u MHz; continue waiting to send the "
798 "Action frame", freq);
802 wpa_printf(MSG_DEBUG, "P2P: Schedule Action frame to be transmitted "
803 "once the driver gets to the requested channel");
804 if (wait_time > wpa_s->max_remain_on_chan)
805 wait_time = wpa_s->max_remain_on_chan;
806 if (wpa_drv_remain_on_channel(wpa_s, freq, wait_time) < 0) {
807 wpa_printf(MSG_DEBUG, "P2P: Failed to request driver "
808 "to remain on channel (%u MHz) for Action "
812 wpa_s->off_channel_freq = 0;
813 wpa_s->roc_waiting_drv_freq = freq;
819 static void wpas_send_action_done(void *ctx)
821 struct wpa_supplicant *wpa_s = ctx;
822 wpa_printf(MSG_DEBUG, "P2P: Action frame sequence done notification");
823 wpabuf_free(wpa_s->pending_action_tx);
824 wpa_s->pending_action_tx = NULL;
825 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) {
826 if (wpa_s->action_tx_wait_time)
827 wpa_drv_send_action_cancel_wait(wpa_s);
828 wpa_s->off_channel_freq = 0;
829 } else if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
830 wpa_drv_cancel_remain_on_channel(wpa_s);
831 wpa_s->off_channel_freq = 0;
832 wpa_s->roc_waiting_drv_freq = 0;
837 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
838 struct p2p_go_neg_results *params)
840 if (wpa_s->go_params == NULL) {
841 wpa_s->go_params = os_malloc(sizeof(*params));
842 if (wpa_s->go_params == NULL)
845 os_memcpy(wpa_s->go_params, params, sizeof(*params));
850 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
851 struct p2p_go_neg_results *res)
853 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
854 MAC2STR(res->peer_interface_addr));
855 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
856 res->ssid, res->ssid_len);
857 wpa_supplicant_ap_deinit(wpa_s);
858 wpas_copy_go_neg_results(wpa_s, res);
859 if (res->wps_method == WPS_PBC)
860 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
862 u16 dev_pw_id = DEV_PW_DEFAULT;
863 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
864 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
865 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
866 wpa_s->p2p_pin, 1, dev_pw_id);
871 static void p2p_go_configured(void *ctx, void *data)
873 struct wpa_supplicant *wpa_s = ctx;
874 struct p2p_go_neg_results *params = data;
875 struct wpa_ssid *ssid;
878 ssid = wpa_s->current_ssid;
879 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
880 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
881 if (wpa_s->global->p2p_group_formation == wpa_s)
882 wpa_s->global->p2p_group_formation = NULL;
883 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
884 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
885 "go_dev_addr=" MACSTR "%s",
887 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
889 params->passphrase ? params->passphrase : "",
890 #ifndef ANDROID_BRCM_P2P_PATCH
891 MAC2STR(wpa_s->parent->own_addr),
893 /* P2P_ADDR: use p2p_dev_addr instead of own addr */
894 MAC2STR(wpa_s->global->p2p_dev_addr),
896 params->persistent_group ? " [PERSISTENT]" : "");
897 if (params->persistent_group)
898 network_id = wpas_p2p_store_persistent_group(
900 #ifndef ANDROID_BRCM_P2P_PATCH
901 wpa_s->parent->own_addr);
903 /* P2P_ADDR: Use p2p device address */
904 wpa_s->global->p2p_dev_addr);
907 network_id = ssid->id;
908 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
909 wpas_p2p_cross_connect_setup(wpa_s);
910 wpas_p2p_set_group_idle_timeout(wpa_s);
914 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
915 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
916 params->peer_interface_addr)) {
917 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
921 if (params->wps_method == WPS_PBC)
922 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
924 else if (wpa_s->p2p_pin[0])
925 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
926 wpa_s->p2p_pin, NULL, 0);
927 os_free(wpa_s->go_params);
928 wpa_s->go_params = NULL;
932 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
933 struct p2p_go_neg_results *params,
936 struct wpa_ssid *ssid;
938 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
941 ssid = wpa_config_add_network(wpa_s->conf);
945 wpa_config_set_network_defaults(ssid);
948 ssid->p2p_persistent_group = params->persistent_group;
949 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
951 ssid->frequency = params->freq;
952 ssid->ssid = os_zalloc(params->ssid_len + 1);
954 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
955 ssid->ssid_len = params->ssid_len;
957 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
958 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
959 ssid->proto = WPA_PROTO_RSN;
960 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
961 ssid->passphrase = os_strdup(params->passphrase);
963 wpa_s->ap_configured_cb = p2p_go_configured;
964 wpa_s->ap_configured_cb_ctx = wpa_s;
965 wpa_s->ap_configured_cb_data = wpa_s->go_params;
966 wpa_s->connect_without_scan = ssid;
967 wpa_s->reassociate = 1;
968 wpa_s->disconnected = 0;
969 wpa_supplicant_req_scan(wpa_s, 0, 0);
973 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
974 const struct wpa_supplicant *src)
976 struct wpa_config *d;
977 const struct wpa_config *s;
982 #define C(n) if (s->n) d->n = os_strdup(s->n)
991 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
992 os_memcpy(d->sec_device_type, s->sec_device_type,
993 sizeof(d->sec_device_type));
994 d->num_sec_device_types = s->num_sec_device_types;
996 d->p2p_group_idle = s->p2p_group_idle;
997 d->p2p_intra_bss = s->p2p_intra_bss;
1001 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1002 enum wpa_driver_if_type type)
1004 char ifname[120], force_ifname[120];
1006 if (wpa_s->pending_interface_name[0]) {
1007 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1008 "- skip creation of a new one");
1009 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1010 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1011 "unknown?! ifname='%s'",
1012 wpa_s->pending_interface_name);
1018 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
1019 wpa_s->p2p_group_idx);
1020 if (os_strlen(ifname) >= IFNAMSIZ &&
1021 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1022 /* Try to avoid going over the IFNAMSIZ length limit */
1023 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1024 wpa_s->p2p_group_idx);
1026 force_ifname[0] = '\0';
1028 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1030 wpa_s->p2p_group_idx++;
1032 wpa_s->pending_interface_type = type;
1033 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1034 wpa_s->pending_interface_addr, NULL) < 0) {
1035 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1040 if (force_ifname[0]) {
1041 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1043 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1044 sizeof(wpa_s->pending_interface_name));
1046 os_strlcpy(wpa_s->pending_interface_name, ifname,
1047 sizeof(wpa_s->pending_interface_name));
1048 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1049 MACSTR, wpa_s->pending_interface_name,
1050 MAC2STR(wpa_s->pending_interface_addr));
1056 static void wpas_p2p_remove_pending_group_interface(
1057 struct wpa_supplicant *wpa_s)
1059 if (!wpa_s->pending_interface_name[0] ||
1060 is_zero_ether_addr(wpa_s->pending_interface_addr))
1061 return; /* No pending virtual interface */
1063 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1064 wpa_s->pending_interface_name);
1065 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1066 wpa_s->pending_interface_name);
1067 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1068 wpa_s->pending_interface_name[0] = '\0';
1072 static struct wpa_supplicant *
1073 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1075 struct wpa_interface iface;
1076 struct wpa_supplicant *group_wpa_s;
1078 if (!wpa_s->pending_interface_name[0]) {
1079 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1080 if (!wpas_p2p_create_iface(wpa_s))
1083 * Something has forced us to remove the pending interface; try
1084 * to create a new one and hope for the best that we will get
1085 * the same local address.
1087 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1088 WPA_IF_P2P_CLIENT) < 0)
1092 os_memset(&iface, 0, sizeof(iface));
1093 iface.ifname = wpa_s->pending_interface_name;
1094 iface.driver = wpa_s->driver->name;
1095 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1096 iface.driver_param = wpa_s->conf->driver_param;
1097 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1098 if (group_wpa_s == NULL) {
1099 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1100 "wpa_supplicant interface");
1103 wpa_s->pending_interface_name[0] = '\0';
1104 group_wpa_s->parent = wpa_s;
1105 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1106 P2P_GROUP_INTERFACE_CLIENT;
1107 wpa_s->global->p2p_group_formation = group_wpa_s;
1109 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1115 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1118 struct wpa_supplicant *wpa_s = eloop_ctx;
1119 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1120 if (wpa_s->global->p2p)
1121 p2p_group_formation_failed(wpa_s->global->p2p);
1122 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1123 wpa_drv_p2p_group_formation_failed(wpa_s);
1124 wpas_group_formation_completed(wpa_s, 0);
1128 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1130 struct wpa_supplicant *wpa_s = ctx;
1132 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1133 wpa_drv_cancel_remain_on_channel(wpa_s);
1134 wpa_s->off_channel_freq = 0;
1135 wpa_s->roc_waiting_drv_freq = 0;
1139 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1141 wpas_notify_p2p_go_neg_completed(wpa_s, res->status);
1142 wpas_p2p_remove_pending_group_interface(wpa_s);
1146 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1147 wpas_notify_p2p_go_neg_completed(wpa_s, P2P_SC_SUCCESS);
1149 if (wpa_s->create_p2p_iface) {
1150 struct wpa_supplicant *group_wpa_s =
1151 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1152 if (group_wpa_s == NULL) {
1153 wpas_p2p_remove_pending_group_interface(wpa_s);
1156 if (group_wpa_s != wpa_s) {
1157 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1158 sizeof(group_wpa_s->p2p_pin));
1159 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1161 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1162 wpa_s->pending_interface_name[0] = '\0';
1163 group_wpa_s->p2p_in_provisioning = 1;
1166 wpas_start_wps_go(group_wpa_s, res, 1);
1168 wpas_start_wps_enrollee(group_wpa_s, res);
1170 wpa_s->p2p_in_provisioning = 1;
1171 wpa_s->global->p2p_group_formation = wpa_s;
1174 wpas_start_wps_go(wpa_s, res, 1);
1176 wpas_start_wps_enrollee(ctx, res);
1179 wpa_s->p2p_long_listen = 0;
1180 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1182 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1183 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1184 (res->peer_config_timeout % 100) * 10000,
1185 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1189 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1191 struct wpa_supplicant *wpa_s = ctx;
1192 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1193 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1195 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1199 void wpas_dev_found(void *ctx, const u8 *addr,
1200 const struct p2p_peer_info *info,
1203 struct wpa_supplicant *wpa_s = ctx;
1204 char devtype[WPS_DEV_TYPE_BUFSIZE];
1206 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1207 " p2p_dev_addr=" MACSTR
1208 " pri_dev_type=%s name='%s' config_methods=0x%x "
1209 "dev_capab=0x%x group_capab=0x%x",
1210 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1211 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1213 info->device_name, info->config_methods,
1214 info->dev_capab, info->group_capab);
1216 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1220 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1222 struct wpa_supplicant *wpa_s = ctx;
1223 #ifdef ANDROID_BRCM_P2P_PATCH
1224 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1225 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1227 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1231 static int wpas_start_listen(void *ctx, unsigned int freq,
1232 unsigned int duration,
1233 const struct wpabuf *probe_resp_ie)
1235 struct wpa_supplicant *wpa_s = ctx;
1237 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1239 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1240 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1241 "report received Probe Request frames");
1245 wpa_s->pending_listen_freq = freq;
1246 wpa_s->pending_listen_duration = duration;
1248 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1249 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1250 "to remain on channel (%u MHz) for Listen "
1252 wpa_s->pending_listen_freq = 0;
1255 wpa_s->off_channel_freq = 0;
1256 wpa_s->roc_waiting_drv_freq = freq;
1262 static void wpas_stop_listen(void *ctx)
1264 struct wpa_supplicant *wpa_s = ctx;
1265 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1266 wpa_drv_cancel_remain_on_channel(wpa_s);
1267 wpa_s->off_channel_freq = 0;
1268 wpa_s->roc_waiting_drv_freq = 0;
1270 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1271 wpa_drv_probe_req_report(wpa_s, 0);
1275 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1277 struct wpa_supplicant *wpa_s = ctx;
1278 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
1282 static struct p2p_srv_bonjour *
1283 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1284 const struct wpabuf *query)
1286 struct p2p_srv_bonjour *bsrv;
1289 len = wpabuf_len(query);
1290 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1291 struct p2p_srv_bonjour, list) {
1292 if (len == wpabuf_len(bsrv->query) &&
1293 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1301 static struct p2p_srv_upnp *
1302 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1303 const char *service)
1305 struct p2p_srv_upnp *usrv;
1307 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1308 struct p2p_srv_upnp, list) {
1309 if (version == usrv->version &&
1310 os_strcmp(service, usrv->service) == 0)
1317 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1322 if (wpabuf_tailroom(resp) < 5)
1325 /* Length (to be filled) */
1326 len_pos = wpabuf_put(resp, 2);
1327 wpabuf_put_u8(resp, srv_proto);
1328 wpabuf_put_u8(resp, srv_trans_id);
1330 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1331 /* Response Data: empty */
1332 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1336 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1337 struct wpabuf *resp, u8 srv_trans_id)
1339 struct p2p_srv_bonjour *bsrv;
1342 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1344 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1345 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1349 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1350 struct p2p_srv_bonjour, list) {
1351 if (wpabuf_tailroom(resp) <
1352 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1354 /* Length (to be filled) */
1355 len_pos = wpabuf_put(resp, 2);
1356 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1357 wpabuf_put_u8(resp, srv_trans_id);
1359 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1360 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1361 wpabuf_head(bsrv->resp),
1362 wpabuf_len(bsrv->resp));
1364 wpabuf_put_buf(resp, bsrv->query); /* Key */
1365 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1366 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1372 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1373 struct wpabuf *resp, u8 srv_trans_id,
1374 const u8 *query, size_t query_len)
1376 struct p2p_srv_bonjour *bsrv;
1380 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1382 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1383 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1384 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1389 if (query_len == 0) {
1390 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1394 if (wpabuf_tailroom(resp) < 5)
1396 /* Length (to be filled) */
1397 len_pos = wpabuf_put(resp, 2);
1398 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1399 wpabuf_put_u8(resp, srv_trans_id);
1401 wpabuf_set(&buf, query, query_len);
1402 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1404 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1408 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1409 /* Response Data: empty */
1410 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1416 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1417 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1418 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1420 if (wpabuf_tailroom(resp) >=
1421 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1423 wpabuf_put_buf(resp, bsrv->query); /* Key */
1424 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1426 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1430 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1431 struct wpabuf *resp, u8 srv_trans_id)
1433 struct p2p_srv_upnp *usrv;
1436 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1438 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1439 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1443 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1444 struct p2p_srv_upnp, list) {
1445 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1448 /* Length (to be filled) */
1449 len_pos = wpabuf_put(resp, 2);
1450 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1451 wpabuf_put_u8(resp, srv_trans_id);
1454 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1456 wpabuf_put_u8(resp, usrv->version);
1457 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1459 wpabuf_put_str(resp, usrv->service);
1460 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1466 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1467 struct wpabuf *resp, u8 srv_trans_id,
1468 const u8 *query, size_t query_len)
1470 struct p2p_srv_upnp *usrv;
1476 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1479 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1480 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1481 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1486 if (query_len == 0) {
1487 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1491 if (wpabuf_tailroom(resp) < 5)
1494 /* Length (to be filled) */
1495 len_pos = wpabuf_put(resp, 2);
1496 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1497 wpabuf_put_u8(resp, srv_trans_id);
1500 str = os_malloc(query_len);
1503 os_memcpy(str, query + 1, query_len - 1);
1504 str[query_len - 1] = '\0';
1506 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1507 struct p2p_srv_upnp, list) {
1508 if (version != usrv->version)
1511 if (os_strcmp(str, "ssdp:all") != 0 &&
1512 os_strstr(usrv->service, str) == NULL)
1515 if (wpabuf_tailroom(resp) < 2)
1519 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1521 wpabuf_put_u8(resp, version);
1523 wpabuf_put_u8(resp, ',');
1527 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1529 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1531 wpabuf_put_str(resp, usrv->service);
1536 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1539 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1540 /* Response Data: empty */
1543 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1547 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1548 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1550 struct wpa_supplicant *wpa_s = ctx;
1551 const u8 *pos = tlvs;
1552 const u8 *end = tlvs + tlvs_len;
1555 struct wpabuf *resp;
1556 u8 srv_proto, srv_trans_id;
1560 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1562 buf_len = 2 * tlvs_len + 1;
1563 buf = os_malloc(buf_len);
1565 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1566 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1568 freq, MAC2STR(sa), dialog_token, update_indic,
1573 if (wpa_s->p2p_sd_over_ctrl_iface)
1574 return; /* to be processed by an external program */
1576 resp = wpabuf_alloc(10000);
1580 while (pos + 1 < end) {
1581 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1582 slen = WPA_GET_LE16(pos);
1584 if (pos + slen > end || slen < 2) {
1585 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1590 tlv_end = pos + slen;
1593 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1595 srv_trans_id = *pos++;
1596 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1599 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1600 pos, tlv_end - pos);
1603 if (wpa_s->force_long_sd) {
1604 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1606 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1607 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1611 switch (srv_proto) {
1612 case P2P_SERV_ALL_SERVICES:
1613 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1614 "for all services");
1615 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1616 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1617 wpa_printf(MSG_DEBUG, "P2P: No service "
1618 "discovery protocols available");
1619 wpas_sd_add_proto_not_avail(
1620 resp, P2P_SERV_ALL_SERVICES,
1624 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1625 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1627 case P2P_SERV_BONJOUR:
1628 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1629 pos, tlv_end - pos);
1632 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1633 pos, tlv_end - pos);
1636 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1637 "protocol %u", srv_proto);
1638 wpas_sd_add_proto_not_avail(resp, srv_proto,
1647 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1648 update_indic, tlvs, tlvs_len);
1650 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1656 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1657 const u8 *tlvs, size_t tlvs_len)
1659 struct wpa_supplicant *wpa_s = ctx;
1660 const u8 *pos = tlvs;
1661 const u8 *end = tlvs + tlvs_len;
1667 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1669 if (tlvs_len > 1500) {
1670 /* TODO: better way for handling this */
1671 wpa_msg_ctrl(wpa_s, MSG_INFO,
1672 P2P_EVENT_SERV_DISC_RESP MACSTR
1673 " %u <long response: %u bytes>",
1674 MAC2STR(sa), update_indic,
1675 (unsigned int) tlvs_len);
1677 buf_len = 2 * tlvs_len + 1;
1678 buf = os_malloc(buf_len);
1680 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1681 wpa_msg_ctrl(wpa_s, MSG_INFO,
1682 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1683 MAC2STR(sa), update_indic, buf);
1689 u8 srv_proto, srv_trans_id, status;
1691 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1692 slen = WPA_GET_LE16(pos);
1694 if (pos + slen > end || slen < 3) {
1695 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1699 tlv_end = pos + slen;
1702 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1704 srv_trans_id = *pos++;
1705 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1708 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1711 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1712 pos, tlv_end - pos);
1717 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1721 void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1722 const struct wpabuf *tlvs)
1724 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1725 return (void *) wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1726 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1728 return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1732 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1733 u8 version, const char *query)
1735 struct wpabuf *tlvs;
1738 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1741 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1742 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1743 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1744 wpabuf_put_u8(tlvs, version);
1745 wpabuf_put_str(tlvs, query);
1746 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1752 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
1754 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1755 return wpa_drv_p2p_sd_cancel_request(wpa_s, (u64) req);
1756 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1758 return p2p_sd_cancel_request(wpa_s->global->p2p, req);
1762 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1763 const u8 *dst, u8 dialog_token,
1764 const struct wpabuf *resp_tlvs)
1766 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1767 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1771 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1773 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1778 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1780 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1781 wpa_drv_p2p_service_update(wpa_s);
1784 if (wpa_s->global->p2p)
1785 p2p_sd_service_update(wpa_s->global->p2p);
1789 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1791 dl_list_del(&bsrv->list);
1792 wpabuf_free(bsrv->query);
1793 wpabuf_free(bsrv->resp);
1798 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1800 dl_list_del(&usrv->list);
1801 os_free(usrv->service);
1806 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1808 struct p2p_srv_bonjour *bsrv, *bn;
1809 struct p2p_srv_upnp *usrv, *un;
1811 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1812 struct p2p_srv_bonjour, list)
1813 wpas_p2p_srv_bonjour_free(bsrv);
1815 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1816 struct p2p_srv_upnp, list)
1817 wpas_p2p_srv_upnp_free(usrv);
1819 wpas_p2p_sd_service_update(wpa_s);
1823 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1824 struct wpabuf *query, struct wpabuf *resp)
1826 struct p2p_srv_bonjour *bsrv;
1828 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1831 wpabuf_free(bsrv->resp);
1836 bsrv = os_zalloc(sizeof(*bsrv));
1839 bsrv->query = query;
1841 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1843 wpas_p2p_sd_service_update(wpa_s);
1848 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1849 const struct wpabuf *query)
1851 struct p2p_srv_bonjour *bsrv;
1853 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1856 wpas_p2p_srv_bonjour_free(bsrv);
1857 wpas_p2p_sd_service_update(wpa_s);
1862 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1863 const char *service)
1865 struct p2p_srv_upnp *usrv;
1867 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1868 return 0; /* Already listed */
1869 usrv = os_zalloc(sizeof(*usrv));
1872 usrv->version = version;
1873 usrv->service = os_strdup(service);
1874 if (usrv->service == NULL) {
1878 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1880 wpas_p2p_sd_service_update(wpa_s);
1885 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1886 const char *service)
1888 struct p2p_srv_upnp *usrv;
1890 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1893 wpas_p2p_srv_upnp_free(usrv);
1894 wpas_p2p_sd_service_update(wpa_s);
1899 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1900 const u8 *peer, const char *params,
1901 unsigned int generated_pin)
1903 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1904 MAC2STR(peer), generated_pin, params);
1908 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1909 const u8 *peer, const char *params)
1911 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1912 MAC2STR(peer), params);
1916 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1917 const u8 *dev_addr, const u8 *pri_dev_type,
1918 const char *dev_name, u16 supp_config_methods,
1919 u8 dev_capab, u8 group_capab)
1921 struct wpa_supplicant *wpa_s = ctx;
1922 char devtype[WPS_DEV_TYPE_BUFSIZE];
1924 u8 empty_dev_type[8];
1925 unsigned int generated_pin = 0;
1927 if (pri_dev_type == NULL) {
1928 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1929 pri_dev_type = empty_dev_type;
1931 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1932 " pri_dev_type=%s name='%s' config_methods=0x%x "
1933 "dev_capab=0x%x group_capab=0x%x",
1935 wps_dev_type_bin2str(pri_dev_type, devtype,
1937 dev_name, supp_config_methods, dev_capab, group_capab);
1938 params[sizeof(params) - 1] = '\0';
1940 if (config_methods & WPS_CONFIG_DISPLAY) {
1941 generated_pin = wps_generate_pin();
1942 wpas_prov_disc_local_display(wpa_s, peer, params,
1944 } else if (config_methods & WPS_CONFIG_KEYPAD)
1945 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1946 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1947 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1948 "%s", MAC2STR(peer), params);
1950 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
1951 P2P_PROV_DISC_SUCCESS,
1952 config_methods, generated_pin);
1956 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1958 struct wpa_supplicant *wpa_s = ctx;
1959 unsigned int generated_pin = 0;
1961 if (config_methods & WPS_CONFIG_DISPLAY)
1962 wpas_prov_disc_local_keypad(wpa_s, peer, "");
1963 else if (config_methods & WPS_CONFIG_KEYPAD) {
1964 generated_pin = wps_generate_pin();
1965 wpas_prov_disc_local_display(wpa_s, peer, "", generated_pin);
1966 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1967 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
1970 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1971 P2P_PROV_DISC_SUCCESS,
1972 config_methods, generated_pin);
1974 if (wpa_s->pending_pd_before_join &&
1975 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1976 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1977 wpa_s->pending_pd_before_join = 0;
1978 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1979 "join-existing-group operation");
1980 wpas_p2p_join_start(wpa_s);
1985 void wpas_prov_disc_fail(void *ctx, const u8 *peer,
1986 enum p2p_prov_disc_status status)
1988 struct wpa_supplicant *wpa_s = ctx;
1990 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1995 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1996 const u8 *go_dev_addr, const u8 *ssid,
1997 size_t ssid_len, int *go, u8 *group_bssid,
1998 int *force_freq, int persistent_group)
2000 struct wpa_supplicant *wpa_s = ctx;
2002 u8 cur_bssid[ETH_ALEN];
2004 struct wpa_supplicant *grp;
2006 if (!persistent_group) {
2007 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2008 " to join an active group", MAC2STR(sa));
2009 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2010 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2012 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2013 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2014 "authorized invitation");
2018 * Do not accept the invitation automatically; notify user and
2021 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2024 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2026 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2027 "running persistent group");
2029 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2033 if (!wpa_s->conf->persistent_reconnect)
2034 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2036 for (s = wpa_s->conf->ssid; s; s = s->next) {
2037 if (s->disabled == 2 &&
2038 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2039 s->ssid_len == ssid_len &&
2040 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2045 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2046 " requested reinvocation of an unknown group",
2048 return P2P_SC_FAIL_UNKNOWN_GROUP;
2051 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2053 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2054 wpa_printf(MSG_DEBUG, "P2P: The only available "
2055 "interface is already in use - reject "
2057 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2059 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2060 } else if (s->mode == WPAS_MODE_P2P_GO) {
2062 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2064 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2065 "interface address for the group");
2066 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2068 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2073 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2074 wpa_s->assoc_freq) {
2075 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2076 "the channel we are already using");
2077 *force_freq = wpa_s->assoc_freq;
2080 res = wpa_drv_shared_freq(wpa_s);
2082 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2083 "with the channel we are already using on a "
2084 "shared interface");
2088 return P2P_SC_SUCCESS;
2092 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2093 const u8 *ssid, size_t ssid_len,
2094 const u8 *go_dev_addr, u8 status,
2097 struct wpa_supplicant *wpa_s = ctx;
2100 for (s = wpa_s->conf->ssid; s; s = s->next) {
2101 if (s->disabled == 2 &&
2102 s->ssid_len == ssid_len &&
2103 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2107 if (status == P2P_SC_SUCCESS) {
2108 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2109 " was accepted; op_freq=%d MHz",
2110 MAC2STR(sa), op_freq);
2112 wpas_p2p_group_add_persistent(
2113 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
2115 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2116 wpa_s->p2p_wps_method);
2121 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2122 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2123 " was rejected (status %u)", MAC2STR(sa), status);
2129 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2130 "sa=" MACSTR " go_dev_addr=" MACSTR
2131 " bssid=" MACSTR " unknown-network",
2132 MAC2STR(sa), MAC2STR(go_dev_addr),
2135 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2136 "sa=" MACSTR " go_dev_addr=" MACSTR
2138 MAC2STR(sa), MAC2STR(go_dev_addr));
2143 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2144 " persistent=%d", MAC2STR(sa), s->id);
2148 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2150 struct wpa_supplicant *wpa_s = ctx;
2151 struct wpa_ssid *ssid;
2154 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2155 "status=%d " MACSTR,
2156 status, MAC2STR(bssid));
2158 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2159 "status=%d ", status);
2161 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2163 if (wpa_s->pending_invite_ssid_id == -1)
2164 return; /* Invitation to active group */
2166 if (status != P2P_SC_SUCCESS) {
2167 wpas_p2p_remove_pending_group_interface(wpa_s);
2171 ssid = wpa_config_get_network(wpa_s->conf,
2172 wpa_s->pending_invite_ssid_id);
2174 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2175 "data matching with invitation");
2179 wpas_p2p_group_add_persistent(wpa_s, ssid,
2180 ssid->mode == WPAS_MODE_P2P_GO, 0);
2184 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2185 struct p2p_channels *chan)
2189 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2192 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2193 chan->reg_class[cla].reg_class = 81;
2194 chan->reg_class[cla].channels = 11;
2195 for (i = 0; i < 11; i++)
2196 chan->reg_class[cla].channel[i] = i + 1;
2199 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2202 /* Operating class 115 - 5 GHz, channels 36-48 */
2203 chan->reg_class[cla].reg_class = 115;
2204 chan->reg_class[cla].channels = 4;
2205 chan->reg_class[cla].channel[0] = 36;
2206 chan->reg_class[cla].channel[1] = 40;
2207 chan->reg_class[cla].channel[2] = 44;
2208 chan->reg_class[cla].channel[3] = 48;
2211 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2214 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2215 chan->reg_class[cla].reg_class = 124;
2216 chan->reg_class[cla].channels = 4;
2217 chan->reg_class[cla].channel[0] = 149;
2218 chan->reg_class[cla].channel[1] = 153;
2219 chan->reg_class[cla].channel[2] = 157;
2220 chan->reg_class[cla].channel[3] = 161;
2223 chan->reg_classes = cla;
2228 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2230 enum hostapd_hw_mode mode)
2234 for (i = 0; i < num_modes; i++) {
2235 if (modes[i].mode == mode)
2243 static int has_channel(struct hostapd_hw_modes *mode, u8 chan, int *flags)
2247 for (i = 0; i < mode->num_channels; i++) {
2248 if (mode->channels[i].chan == chan) {
2250 *flags = mode->channels[i].flag;
2251 return !(mode->channels[i].flag &
2252 (HOSTAPD_CHAN_DISABLED |
2253 HOSTAPD_CHAN_PASSIVE_SCAN |
2254 HOSTAPD_CHAN_NO_IBSS |
2255 HOSTAPD_CHAN_RADAR));
2263 struct p2p_oper_class_map {
2264 enum hostapd_hw_mode mode;
2269 enum { BW20, BW40PLUS, BW40MINUS } bw;
2272 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2273 struct p2p_channels *chan)
2275 struct hostapd_hw_modes *modes, *mode;
2276 u16 num_modes, flags;
2278 struct p2p_oper_class_map op_class[] = {
2279 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2280 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2281 #if 0 /* Do not enable HT40 on 2 GHz for now */
2282 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2283 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2285 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2286 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2287 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2288 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2289 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2290 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2291 { -1, 0, 0, 0, 0, BW20 }
2294 modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
2295 if (modes == NULL) {
2296 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2297 "of all supported channels; assume dualband "
2299 return wpas_p2p_default_channels(wpa_s, chan);
2304 for (op = 0; op_class[op].op_class; op++) {
2305 struct p2p_oper_class_map *o = &op_class[op];
2307 struct p2p_reg_class *reg = NULL;
2309 mode = get_mode(modes, num_modes, o->mode);
2312 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2314 if (!has_channel(mode, ch, &flag))
2316 if (o->bw == BW40MINUS &&
2317 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2318 !has_channel(mode, ch - 4, NULL)))
2320 if (o->bw == BW40PLUS &&
2321 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2322 !has_channel(mode, ch + 4, NULL)))
2325 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2326 "class %u", o->op_class);
2327 reg = &chan->reg_class[cla];
2329 reg->reg_class = o->op_class;
2331 reg->channel[reg->channels] = ch;
2335 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2336 reg->channel, reg->channels);
2340 chan->reg_classes = cla;
2342 ieee80211_sta_free_hw_features(modes, num_modes);
2348 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2351 struct wpa_supplicant *wpa_s = ctx;
2353 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2354 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2360 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2365 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2366 * @global: Pointer to global data from wpa_supplicant_init()
2367 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2368 * Returns: 0 on success, -1 on failure
2370 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2372 struct p2p_config p2p;
2375 #ifdef ANDROID_BRCM_P2P_PATCH
2379 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2382 #ifdef CONFIG_CLIENT_MLME
2383 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
2384 wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
2385 wpa_s->mlme.public_action_cb_ctx = wpa_s;
2387 #endif /* CONFIG_CLIENT_MLME */
2389 if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
2390 wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
2391 /* Continue anyway; this is not really a fatal error */
2397 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2398 struct p2p_params params;
2400 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2401 os_memset(¶ms, 0, sizeof(params));
2402 params.dev_name = wpa_s->conf->device_name;
2403 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2405 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2406 os_memcpy(params.sec_dev_type,
2407 wpa_s->conf->sec_device_type,
2408 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2410 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
2416 os_memset(&p2p, 0, sizeof(p2p));
2417 p2p.msg_ctx = wpa_s;
2419 p2p.p2p_scan = wpas_p2p_scan;
2420 p2p.send_action = wpas_send_action;
2421 p2p.send_action_done = wpas_send_action_done;
2422 p2p.go_neg_completed = wpas_go_neg_completed;
2423 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2424 p2p.dev_found = wpas_dev_found;
2425 p2p.dev_lost = wpas_dev_lost;
2426 p2p.start_listen = wpas_start_listen;
2427 p2p.stop_listen = wpas_stop_listen;
2428 p2p.send_probe_resp = wpas_send_probe_resp;
2429 p2p.sd_request = wpas_sd_request;
2430 p2p.sd_response = wpas_sd_response;
2431 p2p.prov_disc_req = wpas_prov_disc_req;
2432 p2p.prov_disc_resp = wpas_prov_disc_resp;
2433 p2p.prov_disc_fail = wpas_prov_disc_fail;
2434 p2p.invitation_process = wpas_invitation_process;
2435 p2p.invitation_received = wpas_invitation_received;
2436 p2p.invitation_result = wpas_invitation_result;
2437 p2p.get_noa = wpas_get_noa;
2439 #ifdef ANDROID_BRCM_P2P_PATCH
2440 /* P2P_ADDR: Using p2p_dev_addr to hold the actual p2p device address incase if
2441 * we are not using the primary interface for p2p operations.
2443 wpa_drv_driver_cmd(wpa_s, "P2P_DEV_ADDR", buf, sizeof(buf));
2444 os_memcpy(p2p.p2p_dev_addr, buf, ETH_ALEN);
2445 os_memcpy(wpa_s->global->p2p_dev_addr, buf, ETH_ALEN);
2446 os_memcpy(p2p.dev_addr, buf, ETH_ALEN);
2447 wpa_printf(MSG_DEBUG, "P2P: Device address ("MACSTR")", MAC2STR(p2p.p2p_dev_addr));
2449 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2452 os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
2453 p2p.dev_name = wpa_s->conf->device_name;
2454 p2p.manufacturer = wpa_s->conf->manufacturer;
2455 p2p.model_name = wpa_s->conf->model_name;
2456 p2p.model_number = wpa_s->conf->model_number;
2457 p2p.serial_number = wpa_s->conf->serial_number;
2459 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2460 p2p.config_methods = wpa_s->wps->config_methods;
2463 if (wpa_s->conf->p2p_listen_reg_class &&
2464 wpa_s->conf->p2p_listen_channel) {
2465 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2466 p2p.channel = wpa_s->conf->p2p_listen_channel;
2470 * Pick one of the social channels randomly as the listen
2473 os_get_random((u8 *) &r, sizeof(r));
2474 p2p.channel = 1 + (r % 3) * 5;
2476 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2478 if (wpa_s->conf->p2p_oper_reg_class &&
2479 wpa_s->conf->p2p_oper_channel) {
2480 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2481 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2482 p2p.cfg_op_channel = 1;
2483 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2484 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2487 p2p.op_reg_class = 81;
2489 * Use random operation channel from (1, 6, 11) if no other
2490 * preference is indicated.
2492 os_get_random((u8 *) &r, sizeof(r));
2493 p2p.op_channel = 1 + (r % 3) * 5;
2494 p2p.cfg_op_channel = 0;
2495 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2496 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2498 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2499 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2500 p2p.country[2] = 0x04;
2502 os_memcpy(p2p.country, "XX\x04", 3);
2504 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2505 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2510 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2513 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2514 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2515 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2517 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2518 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2520 p2p.max_peers = 100;
2522 if (wpa_s->conf->p2p_ssid_postfix) {
2523 p2p.ssid_postfix_len =
2524 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2525 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2526 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2527 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2528 p2p.ssid_postfix_len);
2531 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2533 global->p2p = p2p_init(&p2p);
2534 if (global->p2p == NULL)
2537 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2538 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2540 p2p_add_wps_vendor_extension(
2541 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2549 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2550 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2552 * This function deinitialize per-interface P2P data.
2554 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2556 if (wpa_s->driver && wpa_s->drv_priv)
2557 wpa_drv_probe_req_report(wpa_s, 0);
2558 os_free(wpa_s->go_params);
2559 wpa_s->go_params = NULL;
2560 wpabuf_free(wpa_s->pending_action_tx);
2561 wpa_s->pending_action_tx = NULL;
2562 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
2563 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2564 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2565 wpa_s->p2p_long_listen = 0;
2566 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2567 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2568 wpas_p2p_remove_pending_group_interface(wpa_s);
2570 /* TODO: remove group interface from the driver if this wpa_s instance
2571 * is on top of a P2P group interface */
2576 * wpas_p2p_deinit_global - Deinitialize global P2P module
2577 * @global: Pointer to global data from wpa_supplicant_init()
2579 * This function deinitializes the global (per device) P2P module.
2581 void wpas_p2p_deinit_global(struct wpa_global *global)
2583 struct wpa_supplicant *wpa_s, *tmp;
2586 if (global->p2p == NULL)
2589 /* Remove remaining P2P group interfaces */
2590 wpa_s = global->ifaces;
2592 wpas_p2p_service_flush(wpa_s);
2593 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2594 wpa_s = wpa_s->next;
2596 enum wpa_driver_if_type type;
2597 tmp = global->ifaces;
2600 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2605 ifname = os_strdup(tmp->ifname);
2606 type = wpas_p2p_if_type(tmp->p2p_group_interface);
2607 wpa_supplicant_remove_iface(global, tmp, 0);
2609 wpa_drv_if_remove(wpa_s, type, ifname);
2614 * Deinit GO data on any possibly remaining interface (if main
2615 * interface is used as GO).
2617 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2618 if (wpa_s->ap_iface)
2619 wpas_p2p_group_deinit(wpa_s);
2622 p2p_deinit(global->p2p);
2627 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2629 if (wpa_s->drv_flags &
2630 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2631 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2632 return 1; /* P2P group requires a new interface in every case
2634 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2635 return 0; /* driver does not support concurrent operations */
2636 if (wpa_s->global->ifaces->next)
2637 return 1; /* more that one interface already in use */
2638 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2639 return 1; /* this interface is already in use */
2644 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2645 const u8 *peer_addr,
2646 enum p2p_wps_method wps_method,
2647 int go_intent, const u8 *own_interface_addr,
2648 unsigned int force_freq, int persistent_group)
2650 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2651 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2652 go_intent, own_interface_addr,
2653 force_freq, persistent_group);
2656 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2657 go_intent, own_interface_addr, force_freq,
2662 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2663 const u8 *peer_addr,
2664 enum p2p_wps_method wps_method,
2665 int go_intent, const u8 *own_interface_addr,
2666 unsigned int force_freq, int persistent_group)
2668 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2671 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2672 go_intent, own_interface_addr, force_freq,
2677 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2679 wpa_s->p2p_join_scan_count++;
2680 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2681 wpa_s->p2p_join_scan_count);
2682 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2683 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2684 " for join operationg - stop join attempt",
2685 MAC2STR(wpa_s->pending_join_iface_addr));
2686 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2687 wpa_msg(wpa_s->parent, MSG_INFO,
2688 P2P_EVENT_GROUP_FORMATION_FAILURE);
2693 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2694 struct wpa_scan_results *scan_res)
2696 struct wpa_bss *bss;
2698 u8 iface_addr[ETH_ALEN];
2700 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2702 if (wpa_s->global->p2p_disabled)
2705 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
2706 scan_res ? (int) scan_res->num : -1);
2709 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2711 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2712 wpa_s->pending_join_iface_addr);
2714 p2p_get_interface_addr(wpa_s->global->p2p,
2715 wpa_s->pending_join_dev_addr,
2717 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2719 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2720 "address for join from " MACSTR " to " MACSTR
2721 " based on newly discovered P2P peer entry",
2722 MAC2STR(wpa_s->pending_join_iface_addr),
2723 MAC2STR(iface_addr));
2724 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2727 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2728 wpa_s->pending_join_iface_addr);
2731 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2732 "from P2P peer table: %d MHz", freq);
2734 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2737 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2738 "from BSS table: %d MHz", freq);
2743 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2744 "prior to joining an existing group (GO " MACSTR
2746 MAC2STR(wpa_s->pending_join_dev_addr), freq);
2747 wpa_s->pending_pd_before_join = 1;
2749 switch (wpa_s->pending_join_wps_method) {
2751 case WPS_PIN_DISPLAY:
2752 method = WPS_CONFIG_KEYPAD;
2754 case WPS_PIN_KEYPAD:
2755 method = WPS_CONFIG_DISPLAY;
2758 method = WPS_CONFIG_PUSHBUTTON;
2765 if (p2p_prov_disc_req(wpa_s->global->p2p,
2766 wpa_s->pending_join_dev_addr, method, 1)
2768 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2769 "Discovery Request before joining an "
2771 wpa_s->pending_pd_before_join = 0;
2776 * Actual join operation will be started from the Action frame
2777 * TX status callback.
2782 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
2783 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2784 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2785 wpas_p2p_check_join_scan_limit(wpa_s);
2789 /* Start join operation immediately */
2790 wpas_p2p_join_start(wpa_s);
2794 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
2796 struct wpa_supplicant *wpa_s = eloop_ctx;
2798 struct wpa_driver_scan_params params;
2799 struct wpabuf *wps_ie, *ies;
2801 os_memset(¶ms, 0, sizeof(params));
2803 /* P2P Wildcard SSID */
2804 params.num_ssids = 1;
2805 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2806 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2808 wpa_s->wps->dev.p2p = 1;
2809 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
2810 WPS_REQ_ENROLLEE, 0, NULL);
2811 if (wps_ie == NULL) {
2812 wpas_p2p_scan_res_join(wpa_s, NULL);
2816 ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
2818 wpabuf_free(wps_ie);
2819 wpas_p2p_scan_res_join(wpa_s, NULL);
2822 wpabuf_put_buf(ies, wps_ie);
2823 wpabuf_free(wps_ie);
2825 p2p_scan_ie(wpa_s->global->p2p, ies);
2827 params.extra_ies = wpabuf_head(ies);
2828 params.extra_ies_len = wpabuf_len(ies);
2831 * Run a scan to update BSS table and start Provision Discovery once
2832 * the new scan results become available.
2834 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
2835 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
2836 ret = ieee80211_sta_req_scan(wpa_s, ¶ms);
2838 ret = wpa_drv_scan(wpa_s, ¶ms);
2843 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
2845 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2846 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2847 wpas_p2p_check_join_scan_limit(wpa_s);
2852 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2853 const u8 *dev_addr, enum p2p_wps_method wps_method)
2855 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2856 MACSTR " dev " MACSTR ")",
2857 MAC2STR(iface_addr), MAC2STR(dev_addr));
2859 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2860 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2861 wpa_s->pending_join_wps_method = wps_method;
2863 /* Make sure we are not running find during connection establishment */
2864 wpas_p2p_stop_find(wpa_s);
2866 wpa_s->p2p_join_scan_count = 0;
2867 wpas_p2p_join_scan(wpa_s, NULL);
2872 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2874 struct wpa_supplicant *group;
2875 struct p2p_go_neg_results res;
2877 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2880 if (group != wpa_s) {
2881 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2882 sizeof(group->p2p_pin));
2883 group->p2p_wps_method = wpa_s->p2p_wps_method;
2886 group->p2p_in_provisioning = 1;
2888 os_memset(&res, 0, sizeof(res));
2889 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2891 res.wps_method = wpa_s->pending_join_wps_method;
2892 wpas_start_wps_enrollee(group, &res);
2895 * Allow a longer timeout for join-a-running-group than normal 15
2896 * second group formation timeout since the GO may not have authorized
2897 * our connection yet.
2899 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2900 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
2908 * wpas_p2p_connect - Request P2P Group Formation to be started
2909 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2910 * @peer_addr: Address of the peer P2P Device
2911 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2912 * @persistent_group: Whether to create a persistent group
2913 * @join: Whether to join an existing group (as a client) instead of starting
2914 * Group Owner negotiation; @peer_addr is BSSID in that case
2915 * @auth: Whether to only authorize the connection instead of doing that and
2916 * initiating Group Owner negotiation
2917 * @go_intent: GO Intent or -1 to use default
2918 * @freq: Frequency for the group or 0 for auto-selection
2919 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2920 * failure, -2 on failure due to channel not currently available,
2921 * -3 if forced channel is not supported
2923 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2924 const char *pin, enum p2p_wps_method wps_method,
2925 int persistent_group, int join, int auth, int go_intent,
2928 int force_freq = 0, oper_freq = 0;
2931 enum wpa_driver_if_type iftype;
2933 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2937 go_intent = wpa_s->conf->p2p_go_intent;
2940 wpa_s->p2p_long_listen = 0;
2942 wpa_s->p2p_wps_method = wps_method;
2945 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
2946 else if (wps_method == WPS_PIN_DISPLAY) {
2947 ret = wps_generate_pin();
2948 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
2950 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
2953 wpa_s->p2p_pin[0] = '\0';
2956 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
2958 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
2959 "connect a running group from " MACSTR,
2960 MAC2STR(peer_addr));
2961 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
2964 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
2965 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
2967 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
2968 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
2971 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) <
2977 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2979 oper_freq = wpa_s->assoc_freq;
2981 oper_freq = wpa_drv_shared_freq(wpa_s);
2987 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
2988 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
2989 "(%u MHz) is not supported for P2P uses",
2994 if (oper_freq > 0 && freq != oper_freq &&
2995 !(wpa_s->drv_flags &
2996 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2997 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2998 "on %u MHz while connected on another "
2999 "channel (%u MHz)", freq, oper_freq);
3002 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3003 "requested channel (%u MHz)", freq);
3005 } else if (oper_freq > 0 &&
3006 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3007 if (!(wpa_s->drv_flags &
3008 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3009 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3010 "while connected on non-P2P supported "
3011 "channel (%u MHz)", oper_freq);
3014 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3015 "(%u MHz) not available for P2P - try to use "
3016 "another channel", oper_freq);
3018 } else if (oper_freq > 0) {
3019 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3020 "channel we are already using (%u MHz) on another "
3021 "interface", oper_freq);
3022 force_freq = oper_freq;
3025 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3027 if (!wpa_s->create_p2p_iface) {
3029 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3030 go_intent, wpa_s->own_addr,
3031 force_freq, persistent_group)
3036 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3037 go_intent, wpa_s->own_addr,
3038 force_freq, persistent_group) < 0)
3043 /* Prepare to add a new interface for the group */
3044 iftype = WPA_IF_P2P_GROUP;
3046 iftype = WPA_IF_P2P_CLIENT;
3047 else if (go_intent == 15)
3048 iftype = WPA_IF_P2P_GO;
3049 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3050 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3051 "interface for the group");
3056 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3058 wpa_s->pending_interface_addr,
3059 force_freq, persistent_group) < 0)
3063 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
3064 wpa_s->pending_interface_addr,
3065 force_freq, persistent_group) < 0) {
3066 wpas_p2p_remove_pending_group_interface(wpa_s);
3074 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3075 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3076 * @freq: Frequency of the channel in MHz
3077 * @duration: Duration of the stay on the channel in milliseconds
3079 * This callback is called when the driver indicates that it has started the
3080 * requested remain-on-channel duration.
3082 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3083 unsigned int freq, unsigned int duration)
3085 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3087 wpa_s->roc_waiting_drv_freq = 0;
3088 wpa_s->off_channel_freq = freq;
3089 wpas_send_action_cb(wpa_s, NULL);
3090 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3091 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3092 wpa_s->pending_listen_duration);
3093 wpa_s->pending_listen_freq = 0;
3098 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3099 unsigned int timeout)
3101 /* Limit maximum Listen state time based on driver limitation. */
3102 if (timeout > wpa_s->max_remain_on_chan)
3103 timeout = wpa_s->max_remain_on_chan;
3105 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3106 return wpa_drv_p2p_listen(wpa_s, timeout);
3108 return p2p_listen(wpa_s->global->p2p, timeout);
3113 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3114 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3115 * @freq: Frequency of the channel in MHz
3117 * This callback is called when the driver indicates that a remain-on-channel
3118 * operation has been completed, i.e., the duration on the requested channel
3121 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3124 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3125 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3126 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
3127 wpa_s->off_channel_freq = 0;
3128 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3130 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3131 return; /* P2P module started a new operation */
3132 if (wpa_s->pending_action_tx)
3134 if (wpa_s->p2p_long_listen > 0)
3135 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3136 if (wpa_s->p2p_long_listen > 0) {
3137 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3138 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3144 * wpas_p2p_group_remove - Remove a P2P group
3145 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3146 * @ifname: Network interface name of the group interface or "*" to remove all
3148 * Returns: 0 on success, -1 on failure
3150 * This function is used to remove a P2P group. This can be used to disconnect
3151 * from a group in which the local end is a P2P Client or to end a P2P Group in
3152 * case the local end is the Group Owner. If a virtual network interface was
3153 * created for this group, that interface will be removed. Otherwise, only the
3154 * configured P2P group network will be removed from the interface.
3156 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3158 struct wpa_global *global = wpa_s->global;
3160 if (os_strcmp(ifname, "*") == 0) {
3161 struct wpa_supplicant *prev;
3162 wpa_s = global->ifaces;
3165 wpa_s = wpa_s->next;
3166 wpas_p2p_disconnect(prev);
3171 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3172 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3176 return wpas_p2p_disconnect(wpa_s);
3180 static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3181 struct p2p_go_neg_results *params,
3187 os_memset(params, 0, sizeof(*params));
3188 params->role_go = 1;
3190 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3191 "frequency %d MHz", freq);
3192 params->freq = freq;
3193 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3194 wpa_s->conf->p2p_oper_channel >= 1 &&
3195 wpa_s->conf->p2p_oper_channel <= 11) {
3196 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3197 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3198 "frequency %d MHz", params->freq);
3199 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3200 wpa_s->conf->p2p_oper_reg_class == 124) {
3201 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3202 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3203 "frequency %d MHz", params->freq);
3204 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3205 wpa_s->best_overall_freq > 0 &&
3206 p2p_supported_freq(wpa_s->global->p2p,
3207 wpa_s->best_overall_freq)) {
3208 params->freq = wpa_s->best_overall_freq;
3209 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3210 "channel %d MHz", params->freq);
3211 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3212 wpa_s->best_24_freq > 0 &&
3213 p2p_supported_freq(wpa_s->global->p2p,
3214 wpa_s->best_24_freq)) {
3215 params->freq = wpa_s->best_24_freq;
3216 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3217 "channel %d MHz", params->freq);
3218 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3219 wpa_s->best_5_freq > 0 &&
3220 p2p_supported_freq(wpa_s->global->p2p,
3221 wpa_s->best_5_freq)) {
3222 params->freq = wpa_s->best_5_freq;
3223 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3224 "channel %d MHz", params->freq);
3226 params->freq = 2412;
3227 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3228 "known)", params->freq);
3231 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3232 wpa_s->assoc_freq && !freq) {
3233 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3235 params->freq = wpa_s->assoc_freq;
3238 res = wpa_drv_shared_freq(wpa_s);
3239 if (res > 0 && !freq) {
3240 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3241 "already using on a shared interface");
3247 static struct wpa_supplicant *
3248 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3251 struct wpa_supplicant *group_wpa_s;
3253 if (!wpas_p2p_create_iface(wpa_s))
3256 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3257 WPA_IF_P2P_CLIENT) < 0)
3259 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3260 if (group_wpa_s == NULL) {
3261 wpas_p2p_remove_pending_group_interface(wpa_s);
3270 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3271 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3272 * @persistent_group: Whether to create a persistent group
3273 * @freq: Frequency for the group or 0 to indicate no hardcoding
3274 * Returns: 0 on success, -1 on failure
3276 * This function creates a new P2P group with the local end as the Group Owner,
3277 * i.e., without using Group Owner Negotiation.
3279 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3282 struct p2p_go_neg_results params;
3285 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3289 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3291 if (wpa_s->best_24_freq > 0 &&
3292 p2p_supported_freq(wpa_s->global->p2p,
3293 wpa_s->best_24_freq)) {
3294 freq = wpa_s->best_24_freq;
3295 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3296 "channel: %d MHz", freq);
3298 os_get_random((u8 *) &r, sizeof(r));
3299 freq = 2412 + (r % 3) * 25;
3300 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3301 "channel: %d MHz", freq);
3306 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3308 if (wpa_s->best_5_freq > 0 &&
3309 p2p_supported_freq(wpa_s->global->p2p,
3310 wpa_s->best_5_freq)) {
3311 freq = wpa_s->best_5_freq;
3312 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3313 "channel: %d MHz", freq);
3315 os_get_random((u8 *) &r, sizeof(r));
3316 freq = 5180 + (r % 4) * 20;
3317 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3318 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3319 "5 GHz channel for P2P group");
3322 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3323 "channel: %d MHz", freq);
3327 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3328 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3329 "(%u MHz) is not supported for P2P uses",
3334 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
3335 p2p_go_params(wpa_s->global->p2p, ¶ms);
3336 params.persistent_group = persistent_group;
3338 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3341 wpas_start_wps_go(wpa_s, ¶ms, 0);
3347 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3348 struct wpa_ssid *params, int addr_allocated)
3350 struct wpa_ssid *ssid;
3352 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3356 wpa_supplicant_ap_deinit(wpa_s);
3358 ssid = wpa_config_add_network(wpa_s->conf);
3361 wpa_config_set_network_defaults(ssid);
3362 ssid->temporary = 1;
3363 ssid->proto = WPA_PROTO_RSN;
3364 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3365 ssid->group_cipher = WPA_CIPHER_CCMP;
3366 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3367 ssid->ssid = os_malloc(params->ssid_len);
3368 if (ssid->ssid == NULL) {
3369 wpa_config_remove_network(wpa_s->conf, ssid->id);
3372 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3373 ssid->ssid_len = params->ssid_len;
3374 ssid->p2p_group = 1;
3375 ssid->export_keys = 1;
3376 if (params->psk_set) {
3377 os_memcpy(ssid->psk, params->psk, 32);
3380 if (params->passphrase)
3381 ssid->passphrase = os_strdup(params->passphrase);
3383 wpa_supplicant_select_network(wpa_s, ssid);
3385 wpa_s->show_group_started = 1;
3391 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3392 struct wpa_ssid *ssid, int addr_allocated,
3395 struct p2p_go_neg_results params;
3398 if (ssid->disabled != 2 || ssid->ssid == NULL)
3401 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3402 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3403 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3408 /* Make sure we are not running find during connection establishment */
3409 wpas_p2p_stop_find(wpa_s);
3411 if (ssid->mode == WPAS_MODE_INFRA)
3412 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3414 if (ssid->mode != WPAS_MODE_P2P_GO)
3417 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
3420 if (ssid->passphrase == NULL ||
3421 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3422 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3426 os_strlcpy(params.passphrase, ssid->passphrase,
3427 sizeof(params.passphrase));
3428 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3429 params.ssid_len = ssid->ssid_len;
3430 params.persistent_group = 1;
3432 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3436 wpas_start_wps_go(wpa_s, ¶ms, 0);
3442 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3443 struct wpabuf *proberesp_ies)
3445 struct wpa_supplicant *wpa_s = ctx;
3446 if (wpa_s->ap_iface) {
3447 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
3449 wpabuf_free(hapd->p2p_beacon_ie);
3450 hapd->p2p_beacon_ie = beacon_ies;
3452 wpabuf_free(hapd->p2p_probe_resp_ie);
3453 hapd->p2p_probe_resp_ie = proberesp_ies;
3455 wpabuf_free(beacon_ies);
3456 wpabuf_free(proberesp_ies);
3458 wpa_supplicant_ap_update_beacon(wpa_s);
3462 static void wpas_p2p_idle_update(void *ctx, int idle)
3464 struct wpa_supplicant *wpa_s = ctx;
3465 if (!wpa_s->ap_iface)
3467 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3469 wpas_p2p_set_group_idle_timeout(wpa_s);
3471 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3475 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
3476 int persistent_group,
3477 int group_formation)
3479 struct p2p_group *group;
3480 struct p2p_group_config *cfg;
3482 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3484 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3487 cfg = os_zalloc(sizeof(*cfg));
3491 cfg->persistent_group = persistent_group;
3492 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3493 if (wpa_s->max_stations &&
3494 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3495 cfg->max_clients = wpa_s->max_stations;
3497 cfg->max_clients = wpa_s->conf->max_num_sta;
3498 cfg->cb_ctx = wpa_s;
3499 cfg->ie_update = wpas_p2p_ie_update;
3500 cfg->idle_update = wpas_p2p_idle_update;
3502 group = p2p_group_init(wpa_s->global->p2p, cfg);
3505 if (!group_formation)
3506 p2p_group_notif_formation_done(group);
3507 wpa_s->p2p_group = group;
3512 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3515 if (!wpa_s->p2p_in_provisioning) {
3516 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3517 "provisioning not in progress");
3521 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3523 if (wpa_s->global->p2p)
3524 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3525 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3526 wpa_drv_wps_success_cb(wpa_s, peer_addr);
3527 wpas_group_formation_completed(wpa_s, 1);
3531 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
3532 struct wps_event_fail *fail)
3534 if (!wpa_s->p2p_in_provisioning) {
3535 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
3536 "provisioning not in progress");
3539 wpas_notify_p2p_wps_failed(wpa_s, fail);
3543 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3544 const char *config_method)
3548 if (os_strcmp(config_method, "display") == 0)
3549 config_methods = WPS_CONFIG_DISPLAY;
3550 else if (os_strcmp(config_method, "keypad") == 0)
3551 config_methods = WPS_CONFIG_KEYPAD;
3552 else if (os_strcmp(config_method, "pbc") == 0 ||
3553 os_strcmp(config_method, "pushbutton") == 0)
3554 config_methods = WPS_CONFIG_PUSHBUTTON;
3558 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3559 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
3563 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
3566 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
3571 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3574 return p2p_scan_result_text(ies, ies_len, buf, end);
3578 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3580 if (!wpa_s->pending_action_tx)
3583 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3584 "operation request");
3585 wpabuf_free(wpa_s->pending_action_tx);
3586 wpa_s->pending_action_tx = NULL;
3590 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3591 enum p2p_discovery_type type,
3592 unsigned int num_req_dev_types, const u8 *req_dev_types)
3594 wpas_p2p_clear_pending_action_tx(wpa_s);
3595 wpa_s->p2p_long_listen = 0;
3597 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3598 return wpa_drv_p2p_find(wpa_s, timeout, type);
3600 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3603 return p2p_find(wpa_s->global->p2p, timeout, type,
3604 num_req_dev_types, req_dev_types);
3608 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3610 wpas_p2p_clear_pending_action_tx(wpa_s);
3611 wpa_s->p2p_long_listen = 0;
3612 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3613 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3615 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3616 wpa_drv_p2p_stop_find(wpa_s);
3620 if (wpa_s->global->p2p)
3621 p2p_stop_find(wpa_s->global->p2p);
3623 wpas_p2p_remove_pending_group_interface(wpa_s);
3627 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3629 struct wpa_supplicant *wpa_s = eloop_ctx;
3630 wpa_s->p2p_long_listen = 0;
3634 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3638 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3641 wpas_p2p_clear_pending_action_tx(wpa_s);
3645 * This is a request for unlimited Listen state. However, at
3646 * least for now, this is mapped to a Listen state for one
3651 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3652 wpa_s->p2p_long_listen = 0;
3654 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
3655 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
3656 wpa_s->p2p_long_listen = timeout * 1000;
3657 eloop_register_timeout(timeout, 0,
3658 wpas_p2p_long_listen_timeout,
3666 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3667 u8 *buf, size_t len, int p2p_group)
3669 struct wpabuf *p2p_ie;
3672 if (wpa_s->global->p2p_disabled)
3674 if (wpa_s->global->p2p == NULL)
3679 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
3680 #ifdef ANDROID_BRCM_P2P_PATCH
3681 if (p2p_ie == NULL) return -1;
3683 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
3685 wpabuf_free(p2p_ie);
3691 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
3692 const u8 *ie, size_t ie_len)
3694 if (wpa_s->global->p2p_disabled)
3696 if (wpa_s->global->p2p == NULL)
3699 return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len);
3703 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
3704 const u8 *sa, const u8 *bssid,
3705 u8 category, const u8 *data, size_t len, int freq)
3707 if (wpa_s->global->p2p_disabled)
3709 if (wpa_s->global->p2p == NULL)
3712 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
3717 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
3719 if (wpa_s->global->p2p_disabled)
3721 if (wpa_s->global->p2p == NULL)
3724 p2p_scan_ie(wpa_s->global->p2p, ies);
3728 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
3730 p2p_group_deinit(wpa_s->p2p_group);
3731 wpa_s->p2p_group = NULL;
3735 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
3737 wpa_s->p2p_long_listen = 0;
3739 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3740 return wpa_drv_p2p_reject(wpa_s, addr);
3742 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3745 return p2p_reject(wpa_s->global->p2p, addr);
3749 /* Invite to reinvoke a persistent group */
3750 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3751 struct wpa_ssid *ssid, const u8 *go_dev_addr)
3753 enum p2p_invite_role role;
3755 #ifdef ANDROID_BRCM_P2P_PATCH
3759 if (ssid->mode == WPAS_MODE_P2P_GO) {
3760 role = P2P_INVITE_ROLE_GO;
3761 if (peer_addr == NULL) {
3762 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
3763 "address in invitation command");
3767 #ifdef ANDROID_BRCM_P2P_PATCH
3768 wpa_printf(MSG_DEBUG, "P2P: Check to see if already runnig persistent wpa_s %p grp ssid %s ssid_len %d", wpa_s, ssid->ssid, ssid->ssid_len);
3769 if(wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go)) {
3770 wpa_printf(MSG_DEBUG, "P2P: We are already running persistent group");
3772 bssid = wpa_s->own_addr;
3774 wpa_printf(MSG_DEBUG, "P2P: We are running persistent group but go is not set");
3776 wpa_printf(MSG_DEBUG, "P2P: We are NOT already running persistent group");
3779 if (wpas_p2p_create_iface(wpa_s)) {
3780 if (wpas_p2p_add_group_interface(wpa_s,
3781 WPA_IF_P2P_GO) < 0) {
3782 wpa_printf(MSG_ERROR, "P2P: Failed to "
3783 "allocate a new interface for the "
3787 bssid = wpa_s->pending_interface_addr;
3789 bssid = wpa_s->own_addr;
3790 #ifdef ANDROID_BRCM_P2P_PATCH
3794 role = P2P_INVITE_ROLE_CLIENT;
3795 peer_addr = ssid->bssid;
3797 wpa_s->pending_invite_ssid_id = ssid->id;
3799 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3800 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
3801 ssid->ssid, ssid->ssid_len,
3804 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3807 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3808 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
3812 /* Invite to join an active group */
3813 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
3814 const u8 *peer_addr, const u8 *go_dev_addr)
3816 struct wpa_global *global = wpa_s->global;
3817 enum p2p_invite_role role;
3819 struct wpa_ssid *ssid;
3821 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3822 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3825 if (wpa_s == NULL) {
3826 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
3830 ssid = wpa_s->current_ssid;
3832 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
3837 if (ssid->mode == WPAS_MODE_P2P_GO) {
3838 role = P2P_INVITE_ROLE_ACTIVE_GO;
3839 bssid = wpa_s->own_addr;
3840 if (go_dev_addr == NULL)
3841 go_dev_addr = wpa_s->parent->own_addr;
3843 role = P2P_INVITE_ROLE_CLIENT;
3844 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
3845 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
3846 "invite to current group");
3849 bssid = wpa_s->bssid;
3850 if (go_dev_addr == NULL &&
3851 !is_zero_ether_addr(wpa_s->go_dev_addr))
3852 go_dev_addr = wpa_s->go_dev_addr;
3854 wpa_s->parent->pending_invite_ssid_id = -1;
3856 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3857 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
3858 ssid->ssid, ssid->ssid_len,
3861 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3864 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3865 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
3870 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
3872 struct wpa_ssid *ssid = wpa_s->current_ssid;
3873 const char *ssid_txt;
3874 u8 go_dev_addr[ETH_ALEN];
3875 int network_id = -1;
3878 if (!wpa_s->show_group_started || !ssid)
3881 wpa_s->show_group_started = 0;
3883 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
3884 os_memset(go_dev_addr, 0, ETH_ALEN);
3885 if (ssid->bssid_set)
3886 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
3887 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3889 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
3891 if (wpa_s->global->p2p_group_formation == wpa_s)
3892 wpa_s->global->p2p_group_formation = NULL;
3894 if (ssid->passphrase == NULL && ssid->psk_set) {
3896 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
3897 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3898 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3900 #ifdef ANDROID_BRCM_P2P_PATCH
3901 wpa_s->ifname, ssid_txt, wpa_s->current_bss->freq, psk,
3903 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
3905 MAC2STR(go_dev_addr),
3906 persistent ? " [PERSISTENT]" : "");
3908 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3909 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3910 "go_dev_addr=" MACSTR "%s",
3911 #ifdef ANDROID_BRCM_P2P_PATCH
3912 wpa_s->ifname, ssid_txt, wpa_s->current_bss->freq,
3914 wpa_s->ifname, ssid_txt, ssid->frequency,
3916 ssid->passphrase ? ssid->passphrase : "",
3917 MAC2STR(go_dev_addr),
3918 persistent ? " [PERSISTENT]" : "");
3922 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
3925 network_id = ssid->id;
3926 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
3930 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
3931 u32 interval1, u32 duration2, u32 interval2)
3933 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3935 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3938 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3939 wpa_s->current_ssid == NULL ||
3940 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
3943 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
3944 wpa_s->own_addr, wpa_s->assoc_freq,
3945 duration1, interval1, duration2, interval2);
3949 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
3950 unsigned int interval)
3952 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3955 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3958 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
3962 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
3964 struct wpa_supplicant *wpa_s = eloop_ctx;
3966 if (wpa_s->conf->p2p_group_idle == 0) {
3967 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
3972 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
3974 wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
3975 wpas_p2p_group_delete(wpa_s);
3979 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
3981 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3982 if (wpa_s->conf->p2p_group_idle == 0)
3985 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
3988 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
3989 wpa_s->conf->p2p_group_idle);
3990 eloop_register_timeout(wpa_s->conf->p2p_group_idle, 0,
3991 wpas_p2p_group_idle_timeout, wpa_s, NULL);
3995 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3996 u16 reason_code, const u8 *ie, size_t ie_len)
3998 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4000 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4003 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
4006 #ifdef ANDROID_BRCM_P2P_PATCH
4007 void wpas_p2p_group_remove_notif(struct wpa_supplicant *wpa_s, u16 reason_code)
4009 if(wpa_s->global->p2p_disabled)
4012 /* If we are running a P2P Client and we received a Deauth/Disassoc from the Go, then remove
4013 the virutal interface on which the client is running. */
4014 if((wpa_s != wpa_s->parent) && (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) && (wpa_s->key_mgmt != WPA_KEY_MGMT_WPS)) {
4016 wpa_printf(MSG_DEBUG, "P2P: [EVENT_DEAUTH] Removing P2P_CLIENT virtual intf.");
4017 wpa_supplicant_cancel_scan(wpa_s);
4018 wpas_p2p_interface_unavailable(wpa_s);
4023 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4024 u16 reason_code, const u8 *ie, size_t ie_len)
4026 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4028 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4031 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
4035 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4037 struct p2p_data *p2p = wpa_s->global->p2p;
4042 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4045 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4046 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4048 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4049 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4052 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4053 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4055 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4056 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4058 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4059 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4060 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4061 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4062 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4065 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4066 p2p_set_sec_dev_types(p2p,
4067 (void *) wpa_s->conf->sec_device_type,
4068 wpa_s->conf->num_sec_device_types);
4070 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4072 p2p_remove_wps_vendor_extensions(p2p);
4073 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4074 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4076 p2p_add_wps_vendor_extension(
4077 p2p, wpa_s->conf->wps_vendor_ext[i]);
4081 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4082 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4084 country[0] = wpa_s->conf->country[0];
4085 country[1] = wpa_s->conf->country[1];
4087 p2p_set_country(p2p, country);
4090 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4091 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4092 wpa_s->conf->p2p_ssid_postfix ?
4093 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4097 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4098 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
4100 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4101 u8 reg_class, channel;
4104 if (wpa_s->conf->p2p_listen_reg_class &&
4105 wpa_s->conf->p2p_listen_channel) {
4106 reg_class = wpa_s->conf->p2p_listen_reg_class;
4107 channel = wpa_s->conf->p2p_listen_channel;
4111 * Pick one of the social channels randomly as the
4114 os_get_random((u8 *) &r, sizeof(r));
4115 channel = 1 + (r % 3) * 5;
4117 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4119 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4122 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4123 u8 op_reg_class, op_channel, cfg_op_channel;
4126 if (wpa_s->conf->p2p_oper_reg_class &&
4127 wpa_s->conf->p2p_oper_channel) {
4128 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4129 op_channel = wpa_s->conf->p2p_oper_channel;
4134 * Use random operation channel from (1, 6, 11)
4135 *if no other preference is indicated.
4137 os_get_random((u8 *) &r, sizeof(r));
4138 op_channel = 1 + (r % 3) * 5;
4141 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4144 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4150 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4153 if (!wpa_s->ap_iface)
4155 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4160 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4162 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4164 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4167 wpa_s->global->cross_connection = enabled;
4168 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4171 struct wpa_supplicant *iface;
4173 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4175 if (iface->cross_connect_enabled == 0)
4178 iface->cross_connect_enabled = 0;
4179 iface->cross_connect_in_use = 0;
4180 wpa_msg(iface->parent, MSG_INFO,
4181 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4182 iface->ifname, iface->cross_connect_uplink);
4190 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4192 struct wpa_supplicant *iface;
4194 if (!uplink->global->cross_connection)
4197 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4198 if (!iface->cross_connect_enabled)
4200 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4203 if (iface->ap_iface == NULL)
4205 if (iface->cross_connect_in_use)
4208 iface->cross_connect_in_use = 1;
4209 wpa_msg(iface->parent, MSG_INFO,
4210 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4211 iface->ifname, iface->cross_connect_uplink);
4216 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4218 struct wpa_supplicant *iface;
4220 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4221 if (!iface->cross_connect_enabled)
4223 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4226 if (!iface->cross_connect_in_use)
4229 wpa_msg(iface->parent, MSG_INFO,
4230 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4231 iface->ifname, iface->cross_connect_uplink);
4232 iface->cross_connect_in_use = 0;
4237 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4239 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4240 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4241 wpa_s->cross_connect_disallowed)
4242 wpas_p2p_disable_cross_connect(wpa_s);
4244 wpas_p2p_enable_cross_connect(wpa_s);
4245 if (!wpa_s->ap_iface)
4246 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4250 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4252 wpas_p2p_disable_cross_connect(wpa_s);
4253 if (!wpa_s->ap_iface)
4254 wpas_p2p_set_group_idle_timeout(wpa_s);
4258 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4260 struct wpa_supplicant *iface;
4262 if (!wpa_s->global->cross_connection)
4265 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4268 if (iface->drv_flags &
4269 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4271 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4274 wpa_s->cross_connect_enabled = 1;
4275 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4276 sizeof(wpa_s->cross_connect_uplink));
4277 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4278 "%s to %s whenever uplink is available",
4279 wpa_s->ifname, wpa_s->cross_connect_uplink);
4281 if (iface->ap_iface || iface->current_ssid == NULL ||
4282 iface->current_ssid->mode != WPAS_MODE_INFRA ||
4283 iface->cross_connect_disallowed ||
4284 iface->wpa_state != WPA_COMPLETED)
4287 wpa_s->cross_connect_in_use = 1;
4288 wpa_msg(wpa_s->parent, MSG_INFO,
4289 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4290 wpa_s->ifname, wpa_s->cross_connect_uplink);
4296 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
4298 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
4299 !wpa_s->p2p_in_provisioning)
4300 return 0; /* not P2P client operation */
4302 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
4304 if (wpa_s != wpa_s->parent)
4305 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
4307 if (wpa_s->global->p2p)
4308 p2p_group_formation_failed(wpa_s->global->p2p);
4310 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4311 wpa_s->parent, NULL);
4313 wpas_group_formation_completed(wpa_s, 0);
4318 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4320 struct p2p_channels chan;
4322 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4325 os_memset(&chan, 0, sizeof(chan));
4326 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
4327 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
4332 p2p_update_channel_list(wpa_s->global->p2p, &chan);
4336 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
4338 struct wpa_global *global = wpa_s->global;
4342 if (global->p2p == NULL)
4345 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
4347 if (wpa_s->pending_interface_name[0] &&
4348 !is_zero_ether_addr(wpa_s->pending_interface_addr))
4351 peer = p2p_get_go_neg_peer(global->p2p);
4353 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
4354 MACSTR, MAC2STR(peer));
4355 p2p_unauthorize(global->p2p, peer);
4358 wpas_p2p_stop_find(wpa_s);
4360 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4361 if (wpa_s == global->p2p_group_formation &&
4362 (wpa_s->p2p_in_provisioning ||
4363 wpa_s->parent->pending_interface_type ==
4364 WPA_IF_P2P_CLIENT)) {
4365 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
4366 "formation found - cancelling",
4369 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4370 wpa_s->parent, NULL);
4371 wpas_p2p_group_delete(wpa_s);
4377 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
4385 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
4387 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4390 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
4391 "being available anymore");
4392 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
4393 wpas_p2p_group_delete(wpa_s);
4397 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
4398 int freq_24, int freq_5, int freq_overall)
4400 struct p2p_data *p2p = wpa_s->global->p2p;
4401 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4403 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
4407 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
4410 struct p2p_data *p2p = wpa_s->global->p2p;
4412 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4415 if (hwaddr_aton(addr, peer))
4418 return p2p_unauthorize(p2p, peer);
4423 * wpas_p2p_disconnect - Disconnect from a P2P Group
4424 * @wpa_s: Pointer to wpa_supplicant data
4425 * Returns: 0 on success, -1 on failure
4427 * This can be used to disconnect from a group in which the local end is a P2P
4428 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4429 * virtual network interface was created for this group, that interface will be
4430 * removed. Otherwise, only the configured P2P group network will be removed
4431 * from the interface.
4433 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
4439 wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
4440 wpas_p2p_group_delete(wpa_s);