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);
1021 #ifdef ANDROID_BRCM_P2P_PATCH
1023 * Monitor interface name is derived from p2p interface name
1024 * We need to reset p2p interface name early to take care of extra character in monitor interface name
1026 if (os_strlen(ifname) + os_strlen(WPA_MONITOR_IFNAME_PREFIX) >= IFNAMSIZ &&
1028 if (os_strlen(ifname) >= IFNAMSIZ &&
1030 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1031 /* Try to avoid going over the IFNAMSIZ length limit */
1032 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1033 wpa_s->p2p_group_idx);
1035 force_ifname[0] = '\0';
1037 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1039 wpa_s->p2p_group_idx++;
1041 wpa_s->pending_interface_type = type;
1042 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1043 wpa_s->pending_interface_addr, NULL) < 0) {
1044 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1049 if (force_ifname[0]) {
1050 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1052 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1053 sizeof(wpa_s->pending_interface_name));
1055 os_strlcpy(wpa_s->pending_interface_name, ifname,
1056 sizeof(wpa_s->pending_interface_name));
1057 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1058 MACSTR, wpa_s->pending_interface_name,
1059 MAC2STR(wpa_s->pending_interface_addr));
1065 static void wpas_p2p_remove_pending_group_interface(
1066 struct wpa_supplicant *wpa_s)
1068 if (!wpa_s->pending_interface_name[0] ||
1069 is_zero_ether_addr(wpa_s->pending_interface_addr))
1070 return; /* No pending virtual interface */
1072 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1073 wpa_s->pending_interface_name);
1074 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1075 wpa_s->pending_interface_name);
1076 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1077 wpa_s->pending_interface_name[0] = '\0';
1081 static struct wpa_supplicant *
1082 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1084 struct wpa_interface iface;
1085 struct wpa_supplicant *group_wpa_s;
1087 if (!wpa_s->pending_interface_name[0]) {
1088 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1089 if (!wpas_p2p_create_iface(wpa_s))
1092 * Something has forced us to remove the pending interface; try
1093 * to create a new one and hope for the best that we will get
1094 * the same local address.
1096 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1097 WPA_IF_P2P_CLIENT) < 0)
1101 os_memset(&iface, 0, sizeof(iface));
1102 iface.ifname = wpa_s->pending_interface_name;
1103 iface.driver = wpa_s->driver->name;
1104 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1105 iface.driver_param = wpa_s->conf->driver_param;
1106 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1107 if (group_wpa_s == NULL) {
1108 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1109 "wpa_supplicant interface");
1112 wpa_s->pending_interface_name[0] = '\0';
1113 group_wpa_s->parent = wpa_s;
1114 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1115 P2P_GROUP_INTERFACE_CLIENT;
1116 wpa_s->global->p2p_group_formation = group_wpa_s;
1118 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1124 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1127 struct wpa_supplicant *wpa_s = eloop_ctx;
1128 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1129 if (wpa_s->global->p2p)
1130 p2p_group_formation_failed(wpa_s->global->p2p);
1131 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1132 wpa_drv_p2p_group_formation_failed(wpa_s);
1133 wpas_group_formation_completed(wpa_s, 0);
1137 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1139 struct wpa_supplicant *wpa_s = ctx;
1141 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1142 wpa_drv_cancel_remain_on_channel(wpa_s);
1143 wpa_s->off_channel_freq = 0;
1144 wpa_s->roc_waiting_drv_freq = 0;
1148 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1150 wpas_notify_p2p_go_neg_completed(wpa_s, res->status);
1151 wpas_p2p_remove_pending_group_interface(wpa_s);
1155 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1156 wpas_notify_p2p_go_neg_completed(wpa_s, P2P_SC_SUCCESS);
1158 if (wpa_s->create_p2p_iface) {
1159 struct wpa_supplicant *group_wpa_s =
1160 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1161 if (group_wpa_s == NULL) {
1162 wpas_p2p_remove_pending_group_interface(wpa_s);
1165 if (group_wpa_s != wpa_s) {
1166 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1167 sizeof(group_wpa_s->p2p_pin));
1168 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1170 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1171 wpa_s->pending_interface_name[0] = '\0';
1172 group_wpa_s->p2p_in_provisioning = 1;
1175 wpas_start_wps_go(group_wpa_s, res, 1);
1177 wpas_start_wps_enrollee(group_wpa_s, res);
1179 wpa_s->p2p_in_provisioning = 1;
1180 wpa_s->global->p2p_group_formation = wpa_s;
1183 wpas_start_wps_go(wpa_s, res, 1);
1185 wpas_start_wps_enrollee(ctx, res);
1188 wpa_s->p2p_long_listen = 0;
1189 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1191 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1192 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1193 (res->peer_config_timeout % 100) * 10000,
1194 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1198 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1200 struct wpa_supplicant *wpa_s = ctx;
1201 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1202 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1204 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1208 void wpas_dev_found(void *ctx, const u8 *addr,
1209 const struct p2p_peer_info *info,
1212 struct wpa_supplicant *wpa_s = ctx;
1213 char devtype[WPS_DEV_TYPE_BUFSIZE];
1215 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1216 " p2p_dev_addr=" MACSTR
1217 " pri_dev_type=%s name='%s' config_methods=0x%x "
1218 "dev_capab=0x%x group_capab=0x%x",
1219 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1220 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1222 info->device_name, info->config_methods,
1223 info->dev_capab, info->group_capab);
1225 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1229 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1231 struct wpa_supplicant *wpa_s = ctx;
1232 #ifdef ANDROID_BRCM_P2P_PATCH
1233 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1234 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1236 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1240 static int wpas_start_listen(void *ctx, unsigned int freq,
1241 unsigned int duration,
1242 const struct wpabuf *probe_resp_ie)
1244 struct wpa_supplicant *wpa_s = ctx;
1246 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1248 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1249 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1250 "report received Probe Request frames");
1254 wpa_s->pending_listen_freq = freq;
1255 wpa_s->pending_listen_duration = duration;
1257 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1258 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1259 "to remain on channel (%u MHz) for Listen "
1261 wpa_s->pending_listen_freq = 0;
1264 wpa_s->off_channel_freq = 0;
1265 wpa_s->roc_waiting_drv_freq = freq;
1271 static void wpas_stop_listen(void *ctx)
1273 struct wpa_supplicant *wpa_s = ctx;
1274 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1275 wpa_drv_cancel_remain_on_channel(wpa_s);
1276 wpa_s->off_channel_freq = 0;
1277 wpa_s->roc_waiting_drv_freq = 0;
1279 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1280 wpa_drv_probe_req_report(wpa_s, 0);
1284 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1286 struct wpa_supplicant *wpa_s = ctx;
1287 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
1291 static struct p2p_srv_bonjour *
1292 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1293 const struct wpabuf *query)
1295 struct p2p_srv_bonjour *bsrv;
1298 len = wpabuf_len(query);
1299 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1300 struct p2p_srv_bonjour, list) {
1301 if (len == wpabuf_len(bsrv->query) &&
1302 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1310 static struct p2p_srv_upnp *
1311 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1312 const char *service)
1314 struct p2p_srv_upnp *usrv;
1316 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1317 struct p2p_srv_upnp, list) {
1318 if (version == usrv->version &&
1319 os_strcmp(service, usrv->service) == 0)
1326 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1331 if (wpabuf_tailroom(resp) < 5)
1334 /* Length (to be filled) */
1335 len_pos = wpabuf_put(resp, 2);
1336 wpabuf_put_u8(resp, srv_proto);
1337 wpabuf_put_u8(resp, srv_trans_id);
1339 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1340 /* Response Data: empty */
1341 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1345 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1346 struct wpabuf *resp, u8 srv_trans_id)
1348 struct p2p_srv_bonjour *bsrv;
1351 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1353 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1354 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1358 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1359 struct p2p_srv_bonjour, list) {
1360 if (wpabuf_tailroom(resp) <
1361 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1363 /* Length (to be filled) */
1364 len_pos = wpabuf_put(resp, 2);
1365 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1366 wpabuf_put_u8(resp, srv_trans_id);
1368 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1369 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1370 wpabuf_head(bsrv->resp),
1371 wpabuf_len(bsrv->resp));
1373 wpabuf_put_buf(resp, bsrv->query); /* Key */
1374 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1375 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1381 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1382 struct wpabuf *resp, u8 srv_trans_id,
1383 const u8 *query, size_t query_len)
1385 struct p2p_srv_bonjour *bsrv;
1389 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1391 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1392 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1393 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1398 if (query_len == 0) {
1399 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1403 if (wpabuf_tailroom(resp) < 5)
1405 /* Length (to be filled) */
1406 len_pos = wpabuf_put(resp, 2);
1407 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1408 wpabuf_put_u8(resp, srv_trans_id);
1410 wpabuf_set(&buf, query, query_len);
1411 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1413 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1417 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1418 /* Response Data: empty */
1419 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1425 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1426 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1427 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1429 if (wpabuf_tailroom(resp) >=
1430 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1432 wpabuf_put_buf(resp, bsrv->query); /* Key */
1433 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1435 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1439 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1440 struct wpabuf *resp, u8 srv_trans_id)
1442 struct p2p_srv_upnp *usrv;
1445 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1447 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1448 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1452 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1453 struct p2p_srv_upnp, list) {
1454 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1457 /* Length (to be filled) */
1458 len_pos = wpabuf_put(resp, 2);
1459 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1460 wpabuf_put_u8(resp, srv_trans_id);
1463 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1465 wpabuf_put_u8(resp, usrv->version);
1466 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1468 wpabuf_put_str(resp, usrv->service);
1469 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1475 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1476 struct wpabuf *resp, u8 srv_trans_id,
1477 const u8 *query, size_t query_len)
1479 struct p2p_srv_upnp *usrv;
1485 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1488 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1489 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1490 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1495 if (query_len == 0) {
1496 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1500 if (wpabuf_tailroom(resp) < 5)
1503 /* Length (to be filled) */
1504 len_pos = wpabuf_put(resp, 2);
1505 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1506 wpabuf_put_u8(resp, srv_trans_id);
1509 str = os_malloc(query_len);
1512 os_memcpy(str, query + 1, query_len - 1);
1513 str[query_len - 1] = '\0';
1515 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1516 struct p2p_srv_upnp, list) {
1517 if (version != usrv->version)
1520 if (os_strcmp(str, "ssdp:all") != 0 &&
1521 os_strstr(usrv->service, str) == NULL)
1524 if (wpabuf_tailroom(resp) < 2)
1528 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1530 wpabuf_put_u8(resp, version);
1532 wpabuf_put_u8(resp, ',');
1536 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1538 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1540 wpabuf_put_str(resp, usrv->service);
1545 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1548 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1549 /* Response Data: empty */
1552 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1556 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1557 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1559 struct wpa_supplicant *wpa_s = ctx;
1560 const u8 *pos = tlvs;
1561 const u8 *end = tlvs + tlvs_len;
1564 struct wpabuf *resp;
1565 u8 srv_proto, srv_trans_id;
1569 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1571 buf_len = 2 * tlvs_len + 1;
1572 buf = os_malloc(buf_len);
1574 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1575 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1577 freq, MAC2STR(sa), dialog_token, update_indic,
1582 if (wpa_s->p2p_sd_over_ctrl_iface)
1583 return; /* to be processed by an external program */
1585 resp = wpabuf_alloc(10000);
1589 while (pos + 1 < end) {
1590 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1591 slen = WPA_GET_LE16(pos);
1593 if (pos + slen > end || slen < 2) {
1594 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1599 tlv_end = pos + slen;
1602 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1604 srv_trans_id = *pos++;
1605 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1608 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1609 pos, tlv_end - pos);
1612 if (wpa_s->force_long_sd) {
1613 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1615 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1616 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1620 switch (srv_proto) {
1621 case P2P_SERV_ALL_SERVICES:
1622 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1623 "for all services");
1624 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1625 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1626 wpa_printf(MSG_DEBUG, "P2P: No service "
1627 "discovery protocols available");
1628 wpas_sd_add_proto_not_avail(
1629 resp, P2P_SERV_ALL_SERVICES,
1633 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1634 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1636 case P2P_SERV_BONJOUR:
1637 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1638 pos, tlv_end - pos);
1641 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1642 pos, tlv_end - pos);
1645 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1646 "protocol %u", srv_proto);
1647 wpas_sd_add_proto_not_avail(resp, srv_proto,
1656 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1657 update_indic, tlvs, tlvs_len);
1659 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1665 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1666 const u8 *tlvs, size_t tlvs_len)
1668 struct wpa_supplicant *wpa_s = ctx;
1669 const u8 *pos = tlvs;
1670 const u8 *end = tlvs + tlvs_len;
1676 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1678 if (tlvs_len > 1500) {
1679 /* TODO: better way for handling this */
1680 wpa_msg_ctrl(wpa_s, MSG_INFO,
1681 P2P_EVENT_SERV_DISC_RESP MACSTR
1682 " %u <long response: %u bytes>",
1683 MAC2STR(sa), update_indic,
1684 (unsigned int) tlvs_len);
1686 buf_len = 2 * tlvs_len + 1;
1687 buf = os_malloc(buf_len);
1689 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1690 wpa_msg_ctrl(wpa_s, MSG_INFO,
1691 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1692 MAC2STR(sa), update_indic, buf);
1698 u8 srv_proto, srv_trans_id, status;
1700 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1701 slen = WPA_GET_LE16(pos);
1703 if (pos + slen > end || slen < 3) {
1704 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1708 tlv_end = pos + slen;
1711 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1713 srv_trans_id = *pos++;
1714 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1717 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1720 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1721 pos, tlv_end - pos);
1726 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1730 void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1731 const struct wpabuf *tlvs)
1733 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1734 return (void *) wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1735 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1737 return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1741 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1742 u8 version, const char *query)
1744 struct wpabuf *tlvs;
1747 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1750 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1751 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1752 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1753 wpabuf_put_u8(tlvs, version);
1754 wpabuf_put_str(tlvs, query);
1755 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1761 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
1763 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1764 return wpa_drv_p2p_sd_cancel_request(wpa_s, (u64) req);
1765 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1767 return p2p_sd_cancel_request(wpa_s->global->p2p, req);
1771 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1772 const u8 *dst, u8 dialog_token,
1773 const struct wpabuf *resp_tlvs)
1775 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1776 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1780 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1782 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1787 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1789 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1790 wpa_drv_p2p_service_update(wpa_s);
1793 if (wpa_s->global->p2p)
1794 p2p_sd_service_update(wpa_s->global->p2p);
1798 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1800 dl_list_del(&bsrv->list);
1801 wpabuf_free(bsrv->query);
1802 wpabuf_free(bsrv->resp);
1807 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1809 dl_list_del(&usrv->list);
1810 os_free(usrv->service);
1815 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1817 struct p2p_srv_bonjour *bsrv, *bn;
1818 struct p2p_srv_upnp *usrv, *un;
1820 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1821 struct p2p_srv_bonjour, list)
1822 wpas_p2p_srv_bonjour_free(bsrv);
1824 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1825 struct p2p_srv_upnp, list)
1826 wpas_p2p_srv_upnp_free(usrv);
1828 wpas_p2p_sd_service_update(wpa_s);
1832 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1833 struct wpabuf *query, struct wpabuf *resp)
1835 struct p2p_srv_bonjour *bsrv;
1837 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1840 wpabuf_free(bsrv->resp);
1845 bsrv = os_zalloc(sizeof(*bsrv));
1848 bsrv->query = query;
1850 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1852 wpas_p2p_sd_service_update(wpa_s);
1857 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1858 const struct wpabuf *query)
1860 struct p2p_srv_bonjour *bsrv;
1862 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1865 wpas_p2p_srv_bonjour_free(bsrv);
1866 wpas_p2p_sd_service_update(wpa_s);
1871 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1872 const char *service)
1874 struct p2p_srv_upnp *usrv;
1876 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1877 return 0; /* Already listed */
1878 usrv = os_zalloc(sizeof(*usrv));
1881 usrv->version = version;
1882 usrv->service = os_strdup(service);
1883 if (usrv->service == NULL) {
1887 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1889 wpas_p2p_sd_service_update(wpa_s);
1894 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1895 const char *service)
1897 struct p2p_srv_upnp *usrv;
1899 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1902 wpas_p2p_srv_upnp_free(usrv);
1903 wpas_p2p_sd_service_update(wpa_s);
1908 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1909 const u8 *peer, const char *params,
1910 unsigned int generated_pin)
1912 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1913 MAC2STR(peer), generated_pin, params);
1917 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1918 const u8 *peer, const char *params)
1920 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1921 MAC2STR(peer), params);
1925 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1926 const u8 *dev_addr, const u8 *pri_dev_type,
1927 const char *dev_name, u16 supp_config_methods,
1928 u8 dev_capab, u8 group_capab)
1930 struct wpa_supplicant *wpa_s = ctx;
1931 char devtype[WPS_DEV_TYPE_BUFSIZE];
1933 u8 empty_dev_type[8];
1934 unsigned int generated_pin = 0;
1936 if (pri_dev_type == NULL) {
1937 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1938 pri_dev_type = empty_dev_type;
1940 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1941 " pri_dev_type=%s name='%s' config_methods=0x%x "
1942 "dev_capab=0x%x group_capab=0x%x",
1944 wps_dev_type_bin2str(pri_dev_type, devtype,
1946 dev_name, supp_config_methods, dev_capab, group_capab);
1947 params[sizeof(params) - 1] = '\0';
1949 if (config_methods & WPS_CONFIG_DISPLAY) {
1950 generated_pin = wps_generate_pin();
1951 wpas_prov_disc_local_display(wpa_s, peer, params,
1953 } else if (config_methods & WPS_CONFIG_KEYPAD)
1954 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1955 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1956 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1957 "%s", MAC2STR(peer), params);
1959 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
1960 P2P_PROV_DISC_SUCCESS,
1961 config_methods, generated_pin);
1965 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1967 struct wpa_supplicant *wpa_s = ctx;
1968 unsigned int generated_pin = 0;
1970 if (config_methods & WPS_CONFIG_DISPLAY)
1971 wpas_prov_disc_local_keypad(wpa_s, peer, "");
1972 else if (config_methods & WPS_CONFIG_KEYPAD) {
1973 generated_pin = wps_generate_pin();
1974 wpas_prov_disc_local_display(wpa_s, peer, "", generated_pin);
1975 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1976 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
1979 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1980 P2P_PROV_DISC_SUCCESS,
1981 config_methods, generated_pin);
1983 if (wpa_s->pending_pd_before_join &&
1984 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1985 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1986 wpa_s->pending_pd_before_join = 0;
1987 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1988 "join-existing-group operation");
1989 wpas_p2p_join_start(wpa_s);
1994 void wpas_prov_disc_fail(void *ctx, const u8 *peer,
1995 enum p2p_prov_disc_status status)
1997 struct wpa_supplicant *wpa_s = ctx;
1999 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2004 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2005 const u8 *go_dev_addr, const u8 *ssid,
2006 size_t ssid_len, int *go, u8 *group_bssid,
2007 int *force_freq, int persistent_group)
2009 struct wpa_supplicant *wpa_s = ctx;
2011 u8 cur_bssid[ETH_ALEN];
2013 struct wpa_supplicant *grp;
2015 if (!persistent_group) {
2016 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2017 " to join an active group", MAC2STR(sa));
2018 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2019 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2021 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2022 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2023 "authorized invitation");
2027 * Do not accept the invitation automatically; notify user and
2030 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2033 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2035 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2036 "running persistent group");
2038 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2042 if (!wpa_s->conf->persistent_reconnect)
2043 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2045 for (s = wpa_s->conf->ssid; s; s = s->next) {
2046 if (s->disabled == 2 &&
2047 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2048 s->ssid_len == ssid_len &&
2049 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2054 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2055 " requested reinvocation of an unknown group",
2057 return P2P_SC_FAIL_UNKNOWN_GROUP;
2060 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2062 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2063 wpa_printf(MSG_DEBUG, "P2P: The only available "
2064 "interface is already in use - reject "
2066 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2068 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2069 } else if (s->mode == WPAS_MODE_P2P_GO) {
2071 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2073 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2074 "interface address for the group");
2075 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2077 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2082 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2083 wpa_s->assoc_freq) {
2084 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2085 "the channel we are already using");
2086 *force_freq = wpa_s->assoc_freq;
2089 res = wpa_drv_shared_freq(wpa_s);
2091 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2092 "with the channel we are already using on a "
2093 "shared interface");
2097 return P2P_SC_SUCCESS;
2101 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2102 const u8 *ssid, size_t ssid_len,
2103 const u8 *go_dev_addr, u8 status,
2106 struct wpa_supplicant *wpa_s = ctx;
2109 for (s = wpa_s->conf->ssid; s; s = s->next) {
2110 if (s->disabled == 2 &&
2111 s->ssid_len == ssid_len &&
2112 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2116 if (status == P2P_SC_SUCCESS) {
2117 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2118 " was accepted; op_freq=%d MHz",
2119 MAC2STR(sa), op_freq);
2121 wpas_p2p_group_add_persistent(
2122 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
2124 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2125 wpa_s->p2p_wps_method);
2130 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2131 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2132 " was rejected (status %u)", MAC2STR(sa), status);
2138 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2139 "sa=" MACSTR " go_dev_addr=" MACSTR
2140 " bssid=" MACSTR " unknown-network",
2141 MAC2STR(sa), MAC2STR(go_dev_addr),
2144 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2145 "sa=" MACSTR " go_dev_addr=" MACSTR
2147 MAC2STR(sa), MAC2STR(go_dev_addr));
2152 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2153 " persistent=%d", MAC2STR(sa), s->id);
2157 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2159 struct wpa_supplicant *wpa_s = ctx;
2160 struct wpa_ssid *ssid;
2163 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2164 "status=%d " MACSTR,
2165 status, MAC2STR(bssid));
2167 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2168 "status=%d ", status);
2170 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2172 if (wpa_s->pending_invite_ssid_id == -1)
2173 return; /* Invitation to active group */
2175 if (status != P2P_SC_SUCCESS) {
2176 wpas_p2p_remove_pending_group_interface(wpa_s);
2180 ssid = wpa_config_get_network(wpa_s->conf,
2181 wpa_s->pending_invite_ssid_id);
2183 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2184 "data matching with invitation");
2188 wpas_p2p_group_add_persistent(wpa_s, ssid,
2189 ssid->mode == WPAS_MODE_P2P_GO, 0);
2193 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2194 struct p2p_channels *chan)
2198 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2201 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2202 chan->reg_class[cla].reg_class = 81;
2203 chan->reg_class[cla].channels = 11;
2204 for (i = 0; i < 11; i++)
2205 chan->reg_class[cla].channel[i] = i + 1;
2208 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2211 /* Operating class 115 - 5 GHz, channels 36-48 */
2212 chan->reg_class[cla].reg_class = 115;
2213 chan->reg_class[cla].channels = 4;
2214 chan->reg_class[cla].channel[0] = 36;
2215 chan->reg_class[cla].channel[1] = 40;
2216 chan->reg_class[cla].channel[2] = 44;
2217 chan->reg_class[cla].channel[3] = 48;
2220 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2223 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2224 chan->reg_class[cla].reg_class = 124;
2225 chan->reg_class[cla].channels = 4;
2226 chan->reg_class[cla].channel[0] = 149;
2227 chan->reg_class[cla].channel[1] = 153;
2228 chan->reg_class[cla].channel[2] = 157;
2229 chan->reg_class[cla].channel[3] = 161;
2232 chan->reg_classes = cla;
2237 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2239 enum hostapd_hw_mode mode)
2243 for (i = 0; i < num_modes; i++) {
2244 if (modes[i].mode == mode)
2252 static int has_channel(struct hostapd_hw_modes *mode, u8 chan, int *flags)
2256 for (i = 0; i < mode->num_channels; i++) {
2257 if (mode->channels[i].chan == chan) {
2259 *flags = mode->channels[i].flag;
2260 return !(mode->channels[i].flag &
2261 (HOSTAPD_CHAN_DISABLED |
2262 HOSTAPD_CHAN_PASSIVE_SCAN |
2263 HOSTAPD_CHAN_NO_IBSS |
2264 HOSTAPD_CHAN_RADAR));
2272 struct p2p_oper_class_map {
2273 enum hostapd_hw_mode mode;
2278 enum { BW20, BW40PLUS, BW40MINUS } bw;
2281 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2282 struct p2p_channels *chan)
2284 struct hostapd_hw_modes *modes, *mode;
2285 u16 num_modes, flags;
2287 struct p2p_oper_class_map op_class[] = {
2288 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2289 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2290 #if 0 /* Do not enable HT40 on 2 GHz for now */
2291 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2292 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2294 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2295 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2296 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2297 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2298 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2299 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2300 { -1, 0, 0, 0, 0, BW20 }
2303 modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
2304 if (modes == NULL) {
2305 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2306 "of all supported channels; assume dualband "
2308 return wpas_p2p_default_channels(wpa_s, chan);
2313 for (op = 0; op_class[op].op_class; op++) {
2314 struct p2p_oper_class_map *o = &op_class[op];
2316 struct p2p_reg_class *reg = NULL;
2318 mode = get_mode(modes, num_modes, o->mode);
2321 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2323 if (!has_channel(mode, ch, &flag))
2325 if (o->bw == BW40MINUS &&
2326 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2327 !has_channel(mode, ch - 4, NULL)))
2329 if (o->bw == BW40PLUS &&
2330 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2331 !has_channel(mode, ch + 4, NULL)))
2334 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2335 "class %u", o->op_class);
2336 reg = &chan->reg_class[cla];
2338 reg->reg_class = o->op_class;
2340 reg->channel[reg->channels] = ch;
2344 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2345 reg->channel, reg->channels);
2349 chan->reg_classes = cla;
2351 ieee80211_sta_free_hw_features(modes, num_modes);
2357 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2360 struct wpa_supplicant *wpa_s = ctx;
2362 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2363 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2369 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2374 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2375 * @global: Pointer to global data from wpa_supplicant_init()
2376 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2377 * Returns: 0 on success, -1 on failure
2379 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2381 struct p2p_config p2p;
2384 #ifdef ANDROID_BRCM_P2P_PATCH
2388 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2391 #ifdef CONFIG_CLIENT_MLME
2392 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
2393 wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
2394 wpa_s->mlme.public_action_cb_ctx = wpa_s;
2396 #endif /* CONFIG_CLIENT_MLME */
2398 if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
2399 wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
2400 /* Continue anyway; this is not really a fatal error */
2406 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2407 struct p2p_params params;
2409 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2410 os_memset(¶ms, 0, sizeof(params));
2411 params.dev_name = wpa_s->conf->device_name;
2412 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2414 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2415 os_memcpy(params.sec_dev_type,
2416 wpa_s->conf->sec_device_type,
2417 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2419 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
2425 os_memset(&p2p, 0, sizeof(p2p));
2426 p2p.msg_ctx = wpa_s;
2428 p2p.p2p_scan = wpas_p2p_scan;
2429 p2p.send_action = wpas_send_action;
2430 p2p.send_action_done = wpas_send_action_done;
2431 p2p.go_neg_completed = wpas_go_neg_completed;
2432 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2433 p2p.dev_found = wpas_dev_found;
2434 p2p.dev_lost = wpas_dev_lost;
2435 p2p.start_listen = wpas_start_listen;
2436 p2p.stop_listen = wpas_stop_listen;
2437 p2p.send_probe_resp = wpas_send_probe_resp;
2438 p2p.sd_request = wpas_sd_request;
2439 p2p.sd_response = wpas_sd_response;
2440 p2p.prov_disc_req = wpas_prov_disc_req;
2441 p2p.prov_disc_resp = wpas_prov_disc_resp;
2442 p2p.prov_disc_fail = wpas_prov_disc_fail;
2443 p2p.invitation_process = wpas_invitation_process;
2444 p2p.invitation_received = wpas_invitation_received;
2445 p2p.invitation_result = wpas_invitation_result;
2446 p2p.get_noa = wpas_get_noa;
2448 #ifdef ANDROID_BRCM_P2P_PATCH
2449 /* P2P_ADDR: Using p2p_dev_addr to hold the actual p2p device address incase if
2450 * we are not using the primary interface for p2p operations.
2452 wpa_drv_driver_cmd(wpa_s, "P2P_DEV_ADDR", buf, sizeof(buf));
2453 os_memcpy(p2p.p2p_dev_addr, buf, ETH_ALEN);
2454 os_memcpy(wpa_s->global->p2p_dev_addr, buf, ETH_ALEN);
2455 os_memcpy(p2p.dev_addr, buf, ETH_ALEN);
2456 wpa_printf(MSG_DEBUG, "P2P: Device address ("MACSTR")", MAC2STR(p2p.p2p_dev_addr));
2458 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2461 os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
2462 p2p.dev_name = wpa_s->conf->device_name;
2463 p2p.manufacturer = wpa_s->conf->manufacturer;
2464 p2p.model_name = wpa_s->conf->model_name;
2465 p2p.model_number = wpa_s->conf->model_number;
2466 p2p.serial_number = wpa_s->conf->serial_number;
2468 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2469 p2p.config_methods = wpa_s->wps->config_methods;
2472 if (wpa_s->conf->p2p_listen_reg_class &&
2473 wpa_s->conf->p2p_listen_channel) {
2474 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2475 p2p.channel = wpa_s->conf->p2p_listen_channel;
2479 * Pick one of the social channels randomly as the listen
2482 os_get_random((u8 *) &r, sizeof(r));
2483 p2p.channel = 1 + (r % 3) * 5;
2485 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2487 if (wpa_s->conf->p2p_oper_reg_class &&
2488 wpa_s->conf->p2p_oper_channel) {
2489 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2490 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2491 p2p.cfg_op_channel = 1;
2492 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2493 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2496 p2p.op_reg_class = 81;
2498 * Use random operation channel from (1, 6, 11) if no other
2499 * preference is indicated.
2501 os_get_random((u8 *) &r, sizeof(r));
2502 p2p.op_channel = 1 + (r % 3) * 5;
2503 p2p.cfg_op_channel = 0;
2504 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2505 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2507 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2508 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2509 p2p.country[2] = 0x04;
2511 os_memcpy(p2p.country, "XX\x04", 3);
2513 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2514 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2519 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2522 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2523 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2524 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2526 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2527 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2529 p2p.max_peers = 100;
2531 if (wpa_s->conf->p2p_ssid_postfix) {
2532 p2p.ssid_postfix_len =
2533 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2534 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2535 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2536 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2537 p2p.ssid_postfix_len);
2540 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2542 global->p2p = p2p_init(&p2p);
2543 if (global->p2p == NULL)
2546 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2547 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2549 p2p_add_wps_vendor_extension(
2550 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2558 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2559 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2561 * This function deinitialize per-interface P2P data.
2563 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2565 if (wpa_s->driver && wpa_s->drv_priv)
2566 wpa_drv_probe_req_report(wpa_s, 0);
2567 os_free(wpa_s->go_params);
2568 wpa_s->go_params = NULL;
2569 wpabuf_free(wpa_s->pending_action_tx);
2570 wpa_s->pending_action_tx = NULL;
2571 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
2572 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2573 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2574 wpa_s->p2p_long_listen = 0;
2575 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2576 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2577 wpas_p2p_remove_pending_group_interface(wpa_s);
2579 /* TODO: remove group interface from the driver if this wpa_s instance
2580 * is on top of a P2P group interface */
2585 * wpas_p2p_deinit_global - Deinitialize global P2P module
2586 * @global: Pointer to global data from wpa_supplicant_init()
2588 * This function deinitializes the global (per device) P2P module.
2590 void wpas_p2p_deinit_global(struct wpa_global *global)
2592 struct wpa_supplicant *wpa_s, *tmp;
2595 if (global->p2p == NULL)
2598 /* Remove remaining P2P group interfaces */
2599 wpa_s = global->ifaces;
2601 wpas_p2p_service_flush(wpa_s);
2602 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2603 wpa_s = wpa_s->next;
2605 enum wpa_driver_if_type type;
2606 tmp = global->ifaces;
2609 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2614 ifname = os_strdup(tmp->ifname);
2615 type = wpas_p2p_if_type(tmp->p2p_group_interface);
2616 wpa_supplicant_remove_iface(global, tmp, 0);
2618 wpa_drv_if_remove(wpa_s, type, ifname);
2623 * Deinit GO data on any possibly remaining interface (if main
2624 * interface is used as GO).
2626 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2627 if (wpa_s->ap_iface)
2628 wpas_p2p_group_deinit(wpa_s);
2631 p2p_deinit(global->p2p);
2636 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2638 if (wpa_s->drv_flags &
2639 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2640 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2641 return 1; /* P2P group requires a new interface in every case
2643 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2644 return 0; /* driver does not support concurrent operations */
2645 if (wpa_s->global->ifaces->next)
2646 return 1; /* more that one interface already in use */
2647 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2648 return 1; /* this interface is already in use */
2653 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2654 const u8 *peer_addr,
2655 enum p2p_wps_method wps_method,
2656 int go_intent, const u8 *own_interface_addr,
2657 unsigned int force_freq, int persistent_group)
2659 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2660 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2661 go_intent, own_interface_addr,
2662 force_freq, persistent_group);
2665 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2666 go_intent, own_interface_addr, force_freq,
2671 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2672 const u8 *peer_addr,
2673 enum p2p_wps_method wps_method,
2674 int go_intent, const u8 *own_interface_addr,
2675 unsigned int force_freq, int persistent_group)
2677 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2680 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2681 go_intent, own_interface_addr, force_freq,
2686 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2688 wpa_s->p2p_join_scan_count++;
2689 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2690 wpa_s->p2p_join_scan_count);
2691 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2692 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2693 " for join operationg - stop join attempt",
2694 MAC2STR(wpa_s->pending_join_iface_addr));
2695 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2696 wpa_msg(wpa_s->parent, MSG_INFO,
2697 P2P_EVENT_GROUP_FORMATION_FAILURE);
2702 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2703 struct wpa_scan_results *scan_res)
2705 struct wpa_bss *bss;
2707 u8 iface_addr[ETH_ALEN];
2709 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2711 if (wpa_s->global->p2p_disabled)
2714 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
2715 scan_res ? (int) scan_res->num : -1);
2718 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2720 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2721 wpa_s->pending_join_iface_addr);
2723 p2p_get_interface_addr(wpa_s->global->p2p,
2724 wpa_s->pending_join_dev_addr,
2726 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2728 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2729 "address for join from " MACSTR " to " MACSTR
2730 " based on newly discovered P2P peer entry",
2731 MAC2STR(wpa_s->pending_join_iface_addr),
2732 MAC2STR(iface_addr));
2733 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2736 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2737 wpa_s->pending_join_iface_addr);
2740 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2741 "from P2P peer table: %d MHz", freq);
2743 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2746 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2747 "from BSS table: %d MHz", freq);
2752 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2753 "prior to joining an existing group (GO " MACSTR
2755 MAC2STR(wpa_s->pending_join_dev_addr), freq);
2756 wpa_s->pending_pd_before_join = 1;
2758 switch (wpa_s->pending_join_wps_method) {
2760 case WPS_PIN_DISPLAY:
2761 method = WPS_CONFIG_KEYPAD;
2763 case WPS_PIN_KEYPAD:
2764 method = WPS_CONFIG_DISPLAY;
2767 method = WPS_CONFIG_PUSHBUTTON;
2774 if (p2p_prov_disc_req(wpa_s->global->p2p,
2775 wpa_s->pending_join_dev_addr, method, 1)
2777 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2778 "Discovery Request before joining an "
2780 wpa_s->pending_pd_before_join = 0;
2785 * Actual join operation will be started from the Action frame
2786 * TX status callback.
2791 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
2792 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2793 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2794 wpas_p2p_check_join_scan_limit(wpa_s);
2798 /* Start join operation immediately */
2799 wpas_p2p_join_start(wpa_s);
2803 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
2805 struct wpa_supplicant *wpa_s = eloop_ctx;
2807 struct wpa_driver_scan_params params;
2808 struct wpabuf *wps_ie, *ies;
2810 os_memset(¶ms, 0, sizeof(params));
2812 /* P2P Wildcard SSID */
2813 params.num_ssids = 1;
2814 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2815 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2817 wpa_s->wps->dev.p2p = 1;
2818 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
2819 WPS_REQ_ENROLLEE, 0, NULL);
2820 if (wps_ie == NULL) {
2821 wpas_p2p_scan_res_join(wpa_s, NULL);
2825 ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
2827 wpabuf_free(wps_ie);
2828 wpas_p2p_scan_res_join(wpa_s, NULL);
2831 wpabuf_put_buf(ies, wps_ie);
2832 wpabuf_free(wps_ie);
2834 p2p_scan_ie(wpa_s->global->p2p, ies);
2836 params.extra_ies = wpabuf_head(ies);
2837 params.extra_ies_len = wpabuf_len(ies);
2840 * Run a scan to update BSS table and start Provision Discovery once
2841 * the new scan results become available.
2843 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
2844 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
2845 ret = ieee80211_sta_req_scan(wpa_s, ¶ms);
2847 ret = wpa_drv_scan(wpa_s, ¶ms);
2852 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
2854 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2855 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2856 wpas_p2p_check_join_scan_limit(wpa_s);
2861 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2862 const u8 *dev_addr, enum p2p_wps_method wps_method)
2864 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2865 MACSTR " dev " MACSTR ")",
2866 MAC2STR(iface_addr), MAC2STR(dev_addr));
2868 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2869 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2870 wpa_s->pending_join_wps_method = wps_method;
2872 /* Make sure we are not running find during connection establishment */
2873 wpas_p2p_stop_find(wpa_s);
2875 wpa_s->p2p_join_scan_count = 0;
2876 wpas_p2p_join_scan(wpa_s, NULL);
2881 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2883 struct wpa_supplicant *group;
2884 struct p2p_go_neg_results res;
2886 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2889 if (group != wpa_s) {
2890 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2891 sizeof(group->p2p_pin));
2892 group->p2p_wps_method = wpa_s->p2p_wps_method;
2895 group->p2p_in_provisioning = 1;
2897 os_memset(&res, 0, sizeof(res));
2898 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2900 res.wps_method = wpa_s->pending_join_wps_method;
2901 wpas_start_wps_enrollee(group, &res);
2904 * Allow a longer timeout for join-a-running-group than normal 15
2905 * second group formation timeout since the GO may not have authorized
2906 * our connection yet.
2908 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2909 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
2917 * wpas_p2p_connect - Request P2P Group Formation to be started
2918 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2919 * @peer_addr: Address of the peer P2P Device
2920 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2921 * @persistent_group: Whether to create a persistent group
2922 * @join: Whether to join an existing group (as a client) instead of starting
2923 * Group Owner negotiation; @peer_addr is BSSID in that case
2924 * @auth: Whether to only authorize the connection instead of doing that and
2925 * initiating Group Owner negotiation
2926 * @go_intent: GO Intent or -1 to use default
2927 * @freq: Frequency for the group or 0 for auto-selection
2928 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2929 * failure, -2 on failure due to channel not currently available,
2930 * -3 if forced channel is not supported
2932 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2933 const char *pin, enum p2p_wps_method wps_method,
2934 int persistent_group, int join, int auth, int go_intent,
2937 int force_freq = 0, oper_freq = 0;
2940 enum wpa_driver_if_type iftype;
2942 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2946 go_intent = wpa_s->conf->p2p_go_intent;
2949 wpa_s->p2p_long_listen = 0;
2951 wpa_s->p2p_wps_method = wps_method;
2954 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
2955 else if (wps_method == WPS_PIN_DISPLAY) {
2956 ret = wps_generate_pin();
2957 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
2959 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
2962 wpa_s->p2p_pin[0] = '\0';
2965 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
2967 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
2968 "connect a running group from " MACSTR,
2969 MAC2STR(peer_addr));
2970 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
2973 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
2974 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
2976 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
2977 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
2980 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) <
2986 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2988 oper_freq = wpa_s->assoc_freq;
2990 oper_freq = wpa_drv_shared_freq(wpa_s);
2996 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
2997 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
2998 "(%u MHz) is not supported for P2P uses",
3003 if (oper_freq > 0 && freq != oper_freq &&
3004 !(wpa_s->drv_flags &
3005 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3006 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3007 "on %u MHz while connected on another "
3008 "channel (%u MHz)", freq, oper_freq);
3011 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3012 "requested channel (%u MHz)", freq);
3014 } else if (oper_freq > 0 &&
3015 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3016 if (!(wpa_s->drv_flags &
3017 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3018 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3019 "while connected on non-P2P supported "
3020 "channel (%u MHz)", oper_freq);
3023 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3024 "(%u MHz) not available for P2P - try to use "
3025 "another channel", oper_freq);
3027 } else if (oper_freq > 0) {
3028 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3029 "channel we are already using (%u MHz) on another "
3030 "interface", oper_freq);
3031 force_freq = oper_freq;
3034 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3036 if (!wpa_s->create_p2p_iface) {
3038 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3039 go_intent, wpa_s->own_addr,
3040 force_freq, persistent_group)
3045 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3046 go_intent, wpa_s->own_addr,
3047 force_freq, persistent_group) < 0)
3052 /* Prepare to add a new interface for the group */
3053 iftype = WPA_IF_P2P_GROUP;
3055 iftype = WPA_IF_P2P_CLIENT;
3056 else if (go_intent == 15)
3057 iftype = WPA_IF_P2P_GO;
3058 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3059 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3060 "interface for the group");
3065 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3067 wpa_s->pending_interface_addr,
3068 force_freq, persistent_group) < 0)
3072 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
3073 wpa_s->pending_interface_addr,
3074 force_freq, persistent_group) < 0) {
3075 wpas_p2p_remove_pending_group_interface(wpa_s);
3083 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3084 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3085 * @freq: Frequency of the channel in MHz
3086 * @duration: Duration of the stay on the channel in milliseconds
3088 * This callback is called when the driver indicates that it has started the
3089 * requested remain-on-channel duration.
3091 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3092 unsigned int freq, unsigned int duration)
3094 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3096 wpa_s->roc_waiting_drv_freq = 0;
3097 wpa_s->off_channel_freq = freq;
3098 wpas_send_action_cb(wpa_s, NULL);
3099 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3100 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3101 wpa_s->pending_listen_duration);
3102 wpa_s->pending_listen_freq = 0;
3107 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3108 unsigned int timeout)
3110 /* Limit maximum Listen state time based on driver limitation. */
3111 if (timeout > wpa_s->max_remain_on_chan)
3112 timeout = wpa_s->max_remain_on_chan;
3114 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3115 return wpa_drv_p2p_listen(wpa_s, timeout);
3117 return p2p_listen(wpa_s->global->p2p, timeout);
3122 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3123 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3124 * @freq: Frequency of the channel in MHz
3126 * This callback is called when the driver indicates that a remain-on-channel
3127 * operation has been completed, i.e., the duration on the requested channel
3130 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3133 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3134 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3135 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
3136 wpa_s->off_channel_freq = 0;
3137 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3139 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3140 return; /* P2P module started a new operation */
3141 if (wpa_s->pending_action_tx)
3143 if (wpa_s->p2p_long_listen > 0)
3144 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3145 if (wpa_s->p2p_long_listen > 0) {
3146 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3147 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3153 * wpas_p2p_group_remove - Remove a P2P group
3154 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3155 * @ifname: Network interface name of the group interface or "*" to remove all
3157 * Returns: 0 on success, -1 on failure
3159 * This function is used to remove a P2P group. This can be used to disconnect
3160 * from a group in which the local end is a P2P Client or to end a P2P Group in
3161 * case the local end is the Group Owner. If a virtual network interface was
3162 * created for this group, that interface will be removed. Otherwise, only the
3163 * configured P2P group network will be removed from the interface.
3165 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3167 struct wpa_global *global = wpa_s->global;
3169 if (os_strcmp(ifname, "*") == 0) {
3170 struct wpa_supplicant *prev;
3171 wpa_s = global->ifaces;
3174 wpa_s = wpa_s->next;
3175 wpas_p2p_disconnect(prev);
3180 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3181 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3185 return wpas_p2p_disconnect(wpa_s);
3189 static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3190 struct p2p_go_neg_results *params,
3196 os_memset(params, 0, sizeof(*params));
3197 params->role_go = 1;
3199 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3200 "frequency %d MHz", freq);
3201 params->freq = freq;
3202 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3203 wpa_s->conf->p2p_oper_channel >= 1 &&
3204 wpa_s->conf->p2p_oper_channel <= 11) {
3205 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3206 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3207 "frequency %d MHz", params->freq);
3208 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3209 wpa_s->conf->p2p_oper_reg_class == 124) {
3210 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3211 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3212 "frequency %d MHz", params->freq);
3213 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3214 wpa_s->best_overall_freq > 0 &&
3215 p2p_supported_freq(wpa_s->global->p2p,
3216 wpa_s->best_overall_freq)) {
3217 params->freq = wpa_s->best_overall_freq;
3218 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3219 "channel %d MHz", params->freq);
3220 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3221 wpa_s->best_24_freq > 0 &&
3222 p2p_supported_freq(wpa_s->global->p2p,
3223 wpa_s->best_24_freq)) {
3224 params->freq = wpa_s->best_24_freq;
3225 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3226 "channel %d MHz", params->freq);
3227 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3228 wpa_s->best_5_freq > 0 &&
3229 p2p_supported_freq(wpa_s->global->p2p,
3230 wpa_s->best_5_freq)) {
3231 params->freq = wpa_s->best_5_freq;
3232 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3233 "channel %d MHz", params->freq);
3235 params->freq = 2412;
3236 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3237 "known)", params->freq);
3240 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3241 wpa_s->assoc_freq && !freq) {
3242 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3244 params->freq = wpa_s->assoc_freq;
3247 res = wpa_drv_shared_freq(wpa_s);
3248 if (res > 0 && !freq) {
3249 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3250 "already using on a shared interface");
3256 static struct wpa_supplicant *
3257 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3260 struct wpa_supplicant *group_wpa_s;
3262 if (!wpas_p2p_create_iface(wpa_s))
3265 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3266 WPA_IF_P2P_CLIENT) < 0)
3268 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3269 if (group_wpa_s == NULL) {
3270 wpas_p2p_remove_pending_group_interface(wpa_s);
3279 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3280 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3281 * @persistent_group: Whether to create a persistent group
3282 * @freq: Frequency for the group or 0 to indicate no hardcoding
3283 * Returns: 0 on success, -1 on failure
3285 * This function creates a new P2P group with the local end as the Group Owner,
3286 * i.e., without using Group Owner Negotiation.
3288 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3291 struct p2p_go_neg_results params;
3294 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3297 #ifdef ANDROID_BRCM_P2P_PATCH
3298 /* Make sure we are not running find during connection establishment */
3299 wpa_printf(MSG_DEBUG, "P2P: Stopping P2P FIND, if any");
3300 wpas_p2p_stop_find(wpa_s);
3304 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3306 if (wpa_s->best_24_freq > 0 &&
3307 p2p_supported_freq(wpa_s->global->p2p,
3308 wpa_s->best_24_freq)) {
3309 freq = wpa_s->best_24_freq;
3310 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3311 "channel: %d MHz", freq);
3313 os_get_random((u8 *) &r, sizeof(r));
3314 freq = 2412 + (r % 3) * 25;
3315 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3316 "channel: %d MHz", freq);
3321 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3323 if (wpa_s->best_5_freq > 0 &&
3324 p2p_supported_freq(wpa_s->global->p2p,
3325 wpa_s->best_5_freq)) {
3326 freq = wpa_s->best_5_freq;
3327 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3328 "channel: %d MHz", freq);
3330 os_get_random((u8 *) &r, sizeof(r));
3331 freq = 5180 + (r % 4) * 20;
3332 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3333 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3334 "5 GHz channel for P2P group");
3337 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3338 "channel: %d MHz", freq);
3342 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3343 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3344 "(%u MHz) is not supported for P2P uses",
3349 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
3350 p2p_go_params(wpa_s->global->p2p, ¶ms);
3351 params.persistent_group = persistent_group;
3353 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3356 wpas_start_wps_go(wpa_s, ¶ms, 0);
3362 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3363 struct wpa_ssid *params, int addr_allocated)
3365 struct wpa_ssid *ssid;
3367 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3371 wpa_supplicant_ap_deinit(wpa_s);
3373 ssid = wpa_config_add_network(wpa_s->conf);
3376 wpa_config_set_network_defaults(ssid);
3377 ssid->temporary = 1;
3378 ssid->proto = WPA_PROTO_RSN;
3379 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3380 ssid->group_cipher = WPA_CIPHER_CCMP;
3381 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3382 ssid->ssid = os_malloc(params->ssid_len);
3383 if (ssid->ssid == NULL) {
3384 wpa_config_remove_network(wpa_s->conf, ssid->id);
3387 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3388 ssid->ssid_len = params->ssid_len;
3389 ssid->p2p_group = 1;
3390 ssid->export_keys = 1;
3391 if (params->psk_set) {
3392 os_memcpy(ssid->psk, params->psk, 32);
3395 if (params->passphrase)
3396 ssid->passphrase = os_strdup(params->passphrase);
3398 wpa_supplicant_select_network(wpa_s, ssid);
3400 wpa_s->show_group_started = 1;
3406 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3407 struct wpa_ssid *ssid, int addr_allocated,
3410 struct p2p_go_neg_results params;
3413 if (ssid->disabled != 2 || ssid->ssid == NULL)
3416 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3417 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3418 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3423 /* Make sure we are not running find during connection establishment */
3424 wpas_p2p_stop_find(wpa_s);
3426 if (ssid->mode == WPAS_MODE_INFRA)
3427 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3429 if (ssid->mode != WPAS_MODE_P2P_GO)
3432 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
3435 if (ssid->passphrase == NULL ||
3436 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3437 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3441 os_strlcpy(params.passphrase, ssid->passphrase,
3442 sizeof(params.passphrase));
3443 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3444 params.ssid_len = ssid->ssid_len;
3445 params.persistent_group = 1;
3447 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3451 wpas_start_wps_go(wpa_s, ¶ms, 0);
3457 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3458 struct wpabuf *proberesp_ies)
3460 struct wpa_supplicant *wpa_s = ctx;
3461 if (wpa_s->ap_iface) {
3462 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
3464 wpabuf_free(hapd->p2p_beacon_ie);
3465 hapd->p2p_beacon_ie = beacon_ies;
3467 wpabuf_free(hapd->p2p_probe_resp_ie);
3468 hapd->p2p_probe_resp_ie = proberesp_ies;
3470 wpabuf_free(beacon_ies);
3471 wpabuf_free(proberesp_ies);
3473 wpa_supplicant_ap_update_beacon(wpa_s);
3477 static void wpas_p2p_idle_update(void *ctx, int idle)
3479 struct wpa_supplicant *wpa_s = ctx;
3480 if (!wpa_s->ap_iface)
3482 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3484 wpas_p2p_set_group_idle_timeout(wpa_s);
3486 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3490 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
3491 int persistent_group,
3492 int group_formation)
3494 struct p2p_group *group;
3495 struct p2p_group_config *cfg;
3497 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3499 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3502 cfg = os_zalloc(sizeof(*cfg));
3506 cfg->persistent_group = persistent_group;
3507 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3508 if (wpa_s->max_stations &&
3509 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3510 cfg->max_clients = wpa_s->max_stations;
3512 cfg->max_clients = wpa_s->conf->max_num_sta;
3513 cfg->cb_ctx = wpa_s;
3514 cfg->ie_update = wpas_p2p_ie_update;
3515 cfg->idle_update = wpas_p2p_idle_update;
3517 group = p2p_group_init(wpa_s->global->p2p, cfg);
3520 if (!group_formation)
3521 p2p_group_notif_formation_done(group);
3522 wpa_s->p2p_group = group;
3527 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3530 if (!wpa_s->p2p_in_provisioning) {
3531 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3532 "provisioning not in progress");
3536 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3538 if (wpa_s->global->p2p)
3539 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3540 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3541 wpa_drv_wps_success_cb(wpa_s, peer_addr);
3542 wpas_group_formation_completed(wpa_s, 1);
3546 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
3547 struct wps_event_fail *fail)
3549 if (!wpa_s->p2p_in_provisioning) {
3550 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
3551 "provisioning not in progress");
3554 wpas_notify_p2p_wps_failed(wpa_s, fail);
3558 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3559 const char *config_method)
3563 if (os_strcmp(config_method, "display") == 0)
3564 config_methods = WPS_CONFIG_DISPLAY;
3565 else if (os_strcmp(config_method, "keypad") == 0)
3566 config_methods = WPS_CONFIG_KEYPAD;
3567 else if (os_strcmp(config_method, "pbc") == 0 ||
3568 os_strcmp(config_method, "pushbutton") == 0)
3569 config_methods = WPS_CONFIG_PUSHBUTTON;
3573 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3574 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
3578 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
3581 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
3586 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3589 return p2p_scan_result_text(ies, ies_len, buf, end);
3593 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3595 if (!wpa_s->pending_action_tx)
3598 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3599 "operation request");
3600 wpabuf_free(wpa_s->pending_action_tx);
3601 wpa_s->pending_action_tx = NULL;
3605 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3606 enum p2p_discovery_type type,
3607 unsigned int num_req_dev_types, const u8 *req_dev_types)
3609 wpas_p2p_clear_pending_action_tx(wpa_s);
3610 wpa_s->p2p_long_listen = 0;
3612 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3613 return wpa_drv_p2p_find(wpa_s, timeout, type);
3615 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3618 return p2p_find(wpa_s->global->p2p, timeout, type,
3619 num_req_dev_types, req_dev_types);
3623 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3625 wpas_p2p_clear_pending_action_tx(wpa_s);
3626 wpa_s->p2p_long_listen = 0;
3627 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3628 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3630 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3631 wpa_drv_p2p_stop_find(wpa_s);
3635 if (wpa_s->global->p2p)
3636 p2p_stop_find(wpa_s->global->p2p);
3638 wpas_p2p_remove_pending_group_interface(wpa_s);
3642 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3644 struct wpa_supplicant *wpa_s = eloop_ctx;
3645 wpa_s->p2p_long_listen = 0;
3649 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3653 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3656 wpas_p2p_clear_pending_action_tx(wpa_s);
3660 * This is a request for unlimited Listen state. However, at
3661 * least for now, this is mapped to a Listen state for one
3666 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3667 wpa_s->p2p_long_listen = 0;
3669 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
3670 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
3671 wpa_s->p2p_long_listen = timeout * 1000;
3672 eloop_register_timeout(timeout, 0,
3673 wpas_p2p_long_listen_timeout,
3681 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3682 u8 *buf, size_t len, int p2p_group)
3684 struct wpabuf *p2p_ie;
3687 if (wpa_s->global->p2p_disabled)
3689 if (wpa_s->global->p2p == NULL)
3694 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
3695 #ifdef ANDROID_BRCM_P2P_PATCH
3696 if (p2p_ie == NULL) return -1;
3698 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
3700 wpabuf_free(p2p_ie);
3706 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
3707 const u8 *ie, size_t ie_len)
3709 if (wpa_s->global->p2p_disabled)
3711 if (wpa_s->global->p2p == NULL)
3714 return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len);
3718 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
3719 const u8 *sa, const u8 *bssid,
3720 u8 category, const u8 *data, size_t len, int freq)
3722 if (wpa_s->global->p2p_disabled)
3724 if (wpa_s->global->p2p == NULL)
3727 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
3732 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
3734 if (wpa_s->global->p2p_disabled)
3736 if (wpa_s->global->p2p == NULL)
3739 p2p_scan_ie(wpa_s->global->p2p, ies);
3743 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
3745 p2p_group_deinit(wpa_s->p2p_group);
3746 wpa_s->p2p_group = NULL;
3750 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
3752 wpa_s->p2p_long_listen = 0;
3754 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3755 return wpa_drv_p2p_reject(wpa_s, addr);
3757 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3760 return p2p_reject(wpa_s->global->p2p, addr);
3764 /* Invite to reinvoke a persistent group */
3765 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3766 struct wpa_ssid *ssid, const u8 *go_dev_addr)
3768 enum p2p_invite_role role;
3770 #ifdef ANDROID_BRCM_P2P_PATCH
3774 if (ssid->mode == WPAS_MODE_P2P_GO) {
3775 role = P2P_INVITE_ROLE_GO;
3776 if (peer_addr == NULL) {
3777 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
3778 "address in invitation command");
3782 #ifdef ANDROID_BRCM_P2P_PATCH
3783 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);
3784 if(wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go)) {
3785 wpa_printf(MSG_DEBUG, "P2P: We are already running persistent group");
3787 bssid = wpa_s->own_addr;
3789 wpa_printf(MSG_DEBUG, "P2P: We are running persistent group but go is not set");
3791 wpa_printf(MSG_DEBUG, "P2P: We are NOT already running persistent group");
3794 if (wpas_p2p_create_iface(wpa_s)) {
3795 if (wpas_p2p_add_group_interface(wpa_s,
3796 WPA_IF_P2P_GO) < 0) {
3797 wpa_printf(MSG_ERROR, "P2P: Failed to "
3798 "allocate a new interface for the "
3802 bssid = wpa_s->pending_interface_addr;
3804 bssid = wpa_s->own_addr;
3805 #ifdef ANDROID_BRCM_P2P_PATCH
3809 role = P2P_INVITE_ROLE_CLIENT;
3810 peer_addr = ssid->bssid;
3812 wpa_s->pending_invite_ssid_id = ssid->id;
3814 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3815 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
3816 ssid->ssid, ssid->ssid_len,
3819 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3822 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3823 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
3827 /* Invite to join an active group */
3828 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
3829 const u8 *peer_addr, const u8 *go_dev_addr)
3831 struct wpa_global *global = wpa_s->global;
3832 enum p2p_invite_role role;
3834 struct wpa_ssid *ssid;
3836 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3837 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3840 if (wpa_s == NULL) {
3841 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
3845 ssid = wpa_s->current_ssid;
3847 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
3852 if (ssid->mode == WPAS_MODE_P2P_GO) {
3853 role = P2P_INVITE_ROLE_ACTIVE_GO;
3854 bssid = wpa_s->own_addr;
3855 if (go_dev_addr == NULL)
3856 go_dev_addr = wpa_s->parent->own_addr;
3858 role = P2P_INVITE_ROLE_CLIENT;
3859 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
3860 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
3861 "invite to current group");
3864 bssid = wpa_s->bssid;
3865 if (go_dev_addr == NULL &&
3866 !is_zero_ether_addr(wpa_s->go_dev_addr))
3867 go_dev_addr = wpa_s->go_dev_addr;
3869 wpa_s->parent->pending_invite_ssid_id = -1;
3871 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3872 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
3873 ssid->ssid, ssid->ssid_len,
3876 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3879 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3880 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
3885 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
3887 struct wpa_ssid *ssid = wpa_s->current_ssid;
3888 const char *ssid_txt;
3889 u8 go_dev_addr[ETH_ALEN];
3890 int network_id = -1;
3893 if (!wpa_s->show_group_started || !ssid)
3896 wpa_s->show_group_started = 0;
3898 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
3899 os_memset(go_dev_addr, 0, ETH_ALEN);
3900 if (ssid->bssid_set)
3901 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
3902 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3904 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
3906 if (wpa_s->global->p2p_group_formation == wpa_s)
3907 wpa_s->global->p2p_group_formation = NULL;
3909 if (ssid->passphrase == NULL && ssid->psk_set) {
3911 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
3912 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3913 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3915 #ifdef ANDROID_BRCM_P2P_PATCH
3916 wpa_s->ifname, ssid_txt, wpa_s->current_bss->freq, psk,
3918 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
3920 MAC2STR(go_dev_addr),
3921 persistent ? " [PERSISTENT]" : "");
3923 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3924 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3925 "go_dev_addr=" MACSTR "%s",
3926 #ifdef ANDROID_BRCM_P2P_PATCH
3927 wpa_s->ifname, ssid_txt, wpa_s->current_bss->freq,
3929 wpa_s->ifname, ssid_txt, ssid->frequency,
3931 ssid->passphrase ? ssid->passphrase : "",
3932 MAC2STR(go_dev_addr),
3933 persistent ? " [PERSISTENT]" : "");
3937 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
3940 network_id = ssid->id;
3941 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
3945 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
3946 u32 interval1, u32 duration2, u32 interval2)
3948 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3950 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3953 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3954 wpa_s->current_ssid == NULL ||
3955 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
3958 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
3959 wpa_s->own_addr, wpa_s->assoc_freq,
3960 duration1, interval1, duration2, interval2);
3964 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
3965 unsigned int interval)
3967 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3970 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3973 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
3977 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
3979 struct wpa_supplicant *wpa_s = eloop_ctx;
3981 if (wpa_s->conf->p2p_group_idle == 0) {
3982 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
3987 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
3989 wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
3990 wpas_p2p_group_delete(wpa_s);
3994 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
3996 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3997 if (wpa_s->conf->p2p_group_idle == 0)
4000 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4003 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
4004 wpa_s->conf->p2p_group_idle);
4005 eloop_register_timeout(wpa_s->conf->p2p_group_idle, 0,
4006 wpas_p2p_group_idle_timeout, wpa_s, NULL);
4010 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4011 u16 reason_code, const u8 *ie, size_t ie_len)
4013 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4015 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4018 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
4021 #ifdef ANDROID_BRCM_P2P_PATCH
4022 void wpas_p2p_group_remove_notif(struct wpa_supplicant *wpa_s, u16 reason_code)
4024 if(wpa_s->global->p2p_disabled)
4027 /* If we are running a P2P Client and we received a Deauth/Disassoc from the Go, then remove
4028 the virutal interface on which the client is running. */
4029 if((wpa_s != wpa_s->parent) && (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) && (wpa_s->key_mgmt != WPA_KEY_MGMT_WPS)) {
4031 wpa_printf(MSG_DEBUG, "P2P: [EVENT_DEAUTH] Removing P2P_CLIENT virtual intf.");
4032 wpa_supplicant_cancel_scan(wpa_s);
4033 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
4034 wpas_p2p_group_delete(wpa_s);
4039 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4040 u16 reason_code, const u8 *ie, size_t ie_len)
4042 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4044 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4047 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
4051 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4053 struct p2p_data *p2p = wpa_s->global->p2p;
4058 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4061 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4062 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4064 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4065 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4068 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4069 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4071 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4072 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4074 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4075 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4076 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4077 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4078 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4081 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4082 p2p_set_sec_dev_types(p2p,
4083 (void *) wpa_s->conf->sec_device_type,
4084 wpa_s->conf->num_sec_device_types);
4086 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4088 p2p_remove_wps_vendor_extensions(p2p);
4089 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4090 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4092 p2p_add_wps_vendor_extension(
4093 p2p, wpa_s->conf->wps_vendor_ext[i]);
4097 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4098 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4100 country[0] = wpa_s->conf->country[0];
4101 country[1] = wpa_s->conf->country[1];
4103 p2p_set_country(p2p, country);
4106 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4107 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4108 wpa_s->conf->p2p_ssid_postfix ?
4109 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4113 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4114 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
4116 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4117 u8 reg_class, channel;
4120 if (wpa_s->conf->p2p_listen_reg_class &&
4121 wpa_s->conf->p2p_listen_channel) {
4122 reg_class = wpa_s->conf->p2p_listen_reg_class;
4123 channel = wpa_s->conf->p2p_listen_channel;
4127 * Pick one of the social channels randomly as the
4130 os_get_random((u8 *) &r, sizeof(r));
4131 channel = 1 + (r % 3) * 5;
4133 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4135 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4138 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4139 u8 op_reg_class, op_channel, cfg_op_channel;
4142 if (wpa_s->conf->p2p_oper_reg_class &&
4143 wpa_s->conf->p2p_oper_channel) {
4144 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4145 op_channel = wpa_s->conf->p2p_oper_channel;
4150 * Use random operation channel from (1, 6, 11)
4151 *if no other preference is indicated.
4153 os_get_random((u8 *) &r, sizeof(r));
4154 op_channel = 1 + (r % 3) * 5;
4157 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4160 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4166 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4169 if (!wpa_s->ap_iface)
4172 #ifdef ANDROID_BRCM_P2P_PATCH
4173 #define NOA_BUF_LEN 50
4174 /* Now get the NOA descriptor from the driver */
4175 hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4178 u8 noa[NOA_BUF_LEN];
4180 wpa_printf(MSG_DEBUG, "P2P: Get NOA attribute from driver");
4181 noa_len = wpa_drv_get_noa(wpa_s, noa, NOA_BUF_LEN);
4183 wpa_printf(MSG_DEBUG, "P2P: Now Update NOA attributes in Beacons/ProbeRsps noa_len %d", noa_len);
4184 return p2p_group_notif_noa(wpa_s->p2p_group, noa, noa_len);
4192 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4197 #ifdef ANDROID_BRCM_P2P_PATCH
4198 int wpas_drv_set_p2p_powersave(struct wpa_supplicant *wpa_s, int legacy_ps, int opp_ps, int ctwindow)
4200 #define NOA_BUF_LEN 50
4201 u8 noa[NOA_BUF_LEN];
4203 wpa_drv_set_p2p_powersave(wpa_s, legacy_ps, opp_ps, ctwindow);
4204 wpa_printf(MSG_DEBUG, "P2P: Get NOA attribute from driver");
4205 noa_len = wpa_drv_get_noa(wpa_s, noa, NOA_BUF_LEN);
4207 wpa_printf(MSG_DEBUG, "P2P: Now Update NOA attributes in Beacons/ProbeRsps noa_len %d", noa_len);
4208 return p2p_group_notif_noa(wpa_s->p2p_group, noa, noa_len);
4215 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4217 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4219 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4222 wpa_s->global->cross_connection = enabled;
4223 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4226 struct wpa_supplicant *iface;
4228 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4230 if (iface->cross_connect_enabled == 0)
4233 iface->cross_connect_enabled = 0;
4234 iface->cross_connect_in_use = 0;
4235 wpa_msg(iface->parent, MSG_INFO,
4236 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4237 iface->ifname, iface->cross_connect_uplink);
4245 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4247 struct wpa_supplicant *iface;
4249 if (!uplink->global->cross_connection)
4252 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4253 if (!iface->cross_connect_enabled)
4255 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4258 if (iface->ap_iface == NULL)
4260 if (iface->cross_connect_in_use)
4263 iface->cross_connect_in_use = 1;
4264 wpa_msg(iface->parent, MSG_INFO,
4265 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4266 iface->ifname, iface->cross_connect_uplink);
4271 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4273 struct wpa_supplicant *iface;
4275 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4276 if (!iface->cross_connect_enabled)
4278 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4281 if (!iface->cross_connect_in_use)
4284 wpa_msg(iface->parent, MSG_INFO,
4285 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4286 iface->ifname, iface->cross_connect_uplink);
4287 iface->cross_connect_in_use = 0;
4292 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4294 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4295 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4296 wpa_s->cross_connect_disallowed)
4297 wpas_p2p_disable_cross_connect(wpa_s);
4299 wpas_p2p_enable_cross_connect(wpa_s);
4300 if (!wpa_s->ap_iface)
4301 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4305 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4307 wpas_p2p_disable_cross_connect(wpa_s);
4308 if (!wpa_s->ap_iface)
4309 wpas_p2p_set_group_idle_timeout(wpa_s);
4313 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4315 struct wpa_supplicant *iface;
4317 if (!wpa_s->global->cross_connection)
4320 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4323 if (iface->drv_flags &
4324 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4326 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4329 wpa_s->cross_connect_enabled = 1;
4330 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4331 sizeof(wpa_s->cross_connect_uplink));
4332 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4333 "%s to %s whenever uplink is available",
4334 wpa_s->ifname, wpa_s->cross_connect_uplink);
4336 if (iface->ap_iface || iface->current_ssid == NULL ||
4337 iface->current_ssid->mode != WPAS_MODE_INFRA ||
4338 iface->cross_connect_disallowed ||
4339 iface->wpa_state != WPA_COMPLETED)
4342 wpa_s->cross_connect_in_use = 1;
4343 wpa_msg(wpa_s->parent, MSG_INFO,
4344 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4345 wpa_s->ifname, wpa_s->cross_connect_uplink);
4351 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
4353 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
4354 !wpa_s->p2p_in_provisioning)
4355 return 0; /* not P2P client operation */
4357 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
4359 if (wpa_s != wpa_s->parent)
4360 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
4362 if (wpa_s->global->p2p)
4363 p2p_group_formation_failed(wpa_s->global->p2p);
4365 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4366 wpa_s->parent, NULL);
4368 wpas_group_formation_completed(wpa_s, 0);
4373 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4375 struct p2p_channels chan;
4377 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4380 os_memset(&chan, 0, sizeof(chan));
4381 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
4382 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
4387 p2p_update_channel_list(wpa_s->global->p2p, &chan);
4391 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
4393 struct wpa_global *global = wpa_s->global;
4397 if (global->p2p == NULL)
4400 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
4402 if (wpa_s->pending_interface_name[0] &&
4403 !is_zero_ether_addr(wpa_s->pending_interface_addr))
4406 peer = p2p_get_go_neg_peer(global->p2p);
4408 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
4409 MACSTR, MAC2STR(peer));
4410 p2p_unauthorize(global->p2p, peer);
4413 wpas_p2p_stop_find(wpa_s);
4415 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4416 if (wpa_s == global->p2p_group_formation &&
4417 (wpa_s->p2p_in_provisioning ||
4418 wpa_s->parent->pending_interface_type ==
4419 WPA_IF_P2P_CLIENT)) {
4420 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
4421 "formation found - cancelling",
4424 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4425 wpa_s->parent, NULL);
4426 wpas_p2p_group_delete(wpa_s);
4432 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
4440 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
4442 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4445 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
4446 "being available anymore");
4447 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
4448 wpas_p2p_group_delete(wpa_s);
4452 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
4453 int freq_24, int freq_5, int freq_overall)
4455 struct p2p_data *p2p = wpa_s->global->p2p;
4456 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4458 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
4462 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
4465 struct p2p_data *p2p = wpa_s->global->p2p;
4467 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4470 if (hwaddr_aton(addr, peer))
4473 return p2p_unauthorize(p2p, peer);
4478 * wpas_p2p_disconnect - Disconnect from a P2P Group
4479 * @wpa_s: Pointer to wpa_supplicant data
4480 * Returns: 0 on success, -1 on failure
4482 * This can be used to disconnect from a group in which the local end is a P2P
4483 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4484 * virtual network interface was created for this group, that interface will be
4485 * removed. Otherwise, only the configured P2P group network will be removed
4486 * from the interface.
4488 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
4494 wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
4495 wpas_p2p_group_delete(wpa_s);