3 * Copyright (c) 2009-2010, Atheros Communications
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #include "common/ieee802_11_common.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/wpa_ctrl.h"
22 #include "wps/wps_i.h"
24 #include "ap/hostapd.h"
25 #include "ap/p2p_hostapd.h"
26 #include "wpa_supplicant_i.h"
29 #include "config_ssid.h"
35 #include "wps_supplicant.h"
36 #include "p2p_supplicant.h"
39 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
40 static struct wpa_supplicant *
41 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
43 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
44 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
45 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
46 const u8 *dev_addr, enum p2p_wps_method wps_method);
47 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
48 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
49 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
50 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
53 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
54 struct wpa_scan_results *scan_res)
58 if (wpa_s->global->p2p_disabled)
61 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
64 for (i = 0; i < scan_res->num; i++) {
65 struct wpa_scan_res *bss = scan_res->res[i];
66 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
67 bss->freq, bss->level,
68 (const u8 *) (bss + 1),
73 p2p_scan_res_handled(wpa_s->global->p2p);
77 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq)
79 struct wpa_supplicant *wpa_s = ctx;
80 struct wpa_driver_scan_params params;
82 struct wpabuf *wps_ie, *ies;
83 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
85 os_memset(¶ms, 0, sizeof(params));
87 /* P2P Wildcard SSID */
89 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
90 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
92 wpa_s->wps->dev.p2p = 1;
93 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
98 ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
103 wpabuf_put_buf(ies, wps_ie);
106 p2p_scan_ie(wpa_s->global->p2p, ies);
108 params.extra_ies = wpabuf_head(ies);
109 params.extra_ies_len = wpabuf_len(ies);
112 case P2P_SCAN_SOCIAL:
113 params.freqs = social_channels;
117 case P2P_SCAN_SPECIFIC:
118 social_channels[0] = freq;
119 social_channels[1] = 0;
120 params.freqs = social_channels;
122 case P2P_SCAN_SOCIAL_PLUS_ONE:
123 social_channels[3] = freq;
124 params.freqs = social_channels;
128 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
129 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
130 ret = ieee80211_sta_req_scan(wpa_s, ¶ms);
132 ret = wpa_drv_scan(wpa_s, ¶ms);
140 #ifdef CONFIG_CLIENT_MLME
141 static void p2p_rx_action_mlme(void *ctx, const u8 *buf, size_t len, int freq)
143 struct wpa_supplicant *wpa_s = ctx;
144 const struct ieee80211_mgmt *mgmt;
147 if (wpa_s->global->p2p_disabled)
149 mgmt = (const struct ieee80211_mgmt *) buf;
150 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
153 p2p_rx_action(wpa_s->global->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
154 mgmt->u.action.category,
155 &mgmt->u.action.u.vs_public_action.action,
156 len - hdr_len, freq);
158 #endif /* CONFIG_CLIENT_MLME */
161 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
163 switch (p2p_group_interface) {
164 case P2P_GROUP_INTERFACE_PENDING:
165 return WPA_IF_P2P_GROUP;
166 case P2P_GROUP_INTERFACE_GO:
167 return WPA_IF_P2P_GO;
168 case P2P_GROUP_INTERFACE_CLIENT:
169 return WPA_IF_P2P_CLIENT;
172 return WPA_IF_P2P_GROUP;
176 static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
178 struct wpa_ssid *ssid;
182 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
184 ssid = wpa_s->current_ssid;
187 * The current SSID was not known, but there may still be a
188 * pending P2P group interface waiting for provisioning.
190 ssid = wpa_s->conf->ssid;
192 if (ssid->p2p_group &&
193 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
194 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
199 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
201 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
202 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
203 wpa_s->reassociate = 0;
204 wpa_s->disconnected = 1;
205 wpa_supplicant_deauthenticate(wpa_s,
206 WLAN_REASON_DEAUTH_LEAVING);
210 if (wpa_s->cross_connect_in_use) {
211 wpa_s->cross_connect_in_use = 0;
212 wpa_msg(wpa_s->parent, MSG_INFO,
213 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
214 wpa_s->ifname, wpa_s->cross_connect_uplink);
216 switch (wpa_s->removal_reason) {
217 case P2P_GROUP_REMOVAL_REQUESTED:
218 reason = " reason=REQUESTED";
220 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
221 reason = " reason=IDLE";
223 case P2P_GROUP_REMOVAL_UNAVAILABLE:
224 reason = " reason=UNAVAILABLE";
230 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s%s",
231 wpa_s->ifname, gtype, reason);
232 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
233 struct wpa_global *global;
235 enum wpa_driver_if_type type;
236 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
238 global = wpa_s->global;
239 ifname = os_strdup(wpa_s->ifname);
240 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
241 wpa_supplicant_remove_iface(wpa_s->global, wpa_s);
242 wpa_s = global->ifaces;
244 wpa_drv_if_remove(wpa_s, type, ifname);
249 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
250 if (ssid && (ssid->p2p_group ||
251 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
252 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
254 if (ssid == wpa_s->current_ssid)
255 wpa_s->current_ssid = NULL;
256 wpas_notify_network_removed(wpa_s, ssid);
257 wpa_config_remove_network(wpa_s->conf, id);
258 wpa_supplicant_clear_status(wpa_s);
260 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
263 wpa_supplicant_ap_deinit(wpa_s);
267 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
269 const u8 *ssid, size_t ssid_len)
277 if (wpa_s->go_params)
278 bssid = wpa_s->go_params->peer_interface_addr;
280 bssid = wpa_s->bssid;
282 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
284 u8 iface_addr[ETH_ALEN];
285 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
287 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
290 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
291 "group is persistent - BSS " MACSTR " not found",
296 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
298 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
299 "group is persistent - BSS " MACSTR
300 " did not include P2P IE", MAC2STR(bssid));
301 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
302 (u8 *) (bss + 1), bss->ie_len);
303 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
304 ((u8 *) bss + 1) + bss->ie_len,
309 group_capab = p2p_get_group_capab(p2p);
310 addr = p2p_get_go_dev_addr(p2p);
311 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
312 "group_capab=0x%x", group_capab);
314 os_memcpy(go_dev_addr, addr, ETH_ALEN);
315 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
318 os_memset(go_dev_addr, 0, ETH_ALEN);
321 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
322 "go_dev_addr=" MACSTR,
323 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
325 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
329 static void wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
330 struct wpa_ssid *ssid,
331 const u8 *go_dev_addr)
336 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
337 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
338 for (s = wpa_s->conf->ssid; s; s = s->next) {
339 if (s->disabled == 2 &&
340 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
341 s->ssid_len == ssid->ssid_len &&
342 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
347 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
349 if (ssid->passphrase && !s->passphrase)
351 else if (ssid->passphrase && s->passphrase &&
352 os_strcmp(ssid->passphrase, s->passphrase) != 0)
355 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
358 s = wpa_config_add_network(wpa_s->conf);
361 wpa_config_set_network_defaults(s);
365 s->p2p_persistent_group = 1;
368 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
369 s->mode = ssid->mode;
370 s->auth_alg = WPA_AUTH_ALG_OPEN;
371 s->key_mgmt = WPA_KEY_MGMT_PSK;
372 s->proto = WPA_PROTO_RSN;
373 s->pairwise_cipher = WPA_CIPHER_CCMP;
374 if (ssid->passphrase) {
375 os_free(s->passphrase);
376 s->passphrase = os_strdup(ssid->passphrase);
380 os_memcpy(s->psk, ssid->psk, 32);
382 if (s->passphrase && !s->psk_set)
383 wpa_config_update_psk(s);
384 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
386 s->ssid = os_malloc(ssid->ssid_len);
389 s->ssid_len = ssid->ssid_len;
390 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
393 #ifndef CONFIG_NO_CONFIG_WRITE
394 if (changed && wpa_s->conf->update_config &&
395 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
396 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
398 #endif /* CONFIG_NO_CONFIG_WRITE */
402 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
405 struct wpa_ssid *ssid;
406 const char *ssid_txt;
409 u8 go_dev_addr[ETH_ALEN];
412 * This callback is likely called for the main interface. Update wpa_s
413 * to use the group interface if a new interface was created for the
416 if (wpa_s->global->p2p_group_formation)
417 wpa_s = wpa_s->global->p2p_group_formation;
418 wpa_s->p2p_in_provisioning = 0;
421 wpa_msg(wpa_s->parent, MSG_INFO,
422 P2P_EVENT_GROUP_FORMATION_FAILURE);
423 wpas_p2p_group_delete(wpa_s);
427 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
429 ssid = wpa_s->current_ssid;
430 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
431 ssid->mode = WPAS_MODE_P2P_GO;
432 p2p_group_notif_formation_done(wpa_s->p2p_group);
433 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
438 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
439 client = ssid->mode == WPAS_MODE_INFRA;
440 if (ssid->mode == WPAS_MODE_P2P_GO) {
441 persistent = ssid->p2p_persistent_group;
442 os_memcpy(go_dev_addr, wpa_s->parent->own_addr,
445 persistent = wpas_p2p_persistent_group(wpa_s,
451 client = wpa_s->p2p_group_interface ==
452 P2P_GROUP_INTERFACE_CLIENT;
455 wpa_s->show_group_started = 0;
458 * Indicate event only after successfully completed 4-way
459 * handshake, i.e., when the interface is ready for data
462 wpa_s->show_group_started = 1;
463 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
465 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
466 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
467 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
469 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
470 MAC2STR(go_dev_addr),
471 persistent ? " [PERSISTENT]" : "");
472 wpas_p2p_cross_connect_setup(wpa_s);
473 wpas_p2p_set_group_idle_timeout(wpa_s);
475 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
476 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
477 "go_dev_addr=" MACSTR "%s",
478 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
479 ssid && ssid->passphrase ? ssid->passphrase : "",
480 MAC2STR(go_dev_addr),
481 persistent ? " [PERSISTENT]" : "");
482 wpas_p2p_cross_connect_setup(wpa_s);
483 wpas_p2p_set_group_idle_timeout(wpa_s);
487 wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
492 static struct wpa_supplicant *
493 wpas_get_tx_interface(struct wpa_supplicant *wpa_s, const u8 *src)
495 struct wpa_supplicant *iface;
497 if (os_memcmp(src, wpa_s->own_addr, ETH_ALEN) == 0)
501 * Try to find a group interface that matches with the source address.
503 iface = wpa_s->global->ifaces;
505 if (os_memcmp(wpa_s->pending_action_src,
506 iface->own_addr, ETH_ALEN) == 0)
511 wpa_printf(MSG_DEBUG, "P2P: Use group interface %s "
512 "instead of interface %s for Action TX",
513 iface->ifname, wpa_s->ifname);
521 static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
523 struct wpa_supplicant *wpa_s = eloop_ctx;
524 struct wpa_supplicant *iface;
528 without_roc = wpa_s->pending_action_without_roc;
529 wpa_s->pending_action_without_roc = 0;
530 wpa_printf(MSG_DEBUG, "P2P: Send Action callback (without_roc=%d "
531 "pending_action_tx=%p)",
532 without_roc, wpa_s->pending_action_tx);
534 if (wpa_s->pending_action_tx == NULL)
538 * This call is likely going to be on the P2P device instance if the
539 * driver uses a separate interface for that purpose. However, some
540 * Action frames are actually sent within a P2P Group and when that is
541 * the case, we need to follow power saving (e.g., GO buffering the
542 * frame for a client in PS mode or a client following the advertised
543 * NoA from its GO). To make that easier for the driver, select the
544 * correct group interface here.
546 iface = wpas_get_tx_interface(wpa_s, wpa_s->pending_action_src);
548 if (wpa_s->off_channel_freq != wpa_s->pending_action_freq &&
549 wpa_s->pending_action_freq != 0 &&
550 wpa_s->pending_action_freq != iface->assoc_freq) {
551 wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX "
552 "waiting for another freq=%u (off_channel_freq=%u "
554 wpa_s->pending_action_freq,
555 wpa_s->off_channel_freq,
557 if (without_roc && wpa_s->off_channel_freq == 0) {
559 * We may get here if wpas_send_action() found us to be
560 * on the correct channel, but remain-on-channel cancel
561 * event was received before getting here.
563 wpa_printf(MSG_DEBUG, "P2P: Schedule "
564 "remain-on-channel to send Action frame");
565 if (wpa_drv_remain_on_channel(
566 wpa_s, wpa_s->pending_action_freq, 200) <
568 wpa_printf(MSG_DEBUG, "P2P: Failed to request "
569 "driver to remain on channel (%u "
570 "MHz) for Action Frame TX",
571 wpa_s->pending_action_freq);
573 wpa_s->roc_waiting_drv_freq =
574 wpa_s->pending_action_freq;
579 wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to "
580 MACSTR " using interface %s",
581 MAC2STR(wpa_s->pending_action_dst), iface->ifname);
582 res = wpa_drv_send_action(iface, wpa_s->pending_action_freq,
583 wpa_s->pending_action_dst,
584 wpa_s->pending_action_src,
585 wpa_s->pending_action_bssid,
586 wpabuf_head(wpa_s->pending_action_tx),
587 wpabuf_len(wpa_s->pending_action_tx));
589 wpa_printf(MSG_DEBUG, "P2P: Failed to send the pending "
592 * Use fake TX status event to allow P2P state machine to
595 wpas_send_action_tx_status(
596 wpa_s, wpa_s->pending_action_dst,
597 wpabuf_head(wpa_s->pending_action_tx),
598 wpabuf_len(wpa_s->pending_action_tx),
599 P2P_SEND_ACTION_FAILED);
604 void wpas_send_action_tx_status(struct wpa_supplicant *wpa_s, const u8 *dst,
605 const u8 *data, size_t data_len,
606 enum p2p_send_action_result result)
608 if (wpa_s->global->p2p_disabled)
611 if (wpa_s->pending_action_tx == NULL) {
612 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - no "
613 "pending operation");
617 if (os_memcmp(dst, wpa_s->pending_action_dst, ETH_ALEN) != 0) {
618 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - unknown "
619 "destination address");
623 wpabuf_free(wpa_s->pending_action_tx);
624 wpa_s->pending_action_tx = NULL;
626 p2p_send_action_cb(wpa_s->global->p2p, wpa_s->pending_action_freq,
627 wpa_s->pending_action_dst,
628 wpa_s->pending_action_src,
629 wpa_s->pending_action_bssid,
632 if (wpa_s->pending_pd_before_join &&
633 (os_memcmp(wpa_s->pending_action_dst, wpa_s->pending_join_dev_addr,
635 os_memcmp(wpa_s->pending_action_dst,
636 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
637 wpa_s->pending_pd_before_join = 0;
638 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
639 "join-existing-group operation");
640 wpas_p2p_join_start(wpa_s);
645 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
646 const u8 *src, const u8 *bssid, const u8 *buf,
647 size_t len, unsigned int wait_time)
649 struct wpa_supplicant *wpa_s = ctx;
651 wpa_printf(MSG_DEBUG, "P2P: Send action frame: freq=%d dst=" MACSTR
652 " src=" MACSTR " bssid=" MACSTR " len=%d",
653 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
656 if (wpa_s->pending_action_tx) {
657 wpa_printf(MSG_DEBUG, "P2P: Dropped pending Action frame TX "
658 "to " MACSTR, MAC2STR(wpa_s->pending_action_dst));
659 wpabuf_free(wpa_s->pending_action_tx);
661 wpa_s->pending_action_tx = wpabuf_alloc(len);
662 if (wpa_s->pending_action_tx == NULL) {
663 wpa_printf(MSG_DEBUG, "P2P: Failed to allocate Action frame "
664 "TX buffer (len=%llu)", (unsigned long long) len);
667 wpabuf_put_data(wpa_s->pending_action_tx, buf, len);
668 os_memcpy(wpa_s->pending_action_src, src, ETH_ALEN);
669 os_memcpy(wpa_s->pending_action_dst, dst, ETH_ALEN);
670 os_memcpy(wpa_s->pending_action_bssid, bssid, ETH_ALEN);
671 wpa_s->pending_action_freq = freq;
674 struct wpa_supplicant *tx_iface;
675 tx_iface = wpas_get_tx_interface(wpa_s, src);
676 if (tx_iface->assoc_freq == freq) {
677 wpa_printf(MSG_DEBUG, "P2P: Already on requested "
678 "channel (TX interface operating channel)");
683 if (wpa_s->off_channel_freq == freq || freq == 0) {
684 wpa_printf(MSG_DEBUG, "P2P: Already on requested channel; "
685 "send Action frame immediately");
686 /* TODO: Would there ever be need to extend the current
687 * duration on the channel? */
688 wpa_s->pending_action_without_roc = 1;
689 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
690 eloop_register_timeout(0, 0, wpas_send_action_cb, wpa_s, NULL);
693 wpa_s->pending_action_without_roc = 0;
695 if (wpa_s->roc_waiting_drv_freq == freq) {
696 wpa_printf(MSG_DEBUG, "P2P: Already waiting for driver to get "
697 "to frequency %u MHz; continue waiting to send the "
698 "Action frame", freq);
702 wpa_printf(MSG_DEBUG, "P2P: Schedule Action frame to be transmitted "
703 "once the driver gets to the requested channel");
704 if (wait_time > wpa_s->max_remain_on_chan)
705 wait_time = wpa_s->max_remain_on_chan;
706 if (wpa_drv_remain_on_channel(wpa_s, freq, wait_time) < 0) {
707 wpa_printf(MSG_DEBUG, "P2P: Failed to request driver "
708 "to remain on channel (%u MHz) for Action "
712 wpa_s->roc_waiting_drv_freq = freq;
718 static void wpas_send_action_done(void *ctx)
720 struct wpa_supplicant *wpa_s = ctx;
721 wpa_printf(MSG_DEBUG, "P2P: Action frame sequence done notification");
722 wpabuf_free(wpa_s->pending_action_tx);
723 wpa_s->pending_action_tx = NULL;
724 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
725 wpa_drv_cancel_remain_on_channel(wpa_s);
726 wpa_s->off_channel_freq = 0;
727 wpa_s->roc_waiting_drv_freq = 0;
732 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
733 struct p2p_go_neg_results *params)
735 if (wpa_s->go_params == NULL) {
736 wpa_s->go_params = os_malloc(sizeof(*params));
737 if (wpa_s->go_params == NULL)
740 os_memcpy(wpa_s->go_params, params, sizeof(*params));
745 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
746 struct p2p_go_neg_results *res)
748 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
749 MAC2STR(res->peer_interface_addr));
750 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
751 res->ssid, res->ssid_len);
752 wpa_supplicant_ap_deinit(wpa_s);
753 wpas_copy_go_neg_results(wpa_s, res);
754 if (res->wps_method == WPS_PBC)
755 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
757 u16 dev_pw_id = DEV_PW_DEFAULT;
758 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
759 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
760 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
761 wpa_s->p2p_pin, 1, dev_pw_id);
766 static void p2p_go_configured(void *ctx, void *data)
768 struct wpa_supplicant *wpa_s = ctx;
769 struct p2p_go_neg_results *params = data;
770 struct wpa_ssid *ssid;
772 ssid = wpa_s->current_ssid;
773 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
774 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
775 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
776 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
777 "go_dev_addr=" MACSTR "%s",
779 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
781 params->passphrase ? params->passphrase : "",
782 MAC2STR(wpa_s->parent->own_addr),
783 params->persistent_group ? " [PERSISTENT]" : "");
784 if (params->persistent_group)
785 wpas_p2p_store_persistent_group(
787 wpa_s->parent->own_addr);
788 wpas_p2p_cross_connect_setup(wpa_s);
789 wpas_p2p_set_group_idle_timeout(wpa_s);
793 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
794 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
795 params->peer_interface_addr)) {
796 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
800 if (params->wps_method == WPS_PBC)
801 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr);
802 else if (wpa_s->p2p_pin[0])
803 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
804 wpa_s->p2p_pin, NULL, 0);
805 os_free(wpa_s->go_params);
806 wpa_s->go_params = NULL;
810 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
811 struct p2p_go_neg_results *params,
814 struct wpa_ssid *ssid;
816 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
819 ssid = wpa_config_add_network(wpa_s->conf);
823 wpa_config_set_network_defaults(ssid);
826 ssid->p2p_persistent_group = params->persistent_group;
827 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
829 ssid->frequency = params->freq;
830 ssid->ssid = os_zalloc(params->ssid_len + 1);
832 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
833 ssid->ssid_len = params->ssid_len;
835 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
836 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
837 ssid->proto = WPA_PROTO_RSN;
838 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
839 ssid->passphrase = os_strdup(params->passphrase);
841 wpa_s->ap_configured_cb = p2p_go_configured;
842 wpa_s->ap_configured_cb_ctx = wpa_s;
843 wpa_s->ap_configured_cb_data = wpa_s->go_params;
844 wpa_s->connect_without_scan = 1;
845 wpa_s->reassociate = 1;
846 wpa_s->disconnected = 0;
847 wpa_supplicant_req_scan(wpa_s, 0, 0);
851 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
852 const struct wpa_supplicant *src)
854 struct wpa_config *d;
855 const struct wpa_config *s;
860 #define C(n) if (s->n) d->n = os_strdup(s->n)
870 d->p2p_group_idle = s->p2p_group_idle;
871 d->p2p_intra_bss = s->p2p_intra_bss;
875 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
876 enum wpa_driver_if_type type)
878 char ifname[120], force_ifname[120];
880 if (wpa_s->pending_interface_name[0]) {
881 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
882 "- skip creation of a new one");
883 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
884 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
885 "unknown?! ifname='%s'",
886 wpa_s->pending_interface_name);
892 os_snprintf(ifname, sizeof(ifname), "%s-p2p-%d", wpa_s->ifname,
893 wpa_s->p2p_group_idx);
894 force_ifname[0] = '\0';
896 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
898 wpa_s->p2p_group_idx++;
900 wpa_s->pending_interface_type = type;
901 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
902 wpa_s->pending_interface_addr) < 0) {
903 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
908 if (force_ifname[0]) {
909 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
911 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
912 sizeof(wpa_s->pending_interface_name));
914 os_strlcpy(wpa_s->pending_interface_name, ifname,
915 sizeof(wpa_s->pending_interface_name));
916 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
917 MACSTR, wpa_s->pending_interface_name,
918 MAC2STR(wpa_s->pending_interface_addr));
924 static void wpas_p2p_remove_pending_group_interface(
925 struct wpa_supplicant *wpa_s)
927 if (!wpa_s->pending_interface_name[0] ||
928 is_zero_ether_addr(wpa_s->pending_interface_addr))
929 return; /* No pending virtual interface */
931 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
932 wpa_s->pending_interface_name);
933 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
934 wpa_s->pending_interface_name);
935 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
936 wpa_s->pending_interface_name[0] = '\0';
940 static struct wpa_supplicant *
941 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
943 struct wpa_interface iface;
944 struct wpa_supplicant *group_wpa_s;
946 if (!wpa_s->pending_interface_name[0]) {
947 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
948 if (!wpas_p2p_create_iface(wpa_s))
951 * Something has forced us to remove the pending interface; try
952 * to create a new one and hope for the best that we will get
953 * the same local address.
955 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
956 WPA_IF_P2P_CLIENT) < 0)
960 os_memset(&iface, 0, sizeof(iface));
961 iface.ifname = wpa_s->pending_interface_name;
962 iface.driver = wpa_s->driver->name;
963 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
964 iface.driver_param = wpa_s->conf->driver_param;
965 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
966 if (group_wpa_s == NULL) {
967 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
968 "wpa_supplicant interface");
971 wpa_s->pending_interface_name[0] = '\0';
972 group_wpa_s->parent = wpa_s;
973 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
974 P2P_GROUP_INTERFACE_CLIENT;
975 wpa_s->global->p2p_group_formation = group_wpa_s;
977 wpas_p2p_clone_config(group_wpa_s, wpa_s);
983 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
986 struct wpa_supplicant *wpa_s = eloop_ctx;
987 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
988 if (wpa_s->global->p2p)
989 p2p_group_formation_failed(wpa_s->global->p2p);
990 wpas_group_formation_completed(wpa_s, 0);
994 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
996 struct wpa_supplicant *wpa_s = ctx;
998 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
999 wpa_drv_cancel_remain_on_channel(wpa_s);
1000 wpa_s->off_channel_freq = 0;
1001 wpa_s->roc_waiting_drv_freq = 0;
1005 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1007 wpas_p2p_remove_pending_group_interface(wpa_s);
1011 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1013 if (wpa_s->create_p2p_iface) {
1014 struct wpa_supplicant *group_wpa_s =
1015 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1016 if (group_wpa_s == NULL) {
1017 wpas_p2p_remove_pending_group_interface(wpa_s);
1020 if (group_wpa_s != wpa_s) {
1021 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1022 sizeof(group_wpa_s->p2p_pin));
1023 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1025 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1026 wpa_s->pending_interface_name[0] = '\0';
1027 group_wpa_s->p2p_in_provisioning = 1;
1030 wpas_start_wps_go(group_wpa_s, res, 1);
1032 wpas_start_wps_enrollee(group_wpa_s, res);
1034 wpa_s->p2p_in_provisioning = 1;
1035 wpa_s->global->p2p_group_formation = wpa_s;
1038 wpas_start_wps_go(wpa_s, res, 1);
1040 wpas_start_wps_enrollee(ctx, res);
1043 wpa_s->p2p_long_listen = 0;
1044 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1046 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1047 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1048 (res->peer_config_timeout % 100) * 10000,
1049 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1053 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1055 struct wpa_supplicant *wpa_s = ctx;
1056 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1057 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1061 void wpas_dev_found(void *ctx, const u8 *addr, const u8 *dev_addr,
1062 const u8 *pri_dev_type, const char *dev_name,
1063 u16 config_methods, u8 dev_capab, u8 group_capab)
1065 struct wpa_supplicant *wpa_s = ctx;
1066 char devtype[WPS_DEV_TYPE_BUFSIZE];
1067 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1068 " p2p_dev_addr=" MACSTR
1069 " pri_dev_type=%s name='%s' config_methods=0x%x "
1070 "dev_capab=0x%x group_capab=0x%x",
1071 MAC2STR(addr), MAC2STR(dev_addr),
1072 wps_dev_type_bin2str(pri_dev_type, devtype, sizeof(devtype)),
1073 dev_name, config_methods, dev_capab, group_capab);
1077 static int wpas_start_listen(void *ctx, unsigned int freq,
1078 unsigned int duration,
1079 const struct wpabuf *probe_resp_ie)
1081 struct wpa_supplicant *wpa_s = ctx;
1083 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1085 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1086 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1087 "report received Probe Request frames");
1091 wpa_s->pending_listen_freq = freq;
1092 wpa_s->pending_listen_duration = duration;
1094 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1095 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1096 "to remain on channel (%u MHz) for Listen "
1098 wpa_s->pending_listen_freq = 0;
1101 wpa_s->roc_waiting_drv_freq = freq;
1107 static void wpas_stop_listen(void *ctx)
1109 struct wpa_supplicant *wpa_s = ctx;
1110 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1111 wpa_drv_cancel_remain_on_channel(wpa_s);
1112 wpa_s->off_channel_freq = 0;
1113 wpa_s->roc_waiting_drv_freq = 0;
1115 wpa_drv_probe_req_report(wpa_s, 0);
1119 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1121 struct wpa_supplicant *wpa_s = ctx;
1122 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
1126 static struct p2p_srv_bonjour *
1127 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1128 const struct wpabuf *query)
1130 struct p2p_srv_bonjour *bsrv;
1133 len = wpabuf_len(query);
1134 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1135 struct p2p_srv_bonjour, list) {
1136 if (len == wpabuf_len(bsrv->query) &&
1137 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1145 static struct p2p_srv_upnp *
1146 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1147 const char *service)
1149 struct p2p_srv_upnp *usrv;
1151 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1152 struct p2p_srv_upnp, list) {
1153 if (version == usrv->version &&
1154 os_strcmp(service, usrv->service) == 0)
1161 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1166 if (wpabuf_tailroom(resp) < 5)
1169 /* Length (to be filled) */
1170 len_pos = wpabuf_put(resp, 2);
1171 wpabuf_put_u8(resp, srv_proto);
1172 wpabuf_put_u8(resp, srv_trans_id);
1174 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1175 /* Response Data: empty */
1176 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1180 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1181 struct wpabuf *resp, u8 srv_trans_id)
1183 struct p2p_srv_bonjour *bsrv;
1186 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1188 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1189 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1193 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1194 struct p2p_srv_bonjour, list) {
1195 if (wpabuf_tailroom(resp) <
1196 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1198 /* Length (to be filled) */
1199 len_pos = wpabuf_put(resp, 2);
1200 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1201 wpabuf_put_u8(resp, srv_trans_id);
1203 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1204 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1205 wpabuf_head(bsrv->resp),
1206 wpabuf_len(bsrv->resp));
1208 wpabuf_put_buf(resp, bsrv->query); /* Key */
1209 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1210 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1216 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1217 struct wpabuf *resp, u8 srv_trans_id,
1218 const u8 *query, size_t query_len)
1220 struct p2p_srv_bonjour *bsrv;
1224 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1226 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1227 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1228 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1233 if (query_len == 0) {
1234 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1238 if (wpabuf_tailroom(resp) < 5)
1240 /* Length (to be filled) */
1241 len_pos = wpabuf_put(resp, 2);
1242 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1243 wpabuf_put_u8(resp, srv_trans_id);
1245 wpabuf_set(&buf, query, query_len);
1246 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1248 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1252 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1253 /* Response Data: empty */
1254 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1260 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1261 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1262 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1264 if (wpabuf_tailroom(resp) >=
1265 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1267 wpabuf_put_buf(resp, bsrv->query); /* Key */
1268 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1270 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1274 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1275 struct wpabuf *resp, u8 srv_trans_id)
1277 struct p2p_srv_upnp *usrv;
1280 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1282 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1283 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1287 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1288 struct p2p_srv_upnp, list) {
1289 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1292 /* Length (to be filled) */
1293 len_pos = wpabuf_put(resp, 2);
1294 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1295 wpabuf_put_u8(resp, srv_trans_id);
1298 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1300 wpabuf_put_u8(resp, usrv->version);
1301 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1303 wpabuf_put_str(resp, usrv->service);
1304 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1310 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1311 struct wpabuf *resp, u8 srv_trans_id,
1312 const u8 *query, size_t query_len)
1314 struct p2p_srv_upnp *usrv;
1320 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1323 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1324 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1325 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1330 if (query_len == 0) {
1331 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1335 if (wpabuf_tailroom(resp) < 5)
1338 /* Length (to be filled) */
1339 len_pos = wpabuf_put(resp, 2);
1340 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1341 wpabuf_put_u8(resp, srv_trans_id);
1344 str = os_malloc(query_len);
1347 os_memcpy(str, query + 1, query_len - 1);
1348 str[query_len - 1] = '\0';
1350 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1351 struct p2p_srv_upnp, list) {
1352 if (version != usrv->version)
1355 if (os_strcmp(str, "ssdp:all") != 0 &&
1356 os_strstr(usrv->service, str) == NULL)
1359 if (wpabuf_tailroom(resp) < 2)
1363 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1365 wpabuf_put_u8(resp, version);
1367 wpabuf_put_u8(resp, ',');
1371 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1373 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1375 wpabuf_put_str(resp, usrv->service);
1379 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1382 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1383 /* Response Data: empty */
1386 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1390 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1391 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1393 struct wpa_supplicant *wpa_s = ctx;
1394 const u8 *pos = tlvs;
1395 const u8 *end = tlvs + tlvs_len;
1398 struct wpabuf *resp;
1399 u8 srv_proto, srv_trans_id;
1403 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1405 buf_len = 2 * tlvs_len + 1;
1406 buf = os_malloc(buf_len);
1408 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1409 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1411 freq, MAC2STR(sa), dialog_token, update_indic,
1416 if (wpa_s->p2p_sd_over_ctrl_iface)
1417 return; /* to be processed by an external program */
1419 resp = wpabuf_alloc(10000);
1423 while (pos + 1 < end) {
1424 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1425 slen = WPA_GET_LE16(pos);
1427 if (pos + slen > end || slen < 2) {
1428 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1433 tlv_end = pos + slen;
1436 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1438 srv_trans_id = *pos++;
1439 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1442 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1443 pos, tlv_end - pos);
1446 if (wpa_s->force_long_sd) {
1447 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1449 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1450 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1454 switch (srv_proto) {
1455 case P2P_SERV_ALL_SERVICES:
1456 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1457 "for all services");
1458 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1459 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1460 wpa_printf(MSG_DEBUG, "P2P: No service "
1461 "discovery protocols available");
1462 wpas_sd_add_proto_not_avail(
1463 resp, P2P_SERV_ALL_SERVICES,
1467 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1468 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1470 case P2P_SERV_BONJOUR:
1471 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1472 pos, tlv_end - pos);
1475 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1476 pos, tlv_end - pos);
1479 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1480 "protocol %u", srv_proto);
1481 wpas_sd_add_proto_not_avail(resp, srv_proto,
1490 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1496 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1497 const u8 *tlvs, size_t tlvs_len)
1499 struct wpa_supplicant *wpa_s = ctx;
1500 const u8 *pos = tlvs;
1501 const u8 *end = tlvs + tlvs_len;
1507 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1509 if (tlvs_len > 1500) {
1510 /* TODO: better way for handling this */
1511 wpa_msg_ctrl(wpa_s, MSG_INFO,
1512 P2P_EVENT_SERV_DISC_RESP MACSTR
1513 " %u <long response: %u bytes>",
1514 MAC2STR(sa), update_indic,
1515 (unsigned int) tlvs_len);
1517 buf_len = 2 * tlvs_len + 1;
1518 buf = os_malloc(buf_len);
1520 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1521 wpa_msg_ctrl(wpa_s, MSG_INFO,
1522 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1523 MAC2STR(sa), update_indic, buf);
1529 u8 srv_proto, srv_trans_id, status;
1531 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1532 slen = WPA_GET_LE16(pos);
1534 if (pos + slen > end || slen < 3) {
1535 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1539 tlv_end = pos + slen;
1542 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1544 srv_trans_id = *pos++;
1545 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1548 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1551 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1552 pos, tlv_end - pos);
1559 void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1560 const struct wpabuf *tlvs)
1562 return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1566 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1567 u8 version, const char *query)
1569 struct wpabuf *tlvs;
1572 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1575 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1576 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1577 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1578 wpabuf_put_u8(tlvs, version);
1579 wpabuf_put_str(tlvs, query);
1580 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1586 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
1588 return p2p_sd_cancel_request(wpa_s->global->p2p, req);
1592 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1593 const u8 *dst, u8 dialog_token,
1594 const struct wpabuf *resp_tlvs)
1596 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1601 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1603 p2p_sd_service_update(wpa_s->global->p2p);
1607 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1609 dl_list_del(&bsrv->list);
1610 wpabuf_free(bsrv->query);
1611 wpabuf_free(bsrv->resp);
1616 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1618 dl_list_del(&usrv->list);
1619 os_free(usrv->service);
1624 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1626 struct p2p_srv_bonjour *bsrv, *bn;
1627 struct p2p_srv_upnp *usrv, *un;
1629 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1630 struct p2p_srv_bonjour, list)
1631 wpas_p2p_srv_bonjour_free(bsrv);
1633 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1634 struct p2p_srv_upnp, list)
1635 wpas_p2p_srv_upnp_free(usrv);
1637 wpas_p2p_sd_service_update(wpa_s);
1641 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1642 struct wpabuf *query, struct wpabuf *resp)
1644 struct p2p_srv_bonjour *bsrv;
1646 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1649 wpabuf_free(bsrv->resp);
1654 bsrv = os_zalloc(sizeof(*bsrv));
1657 bsrv->query = query;
1659 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1661 wpas_p2p_sd_service_update(wpa_s);
1666 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1667 const struct wpabuf *query)
1669 struct p2p_srv_bonjour *bsrv;
1671 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1674 wpas_p2p_srv_bonjour_free(bsrv);
1675 wpas_p2p_sd_service_update(wpa_s);
1680 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1681 const char *service)
1683 struct p2p_srv_upnp *usrv;
1685 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1686 return 0; /* Already listed */
1687 usrv = os_zalloc(sizeof(*usrv));
1690 usrv->version = version;
1691 usrv->service = os_strdup(service);
1692 if (usrv->service == NULL) {
1696 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1698 wpas_p2p_sd_service_update(wpa_s);
1703 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1704 const char *service)
1706 struct p2p_srv_upnp *usrv;
1708 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1711 wpas_p2p_srv_upnp_free(usrv);
1712 wpas_p2p_sd_service_update(wpa_s);
1717 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1718 const u8 *peer, const char *params)
1720 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1721 MAC2STR(peer), wps_generate_pin(), params);
1725 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1726 const u8 *peer, const char *params)
1728 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1729 MAC2STR(peer), params);
1733 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1734 const u8 *dev_addr, const u8 *pri_dev_type,
1735 const char *dev_name, u16 supp_config_methods,
1736 u8 dev_capab, u8 group_capab)
1738 struct wpa_supplicant *wpa_s = ctx;
1739 char devtype[WPS_DEV_TYPE_BUFSIZE];
1741 u8 empty_dev_type[8];
1743 if (pri_dev_type == NULL) {
1744 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1745 pri_dev_type = empty_dev_type;
1747 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1748 " pri_dev_type=%s name='%s' config_methods=0x%x "
1749 "dev_capab=0x%x group_capab=0x%x",
1751 wps_dev_type_bin2str(pri_dev_type, devtype,
1753 dev_name, supp_config_methods, dev_capab, group_capab);
1754 params[sizeof(params) - 1] = '\0';
1756 if (config_methods & WPS_CONFIG_DISPLAY)
1757 wpas_prov_disc_local_display(wpa_s, peer, params);
1758 else if (config_methods & WPS_CONFIG_KEYPAD)
1759 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1760 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1761 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1762 "%s", MAC2STR(peer), params);
1766 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1768 struct wpa_supplicant *wpa_s = ctx;
1769 if (config_methods & WPS_CONFIG_DISPLAY)
1770 wpas_prov_disc_local_keypad(wpa_s, peer, "");
1771 else if (config_methods & WPS_CONFIG_KEYPAD)
1772 wpas_prov_disc_local_display(wpa_s, peer, "");
1773 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1774 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
1777 if (wpa_s->pending_pd_before_join &&
1778 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1779 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1780 wpa_s->pending_pd_before_join = 0;
1781 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1782 "join-existing-group operation");
1783 wpas_p2p_join_start(wpa_s);
1788 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1789 const u8 *go_dev_addr, const u8 *ssid,
1790 size_t ssid_len, int *go, u8 *group_bssid,
1791 int *force_freq, int persistent_group)
1793 struct wpa_supplicant *wpa_s = ctx;
1795 u8 cur_bssid[ETH_ALEN];
1798 if (!persistent_group) {
1799 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1800 " to join an active group", MAC2STR(sa));
1801 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
1802 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
1804 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
1805 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1806 "authorized invitation");
1810 * Do not accept the invitation automatically; notify user and
1813 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1816 if (!wpa_s->conf->persistent_reconnect)
1817 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1819 for (s = wpa_s->conf->ssid; s; s = s->next) {
1820 if (s->disabled == 2 &&
1821 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1822 s->ssid_len == ssid_len &&
1823 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1828 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1829 " requested reinvocation of an unknown group",
1831 return P2P_SC_FAIL_UNKNOWN_GROUP;
1834 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1836 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1837 wpa_printf(MSG_DEBUG, "P2P: The only available "
1838 "interface is already in use - reject "
1840 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1842 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
1843 } else if (s->mode == WPAS_MODE_P2P_GO) {
1845 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
1847 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
1848 "interface address for the group");
1849 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1851 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
1856 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
1857 wpa_s->assoc_freq) {
1858 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1859 "the channel we are already using");
1860 *force_freq = wpa_s->assoc_freq;
1863 res = wpa_drv_shared_freq(wpa_s);
1865 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1866 "with the channel we are already using on a "
1867 "shared interface");
1871 return P2P_SC_SUCCESS;
1875 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
1876 const u8 *ssid, size_t ssid_len,
1877 const u8 *go_dev_addr, u8 status,
1880 struct wpa_supplicant *wpa_s = ctx;
1883 for (s = wpa_s->conf->ssid; s; s = s->next) {
1884 if (s->disabled == 2 &&
1885 s->ssid_len == ssid_len &&
1886 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1890 if (status == P2P_SC_SUCCESS) {
1891 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1892 " was accepted; op_freq=%d MHz",
1893 MAC2STR(sa), op_freq);
1895 wpas_p2p_group_add_persistent(
1896 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
1898 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
1899 wpa_s->p2p_wps_method);
1904 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
1905 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1906 " was rejected (status %u)", MAC2STR(sa), status);
1912 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1913 "sa=" MACSTR " go_dev_addr=" MACSTR
1914 " bssid=" MACSTR " unknown-network",
1915 MAC2STR(sa), MAC2STR(go_dev_addr),
1918 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1919 "sa=" MACSTR " go_dev_addr=" MACSTR
1921 MAC2STR(sa), MAC2STR(go_dev_addr));
1926 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
1927 " persistent=%d", MAC2STR(sa), s->id);
1931 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
1933 struct wpa_supplicant *wpa_s = ctx;
1934 struct wpa_ssid *ssid;
1937 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1938 "status=%d " MACSTR,
1939 status, MAC2STR(bssid));
1941 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1942 "status=%d ", status);
1945 if (wpa_s->pending_invite_ssid_id == -1)
1946 return; /* Invitation to active group */
1948 if (status != P2P_SC_SUCCESS) {
1949 wpas_p2p_remove_pending_group_interface(wpa_s);
1953 ssid = wpa_config_get_network(wpa_s->conf,
1954 wpa_s->pending_invite_ssid_id);
1956 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
1957 "data matching with invitation");
1961 wpas_p2p_group_add_persistent(wpa_s, ssid,
1962 ssid->mode == WPAS_MODE_P2P_GO, 0);
1966 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
1967 struct p2p_channels *chan)
1971 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
1974 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1975 chan->reg_class[cla].reg_class = 81;
1976 chan->reg_class[cla].channels = 11;
1977 for (i = 0; i < 11; i++)
1978 chan->reg_class[cla].channel[i] = i + 1;
1981 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
1984 /* Operating class 115 - 5 GHz, channels 36-48 */
1985 chan->reg_class[cla].reg_class = 115;
1986 chan->reg_class[cla].channels = 4;
1987 chan->reg_class[cla].channel[0] = 36;
1988 chan->reg_class[cla].channel[1] = 40;
1989 chan->reg_class[cla].channel[2] = 44;
1990 chan->reg_class[cla].channel[3] = 48;
1993 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
1996 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1997 chan->reg_class[cla].reg_class = 124;
1998 chan->reg_class[cla].channels = 4;
1999 chan->reg_class[cla].channel[0] = 149;
2000 chan->reg_class[cla].channel[1] = 153;
2001 chan->reg_class[cla].channel[2] = 157;
2002 chan->reg_class[cla].channel[3] = 161;
2005 chan->reg_classes = cla;
2010 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2012 enum hostapd_hw_mode mode)
2016 for (i = 0; i < num_modes; i++) {
2017 if (modes[i].mode == mode)
2025 static int has_channel(struct hostapd_hw_modes *mode, u8 chan)
2029 for (i = 0; i < mode->num_channels; i++) {
2030 if (mode->channels[i].chan == chan) {
2031 return !(mode->channels[i].flag &
2032 (HOSTAPD_CHAN_DISABLED |
2033 HOSTAPD_CHAN_PASSIVE_SCAN |
2034 HOSTAPD_CHAN_NO_IBSS |
2035 HOSTAPD_CHAN_RADAR));
2043 struct p2p_oper_class_map {
2044 enum hostapd_hw_mode mode;
2051 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2052 struct p2p_channels *chan)
2054 struct hostapd_hw_modes *modes, *mode;
2055 u16 num_modes, flags;
2057 struct p2p_oper_class_map op_class[] = {
2058 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1 },
2059 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1 },
2060 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4 },
2061 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4 },
2062 #if 0 /* TODO: 40 MHz channels */
2063 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1 },
2064 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1 },
2065 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8 },
2066 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8 },
2067 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8 },
2068 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8 },
2073 modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
2074 if (modes == NULL) {
2075 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2076 "of all supported channels; assume dualband "
2078 return wpas_p2p_default_channels(wpa_s, chan);
2083 for (op = 0; op_class[op].op_class; op++) {
2084 struct p2p_oper_class_map *o = &op_class[op];
2086 struct p2p_reg_class *reg = NULL;
2088 mode = get_mode(modes, num_modes, o->mode);
2091 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2092 if (!has_channel(mode, ch))
2095 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2096 "class %u", o->op_class);
2097 reg = &chan->reg_class[cla];
2099 reg->reg_class = o->op_class;
2101 reg->channel[reg->channels] = ch;
2105 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2106 reg->channel, reg->channels);
2110 chan->reg_classes = cla;
2112 ieee80211_sta_free_hw_features(modes, num_modes);
2118 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2121 struct wpa_supplicant *wpa_s = ctx;
2123 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2124 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2130 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2135 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2136 * @global: Pointer to global data from wpa_supplicant_init()
2137 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2138 * Returns: 0 on success, -1 on failure
2140 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2142 struct p2p_config p2p;
2146 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2149 #ifdef CONFIG_CLIENT_MLME
2150 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
2151 wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
2152 wpa_s->mlme.public_action_cb_ctx = wpa_s;
2154 #endif /* CONFIG_CLIENT_MLME */
2156 if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
2157 wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
2158 /* Continue anyway; this is not really a fatal error */
2164 os_memset(&p2p, 0, sizeof(p2p));
2165 p2p.msg_ctx = wpa_s;
2167 p2p.p2p_scan = wpas_p2p_scan;
2168 p2p.send_action = wpas_send_action;
2169 p2p.send_action_done = wpas_send_action_done;
2170 p2p.go_neg_completed = wpas_go_neg_completed;
2171 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2172 p2p.dev_found = wpas_dev_found;
2173 p2p.start_listen = wpas_start_listen;
2174 p2p.stop_listen = wpas_stop_listen;
2175 p2p.send_probe_resp = wpas_send_probe_resp;
2176 p2p.sd_request = wpas_sd_request;
2177 p2p.sd_response = wpas_sd_response;
2178 p2p.prov_disc_req = wpas_prov_disc_req;
2179 p2p.prov_disc_resp = wpas_prov_disc_resp;
2180 p2p.invitation_process = wpas_invitation_process;
2181 p2p.invitation_received = wpas_invitation_received;
2182 p2p.invitation_result = wpas_invitation_result;
2183 p2p.get_noa = wpas_get_noa;
2185 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2186 os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
2187 p2p.dev_name = wpa_s->conf->device_name;
2189 if (wpa_s->conf->p2p_listen_reg_class &&
2190 wpa_s->conf->p2p_listen_channel) {
2191 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2192 p2p.channel = wpa_s->conf->p2p_listen_channel;
2196 * Pick one of the social channels randomly as the listen
2199 os_get_random((u8 *) &r, sizeof(r));
2200 p2p.channel = 1 + (r % 3) * 5;
2203 if (wpa_s->conf->p2p_oper_reg_class &&
2204 wpa_s->conf->p2p_oper_channel) {
2205 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2206 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2208 p2p.op_reg_class = 81;
2210 * For initial tests, pick the operation channel randomly.
2211 * TODO: Use scan results (etc.) to select the best channel.
2213 os_get_random((u8 *) &r, sizeof(r));
2214 p2p.op_channel = 1 + r % 11;
2216 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d "
2217 "Own preferred operation channel: %d",
2218 p2p.channel, p2p.op_channel);
2219 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2220 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2221 p2p.country[2] = 0x04;
2223 os_memcpy(p2p.country, "US\x04", 3);
2225 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2226 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2231 if (wpa_s->conf->device_type &&
2232 wps_dev_type_str2bin(wpa_s->conf->device_type, p2p.pri_dev_type) <
2234 wpa_printf(MSG_ERROR, "P2P: Invalid device_type");
2238 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
2239 if (wpa_s->conf->sec_device_type[i] == NULL)
2241 if (wps_dev_type_str2bin(
2242 wpa_s->conf->sec_device_type[i],
2243 p2p.sec_dev_type[p2p.num_sec_dev_types]) < 0) {
2244 wpa_printf(MSG_ERROR, "P2P: Invalid sec_device_type");
2247 p2p.num_sec_dev_types++;
2248 if (p2p.num_sec_dev_types == P2P_SEC_DEVICE_TYPES)
2252 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2253 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2255 p2p.max_peers = 100;
2257 if (wpa_s->conf->p2p_ssid_postfix) {
2258 p2p.ssid_postfix_len =
2259 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2260 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2261 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2262 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2263 p2p.ssid_postfix_len);
2266 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2268 global->p2p = p2p_init(&p2p);
2269 if (global->p2p == NULL)
2277 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2278 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2280 * This function deinitialize per-interface P2P data.
2282 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2284 if (wpa_s->driver && wpa_s->drv_priv)
2285 wpa_drv_probe_req_report(wpa_s, 0);
2286 os_free(wpa_s->go_params);
2287 wpa_s->go_params = NULL;
2288 wpabuf_free(wpa_s->pending_action_tx);
2289 wpa_s->pending_action_tx = NULL;
2290 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
2291 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2292 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2293 wpa_s->p2p_long_listen = 0;
2294 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2295 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2296 wpas_p2p_remove_pending_group_interface(wpa_s);
2298 /* TODO: remove group interface from the driver if this wpa_s instance
2299 * is on top of a P2P group interface */
2304 * wpas_p2p_deinit_global - Deinitialize global P2P module
2305 * @global: Pointer to global data from wpa_supplicant_init()
2307 * This function deinitializes the global (per device) P2P module.
2309 void wpas_p2p_deinit_global(struct wpa_global *global)
2311 struct wpa_supplicant *wpa_s, *tmp;
2314 if (global->p2p == NULL)
2317 /* Remove remaining P2P group interfaces */
2318 wpa_s = global->ifaces;
2320 wpas_p2p_service_flush(wpa_s);
2321 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2322 wpa_s = wpa_s->next;
2324 enum wpa_driver_if_type type;
2325 tmp = global->ifaces;
2328 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2333 ifname = os_strdup(tmp->ifname);
2334 type = wpas_p2p_if_type(tmp->p2p_group_interface);
2335 wpa_supplicant_remove_iface(global, tmp);
2337 wpa_drv_if_remove(wpa_s, type, ifname);
2342 * Deinit GO data on any possibly remaining interface (if main
2343 * interface is used as GO).
2345 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2346 if (wpa_s->ap_iface)
2347 wpas_p2p_group_deinit(wpa_s);
2350 p2p_deinit(global->p2p);
2355 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2357 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
2358 return 1; /* P2P group requires a new interface in every case
2360 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2361 return 0; /* driver does not support concurrent operations */
2362 if (wpa_s->global->ifaces->next)
2363 return 1; /* more that one interface already in use */
2364 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2365 return 1; /* this interface is already in use */
2370 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2371 const u8 *peer_addr,
2372 enum p2p_wps_method wps_method,
2373 int go_intent, const u8 *own_interface_addr,
2374 unsigned int force_freq, int persistent_group)
2376 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2377 go_intent, own_interface_addr, force_freq,
2382 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2383 const u8 *peer_addr,
2384 enum p2p_wps_method wps_method,
2385 int go_intent, const u8 *own_interface_addr,
2386 unsigned int force_freq, int persistent_group)
2388 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2389 go_intent, own_interface_addr, force_freq,
2394 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2395 struct wpa_scan_results *scan_res)
2397 struct wpa_bss *bss;
2400 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2402 if (wpa_s->global->p2p_disabled)
2405 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
2406 scan_res ? (int) scan_res->num : -1);
2409 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2411 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2412 wpa_s->pending_join_iface_addr);
2414 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2415 "from P2P peer table: %d MHz", freq);
2417 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2420 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2421 "from BSS table: %d MHz", freq);
2426 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2427 "prior to joining an existing group (GO " MACSTR
2429 MAC2STR(wpa_s->pending_join_dev_addr), freq);
2430 wpa_s->pending_pd_before_join = 1;
2432 switch (wpa_s->pending_join_wps_method) {
2434 case WPS_PIN_DISPLAY:
2435 method = WPS_CONFIG_KEYPAD;
2437 case WPS_PIN_KEYPAD:
2438 method = WPS_CONFIG_DISPLAY;
2441 method = WPS_CONFIG_PUSHBUTTON;
2448 if (p2p_prov_disc_req(wpa_s->global->p2p,
2449 wpa_s->pending_join_dev_addr, method, 1)
2451 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2452 "Discovery Request before joining an "
2454 wpa_s->pending_pd_before_join = 0;
2459 * Actual join operation will be started from the Action frame
2460 * TX status callback.
2465 wpa_printf(MSG_DEBUG, "P2P: Target BSS/GO not yet in BSS table - "
2466 "cannot send Provision Discovery Request");
2469 /* Start join operation immediately */
2470 wpas_p2p_join_start(wpa_s);
2474 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
2476 struct wpa_supplicant *wpa_s = eloop_ctx;
2478 struct wpa_driver_scan_params params;
2479 struct wpabuf *wps_ie, *ies;
2481 os_memset(¶ms, 0, sizeof(params));
2483 /* P2P Wildcard SSID */
2484 params.num_ssids = 1;
2485 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2486 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2488 wpa_s->wps->dev.p2p = 1;
2489 wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
2491 if (wps_ie == NULL) {
2492 wpas_p2p_scan_res_join(wpa_s, NULL);
2496 ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
2498 wpabuf_free(wps_ie);
2499 wpas_p2p_scan_res_join(wpa_s, NULL);
2502 wpabuf_put_buf(ies, wps_ie);
2503 wpabuf_free(wps_ie);
2505 p2p_scan_ie(wpa_s->global->p2p, ies);
2507 params.extra_ies = wpabuf_head(ies);
2508 params.extra_ies_len = wpabuf_len(ies);
2511 * Run a scan to update BSS table and start Provision Discovery once
2512 * the new scan results become available.
2514 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
2515 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
2516 ret = ieee80211_sta_req_scan(wpa_s, ¶ms);
2518 ret = wpa_drv_scan(wpa_s, ¶ms);
2523 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
2525 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2526 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2531 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2532 const u8 *dev_addr, enum p2p_wps_method wps_method)
2534 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2535 MACSTR " dev " MACSTR ")",
2536 MAC2STR(iface_addr), MAC2STR(dev_addr));
2538 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2539 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2540 wpa_s->pending_join_wps_method = wps_method;
2542 /* Make sure we are not running find during connection establishment */
2543 wpas_p2p_stop_find(wpa_s);
2545 wpas_p2p_join_scan(wpa_s, NULL);
2550 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2552 struct wpa_supplicant *group;
2553 struct p2p_go_neg_results res;
2555 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2558 if (group != wpa_s) {
2559 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2560 sizeof(group->p2p_pin));
2561 group->p2p_wps_method = wpa_s->p2p_wps_method;
2564 group->p2p_in_provisioning = 1;
2566 os_memset(&res, 0, sizeof(res));
2567 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2569 res.wps_method = wpa_s->pending_join_wps_method;
2570 wpas_start_wps_enrollee(group, &res);
2573 * Allow a longer timeout for join-a-running-group than normal 15
2574 * second group formation timeout since the GO may not have authorized
2575 * our connection yet.
2577 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2578 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
2586 * wpas_p2p_connect - Request P2P Group Formation to be started
2587 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2588 * @peer_addr: Address of the peer P2P Device
2589 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2590 * @persistent_group: Whether to create a persistent group
2591 * @join: Whether to join an existing group (as a client) instead of starting
2592 * Group Owner negotiation; @peer_addr is BSSID in that case
2593 * @auth: Whether to only authorize the connection instead of doing that and
2594 * initiating Group Owner negotiation
2595 * @go_intent: GO Intent or -1 to use default
2596 * @freq: Frequency for the group or 0 for auto-selection
2597 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2598 * failure, -2 on failure due to channel not currently available,
2599 * -3 if forced channel is not supported
2601 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2602 const char *pin, enum p2p_wps_method wps_method,
2603 int persistent_group, int join, int auth, int go_intent,
2606 int force_freq = 0, oper_freq = 0;
2609 enum wpa_driver_if_type iftype;
2612 go_intent = wpa_s->conf->p2p_go_intent;
2615 wpa_s->p2p_long_listen = 0;
2617 wpa_s->p2p_wps_method = wps_method;
2620 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
2621 else if (wps_method == WPS_PIN_DISPLAY) {
2622 ret = wps_generate_pin();
2623 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
2625 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
2628 wpa_s->p2p_pin[0] = '\0';
2631 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
2633 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
2634 "connect a running group from " MACSTR,
2635 MAC2STR(peer_addr));
2636 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
2639 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
2640 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
2642 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
2643 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
2646 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) <
2652 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2654 oper_freq = wpa_s->assoc_freq;
2656 oper_freq = wpa_drv_shared_freq(wpa_s);
2662 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
2663 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
2664 "(%u MHz) is not supported for P2P uses",
2669 if (oper_freq > 0 && freq != oper_freq &&
2670 !(wpa_s->drv_flags &
2671 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2672 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2673 "on %u MHz while connected on another "
2674 "channel (%u MHz)", freq, oper_freq);
2677 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2678 "requested channel (%u MHz)", freq);
2680 } else if (oper_freq > 0 &&
2681 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
2682 if (!(wpa_s->drv_flags &
2683 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2684 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2685 "while connected on non-P2P supported "
2686 "channel (%u MHz)", oper_freq);
2689 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
2690 "(%u MHz) not available for P2P - try to use "
2691 "another channel", oper_freq);
2693 } else if (oper_freq > 0) {
2694 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2695 "channel we are already using (%u MHz) on another "
2696 "interface", oper_freq);
2697 force_freq = oper_freq;
2700 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
2702 if (!wpa_s->create_p2p_iface) {
2704 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2705 go_intent, wpa_s->own_addr,
2706 force_freq, persistent_group)
2711 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
2712 go_intent, wpa_s->own_addr,
2713 force_freq, persistent_group) < 0)
2718 /* Prepare to add a new interface for the group */
2719 iftype = WPA_IF_P2P_GROUP;
2721 iftype = WPA_IF_P2P_CLIENT;
2722 else if (go_intent == 15)
2723 iftype = WPA_IF_P2P_GO;
2724 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
2725 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2726 "interface for the group");
2731 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2733 wpa_s->pending_interface_addr,
2734 force_freq, persistent_group) < 0)
2738 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
2739 wpa_s->pending_interface_addr,
2740 force_freq, persistent_group) < 0) {
2741 wpas_p2p_remove_pending_group_interface(wpa_s);
2749 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2750 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2751 * @freq: Frequency of the channel in MHz
2752 * @duration: Duration of the stay on the channel in milliseconds
2754 * This callback is called when the driver indicates that it has started the
2755 * requested remain-on-channel duration.
2757 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2758 unsigned int freq, unsigned int duration)
2760 wpa_s->roc_waiting_drv_freq = 0;
2761 wpa_s->off_channel_freq = freq;
2762 wpas_send_action_cb(wpa_s, NULL);
2763 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
2764 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
2765 wpa_s->pending_listen_duration);
2766 wpa_s->pending_listen_freq = 0;
2771 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
2772 unsigned int timeout)
2774 /* Limit maximum Listen state time based on driver limitation. */
2775 if (timeout > wpa_s->max_remain_on_chan)
2776 timeout = wpa_s->max_remain_on_chan;
2778 return p2p_listen(wpa_s->global->p2p, timeout);
2783 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
2784 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2785 * @freq: Frequency of the channel in MHz
2787 * This callback is called when the driver indicates that a remain-on-channel
2788 * operation has been completed, i.e., the duration on the requested channel
2791 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2794 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
2795 "(p2p_long_listen=%d pending_action_tx=%p)",
2796 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
2797 wpa_s->off_channel_freq = 0;
2798 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
2799 return; /* P2P module started a new operation */
2800 if (wpa_s->pending_action_tx)
2802 if (wpa_s->p2p_long_listen > 0)
2803 wpa_s->p2p_long_listen -= 5;
2804 if (wpa_s->p2p_long_listen > 0) {
2805 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
2806 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen * 1000);
2812 * wpas_p2p_group_remove - Remove a P2P group
2813 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2814 * @ifname: Network interface name of the group interface or "*" to remove all
2816 * Returns: 0 on success, -1 on failure
2818 * This function is used to remove a P2P group. This can be used to disconnect
2819 * from a group in which the local end is a P2P Client or to end a P2P Group in
2820 * case the local end is the Group Owner. If a virtual network interface was
2821 * created for this group, that interface will be removed. Otherwise, only the
2822 * configured P2P group network will be removed from the interface.
2824 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
2826 struct wpa_global *global = wpa_s->global;
2828 if (os_strcmp(ifname, "*") == 0) {
2829 struct wpa_supplicant *prev;
2830 wpa_s = global->ifaces;
2833 wpa_s = wpa_s->next;
2834 prev->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
2835 wpas_p2p_group_delete(prev);
2840 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2841 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2848 wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
2849 wpas_p2p_group_delete(wpa_s);
2855 static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
2856 struct p2p_go_neg_results *params,
2862 os_memset(params, 0, sizeof(*params));
2863 params->role_go = 1;
2864 params->freq = 2412;
2866 params->freq = freq;
2867 else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
2868 wpa_s->conf->p2p_oper_channel >= 1 &&
2869 wpa_s->conf->p2p_oper_channel <= 11)
2870 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
2871 else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
2872 wpa_s->conf->p2p_oper_reg_class == 118)
2873 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
2874 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2875 wpa_s->assoc_freq && !freq) {
2876 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2878 params->freq = wpa_s->assoc_freq;
2881 res = wpa_drv_shared_freq(wpa_s);
2882 if (res > 0 && !freq) {
2883 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2884 "already using on a shared interface");
2890 static struct wpa_supplicant *
2891 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
2894 struct wpa_supplicant *group_wpa_s;
2896 if (!wpas_p2p_create_iface(wpa_s))
2899 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2900 WPA_IF_P2P_CLIENT) < 0)
2902 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
2903 if (group_wpa_s == NULL) {
2904 wpas_p2p_remove_pending_group_interface(wpa_s);
2913 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
2914 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2915 * @persistent_group: Whether to create a persistent group
2916 * @freq: Frequency for the group or 0 to indicate no hardcoding
2917 * Returns: 0 on success, -1 on failure
2919 * This function creates a new P2P group with the local end as the Group Owner,
2920 * i.e., without using Group Owner Negotiation.
2922 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
2925 struct p2p_go_neg_results params;
2927 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
2928 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
2929 "(%u MHz) is not supported for P2P uses",
2934 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
2935 p2p_go_params(wpa_s->global->p2p, ¶ms);
2936 params.persistent_group = persistent_group;
2938 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
2941 wpas_start_wps_go(wpa_s, ¶ms, 0);
2947 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
2948 struct wpa_ssid *params, int addr_allocated)
2950 struct wpa_ssid *ssid;
2952 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
2956 wpa_supplicant_ap_deinit(wpa_s);
2958 ssid = wpa_config_add_network(wpa_s->conf);
2961 wpas_notify_network_added(wpa_s, ssid);
2962 wpa_config_set_network_defaults(ssid);
2963 ssid->temporary = 1;
2964 ssid->proto = WPA_PROTO_RSN;
2965 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
2966 ssid->group_cipher = WPA_CIPHER_CCMP;
2967 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
2968 ssid->ssid = os_malloc(params->ssid_len);
2969 if (ssid->ssid == NULL) {
2970 wpas_notify_network_removed(wpa_s, ssid);
2971 wpa_config_remove_network(wpa_s->conf, ssid->id);
2974 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2975 ssid->ssid_len = params->ssid_len;
2976 ssid->p2p_group = 1;
2977 if (params->psk_set) {
2978 os_memcpy(ssid->psk, params->psk, 32);
2981 if (params->passphrase)
2982 ssid->passphrase = os_strdup(params->passphrase);
2984 wpa_supplicant_select_network(wpa_s, ssid);
2986 wpa_s->show_group_started = 1;
2992 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
2993 struct wpa_ssid *ssid, int addr_allocated,
2996 struct p2p_go_neg_results params;
2998 if (ssid->disabled != 2 || ssid->ssid == NULL)
3001 wpa_s->p2p_long_listen = 0;
3002 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3004 if (ssid->mode == WPAS_MODE_INFRA)
3005 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3007 if (ssid->mode != WPAS_MODE_P2P_GO)
3010 wpas_p2p_init_go_params(wpa_s, ¶ms, freq);
3013 if (ssid->passphrase == NULL ||
3014 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3015 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3019 os_strlcpy(params.passphrase, ssid->passphrase,
3020 sizeof(params.passphrase));
3021 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3022 params.ssid_len = ssid->ssid_len;
3023 params.persistent_group = 1;
3025 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3029 wpas_start_wps_go(wpa_s, ¶ms, 0);
3035 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3036 struct wpabuf *proberesp_ies)
3038 struct wpa_supplicant *wpa_s = ctx;
3039 if (wpa_s->ap_iface) {
3040 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
3042 wpabuf_free(hapd->p2p_beacon_ie);
3043 hapd->p2p_beacon_ie = beacon_ies;
3045 wpabuf_free(hapd->p2p_probe_resp_ie);
3046 hapd->p2p_probe_resp_ie = proberesp_ies;
3048 wpabuf_free(beacon_ies);
3049 wpabuf_free(proberesp_ies);
3051 wpa_supplicant_ap_update_beacon(wpa_s);
3055 static void wpas_p2p_idle_update(void *ctx, int idle)
3057 struct wpa_supplicant *wpa_s = ctx;
3058 if (!wpa_s->ap_iface)
3060 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3062 wpas_p2p_set_group_idle_timeout(wpa_s);
3064 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3068 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
3069 int persistent_group,
3070 int group_formation)
3072 struct p2p_group *group;
3073 struct p2p_group_config *cfg;
3075 cfg = os_zalloc(sizeof(*cfg));
3079 cfg->persistent_group = persistent_group;
3080 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3081 if (wpa_s->max_stations &&
3082 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3083 cfg->max_clients = wpa_s->max_stations;
3085 cfg->max_clients = wpa_s->conf->max_num_sta;
3086 cfg->cb_ctx = wpa_s;
3087 cfg->ie_update = wpas_p2p_ie_update;
3088 cfg->idle_update = wpas_p2p_idle_update;
3090 group = p2p_group_init(wpa_s->global->p2p, cfg);
3093 if (!group_formation)
3094 p2p_group_notif_formation_done(group);
3095 wpa_s->p2p_group = group;
3100 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3103 if (!wpa_s->p2p_in_provisioning) {
3104 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3105 "provisioning not in progress");
3109 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3111 if (wpa_s->global->p2p)
3112 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3113 wpas_group_formation_completed(wpa_s, 1);
3117 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3118 const char *config_method)
3122 if (os_strcmp(config_method, "display") == 0)
3123 config_methods = WPS_CONFIG_DISPLAY;
3124 else if (os_strcmp(config_method, "keypad") == 0)
3125 config_methods = WPS_CONFIG_KEYPAD;
3126 else if (os_strcmp(config_method, "pbc") == 0 ||
3127 os_strcmp(config_method, "pushbutton") == 0)
3128 config_methods = WPS_CONFIG_PUSHBUTTON;
3132 if (wpa_s->global->p2p == NULL)
3135 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
3140 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3143 return p2p_scan_result_text(ies, ies_len, buf, end);
3147 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3149 if (!wpa_s->pending_action_tx)
3152 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3153 "operation request");
3154 wpabuf_free(wpa_s->pending_action_tx);
3155 wpa_s->pending_action_tx = NULL;
3159 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3160 enum p2p_discovery_type type)
3162 wpas_p2p_clear_pending_action_tx(wpa_s);
3163 wpa_s->p2p_long_listen = 0;
3165 return p2p_find(wpa_s->global->p2p, timeout, type);
3169 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3171 wpas_p2p_clear_pending_action_tx(wpa_s);
3172 wpa_s->p2p_long_listen = 0;
3173 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3175 p2p_stop_find(wpa_s->global->p2p);
3177 wpas_p2p_remove_pending_group_interface(wpa_s);
3181 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3183 struct wpa_supplicant *wpa_s = eloop_ctx;
3184 wpa_s->p2p_long_listen = 0;
3188 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3192 wpas_p2p_clear_pending_action_tx(wpa_s);
3196 * This is a request for unlimited Listen state. However, at
3197 * least for now, this is mapped to a Listen state for one
3202 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3203 wpa_s->p2p_long_listen = 0;
3205 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
3206 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
3207 wpa_s->p2p_long_listen = timeout;
3208 eloop_register_timeout(timeout, 0,
3209 wpas_p2p_long_listen_timeout,
3217 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3218 u8 *buf, size_t len, int p2p_group)
3220 struct wpabuf *p2p_ie;
3223 if (wpa_s->global->p2p_disabled)
3225 if (wpa_s->global->p2p == NULL)
3230 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
3231 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
3233 wpabuf_free(p2p_ie);
3239 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
3240 const u8 *ie, size_t ie_len)
3242 if (wpa_s->global->p2p_disabled)
3244 if (wpa_s->global->p2p == NULL)
3247 return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len);
3251 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
3252 const u8 *sa, const u8 *bssid,
3253 u8 category, const u8 *data, size_t len, int freq)
3255 if (wpa_s->global->p2p_disabled)
3257 if (wpa_s->global->p2p == NULL)
3260 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
3265 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
3267 if (wpa_s->global->p2p_disabled)
3269 if (wpa_s->global->p2p == NULL)
3272 p2p_scan_ie(wpa_s->global->p2p, ies);
3276 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
3278 p2p_group_deinit(wpa_s->p2p_group);
3279 wpa_s->p2p_group = NULL;
3283 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
3285 wpa_s->p2p_long_listen = 0;
3287 return p2p_reject(wpa_s->global->p2p, addr);
3291 /* Invite to reinvoke a persistent group */
3292 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3293 struct wpa_ssid *ssid, const u8 *go_dev_addr)
3295 enum p2p_invite_role role;
3298 if (ssid->mode == WPAS_MODE_P2P_GO) {
3299 role = P2P_INVITE_ROLE_GO;
3300 if (peer_addr == NULL) {
3301 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
3302 "address in invitation command");
3305 if (wpas_p2p_create_iface(wpa_s)) {
3306 if (wpas_p2p_add_group_interface(wpa_s,
3307 WPA_IF_P2P_GO) < 0) {
3308 wpa_printf(MSG_ERROR, "P2P: Failed to "
3309 "allocate a new interface for the "
3313 bssid = wpa_s->pending_interface_addr;
3315 bssid = wpa_s->own_addr;
3317 role = P2P_INVITE_ROLE_CLIENT;
3318 peer_addr = ssid->bssid;
3320 wpa_s->pending_invite_ssid_id = ssid->id;
3322 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3323 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
3327 /* Invite to join an active group */
3328 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
3329 const u8 *peer_addr, const u8 *go_dev_addr)
3331 struct wpa_global *global = wpa_s->global;
3332 enum p2p_invite_role role;
3334 struct wpa_ssid *ssid;
3336 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3337 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3340 if (wpa_s == NULL) {
3341 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
3345 ssid = wpa_s->current_ssid;
3347 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
3352 if (ssid->mode == WPAS_MODE_P2P_GO) {
3353 role = P2P_INVITE_ROLE_ACTIVE_GO;
3354 bssid = wpa_s->own_addr;
3355 if (go_dev_addr == NULL)
3356 go_dev_addr = wpa_s->parent->own_addr;
3358 role = P2P_INVITE_ROLE_CLIENT;
3359 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
3360 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
3361 "invite to current group");
3364 bssid = wpa_s->bssid;
3365 if (go_dev_addr == NULL &&
3366 !is_zero_ether_addr(wpa_s->go_dev_addr))
3367 go_dev_addr = wpa_s->go_dev_addr;
3369 wpa_s->pending_invite_ssid_id = -1;
3371 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3372 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
3377 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
3379 struct wpa_ssid *ssid = wpa_s->current_ssid;
3380 const char *ssid_txt;
3381 u8 go_dev_addr[ETH_ALEN];
3384 if (!wpa_s->show_group_started || !ssid)
3387 wpa_s->show_group_started = 0;
3389 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
3390 os_memset(go_dev_addr, 0, ETH_ALEN);
3391 if (ssid->bssid_set)
3392 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
3393 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3395 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
3397 if (wpa_s->global->p2p_group_formation == wpa_s)
3398 wpa_s->global->p2p_group_formation = NULL;
3400 if (ssid->passphrase == NULL && ssid->psk_set) {
3402 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
3403 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3404 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3406 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
3407 MAC2STR(go_dev_addr),
3408 persistent ? " [PERSISTENT]" : "");
3410 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3411 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3412 "go_dev_addr=" MACSTR "%s",
3413 wpa_s->ifname, ssid_txt, ssid->frequency,
3414 ssid->passphrase ? ssid->passphrase : "",
3415 MAC2STR(go_dev_addr),
3416 persistent ? " [PERSISTENT]" : "");
3420 wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
3425 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
3426 u32 interval1, u32 duration2, u32 interval2)
3428 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3429 wpa_s->current_ssid == NULL ||
3430 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
3433 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
3434 wpa_s->own_addr, wpa_s->assoc_freq,
3435 duration1, interval1, duration2, interval2);
3439 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
3440 unsigned int interval)
3442 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
3446 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
3448 struct wpa_supplicant *wpa_s = eloop_ctx;
3450 if (wpa_s->conf->p2p_group_idle == 0) {
3451 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
3456 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
3458 wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
3459 wpas_p2p_group_delete(wpa_s);
3463 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
3465 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3466 if (wpa_s->conf->p2p_group_idle == 0)
3469 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
3472 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
3473 wpa_s->conf->p2p_group_idle);
3474 eloop_register_timeout(wpa_s->conf->p2p_group_idle, 0,
3475 wpas_p2p_group_idle_timeout, wpa_s, NULL);
3479 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3480 u16 reason_code, const u8 *ie, size_t ie_len)
3482 if (wpa_s->global->p2p_disabled)
3485 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3489 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3490 u16 reason_code, const u8 *ie, size_t ie_len)
3492 if (wpa_s->global->p2p_disabled)
3495 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3499 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
3501 struct p2p_data *p2p = wpa_s->global->p2p;
3506 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3509 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
3510 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
3512 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
3514 if (wpa_s->conf->device_type) {
3515 if (wps_dev_type_str2bin(wpa_s->conf->device_type,
3516 pri_dev_type) < 0) {
3517 wpa_printf(MSG_ERROR, "P2P: Invalid "
3520 p2p_set_pri_dev_type(p2p, pri_dev_type);
3524 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
3525 u8 sec_dev_type[P2P_SEC_DEVICE_TYPES][8];
3528 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
3529 if (wpa_s->conf->sec_device_type[i] == NULL)
3531 if (wps_dev_type_str2bin(
3532 wpa_s->conf->sec_device_type[i],
3533 sec_dev_type[num]) < 0) {
3534 wpa_printf(MSG_ERROR, "P2P: Invalid "
3539 if (num == P2P_SEC_DEVICE_TYPES)
3542 p2p_set_sec_dev_types(p2p, (void *) sec_dev_type, num);
3545 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
3546 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3548 country[0] = wpa_s->conf->country[0];
3549 country[1] = wpa_s->conf->country[1];
3551 p2p_set_country(p2p, country);
3554 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
3555 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
3556 wpa_s->conf->p2p_ssid_postfix ?
3557 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
3561 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
3562 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
3566 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
3569 if (!wpa_s->ap_iface)
3571 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
3576 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
3578 if (wpa_s->global->p2p_disabled)
3580 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3583 wpa_s->global->cross_connection = enabled;
3584 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
3587 struct wpa_supplicant *iface;
3589 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
3591 if (iface->cross_connect_enabled == 0)
3594 iface->cross_connect_enabled = 0;
3595 iface->cross_connect_in_use = 0;
3596 wpa_msg(iface->parent, MSG_INFO,
3597 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
3598 iface->ifname, iface->cross_connect_uplink);
3606 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
3608 struct wpa_supplicant *iface;
3610 if (!uplink->global->cross_connection)
3613 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
3614 if (!iface->cross_connect_enabled)
3616 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
3619 if (iface->ap_iface == NULL)
3621 if (iface->cross_connect_in_use)
3624 iface->cross_connect_in_use = 1;
3625 wpa_msg(iface->parent, MSG_INFO,
3626 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
3627 iface->ifname, iface->cross_connect_uplink);
3632 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
3634 struct wpa_supplicant *iface;
3636 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
3637 if (!iface->cross_connect_enabled)
3639 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
3642 if (!iface->cross_connect_in_use)
3645 wpa_msg(iface->parent, MSG_INFO,
3646 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
3647 iface->ifname, iface->cross_connect_uplink);
3648 iface->cross_connect_in_use = 0;
3653 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
3655 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
3656 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
3657 wpa_s->cross_connect_disallowed)
3658 wpas_p2p_disable_cross_connect(wpa_s);
3660 wpas_p2p_enable_cross_connect(wpa_s);
3661 if (!wpa_s->ap_iface)
3662 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3666 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
3668 wpas_p2p_disable_cross_connect(wpa_s);
3669 if (!wpa_s->ap_iface)
3670 wpas_p2p_set_group_idle_timeout(wpa_s);
3674 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
3676 struct wpa_supplicant *iface;
3678 if (!wpa_s->global->cross_connection)
3681 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3684 if (iface->drv_flags &
3685 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
3687 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
3690 wpa_s->cross_connect_enabled = 1;
3691 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
3692 sizeof(wpa_s->cross_connect_uplink));
3693 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
3694 "%s to %s whenever uplink is available",
3695 wpa_s->ifname, wpa_s->cross_connect_uplink);
3697 if (iface->ap_iface || iface->current_ssid == NULL ||
3698 iface->current_ssid->mode != WPAS_MODE_INFRA ||
3699 iface->cross_connect_disallowed ||
3700 iface->wpa_state != WPA_COMPLETED)
3703 wpa_s->cross_connect_in_use = 1;
3704 wpa_msg(wpa_s->parent, MSG_INFO,
3705 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
3706 wpa_s->ifname, wpa_s->cross_connect_uplink);
3712 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
3714 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
3715 !wpa_s->p2p_in_provisioning)
3716 return 0; /* not P2P client operation */
3718 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
3720 wpas_group_formation_completed(wpa_s, 0);
3725 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
3727 struct p2p_channels chan;
3729 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
3732 os_memset(&chan, 0, sizeof(chan));
3733 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
3734 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
3739 p2p_update_channel_list(wpa_s->global->p2p, &chan);
3743 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
3745 struct wpa_global *global = wpa_s->global;
3748 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
3750 if (wpa_s->pending_interface_name[0] &&
3751 !is_zero_ether_addr(wpa_s->pending_interface_addr))
3754 wpas_p2p_stop_find(wpa_s);
3756 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3757 if (wpa_s == global->p2p_group_formation &&
3758 (wpa_s->p2p_in_provisioning ||
3759 wpa_s->parent->pending_interface_type ==
3760 WPA_IF_P2P_CLIENT)) {
3761 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
3762 "formation found - cancelling",
3765 wpas_p2p_group_delete(wpa_s);
3771 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
3779 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
3781 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
3784 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
3785 "being available anymore");
3786 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
3787 wpas_p2p_group_delete(wpa_s);