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 "wpa_supplicant_i.h"
28 #include "config_ssid.h"
34 #include "wps_supplicant.h"
35 #include "p2p_supplicant.h"
38 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
39 static struct wpa_supplicant *
40 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
42 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
43 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
46 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
47 struct wpa_scan_results *scan_res)
51 if (wpa_s->global->p2p_disabled)
54 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
57 for (i = 0; i < scan_res->num; i++) {
58 struct wpa_scan_res *bss = scan_res->res[i];
59 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
60 bss->freq, bss->level,
61 (const u8 *) (bss + 1),
66 p2p_scan_res_handled(wpa_s->global->p2p);
70 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq)
72 struct wpa_supplicant *wpa_s = ctx;
73 struct wpa_driver_scan_params params;
75 struct wpabuf *wps_ie, *ies;
76 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
78 os_memset(¶ms, 0, sizeof(params));
80 /* P2P Wildcard SSID */
82 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
83 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
85 wpa_s->wps->dev.p2p = 1;
86 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
91 ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
96 wpabuf_put_buf(ies, wps_ie);
99 p2p_scan_ie(wpa_s->global->p2p, ies);
101 params.extra_ies = wpabuf_head(ies);
102 params.extra_ies_len = wpabuf_len(ies);
105 case P2P_SCAN_SOCIAL:
106 params.freqs = social_channels;
110 case P2P_SCAN_SPECIFIC:
111 social_channels[0] = freq;
112 social_channels[1] = 0;
113 params.freqs = social_channels;
115 case P2P_SCAN_SOCIAL_PLUS_ONE:
116 social_channels[3] = freq;
117 params.freqs = social_channels;
121 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
122 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
123 ret = ieee80211_sta_req_scan(wpa_s, ¶ms);
125 ret = wpa_drv_scan(wpa_s, ¶ms);
133 #ifdef CONFIG_CLIENT_MLME
134 static void p2p_rx_action_mlme(void *ctx, const u8 *buf, size_t len, int freq)
136 struct wpa_supplicant *wpa_s = ctx;
137 const struct ieee80211_mgmt *mgmt;
140 if (wpa_s->global->p2p_disabled)
142 mgmt = (const struct ieee80211_mgmt *) buf;
143 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
146 p2p_rx_action(wpa_s->global->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
147 mgmt->u.action.category,
148 &mgmt->u.action.u.vs_public_action.action,
149 len - hdr_len, freq);
151 #endif /* CONFIG_CLIENT_MLME */
154 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
156 switch (p2p_group_interface) {
157 case P2P_GROUP_INTERFACE_PENDING:
158 return WPA_IF_P2P_GROUP;
159 case P2P_GROUP_INTERFACE_GO:
160 return WPA_IF_P2P_GO;
161 case P2P_GROUP_INTERFACE_CLIENT:
162 return WPA_IF_P2P_CLIENT;
165 return WPA_IF_P2P_GROUP;
169 static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
171 struct wpa_ssid *ssid;
174 ssid = wpa_s->current_ssid;
177 * The current SSID was not known, but there may still be a
178 * pending P2P group interface waiting for provisioning.
180 ssid = wpa_s->conf->ssid;
182 if (ssid->p2p_group &&
183 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
184 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
189 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
191 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
192 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
193 wpa_s->reassociate = 0;
194 wpa_s->disconnected = 1;
195 wpa_supplicant_deauthenticate(wpa_s,
196 WLAN_REASON_DEAUTH_LEAVING);
200 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s",
201 wpa_s->ifname, gtype);
202 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
203 struct wpa_global *global;
205 enum wpa_driver_if_type type;
206 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
208 global = wpa_s->global;
209 ifname = os_strdup(wpa_s->ifname);
210 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
211 wpa_supplicant_remove_iface(wpa_s->global, wpa_s);
212 wpa_s = global->ifaces;
214 wpa_drv_if_remove(wpa_s, type, ifname);
219 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
220 if (ssid && (ssid->p2p_group ||
221 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
222 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
224 if (ssid == wpa_s->current_ssid)
225 wpa_s->current_ssid = NULL;
226 wpas_notify_network_removed(wpa_s, ssid);
227 wpa_config_remove_network(wpa_s->conf, id);
228 wpa_supplicant_clear_status(wpa_s);
230 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
233 wpa_supplicant_ap_deinit(wpa_s);
237 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
239 const u8 *ssid, size_t ssid_len)
247 if (wpa_s->go_params)
248 bssid = wpa_s->go_params->peer_interface_addr;
250 bssid = wpa_s->bssid;
252 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
254 u8 iface_addr[ETH_ALEN];
255 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
257 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
260 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
261 "group is persistent - BSS " MACSTR " not found",
266 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
268 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
269 "group is persistent - BSS " MACSTR
270 " did not include P2P IE", MAC2STR(bssid));
271 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
272 (u8 *) (bss + 1), bss->ie_len);
273 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
274 ((u8 *) bss + 1) + bss->ie_len,
279 group_capab = p2p_get_group_capab(p2p);
280 addr = p2p_get_go_dev_addr(p2p);
281 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
282 "group_capab=0x%x", group_capab);
284 os_memcpy(go_dev_addr, addr, ETH_ALEN);
285 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
288 os_memset(go_dev_addr, 0, ETH_ALEN);
291 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
292 "go_dev_addr=" MACSTR,
293 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
295 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
299 static void wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
300 struct wpa_ssid *ssid,
301 const u8 *go_dev_addr)
306 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
307 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
308 for (s = wpa_s->conf->ssid; s; s = s->next) {
309 if (s->disabled == 2 &&
310 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
311 s->ssid_len == ssid->ssid_len &&
312 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
317 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
319 if (ssid->passphrase && !s->passphrase)
321 else if (ssid->passphrase && s->passphrase &&
322 os_strcmp(ssid->passphrase, s->passphrase) != 0)
325 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
328 s = wpa_config_add_network(wpa_s->conf);
331 wpa_config_set_network_defaults(s);
335 s->p2p_persistent_group = 1;
338 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
339 s->mode = ssid->mode;
340 s->auth_alg = WPA_AUTH_ALG_OPEN;
341 s->key_mgmt = WPA_KEY_MGMT_PSK;
342 s->proto = WPA_PROTO_RSN;
343 s->pairwise_cipher = WPA_CIPHER_CCMP;
344 if (ssid->passphrase) {
345 os_free(s->passphrase);
346 s->passphrase = os_strdup(ssid->passphrase);
350 os_memcpy(s->psk, ssid->psk, 32);
352 if (s->passphrase && !s->psk_set)
353 wpa_config_update_psk(s);
354 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
356 s->ssid = os_malloc(ssid->ssid_len);
359 s->ssid_len = ssid->ssid_len;
360 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
363 #ifndef CONFIG_NO_CONFIG_WRITE
364 if (changed && wpa_s->conf->update_config &&
365 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
366 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
368 #endif /* CONFIG_NO_CONFIG_WRITE */
372 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
375 struct wpa_ssid *ssid;
376 const char *ssid_txt;
379 u8 go_dev_addr[ETH_ALEN];
382 * This callback is likely called for the main interface. Update wpa_s
383 * to use the group interface if a new interface was created for the
386 if (wpa_s->global->p2p_group_formation)
387 wpa_s = wpa_s->global->p2p_group_formation;
388 wpa_s->p2p_in_provisioning = 0;
391 wpa_msg(wpa_s->parent, MSG_INFO,
392 P2P_EVENT_GROUP_FORMATION_FAILURE);
393 wpas_p2p_group_delete(wpa_s);
397 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
399 ssid = wpa_s->current_ssid;
400 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
401 ssid->mode = WPAS_MODE_P2P_GO;
402 p2p_group_notif_formation_done(wpa_s->p2p_group);
403 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
408 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
409 client = ssid->mode == WPAS_MODE_INFRA;
410 if (ssid->mode == WPAS_MODE_P2P_GO) {
411 persistent = ssid->p2p_persistent_group;
412 os_memcpy(go_dev_addr, wpa_s->parent->own_addr,
415 persistent = wpas_p2p_persistent_group(wpa_s,
421 client = wpa_s->p2p_group_interface ==
422 P2P_GROUP_INTERFACE_CLIENT;
425 wpa_s->show_group_started = 0;
428 * Indicate event only after successfully completed 4-way
429 * handshake, i.e., when the interface is ready for data
432 wpa_s->show_group_started = 1;
433 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
435 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
436 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
437 "%s GO ssid=\"%s\" psk=%s go_dev_addr=" MACSTR "%s",
438 wpa_s->ifname, ssid_txt, psk, MAC2STR(go_dev_addr),
439 persistent ? " [PERSISTENT]" : "");
441 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
442 "%s GO ssid=\"%s\" passphrase=\"%s\" go_dev_addr="
444 wpa_s->ifname, ssid_txt,
445 ssid && ssid->passphrase ? ssid->passphrase : "",
446 MAC2STR(go_dev_addr),
447 persistent ? " [PERSISTENT]" : "");
451 wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
456 static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
458 struct wpa_supplicant *wpa_s = eloop_ctx;
459 struct wpa_supplicant *iface;
463 without_roc = wpa_s->pending_action_without_roc;
464 wpa_s->pending_action_without_roc = 0;
466 if (wpa_s->pending_action_tx == NULL)
469 if (wpa_s->off_channel_freq != wpa_s->pending_action_freq &&
470 wpa_s->pending_action_freq != 0) {
471 wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX "
472 "waiting for another freq=%u (off_channel_freq=%u)",
473 wpa_s->pending_action_freq,
474 wpa_s->off_channel_freq);
475 if (without_roc && wpa_s->off_channel_freq == 0) {
477 * We may get here if wpas_send_action() found us to be
478 * on the correct channel, but remain-on-channel cancel
479 * event was received before getting here.
481 wpa_printf(MSG_DEBUG, "P2P: Schedule "
482 "remain-on-channel to send Action frame");
483 if (wpa_drv_remain_on_channel(
484 wpa_s, wpa_s->pending_action_freq, 200) <
486 wpa_printf(MSG_DEBUG, "P2P: Failed to request "
487 "driver to remain on channel (%u "
488 "MHz) for Action Frame TX",
489 wpa_s->pending_action_freq);
496 * This call is likely going to be on the P2P device instance if the
497 * driver uses a separate interface for that purpose. However, some
498 * Action frames are actually sent within a P2P Group and when that is
499 * the case, we need to follow power saving (e.g., GO buffering the
500 * frame for a client in PS mode or a client following the adverised
501 * NoA from its GO). To make that easier for the driver, select the
502 * correct group interface here.
504 if (os_memcmp(wpa_s->pending_action_src, wpa_s->own_addr, ETH_ALEN) !=
507 * Try to find a group interface that matches with the source
510 iface = wpa_s->global->ifaces;
512 if (os_memcmp(wpa_s->pending_action_src,
513 iface->own_addr, ETH_ALEN) == 0)
517 wpa_printf(MSG_DEBUG, "P2P: Use group interface %s "
518 "instead of interface %s for Action TX",
519 iface->ifname, wpa_s->ifname);
525 wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to "
526 MACSTR " using interface %s",
527 MAC2STR(wpa_s->pending_action_dst), iface->ifname);
528 res = wpa_drv_send_action(iface, wpa_s->pending_action_freq,
529 wpa_s->pending_action_dst,
530 wpa_s->pending_action_src,
531 wpa_s->pending_action_bssid,
532 wpabuf_head(wpa_s->pending_action_tx),
533 wpabuf_len(wpa_s->pending_action_tx));
535 wpa_printf(MSG_DEBUG, "P2P: Failed to send the pending "
538 * Use fake TX status event to allow P2P state machine to
541 wpas_send_action_tx_status(
542 wpa_s, wpa_s->pending_action_dst,
543 wpabuf_head(wpa_s->pending_action_tx),
544 wpabuf_len(wpa_s->pending_action_tx), 0);
549 void wpas_send_action_tx_status(struct wpa_supplicant *wpa_s, const u8 *dst,
550 const u8 *data, size_t data_len, int ack)
552 if (wpa_s->global->p2p_disabled)
555 if (wpa_s->pending_action_tx == NULL) {
556 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - no "
557 "pending operation");
561 if (os_memcmp(dst, wpa_s->pending_action_dst, ETH_ALEN) != 0) {
562 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - unknown "
563 "destination address");
567 wpabuf_free(wpa_s->pending_action_tx);
568 wpa_s->pending_action_tx = NULL;
570 p2p_send_action_cb(wpa_s->global->p2p, wpa_s->pending_action_freq,
571 wpa_s->pending_action_dst,
572 wpa_s->pending_action_src,
573 wpa_s->pending_action_bssid,
576 if (wpa_s->pending_pd_before_join &&
577 (os_memcmp(wpa_s->pending_action_dst, wpa_s->pending_join_dev_addr,
579 os_memcmp(wpa_s->pending_action_dst,
580 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
581 wpa_s->pending_pd_before_join = 0;
582 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
583 "join-existing-group operation");
584 wpas_p2p_join_start(wpa_s);
589 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
590 const u8 *src, const u8 *bssid, const u8 *buf,
591 size_t len, unsigned int wait_time)
593 struct wpa_supplicant *wpa_s = ctx;
595 wpa_printf(MSG_DEBUG, "P2P: Send action frame: freq=%d dst=" MACSTR
596 " src=" MACSTR " bssid=" MACSTR,
597 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid));
599 if (wpa_s->pending_action_tx) {
600 wpa_printf(MSG_DEBUG, "P2P: Dropped pending Action frame TX "
601 "to " MACSTR, MAC2STR(wpa_s->pending_action_dst));
602 wpabuf_free(wpa_s->pending_action_tx);
604 wpa_s->pending_action_tx = wpabuf_alloc(len);
605 if (wpa_s->pending_action_tx == NULL)
607 wpabuf_put_data(wpa_s->pending_action_tx, buf, len);
608 os_memcpy(wpa_s->pending_action_src, src, ETH_ALEN);
609 os_memcpy(wpa_s->pending_action_dst, dst, ETH_ALEN);
610 os_memcpy(wpa_s->pending_action_bssid, bssid, ETH_ALEN);
611 wpa_s->pending_action_freq = freq;
613 if (wpa_s->off_channel_freq == freq || freq == 0) {
614 /* Already on requested channel; send immediately */
615 /* TODO: Would there ever be need to extend the current
616 * duration on the channel? */
617 wpa_s->pending_action_without_roc = 1;
618 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
619 eloop_register_timeout(0, 0, wpas_send_action_cb, wpa_s, NULL);
622 wpa_s->pending_action_without_roc = 0;
624 wpa_printf(MSG_DEBUG, "P2P: Schedule Action frame to be transmitted "
625 "once the driver gets to the requested channel");
626 if (wait_time > wpa_s->max_remain_on_chan)
627 wait_time = wpa_s->max_remain_on_chan;
628 if (wpa_drv_remain_on_channel(wpa_s, freq, wait_time) < 0) {
629 wpa_printf(MSG_DEBUG, "P2P: Failed to request driver "
630 "to remain on channel (%u MHz) for Action "
639 static void wpas_send_action_done(void *ctx)
641 struct wpa_supplicant *wpa_s = ctx;
642 wpa_printf(MSG_DEBUG, "P2P: Action frame sequence done notification");
643 wpabuf_free(wpa_s->pending_action_tx);
644 wpa_s->pending_action_tx = NULL;
645 if (wpa_s->off_channel_freq) {
646 wpa_drv_cancel_remain_on_channel(wpa_s);
647 wpa_s->off_channel_freq = 0;
652 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
653 struct p2p_go_neg_results *params)
655 if (wpa_s->go_params == NULL) {
656 wpa_s->go_params = os_malloc(sizeof(*params));
657 if (wpa_s->go_params == NULL)
660 os_memcpy(wpa_s->go_params, params, sizeof(*params));
665 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
666 struct p2p_go_neg_results *res)
668 wpa_supplicant_ap_deinit(wpa_s);
669 wpas_copy_go_neg_results(wpa_s, res);
670 if (res->wps_method == WPS_PBC)
671 wpas_wps_start_pbc(wpa_s, NULL /* res->peer_interface_addr */,
674 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
679 static void p2p_go_configured(void *ctx, void *data)
681 struct wpa_supplicant *wpa_s = ctx;
682 struct p2p_go_neg_results *params = data;
683 struct wpa_ssid *ssid;
685 ssid = wpa_s->current_ssid;
686 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
687 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
688 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
689 "%s GO ssid=\"%s\" passphrase=\"%s\" go_dev_addr="
692 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
693 params->passphrase ? params->passphrase : "",
694 MAC2STR(wpa_s->parent->own_addr),
695 params->persistent_group ? " [PERSISTENT]" : "");
696 if (params->persistent_group)
697 wpas_p2p_store_persistent_group(
699 wpa_s->parent->own_addr);
703 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
704 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
705 params->peer_interface_addr)) {
706 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
710 if (params->wps_method == WPS_PBC)
711 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr);
712 else if (wpa_s->p2p_pin[0])
713 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
714 wpa_s->p2p_pin, NULL, 0);
715 os_free(wpa_s->go_params);
716 wpa_s->go_params = NULL;
720 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
721 struct p2p_go_neg_results *params,
724 struct wpa_ssid *ssid;
726 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
729 ssid = wpa_config_add_network(wpa_s->conf);
733 wpa_config_set_network_defaults(ssid);
736 ssid->p2p_persistent_group = params->persistent_group;
737 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
739 ssid->frequency = params->freq;
740 ssid->ssid = os_zalloc(params->ssid_len + 1);
742 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
743 ssid->ssid_len = params->ssid_len;
745 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
746 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
747 ssid->proto = WPA_PROTO_RSN;
748 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
749 ssid->passphrase = os_strdup(params->passphrase);
751 wpa_s->ap_configured_cb = p2p_go_configured;
752 wpa_s->ap_configured_cb_ctx = wpa_s;
753 wpa_s->ap_configured_cb_data = wpa_s->go_params;
754 wpa_s->connect_without_scan = 1;
755 wpa_s->reassociate = 1;
756 wpa_s->disconnected = 0;
757 wpa_supplicant_req_scan(wpa_s, 0, 0);
761 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
762 const struct wpa_supplicant *src)
764 struct wpa_config *d;
765 const struct wpa_config *s;
770 #define C(n) if (s->n) d->n = os_strdup(s->n)
782 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
783 enum wpa_driver_if_type type)
785 char ifname[120], force_ifname[120];
787 if (wpa_s->pending_interface_name[0]) {
788 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
789 "- skip creation of a new one");
790 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
791 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
792 "unknown?! ifname='%s'",
793 wpa_s->pending_interface_name);
799 os_snprintf(ifname, sizeof(ifname), "%s-p2p-%d", wpa_s->ifname,
800 wpa_s->p2p_group_idx);
801 force_ifname[0] = '\0';
803 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
805 wpa_s->p2p_group_idx++;
807 wpa_s->pending_interface_type = type;
808 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
809 wpa_s->pending_interface_addr) < 0) {
810 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
815 if (force_ifname[0]) {
816 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
818 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
819 sizeof(wpa_s->pending_interface_name));
821 os_strlcpy(wpa_s->pending_interface_name, ifname,
822 sizeof(wpa_s->pending_interface_name));
823 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
824 MACSTR, wpa_s->pending_interface_name,
825 MAC2STR(wpa_s->pending_interface_addr));
831 static void wpas_p2p_remove_pending_group_interface(
832 struct wpa_supplicant *wpa_s)
834 if (!wpa_s->pending_interface_name[0] ||
835 is_zero_ether_addr(wpa_s->pending_interface_addr))
836 return; /* No pending virtual interface */
838 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
839 wpa_s->pending_interface_name);
840 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
841 wpa_s->pending_interface_name);
842 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
843 wpa_s->pending_interface_name[0] = '\0';
847 static struct wpa_supplicant *
848 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
850 struct wpa_interface iface;
851 struct wpa_supplicant *group_wpa_s;
853 if (!wpa_s->pending_interface_name[0]) {
854 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
858 os_memset(&iface, 0, sizeof(iface));
859 iface.ifname = wpa_s->pending_interface_name;
860 iface.driver = wpa_s->driver->name;
861 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
862 iface.driver_param = wpa_s->conf->driver_param;
863 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
864 if (group_wpa_s == NULL) {
865 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
866 "wpa_supplicant interface");
869 wpa_s->pending_interface_name[0] = '\0';
870 group_wpa_s->parent = wpa_s;
871 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
872 P2P_GROUP_INTERFACE_CLIENT;
873 wpa_s->global->p2p_group_formation = group_wpa_s;
875 wpas_p2p_clone_config(group_wpa_s, wpa_s);
881 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
884 struct wpa_supplicant *wpa_s = eloop_ctx;
885 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
886 if (wpa_s->global->p2p)
887 p2p_group_formation_failed(wpa_s->global->p2p);
888 wpas_group_formation_completed(wpa_s, 0);
892 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
894 struct wpa_supplicant *wpa_s = ctx;
896 if (wpa_s->off_channel_freq) {
897 wpa_drv_cancel_remain_on_channel(wpa_s);
898 wpa_s->off_channel_freq = 0;
902 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
904 wpas_p2p_remove_pending_group_interface(wpa_s);
908 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
910 if (wpa_s->create_p2p_iface) {
911 struct wpa_supplicant *group_wpa_s =
912 wpas_p2p_init_group_interface(wpa_s, res->role_go);
913 if (group_wpa_s == NULL) {
914 wpas_p2p_remove_pending_group_interface(wpa_s);
917 if (group_wpa_s != wpa_s)
918 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
919 sizeof(group_wpa_s->p2p_pin));
920 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
921 wpa_s->pending_interface_name[0] = '\0';
922 group_wpa_s->p2p_in_provisioning = 1;
925 wpas_start_wps_go(group_wpa_s, res, 1);
927 wpas_start_wps_enrollee(group_wpa_s, res);
929 wpa_s->p2p_in_provisioning = 1;
930 wpa_s->global->p2p_group_formation = wpa_s;
933 wpas_start_wps_go(wpa_s, res, 1);
935 wpas_start_wps_enrollee(ctx, res);
938 wpa_s->p2p_long_listen = 0;
939 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
941 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
942 /* TODO: add peer Config Timeout */
943 eloop_register_timeout(15, 0, wpas_p2p_group_formation_timeout, wpa_s,
948 void wpas_go_neg_req_rx(void *ctx, const u8 *src)
950 struct wpa_supplicant *wpa_s = ctx;
951 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR,
956 void wpas_dev_found(void *ctx, const u8 *addr, const u8 *dev_addr,
957 const u8 *pri_dev_type, const char *dev_name,
958 u16 config_methods, u8 dev_capab, u8 group_capab)
960 struct wpa_supplicant *wpa_s = ctx;
961 char devtype[WPS_DEV_TYPE_BUFSIZE];
962 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
963 " p2p_dev_addr=" MACSTR
964 " pri_dev_type=%s name='%s' config_methods=0x%x "
965 "dev_capab=0x%x group_capab=0x%x",
966 MAC2STR(addr), MAC2STR(dev_addr),
967 wps_dev_type_bin2str(pri_dev_type, devtype, sizeof(devtype)),
968 dev_name, config_methods, dev_capab, group_capab);
972 static int wpas_start_listen(void *ctx, unsigned int freq,
973 unsigned int duration,
974 const struct wpabuf *probe_resp_ie)
976 struct wpa_supplicant *wpa_s = ctx;
978 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie);
980 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
981 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
982 "report received Probe Request frames");
986 wpa_s->pending_listen_freq = freq;
987 wpa_s->pending_listen_duration = duration;
989 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
990 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
991 "to remain on channel (%u MHz) for Listen "
993 wpa_s->pending_listen_freq = 0;
1001 static void wpas_stop_listen(void *ctx)
1003 struct wpa_supplicant *wpa_s = ctx;
1004 if (wpa_s->off_channel_freq) {
1005 wpa_drv_cancel_remain_on_channel(wpa_s);
1006 wpa_s->off_channel_freq = 0;
1008 wpa_drv_probe_req_report(wpa_s, 0);
1012 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1014 struct wpa_supplicant *wpa_s = ctx;
1015 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
1019 static struct p2p_srv_bonjour *
1020 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1021 const struct wpabuf *query)
1023 struct p2p_srv_bonjour *bsrv;
1026 len = wpabuf_len(query);
1027 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1028 struct p2p_srv_bonjour, list) {
1029 if (len == wpabuf_len(bsrv->query) &&
1030 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1038 static struct p2p_srv_upnp *
1039 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1040 const char *service)
1042 struct p2p_srv_upnp *usrv;
1044 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1045 struct p2p_srv_upnp, list) {
1046 if (version == usrv->version &&
1047 os_strcmp(service, usrv->service) == 0)
1054 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1059 if (wpabuf_tailroom(resp) < 5)
1062 /* Length (to be filled) */
1063 len_pos = wpabuf_put(resp, 2);
1064 wpabuf_put_u8(resp, srv_proto);
1065 wpabuf_put_u8(resp, srv_trans_id);
1067 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1068 /* Response Data: empty */
1069 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1073 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1074 struct wpabuf *resp, u8 srv_trans_id)
1076 struct p2p_srv_bonjour *bsrv;
1079 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1081 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1082 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1086 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1087 struct p2p_srv_bonjour, list) {
1088 if (wpabuf_tailroom(resp) <
1089 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1091 /* Length (to be filled) */
1092 len_pos = wpabuf_put(resp, 2);
1093 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1094 wpabuf_put_u8(resp, srv_trans_id);
1096 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1097 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1098 wpabuf_head(bsrv->resp),
1099 wpabuf_len(bsrv->resp));
1101 wpabuf_put_buf(resp, bsrv->query); /* Key */
1102 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1103 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1109 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1110 struct wpabuf *resp, u8 srv_trans_id,
1111 const u8 *query, size_t query_len)
1113 struct p2p_srv_bonjour *bsrv;
1117 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1119 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1120 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1121 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1126 if (query_len == 0) {
1127 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1131 if (wpabuf_tailroom(resp) < 5)
1133 /* Length (to be filled) */
1134 len_pos = wpabuf_put(resp, 2);
1135 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1136 wpabuf_put_u8(resp, srv_trans_id);
1138 wpabuf_set(&buf, query, query_len);
1139 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1141 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1145 wpabuf_put_u8(resp, P2P_SD_QUERY_DATA_NOT_AVAILABLE);
1146 /* Response Data: empty */
1147 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1153 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1154 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1155 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1157 if (wpabuf_tailroom(resp) >=
1158 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1160 wpabuf_put_buf(resp, bsrv->query); /* Key */
1161 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1163 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1167 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1168 struct wpabuf *resp, u8 srv_trans_id)
1170 struct p2p_srv_upnp *usrv;
1173 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1175 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1176 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1180 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1181 struct p2p_srv_upnp, list) {
1182 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1185 /* Length (to be filled) */
1186 len_pos = wpabuf_put(resp, 2);
1187 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1188 wpabuf_put_u8(resp, srv_trans_id);
1191 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1193 wpabuf_put_u8(resp, usrv->version);
1194 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1196 wpabuf_put_str(resp, usrv->service);
1197 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1203 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1204 struct wpabuf *resp, u8 srv_trans_id,
1205 const u8 *query, size_t query_len)
1207 struct p2p_srv_upnp *usrv;
1213 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1216 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1217 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1218 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1223 if (query_len == 0) {
1224 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1229 str = os_malloc(query_len);
1232 os_memcpy(str, query + 1, query_len - 1);
1233 str[query_len - 1] = '\0';
1235 if (wpabuf_tailroom(resp) < 5)
1238 /* Length (to be filled) */
1239 len_pos = wpabuf_put(resp, 2);
1240 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1241 wpabuf_put_u8(resp, srv_trans_id);
1243 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1244 struct p2p_srv_upnp, list) {
1245 if (version != usrv->version)
1248 if (os_strcmp(str, "ssdp:all") != 0 &&
1249 os_strstr(usrv->service, str) == NULL)
1252 if (wpabuf_tailroom(resp) < 2)
1256 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1258 wpabuf_put_u8(resp, version);
1260 wpabuf_put_u8(resp, ',');
1264 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1266 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1268 wpabuf_put_str(resp, usrv->service);
1272 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1275 wpabuf_put_u8(resp, P2P_SD_QUERY_DATA_NOT_AVAILABLE);
1276 /* Response Data: empty */
1279 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1283 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1284 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1286 struct wpa_supplicant *wpa_s = ctx;
1287 const u8 *pos = tlvs;
1288 const u8 *end = tlvs + tlvs_len;
1291 struct wpabuf *resp;
1292 u8 srv_proto, srv_trans_id;
1296 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1298 buf_len = 2 * tlvs_len + 1;
1299 buf = os_malloc(buf_len);
1301 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1302 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1304 freq, MAC2STR(sa), dialog_token, update_indic,
1309 if (wpa_s->p2p_sd_over_ctrl_iface)
1310 return; /* to be processed by an external program */
1312 resp = wpabuf_alloc(10000);
1316 while (pos + 1 < end) {
1317 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1318 slen = WPA_GET_LE16(pos);
1320 if (pos + slen > end || slen < 2) {
1321 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1326 tlv_end = pos + slen;
1329 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1331 srv_trans_id = *pos++;
1332 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1335 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1336 pos, tlv_end - pos);
1338 switch (srv_proto) {
1339 case P2P_SERV_ALL_SERVICES:
1340 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1341 "for all services");
1342 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1343 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1344 wpa_printf(MSG_DEBUG, "P2P: No service "
1345 "discovery protocols available");
1346 wpas_sd_add_proto_not_avail(
1347 resp, P2P_SERV_ALL_SERVICES,
1351 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1352 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1354 case P2P_SERV_BONJOUR:
1355 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1356 pos, tlv_end - pos);
1359 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1360 pos, tlv_end - pos);
1363 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1364 "protocol %u", srv_proto);
1365 wpas_sd_add_proto_not_avail(resp, srv_proto,
1373 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1379 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1380 const u8 *tlvs, size_t tlvs_len)
1382 struct wpa_supplicant *wpa_s = ctx;
1383 const u8 *pos = tlvs;
1384 const u8 *end = tlvs + tlvs_len;
1390 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1392 buf_len = 2 * tlvs_len + 1;
1393 buf = os_malloc(buf_len);
1395 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1396 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_RESP MACSTR
1398 MAC2STR(sa), update_indic, buf);
1403 u8 srv_proto, srv_trans_id, status;
1405 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1406 slen = WPA_GET_LE16(pos);
1408 if (pos + slen > end || slen < 3) {
1409 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1413 tlv_end = pos + slen;
1416 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1418 srv_trans_id = *pos++;
1419 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1422 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1425 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1426 pos, tlv_end - pos);
1433 void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1434 const struct wpabuf *tlvs)
1436 return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1440 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1441 u8 version, const char *query)
1443 struct wpabuf *tlvs;
1446 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1449 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1450 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1451 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1452 wpabuf_put_u8(tlvs, version);
1453 wpabuf_put_str(tlvs, query);
1454 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1460 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
1462 return p2p_sd_cancel_request(wpa_s->global->p2p, req);
1466 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1467 const u8 *dst, u8 dialog_token,
1468 const struct wpabuf *resp_tlvs)
1470 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1475 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1477 p2p_sd_service_update(wpa_s->global->p2p);
1481 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1483 dl_list_del(&bsrv->list);
1484 wpabuf_free(bsrv->query);
1485 wpabuf_free(bsrv->resp);
1490 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1492 dl_list_del(&usrv->list);
1493 os_free(usrv->service);
1498 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1500 struct p2p_srv_bonjour *bsrv, *bn;
1501 struct p2p_srv_upnp *usrv, *un;
1503 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1504 struct p2p_srv_bonjour, list)
1505 wpas_p2p_srv_bonjour_free(bsrv);
1507 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1508 struct p2p_srv_upnp, list)
1509 wpas_p2p_srv_upnp_free(usrv);
1511 wpas_p2p_sd_service_update(wpa_s);
1515 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1516 struct wpabuf *query, struct wpabuf *resp)
1518 struct p2p_srv_bonjour *bsrv;
1520 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1523 wpabuf_free(bsrv->resp);
1528 bsrv = os_zalloc(sizeof(*bsrv));
1531 bsrv->query = query;
1533 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1535 wpas_p2p_sd_service_update(wpa_s);
1540 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1541 const struct wpabuf *query)
1543 struct p2p_srv_bonjour *bsrv;
1545 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1548 wpas_p2p_srv_bonjour_free(bsrv);
1549 wpas_p2p_sd_service_update(wpa_s);
1554 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1555 const char *service)
1557 struct p2p_srv_upnp *usrv;
1559 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1560 return 0; /* Already listed */
1561 usrv = os_zalloc(sizeof(*usrv));
1564 usrv->version = version;
1565 usrv->service = os_strdup(service);
1566 if (usrv->service == NULL) {
1570 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1572 wpas_p2p_sd_service_update(wpa_s);
1577 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1578 const char *service)
1580 struct p2p_srv_upnp *usrv;
1582 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1585 wpas_p2p_srv_upnp_free(usrv);
1586 wpas_p2p_sd_service_update(wpa_s);
1591 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1592 const u8 *peer, const char *params)
1594 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1595 MAC2STR(peer), wps_generate_pin(), params);
1599 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1600 const u8 *peer, const char *params)
1602 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1603 MAC2STR(peer), params);
1607 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1608 const u8 *dev_addr, const u8 *pri_dev_type,
1609 const char *dev_name, u16 supp_config_methods,
1610 u8 dev_capab, u8 group_capab)
1612 struct wpa_supplicant *wpa_s = ctx;
1613 char devtype[WPS_DEV_TYPE_BUFSIZE];
1615 u8 empty_dev_type[8];
1617 if (pri_dev_type == NULL) {
1618 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1619 pri_dev_type = empty_dev_type;
1621 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1622 " pri_dev_type=%s name='%s' config_methods=0x%x "
1623 "dev_capab=0x%x group_capab=0x%x",
1625 wps_dev_type_bin2str(pri_dev_type, devtype,
1627 dev_name, supp_config_methods, dev_capab, group_capab);
1628 params[sizeof(params) - 1] = '\0';
1630 if (config_methods & WPS_CONFIG_DISPLAY)
1631 wpas_prov_disc_local_display(wpa_s, peer, params);
1632 else if (config_methods & WPS_CONFIG_KEYPAD)
1633 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1634 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1635 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1636 "%s", MAC2STR(peer), params);
1640 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1642 struct wpa_supplicant *wpa_s = ctx;
1643 if (config_methods & WPS_CONFIG_DISPLAY)
1644 wpas_prov_disc_local_keypad(wpa_s, peer, "");
1645 else if (config_methods & WPS_CONFIG_KEYPAD)
1646 wpas_prov_disc_local_display(wpa_s, peer, "");
1647 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1648 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
1653 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1654 const u8 *go_dev_addr, const u8 *ssid,
1655 size_t ssid_len, int *go, u8 *group_bssid,
1656 int *force_freq, int persistent_group)
1658 struct wpa_supplicant *wpa_s = ctx;
1660 u8 cur_bssid[ETH_ALEN];
1663 if (!persistent_group) {
1664 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1665 " to join an active group", MAC2STR(sa));
1667 * Do not accept the invitation automatically; notify user and
1670 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1673 if (!wpa_s->conf->persistent_reconnect)
1674 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1676 for (s = wpa_s->conf->ssid; s; s = s->next) {
1677 if (s->disabled == 2 &&
1678 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1679 s->ssid_len == ssid_len &&
1680 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1685 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1686 " requested reinvocation of an unknown group",
1688 return P2P_SC_FAIL_UNKNOWN_GROUP;
1691 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1693 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1694 wpa_printf(MSG_DEBUG, "P2P: The only available "
1695 "interface is already in use - reject "
1697 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1699 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
1700 } else if (s->mode == WPAS_MODE_P2P_GO) {
1702 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
1704 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
1705 "interface address for the group");
1706 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1708 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
1712 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
1713 wpa_s->assoc_freq) {
1714 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1715 "the channel we are already using");
1716 *force_freq = wpa_s->assoc_freq;
1719 res = wpa_drv_shared_freq(wpa_s);
1721 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1722 "with the channel we are already using on a "
1723 "shared interface");
1727 return P2P_SC_SUCCESS;
1731 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
1732 const u8 *ssid, size_t ssid_len,
1733 const u8 *go_dev_addr, u8 status,
1736 struct wpa_supplicant *wpa_s = ctx;
1739 for (s = wpa_s->conf->ssid; s; s = s->next) {
1740 if (s->disabled == 2 &&
1741 s->ssid_len == ssid_len &&
1742 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1746 if (status == P2P_SC_SUCCESS) {
1747 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1748 " was accepted; op_freq=%d MHz",
1749 MAC2STR(sa), op_freq);
1751 wpas_p2p_group_add_persistent(
1752 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
1757 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
1758 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1759 " was rejected (status %u)", MAC2STR(sa), status);
1765 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1766 "sa=" MACSTR " go_dev_addr=" MACSTR
1767 " bssid=" MACSTR " unknown-network",
1768 MAC2STR(sa), MAC2STR(go_dev_addr),
1771 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1772 "sa=" MACSTR " go_dev_addr=" MACSTR
1774 MAC2STR(sa), MAC2STR(go_dev_addr));
1779 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
1780 " persistent=%d", MAC2STR(sa), s->id);
1784 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
1786 struct wpa_supplicant *wpa_s = ctx;
1787 struct wpa_ssid *ssid;
1790 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1791 "status=%d " MACSTR,
1792 status, MAC2STR(bssid));
1794 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1795 "status=%d ", status);
1798 if (status != P2P_SC_SUCCESS) {
1799 wpas_p2p_remove_pending_group_interface(wpa_s);
1803 ssid = wpa_config_get_network(wpa_s->conf,
1804 wpa_s->pending_invite_ssid_id);
1806 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
1807 "data matching with invitation");
1811 wpas_p2p_group_add_persistent(wpa_s, ssid,
1812 ssid->mode == WPAS_MODE_P2P_GO, 0);
1816 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
1817 struct p2p_config *p2p)
1819 struct hostapd_hw_modes *modes;
1820 u16 num_modes, flags;
1822 int band24 = 0, band5_low = 0, band5_high = 0;
1824 /* TODO: more detailed selection of channels within reg_class based on
1825 * driver capabilities */
1827 modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
1828 if (modes == NULL) {
1829 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
1830 "of all supported channels; assume dualband "
1832 band24 = band5_low = band5_high = 1;
1834 for (i = 0; i < num_modes; i++) {
1835 struct hostapd_hw_modes *mode;
1837 if (mode->mode == HOSTAPD_MODE_IEEE80211G) {
1839 } else if (mode->mode == HOSTAPD_MODE_IEEE80211A) {
1841 for (j = 0; j < mode->num_channels; j++) {
1842 struct hostapd_channel_data *ch;
1843 ch = &mode->channels[j];
1846 else if (ch->chan == 157)
1856 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
1859 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1860 p2p->channels.reg_class[cla].reg_class = 81;
1861 p2p->channels.reg_class[cla].channels = 13;
1862 for (i = 0; i < 13; i++)
1863 p2p->channels.reg_class[cla].channel[i] = i + 1;
1866 /* Operating class 82 - 2.4 GHz band channel 14 */
1867 p2p->channels.reg_class[cla].reg_class = 82;
1868 p2p->channels.reg_class[cla].channels = 1;
1869 p2p->channels.reg_class[cla].channel[0] = 14;
1873 /* Operating class 83 - 2.4 GHz band channels 1..9; 40 MHz */
1874 p2p->channels.reg_class[cla].reg_class = 83;
1875 p2p->channels.reg_class[cla].channels = 9;
1876 for (i = 0; i < 9; i++)
1877 p2p->channels.reg_class[cla].channel[i] = i + 1;
1880 /* Operating class 84 - 2.4 GHz band channels 5..13; 40 MHz */
1881 p2p->channels.reg_class[cla].reg_class = 84;
1882 p2p->channels.reg_class[cla].channels = 9;
1883 for (i = 0; i < 9; i++)
1884 p2p->channels.reg_class[cla].channel[i] = i + 5;
1890 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
1891 "lower 5 GHz band");
1893 /* Operating class 115 - 5 GHz, channels 36-48 */
1894 p2p->channels.reg_class[cla].reg_class = 115;
1895 p2p->channels.reg_class[cla].channels = 4;
1896 p2p->channels.reg_class[cla].channel[0] = 36;
1897 p2p->channels.reg_class[cla].channel[1] = 40;
1898 p2p->channels.reg_class[cla].channel[2] = 44;
1899 p2p->channels.reg_class[cla].channel[3] = 48;
1903 /* Operating class 116 - 5 GHz, channels 36,44; 40 MHz */
1904 p2p->channels.reg_class[cla].reg_class = 116;
1905 p2p->channels.reg_class[cla].channels = 2;
1906 p2p->channels.reg_class[cla].channel[0] = 36;
1907 p2p->channels.reg_class[cla].channel[1] = 44;
1910 /* Operating class 117 - 5 GHz, channels 40,48; 40 MHz */
1911 p2p->channels.reg_class[cla].reg_class = 117;
1912 p2p->channels.reg_class[cla].channels = 2;
1913 p2p->channels.reg_class[cla].channel[0] = 40;
1914 p2p->channels.reg_class[cla].channel[1] = 48;
1920 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
1921 "higher 5 GHz band");
1923 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1924 p2p->channels.reg_class[cla].reg_class = 124;
1925 p2p->channels.reg_class[cla].channels = 4;
1926 p2p->channels.reg_class[cla].channel[0] = 149;
1927 p2p->channels.reg_class[cla].channel[1] = 153;
1928 p2p->channels.reg_class[cla].channel[2] = 157;
1929 p2p->channels.reg_class[cla].channel[3] = 161;
1933 /* Operating class 126 - 5 GHz, channels 149,157; 40 MHz */
1934 p2p->channels.reg_class[cla].reg_class = 126;
1935 p2p->channels.reg_class[cla].channels = 2;
1936 p2p->channels.reg_class[cla].channel[0] = 149;
1937 p2p->channels.reg_class[cla].channel[1] = 157;
1940 /* Operating class 127 - 5 GHz, channels 153,161; 40 MHz */
1941 p2p->channels.reg_class[cla].reg_class = 127;
1942 p2p->channels.reg_class[cla].channels = 2;
1943 p2p->channels.reg_class[cla].channel[0] = 153;
1944 p2p->channels.reg_class[cla].channel[1] = 161;
1949 p2p->channels.reg_classes = cla;
1952 ieee80211_sta_free_hw_features(modes, num_modes);
1958 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
1961 struct wpa_supplicant *wpa_s = ctx;
1963 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
1964 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
1970 return wpa_drv_get_noa(wpa_s, buf, buf_len);
1975 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
1976 * @global: Pointer to global data from wpa_supplicant_init()
1977 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
1978 * Returns: 0 on success, -1 on failure
1980 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
1982 struct p2p_config p2p;
1986 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
1989 #ifdef CONFIG_CLIENT_MLME
1990 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
1991 wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
1992 wpa_s->mlme.public_action_cb_ctx = wpa_s;
1994 #endif /* CONFIG_CLIENT_MLME */
1996 if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
1997 wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
1998 /* Continue anyway; this is not really a fatal error */
2004 os_memset(&p2p, 0, sizeof(p2p));
2005 p2p.msg_ctx = wpa_s;
2007 p2p.p2p_scan = wpas_p2p_scan;
2008 p2p.send_action = wpas_send_action;
2009 p2p.send_action_done = wpas_send_action_done;
2010 p2p.go_neg_completed = wpas_go_neg_completed;
2011 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2012 p2p.dev_found = wpas_dev_found;
2013 p2p.start_listen = wpas_start_listen;
2014 p2p.stop_listen = wpas_stop_listen;
2015 p2p.send_probe_resp = wpas_send_probe_resp;
2016 p2p.sd_request = wpas_sd_request;
2017 p2p.sd_response = wpas_sd_response;
2018 p2p.prov_disc_req = wpas_prov_disc_req;
2019 p2p.prov_disc_resp = wpas_prov_disc_resp;
2020 p2p.invitation_process = wpas_invitation_process;
2021 p2p.invitation_received = wpas_invitation_received;
2022 p2p.invitation_result = wpas_invitation_result;
2023 p2p.get_noa = wpas_get_noa;
2025 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2026 os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
2027 p2p.dev_name = wpa_s->conf->device_name;
2029 if (wpa_s->conf->p2p_listen_reg_class &&
2030 wpa_s->conf->p2p_listen_channel) {
2031 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2032 p2p.channel = wpa_s->conf->p2p_listen_channel;
2036 * Pick one of the social channels randomly as the listen
2039 os_get_random((u8 *) &r, sizeof(r));
2040 p2p.channel = 1 + (r % 3) * 5;
2043 if (wpa_s->conf->p2p_oper_reg_class &&
2044 wpa_s->conf->p2p_oper_channel) {
2045 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2046 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2048 p2p.op_reg_class = 81;
2050 * For initial tests, pick the operation channel randomly.
2051 * TODO: Use scan results (etc.) to select the best channel.
2053 p2p.op_channel = 1 + r % 11;
2055 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d "
2056 "Own preferred operation channel: %d",
2057 p2p.channel, p2p.op_channel);
2058 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2059 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2060 p2p.country[2] = 0x04;
2062 os_memcpy(p2p.country, "US\x04", 3);
2064 if (wpas_p2p_setup_channels(wpa_s, &p2p)) {
2065 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2070 if (wpa_s->conf->device_type &&
2071 wps_dev_type_str2bin(wpa_s->conf->device_type, p2p.pri_dev_type) <
2073 wpa_printf(MSG_ERROR, "P2P: Invalid device_type");
2077 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
2078 if (wpa_s->conf->sec_device_type[i] == NULL)
2080 if (wps_dev_type_str2bin(
2081 wpa_s->conf->sec_device_type[i],
2082 p2p.sec_dev_type[p2p.num_sec_dev_types]) < 0) {
2083 wpa_printf(MSG_ERROR, "P2P: Invalid sec_device_type");
2086 p2p.num_sec_dev_types++;
2087 if (p2p.num_sec_dev_types == P2P_SEC_DEVICE_TYPES)
2091 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2092 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2094 p2p.max_peers = 100;
2096 if (wpa_s->conf->p2p_ssid_postfix) {
2097 p2p.ssid_postfix_len =
2098 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2099 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2100 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2101 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2102 p2p.ssid_postfix_len);
2105 global->p2p = p2p_init(&p2p);
2106 if (global->p2p == NULL)
2114 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2115 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2117 * This function deinitialize per-interface P2P data.
2119 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2121 if (wpa_s->driver && wpa_s->drv_priv)
2122 wpa_drv_probe_req_report(wpa_s, 0);
2123 os_free(wpa_s->go_params);
2124 wpa_s->go_params = NULL;
2125 wpabuf_free(wpa_s->pending_action_tx);
2126 wpa_s->pending_action_tx = NULL;
2127 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
2128 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2129 wpa_s->p2p_long_listen = 0;
2130 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2131 wpas_p2p_remove_pending_group_interface(wpa_s);
2133 /* TODO: remove group interface from the driver if this wpa_s instance
2134 * is on top of a P2P group interface */
2139 * wpas_p2p_deinit_global - Deinitialize global P2P module
2140 * @global: Pointer to global data from wpa_supplicant_init()
2142 * This function deinitializes the global (per device) P2P module.
2144 void wpas_p2p_deinit_global(struct wpa_global *global)
2146 struct wpa_supplicant *wpa_s, *tmp;
2149 if (global->p2p == NULL)
2152 /* Remove remaining P2P group interfaces */
2153 wpa_s = global->ifaces;
2154 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2155 wpa_s = wpa_s->next;
2157 enum wpa_driver_if_type type;
2158 tmp = global->ifaces;
2161 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2166 ifname = os_strdup(tmp->ifname);
2167 type = wpas_p2p_if_type(tmp->p2p_group_interface);
2168 wpa_supplicant_remove_iface(global, tmp);
2170 wpa_drv_if_remove(wpa_s, type, ifname);
2174 p2p_deinit(global->p2p);
2179 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2181 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
2182 return 1; /* P2P group requires a new interface in every case
2184 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2185 return 0; /* driver does not support concurrent operations */
2186 if (wpa_s->global->ifaces->next)
2187 return 1; /* more that one interface already in use */
2188 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2189 return 1; /* this interface is already in use */
2194 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2195 const u8 *peer_addr,
2196 enum p2p_wps_method wps_method,
2197 int go_intent, const u8 *own_interface_addr,
2198 unsigned int force_freq, int persistent_group)
2200 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2201 go_intent, own_interface_addr, force_freq,
2206 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2207 const u8 *peer_addr,
2208 enum p2p_wps_method wps_method,
2209 int go_intent, const u8 *own_interface_addr,
2210 unsigned int force_freq, int persistent_group)
2212 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2213 go_intent, own_interface_addr, force_freq,
2218 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2219 const u8 *dev_addr, enum p2p_wps_method wps_method)
2221 struct wpa_bss *bss;
2223 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2224 MACSTR " dev " MACSTR ")",
2225 MAC2STR(iface_addr), MAC2STR(dev_addr));
2227 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2228 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2229 wpa_s->pending_join_wps_method = wps_method;
2231 /* Make sure we are not running find during connection establishment */
2232 wpas_p2p_stop_find(wpa_s);
2234 bss = wpa_bss_get_bssid(wpa_s, iface_addr);
2238 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2239 "prior to joining an existing group (GO " MACSTR
2241 MAC2STR(dev_addr), bss->freq);
2242 wpa_s->pending_pd_before_join = 1;
2244 switch (wps_method) {
2246 case WPS_PIN_DISPLAY:
2247 method = WPS_CONFIG_KEYPAD;
2249 case WPS_PIN_KEYPAD:
2250 method = WPS_CONFIG_DISPLAY;
2253 method = WPS_CONFIG_PUSHBUTTON;
2260 if (p2p_prov_disc_req(wpa_s->global->p2p, dev_addr, method, 1)
2262 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2263 "Discovery Request before joining an "
2265 wpa_s->pending_pd_before_join = 0;
2270 * Actual join operation will be started from the Action frame
2271 * TX status callback.
2276 wpa_printf(MSG_DEBUG, "P2P: Target BSS/GO not yet in BSS table - "
2277 "cannot send Provision Discovery Request");
2280 /* Start join operation immediately */
2281 return wpas_p2p_join_start(wpa_s);
2285 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2287 struct wpa_supplicant *group;
2288 struct p2p_go_neg_results res;
2290 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2294 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2295 sizeof(group->p2p_pin));
2297 group->p2p_in_provisioning = 1;
2299 os_memset(&res, 0, sizeof(res));
2300 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2302 res.wps_method = wpa_s->pending_join_wps_method;
2303 wpas_start_wps_enrollee(group, &res);
2310 * wpas_p2p_connect - Request P2P Group Formation to be started
2311 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2312 * @peer_addr: Address of the peer P2P Device
2313 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2314 * @persistent_group: Whether to create a persistent group
2315 * @join: Whether to join an existing group (as a client) instead of starting
2316 * Group Owner negotiation; @peer_addr is BSSID in that case
2317 * @auth: Whether to only authorize the connection instead of doing that and
2318 * initiating Group Owner negotiation
2319 * @go_intent: GO Intent or -1 to use default
2320 * @freq: Frequency for the group or 0 for auto-selection
2321 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on failure
2323 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2324 const char *pin, enum p2p_wps_method wps_method,
2325 int persistent_group, int join, int auth, int go_intent,
2331 enum wpa_driver_if_type iftype;
2334 go_intent = wpa_s->conf->p2p_go_intent;
2337 wpa_s->p2p_long_listen = 0;
2340 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
2341 else if (wps_method == WPS_PIN_DISPLAY) {
2342 ret = wps_generate_pin();
2343 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
2345 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
2348 wpa_s->p2p_pin[0] = '\0';
2351 u8 iface_addr[ETH_ALEN];
2352 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
2354 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
2355 if (wpas_p2p_join(wpa_s, iface_addr, peer_addr, wps_method) <
2363 else if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2365 force_freq = wpa_s->assoc_freq;
2367 force_freq = wpa_drv_shared_freq(wpa_s);
2372 if (force_freq > 0) {
2373 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2374 "channel we are already using (%u MHz) on another "
2375 "interface", force_freq);
2378 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
2380 if (!wpa_s->create_p2p_iface) {
2382 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2383 go_intent, wpa_s->own_addr,
2384 force_freq, persistent_group)
2389 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
2390 go_intent, wpa_s->own_addr,
2391 force_freq, persistent_group) < 0)
2396 /* Prepare to add a new interface for the group */
2397 iftype = WPA_IF_P2P_GROUP;
2399 iftype = WPA_IF_P2P_CLIENT;
2400 else if (go_intent == 15)
2401 iftype = WPA_IF_P2P_GO;
2402 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
2403 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2404 "interface for the group");
2409 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2411 wpa_s->pending_interface_addr,
2412 force_freq, persistent_group) < 0)
2416 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
2417 wpa_s->pending_interface_addr,
2418 force_freq, persistent_group) < 0) {
2419 wpas_p2p_remove_pending_group_interface(wpa_s);
2427 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2428 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2429 * @freq: Frequency of the channel in MHz
2430 * @duration: Duration of the stay on the channel in milliseconds
2432 * This callback is called when the driver indicates that it has started the
2433 * requested remain-on-channel duration.
2435 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2436 unsigned int freq, unsigned int duration)
2438 wpa_s->off_channel_freq = freq;
2439 wpas_send_action_cb(wpa_s, NULL);
2440 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
2441 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
2442 wpa_s->pending_listen_duration);
2443 wpa_s->pending_listen_freq = 0;
2448 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
2449 unsigned int timeout)
2451 /* Limit maximum Listen state time based on driver limitation. */
2452 if (timeout > wpa_s->max_remain_on_chan)
2453 timeout = wpa_s->max_remain_on_chan;
2455 return p2p_listen(wpa_s->global->p2p, timeout);
2460 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
2461 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2462 * @freq: Frequency of the channel in MHz
2464 * This callback is called when the driver indicates that a remain-on-channel
2465 * operation has been completed, i.e., the duration on the requested channel
2468 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2471 wpa_s->off_channel_freq = 0;
2472 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
2473 return; /* P2P module started a new operation */
2474 if (wpa_s->pending_action_tx)
2476 if (wpa_s->p2p_long_listen > 0)
2477 wpa_s->p2p_long_listen -= 5;
2478 if (wpa_s->p2p_long_listen > 0) {
2479 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
2480 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen * 1000);
2486 * wpas_p2p_group_remove - Remove a P2P group
2487 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2488 * @ifname: Network interface name of the group interface or "*" to remove all
2490 * Returns: 0 on success, -1 on failure
2492 * This function is used to remove a P2P group. This can be used to disconnect
2493 * from a group in which the local end is a P2P Client or to end a P2P Group in
2494 * case the local end is the Group Owner. If a virtual network interface was
2495 * created for this group, that interface will be removed. Otherwise, only the
2496 * configured P2P group network will be removed from the interface.
2498 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
2500 struct wpa_global *global = wpa_s->global;
2502 if (os_strcmp(ifname, "*") == 0) {
2503 struct wpa_supplicant *prev;
2504 wpa_s = global->ifaces;
2507 wpa_s = wpa_s->next;
2508 wpas_p2p_group_delete(prev);
2513 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2514 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2521 wpas_p2p_group_delete(wpa_s);
2527 static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
2528 struct p2p_go_neg_results *params,
2534 os_memset(params, 0, sizeof(*params));
2535 params->role_go = 1;
2536 params->freq = 2412;
2538 params->freq = freq;
2539 else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
2540 wpa_s->conf->p2p_oper_channel >= 1 &&
2541 wpa_s->conf->p2p_oper_channel <= 11)
2542 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
2543 else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
2544 wpa_s->conf->p2p_oper_reg_class == 118)
2545 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
2546 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2547 wpa_s->assoc_freq && !freq) {
2548 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2550 params->freq = wpa_s->assoc_freq;
2553 res = wpa_drv_shared_freq(wpa_s);
2554 if (res > 0 && !freq) {
2555 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2556 "already using on a shared interface");
2562 static struct wpa_supplicant *
2563 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
2566 struct wpa_supplicant *group_wpa_s;
2568 if (!wpas_p2p_create_iface(wpa_s))
2571 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2572 WPA_IF_P2P_CLIENT) < 0)
2574 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
2575 if (group_wpa_s == NULL) {
2576 wpas_p2p_remove_pending_group_interface(wpa_s);
2585 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
2586 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2587 * @persistent_group: Whether to create a persistent group
2588 * @freq: Frequency for the group or 0 to indicate no hardcoding
2589 * Returns: 0 on success, -1 on failure
2591 * This function creates a new P2P group with the local end as the Group Owner,
2592 * i.e., without using Group Owner Negotiation.
2594 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
2597 struct p2p_go_neg_results params;
2599 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
2600 p2p_go_params(wpa_s->global->p2p, ¶ms);
2601 params.persistent_group = persistent_group;
2603 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
2606 wpas_start_wps_go(wpa_s, ¶ms, 0);
2612 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
2613 struct wpa_ssid *params, int addr_allocated)
2615 struct wpa_ssid *ssid;
2617 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
2621 wpa_supplicant_ap_deinit(wpa_s);
2623 ssid = wpa_config_add_network(wpa_s->conf);
2626 wpas_notify_network_added(wpa_s, ssid);
2627 wpa_config_set_network_defaults(ssid);
2628 ssid->temporary = 1;
2629 ssid->proto = WPA_PROTO_RSN;
2630 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
2631 ssid->group_cipher = WPA_CIPHER_CCMP;
2632 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
2633 ssid->ssid = os_malloc(params->ssid_len);
2634 if (ssid->ssid == NULL) {
2635 wpas_notify_network_removed(wpa_s, ssid);
2636 wpa_config_remove_network(wpa_s->conf, ssid->id);
2639 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2640 ssid->ssid_len = params->ssid_len;
2641 ssid->p2p_group = 1;
2642 if (params->psk_set) {
2643 os_memcpy(ssid->psk, params->psk, 32);
2646 if (params->passphrase)
2647 ssid->passphrase = os_strdup(params->passphrase);
2649 wpa_supplicant_select_network(wpa_s, ssid);
2651 wpa_s->show_group_started = 1;
2657 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
2658 struct wpa_ssid *ssid, int addr_allocated,
2661 struct p2p_go_neg_results params;
2663 if (ssid->disabled != 2 || ssid->ssid == NULL)
2666 wpa_s->p2p_long_listen = 0;
2667 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2669 if (ssid->mode == WPAS_MODE_INFRA)
2670 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
2672 if (ssid->mode != WPAS_MODE_P2P_GO)
2675 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
2678 if (ssid->passphrase == NULL ||
2679 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
2680 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
2684 os_strlcpy(params.passphrase, ssid->passphrase,
2685 sizeof(params.passphrase));
2686 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
2687 params.ssid_len = ssid->ssid_len;
2688 params.persistent_group = 1;
2690 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
2694 wpas_start_wps_go(wpa_s, ¶ms, 0);
2700 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
2701 struct wpabuf *proberesp_ies)
2703 struct wpa_supplicant *wpa_s = ctx;
2704 if (wpa_s->ap_iface) {
2705 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
2707 wpabuf_free(hapd->p2p_beacon_ie);
2708 hapd->p2p_beacon_ie = beacon_ies;
2710 wpabuf_free(hapd->p2p_probe_resp_ie);
2711 hapd->p2p_probe_resp_ie = proberesp_ies;
2713 wpabuf_free(beacon_ies);
2714 wpabuf_free(proberesp_ies);
2716 wpa_supplicant_ap_update_beacon(wpa_s);
2720 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
2721 int persistent_group,
2722 int group_formation)
2724 struct p2p_group *group;
2725 struct p2p_group_config *cfg;
2727 cfg = os_zalloc(sizeof(*cfg));
2731 cfg->persistent_group = persistent_group;
2732 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
2733 cfg->cb_ctx = wpa_s;
2734 cfg->ie_update = wpas_p2p_ie_update;
2736 group = p2p_group_init(wpa_s->global->p2p, cfg);
2739 if (!group_formation)
2740 p2p_group_notif_formation_done(group);
2741 wpa_s->p2p_group = group;
2746 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2749 if (!wpa_s->p2p_in_provisioning) {
2750 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
2751 "provisioning not in progress");
2755 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
2757 if (wpa_s->global->p2p)
2758 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
2759 wpas_group_formation_completed(wpa_s, 1);
2763 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2764 const char *config_method)
2768 if (os_strcmp(config_method, "display") == 0)
2769 config_methods = WPS_CONFIG_DISPLAY;
2770 else if (os_strcmp(config_method, "keypad") == 0)
2771 config_methods = WPS_CONFIG_KEYPAD;
2772 else if (os_strcmp(config_method, "pbc") == 0 ||
2773 os_strcmp(config_method, "pushbutton") == 0)
2774 config_methods = WPS_CONFIG_PUSHBUTTON;
2778 if (wpa_s->global->p2p == NULL)
2781 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
2786 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
2789 return p2p_scan_result_text(ies, ies_len, buf, end);
2793 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
2794 enum p2p_discovery_type type)
2796 wpa_s->p2p_long_listen = 0;
2798 return p2p_find(wpa_s->global->p2p, timeout, type);
2802 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
2804 wpa_s->p2p_long_listen = 0;
2806 p2p_stop_find(wpa_s->global->p2p);
2808 wpas_p2p_remove_pending_group_interface(wpa_s);
2812 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
2814 struct wpa_supplicant *wpa_s = eloop_ctx;
2815 wpa_s->p2p_long_listen = 0;
2819 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
2825 * This is a request for unlimited Listen state. However, at
2826 * least for now, this is mapped to a Listen state for one
2831 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2832 wpa_s->p2p_long_listen = 0;
2834 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
2835 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
2836 wpa_s->p2p_long_listen = timeout;
2837 eloop_register_timeout(timeout, 0,
2838 wpas_p2p_long_listen_timeout,
2846 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, const u8 *bssid,
2847 u8 *buf, size_t len, int p2p_group)
2849 if (wpa_s->global->p2p_disabled)
2851 if (wpa_s->global->p2p == NULL)
2854 return p2p_assoc_req_ie(wpa_s->global->p2p, bssid, buf, len,
2859 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
2860 const u8 *ie, size_t ie_len)
2862 if (wpa_s->global->p2p_disabled)
2864 if (wpa_s->global->p2p == NULL)
2867 return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len);
2871 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
2872 const u8 *sa, const u8 *bssid,
2873 u8 category, const u8 *data, size_t len, int freq)
2875 if (wpa_s->global->p2p_disabled)
2877 if (wpa_s->global->p2p == NULL)
2880 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
2885 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
2887 if (wpa_s->global->p2p_disabled)
2889 if (wpa_s->global->p2p == NULL)
2892 p2p_scan_ie(wpa_s->global->p2p, ies);
2896 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
2898 p2p_group_deinit(wpa_s->p2p_group);
2899 wpa_s->p2p_group = NULL;
2903 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
2905 wpa_s->p2p_long_listen = 0;
2907 return p2p_reject(wpa_s->global->p2p, addr);
2911 /* Invite to reinvoke a persistent group */
2912 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2913 struct wpa_ssid *ssid, const u8 *go_dev_addr)
2915 enum p2p_invite_role role;
2918 if (ssid->mode == WPAS_MODE_P2P_GO) {
2919 role = P2P_INVITE_ROLE_GO;
2920 if (peer_addr == NULL) {
2921 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
2922 "address in invitation command");
2925 if (wpas_p2p_create_iface(wpa_s)) {
2926 if (wpas_p2p_add_group_interface(wpa_s,
2927 WPA_IF_P2P_GO) < 0) {
2928 wpa_printf(MSG_ERROR, "P2P: Failed to "
2929 "allocate a new interface for the "
2933 bssid = wpa_s->pending_interface_addr;
2935 bssid = wpa_s->own_addr;
2937 role = P2P_INVITE_ROLE_CLIENT;
2938 peer_addr = ssid->bssid;
2940 wpa_s->pending_invite_ssid_id = ssid->id;
2942 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
2943 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
2947 /* Invite to join an active group */
2948 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
2949 const u8 *peer_addr, const u8 *go_dev_addr)
2951 struct wpa_global *global = wpa_s->global;
2952 enum p2p_invite_role role;
2954 struct wpa_ssid *ssid;
2956 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2957 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2960 if (wpa_s == NULL) {
2961 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
2965 ssid = wpa_s->current_ssid;
2967 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
2972 if (ssid->mode == WPAS_MODE_P2P_GO) {
2973 role = P2P_INVITE_ROLE_ACTIVE_GO;
2974 bssid = wpa_s->own_addr;
2975 if (go_dev_addr == NULL)
2976 go_dev_addr = wpa_s->own_addr;
2978 role = P2P_INVITE_ROLE_CLIENT;
2979 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
2980 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
2981 "invite to current group");
2984 bssid = wpa_s->bssid;
2985 if (go_dev_addr == NULL &&
2986 !is_zero_ether_addr(wpa_s->go_dev_addr))
2987 go_dev_addr = wpa_s->go_dev_addr;
2989 wpa_s->pending_invite_ssid_id = -1;
2991 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
2992 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 0);
2996 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
2998 struct wpa_ssid *ssid = wpa_s->current_ssid;
2999 const char *ssid_txt;
3000 u8 go_dev_addr[ETH_ALEN];
3003 if (!wpa_s->show_group_started || !ssid)
3006 wpa_s->show_group_started = 0;
3008 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
3009 os_memset(go_dev_addr, 0, ETH_ALEN);
3010 if (ssid->bssid_set)
3011 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
3012 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3014 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
3016 if (ssid->passphrase == NULL && ssid->psk_set) {
3018 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
3019 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3020 "%s client ssid=\"%s\" psk=%s go_dev_addr=" MACSTR
3022 wpa_s->ifname, ssid_txt, psk, MAC2STR(go_dev_addr),
3023 persistent ? " [PERSISTENT]" : "");
3025 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3026 "%s client ssid=\"%s\" passphrase=\"%s\" go_dev_addr="
3028 wpa_s->ifname, ssid_txt,
3029 ssid->passphrase ? ssid->passphrase : "",
3030 MAC2STR(go_dev_addr),
3031 persistent ? " [PERSISTENT]" : "");
3035 wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
3040 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
3041 u32 interval1, u32 duration2, u32 interval2)
3043 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3044 wpa_s->current_ssid == NULL ||
3045 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
3048 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
3049 wpa_s->own_addr, wpa_s->assoc_freq,
3050 duration1, interval1, duration2, interval2);
3054 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
3055 unsigned int interval)
3057 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
3061 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3062 u16 reason_code, const u8 *ie, size_t ie_len)
3064 if (wpa_s->global->p2p_disabled)
3067 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3071 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3072 u16 reason_code, const u8 *ie, size_t ie_len)
3074 if (wpa_s->global->p2p_disabled)
3077 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3081 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
3083 struct p2p_data *p2p = wpa_s->global->p2p;
3088 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
3089 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
3091 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
3093 if (wpa_s->conf->device_type) {
3094 if (wps_dev_type_str2bin(wpa_s->conf->device_type,
3095 pri_dev_type) < 0) {
3096 wpa_printf(MSG_ERROR, "P2P: Invalid "
3099 p2p_set_pri_dev_type(p2p, pri_dev_type);
3103 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
3104 u8 sec_dev_type[P2P_SEC_DEVICE_TYPES][8];
3107 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
3108 if (wpa_s->conf->sec_device_type[i] == NULL)
3110 if (wps_dev_type_str2bin(
3111 wpa_s->conf->sec_device_type[i],
3112 sec_dev_type[num]) < 0) {
3113 wpa_printf(MSG_ERROR, "P2P: Invalid "
3118 if (num == P2P_SEC_DEVICE_TYPES)
3121 p2p_set_sec_dev_types(p2p, (void *) sec_dev_type, num);
3124 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
3125 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3127 country[0] = wpa_s->conf->country[0];
3128 country[1] = wpa_s->conf->country[1];
3130 p2p_set_country(p2p, country);
3133 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
3134 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
3135 wpa_s->conf->p2p_ssid_postfix ?
3136 os_strlen(wpa_s->conf->p2p_ssid_postfix) :